File: ciff.c

package info (click to toggle)
exifprobe 2.0.1%2Bgit20201230.eee65ff-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,312 kB
  • sloc: ansic: 34,801; sh: 407; makefile: 82
file content (1686 lines) | stat: -rw-r--r-- 78,066 bytes parent folder | download
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
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- */
/*          EXIFPROBE - TIFF/JPEG/EXIF image file probe               */
/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- */
/* Copyright (C) 2005 by Duane H. Hesser. All rights reserved.        */
/*                                                                    */
/* See the file LICENSE.EXIFPROBE for terms of use.                   */
/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- */

#ifndef lint
static char *ModuleId = "@(#) $Id: ciff.c,v 1.8 2005/07/24 17:03:18 alex Exp $";
#endif

/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- */
/* Canon CIFF/CRW routines                                            */
/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- */
/* The information coded here is derived from the CIFF specification  */
/* found at                                                           */
/*    http://xyrion.org/ciff/CIFFspecV1R04.pdf                        */

/* with additional information from Phil Harvey's website at:         */
/*    http://www.sno.phy.queensu.ca/~phil/exiftool/canon_raw.html     */
/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- */

#define _POSIX_C_SOURCE 200809L /* for strdup() */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

#include "defs.h"
#include "datadefs.h"
#include "summary.h"
#include "maker.h"
#include "misc.h"
#include "tags.h"
#include "extern.h"
#include "ciff.h"
#include "canon_extern.h"
#include "maker_extern.h"


unsigned long
process_ciff(FILE *inptr,struct fileheader *header,unsigned long fileoffset_base,
                        unsigned long heaplength,struct image_summary *summary_entry,
                        char *parent_name,int level,int indent)
{
    struct ciff_header *ciffheader;
    unsigned long max_offset = 0UL;
    unsigned short byteorder = 0;
    unsigned long heap_start = 0UL;
    unsigned long offset_table_end = 0UL;

    if(inptr)
    {
        if(header)
        {
            if(header->ciff_header)
            {
                ciffheader = header->ciff_header;
                byteorder = ciffheader->byteorder;
                heap_start = fileoffset_base + ciffheader->headerlength;
                if(heaplength)
                    offset_table_end = fileoffset_base + heaplength;
                else if(fseek(inptr,0L,SEEK_END) == 0)
                    offset_table_end = ftell(inptr);
                if(ferror(inptr) == 0)
                {
                    max_offset = process_ciff_dir(inptr,heap_start,offset_table_end,
                                                    ciffheader->subtype,parent_name,
                                                    summary_entry,byteorder,
                                                    level,indent);
                    if(max_offset > 0L)
                        max_offset += 4;
                }
                else
                    fprintf(stderr,"%s: cannot read offset table\n",Progname);
            }
            else
                fprintf(stderr,"%s: null ciffheader to process_ciff()\n",Progname);
        }
        else
            fprintf(stderr,"%s: null fileheader to process_ciff()\n",Progname);
    }
    else
        fprintf(stderr,"%s: no open file pointer to read Print Image data\n",
                Progname);

    return(max_offset);
}

unsigned long
process_ciff_dir(FILE *inptr,unsigned long start_offset,unsigned long end_offset,
                char *subtype,char *dirname,struct image_summary * summary_entry,
                unsigned short byteorder,int level,int indent)
{
    struct ciff_direntry *entry;
    unsigned long dircount_loc;
    unsigned long max_dir_offset = 0L;
    unsigned long entry_offset,entry_offset_start;
    unsigned long next_entry_offset,dir_offset;
    unsigned short num_entries;
    char *tablename = CNULL;
    int i;
    int chpr = 0;

    dircount_loc = read_ulong(inptr,byteorder,end_offset - 4) + start_offset;
    num_entries = read_ushort(inptr,byteorder,dircount_loc);
    entry_offset = entry_offset_start = dircount_loc + 2;
    entry = NULL;

    max_dir_offset = start_offset;

    /* Show where this directory is in the heap                       */
    print_tag_address(SECTION|ENTRY,start_offset,indent,"@");
    if((PRINT_SECTION && dirname))
    {
        chpr += printf("<%s> HEAP, length %lu",dirname,end_offset - start_offset);
        chpr = newline(chpr);
    }
    else
    {
        if((PRINT_TAGINFO))
        {
            /* Always want to know WHICH heap offset                  */
            chpr += printf("%s.",dirname);
            chpr += printf("%-*.*s",CIFFTAGWIDTH,CIFFTAGWIDTH,"HeapOffset");
        }
        if((PRINT_VALUE))
        {
            if(PRINT_BOTH_OFFSET)
                chpr += printf(" = @%#lx=%lu",start_offset,start_offset);
            else if(PRINT_HEX_OFFSET)
                chpr += printf(" = @%#lx",start_offset);
            else 
                chpr += printf(" = @%lu",start_offset);
        }
        chpr = newline(chpr);
    }

    /* Now burrow down to the bottom, recursively. This will show the */
    /* lower level directories and entries first.                     */
    for(i = 0; i < num_entries; ++i)
    {
        entry = read_ciff_direntry(inptr,entry,byteorder,entry_offset);
        if(ferror(inptr))
        {
            fprintf(stderr,"%s: error reading directory entry at %lu\n",
                                                Progname,entry_offset);
            break;
        }
        if(entry == NULL)
        {
            fprintf(stderr,"%s: null entry %d\n",Progname,i);
            break;
        }
        next_entry_offset = ftell(inptr);
        dir_offset =
            process_ciff_direntry(inptr,CIFF_INHEAP,byteorder,entry,start_offset,
                                            dirname,summary_entry,level,indent);
        entry_offset = next_entry_offset;
    }

    /* Next show the directory info; this will display higher level   */
    /* directories and their direct entry values as the recursive     */
    /* calls unwind back to the top.                                  */
    if(PRINT_SECTION)
    {
        if(level == 0)
        {
            if(subtype && (strncmp(subtype,"JPGM",4) == 0))
                tablename = splice(dirname,".","ImageProperties");
            else
                tablename = splice(dirname," ","offset table");
            print_tag_address(SECTION,dircount_loc,indent,"@");
            chpr += printf("<%s> %u entries",tablename,num_entries);
        }
        else
        {
            dirname = dirname ? dirname : QSTRING;
            print_tag_address(SECTION,dircount_loc,indent+MEDIUMINDENT,"@");
            chpr += printf("<%s DIRECTORY>, %u entries",dirname,num_entries);
            indent += MEDIUMINDENT;
        }
        chpr = newline(chpr);
    }
 
    entry_offset = entry_offset_start;
    max_dir_offset = start_offset;
    for(i = 0; i < num_entries; ++i)
    {
        entry = read_ciff_direntry(inptr,entry,byteorder,entry_offset);
        next_entry_offset = ftell(inptr);

        if((PRINT_SECTION)) 
        {
            print_tag_address(ENTRY,entry_offset,indent + MEDIUMINDENT,"@");
            print_ciff_taginfo(entry,start_offset);
        }
        if(PRINT_VALUE)
        {
            print_ciff_value(inptr,entry,byteorder,start_offset,entry_offset,
                                                    dirname,summary_entry
                                                    ,level,indent);
        }
        entry_offset = next_entry_offset;
        if(entry_offset > max_dir_offset)
            max_dir_offset = entry_offset;
    }
    /* Close off the directory; at the top, report the location of    */
    /* the offset table (or top level directory if JPGM) and loction  */
    /* where its location was found (at the end of the file).         */
    if(PRINT_SECTION)
    {
        if(level > 0)
        {
            print_tag_address(SECTION|ENTRY,max_dir_offset - 1,indent,"-");
            dirname = dirname ? dirname : QSTRING;
            chpr += printf("</%s DIRECTORY>\n",dirname);
            print_tag_address(SECTION|ENTRY,end_offset - 4,indent,"-");
            chpr += printf("%s DIRECTORY offset = %lu\n",dirname,dircount_loc);
            print_tag_address(SECTION|ENTRY,end_offset - 1,indent-MEDIUMINDENT,"-");
            chpr += printf("</%s>",dirname);
        }
        else
        {
            print_tag_address(SECTION|ENTRY,max_dir_offset - 1,indent,"-");
            /* tablename is dynamically allocated */
            tablename = tablename ? tablename : strdup(QSTRING);
            chpr += printf("</%s>\n",tablename);
            print_tag_address(SECTION|ENTRY,end_offset - 4,indent,"-");
            chpr += printf("%s location = @%lu",tablename,dircount_loc);
        }
    }
    else if(level == 0)
    {
        print_tag_address(SECTION|ENTRY,end_offset - 1,indent,"-");
        if((PRINT_TAGINFO))
        {
            if((PRINT_LONGNAMES)) 
                chpr += printf("%s.",dirname);
            chpr += printf("OffsetTableOffset");
        }
        if((PRINT_VALUE))
            chpr += printf(" = @%lu",dircount_loc);
    }
    chpr = newline(chpr);
    if(entry)
        free(entry);
    if(tablename)
        free(tablename);
    return(max_dir_offset);
}


/* Print the part of a CIFF entry describing the entry tag, including */
/* it's tag number, name and type. Only the items enabled in          */
/* "Print_options" are printed.                                       */

void
print_ciff_taginfo(struct ciff_direntry *entry,unsigned long fileoffset_base)
{
    char *nameoftag = NULL;
    int tagwidth = CIFFTAGWIDTH;
    unsigned short tag,format,location;
    int chpr = 0;

    if(entry && (PRINT_ENTRY))
    {
        tag = entry->type & CIFF_TYPEMASK;
        format = entry->type & CIFF_FORMATMASK;
        location = entry->type & CIFF_LOCATIONMASK;
        if(PRINT_BOTH_TAGNO)
            chpr += printf("<%#06x=%5u> ",tag,tag);
        else if(PRINT_DEC_TAGNO)
            chpr += printf("<%5u> ",tag);
        else if(PRINT_HEX_TAGNO)
            chpr += printf("<%#06x> ",tag);
        if((PRINT_TAGNAME))
        {
            nameoftag = cifftagname(tag);
            if(nameoftag)
            {
                chpr += printf("%-*.*s",tagwidth,tagwidth,nameoftag);
                free(nameoftag);
            }
        }
        if(PRINT_TYPE)
        {
            chpr += printf(" [%#06x ",format);
            switch(format)
            {
                case 0x0000: chpr += printf("BYTE  "); break;
                case 0x0800: chpr += printf("ASCII "); break;
                case 0x1000: chpr += printf("SHORT "); break;
                case 0x1800: chpr += printf("LONG  "); break;
                case 0x2000: chpr += printf("STRUCT"); break;
                case 0x2800: chpr += printf("SUBDIR"); break;
                case 0x3000: chpr += printf("SUBDIR"); break;
                case 0x3800:
                default:
                    chpr += printf("UNDEFINED");
                    break;
            }
            switch(location)
            {
                case 0x0000: chpr += printf(" INHEAP %10lu %10lu",entry->length,entry->offset); break;
                case 0x4000: chpr += printf(" INREC  %10lu %10lu",entry->length,entry->offset); break;
                case 0x8000: chpr += printf(" X8000  %10lu %10lu",entry->length,entry->offset); break;
                case 0xc000: chpr += printf(" XC000  %10lu %10lu",entry->length,entry->offset); break;
                default:
                    chpr += printf(" UNKNOWNLOC");
                    break;
            }
            chpr += printf("] ");
        }
        /* ###%%% should be done immediately before value is printed; */
        /* not here                                                   */
        if(PRINT_VALUE)
            chpr += printf(" = "); 
    }
    setcharsprinted(chpr);
}

/* Print the "value" portion of a directory entry. If the value is    */
/* contained fully within the entry, the direntry processor is needed */
/* to decode the tag value, otherwise, just print the address in the  */
/* heap where things have already been handled.                       */

void
print_ciff_value(FILE *inptr,struct ciff_direntry *entry, unsigned short byteorder,
                            unsigned long fileoffset_base,unsigned long entry_offset,
                            char *dirname,struct image_summary *summary_entry,
                            int level,int indent)
{
    unsigned short location;
    unsigned long max_dir_offset = 0;
    int chpr = 0;

    location = entry->type & CIFF_LOCATIONMASK;
    if(location == CIFF_INREC)
    {
        /* chpr += printf("INREC"); */
        max_dir_offset = process_ciff_direntry(inptr,CIFF_INREC,byteorder,entry,
                                        entry_offset,dirname,summary_entry,level,
                                        indent);
    }
    else if(PRINT_SECTION)
    {
        if(PRINT_BOTH_OFFSET)
            chpr += printf("@%#lx=%lu",entry->offset + fileoffset_base,
                    entry->offset + fileoffset_base);
        else if(PRINT_HEX_OFFSET)
            chpr += printf("@%#lx",entry->offset + fileoffset_base);
        else /* default to decimal offset if nothing is selected      */
            chpr += printf("@%lu",entry->offset + fileoffset_base);

        if(fileoffset_base && (PRINT_ENTRY_RELOFFSET))
            chpr += printf(" (%lu) ",entry->offset);
        chpr = newline(chpr);
    }
    setcharsprinted(chpr);
}

unsigned long
canon_colorspace(FILE *inptr,unsigned short byteorder,char *tagprefix,
                            unsigned long offset,unsigned long dirlength,int indent)
{
    unsigned long count,value;
    unsigned long end_offset;
    int i;
    int chpr = 0;

    if(PRINT_SECTION)
    {
        chpr += printf(", length %lu",dirlength);
        chpr = newline(chpr);
    }

    end_offset = offset + dirlength;
    count = (dirlength / sizeof(unsigned short));

    /* There should be just one (no idea why they didn't do it        */
    /* INRECORD), and there is no 2-byte count                        */
    for(i = 0; i < count; ++i)
    {
        value = read_ushort(inptr,byteorder,offset);
        print_tag_address(ENTRY,offset,indent,"@");
        if((PRINT_TAGINFO))
        {
            if(tagprefix && (PRINT_LONGNAMES))
                chpr = printf("%s.",tagprefix);
            chpr = printf("%-*.*s",CIFFTAGWIDTH,CIFFTAGWIDTH,"type");
        }
        if((PRINT_VALUE))
        {
            chpr += printf(" = %lu ",value);
            if(value == 1)
                chpr += printf(" = \"real-world\"");
            else if(value == 2)
                chpr += printf(" = \"document\"");
            else if(value == 0xfff)
                chpr += printf(" = \"uncalibrated\"");
            else
                chpr += printf(" = \"undefined\"");
        }
        chpr = newline(chpr);
        offset += 2;
    }
    setcharsprinted(chpr);
    return(end_offset);
}

/* Decode the "capture time"; print in the Exif "DateTimeOriginal"    */
/* format.                                                            */
unsigned long
canon_ct_to_datetime(FILE *inptr,unsigned short byteorder,char *tagname,unsigned long offset,
                                                unsigned long dirlength,int indent)
{
    unsigned long end_offset = 0UL;
    unsigned long count;
    unsigned long timezone;
    unsigned long zoneinfo;
    struct tm *ts_time;
    time_t timestamp;
    int chpr = 0;

    if(PRINT_SECTION)
    {
        chpr += printf(", length %lu",dirlength);
        chpr = newline(chpr);
    }

    count = dirlength / sizeof(unsigned long);
    end_offset = offset + dirlength;
    print_tag_address(ENTRY,offset,indent+MEDIUMINDENT,"@");
    tagname = tagname ? tagname : QSTRING;
    if(PRINT_TAGNAME)
        chpr += printf("%s",tagname);
    if(PRINT_VALUE)
    {
        chpr += printf(" = ");
        print_ulong(inptr,count,byteorder,offset);
    }
    timestamp = read_ulong(inptr,byteorder,offset);
    timezone = read_ulong(inptr,byteorder,HERE);
    zoneinfo = read_ulong(inptr,byteorder,HERE);
    if(PRINT_VALUE)
    {
        ts_time = gmtime(&timestamp);
        chpr += printf(" = %02d:%02d:%02d %02d:%02d:%02d GMT",ts_time->tm_year+1900,ts_time->tm_mon+1,
                ts_time->tm_mday,ts_time->tm_hour,ts_time->tm_min,ts_time->tm_sec);
        if((zoneinfo & 0x80000000) && timezone)
            chpr += printf(" + %lu",timezone);
    }
    chpr = newline(chpr);

    return(end_offset);
}

/* Print a "value" from a CIFF structure entry, according to the CIFF */
/* type recorded in the entry. Print in hex or decimal (or both)      */
/* according to the global Print_options variable.                    */

/* This is generally undefined data, usually with unknown tagname or  */
/* data interpretation. It may be large, so line-by-line output is    */
/* avoided in favor of the abbreviated 'print_TYPE()' routines, and   */
/* an opportunity is provided to hex/ascii dump the section .         */

unsigned long
print_ciffinheapdata(FILE *inptr,unsigned short byteorder,char *dirname,
            char *tagname,unsigned long offset,unsigned long dirlength,
            unsigned short data_format,int indent)
{
    unsigned long end_offset = 0UL;
    unsigned long dumplength;
    unsigned short count;
    int chpr = 0;

    if(inptr)
    {
        end_offset = offset + dirlength;
        switch(data_format)
        {
            case 0x0000: count = dirlength; break;
            case 0x0800: count = dirlength; break;
            case 0x1000: count = dirlength/2; break;
            case 0x1800: count = dirlength/4; break;
            default: count = dirlength; break;  /* handle as bytes?   */
        }
        dirname = dirname ? dirname : QSTRING;
        tagname = tagname ? tagname : QSTRING;
        if((PRINT_SECTION))
        {
            print_tag_address(SECTION|ENTRY,offset, indent,"@");
            chpr += printf("<%s.%s> data, %u entries, length %lu",dirname,tagname,count,dirlength);
            chpr = newline(chpr);
        }
        if((PRINT_TAGINFO))
        {
            print_tag_address(ENTRY,offset,indent+MEDIUMINDENT,"@");
            if((PRINT_LONGNAMES))
                chpr += printf("%s.",dirname);
            chpr += printf("%-*.*s",CIFFTAGWIDTH,CIFFTAGWIDTH,tagname);
        }
        if(PRINT_VALUE)
        {
            chpr += printf(" = ");
            switch(data_format)
            {
                case 0x1000:    /* DC_USHORT                      */
                            print_ushort(inptr,count,byteorder,offset);
                            break;
                case 0x1800:   /* DC_UINT32:                      */
                            print_ulong(inptr,count,byteorder,offset);
                            break;
                case 0x0800:     /* DC_ASCII:                     */
                            print_ascii(inptr,count,offset);
                            break;
                case 0x0000:     /* DC_BYTE:                      */
                            print_ubytes(inptr,count,offset);
                            break;
                default:
                            chpr += printf("(data format %#x)",data_format);
                            break;
            }
            if(Max_undefined > 0L)
            {
                if((Max_undefined == DUMPALL) ||
                        (Max_undefined > dirlength))
                    dumplength = dirlength;
                else 
                    dumplength = Max_undefined;
                chpr = newline(chpr);
                hexdump(inptr,offset,dirlength,dumplength,16,indent+MEDIUMINDENT,
                                                        MEDIUMINDENT);
                chpr = newline(chpr);
            }
        }
        if(PRINT_SECTION)
        {
            chpr = newline(chpr);
            print_tag_address(SECTION|ENTRY,end_offset - 1, indent,"-");
            dirname = dirname ? dirname : QSTRING;
            chpr += printf("</%s.%s>",dirname,tagname);
        }
        setcharsprinted(chpr);
    }
    return(end_offset);
}

/* Print a "value" from a CIFF directory entry when the value is      */
/* contained within the record, rather than INHEAP. The data is       */
/* printed according to the CIFF type recorded in the entry, in hex   */
/* or decimal (or both) according to the global Print_options         */
/* variable.                                                          */

/* This is generally undefined data, usually with unknown tagname or  */
/* data interpretation.                                               */

unsigned long
print_ciffinrecdata(FILE *inptr,unsigned short byteorder,char *tagname,
                                unsigned long offset,unsigned long dirlength,
                                unsigned short data_format,int indent)
{
    unsigned long end_offset = 0UL;
    unsigned short count;
    int chpr = 0;

    if(inptr)
    {
        end_offset = offset + dirlength;
        if((PRINT_VALUE))
        {
            count = dirlength;
            chpr = count;
            switch(data_format)
            {
                case 0x1000:    /* DC_USHORT                          */
                            print_ushort(inptr,count/2,byteorder,offset);
                            break;
                case 0x1800:    /* DC_UINT32:                         */
                            print_ulong(inptr,count/4,byteorder,offset);
                            break;
                case 0x0800:    /* DC_ASCII:                          */
                            print_ascii(inptr,count,offset); 
                            break;
                case 0x0000:    /* DC_BYTE:                           */
                            print_ubytes(inptr,count,offset);
                            break;
                default:
                            chpr += printf("(data format %#x)",data_format);
                            break;
            }
            setcharsprinted(chpr);
        }
    }
    return(end_offset);
}

unsigned long
process_ciff_direntry(FILE *inptr,unsigned short location,unsigned short byteorder,
                        struct ciff_direntry *entry, unsigned long fileoffset_base,
                        char *dirname,struct image_summary *summary_entry,int level,
                        int indent)
{
    unsigned long max_offset = 0;
    unsigned long endofdir;
    unsigned short tag,marker,shvalue;
    char *nameoftag = NULL;
    char *fullnameoftag = NULL;
    char *tagname,*makename;
    int entrywidth = CIFFTAGWIDTH;
    int status = 0;
    int chpr = 0;
    int parindent = 0;
    int make,model,tagwidth;
    int makelen;
    float fvalue;

    if(entry)
    {
        if((entry->type & CIFF_LOCATIONMASK) == location)
        {
            chpr = 0;
            max_offset = fileoffset_base;
            /* ###%%%
            if((entry->type & CIFF_LOCATIONMASK) == CIFF_INHEAP)
                chpr += printf("INHEAP");
            */
            tag = entry->type & CIFF_TYPEMASK;
            nameoftag = cifftagname(tag);
            fullnameoftag = splice(dirname,".",nameoftag);
            if(PRINT_LONGNAMES)
            {
                tagname = fullnameoftag;
                if(dirname)
                    entrywidth += strlen(dirname) + 1;
            }
            else
                tagname = nameoftag;
            
            switch(tag)
            {
                case CIFFTAG_COLORINFO2:    /* fall through           */
                case CIFFTAG_COLORINFO1:    /* INHEAPDATA             */
                    if((PRINT_SECTION))
                        indent += MEDIUMINDENT;
                    max_offset = print_ciffinheapdata(inptr,byteorder,dirname,
                            nameoftag,entry->offset + fileoffset_base,entry->length,
                            entry->type&CIFF_FORMATMASK,indent/* +MEDIUMINDENT*/);
                    break;
                case CIFFTAG_ROMOPERATIONMODE:  /* ASCII INREC        */
                    if(!(PRINT_SECTION) && (PRINT_TAGINFO))
                    {
                        print_tag_address(ENTRY,fileoffset_base + 2,indent+MEDIUMINDENT,"@");
                        chpr = printf("%-*.*s",entrywidth,entrywidth,tagname);
                    }
                    if(PRINT_VALUE)
                    {
                        print_ascii(inptr,8,fileoffset_base + 2);
                        max_offset = fileoffset_base + 2 + 8;
                    }
                    break;
                case CIFFTAG_RAWMAKEMODEL: /* ###%%% ASCII SPECIAL    */
                    print_tag_address(ENTRY,entry->offset + fileoffset_base,indent+MEDIUMINDENT,
                                                                                "@");
                    makename = (char *)read_bytes(inptr, entry->length,
                                                    entry->offset + fileoffset_base);
                    max_offset = ftell(inptr);
                    Make_name = strdup(makename);
                    makelen = strlen(makename);
                    Model_name = strdup(makename + makelen + 1);
                    dirname = dirname ? dirname : QSTRING;
                    if((LIST_MODE))
                    {
                        /* Make pseudo-tags and separate Make and     */
                        /* Model                                      */
                        if(PRINT_TAGNAME)
                            chpr += printf("%s.%-*.*s",dirname,CIFFTAGWIDTH,CIFFTAGWIDTH,"Make");
                        if(PRINT_VALUE)
                        {
                            chpr += printf(" = \"%s\"",Make_name);
                            if(Use_Make_name)
                                chpr += printf(" (Using: Make=%s)",Use_Make_name);
                        }
                        chpr = newline(chpr);
                        print_tag_address(ENTRY,entry->offset + fileoffset_base + strlen(Make_name) + 1,
                                                                            indent+MEDIUMINDENT,"@");
                        if(PRINT_TAGNAME)
                            chpr += printf("%s.%-*.*s",dirname,CIFFTAGWIDTH,CIFFTAGWIDTH,"Model");
                        if(PRINT_VALUE)
                        {
                            chpr += printf(" = \"%s\"",Model_name);
                            if(Use_Make_name)
                                chpr += printf(" (Using: Model=%s)",Use_Model_name);
                        }
                    }
                    else
                    {
                        if(PRINT_TAGNAME)
                        {
                            if((PRINT_SECTION))
                                chpr += printf("%s.%-*.*s",dirname,CIFFTAGWIDTH,CIFFTAGWIDTH,nameoftag);
                            else
                                chpr += printf("%-*.*s",CIFFTAGWIDTH,CIFFTAGWIDTH,nameoftag);
                        }
                        if(PRINT_VALUE)
                        {
                            chpr += printf(" = ");
                            if((PRINT_ASCII_IGNORE_LENGTH))
                                chpr += printf("%s\\0%s",Make_name,Model_name);
                            else
                                print_ascii(inptr,entry->length,entry->offset + fileoffset_base);
                            if(Use_Make_name)
                                chpr += printf(" Using: Make=%s",Use_Make_name);
                            if(Use_Model_name)
                                chpr += printf(" Using: Model=%s",Use_Model_name);
                        }
                    }
                    break;
                case CIFFTAG_FILEDESCRIPTION:   /* ASCII INHEAP       */
                case CIFFTAG_FIRMWAREVERSION:   /* ASCII INHEAP       */
                case CIFFTAG_OWNERNAME:         /* ASCII INHEAP       */
                case CIFFTAG_IMAGETYPE:         /* ASCII INHEAP       */
                case CIFFTAG_ORIGINALFILENAME:  /* ASCII INHEAP       */
                case CIFFTAG_THUMBNAILFILENAME: /* ASCII INHEAP       */
                    print_tag_address(ENTRY,entry->offset + fileoffset_base,indent+MEDIUMINDENT,"@");
                    if((PRINT_TAGINFO))
                        chpr = printf("%-*.*s",entrywidth,entrywidth,tagname);
                    if(PRINT_VALUE)
                    {
                        chpr += printf(" = ");
                        print_ascii(inptr,entry->length,entry->offset + fileoffset_base);
                        max_offset = ftell(inptr);
                    }
                    break;
                case CIFFTAG_BASEISO:       /* INREC */
                    if(!(PRINT_SECTION) && (PRINT_TAGINFO))
                    {
                        print_tag_address(ENTRY,fileoffset_base + 2,indent+MEDIUMINDENT,"@");
                        chpr = printf("%-*.*s",entrywidth,entrywidth,tagname);
                    }
                    if(PRINT_VALUE)
                        chpr += printf("%lu",entry->length);
                    max_offset = ftell(inptr);
                    break;
                case CIFFTAG_FOCALLENGTH:     /* INREC                */
                    /* length mm, xy size in .001 inch ###%%% ???     */
                    if((PRINT_VALUE) && (PRINT_SECTION))
                    {
                        indent += 19;
                        chpr = printf("{");
                        chpr = newline(chpr);
                        parindent = charsprinted() - LARGEINDENT;
                        indent += MEDIUMINDENT;
                    }
                    if(PRINT_ENTRY)
                    {
                        entrywidth = strlen("TargetCompressionRatio");
                        print_tag_address(ENTRY,fileoffset_base + 2,indent + MEDIUMINDENT,"@");
                        parindent = charsprinted() - LARGEINDENT;
                        if((PRINT_TAGINFO))
                        {
                            if((PRINT_LONGNAMES))
                                chpr += printf("%s.",fullnameoftag);
                            chpr += printf("%-*.*s",entrywidth,entrywidth,"Undefined"); 
                        }
                        if((PRINT_VALUE))
                        {
                            shvalue = read_ushort(inptr,byteorder,fileoffset_base + 2);
                            printf(" = %u",shvalue);
                            chpr = newline(chpr);
                        }
                        print_tag_address(ENTRY,fileoffset_base + 4,indent + MEDIUMINDENT,"@");
                        if((PRINT_TAGINFO))
                        {
                            if((PRINT_LONGNAMES))
                                chpr += printf("%s.",fullnameoftag);
                            chpr += printf("%-*.*s",entrywidth,entrywidth,
                                                                        "FocalLength"); 
                        }
                        if((PRINT_VALUE))
                        {
                            shvalue = read_ushort(inptr,byteorder,fileoffset_base + 4);
                            chpr += printf(" = %u mm",shvalue);
                            chpr = newline(chpr);
                        }
                        print_tag_address(ENTRY,fileoffset_base + 6,indent + MEDIUMINDENT,"@");
                        if((PRINT_TAGINFO))
                        {
                            if((PRINT_LONGNAMES))
                                chpr += printf("%s.",fullnameoftag);
                            chpr += printf("%-*.*s",entrywidth,entrywidth,
                                                                        "FocalPlaneXDim"); 
                        }
                        if((PRINT_VALUE))
                        {
                            shvalue = read_ushort(inptr,byteorder,fileoffset_base + 6);
                            chpr += printf(" = %u inches/1000",shvalue);
                            chpr = newline(chpr);
                        }
                        print_tag_address(ENTRY,fileoffset_base + 8,indent + MEDIUMINDENT,"@");
                        if((PRINT_TAGINFO))
                        {
                            if((PRINT_LONGNAMES))
                                chpr += printf("%s.",fullnameoftag);
                            chpr += printf("%-*.*s",entrywidth,entrywidth,
                                                                        "FocalPlaneYDim"); 
                        }
                        if((PRINT_VALUE))
                        {
                            shvalue = read_ushort(inptr,byteorder,fileoffset_base + 8);
                            chpr += printf(" = %u inches/1000",shvalue);
                            if(PRINT_SECTION)
                            {
                                chpr = newline(chpr);
                                print_tag_address(ENTRY,fileoffset_base + 9,0,"@");
                                parindent -= charsprinted();
                                putindent(parindent);
                                chpr = printf("}");
                                indent -= MEDIUMINDENT;
                            }
                        }
                    }
                    break;
                case CIFFTAG_SHOTINFO: /* INHEAP subroutine           */
                    if((PRINT_SECTION))
                    {
                        PUSHCOLOR(MAKER_COLOR);
                        print_tag_address(SECTION|ENTRY,entry->offset + fileoffset_base,
                                            indent+MEDIUMINDENT,"@");
                        chpr += printf("<%s> data, %lu entries",fullnameoftag,(entry->length/sizeof(short)) - 1);
                        indent += MEDIUMINDENT;
                    }
                    max_offset = canon_shotinfo(inptr,byteorder,tagname,
                                                entry->offset + fileoffset_base,
                                                entry->length,indent+MEDIUMINDENT);
                    if((PRINT_SECTION))
                    {
                        indent -= MEDIUMINDENT;
                        print_tag_address(SECTION|ENTRY,entry->offset + fileoffset_base + entry->length - 1,
                                            indent+MEDIUMINDENT,"-");
                        chpr += printf("</%s>",fullnameoftag);
                        POPCOLOR();
                    }
                    break;
                case CIFFTAG_CAMERASETTINGS:    /* INHEAP subroutine */
                    if((PRINT_SECTION))
                    {
                        PUSHCOLOR(MAKER_COLOR);
                        print_tag_address(SECTION|ENTRY,entry->offset + fileoffset_base,
                                            indent+MEDIUMINDENT,"@");
                        chpr += printf("<%s> data, %lu entries",fullnameoftag,
                                            (entry->length/sizeof(short)) - 1);
                        indent += MEDIUMINDENT;
                    }
                    max_offset = canon_camera_settings(inptr,byteorder,
                                                    tagname,entry->offset + fileoffset_base,
                                                    entry->length,indent+MEDIUMINDENT);
                    if((PRINT_SECTION))
                    {
                        indent -= MEDIUMINDENT;
                        print_tag_address(SECTION|ENTRY,entry->offset + fileoffset_base + entry->length - 1,
                                                                    indent+MEDIUMINDENT,"-");
                        chpr += printf("</%s>",fullnameoftag);
                        POPCOLOR();
                    }
                    break;
                case CIFFTAG_SENSORINFO:    /* INHEAP subroutine */
                    if((PRINT_SECTION))
                    {
                        PUSHCOLOR(MAKER_COLOR);
                        print_tag_address(SECTION|ENTRY,entry->offset + fileoffset_base,
                                            indent+MEDIUMINDENT,"@");
                        chpr += printf("<%s> data, %lu entries",fullnameoftag,(entry->length/sizeof(short)) - 1);
                        indent += MEDIUMINDENT;
                    }
                    max_offset = canon_sensorinfo(inptr,byteorder,tagname,entry->offset + fileoffset_base,
                                                                entry->length,indent+MEDIUMINDENT);
                    if((PRINT_SECTION))
                    {
                        indent -= MEDIUMINDENT;
                        print_tag_address(SECTION|ENTRY,entry->offset + fileoffset_base + entry->length - 1,
                                            indent+MEDIUMINDENT,"-");
                        chpr += printf("</%s>",fullnameoftag);
                        POPCOLOR();
                    }
                    break;
                case CIFFTAG_CUSTOMFUNCTIONS:   /* INHEAP subroutine */
                    if(Use_Make_name)
                        make = maker_number(Use_Make_name);
                    else
                        make = maker_number(Make_name);
                    if(Use_Model_name)
                        model = model_number(make,Use_Model_name,NULLSTRING);
                    else
                        model = model_number(make,Model_name,NULLSTRING);
                    if((PRINT_SECTION))
                    {
                        PUSHCOLOR(MAKER_COLOR);
                        print_tag_address(SECTION|ENTRY,entry->offset + fileoffset_base,
                                            indent+MEDIUMINDENT,"@");
                        chpr += printf("<%s> data, %lu entries",fullnameoftag,
                                                    (entry->length/sizeof(short)) - 1);
                        indent += MEDIUMINDENT;
                    }
                    max_offset = canon_customfunctions(inptr,byteorder,
                                            tagname,entry->offset + fileoffset_base,
                                            entry->length,model,indent+MEDIUMINDENT);
                    if((PRINT_SECTION))
                    {
                        indent -= MEDIUMINDENT;
                        print_tag_address(SECTION|ENTRY,entry->offset + fileoffset_base + entry->length - 1,
                                            indent+MEDIUMINDENT,"-");
                        chpr += printf("</%s>",fullnameoftag);
                        POPCOLOR();
                    }
                    break;
                case CIFFTAG_PICTUREINFO:   /* INHEAP subroutine */
                    if((PRINT_SECTION))
                    {
                        PUSHCOLOR(MAKER_COLOR);
                        print_tag_address(SECTION|ENTRY,entry->offset + fileoffset_base,
                                            indent+MEDIUMINDENT,"@");
                        chpr += printf("<%s> data, %lu entries",fullnameoftag,(entry->length/sizeof(short)) - 1);
                        indent += MEDIUMINDENT;
                    }
                    max_offset = canon_pictureinfo(inptr,byteorder,
                                                    tagname,entry->offset + fileoffset_base,
                                                    entry->length,indent+MEDIUMINDENT);
                    if((PRINT_SECTION))
                    {
                        indent -= MEDIUMINDENT;
                        print_tag_address(SECTION|ENTRY,entry->offset + fileoffset_base + entry->length - 1,
                                            indent+MEDIUMINDENT,"-");
                        chpr += printf("</%s>",fullnameoftag);
                        POPCOLOR();
                    }
                    break;
                case CIFFTAG_WHITEBALANCETABLE: /* INHEAP subroutine */
                    if((PRINT_SECTION))
                    {
                        PUSHCOLOR(MAKER_COLOR);
                        print_tag_address(SECTION|ENTRY,entry->offset + fileoffset_base,
                                            indent+MEDIUMINDENT,"@");
                        chpr += printf("<%s> data, %lu entries",fullnameoftag,
                                                (entry->length - sizeof(unsigned short))/sizeof(float));
                        indent += MEDIUMINDENT;
                    }
                    max_offset = canon_whitebalancetable(inptr,byteorder,tagname,
                                    entry->offset + fileoffset_base,entry->length,indent+MEDIUMINDENT);
                    if((PRINT_SECTION))
                    {
                        indent -= MEDIUMINDENT;
                        print_tag_address(SECTION|ENTRY,entry->offset + fileoffset_base + entry->length - 1,
                                            indent+MEDIUMINDENT,"-");
                        chpr += printf("</%s>",fullnameoftag);
                        POPCOLOR();
                    }
                    break;
                case CIFFTAG_IMAGESPEC:   /* INREC */
                    if((PRINT_VALUE) && (PRINT_SECTION))
                    {
                        indent += 19;
                        chpr = printf("{");
                        chpr = newline(chpr);
                        parindent = charsprinted();
                    }
                    if(PRINT_ENTRY)
                    {
                        entrywidth = strlen("TargetCompressionRatio");
                        dirname = dirname ? dirname : QSTRING;
                        print_tag_address(ENTRY,fileoffset_base + 2,indent + LARGEINDENT,"@");
                        parindent = charsprinted() - LARGEINDENT;
                        if((PRINT_TAGINFO))
                        {
                            if((PRINT_LONGNAMES))
                                chpr += printf("%s.",fullnameoftag);
                            chpr += printf("%-*.*s",entrywidth,entrywidth,"FileFormat"); 
                        }
                        if((PRINT_VALUE))
                        {
                            chpr += printf(" = ");
                            switch(entry->length)
                            {
                                case 0x10000: chpr += printf("JPEG quantized\n"); break;
                                case 0x10002: chpr += printf("JPEG non-quantized\n"); break;
                                case 0x10003: chpr += printf("JPEG nq picture/q text\n"); break;
                                case 0x20001: chpr += printf("CRW\n"); break;
                                default: chpr += printf("unknown\n"); break;
                            }
                        }
                        print_tag_address(ENTRY,fileoffset_base + 6,indent + LARGEINDENT,"@");
                        if((PRINT_TAGINFO))
                        {
                            if((PRINT_LONGNAMES))
                                chpr += printf("%s.",fullnameoftag);
                            chpr += printf("%-*.*s",entrywidth,entrywidth,"TargetCompressionRatio"); 
                        }
                        if((PRINT_VALUE))
                        {
                            fvalue = to_float(entry->offset);
                            chpr += printf(" = %f",fvalue);
                            if(PRINT_SECTION)
                            {
                                chpr = newline(chpr);
                                print_tag_address(ENTRY,fileoffset_base + 9,0,"@");
                                parindent -= charsprinted();
                                putindent(parindent);
                                chpr = printf("}");
                            }
                        }
                    }
                    break;
                case CIFFTAG_SHUTTERRELEASEMETHOD:  /* INREC */
                    if(!(PRINT_SECTION) && (PRINT_TAGINFO))
                    {
                        print_tag_address(ENTRY,fileoffset_base + 2,indent + MEDIUMINDENT,"@");
                        chpr = printf("%-*.*s",entrywidth,entrywidth,tagname);
                        if(PRINT_VALUE)
                            chpr += printf(" = ");
                    }
                    if(PRINT_VALUE)
                    {
                        print_ushort(inptr,4,byteorder,fileoffset_base + 2);
                        chpr += printf("%lu = ",entry->length & 0xffff);
                        if((entry->length & 0xffff) == 0)
                            chpr += printf("single-shot");
                        else if((entry->length & 0xffff) == 1)
                            chpr += printf("continuous");
                        else
                            chpr += printf("undefined");
                    }
                    break;
                case CIFFTAG_SHUTTERRELEASETIMING:  /* INREC */
                    if(!(PRINT_SECTION) && (PRINT_TAGINFO))
                    {
                        print_tag_address(ENTRY,fileoffset_base + 2,indent + MEDIUMINDENT,"@");
                        chpr = printf("%-*.*s",entrywidth,entrywidth,tagname);
                        if(PRINT_VALUE)
                            chpr += printf(" = ");
                    }
                    if(PRINT_VALUE)
                    {
                        print_ushort(inptr,4,byteorder,fileoffset_base + 2);
                        chpr += printf(" = %lu = ",entry->length & 0xffff);
                        if((entry->length & 0xffff) == 0)
                            chpr += printf("shutter-priority");
                        else if((entry->length & 0xffff) == 1)
                            chpr += printf("aperture-priority");
                        else
                            chpr += printf("undefined");
                    }
                    break; 
                case CIFFTAG_SELFTIMERTIME: /* INREC */
                    if(!(PRINT_SECTION) && (PRINT_TAGINFO))
                    {
                        print_tag_address(ENTRY,fileoffset_base + 2,indent + MEDIUMINDENT,"@");
                        chpr = printf("%-*.*s",entrywidth,entrywidth,tagname);
                    }
                    if(PRINT_VALUE)
                        chpr += printf(" = %lu msec ",entry->length & 0xffff);
                    break;
                case CIFFTAG_TARGETDISTANCESETTING: /* INREC */
                    if(!(PRINT_SECTION) && (PRINT_TAGINFO))
                    {
                        print_tag_address(ENTRY,fileoffset_base + 2,indent + MEDIUMINDENT,"@");
                        chpr = printf("%-*.*s",entrywidth,entrywidth,tagname);
                    }
                    if(PRINT_VALUE)
                    {
                        float fvalue;

                        fvalue = to_float(entry->length);
                        chpr += printf("%f mm",fvalue);
                    }
                    break;
                case CIFFTAG_COLORSPACE:    /* INHEAP subroutine      */
                    if((PRINT_SECTION))
                    {
                        PUSHCOLOR(MAKER_COLOR);
                        print_tag_address(SECTION|ENTRY,entry->offset + fileoffset_base,
                                            indent+MEDIUMINDENT,"@");
                        chpr += printf("<%s> data, %lu entries",fullnameoftag,(entry->length/sizeof(unsigned short)));
                        indent += MEDIUMINDENT;
                    }
                    max_offset = canon_colorspace(inptr,byteorder,
                                                    tagname,entry->offset + fileoffset_base,
                                                    entry->length,indent+MEDIUMINDENT);
                    if((PRINT_SECTION))
                    {
                        indent -= MEDIUMINDENT;
                        print_tag_address(SECTION|ENTRY,entry->offset + fileoffset_base + entry->length - 1,
                                            indent+MEDIUMINDENT,"-");
                        chpr += printf("</%s>",fullnameoftag);
                        POPCOLOR();
                    }
                    break;
                case CIFFTAG_TARGETIMAGETYPE:   /* INREC */
                    if(!(PRINT_SECTION) && (PRINT_TAGINFO))
                    {
                        print_tag_address(ENTRY,fileoffset_base + 2,indent + MEDIUMINDENT,"@");
                        chpr = printf("%-*.*s",entrywidth,entrywidth,tagname);
                        if(PRINT_VALUE)
                            chpr += printf(" = ");
                    }
                    if(PRINT_VALUE)
                    {
                        print_ushort(inptr,4,byteorder,fileoffset_base + 2);
                        chpr += printf(" = %lu = ",entry->length & 0xffff);
                        if((entry->length & 0xffff) == 0)
                            chpr += printf("real-world");
                        else if((entry->length & 0xffff) == 1)
                            chpr += printf("document");
                        else
                            chpr += printf("undefined");
                    }
                    break; 
                case CIFFTAG_SERIALNUMBER:  /* INREC                  */
                    if(!(PRINT_SECTION) && (PRINT_TAGINFO))
                    {
                        print_tag_address(ENTRY,fileoffset_base + 2,indent + MEDIUMINDENT,"@");
                        chpr = printf("%-*.*s",entrywidth,entrywidth,tagname);
                        if(PRINT_VALUE)
                            chpr += printf(" = ");
                    }
                    if(PRINT_VALUE)
                    {
                        chpr += printf("%lu",entry->length);
                        print_canon_serialno(entry->length);
                    }
                    break;
                case CIFFTAG_CAPTUREDTIME: /* INHEAP subroutine */
                    if((PRINT_SECTION))
                    {
                        print_tag_address(SECTION|ENTRY,entry->offset + fileoffset_base,indent+MEDIUMINDENT,
                                                                                    "@");
                        chpr += printf("<%s> data, %lu entries",fullnameoftag,(entry->length/sizeof(unsigned long)));
                    }
                    max_offset = canon_ct_to_datetime(inptr,byteorder,
                                                tagname,entry->offset + fileoffset_base,
                                                entry->length,indent+MEDIUMINDENT);
                    if((PRINT_SECTION))
                    {
                        print_tag_address(SECTION|ENTRY,entry->offset + fileoffset_base + entry->length - 1,
                                            indent+MEDIUMINDENT,"-");
                        chpr += printf("</%s>",fullnameoftag);
                        POPCOLOR();
                    }
                    break;
                case CIFFTAG_EXPOSUREINFO: /* INHEAP, 3 floats; EC, Tv, Av */
                    if((PRINT_SECTION))
                    {
                        print_tag_address(SECTION,entry->offset + fileoffset_base,indent+MEDIUMINDENT,
                                                                                "@");
                        chpr = printf("<%s> data, %lu entries",fullnameoftag,(entry->length/sizeof(unsigned long)));
                        indent += MEDIUMINDENT;
                    }
                    max_offset = canon_exposureinfo(inptr,byteorder,tagname,
                                                    entry->offset + fileoffset_base,
                                                    entry->length,indent+MEDIUMINDENT,
                                                    summary_entry);
                    indent -= MEDIUMINDENT;
                    if((PRINT_SECTION))
                    {
                        print_tag_address(SECTION,entry->offset + fileoffset_base + entry->length - 1,
                                            indent+MEDIUMINDENT,"-");
                        chpr += printf("</%s>",fullnameoftag);
                        POPCOLOR();
                    }
                    break;
                case CIFFTAG_IMAGEINFO: /* INHEAP subroutine */
                    if((PRINT_SECTION))
                    {
                        print_tag_address(SECTION,entry->offset + fileoffset_base,indent+MEDIUMINDENT,
                                                                                "@");
                        chpr = printf("<%s> data, %lu entries",fullnameoftag,(entry->length/sizeof(unsigned long)));
                    }
                    max_offset = canon_imageinfo(inptr,byteorder,tagname,
                                                    entry->offset + fileoffset_base,
                                                    entry->length,indent+MEDIUMINDENT,
                                                    summary_entry);
                    if((PRINT_SECTION))
                    {
                        print_tag_address(SECTION,entry->offset + fileoffset_base + entry->length - 1,
                                            indent+MEDIUMINDENT,"-");
                        chpr += printf("</%s>",fullnameoftag);
                        POPCOLOR();
                    }
                    break;
                case CIFFTAG_FLASHINFO: /* INREC two floats, flash Guidenumber & Threshold */
                    if((PRINT_VALUE) && (PRINT_SECTION))
                    {
                        indent += 19;
                        chpr = printf("{");
                        chpr = newline(chpr);
                        parindent = charsprinted();
                    }
                    if(PRINT_ENTRY)
                    {
                        float fvalue;

                        entrywidth = strlen("FlashGuideNumber");
                        dirname = dirname ? dirname : QSTRING;
                        print_tag_address(ENTRY,fileoffset_base + 2,indent+MEDIUMINDENT,"@");
                        parindent = charsprinted() - MEDIUMINDENT;
                        if((PRINT_TAGINFO))
                        {
                            if((PRINT_LONGNAMES))
                                chpr += printf("%s.",fullnameoftag);
                            chpr += printf("%-*.*s",entrywidth,entrywidth,"FlashGuideNumber"); 
                        }
                        if((PRINT_VALUE))
                        {
                            fvalue = to_float(entry->length);
                            chpr += printf(" = %f",fvalue);
                        }

                        chpr = newline(chpr);
                        print_tag_address(ENTRY,fileoffset_base + 6,indent+MEDIUMINDENT,"@");
                        if((PRINT_TAGINFO))
                        {
                            if((PRINT_LONGNAMES))
                                chpr += printf("%s.",fullnameoftag);
                            chpr += printf("%-*.*s",entrywidth,entrywidth,
                                                                        "FlashThreshold"); 
                        }
                        if((PRINT_VALUE))
                        {
                            fvalue = to_float(entry->offset);
                            chpr += printf(" = %f",fvalue);
                        }
                    }
                    if(PRINT_SECTION)
                    {
                        chpr = newline(chpr);
                        print_tag_address(ENTRY,fileoffset_base + 9,0,"@");
                        parindent -= charsprinted();
                        putindent(parindent);
                        chpr = printf("}");
                    }
                    break;
                case CIFFTAG_MEASUREDEV:    /* INREC */
                    if(!(PRINT_SECTION) && (PRINT_TAGINFO))
                    {
                        print_tag_address(ENTRY,fileoffset_base + 2,indent+MEDIUMINDENT,"@");
                        chpr = printf("%-*.*s",entrywidth,entrywidth,tagname);
                    }
                    if(PRINT_VALUE)
                    {
                        float fvalue;

                        fvalue = to_float(entry->length);
                        chpr += printf("%f",fvalue);
                    }
                    break;
                case CIFFTAG_RAWIMAGEDATA:  /* INHEAP */
                    if(PRINT_SECTION)
                    {
                        print_tag_address(SECTION|ENTRY,entry->offset + fileoffset_base,
                                            indent,"@");
                        chpr += printf("Start of %s, length %lu\n",nameoftag,entry->length);
                        if((PRINT_VALUE))
                            dumpsection(inptr,entry->offset + fileoffset_base,entry->length,indent + SMALLINDENT);
                        print_tag_address(SECTION|ENTRY,entry->offset + fileoffset_base + entry->length - 1,
                                            indent,"@");
                        chpr += printf("End of %s",nameoftag);
                    }
                    else
                    {
                        print_tag_address(ENTRY,entry->offset + fileoffset_base,
                                                    indent+MEDIUMINDENT,"@");
                        /* pseudo-tags                                */
                        if(PRINT_TAGINFO)
                            chpr += printf("%s.%-*.*s",dirname,CIFFTAGWIDTH,CIFFTAGWIDTH,nameoftag);
                        if((PRINT_ENTRY))
                        {
                            if((PRINT_VALUE))
                            {
                                if(PRINT_BOTH_OFFSET)
                                    chpr += printf(" = @%#lx=%lu",entry->offset + fileoffset_base,
                                            entry->offset + fileoffset_base);
                                else if(PRINT_HEX_OFFSET)
                                    chpr += printf(" = @%#lx",entry->offset + fileoffset_base);
                                else 
                                    chpr += printf(" = @%lu",entry->offset + fileoffset_base);

                                if(fileoffset_base && (PRINT_ENTRY_RELOFFSET))
                                    chpr += printf(" (%lu)",entry->offset);
                            }
                            chpr = newline(chpr);
                            print_tag_address(ENTRY,0,indent+MEDIUMINDENT,"@");
                            if((PRINT_TAGINFO))
                            {
                                tagwidth = CIFFTAGWIDTH - strlen(nameoftag) - 1;
                                chpr += printf("%s.%s.%-*.*s",dirname,nameoftag,tagwidth,
                                                            tagwidth,"length");
                            }
                            if((PRINT_VALUE))
                                chpr += printf(" = %lu",entry->length);
                        }
                    }
                    max_offset = entry->offset + fileoffset_base + entry->length;
                    summary_entry->offset = entry->offset + fileoffset_base;
                    summary_entry->length = entry->length;
                    summary_entry->subfiletype = PRIMARY_TYPE;
                    summary_entry->imageformat = IMGFMT_CRW;
                    summary_entry->compression = is_compressed_crw(inptr,
                                                        entry->offset + fileoffset_base,
                                                        entry->length);
                    summary_entry->entry_lock = lock_number(summary_entry);

                    break;
                case CIFFTAG_JPEGIMAGE: /* INHEAP */
                    if((PRINT_SECTION))
                    {
                        print_tag_address(SECTION|ENTRY,entry->offset + fileoffset_base,
                                                                indent,"@");
                        chpr += printf("Start of %s length %lu\n",nameoftag,entry->length);
                    }
                    summary_entry = new_summary_entry(summary_entry,JPEG_SOI,JPEG_SOI);
                    if(summary_entry)
                        summary_entry->subfiletype = REDUCED_RES_TYPE;
                    marker = read_ushort(inptr,TIFF_MOTOROLA,entry->offset + fileoffset_base);
                    max_offset = process_jpeg_segments(inptr,entry->offset + fileoffset_base,
                                        marker, entry->length,summary_entry,fullnameoftag,
                                        "@",indent+MEDIUMINDENT);
                    if((PRINT_SECTION))
                    {
                        if((status = jpeg_status(0) == JPEG_EARLY_EOI))
                            chpr = newline(chpr);
                        jpeg_status(status);
                        print_tag_address(SECTION|ENTRY,entry->offset + fileoffset_base + entry->length - 1,
                                            indent,"@");
                        chpr += printf("End of %s",nameoftag);
                    }
                    else 
                    {
                        print_tag_address(ENTRY,entry->offset + fileoffset_base,
                                                    indent+MEDIUMINDENT,"@");
                        /* pseudo-tags                                */
                        if((PRINT_TAGINFO))
                            chpr += printf("%s.%-*.*s",dirname,CIFFTAGWIDTH,CIFFTAGWIDTH,nameoftag);
                        if((PRINT_ENTRY))
                        {
                            if((PRINT_VALUE))
                            {
                                if(PRINT_BOTH_OFFSET)
                                    chpr += printf(" = @%#lx/%lu",entry->offset + fileoffset_base,
                                            entry->offset + fileoffset_base);
                                else if(PRINT_HEX_OFFSET)
                                    chpr += printf(" = @%#lx",entry->offset + fileoffset_base);
                                else 
                                    chpr += printf(" = @%lu",entry->offset + fileoffset_base);

                                if(fileoffset_base && (PRINT_ENTRY_RELOFFSET))
                                    chpr += printf(" (%lu)",entry->offset);
                            }
                            chpr = newline(chpr);
                            print_tag_address(ENTRY,0,indent+MEDIUMINDENT,"@");
                            if((PRINT_TAGINFO))
                            {
                                tagwidth = CIFFTAGWIDTH - strlen(nameoftag) - 1;
                                chpr += printf("%s.%s.%-*.*s",dirname,nameoftag,tagwidth,
                                                    tagwidth,"length");
                            }
                            if((PRINT_VALUE))
                                chpr += printf(" = %lu",entry->length);
                        }
                    }
                    print_jpeg_status();
                    break;
                case CIFFTAG_JPEGTHUMBNAIL: /* INHEAP */
                    if((PRINT_SECTION))
                    {
                        print_tag_address(SECTION|ENTRY,entry->offset + fileoffset_base,
                                            indent,"-");
                        chpr += printf("Start of %s length %lu\n",nameoftag,entry->length);
                    }
                    summary_entry = new_summary_entry(summary_entry,FILEFMT_CIFF,IMGFMT_JPEG);
                    if(summary_entry)
                        summary_entry->subfiletype = THUMBNAIL_TYPE;
                    marker = read_ushort(inptr,TIFF_MOTOROLA,entry->offset + fileoffset_base);
                    max_offset = process_jpeg_segments(inptr,entry->offset + fileoffset_base,marker,
                                                        entry->length,summary_entry,fullnameoftag,
                                                        "@",indent+MEDIUMINDENT);
                    if((PRINT_SECTION))
                    {
                        if((status = jpeg_status(0) == JPEG_EARLY_EOI))
                            chpr = newline(chpr);
                        jpeg_status(status);
                        print_tag_address(SECTION|ENTRY,entry->offset + fileoffset_base + entry->length - 1,
                                            indent,"-");
                        chpr += printf("End of %s",nameoftag);
                    }
                    else
                    {
                        print_tag_address(ENTRY,entry->offset + fileoffset_base,
                                                    indent+MEDIUMINDENT,"@");
                        /* pseudo-tags                                */
                        if((PRINT_TAGINFO))
                            chpr += printf("%s.%-*.*s",dirname,CIFFTAGWIDTH,CIFFTAGWIDTH,nameoftag);
                        if((PRINT_ENTRY))
                        {
                            if((PRINT_VALUE))
                            {
                                if(PRINT_BOTH_OFFSET)
                                    chpr += printf(" = @%#lx/%lu",entry->offset + fileoffset_base,
                                            entry->offset + fileoffset_base);
                                else if(PRINT_HEX_OFFSET)
                                    chpr += printf(" = @%#lx",entry->offset + fileoffset_base);
                                else 
                                    chpr += printf(" = @%lu",entry->offset + fileoffset_base);

                                if(fileoffset_base && (PRINT_ENTRY_RELOFFSET))
                                    chpr += printf(" (%lu)",entry->offset);
                            }
                                    chpr = newline(chpr);
                            print_tag_address(ENTRY,0,indent+MEDIUMINDENT,"@");
                            if((PRINT_TAGINFO))
                            {
                                tagwidth = CIFFTAGWIDTH - strlen(nameoftag) - 1;
                                chpr += printf("%s.%s.%-*.*s",dirname,nameoftag,tagwidth,
                                                tagwidth,"length");
                            }
                            if((PRINT_VALUE))
                                chpr += printf(" = %lu",entry->length);
                        }
                    }
                    print_jpeg_status();
                    break;
                case CIFFTAG_IMAGEDESCRIPTION:  /* SUBDIR             */
                    endofdir = entry->offset+fileoffset_base+entry->length;
                    if((PRINT_SECTION))
                        tagname = fullnameoftag;
                    max_offset = process_ciff_dir(inptr,entry->offset + fileoffset_base,
                                                    endofdir,CNULL,tagname,summary_entry,
                                                    byteorder,level+1,indent+MEDIUMINDENT);
                    break;
                case CIFFTAG_CAMERAOBJECT:  /* SUBDIR                 */
                    endofdir = entry->offset+fileoffset_base+entry->length;
                    if((PRINT_SECTION))
                        tagname = fullnameoftag;
                    max_offset = process_ciff_dir(inptr,entry->offset + fileoffset_base,
                                                    endofdir,CNULL,tagname,summary_entry,
                                                    byteorder,level+1,indent+MEDIUMINDENT);
                    break;
                case CIFFTAG_SHOOTINGRECORD:    /* SUBDIR             */
                    endofdir = entry->offset+fileoffset_base+entry->length;
                    if((PRINT_SECTION))
                        tagname = fullnameoftag;
                    max_offset = process_ciff_dir(inptr,entry->offset + fileoffset_base,
                                                    endofdir,CNULL,tagname,summary_entry,
                                                    byteorder,level+1,indent+MEDIUMINDENT);
                    break;
                case CIFFTAG_MEASUREDINFO:  /* SUBDIR                 */
                    endofdir = entry->offset+fileoffset_base+entry->length;
                    if((PRINT_SECTION))
                        tagname = fullnameoftag;
                    max_offset = process_ciff_dir(inptr,entry->offset + fileoffset_base,
                                                    endofdir,CNULL,tagname,summary_entry,
                                                    byteorder,level+1,indent+MEDIUMINDENT);
                    break;
                case CIFFTAG_CAMERASPECIFICATION:   /* SUBDIR         */
                    endofdir = entry->offset+fileoffset_base+entry->length;
                    if((PRINT_SECTION))
                        tagname = fullnameoftag;
                    max_offset = process_ciff_dir(inptr,entry->offset + fileoffset_base,
                                                    endofdir,CNULL,tagname,summary_entry,
                                                    byteorder,level+1,indent+MEDIUMINDENT);
                    break;
                case CIFFTAG_IMAGEPROPS:    /* SUBDIR                 */
                    endofdir = entry->offset+fileoffset_base+entry->length;
                    if((PRINT_SECTION))
                        tagname = fullnameoftag;
                    max_offset = process_ciff_dir(inptr,entry->offset + fileoffset_base,
                                                    endofdir,CNULL,tagname,summary_entry,
                                                    byteorder,level+1,indent);
                    break;
                case CIFFTAG_EXIFINFORMATION:   /* SUBDIR             */
                    endofdir = entry->offset+fileoffset_base+entry->length;
                    if((PRINT_SECTION))
                        tagname = fullnameoftag;
                    PUSHCOLOR(EXIF_COLOR);
                    max_offset = process_ciff_dir(inptr,entry->offset + fileoffset_base,
                                                    endofdir,CNULL,tagname,summary_entry,
                                                    byteorder,level+1,indent+MEDIUMINDENT);
                    POPCOLOR();
                    break;
#if 0
                case CIFFTAG_FILENUMBER: break;   /* no interp reqd, use default */
                case CIFFTAG_DECODERTABLE: break;  /* 4 values; unknown, use default */
                case CIFFTAG_COMPONENTVERSION: break; /* unknown, use default */
                case CIFFTAG_RELEASESETTING: break; /* spec lists but doesn't define */
                case CIFFTAG_0X1834: break; /* seen in ExifInformation; unknown, use default */  
                case CIFFTAG_0x183B: break; /* seen in ExifInformation; unknown, use default */
                case CIFFTAG_RECORDID: break;   /* no conversion reqd */
#endif
                case CIFFTAG_NULLRECORD:    /* fall thru to default   */
                case CIFFTAG_FREEBYTES:     /* fall thru to default   */
                default:
                    if(location == CIFF_INHEAP)
                    {
                        if((PRINT_SECTION))
                            indent += MEDIUMINDENT;
                        max_offset = print_ciffinheapdata(inptr,byteorder,dirname,
                                nameoftag,entry->offset + fileoffset_base,entry->length,
                                entry->type&CIFF_FORMATMASK,indent);
                    }
                    else if(location == CIFF_INREC)
                    {
                        if(!(PRINT_SECTION) && (PRINT_TAGINFO))
                        {
                            print_tag_address(ENTRY,fileoffset_base + 2,indent+MEDIUMINDENT,"@");
                            chpr += printf("%-*.*s",entrywidth,entrywidth,tagname); 
                            if(PRINT_VALUE)
                                chpr += printf(" = ");
                        }
                        max_offset = print_ciffinrecdata(inptr,byteorder,tagname,
                                    fileoffset_base + 2,8,entry->type&CIFF_FORMATMASK,
                                    indent+MEDIUMINDENT);
                    }
                    else
                        max_offset = 0;
                    break;
            }
            chpr = newline(chpr);
            if(fullnameoftag)
                free(fullnameoftag);
            if(nameoftag)
                free(nameoftag);
            fullnameoftag = nameoftag = CNULL;
        }
    }
    return(max_offset);
}

/* This checks for compression in "raw" CRW files in the manner       */
/* suggested by Dave Coffin's "dcraw" program. The ImageInfo data     */
/* doesn't seem to say.                                               */
/* A return value of 0 indicates uncompressed data. A return value of */
/* 1 indicates compressed.                                            */

int
is_compressed_crw(FILE *inptr,unsigned long start_offset,unsigned long imagelength)
{
    int is_compressed = 0;
    int maxcheck = 16384;   /* 0x4000                                 */
    int count = 0;
    int ch,lastch;

    if((inptr) && (fseek(inptr,start_offset + 512,SEEK_SET) != -1))
    {
        if(imagelength < maxcheck)
            maxcheck = imagelength;
        lastch = 0;
        while(((ch = fgetc(inptr)) != EOF) && (count++ < maxcheck))
        {
            if(lastch == 0xff)
            {
                is_compressed = 1;
                if(ch)
                {
                    is_compressed = 0;
                    break;
                }
            }
            lastch = ch;
        }
    }
    return(is_compressed);
}

/* Print CIFF header information if options permit. Return 0 if       */
/* information is valid to print, or -1 if the header is invalid      */

int
print_ciff_header(struct fileheader *header,unsigned long section_id)
{
    struct ciff_header *ciffheader = NULL;
    unsigned short byteorder = 0;
    int status = -1;
    int chpr = 0;

    if(header)
    {
        byteorder = header->file_marker;
        if(header->ciff_header)
        {
            ciffheader = header->ciff_header;
            if(header->probe_magic == PROBE_CIFFMAGIC)
            {
                if(Print_options & section_id)
                    chpr += printf("CIFF");
                switch(byteorder)
                {
                case TIFF_INTEL:
                case TIFF_MOTOROLA:
                    if(Print_options & section_id)
                    {
                        print_byteorder(byteorder,1);
                        chpr += printf(" \"%s%s\", version %#lx, heap offset = %#lx/%lu",
                                ciffheader->type,ciffheader->subtype,
                                ciffheader->version,ciffheader->headerlength,
                                ciffheader->headerlength);
                    }
                    status = 0;
                    break;
                default:
                    if(Print_options & section_id)
                        chpr += printf("INVALID CIFF BYTEORDER ");
                        print_byteorder(byteorder,1);
                        chpr += printf(" (CIFF type \"%s%s\")",ciffheader->type,
                                                        ciffheader->subtype);
                    break;
                }
            }
            else if(Print_options & section_id)
            {
                    chpr += printf("INVALID CIFF IDENTIFIER ");
                    print_magic(header->probe_magic,4);
                    chpr += printf(" byteorder ");
                    print_byteorder(byteorder,1);
            }
        }
        else
            fprintf(stderr,"%s: null ciffheader to print_ciff_header()\n",Progname);
    }
    else
        fprintf(stderr,"%s: null fileheader to print_ciff_header()\n",Progname);
    chpr = newline(chpr);
    return(status);
}

struct ciff_header *
read_ciffheader(FILE *inptr,unsigned short byteorder,unsigned long offset)
{
    static struct ciff_header header;
    struct ciff_header *headerptr = NULL;
    unsigned char *string;

    memset(&header,0,sizeof(struct ciff_header));
    if(inptr)
    {
        header.byteorder = read_ushort(inptr,byteorder,offset);
        header.headerlength = read_ulong(inptr,byteorder,HERE);
        string = read_bytes(inptr,4,HERE);
        if(string)
        {
            strncpy(header.type,(char *)string,4);
            string = read_bytes(inptr,4,HERE);
            if(string)
                strncpy(header.subtype,(char *)string,4);
        }
        header.version = read_ulong(inptr,byteorder,HERE);
        header.reserved1 = read_ulong(inptr,byteorder,HERE);
        header.reserved2 = read_ulong(inptr,byteorder,HERE);
        if((header.byteorder == byteorder) &&
                (strncmp(header.type,"HEAP",4) == 0) &&
                                    (header.headerlength > 0))
        {
            headerptr = &header;
        }
    }
    return(headerptr);
}


struct ciff_direntry *
read_ciff_direntry(FILE *inptr,struct ciff_direntry *entry,unsigned short byteorder,unsigned long offset)
{
    if(inptr)
    {
        if(entry == NULL)
            entry = (struct ciff_direntry *)malloc(sizeof(struct ciff_direntry));
        if(entry)
        {
            memset(entry,0,sizeof(struct ciff_direntry));
            entry->type = read_ushort(inptr,byteorder,offset);
            entry->length = read_ulong(inptr,byteorder,offset + 2);
            entry->offset = read_ulong(inptr,byteorder,offset + 6);
        }
    }
    return(entry);
}