summaryrefslogtreecommitdiff
path: root/msodumper/docrecord.py
blob: dea48bcd4a7872e7c79217e7387d2f56966730a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
#!/usr/bin/env python3
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
from builtins import range
import locale
from . import globals
from .binarystream import BinaryStream
from . import docsprm
from . import msodraw


def getWordModel(mainStream):
    model = globals.ModelBase(globals.ModelBase.HostAppType.Word)
    model.delayStream = mainStream
    return model


class FcCompressed(BinaryStream):
    """The FcCompressed structure specifies the location of text in the WordDocument Stream."""
    def __init__(self, bytes, mainStream, offset, size):
        BinaryStream.__init__(self, bytes, mainStream=mainStream)
        self.pos = offset
        self.size = size

        buf = self.readuInt32()
        self.fc = buf & ((2 ** 32 - 1) >> 2)  # bits 0..29
        self.fCompressed = self.getBit(buf, 30)
        self.r1 = self.getBit(buf, 31)

    def dump(self):
        print('<fcCompressed type="FcCompressed" offset="%d" size="%d bytes">' % (self.pos, self.size))
        self.printAndSet("fc", self.fc)
        self.printAndSet("fCompressed", self.fCompressed)
        self.printAndSet("r1", self.r1)
        print('</fcCompressed>')


class Pcd(BinaryStream):
    """The Pcd structure specifies the location of text in the WordDocument Stream and additional properties for this text."""
    def __init__(self, bytes, mainStream, offset, size):
        BinaryStream.__init__(self, bytes, mainStream=mainStream)
        self.pos = offset
        self.size = size

        buf = self.readuInt16()
        self.fNoParaLast = self.getBit(buf, 0)
        self.fR1 = self.getBit(buf, 1)
        self.fDirty = self.getBit(buf, 2)
        self.fR2 = buf & (2 ** 13 - 1)
        self.fc = FcCompressed(self.bytes, self.mainStream, self.pos, 4)
        self.pos += 4

    def dump(self):
        print('<pcd type="Pcd" offset="%d" size="%d bytes">' % (self.pos, self.size))
        self.printAndSet("fNoParaLast", self.fNoParaLast)
        self.printAndSet("fR1", self.fR1)
        self.printAndSet("fDirty", self.fDirty)
        self.printAndSet("fR2", self.fR2)
        self.fc.dump()
        print('</pcd>')


class PLC:
    """The PLC structure is an array of character positions followed by an array of data elements."""
    def __init__(self, totalSize, structSize):
        self.totalSize = totalSize
        self.structSize = structSize

    def getElements(self):
        return (self.totalSize - 4) // (4 + self.structSize)  # defined by 2.2.2

    def getOffset(self, pos, i):
        return self.getPLCOffset(pos, self.getElements(), self.structSize, i)

    @staticmethod
    def getPLCOffset(pos, elements, structSize, i):
        return pos + (4 * (elements + 1)) + (structSize * i)


class BKC(BinaryStream):
    """The BKC structure contains information about how a bookmark interacts with tables."""
    def __init__(self, bkc):
        self.bkc = bkc

    def dump(self):
        print('<bkc type="BKC">')
        self.printAndSet("itcFirst", self.bkc & 0x007f)  # 1..7th bits
        self.printAndSet("fPub", self.getBit(self.bkc, 8))
        self.printAndSet("itcLim", (self.bkc & 0x3f00) >> 8)  # 9..14th bits
        self.printAndSet("fNative", self.getBit(self.bkc, 15))
        self.printAndSet("fCol", self.getBit(self.bkc, 16))
        print('</bkc>')


class FBKF(BinaryStream):
    """The FBKF structure contains information about a bookmark."""
    def __init__(self, plcfAtnBkf, offset):
        BinaryStream.__init__(self, plcfAtnBkf.bytes)
        self.pos = offset

    def dump(self):
        print('<aFBKF type="FBKF" offset="%d">' % self.pos)
        self.printAndSet("ibkl", self.readuInt16())
        BKC(self.readuInt16()).dump()
        print('</aFBKF>')


class PlcfBkf(BinaryStream, PLC):
    """A PLCFBKF is a PLC whose data elements are FBKF structures."""
    def __init__(self, mainStream, offset, size):
        BinaryStream.__init__(self, mainStream.getTableStream().bytes, mainStream=mainStream)
        PLC.__init__(self, size, 4)  # 4 is defined by 2.8.10
        self.pos = offset
        self.size = size
        self.aCP = []
        self.aFBKF = []

    def dump(self):
        print('<plcfBkf type="PlcfBkf" offset="%d" size="%d bytes">' % (self.pos, self.size))
        pos = self.pos
        for i in range(self.getElements()):
            # aCp
            start = self.getuInt32(pos=pos)
            self.aCP.append(start)
            print('<aCP index="%d" bookmarkStart="%d">' % (i, start))
            pos += 4

            # aFBKF
            aFBKF = FBKF(self, self.getOffset(self.pos, i))
            aFBKF.dump()
            self.aFBKF.append(aFBKF)
            print('</aCP>')
        print('</plcfBkf>')


class FBKFD(BinaryStream):
    """Specified by [MS-DOC] 2.9.71, contains information about a bookmark."""
    def __init__(self, parent, offset):
        BinaryStream.__init__(self, parent.bytes)
        self.pos = offset

    def dump(self):
        print('<aFBKFD type="FBKFD" offset="%d">' % self.pos)
        FBKF(self, self.pos).dump()
        self.pos += 4
        self.printAndSet("cDepth", self.readInt16())
        print('</aFBKFD>')


class PlcfBkfd(BinaryStream, PLC):
    """Specified by [MS-DOC] 2.8.11, a PLC whose data elements are FBKFD structures."""
    def __init__(self, mainStream):
        BinaryStream.__init__(self, mainStream.getTableStream().bytes, mainStream=mainStream)
        PLC.__init__(self, mainStream.lcbPlcfBkfFactoid, 6)  # 6 is defined by 2.8.10
        self.pos = mainStream.fcPlcfBkfFactoid
        self.size = mainStream.lcbPlcfBkfFactoid
        self.aCP = []
        self.aFBKFD = []

    def dump(self):
        print('<plcfBkfd type="PlcfBkfd" offset="%d" size="%d bytes">' % (self.pos, self.size))
        pos = self.pos
        for i in range(self.getElements()):
            # aCp
            start = self.getuInt32(pos=pos)
            self.aCP.append(start)
            print('<aCP index="%d" bookmarkStart="%d">' % (i, start))
            pos += 4

            # aFBKFD
            aFBKFD = FBKFD(self, self.getOffset(self.pos, i))
            aFBKFD.dump()
            self.aFBKFD.append(aFBKFD)
            print('</aCP>')
        print('</plcfBkfd>')


class FBKLD(BinaryStream):
    """Specified by [MS-DOC] 2.9.72, contains information about a bookmark."""
    def __init__(self, parent, offset):
        BinaryStream.__init__(self, parent.bytes)
        self.pos = offset

    def dump(self):
        print('<aFBKLD type="FBKLD" offset="%d">' % self.pos)
        self.printAndSet("ibkf", self.readuInt16())
        self.printAndSet("cDepth", self.readuInt16())
        print('</aFBKLD>')


class PlcfBkld(BinaryStream, PLC):
    """Specified by [MS-DOC] 2.8.13, a PLC whose data elements are FBKLD structures."""
    def __init__(self, mainStream):
        BinaryStream.__init__(self, mainStream.getTableStream().bytes, mainStream=mainStream)
        PLC.__init__(self, mainStream.lcbPlcfBklFactoid, 4)  # 4 is defined by the spec
        self.pos = mainStream.fcPlcfBklFactoid
        self.size = mainStream.lcbPlcfBklFactoid
        self.aCP = []
        self.aFBKLD = []

    def dump(self):
        print('<plcfBkld type="PlcfBkld" offset="%d" size="%d bytes">' % (self.pos, self.size))
        pos = self.pos
        for i in range(self.getElements()):
            # aCp
            start = self.getuInt32(pos=pos)
            self.aCP.append(start)
            print('<aCP index="%d" bookmarkEnd="%d">' % (i, start))
            pos += 4

            # aFBKLD
            aFBKLD = FBKLD(self, self.getOffset(self.pos, i))
            aFBKLD.dump()
            self.aFBKLD.append(aFBKLD)
            print('</aCP>')
        print('</plcfBkld>')


class FactoidSpls(BinaryStream):
    """Specified by [MS-DOC] 2.9.67, an SPLS structure that specifies the state
    of the smart tag recognizer over a range of text."""
    def __init__(self, parent, offset):
        BinaryStream.__init__(self, parent.bytes)
        self.pos = offset

    def dump(self):
        print('<factoidSpls type="FactoidSpls" offset="%d">' % self.pos)
        SPLS("spls", self, self.pos).dump()
        print('</factoidSpls>')


class Plcffactoid(BinaryStream, PLC):
    """Specified by [MS-DOC] 2.8.18, a PLC whose data elements are FactoidSpls structures."""
    def __init__(self, mainStream):
        BinaryStream.__init__(self, mainStream.getTableStream().bytes, mainStream=mainStream)
        PLC.__init__(self, mainStream.lcbPlcffactoid, 2)  # 2 is defined by the spec
        self.pos = mainStream.fcPlcffactoid
        self.size = mainStream.lcbPlcffactoid
        self.aCPs = []
        self.aFactoidSpls = []

    def dump(self):
        print('<plcffactoid type="Plcffactoid" offset="%d" size="%d bytes">' % (self.pos, self.size))
        pos = self.pos
        for i in range(self.getElements() + 1):
            # aCp
            aCp = self.getuInt32(pos=pos)
            self.aCPs.append(aCp)
            print('<aCP index="%d" value="%d">' % (i, aCp))
            pos += 4

            if i < self.getElements():
                # aFactoidSpls
                aFactoidSpls = FactoidSpls(self, self.getOffset(self.pos, i))
                aFactoidSpls.dump()
                self.aFactoidSpls.append(aFactoidSpls)
            print('</aCP>')
        print('</plcffactoid>')


class Fldch(BinaryStream):
    """The fldch structure determines the type of the field character."""
    def __init__(self, parent):
        BinaryStream.__init__(self, parent.bytes)
        self.pos = parent.pos
        self.parent = parent

    def dump(self):
        print('<fldch type="fldch" offset="%d" size="1 byte">' % self.pos)
        buf = self.readuInt8()
        self.printAndSet("ch", buf & 0x1f)  # 1..5th bits
        self.printAndSet("reserved", (buf & 0xe0) >> 5)  # 6..8th bits
        print('</fldch>')
        self.parent.pos = self.pos


class Fld(BinaryStream):
    """The Fld structure specifies a field character."""
    def __init__(self, parent, offset):
        BinaryStream.__init__(self, parent.bytes)
        self.pos = offset

    def dump(self):
        print('<fld type="FLD" offset="%d" size="2 bytes">' % self.pos)
        self.fldch = Fldch(self)
        self.fldch.dump()
        self.printAndSet("grffld", self.readuInt8())  # TODO parse flt and grffldEnd
        print('</fld>')


class PlcFld(BinaryStream, PLC):
    """The Plcfld structure specifies the location of fields in the document."""
    def __init__(self, mainStream):
        BinaryStream.__init__(self, mainStream.getTableStream().bytes, mainStream=mainStream)
        PLC.__init__(self, mainStream.lcbPlcfFldMom, 2)  # 2 is defined by 2.8.25
        self.pos = mainStream.fcPlcfFldMom
        self.size = mainStream.lcbPlcfFldMom

    def dump(self):
        print('<plcFld type="PlcFld" offset="%d" size="%d bytes">' % (self.pos, self.size))
        pos = self.pos
        aFlds = []
        for i in range(self.getElements()):
            # aCp
            value = self.getuInt32(pos=pos)
            print('<aCP index="%d" value="%d">' % (i, value))
            pos += 4

            # aFld
            aFld = Fld(self, self.getOffset(self.pos, i))
            aFld.dump()

            # This is a separator and the previous was a start: display the field instructions.
            if aFld.fldch.ch == 0x14 and aFlds[-1][1].fldch.ch == 0x13:
                print('<transformed value="%s"/>' % self.quoteAttr(self.mainStream.retrieveCPs(aFlds[-1][0] + 1, value)))
            # This is an end and the previous was a separator: display the field result.
            elif aFld.fldch.ch == 0x15 and aFlds[-1][1].fldch.ch == 0x14:
                print('<transformed value="%s"/>' % self.quoteAttr(self.mainStream.retrieveCPs(aFlds[-1][0] + 1, value)))
            aFlds.append((value, aFld))
            print('</aCP>')
        print('</plcFld>')


class PlcfBkl(BinaryStream, PLC):
    """The Plcfbkl structure is a PLC that contains only CPs and no additional data."""
    def __init__(self, mainStream, offset, size, start):
        BinaryStream.__init__(self, mainStream.getTableStream().bytes, mainStream=mainStream)
        PLC.__init__(self, size, 0)  # 0 is defined by 2.8.12
        self.pos = offset
        self.size = size
        self.start = start

    def dump(self):
        print('<plcfBkl type="PlcfBkl" offset="%d" size="%d bytes">' % (self.pos, self.size))
        pos = self.pos
        for i in range(self.getElements()):
            # aCp
            end = self.getuInt32(pos=pos)
            print('<aCP index="%d" bookmarkEnd="%d">' % (i, end))
            start = self.start.aCP[self.start.aFBKF[i].ibkl]
            print('<transformed value="%s"/>' % self.quoteAttr(self.mainStream.retrieveCPs(start, end)))
            pos += 4
            print('</aCP>')
        print('</plcfBkl>')


class PlcPcd(BinaryStream, PLC):
    """The PlcPcd structure is a PLC whose data elements are Pcds (8 bytes each)."""
    def __init__(self, bytes, mainStream, offset, size):
        BinaryStream.__init__(self, bytes, mainStream=mainStream)
        PLC.__init__(self, size, 8)  # 8 is defined by 2.8.35
        self.pos = offset
        self.size = size
        self.aCp = []
        self.aPcd = []
        self.ranges = []

        pos = self.pos
        for i in range(self.getElements()):
            # aCp
            start = self.getuInt32(pos=pos)
            end = self.getuInt32(pos=pos + 4)
            self.ranges.append((start, end))
            self.aCp.append(start)
            pos += 4

            # aPcd
            aPcd = Pcd(self.bytes, self.mainStream, self.getOffset(self.pos, i), 8)
            self.aPcd.append(aPcd)

    def dump(self):
        print('<plcPcd type="PlcPcd" offset="%d" size="%d bytes">' % (self.pos, self.size))
        for i in range(self.getElements()):
            start, end = self.ranges[i]
            print('<aCP index="%d" start="%d" end="%d">' % (i, start, end))
            self.aPcd[i].dump()
            print('<transformed value="%s"/>' % self.quoteAttr(self.mainStream.retrieveCPs(start, end)))
            print('</aCP>')
        print('</plcPcd>')


class Sepx(BinaryStream):
    """The Sepx structure specifies an array of Prl structures and the size of the array."""
    def __init__(self, sed):
        BinaryStream.__init__(self, sed.plcfSed.mainStream.bytes)
        self.pos = sed.fcSepx

    def dump(self):
        print('<sepx type="Sepx" offset="%d">' % self.pos)
        self.printAndSet("cb", self.readInt16())
        pos = self.pos
        while (self.cb - (pos - self.pos)) > 0:
            prl = Prl(self, pos)
            prl.dump()
            pos += prl.getSize()
        print('</sepx>')


class Sed(BinaryStream):
    """The Sed structure specifies the location of the section properties."""
    size = 12  # defined by 2.8.26

    def __init__(self, plcfSed, offset):
        BinaryStream.__init__(self, plcfSed.bytes)
        self.pos = offset
        self.plcfSed = plcfSed

    def dump(self):
        print('<aSed type="Sed" offset="%d" size="%d bytes">' % (self.pos, Sed.size))
        self.printAndSet("fn", self.readuInt16())
        self.printAndSet("fcSepx", self.readuInt32())
        if self.fcSepx != 0xffffffff:
            Sepx(self).dump()
        self.printAndSet("fnMpr", self.readuInt16())
        self.printAndSet("fcMpr", self.readuInt32())
        print('</aSed>')


class PlcfSed(BinaryStream, PLC):
    """The PlcfSed structure is a PLC structure where the data elements are Sed structures."""
    def __init__(self, mainStream, offset, size):
        BinaryStream.__init__(self, mainStream.getTableStream().bytes, mainStream=mainStream)
        PLC.__init__(self, size, Sed.size)
        self.pos = offset
        self.size = size

    def dump(self):
        print('<plcfSed type="PlcfSed" offset="%d" size="%d bytes">' % (self.pos, self.size))
        pos = self.pos
        for i in range(self.getElements()):
            # aCp
            start = self.getuInt32(pos=pos)
            end = self.getuInt32(pos=pos + 4)
            print('<aCP index="%d" start="%d" end="%d">' % (i, start, end))
            pos += 4

            # aSed
            aSed = Sed(self, self.getOffset(self.pos, i))
            aSed.dump()

            print('<transformed value="%s"/>' % self.quoteAttr(self.mainStream.retrieveCPs(start, end)))
            print('</aCP>')
        print('</plcfSed>')


class Tcg(BinaryStream):
    """The Tcg structure specifies command-related customizations."""
    def __init__(self, mainStream, offset, size):
        BinaryStream.__init__(self, mainStream.getTableStream().bytes)
        self.pos = offset
        self.size = size

    def dump(self):
        print('<tcg type="Tcg" offset="%d" size="%d bytes">' % (self.pos, self.size))
        self.printAndSet("nTcgVer", self.readuInt8())
        self.printAndSet("chTerminator", self.readuInt8())
        if self.chTerminator != 0x40:
            print('<todo what="Tcg: chTerminator != 0x40"/>')
        print('</tcg>')


class Sty(BinaryStream):
    """The Sty structure specifies the type of the selection that was made."""
    def __init__(self, parent):
        BinaryStream.__init__(self, parent.bytes)
        self.pos = parent.pos
        self.parent = parent

    def dump(self):
        value = self.readuInt16()
        styMap = {
            0x0000: "styNil",
            0x0001: "styChar",
            0x0002: "styWord",
            0x0003: "stySent",
            0x0004: "styPara",
            0x0005: "styLine",
            0x000C: "styCol",
            0x000D: "styRow",
            0x000E: "styColAll",
            0x000F: "styWholeTable",
            0x001B: "styPrefix",
        }
        print('<sty name="%s" value="%s"/>' % (styMap[value], hex(value)))
        self.parent.pos = self.pos


class Selsf(BinaryStream):
    size = 36  # defined by 2.9.241
    """The Selsf structure specifies the last selection that was made to the document."""
    def __init__(self, mainStream):
        BinaryStream.__init__(self, mainStream.getTableStream().bytes)
        self.pos = mainStream.fcWss
        self.size = mainStream.lcbWss
        self.mainStream = mainStream

    def dump(self):
        print('<selsf type="Selsf" offset="%d" size="%d bytes">' % (self.pos, self.size))

        buf = self.readuInt16()
        self.printAndSet("fRightward", self.getBit(buf, 0))
        self.printAndSet("unused1", self.getBit(buf, 1))
        self.printAndSet("fWithinCell", self.getBit(buf, 2))
        self.printAndSet("fTableAnchor", self.getBit(buf, 3))
        self.printAndSet("fTableSelNonShrink", self.getBit(buf, 4))
        self.printAndSet("unused2", self.getBit(buf, 5))
        self.printAndSet("fDiscontiguous", self.getBit(buf, 6))
        self.printAndSet("fPrefix", self.getBit(buf, 7))
        self.printAndSet("fShape", self.getBit(buf, 8))
        self.printAndSet("fFrame", self.getBit(buf, 9))
        self.printAndSet("fColumn", self.getBit(buf, 10))
        self.printAndSet("fTable", self.getBit(buf, 11))
        self.printAndSet("fGraphics", self.getBit(buf, 12))
        self.printAndSet("fBlock", self.getBit(buf, 13))
        self.printAndSet("unused3", self.getBit(buf, 14))
        self.printAndSet("fIns", self.getBit(buf, 15))

        buf = self.readuInt8()
        self.printAndSet("fForward", buf & 0x7f)  # 1..7th bits
        self.printAndSet("fPrefixW2007", self.getBit(buf, 7))

        self.printAndSet("fInsEnd", self.readuInt8())
        self.printAndSet("cpFirst", self.readuInt32())
        self.printAndSet("cpLim", self.readuInt32())
        self.printAndSet("unused4", self.readuInt32())
        self.printAndSet("blktblSel", self.readuInt32())
        self.printAndSet("cpAnchor", self.readuInt32())
        Sty(self).dump()
        self.printAndSet("unused5", self.readuInt16())
        self.printAndSet("cpAnchorShrink", self.readuInt32())
        self.printAndSet("xaTableLeft", self.readInt16())
        self.printAndSet("xaTableRight", self.readInt16())
        assert self.pos == self.mainStream.fcWss + Selsf.size
        print('</selsf>')


class COLORREF(BinaryStream):
    """The COLORREF structure specifies a color in terms of its red, green, and blue components."""
    def __init__(self, parent):
        BinaryStream.__init__(self, parent.bytes)
        self.pos = parent.pos
        self.red = self.readuInt8()
        self.green = self.readuInt8()
        self.blue = self.readuInt8()
        self.fAuto = self.readuInt8()
        parent.pos = self.pos

    def dump(self, name):
        print('<%s type="COLORREF">' % name)
        self.printAndSet("red", self.red)
        self.printAndSet("green", self.green)
        self.printAndSet("blue", self.blue)
        self.printAndSet("fAuto", self.fAuto)
        print('</%s>' % name)


class BRC(BinaryStream):
    """The Brc structure specifies a border."""
    def __init__(self, parent, name="brc"):
        BinaryStream.__init__(self, parent.bytes)
        self.pos = parent.pos
        self.parent = parent
        self.name = name
        self.posOrig = self.pos
        self.cv = COLORREF(self)
        self.dptLineWidth = self.readuInt8()
        self.brcType = self.readuInt8()
        buf = self.readuInt16()
        self.dptSpace = buf & 0x1f  # 1..5th bits
        self.fShadow = self.getBit(buf, 5)
        self.fFrame = self.getBit(buf, 6)
        self.fReserved = (buf & 0xff80) >> 7  # 8..16th bits

    def dump(self):
        print('<%s type="BRC" offset="%d">' % (self.name, self.posOrig))
        self.cv.dump("cv")
        self.printAndSet("dptLineWidth", self.dptLineWidth)
        self.printAndSet("brcType", self.brcType, dict=BrcType)
        self.printAndSet("dptSpace", self.dptSpace)
        self.printAndSet("fShadow", self.fShadow)
        self.printAndSet("fFrame", self.fFrame)
        self.printAndSet("fReserved", self.fReserved)
        print('</%s>' % self.name)
        self.parent.pos = self.pos


class PChgTabsDel(BinaryStream):
    """The PChgTabsDel structure specifies the locations at which custom tab stops are ignored."""
    def __init__(self, parent):
        BinaryStream.__init__(self, parent.bytes)
        self.parent = parent
        self.pos = parent.pos

    def dump(self):
        print('<pchgTabsDel type="PChgTabsDel" offset="%d">' % self.pos)
        self.printAndSet("cTabs", self.readuInt8())
        for i in range(self.cTabs):
            print('<rgdxaDel index="%d" value="%d"/>' % (i, self.readInt16()))
        print('</pchgTabsDel>')
        self.parent.pos = self.pos


class PChgTabsDelClose(BinaryStream):
    """The PChgTabsDelClose structure specifies the locations at which custom tab stops are ignored."""
    def __init__(self, parent):
        BinaryStream.__init__(self, parent.bytes)
        self.parent = parent
        self.pos = parent.pos

    def dump(self):
        print('<pchgTabsDelClose type="PChgTabsDelClose" offset="%d">' % self.pos)
        self.printAndSet("cTabs", self.readuInt8())
        for i in range(self.cTabs):
            print('<rgdxaDel index="%d" value="%d"/>' % (i, self.readInt16()))
        for i in range(self.cTabs):
            print('<rgdxaClose index="%d" value="%d"/>' % (i, self.readInt16()))
        print('</pchgTabsDelClose>')
        self.parent.pos = self.pos


class PChgTabsAdd(BinaryStream):
    """The PChgTabsAdd structure specifies the locations and properties of custom tab stops."""
    def __init__(self, parent):
        BinaryStream.__init__(self, parent.bytes)
        self.parent = parent
        self.pos = parent.pos

    def dump(self):
        print('<pchgTabsAdd type="PChgTabsAdd" offset="%d">' % self.pos)
        self.printAndSet("cTabs", self.readuInt8())
        for i in range(self.cTabs):
            print('<rgdxaAdd index="%d" value="%d"/>' % (i, self.readInt16()))
        for i in range(self.cTabs):
            print('<rgtbdAdd index="%d" value="%d"/>' % (i, self.readuInt8()))
        print('</pchgTabsAdd>')
        self.parent.pos = self.pos


class LSPD(BinaryStream):
    """Specifies the spacing between lines in a paragraph."""
    def __init__(self, parent):
        BinaryStream.__init__(self, parent.bytes)
        self.pos = parent.pos

    def dump(self):
        print('<lspd type="LSPD" offset="%d">' % self.pos)
        self.printAndSet("dyaLine", self.readuInt16())
        self.printAndSet("fMultLinespace", self.readuInt16())
        print('</lspd>')


class PChgTabsPapxOperand(BinaryStream):
    """The PChgTabsPapxOperand structure is used by sprmPChgTabsPapx to specify custom tab stops to be added or ignored."""
    def __init__(self, parent):
        BinaryStream.__init__(self, parent.bytes)
        self.pos = parent.pos

    def dump(self):
        print('<pchgTabsPapxOperand type="PChgTabsPapxOperand" offset="%d">' % self.pos)
        self.printAndSet("cb", self.readuInt8())
        PChgTabsDel(self).dump()
        PChgTabsAdd(self).dump()
        print('</pchgTabsPapxOperand>')


class PChgTabsOperand(BinaryStream):
    """The PChgTabsOperand structure is used by sprmPChgTabs to specify a list
    of custom tab stops to add and another list of custom tab stops to
    ignore."""
    def __init__(self, parent):
        BinaryStream.__init__(self, parent.bytes)
        self.pos = parent.pos

    def dump(self):
        print('<pchgTabsOperand type="PChgTabsOperand" offset="%d">' % self.pos)
        self.printAndSet("cb", self.readuInt8())
        PChgTabsDelClose(self).dump()
        PChgTabsAdd(self).dump()
        print('</pchgTabsOperand>')


# The Ico structure specifies an entry in the color palette that is listed in the following table.
Ico = {
    0x00: "Red: 0x00, Green: 0x00, Blue: 0x00, fAuto: 0xFF",
    0x01: "Red: 0x00, Green: 0x00, Blue: 0x00, fAuto: 0x00",
    0x02: "Red: 0x00, Green: 0x00, Blue: 0xFF, fAuto: 0x00",
    0x03: "Red: 0x00, Green: 0xFF, Blue: 0xFF, fAuto: 0x00",
    0x04: "Red: 0x00, Green: 0xFF, Blue: 0x00, fAuto: 0x00",
    0x05: "Red: 0xFF, Green: 0x00, Blue: 0xFF, fAuto: 0x00",
    0x06: "Red: 0xFF, Green: 0x00, Blue: 0x00, fAuto: 0x00",
    0x07: "Red: 0xFF, Green: 0xFF, Blue: 0x00, fAuto: 0x00",
    0x08: "Red: 0xFF, Green: 0xFF, Blue: 0xFF, fAuto: 0x00",
    0x09: "Red: 0x00, Green: 0x00, Blue: 0x80, fAuto: 0x00",
    0x0A: "Red: 0x00, Green: 0x80, Blue: 0x80, fAuto: 0x00",
    0x0B: "Red: 0x00, Green: 0x80, Blue: 0x00, fAuto: 0x00",
    0x0C: "Red: 0x80, Green: 0x00, Blue: 0x80, fAuto: 0x00",
    0x0D: "Red: 0x80, Green: 0x00, Blue: 0x80, fAuto: 0x00",
    0x0E: "Red: 0x80, Green: 0x80, Blue: 0x00, fAuto: 0x00",
    0x0F: "Red: 0x80, Green: 0x80, Blue: 0x80, fAuto: 0x00",
    0x10: "Red: 0xC0, Green: 0xC0, Blue: 0xC0, fAuto: 0x00",
}

# The Ipat enumeration is an index to a shading pattern.
Ipat = {
    0x0000: "ipatAuto",
    0x0001: "ipatSolid",
    0x0002: "ipatPct5",
    0x0003: "ipatPct10",
    0x0004: "ipatPct20",
    0x0005: "ipatPct25",
    0x0006: "ipatPct30",
    0x0007: "ipatPct40",
    0x0008: "ipatPct50",
    0x0009: "ipatPct60",
    0x000A: "ipatPct70",
    0x000B: "ipatPct75",
    0x000C: "ipatPct80",
    0x000D: "ipatPct90",
    0x000E: "ipatDkHorizontal",
    0x000F: "ipatDkVertical",
    0x0010: "ipatDkForeDiag",
    0x0011: "ipatDkBackDiag",
    0x0012: "ipatDkCross",
    0x0013: "ipatDkDiagCross",
    0x0014: "ipatHorizontal",
    0x0015: "ipatVertical",
    0x0016: "ipatForeDiag",
    0x0017: "ipatBackDiag",
    0x0018: "ipatCross",
    0x0019: "ipatDiagCross",
    0x0023: "ipatPctNew2",
    0x0024: "ipatPctNew7",
    0x0025: "ipatPctNew12",
    0x0026: "ipatPctNew15",
    0x0027: "ipatPctNew17",
    0x0028: "ipatPctNew22",
    0x0029: "ipatPctNew27",
    0x002A: "ipatPctNew32",
    0x002B: "ipatPctNew35",
    0x002C: "ipatPctNew37",
    0x002D: "ipatPctNew42",
    0x002E: "ipatPctNew45",
    0x002F: "ipatPctNew47",
    0x0030: "ipatPctNew52",
    0x0031: "ipatPctNew55",
    0x0032: "ipatPctNew57",
    0x0033: "ipatPctNew62",
    0x0034: "ipatPctNew65",
    0x0035: "ipatPctNew67",
    0x0036: "ipatPctNew72",
    0x0037: "ipatPctNew77",
    0x0038: "ipatPctNew82",
    0x0039: "ipatPctNew85",
    0x003A: "ipatPctNew87",
    0x003B: "ipatPctNew92",
    0x003C: "ipatPctNew95",
    0x003D: "ipatPctNew97",
    0xFFFF: "ipatNil"
}


class Shd80(BinaryStream):
    """The Shd80 structure specifies the colors and pattern that are used for background shading."""
    size = 2  # in bytes, see 2.9.245

    def __init__(self, parent, index):
        BinaryStream.__init__(self, parent.bytes)
        self.pos = parent.pos
        self.parent = parent
        self.index = index

    def dump(self):
        print('<shd80 type="Shd80" offset="%d" index="%d">' % (self.pos, self.index))
        buf = self.readuInt16()
        self.printAndSet("icoFore", buf & 0x001f, dict=Ico)  # 1..5th bits
        self.printAndSet("icoBack", (buf & 0x03e0) >> 5, dict=Ico)  # 6..10th bits
        self.printAndSet("ipat", (buf & 0xfc00) >> 10, dict=Ipat)  # 11.16th bits
        print('</shd80>')
        self.parent.pos = self.pos


class DefTableShd80Operand(BinaryStream):
    """The DefTableSdh800Operand structure is an operand that is used by several Table Sprms to
    specify each style of background shading that is applied to each of the cells in a single row."""
    def __init__(self, parent):
        BinaryStream.__init__(self, parent.bytes)
        self.pos = parent.pos

    def dump(self):
        print('<defTableShd80Operand type="DefTableShd80Operand" offset="%d">' % self.pos)
        self.printAndSet("cb", self.readuInt8())
        for i in range(round(self.cb / Shd80.size)):
            Shd80(self, i).dump()
        print('</defTableShd80Operand>')


class CMajorityOperand(BinaryStream):
    """The CMajorityOperand structure is used by sprmCMajority to specify which
    character properties of the text to reset to match that of the underlying
    paragraph style."""
    def __init__(self, parent):
        BinaryStream.__init__(self, parent.bytes)
        self.pos = parent.pos

    def dump(self):
        print('<cMajorityOperand type="CMajorityOperand" offset="%d">' % self.pos)
        self.printAndSet("cb", self.readuInt8())
        pos = 0
        print('<grpprl offset="%d" size="%d bytes">' % (self.pos, self.cb))
        while self.cb - pos > 0:
            prl = Prl(self, self.pos + pos)
            prl.dump()
            pos += prl.getSize()
        print('</grpprl>')
        print('</cMajorityOperand>')


class BrcCvOperand(BinaryStream):
    """The BrcCvOperand structure specifies border colors."""
    def __init__(self, parent):
        BinaryStream.__init__(self, parent.bytes)
        self.pos = parent.pos

    def dump(self):
        print('<bcrCvOperand type="BrcCvOperand" offset="%d">' % self.pos)
        self.printAndSet("cb", self.readuInt8())
        pos = self.pos
        print('<rgcv offset="%d" size="%d bytes">' % (self.pos, self.cb))
        while self.pos - pos < self.cb:
            COLORREF(self).dump("cv")
        print('</rgcv>')
        print('</bcrCvOperand>')


# The PgbApplyTo enumeration is used to specify the pages to which a page border applies.
PgbApplyTo = {
    0x0: "pgbAllPages",
    0x1: "pgbFirstPage",
    0x2: "pgbAllButFirst"
}

# The PgbOffsetFrom enumeration is used to specify the location from which the offset of a page
# border is measured.
PgbOffsetFrom = {
    0x0: "pgbFromText",
    0x1: "pgbFromEdge"
}

# The PgbPageDepth enumeration is used to specify the "depth" of a page border in relation to other
# page elements.
PgbPageDepth = {
    0x0: "pgbAtFront",
    0x1: "pgbAtBack",
}


class SPgbPropOperand(BinaryStream):
    """The SPgbPropOperand structure is the operand to sprmSPgbProp. It specifies the properties of a
    page border."""
    def __init__(self, parent):
        BinaryStream.__init__(self, parent.bytes)
        self.pos = parent.pos

    def dump(self):
        print('<sPgbPropOperand type="SPgbPropOperand" offset="%d">' % self.pos)
        buf = self.readuInt8()
        self.printAndSet("pgbApplyTo", buf & 0x7, dict=PgbApplyTo)  # 1..3rd bits
        self.printAndSet("pgbPageDepth", (buf & 0x18) >> 3, dict=PgbPageDepth)  # 4..5th bits
        self.printAndSet("pgbOffsetFrom", (buf & 0xe0) >> 5, dict=PgbOffsetFrom)  # 6..8th bits
        self.printAndSet("reserved", self.readuInt8())
        print('</sPgbPropOperand>')


class MFPF(BinaryStream):
    """The MFPF structure specifies the type of picture data that is stored."""
    def __init__(self, parent):
        BinaryStream.__init__(self, parent.bytes)
        self.pos = parent.pos
        self.parent = parent

    def dump(self):
        mmDict = {
            0x0064: "MM_SHAPE",
            0x0066: "MM_SHAPEFILE",
        }
        print('<mfpf type="MFPF" offset="%d">' % self.pos)
        self.printAndSet("mm", self.readInt16(), dict=mmDict, default="todo")
        self.printAndSet("xExt", self.readuInt16())
        self.printAndSet("yExt", self.readuInt16())
        self.printAndSet("swHMF", self.readuInt16())
        self.parent.pos = self.pos
        print('</mfpf>')


class PICF_Shape(BinaryStream):
    """The PICF_Shape structure specifies additional header information for
    pictures of type MM_SHAPE or MM_SHAPEFILE."""
    def __init__(self, parent, name):
        BinaryStream.__init__(self, parent.bytes)
        self.pos = parent.pos
        self.parent = parent
        self.name = name

    def dump(self):
        print('<%s type="PICF_Shape" offset="%d">' % (self.name, self.pos))
        self.printAndSet("grf", self.readuInt32())
        self.printAndSet("padding1", self.readuInt32())
        self.printAndSet("mmpm", self.readuInt16())
        self.printAndSet("padding2", self.readuInt32())
        self.parent.pos = self.pos
        print('</%s>' % self.name)


# BrcType is an unsigned integer that specifies the type of border.
BrcType = {
    0x00: "none",
    0x01: "single",
    0x03: "double",
    0x05: "A thin single solid line.",
    0x06: "dotted",
    0x07: "dashed",
    0x08: "dotDash",
    0x09: "dotDotDash",
    0x0A: "triple",
    0x0B: "thinThickSmallGap",
    0x0C: "thickThinSmallGap",
    0x0D: "thinThickThinSmallGap",
    0x0E: "thinThickMediumGap",
    0x0F: "thickThinMediumGap",
    0x10: "thinThickThinMediumGap",
    0x11: "thinThickLargeGap",
    0x12: "thickThinLargeGap",
    0x13: "thinThickThinLargeGap",
    0x14: "wave",
    0x15: "doubleWave",
    0x16: "dashSmallGap",
    0x17: "dashDotStroked",
    0x18: "threeDEmboss",
    0x19: "threeDEngrave",
    0x1A: "outset",
    0x1B: "inset",
    0x40: "apples",
    0x41: "archedScallops",
    0x42: "babyPacifier",
    0x43: "babyRattle",
    0x44: "balloons3Colors",
    0x45: "balloonsHotAir",
    0x46: "basicBlackDashes",
    0x47: "basicBlackDots",
    0x48: "basicBlackSquares",
    0x49: "basicThinLines",
    0x4A: "basicWhiteDashes",
    0x4B: "basicWhiteDots",
    0x4C: "basicWhiteSquares",
    0x4D: "basicWideInline",
    0x4E: "basicWideMidline",
    0x4F: "basicWideOutline",
    0x50: "bats",
    0x51: "birds",
    0x52: "birdsFlight",
    0x53: "cabins",
    0x54: "cakeSlice",
    0x55: "candyCorn",
    0x56: "celticKnotwork",
    0x57: "certificateBanner",
    0x58: "chainLink",
    0x59: "champagneBottle",
    0x5A: "checkedBarBlack",
    0x5B: "checkedBarColor",
    0x5C: "checkered",
    0x5D: "christmasTree",
    0x5E: "circlesLines",
    0x5F: "circlesRectangles",
    0x60: "classicalWave",
    0x61: "clocks",
    0x62: "compass",
    0x63: "confetti",
    0x64: "confettiGrays",
    0x65: "confettiOutline",
    0x66: "confettiStreamers",
    0x67: "confettiWhite",
    0x68: "cornerTriangles",
    0x69: "couponCutoutDashes",
    0x6A: "couponCutoutDots",
    0x6B: "crazyMaze",
    0x6C: "creaturesButterfly",
    0x6D: "creaturesFish",
    0x6E: "creaturesInsects",
    0x6F: "creaturesLadyBug",
    0x70: "crossStitch",
    0x71: "cup",
    0x72: "decoArch",
    0x73: "decoArchColor",
    0x74: "decoBlocks",
    0x75: "diamondsGray",
    0x76: "doubleD",
    0x77: "doubleDiamonds",
    0x78: "earth1",
    0x79: "earth2",
    0x7A: "eclipsingSquares1",
    0x7B: "eclipsingSquares2",
    0x7C: "eggsBlack",
    0x7D: "fans",
    0x7E: "film",
    0x7F: "firecrackers",
    0x80: "flowersBlockPrint",
    0x81: "flowersDaisies",
    0x82: "flowersModern1",
    0x83: "flowersModern2",
    0x84: "flowersPansy",
    0x85: "flowersRedRose",
    0x86: "flowersRoses",
    0x87: "flowersTeacup",
    0x88: "flowersTiny",
    0x89: "gems",
    0x8A: "gingerbreadMan",
    0x8B: "gradient",
    0x8C: "handmade1",
    0x8D: "handmade2",
    0x8E: "heartBalloon",
    0x8F: "heartGray",
    0x90: "hearts",
    0x91: "heebieJeebies",
    0x92: "holly",
    0x93: "houseFunky",
    0x94: "hypnotic",
    0x95: "iceCreamCones",
    0x96: "lightBulb",
    0x97: "lightning1",
    0x98: "lightning2",
    0x99: "mapPins",
    0x9A: "mapleLeaf",
    0x9B: "mapleMuffins",
    0x9C: "marquee",
    0x9D: "marqueeToothed",
    0x9E: "moons",
    0x9F: "mosaic",
    0xA0: "musicNotes",
    0xA1: "northwest",
    0xA2: "ovals",
    0xA3: "packages",
    0xA4: "palmsBlack",
    0xA5: "palmsColor",
    0xA6: "paperClips",
    0xA7: "papyrus",
    0xA8: "partyFavor",
    0xA9: "partyGlass",
    0xAA: "pencils",
    0xAB: "people",
    0xAC: "peopleWaving",
    0xAD: "peopleHats",
    0xAE: "poinsettias",
    0xAF: "postageStamp",
    0xB0: "pumpkin1",
    0xB1: "pushPinNote2",
    0xB2: "pushPinNote1",
    0xB3: "pyramids",
    0xB4: "pyramidsAbove",
    0xB5: "quadrants",
    0xB6: "rings",
    0xB7: "safari",
    0xB8: "sawtooth",
    0xB9: "sawtoothGray",
    0xBA: "scaredCat",
    0xBB: "seattle",
    0xBC: "shadowedSquares",
    0xBD: "sharksTeeth",
    0xBE: "shorebirdTracks",
    0xBF: "skyrocket",
    0xC0: "snowflakeFancy",
    0xC1: "snowflakes",
    0xC2: "sombrero",
    0xC3: "southwest",
    0xC4: "stars",
    0xC5: "starsTop",
    0xC6: "stars3d",
    0xC7: "starsBlack",
    0xC8: "starsShadowed",
    0xC9: "sun",
    0xCA: "swirligig",
    0xCB: "tornPaper",
    0xCC: "tornPaperBlack",
    0xCD: "trees",
    0xCE: "triangleParty",
    0xCF: "triangles",
    0xD0: "tribal1",
    0xD1: "tribal2",
    0xD2: "tribal3",
    0xD3: "tribal4",
    0xD4: "tribal5",
    0xD5: "tribal6",
    0xD6: "twistedLines1",
    0xD7: "twistedLines2",
    0xD8: "vine",
    0xD9: "waveline",
    0xDA: "weavingAngles",
    0xDB: "weavingBraid",
    0xDC: "weavingRibbon",
    0xDD: "weavingStrips",
    0xDE: "whiteFlowers",
    0xDF: "woodwork",
    0xE0: "xIllusions",
    0xE1: "zanyTriangles",
    0xE2: "zigZag",
    0xE3: "zigZagStitch"
}


class Brc80(BinaryStream):
    """The Brc80 structure describes a border."""
    def __init__(self, parent, name):
        BinaryStream.__init__(self, parent.bytes)
        self.pos = parent.pos
        self.parent = parent
        self.name = name

    def dump(self):
        buf = self.readuInt32()
        print('<%s type="Brc80" offset="%d">' % (self.name, self.pos))
        self.printAndSet("dptLineWidth", buf & 0x000000ff)  # 1..8th bits
        self.printAndSet("brcType", (buf & 0x0000ff00) >> 8, dict=BrcType)  # 9..16th bits
        self.printAndSet("ico", (buf & 0x00ff0000) >> 16, dict=Ico)  # 17..24th bits
        self.printAndSet("dptSpace", (buf & 0x1f000000) >> 24)  # 25..29th bits
        self.printAndSet("fShadow", self.getBit(buf, 29))
        self.printAndSet("fFrame", self.getBit(buf, 30))
        self.printAndSet("reserved", self.getBit(buf, 31))
        print('</%s>' % self.name)
        self.parent.pos = self.pos


class Brc80MayBeNil(BinaryStream):
    """The Brc80MayBeNil structure is a Brc80 structure. When all bits are set,
    this structure specifies that the region in question has no border."""
    def __init__(self, parent, name):
        BinaryStream.__init__(self, parent.bytes)
        self.pos = parent.pos
        self.parent = parent
        self.name = name

    def dump(self):
        buf = self.getuInt32()
        if buf == 0xFFFFFFFF:
            print('<%s type="Brc80MayBeNil" offset="%d" value="%s"/>' % (self.name, self.pos, hex(buf)))
            self.pos += 4
        else:
            print('<%s type="Brc80MayBeNil" offset="%d">' % (self.name, self.pos))
            Brc80(self, self.name).dump()
            print('</%s>' % self.name)
        self.parent.pos = self.pos


class PICMID(BinaryStream):
    """The PICMID structure specifies the size and border information for a picture."""
    def __init__(self, parent):
        BinaryStream.__init__(self, parent.bytes)
        self.pos = parent.pos
        self.parent = parent

    def dump(self):
        print('<picmid type="PICMID" offset="%d">' % self.pos)
        self.printAndSet("dxaGoal", self.readuInt16())
        self.printAndSet("dyaGoal", self.readuInt16())
        self.printAndSet("mx", self.readuInt16())
        self.printAndSet("my", self.readuInt16())
        self.printAndSet("dxaReserved1", self.readuInt16())
        self.printAndSet("dyaReserved1", self.readuInt16())
        self.printAndSet("dxaReserved2", self.readuInt16())
        self.printAndSet("dyaReserved2", self.readuInt16())
        self.printAndSet("fReserved", self.readuInt8())
        self.printAndSet("bpp", self.readuInt8())
        Brc80(self, "brcTop80").dump()
        Brc80(self, "brcLeft80").dump()
        Brc80(self, "brcBottom80").dump()
        Brc80(self, "brcRight80").dump()
        self.printAndSet("dxaReserved3", self.readuInt16())
        self.printAndSet("dyaReserved3", self.readuInt16())
        self.parent.pos = self.pos
        print('</picmid>')


class PICF(BinaryStream):
    """The PICF structure specifies the type of a picture, as well as the size
    of the picture and information about its border."""
    def __init__(self, parent):
        BinaryStream.__init__(self, parent.bytes)
        self.pos = parent.pos
        self.parent = parent

    def dump(self):
        print('<picf type="PICF" offset="%d">' % self.pos)
        posOrig = self.pos
        self.printAndSet("lcb", self.readInt32())
        self.printAndSet("cbHeader", self.readInt16())
        assert self.cbHeader == 0x44
        self.mfpf = MFPF(self)
        self.mfpf.dump()
        if self.mfpf.mm == 0x0064:  # MM_SHAPEFILE
            PICF_Shape(self, "innerHeader").dump()
            PICMID(self).dump()
            self.printAndSet("cProps", self.readuInt16())
        else:
            self.pos = posOrig + self.cbHeader
        self.parent.pos = self.pos
        print('</picf>')


IType = {
    0: "iTypeText",
    1: "iTypeChck",
    2: "iTypeDrop"
}


ITypeTxt = {
    0: "iTypeTxtReg",
    1: "iTypeTxtNum",
    2: "iTypeTxtDate",
    3: "iTypeTxtCurDate",
    4: "iTypeTxtCurTime",
    5: "iTypeTxtCalc"
}


class FFDataBits(BinaryStream):
    """The FFDataBits structure specifies the type and properties for a form
    field that is specified by a FFData."""
    def __init__(self, parent):
        BinaryStream.__init__(self, parent.bytes)
        self.pos = parent.pos
        self.parent = parent

    def dump(self):
        print('<FFDataBits>')
        buf = self.readuInt8()
        self.printAndSet("iType", buf & 0x0003, dict=IType)  # 1..2nd bits
        self.printAndSet("iRes", (buf & 0x007c) >> 2)  # 3..7th bits
        self.printAndSet("fOwnHelp", self.getBit(buf, 8))
        buf = self.readuInt8()
        self.printAndSet("fOwnStat", self.getBit(buf, 1))
        self.printAndSet("fProt", self.getBit(buf, 2))
        self.printAndSet("iSize", self.getBit(buf, 3))
        self.printAndSet("iTypeTxt", (buf & 0x0038) >> 3, dict=ITypeTxt)  # 4..6th bits
        self.printAndSet("fRecalc", self.getBit(buf, 7))
        self.printAndSet("fHasListBox", self.getBit(buf, 8))
        print('</FFDataBits>')
        self.parent.pos = self.pos


class FFData(BinaryStream):
    """The FFData structure specifies form field data for a text box, check
    box, or drop-down list box. (Page 348 of [MS-DOC] spec.)"""
    def __init__(self, parent):
        BinaryStream.__init__(self, parent.bytes)
        self.pos = parent.pos
        self.parent = parent

    def dump(self):
        print('<FFData>')
        self.printAndSet("version", self.readuInt32())
        self.bits = FFDataBits(self)
        self.bits.dump()
        self.printAndSet("cch", self.readuInt16())
        self.printAndSet("hps", self.readuInt16())
        xstzName = Xstz(self, "xstzName")
        xstzName.dump()
        self.pos = xstzName.pos
        xstzTextDef = Xstz(self, "xstzTextDef")
        xstzTextDef.dump()
        self.pos = xstzTextDef.pos
        if self.bits.iType == 1 or self.bits.iType == 2:  # iTypeChck or iTypeDrop
            self.printAndSet("wDef", self.readuInt16())
        xstzTextFormat = Xstz(self, "xstzTextFormat")
        xstzTextFormat.dump()
        self.pos = xstzTextFormat.pos
        xstzHelpText = Xstz(self, "xstzHelpText")
        xstzHelpText.dump()
        self.pos = xstzHelpText.pos
        xstzStatText = Xstz(self, "xstzStatText")
        xstzStatText.dump()
        self.pos = xstzStatText.pos
        xstzEntryMcr = Xstz(self, "xstzEntryMcr")
        xstzEntryMcr.dump()
        self.pos = xstzEntryMcr.pos
        xstzExitMcr = Xstz(self, "xstzExitMcr")
        xstzExitMcr.dump()
        self.pos = xstzExitMcr.pos
        if self.bits.iType == 2:  # iTypeDrop
            print('<todo what="FFData::dump(): handle hsttbDropList for iTypeDrop"/>')
        print('</FFData>')


class NilPICFAndBinData(BinaryStream):
    """The NilPICFAndBinData structure that holds header information and binary
    data for a hyperlink, form field, or add-in field. The NilPICFAndBinData
    structure MUST be stored in the Data Stream."""
    def __init__(self, parent):
        dataStream = parent.mainStream.doc.getDirectoryStreamByName(b"Data")
        BinaryStream.__init__(self, dataStream.bytes)
        self.pos = parent.operand
        self.parent = parent

    def dump(self):
        print('<NilPICFAndBinData>')
        # self -> sprm -> prl -> chpx -> chpxFkp
        chpxFkp = self.parent.parent.parent.parent
        self.printAndSet("lcb", self.readInt32())
        self.printAndSet("cbHeader", self.readInt16())
        self.printAndSet("ignored0", self.readInt32())
        self.printAndSet("ignored1", self.readInt32())
        self.printAndSet("ignored2", self.readInt32())
        self.printAndSet("ignored3", self.readInt32())
        self.printAndSet("ignored4", self.readInt32())
        self.printAndSet("ignored5", self.readInt32())
        self.printAndSet("ignored6", self.readInt32())
        self.printAndSet("ignored7", self.readInt32())
        self.printAndSet("ignored8", self.readInt32())
        self.printAndSet("ignored9", self.readInt32())
        self.printAndSet("ignored10", self.readInt32())
        self.printAndSet("ignored11", self.readInt32())
        self.printAndSet("ignored12", self.readInt32())
        self.printAndSet("ignored13", self.readInt32())
        self.printAndSet("ignored14", self.readInt32())
        self.printAndSet("ignored15", self.readInt16())
        if len(chpxFkp.transformeds) > 1:
            fieldType = chpxFkp.transformeds[-2]
        else:
            fieldType = ")-MISSING-("
        if fieldType == " FORMTEXT ":
            FFData(self).dump()
        else:
            print('<todo what="NilPICFAndBinData::dump(): handle %s"/>' % fieldType)
        print('</NilPICFAndBinData>')


class PICFAndOfficeArtData(BinaryStream):
    """The PICFAndOfficeArtData structure specifies header information and
    binary data for a picture."""
    def __init__(self, parent):
        dataStream = parent.mainStream.doc.getDirectoryStreamByName(b"Data")
        BinaryStream.__init__(self, dataStream.bytes)
        self.pos = parent.operand
        self.parent = parent

    def dump(self):
        print('<PICFAndOfficeArtData>')
        found = False
        for prl in self.parent.parent.parent.prls:
            if prl.sprm.sprm in (0x0806, 0x080a):  # sprmCFData, sprmCFOle2
                found = True
                break
        if not found:
            pos = self.pos
            picf = PICF(self)
            picf.dump()
            assert self.pos == pos + 68
            if picf.mfpf.mm == 0x0066:  # MM_SHAPEFILE
                print('<todo what="PICFAndOfficeArtData::dump(): picf.mfpf.mm == MM_SHAPEFILE is unhandled"/>')
            elif picf.mfpf.mm == 0x0064:  # MM_SHAPE
                remaining = picf.lcb - (self.pos - pos)
                msodraw.InlineSpContainer(self, remaining).dumpXml(self, getWordModel(self.parent.mainStream))
            else:
                print('<todo what="PICFAndOfficeArtData::dump(): picf.mfpf.mm is unhandled (not MM_SHAPE or MM_SHAPEFILE): %d"/>' % picf.mfpf.mm)
        else:
            print('<todo what="PICFAndOfficeArtData::dump(): handle sprmCFData or sprmCFOle2"/>')
        print('</PICFAndOfficeArtData>')


# The TextFlow enumeration specifies the rotation settings for a block of text and for the individual
# East Asian characters in each line of the block.
TextFlow = {
    0x0000: "grpfTFlrtb",
    0x0001: "grpfTFtbrl",
    0x0003: "grpfTFbtlr",
    0x0004: "grpfTFlrtbv",
    0x0005: "grpfTFtbrlv"
}

# The VerticalMergeFlag enumeration provides a 2-bit value that specifies whether a table cell is
# merged with the cells above or below it.
VerticalMergeFlag = {
    0x00: "fvmClear",
    0x01: "fvmMerge",
    0x03: "fvmRestart"
}

# The VerticalAlign enumeration specifies the vertical alignment of content within table cells.
VerticalAlign = {
    0x00: "vaTop",
    0x01: "vaCenter",
    0x02: "vaBottom",
}

# The Fts enumeration specifies how the preferred width for a table, table indent, table cell, cell
# margin, or cell spacing is defined.
Fts = {
    0x00: "ftsNil",
    0x01: "ftsAuto",
    0x02: "ftsPercent",
    0x03: "ftsDxa",
    0x13: "ftsDxaSys"
}


class SHD(BinaryStream):
    """The Shd structure specifies the colors and pattern that are used for background shading."""
    def __init__(self, parent):
        BinaryStream.__init__(self, parent.bytes)
        self.parent = parent
        self.pos = parent.pos

    def dump(self):
        print('<shd type="SHD" offset="%d">' % self.pos)
        COLORREF(self).dump("cvFore")
        COLORREF(self).dump("cvBack")
        self.printAndSet("ipat", self.readuInt16(), dict=Ipat)
        print('</shd>')


class TCGRF(BinaryStream):
    """A TCGRF structure specifies the text layout and cell merge properties for a single cell in a table."""
    def __init__(self, parent):
        BinaryStream.__init__(self, parent.bytes)
        self.parent = parent
        self.pos = parent.pos

    def dump(self):
        print('<tcgrf type="TCGRF" offset="%d">' % self.pos)
        buf = self.readuInt16()
        self.printAndSet("horzMerge", buf & 0x0003)  # 1..2nd bits
        self.printAndSet("textFlow", (buf & 0x001c) >> 2, dict=TextFlow, default="todo")  # 3..6th bits
        self.printAndSet("vertMerge", (buf & 0x0060) >> 6, dict=VerticalMergeFlag)  # 7..8th bits
        self.printAndSet("vertAlign", (buf & 0x0180) >> 8, dict=VerticalAlign)  # 9..10th bits
        self.printAndSet("ftsWidth", (buf & 0x0e00) >> 10, dict=Fts)  # 11..12th bits
        self.printAndSet("fFitText", self.getBit(buf, 12))
        self.printAndSet("fNoWrap", self.getBit(buf, 13))
        self.printAndSet("fHideMark", self.getBit(buf, 14))
        self.printAndSet("fUnused", self.getBit(buf, 15))
        print('</tcgrf>')
        self.parent.pos = self.pos


class TC80(BinaryStream):
    """The TC80 structure specifies the border and other formatting for a single cell in a table."""
    def __init__(self, parent, index):
        BinaryStream.__init__(self, parent.bytes)
        self.parent = parent
        self.pos = parent.pos
        self.index = index

    def dump(self):
        print('<tc80 index="%d">' % self.index)
        TCGRF(self).dump()
        self.printAndSet("wWidth", self.readuInt16(), hexdump=False)
        Brc80MayBeNil(self, "brcTop").dump()
        Brc80MayBeNil(self, "brcLeft").dump()
        Brc80MayBeNil(self, "brcBottom").dump()
        Brc80MayBeNil(self, "brcRight").dump()
        print('</tc80>')
        self.parent.pos = self.pos


class TDefTableOperand(BinaryStream):
    """The TDefTableOperand structure is the operand that is used by the
    sprmTDefTable value. It specifies the initial layout of the columns in the
    current table row."""
    def __init__(self, parent):
        BinaryStream.__init__(self, parent.bytes)
        self.pos = parent.pos

    def dump(self):
        print('<tDefTableOperand>')
        self.printAndSet("cb", self.readuInt16())
        size = self.pos + self.cb - 1
        self.printAndSet("NumberOfColumns", self.readuInt8())
        for i in range(self.NumberOfColumns + 1):
            print('<rgdxaCenter index="%d" value="%d"/>' % (i, self.readInt16()))
        i = 0
        while self.pos < size:
            TC80(self, i).dump()
            i += 1
        print('</tDefTableOperand>')


class TableBordersOperand(BinaryStream):
    """The TableBordersOperand structure specifies a set of borders for a table row."""
    def __init__(self, parent):
        BinaryStream.__init__(self, parent.bytes)
        self.pos = parent.pos

    def dump(self):
        print('<tableBordersOperand>')
        self.printAndSet("cb", self.readuInt8())
        posOrig = self.pos
        BRC(self, "brcTop").dump()
        BRC(self, "brcLeft").dump()
        BRC(self, "brcBottom").dump()
        BRC(self, "brcRight").dump()
        BRC(self, "brcHorizontalInside").dump()
        BRC(self, "brcVerticalInside").dump()
        assert self.pos == posOrig + 0x30
        print('</tableBordersOperand>')


class TableBordersOperand80(BinaryStream):
    """The TableBordersOperand80 structure is an operand that specifies the
    borders which are applied to a row of table cells."""
    def __init__(self, parent):
        BinaryStream.__init__(self, parent.bytes)
        self.pos = parent.pos

    def dump(self):
        print('<tableBordersOperand80>')
        self.printAndSet("cb", self.readuInt8())
        posOrig = self.pos
        Brc80MayBeNil(self, "brcTop").dump()
        Brc80MayBeNil(self, "brcLeft").dump()
        Brc80MayBeNil(self, "brcBottom").dump()
        Brc80MayBeNil(self, "brcRight").dump()
        Brc80MayBeNil(self, "brcHorizontalInside").dump()
        Brc80MayBeNil(self, "brcVerticalInside").dump()
        assert self.pos == posOrig + 0x18
        print('</tableBordersOperand80>')


class SHDOperand(BinaryStream):
    """The SDHOperand structure is an operand that is used by several Sprm
    structures to specify the background shading to be applied."""
    def __init__(self, parent):
        BinaryStream.__init__(self, parent.bytes)
        self.pos = parent.pos

    def dump(self):
        print('<shdOperand>')
        self.printAndSet("cb", self.readuInt8())
        SHD(self).dump()
        print('</shdOperand>')


class BrcOperand(BinaryStream):
    """The BrcOperand structure is the operand to several SPRMs that control borders."""
    def __init__(self, parent):
        BinaryStream.__init__(self, parent.bytes)
        self.pos = parent.pos
        self.posOrig = self.pos
        self.cb = self.readuInt8()
        self.brc = BRC(self)

    def dump(self):
        print('<brcOperand type="BrcOperand" offset="%d">' % self.posOrig)
        self.brc.dump()
        print('</brcOperand>')


class Sprm(BinaryStream):
    """The Sprm structure specifies a modification to a property of a character, paragraph, table, or section."""
    def __init__(self, parent, mainStream=None, transformed=None):
        BinaryStream.__init__(self, parent.bytes, mainStream=mainStream)
        self.parent = parent
        self.transformed = transformed
        self.pos = parent.pos
        self.operandSizeMap = {
            0: 1,
            1: 1,
            2: 2,
            3: 4,
            4: 2,
            5: 2,
            7: 3,
        }

        self.sprm = self.readuInt16()

        self.ispmd = (self.sprm & 0x1ff)  # 1-9th bits
        self.fSpec = (self.sprm & 0x200) >> 9  # 10th bit
        self.sgc = (self.sprm & 0x1c00) >> 10  # 11-13th bits
        self.spra = (self.sprm & 0xe000) >> 13  # 14-16th bits

        self.ct = False  # If it's a complex type, it can't be dumped as a simple string.
        self.operand = "todo"
        if self.getOperandSize() == 1:
            self.operand = self.getuInt8()
        elif self.getOperandSize() == 2:
            self.operand = self.getuInt16()
            if self.sprm == 0x522f:
                self.ct = SPgbPropOperand(self)
        elif self.getOperandSize() == 3:
            self.operand = self.getuInt24()
        elif self.getOperandSize() == 4:
            self.operand = self.getuInt32()
            if self.sprm == 0x6a03 and transformed == r"\x01":  # sprmCPicLocation
                # Can't decide right now, depends on if there will be an sprmCFData later or not.
                self.ct = True
            elif self.sprm == 0x6646:  # sprmPHugePapx
                dataStream = mainStream.doc.getDirectoryStreamByName(b"Data")
                dataStream.pos = self.operand
                self.ct = PrcData(dataStream)
            elif self.sprm == 0x6412:
                self.ct = LSPD(self)
        elif self.getOperandSize() == 7:
            self.operand = self.getuInt64() & 0x0fffffff
        elif self.getOperandSize() == 9:
            # top, left, bottom and right page / paragraph borders
            if self.sprm in (0xd234, 0xd235, 0xd236, 0xd237, 0xc64e, 0xc64f, 0xc650, 0xc651):
                self.ct = BrcOperand(self)
            elif self.sprm == 0xc60d:
                self.ct = PChgTabsPapxOperand(self)
            elif self.sprm == 0xc615:
                self.ct = PChgTabsOperand(self)
            elif self.sprm == 0xd609:
                self.ct = DefTableShd80Operand(self)
            elif self.sprm == 0xca47:
                self.ct = CMajorityOperand(self)
            elif self.sprm in (0xD61A, 0xD61B, 0xD61C, 0xD61D):
                self.ct = BrcCvOperand(self)
            else:
                print('<todo what="Sprm::__init__() unhandled sprm of size 9: %s"/>' % hex(self.sprm))
        else:
            if self.sprm == 0xd608:
                self.ct = TDefTableOperand(self)
            elif self.sprm == 0xca71:
                self.ct = SHDOperand(self)
            elif self.sprm == 0xd613:
                self.ct = TableBordersOperand(self)
            elif self.sprm == 0xd605:
                self.ct = TableBordersOperand80(self)
            elif self.sprm == 0xc60d:
                self.ct = PChgTabsPapxOperand(self)
            else:
                print('<todo what="Sprm::__init__() unhandled sprm of size %s: %s"/>' % (self.getOperandSize(), hex(self.sprm)))

    def dump(self):
        sgcmap = {
            1: 'paragraph',
            2: 'character',
            3: 'picture',
            4: 'section',
            5: 'table'
        }
        nameMap = {
            1: docsprm.parMap,
            2: docsprm.chrMap,
            3: docsprm.picMap,
            4: docsprm.secMap,
            5: docsprm.tblMap,
        }
        attrs = []
        close = False
        attrs.append('value="%s"' % hex(self.sprm))
        attrs.append('ispmd="%s"' % hex(self.ispmd))
        attrs.append('fSpec="%s"' % hex(self.fSpec))
        if self.sgc in sgcmap:
            attrs.append('sgc="%s"' % sgcmap[self.sgc])
        attrs.append('spra="%s"' % self.spra)
        if self.sgc in nameMap and self.sprm in nameMap[self.sgc]:
            attrs.append('name="%s"' % nameMap[self.sgc][self.sprm])
        attrs.append('operandSize="%s"' % self.getOperandSize())
        if not self.ct:
            close = True
            if self.operand == "todo":
                attrs.append('operand=""')
            else:
                attrs.append('operand="%s"' % hex(self.operand))
        print('<sprm %s%s>' % (" ".join(attrs), {True: "/", False: ""}[close]))
        if self.ct:
            if type(self.ct) == bool:
                if self.sprm == 0x6a03 and self.transformed == r"\x01":
                    haveCFData = False
                    for prl in self.parent.parent.prls:
                        if prl.sprm.sprm == 0x0806:  # sprmCFData
                            haveCFData = True
                            break
                    if haveCFData:
                        self.ct = NilPICFAndBinData(self)
                    else:
                        self.ct = PICFAndOfficeArtData(self)
            self.ct.dump()
            print('</sprm>')

    def getOperandSize(self):
        if self.spra == 6:  # variable
            if self.sprm not in [0xD608, 0xC615]:  # sprmTDefTable, sprmPChgTabs
                # these structures are prefixed with their size
                return self.getuInt8() + 1
            elif self.sprm == 0xD608:
                return self.getuInt16() + 1
            elif self.sprm == 0xC615:
                cb = self.getuInt8()
                if cb < 255:
                    return cb + 1
                else:
                    raise Exception("PChgTabsOperand: cb is 255")
            raise Exception("No idea what is the size of SPRM %s" % hex(self.sprm))
        return self.operandSizeMap[self.spra]


class Prl(BinaryStream):
    """The Prl structure is a Sprm that is followed by an operand."""
    def __init__(self, parent, offset, mainStream=None, transformed=None, index=None):
        BinaryStream.__init__(self, parent.bytes)
        self.parent = parent
        self.pos = offset
        self.posOrig = self.pos
        self.sprm = Sprm(self, mainStream, transformed)
        self.pos += 2
        self.index = index

    def dump(self):
        indexstr = ""
        if self.index is not None:
            indexstr = ' index="%d"' % self.index
        print('<prl type="Prl" offset="%d"%s>' % (self.posOrig, indexstr))
        self.sprm.dump()
        print('</prl>')

    def getSize(self):
        return 2 + self.sprm.getOperandSize()


class GrpPrlAndIstd(BinaryStream):
    """The GrpPrlAndIstd structure specifies the style and properties that are applied to a paragraph, a table row, or a table cell."""
    def __init__(self, bytes, offset, size, mainStream=None):
        BinaryStream.__init__(self, bytes, mainStream=mainStream)
        self.pos = offset
        self.size = size

    def dump(self):
        print('<grpPrlAndIstd type="GrpPrlAndIstd" offset="%d" size="%d bytes">' % (self.pos, self.size))
        pos = self.pos
        self.printAndSet("istd", self.getuInt16())
        pos += 2
        while (self.size - (pos - self.pos)) > 0:
            prl = Prl(self, pos, mainStream=self.mainStream)
            prl.dump()
            pos += prl.getSize()
        print('</grpPrlAndIstd>')


class Chpx(BinaryStream):
    """The Chpx structure specifies a set of properties for text."""
    def __init__(self, parent, mainStream, offset, transformed=None):
        BinaryStream.__init__(self, parent.bytes, mainStream=mainStream)
        self.parent = parent
        self.pos = offset
        self.transformed = transformed

        self.cb = self.readuInt8()
        pos = self.pos
        index = 0
        self.prls = []
        while (self.cb - (pos - self.pos)) > 0:
            prl = Prl(self, pos, self.mainStream, self.transformed, index)
            self.prls.append(prl)
            pos += prl.getSize()
            index += 1

    def dump(self):
        print('<chpx type="Chpx" offset="%d">' % self.pos)
        self.printAndSet("cb", self.cb)
        for prl in self.prls:
            prl.dump()
        print('</chpx>')


class PapxInFkp(BinaryStream):
    """The PapxInFkp structure specifies a set of text properties."""
    def __init__(self, bytes, mainStream, offset):
        BinaryStream.__init__(self, bytes, mainStream=mainStream)
        self.pos = offset

    def dump(self):
        print('<papxInFkp type="PapxInFkp" offset="%d">' % self.pos)
        self.printAndSet("cb", self.readuInt8())
        if self.cb == 0:
            self.printAndSet("cb_", self.readuInt8())
            grpPrlAndIstd = GrpPrlAndIstd(self.bytes, self.pos, 2 * self.cb_, mainStream=self.mainStream)
        else:
            grpPrlAndIstd = GrpPrlAndIstd(self.bytes, self.pos, 2 * self.cb - 1, mainStream=self.mainStream)
        grpPrlAndIstd.dump()
        print('</papxInFkp>')


class BxPap(BinaryStream):
    """The BxPap structure specifies the offset of a PapxInFkp in PapxFkp."""
    size = 13  # in bytes, see 2.9.23

    def __init__(self, bytes, mainStream, offset, parentoffset):
        BinaryStream.__init__(self, bytes, mainStream=mainStream)
        self.pos = offset
        self.parentpos = parentoffset

    def dump(self):
        print('<bxPap type="BxPap" offset="%d" size="%d bytes">' % (self.pos, self.size))
        self.printAndSet("bOffset", self.readuInt8())
        papxInFkp = PapxInFkp(self.bytes, self.mainStream, self.parentpos + self.bOffset * 2)
        papxInFkp.dump()
        print('</bxPap>')


class ChpxFkp(BinaryStream):
    """The ChpxFkp structure maps text to its character properties."""
    def __init__(self, pnFkpChpx, offset, size):
        BinaryStream.__init__(self, pnFkpChpx.mainStream.bytes, mainStream=pnFkpChpx.mainStream)
        self.pos = offset
        self.size = size
        self.pnFkpChpx = pnFkpChpx

    def dump(self):
        print('<chpxFkp type="ChpxFkp" offset="%d" size="%d bytes">' % (self.pos, self.size))
        self.crun = self.getuInt8(pos=self.pos + self.size - 1)
        pos = self.pos
        self.transformeds = []
        for i in range(self.crun):
            # rgfc
            start = self.getuInt32(pos=pos)
            end = self.getuInt32(pos=pos + 4)
            print('<rgfc index="%d" start="%d" end="%d">' % (i, start, end))
            self.transformed = self.quoteAttr(self.pnFkpChpx.mainStream.retrieveOffset(start, end))
            print('<transformed value="%s"/>' % self.transformed)
            self.transformeds.append(self.transformed)
            pos += 4

            # rgbx
            offset = PLC.getPLCOffset(self.pos, self.crun, 1, i)
            chpxOffset = self.getuInt8(pos=offset) * 2
            chpx = Chpx(self, self.mainStream, self.pos + chpxOffset, self.transformed)
            chpx.dump()
            print('</rgfc>')

        self.printAndSet("crun", self.crun)
        print('</chpxFkp>')


class PapxFkp(BinaryStream):
    """The PapxFkp structure maps paragraphs, table rows, and table cells to their properties."""
    def __init__(self, bytes, mainStream, offset, size):
        BinaryStream.__init__(self, mainStream.bytes, mainStream=mainStream)
        self.pos = offset
        self.size = size

    def dump(self):
        print('<papxFkp type="PapxFkp" offset="%d" size="%d bytes">' % (self.pos, self.size))
        self.cpara = self.getuInt8(pos=self.pos + self.size - 1)
        pos = self.pos
        for i in range(self.cpara):
            # rgfc
            start = self.getuInt32(pos=pos)
            end = self.getuInt32(pos=pos + 4)
            print('<rgfc index="%d" start="%d" end="%d">' % (i, start, end))
            print('<transformed value="%s"/>' % self.quoteAttr(self.mainStream.retrieveOffset(start, end)))
            pos += 4

            # rgbx
            offset = PLC.getPLCOffset(self.pos, self.cpara, BxPap.size, i)
            bxPap = BxPap(self.bytes, self.mainStream, offset, self.pos)
            bxPap.dump()
            print('</rgfc>')

        self.printAndSet("cpara", self.cpara)
        print('</papxFkp>')


class PnFkpChpx(BinaryStream):
    """The PnFkpChpx structure specifies the location in the WordDocument Stream of a ChpxFkp structure."""
    def __init__(self, plcBteChpx, offset, size, name):
        BinaryStream.__init__(self, plcBteChpx.bytes, mainStream=plcBteChpx.mainStream)
        self.pos = offset
        self.size = size
        self.name = name
        self.plcBteChpx = plcBteChpx

    def dump(self):
        print('<%s type="PnFkpChpx" offset="%d" size="%d bytes">' % (self.name, self.pos, self.size))
        buf = self.readuInt32()
        self.printAndSet("pn", buf & (2 ** 22 - 1))
        chpxFkp = ChpxFkp(self, self.pn * 512, 512)
        chpxFkp.dump()
        print('</%s>' % self.name)


class LPXCharBuffer9(BinaryStream):
    """The LPXCharBuffer9 structure is a length-prefixed buffer for up to 9 Unicode characters."""
    def __init__(self, parent, name):
        BinaryStream.__init__(self, parent.bytes)
        self.pos = parent.pos
        self.name = name

    def dump(self):
        print('<%s type="LPXCharBuffer9" offset="%d" size="20 bytes">' % (self.name, self.pos))
        self.printAndSet("cch", self.readuInt16())
        self.printAndSet("xcharArray", self.bytes[self.pos:self.pos + (self.cch * 2)].decode('utf-16'), hexdump=False)
        print('</%s>' % self.name)


class ATRDPre10(BinaryStream):
    """The ATRDPre10 structure contains information about a comment in the document."""
    def __init__(self, aPlcfandRef, offset):
        BinaryStream.__init__(self, aPlcfandRef.bytes)
        self.pos = offset

    def dump(self):
        print('<aATRDPre10 type="ATRDPre10" offset="%d" size="30 bytes">' % self.pos)
        xstUsrInitl = LPXCharBuffer9(self, "xstUsrInitl")
        xstUsrInitl.dump()
        self.pos += 20
        self.printAndSet("ibst", self.readuInt16())
        self.printAndSet("bitsNotUsed", self.readuInt16())
        self.printAndSet("grfNotUsed", self.readuInt16())
        self.printAndSet("ITagBkmk", self.readInt32())
        print('</aATRDPre10>')


class PnFkpPapx(BinaryStream):
    """The PnFkpPapx structure specifies the offset of a PapxFkp in the WordDocument Stream."""
    def __init__(self, bytes, mainStream, offset, size, name):
        BinaryStream.__init__(self, bytes, mainStream=mainStream)
        self.pos = offset
        self.size = size
        self.name = name

    def dump(self):
        print('<%s type="PnFkpPapx" offset="%d" size="%d bytes">' % (self.name, self.pos, self.size))
        buf = self.readuInt32()
        self.printAndSet("pn", buf & (2 ** 22 - 1))
        papxFkp = PapxFkp(self.bytes, self.mainStream, self.pn * 512, 512)
        papxFkp.dump()
        print('</%s>' % self.name)


class PlcBteChpx(BinaryStream, PLC):
    """The PlcBteChpx structure is a PLC that maps the offsets of text in the WordDocument stream to the character properties of that text."""
    def __init__(self, mainStream):
        BinaryStream.__init__(self, mainStream.getTableStream().bytes, mainStream=mainStream)
        PLC.__init__(self, mainStream.lcbPlcfBteChpx, 4)
        self.pos = mainStream.fcPlcfBteChpx
        self.size = mainStream.lcbPlcfBteChpx

    def dump(self):
        print('<plcBteChpx type="PlcBteChpx" offset="%d" size="%d bytes">' % (self.pos, self.size))
        pos = self.pos
        for i in range(self.getElements()):
            # aFC
            start = self.getuInt32(pos=pos)
            end = self.getuInt32(pos=pos + 4)
            print('<aFC index="%d" start="%d" end="%d">' % (i, start, end))
            pos += 4

            # aPnBteChpx
            aPnBteChpx = PnFkpChpx(self, self.getOffset(self.pos, i), 4, "aPnBteChpx")
            aPnBteChpx.dump()
            print('</aFC>')
        print('</plcBteChpx>')


class PlcfHdd(BinaryStream, PLC):
    """The Plcfhdd structure is a PLC that contains only CPs and no additional data. It specifies where
    header document stories begin and end."""
    def __init__(self, mainStream):
        BinaryStream.__init__(self, mainStream.getTableStream().bytes, mainStream=mainStream)
        PLC.__init__(self, mainStream.lcbPlcfHdd, 0)
        self.pos = mainStream.fcPlcfHdd
        self.size = mainStream.lcbPlcfHdd

    def getContents(self, i):
        if i <= 5:
            contentsMap = {
                0: "Footnote separator",
                1: "Footnote continuation separator",
                2: "Footnote continuation notice",
                3: "Endnote separator",
                4: "Endnote continuation separator",
                5: "Endnote continuation notice",
            }
            return contentsMap[i]
        else:
            contentsMap = {
                0: "Even page header",
                1: "Odd page header",
                2: "Even page footer",
                3: "Odd page footer",
                4: "First page header",
                5: "First page footer",
            }
            sectionIndex = i / 6
            contentsIndex = i % 6
            return "%s (section #%s)" % (contentsMap[contentsIndex], sectionIndex)

    def dump(self):
        print('<plcfHdd type="PlcfHdd" offset="%d" size="%d bytes">' % (self.pos, self.size))
        offset = self.mainStream.getHeaderOffset()
        pos = self.pos
        for i in range(self.getElements() - 1):
            start = self.getuInt32(pos=pos)
            end = self.getuInt32(pos=pos + 4)
            print('<aCP index="%d" contents="%s" start="%d" end="%d">' % (i, self.getContents(i), start, end))
            print('<transformed value="%s"/>' % self.quoteAttr(self.mainStream.retrieveCPs(offset + start, offset + end)))
            pos += 4
            print('</aCP>')
        print('</plcfHdd>')


class PlcfandTxt(BinaryStream, PLC):
    """The PlcfandTxt structure is a PLC that contains only CPs and no additional data."""
    def __init__(self, mainStream, offset, size):
        BinaryStream.__init__(self, mainStream.getTableStream().bytes, mainStream=mainStream)
        PLC.__init__(self, size, 0)
        self.pos = offset
        self.size = size

    def dump(self):
        print('<plcfandTxt type="PlcfandTxt" offset="%d" size="%d bytes">' % (self.pos, self.size))
        offset = self.mainStream.getCommentOffset()
        pos = self.pos
        for i in range(self.getElements() - 1):
            start = self.getuInt32(pos=pos)
            end = self.getuInt32(pos=pos + 4)
            print('<aCP index="%d" start="%d" end="%d">' % (i, start, end))
            print('<transformed value="%s"/>' % self.quoteAttr(self.mainStream.retrieveCPs(offset + start, offset + end)))
            pos += 4
            print('</aCP>')
        print('</plcfandTxt>')


class PlcfandRef(BinaryStream, PLC):
    """The PlcfandRef structure is a PLC whose data elements are ATRDPre10 structures."""
    def __init__(self, mainStream, offset, size):
        BinaryStream.__init__(self, mainStream.getTableStream().bytes, mainStream=mainStream)
        PLC.__init__(self, size, 30)
        self.pos = offset
        self.size = size

    def dump(self):
        print('<plcfandRef type="PlcfandRef" offset="%d" size="%d bytes">' % (self.pos, self.size))
        pos = self.pos
        for i in range(self.getElements()):
            start = self.getuInt32(pos=pos)
            print('<aCP index="%d" commentEnd="%d">' % (i, start))
            print('<transformed value="%s"/>' % self.quoteAttr(self.mainStream.retrieveCP(start)))
            pos += 4

            # aATRDPre10
            aATRDPre10 = ATRDPre10(self, self.getOffset(self.pos, i))
            aATRDPre10.dump()
            print('</aCP>')
        print('</plcfandRef>')


class PlcBtePapx(BinaryStream, PLC):
    """The PlcBtePapx structure is a PLC that specifies paragraph, table row, or table cell properties."""
    def __init__(self, bytes, mainStream, offset, size):
        BinaryStream.__init__(self, bytes, mainStream=mainStream)
        PLC.__init__(self, size, 4)
        self.pos = offset
        self.size = size

    def dump(self):
        print('<plcBtePapx type="PlcBtePapx" offset="%d" size="%d bytes">' % (self.pos, self.size))
        pos = self.pos
        for i in range(self.getElements()):
            # aFC
            start = self.getuInt32(pos=pos)
            end = self.getuInt32(pos=pos + 4)
            print('<aFC index="%d" start="%d" end="%d">' % (i, start, end))
            pos += 4

            # aPnBtePapx
            aPnBtePapx = PnFkpPapx(self.bytes, self.mainStream, self.getOffset(self.pos, i), 4, "aPnBtePapx")
            aPnBtePapx.dump()
            print('</aFC>')
        print('</plcBtePapx>')


class Pcdt(BinaryStream):
    """The Pcdt structure contains a PlcPcd structure and specifies its size."""
    def __init__(self, bytes, mainStream, offset, size):
        BinaryStream.__init__(self, bytes, mainStream=mainStream)
        self.pos = offset
        self.size = size

        self.clxt = self.readuInt8()
        self.lcb = self.readuInt32()
        self.plcPcd = PlcPcd(self.bytes, self.mainStream, self.pos, self.lcb)

    def dump(self):
        print('<pcdt type="Pcdt" offset="%d" size="%d bytes">' % (self.pos, self.size))
        self.printAndSet("clxt", self.clxt)
        self.printAndSet("lcb", self.lcb)
        self.plcPcd.dump()
        print('</pcdt>')


class PrcData(BinaryStream):
    """The PrcData structure specifies an array of Prl elements and the size of
    the array."""
    def __init__(self, parent):
        BinaryStream.__init__(self, parent.bytes)
        self.pos = parent.pos

        self.cbGrpprl = self.readInt16()
        pos = 0
        self.prls = []
        while self.cbGrpprl - pos > 0:
            prl = Prl(self, self.pos + pos)
            pos += prl.getSize()
            self.prls.append(prl)
        self.pos += self.cbGrpprl
        parent.pos = self.pos

    def dump(self):
        print('<prcData>')
        self.printAndSet("cbGrpprl", self.cbGrpprl)
        print('<grpPrl>')
        for i in self.prls:
            i.dump()
        print('</grpPrl>')
        print('</prcData>')


class Prc(BinaryStream):
    """The Prc structure specifies a set of properties for document content
    that is referenced by a Pcd structure."""
    def __init__(self, parent):
        BinaryStream.__init__(self, parent.bytes)
        self.pos = parent.pos

        self.clxt = self.readuInt8()
        self.prcData = PrcData(self)
        parent.pos = self.pos

    def dump(self, index):
        print('<prc index="%d">' % index)
        self.prcData.dump()
        print('</prc>')


class Clx(BinaryStream):
    def __init__(self, bytes, mainStream, offset, size):
        BinaryStream.__init__(self, bytes, mainStream=mainStream)
        self.pos = offset
        self.size = size

        self.firstByte = self.getuInt8()
        self.prcs = []
        while True:
            self.firstByte = self.getuInt8()
            if self.firstByte != 0x01:
                break
            self.prcs.append(Prc(self))
        self.pcdt = Pcdt(self.bytes, self.mainStream, self.pos, self.size)

    def dump(self):
        print('<clx type="Clx" offset="%d" size="%d bytes">' % (self.pos, self.size))
        for index, elem in enumerate(self.prcs):
            elem.dump(index)
        self.pcdt.dump()
        print('</clx>')


class Copts60(BinaryStream):
    """The Copts60 structure specifies compatibility options."""
    def __init__(self, dop):
        BinaryStream.__init__(self, dop.bytes)
        self.pos = dop.pos

    def dump(self):
        print('<copts60 type="Copts60" offset="%s" size="2 bytes">' % self.pos)
        # Copts60 first byte
        buf = self.readuInt8()
        self.printAndSet("fNoTabForInd", self.getBit(buf, 0))
        self.printAndSet("fNoSpaceRaiseLower", self.getBit(buf, 1))
        self.printAndSet("fSuppressSpBfAfterPgBrk", self.getBit(buf, 2))
        self.printAndSet("fWrapTrailSpaces", self.getBit(buf, 3))
        self.printAndSet("fMapPrintTextColor", self.getBit(buf, 4))
        self.printAndSet("fNoColumnBalance", self.getBit(buf, 5))
        self.printAndSet("fConvMailMergeEsc", self.getBit(buf, 6))
        self.printAndSet("fSuppressTopSpacing", self.getBit(buf, 7))

        # Copts60 second byte
        buf = self.readuInt8()
        self.printAndSet("fOrigWordTableRules", self.getBit(buf, 0))
        self.printAndSet("unused14", self.getBit(buf, 1))
        self.printAndSet("fShowBreaksInFrames", self.getBit(buf, 2))
        self.printAndSet("fSwapBordersFacingPgs", self.getBit(buf, 3))
        self.printAndSet("fLeaveBackslashAlone", self.getBit(buf, 4))
        self.printAndSet("fExpShRtn", self.getBit(buf, 5))
        self.printAndSet("fDntULTrlSpc", self.getBit(buf, 6))
        self.printAndSet("fDntBlnSbDbWid", self.getBit(buf, 7))
        print('</copts60>')


class DTTM(BinaryStream):
    """The DTTM structure specifies date and time."""
    def __init__(self, parent, name):
        BinaryStream.__init__(self, parent.bytes)
        self.parent = parent
        self.pos = parent.pos
        self.name = name

    def dump(self):
        buf = self.readuInt32()
        print('<%s type="DTTM" offset="%d" size="4 bytes">' % (self.name, self.pos))
        self.printAndSet("mint", buf & 0x0000003f)  # 1..6th bits
        self.printAndSet("hr", (buf & 0x000007c0) >> 6)  # 7..11th bits
        self.printAndSet("dom", (buf & 0x0000f800) >> 11)  # 12..16th bits
        self.printAndSet("mon", (buf & 0x000f0000) >> 16)  # 17..20th bits
        self.printAndSet("yr", (buf & 0x1ff00000) >> 20)  # 21..29th bits
        self.printAndSet("wdy", (buf & 0xe0000000) >> 29)  # 30..32th bits
        print('<transformed value="%s-%s-%s %s:%s"/>' % (1900 + self.yr, self.mon, self.dom, self.hr, self.mint))
        print('</%s>' % self.name)
        self.parent.pos = self.pos


class GRFSTD(BinaryStream):
    """The GRFSTD structure specifies the general properties of a style."""
    def __init__(self, parent):
        BinaryStream.__init__(self, parent.bytes)
        self.parent = parent
        self.pos = parent.pos

    def dump(self):
        print('<grfstd type="GRFSTD" offset="%d" size="2 bytes">' % self.pos)
        buf = self.readuInt8()
        self.printAndSet("fAutoRedef", self.getBit(buf, 0))
        self.printAndSet("fHidden", self.getBit(buf, 1))
        self.printAndSet("f97LidsSet", self.getBit(buf, 2))
        self.printAndSet("fCopyLang", self.getBit(buf, 3))
        self.printAndSet("fPersonalCompose", self.getBit(buf, 4))
        self.printAndSet("fPersonalReply", self.getBit(buf, 5))
        self.printAndSet("fPersonal", self.getBit(buf, 6))
        self.printAndSet("fNoHtmlExport", self.getBit(buf, 7))

        buf = self.readuInt8()
        self.printAndSet("fSemiHidden", self.getBit(buf, 0))
        self.printAndSet("fLocked", self.getBit(buf, 1))
        self.printAndSet("fInternalUse", self.getBit(buf, 2))
        self.printAndSet("fUnhideWhenUsed", self.getBit(buf, 3))
        self.printAndSet("fQFormat", self.getBit(buf, 4))
        self.printAndSet("fReserved", (buf & 0xe0) >> 5)  # 6..8th bits
        print('</grfstd>')
        self.parent.pos = self.pos


class DopBase(BinaryStream):
    """The DopBase structure contains document and compatibility settings."""
    size = 84

    def __init__(self, dop):
        BinaryStream.__init__(self, dop.bytes)
        self.pos = dop.pos
        self.dop = dop

    def dump(self):
        print('<dopBase offset="%d" size="%d bytes">' % (self.pos, 84))
        buf = self.readuInt8()
        self.printAndSet("fFacingPages", self.getBit(buf, 0))
        self.printAndSet("unused1", self.getBit(buf, 1))
        self.printAndSet("fPMHMainDoc", self.getBit(buf, 2))
        self.printAndSet("unused2", (buf & 0x18) >> 3)  # 4..5th bits
        self.printAndSet("fpc", (buf & 0x60) >> 5)  # 6..7th bits
        self.printAndSet("unused3", self.getBit(buf, 7))

        self.printAndSet("unused4", self.readuInt8())

        buf = self.readuInt16()
        self.printAndSet("rncFtn", buf & 0x03)  # 1..2nd bits
        self.printAndSet("nFtn", (buf & 0xfffc) >> 2)  # 3..16th bits

        buf = self.readuInt8()
        self.printAndSet("unused5", self.getBit(buf, 0))
        self.printAndSet("unused6", self.getBit(buf, 1))
        self.printAndSet("unused7", self.getBit(buf, 2))
        self.printAndSet("unused8", self.getBit(buf, 3))
        self.printAndSet("unused9", self.getBit(buf, 4))
        self.printAndSet("unused10", self.getBit(buf, 5))
        self.printAndSet("fSplAllDone", self.getBit(buf, 6))
        self.printAndSet("fSplAllClean", self.getBit(buf, 7))

        buf = self.readuInt8()
        self.printAndSet("fSplHideErrors", self.getBit(buf, 0))
        self.printAndSet("fGramHideErrors", self.getBit(buf, 1))
        self.printAndSet("fLabelDoc", self.getBit(buf, 2))
        self.printAndSet("fHyphCapitals", self.getBit(buf, 3))
        self.printAndSet("fAutoHyphen", self.getBit(buf, 4))
        self.printAndSet("fFormNoFields", self.getBit(buf, 5))
        self.printAndSet("fLinkStyles", self.getBit(buf, 6))
        self.printAndSet("fRevMarking", self.getBit(buf, 7))

        buf = self.readuInt8()
        self.printAndSet("unused11", self.getBit(buf, 0))
        self.printAndSet("fExactCWords", self.getBit(buf, 1))
        self.printAndSet("fPagHidden", self.getBit(buf, 2))
        self.printAndSet("fPagResults", self.getBit(buf, 3))
        self.printAndSet("fLockAtn", self.getBit(buf, 4))
        self.printAndSet("fMirrorMargins", self.getBit(buf, 5))
        self.printAndSet("fWord97Compat", self.getBit(buf, 6))
        self.printAndSet("unused12", self.getBit(buf, 7))

        buf = self.readuInt8()
        self.printAndSet("unused13", self.getBit(buf, 0))
        self.printAndSet("fProtEnabled", self.getBit(buf, 1))
        self.printAndSet("fDispFormFldSel", self.getBit(buf, 2))
        self.printAndSet("fRMView", self.getBit(buf, 3))
        self.printAndSet("fRMPrint", self.getBit(buf, 4))
        self.printAndSet("fLockVbaProj", self.getBit(buf, 5))
        self.printAndSet("fLockRev", self.getBit(buf, 6))
        self.printAndSet("fEmbedFonts", self.getBit(buf, 7))

        copts60 = Copts60(self)
        copts60.dump()
        self.pos += 2

        self.printAndSet("dxaTab", self.readuInt16())
        self.printAndSet("cpgWebOpt", self.readuInt16())
        self.printAndSet("dxaHotZ", self.readuInt16())
        self.printAndSet("cConsecHypLim", self.readuInt16())
        self.printAndSet("wSpare2", self.readuInt16())
        DTTM(self, "dttmCreated").dump()
        DTTM(self, "dttmRevised").dump()
        DTTM(self, "dttmLastprint").dump()
        self.printAndSet("nRevision", self.readInt16())
        self.printAndSet("tmEdited", self.readInt32())
        self.printAndSet("cWords", self.readInt32())
        self.printAndSet("cCh", self.readInt32())
        self.printAndSet("cPg", self.readInt16())
        self.printAndSet("cParas", self.readInt32())

        buf = self.readuInt16()
        self.printAndSet("rncEdn", buf & 0x0003)  # 1..2nd bits
        self.printAndSet("nEdn", (buf & 0xfffc) >> 2)  # 3..16th bits

        buf = self.readuInt16()
        self.printAndSet("epc", buf & 0x0003)  # 1..2nd bits
        self.printAndSet("unused14", (buf & 0x003c) >> 2)  # 3..6th bits
        self.printAndSet("unused15", (buf & 0x03c0) >> 6)  # 7..10th bits
        self.printAndSet("fPrintFormData", self.getBit(buf, 10))
        self.printAndSet("fSaveFormData", self.getBit(buf, 11))
        self.printAndSet("fShadeFormData", self.getBit(buf, 12))
        self.printAndSet("fShadeMergeFields", self.getBit(buf, 13))
        self.printAndSet("reserved2", self.getBit(buf, 14))
        self.printAndSet("fIncludeSubdocsInStats", self.getBit(buf, 15))

        self.printAndSet("cLines", self.readInt32())
        self.printAndSet("cWordsWithSubdocs", self.readInt32())
        self.printAndSet("cChWithSubdocs", self.readInt32())
        self.printAndSet("cPgWithSubdocs", self.readInt16())
        self.printAndSet("cParasWithSubdocs", self.readInt32())
        self.printAndSet("cLinesWithSubdocs", self.readInt32())
        self.printAndSet("lKeyProtDoc", self.readInt32())

        buf = self.readuInt16()
        self.printAndSet("wvkoSaved", buf & 0x0007)  # 1..3rd bits
        self.printAndSet("pctWwdSaved", (buf & 0x0ff8) >> 3)  # 4..12th bits
        self.printAndSet("zkSaved", (buf & 0x3000) >> 12)  # 13..14th bits
        self.printAndSet("unused16", self.getBit(buf, 14))
        self.printAndSet("iGutterPos", self.getBit(buf, 15))
        print('</dopBase>')
        assert self.pos == self.dop.pos + DopBase.size
        self.dop.pos = self.pos


class Copts80(BinaryStream):
    """The Copts80 structure specifies compatibility options."""
    def __init__(self, dop):
        BinaryStream.__init__(self, dop.bytes)
        self.pos = dop.pos

    def dump(self):
        print('<copts80 type="Copts80" offset="%d" size="4 bytes">' % self.pos)
        Copts60(self).dump()
        self.pos += 2

        buf = self.readuInt8()
        self.printAndSet("fSuppressTopSpacingMac5", self.getBit(buf, 0))
        self.printAndSet("fTruncDxaExpand", self.getBit(buf, 1))
        self.printAndSet("fPrintBodyBeforeHdr", self.getBit(buf, 2))
        self.printAndSet("fNoExtLeading", self.getBit(buf, 3))
        self.printAndSet("fDontMakeSpaceForUL", self.getBit(buf, 4))
        self.printAndSet("fMWSmallCaps", self.getBit(buf, 5))
        self.printAndSet("f2ptExtLeadingOnly", self.getBit(buf, 6))
        self.printAndSet("fTruncFontHeight", self.getBit(buf, 7))

        buf = self.readuInt8()
        self.printAndSet("fSubOnSize", self.getBit(buf, 0))
        self.printAndSet("fLineWrapLikeWord6", self.getBit(buf, 1))
        self.printAndSet("fWW6BorderRules", self.getBit(buf, 2))
        self.printAndSet("fExactOnTop", self.getBit(buf, 3))
        self.printAndSet("fExtraAfter", self.getBit(buf, 4))
        self.printAndSet("fWPSpace", self.getBit(buf, 5))
        self.printAndSet("fWPJust", self.getBit(buf, 6))
        self.printAndSet("fPrintMet", self.getBit(buf, 7))
        print('</copts80>')


class Copts(BinaryStream):
    """A structure that specifies compatibility options."""
    size = 32

    def __init__(self, dop):
        BinaryStream.__init__(self, dop.bytes)
        self.pos = dop.pos
        self.dop = dop

    def dump(self):
        print('<copts type="Copts" offset="%d" size="%d bytes">' % (self.pos, Copts.size))
        Copts80(self).dump()
        self.pos += 4

        buf = self.readuInt8()
        self.printAndSet("fSpLayoutLikeWW8", self.getBit(buf, 0))
        self.printAndSet("fFtnLayoutLikeWW8", self.getBit(buf, 1))
        self.printAndSet("fDontUseHTMLParagraphAutoSpacing", self.getBit(buf, 2))
        self.printAndSet("fDontAdjustLineHeightInTable", self.getBit(buf, 3))
        self.printAndSet("fForgetLastTabAlign", self.getBit(buf, 4))
        self.printAndSet("fUseAutospaceForFullWidthAlpha", self.getBit(buf, 5))
        self.printAndSet("fAlignTablesRowByRow", self.getBit(buf, 6))
        self.printAndSet("fLayoutRawTableWidth", self.getBit(buf, 7))

        buf = self.readuInt8()
        self.printAndSet("fLayoutTableRowsApart", self.getBit(buf, 0))
        self.printAndSet("fUseWord97LineBreakingRules", self.getBit(buf, 1))
        self.printAndSet("fDontBreakWrappedTables", self.getBit(buf, 2))
        self.printAndSet("fDontSnapToGridInCell", self.getBit(buf, 3))
        self.printAndSet("fDontAllowFieldEndSelect", self.getBit(buf, 4))
        self.printAndSet("fApplyBreakingRules", self.getBit(buf, 5))
        self.printAndSet("fDontWrapTextWithPunct", self.getBit(buf, 6))
        self.printAndSet("fDontUseAsianBreakRules", self.getBit(buf, 7))

        buf = self.readuInt8()
        self.printAndSet("fUseWord2002TableStyleRules", self.getBit(buf, 0))
        self.printAndSet("fGrowAutoFit", self.getBit(buf, 1))
        self.printAndSet("fUseNormalStyleForList", self.getBit(buf, 2))
        self.printAndSet("fDontUseIndentAsNumberingTabStop", self.getBit(buf, 3))
        self.printAndSet("fFELineBreak11", self.getBit(buf, 4))
        self.printAndSet("fAllowSpaceOfSameStyleInTable", self.getBit(buf, 5))
        self.printAndSet("fWW11IndentRules", self.getBit(buf, 6))
        self.printAndSet("fDontAutofitConstrainedTables", self.getBit(buf, 7))

        buf = self.readuInt8()
        self.printAndSet("fAutofitLikeWW11", self.getBit(buf, 0))
        self.printAndSet("fUnderlineTabInNumList", self.getBit(buf, 1))
        self.printAndSet("fHangulWidthLikeWW11", self.getBit(buf, 2))
        self.printAndSet("fSplitPgBreakAndParaMark", self.getBit(buf, 3))
        self.printAndSet("fDontVertAlignCellWithSp", self.getBit(buf, 4))
        self.printAndSet("fDontBreakConstrainedForcedTables", self.getBit(buf, 5))
        self.printAndSet("fDontVertAlignInTxbx", self.getBit(buf, 6))
        self.printAndSet("fWord11KerningPairs", self.getBit(buf, 7))

        buf = self.readuInt32()
        self.printAndSet("fCachedColBalance", self.getBit(buf, 0))
        self.printAndSet("empty1", (buf & 0xfffffffe) >> 1)  # 2..32th bits
        self.printAndSet("empty2", self.readuInt32())
        self.printAndSet("empty3", self.readuInt32())
        self.printAndSet("empty4", self.readuInt32())
        self.printAndSet("empty5", self.readuInt32())
        self.printAndSet("empty6", self.readuInt32())
        print('</copts>')
        assert self.pos == self.dop.pos + Copts.size
        self.dop.pos = self.pos


class Dop95(BinaryStream):
    """The Dop95 structure contains document and compatibility settings."""
    size = 88

    def __init__(self, dop, dopSize):
        BinaryStream.__init__(self, dop.bytes)
        self.pos = dop.pos
        self.dop = dop
        self.dopSize = dopSize

    def dump(self):
        print('<dop95 type="Dop95" offset="%d" size="88 bytes">' % self.pos)
        pos = self.pos
        dopBase = DopBase(self)
        dopBase.dump()
        if self.pos >= pos + self.dopSize:
            print('</dop95>')
            self.dop.pos = self.pos
            return
        Copts80(self).dump()
        self.pos += 4
        print('</dop95>')
        assert self.pos == self.dop.pos + Dop95.size
        self.dop.pos = self.pos


class DopTypography(BinaryStream):
    """The DopTypography structure contains East Asian language typography settings."""
    size = 310

    def __init__(self, dop):
        BinaryStream.__init__(self, dop.bytes)
        self.pos = dop.pos
        self.dop = dop

    def dump(self):
        print('<dopTypography type="DopTypography" offset="%d" size="310 bytes">' % self.pos)
        buf = self.readuInt16()
        self.printAndSet("fKerningPunct", self.getBit(buf, 0))
        self.printAndSet("iJustification", (buf & 0x0006) >> 1)  # 2..3rd bits
        self.printAndSet("iLevelOfKinsoku", (buf & 0x0018) >> 1)  # 4..5th bits
        self.printAndSet("f2on1", self.getBit(buf, 5))
        self.printAndSet("unused", self.getBit(buf, 6))
        self.printAndSet("iCustomKsu", (buf & 0x0380) >> 7)  # 8..10th bits
        self.printAndSet("fJapaneseUseLevel2", self.getBit(buf, 10))
        self.printAndSet("reserved", (buf & 0xf800) >> 11)  # 12..16th bits

        self.printAndSet("cchFollowingPunct", self.readInt16())
        self.printAndSet("cchLeadingPunct", self.readInt16())

        self.printAndSet("rgxchFPunct", self.getString(self.cchFollowingPunct), hexdump=False)
        self.pos += 202

        self.printAndSet("rgxchLPunct", self.getString(self.cchLeadingPunct), hexdump=False)
        self.pos += 102

        print('</dopTypography>')
        assert self.pos == self.dop.pos + DopTypography.size
        self.dop.pos = self.pos


class Dogrid(BinaryStream):
    """The Dogrid structure specifies parameters for the drawn object properties of the document."""
    size = 10

    def __init__(self, dop):
        BinaryStream.__init__(self, dop.bytes)
        self.pos = dop.pos
        self.dop = dop

    def dump(self):
        print('<dogrid type="Dogrid" offset="%d" size="%d bytes">' % (self.pos, Dogrid.size))
        self.printAndSet("xaGrid", self.readuInt16())
        self.printAndSet("yaGrid", self.readuInt16())
        self.printAndSet("dxaGrid", self.readuInt16())
        self.printAndSet("dyaGrid", self.readuInt16())

        buf = self.readuInt8()
        self.printAndSet("dyGridDisplay", (buf & 0x7f))  # 1..7th bits
        self.printAndSet("unused", self.getBit(buf, 7))

        buf = self.readuInt8()
        self.printAndSet("dxGridDisplay", (buf & 0x7f))  # 1..7th bits
        self.printAndSet("fFollowMargins", self.getBit(buf, 7))
        print('</dogrid>')
        assert self.pos == self.dop.pos + Dogrid.size
        self.dop.pos = self.pos


class Asumyi(BinaryStream):
    """The Asumyi structure specifies AutoSummary state information"""
    def __init__(self, dop):
        BinaryStream.__init__(self, dop.bytes)
        self.pos = dop.pos

    def dump(self):
        print('<asumyi type="Asumyi" offset="%d" size="12 bytes">' % self.pos)
        buf = self.readuInt16()
        self.printAndSet("fValid", self.getBit(buf, 0))
        self.printAndSet("fView", self.getBit(buf, 1))
        self.printAndSet("iViewBy", (buf & 0x0c) >> 2)  # 3..4th bits
        self.printAndSet("fUpdateProps", self.getBit(buf, 4))
        self.printAndSet("reserved", (buf & 0xffe0) >> 5)  # 6..16th bits

        self.printAndSet("wDlgLevel", self.readuInt16())
        self.printAndSet("lHighestLevel", self.readuInt32())
        self.printAndSet("lCurrentLevel", self.readuInt32())
        print('</asumyi>')


class Dop97(BinaryStream):
    """The Dop97 structure contains document and compatibility settings."""
    size = 500

    def __init__(self, dop, dopSize):
        BinaryStream.__init__(self, dop.bytes)
        self.pos = dop.pos
        self.dop = dop
        self.dopSize = dopSize

    def dump(self):
        print('<dop97 type="Dop97" offset="%d" size="%d bytes">' % (self.pos, Dop97.size))
        pos = self.pos
        dop95 = Dop95(self, self.dopSize)
        dop95.dump()
        if self.pos >= pos + self.dopSize:
            print('</dop97>')
            self.dop.pos = self.pos
            return

        self.printAndSet("adt", self.readuInt16())
        dopTypography = DopTypography(self)
        dopTypography.dump()
        dogrid = Dogrid(self)
        dogrid.dump()

        buf = self.readuInt8()
        self.printAndSet("unused1", self.getBit(buf, 0))
        self.printAndSet("lvlDop", (buf & 0x1e) >> 1)  # 2..5th bits
        self.printAndSet("fGramAllDone", self.getBit(buf, 5))
        self.printAndSet("fGramAllClean", self.getBit(buf, 6))
        self.printAndSet("fSubsetFonts", self.getBit(buf, 7))

        buf = self.readuInt8()
        self.printAndSet("unused2", self.getBit(buf, 0))
        self.printAndSet("fHtmlDoc", self.getBit(buf, 1))
        self.printAndSet("fDiskLvcInvalid", self.getBit(buf, 2))
        self.printAndSet("fSnapBorder", self.getBit(buf, 3))
        self.printAndSet("fIncludeHeader", self.getBit(buf, 4))
        self.printAndSet("fIncludeFooter", self.getBit(buf, 5))
        self.printAndSet("unused3", self.getBit(buf, 6))
        self.printAndSet("unused4", self.getBit(buf, 7))

        self.printAndSet("unused5", self.readuInt16())
        Asumyi(self).dump()
        self.pos += 12
        self.printAndSet("cChWS", self.readuInt32())
        self.printAndSet("cChWSWithSubdocs", self.readuInt32())
        self.printAndSet("grfDocEvents", self.readuInt32())

        buf = self.readuInt32()
        self.printAndSet("fVirusPrompted", self.getBit(buf, 0))
        self.printAndSet("fVirusLoadSafe", self.getBit(buf, 1))
        self.printAndSet("KeyVirusSession30", (buf & 0xfffffffc) >> 2)

        self.printAndSet("space1", self.readuInt32())
        self.printAndSet("space2", self.readuInt32())
        self.printAndSet("space3", self.readuInt32())
        self.printAndSet("space4", self.readuInt32())
        self.printAndSet("space5", self.readuInt32())
        self.printAndSet("space6", self.readuInt32())
        self.printAndSet("space7", self.readuInt32())
        self.printAndSet("space8", self.readuInt16())

        self.printAndSet("cpMaxListCacheMainDoc", self.readuInt32())
        self.printAndSet("ilfoLastBulletMain", self.readuInt16())
        self.printAndSet("ilfoLastNumberMain", self.readuInt16())
        self.printAndSet("cDBC", self.readuInt32())
        self.printAndSet("cDBCWithSubdocs", self.readuInt32())
        self.printAndSet("reserved3a", self.readuInt32())
        self.printAndSet("nfcFtnRef", self.readuInt16())
        self.printAndSet("nfcEdnRef", self.readuInt16())
        self.printAndSet("hpsZoomFontPag", self.readuInt16())
        self.printAndSet("dywDispPag", self.readuInt16())
        print('</dop97>')
        assert self.pos == self.dop.pos + Dop97.size
        self.dop.pos = self.pos


class Dop2000(BinaryStream):
    """The Dop2000 structure contains document and compatibility settings."""
    size = 544

    def __init__(self, dop, dopSize):
        BinaryStream.__init__(self, dop.bytes)
        self.pos = dop.pos
        self.dop = dop
        self.dopSize = dopSize

    def dump(self):
        print('<dop2000 type="Dop2000" offset="%d" size="544 bytes">' % self.pos)
        dop97 = Dop97(self, self.dopSize)
        dop97.dump()

        if self.pos == self.size:
            print('<info what="Dop2000 size is smaller than expected."/>')
            print('</dop2000>')
            self.dop.pos = self.pos
            return

        self.printAndSet("ilvlLastBulletMain", self.readuInt8())
        self.printAndSet("ilvlLastNumberMain", self.readuInt8())
        self.printAndSet("istdClickParaType", self.readuInt16())

        buf = self.readuInt8()
        self.printAndSet("fLADAllDone", self.getBit(buf, 0))
        self.printAndSet("fEnvelopeVis", self.getBit(buf, 1))
        self.printAndSet("fMaybeTentativeListInDoc", self.getBit(buf, 2))
        self.printAndSet("fMaybeFitText", self.getBit(buf, 3))
        self.printAndSet("empty1", (buf & 0xf0) >> 4)  # 5..8th bits

        buf = self.readuInt8()
        self.printAndSet("fFCCAllDone", self.getBit(buf, 0))
        self.printAndSet("fRelyOnCSS_WebOpt", self.getBit(buf, 1))
        self.printAndSet("fRelyOnVML_WebOpt", self.getBit(buf, 2))
        self.printAndSet("fAllowPNG_WebOpt", self.getBit(buf, 3))
        self.printAndSet("screenSize_WebOpt", (buf & 0xf0) >> 4)  # 5..8th bits

        buf = self.readuInt16()
        self.printAndSet("fOrganizeInFolder_WebOpt", self.getBit(buf, 0))
        self.printAndSet("fUseLongFileNames_WebOpt", self.getBit(buf, 1))
        self.printAndSet("iPixelsPerInch_WebOpt", (buf & 0x0ffc) >> 2)  # 3..12th bits
        self.printAndSet("fWebOptionsInit", self.getBit(buf, 12))
        self.printAndSet("fMaybeFEL", self.getBit(buf, 12))
        self.printAndSet("fCharLineUnits", self.getBit(buf, 12))
        self.printAndSet("unused1", self.getBit(buf, 12))

        copts = Copts(self)
        copts.dump()

        self.printAndSet("verCompatPre10", self.readuInt16())
        buf = self.readuInt8()
        self.printAndSet("fNoMargPgvwSaved", self.getBit(buf, 0))
        self.printAndSet("unused2", self.getBit(buf, 1))
        self.printAndSet("unused3", self.getBit(buf, 2))
        self.printAndSet("unused4", self.getBit(buf, 3))
        self.printAndSet("fBulletProofed", self.getBit(buf, 4))
        self.printAndSet("empty2", self.getBit(buf, 5))
        self.printAndSet("fSaveUim", self.getBit(buf, 6))
        self.printAndSet("fFilterPrivacy", self.getBit(buf, 7))

        buf = self.readuInt8()
        self.printAndSet("empty3", self.getBit(buf, 0))
        self.printAndSet("fSeenRepairs", self.getBit(buf, 1))
        self.printAndSet("fHasXML", self.getBit(buf, 2))
        self.printAndSet("unused5", self.getBit(buf, 3))
        self.printAndSet("fValidateXML", self.getBit(buf, 4))
        self.printAndSet("fSaveInvalidXML", self.getBit(buf, 5))
        self.printAndSet("fShowXMLErrors", self.getBit(buf, 6))
        self.printAndSet("fAlwaysMergeEmptyNamespace", self.getBit(buf, 7))
        print('</dop2000>')
        assert self.pos == self.dop.pos + Dop2000.size
        self.dop.pos = self.pos


class Dop2002(BinaryStream):
    """The Dop2002 structure contains document and compatibility settings."""
    size = 594

    def __init__(self, dop, dopSize):
        BinaryStream.__init__(self, dop.bytes)
        self.pos = dop.pos
        self.dop = dop
        self.dopSize = dopSize

    def dump(self):
        print('<dop2002 type="Dop2002" offset="%d" size="%d bytes">' % (self.pos, Dop2002.size))
        dop2000 = Dop2000(self, self.dopSize)
        dop2000.dump()

        self.printAndSet("unused", self.readuInt32())

        buf = self.readuInt8()
        self.printAndSet("fDoNotEmbedSystemFont", self.getBit(buf, 0))
        self.printAndSet("fWordCompat", self.getBit(buf, 1))
        self.printAndSet("fLiveRecover", self.getBit(buf, 2))
        self.printAndSet("fEmbedFactoids", self.getBit(buf, 3))
        self.printAndSet("fFactoidXML", self.getBit(buf, 4))
        self.printAndSet("fFactoidAllDone", self.getBit(buf, 5))
        self.printAndSet("fFolioPrint", self.getBit(buf, 6))
        self.printAndSet("fReverseFolio", self.getBit(buf, 7))

        buf = self.readuInt8()
        self.printAndSet("iTextLineEnding", (buf & 0x7))  # 1..3rd bits
        self.printAndSet("fHideFcc", self.getBit(buf, 3))
        self.printAndSet("fAcetateShowMarkup", self.getBit(buf, 4))
        self.printAndSet("fAcetateShowAtn", self.getBit(buf, 5))
        self.printAndSet("fAcetateShowInsDel", self.getBit(buf, 6))
        self.printAndSet("fAcetateShowProps", self.getBit(buf, 7))

        self.printAndSet("istdTableDflt", self.readuInt16())
        self.printAndSet("verCompat", self.readuInt16())
        self.printAndSet("grfFmtFilter", self.readuInt16())
        self.printAndSet("iFolioPages", self.readuInt16())
        self.printAndSet("cpgText", self.readuInt32())
        self.printAndSet("cpMinRMText", self.readuInt32())
        self.printAndSet("cpMinRMFtn", self.readuInt32())
        self.printAndSet("cpMinRMHdd", self.readuInt32())
        self.printAndSet("cpMinRMAtn", self.readuInt32())
        self.printAndSet("cpMinRMEdn", self.readuInt32())
        self.printAndSet("cpMinRmTxbx", self.readuInt32())
        self.printAndSet("cpMinRmHdrTxbx", self.readuInt32())
        self.printAndSet("rsidRoot", self.readuInt32())
        print('</dop2002>')
        assert self.pos == self.dop.pos + Dop2002.size
        self.dop.pos = self.pos


class Dop2003(BinaryStream):
    """The Dop2003 structure contains document and compatibility settings."""
    size = 616

    def __init__(self, dop, dopSize):
        BinaryStream.__init__(self, dop.bytes)
        self.pos = dop.pos
        self.dop = dop
        self.dopSize = dopSize

    def dump(self):
        print('<dop2003 type="Dop2003" offset="%d" size="616 bytes">' % self.pos)
        dop2002 = Dop2002(self, self.dopSize)
        dop2002.dump()

        buf = self.readuInt8()
        self.printAndSet("fTreatLockAtnAsReadOnly", self.getBit(buf, 0))
        self.printAndSet("fStyleLock", self.getBit(buf, 1))
        self.printAndSet("fAutoFmtOverride", self.getBit(buf, 2))
        self.printAndSet("fRemoveWordML", self.getBit(buf, 3))
        self.printAndSet("fApplyCustomXForm", self.getBit(buf, 4))
        self.printAndSet("fStyleLockEnforced", self.getBit(buf, 5))
        self.printAndSet("fFakeLockAtn", self.getBit(buf, 6))
        self.printAndSet("fIgnoreMixedContent", self.getBit(buf, 7))

        buf = self.readuInt8()
        self.printAndSet("fShowPlaceholderText", self.getBit(buf, 0))
        self.printAndSet("unused", self.getBit(buf, 1))
        self.printAndSet("fWord97Doc", self.getBit(buf, 2))
        self.printAndSet("fStyleLockTheme", self.getBit(buf, 3))
        self.printAndSet("fStyleLockQFSet", self.getBit(buf, 4))
        self.printAndSet("empty1", (buf & 0xe0) >> 5)  # 6..8th bits
        self.printAndSet("empty1_", self.readuInt16())

        buf = self.readuInt8()
        self.printAndSet("fReadingModeInkLockDown", self.getBit(buf, 0))
        self.printAndSet("fAcetateShowInkAtn", self.getBit(buf, 1))
        self.printAndSet("fFilterDttm", self.getBit(buf, 2))
        self.printAndSet("fEnforceDocProt", self.getBit(buf, 3))
        self.printAndSet("iDocProtCur", (buf & 0x70) >> 4)  # 5..7th bits
        self.printAndSet("fDispBkSpSaved", self.getBit(buf, 7))

        self.printAndSet("empty2", self.readuInt8())
        self.printAndSet("dxaPageLock", self.readuInt32())
        self.printAndSet("dyaPageLock", self.readuInt32())
        self.printAndSet("pctFontLock", self.readuInt32())
        self.printAndSet("grfitbid", self.readuInt8())
        self.printAndSet("empty3", self.readuInt8())
        self.printAndSet("ilfoMacAtCleanup", self.readuInt16())
        print('</dop2003>')
        assert self.pos == self.dop.pos + Dop2003.size
        self.dop.pos = self.pos


class DopMth(BinaryStream):
    """The DopMth structure specifies document-wide math settings."""
    def __init__(self, dop):
        BinaryStream.__init__(self, dop.bytes)
        self.pos = dop.pos

    def dump(self):
        print('<dopMth type="DopMth" offset="%d" size="34 bytes">' % self.pos)
        buf = self.readuInt32()
        self.printAndSet("mthbrk", (buf & 0x03))  # 1..2nd bits
        self.printAndSet("mthbrkSub", (buf & 0xc) >> 2)  # 3..4th bits
        self.printAndSet("mthbpjc", (buf & 0x70) >> 4)  # 5..7th bits
        self.printAndSet("reserved1", self.getBit(buf, 7))
        self.printAndSet("fMathSmallFrac", self.getBit(buf, 8))
        self.printAndSet("fMathIntLimUndOvr", self.getBit(buf, 9))
        self.printAndSet("fMathNaryLimUndOvr", self.getBit(buf, 10))
        self.printAndSet("fMathWrapAlignLeft", self.getBit(buf, 11))
        self.printAndSet("fMathUseDispDefaults", self.getBit(buf, 12))
        self.printAndSet("reserved2", (buf & 0xffffe000) >> 13)  # 14..32th bits

        self.printAndSet("ftcMath", self.readuInt16())
        self.printAndSet("dxaLeftMargin", self.readuInt32())
        self.printAndSet("dxaRightMargin", self.readuInt32())
        self.printAndSet("empty1", self.readuInt32())
        self.printAndSet("empty2", self.readuInt32())
        self.printAndSet("empty3", self.readuInt32())
        self.printAndSet("empty4", self.readuInt32())
        self.printAndSet("dxaIndentWrapped", self.readuInt32())
        print('</dopMth>')


class Dop2007(BinaryStream):
    """The Dop2007 structure contains document and compatibility settings."""
    def __init__(self, dop, dopSize):
        BinaryStream.__init__(self, dop.bytes)
        self.pos = dop.pos
        self.dop = dop
        self.dopSize = dopSize

    def dump(self):
        print('<dop2007 type="Dop2007" offset="%d">' % self.pos)
        dop2003 = Dop2003(self, self.dopSize)
        dop2003.dump()

        self.printAndSet("reserved1", self.readuInt32())

        buf = self.readuInt16()
        self.printAndSet("fRMTrackFormatting", self.getBit(buf, 0))
        self.printAndSet("fRMTrackMoves", self.getBit(buf, 1))
        self.printAndSet("reserved2", self.getBit(buf, 2))
        self.printAndSet("empty1", self.getBit(buf, 3))
        self.printAndSet("empty2", self.getBit(buf, 4))
        self.printAndSet("ssm", (buf & 0x01e0) >> 5)  # 6..9th bits
        self.printAndSet("fReadingModeInkLockDownActualPage", self.getBit(buf, 9))
        self.printAndSet("fAutoCompressPictures", self.getBit(buf, 10))
        self.printAndSet("reserved3", (buf & 0xf800) >> 11)  # 12..16th bits
        self.printAndSet("reserved3_", self.readuInt16())

        self.printAndSet("empty3", self.readuInt32())
        self.printAndSet("empty4", self.readuInt32())
        self.printAndSet("empty5", self.readuInt32())
        self.printAndSet("empty6", self.readuInt32())
        DopMth(self).dump()
        self.pos += 34
        print('</dop2007>')


class RC4EncryptionHeader(BinaryStream):
    """The encryption header structure used for RC4 encryption."""
    def __init__(self, fib, pos, size):
        BinaryStream.__init__(self, fib.getTableStream().bytes)
        self.fib = fib
        self.pos = pos
        self.size = size

    def dump(self):
        print('<RC4EncryptionHeader>')
        self.Salt = self.readBytes(16)
        print('<Salt value="%s"/>' % globals.encodeName(self.Salt))
        self.EncryptedVerifier = self.readBytes(16)
        print('<EncryptedVerifier value="%s"/>' % globals.encodeName(self.EncryptedVerifier))
        self.EncryptedVerifierHash = self.readBytes(16)
        print('<EncryptedVerifierHash value="%s"/>' % globals.encodeName(self.EncryptedVerifierHash))
        print('</RC4EncryptionHeader>')
        assert self.pos == self.size


class Dop(BinaryStream):
    """The Dop structure contains the document and compatibility settings for the document."""
    def __init__(self, fib):
        BinaryStream.__init__(self, fib.getTableStream().bytes)
        self.pos = fib.fcDop
        self.size = fib.lcbDop
        self.fib = fib

    def dump(self):
        print('<dop type="Dop" offset="%s" size="%d bytes">' % (self.pos, self.size))
        if self.fib.nFibNew == 0:
            Dop97(self, self.size).dump()
        elif self.fib.nFibNew == 0x00d9:
            Dop2000(self, self.size).dump()
        elif self.fib.nFibNew == 0x0101:
            Dop2002(self, self.size).dump()
        elif self.fib.nFibNew == 0x0112:
            Dop2007(self, self.size).dump()
        else:
            print("""<todo what="Dop.dump() doesn't know how to handle nFibNew = %s"/>""" % hex(self.fib.nFibNew))
        print('</dop>')


class FFID(BinaryStream):
    """The FFID structure specifies the font family and character pitch for a font."""
    def __init__(self, bytes, offset):
        BinaryStream.__init__(self, bytes)
        self.pos = offset
        self.unused1 = None
        self.unused2 = None

    def dump(self):
        self.ffid = self.readuInt8()

        self.prq = (self.ffid & 0x3)  # first two bits
        self.fTrueType = (self.ffid & 0x4) >> 2  # 3rd bit
        self.unused1 = (self.ffid & 0x8) >> 3  # 4th bit
        self.ff = (self.ffid & 0x70) >> 4  # 5-7th bits
        self.unused2 = (self.ffid & 0x80) >> 7  # 8th bit

        print('<ffid value="%s" prq="%s" fTrueType="%s" ff="%s"/>' % (hex(self.ffid), hex(self.prq), self.fTrueType, hex(self.ff)))


class PANOSE(BinaryStream):
    """The PANOSE structure defines the PANOSE font classification values for a TrueType font."""
    def __init__(self, bytes, offset):
        BinaryStream.__init__(self, bytes)
        self.pos = offset

    def dump(self):
        print('<panose type="PANOSE" offset="%s" size="10 bytes">' % self.pos)
        for i in ["bFamilyType", "bSerifStyle", "bWeight", "bProportion", "bContrast", "bStrokeVariation", "bArmStyle", "bLetterform", "bMidline", "bHeight"]:
            self.printAndSet(i, self.readuInt8())
        print('</panose>')


class FontSignature(BinaryStream):
    """Contains information identifying the code pages and Unicode subranges for which a given font provides glyphs."""
    def __init__(self, bytes, offset):
        BinaryStream.__init__(self, bytes)
        self.pos = offset

    def dump(self):
        fsUsb1 = self.readuInt32()
        fsUsb2 = self.readuInt32()
        fsUsb3 = self.readuInt32()
        fsUsb4 = self.readuInt32()
        fsCsb1 = self.readuInt32()
        fsCsb2 = self.readInt32()
        print('<fontSignature fsUsb1="%s" fsUsb2="%s" fsUsb3="%s" fsUsb4="%s" fsCsb1="%s" fsCsb2="%s"/>' %
              (hex(fsUsb1), hex(fsUsb2), hex(fsUsb3), hex(fsUsb4), hex(fsCsb1), hex(fsCsb2))
              )


class FFN(BinaryStream):
    """The FFN structure specifies information about a font that is used in the document."""
    def __init__(self, bytes, mainStream, offset, size):
        BinaryStream.__init__(self, bytes, mainStream=mainStream)
        self.pos = offset
        self.size = size

    def dump(self):
        print('<ffn type="FFN" offset="%d" size="%d bytes">' % (self.pos, self.size))
        FFID(self.bytes, self.pos).dump()
        self.pos += 1
        self.printAndSet("wWeight", self.readInt16(), hexdump=False)
        self.printAndSet("chs", self.readuInt8(), hexdump=False)
        self.printAndSet("ixchSzAlt", self.readuInt8())
        PANOSE(self.bytes, self.pos).dump()
        self.pos += 10
        FontSignature(self.bytes, self.pos).dump()
        self.pos += 24
        print('<xszFfn value="%s"/>' % self.readString().replace('\\x00', ''))
        print('</ffn>')


class SttbfFfn(BinaryStream):
    """The SttbfFfn structure is an STTB whose strings are FFN records that specify details of system fonts."""
    def __init__(self, bytes, mainStream, offset, size):
        BinaryStream.__init__(self, bytes, mainStream=mainStream)
        self.pos = offset
        self.size = size

    def dump(self):
        print('<sttbfFfn type="SttbfFfn" offset="%d" size="%d bytes">' % (self.pos, self.size))
        self.printAndSet("cData", self.readuInt16())
        self.printAndSet("cbExtra", self.readuInt16())
        for i in range(self.cData):
            cchData = self.readuInt8()
            print('<cchData index="%d" offset="%d" size="%d bytes">' % (i, self.pos, cchData))
            FFN(self.bytes, self.mainStream, self.pos, cchData).dump()
            self.pos += cchData
            print('</cchData>')
        print('</sttbfFfn>')


class GrpXstAtnOwners(BinaryStream):
    """This array contains the names of authors of comments in the document."""
    def __init__(self, mainStream):
        BinaryStream.__init__(self, mainStream.getTableStream().bytes)
        self.pos = mainStream.fcGrpXstAtnOwners
        self.size = mainStream.lcbGrpXstAtnOwners
        self.mainStream = mainStream

    def dump(self):
        posOrig = self.pos
        print('<grpXstAtnOwners type="GrpXstAtnOwners" offset="%d" size="%d bytes">' % (self.pos, self.size))
        while self.pos < posOrig + self.size:
            xst = Xst(self)
            xst.dump()
            self.pos = xst.pos
        print('</grpXstAtnOwners>')


class SttbfAssoc(BinaryStream):
    """The SttbfAssoc structure is an STTB that contains strings which are associated with this document."""
    def __init__(self, mainStream):
        BinaryStream.__init__(self, mainStream.getTableStream().bytes)
        self.pos = mainStream.fcSttbfAssoc
        self.size = mainStream.lcbSttbfAssoc
        self.mainStream = mainStream

    def dump(self):
        indexMap = {
            0x00: "Unused. MUST be ignored.",
            0x01: "The path of the associated document template (2), if it is not the default Normal template.",
            0x02: "The title of the document.",
            0x03: "The subject of the document.",
            0x04: "Key words associated with the document.",
            0x05: "Unused. This index MUST be ignored.",
            0x06: "The author of the document.",
            0x07: "The user who last revised the document.",
            0x08: "The path of the associated mail merge data source.",
            0x09: "The path of the associated mail merge header document.",
            0x0A: "Unused. This index MUST be ignored.",
            0x0B: "Unused. This index MUST be ignored.",
            0x0C: "Unused. This index MUST be ignored.",
            0x0D: "Unused. This index MUST be ignored.",
            0x0E: "Unused. This index MUST be ignored.",
            0x0F: "Unused. This index MUST be ignored.",
            0x10: "Unused. This index MUST be ignored.",
            0x11: "The write-reservation password of the document.",
        }
        print('<sttbfAssoc type="SttbfAssoc" offset="%d" size="%d bytes">' % (self.pos, self.size))
        self.printAndSet("fExtend", self.readuInt16())
        self.printAndSet("cData", self.readuInt16())
        self.printAndSet("cbExtra", self.readuInt16())
        for i in range(self.cData):
            cchData = self.readuInt16()
            if i in indexMap.keys():
                meaning = indexMap[i]
            else:
                meaning = "unknown"
            if self.pos + 2 * cchData > self.size:
                self.cData = 0
                print('<info what="SttbfAssoc::dump() wanted to read beyond the end of the stream"/>')
                break
            print('<cchData index="%s" meaning="%s" offset="%d" size="%d bytes">' % (hex(i), meaning, self.pos, cchData))
            print('<string value="%s"/>' % globals.encodeName(self.bytes[self.pos:self.pos + 2 * cchData].decode('utf-16'), lowOnly=True))
            self.pos += 2 * cchData
            print('</cchData>')
        # Probably this was cleared manually.
        if self.cData != 0:
            assert self.pos == self.mainStream.fcSttbfAssoc + self.size
        print('</sttbfAssoc>')


class SttbfRMark(BinaryStream):
    """The SttbfRMark structure is an STTB structure where the strings specify the names of the authors of the revision marks, comments, and e-mail messages in the document."""
    def __init__(self, mainStream):
        BinaryStream.__init__(self, mainStream.getTableStream().bytes)
        self.pos = mainStream.fcSttbfRMark
        self.size = mainStream.lcbSttbfRMark
        self.mainStream = mainStream

    def dump(self):
        print('<sttbfRMark type="SttbfRMark" offset="%d" size="%d bytes">' % (self.pos, self.size))
        self.printAndSet("fExtend", self.readuInt16())
        self.printAndSet("cData", self.readuInt16())
        self.printAndSet("cbExtra", self.readuInt16())
        for i in range(self.cData):
            cchData = self.readuInt16()
            print('<cchData index="%s" offset="%d" size="%d bytes">' % (i, self.pos, cchData))
            print('<string value="%s"/>' % globals.encodeName(self.bytes[self.pos:self.pos + 2 * cchData].decode('utf-16'), lowOnly=True))
            self.pos += 2 * cchData
            print('</cchData>')
        if self.cData != 0:
            assert self.pos == self.mainStream.fcSttbfRMark + self.size
        print('</sttbfRMark>')


class OfficeArtWordDrawing(BinaryStream):
    """The OfficeArtWordDrawing structure specifies information about the drawings in the document."""
    def __init__(self, officeArtContent):
        BinaryStream.__init__(self, officeArtContent.bytes)
        self.pos = officeArtContent.pos
        self.officeArtContent = officeArtContent

    def dump(self):
        print('<officeArtWordDrawing type="OfficeArtWordDrawing" pos="%d">' % self.pos)
        self.printAndSet("dgglbl", self.readuInt8())
        msodraw.DgContainer(self, "container").dumpXml(self, getWordModel(self.officeArtContent.mainStream))
        print('</officeArtWordDrawing>')
        self.officeArtContent.pos = self.pos


class OfficeArtContent(BinaryStream):
    """The OfficeArtContent structure specifies information about a drawing in the document."""
    def __init__(self, mainStream):
        BinaryStream.__init__(self, mainStream.getTableStream().bytes)
        self.pos = mainStream.fcDggInfo
        self.size = mainStream.lcbDggInfo
        self.mainStream = mainStream

    def dump(self):
        print('<officeArtContent type="OfficeArtContent" offset="%d" size="%d bytes">' % (self.pos, self.size))
        msodraw.DggContainer(self, "DrawingGroupData").dumpXml(self, getWordModel(self.mainStream))
        print('<Drawings type="main" offset="%d">' % self.pos)
        OfficeArtWordDrawing(self).dump()
        print('</Drawings>')
        if self.pos < self.mainStream.fcDggInfo + self.size:
            print('<Drawings type="header" offset="%d">' % self.pos)
            OfficeArtWordDrawing(self).dump()
            print('</Drawings>')
        assert self.pos == self.mainStream.fcDggInfo + self.size
        print('</officeArtContent>')


class ATNBE(BinaryStream):
    """The ATNBE structure contains information about an annotation bookmark in the document."""
    size = 10  # in bytes, see 2.9.4

    def __init__(self, sttbfAtnBkmk):
        BinaryStream.__init__(self, sttbfAtnBkmk.bytes)
        self.pos = sttbfAtnBkmk.pos

    def dump(self):
        print('<atnbe type="ATNBE">')
        self.printAndSet("bmc", self.readuInt16())
        self.printAndSet("ITag", self.readuInt32())
        self.printAndSet("ITagOld", self.readuInt32())
        print('</atnbe>')


class SttbfAtnBkmk(BinaryStream):
    """The SttbfAtnBkmk structure is an STTB whose strings are all of zero length."""
    def __init__(self, mainStream, offset, size):
        BinaryStream.__init__(self, mainStream.getTableStream().bytes)
        self.pos = offset
        self.size = size

    def dump(self):
        print('<sttbfAtnBkmk type="SttbfAtnBkmk" offset="%d" size="%d bytes">' % (self.pos, self.size))
        self.printAndSet("fExtended", self.readuInt16())
        self.printAndSet("cData", self.readuInt16())
        self.printAndSet("cbExtra", self.readuInt16())
        for i in range(self.cData):
            cchData = self.readuInt16()
            print('<cchData index="%d" offset="%d" size="%d bytes"/>' % (i, self.pos, cchData))
            print('<extraData index="%d" offset="%d" size="%d bytes">' % (i, self.pos, ATNBE.size))
            atnbe = ATNBE(self)
            atnbe.dump()
            self.pos += ATNBE.size
            print('</extraData>')
        print('</sttbfAtnBkmk>')


class Stshif(BinaryStream):
    """The Stshif structure specifies general stylesheet information."""
    def __init__(self, bytes, mainStream, offset):
        BinaryStream.__init__(self, bytes, mainStream=mainStream)
        self.pos = offset
        self.size = 18

    def dump(self):
        print('<stshif type="Stshif" offset="%d" size="%d bytes">' % (self.pos, self.size))
        self.printAndSet("cstd", self.readuInt16())
        self.printAndSet("cbSTDBaseInFile", self.readuInt16())
        buf = self.readuInt16()
        self.printAndSet("fStdStylenamesWritten", buf & 1)  # first bit
        self.printAndSet("fReserved", (buf & 0xfe) >> 1)  # 2..16th bits
        self.printAndSet("stiMaxWhenSaved", self.readuInt16())
        self.printAndSet("istdMaxFixedWhenSaved", self.readuInt16())
        self.printAndSet("nVerBuiltInNamesWhenSaved", self.readuInt16())
        self.printAndSet("ftcAsci", self.readuInt16())
        self.printAndSet("ftcFE", self.readuInt16())
        self.printAndSet("ftcOther", self.readuInt16())
        print('</stshif>')


class LSD(BinaryStream):
    """The LSD structure specifies the properties to be used for latent application-defined styles (see StshiLsd) when they are created."""
    def __init__(self, bytes, offset):
        BinaryStream.__init__(self, bytes)
        self.pos = offset

    def dump(self):
        buf = self.readuInt16()
        self.printAndSet("fLocked", self.getBit(buf, 1))
        self.printAndSet("fSemiHidden", self.getBit(buf, 2))
        self.printAndSet("fUnhideWhenUsed", self.getBit(buf, 3))
        self.printAndSet("fQFormat", self.getBit(buf, 4))
        self.printAndSet("iPriority", (buf & 0xfff0) >> 4)  # 5-16th bits
        self.printAndSet("fReserved", self.readuInt16())


class StshiLsd(BinaryStream):
    """The StshiLsd structure specifies latent style data for application-defined styles."""
    def __init__(self, bytes, stshi, offset):
        BinaryStream.__init__(self, bytes)
        self.stshi = stshi
        self.pos = offset

    def dump(self):
        print('<stshiLsd type="StshiLsd" offset="%d">' % (self.pos))
        self.printAndSet("cbLSD", self.readuInt16())
        for i in range(self.stshi.stshif.stiMaxWhenSaved):
            print('<mpstiilsd index="%d" type="LSD">' % i)
            LSD(self.bytes, self.pos).dump()
            print('</mpstiilsd>')
            self.pos += self.cbLSD
        print('</stshiLsd>')


class STSHI(BinaryStream):
    """The STSHI structure specifies general stylesheet and related information."""
    def __init__(self, bytes, mainStream, offset, size):
        BinaryStream.__init__(self, bytes, mainStream=mainStream)
        self.pos = offset
        self.size = size

    def dump(self):
        print('<stshi type="STSHI" offset="%d" size="%d bytes">' % (self.pos, self.size))
        posOrig = self.pos
        self.stshif = Stshif(self.bytes, self.mainStream, self.pos)
        self.stshif.dump()
        self.pos += self.stshif.size
        if self.pos - posOrig < self.size:
            self.printAndSet("ftcBi", self.readuInt16())
            if self.pos - posOrig < self.size:
                stshiLsd = StshiLsd(self.bytes, self, self.pos)
                stshiLsd.dump()
        print('</stshi>')


class LPStshi(BinaryStream):
    """The LPStshi structure specifies general stylesheet information."""
    def __init__(self, bytes, mainStream, offset):
        BinaryStream.__init__(self, bytes, mainStream=mainStream)
        self.pos = offset

    def dump(self):
        print('<lpstshi type="LPStshi" offset="%d">' % self.pos)
        self.printAndSet("cbStshi", self.readuInt16(), hexdump=False)
        self.stshi = STSHI(self.bytes, self.mainStream, self.pos, self.cbStshi)
        self.stshi.dump()
        self.pos += self.cbStshi
        print('</lpstshi>')


class StdfBase(BinaryStream):
    """The Stdf structure specifies general information about the style."""
    def __init__(self, bytes, mainStream, offset):
        BinaryStream.__init__(self, bytes, mainStream=mainStream)
        self.pos = offset
        self.size = 10

    def dump(self):
        print('<stdfBase type="StdfBase" offset="%d" size="%d bytes">' % (self.pos, self.size))
        buf = self.readuInt16()
        self.printAndSet("sti", buf & 0x0fff)  # 1..12th bits
        self.printAndSet("fScratch", self.getBit(buf, 13))
        self.printAndSet("fInvalHeight", self.getBit(buf, 14))
        self.printAndSet("fHasUpe", self.getBit(buf, 15))
        self.printAndSet("fMassCopy", self.getBit(buf, 16))
        buf = self.readuInt16()
        self.stk = buf & 0x000f  # 1..4th bits
        stkmap = {
            1: "paragraph",
            2: "character",
            3: "table",
            4: "numbering"
        }
        print('<stk value="%d" name="%s"/>' % (self.stk, stkmap[self.stk]))
        self.printAndSet("istdBase", (buf & 0xfff0) >> 4)  # 5..16th bits
        buf = self.readuInt16()
        self.printAndSet("cupx", buf & 0x000f)  # 1..4th bits
        self.printAndSet("istdNext", (buf & 0xfff0) >> 4)  # 5..16th bits
        self.printAndSet("bchUpe", self.readuInt16(), hexdump=False)
        GRFSTD(self).dump()
        print('</stdfBase>')


class StdfPost2000(BinaryStream):
    """The StdfPost2000 structure specifies general information about a style."""
    def __init__(self, stdf):
        BinaryStream.__init__(self, stdf.bytes, mainStream=stdf.mainStream)
        self.pos = stdf.pos
        self.size = 8

    def dump(self):
        print('<stdfPost2000 type="StdfPost2000" offset="%d" size="%d bytes">' % (self.pos, self.size))
        buf = self.readuInt16()
        self.printAndSet("istdLink", buf & 0xfff)  # 1..12th bits
        self.printAndSet("fHasOriginalStyle", self.getBit(buf, 13))  # 13th bit
        self.printAndSet("fSpare", (buf & 0xe000) >> 13)  # 14..16th bits
        self.printAndSet("rsid", self.readuInt32())
        buf = self.readuInt16()
        self.printAndSet("iftcHtml", buf & 0x7)  # 1..3rd bits
        self.printAndSet("unused", self.getBit(buf, 4))
        self.printAndSet("iPriority", (buf & 0xfff0) >> 4)  # 5..16th bits
        print('</stdfPost2000>')


class Stdf(BinaryStream):
    """The Stdf structure specifies general information about the style."""
    def __init__(self, std):
        BinaryStream.__init__(self, std.bytes, mainStream=std.mainStream)
        self.std = std
        self.pos = std.pos

    def dump(self):
        print('<stdf type="Stdf" offset="%d">' % self.pos)
        self.stdfBase = StdfBase(self.bytes, self.mainStream, self.pos)
        self.stdfBase.dump()
        self.pos += self.stdfBase.size
        if self.pos - self.std.pos < self.std.size:
            stsh = self.std.lpstd.stsh  # root of the stylesheet table
            cbSTDBaseInFile = stsh.lpstshi.stshi.stshif.cbSTDBaseInFile
            print('<stdfPost2000OrNone cbSTDBaseInFile="%s">' % hex(cbSTDBaseInFile))
            if cbSTDBaseInFile == 0x0012:
                stdfPost2000 = StdfPost2000(self)
                stdfPost2000.dump()
                self.pos = stdfPost2000.pos
            print('</stdfPost2000OrNone>')
        print('</stdf>')


class Xst(BinaryStream):
    """The Xst structure is a string. The string is prepended by its length and is not null-terminated."""
    def __init__(self, parent):
        BinaryStream.__init__(self, parent.bytes)
        self.pos = parent.pos

    def dump(self):
        print('<xst type="Xst" offset="%d">' % self.pos)
        self.printAndSet("cch", self.readuInt16())
        lowOnly = locale.getdefaultlocale()[1] == "UTF-8"
        print('<rgtchar value="%s"/>' % globals.encodeName(self.bytes[self.pos:self.pos + 2 * self.cch].decode('utf-16'), lowOnly=lowOnly))
        self.pos += 2 * self.cch
        print('</xst>')


class Xstz(BinaryStream):
    """The Xstz structure is a string. The string is prepended by its length and is null-terminated."""
    def __init__(self, parent, name="xstz"):
        BinaryStream.__init__(self, parent.bytes)
        self.pos = parent.pos
        self.name = name

    def dump(self):
        print('<%s type="Xstz" offset="%d">' % (self.name, self.pos))
        xst = Xst(self)
        xst.dump()
        self.pos = xst.pos
        self.printAndSet("chTerm", self.readuInt16())
        print('</%s>' % self.name)


class UpxPapx(BinaryStream):
    """The UpxPapx structure specifies the paragraph formatting properties that differ from the parent"""
    def __init__(self, lPUpxPapx):
        BinaryStream.__init__(self, lPUpxPapx.bytes)
        self.lPUpxPapx = lPUpxPapx
        self.pos = lPUpxPapx.pos

    def dump(self):
        print('<upxPapx type="UpxPapx" offset="%d">' % self.pos)
        self.printAndSet("istd", self.readuInt16())
        size = self.lPUpxPapx.cbUpx - 2
        pos = 0
        print('<grpprlPapx offset="%d" size="%d bytes">' % (self.pos, size))
        while size - pos > 0:
            prl = Prl(self, self.pos + pos)
            prl.dump()
            pos += prl.getSize()
        print('</grpprlPapx>')
        print('</upxPapx>')


class UpxChpx(BinaryStream):
    """The UpxChpx structure specifies the character formatting properties that differ from the parent"""
    def __init__(self, lPUpxChpx):
        BinaryStream.__init__(self, lPUpxChpx.bytes)
        self.lPUpxChpx = lPUpxChpx
        self.pos = lPUpxChpx.pos

    def dump(self):
        print('<upxChpx type="UpxChpx" offset="%d">' % self.pos)
        size = self.lPUpxChpx.cbUpx
        pos = 0
        print('<grpprlChpx offset="%d" size="%d bytes">' % (self.pos, size))
        while size - pos > 0:
            prl = Prl(self, self.pos + pos)
            prl.dump()
            pos += prl.getSize()
        print('</grpprlChpx>')
        print('</upxChpx>')


class UpxTapx(BinaryStream):
    """The UpxTapx structure specifies the table formatting properties that differ from the parent"""
    def __init__(self, lPUpxTapx):
        BinaryStream.__init__(self, lPUpxTapx.bytes)
        self.lPUpxTapx = lPUpxTapx
        self.pos = lPUpxTapx.pos

    def dump(self):
        print('<upxTapx type="UpxTapx" offset="%d">' % self.pos)
        size = self.lPUpxTapx.cbUpx
        pos = 0
        print('<grpprlTapx offset="%d" size="%d bytes">' % (self.pos, size))
        while size - pos > 0:
            prl = Prl(self, self.pos + pos)
            prl.dump()
            pos += prl.getSize()
        print('</grpprlTapx>')
        print('</upxTapx>')


class UPXPadding:
    """The UPXPadding structure specifies the padding that is used to pad the UpxPapx/Chpx/Tapx structures if any of them are an odd number of bytes in length."""
    def __init__(self, parent):
        self.parent = parent
        self.pos = parent.pos

    def pad(self):
        if self.parent.cbUpx % 2 == 1:
            self.pos += 1


class LPUpxPapx(BinaryStream):
    """The LPUpxPapx structure specifies paragraph formatting properties."""
    def __init__(self, stkParaGRLPUPX):
        BinaryStream.__init__(self, stkParaGRLPUPX.bytes)
        self.pos = stkParaGRLPUPX.pos

    def dump(self):
        print('<lPUpxPapx type="LPUpxPapx" offset="%d">' % self.pos)
        self.printAndSet("cbUpx", self.readuInt16())
        upxPapx = UpxPapx(self)
        upxPapx.dump()
        self.pos += self.cbUpx
        uPXPadding = UPXPadding(self)
        uPXPadding.pad()
        self.pos = uPXPadding.pos
        print('</lPUpxPapx>')


class LPUpxChpx(BinaryStream):
    """The LPUpxChpx structure specifies character formatting properties."""
    def __init__(self, stkParaGRLPUPX):
        BinaryStream.__init__(self, stkParaGRLPUPX.bytes)
        self.pos = stkParaGRLPUPX.pos

    def dump(self):
        print('<lPUpxChpx type="LPUpxChpx" offset="%d">' % self.pos)
        self.printAndSet("cbUpx", self.readuInt16())
        upxChpx = UpxChpx(self)
        upxChpx.dump()
        self.pos += self.cbUpx
        uPXPadding = UPXPadding(self)
        uPXPadding.pad()
        self.pos = uPXPadding.pos
        print('</lPUpxChpx>')


class LPUpxTapx(BinaryStream):
    """The LPUpxTapx structure specifies table formatting properties."""
    def __init__(self, stkParaGRLPUPX):
        BinaryStream.__init__(self, stkParaGRLPUPX.bytes)
        self.pos = stkParaGRLPUPX.pos

    def dump(self):
        print('<lPUpxTapx type="LPUpxTapx" offset="%d">' % self.pos)
        self.printAndSet("cbUpx", self.readuInt16())
        upxTapx = UpxTapx(self)
        upxTapx.dump()
        self.pos += self.cbUpx
        uPXPadding = UPXPadding(self)
        uPXPadding.pad()
        self.pos = uPXPadding.pos
        print('</lPUpxTapx>')


class StkListGRLPUPX(BinaryStream):
    """The StkListGRLPUPX structure that specifies the formatting properties for a list style."""
    def __init__(self, grLPUpxSw):
        BinaryStream.__init__(self, grLPUpxSw.bytes)
        self.grLPUpxSw = grLPUpxSw
        self.pos = grLPUpxSw.pos

    def dump(self):
        print('<stkListGRLPUPX type="StkListGRLPUPX" offset="%d">' % self.pos)
        lpUpxPapx = LPUpxPapx(self)
        lpUpxPapx.dump()
        self.pos = lpUpxPapx.pos
        print('</stkListGRLPUPX>')


class StkTableGRLPUPX(BinaryStream):
    """The StkTableGRLPUPX structure that specifies the formatting properties for a table style."""
    def __init__(self, grLPUpxSw):
        BinaryStream.__init__(self, grLPUpxSw.bytes)
        self.grLPUpxSw = grLPUpxSw
        self.pos = grLPUpxSw.pos

    def dump(self):
        print('<stkTableGRLPUPX type="StkTableGRLPUPX" offset="%d">' % self.pos)
        lpUpxTapx = LPUpxTapx(self)
        lpUpxTapx.dump()
        self.pos = lpUpxTapx.pos
        lpUpxPapx = LPUpxPapx(self)
        lpUpxPapx.dump()
        self.pos = lpUpxPapx.pos
        lpUpxChpx = LPUpxChpx(self)
        lpUpxChpx.dump()
        self.pos = lpUpxChpx.pos
        print('</stkTableGRLPUPX>')


class StkCharGRLPUPX(BinaryStream):
    """The StkCharGRLPUPX structure that specifies the formatting properties for a character style."""
    def __init__(self, grLPUpxSw):
        BinaryStream.__init__(self, grLPUpxSw.bytes)
        self.grLPUpxSw = grLPUpxSw
        self.pos = grLPUpxSw.pos
        self.grLPUpxSw = grLPUpxSw

    def dump(self):
        print('<stkCharGRLPUPX type="StkCharGRLPUPX" offset="%d">' % self.pos)
        if self.grLPUpxSw.std.stdf.stdfBase.cupx == 1:
            lpUpxChpx = LPUpxChpx(self)
            lpUpxChpx.dump()
            self.pos = lpUpxChpx.pos
        else:
            print('<todo what="StkCharGRLPUPX: cupx != 1"/>')
        print('</stkCharGRLPUPX>')


class StkParaGRLPUPX(BinaryStream):
    """The StkParaGRLPUPX structure that specifies the formatting properties for a paragraph style."""
    def __init__(self, grLPUpxSw):
        BinaryStream.__init__(self, grLPUpxSw.bytes)
        self.grLPUpxSw = grLPUpxSw
        self.pos = grLPUpxSw.pos
        self.grLPUpxSw = grLPUpxSw

    def dump(self):
        print('<stkParaGRLPUPX type="StkParaGRLPUPX" offset="%d">' % self.pos)
        if self.grLPUpxSw.std.stdf.stdfBase.cupx == 2:
            lPUpxPapx = LPUpxPapx(self)
            lPUpxPapx.dump()
            self.pos = lPUpxPapx.pos
            lpUpxChpx = LPUpxChpx(self)
            lpUpxChpx.dump()
            self.pos = lpUpxChpx.pos
        else:
            print('<todo what="StkParaGRLPUPX: cupx != 2"/>')
        print('</stkParaGRLPUPX>')


class GrLPUpxSw(BinaryStream):
    """The GrLPUpxSw structure is an array of variable-size structures that specify the formatting of the style."""
    def __init__(self, std):
        BinaryStream.__init__(self, std.bytes)
        self.std = std
        self.pos = std.pos

    def dump(self):
        stkMap = {
            1: StkParaGRLPUPX,
            2: StkCharGRLPUPX,
            3: StkTableGRLPUPX,
            4: StkListGRLPUPX
        }
        child = stkMap[self.std.stdf.stdfBase.stk](self)
        child.dump()
        self.pos = child.pos


class STD(BinaryStream):
    """The STD structure specifies a style definition."""
    def __init__(self, lpstd):
        BinaryStream.__init__(self, lpstd.bytes, mainStream=lpstd.mainStream)
        self.lpstd = lpstd
        self.pos = lpstd.pos
        self.posOrig = self.pos
        self.size = lpstd.cbStd

    def dump(self):
        print('<std type="STD" offset="%d" size="%d bytes">' % (self.pos, self.size))
        self.stdf = Stdf(self)
        self.stdf.dump()
        self.pos = self.stdf.pos
        if self.pos - self.posOrig < self.size:
            xstzName = Xstz(self)
            xstzName.dump()
            self.pos = xstzName.pos
            grLPUpxSw = GrLPUpxSw(self)
            grLPUpxSw.dump()
            self.pos = grLPUpxSw.pos
        print('</std>')


class LPStd(BinaryStream):
    """The LPStd structure specifies a length-prefixed style definition."""
    def __init__(self, stsh):
        BinaryStream.__init__(self, stsh.bytes, mainStream=stsh.mainStream)
        self.stsh = stsh
        self.pos = stsh.pos

    def dump(self):
        self.printAndSet("cbStd", self.readuInt16())
        posOrig = self.pos
        if self.cbStd > 0:
            std = STD(self)
            std.dump()
            self.pos = std.pos
        self.pos = posOrig + self.cbStd


class STSH(BinaryStream):
    """The STSH structure specifies the stylesheet for a document."""
    def __init__(self, bytes, mainStream, offset, size):
        BinaryStream.__init__(self, bytes, mainStream=mainStream)
        self.pos = offset
        self.size = size

    def dump(self):
        print('<stsh type="STSH" offset="%d" size="%d bytes">' % (self.pos, self.size))
        self.lpstshi = LPStshi(self.bytes, self.mainStream, self.pos)
        self.lpstshi.dump()
        self.pos = self.lpstshi.pos
        for i in range(self.lpstshi.stshi.stshif.cstd):
            print('<rglpstd index="%d" type="LPStd" offset="%d">' % (i, self.pos))
            lpstd = LPStd(self)
            lpstd.dump()
            self.pos = lpstd.pos
            print('</rglpstd>')
        print('</stsh>')


class Rca(BinaryStream):
    """The Rca structure is used to define the coordinates of a rectangular area in the document."""
    def __init__(self, parent):
        BinaryStream.__init__(self, parent.bytes)
        self.parent = parent
        self.pos = parent.pos

    def dump(self):
        print('<rca type="Rca" offset="%s">' % self.pos)
        self.printAndSet("left", self.readuInt32())
        self.printAndSet("top", self.readuInt32())
        self.printAndSet("right", self.readuInt32())
        self.printAndSet("bottom", self.readuInt32())
        print('</rca>')
        self.parent.pos = self.pos


class SPA(BinaryStream):
    """The Spa structure specifies information about the shapes and drawings that the document contains."""
    size = 26  # defined by 2.8.37

    def __init__(self, parent, offset):
        BinaryStream.__init__(self, parent.bytes)
        self.parent = parent
        self.pos = offset

    def dump(self):
        pos = self.pos
        print('<spa type="SPA" offset="%s" size="%d bytes">' % (self.pos, SPA.size))
        self.printAndSet("lid", self.readuInt32())
        Rca(self).dump()
        buf = self.readuInt16()
        self.printAndSet("fHdr", self.getBit(buf, 0))  # 1st bit
        self.printAndSet("bx", (buf & 0x6) >> 1)  # 2..3rd bits
        self.printAndSet("by", (buf & 0x18) >> 3)  # 4..5th bits
        self.printAndSet("wr", (buf & 0x1e0) >> 5)  # 6..9th bits
        self.printAndSet("wrk", (buf & 0x1e00) >> 9)  # 10..13th bits
        self.printAndSet("fRcaSimple", self.getBit(buf, 13))  # 14th bit
        self.printAndSet("fBelowText", self.getBit(buf, 14))  # 15th bit
        self.printAndSet("fAnchorLock", self.getBit(buf, 15))  # 16th bit
        self.printAndSet("cTxbx", self.readuInt32())
        print('</spa>')
        assert pos + SPA.size == self.pos


class SPLS(BinaryStream):
    """The SPLS structure specifies the current state of a range of text with regard to one of the language checking features."""
    size = 2  # defined by 2.9.253

    def __init__(self, name, plcfSpl, offset):
        BinaryStream.__init__(self, plcfSpl.bytes)
        self.name = name
        self.plcfSpl = plcfSpl
        self.pos = offset

    def dump(self):
        splfMap = {
            0x1: "splfPending",
            0x2: "splfMaybeDirty",
            0x3: "splfDirty",
            0x4: "splfEdit",
            0x5: "splfForeign",
            0x7: "splfClean",
            0x8: "splfNoLAD",
            0xA: "splfErrorMin",
            0xB: "splfRepeatWord",
            0xC: "splfUnknownWord",
        }
        buf = self.readuInt16()
        print('<spls type="SPLS" offset="%d" size="%d bytes" value="%s">' % (self.pos, SPLS.size, hex(buf)))
        self.printAndSet("splf", buf & 0x000f, end=False)  # 1..4th bits
        if self.splf in splfMap:
            print('<transformed name="%s"/>' % splfMap[self.splf])
        print('</splf>')
        self.printAndSet("fError", self.getBit(buf, 4))
        self.printAndSet("fExtend", self.getBit(buf, 5))
        self.printAndSet("fTypo", self.getBit(buf, 6))
        self.printAndSet("unused", (buf & 0xff80) >> 7)  # 8..16th bits
        print('</spls>')


class PlcfSpl(BinaryStream, PLC):
    """The Plcfspl structure is a Plc structure whose data elements are SpellingSpls structures."""
    def __init__(self, mainStream):
        BinaryStream.__init__(self, mainStream.getTableStream().bytes, mainStream=mainStream)
        PLC.__init__(self, mainStream.lcbPlcfSpl, 2)  # 2 is defined by 2.8.28
        self.pos = mainStream.fcPlcfSpl
        self.size = mainStream.lcbPlcfSpl

    def dump(self):
        print('<plcfSpl type="PlcfSpl" offset="%d" size="%d bytes">' % (self.pos, self.size))
        pos = self.pos
        for i in range(self.getElements()):
            # aCp
            start = self.getuInt32(pos=pos)
            end = self.getuInt32(pos=pos + 4)
            print('<aCP index="%d" start="%d" end="%d">' % (i, start, end))
            pos += 4

            # aSpellingSpls
            aSpellingSpls = SPLS("SpellingSpls", self, self.getOffset(self.pos, i))
            aSpellingSpls.dump()

            print('<transformed value="%s"/>' % self.quoteAttr(self.mainStream.retrieveCPs(start, end)))
            print('</aCP>')
        print('</plcfSpl>')


class FTXBXNonReusable(BinaryStream):
    """The FTXBXNonReusable structure is used within the FTXBXS structure when that structure
    describes a real textbox. A real textbox is any shape object into which text is added, and that is the
    first or only shape in a linked chain."""
    def __init__(self, parent):
        BinaryStream.__init__(self, parent.bytes)
        self.parent = parent
        self.pos = parent.pos

    def dump(self):
        print('<ftxbxsunion type="FTXBXNonReusable" offset="%d" size="8 bytes">' % (self.pos))
        self.printAndSet("cTxbx", self.readuInt32())
        self.printAndSet("cTxbxEdit", self.readuInt32())
        print('</ftxbxsunion>')
        self.parent.pos = self.pos


class FTXBXSReusable(BinaryStream):
    """The FTXBXSReusable structure is used within the FTXBXS structure when it describes a spare
    structure that can be reused by the application and converted into an actual textbox."""
    def __init__(self, parent):
        BinaryStream.__init__(self, parent.bytes)
        self.parent = parent
        self.pos = parent.pos

    def dump(self):
        print('<ftxbxsunion type="FTXBXReusable" offset="%d" size="8 bytes">' % (self.pos))
        self.printAndSet("iNextReuse", self.readuInt32())
        self.printAndSet("cReusable", self.readuInt32())
        print('</ftxbxsunion>')
        self.parent.pos = self.pos


class FTXBXS(BinaryStream):
    """Associates ranges of text from the Textboxes Document and the Header
    Textboxes Document, with shape objects."""
    size = 22  # 2.8.32

    def __init__(self, parent, offset):
        BinaryStream.__init__(self, parent.bytes)
        self.parent = parent
        self.pos = self.posOrig = offset

    def dump(self):
        print('<aFTXBXS type="FTXBXS" offset="%d" size="%d bytes">' % (self.pos, FTXBXS.size))
        self.fReusable = self.getuInt16(pos=self.pos + 8)
        if self.fReusable:
            FTXBXSReusable(self).dump()
        else:
            FTXBXNonReusable(self).dump()
        self.printAndSet("fReusable", self.readuInt16())
        self.printAndSet("itxbxsDest", self.readuInt32())
        self.printAndSet("lid", self.readuInt32())
        self.printAndSet("txidUndo", self.readuInt32())
        print('</aFTXBXS>')
        if not self.fReusable:
            assert self.posOrig + FTXBXS.size == self.pos


class PlcftxbxTxt(BinaryStream, PLC):
    """Specifies which ranges of text are contained in which textboxes."""
    def __init__(self, mainStream):
        BinaryStream.__init__(self, mainStream.getTableStream().bytes, mainStream=mainStream)
        PLC.__init__(self, mainStream.lcbPlcftxbxTxt, FTXBXS.size)
        self.pos = mainStream.fcPlcftxbxTxt
        self.size = mainStream.lcbPlcftxbxTxt

    def dump(self):
        print('<plcftxbxTxt type="PlcftxbxTxt" offset="%d" size="%d bytes">' % (self.pos, self.size))
        offset = self.mainStream.getHeaderOffset()
        pos = self.pos
        for i in range(self.getElements() - 1):
            # aCp
            start = self.getuInt32(pos=pos)
            end = self.getuInt32(pos=pos + 4)
            print('<aCP index="%d" start="%d" end="%d">' % (i, start, end))
            pos += 4

            # aFTXBXS
            aFTXBXS = FTXBXS(self, self.getOffset(self.pos, i))
            aFTXBXS.dump()

            print('<transformed value="%s"/>' % self.quoteAttr(self.mainStream.retrieveCPs(offset + start, offset + end)))
            print('</aCP>')
        print('</plcftxbxTxt>')


class Tbkd(BinaryStream):
    """The Tbkd structure is used by the PlcftxbxBkd and PlcfTxbxHdrBkd structures to associate ranges of
    text from the Textboxes Document and the Header Textboxes Document with FTXBXS objects."""
    size = 6  # 2.9.309

    def __init__(self, parent, offset):
        BinaryStream.__init__(self, parent.bytes)
        self.parent = parent
        self.pos = self.posOrig = offset

    def dump(self):
        print('<aTbkd type="Tbkd" offset="%d" size="%d bytes">' % (self.pos, Tbkd.size))
        self.printAndSet("itxbxs", self.readuInt16())
        self.printAndSet("dcpDepend", self.readuInt16())
        buf = self.readuInt16()
        self.printAndSet("reserved1", buf & 0x03ff)  # 1..10th bits
        self.printAndSet("fMarkDelete", self.getBit(buf, 10))
        self.printAndSet("fUnk", self.getBit(buf, 11))
        self.printAndSet("fTextOverflow", self.getBit(buf, 12))
        self.printAndSet("reserved2", (buf & 0xe000) >> 13)  # 14..16th bits
        print('</aTbkd>')
        assert self.posOrig + Tbkd.size == self.pos


class PlcftxbxBkd(BinaryStream, PLC):
    """Specifies which ranges of text go inside which textboxes."""
    def __init__(self, mainStream):
        BinaryStream.__init__(self, mainStream.getTableStream().bytes, mainStream=mainStream)
        PLC.__init__(self, mainStream.lcbPlcfTxbxBkd, 6)
        self.pos = mainStream.fcPlcfTxbxBkd
        self.size = mainStream.lcbPlcfTxbxBkd

    def dump(self):
        print('<plcftxbxBkd type="PlcftxbxBkd" offset="%d" size="%d bytes">' % (self.pos, self.size))
        offset = self.mainStream.getHeaderOffset()
        pos = self.pos
        for i in range(self.getElements()):
            # aCp
            start = self.getuInt32(pos=pos)
            end = self.getuInt32(pos=pos + 4)
            print('<aCP index="%d" start="%d" end="%d">' % (i, start, end))
            pos += 4

            # aTbkd
            Tbkd(self, self.getOffset(self.pos, i)).dump()
            print('<transformed value="%s"/>' % self.quoteAttr(self.mainStream.retrieveCPs(offset + start, offset + end)))
            print('</aCP>')
        print('</plcftxbxBkd>')


class PlcfSpa(BinaryStream, PLC):
    """The PlcfSpa structure is a PLC structure in which the data elements are
    SPA structures."""
    def __init__(self, mainStream, pos, size):
        BinaryStream.__init__(self, mainStream.getTableStream().bytes, mainStream=mainStream)
        PLC.__init__(self, size, 26)  # 2.8.37
        self.pos = pos
        self.size = size

    def dump(self):
        print('<plcfSpa type="PlcfSpa" offset="%d" size="%d bytes">' % (self.pos, self.size))
        pos = self.pos
        for i in range(self.getElements()):
            # aCp
            start = self.getuInt32(pos=pos)
            end = self.getuInt32(pos=pos + 4)
            print('<aCP index="%d" start="%d" end="%d">' % (i, start, end))
            pos += 4

            # aSpa
            aSpa = SPA(self, self.getOffset(self.pos, i))
            aSpa.dump()

            print('<transformed value="%s"/>' % self.quoteAttr(self.mainStream.retrieveCPs(start, end)))
            print('</aCP>')
        print('</plcfSpa>')


class PlcfGram(BinaryStream, PLC):
    """The PlcfGram structure is a Plc structure whose data elements are GrammarSpls structures."""
    def __init__(self, mainStream):
        BinaryStream.__init__(self, mainStream.getTableStream().bytes, mainStream=mainStream)
        PLC.__init__(self, mainStream.lcbPlcfGram, 2)  # 2 is defined by 2.8.21
        self.pos = mainStream.fcPlcfGram
        self.size = mainStream.lcbPlcfGram

    def dump(self):
        print('<plcfGram type="PlcfGram" offset="%d" size="%d bytes">' % (self.pos, self.size))
        pos = self.pos
        for i in range(self.getElements()):
            # aCp
            start = self.getuInt32(pos=pos)
            end = self.getuInt32(pos=pos + 4)
            print('<aCP index="%d" start="%d" end="%d">' % (i, start, end))
            pos += 4

            # aGrammarSpls
            aGrammarSpls = SPLS("GrammarSpls", self, self.getOffset(self.pos, i))
            aGrammarSpls.dump()

            print('<transformed value="%s"/>' % self.quoteAttr(self.mainStream.retrieveCPs(start, end)))
            print('</aCP>')
        print('</plcfGram>')


class Grfhic(BinaryStream):
    """The grfhic structure is a set of HTML incompatibility flags that specify
    the HTML incompatibilities of a list structure."""
    def __init__(self, parent):
        BinaryStream.__init__(self, parent.bytes)
        self.pos = parent.pos
        self.parent = parent

    def dump(self):
        print('<grfhic type="grfhic">')
        buf = self.readuInt8()
        self.printAndSet("fhicChecked", self.getBit(buf, 0))
        self.printAndSet("fhicFormat", self.getBit(buf, 1))
        self.printAndSet("fhicListText", self.getBit(buf, 2))
        self.printAndSet("fhicPeriod", self.getBit(buf, 3))
        self.printAndSet("fhicLeft1", self.getBit(buf, 4))
        self.printAndSet("fhicListTab", self.getBit(buf, 5))
        self.printAndSet("unused", self.getBit(buf, 6))
        self.printAndSet("fhicBullet", self.getBit(buf, 7))
        self.parent.pos = self.pos
        print('</grfhic>')


class LSTF(BinaryStream):
    """The LSTF structure contains formatting properties that apply to an entire list."""
    def __init__(self, plfLst, index):
        BinaryStream.__init__(self, plfLst.bytes)
        self.pos = plfLst.pos
        self.size = 28
        self.index = index

    def dump(self):
        print('<lstf type="LSTF" index="%d" offset="%d" size="%d bytes">' % (self.index, self.pos, self.size))
        self.printAndSet("lsid", self.readInt32())
        self.printAndSet("tplc", self.readInt32())
        for i in range(9):
            print('<rgistdPara index="%d" value="%s"/>' % (i, self.readInt16()))
        buf = self.readuInt8()
        self.printAndSet("fSimpleList", self.getBit(buf, 0))
        self.printAndSet("unused1", self.getBit(buf, 1))
        self.printAndSet("fAutoNum", self.getBit(buf, 2))
        self.printAndSet("unused2", self.getBit(buf, 3))
        self.printAndSet("fHybrid", self.getBit(buf, 4))
        self.printAndSet("reserved1", (buf & 0xe0) >> 5)  # 6..8th bits
        Grfhic(self).dump()
        print('</lstf>')


class LVLF(BinaryStream):
    """The LVLF structure contains formatting properties for an individual level in a list."""
    def __init__(self, lvl):
        BinaryStream.__init__(self, lvl.bytes)
        self.pos = lvl.pos

    def dump(self):
        print('<lvlf type="LVLF" offset="%d">' % self.pos)
        self.printAndSet("iStartAt", self.readInt32())
        self.printAndSet("nfc", self.readuInt8())
        buf = self.readuInt8()
        self.printAndSet("jc", buf & 0x3)  # 1..2nd bits
        self.printAndSet("fLegal", self.getBit(buf, 2))
        self.printAndSet("fNoRestart", self.getBit(buf, 3))
        self.printAndSet("fIndentSav", self.getBit(buf, 4))
        self.printAndSet("fConverted", self.getBit(buf, 5))
        self.printAndSet("unused1", self.getBit(buf, 6))
        self.printAndSet("fTentative", self.getBit(buf, 7))
        for i in range(9):
            print('<rgrgbxchNums index="%d" value="%s"/>' % (i, self.readuInt8()))
        self.printAndSet("ixchFollow", self.readuInt8())
        self.printAndSet("dxaIndentSav", self.readInt32())
        self.printAndSet("unused2", self.readuInt32())
        self.printAndSet("cbGrpprlChpx", self.readuInt8())
        self.printAndSet("cbGrpprlPapx", self.readuInt8())
        self.printAndSet("ilvlRestartLim", self.readuInt8())
        Grfhic(self).dump()
        print('</lvlf>')


class LVL(BinaryStream):
    """The LVL structure contains formatting information about a specific level in a list."""
    def __init__(self, plfLst, index):
        BinaryStream.__init__(self, plfLst.bytes)
        self.pos = plfLst.pos
        self.index = index

    def dump(self):
        print('<lvl type="LVL" index="%d" offset="%d">' % (self.index, self.pos))
        lvlf = LVLF(self)
        lvlf.dump()
        self.pos = lvlf.pos

        print('<grpprlPapx offset="%d">' % self.pos)
        pos = self.pos
        while (lvlf.cbGrpprlPapx - (pos - self.pos)) > 0:
            prl = Prl(self, pos)
            prl.dump()
            pos += prl.getSize()
        self.pos = pos
        print('</grpprlPapx>')

        print('<grpprlChpx offset="%d">' % self.pos)
        pos = self.pos
        while (lvlf.cbGrpprlChpx - (pos - self.pos)) > 0:
            prl = Prl(self, pos)
            prl.dump()
            pos += prl.getSize()
        self.pos = pos
        print('</grpprlChpx>')
        xst = Xst(self)
        xst.dump()
        self.pos = xst.pos
        print('</lvl>')


class PlfLst(BinaryStream):
    """The PlfLst structure contains the list formatting information for the document."""
    def __init__(self, mainStream):
        BinaryStream.__init__(self, mainStream.getTableStream().bytes, mainStream=mainStream)
        self.pos = mainStream.fcPlfLst
        self.size = mainStream.lcbPlfLst

    def dump(self):
        print('<plfLst type="PlfLst" offset="%d" size="%d bytes">' % (self.pos, self.size))
        self.printAndSet("cLst", self.readInt16())
        cLvl = 0
        for i in range(self.cLst):
            rgLstf = LSTF(self, i)
            rgLstf.dump()
            if rgLstf.fSimpleList:
                cLvl += 1
            else:
                cLvl += 9
            self.pos = rgLstf.pos
        for i in range(cLvl):
            lvl = LVL(self, i)
            lvl.dump()
            self.pos = lvl.pos
        print('</plfLst>')


class LFO(BinaryStream):
    """The LFO structure specifies the LSTF element that corresponds to a list that contains a paragraph."""
    def __init__(self, plfLfo, name, index):
        BinaryStream.__init__(self, plfLfo.bytes)
        self.pos = plfLfo.pos
        self.name = name
        self.index = index

    def dump(self):
        print('<%s type="LFO" index="%s" offset="%d">' % (self.name, self.index, self.pos))
        self.printAndSet("lsid", self.readInt32())
        self.printAndSet("unused1", self.readuInt32())
        self.printAndSet("unused2", self.readuInt32())
        self.printAndSet("clfolvl", self.readuInt8())
        self.printAndSet("ibstFltAutoNum", self.readuInt8())
        Grfhic(self).dump()
        self.printAndSet("unused3", self.readuInt8())
        print('</%s>' % self.name)


class LFOData(BinaryStream):
    """The LFOData structure contains the Main Document CP of the corresponding LFO."""
    def __init__(self, plfLfo, lfo):
        BinaryStream.__init__(self, plfLfo.bytes)
        self.pos = plfLfo.pos
        self.lfo = lfo

    def dump(self):
        print('<lfoData type="LFOData" offset="%d">' % self.pos)
        self.printAndSet("cp", self.readuInt32())
        if self.lfo.clfolvl > 0:
            print('<todo what="LFOData: clfolvl != 0"/>')
        print('</lfoData>')


class PlfLfo(BinaryStream):
    """The PlfLfo structure contains the list format override data for the document."""
    def __init__(self, mainStream):
        BinaryStream.__init__(self, mainStream.getTableStream().bytes, mainStream=mainStream)
        self.pos = mainStream.fcPlfLfo
        self.size = mainStream.lcbPlfLfo

    def dump(self):
        print('<plfLfo type="PlfLfo" offset="%d" size="%d bytes">' % (self.pos, self.size))
        self.printAndSet("lfoMac", self.readInt32())
        lfos = []
        for i in range(self.lfoMac):
            lfo = LFO(self, "rgLfo", i)
            lfos.append(lfo)
            lfo.dump()
            self.pos = lfo.pos
        for i in range(self.lfoMac):
            lfoData = LFOData(self, lfos[i])
            lfoData.dump()
            self.pos = lfoData.pos
        print('</plfLfo>')


class SttbListNames(BinaryStream):
    """The SttbListNames structure is an STTB structure whose strings are the names used by the LISTNUM field."""
    def __init__(self, mainStream):
        BinaryStream.__init__(self, mainStream.getTableStream().bytes, mainStream=mainStream)
        self.pos = mainStream.fcSttbListNames
        self.size = mainStream.lcbSttbListNames

    def dump(self):
        print('<sttbListNames type="SttbListNames" offset="%d" size="%d bytes">' % (self.pos, self.size))
        self.printAndSet("fExtend", self.readuInt16())
        self.printAndSet("cData", self.readuInt16())
        self.printAndSet("cbExtra", self.readuInt16())
        for i in range(self.cData):
            cchData = self.readuInt16()
            print('<cchData index="%s" offset="%d" size="%d bytes">' % (i, self.pos, cchData))
            print('<string value="%s"/>' % globals.encodeName(self.bytes[self.pos:self.pos + 2 * cchData].decode('utf-16'), lowOnly=True))
            self.pos += 2 * cchData
            print('</cchData>')
        print('</sttbListNames>')


class PBString(BinaryStream):
    """Specified by [MS-OSHARED] 2.3.4.5, specifies a null-terminated string."""
    def __init__(self, parent, name, index=None):
        BinaryStream.__init__(self, parent.bytes)
        self.parent = parent
        self.pos = parent.pos
        self.name = name
        self.index = index

    def dump(self):
        if self.index is None:
            print('<%s type="PBString">' % self.name)
        else:
            print('<%s type="PBString" index="%s">' % (self.name, self.index))
        buf = self.readuInt16()
        self.printAndSet("cch", buf & 0x7fff)  # bits 1..15
        self.printAndSet("fAnsiString", self.getBit(buf, 15))

        bytes = []
        if self.fAnsiString:
            cch = self.cch
        else:
            cch = self.cch * 2
        for dummy in range(cch):
            c = self.readuInt8()
            bytes.append(c)

        if self.fAnsiString == 1:
            encoding = "ascii"
        else:
            encoding = "utf-16"
        self.printAndSet("rgxch", globals.encodeName("".join(map(lambda c: chr(c), bytes)).decode(encoding), lowOnly=True).encode('utf-8'), hexdump=False)

        print('</%s>' % self.name)
        self.parent.pos = self.pos


class FactoidType(BinaryStream):
    """Specified by [MS-OSHARED] 2.3.4.2, specifies the type of smart tag."""
    def __init__(self, parent):
        BinaryStream.__init__(self, parent.bytes)
        self.parent = parent
        self.pos = parent.pos

    def dump(self):
        print('<factoidType>')
        self.printAndSet("cbFactoid", self.readuInt32())
        self.printAndSet("id", self.readuInt32())
        self.rgbUri = PBString(self, "rgbUri")
        self.rgbUri.dump()
        self.rgbTag = PBString(self, "rgbTag")
        self.rgbTag.dump()
        self.rgbDownLoadURL = PBString(self, "rgbDownLoadURL")
        self.rgbDownLoadURL.dump()
        print('</factoidType>')
        self.parent.pos = self.pos


class PropertyBagStore(BinaryStream):
    """Specified by [MS-OSHARED] 2.3.4.1, specifies the shared data for the
    smart tags embedded in the document."""
    def __init__(self, parent):
        BinaryStream.__init__(self, parent.bytes)
        self.parent = parent
        self.pos = parent.pos

    def dump(self):
        print('<propBagStore type="PropertyBagStore" offset="%s">' % self.pos)
        self.printAndSet("cFactoidType", self.readuInt32())
        print('<factoidTypes>')
        self.factoidTypes = []
        for i in range(self.cFactoidType):
            factoidType = FactoidType(self)
            factoidType.dump()
            self.factoidTypes.append(factoidType)
        print('</factoidTypes>')
        self.printAndSet("cbHdr", self.readuInt16())
        assert self.cbHdr == 0xc
        self.printAndSet("sVer", self.readuInt16())
        assert self.sVer == 0x0100
        self.printAndSet("cfactoid", self.readuInt32())
        self.printAndSet("cste", self.readuInt32())
        print('<stringTable>')
        self.stringTable = []
        for i in range(self.cste):
            string = PBString(self, "stringTable", index=i)
            string.dump()
            self.stringTable.append(string)
        print('</stringTable>')
        print('</propBagStore>')
        self.parent.pos = self.pos


class Property(BinaryStream):
    """Specified by [MS-OSHARED] 2.3.4.4, specifies the indexes into the string
    table entries of the stringTable field."""
    def __init__(self, parent, index):
        BinaryStream.__init__(self, parent.bytes)
        self.parent = parent
        self.pos = parent.pos
        self.index = index

    def dump(self):
        print('<property type="Property" offset="%s" index="%s">' % (self.pos, self.index))
        self.printAndSet("keyIndex", self.readuInt32(), hexdump=False)
        self.printAndSet("valueIndex", self.readuInt32(), hexdump=False)
        print('</property>')
        self.parent.pos = self.pos


class PropertyBag(BinaryStream):
    """Specified by [MS-OSHARED] 2.3.4.3, specifies the smart tag data."""
    def __init__(self, parent, index):
        BinaryStream.__init__(self, parent.bytes)
        self.parent = parent
        self.pos = parent.pos
        self.index = index

    def dump(self):
        print('<propBag type="PropertyBag" offset="%s" index="%s">' % (self.pos, self.index))
        self.printAndSet("id", self.readuInt16())
        self.printAndSet("cProp", self.readuInt16())
        self.printAndSet("cbUnknown", self.readuInt16())
        for i in range(self.cProp):
            Property(self, i).dump()
        print('</propBag>')
        self.parent.pos = self.pos


class SmartTagData(BinaryStream):
    """Specified by [MS-DOC] 2.9.251, stores information about all the smart
    tags in the document."""
    def __init__(self, mainStream):
        BinaryStream.__init__(self, mainStream.getTableStream().bytes, mainStream=mainStream)
        self.pos = mainStream.fcFactoidData
        self.size = mainStream.lcbFactoidData

    def dump(self):
        posOrig = self.pos
        print('<smartTagData type="SmartTagData" offset="%d" size="%d bytes">' % (self.pos, self.size))
        self.propBagStore = PropertyBagStore(self)
        self.propBagStore.dump()
        i = 0
        while self.pos < posOrig + self.size:
            self.propBag = PropertyBag(self, i)
            self.propBag.dump()
            i += 1
        print('</smartTagData>')


class SttbSavedBy(BinaryStream):
    """The SttbSavedBy structure is an STTB structure that specifies the save history of this document."""
    def __init__(self, mainStream):
        BinaryStream.__init__(self, mainStream.getTableStream().bytes, mainStream=mainStream)
        self.pos = mainStream.fcSttbSavedBy
        self.size = mainStream.lcbSttbSavedBy

    def dump(self):
        print('<sttbSavedBy type="SttbSavedBy" offset="%d" size="%d">' % (self.pos, self.size))
        self.printAndSet("fExtend", self.readuInt16())
        self.printAndSet("cData", self.readuInt16())
        self.printAndSet("cbExtra", self.readuInt16())
        for i in range(self.cData):
            cchData = self.readuInt16()
            print('<cchData index="%s" offset="%d" size="%d bytes">' % (i, self.pos, cchData))
            print('<string value="%s"/>' % globals.encodeName(self.bytes[self.pos:self.pos + 2 * cchData].decode('utf-16'), lowOnly=True))
            self.pos += 2 * cchData
            print('</cchData>')
        # Probably this was cleared manually.
        if self.cData != 0:
            assert self.pos == self.mainStream.fcSttbSavedBy + self.size
        print('</sttbSavedBy>')


class SttbfBkmk(BinaryStream):
    """The SttbfBkmk structure is an STTB structure whose strings specify the names of bookmarks."""
    def __init__(self, mainStream):
        BinaryStream.__init__(self, mainStream.getTableStream().bytes)
        self.pos = mainStream.fcSttbfBkmk
        self.size = mainStream.lcbSttbfBkmk
        self.mainStream = mainStream

    def dump(self):
        print('<sttbfBkmk type="SttbfBkmk" offset="%d" size="%d bytes">' % (self.pos, self.size))
        self.printAndSet("fExtended", self.readuInt16())
        self.printAndSet("cData", self.readuInt16())
        self.printAndSet("cbExtra", self.readuInt16())
        for i in range(self.cData):
            cchData = self.readuInt16()
            print('<cchData index="%d" offset="%d" size="%d bytes">' % (i, self.pos, cchData))
            print('<string value="%s"/>' % globals.encodeName(self.bytes[self.pos:self.pos + 2 * cchData].decode('utf-16'), lowOnly=True))
            self.pos += 2 * cchData
            print('</cchData>')
        assert self.pos == self.mainStream.fcSttbfBkmk + self.size
        print('</sttbfBkmk>')


# The FTO enumerated type identifies the feature that is responsible to create
# a given smart tag in a document.
FTO = {
    0x0000: "ftoUnknown",
    0x0001: "ftoGrammar",
    0x0002: "ftoScanDll",
    0x0003: "ftoVB"
}


class FACTOIDINFO(BinaryStream):
    """Specified by [MS-DOC] 2.9.66, contains information about a smart tag
    bookmark in the document."""
    def __init__(self, parent, index):
        BinaryStream.__init__(self, parent.bytes)
        self.parent = parent
        self.pos = parent.pos
        self.index = index

    def dump(self):
        print('<factoidinfo index="%s">' % self.index)
        self.printAndSet("dwId", self.readuInt32())
        buf = self.readuInt16()
        self.printAndSet("fSubEntry", self.getBit(buf, 0))
        self.printAndSet("fUnused", (buf & 0xfffe) >> 1)  # 2..16th bits
        self.printAndSet("fto", self.readuInt16(), dict=FTO)
        self.printAndSet("pfpb", self.readuInt32())
        print('</factoidinfo>')
        self.parent.pos = self.pos


class SttbfBkmkFactoid(BinaryStream):
    """Specified by [MS-DOC] 2.9.281, an STTB whose strings are FACTOIDINFO structures."""
    def __init__(self, mainStream):
        BinaryStream.__init__(self, mainStream.getTableStream().bytes)
        self.pos = mainStream.fcSttbfBkmkFactoid
        self.size = mainStream.lcbSttbfBkmkFactoid
        self.mainStream = mainStream

    def dump(self):
        print('<sttbfBkmkFactoid type="SttbfBkmkFactoid" offset="%d" size="%d bytes">' % (self.pos, self.size))
        self.printAndSet("fExtended", self.readuInt16())
        assert self.fExtended == 0xffff
        self.printAndSet("cData", self.readuInt16())
        self.printAndSet("cbExtra", self.readuInt16())
        assert self.cbExtra == 0
        for i in range(self.cData):
            self.printAndSet("cchData", self.readuInt16())
            assert self.cchData == 0x6
            FACTOIDINFO(self, i).dump()
        assert self.pos == self.mainStream.fcSttbfBkmkFactoid + self.size
        print('</sttbfBkmkFactoid>')

# vim:set filetype=python shiftwidth=4 softtabstop=4 expandtab: