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
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
|
//---------------------------------------------------------------------------------
//
// Little Color Management System
// Copyright (c) 1998-2018 Marti Maria Saguer
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the Software
// is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//---------------------------------------------------------------------------------
//
#include "lcms2_internal.h"
// IT8.7 / CGATS.17-200x handling -----------------------------------------------------------------------------
#define MAXID 128 // Max length of identifier
#define MAXSTR 1024 // Max length of string
#define MAXTABLES 255 // Max Number of tables in a single stream
#define MAXINCLUDE 20 // Max number of nested includes
#define DEFAULT_DBL_FORMAT "%.10g" // Double formatting
#ifdef CMS_IS_WINDOWS_
# include <io.h>
# define DIR_CHAR '\\'
#else
# define DIR_CHAR '/'
#endif
// Symbols
typedef enum {
SUNDEFINED,
SINUM, // Integer
SDNUM, // Real
SIDENT, // Identifier
SSTRING, // string
SCOMMENT, // comment
SEOLN, // End of line
SEOF, // End of stream
SSYNERROR, // Syntax error found on stream
// Keywords
SBEGIN_DATA,
SBEGIN_DATA_FORMAT,
SEND_DATA,
SEND_DATA_FORMAT,
SKEYWORD,
SDATA_FORMAT_ID,
SINCLUDE
} SYMBOL;
// How to write the value
typedef enum {
WRITE_UNCOOKED,
WRITE_STRINGIFY,
WRITE_HEXADECIMAL,
WRITE_BINARY,
WRITE_PAIR
} WRITEMODE;
// Linked list of variable names
typedef struct _KeyVal {
struct _KeyVal* Next;
char* Keyword; // Name of variable
struct _KeyVal* NextSubkey; // If key is a dictionary, points to the next item
char* Subkey; // If key is a dictionary, points to the subkey name
char* Value; // Points to value
WRITEMODE WriteAs; // How to write the value
} KEYVALUE;
// Linked list of memory chunks (Memory sink)
typedef struct _OwnedMem {
struct _OwnedMem* Next;
void * Ptr; // Point to value
} OWNEDMEM;
// Suballocator
typedef struct _SubAllocator {
cmsUInt8Number* Block;
cmsUInt32Number BlockSize;
cmsUInt32Number Used;
} SUBALLOCATOR;
// Table. Each individual table can hold properties and rows & cols
typedef struct _Table {
char SheetType[MAXSTR]; // The first row of the IT8 (the type)
int nSamples, nPatches; // Cols, Rows
int SampleID; // Pos of ID
KEYVALUE* HeaderList; // The properties
char** DataFormat; // The binary stream descriptor
char** Data; // The binary stream
} TABLE;
// File stream being parsed
typedef struct _FileContext {
char FileName[cmsMAX_PATH]; // File name if being read from file
FILE* Stream; // File stream or NULL if holded in memory
} FILECTX;
// This struct hold all information about an open IT8 handler.
typedef struct {
cmsUInt32Number TablesCount; // How many tables in this stream
cmsUInt32Number nTable; // The actual table
TABLE Tab[MAXTABLES];
// Memory management
OWNEDMEM* MemorySink; // The storage backend
SUBALLOCATOR Allocator; // String suballocator -- just to keep it fast
// Parser state machine
SYMBOL sy; // Current symbol
int ch; // Current character
cmsInt32Number inum; // integer value
cmsFloat64Number dnum; // real value
char id[MAXID]; // identifier
char str[MAXSTR]; // string
// Allowed keywords & datasets. They have visibility on whole stream
KEYVALUE* ValidKeywords;
KEYVALUE* ValidSampleID;
char* Source; // Points to loc. being parsed
cmsInt32Number lineno; // line counter for error reporting
FILECTX* FileStack[MAXINCLUDE]; // Stack of files being parsed
cmsInt32Number IncludeSP; // Include Stack Pointer
char* MemoryBlock; // The stream if holded in memory
char DoubleFormatter[MAXID];// Printf-like 'cmsFloat64Number' formatter
} cmsIT8;
// The stream for save operations
typedef struct {
FILE* stream; // For save-to-file behaviour
cmsUInt8Number* Base;
cmsUInt8Number* Ptr; // For save-to-mem behaviour
cmsUInt32Number Used;
cmsUInt32Number Max;
} SAVESTREAM;
// ------------------------------------------------------ cmsIT8 parsing routines
// A keyword
typedef struct {
const char *id;
SYMBOL sy;
} KEYWORD;
// The keyword->symbol translation table. Sorting is required.
static const KEYWORD TabKeys[] = {
{"$INCLUDE", SINCLUDE}, // This is an extension!
{".INCLUDE", SINCLUDE}, // This is an extension!
{"BEGIN_DATA", SBEGIN_DATA },
{"BEGIN_DATA_FORMAT", SBEGIN_DATA_FORMAT },
{"DATA_FORMAT_IDENTIFIER", SDATA_FORMAT_ID},
{"END_DATA", SEND_DATA},
{"END_DATA_FORMAT", SEND_DATA_FORMAT},
{"KEYWORD", SKEYWORD}
};
#define NUMKEYS (sizeof(TabKeys)/sizeof(KEYWORD))
// Predefined properties
// A property
typedef struct {
const char *id; // The identifier
WRITEMODE as; // How is supposed to be written
} PROPERTY;
static PROPERTY PredefinedProperties[] = {
{"NUMBER_OF_FIELDS", WRITE_UNCOOKED}, // Required - NUMBER OF FIELDS
{"NUMBER_OF_SETS", WRITE_UNCOOKED}, // Required - NUMBER OF SETS
{"ORIGINATOR", WRITE_STRINGIFY}, // Required - Identifies the specific system, organization or individual that created the data file.
{"FILE_DESCRIPTOR", WRITE_STRINGIFY}, // Required - Describes the purpose or contents of the data file.
{"CREATED", WRITE_STRINGIFY}, // Required - Indicates date of creation of the data file.
{"DESCRIPTOR", WRITE_STRINGIFY}, // Required - Describes the purpose or contents of the data file.
{"DIFFUSE_GEOMETRY", WRITE_STRINGIFY}, // The diffuse geometry used. Allowed values are "sphere" or "opal".
{"MANUFACTURER", WRITE_STRINGIFY},
{"MANUFACTURE", WRITE_STRINGIFY}, // Some broken Fuji targets does store this value
{"PROD_DATE", WRITE_STRINGIFY}, // Identifies year and month of production of the target in the form yyyy:mm.
{"SERIAL", WRITE_STRINGIFY}, // Uniquely identifies individual physical target.
{"MATERIAL", WRITE_STRINGIFY}, // Identifies the material on which the target was produced using a code
// uniquely identifying th e material. This is intend ed to be used for IT8.7
// physical targets only (i.e . IT8.7/1 a nd IT8.7/2).
{"INSTRUMENTATION", WRITE_STRINGIFY}, // Used to report the specific instrumentation used (manufacturer and
// model number) to generate the data reported. This data will often
// provide more information about the particular data collected than an
// extensive list of specific details. This is particularly important for
// spectral data or data derived from spectrophotometry.
{"MEASUREMENT_SOURCE", WRITE_STRINGIFY}, // Illumination used for spectral measurements. This data helps provide
// a guide to the potential for issues of paper fluorescence, etc.
{"PRINT_CONDITIONS", WRITE_STRINGIFY}, // Used to define the characteristics of the printed sheet being reported.
// Where standard conditions have been defined (e.g., SWOP at nominal)
// named conditions may suffice. Otherwise, detailed information is
// needed.
{"SAMPLE_BACKING", WRITE_STRINGIFY}, // Identifies the backing material used behind the sample during
// measurement. Allowed values are "black", "white", or {"na".
{"CHISQ_DOF", WRITE_STRINGIFY}, // Degrees of freedom associated with the Chi squared statistic
// below properties are new in recent specs:
{"MEASUREMENT_GEOMETRY", WRITE_STRINGIFY}, // The type of measurement, either reflection or transmission, should be indicated
// along with details of the geometry and the aperture size and shape. For example,
// for transmission measurements it is important to identify 0/diffuse, diffuse/0,
// opal or integrating sphere, etc. For reflection it is important to identify 0/45,
// 45/0, sphere (specular included or excluded), etc.
{"FILTER", WRITE_STRINGIFY}, // Identifies the use of physical filter(s) during measurement. Typically used to
// denote the use of filters such as none, D65, Red, Green or Blue.
{"POLARIZATION", WRITE_STRINGIFY}, // Identifies the use of a physical polarization filter during measurement. Allowed
// values are {"yes", "white", "none" or "na".
{"WEIGHTING_FUNCTION", WRITE_PAIR}, // Indicates such functions as: the CIE standard observer functions used in the
// calculation of various data parameters (2 degree and 10 degree), CIE standard
// illuminant functions used in the calculation of various data parameters (e.g., D50,
// D65, etc.), density status response, etc. If used there shall be at least one
// name-value pair following the WEIGHTING_FUNCTION tag/keyword. The first attribute
// in the set shall be {"name" and shall identify the particular parameter used.
// The second shall be {"value" and shall provide the value associated with that name.
// For ASCII data, a string containing the Name and Value attribute pairs shall follow
// the weighting function keyword. A semi-colon separates attribute pairs from each
// other and within the attribute the name and value are separated by a comma.
{"COMPUTATIONAL_PARAMETER", WRITE_PAIR}, // Parameter that is used in computing a value from measured data. Name is the name
// of the calculation, parameter is the name of the parameter used in the calculation
// and value is the value of the parameter.
{"TARGET_TYPE", WRITE_STRINGIFY}, // The type of target being measured, e.g. IT8.7/1, IT8.7/3, user defined, etc.
{"COLORANT", WRITE_STRINGIFY}, // Identifies the colorant(s) used in creating the target.
{"TABLE_DESCRIPTOR", WRITE_STRINGIFY}, // Describes the purpose or contents of a data table.
{"TABLE_NAME", WRITE_STRINGIFY} // Provides a short name for a data table.
};
#define NUMPREDEFINEDPROPS (sizeof(PredefinedProperties)/sizeof(PROPERTY))
// Predefined sample types on dataset
static const char* PredefinedSampleID[] = {
"SAMPLE_ID", // Identifies sample that data represents
"STRING", // Identifies label, or other non-machine readable value.
// Value must begin and end with a " symbol
"CMYK_C", // Cyan component of CMYK data expressed as a percentage
"CMYK_M", // Magenta component of CMYK data expressed as a percentage
"CMYK_Y", // Yellow component of CMYK data expressed as a percentage
"CMYK_K", // Black component of CMYK data expressed as a percentage
"D_RED", // Red filter density
"D_GREEN", // Green filter density
"D_BLUE", // Blue filter density
"D_VIS", // Visual filter density
"D_MAJOR_FILTER", // Major filter d ensity
"RGB_R", // Red component of RGB data
"RGB_G", // Green component of RGB data
"RGB_B", // Blue com ponent of RGB data
"SPECTRAL_NM", // Wavelength of measurement expressed in nanometers
"SPECTRAL_PCT", // Percentage reflectance/transmittance
"SPECTRAL_DEC", // Reflectance/transmittance
"XYZ_X", // X component of tristimulus data
"XYZ_Y", // Y component of tristimulus data
"XYZ_Z", // Z component of tristimulus data
"XYY_X", // x component of chromaticity data
"XYY_Y", // y component of chromaticity data
"XYY_CAPY", // Y component of tristimulus data
"LAB_L", // L* component of Lab data
"LAB_A", // a* component of Lab data
"LAB_B", // b* component of Lab data
"LAB_C", // C*ab component of Lab data
"LAB_H", // hab component of Lab data
"LAB_DE", // CIE dE
"LAB_DE_94", // CIE dE using CIE 94
"LAB_DE_CMC", // dE using CMC
"LAB_DE_2000", // CIE dE using CIE DE 2000
"MEAN_DE", // Mean Delta E (LAB_DE) of samples compared to batch average
// (Used for data files for ANSI IT8.7/1 and IT8.7/2 targets)
"STDEV_X", // Standard deviation of X (tristimulus data)
"STDEV_Y", // Standard deviation of Y (tristimulus data)
"STDEV_Z", // Standard deviation of Z (tristimulus data)
"STDEV_L", // Standard deviation of L*
"STDEV_A", // Standard deviation of a*
"STDEV_B", // Standard deviation of b*
"STDEV_DE", // Standard deviation of CIE dE
"CHI_SQD_PAR"}; // The average of the standard deviations of L*, a* and b*. It is
// used to derive an estimate of the chi-squared parameter which is
// recommended as the predictor of the variability of dE
#define NUMPREDEFINEDSAMPLEID (sizeof(PredefinedSampleID)/sizeof(char *))
//Forward declaration of some internal functions
static void* AllocChunk(cmsContext ContextID, cmsIT8* it8, cmsUInt32Number size);
// Checks whatever c is a separator
static
cmsBool isseparator(int c)
{
return (c == ' ') || (c == '\t') ;
}
// Checks whatever c is a valid identifier char
static
cmsBool ismiddle(int c)
{
return (!isseparator(c) && (c != '#') && (c !='\"') && (c != '\'') && (c > 32) && (c < 127));
}
// Checks whatsever c is a valid identifier middle char.
static
cmsBool isidchar(int c)
{
return isalnum(c) || ismiddle(c);
}
// Checks whatsever c is a valid identifier first char.
static
cmsBool isfirstidchar(int c)
{
return !isdigit(c) && ismiddle(c);
}
// Guess whether the supplied path looks like an absolute path
static
cmsBool isabsolutepath(const char *path)
{
char ThreeChars[4];
if(path == NULL)
return FALSE;
if (path[0] == 0)
return FALSE;
strncpy(ThreeChars, path, 3);
ThreeChars[3] = 0;
if(ThreeChars[0] == DIR_CHAR)
return TRUE;
#ifdef CMS_IS_WINDOWS_
if (isalpha((int) ThreeChars[0]) && ThreeChars[1] == ':')
return TRUE;
#endif
return FALSE;
}
// Makes a file path based on a given reference path
// NOTE: this function doesn't check if the path exists or even if it's legal
static
cmsBool BuildAbsolutePath(const char *relPath, const char *basePath, char *buffer, cmsUInt32Number MaxLen)
{
char *tail;
cmsUInt32Number len;
// Already absolute?
if (isabsolutepath(relPath)) {
strncpy(buffer, relPath, MaxLen);
buffer[MaxLen-1] = 0;
return TRUE;
}
// No, search for last
strncpy(buffer, basePath, MaxLen);
buffer[MaxLen-1] = 0;
tail = strrchr(buffer, DIR_CHAR);
if (tail == NULL) return FALSE; // Is not absolute and has no separators??
len = (cmsUInt32Number) (tail - buffer);
if (len >= MaxLen) return FALSE;
// No need to assure zero terminator over here
strncpy(tail + 1, relPath, MaxLen - len);
return TRUE;
}
// Make sure no exploit is being even tried
static
const char* NoMeta(const char* str)
{
if (strchr(str, '%') != NULL)
return "**** CORRUPTED FORMAT STRING ***";
return str;
}
// Syntax error
static
cmsBool SynError(cmsContext ContextID, cmsIT8* it8, const char *Txt, ...)
{
char Buffer[256], ErrMsg[1024];
va_list args;
va_start(args, Txt);
vsnprintf(Buffer, 255, Txt, args);
Buffer[255] = 0;
va_end(args);
snprintf(ErrMsg, 1023, "%s: Line %d, %s", it8->FileStack[it8 ->IncludeSP]->FileName, it8->lineno, Buffer);
ErrMsg[1023] = 0;
it8->sy = SSYNERROR;
cmsSignalError(ContextID, cmsERROR_CORRUPTION_DETECTED, "%s", ErrMsg);
return FALSE;
}
// Check if current symbol is same as specified. issue an error else.
static
cmsBool Check(cmsContext ContextID, cmsIT8* it8, SYMBOL sy, const char* Err)
{
if (it8 -> sy != sy)
return SynError(ContextID, it8, NoMeta(Err));
return TRUE;
}
// Read Next character from stream
static
void NextCh(cmsIT8* it8)
{
if (it8 -> FileStack[it8 ->IncludeSP]->Stream) {
it8 ->ch = fgetc(it8 ->FileStack[it8 ->IncludeSP]->Stream);
if (feof(it8 -> FileStack[it8 ->IncludeSP]->Stream)) {
if (it8 ->IncludeSP > 0) {
fclose(it8 ->FileStack[it8->IncludeSP--]->Stream);
it8 -> ch = ' '; // Whitespace to be ignored
} else
it8 ->ch = 0; // EOF
}
}
else {
it8->ch = *it8->Source;
if (it8->ch) it8->Source++;
}
}
// Try to see if current identifier is a keyword, if so return the referred symbol
static
SYMBOL BinSrchKey(const char *id)
{
int l = 1;
int r = NUMKEYS;
int x, res;
while (r >= l)
{
x = (l+r)/2;
res = cmsstrcasecmp(id, TabKeys[x-1].id);
if (res == 0) return TabKeys[x-1].sy;
if (res < 0) r = x - 1;
else l = x + 1;
}
return SUNDEFINED;
}
// 10 ^n
static
cmsFloat64Number xpow10(int n)
{
return pow(10, (cmsFloat64Number) n);
}
// Reads a Real number, tries to follow from integer number
static
void ReadReal(cmsIT8* it8, cmsInt32Number inum)
{
it8->dnum = (cmsFloat64Number)inum;
while (isdigit(it8->ch)) {
it8->dnum = (cmsFloat64Number)it8->dnum * 10.0 + (cmsFloat64Number)(it8->ch - '0');
NextCh(it8);
}
if (it8->ch == '.') { // Decimal point
cmsFloat64Number frac = 0.0; // fraction
int prec = 0; // precision
NextCh(it8); // Eats dec. point
while (isdigit(it8->ch)) {
frac = frac * 10.0 + (cmsFloat64Number)(it8->ch - '0');
prec++;
NextCh(it8);
}
it8->dnum = it8->dnum + (frac / xpow10(prec));
}
// Exponent, example 34.00E+20
if (toupper(it8->ch) == 'E') {
cmsInt32Number e;
cmsInt32Number sgn;
NextCh(it8); sgn = 1;
if (it8->ch == '-') {
sgn = -1; NextCh(it8);
}
else
if (it8->ch == '+') {
sgn = +1;
NextCh(it8);
}
e = 0;
while (isdigit(it8->ch)) {
cmsInt32Number digit = (it8->ch - '0');
if ((cmsFloat64Number)e * 10.0 + (cmsFloat64Number)digit < (cmsFloat64Number)+2147483647.0)
e = e * 10 + digit;
NextCh(it8);
}
e = sgn*e;
it8->dnum = it8->dnum * xpow10(e);
}
}
// Parses a float number
// This can not call directly atof because it uses locale dependent
// parsing, while CCMX files always use . as decimal separator
static
cmsFloat64Number ParseFloatNumber(const char *Buffer)
{
cmsFloat64Number dnum = 0.0;
int sign = 1;
// keep safe
if (Buffer == NULL) return 0.0;
if (*Buffer == '-' || *Buffer == '+') {
sign = (*Buffer == '-') ? -1 : 1;
Buffer++;
}
while (*Buffer && isdigit((int)*Buffer)) {
dnum = dnum * 10.0 + (*Buffer - '0');
if (*Buffer) Buffer++;
}
if (*Buffer == '.') {
cmsFloat64Number frac = 0.0; // fraction
int prec = 0; // precision
if (*Buffer) Buffer++;
while (*Buffer && isdigit((int)*Buffer)) {
frac = frac * 10.0 + (*Buffer - '0');
prec++;
if (*Buffer) Buffer++;
}
dnum = dnum + (frac / xpow10(prec));
}
// Exponent, example 34.00E+20
if (*Buffer && toupper(*Buffer) == 'E') {
int e;
int sgn;
if (*Buffer) Buffer++;
sgn = 1;
if (*Buffer == '-') {
sgn = -1;
if (*Buffer) Buffer++;
}
else
if (*Buffer == '+') {
sgn = +1;
if (*Buffer) Buffer++;
}
e = 0;
while (*Buffer && isdigit((int)*Buffer)) {
cmsInt32Number digit = (*Buffer - '0');
if ((cmsFloat64Number)e * 10.0 + digit < (cmsFloat64Number)+2147483647.0)
e = e * 10 + digit;
if (*Buffer) Buffer++;
}
e = sgn*e;
dnum = dnum * xpow10(e);
}
return sign * dnum;
}
// Reads next symbol
static
void InSymbol(cmsContext ContextID, cmsIT8* it8)
{
register char *idptr;
register int k;
SYMBOL key;
int sng;
do {
while (isseparator(it8->ch))
NextCh(it8);
if (isfirstidchar(it8->ch)) { // Identifier
k = 0;
idptr = it8->id;
do {
if (++k < MAXID) *idptr++ = (char) it8->ch;
NextCh(it8);
} while (isidchar(it8->ch));
*idptr = '\0';
key = BinSrchKey(it8->id);
if (key == SUNDEFINED) it8->sy = SIDENT;
else it8->sy = key;
}
else // Is a number?
if (isdigit(it8->ch) || it8->ch == '.' || it8->ch == '-' || it8->ch == '+')
{
int sign = 1;
if (it8->ch == '-') {
sign = -1;
NextCh(it8);
}
it8->inum = 0;
it8->sy = SINUM;
if (it8->ch == '0') { // 0xnnnn (Hexa) or 0bnnnn (Binary)
NextCh(it8);
if (toupper(it8->ch) == 'X') {
int j;
NextCh(it8);
while (isxdigit(it8->ch))
{
it8->ch = toupper(it8->ch);
if (it8->ch >= 'A' && it8->ch <= 'F') j = it8->ch -'A'+10;
else j = it8->ch - '0';
if ((cmsFloat64Number) it8->inum * 16.0 + (cmsFloat64Number) j > (cmsFloat64Number)+2147483647.0)
{
SynError(ContextID, it8, "Invalid hexadecimal number");
return;
}
it8->inum = it8->inum * 16 + j;
NextCh(it8);
}
return;
}
if (toupper(it8->ch) == 'B') { // Binary
int j;
NextCh(it8);
while (it8->ch == '0' || it8->ch == '1')
{
j = it8->ch - '0';
if ((cmsFloat64Number) it8->inum * 2.0 + j > (cmsFloat64Number)+2147483647.0)
{
SynError(ContextID, it8, "Invalid binary number");
return;
}
it8->inum = it8->inum * 2 + j;
NextCh(it8);
}
return;
}
}
while (isdigit(it8->ch)) {
cmsInt32Number digit = (it8->ch - '0');
if ((cmsFloat64Number) it8->inum * 10.0 + (cmsFloat64Number) digit > (cmsFloat64Number) +2147483647.0) {
ReadReal(it8, it8->inum);
it8->sy = SDNUM;
it8->dnum *= sign;
return;
}
it8->inum = it8->inum * 10 + digit;
NextCh(it8);
}
if (it8->ch == '.') {
ReadReal(it8, it8->inum);
it8->sy = SDNUM;
it8->dnum *= sign;
return;
}
it8 -> inum *= sign;
// Special case. Numbers followed by letters are taken as identifiers
if (isidchar(it8 ->ch)) {
if (it8 ->sy == SINUM) {
snprintf(it8->id, 127, "%d", it8->inum);
}
else {
snprintf(it8->id, 127, it8 ->DoubleFormatter, it8->dnum);
}
k = (int) strlen(it8 ->id);
idptr = it8 ->id + k;
do {
if (++k < MAXID) *idptr++ = (char) it8->ch;
NextCh(it8);
} while (isidchar(it8->ch));
*idptr = '\0';
it8->sy = SIDENT;
}
return;
}
else
switch ((int) it8->ch) {
// EOF marker -- ignore it
case '\x1a':
NextCh(it8);
break;
// Eof stream markers
case 0:
case -1:
it8->sy = SEOF;
break;
// Next line
case '\r':
NextCh(it8);
if (it8 ->ch == '\n')
NextCh(it8);
it8->sy = SEOLN;
it8->lineno++;
break;
case '\n':
NextCh(it8);
it8->sy = SEOLN;
it8->lineno++;
break;
// Comment
case '#':
NextCh(it8);
while (it8->ch && it8->ch != '\n' && it8->ch != '\r')
NextCh(it8);
it8->sy = SCOMMENT;
break;
// String.
case '\'':
case '\"':
idptr = it8->str;
sng = it8->ch;
k = 0;
NextCh(it8);
while (k < (MAXSTR-1) && it8->ch != sng) {
if (it8->ch == '\n'|| it8->ch == '\r') k = MAXSTR+1;
else {
*idptr++ = (char) it8->ch;
NextCh(it8);
k++;
}
}
it8->sy = SSTRING;
*idptr = '\0';
NextCh(it8);
break;
default:
SynError(ContextID, it8, "Unrecognized character: 0x%x", it8 ->ch);
return;
}
} while (it8->sy == SCOMMENT);
// Handle the include special token
if (it8 -> sy == SINCLUDE) {
FILECTX* FileNest;
if(it8 -> IncludeSP >= (MAXINCLUDE-1)) {
SynError(ContextID, it8, "Too many recursion levels");
return;
}
InSymbol(ContextID, it8);
if (!Check(ContextID, it8, SSTRING, "Filename expected")) return;
FileNest = it8 -> FileStack[it8 -> IncludeSP + 1];
if(FileNest == NULL) {
FileNest = it8 ->FileStack[it8 -> IncludeSP + 1] = (FILECTX*)AllocChunk(ContextID, it8, sizeof(FILECTX));
//if(FileNest == NULL)
// TODO: how to manage out-of-memory conditions?
}
if (BuildAbsolutePath(it8->str,
it8->FileStack[it8->IncludeSP]->FileName,
FileNest->FileName, cmsMAX_PATH-1) == FALSE) {
SynError(ContextID, it8, "File path too long");
return;
}
FileNest->Stream = fopen(FileNest->FileName, "rt");
if (FileNest->Stream == NULL) {
SynError(ContextID, it8, "File %s not found", FileNest->FileName);
return;
}
it8->IncludeSP++;
it8 ->ch = ' ';
InSymbol(ContextID, it8);
}
}
// Checks end of line separator
static
cmsBool CheckEOLN(cmsContext ContextID, cmsIT8* it8)
{
if (!Check(ContextID, it8, SEOLN, "Expected separator")) return FALSE;
while (it8 -> sy == SEOLN)
InSymbol(ContextID, it8);
return TRUE;
}
// Skip a symbol
static
void Skip(cmsContext ContextID, cmsIT8* it8, SYMBOL sy)
{
if (it8->sy == sy && it8->sy != SEOF)
InSymbol(ContextID, it8);
}
// Skip multiple EOLN
static
void SkipEOLN(cmsContext ContextID, cmsIT8* it8)
{
while (it8->sy == SEOLN) {
InSymbol(ContextID, it8);
}
}
// Returns a string holding current value
static
cmsBool GetVal(cmsContext ContextID, cmsIT8* it8, char* Buffer, cmsUInt32Number max, const char* ErrorTitle)
{
switch (it8->sy) {
case SEOLN: // Empty value
Buffer[0]=0;
break;
case SIDENT: strncpy(Buffer, it8->id, max);
Buffer[max-1]=0;
break;
case SINUM: snprintf(Buffer, max, "%d", it8 -> inum); break;
case SDNUM: snprintf(Buffer, max, it8->DoubleFormatter, it8 -> dnum); break;
case SSTRING: strncpy(Buffer, it8->str, max);
Buffer[max-1] = 0;
break;
default:
return SynError(ContextID, it8, "%s", ErrorTitle);
}
Buffer[max] = 0;
return TRUE;
}
// ---------------------------------------------------------- Table
static
TABLE* GetTable(cmsContext ContextID, cmsIT8* it8)
{
if ((it8 -> nTable >= it8 ->TablesCount)) {
SynError(ContextID, it8, "Table %d out of sequence", it8 -> nTable);
return it8 -> Tab;
}
return it8 ->Tab + it8 ->nTable;
}
// ---------------------------------------------------------- Memory management
// Frees an allocator and owned memory
void CMSEXPORT cmsIT8Free(cmsContext ContextID, cmsHANDLE hIT8)
{
cmsIT8* it8 = (cmsIT8*) hIT8;
if (it8 == NULL)
return;
if (it8->MemorySink) {
OWNEDMEM* p;
OWNEDMEM* n;
for (p = it8->MemorySink; p != NULL; p = n) {
n = p->Next;
if (p->Ptr) _cmsFree(ContextID, p->Ptr);
_cmsFree(ContextID, p);
}
}
if (it8->MemoryBlock)
_cmsFree(ContextID, it8->MemoryBlock);
_cmsFree(ContextID, it8);
}
// Allocates a chunk of data, keep linked list
static
void* AllocBigBlock(cmsContext ContextID, cmsIT8* it8, cmsUInt32Number size)
{
OWNEDMEM* ptr1;
void* ptr = _cmsMallocZero(ContextID, size);
if (ptr != NULL) {
ptr1 = (OWNEDMEM*) _cmsMallocZero(ContextID, sizeof(OWNEDMEM));
if (ptr1 == NULL) {
_cmsFree(ContextID, ptr);
return NULL;
}
ptr1-> Ptr = ptr;
ptr1-> Next = it8 -> MemorySink;
it8 -> MemorySink = ptr1;
}
return ptr;
}
// Suballocator.
static
void* AllocChunk(cmsContext ContextID, cmsIT8* it8, cmsUInt32Number size)
{
cmsUInt32Number Free = it8 ->Allocator.BlockSize - it8 ->Allocator.Used;
cmsUInt8Number* ptr;
size = _cmsALIGNMEM(size);
if (size > Free) {
if (it8 -> Allocator.BlockSize == 0)
it8 -> Allocator.BlockSize = 20*1024;
else
it8 ->Allocator.BlockSize *= 2;
if (it8 ->Allocator.BlockSize < size)
it8 ->Allocator.BlockSize = size;
it8 ->Allocator.Used = 0;
it8 ->Allocator.Block = (cmsUInt8Number*) AllocBigBlock(ContextID, it8, it8 ->Allocator.BlockSize);
}
ptr = it8 ->Allocator.Block + it8 ->Allocator.Used;
it8 ->Allocator.Used += size;
return (void*) ptr;
}
// Allocates a string
static
char *AllocString(cmsContext ContextID, cmsIT8* it8, const char* str)
{
cmsUInt32Number Size = (cmsUInt32Number) strlen(str)+1;
char *ptr;
ptr = (char *) AllocChunk(ContextID, it8, Size);
if (ptr) strncpy (ptr, str, Size-1);
return ptr;
}
// Searches through linked list
static
cmsBool IsAvailableOnList(cmsContext ContextID, KEYVALUE* p, const char* Key, const char* Subkey, KEYVALUE** LastPtr)
{
cmsUNUSED_PARAMETER(ContextID);
if (LastPtr) *LastPtr = p;
for (; p != NULL; p = p->Next) {
if (LastPtr) *LastPtr = p;
if (*Key != '#') { // Comments are ignored
if (cmsstrcasecmp(Key, p->Keyword) == 0)
break;
}
}
if (p == NULL)
return FALSE;
if (Subkey == 0)
return TRUE;
for (; p != NULL; p = p->NextSubkey) {
if (p ->Subkey == NULL) continue;
if (LastPtr) *LastPtr = p;
if (cmsstrcasecmp(Subkey, p->Subkey) == 0)
return TRUE;
}
return FALSE;
}
// Add a property into a linked list
static
KEYVALUE* AddToList(cmsContext ContextID, cmsIT8* it8, KEYVALUE** Head, const char *Key, const char *Subkey, const char* xValue, WRITEMODE WriteAs)
{
KEYVALUE* p;
KEYVALUE* last;
// Check if property is already in list
if (IsAvailableOnList(ContextID, *Head, Key, Subkey, &p)) {
// This may work for editing properties
// return SynError(ContextID, it8, "duplicate key <%s>", Key);
}
else {
last = p;
// Allocate the container
p = (KEYVALUE*) AllocChunk(ContextID, it8, sizeof(KEYVALUE));
if (p == NULL)
{
SynError(ContextID, it8, "AddToList: out of memory");
return NULL;
}
// Store name and value
p->Keyword = AllocString(ContextID, it8, Key);
p->Subkey = (Subkey == NULL) ? NULL : AllocString(ContextID, it8, Subkey);
// Keep the container in our list
if (*Head == NULL) {
*Head = p;
}
else
{
if (Subkey != NULL && last != NULL) {
last->NextSubkey = p;
// If Subkey is not null, then last is the last property with the same key,
// but not necessarily is the last property in the list, so we need to move
// to the actual list end
while (last->Next != NULL)
last = last->Next;
}
if (last != NULL) last->Next = p;
}
p->Next = NULL;
p->NextSubkey = NULL;
}
p->WriteAs = WriteAs;
if (xValue != NULL) {
p->Value = AllocString(ContextID, it8, xValue);
}
else {
p->Value = NULL;
}
return p;
}
static
KEYVALUE* AddAvailableProperty(cmsContext ContextID, cmsIT8* it8, const char* Key, WRITEMODE as)
{
return AddToList(ContextID, it8, &it8->ValidKeywords, Key, NULL, NULL, as);
}
static
KEYVALUE* AddAvailableSampleID(cmsContext ContextID, cmsIT8* it8, const char* Key)
{
return AddToList(ContextID, it8, &it8->ValidSampleID, Key, NULL, NULL, WRITE_UNCOOKED);
}
static
void AllocTable(cmsContext ContextID, cmsIT8* it8)
{
TABLE* t;
cmsUNUSED_PARAMETER(ContextID);
t = it8 ->Tab + it8 ->TablesCount;
t->HeaderList = NULL;
t->DataFormat = NULL;
t->Data = NULL;
it8 ->TablesCount++;
}
cmsInt32Number CMSEXPORT cmsIT8SetTable(cmsContext ContextID, cmsHANDLE IT8, cmsUInt32Number nTable)
{
cmsIT8* it8 = (cmsIT8*) IT8;
if (nTable >= it8 ->TablesCount) {
if (nTable == it8 ->TablesCount) {
AllocTable(ContextID, it8);
}
else {
SynError(ContextID, it8, "Table %d is out of sequence", nTable);
return -1;
}
}
it8 ->nTable = nTable;
return (cmsInt32Number) nTable;
}
// Init an empty container
cmsHANDLE CMSEXPORT cmsIT8Alloc(cmsContext ContextID)
{
cmsIT8* it8;
cmsUInt32Number i;
it8 = (cmsIT8*) _cmsMallocZero(ContextID, sizeof(cmsIT8));
if (it8 == NULL) return NULL;
AllocTable(ContextID, it8);
it8->MemoryBlock = NULL;
it8->MemorySink = NULL;
it8 ->nTable = 0;
it8->Allocator.Used = 0;
it8->Allocator.Block = NULL;
it8->Allocator.BlockSize = 0;
it8->ValidKeywords = NULL;
it8->ValidSampleID = NULL;
it8 -> sy = SUNDEFINED;
it8 -> ch = ' ';
it8 -> Source = NULL;
it8 -> inum = 0;
it8 -> dnum = 0.0;
it8->FileStack[0] = (FILECTX*)AllocChunk(ContextID, it8, sizeof(FILECTX));
it8->IncludeSP = 0;
it8 -> lineno = 1;
strcpy(it8->DoubleFormatter, DEFAULT_DBL_FORMAT);
cmsIT8SetSheetType(ContextID, (cmsHANDLE) it8, "CGATS.17");
// Initialize predefined properties & data
for (i=0; i < NUMPREDEFINEDPROPS; i++)
AddAvailableProperty(ContextID, it8, PredefinedProperties[i].id, PredefinedProperties[i].as);
for (i=0; i < NUMPREDEFINEDSAMPLEID; i++)
AddAvailableSampleID(ContextID, it8, PredefinedSampleID[i]);
return (cmsHANDLE) it8;
}
const char* CMSEXPORT cmsIT8GetSheetType(cmsContext ContextID, cmsHANDLE hIT8)
{
return GetTable(ContextID, (cmsIT8*) hIT8)->SheetType;
}
cmsBool CMSEXPORT cmsIT8SetSheetType(cmsContext ContextID, cmsHANDLE hIT8, const char* Type)
{
TABLE* t = GetTable(ContextID, (cmsIT8*) hIT8);
strncpy(t ->SheetType, Type, MAXSTR-1);
t ->SheetType[MAXSTR-1] = 0;
return TRUE;
}
cmsBool CMSEXPORT cmsIT8SetComment(cmsContext ContextID, cmsHANDLE hIT8, const char* Val)
{
cmsIT8* it8 = (cmsIT8*) hIT8;
if (!Val) return FALSE;
if (!*Val) return FALSE;
return AddToList(ContextID, it8, &GetTable(ContextID, it8)->HeaderList, "# ", NULL, Val, WRITE_UNCOOKED) != NULL;
}
// Sets a property
cmsBool CMSEXPORT cmsIT8SetPropertyStr(cmsContext ContextID, cmsHANDLE hIT8, const char* Key, const char *Val)
{
cmsIT8* it8 = (cmsIT8*) hIT8;
if (!Val) return FALSE;
if (!*Val) return FALSE;
return AddToList(ContextID, it8, &GetTable(ContextID, it8)->HeaderList, Key, NULL, Val, WRITE_STRINGIFY) != NULL;
}
cmsBool CMSEXPORT cmsIT8SetPropertyDbl(cmsContext ContextID, cmsHANDLE hIT8, const char* cProp, cmsFloat64Number Val)
{
cmsIT8* it8 = (cmsIT8*) hIT8;
char Buffer[1024];
snprintf(Buffer, 1023, it8->DoubleFormatter, Val);
return AddToList(ContextID, it8, &GetTable(ContextID, it8)->HeaderList, cProp, NULL, Buffer, WRITE_UNCOOKED) != NULL;
}
cmsBool CMSEXPORT cmsIT8SetPropertyHex(cmsContext ContextID, cmsHANDLE hIT8, const char* cProp, cmsUInt32Number Val)
{
cmsIT8* it8 = (cmsIT8*) hIT8;
char Buffer[1024];
snprintf(Buffer, 1023, "%u", Val);
return AddToList(ContextID, it8, &GetTable(ContextID, it8)->HeaderList, cProp, NULL, Buffer, WRITE_HEXADECIMAL) != NULL;
}
cmsBool CMSEXPORT cmsIT8SetPropertyUncooked(cmsContext ContextID, cmsHANDLE hIT8, const char* Key, const char* Buffer)
{
cmsIT8* it8 = (cmsIT8*) hIT8;
return AddToList(ContextID, it8, &GetTable(ContextID, it8)->HeaderList, Key, NULL, Buffer, WRITE_UNCOOKED) != NULL;
}
cmsBool CMSEXPORT cmsIT8SetPropertyMulti(cmsContext ContextID, cmsHANDLE hIT8, const char* Key, const char* SubKey, const char *Buffer)
{
cmsIT8* it8 = (cmsIT8*) hIT8;
return AddToList(ContextID, it8, &GetTable(ContextID, it8)->HeaderList, Key, SubKey, Buffer, WRITE_PAIR) != NULL;
}
// Gets a property
const char* CMSEXPORT cmsIT8GetProperty(cmsContext ContextID, cmsHANDLE hIT8, const char* Key)
{
cmsIT8* it8 = (cmsIT8*) hIT8;
KEYVALUE* p;
if (IsAvailableOnList(ContextID, GetTable(ContextID, it8) -> HeaderList, Key, NULL, &p))
{
return p -> Value;
}
return NULL;
}
cmsFloat64Number CMSEXPORT cmsIT8GetPropertyDbl(cmsContext ContextID, cmsHANDLE hIT8, const char* cProp)
{
const char *v = cmsIT8GetProperty(ContextID, hIT8, cProp);
if (v == NULL) return 0.0;
return ParseFloatNumber(v);
}
const char* CMSEXPORT cmsIT8GetPropertyMulti(cmsContext ContextID, cmsHANDLE hIT8, const char* Key, const char *SubKey)
{
cmsIT8* it8 = (cmsIT8*) hIT8;
KEYVALUE* p;
if (IsAvailableOnList(ContextID, GetTable(ContextID, it8) -> HeaderList, Key, SubKey, &p)) {
return p -> Value;
}
return NULL;
}
// ----------------------------------------------------------------- Datasets
static
void AllocateDataFormat(cmsContext ContextID, cmsIT8* it8)
{
TABLE* t = GetTable(ContextID, it8);
if (t -> DataFormat) return; // Already allocated
t -> nSamples = (int) cmsIT8GetPropertyDbl(ContextID, it8, "NUMBER_OF_FIELDS");
if (t -> nSamples <= 0) {
SynError(ContextID, it8, "AllocateDataFormat: Unknown NUMBER_OF_FIELDS");
t -> nSamples = 10;
}
t -> DataFormat = (char**) AllocChunk (ContextID, it8, ((cmsUInt32Number) t->nSamples + 1) * sizeof(char *));
if (t->DataFormat == NULL) {
SynError(ContextID, it8, "AllocateDataFormat: Unable to allocate dataFormat array");
}
}
static
const char *GetDataFormat(cmsContext ContextID, cmsIT8* it8, int n)
{
TABLE* t = GetTable(ContextID, it8);
if (t->DataFormat)
return t->DataFormat[n];
return NULL;
}
static
cmsBool SetDataFormat(cmsContext ContextID, cmsIT8* it8, int n, const char *label)
{
TABLE* t = GetTable(ContextID, it8);
if (!t->DataFormat)
AllocateDataFormat(ContextID, it8);
if (n > t -> nSamples) {
SynError(ContextID, it8, "More than NUMBER_OF_FIELDS fields.");
return FALSE;
}
if (t->DataFormat) {
t->DataFormat[n] = AllocString(ContextID, it8, label);
}
return TRUE;
}
cmsBool CMSEXPORT cmsIT8SetDataFormat(cmsContext ContextID, cmsHANDLE h, int n, const char *Sample)
{
cmsIT8* it8 = (cmsIT8*)h;
return SetDataFormat(ContextID, it8, n, Sample);
}
static
void AllocateDataSet(cmsContext ContextID, cmsIT8* it8)
{
TABLE* t = GetTable(ContextID, it8);
if (t -> Data) return; // Already allocated
t-> nSamples = atoi(cmsIT8GetProperty(ContextID, it8, "NUMBER_OF_FIELDS"));
t-> nPatches = atoi(cmsIT8GetProperty(ContextID, it8, "NUMBER_OF_SETS"));
if (t -> nSamples < 0 || t->nSamples > 0x7ffe || t->nPatches < 0 || t->nPatches > 0x7ffe)
{
SynError(ContextID, it8, "AllocateDataSet: too much data");
}
else {
t->Data = (char**)AllocChunk(ContextID, it8, ((cmsUInt32Number)t->nSamples + 1) * ((cmsUInt32Number)t->nPatches + 1) * sizeof(char*));
if (t->Data == NULL) {
SynError(ContextID, it8, "AllocateDataSet: Unable to allocate data array");
}
}
}
static
char* GetData(cmsContext ContextID, cmsIT8* it8, int nSet, int nField)
{
TABLE* t = GetTable(ContextID, it8);
int nSamples = t -> nSamples;
int nPatches = t -> nPatches;
if (nSet >= nPatches || nField >= nSamples)
return NULL;
if (!t->Data) return NULL;
return t->Data [nSet * nSamples + nField];
}
static
cmsBool SetData(cmsContext ContextID, cmsIT8* it8, int nSet, int nField, const char *Val)
{
TABLE* t = GetTable(ContextID, it8);
if (!t->Data)
AllocateDataSet(ContextID, it8);
if (!t->Data) return FALSE;
if (nSet > t -> nPatches || nSet < 0) {
return SynError(ContextID, it8, "Patch %d out of range, there are %d patches", nSet, t -> nPatches);
}
if (nField > t ->nSamples || nField < 0) {
return SynError(ContextID, it8, "Sample %d out of range, there are %d samples", nField, t ->nSamples);
}
t->Data [nSet * t -> nSamples + nField] = AllocString(ContextID, it8, Val);
return TRUE;
}
// --------------------------------------------------------------- File I/O
// Writes a string to file
static
void WriteStr(cmsContext ContextID, SAVESTREAM* f, const char *str)
{
cmsUInt32Number len;
cmsUNUSED_PARAMETER(ContextID);
if (str == NULL)
str = " ";
// Length to write
len = (cmsUInt32Number) strlen(str);
f ->Used += len;
if (f ->stream) { // Should I write it to a file?
if (fwrite(str, 1, len, f->stream) != len) {
cmsSignalError(0, cmsERROR_WRITE, "Write to file error in CGATS parser");
return;
}
}
else { // Or to a memory block?
if (f ->Base) { // Am I just counting the bytes?
if (f ->Used > f ->Max) {
cmsSignalError(0, cmsERROR_WRITE, "Write to memory overflows in CGATS parser");
return;
}
memmove(f ->Ptr, str, len);
f->Ptr += len;
}
}
}
// Write formatted
static
void Writef(cmsContext ContextID, SAVESTREAM* f, const char* frm, ...)
{
char Buffer[4096];
va_list args;
va_start(args, frm);
vsnprintf(Buffer, 4095, frm, args);
Buffer[4095] = 0;
WriteStr(ContextID, f, Buffer);
va_end(args);
}
// Writes full header
static
void WriteHeader(cmsContext ContextID, cmsIT8* it8, SAVESTREAM* fp)
{
KEYVALUE* p;
TABLE* t = GetTable(ContextID, it8);
// Writes the type
WriteStr(ContextID, fp, t->SheetType);
WriteStr(ContextID, fp, "\n");
for (p = t->HeaderList; (p != NULL); p = p->Next)
{
if (*p ->Keyword == '#') {
char* Pt;
WriteStr(ContextID, fp, "#\n# ");
for (Pt = p ->Value; *Pt; Pt++) {
Writef(ContextID, fp, "%c", *Pt);
if (*Pt == '\n') {
WriteStr(ContextID, fp, "# ");
}
}
WriteStr(ContextID, fp, "\n#\n");
continue;
}
if (!IsAvailableOnList(ContextID, it8-> ValidKeywords, p->Keyword, NULL, NULL)) {
#ifdef CMS_STRICT_CGATS
WriteStr(ContextID, fp, "KEYWORD\t\"");
WriteStr(ContextID, fp, p->Keyword);
WriteStr(ContextID, fp, "\"\n");
#endif
AddAvailableProperty(ContextID, it8, p->Keyword, WRITE_UNCOOKED);
}
WriteStr(ContextID, fp, p->Keyword);
if (p->Value) {
switch (p ->WriteAs) {
case WRITE_UNCOOKED:
Writef(ContextID, fp, "\t%s", p ->Value);
break;
case WRITE_STRINGIFY:
Writef(ContextID, fp, "\t\"%s\"", p->Value );
break;
case WRITE_HEXADECIMAL:
Writef(ContextID, fp, "\t0x%X", atoi(p ->Value));
break;
case WRITE_BINARY:
Writef(ContextID, fp, "\t0x%B", atoi(p ->Value));
break;
case WRITE_PAIR:
Writef(ContextID, fp, "\t\"%s,%s\"", p->Subkey, p->Value);
break;
default: SynError(ContextID, it8, "Unknown write mode %d", p ->WriteAs);
return;
}
}
WriteStr(ContextID, fp, "\n");
}
}
// Writes the data format
static
void WriteDataFormat(cmsContext ContextID, SAVESTREAM* fp, cmsIT8* it8)
{
int i, nSamples;
TABLE* t = GetTable(ContextID, it8);
if (!t -> DataFormat) return;
WriteStr(ContextID, fp, "BEGIN_DATA_FORMAT\n");
WriteStr(ContextID, fp, " ");
nSamples = atoi(cmsIT8GetProperty(ContextID, it8, "NUMBER_OF_FIELDS"));
for (i = 0; i < nSamples; i++) {
WriteStr(ContextID, fp, t->DataFormat[i]);
WriteStr(ContextID, fp, ((i == (nSamples-1)) ? "\n" : "\t"));
}
WriteStr (ContextID, fp, "END_DATA_FORMAT\n");
}
// Writes data array
static
void WriteData(cmsContext ContextID, SAVESTREAM* fp, cmsIT8* it8)
{
int i, j;
TABLE* t = GetTable(ContextID, it8);
if (!t->Data) return;
WriteStr (ContextID, fp, "BEGIN_DATA\n");
t->nPatches = atoi(cmsIT8GetProperty(ContextID, it8, "NUMBER_OF_SETS"));
for (i = 0; i < t-> nPatches; i++) {
WriteStr(ContextID, fp, " ");
for (j = 0; j < t->nSamples; j++) {
char *ptr = t->Data[i*t->nSamples+j];
if (ptr == NULL) WriteStr(ContextID, fp, "\"\"");
else {
// If value contains whitespace, enclose within quote
if (strchr(ptr, ' ') != NULL) {
WriteStr(ContextID, fp, "\"");
WriteStr(ContextID, fp, ptr);
WriteStr(ContextID, fp, "\"");
}
else
WriteStr(ContextID, fp, ptr);
}
WriteStr(ContextID, fp, ((j == (t->nSamples-1)) ? "\n" : "\t"));
}
}
WriteStr(ContextID, fp, "END_DATA\n");
}
// Saves whole file
cmsBool CMSEXPORT cmsIT8SaveToFile(cmsContext ContextID, cmsHANDLE hIT8, const char* cFileName)
{
SAVESTREAM sd;
cmsUInt32Number i;
cmsIT8* it8 = (cmsIT8*) hIT8;
memset(&sd, 0, sizeof(sd));
sd.stream = fopen(cFileName, "wt");
if (!sd.stream) return FALSE;
for (i=0; i < it8 ->TablesCount; i++) {
cmsIT8SetTable(ContextID, hIT8, i);
WriteHeader(ContextID, it8, &sd);
WriteDataFormat(ContextID, &sd, it8);
WriteData(ContextID, &sd, it8);
}
if (fclose(sd.stream) != 0) return FALSE;
return TRUE;
}
// Saves to memory
cmsBool CMSEXPORT cmsIT8SaveToMem(cmsContext ContextID, cmsHANDLE hIT8, void *MemPtr, cmsUInt32Number* BytesNeeded)
{
SAVESTREAM sd;
cmsUInt32Number i;
cmsIT8* it8 = (cmsIT8*) hIT8;
memset(&sd, 0, sizeof(sd));
sd.stream = NULL;
sd.Base = (cmsUInt8Number*) MemPtr;
sd.Ptr = sd.Base;
sd.Used = 0;
if (sd.Base)
sd.Max = *BytesNeeded; // Write to memory?
else
sd.Max = 0; // Just counting the needed bytes
for (i=0; i < it8 ->TablesCount; i++) {
cmsIT8SetTable(ContextID, hIT8, i);
WriteHeader(ContextID, it8, &sd);
WriteDataFormat(ContextID, &sd, it8);
WriteData(ContextID, &sd, it8);
}
sd.Used++; // The \0 at the very end
if (sd.Base)
*sd.Ptr = 0;
*BytesNeeded = sd.Used;
return TRUE;
}
// -------------------------------------------------------------- Higher level parsing
static
cmsBool DataFormatSection(cmsContext ContextID, cmsIT8* it8)
{
int iField = 0;
TABLE* t = GetTable(ContextID, it8);
InSymbol(ContextID, it8); // Eats "BEGIN_DATA_FORMAT"
CheckEOLN(ContextID, it8);
while (it8->sy != SEND_DATA_FORMAT &&
it8->sy != SEOLN &&
it8->sy != SEOF &&
it8->sy != SSYNERROR) {
if (it8->sy != SIDENT) {
return SynError(ContextID, it8, "Sample type expected");
}
if (!SetDataFormat(ContextID, it8, iField, it8->id)) return FALSE;
iField++;
InSymbol(ContextID, it8);
SkipEOLN(ContextID, it8);
}
SkipEOLN(ContextID, it8);
Skip(ContextID, it8, SEND_DATA_FORMAT);
SkipEOLN(ContextID, it8);
if (iField != t ->nSamples) {
SynError(ContextID, it8, "Count mismatch. NUMBER_OF_FIELDS was %d, found %d\n", t ->nSamples, iField);
}
return TRUE;
}
static
cmsBool DataSection (cmsContext ContextID, cmsIT8* it8)
{
int iField = 0;
int iSet = 0;
char Buffer[256];
TABLE* t = GetTable(ContextID, it8);
InSymbol(ContextID, it8); // Eats "BEGIN_DATA"
CheckEOLN(ContextID, it8);
if (!t->Data)
AllocateDataSet(ContextID, it8);
while (it8->sy != SEND_DATA && it8->sy != SEOF)
{
if (iField >= t -> nSamples) {
iField = 0;
iSet++;
}
if (it8->sy != SEND_DATA && it8->sy != SEOF) {
if (!GetVal(ContextID, it8, Buffer, 255, "Sample data expected"))
return FALSE;
if (!SetData(ContextID, it8, iSet, iField, Buffer))
return FALSE;
iField++;
InSymbol(ContextID, it8);
SkipEOLN(ContextID, it8);
}
}
SkipEOLN(ContextID, it8);
Skip(ContextID, it8, SEND_DATA);
SkipEOLN(ContextID, it8);
// Check for data completion.
if ((iSet+1) != t -> nPatches)
return SynError(ContextID, it8, "Count mismatch. NUMBER_OF_SETS was %d, found %d\n", t ->nPatches, iSet+1);
return TRUE;
}
static
cmsBool HeaderSection(cmsContext ContextID, cmsIT8* it8)
{
char VarName[MAXID];
char Buffer[MAXSTR];
KEYVALUE* Key;
while (it8->sy != SEOF &&
it8->sy != SSYNERROR &&
it8->sy != SBEGIN_DATA_FORMAT &&
it8->sy != SBEGIN_DATA) {
switch (it8 -> sy) {
case SKEYWORD:
InSymbol(ContextID, it8);
if (!GetVal(ContextID, it8, Buffer, MAXSTR-1, "Keyword expected")) return FALSE;
if (!AddAvailableProperty(ContextID, it8, Buffer, WRITE_UNCOOKED)) return FALSE;
InSymbol(ContextID, it8);
break;
case SDATA_FORMAT_ID:
InSymbol(ContextID, it8);
if (!GetVal(ContextID, it8, Buffer, MAXSTR-1, "Keyword expected")) return FALSE;
if (!AddAvailableSampleID(ContextID, it8, Buffer)) return FALSE;
InSymbol(ContextID, it8);
break;
case SIDENT:
strncpy(VarName, it8->id, MAXID - 1);
VarName[MAXID - 1] = 0;
if (!IsAvailableOnList(ContextID, it8->ValidKeywords, VarName, NULL, &Key)) {
#ifdef CMS_STRICT_CGATS
return SynError(ContextID, it8, "Undefined keyword '%s'", VarName);
#else
Key = AddAvailableProperty(ContextID, it8, VarName, WRITE_UNCOOKED);
if (Key == NULL) return FALSE;
#endif
}
InSymbol(ContextID, it8);
if (!GetVal(ContextID, it8, Buffer, MAXSTR - 1, "Property data expected")) return FALSE;
if (Key->WriteAs != WRITE_PAIR) {
AddToList(ContextID, it8, &GetTable(ContextID, it8)->HeaderList, VarName, NULL, Buffer,
(it8->sy == SSTRING) ? WRITE_STRINGIFY : WRITE_UNCOOKED);
}
else {
const char *Subkey;
char *Nextkey;
if (it8->sy != SSTRING)
return SynError(ContextID, it8, "Invalid value '%s' for property '%s'.", Buffer, VarName);
// chop the string as a list of "subkey, value" pairs, using ';' as a separator
for (Subkey = Buffer; Subkey != NULL; Subkey = Nextkey)
{
char *Value, *temp;
// identify token pair boundary
Nextkey = (char*)strchr(Subkey, ';');
if (Nextkey)
*Nextkey++ = '\0';
// for each pair, split the subkey and the value
Value = (char*)strrchr(Subkey, ',');
if (Value == NULL)
return SynError(ContextID, it8, "Invalid value for property '%s'.", VarName);
// gobble the spaces before the coma, and the coma itself
temp = Value++;
do *temp-- = '\0'; while (temp >= Subkey && *temp == ' ');
// gobble any space at the right
temp = Value + strlen(Value) - 1;
while (*temp == ' ') *temp-- = '\0';
// trim the strings from the left
Subkey += strspn(Subkey, " ");
Value += strspn(Value, " ");
if (Subkey[0] == 0 || Value[0] == 0)
return SynError(ContextID, it8, "Invalid value for property '%s'.", VarName);
AddToList(ContextID, it8, &GetTable(ContextID, it8)->HeaderList, VarName, Subkey, Value, WRITE_PAIR);
}
}
InSymbol(ContextID, it8);
break;
case SEOLN: break;
default:
return SynError(ContextID, it8, "expected keyword or identifier");
}
SkipEOLN(ContextID, it8);
}
return TRUE;
}
static
void ReadType(cmsIT8* it8, char* SheetTypePtr)
{
cmsInt32Number cnt = 0;
// First line is a very special case.
while (isseparator(it8->ch))
NextCh(it8);
while (it8->ch != '\r' && it8 ->ch != '\n' && it8->ch != '\t' && it8 -> ch != 0) {
if (cnt++ < MAXSTR)
*SheetTypePtr++= (char) it8 ->ch;
NextCh(it8);
}
*SheetTypePtr = 0;
}
static
cmsBool ParseIT8(cmsContext ContextID, cmsIT8* it8, cmsBool nosheet)
{
char* SheetTypePtr = it8 ->Tab[0].SheetType;
if (nosheet == 0) {
ReadType(it8, SheetTypePtr);
}
InSymbol(ContextID, it8);
SkipEOLN(ContextID, it8);
while (it8-> sy != SEOF &&
it8-> sy != SSYNERROR) {
switch (it8 -> sy) {
case SBEGIN_DATA_FORMAT:
if (!DataFormatSection(ContextID, it8)) return FALSE;
break;
case SBEGIN_DATA:
if (!DataSection(ContextID, it8)) return FALSE;
if (it8 -> sy != SEOF) {
AllocTable(ContextID, it8);
it8 ->nTable = it8 ->TablesCount - 1;
// Read sheet type if present. We only support identifier and string.
// <ident> <eoln> is a type string
// anything else, is not a type string
if (nosheet == 0) {
if (it8 ->sy == SIDENT) {
// May be a type sheet or may be a prop value statement. We cannot use insymbol in
// this special case...
while (isseparator(it8->ch))
NextCh(it8);
// If a newline is found, then this is a type string
if (it8 ->ch == '\n' || it8->ch == '\r') {
cmsIT8SetSheetType(ContextID, it8, it8 ->id);
InSymbol(ContextID, it8);
}
else
{
// It is not. Just continue
cmsIT8SetSheetType(ContextID, it8, "");
}
}
else
// Validate quoted strings
if (it8 ->sy == SSTRING) {
cmsIT8SetSheetType(ContextID, it8, it8 ->str);
InSymbol(ContextID, it8);
}
}
}
break;
case SEOLN:
SkipEOLN(ContextID, it8);
break;
default:
if (!HeaderSection(ContextID, it8)) return FALSE;
}
}
return (it8 -> sy != SSYNERROR);
}
// Init useful pointers
static
void CookPointers(cmsContext ContextID, cmsIT8* it8)
{
int idField, i;
char* Fld;
cmsUInt32Number j;
cmsUInt32Number nOldTable = it8 ->nTable;
for (j=0; j < it8 ->TablesCount; j++) {
TABLE* t = it8 ->Tab + j;
t -> SampleID = 0;
it8 ->nTable = j;
for (idField = 0; idField < t -> nSamples; idField++)
{
if (t ->DataFormat == NULL){
SynError(ContextID, it8, "Undefined DATA_FORMAT");
return;
}
Fld = t->DataFormat[idField];
if (!Fld) continue;
if (cmsstrcasecmp(Fld, "SAMPLE_ID") == 0) {
t -> SampleID = idField;
for (i=0; i < t -> nPatches; i++) {
char *Data = GetData(ContextID, it8, i, idField);
if (Data) {
char Buffer[256];
strncpy(Buffer, Data, 255);
Buffer[255] = 0;
if (strlen(Buffer) <= strlen(Data))
strcpy(Data, Buffer);
else
SetData(ContextID, it8, i, idField, Buffer);
}
}
}
// "LABEL" is an extension. It keeps references to forward tables
if ((cmsstrcasecmp(Fld, "LABEL") == 0) || Fld[0] == '$' ) {
// Search for table references...
for (i=0; i < t -> nPatches; i++) {
char *Label = GetData(ContextID, it8, i, idField);
if (Label) {
cmsUInt32Number k;
// This is the label, search for a table containing
// this property
for (k=0; k < it8 ->TablesCount; k++) {
TABLE* Table = it8 ->Tab + k;
KEYVALUE* p;
if (IsAvailableOnList(ContextID, Table->HeaderList, Label, NULL, &p)) {
// Available, keep type and table
char Buffer[256];
char *Type = p ->Value;
int nTable = (int) k;
snprintf(Buffer, 255, "%s %d %s", Label, nTable, Type );
SetData(ContextID, it8, i, idField, Buffer);
}
}
}
}
}
}
}
it8 ->nTable = nOldTable;
}
// Try to infere if the file is a CGATS/IT8 file at all. Read first line
// that should be something like some printable characters plus a \n
// returns 0 if this is not like a CGATS, or an integer otherwise. This integer is the number of words in first line?
static
int IsMyBlock(const cmsUInt8Number* Buffer, cmsUInt32Number n)
{
int words = 1, space = 0, quot = 0;
cmsUInt32Number i;
if (n < 10) return 0; // Too small
if (n > 132)
n = 132;
for (i = 1; i < n; i++) {
switch(Buffer[i])
{
case '\n':
case '\r':
return ((quot == 1) || (words > 2)) ? 0 : words;
case '\t':
case ' ':
if(!quot && !space)
space = 1;
break;
case '\"':
quot = !quot;
break;
default:
if (Buffer[i] < 32) return 0;
if (Buffer[i] > 127) return 0;
words += space;
space = 0;
break;
}
}
return 0;
}
static
cmsBool IsMyFile(const char* FileName)
{
FILE *fp;
cmsUInt32Number Size;
cmsUInt8Number Ptr[133];
fp = fopen(FileName, "rt");
if (!fp) {
cmsSignalError(0, cmsERROR_FILE, "File '%s' not found", FileName);
return FALSE;
}
Size = (cmsUInt32Number) fread(Ptr, 1, 132, fp);
if (fclose(fp) != 0)
return FALSE;
Ptr[Size] = '\0';
return IsMyBlock(Ptr, Size);
}
// ---------------------------------------------------------- Exported routines
cmsHANDLE CMSEXPORT cmsIT8LoadFromMem(cmsContext ContextID, const void *Ptr, cmsUInt32Number len)
{
cmsHANDLE hIT8;
cmsIT8* it8;
int type;
_cmsAssert(Ptr != NULL);
_cmsAssert(len != 0);
type = IsMyBlock((const cmsUInt8Number*)Ptr, len);
if (type == 0) return NULL;
hIT8 = cmsIT8Alloc(ContextID);
if (!hIT8) return NULL;
it8 = (cmsIT8*) hIT8;
it8 ->MemoryBlock = (char*) _cmsMalloc(ContextID, len + 1);
if (it8->MemoryBlock == NULL)
{
cmsIT8Free(ContextID, hIT8);
return FALSE;
}
strncpy(it8 ->MemoryBlock, (const char*) Ptr, len);
it8 ->MemoryBlock[len] = 0;
strncpy(it8->FileStack[0]->FileName, "", cmsMAX_PATH-1);
it8-> Source = it8 -> MemoryBlock;
if (!ParseIT8(ContextID, it8, type-1)) {
cmsIT8Free(ContextID, hIT8);
return FALSE;
}
CookPointers(ContextID, it8);
it8 ->nTable = 0;
_cmsFree(ContextID, it8->MemoryBlock);
it8 -> MemoryBlock = NULL;
return hIT8;
}
cmsHANDLE CMSEXPORT cmsIT8LoadFromFile(cmsContext ContextID, const char* cFileName)
{
cmsHANDLE hIT8;
cmsIT8* it8;
int type;
_cmsAssert(cFileName != NULL);
type = IsMyFile(cFileName);
if (type == 0) return NULL;
hIT8 = cmsIT8Alloc(ContextID);
it8 = (cmsIT8*) hIT8;
if (!hIT8) return NULL;
it8 ->FileStack[0]->Stream = fopen(cFileName, "rt");
if (!it8 ->FileStack[0]->Stream) {
cmsIT8Free(ContextID, hIT8);
return NULL;
}
strncpy(it8->FileStack[0]->FileName, cFileName, cmsMAX_PATH-1);
it8->FileStack[0]->FileName[cmsMAX_PATH-1] = 0;
if (!ParseIT8(ContextID, it8, type-1)) {
fclose(it8 ->FileStack[0]->Stream);
cmsIT8Free(ContextID, hIT8);
return NULL;
}
CookPointers(ContextID, it8);
it8 ->nTable = 0;
if (fclose(it8 ->FileStack[0]->Stream)!= 0) {
cmsIT8Free(ContextID, hIT8);
return NULL;
}
return hIT8;
}
int CMSEXPORT cmsIT8EnumDataFormat(cmsContext ContextID, cmsHANDLE hIT8, char ***SampleNames)
{
cmsIT8* it8 = (cmsIT8*) hIT8;
TABLE* t;
_cmsAssert(hIT8 != NULL);
t = GetTable(ContextID, it8);
if (SampleNames)
*SampleNames = t -> DataFormat;
return t -> nSamples;
}
cmsUInt32Number CMSEXPORT cmsIT8EnumProperties(cmsContext ContextID, cmsHANDLE hIT8, char ***PropertyNames)
{
cmsIT8* it8 = (cmsIT8*) hIT8;
KEYVALUE* p;
cmsUInt32Number n;
char **Props;
TABLE* t;
_cmsAssert(hIT8 != NULL);
t = GetTable(ContextID, it8);
// Pass#1 - count properties
n = 0;
for (p = t -> HeaderList; p != NULL; p = p->Next) {
n++;
}
Props = (char **) AllocChunk(ContextID, it8, sizeof(char *) * n);
// Pass#2 - Fill pointers
n = 0;
for (p = t -> HeaderList; p != NULL; p = p->Next) {
Props[n++] = p -> Keyword;
}
*PropertyNames = Props;
return n;
}
cmsUInt32Number CMSEXPORT cmsIT8EnumPropertyMulti(cmsContext ContextID, cmsHANDLE hIT8, const char* cProp, const char ***SubpropertyNames)
{
cmsIT8* it8 = (cmsIT8*) hIT8;
KEYVALUE *p, *tmp;
cmsUInt32Number n;
const char **Props;
TABLE* t;
_cmsAssert(hIT8 != NULL);
t = GetTable(ContextID, it8);
if(!IsAvailableOnList(ContextID, t->HeaderList, cProp, NULL, &p)) {
*SubpropertyNames = 0;
return 0;
}
// Pass#1 - count properties
n = 0;
for (tmp = p; tmp != NULL; tmp = tmp->NextSubkey) {
if(tmp->Subkey != NULL)
n++;
}
Props = (const char **) AllocChunk(ContextID, it8, sizeof(char *) * n);
// Pass#2 - Fill pointers
n = 0;
for (tmp = p; tmp != NULL; tmp = tmp->NextSubkey) {
if(tmp->Subkey != NULL)
Props[n++] = p ->Subkey;
}
*SubpropertyNames = Props;
return n;
}
static
int LocatePatch(cmsContext ContextID, cmsIT8* it8, const char* cPatch)
{
int i;
const char *data;
TABLE* t = GetTable(ContextID, it8);
for (i=0; i < t-> nPatches; i++) {
data = GetData(ContextID, it8, i, t->SampleID);
if (data != NULL) {
if (cmsstrcasecmp(data, cPatch) == 0)
return i;
}
}
// SynError(ContextID, it8, "Couldn't find patch '%s'\n", cPatch);
return -1;
}
static
int LocateEmptyPatch(cmsContext ContextID, cmsIT8* it8)
{
int i;
const char *data;
TABLE* t = GetTable(ContextID, it8);
for (i=0; i < t-> nPatches; i++) {
data = GetData(ContextID, it8, i, t->SampleID);
if (data == NULL)
return i;
}
return -1;
}
static
int LocateSample(cmsContext ContextID, cmsIT8* it8, const char* cSample)
{
int i;
const char *fld;
TABLE* t = GetTable(ContextID, it8);
for (i=0; i < t->nSamples; i++) {
fld = GetDataFormat(ContextID, it8, i);
if (fld != NULL) {
if (cmsstrcasecmp(fld, cSample) == 0)
return i;
}
}
return -1;
}
int CMSEXPORT cmsIT8FindDataFormat(cmsContext ContextID, cmsHANDLE hIT8, const char* cSample)
{
cmsIT8* it8 = (cmsIT8*) hIT8;
_cmsAssert(hIT8 != NULL);
return LocateSample(ContextID, it8, cSample);
}
const char* CMSEXPORT cmsIT8GetDataRowCol(cmsContext ContextID, cmsHANDLE hIT8, int row, int col)
{
cmsIT8* it8 = (cmsIT8*) hIT8;
_cmsAssert(hIT8 != NULL);
return GetData(ContextID, it8, row, col);
}
cmsFloat64Number CMSEXPORT cmsIT8GetDataRowColDbl(cmsContext ContextID, cmsHANDLE hIT8, int row, int col)
{
const char* Buffer;
Buffer = cmsIT8GetDataRowCol(ContextID, hIT8, row, col);
if (Buffer == NULL) return 0.0;
return ParseFloatNumber(Buffer);
}
cmsBool CMSEXPORT cmsIT8SetDataRowCol(cmsContext ContextID, cmsHANDLE hIT8, int row, int col, const char* Val)
{
cmsIT8* it8 = (cmsIT8*) hIT8;
_cmsAssert(hIT8 != NULL);
return SetData(ContextID, it8, row, col, Val);
}
cmsBool CMSEXPORT cmsIT8SetDataRowColDbl(cmsContext ContextID, cmsHANDLE hIT8, int row, int col, cmsFloat64Number Val)
{
cmsIT8* it8 = (cmsIT8*) hIT8;
char Buff[256];
_cmsAssert(hIT8 != NULL);
snprintf(Buff, 255, it8->DoubleFormatter, Val);
return SetData(ContextID, it8, row, col, Buff);
}
const char* CMSEXPORT cmsIT8GetData(cmsContext ContextID, cmsHANDLE hIT8, const char* cPatch, const char* cSample)
{
cmsIT8* it8 = (cmsIT8*) hIT8;
int iField, iSet;
_cmsAssert(hIT8 != NULL);
iField = LocateSample(ContextID, it8, cSample);
if (iField < 0) {
return NULL;
}
iSet = LocatePatch(ContextID, it8, cPatch);
if (iSet < 0) {
return NULL;
}
return GetData(ContextID, it8, iSet, iField);
}
cmsFloat64Number CMSEXPORT cmsIT8GetDataDbl(cmsContext ContextID, cmsHANDLE it8, const char* cPatch, const char* cSample)
{
const char* Buffer;
Buffer = cmsIT8GetData(ContextID, it8, cPatch, cSample);
return ParseFloatNumber(Buffer);
}
cmsBool CMSEXPORT cmsIT8SetData(cmsContext ContextID, cmsHANDLE hIT8, const char* cPatch, const char* cSample, const char *Val)
{
cmsIT8* it8 = (cmsIT8*) hIT8;
int iField, iSet;
TABLE* t;
_cmsAssert(hIT8 != NULL);
t = GetTable(ContextID, it8);
iField = LocateSample(ContextID, it8, cSample);
if (iField < 0)
return FALSE;
if (t-> nPatches == 0) {
AllocateDataFormat(ContextID, it8);
AllocateDataSet(ContextID, it8);
CookPointers(ContextID, it8);
}
if (cmsstrcasecmp(cSample, "SAMPLE_ID") == 0) {
iSet = LocateEmptyPatch(ContextID, it8);
if (iSet < 0) {
return SynError(ContextID, it8, "Couldn't add more patches '%s'\n", cPatch);
}
iField = t -> SampleID;
}
else {
iSet = LocatePatch(ContextID, it8, cPatch);
if (iSet < 0) {
return FALSE;
}
}
return SetData(ContextID, it8, iSet, iField, Val);
}
cmsBool CMSEXPORT cmsIT8SetDataDbl(cmsContext ContextID, cmsHANDLE hIT8, const char* cPatch,
const char* cSample,
cmsFloat64Number Val)
{
cmsIT8* it8 = (cmsIT8*) hIT8;
char Buff[256];
_cmsAssert(hIT8 != NULL);
snprintf(Buff, 255, it8->DoubleFormatter, Val);
return cmsIT8SetData(ContextID, hIT8, cPatch, cSample, Buff);
}
// Buffer should get MAXSTR at least
const char* CMSEXPORT cmsIT8GetPatchName(cmsContext ContextID, cmsHANDLE hIT8, int nPatch, char* buffer)
{
cmsIT8* it8 = (cmsIT8*) hIT8;
TABLE* t;
char* Data;
_cmsAssert(hIT8 != NULL);
t = GetTable(ContextID, it8);
Data = GetData(ContextID, it8, nPatch, t->SampleID);
if (!Data) return NULL;
if (!buffer) return Data;
strncpy(buffer, Data, MAXSTR-1);
buffer[MAXSTR-1] = 0;
return buffer;
}
int CMSEXPORT cmsIT8GetPatchByName(cmsContext ContextID, cmsHANDLE hIT8, const char *cPatch)
{
_cmsAssert(hIT8 != NULL);
cmsUNUSED_PARAMETER(ContextID);
return LocatePatch(ContextID, (cmsIT8*)hIT8, cPatch);
}
cmsUInt32Number CMSEXPORT cmsIT8TableCount(cmsContext ContextID, cmsHANDLE hIT8)
{
cmsIT8* it8 = (cmsIT8*) hIT8;
cmsUNUSED_PARAMETER(ContextID);
_cmsAssert(hIT8 != NULL);
return it8 ->TablesCount;
}
// This handles the "LABEL" extension.
// Label, nTable, Type
int CMSEXPORT cmsIT8SetTableByLabel(cmsContext ContextID, cmsHANDLE hIT8, const char* cSet, const char* cField, const char* ExpectedType)
{
const char* cLabelFld;
char Type[256], Label[256];
cmsUInt32Number nTable;
_cmsAssert(hIT8 != NULL);
if (cField != NULL && *cField == 0)
cField = "LABEL";
if (cField == NULL)
cField = "LABEL";
cLabelFld = cmsIT8GetData(ContextID, hIT8, cSet, cField);
if (!cLabelFld) return -1;
if (sscanf(cLabelFld, "%255s %u %255s", Label, &nTable, Type) != 3)
return -1;
if (ExpectedType != NULL && *ExpectedType == 0)
ExpectedType = NULL;
if (ExpectedType) {
if (cmsstrcasecmp(Type, ExpectedType) != 0) return -1;
}
return cmsIT8SetTable(ContextID, hIT8, nTable);
}
cmsBool CMSEXPORT cmsIT8SetIndexColumn(cmsContext ContextID, cmsHANDLE hIT8, const char* cSample)
{
cmsIT8* it8 = (cmsIT8*) hIT8;
int pos;
_cmsAssert(hIT8 != NULL);
pos = LocateSample(ContextID, it8, cSample);
if(pos == -1)
return FALSE;
it8->Tab[it8->nTable].SampleID = pos;
return TRUE;
}
void CMSEXPORT cmsIT8DefineDblFormat(cmsContext ContextID, cmsHANDLE hIT8, const char* Formatter)
{
cmsIT8* it8 = (cmsIT8*) hIT8;
cmsUNUSED_PARAMETER(ContextID);
_cmsAssert(hIT8 != NULL);
if (Formatter == NULL)
strcpy(it8->DoubleFormatter, DEFAULT_DBL_FORMAT);
else
strncpy(it8->DoubleFormatter, Formatter, sizeof(it8->DoubleFormatter));
it8 ->DoubleFormatter[sizeof(it8 ->DoubleFormatter)-1] = 0;
}
|