File: tpl.c

package info (click to toggle)
proftpd-dfsg 1.3.5-1.1%2Bdeb8u2
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 30,768 kB
  • ctags: 17,089
  • sloc: perl: 211,941; ansic: 153,765; php: 11,586; sh: 11,085; makefile: 2,323; xml: 93
file content (2472 lines) | stat: -rwxr-xr-x 94,539 bytes parent folder | download | duplicates (2)
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
/*
Copyright (c) 2005-2010, Troy D. Hanson     http://tpl.sourceforge.net
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#define TPL_VERSION 1.5

static const char id[]="$Id: tpl.c,v 1.4 2012/06/18 16:48:30 castaglia Exp $";


#include <stdlib.h>  /* malloc */
#include <stdarg.h>  /* va_list */
#include <string.h>  /* memcpy, memset, strchr */
#include <stdio.h>   /* printf (tpl_hook.oops default function) */

#ifndef _WIN32
#include <unistd.h>     /* for ftruncate */
#else
#include <io.h>
#define ftruncate(x,y) _chsize(x,y)
#endif
#include <sys/types.h>  /* for 'open' */
#include <sys/stat.h>   /* for 'open' */
#include <fcntl.h>      /* for 'open' */
#include <errno.h>
#ifndef _WIN32
#include <inttypes.h>   /* uint32_t, uint64_t, etc */
#else
typedef unsigned short ushort;
typedef __int16 int16_t;
typedef __int32 int32_t;
typedef __int64 int64_t;
typedef unsigned __int16 uint16_t;
typedef unsigned __int32 uint32_t;
typedef unsigned __int64 uint64_t;
#endif


#if ( defined __MINGW32__ || defined _WIN32 )
#include "win/mman.h"   /* mmap */
#else
#include <sys/mman.h>   /* mmap */
#endif

#include "tpl.h"

#define TPL_GATHER_BUFLEN 8192
#define TPL_MAGIC "tpl"

/* macro to add a structure to a doubly-linked list */
#define DL_ADD(head,add)                                        \
    do {                                                        \
        if (head) {                                             \
            (add)->prev = (head)->prev;                         \
            (head)->prev->next = (add);                         \
            (head)->prev = (add);                               \
            (add)->next = NULL;                                 \
        } else {                                                \
            (head)=(add);                                       \
            (head)->prev = (head);                              \
            (head)->next = NULL;                                \
        }                                                       \
    } while (0);

#define fatal_oom() tpl_hook.fatal("out of memory\n")

/* bit flags (internal). preceded by the external flags in tpl.h */
#define TPL_WRONLY         (1 << 9)  /* app has initiated tpl packing  */
#define TPL_RDONLY         (1 << 10)  /* tpl was loaded (for unpacking) */
#define TPL_XENDIAN        (1 << 11)  /* swap endianness when unpacking */
#define TPL_OLD_STRING_FMT (1 << 12) /* tpl has strings in 1.2 format */

/* values for the flags byte that appears after the magic prefix */
#define TPL_SUPPORTED_BITFLAGS 3
#define TPL_FL_BIGENDIAN   (1 << 0)
#define TPL_FL_NULLSTRINGS (1 << 1)

/* char values for node type */
#define TPL_TYPE_ROOT   0
#define TPL_TYPE_INT32  1
#define TPL_TYPE_UINT32 2
#define TPL_TYPE_BYTE   3
#define TPL_TYPE_STR    4
#define TPL_TYPE_ARY    5
#define TPL_TYPE_BIN    6
#define TPL_TYPE_DOUBLE 7
#define TPL_TYPE_INT64  8
#define TPL_TYPE_UINT64 9
#define TPL_TYPE_INT16  10
#define TPL_TYPE_UINT16 11
#define TPL_TYPE_POUND  12

/* error codes */
#define ERR_NOT_MINSIZE        (-1)
#define ERR_MAGIC_MISMATCH     (-2)
#define ERR_INCONSISTENT_SZ    (-3)
#define ERR_FMT_INVALID        (-4)
#define ERR_FMT_MISSING_NUL    (-5)
#define ERR_FMT_MISMATCH       (-6)
#define ERR_FLEN_MISMATCH      (-7)
#define ERR_INCONSISTENT_SZ2   (-8)
#define ERR_INCONSISTENT_SZ3   (-9)
#define ERR_INCONSISTENT_SZ4   (-10)
#define ERR_UNSUPPORTED_FLAGS  (-11)

/* access to A(...) nodes by index */
typedef struct tpl_pidx {
    struct tpl_node *node;
    struct tpl_pidx *next,*prev;
} tpl_pidx;

/* A(...) node datum */
typedef struct tpl_atyp {
    uint32_t num;    /* num elements */
    size_t sz;       /* size of each backbone's datum */
    struct tpl_backbone *bb,*bbtail; 
    void *cur;                       
} tpl_atyp;

/* backbone to extend A(...) lists dynamically */
typedef struct tpl_backbone {
    struct tpl_backbone *next;
    /* when this structure is malloc'd, extra space is alloc'd at the
     * end to store the backbone "datum", and data points to it. */
#if defined(__STDC_VERSION__) &&  __STDC_VERSION__ < 199901
    char *data;  
#else
    char data[];
#endif
} tpl_backbone;

/* mmap record */
typedef struct tpl_mmap_rec {
    int fd;
    void *text;
    size_t text_sz;
} tpl_mmap_rec;

/* root node datum */
typedef struct tpl_root_data {
    int flags;
    tpl_pidx *pidx;
    tpl_mmap_rec mmap;
    char *fmt;
    int *fxlens, num_fxlens;
} tpl_root_data;

/* node type to size mapping */
struct tpl_type_t {
    char c;
    int sz;
};


/* Internal prototypes */
static tpl_node *tpl_node_new(tpl_node *parent);
static tpl_node *tpl_find_i(tpl_node *n, int i);
static void *tpl_cpv(void *datav, void *data, size_t sz);
static void *tpl_extend_backbone(tpl_node *n);
static char *tpl_fmt(tpl_node *r);
static void *tpl_dump_atyp(tpl_node *n, tpl_atyp* at, void *dv);
static size_t tpl_ser_osz(tpl_node *n);
static void tpl_free_atyp(tpl_node *n,tpl_atyp *atyp);
static int tpl_dump_to_mem(tpl_node *r, void *addr, size_t sz);
static int tpl_mmap_file(char *filename, tpl_mmap_rec *map_rec);
static int tpl_mmap_output_file(char *filename, size_t sz, void **text_out);
static int tpl_cpu_bigendian(void);
static int tpl_needs_endian_swap(void *);
static void tpl_byteswap(void *word, int len);
static void tpl_fatal(char *fmt, ...);
static int tpl_serlen(tpl_node *r, tpl_node *n, void *dv, size_t *serlen);
static int tpl_unpackA0(tpl_node *r);
static int tpl_oops(const char *fmt, ...);
static int tpl_gather_mem( char *buf, size_t len, tpl_gather_t **gs, tpl_gather_cb *cb, void *data);
static int tpl_gather_nonblocking( int fd, tpl_gather_t **gs, tpl_gather_cb *cb, void *data);
static int tpl_gather_blocking(int fd, void **img, size_t *sz);
static tpl_node *tpl_map_va(char *fmt, va_list ap);

/* This is used internally to help calculate padding when a 'double' 
 * follows a smaller datatype in a structure. Normally under gcc
 * on x86, d will be aligned at +4, however use of -malign-double
 * causes d to be aligned at +8 (this is actually faster on x86).
 * Also SPARC and x86_64 seem to align always on +8. 
 */
struct tpl_double_alignment_detector {
    char a;
    double d;  /* some platforms align this on +4, others on +8 */
};

/* this is another case where alignment varies. mac os x/gcc was observed
 * to align the int64_t at +4 under -m32 and at +8 under -m64 */
struct tpl_int64_alignment_detector {
    int i;
    int64_t j;  /* some platforms align this on +4, others on +8 */
};

typedef struct {
  size_t inter_elt_len; /* padded inter-element len; i.e. &a[1].field - &a[0].field */
  tpl_node *iter_start_node; /* node to jump back to, as we start each new iteration */
  size_t iternum; /* current iteration number (total req'd. iter's in n->num) */
} tpl_pound_data;

/* Hooks for customizing tpl mem alloc, error handling, etc. Set defaults. */
tpl_hook_t tpl_hook = {
    /* .oops =       */ tpl_oops,
    /* .malloc =     */ malloc,
    /* .realloc =    */ realloc,
    /* .free =       */ free,
    /* .fatal =      */ tpl_fatal,
    /* .gather_max = */ 0 /* max tpl size (bytes) for tpl_gather */
};

static const char tpl_fmt_chars[] = "AS($)BiucsfIUjv#"; /* valid format chars */
static const char tpl_S_fmt_chars[] = "iucsfIUjv#$()"; /* valid within S(...) */
static const char tpl_datapeek_ok_chars[] = "iucsfIUjv"; /* valid in datapeek */
static const struct tpl_type_t tpl_types[] = {
    /* [TPL_TYPE_ROOT] =   */  {'r', 0},
    /* [TPL_TYPE_INT32] =  */  {'i', sizeof(int32_t)},
    /* [TPL_TYPE_UINT32] = */  {'u', sizeof(uint32_t)},
    /* [TPL_TYPE_BYTE] =   */  {'c', sizeof(char)},
    /* [TPL_TYPE_STR] =    */  {'s', sizeof(char*)},
    /* [TPL_TYPE_ARY] =    */  {'A', 0},
    /* [TPL_TYPE_BIN] =    */  {'B', 0},
    /* [TPL_TYPE_DOUBLE] = */  {'f', 8}, /* not sizeof(double) as that varies */
    /* [TPL_TYPE_INT64] =  */  {'I', sizeof(int64_t)},
    /* [TPL_TYPE_UINT64] = */  {'U', sizeof(uint64_t)},
    /* [TPL_TYPE_INT16] =  */  {'j', sizeof(int16_t)},
    /* [TPL_TYPE_UINT16] = */  {'v', sizeof(uint16_t)},
    /* [TPL_TYPE_POUND] =  */  {'#', 0},
};

/* default error-reporting function. Just writes to stderr. */
static int tpl_oops(const char *fmt, ...) {
    va_list ap;
    va_start(ap,fmt);
    vfprintf(stderr,fmt,ap);
    va_end(ap);
    return 0;
}


static tpl_node *tpl_node_new(tpl_node *parent) {
    tpl_node *n;
    if ((n=tpl_hook.malloc(sizeof(tpl_node))) == NULL) {
        fatal_oom();
    }
    n->addr=NULL;
    n->data=NULL;
    n->num=1;
    n->ser_osz=0;
    n->children=NULL;
    n->next=NULL;
    n->parent=parent;
    return n;
}

/* Used in S(..) formats to pack several fields from a structure based on 
 * only the structure address. We need to calculate field addresses 
 * manually taking into account the size of the fields and intervening padding.
 * The wrinkle is that double is not normally aligned on x86-32 but the
 * -malign-double compiler option causes it to be. Double are aligned
 * on Sparc, and apparently on 64 bit x86. We use a helper structure 
 * to detect whether double is aligned in this compilation environment.
 */
static char *calc_field_addr(tpl_node *parent, int type,char *struct_addr, int ordinal) {
    tpl_node *prev;
    int offset;
    int align_sz;

    if (ordinal == 1) return struct_addr;  /* first field starts on structure address */

    /* generate enough padding so field addr is divisible by it's align_sz. 4, 8, etc */
    prev = parent->children->prev; 
    switch(type) {
      case TPL_TYPE_DOUBLE:
        align_sz = sizeof(struct tpl_double_alignment_detector) > 12 ? 8 : 4; 
        break;
      case TPL_TYPE_INT64:
      case TPL_TYPE_UINT64:
        align_sz = sizeof(struct tpl_int64_alignment_detector) > 12 ? 8 : 4; 
        break;
      default:
        align_sz = tpl_types[type].sz;
        break;
    }
    offset = ((uintptr_t)prev->addr - (uintptr_t)struct_addr)
            + (tpl_types[prev->type].sz * prev->num);
    offset = (offset + align_sz - 1) / align_sz * align_sz;
    return struct_addr + offset;
}

TPL_API tpl_node *tpl_map(char *fmt,...) {
  va_list ap;
  tpl_node *tn;

  va_start(ap,fmt);
  tn = tpl_map_va(fmt, ap);
  va_end(ap);
  return tn;
}

static tpl_node *tpl_map_va(char *fmt, va_list ap) {
    int lparen_level=0,expect_lparen=0,t=0,in_structure=0,ordinal=0;
    int in_nested_structure=0;
    char *c, *peek, *struct_addr=NULL, *struct_next;
    tpl_node *root,*parent,*n=NULL,*preceding,*iter_start_node=NULL,
             *struct_widest_node=NULL, *np; tpl_pidx *pidx;
    tpl_pound_data *pd;
    int *fxlens, num_fxlens, pound_num, pound_prod, applies_to_struct;
    int contig_fxlens[10]; /* temp space for contiguous fxlens */
    int num_contig_fxlens, i, j;
    ptrdiff_t inter_elt_len=0; /* padded element length of contiguous structs in array */


    root = tpl_node_new(NULL);
    root->type = TPL_TYPE_ROOT; 
    root->data = (tpl_root_data*)tpl_hook.malloc(sizeof(tpl_root_data));
    if (!root->data) fatal_oom();
    memset((tpl_root_data*)root->data,0,sizeof(tpl_root_data));

    /* set up root nodes special ser_osz to reflect overhead of preamble */
    root->ser_osz =  sizeof(uint32_t); /* tpl leading length */
    root->ser_osz += strlen(fmt) + 1;  /* fmt + NUL-terminator */
    root->ser_osz += 4;                /* 'tpl' magic prefix + flags byte */

    parent=root;

    c=fmt;
    while (*c != '\0') {
        switch (*c) {
            case 'c':
            case 'i':
            case 'u':
            case 'j':
            case 'v':
            case 'I':
            case 'U':
            case 'f':
                if      (*c=='c') t=TPL_TYPE_BYTE;
                else if (*c=='i') t=TPL_TYPE_INT32;
                else if (*c=='u') t=TPL_TYPE_UINT32;
                else if (*c=='j') t=TPL_TYPE_INT16;
                else if (*c=='v') t=TPL_TYPE_UINT16;
                else if (*c=='I') t=TPL_TYPE_INT64;
                else if (*c=='U') t=TPL_TYPE_UINT64;
                else if (*c=='f') t=TPL_TYPE_DOUBLE;

                if (expect_lparen) goto fail;
                n = tpl_node_new(parent);
                n->type = t;
                if (in_structure) {
                    if (ordinal == 1) {
                      /* for S(...)# iteration. Apply any changes to case 's' too!!! */
                      iter_start_node = n; 
                      struct_widest_node = n;
                    }
                    if (tpl_types[n->type].sz > tpl_types[struct_widest_node->type].sz) {
                      struct_widest_node = n;
                    }
                    n->addr = calc_field_addr(parent,n->type,struct_addr,ordinal++);
                } else n->addr = (void*)va_arg(ap,void*);
                n->data = tpl_hook.malloc(tpl_types[t].sz);
                if (!n->data) fatal_oom();
                if (n->parent->type == TPL_TYPE_ARY) 
                    ((tpl_atyp*)(n->parent->data))->sz += tpl_types[t].sz;
                DL_ADD(parent->children,n);
                break;
            case 's':
                if (expect_lparen) goto fail;
                n = tpl_node_new(parent);
                n->type = TPL_TYPE_STR;
                if (in_structure) {
                    if (ordinal == 1) {
                      iter_start_node = n; /* for S(...)# iteration */
                      struct_widest_node = n;
                    }
                    if (tpl_types[n->type].sz > tpl_types[struct_widest_node->type].sz) {
                      struct_widest_node = n;
                    }
                    n->addr = calc_field_addr(parent,n->type,struct_addr,ordinal++);
                } else n->addr = (void*)va_arg(ap,void*);
                n->data = tpl_hook.malloc(sizeof(char*));
                if (!n->data) fatal_oom();
                *(char**)(n->data) = NULL;
                if (n->parent->type == TPL_TYPE_ARY) 
                    ((tpl_atyp*)(n->parent->data))->sz += sizeof(void*);
                DL_ADD(parent->children,n);
                break;
            case '#':
                /* apply a 'num' to preceding atom */
                if (!parent->children) goto fail;
                preceding = parent->children->prev; /* first child's prev is 'last child'*/
                t = preceding->type;
                applies_to_struct = (*(c-1) == ')') ? 1 : 0;
                if (!applies_to_struct) {
                  if (!(t == TPL_TYPE_BYTE   || t == TPL_TYPE_INT32 ||
                        t == TPL_TYPE_UINT32 || t == TPL_TYPE_DOUBLE ||
                        t == TPL_TYPE_UINT64 || t == TPL_TYPE_INT64 || 
                        t == TPL_TYPE_UINT16 || t == TPL_TYPE_INT16 || 
                        t == TPL_TYPE_STR )) goto fail;
                }
                /* count up how many contiguous # and form their product */
                pound_prod=1;
                num_contig_fxlens=0;
                for(peek=c; *peek == '#'; peek++) {
                  pound_num = va_arg(ap, int);
                  if (pound_num < 1) {
                    tpl_hook.fatal("non-positive iteration count %d\n", pound_num);
                  }
                  if (num_contig_fxlens >= (sizeof(contig_fxlens)/sizeof(contig_fxlens[0]))) {
                    tpl_hook.fatal("contiguous # exceeds hardcoded limit\n");
                  }
                  contig_fxlens[num_contig_fxlens++] = pound_num;
                  pound_prod *= pound_num;
                }
                /* increment c to skip contiguous # so its points to last one */
                c = peek-1;
                /* differentiate atom-# from struct-# by noting preceding rparen */
                if (applies_to_struct) { /* insert # node to induce looping */
                  n = tpl_node_new(parent);
                  n->type = TPL_TYPE_POUND;
                  n->num = pound_prod;
                  n->data = tpl_hook.malloc(sizeof(tpl_pound_data));
                  if (!n->data) fatal_oom();
                  pd = (tpl_pound_data*)n->data;
                  pd->inter_elt_len = inter_elt_len;
                  pd->iter_start_node = iter_start_node; 
                  pd->iternum = 0;
                  DL_ADD(parent->children,n);
                  /* multiply the 'num' and data space on each atom in the structure */
                  for(np = iter_start_node; np != n; np = np->next) {
                    if (n->parent->type == TPL_TYPE_ARY) {
                      ((tpl_atyp*)(n->parent->data))->sz += 
                         tpl_types[np->type].sz * (np->num * (n->num - 1));
                    }
                    np->data = tpl_hook.realloc(np->data, tpl_types[np->type].sz * 
                                                          np->num * n->num);
                    if (!np->data) fatal_oom();
                    memset(np->data, 0, tpl_types[np->type].sz * np->num * n->num);
                  }
                } else { /* simple atom-# form does not require a loop */
                  preceding->num = pound_prod;
                  preceding->data = tpl_hook.realloc(preceding->data, 
                      tpl_types[t].sz * preceding->num);
                  if (!preceding->data) fatal_oom();
                  memset(preceding->data,0,tpl_types[t].sz * preceding->num);
                  if (n->parent->type == TPL_TYPE_ARY) {
                      ((tpl_atyp*)(n->parent->data))->sz += tpl_types[t].sz * 
                                                            (preceding->num-1);
                  }
                }
                root->ser_osz += (sizeof(uint32_t) * num_contig_fxlens);

                j = ((tpl_root_data*)root->data)->num_fxlens; /* before incrementing */
                (((tpl_root_data*)root->data)->num_fxlens) += num_contig_fxlens;
                num_fxlens = ((tpl_root_data*)root->data)->num_fxlens; /* new value */
                fxlens = ((tpl_root_data*)root->data)->fxlens;
                fxlens = tpl_hook.realloc(fxlens, sizeof(int) * num_fxlens);
                if (!fxlens) fatal_oom();
                ((tpl_root_data*)root->data)->fxlens = fxlens;
                for(i=0; i < num_contig_fxlens; i++) fxlens[j++] = contig_fxlens[i];

                break;
            case 'B':
                if (expect_lparen) goto fail;
                if (in_structure) goto fail;
                n = tpl_node_new(parent);
                n->type = TPL_TYPE_BIN;
                n->addr = (tpl_bin*)va_arg(ap,void*);
                n->data = tpl_hook.malloc(sizeof(tpl_bin*));
                if (!n->data) fatal_oom();
                *((tpl_bin**)n->data) = NULL;
                if (n->parent->type == TPL_TYPE_ARY) 
                    ((tpl_atyp*)(n->parent->data))->sz += sizeof(tpl_bin);
                DL_ADD(parent->children,n);
                break;
            case 'A':
                if (in_structure) goto fail;
                n = tpl_node_new(parent);
                n->type = TPL_TYPE_ARY;
                DL_ADD(parent->children,n);
                parent = n;
                expect_lparen=1;
                pidx = (tpl_pidx*)tpl_hook.malloc(sizeof(tpl_pidx));
                if (!pidx) fatal_oom();
                pidx->node = n;
                pidx->next = NULL;
                DL_ADD(((tpl_root_data*)(root->data))->pidx,pidx);
                /* set up the A's tpl_atyp */
                n->data = (tpl_atyp*)tpl_hook.malloc(sizeof(tpl_atyp));
                if (!n->data) fatal_oom();
                ((tpl_atyp*)(n->data))->num = 0;
                ((tpl_atyp*)(n->data))->sz = 0;
                ((tpl_atyp*)(n->data))->bb = NULL;
                ((tpl_atyp*)(n->data))->bbtail = NULL;
                ((tpl_atyp*)(n->data))->cur = NULL;
                if (n->parent->type == TPL_TYPE_ARY) 
                    ((tpl_atyp*)(n->parent->data))->sz += sizeof(void*);
                break;
            case 'S':
                if (in_structure) goto fail;
                expect_lparen=1;
                ordinal=1;  /* index upcoming atoms in S(..) */
                in_structure=1+lparen_level; /* so we can tell where S fmt ends */
                struct_addr = (char*)va_arg(ap,void*);
                break;
            case '$': /* nested structure */
                if (!in_structure) goto fail;
                expect_lparen=1;
                in_nested_structure++;
                break;
            case ')':
                lparen_level--;
                if (lparen_level < 0) goto fail;
                if (*(c-1) == '(') goto fail;
                if (in_nested_structure) in_nested_structure--;
                else if (in_structure && (in_structure-1 == lparen_level)) {
                  /* calculate delta between contiguous structures in array */
                  struct_next = calc_field_addr(parent, struct_widest_node->type, 
                                                struct_addr, ordinal++);
                  inter_elt_len = struct_next - struct_addr;
                  in_structure=0;
                }
                else parent = parent->parent; /* rparen ends A() type, not S() type */
                break;
            case '(':
                if (!expect_lparen) goto fail;
                expect_lparen=0;
                lparen_level++;
                break;
            default:
                tpl_hook.oops("unsupported option %c\n", *c);
                goto fail;
        }
        c++;
    }
    if (lparen_level != 0) goto fail;

    /* copy the format string, save for convenience */
    ((tpl_root_data*)(root->data))->fmt = tpl_hook.malloc(strlen(fmt)+1);
    if (((tpl_root_data*)(root->data))->fmt == NULL) 
        fatal_oom();
    memcpy(((tpl_root_data*)(root->data))->fmt,fmt,strlen(fmt)+1);

    return root;

fail:
    tpl_hook.oops("failed to parse %s\n", fmt);
    tpl_free(root);
    return NULL;
}

static int tpl_unmap_file( tpl_mmap_rec *mr) {

    if ( munmap( mr->text, mr->text_sz ) == -1 ) {
        tpl_hook.oops("Failed to munmap: %s\n", strerror(errno));
    }
    close(mr->fd);
    mr->text = NULL;
    mr->text_sz = 0;
    return 0;
}

static void tpl_free_keep_map(tpl_node *r) {
    int mmap_bits = (TPL_RDONLY|TPL_FILE);
    int ufree_bits = (TPL_MEM|TPL_UFREE);
    tpl_node *nxtc,*c;
    int find_next_node=0,looking,i;
    size_t sz;

    /* For mmap'd files, or for 'ufree' memory images , do appropriate release */
    if ((((tpl_root_data*)(r->data))->flags & mmap_bits) == mmap_bits) {
        tpl_unmap_file( &((tpl_root_data*)(r->data))->mmap); 
    } else if ((((tpl_root_data*)(r->data))->flags & ufree_bits) == ufree_bits) {
        tpl_hook.free( ((tpl_root_data*)(r->data))->mmap.text );
    }

    c = r->children;
    if (c) {
        while(c->type != TPL_TYPE_ROOT) {    /* loop until we come back to root node */
            switch (c->type) {
                case TPL_TYPE_BIN:
                    /* free any binary buffer hanging from tpl_bin */
                    if ( *((tpl_bin**)(c->data)) ) {
                        if ( (*((tpl_bin**)(c->data)))->addr ) {
                            tpl_hook.free( (*((tpl_bin**)(c->data)))->addr );
                        }
                        *((tpl_bin**)c->data) = NULL; /* reset tpl_bin */
                    }
                    find_next_node=1;
                    break;
                case TPL_TYPE_STR:
                    /* free any packed (copied) string */
                    for(i=0; i < c->num; i++) {
                      char *str = ((char**)c->data)[i];
                      if (str) {
                        tpl_hook.free(str);
                        ((char**)c->data)[i] = NULL;
                      }
                    }
                    find_next_node=1;
                    break;
                case TPL_TYPE_INT32:
                case TPL_TYPE_UINT32:
                case TPL_TYPE_INT64:
                case TPL_TYPE_UINT64:
                case TPL_TYPE_BYTE:
                case TPL_TYPE_DOUBLE:
                case TPL_TYPE_INT16:
                case TPL_TYPE_UINT16:
                case TPL_TYPE_POUND:
                    find_next_node=1;
                    break;
                case TPL_TYPE_ARY:
                    c->ser_osz = 0; /* zero out the serialization output size */

                    sz = ((tpl_atyp*)(c->data))->sz;  /* save sz to use below */
                    tpl_free_atyp(c,c->data);

                    /* make new atyp */
                    c->data = (tpl_atyp*)tpl_hook.malloc(sizeof(tpl_atyp));
                    if (!c->data) fatal_oom();
                    ((tpl_atyp*)(c->data))->num = 0;
                    ((tpl_atyp*)(c->data))->sz = sz;  /* restore bb datum sz */
                    ((tpl_atyp*)(c->data))->bb = NULL;
                    ((tpl_atyp*)(c->data))->bbtail = NULL;
                    ((tpl_atyp*)(c->data))->cur = NULL;

                    c = c->children; 
                    break;
                default:
                    tpl_hook.fatal("unsupported format character\n");
                    break;
            }

            if (find_next_node) {
                find_next_node=0;
                looking=1;
                while(looking) {
                    if (c->next) {
                        nxtc=c->next;
                        c=nxtc;
                        looking=0;
                    } else {
                        if (c->type == TPL_TYPE_ROOT) break; /* root node */
                        else {
                            nxtc=c->parent;
                            c=nxtc;
                        }
                    }
                }
            }
        }
    }

    ((tpl_root_data*)(r->data))->flags = 0;  /* reset flags */
}

TPL_API void tpl_free(tpl_node *r) {
    int mmap_bits = (TPL_RDONLY|TPL_FILE);
    int ufree_bits = (TPL_MEM|TPL_UFREE);
    tpl_node *nxtc,*c;
    int find_next_node=0,looking,i;
    tpl_pidx *pidx,*pidx_nxt;

    /* For mmap'd files, or for 'ufree' memory images , do appropriate release */
    if ((((tpl_root_data*)(r->data))->flags & mmap_bits) == mmap_bits) {
        tpl_unmap_file( &((tpl_root_data*)(r->data))->mmap); 
    } else if ((((tpl_root_data*)(r->data))->flags & ufree_bits) == ufree_bits) {
        tpl_hook.free( ((tpl_root_data*)(r->data))->mmap.text );
    }

    c = r->children;
    if (c) {
        while(c->type != TPL_TYPE_ROOT) {    /* loop until we come back to root node */
            switch (c->type) {
                case TPL_TYPE_BIN:
                    /* free any binary buffer hanging from tpl_bin */
                    if ( *((tpl_bin**)(c->data)) ) {
                        if ( (*((tpl_bin**)(c->data)))->sz != 0 ) {
                            tpl_hook.free( (*((tpl_bin**)(c->data)))->addr );
                        }
                        tpl_hook.free(*((tpl_bin**)c->data)); /* free tpl_bin */
                    }
                    tpl_hook.free(c->data);  /* free tpl_bin* */
                    find_next_node=1;
                    break;
                case TPL_TYPE_STR:
                    /* free any packed (copied) string */
                    for(i=0; i < c->num; i++) {
                      char *str = ((char**)c->data)[i];
                      if (str) {
                        tpl_hook.free(str);
                        ((char**)c->data)[i] = NULL;
                      }
                    }
                    tpl_hook.free(c->data);
                    find_next_node=1;
                    break;
                case TPL_TYPE_INT32:
                case TPL_TYPE_UINT32:
                case TPL_TYPE_INT64:
                case TPL_TYPE_UINT64:
                case TPL_TYPE_BYTE:
                case TPL_TYPE_DOUBLE:
                case TPL_TYPE_INT16:
                case TPL_TYPE_UINT16:
                case TPL_TYPE_POUND:
                    tpl_hook.free(c->data);
                    find_next_node=1;
                    break;
                case TPL_TYPE_ARY:
                    tpl_free_atyp(c,c->data);
                    if (c->children) c = c->children; /* normal case */
                    else find_next_node=1; /* edge case, handle bad format A() */
                    break;
                default:
                    tpl_hook.fatal("unsupported format character\n");
                    break;
            }

            if (find_next_node) {
                find_next_node=0;
                looking=1;
                while(looking) {
                    if (c->next) {
                        nxtc=c->next;
                        tpl_hook.free(c);
                        c=nxtc;
                        looking=0;
                    } else {
                        if (c->type == TPL_TYPE_ROOT) break; /* root node */
                        else {
                            nxtc=c->parent;
                            tpl_hook.free(c);
                            c=nxtc;
                        }
                    }
                }
            }
        }
    }

    /* free root */
    for(pidx=((tpl_root_data*)(r->data))->pidx; pidx; pidx=pidx_nxt) {
        pidx_nxt = pidx->next;
        tpl_hook.free(pidx);
    }
    tpl_hook.free(((tpl_root_data*)(r->data))->fmt);
    if (((tpl_root_data*)(r->data))->num_fxlens > 0) {
        tpl_hook.free(((tpl_root_data*)(r->data))->fxlens);
    }
    tpl_hook.free(r->data);  /* tpl_root_data */
    tpl_hook.free(r);
}


/* Find the i'th packable ('A' node) */
static tpl_node *tpl_find_i(tpl_node *n, int i) {
    int j=0;
    tpl_pidx *pidx;
    if (n->type != TPL_TYPE_ROOT) return NULL;
    if (i == 0) return n;  /* packable 0 is root */
    for(pidx=((tpl_root_data*)(n->data))->pidx; pidx; pidx=pidx->next) {
        if (++j == i) return pidx->node;
    }
    return NULL;
}

static void *tpl_cpv(void *datav, void *data, size_t sz) {
    if (sz>0) memcpy(datav,data,sz);
    return (void*)((uintptr_t)datav + sz);
}

static void *tpl_extend_backbone(tpl_node *n) {
    tpl_backbone *bb;
    bb = (tpl_backbone*)tpl_hook.malloc(sizeof(tpl_backbone) +
      ((tpl_atyp*)(n->data))->sz );  /* datum hangs on coattails of bb */
    if (!bb) fatal_oom();
#if defined(__STDC_VERSION__) &&  __STDC_VERSION__ < 199901
    bb->data = (char*)((uintptr_t)bb + sizeof(tpl_backbone)); 
#endif
    memset(bb->data,0,((tpl_atyp*)(n->data))->sz);
    bb->next = NULL;
    /* Add the new backbone to the tail, also setting head if necessary  */
    if (((tpl_atyp*)(n->data))->bb == NULL) {
        ((tpl_atyp*)(n->data))->bb = bb;
        ((tpl_atyp*)(n->data))->bbtail = bb;
    } else {
        ((tpl_atyp*)(n->data))->bbtail->next = bb;
        ((tpl_atyp*)(n->data))->bbtail = bb;
    }

    ((tpl_atyp*)(n->data))->num++;
    return bb->data;
}

/* Get the format string corresponding to a given tpl (root node) */
static char *tpl_fmt(tpl_node *r) {
    return ((tpl_root_data*)(r->data))->fmt;
}

/* Get the fmt # lengths as a contiguous buffer of ints (length num_fxlens) */
static int *tpl_fxlens(tpl_node *r, int *num_fxlens) {
    *num_fxlens = ((tpl_root_data*)(r->data))->num_fxlens;
    return ((tpl_root_data*)(r->data))->fxlens;
}

/* called when serializing an 'A' type node into a buffer which has
 * already been set up with the proper space. The backbone is walked
 * which was obtained from the tpl_atyp header passed in. 
 */
static void *tpl_dump_atyp(tpl_node *n, tpl_atyp* at, void *dv) {
    tpl_backbone *bb;
    tpl_node *c;
    void *datav;
    uint32_t slen;
    tpl_bin *binp;
    char *strp;
    tpl_atyp *atypp;
    tpl_pound_data *pd;
    int i;
    size_t itermax;

    /* handle 'A' nodes */
    dv = tpl_cpv(dv,&at->num,sizeof(uint32_t));  /* array len */
    for(bb=at->bb; bb; bb=bb->next) {
        datav = bb->data;
        c=n->children;
        while(c) {
            switch (c->type) {
                case TPL_TYPE_BYTE:
                case TPL_TYPE_DOUBLE:
                case TPL_TYPE_INT32:
                case TPL_TYPE_UINT32:
                case TPL_TYPE_INT64:
                case TPL_TYPE_UINT64:
                case TPL_TYPE_INT16:
                case TPL_TYPE_UINT16:
                    dv = tpl_cpv(dv,datav,tpl_types[c->type].sz * c->num);
                    datav = (void*)((uintptr_t)datav + tpl_types[c->type].sz * c->num);
                    break;
                case TPL_TYPE_BIN:
                    /* dump the buffer length followed by the buffer */
                    memcpy(&binp,datav,sizeof(tpl_bin*)); /* cp to aligned */
                    slen = binp->sz;
                    dv = tpl_cpv(dv,&slen,sizeof(uint32_t));
                    dv = tpl_cpv(dv,binp->addr,slen);
                    datav = (void*)((uintptr_t)datav + sizeof(tpl_bin*));
                    break;
                case TPL_TYPE_STR:
                    /* dump the string length followed by the string */
                    for(i=0; i < c->num; i++) {
                      memcpy(&strp,datav,sizeof(char*)); /* cp to aligned */
                      slen = strp ? (strlen(strp)+1) : 0;
                      dv = tpl_cpv(dv,&slen,sizeof(uint32_t));
                      if (slen > 1) dv = tpl_cpv(dv,strp,slen-1);
                      datav = (void*)((uintptr_t)datav + sizeof(char*));
                    }
                    break;
                case TPL_TYPE_ARY:
                    memcpy(&atypp,datav,sizeof(tpl_atyp*)); /* cp to aligned */
                    dv = tpl_dump_atyp(c,atypp,dv);
                    datav = (void*)((uintptr_t)datav + sizeof(void*));
                    break;
                case TPL_TYPE_POUND:
                    /* iterate over the preceding nodes */
                    pd = (tpl_pound_data*)c->data;
                    itermax = c->num;
                    if (++(pd->iternum) < itermax) {
                      c = pd->iter_start_node;
                      continue;
                    } else { /* loop complete. */
                      pd->iternum = 0;
                    }
                    break;
                default:
                    tpl_hook.fatal("unsupported format character\n");
                    break;
            }
            c=c->next;
        }
    }
    return dv;
}

/* figure the serialization output size needed for tpl whose root is n*/
static size_t tpl_ser_osz(tpl_node *n) {
    tpl_node *c, *np;
    size_t sz, itermax;
    tpl_bin *binp;
    char *strp;
    tpl_pound_data *pd;
    int i;

    /* handle the root node ONLY (subtree's ser_osz have been bubbled-up) */
    if (n->type != TPL_TYPE_ROOT) {
        tpl_hook.fatal("internal error: tpl_ser_osz on non-root node\n");
    }

    sz = n->ser_osz;    /* start with fixed overhead, already stored */
    c=n->children;
    while (c) {
        switch (c->type) {
            case TPL_TYPE_BYTE:
            case TPL_TYPE_DOUBLE:
            case TPL_TYPE_INT32:
            case TPL_TYPE_UINT32:
            case TPL_TYPE_INT64:
            case TPL_TYPE_UINT64:
            case TPL_TYPE_INT16:
            case TPL_TYPE_UINT16:
                sz += tpl_types[c->type].sz * c->num;
                break;
            case TPL_TYPE_BIN:
                sz += sizeof(uint32_t);  /* binary buf len */
                memcpy(&binp,c->data,sizeof(tpl_bin*)); /* cp to aligned */
                sz += binp->sz; 
                break;
            case TPL_TYPE_STR:
                for(i=0; i < c->num; i++) {
                  sz += sizeof(uint32_t);  /* string len */
                  memcpy(&strp,&((char**)c->data)[i],sizeof(char*)); /* cp to aligned */
                  sz += strp ? strlen(strp) : 0;
                }
                break;
            case TPL_TYPE_ARY:
                sz += sizeof(uint32_t);  /* array len */
                sz += c->ser_osz;        /* bubbled-up child array ser_osz */
                break;
            case TPL_TYPE_POUND:
                /* iterate over the preceding nodes */
                itermax = c->num;
                pd = (tpl_pound_data*)c->data;
                if (++(pd->iternum) < itermax) {
                  for(np=pd->iter_start_node; np != c; np = np->next) {
                     np->data = (char*)(np->data) + 
                                (tpl_types[np->type].sz * np->num);
                  }
                  c = pd->iter_start_node;
                  continue;
                } else { /* loop complete. */
                  pd->iternum = 0;
                  for(np=pd->iter_start_node; np != c; np = np->next) {
                     np->data = (char*)(np->data) - ((itermax-1) * 
                                                     tpl_types[np->type].sz * 
                                                     np->num);
                  }
                }
                break;
            default:
                tpl_hook.fatal("unsupported format character\n");
                break;
        }
        c=c->next;
    }
    return sz;
}


TPL_API int tpl_dump(tpl_node *r, int mode, ...) {
    va_list ap;
    char *filename, *bufv;
    void **addr_out,*buf, *pa_addr;
    int fd,rc=0;
    size_t sz,*sz_out, pa_sz;

    if (((tpl_root_data*)(r->data))->flags & TPL_RDONLY) {  /* unusual */
        tpl_hook.oops("error: tpl_dump called for a loaded tpl\n");
        return -1;
    }

    sz = tpl_ser_osz(r); /* compute the size needed to serialize  */

    va_start(ap,mode);
    if (mode & TPL_FILE) {
        filename = va_arg(ap,char*);
        fd = tpl_mmap_output_file(filename, sz, &buf);
        if (fd == -1) rc = -1;
        else {
            rc = tpl_dump_to_mem(r,buf,sz);
            if (msync(buf,sz,MS_SYNC) == -1) {
                tpl_hook.oops("msync failed on fd %d: %s\n", fd, strerror(errno));
            }
            if (munmap(buf, sz) == -1) {
                tpl_hook.oops("munmap failed on fd %d: %s\n", fd, strerror(errno));
            }
            close(fd);
        }
    } else if (mode & TPL_FD) {
        fd = va_arg(ap, int);
        if ( (buf = tpl_hook.malloc(sz)) == NULL) fatal_oom();
        tpl_dump_to_mem(r,buf,sz);
        bufv = buf;
        do {
            rc = write(fd,bufv,sz);
            if (rc > 0) {
                sz -= rc;
                bufv += rc;
            } else if (rc == -1) {
                if (errno == EINTR || errno == EAGAIN) continue;
                tpl_hook.oops("error writing to fd %d: %s\n", fd, strerror(errno));
                free(buf);
                return -1;
            }
        } while (sz > 0);
        free(buf);
        rc = 0;
    } else if (mode & TPL_MEM) {
        if (mode & TPL_PREALLOCD) { /* caller allocated */
          pa_addr = (void*)va_arg(ap, void*);
          pa_sz = va_arg(ap, size_t);
          if (pa_sz < sz) {
              tpl_hook.oops("tpl_dump: buffer too small, need %d bytes\n", sz);
              return -1;
          }
          rc=tpl_dump_to_mem(r,pa_addr,sz);
        } else { /* we allocate */
          addr_out = (void**)va_arg(ap, void*);
          sz_out = va_arg(ap, size_t*);
          if ( (buf = tpl_hook.malloc(sz)) == NULL) fatal_oom();
          *sz_out = sz;
          *addr_out = buf;
          rc=tpl_dump_to_mem(r,buf,sz);
        }
    } else if (mode & TPL_GETSIZE) {
        sz_out = va_arg(ap, size_t*);
        *sz_out = sz;
    } else {
        tpl_hook.oops("unsupported tpl_dump mode %d\n", mode);
        rc=-1;
    }
    va_end(ap);
    return rc;
}

/* This function expects the caller to have set up a memory buffer of 
 * adequate size to hold the serialized tpl. The sz parameter must be
 * the result of tpl_ser_osz(r).
 */
static int tpl_dump_to_mem(tpl_node *r,void *addr,size_t sz) {
    uint32_t slen, sz32;
    int *fxlens, num_fxlens, i;
    void *dv;
    char *fmt,flags;
    tpl_node *c, *np;
    tpl_pound_data *pd;
    size_t itermax;

    fmt = tpl_fmt(r);
    flags = 0;
    if (tpl_cpu_bigendian()) flags |= TPL_FL_BIGENDIAN;
    if (strchr(fmt,'s')) flags |= TPL_FL_NULLSTRINGS;
    sz32 = sz; 

    dv = addr;
    dv = tpl_cpv(dv,TPL_MAGIC,3);         /* copy tpl magic prefix */
    dv = tpl_cpv(dv,&flags,1);            /* copy flags byte */
    dv = tpl_cpv(dv,&sz32,sizeof(uint32_t));/* overall length (inclusive) */
    dv = tpl_cpv(dv,fmt,strlen(fmt)+1);   /* copy format with NUL-term */
    fxlens = tpl_fxlens(r,&num_fxlens);
    dv = tpl_cpv(dv,fxlens,num_fxlens*sizeof(uint32_t));/* fmt # lengths */

    /* serialize the tpl content, iterating over direct children of root */
    c = r->children;
    while (c) {
        switch (c->type) {
            case TPL_TYPE_BYTE:
            case TPL_TYPE_DOUBLE:
            case TPL_TYPE_INT32:
            case TPL_TYPE_UINT32:
            case TPL_TYPE_INT64:
            case TPL_TYPE_UINT64:
            case TPL_TYPE_INT16:
            case TPL_TYPE_UINT16:
                dv = tpl_cpv(dv,c->data,tpl_types[c->type].sz * c->num);
                break;
            case TPL_TYPE_BIN:
                slen = (*(tpl_bin**)(c->data))->sz;
                dv = tpl_cpv(dv,&slen,sizeof(uint32_t));  /* buffer len */
                dv = tpl_cpv(dv,(*(tpl_bin**)(c->data))->addr,slen); /* buf */
                break;
            case TPL_TYPE_STR:
                for(i=0; i < c->num; i++) {
                  char *str = ((char**)c->data)[i];
                  slen = str ? strlen(str)+1 : 0;
                  dv = tpl_cpv(dv,&slen,sizeof(uint32_t));  /* string len */
                  if (slen>1) dv = tpl_cpv(dv,str,slen-1); /*string*/
                }
                break;
            case TPL_TYPE_ARY:
                dv = tpl_dump_atyp(c,(tpl_atyp*)c->data,dv);
                break;
            case TPL_TYPE_POUND:
                 pd = (tpl_pound_data*)c->data;
                 itermax = c->num;
                 if (++(pd->iternum) < itermax) {

                   /* in start or midst of loop. advance data pointers. */
                   for(np=pd->iter_start_node; np != c; np = np->next) {
                     np->data = (char*)(np->data) + 
                                (tpl_types[np->type].sz * np->num);
                   }
                   /* do next iteration */
                   c = pd->iter_start_node;
                   continue;

                 } else { /* loop complete. */
                 
                   /* reset iteration index and addr/data pointers. */
                   pd->iternum = 0;
                   for(np=pd->iter_start_node; np != c; np = np->next) {
                     np->data = (char*)(np->data) - ((itermax-1) * 
                                                     tpl_types[np->type].sz * 
                                                     np->num);
                   }

                 }
                 break;
            default:
                tpl_hook.fatal("unsupported format character\n");
                break;
        }
        c = c->next;
    }

    return 0;
}

static int tpl_cpu_bigendian() {
   unsigned i = 1;
   char *c;
   c = (char*)&i;
   return (c[0] == 1 ? 0 : 1);
}


/*
 * algorithm for sanity-checking a tpl image:
 * scan the tpl whilst not exceeding the buffer size (bufsz) ,
 * formulating a calculated (expected) size of the tpl based
 * on walking its data. When calcsize has been calculated it
 * should exactly match the buffer size (bufsz) and the internal
 * recorded size (intlsz)
 */
static int tpl_sanity(tpl_node *r, int excess_ok) {
    uint32_t intlsz;
    int found_nul=0,rc, octothorpes=0, num_fxlens, *fxlens, flen;
    void *d, *dv;
    char intlflags, *fmt, c, *mapfmt;
    size_t bufsz, serlen;

    d = ((tpl_root_data*)(r->data))->mmap.text;
    bufsz = ((tpl_root_data*)(r->data))->mmap.text_sz;

    dv = d;
    if (bufsz < (4 + sizeof(uint32_t) + 1)) return ERR_NOT_MINSIZE; /* min sz: magic+flags+len+nul */
    if (memcmp(dv,TPL_MAGIC, 3) != 0) return ERR_MAGIC_MISMATCH; /* missing tpl magic prefix */
    if (tpl_needs_endian_swap(dv)) ((tpl_root_data*)(r->data))->flags |= TPL_XENDIAN;
    dv = (void*)((uintptr_t)dv + 3);
    memcpy(&intlflags,dv,sizeof(char));  /* extract flags */
    if (intlflags & ~TPL_SUPPORTED_BITFLAGS) return ERR_UNSUPPORTED_FLAGS;
    /* TPL1.3 stores strings with a "length+1" prefix to discern NULL strings from
       empty strings from non-empty strings; TPL1.2 only handled the latter two. 
       So we need to be mindful of which string format we're reading from. */
    if (!(intlflags & TPL_FL_NULLSTRINGS)) {
      ((tpl_root_data*)(r->data))->flags |= TPL_OLD_STRING_FMT;
    }
    dv = (void*)((uintptr_t)dv + 1);
    memcpy(&intlsz,dv,sizeof(uint32_t));  /* extract internal size */
    if (((tpl_root_data*)(r->data))->flags & TPL_XENDIAN) tpl_byteswap(&intlsz, sizeof(uint32_t));
    if (!excess_ok && (intlsz != bufsz)) return ERR_INCONSISTENT_SZ;  /* inconsisent buffer/internal size */
    dv = (void*)((uintptr_t)dv + sizeof(uint32_t));

    /* dv points to the start of the format string. Look for nul w/in buf sz */
    fmt = (char*)dv;
    while ((uintptr_t)dv-(uintptr_t)d < bufsz && !found_nul) {
        if ( (c = *(char*)dv) != '\0') {
            if (strchr(tpl_fmt_chars,c) == NULL) 
               return ERR_FMT_INVALID;  /* invalid char in format string */
            if ( (c = *(char*)dv) == '#') octothorpes++;
            dv = (void*)((uintptr_t)dv + 1);
        }
        else found_nul = 1;
    }
    if (!found_nul) return ERR_FMT_MISSING_NUL;  /* runaway format string */
    dv = (void*)((uintptr_t)dv + 1);   /* advance to octothorpe lengths buffer */
    
    /* compare the map format to the format of this tpl image */
    mapfmt = tpl_fmt(r);
    rc = strcmp(mapfmt,fmt);
    if (rc != 0) return ERR_FMT_MISMATCH; 

    /* compare octothorpe lengths in image to the mapped values */
    if ((((uintptr_t)dv + (octothorpes * 4)) - (uintptr_t)d) > bufsz) return ERR_INCONSISTENT_SZ4;
    fxlens = tpl_fxlens(r,&num_fxlens);  /* mapped fxlens */
    while(num_fxlens--) {
        memcpy(&flen,dv,sizeof(uint32_t)); /* stored flen */
        if (((tpl_root_data*)(r->data))->flags & TPL_XENDIAN) tpl_byteswap(&flen, sizeof(uint32_t));
        if (flen != *fxlens) return ERR_FLEN_MISMATCH;
        dv = (void*)((uintptr_t)dv + sizeof(uint32_t));
        fxlens++;
    }

    /* dv now points to beginning of data */
    rc = tpl_serlen(r,r,dv,&serlen);  /* get computed serlen of data part */
    if (rc == -1) return ERR_INCONSISTENT_SZ2; /* internal inconsistency in tpl image */
    serlen += ((uintptr_t)dv - (uintptr_t)d);   /* add back serlen of preamble part */
    if (excess_ok && (bufsz < serlen)) return ERR_INCONSISTENT_SZ3;  
    if (!excess_ok && (serlen != bufsz)) return ERR_INCONSISTENT_SZ3;  /* buffer/internal sz exceeds serlen */
    return 0;
}

static void *tpl_find_data_start(void *d) {
    int octothorpes=0;
    d = (void*)((uintptr_t)d + 4); /* skip TPL_MAGIC and flags byte */
    d = (void*)((uintptr_t)d + 4); /* skip int32 overall len */
    while(*(char*)d != '\0') {
        if (*(char*)d == '#') octothorpes++;
        d = (void*)((uintptr_t)d + 1);
    }
    d = (void*)((uintptr_t)d +  1);  /* skip NUL */
    d = (void*)((uintptr_t)d +  (octothorpes * sizeof(uint32_t)));  /* skip # array lens */
    return d;
}

static int tpl_needs_endian_swap(void *d) {
    char *c;
    int cpu_is_bigendian;
    c = (char*)d;
    cpu_is_bigendian = tpl_cpu_bigendian();
    return ((c[3] & TPL_FL_BIGENDIAN) == cpu_is_bigendian) ? 0 : 1;
}

static size_t tpl_size_for(char c) {
  int i;
  for(i=0; i < sizeof(tpl_types)/sizeof(tpl_types[0]); i++) {
    if (tpl_types[i].c == c) return tpl_types[i].sz;
  }
  return 0;
}

TPL_API char* tpl_peek(int mode, ...) {
    va_list ap;
    int xendian=0,found_nul=0,old_string_format=0;
    char *filename=NULL, *datapeek_f=NULL, *datapeek_c=NULL, *datapeek_s=NULL;
    void *addr=NULL, *dv, *datapeek_p=NULL;
    size_t sz=0, fmt_len, first_atom, num_fxlens=0;
    uint32_t datapeek_ssz, datapeek_csz, datapeek_flen;
    tpl_mmap_rec mr = {0,NULL,0};
    char *fmt,*fmt_cpy=NULL,c;
    uint32_t intlsz, **fxlens=NULL, *num_fxlens_out=NULL, *fxlensv;

    va_start(ap,mode);
    if ((mode & TPL_FXLENS) && (mode & TPL_DATAPEEK)) {
        tpl_hook.oops("TPL_FXLENS and TPL_DATAPEEK mutually exclusive\n");
        goto fail;
    }
    if (mode & TPL_FILE) filename = va_arg(ap,char *);
    else if (mode & TPL_MEM) {
        addr = va_arg(ap,void *);
        sz = va_arg(ap,size_t);
    } else {
        tpl_hook.oops("unsupported tpl_peek mode %d\n", mode);
        goto fail;
    }
    if (mode & TPL_DATAPEEK) {
        datapeek_f = va_arg(ap, char*);
    }
    if (mode & TPL_FXLENS) {
        num_fxlens_out = va_arg(ap,uint32_t *);
        fxlens = va_arg(ap,uint32_t **);
        *num_fxlens_out = 0;
        *fxlens = NULL;
    }

    if (mode & TPL_FILE) {
        if (tpl_mmap_file(filename, &mr) != 0) {
            tpl_hook.oops("tpl_peek failed for file %s\n", filename);
            goto fail;
        }
        addr = mr.text;
        sz = mr.text_sz;
    }

    dv = addr;
    if (sz < (4 + sizeof(uint32_t) + 1)) goto fail; /* min sz */
    if (memcmp(dv,TPL_MAGIC, 3) != 0) goto fail; /* missing tpl magic prefix */
    if (tpl_needs_endian_swap(dv)) xendian=1;
    if ((((char*)dv)[3] & TPL_FL_NULLSTRINGS)==0) old_string_format=1;
    dv = (void*)((uintptr_t)dv + 4);
    memcpy(&intlsz,dv,sizeof(uint32_t));  /* extract internal size */
    if (xendian) tpl_byteswap(&intlsz, sizeof(uint32_t));
    if (intlsz != sz) goto fail;  /* inconsisent buffer/internal size */
    dv = (void*)((uintptr_t)dv + sizeof(uint32_t));

    /* dv points to the start of the format string. Look for nul w/in buf sz */
    fmt = (char*)dv;
    while ((uintptr_t)dv-(uintptr_t)addr < sz && !found_nul) {
        if ( (c = *(char*)dv) == '\0') {
            found_nul = 1;
        } else if (c == '#') {
          num_fxlens++;
        }
        dv = (void*)((uintptr_t)dv + 1);
    }
    if (!found_nul) goto fail;  /* runaway format string */
    fmt_len = (char*)dv - fmt;  /* include space for \0 */
    fmt_cpy = tpl_hook.malloc(fmt_len);
    if (fmt_cpy == NULL) {
        fatal_oom();
    }
    memcpy(fmt_cpy, fmt, fmt_len);

    /* retrieve the octothorpic lengths if requested */
    if (num_fxlens > 0) {
      if (sz < ((uintptr_t)dv + (num_fxlens * sizeof(uint32_t)) - (uintptr_t)addr)) {
        goto fail;
      }
    }
    if ((mode & TPL_FXLENS) && (num_fxlens > 0)) {
      *fxlens = tpl_hook.malloc(num_fxlens * sizeof(uint32_t));
      if (*fxlens == NULL) tpl_hook.fatal("out of memory");
      *num_fxlens_out = num_fxlens;
      fxlensv = *fxlens;
      while(num_fxlens--) {
          memcpy(fxlensv,dv,sizeof(uint32_t)); 
          if (xendian) tpl_byteswap(fxlensv, sizeof(uint32_t));
          dv = (void*)((uintptr_t)dv + sizeof(uint32_t));
          fxlensv++;
      }
    }
    /* if caller requested, peek into the specified data elements */
    if (mode & TPL_DATAPEEK) {

       first_atom = strspn(fmt, "S()"); /* skip any leading S() */

       datapeek_flen = strlen(datapeek_f);
       if (strspn(datapeek_f, tpl_datapeek_ok_chars) < datapeek_flen) {
         tpl_hook.oops("invalid TPL_DATAPEEK format: %s\n", datapeek_f);
         tpl_hook.free(fmt_cpy); fmt_cpy = NULL; /* fail */
         goto fail;
       }

       if (strncmp( &fmt[first_atom], datapeek_f, datapeek_flen) != 0) {
         tpl_hook.oops("TPL_DATAPEEK format mismatches tpl iamge\n");
         tpl_hook.free(fmt_cpy); fmt_cpy = NULL; /* fail */
         goto fail;
       }

       /* advance to data start, then copy out requested elements */
       dv = (void*)((uintptr_t)dv +  (num_fxlens * sizeof(uint32_t)));  
       for(datapeek_c = datapeek_f; datapeek_c != NULL && *datapeek_c != '\0'; datapeek_c++) {
         datapeek_p = va_arg(ap, void*);
         if (*datapeek_c == 's') {  /* special handling for strings */
           if ((uintptr_t)dv-(uintptr_t)addr + sizeof(uint32_t) > sz) {
             tpl_hook.oops("tpl_peek: tpl has insufficient length\n");
             tpl_hook.free(fmt_cpy); fmt_cpy = NULL; /* fail */
             goto fail;
           }
           memcpy(&datapeek_ssz,dv,sizeof(uint32_t)); /* get slen */
           if (xendian) tpl_byteswap(&datapeek_ssz, sizeof(uint32_t));
           if (old_string_format) datapeek_ssz++;
           dv = (void*)((uintptr_t)dv + sizeof(uint32_t)); /* adv. to str */
           if (datapeek_ssz == 0) datapeek_s = NULL;
           else {
             if ((uintptr_t)dv-(uintptr_t)addr + datapeek_ssz-1 > sz) {
               tpl_hook.oops("tpl_peek: tpl has insufficient length\n");
               tpl_hook.free(fmt_cpy); fmt_cpy = NULL; /* fail */
               goto fail;
             }
             datapeek_s = tpl_hook.malloc(datapeek_ssz);
             if (datapeek_s == NULL) fatal_oom();
             memcpy(datapeek_s, dv, datapeek_ssz-1);
             datapeek_s[datapeek_ssz-1] = '\0';
             dv = (void*)((uintptr_t)dv + datapeek_ssz-1);
           }
           *(char**)datapeek_p = datapeek_s;
         } else {
           datapeek_csz = tpl_size_for(*datapeek_c);
           if ((uintptr_t)dv-(uintptr_t)addr + datapeek_csz > sz) {
             tpl_hook.oops("tpl_peek: tpl has insufficient length\n");
             tpl_hook.free(fmt_cpy); fmt_cpy = NULL; /* fail */
             goto fail;
           }
           memcpy(datapeek_p, dv, datapeek_csz);
           if (xendian) tpl_byteswap(datapeek_p, datapeek_csz);
           dv = (void*)((uintptr_t)dv + datapeek_csz);
         }
       }
    }

fail:
    va_end(ap);
    if ((mode & TPL_FILE) && mr.text != NULL) tpl_unmap_file( &mr );
    return fmt_cpy;
}

/* tpl_jot(TPL_FILE, "file.tpl", "si", &s, &i); */
/* tpl_jot(TPL_MEM, &buf, &sz, "si", &s, &i); */
/* tpl_jot(TPL_FD, fd, "si", &s, &i); */
TPL_API int tpl_jot(int mode, ...) {
    va_list ap;
    char *filename, *fmt;
    size_t *sz;
    int fd, rc=0;
    void **buf;
    tpl_node *tn;

    va_start(ap,mode);
    if (mode & TPL_FILE) {
      filename = va_arg(ap,char*);
      fmt = va_arg(ap,char*);
      tn = tpl_map_va(fmt, ap);
      if (tn == NULL) { rc=-1; goto fail;}
      tpl_pack(tn, 0);
      rc = tpl_dump(tn, TPL_FILE, filename);
      tpl_free(tn);
    } else if (mode & TPL_MEM) {
      buf = va_arg(ap,void*);
      sz = va_arg(ap,size_t*);
      fmt = va_arg(ap,char*);
      tn = tpl_map_va(fmt,ap);
      if (tn == NULL) { rc=-1; goto fail;}
      tpl_pack(tn,0);
      rc = tpl_dump(tn, TPL_MEM, buf, sz);
      tpl_free(tn);
    } else if (mode & TPL_FD) {
      fd = va_arg(ap,int);
      fmt = va_arg(ap,char*);
      tn = tpl_map_va(fmt,ap);
      if (tn == NULL) { rc=-1; goto fail;}
      tpl_pack(tn,0);
      rc = tpl_dump(tn, TPL_FD, fd);
      tpl_free(tn);
    } else {
      tpl_hook.fatal("invalid tpl_jot mode\n");
    }

fail:
    va_end(ap);
    return rc;
}

TPL_API int tpl_load(tpl_node *r, int mode, ...) {
    va_list ap;
    int rc=0,fd=0;
    char *filename=NULL;
    void *addr=NULL;
    size_t sz;

    va_start(ap,mode);
    if (mode & TPL_FILE) filename = va_arg(ap,char *);
    else if (mode & TPL_MEM) {
        addr = va_arg(ap,void *);
        sz = va_arg(ap,size_t);
    } else if (mode & TPL_FD) {
        fd = va_arg(ap,int);
    } else {
        tpl_hook.oops("unsupported tpl_load mode %d\n", mode);
        return -1;
    }
    va_end(ap);

    if (r->type != TPL_TYPE_ROOT) {
        tpl_hook.oops("error: tpl_load to non-root node\n");
        return -1;
    }
    if (((tpl_root_data*)(r->data))->flags & (TPL_WRONLY|TPL_RDONLY)) {
        /* already packed or loaded, so reset it as if newly mapped */
        tpl_free_keep_map(r);
    }
    if (mode & TPL_FILE) {
        if (tpl_mmap_file(filename, &((tpl_root_data*)(r->data))->mmap) != 0) {
            tpl_hook.oops("tpl_load failed for file %s\n", filename);
            return -1;
        }
        if ( (rc = tpl_sanity(r, (mode & TPL_EXCESS_OK))) != 0) {
            if (rc == ERR_FMT_MISMATCH) {
                tpl_hook.oops("%s: format signature mismatch\n", filename);
            } else if (rc == ERR_FLEN_MISMATCH) { 
                tpl_hook.oops("%s: array lengths mismatch\n", filename);
            } else { 
                tpl_hook.oops("%s: not a valid tpl file\n", filename); 
            }
            tpl_unmap_file( &((tpl_root_data*)(r->data))->mmap );
            return -1;
        }
        ((tpl_root_data*)(r->data))->flags = (TPL_FILE | TPL_RDONLY);
    } else if (mode & TPL_MEM) {
        ((tpl_root_data*)(r->data))->mmap.text = addr;
        ((tpl_root_data*)(r->data))->mmap.text_sz = sz;
        if ( (rc = tpl_sanity(r, (mode & TPL_EXCESS_OK))) != 0) {
            if (rc == ERR_FMT_MISMATCH) {
                tpl_hook.oops("format signature mismatch\n");
            } else { 
                tpl_hook.oops("not a valid tpl file\n"); 
            }
            return -1;
        }
        ((tpl_root_data*)(r->data))->flags = (TPL_MEM | TPL_RDONLY);
        if (mode & TPL_UFREE) ((tpl_root_data*)(r->data))->flags |= TPL_UFREE;
    } else if (mode & TPL_FD) {
        /* if fd read succeeds, resulting mem img is used for load */
        if (tpl_gather(TPL_GATHER_BLOCKING,fd,&addr,&sz) > 0) {
            return tpl_load(r, TPL_MEM|TPL_UFREE, addr, sz);
        } else return -1;
    } else {
        tpl_hook.oops("invalid tpl_load mode %d\n", mode);
        return -1;
    }
    /* this applies to TPL_MEM or TPL_FILE */
    if (tpl_needs_endian_swap(((tpl_root_data*)(r->data))->mmap.text))
        ((tpl_root_data*)(r->data))->flags |= TPL_XENDIAN;
    tpl_unpackA0(r);   /* prepare root A nodes for use */
    return 0;
}

TPL_API int tpl_Alen(tpl_node *r, int i) {
    tpl_node *n;

    n = tpl_find_i(r,i);
    if (n == NULL) {
        tpl_hook.oops("invalid index %d to tpl_unpack\n", i);
        return -1;
    }
    if (n->type != TPL_TYPE_ARY) return -1;
    return ((tpl_atyp*)(n->data))->num;
}

static void tpl_free_atyp(tpl_node *n, tpl_atyp *atyp) {
    tpl_backbone *bb,*bbnxt;
    tpl_node *c;
    void *dv;
    tpl_bin *binp;
    tpl_atyp *atypp;
    char *strp;
    size_t itermax;
    tpl_pound_data *pd;
    int i;

    bb = atyp->bb;
    while (bb) {
        bbnxt = bb->next;
        dv = bb->data;
        c=n->children; 
        while (c) {
            switch (c->type) {
                case TPL_TYPE_BYTE:
                case TPL_TYPE_DOUBLE:
                case TPL_TYPE_INT32:
                case TPL_TYPE_UINT32:
                case TPL_TYPE_INT64:
                case TPL_TYPE_UINT64:
                case TPL_TYPE_INT16:
                case TPL_TYPE_UINT16:
                    dv = (void*)((uintptr_t)dv + tpl_types[c->type].sz*c->num);
                    break;
                case TPL_TYPE_BIN:
                    memcpy(&binp,dv,sizeof(tpl_bin*)); /* cp to aligned */
                    if (binp->addr) tpl_hook.free( binp->addr ); /* free buf */
                    tpl_hook.free(binp);  /* free tpl_bin */
                    dv = (void*)((uintptr_t)dv + sizeof(tpl_bin*));
                    break;
                case TPL_TYPE_STR:
                    for(i=0; i < c->num; i++) {
                      memcpy(&strp,dv,sizeof(char*)); /* cp to aligned */
                      if (strp) tpl_hook.free(strp); /* free string */
                      dv = (void*)((uintptr_t)dv + sizeof(char*));
                    }
                    break;
                case TPL_TYPE_POUND:
                    /* iterate over the preceding nodes */
                    itermax = c->num;
                    pd = (tpl_pound_data*)c->data;
                    if (++(pd->iternum) < itermax) {
                      c = pd->iter_start_node;
                      continue;
                    } else { /* loop complete. */
                      pd->iternum = 0;
                    }
                    break;
                case TPL_TYPE_ARY:
                    memcpy(&atypp,dv,sizeof(tpl_atyp*)); /* cp to aligned */
                    tpl_free_atyp(c,atypp);  /* free atyp */
                    dv = (void*)((uintptr_t)dv + sizeof(void*));
                    break;
                default:
                    tpl_hook.fatal("unsupported format character\n");
                    break;
            }
            c=c->next;
        }
        tpl_hook.free(bb);
        bb = bbnxt;
    }
    tpl_hook.free(atyp);
}

/* determine (by walking) byte length of serialized r/A node at address dv 
 * returns 0 on success, or -1 if the tpl isn't trustworthy (fails consistency)
 */
static int tpl_serlen(tpl_node *r, tpl_node *n, void *dv, size_t *serlen) {
    uint32_t slen;
    int num=0,fidx;
    tpl_node *c;
    size_t len=0, alen, buf_past, itermax;
    tpl_pound_data *pd;

    buf_past = ((uintptr_t)((tpl_root_data*)(r->data))->mmap.text + 
                      ((tpl_root_data*)(r->data))->mmap.text_sz);

    if (n->type == TPL_TYPE_ROOT) num = 1;
    else if (n->type == TPL_TYPE_ARY) {
        if ((uintptr_t)dv + sizeof(uint32_t) > buf_past) return -1;
        memcpy(&num,dv,sizeof(uint32_t));
        if (((tpl_root_data*)(r->data))->flags & TPL_XENDIAN)
             tpl_byteswap(&num, sizeof(uint32_t));
        dv = (void*)((uintptr_t)dv + sizeof(uint32_t));
        len += sizeof(uint32_t);
    } else tpl_hook.fatal("internal error in tpl_serlen\n");

    while (num-- > 0) {
        c=n->children; 
        while (c) {
            switch (c->type) {
                case TPL_TYPE_BYTE:
                case TPL_TYPE_DOUBLE:
                case TPL_TYPE_INT32:
                case TPL_TYPE_UINT32:
                case TPL_TYPE_INT64:
                case TPL_TYPE_UINT64:
                case TPL_TYPE_INT16:
                case TPL_TYPE_UINT16:
                    for(fidx=0; fidx < c->num; fidx++) {  /* octothorpe support */
                        if ((uintptr_t)dv + tpl_types[c->type].sz > buf_past) return -1;
                        dv = (void*)((uintptr_t)dv + tpl_types[c->type].sz);
                        len += tpl_types[c->type].sz;
                    }
                    break;
                case TPL_TYPE_BIN:
                    len += sizeof(uint32_t);
                    if ((uintptr_t)dv + sizeof(uint32_t) > buf_past) return -1;
                    memcpy(&slen,dv,sizeof(uint32_t));
                    if (((tpl_root_data*)(r->data))->flags & TPL_XENDIAN)
                        tpl_byteswap(&slen, sizeof(uint32_t));
                    len += slen;
                    dv = (void*)((uintptr_t)dv + sizeof(uint32_t));
                    if ((uintptr_t)dv + slen > buf_past) return -1;
                    dv = (void*)((uintptr_t)dv + slen);
                    break;
                case TPL_TYPE_STR:
                    for(fidx=0; fidx < c->num; fidx++) {  /* octothorpe support */
                      len += sizeof(uint32_t);
                      if ((uintptr_t)dv + sizeof(uint32_t) > buf_past) return -1;
                      memcpy(&slen,dv,sizeof(uint32_t));
                      if (((tpl_root_data*)(r->data))->flags & TPL_XENDIAN)
                          tpl_byteswap(&slen, sizeof(uint32_t));
                      if (!(((tpl_root_data*)(r->data))->flags & TPL_OLD_STRING_FMT))
                         slen = (slen>1) ? (slen-1) : 0;
                      len += slen;
                      dv = (void*)((uintptr_t)dv + sizeof(uint32_t));
                      if ((uintptr_t)dv + slen > buf_past) return -1;
                      dv = (void*)((uintptr_t)dv + slen);
                    }
                    break;
                case TPL_TYPE_ARY:
                    if ( tpl_serlen(r,c,dv, &alen) == -1) return -1;
                    dv = (void*)((uintptr_t)dv + alen);
                    len += alen;
                    break;
                case TPL_TYPE_POUND:
                    /* iterate over the preceding nodes */
                    itermax = c->num;
                    pd = (tpl_pound_data*)c->data;
                    if (++(pd->iternum) < itermax) {
                      c = pd->iter_start_node;
                      continue;
                    } else { /* loop complete. */
                      pd->iternum = 0;
                    }
                    break;
                default:
                    tpl_hook.fatal("unsupported format character\n");
                    break;
            }
            c=c->next;
        }
    }
    *serlen = len;
    return 0;
}

static int tpl_mmap_output_file(char *filename, size_t sz, void **text_out) {
    void *text;
    int fd,perms;

#ifndef _WIN32
    perms = S_IRUSR|S_IWUSR|S_IWGRP|S_IRGRP|S_IROTH;  /* ug+w o+r */
    fd=open(filename,O_CREAT|O_TRUNC|O_RDWR,perms);
#else
	perms = _S_IWRITE;
    fd=_open(filename,_O_CREAT|_O_TRUNC|_O_RDWR,perms);
#endif

    if ( fd == -1 ) {
        tpl_hook.oops("Couldn't open file %s: %s\n", filename, strerror(errno));
        return -1;
    }

    text = mmap(0, sz, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
    if (text == MAP_FAILED) {
        tpl_hook.oops("Failed to mmap %s: %s\n", filename, strerror(errno));
        close(fd);
        return -1;
    }
    if (ftruncate(fd,sz) == -1) {
        tpl_hook.oops("ftruncate failed: %s\n", strerror(errno));
        munmap( text, sz );
        close(fd);
        return -1;
    }
    *text_out = text;
    return fd;
}

static int tpl_mmap_file(char *filename, tpl_mmap_rec *mr) {
    struct stat stat_buf;

    if ( (mr->fd = open(filename, O_RDONLY)) == -1 ) {
        tpl_hook.oops("Couldn't open file %s: %s\n", filename, strerror(errno));
        return -1;
    }

    if ( fstat(mr->fd, &stat_buf) == -1) {
        close(mr->fd);
        tpl_hook.oops("Couldn't stat file %s: %s\n", filename, strerror(errno));
        return -1;
    }

    mr->text_sz = (size_t)stat_buf.st_size;  
    mr->text = mmap(0, mr->text_sz, PROT_READ, MAP_PRIVATE, mr->fd, 0);
    if (mr->text == MAP_FAILED) {
        close(mr->fd);
        tpl_hook.oops("Failed to mmap %s: %s\n", filename, strerror(errno));
        return -1;
    }

    return 0;
}

TPL_API int tpl_pack(tpl_node *r, int i) {
    tpl_node *n, *child, *np;
    void *datav=NULL;
    size_t sz, itermax;
    uint32_t slen;
    char *str;
    tpl_bin *bin;
    tpl_pound_data *pd;
    int fidx;

    n = tpl_find_i(r,i);
    if (n == NULL) {
        tpl_hook.oops("invalid index %d to tpl_pack\n", i);
        return -1;
    }

    if (((tpl_root_data*)(r->data))->flags & TPL_RDONLY) {
        /* convert to an writeable tpl, initially empty */
        tpl_free_keep_map(r);
    }

    ((tpl_root_data*)(r->data))->flags |= TPL_WRONLY;

    if (n->type == TPL_TYPE_ARY) datav = tpl_extend_backbone(n);
    child = n->children;
    while(child) {
        switch(child->type) {
            case TPL_TYPE_BYTE:
            case TPL_TYPE_DOUBLE:
            case TPL_TYPE_INT32:
            case TPL_TYPE_UINT32:
            case TPL_TYPE_INT64:
            case TPL_TYPE_UINT64:
            case TPL_TYPE_INT16:
            case TPL_TYPE_UINT16:
                /* no need to use fidx iteration here; we can copy multiple values in one memcpy */
                memcpy(child->data,child->addr,tpl_types[child->type].sz * child->num);
                if (datav) datav = tpl_cpv(datav,child->data,tpl_types[child->type].sz * child->num);
                if (n->type == TPL_TYPE_ARY) n->ser_osz += tpl_types[child->type].sz * child->num;
                break;
            case TPL_TYPE_BIN:
                /* copy the buffer to be packed */ 
                slen = ((tpl_bin*)child->addr)->sz;
                if (slen >0) {
                    str = tpl_hook.malloc(slen);
                    if (!str) fatal_oom();
                    memcpy(str,((tpl_bin*)child->addr)->addr,slen);
                } else str = NULL;
                /* and make a tpl_bin to point to it */
                bin = tpl_hook.malloc(sizeof(tpl_bin));
                if (!bin) fatal_oom();
                bin->addr = str;
                bin->sz = slen;
                /* now pack its pointer, first deep freeing any pre-existing bin */
                if (*(tpl_bin**)(child->data) != NULL) {
                    if ((*(tpl_bin**)(child->data))->sz != 0) {
                            tpl_hook.free( (*(tpl_bin**)(child->data))->addr );
                    }
                    tpl_hook.free(*(tpl_bin**)(child->data));  
                }
                memcpy(child->data,&bin,sizeof(tpl_bin*));
                if (datav) {
                    datav = tpl_cpv(datav, &bin, sizeof(tpl_bin*));
                    *(tpl_bin**)(child->data) = NULL;  
                }
                if (n->type == TPL_TYPE_ARY) {
                    n->ser_osz += sizeof(uint32_t); /* binary buf len word */
                    n->ser_osz += bin->sz;          /* binary buf */
                }
                break;
            case TPL_TYPE_STR:
                for(fidx=0; fidx < child->num; fidx++) {
                  /* copy the string to be packed. slen includes \0. this 
                     block also works if the string pointer is NULL. */
                  char *caddr = ((char**)child->addr)[fidx];
                  char **cdata = &((char**)child->data)[fidx];
                  slen = caddr ?  (strlen(caddr) + 1) : 0;
                  if (slen) {
                    str = tpl_hook.malloc(slen);
                    if (!str) fatal_oom();
                    memcpy(str,caddr,slen); /* include \0 */
                  } else {
                    str = NULL;
                  } 
                  /* now pack its pointer, first freeing any pre-existing string */
                  if (*cdata != NULL) {
                      tpl_hook.free(*cdata);  
                  }
                  memcpy(cdata,&str,sizeof(char*));
                  if (datav) {
                      datav = tpl_cpv(datav, &str, sizeof(char*));
                      *cdata = NULL;  
                  }
                  if (n->type == TPL_TYPE_ARY) {
                      n->ser_osz += sizeof(uint32_t); /* string len word */
                      if (slen>1) n->ser_osz += slen-1;/* string (without nul) */
                  }
                }
                break;
            case TPL_TYPE_ARY:
                /* copy the child's tpl_atype* and reset it to empty */
                if (datav) {
                    sz = ((tpl_atyp*)(child->data))->sz;
                    datav = tpl_cpv(datav, &child->data, sizeof(void*));
                    child->data = tpl_hook.malloc(sizeof(tpl_atyp));
                    if (!child->data) fatal_oom();
                    ((tpl_atyp*)(child->data))->num = 0;
                    ((tpl_atyp*)(child->data))->sz = sz;
                    ((tpl_atyp*)(child->data))->bb = NULL;
                    ((tpl_atyp*)(child->data))->bbtail = NULL;
                }
                /* parent is array? then bubble up child array's ser_osz */
                if (n->type == TPL_TYPE_ARY) {
                    n->ser_osz += sizeof(uint32_t); /* array len word */
                    n->ser_osz += child->ser_osz;   /* child array ser_osz */
                    child->ser_osz = 0;             /* reset child array ser_osz */
                }
                break;

            case TPL_TYPE_POUND: 
                /* we need to iterate n times over preceding nodes in S(...). 
                 * we may be in the midst of an iteration each time or starting. */
                 pd = (tpl_pound_data*)child->data;
                 itermax = child->num;

                 /* itermax is total num of iterations needed  */
                 /* pd->iternum is current iteration index  */
                 /* pd->inter_elt_len is element-to-element len of contiguous structs */
                 /* pd->iter_start_node is where we jump to at each iteration. */

                 if (++(pd->iternum) < itermax) {

                   /* in start or midst of loop. advance addr/data pointers. */
                   for(np=pd->iter_start_node; np != child; np = np->next) {
                     np->data = (char*)(np->data) + 
                          (tpl_types[np->type].sz * np->num);
                     np->addr = (char*)(np->addr) + pd->inter_elt_len;
                   }
                   /* do next iteration */
                   child = pd->iter_start_node;
                   continue;

                 } else { /* loop complete. */
                 
                   /* reset iteration index and addr/data pointers. */
                   pd->iternum = 0;
                   for(np=pd->iter_start_node; np != child; np = np->next) {
                     np->data = (char*)(np->data) - ((itermax-1) *
                                                      tpl_types[np->type].sz * 
                                                      np->num);
                     np->addr = (char*)(np->addr) - ((itermax-1) * pd->inter_elt_len);
                   }

                 }
                break;
            default:
                tpl_hook.fatal("unsupported format character\n");
                break;
        }
        child=child->next;
    }
    return 0;
}

TPL_API int tpl_unpack(tpl_node *r, int i) {
    tpl_node *n, *c, *np;
    uint32_t slen;
    int rc=1, fidx;
    char *str;
    void *dv=NULL, *caddr;
    size_t A_bytes, itermax;
    tpl_pound_data *pd;
    void *img;
    size_t sz;


    /* handle unusual case of tpl_pack,tpl_unpack without an 
     * intervening tpl_dump. do a dump/load implicitly. */
    if (((tpl_root_data*)(r->data))->flags & TPL_WRONLY) {
        if (tpl_dump(r,TPL_MEM,&img,&sz) != 0) return -1;
        if (tpl_load(r,TPL_MEM|TPL_UFREE,img,sz) != 0) {
            tpl_hook.free(img);
            return -1;
        };
    }

    n = tpl_find_i(r,i);
    if (n == NULL) {
        tpl_hook.oops("invalid index %d to tpl_unpack\n", i);
        return -1;
    }

    /* either root node or an A node */
    if (n->type == TPL_TYPE_ROOT) {
        dv = tpl_find_data_start( ((tpl_root_data*)(n->data))->mmap.text );
    } else if (n->type == TPL_TYPE_ARY) {
        if (((tpl_atyp*)(n->data))->num <= 0) return 0; /* array consumed */
        else rc = ((tpl_atyp*)(n->data))->num--;
        dv = ((tpl_atyp*)(n->data))->cur;
        if (!dv) tpl_hook.fatal("must unpack parent of node before node itself\n");
    }

    c = n->children;
    while (c) {
        switch (c->type) {
            case TPL_TYPE_BYTE:
            case TPL_TYPE_DOUBLE:
            case TPL_TYPE_INT32:
            case TPL_TYPE_UINT32:
            case TPL_TYPE_INT64:
            case TPL_TYPE_UINT64:
            case TPL_TYPE_INT16:
            case TPL_TYPE_UINT16:
                /* unpack elements of cross-endian octothorpic array individually */
                if (((tpl_root_data*)(r->data))->flags & TPL_XENDIAN) {
                    for(fidx=0; fidx < c->num; fidx++) {
                        caddr = (void*)((uintptr_t)c->addr + (fidx * tpl_types[c->type].sz));
                        memcpy(caddr,dv,tpl_types[c->type].sz);
                        tpl_byteswap(caddr, tpl_types[c->type].sz);
                        dv = (void*)((uintptr_t)dv + tpl_types[c->type].sz);
                    }
                } else {
                    /* bulk unpack ok if not cross-endian */
                    memcpy(c->addr, dv, tpl_types[c->type].sz * c->num);
                    dv = (void*)((uintptr_t)dv + tpl_types[c->type].sz * c->num);
                }
                break;
            case TPL_TYPE_BIN:
                memcpy(&slen,dv,sizeof(uint32_t));
                if (((tpl_root_data*)(r->data))->flags & TPL_XENDIAN)
                    tpl_byteswap(&slen, sizeof(uint32_t));
                if (slen > 0) {
                    str = (char*)tpl_hook.malloc(slen);
                    if (!str) fatal_oom();
                } else str=NULL;
                dv = (void*)((uintptr_t)dv + sizeof(uint32_t));
                if (slen>0) memcpy(str,dv,slen);
                memcpy(&(((tpl_bin*)c->addr)->addr),&str,sizeof(void*));
                memcpy(&(((tpl_bin*)c->addr)->sz),&slen,sizeof(uint32_t));
                dv = (void*)((uintptr_t)dv + slen);
                break;
            case TPL_TYPE_STR:
                for(fidx=0; fidx < c->num; fidx++) {
                  memcpy(&slen,dv,sizeof(uint32_t));
                  if (((tpl_root_data*)(r->data))->flags & TPL_XENDIAN)
                      tpl_byteswap(&slen, sizeof(uint32_t));
                  if (((tpl_root_data*)(r->data))->flags & TPL_OLD_STRING_FMT)
                    slen += 1;
                  dv = (void*)((uintptr_t)dv + sizeof(uint32_t));
                  if (slen) {  /* slen includes \0 */
                    str = (char*)tpl_hook.malloc(slen);
                    if (!str) fatal_oom();
                    if (slen>1) memcpy(str,dv,slen-1);
                    str[slen-1] = '\0'; /* nul terminate */
                    dv = (void*)((uintptr_t)dv + slen-1);
                  } else str=NULL;
                  memcpy(&((char**)c->addr)[fidx],&str,sizeof(char*));
                }
                break;
            case TPL_TYPE_POUND:
                /* iterate over preceding nodes */
                pd = (tpl_pound_data*)c->data;
                itermax = c->num;
                if (++(pd->iternum) < itermax) {
                  /* in start or midst of loop. advance addr/data pointers. */
                  for(np=pd->iter_start_node; np != c; np = np->next) {
                    np->addr = (char*)(np->addr) + pd->inter_elt_len;
                  }
                  /* do next iteration */
                  c = pd->iter_start_node;
                  continue;

                } else { /* loop complete. */
                
                  /* reset iteration index and addr/data pointers. */
                  pd->iternum = 0;
                  for(np=pd->iter_start_node; np != c; np = np->next) {
                    np->addr = (char*)(np->addr) - ((itermax-1) * pd->inter_elt_len);
                  }

                }
                break;
            case TPL_TYPE_ARY:
                if (tpl_serlen(r,c,dv, &A_bytes) == -1) 
                    tpl_hook.fatal("internal error in unpack\n");
                memcpy( &((tpl_atyp*)(c->data))->num, dv, sizeof(uint32_t));
                if (((tpl_root_data*)(r->data))->flags & TPL_XENDIAN)
                    tpl_byteswap(&((tpl_atyp*)(c->data))->num, sizeof(uint32_t));
                ((tpl_atyp*)(c->data))->cur = (void*)((uintptr_t)dv+sizeof(uint32_t));
                dv = (void*)((uintptr_t)dv + A_bytes);
                break;
            default:
                tpl_hook.fatal("unsupported format character\n");
                break;
        }

        c = c->next;
    }
    if (n->type == TPL_TYPE_ARY) ((tpl_atyp*)(n->data))->cur = dv; /* next element */
    return rc;
}

/* Specialized function that unpacks only the root's A nodes, after tpl_load  */
static int tpl_unpackA0(tpl_node *r) {
    tpl_node *n, *c;
    uint32_t slen;
    int rc=1,fidx,i;
    void *dv;
    size_t A_bytes, itermax;
    tpl_pound_data *pd;

    n = r;
    dv = tpl_find_data_start( ((tpl_root_data*)(r->data))->mmap.text);

    c=n->children;
    while (c)  {
        switch (c->type) {
            case TPL_TYPE_BYTE:
            case TPL_TYPE_DOUBLE:
            case TPL_TYPE_INT32:
            case TPL_TYPE_UINT32:
            case TPL_TYPE_INT64:
            case TPL_TYPE_UINT64:
            case TPL_TYPE_INT16:
            case TPL_TYPE_UINT16:
                for(fidx=0;fidx < c->num; fidx++) {
                    dv = (void*)((uintptr_t)dv + tpl_types[c->type].sz);
                }
                break;
            case TPL_TYPE_BIN:
                memcpy(&slen,dv,sizeof(uint32_t));
                if (((tpl_root_data*)(r->data))->flags & TPL_XENDIAN)
                    tpl_byteswap(&slen, sizeof(uint32_t));
                dv = (void*)((uintptr_t)dv + sizeof(uint32_t));
                dv = (void*)((uintptr_t)dv + slen);
                break;
            case TPL_TYPE_STR:
                for(i=0; i<c->num; i++) {
                  memcpy(&slen,dv,sizeof(uint32_t));
                  if (((tpl_root_data*)(r->data))->flags & TPL_XENDIAN)
                      tpl_byteswap(&slen, sizeof(uint32_t));
                  if (((tpl_root_data*)(r->data))->flags & TPL_OLD_STRING_FMT)
                    slen += 1;
                  dv = (void*)((uintptr_t)dv + sizeof(uint32_t));
                  if (slen>1) dv = (void*)((uintptr_t)dv + slen-1);
                }
                break;
            case TPL_TYPE_POUND:
                /* iterate over the preceding nodes */
                itermax = c->num;
                pd = (tpl_pound_data*)c->data;
                if (++(pd->iternum) < itermax) {
                  c = pd->iter_start_node;
                  continue;
                } else { /* loop complete. */
                  pd->iternum = 0;
                }
                break;
            case TPL_TYPE_ARY:
                if ( tpl_serlen(r,c,dv, &A_bytes) == -1) 
                    tpl_hook.fatal("internal error in unpackA0\n");
                memcpy( &((tpl_atyp*)(c->data))->num, dv, sizeof(uint32_t));
                if (((tpl_root_data*)(r->data))->flags & TPL_XENDIAN)
                    tpl_byteswap(&((tpl_atyp*)(c->data))->num, sizeof(uint32_t));
                ((tpl_atyp*)(c->data))->cur = (void*)((uintptr_t)dv+sizeof(uint32_t));
                dv = (void*)((uintptr_t)dv + A_bytes);
                break;
            default:
                tpl_hook.fatal("unsupported format character\n");
                break;
        }
        c=c->next;
    }
    return rc;
}

/* In-place byte order swapping of a word of length "len" bytes */
static void tpl_byteswap(void *word, int len) {
    int i;
    char c, *w;
    w = (char*)word;
    for(i=0; i<len/2; i++) {
        c = w[i];
        w[i] = w[len-1-i];
        w[len-1-i] = c;
    }
}

static void tpl_fatal(char *fmt, ...) {
    va_list ap;
    char exit_msg[100];

    va_start(ap,fmt);
    vsnprintf(exit_msg, 100, fmt, ap);
    va_end(ap);

    tpl_hook.oops("%s", exit_msg);
    exit(-1);
}

TPL_API int tpl_gather(int mode, ...) {
    va_list ap;
    int fd,rc=0;
    size_t *szp,sz;
    void **img,*addr,*data;
    tpl_gather_t **gs;
    tpl_gather_cb *cb;

    va_start(ap,mode);
    switch (mode) {
        case TPL_GATHER_BLOCKING:
            fd = va_arg(ap,int);
            img = va_arg(ap,void*);
            szp = va_arg(ap,size_t*);
            rc = tpl_gather_blocking(fd,img,szp);
            break;
        case TPL_GATHER_NONBLOCKING:
            fd = va_arg(ap,int);
            gs = (tpl_gather_t**)va_arg(ap,void*);
            cb = (tpl_gather_cb*)va_arg(ap,tpl_gather_cb*);
            data = va_arg(ap,void*);
            rc = tpl_gather_nonblocking(fd,gs,cb,data);
            break;
        case TPL_GATHER_MEM:
            addr = va_arg(ap,void*);
            sz = va_arg(ap,size_t);
            gs = (tpl_gather_t**)va_arg(ap,void*);
            cb = (tpl_gather_cb*)va_arg(ap,tpl_gather_cb*);
            data = va_arg(ap,void*);
            rc = tpl_gather_mem(addr,sz,gs,cb,data);
            break;
        default:
            tpl_hook.fatal("unsupported tpl_gather mode %d\n",mode);
            break;
    }
    va_end(ap);
    return rc;
}

/* dequeue a tpl by reading until one full tpl image is obtained.
 * We take care not to read past the end of the tpl.
 * This is intended as a blocking call i.e. for use with a blocking fd.
 * It can be given a non-blocking fd, but the read spins if we have to wait.
 */
static int tpl_gather_blocking(int fd, void **img, size_t *sz) {
    char preamble[8];
    int i=0, rc;
    uint32_t tpllen;

    do { 
        rc = read(fd,&preamble[i],8-i);
        i += (rc>0) ? rc : 0;
    } while ((rc==-1 && (errno==EINTR||errno==EAGAIN)) || (rc>0 && i<8));

    if (rc<0) {
        tpl_hook.oops("tpl_gather_fd_blocking failed: %s\n", strerror(errno));
        return -1;
    } else if (rc == 0) {
        /* tpl_hook.oops("tpl_gather_fd_blocking: eof\n"); */
        return 0;
    } else if (i != 8) {
        tpl_hook.oops("internal error\n");
        return -1;
    }

    if (preamble[0] == 't' && preamble[1] == 'p' && preamble[2] == 'l') {
        memcpy(&tpllen,&preamble[4],4);
        if (tpl_needs_endian_swap(preamble)) tpl_byteswap(&tpllen,4);
    } else {
        tpl_hook.oops("tpl_gather_fd_blocking: non-tpl input\n");
        return -1;
    }

    /* malloc space for remainder of tpl image (overall length tpllen) 
     * and read it in
     */
    if (tpl_hook.gather_max > 0 && 
        tpllen > tpl_hook.gather_max) {
        tpl_hook.oops("tpl exceeds max length %d\n", 
            tpl_hook.gather_max);
        return -2;
    }
    *sz = tpllen;
    if ( (*img = tpl_hook.malloc(tpllen)) == NULL) {
        fatal_oom();
    }

    memcpy(*img,preamble,8);  /* copy preamble to output buffer */
    i=8;
    do { 
        rc = read(fd,&((*(char**)img)[i]),tpllen-i);
        i += (rc>0) ? rc : 0;
    } while ((rc==-1 && (errno==EINTR||errno==EAGAIN)) || (rc>0 && i<tpllen));

    if (rc<0) {
        tpl_hook.oops("tpl_gather_fd_blocking failed: %s\n", strerror(errno));
        tpl_hook.free(*img);
        return -1;
    } else if (rc == 0) {
        /* tpl_hook.oops("tpl_gather_fd_blocking: eof\n"); */
        tpl_hook.free(*img);
        return 0;
    } else if (i != tpllen) {
        tpl_hook.oops("internal error\n");
        tpl_hook.free(*img);
        return -1;
    }

    return 1;
}

/* Used by select()-driven apps which want to gather tpl images piecemeal */
/* the file descriptor must be non-blocking for this functino to work. */
static int tpl_gather_nonblocking( int fd, tpl_gather_t **gs, tpl_gather_cb *cb, void *data) {
    char buf[TPL_GATHER_BUFLEN], *img, *tpl;
    int rc, keep_looping, cbrc=0;
    size_t catlen;
    uint32_t tpllen;

    while (1) {
        rc = read(fd,buf,TPL_GATHER_BUFLEN);
        if (rc == -1) {
            if (errno == EINTR) continue;  /* got signal during read, ignore */
            if (errno == EAGAIN) return 1; /* nothing to read right now */
            else {
                tpl_hook.oops("tpl_gather failed: %s\n", strerror(errno));
                if (*gs) {
                    tpl_hook.free((*gs)->img);
                    tpl_hook.free(*gs);
                    *gs = NULL;
                }
                return -1;                 /* error, caller should close fd  */
            }
        } else if (rc == 0) {
            if (*gs) {
                tpl_hook.oops("tpl_gather: partial tpl image precedes EOF\n");
                tpl_hook.free((*gs)->img);
                tpl_hook.free(*gs);
                *gs = NULL;
            }
            return 0;                      /* EOF, caller should close fd */
        } else {
            /* concatenate any partial tpl from last read with new buffer */
            if (*gs) {
                catlen = (*gs)->len + rc;
                if (tpl_hook.gather_max > 0 && 
                    catlen > tpl_hook.gather_max) {
                    tpl_hook.free( (*gs)->img );
                    tpl_hook.free( (*gs) );
                    *gs = NULL;
                    tpl_hook.oops("tpl exceeds max length %d\n", 
                        tpl_hook.gather_max);
                    return -2;              /* error, caller should close fd */
                }
                if ( (img = tpl_hook.realloc((*gs)->img, catlen)) == NULL) {
                    fatal_oom();
                }
                memcpy(img + (*gs)->len, buf, rc);
                tpl_hook.free(*gs);
                *gs = NULL;
            } else {
                img = buf;
                catlen = rc;
            }
            /* isolate any full tpl(s) in img and invoke cb for each */
            tpl = img;
            keep_looping = (tpl+8 < img+catlen) ? 1 : 0;
            while (keep_looping) {
                if (strncmp("tpl", tpl, 3) != 0) {
                    tpl_hook.oops("tpl prefix invalid\n");
                    if (img != buf) tpl_hook.free(img);
                    tpl_hook.free(*gs);
                    *gs = NULL;
                    return -3; /* error, caller should close fd */
                }
                memcpy(&tpllen,&tpl[4],4);
                if (tpl_needs_endian_swap(tpl)) tpl_byteswap(&tpllen,4);
                if (tpl+tpllen <= img+catlen) {
                    cbrc = (cb)(tpl,tpllen,data);  /* invoke cb for tpl image */
                    tpl += tpllen;                 /* point to next tpl image */
                    if (cbrc < 0) keep_looping = 0;
                    else keep_looping = (tpl+8 < img+catlen) ? 1 : 0;
                } else keep_looping=0;
            } 
            /* check if app callback requested closure of tpl source */
            if (cbrc < 0) {
                tpl_hook.oops("tpl_fd_gather aborted by app callback\n");
                if (img != buf) tpl_hook.free(img);
                if (*gs) tpl_hook.free(*gs);
                *gs = NULL;
                return -4;
            }
            /* store any leftover, partial tpl fragment for next read */
            if (tpl == img && img != buf) {  
                /* consumed nothing from img!=buf */
                if ( (*gs = tpl_hook.malloc(sizeof(tpl_gather_t))) == NULL ) {
                    fatal_oom();
                }
                (*gs)->img = tpl;
                (*gs)->len = catlen;
            } else if (tpl < img+catlen) {  
                /* consumed 1+ tpl(s) from img!=buf or 0 from img==buf */
                if ( (*gs = tpl_hook.malloc(sizeof(tpl_gather_t))) == NULL ) {
                    fatal_oom();
                }
                if ( ((*gs)->img = tpl_hook.malloc(img+catlen - tpl)) == NULL ) {
                    fatal_oom();
                }
                (*gs)->len = img+catlen - tpl;
                memcpy( (*gs)->img, tpl, img+catlen - tpl);
                /* free partially consumed concat buffer if used */
                if (img != buf) tpl_hook.free(img); 
            } else {                        /* tpl(s) fully consumed */
                /* free consumed concat buffer if used */
                if (img != buf) tpl_hook.free(img); 
            }
        }
    } 
}

/* gather tpl piecemeal from memory buffer (not fd) e.g., from a lower-level api */
static int tpl_gather_mem( char *buf, size_t len, tpl_gather_t **gs, tpl_gather_cb *cb, void *data) {
    char *img, *tpl;
    int keep_looping, cbrc=0;
    size_t catlen;
    uint32_t tpllen;

    /* concatenate any partial tpl from last read with new buffer */
    if (*gs) {
        catlen = (*gs)->len + len;
        if (tpl_hook.gather_max > 0 && 
            catlen > tpl_hook.gather_max) {
            tpl_hook.free( (*gs)->img );
            tpl_hook.free( (*gs) );
            *gs = NULL;
            tpl_hook.oops("tpl exceeds max length %d\n", 
                tpl_hook.gather_max);
            return -2;              /* error, caller should stop accepting input from source*/
        }
        if ( (img = tpl_hook.realloc((*gs)->img, catlen)) == NULL) {
            fatal_oom();
        }
        memcpy(img + (*gs)->len, buf, len);
        tpl_hook.free(*gs);
        *gs = NULL;
    } else {
        img = buf;
        catlen = len;
    }
    /* isolate any full tpl(s) in img and invoke cb for each */
    tpl = img;
    keep_looping = (tpl+8 < img+catlen) ? 1 : 0;
    while (keep_looping) {
        if (strncmp("tpl", tpl, 3) != 0) {
            tpl_hook.oops("tpl prefix invalid\n");
            if (img != buf) tpl_hook.free(img);
            tpl_hook.free(*gs);
            *gs = NULL;
            return -3; /* error, caller should stop accepting input from source*/
        }
        memcpy(&tpllen,&tpl[4],4);
        if (tpl_needs_endian_swap(tpl)) tpl_byteswap(&tpllen,4);
        if (tpl+tpllen <= img+catlen) {
            cbrc = (cb)(tpl,tpllen,data);  /* invoke cb for tpl image */
            tpl += tpllen;               /* point to next tpl image */
            if (cbrc < 0) keep_looping = 0;
            else keep_looping = (tpl+8 < img+catlen) ? 1 : 0;
        } else keep_looping=0;
    } 
    /* check if app callback requested closure of tpl source */
    if (cbrc < 0) {
        tpl_hook.oops("tpl_mem_gather aborted by app callback\n");
        if (img != buf) tpl_hook.free(img);
        if (*gs) tpl_hook.free(*gs);
        *gs = NULL;
        return -4;
    }
    /* store any leftover, partial tpl fragment for next read */
    if (tpl == img && img != buf) {  
        /* consumed nothing from img!=buf */
        if ( (*gs = tpl_hook.malloc(sizeof(tpl_gather_t))) == NULL ) {
            fatal_oom();
        }
        (*gs)->img = tpl;
        (*gs)->len = catlen;
    } else if (tpl < img+catlen) {  
        /* consumed 1+ tpl(s) from img!=buf or 0 from img==buf */
        if ( (*gs = tpl_hook.malloc(sizeof(tpl_gather_t))) == NULL ) {
            fatal_oom();
        }
        if ( ((*gs)->img = tpl_hook.malloc(img+catlen - tpl)) == NULL ) {
            fatal_oom();
        }
        (*gs)->len = img+catlen - tpl;
        memcpy( (*gs)->img, tpl, img+catlen - tpl);
        /* free partially consumed concat buffer if used */
        if (img != buf) tpl_hook.free(img); 
    } else {                        /* tpl(s) fully consumed */
        /* free consumed concat buffer if used */
        if (img != buf) tpl_hook.free(img); 
    }
    return 1;
}