forked from hfiguiere/exifprobe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
readfile.c
823 lines (755 loc) · 26.4 KB
/
readfile.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- */
/* EXIFPROBE - TIFF/JPEG/EXIF image file probe */
/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- */
/* Copyright (C) 2002,2005 by Duane H. Hesser. All rights reserved. */
/* */
/* See the file LICENSE.EXIFPROBE for terms of use. */
/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- */
#ifndef lint
static char *ModuleId = "@(#) $Id: readfile.c,v 1.30 2005/07/24 18:15:28 alex Exp $";
#endif
/* This file contains routines for reading specific value types with */
/* byte-swapping for integer values, specialized routines for */
/* strings, a file header decoder for various imagefile types, and a */
/* general IFD entry reader. */
/* The code needs to know the native byteorder of the machine, which */
/* is currently not autoconfigured. If not sure, look for a file like */
/* <sys/endian.h>. "MOTOROLA" byteorder is "big-endian", "INTEL" */
/* byteorder is "little-endian". The code does not currently handle */
/* "pdp11" byteorder. */
#include <stdio.h>
#include <stdint.h>
#include <sys/types.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include "byteorder.h"
#include "defs.h"
#include "datadefs.h"
#include "summary.h"
#include "misc.h"
#include "tags.h"
#include "extern.h"
#include "ciff.h"
#include "x3f.h"
/* Read a short integer value in the expectation that it is a TIFF or */
/* JPEG header marker/tag. If a valid JPEG_SOI is found, we're done. */
/* If the value is a TIFF indicator, but the TIFF magic marker is not */
/* found, the magic number is then checked for a valid ORF or CIFF */
/* header. If the marker is not a recognized magic number/string, */
/* start over and check for a JP2 header box, or MRW, RAF, or X3F */
/* header. */
/* When appropriage, ciff, mrw, x3f or jp2 header structures are */
/* filled in and the fileheader magic set to indictate which is */
/* valid. */
/* The return is a fileheader structure containing an exifprobe */
/* private "magic number" and appropriate information about the file */
/* structure, if any. */
struct fileheader *
read_imageheader(FILE *inptr,unsigned long offset)
{
static struct fileheader fileheader;
struct ciff_header *ciffheader;
struct jp2_header jp2header;
struct mrw_header mrwheader;
struct x3f_header *x3fheader;
unsigned short marker,magic;
unsigned char *type;
unsigned long umagic,length;
char *string;
marker = 0;
fileheader.probe_magic = PROBE_NOMAGIC;
fileheader.file_marker = 0;
fileheader.ciff_header = NULL;
fileheader.x3f_header = NULL;
memset(&fileheader.jp2_header,'\0',sizeof(struct jp2_header));
memset(&fileheader.mrw_header,'\0',sizeof(struct mrw_header));
if(inptr && !feof(inptr) && !ferror(inptr))
{
/* The order of bytes for this read does not matter for TIFF, */
/* but must be high-byte first for JPEG. */
marker = read_ushort(inptr,TIFF_MOTOROLA,offset);
switch(marker)
{
case TIFF_MOTOROLA:
case TIFF_INTEL:
if((magic = read_ushort(inptr,marker,HERE)) == TIFF_MAGIC)
{
fileheader.probe_magic = TIFF_MAGIC;
fileheader.file_marker = marker;
}
else if(magic == ORF1_MAGIC)
{
fileheader.probe_magic = ORF1_MAGIC;
fileheader.file_marker = marker;
}
else if(magic == ORF2_MAGIC)
{
fileheader.probe_magic = ORF2_MAGIC;
fileheader.file_marker = marker;
}
else if(magic == RW2_MAGIC)
{
fileheader.probe_magic = RW2_MAGIC;
fileheader.file_marker = marker;
}
else if((ciffheader = read_ciffheader(inptr,marker,offset)))
{
fileheader.file_marker = ciffheader->byteorder;
fileheader.probe_magic = PROBE_CIFFMAGIC;
fileheader.ciff_header = ciffheader;
}
else
{
fileheader.probe_magic = PROBE_NOMAGIC;
fileheader.file_marker = 0;
printf(" invalid TIFF magic (%#06x)",magic);
printf(", apparent TIFF byteorder is ");
print_byteorder(marker,1);
(void)newline(0);
}
break;
case JPEG_SOI:
fileheader.probe_magic = JPEG_SOI;
fileheader.file_marker = marker;
break;
default:
fileheader.probe_magic = PROBE_NOMAGIC;
fileheader.file_marker = 0;
if(marker == 0)
{
/* check for jpeg2000 */
length = read_ulong(inptr,TIFF_MOTOROLA,offset);
if(length == 12)
{
type = read_bytes(inptr,4,HERE);
if(type && ((strncmp((char *)type,"jP\040\040 ",4) == 0) ||
(strncmp((char *)type,"jP\032\032 ",4) == 0)))
{
umagic = read_ulong(inptr,TIFF_MOTOROLA,HERE);
if(umagic == 0x0d0a870a)
{
fileheader.probe_magic = PROBE_JP2MAGIC;
fileheader.file_marker = 0;
jp2header.length = length;
jp2header.type = to_ulong(type,TIFF_MOTOROLA);
if(!ferror(inptr) && !feof(inptr))
{
jp2header.magic = umagic;
fileheader.jp2_header = jp2header;
}
}
}
}
}
else if(marker == 0x004d)
{
umagic = read_ulong(inptr,TIFF_MOTOROLA,offset);
if(umagic == 0x004d524d)
{
/* Minolta MRW */
mrwheader.mrw_magic = umagic;
mrwheader.mrw_dataoffset = read_ulong(inptr,TIFF_MOTOROLA,HERE);
if(!ferror(inptr) && !feof(inptr))
{
fileheader.probe_magic = PROBE_MRWMAGIC;
fileheader.mrw_header = mrwheader;
}
}
}
else if((marker == 0x5546) || (marker == 0x4655))
{
/* Fujifile RAF */
string = read_string(inptr,offset,16);
if(string && (strncmp(string,"FUJIFILMCCD-RAW ",16) == 0))
fileheader.probe_magic = PROBE_RAFMAGIC;
}
else if(marker == 0x464f) /* "FO" */
{
/* Sigma/Foveon X3F */
umagic = read_ulong(inptr,TIFF_INTEL,offset);
if(umagic == PROBE_X3FMAGIC)
{
x3fheader = read_x3fheader(inptr,offset);
if(x3fheader)
{ fileheader.x3f_header = x3fheader;
fileheader.probe_magic = PROBE_X3FMAGIC;
}
}
}
else
{
/* Unknown format */
fileheader.probe_magic = PROBE_NOMAGIC;
fileheader.file_marker = 0;
fileheader.ciff_header = NULL;
fileheader.x3f_header = NULL;
}
break;
}
}
return(&fileheader);
}
/* Read a generic TIFF IFD entry at the given offset, swapping */
/* byteorder of entry values as required. */
/* The return is a pointer to a static structure representing the */
/* entry. */
struct ifd_entry *
read_ifd_entry(FILE *inptr,unsigned short byteorder,unsigned long offset)
{
static struct ifd_entry entry;
unsigned char *value;
memset(&entry,0,sizeof(struct ifd_entry));
if(inptr && !feof(inptr) && !ferror(inptr))
{
entry.tag = read_ushort(inptr,byteorder,offset);
entry.value_type = read_ushort(inptr,byteorder,HERE);
entry.count = read_ulong(inptr,byteorder,HERE);
value = read_bytes(inptr,4,HERE);
if(value)
{
if((value_type_size(entry.value_type) * entry.count) > 4)
entry.value = to_ulong(value,byteorder); /* offset */
else /* actual value(s) */
{
entry.value = 0L;
switch(entry.value_type)
{
case SSHORT:
case SHORT:
if(entry.count == 1)
entry.value = to_ushort(value,byteorder);
else
{
entry.value = to_ushort(value,byteorder);
if(byteorder == TIFF_MOTOROLA)
entry.value |= (((unsigned long)to_ushort(value + 2,byteorder) & 0xffff) << 16);
else
entry.value = (to_ushort(value + 2,byteorder) & 0xffff) |
((entry.value & 0xffff) << 16);
}
break;
case BYTE:
case SBYTE:
case UNDEFINED:
case ASCII:
entry.value = *(unsigned long *)value;
break;
case LONG:
case SLONG:
default:
entry.value = to_ulong(value,byteorder);
break;
}
}
}
else
entry.value = 0L;
}
return(&entry);
}
/* Read 2 bytes at the given offset and interpret them as an unsigned */
/* short integer value, swapping bytes as necessary. */
unsigned short
read_ushort(FILE *inptr,unsigned short byteorder,unsigned long offset)
{
unsigned char rbuf[2];
unsigned long curoffset;
unsigned short value = 0;
int chpr = 0;
if(inptr)
{
clearerr(inptr);
curoffset = ftell(inptr);
if((offset != HERE) && (fseek(inptr,offset,SEEK_SET) == -1))
{
printred(" SEEK FAILED to read unsigned short at offset ");
printf("%lu",offset);
}
else if(fread(rbuf,1,sizeof(unsigned short),inptr) == sizeof(unsigned short))
value = to_ushort(rbuf,byteorder);
else
{
printred(" FAILED to read unsigned short value at offset ");
if(offset == HERE)
offset = curoffset;
chpr += printf("%lu",offset);
}
if(ferror(inptr))
{
chpr += printf(" (");
setcharsprinted(chpr);
chpr = 0;
printred(strerror(errno));
chpr += printf(")");
chpr = newline(chpr);
}
else if(feof(inptr))
{
printred(" (EOF)");
chpr = newline(chpr);
}
}
setcharsprinted(chpr);
return(value);
}
/* read a single byte at the given offset and return it as an */
/* unsigned short value */
unsigned short
read_ubyte(FILE *inptr,unsigned long offset)
{
unsigned char rbuf[1];
unsigned long curoffset;
unsigned short value = 0;
int chpr = 0;
if(inptr)
{
clearerr(inptr);
curoffset = ftell(inptr);
if((offset != HERE) && (fseek(inptr,offset,SEEK_SET) == -1))
{
printred(" SEEK FAILED to read unsigned byte at offset ");
chpr += printf("%lu",offset);
}
else if(fread(rbuf,1,1,inptr) == 1)
value = rbuf[0] & 0xff;
else
{
if(offset == HERE)
offset = curoffset;
printred(" FAILED to read unsigned byte at offset ");
chpr += printf("%lu",offset);
}
if(ferror(inptr))
{
chpr += printf(" (");
setcharsprinted(chpr);
chpr = 0;
printred(strerror(errno));
chpr += printf(")");
chpr = newline(chpr);
}
else if(feof(inptr))
{
printred(" (EOF)");
chpr = newline(chpr);
}
}
setcharsprinted(chpr);
return(value);
}
/* Read 4 bytes at the given offset and interpret as an unsigned long */
/* integer, swapping bytes as necessary */
/* NOTE: the routine *reads* a 4 byte number, but returns it as an */
/* unsigned long in the native machine size. */
unsigned long
read_ulong(FILE *inptr,unsigned short byteorder,unsigned long offset)
{
unsigned char rbuf[4];
unsigned long curoffset;
unsigned long value = 0;
int chpr = 0;
if(inptr)
{
clearerr(inptr);
curoffset = ftell(inptr);
if((offset != HERE) && (fseek(inptr,offset,SEEK_SET) == -1))
{
printred(" SEEK FAILED to read unsigned 32bit integer at offset ");
printf("%lu",offset);
}
else if(fread(rbuf,1,4,inptr) == 4)
value = to_ulong(rbuf,byteorder);
else
{
printred(" FAILED to read unsigned 32bit integer at offset ");
if(offset == HERE)
offset = curoffset;
chpr += printf("%lu",offset);
}
if(ferror(inptr))
{
chpr += printf(" (");
setcharsprinted(chpr);
chpr = 0;
printred(strerror(errno));
chpr += printf(")");
chpr = newline(chpr);
}
else if(feof(inptr))
{
printred(" (EOF)");
chpr = newline(chpr);
}
}
setcharsprinted(chpr);
return(value);
}
float
read_float(FILE *inptr,unsigned short byteorder,unsigned long offset)
{
float value = 0.0;
unsigned long ulval;
ulval = read_ulong(inptr,byteorder,offset);
value = to_float(ulval);
return(value);
}
/* Read 8 bytes at the given offset and interpret as a long long, */
/* swapping bytes as necessary. */
unsigned long long
read_ulong64(FILE *inptr,unsigned short byteorder,unsigned long offset)
{
unsigned char rbuf[sizeof(double)];
unsigned long curoffset;
unsigned long long value = 0;
int chpr = 0;
if(inptr)
{
clearerr(inptr);
curoffset = ftell(inptr);
if((offset != HERE) && (fseek(inptr,offset,SEEK_SET) == -1))
chpr += printf(" seek failed to offset %lu to read unsigned long\n",
offset);
else if(fread(rbuf,1,sizeof(long long),inptr) == sizeof(long long))
value = to_ulong64(rbuf,byteorder);
else
{
printred(" FAILED to read unsigned 64bit integer at offset ");
if(offset == HERE)
offset = curoffset;
chpr += printf("%lu\n",offset);
}
}
setcharsprinted(chpr);
return(value);
}
unsigned long long
to_ulong64(unsigned char *buf,unsigned short byteorder)
{
unsigned long long value = 0;
unsigned char swap[sizeof(long long)];
unsigned char *p;
int i,size;
if(buf)
{
p = buf;
#ifdef NATIVE_BYTEORDER_BIGENDIAN
if(byteorder == TIFF_INTEL)
#else
if(byteorder == TIFF_MOTOROLA)
#endif
{
size = sizeof(long long);
for(i = 0; i < size; ++i)
swap[i] = buf[size - 1 - i];
p = swap;
}
value = *(long long *)p;
}
return(value);
}
/* Read 8 bytes at the given offset and interpret as a double, */
/* swapping bytes as necessary. */
double
read_double(FILE *inptr,unsigned short byteorder,unsigned long offset)
{
unsigned char rbuf[sizeof(double)];
unsigned long curoffset;
double value = 0.0;
int chpr = 0;
if(inptr)
{
clearerr(inptr);
curoffset = ftell(inptr);
if((offset != HERE) && (fseek(inptr,offset,SEEK_SET) == -1))
chpr += printf(" seek failed to offset %lu to read unsigned long\n",
offset);
else if(fread(rbuf,1,sizeof(double),inptr) == sizeof(double))
value = to_double(rbuf,byteorder);
else
{
if(offset == HERE)
offset = curoffset;
PUSHCOLOR(RED);
chpr += printf(" failed to read double value at offset %lu\n",offset);
POPCOLOR();
}
}
setcharsprinted(chpr);
return(value);
}
/* Read 'nbytes' of bytes at the given offset; no byte swapping. */
/* The result is a chunk of bytes...not a string. */
unsigned char *
read_bytes(FILE *inptr,unsigned long nbytes,unsigned long offset)
{
static unsigned char rbuf[MAXBUFLEN];
unsigned long curoffset;
unsigned char *value = NULL;
int chpr = 0;
memset(rbuf,0,MAXBUFLEN);
if(inptr)
{
if(nbytes > MAXBUFLEN)
{
chpr += printf(" read %lu bytes would overrun buffer; truncating to %d\n",
nbytes,MAXBUFLEN);
nbytes = MAXBUFLEN;
}
if(nbytes)
{
if(inptr) /* ###%%% redundant; FIXME */
{
clearerr(inptr);
curoffset = ftell(inptr);
if((offset != HERE) && (fseek(inptr,offset,SEEK_SET) == -1))
{
printred(" SEEK FAILED to read unsigned bytes at offset ");
chpr += printf("%lu",offset);
}
else
{
if(fread(rbuf,1,nbytes,inptr) == nbytes)
value = rbuf;
else
{
if(offset == HERE)
offset = curoffset;
PUSHCOLOR(RED);
chpr += printf(" FAILED to read %lu unsigned bytes at offset ",nbytes);
chpr += printf("%lu",offset);
POPCOLOR();
}
}
if(ferror(inptr))
{
chpr += printf(" (");
setcharsprinted(chpr);
chpr = 0;
printred(strerror(errno));
chpr += printf(")");
chpr = newline(chpr);
}
else if(feof(inptr))
{
printred(" (EOF)");
chpr = newline(chpr);
}
}
}
}
return(value);
}
/* convert a couple of bytes to an unsigned short integer in native */
/* byte order, swapping bytes if file byteorder differs. */
/* If byteorder is not TIFF_INTEL or TIFF_MOTOROLA, the native byte */
/* order is used. */
unsigned short
to_ushort(unsigned char *buf,unsigned short byteorder)
{
unsigned short value = 0;
if(buf)
{
value = *(unsigned short *)buf;
#ifdef NATIVE_BYTEORDER_BIGENDIAN
if(byteorder == TIFF_INTEL)
value = (buf[1] << 8) | buf[0];
#else
if(byteorder == TIFF_MOTOROLA)
value = (buf[0] << 8) | buf[1];
#endif
}
return(value);
}
/* convert a bunch of bytes to an unsigned long integer in native */
/* byte order, swapping bytes if file byteorder differs. */
/* If byteorder is not TIFF_INTEL or TIFF_MOTOROLA, the native byte */
/* order is used. */
unsigned long
to_ulong(unsigned char *buf,unsigned short byteorder)
{
u_int32_t value = 0;
if(buf)
{
value = *(u_int32_t *)buf;
#ifdef NATIVE_BYTEORDER_BIGENDIAN
if(byteorder == TIFF_INTEL)
#else
if(byteorder == TIFF_MOTOROLA)
#endif
value = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
}
return((unsigned long)value);
}
/* convert a bunch of bytes to a 'float' value in native byte order, */
/* swapping bytes if file byteorder differs. If byteorder is not */
/* TIFF_INTEL or TIFF_MOTOROLA, the native byte order is used. */
/* This is pretty primitive; it assumes that the native floating */
/* point format is the same as the floating point format written to */
/* the image file, probably IEEE as required by the TIFF6 */
/* specification. See 'libtiff' for some conversion routines. */
float
to_float(unsigned long uval)
{
float value = 0.0;
unsigned long *uval_ptr = &uval;
value = *(float *)uval_ptr;
return(value);
}
/* convert a bunch of bytes to a 'double' value in native byte order, */
/* swapping bytes if file byteorder differs. If byteorder is not */
/* TIFF_INTEL or TIFF_MOTOROLA, the native byte order is used. */
/* This is pretty primitive; it assumes that the native floating */
/* point format is the same as the floating point format written to */
/* the image file, probably IEEE as required by the TIFF6 */
/* specification. See 'libtiff' for some conversion routines. */
double
to_double(unsigned char *buf,unsigned short byteorder)
{
double value = 0.0;
unsigned char swap[sizeof(double)];
unsigned char *p;
int i,size;
if(buf)
{
p = buf;
#ifdef NATIVE_BYTEORDER_BIGENDIAN
if(byteorder == TIFF_INTEL)
#else
if(byteorder == TIFF_MOTOROLA)
#endif
{
size = sizeof(double);
for(i = 0; i < size; ++i)
swap[i] = buf[size - 1 - i];
p = swap;
}
value = *(double *)p;
}
return(value);
}
/* Read a few bytes at 'offset' which are expected to be a JPEG APPn */
/* id string, where 'n' is given as an argument. */
char *
read_appstring(FILE *inptr,unsigned short apptype,unsigned long offset)
{
unsigned long length;
char *appstring = NULLSTRING;
if(inptr)
{
switch(apptype)
{
case JPEG_APP0:
length = 32; /* should be 5; allow for garbage */
break;
case JPEG_APP1:
case JPEG_APP2:
length = 32; /* should be 5; allow for garbage */
break;
default:
length = 32;
break;
}
if(length)
appstring = read_string(inptr,offset,length);
}
return(appstring);
}
/* Read an ascii string, up to a null byte or 'max_bytes', at */
/* 'offset' relative to the beginning of the file. */
/* Returns the string in static storage. Use it or lose it. */
char *
read_string(FILE *inptr,unsigned long offset,unsigned long max_bytes)
{
static char stringbuf[MAXBUFLEN];
unsigned long curoffset;
char *bufp;
int nread = 0;
int chpr = 0;
bufp = stringbuf;
*bufp = '\0';
if(inptr)
{
clearerr(inptr);
curoffset = ftell(inptr);
if((offset != HERE) && (fseek(inptr,offset,SEEK_SET) == -1))
{
printred(" SEEK FAILED to read ascii string at offset ");
chpr += printf("%lu",offset);
}
else if(max_bytes > 0)
{
if(max_bytes > MAXBUFLEN)
max_bytes = MAXBUFLEN - 2;
while((nread < max_bytes) &&
(*bufp = fgetc(inptr)) &&
!feof(inptr) &&
!ferror(inptr) &&
isascii(*bufp++))
{
++nread;
}
*bufp = '\0';
if(feof(inptr) || ferror(inptr))
{
if(offset == HERE)
offset = curoffset;
PUSHCOLOR(RED);
chpr += printf(" FAILED to read character at offset ");
chpr += printf("%lu",offset);
POPCOLOR();
if(ferror(inptr))
{
chpr += printf(" (");
setcharsprinted(chpr);
chpr = 0;
printred(strerror(errno));
chpr += printf(")");
chpr = newline(chpr);
}
else if(feof(inptr))
{
printred(" (EOF)");
chpr = newline(chpr);
}
}
}
}
setcharsprinted(chpr);
return(stringbuf);
}
int
ateof(FILE *inptr)
{
int atend = 0;
if(feof(inptr))
atend++;
else if(fgetc(inptr) == -1)
atend++;
return(atend);
}
/* Get the size of the file open on the argument stream, by seeking */
/* to the end and enquiring of the resulting offset. Return the file */
/* offset to its original location. If anything goes wrong, return 0, */
/* indicating failure (or a zero length file). */
unsigned long
get_filesize(FILE *inptr)
{
unsigned long size = 0L;
long current_offset;
if(inptr)
{
clearerr(inptr);
/* move back here after getting the size */
current_offset = ftell(inptr);
if(fseek(inptr,0L,SEEK_END) == 0)
{
size = ftell(inptr);
if(fseek(inptr,current_offset,SEEK_SET) != 0)
clearerr(inptr); /* silently */
}
else
size = 0L;
}
return(size);
}