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
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359
11360
11361
11362
11363
11364
11365
11366
11367
11368
11369
11370
11371
11372
11373
11374
11375
11376
11377
11378
11379
11380
11381
11382
11383
11384
11385
11386
11387
11388
11389
11390
11391
11392
11393
11394
11395
11396
11397
11398
11399
11400
11401
11402
11403
11404
11405
11406
11407
11408
11409
11410
11411
11412
11413
11414
11415
11416
11417
11418
11419
11420
11421
11422
11423
11424
11425
11426
11427
11428
11429
11430
11431
11432
11433
11434
11435
11436
11437
11438
11439
11440
11441
11442
11443
11444
11445
11446
11447
11448
11449
11450
11451
11452
11453
11454
11455
11456
11457
11458
11459
11460
11461
11462
11463
11464
11465
11466
11467
11468
11469
11470
11471
11472
11473
11474
11475
11476
11477
11478
11479
11480
11481
11482
11483
11484
11485
11486
11487
11488
11489
11490
11491
11492
11493
11494
11495
11496
11497
11498
11499
11500
11501
11502
11503
11504
11505
11506
11507
11508
11509
11510
11511
11512
11513
11514
11515
11516
11517
11518
11519
11520
11521
11522
11523
11524
11525
11526
11527
11528
11529
11530
11531
11532
11533
11534
11535
11536
11537
11538
11539
11540
11541
11542
11543
11544
11545
11546
11547
11548
11549
11550
11551
11552
11553
11554
11555
11556
11557
11558
11559
11560
11561
11562
11563
11564
11565
11566
11567
11568
11569
11570
11571
11572
11573
11574
11575
11576
11577
11578
11579
11580
11581
11582
11583
11584
11585
11586
11587
11588
11589
11590
11591
11592
11593
11594
11595
11596
11597
11598
11599
11600
11601
11602
11603
11604
11605
11606
11607
11608
11609
11610
11611
11612
11613
11614
11615
11616
11617
11618
11619
11620
11621
11622
11623
11624
11625
11626
11627
11628
11629
11630
11631
11632
11633
11634
11635
11636
11637
11638
11639
11640
11641
11642
11643
11644
11645
11646
11647
11648
11649
11650
11651
11652
11653
11654
11655
11656
11657
11658
11659
11660
11661
11662
11663
11664
11665
11666
11667
11668
11669
11670
11671
11672
11673
11674
11675
11676
11677
11678
11679
11680
11681
11682
11683
11684
11685
11686
11687
11688
11689
11690
11691
11692
11693
11694
11695
11696
11697
11698
11699
11700
11701
11702
11703
11704
11705
11706
11707
11708
11709
11710
11711
11712
11713
11714
11715
11716
11717
11718
11719
11720
11721
11722
11723
11724
11725
11726
11727
11728
11729
11730
11731
11732
11733
11734
11735
11736
11737
11738
11739
11740
11741
11742
11743
11744
11745
11746
11747
11748
11749
11750
11751
11752
11753
11754
11755
11756
11757
11758
11759
11760
11761
11762
11763
11764
11765
11766
11767
11768
11769
11770
11771
11772
11773
11774
11775
11776
11777
11778
11779
11780
11781
11782
11783
11784
11785
11786
11787
11788
11789
11790
11791
11792
11793
11794
11795
11796
11797
11798
11799
11800
11801
11802
11803
11804
11805
11806
11807
11808
11809
11810
11811
11812
11813
11814
11815
11816
11817
11818
11819
11820
11821
11822
11823
11824
11825
11826
11827
11828
11829
11830
11831
11832
11833
11834
11835
11836
11837
11838
11839
11840
11841
11842
11843
11844
11845
11846
11847
11848
11849
11850
11851
11852
11853
11854
11855
11856
11857
11858
11859
11860
11861
11862
11863
11864
11865
11866
11867
11868
11869
11870
11871
11872
11873
11874
11875
11876
11877
11878
11879
11880
11881
11882
11883
11884
11885
11886
11887
11888
11889
11890
11891
11892
11893
11894
11895
11896
11897
11898
11899
11900
11901
11902
11903
11904
11905
11906
11907
11908
11909
11910
11911
11912
11913
11914
11915
11916
11917
11918
11919
11920
11921
11922
11923
11924
11925
11926
11927
11928
11929
11930
11931
11932
11933
11934
11935
11936
11937
11938
11939
11940
11941
11942
11943
11944
11945
11946
11947
11948
11949
11950
11951
11952
11953
11954
11955
11956
11957
11958
11959
11960
11961
11962
11963
11964
11965
11966
11967
11968
11969
11970
11971
11972
11973
11974
11975
11976
11977
11978
11979
11980
11981
11982
11983
11984
11985
11986
11987
11988
11989
11990
11991
11992
11993
11994
11995
11996
11997
11998
11999
12000
12001
12002
12003
12004
12005
12006
12007
12008
12009
12010
12011
12012
12013
12014
12015
12016
12017
12018
12019
12020
12021
12022
12023
12024
12025
12026
12027
12028
12029
12030
12031
12032
12033
12034
12035
12036
12037
12038
12039
12040
12041
12042
12043
12044
12045
12046
12047
12048
12049
12050
12051
12052
12053
12054
12055
12056
12057
12058
12059
12060
12061
12062
12063
12064
12065
12066
12067
12068
12069
12070
12071
12072
12073
12074
12075
12076
12077
12078
12079
12080
12081
12082
12083
12084
12085
12086
12087
12088
12089
12090
12091
12092
12093
12094
12095
12096
12097
12098
12099
12100
12101
12102
12103
12104
12105
12106
12107
12108
12109
12110
12111
12112
12113
12114
12115
12116
12117
12118
12119
12120
12121
12122
12123
12124
12125
12126
12127
12128
12129
12130
12131
12132
12133
12134
12135
12136
12137
12138
12139
12140
12141
12142
12143
12144
12145
12146
12147
12148
12149
12150
12151
12152
12153
12154
12155
12156
12157
12158
12159
12160
12161
12162
12163
12164
12165
12166
12167
12168
12169
12170
12171
12172
12173
12174
12175
12176
12177
12178
12179
12180
12181
12182
12183
12184
12185
12186
12187
12188
12189
12190
12191
12192
12193
12194
12195
12196
12197
12198
12199
12200
12201
12202
12203
12204
12205
12206
12207
12208
12209
12210
12211
12212
12213
12214
12215
12216
12217
12218
12219
12220
12221
12222
12223
12224
12225
12226
12227
12228
12229
12230
12231
12232
12233
12234
12235
12236
12237
12238
12239
12240
12241
12242
12243
12244
12245
12246
12247
12248
12249
12250
12251
12252
12253
12254
12255
12256
12257
12258
12259
12260
12261
12262
12263
12264
12265
12266
12267
12268
12269
12270
12271
12272
12273
12274
12275
12276
12277
12278
12279
12280
12281
12282
12283
12284
12285
12286
12287
12288
12289
12290
12291
12292
12293
12294
12295
12296
12297
12298
12299
12300
12301
12302
12303
12304
12305
12306
12307
12308
12309
12310
12311
12312
12313
12314
12315
12316
12317
12318
12319
12320
12321
12322
12323
12324
12325
12326
12327
12328
12329
12330
12331
12332
12333
12334
12335
12336
12337
12338
12339
12340
12341
12342
12343
12344
12345
12346
12347
12348
12349
12350
12351
12352
12353
12354
12355
12356
12357
12358
12359
12360
12361
12362
12363
12364
12365
12366
12367
12368
12369
12370
12371
12372
12373
12374
12375
12376
12377
12378
12379
12380
12381
12382
12383
12384
12385
12386
12387
12388
12389
12390
12391
12392
12393
12394
12395
12396
12397
12398
12399
12400
12401
12402
12403
12404
12405
12406
12407
12408
12409
12410
12411
12412
12413
12414
12415
12416
12417
12418
12419
12420
12421
12422
12423
12424
12425
12426
12427
12428
12429
12430
12431
12432
12433
12434
12435
12436
12437
12438
12439
12440
12441
12442
12443
12444
12445
12446
12447
12448
12449
12450
12451
12452
12453
12454
12455
12456
12457
12458
12459
12460
12461
12462
12463
12464
12465
12466
12467
12468
12469
12470
12471
12472
12473
12474
12475
12476
12477
12478
12479
12480
12481
12482
12483
12484
12485
12486
12487
12488
12489
12490
12491
12492
12493
12494
12495
12496
12497
12498
12499
12500
12501
12502
12503
12504
12505
12506
12507
12508
12509
12510
12511
12512
12513
12514
12515
12516
12517
12518
12519
12520
12521
12522
12523
12524
12525
12526
12527
12528
12529
12530
12531
12532
12533
12534
12535
12536
12537
12538
12539
12540
12541
12542
12543
12544
12545
12546
12547
12548
12549
12550
12551
12552
12553
12554
12555
12556
12557
12558
12559
12560
12561
12562
12563
12564
12565
12566
12567
12568
12569
12570
12571
12572
12573
12574
12575
12576
12577
12578
12579
12580
12581
12582
12583
12584
12585
12586
12587
12588
12589
12590
12591
12592
12593
12594
12595
12596
12597
12598
12599
12600
12601
12602
12603
12604
12605
12606
12607
12608
12609
12610
12611
12612
12613
12614
12615
12616
12617
12618
12619
12620
12621
12622
12623
12624
12625
12626
12627
12628
12629
12630
12631
12632
12633
12634
12635
12636
12637
12638
12639
12640
12641
12642
12643
12644
12645
12646
12647
12648
12649
12650
12651
12652
12653
12654
12655
12656
12657
12658
12659
12660
12661
12662
12663
12664
12665
12666
12667
12668
12669
12670
12671
12672
12673
12674
12675
12676
12677
12678
12679
12680
12681
12682
12683
12684
12685
12686
12687
12688
12689
12690
12691
12692
12693
12694
12695
12696
12697
12698
12699
12700
12701
12702
12703
12704
12705
12706
12707
12708
12709
12710
12711
12712
12713
12714
12715
12716
12717
12718
12719
12720
12721
12722
12723
12724
12725
12726
12727
12728
12729
12730
12731
12732
12733
12734
12735
12736
12737
12738
12739
12740
12741
12742
12743
12744
12745
12746
12747
12748
12749
12750
12751
12752
12753
12754
12755
12756
12757
12758
12759
12760
12761
12762
12763
12764
12765
12766
12767
12768
12769
12770
12771
12772
12773
12774
12775
12776
12777
12778
12779
12780
12781
12782
12783
12784
12785
12786
12787
12788
12789
12790
12791
12792
12793
12794
12795
12796
12797
12798
12799
12800
12801
12802
12803
12804
12805
12806
12807
12808
12809
12810
12811
12812
12813
12814
12815
12816
12817
12818
12819
12820
12821
12822
12823
12824
12825
12826
12827
12828
12829
12830
12831
12832
12833
12834
12835
12836
12837
12838
12839
12840
12841
12842
12843
12844
12845
12846
12847
12848
12849
12850
12851
12852
12853
12854
12855
12856
12857
12858
12859
12860
12861
12862
12863
12864
12865
12866
12867
12868
12869
12870
12871
12872
12873
12874
12875
12876
12877
12878
12879
12880
12881
12882
12883
12884
12885
12886
12887
12888
12889
12890
12891
12892
12893
12894
12895
12896
12897
12898
12899
12900
12901
12902
12903
12904
12905
12906
12907
12908
12909
12910
12911
12912
12913
12914
12915
12916
12917
12918
12919
12920
12921
12922
12923
12924
12925
12926
12927
12928
12929
12930
12931
12932
12933
12934
12935
12936
12937
12938
12939
12940
12941
12942
12943
12944
12945
12946
12947
12948
12949
12950
12951
12952
12953
12954
12955
12956
12957
12958
12959
12960
12961
12962
12963
12964
12965
12966
12967
12968
12969
12970
12971
12972
12973
12974
12975
12976
12977
12978
12979
12980
12981
12982
12983
12984
12985
12986
12987
12988
12989
12990
12991
12992
12993
12994
12995
12996
12997
12998
12999
13000
13001
13002
13003
13004
13005
13006
13007
13008
13009
13010
13011
13012
13013
13014
13015
13016
13017
13018
13019
13020
13021
13022
13023
13024
13025
13026
13027
13028
13029
13030
13031
13032
13033
13034
13035
13036
13037
13038
13039
13040
13041
13042
13043
13044
13045
13046
13047
13048
13049
13050
13051
13052
13053
13054
13055
13056
13057
13058
13059
13060
13061
13062
13063
13064
13065
13066
13067
13068
13069
13070
13071
13072
13073
13074
13075
13076
13077
13078
13079
13080
13081
13082
13083
13084
13085
13086
13087
13088
13089
13090
13091
13092
13093
13094
13095
13096
13097
13098
13099
13100
13101
13102
13103
13104
13105
13106
13107
13108
13109
13110
13111
13112
13113
13114
13115
13116
13117
13118
13119
13120
13121
13122
13123
13124
13125
13126
13127
13128
13129
13130
13131
13132
13133
13134
13135
13136
13137
13138
13139
13140
13141
13142
13143
13144
13145
13146
13147
13148
13149
13150
13151
13152
13153
13154
13155
13156
13157
13158
13159
13160
13161
13162
13163
13164
13165
13166
13167
13168
13169
13170
13171
13172
13173
13174
13175
13176
13177
13178
13179
13180
13181
13182
13183
13184
13185
13186
13187
13188
13189
13190
13191
13192
13193
13194
13195
13196
13197
13198
13199
13200
13201
13202
13203
13204
13205
13206
13207
13208
13209
13210
13211
13212
13213
13214
13215
13216
13217
13218
13219
13220
13221
13222
13223
13224
13225
13226
13227
13228
13229
13230
13231
13232
13233
13234
13235
13236
13237
13238
13239
13240
13241
13242
13243
13244
13245
13246
13247
13248
13249
13250
13251
13252
13253
13254
13255
13256
13257
13258
13259
13260
13261
13262
13263
13264
13265
13266
13267
13268
13269
13270
13271
13272
13273
13274
13275
13276
13277
13278
13279
13280
13281
13282
13283
13284
13285
13286
13287
13288
13289
13290
13291
13292
13293
13294
13295
13296
13297
13298
13299
13300
13301
13302
13303
13304
13305
13306
13307
13308
13309
13310
13311
13312
13313
13314
13315
13316
13317
13318
13319
13320
13321
13322
13323
13324
13325
13326
13327
13328
13329
13330
13331
13332
13333
13334
13335
13336
13337
13338
13339
13340
13341
13342
13343
13344
13345
13346
13347
13348
13349
13350
13351
13352
13353
13354
13355
13356
13357
13358
13359
13360
13361
13362
13363
13364
13365
13366
13367
13368
13369
13370
13371
13372
13373
13374
13375
13376
13377
13378
13379
13380
13381
13382
13383
13384
13385
13386
13387
13388
13389
13390
13391
13392
13393
13394
13395
13396
13397
13398
13399
13400
13401
13402
13403
13404
13405
13406
13407
13408
13409
13410
13411
13412
13413
13414
13415
13416
13417
13418
13419
13420
13421
13422
13423
13424
13425
13426
13427
13428
13429
13430
13431
13432
13433
13434
13435
13436
13437
13438
13439
13440
13441
13442
13443
13444
13445
13446
13447
13448
13449
13450
13451
13452
13453
13454
13455
13456
13457
13458
13459
13460
13461
13462
13463
13464
13465
13466
13467
13468
13469
13470
13471
13472
13473
13474
13475
13476
13477
13478
13479
13480
13481
13482
13483
13484
13485
13486
13487
13488
13489
13490
13491
13492
13493
13494
13495
13496
13497
13498
13499
13500
13501
13502
13503
13504
13505
13506
13507
13508
13509
13510
13511
13512
13513
13514
13515
13516
13517
13518
13519
13520
13521
13522
13523
13524
13525
13526
13527
13528
13529
13530
13531
13532
13533
13534
13535
13536
13537
13538
13539
13540
13541
13542
13543
13544
13545
13546
13547
13548
13549
13550
13551
13552
13553
13554
13555
13556
13557
13558
13559
13560
13561
13562
13563
13564
13565
13566
13567
13568
13569
13570
13571
13572
13573
13574
13575
13576
13577
13578
13579
13580
13581
13582
13583
13584
13585
13586
13587
13588
13589
13590
13591
13592
13593
13594
13595
13596
13597
13598
13599
13600
13601
13602
13603
13604
13605
13606
13607
13608
13609
13610
13611
13612
13613
13614
13615
13616
13617
13618
13619
13620
13621
13622
13623
13624
13625
13626
13627
13628
13629
13630
13631
13632
13633
13634
13635
13636
13637
13638
13639
13640
13641
13642
13643
13644
13645
13646
13647
13648
13649
13650
13651
13652
13653
13654
13655
13656
13657
13658
13659
13660
13661
13662
13663
13664
13665
13666
13667
13668
13669
13670
13671
13672
13673
13674
13675
13676
13677
13678
13679
13680
13681
13682
13683
13684
13685
13686
13687
13688
|
/* proto.c
* Routines for protocol tree
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <gerald@wireshark.org>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "config.h"
#define WS_LOG_DOMAIN LOG_DOMAIN_EPAN
#include <stdio.h>
#include <string.h>
#include <glib.h>
#include <float.h>
#include <inttypes.h>
#include <errno.h>
#include <wsutil/bits_ctz.h>
#include <wsutil/bits_count_ones.h>
#include <wsutil/sign_ext.h>
#include <wsutil/utf8_entities.h>
#include <wsutil/json_dumper.h>
#include <wsutil/wslog.h>
#include <wsutil/ws_assert.h>
#include <wsutil/unicode-utils.h>
#include <ftypes/ftypes.h>
#include "packet.h"
#include "exceptions.h"
#include "ptvcursor.h"
#include "strutil.h"
#include "addr_resolv.h"
#include "address_types.h"
#include "oids.h"
#include "proto.h"
#include "epan_dissect.h"
#include "tvbuff.h"
#include <epan/wmem_scopes.h>
#include "charsets.h"
#include "column-info.h"
#include "to_str.h"
#include "osi-utils.h"
#include "expert.h"
#include "show_exception.h"
#include "in_cksum.h"
#include "register-int.h"
#include <wsutil/crash_info.h>
#include <wsutil/epochs.h>
/* Ptvcursor limits */
#define SUBTREE_ONCE_ALLOCATION_NUMBER 8
#define SUBTREE_MAX_LEVELS 256
typedef struct __subtree_lvl {
gint cursor_offset;
proto_item *it;
proto_tree *tree;
} subtree_lvl;
struct ptvcursor {
wmem_allocator_t *scope;
subtree_lvl *pushed_tree;
guint8 pushed_tree_index;
guint8 pushed_tree_max;
proto_tree *tree;
tvbuff_t *tvb;
gint offset;
};
#define cVALS(x) (const value_string*)(x)
/** See inlined comments.
@param tree the tree to append this item to
@param free_block a code block to call to free resources if this returns
@return NULL if 'tree' is null */
#define CHECK_FOR_NULL_TREE_AND_FREE(tree, free_block) \
if (!tree) { \
free_block; \
return NULL; \
}
/** See inlined comments.
@param tree the tree to append this item to
@param free_block a code block to call to free resources if this returns
@return NULL if 'tree' is null */
#define CHECK_FOR_NULL_TREE(tree) \
CHECK_FOR_NULL_TREE_AND_FREE(tree, ((void)0))
/** See inlined comments.
@param length the length of this item
@param cleanup_block a code block to call to free resources if this returns
@return NULL if 'length' is lower -1 or equal 0 */
#define CHECK_FOR_ZERO_OR_MINUS_LENGTH_AND_CLEANUP(length, cleanup_block) \
if (length < -1 || length == 0 ) { \
cleanup_block; \
return NULL; \
}
/** See inlined comments.
@param length the length of this item
@return NULL if 'length' is lower -1 or equal 0 */
#define CHECK_FOR_ZERO_OR_MINUS_LENGTH(length) \
CHECK_FOR_ZERO_OR_MINUS_LENGTH_AND_CLEANUP(length, ((void)0))
/** See inlined comments.
@param tree the tree to append this item to
@param hfindex field index
@param hfinfo header_field
@param free_block a code block to call to free resources if this returns
@return the header field matching 'hfinfo' */
#define TRY_TO_FAKE_THIS_ITEM_OR_FREE(tree, hfindex, hfinfo, free_block) \
/* If this item is not referenced we don't have to do much work \
at all but we should still return a node so that field items \
below this node (think proto_item_add_subtree()) will still \
have somewhere to attach to or else filtering will not work \
(they would be ignored since tree would be NULL). \
DON'T try to fake a node where PTREE_FINFO(tree) is NULL \
since dissectors that want to do proto_item_set_len() or \
other operations that dereference this would crash. \
We fake FT_PROTOCOL unless some clients have requested us \
not to do so. \
*/ \
PTREE_DATA(tree)->count++; \
PROTO_REGISTRAR_GET_NTH(hfindex, hfinfo); \
if (PTREE_DATA(tree)->count > prefs.gui_max_tree_items) { \
free_block; \
if (wireshark_abort_on_too_many_items) \
ws_error("Adding %s would put more than %d items in the tree -- possible infinite loop (max number of items can be increased in advanced preferences)", \
hfinfo->abbrev, prefs.gui_max_tree_items); \
/* Let the exception handler add items to the tree */ \
PTREE_DATA(tree)->count = 0; \
THROW_MESSAGE(DissectorError, \
wmem_strdup_printf(PNODE_POOL(tree), \
"Adding %s would put more than %d items in the tree -- possible infinite loop (max number of items can be increased in advanced preferences)", \
hfinfo->abbrev, prefs.gui_max_tree_items)); \
} \
if (!(PTREE_DATA(tree)->visible)) { \
if (PTREE_FINFO(tree)) { \
if ((hfinfo->ref_type != HF_REF_TYPE_DIRECT) \
&& (hfinfo->type != FT_PROTOCOL || \
PTREE_DATA(tree)->fake_protocols)) { \
free_block; \
/* just return tree back to the caller */\
return tree; \
} \
} \
}
/** See inlined comments.
@param tree the tree to append this item to
@param hfindex field index
@param hfinfo header_field
@return the header field matching 'hfinfo' */
#define TRY_TO_FAKE_THIS_ITEM(tree, hfindex, hfinfo) \
TRY_TO_FAKE_THIS_ITEM_OR_FREE(tree, hfindex, hfinfo, ((void)0))
/** See inlined comments.
@param pi the created protocol item we're about to return */
#define TRY_TO_FAKE_THIS_REPR(pi) \
ws_assert(pi); \
if (!(PTREE_DATA(pi)->visible)) { \
/* If the tree (GUI) isn't visible it's pointless for us to generate the protocol \
* items string representation */ \
return pi; \
}
/* Same as above but returning void */
#define TRY_TO_FAKE_THIS_REPR_VOID(pi) \
if (!pi) \
return; \
if (!(PTREE_DATA(pi)->visible)) { \
/* If the tree (GUI) isn't visible it's pointless for us to generate the protocol \
* items string representation */ \
return; \
}
/* Similar to above, but allows a NULL tree */
#define TRY_TO_FAKE_THIS_REPR_NESTED(pi) \
if ((pi == NULL) || (!(PTREE_DATA(pi)->visible))) { \
/* If the tree (GUI) isn't visible it's pointless for us to generate the protocol \
* items string representation */ \
return pi; \
}
#ifdef ENABLE_CHECK_FILTER
#define CHECK_HF_VALUE(type, spec, start_values) \
{ \
const type *current; \
int n, m; \
current = start_values; \
for (n=0; current; n++, current++) { \
/* Drop out if we reached the end. */ \
if ((current->value == 0) && (current->strptr == NULL)) { \
break; \
} \
/* Check value against all previous */ \
for (m=0; m < n; m++) { \
/* There are lots of duplicates with the same string, \
so only report if different... */ \
if ((start_values[m].value == current->value) && \
(strcmp(start_values[m].strptr, current->strptr) != 0)) { \
ws_warning("Field '%s' (%s) has a conflicting entry in its" \
" value_string: %" spec " is at indices %u (%s) and %u (%s)", \
hfinfo->name, hfinfo->abbrev, \
current->value, m, start_values[m].strptr, n, current->strptr); \
} \
} \
} \
}
#endif
static const char *hf_try_val_to_str(guint32 value, const header_field_info *hfinfo);
static const char *hf_try_val64_to_str(guint64 value, const header_field_info *hfinfo);
static const char *hf_try_val_to_str_const(guint32 value, const header_field_info *hfinfo, const char *unknown_str);
static const char *hf_try_val64_to_str_const(guint64 value, const header_field_info *hfinfo, const char *unknown_str);
static int hfinfo_bitoffset(const header_field_info *hfinfo);
static int hfinfo_mask_bitwidth(const header_field_info *hfinfo);
static int hfinfo_container_bitwidth(const header_field_info *hfinfo);
#define label_concat(dst, pos, src) \
ws_label_strcpy(dst, ITEM_LABEL_LENGTH, pos, src, 0)
static void mark_truncated(char *label_str, gsize name_pos, const size_t size);
static void label_mark_truncated(char *label_str, gsize name_pos);
#define LABEL_MARK_TRUNCATED_START(label_str) label_mark_truncated(label_str, 0)
static void fill_label_boolean(field_info *fi, gchar *label_str);
static void fill_label_bitfield_char(field_info *fi, gchar *label_str);
static void fill_label_bitfield(field_info *fi, gchar *label_str, gboolean is_signed);
static void fill_label_bitfield64(field_info *fi, gchar *label_str, gboolean is_signed);
static void fill_label_char(field_info *fi, gchar *label_str);
static void fill_label_number(field_info *fi, gchar *label_str, gboolean is_signed);
static void fill_label_number64(field_info *fi, gchar *label_str, gboolean is_signed);
static size_t fill_display_label_float(field_info *fi, gchar *label_str);
static void fill_label_float(field_info *fi, gchar *label_str);
static const char *hfinfo_number_value_format_display(const header_field_info *hfinfo, int display, char buf[32], guint32 value);
static const char *hfinfo_number_value_format_display64(const header_field_info *hfinfo, int display, char buf[48], guint64 value);
static const char *hfinfo_char_vals_format(const header_field_info *hfinfo, char buf[32], guint32 value);
static const char *hfinfo_number_vals_format(const header_field_info *hfinfo, char buf[32], guint32 value);
static const char *hfinfo_number_vals_format64(const header_field_info *hfinfo, char buf[48], guint64 value);
static const char *hfinfo_number_value_format(const header_field_info *hfinfo, char buf[32], guint32 value);
static const char *hfinfo_number_value_format64(const header_field_info *hfinfo, char buf[48], guint64 value);
static const char *hfinfo_char_value_format(const header_field_info *hfinfo, char buf[32], guint32 value);
static const char *hfinfo_numeric_value_format(const header_field_info *hfinfo, char buf[32], guint32 value);
static const char *hfinfo_numeric_value_format64(const header_field_info *hfinfo, char buf[48], guint64 value);
static void proto_cleanup_base(void);
static proto_item *
proto_tree_add_node(proto_tree *tree, field_info *fi);
static void
get_hfi_length(header_field_info *hfinfo, tvbuff_t *tvb, const gint start, gint *length,
gint *item_length, const guint encoding);
static gint
get_full_length(header_field_info *hfinfo, tvbuff_t *tvb, const gint start,
gint length, guint item_length, const gint encoding);
static field_info *
new_field_info(proto_tree *tree, header_field_info *hfinfo, tvbuff_t *tvb,
const gint start, const gint item_length);
static proto_item *
proto_tree_add_pi(proto_tree *tree, header_field_info *hfinfo, tvbuff_t *tvb,
gint start, gint *length);
static void
proto_tree_set_representation_value(proto_item *pi, const char *format, va_list ap);
static void
proto_tree_set_representation(proto_item *pi, const char *format, va_list ap);
static void
proto_tree_set_protocol_tvb(field_info *fi, tvbuff_t *tvb, const char* field_data, int length);
static void
proto_tree_set_bytes(field_info *fi, const guint8* start_ptr, gint length);
static void
proto_tree_set_bytes_tvb(field_info *fi, tvbuff_t *tvb, gint offset, gint length);
static void
proto_tree_set_bytes_gbytearray(field_info *fi, const GByteArray *value);
static void
proto_tree_set_time(field_info *fi, const nstime_t *value_ptr);
static void
proto_tree_set_string(field_info *fi, const char* value);
static void
proto_tree_set_ax25(field_info *fi, const guint8* value);
static void
proto_tree_set_ax25_tvb(field_info *fi, tvbuff_t *tvb, gint start);
static void
proto_tree_set_vines(field_info *fi, const guint8* value);
static void
proto_tree_set_vines_tvb(field_info *fi, tvbuff_t *tvb, gint start);
static void
proto_tree_set_ether(field_info *fi, const guint8* value);
static void
proto_tree_set_ether_tvb(field_info *fi, tvbuff_t *tvb, gint start);
static void
proto_tree_set_ipxnet(field_info *fi, guint32 value);
static void
proto_tree_set_ipv4(field_info *fi, ws_in4_addr value);
static void
proto_tree_set_ipv6(field_info *fi, const ws_in6_addr* value);
static void
proto_tree_set_ipv6_tvb(field_info *fi, tvbuff_t *tvb, gint start, gint length);
static void
proto_tree_set_fcwwn_tvb(field_info *fi, tvbuff_t *tvb, gint start, gint length);
static void
proto_tree_set_guid(field_info *fi, const e_guid_t *value_ptr);
static void
proto_tree_set_guid_tvb(field_info *fi, tvbuff_t *tvb, gint start, const guint encoding);
static void
proto_tree_set_oid(field_info *fi, const guint8* value_ptr, gint length);
static void
proto_tree_set_oid_tvb(field_info *fi, tvbuff_t *tvb, gint start, gint length);
static void
proto_tree_set_system_id(field_info *fi, const guint8* value_ptr, gint length);
static void
proto_tree_set_system_id_tvb(field_info *fi, tvbuff_t *tvb, gint start, gint length);
static void
proto_tree_set_boolean(field_info *fi, guint64 value);
static void
proto_tree_set_float(field_info *fi, float value);
static void
proto_tree_set_double(field_info *fi, double value);
static void
proto_tree_set_uint(field_info *fi, guint32 value);
static void
proto_tree_set_int(field_info *fi, gint32 value);
static void
proto_tree_set_uint64(field_info *fi, guint64 value);
static void
proto_tree_set_int64(field_info *fi, gint64 value);
static void
proto_tree_set_eui64(field_info *fi, const guint64 value);
static void
proto_tree_set_eui64_tvb(field_info *fi, tvbuff_t *tvb, gint start, const guint encoding);
/* Handle type length mismatch (now filterable) expert info */
static int proto_type_length_mismatch = -1;
static expert_field ei_type_length_mismatch_error = EI_INIT;
static expert_field ei_type_length_mismatch_warn = EI_INIT;
static void register_type_length_mismatch(void);
/* Handle byte array string decoding errors with expert info */
static int proto_byte_array_string_decoding_error = -1;
static expert_field ei_byte_array_string_decoding_failed_error = EI_INIT;
static void register_byte_array_string_decodinws_error(void);
/* Handle date and time string decoding errors with expert info */
static int proto_date_time_string_decoding_error = -1;
static expert_field ei_date_time_string_decoding_failed_error = EI_INIT;
static void register_date_time_string_decodinws_error(void);
/* Handle string errors expert info */
static int proto_string_errors = -1;
static expert_field ei_string_trailing_characters = EI_INIT;
static void register_string_errors(void);
static int proto_register_field_init(header_field_info *hfinfo, const int parent);
/* special-case header field used within proto.c */
static header_field_info hfi_text_only =
{ "Text item", "text", FT_NONE, BASE_NONE, NULL, 0x0, NULL, HFILL };
int hf_text_only = -1;
/* Structure for information about a protocol */
struct _protocol {
const char *name; /* long description */
const char *short_name; /* short description */
const char *filter_name; /* name of this protocol in filters */
GPtrArray *fields; /* fields for this protocol */
int proto_id; /* field ID for this protocol */
gboolean is_enabled; /* TRUE if protocol is enabled */
gboolean enabled_by_default; /* TRUE if protocol is enabled by default */
gboolean can_toggle; /* TRUE if is_enabled can be changed */
int parent_proto_id; /* Used to identify "pino"s (Protocol In Name Only).
For dissectors that need a protocol name so they
can be added to a dissector table, but use the
parent_proto_id for things like enable/disable */
GList *heur_list; /* Heuristic dissectors associated with this protocol */
};
/* List of all protocols */
static GList *protocols = NULL;
/* Structure stored for deregistered g_slice */
struct g_slice_data {
gsize block_size;
gpointer mem_block;
};
/* Deregistered fields */
static GPtrArray *deregistered_fields = NULL;
static GPtrArray *deregistered_data = NULL;
static GPtrArray *deregistered_slice = NULL;
/* indexed by prefix, contains initializers */
static GHashTable* prefixes = NULL;
/* Contains information about a field when a dissector calls
* proto_tree_add_item. */
#define FIELD_INFO_NEW(pool, fi) fi = wmem_new(pool, field_info)
#define FIELD_INFO_FREE(pool, fi) wmem_free(pool, fi)
/* Contains the space for proto_nodes. */
#define PROTO_NODE_INIT(node) \
node->first_child = NULL; \
node->last_child = NULL; \
node->next = NULL;
#define PROTO_NODE_FREE(pool, node) \
wmem_free(pool, node)
/* String space for protocol and field items for the GUI */
#define ITEM_LABEL_NEW(pool, il) \
il = wmem_new(pool, item_label_t);
#define ITEM_LABEL_FREE(pool, il) \
wmem_free(pool, il);
#define PROTO_REGISTRAR_GET_NTH(hfindex, hfinfo) \
if((guint)hfindex >= gpa_hfinfo.len && wireshark_abort_on_dissector_bug) \
ws_error("Unregistered hf! index=%d", hfindex); \
DISSECTOR_ASSERT_HINT((guint)hfindex < gpa_hfinfo.len, "Unregistered hf!"); \
DISSECTOR_ASSERT_HINT(gpa_hfinfo.hfi[hfindex] != NULL, "Unregistered hf!"); \
hfinfo = gpa_hfinfo.hfi[hfindex];
/* List which stores protocols and fields that have been registered */
typedef struct _gpa_hfinfo_t {
guint32 len;
guint32 allocated_len;
header_field_info **hfi;
} gpa_hfinfo_t;
static gpa_hfinfo_t gpa_hfinfo;
/* Hash table of abbreviations and IDs */
static GHashTable *gpa_name_map = NULL;
static header_field_info *same_name_hfinfo;
/* Hash table protocol aliases. const char * -> const char * */
static GHashTable *gpa_protocol_aliases = NULL;
/*
* We're called repeatedly with the same field name when sorting a column.
* Cache our last gpa_name_map hit for faster lookups.
*/
static char *last_field_name = NULL;
static header_field_info *last_hfinfo;
static void save_same_name_hfinfo(gpointer data)
{
same_name_hfinfo = (header_field_info*)data;
}
/* Points to the first element of an array of bits, indexed by
a subtree item type; that array element is TRUE if subtrees of
an item of that type are to be expanded. */
static guint32 *tree_is_expanded;
/* Number of elements in that array. */
int num_tree_types;
/* Name hashtables for fast detection of duplicate names */
static GHashTable* proto_names = NULL;
static GHashTable* proto_short_names = NULL;
static GHashTable* proto_filter_names = NULL;
static const char *reserved_filter_names[] = {
/* Display filter keywords. */
"eq",
"ne",
"all_eq",
"any_eq",
"all_ne",
"any_ne",
"gt",
"ge",
"lt",
"le",
"bitwise_and",
"contains",
"matches",
"not",
"and",
"or",
"xor",
"in",
"any",
"all",
"true",
"false",
"nan",
"inf",
NULL
};
static GHashTable *proto_reserved_filter_names = NULL;
static gint
proto_compare_name(gconstpointer p1_arg, gconstpointer p2_arg)
{
const protocol_t *p1 = (const protocol_t *)p1_arg;
const protocol_t *p2 = (const protocol_t *)p2_arg;
return g_ascii_strcasecmp(p1->short_name, p2->short_name);
}
static GSList *dissector_plugins = NULL;
#ifdef HAVE_PLUGINS
void
proto_register_plugin(const proto_plugin *plug)
{
dissector_plugins = g_slist_prepend(dissector_plugins, (proto_plugin *)plug);
}
#else /* HAVE_PLUGINS */
void
proto_register_plugin(const proto_plugin *plug _U_)
{
ws_warning("proto_register_plugin: built without support for binary plugins");
}
#endif /* HAVE_PLUGINS */
static void
call_plugin_register_protoinfo(gpointer data, gpointer user_data _U_)
{
proto_plugin *plug = (proto_plugin *)data;
if (plug->register_protoinfo) {
plug->register_protoinfo();
}
}
static void
call_plugin_register_handoff(gpointer data, gpointer user_data _U_)
{
proto_plugin *plug = (proto_plugin *)data;
if (plug->register_handoff) {
plug->register_handoff();
}
}
/* initialize data structures and register protocols and fields */
void
proto_init(GSList *register_all_plugin_protocols_list,
GSList *register_all_plugin_handoffs_list,
register_cb cb,
gpointer client_data)
{
proto_cleanup_base();
proto_names = g_hash_table_new(g_str_hash, g_str_equal);
proto_short_names = g_hash_table_new(g_str_hash, g_str_equal);
proto_filter_names = g_hash_table_new(g_str_hash, g_str_equal);
proto_reserved_filter_names = g_hash_table_new(g_str_hash, g_str_equal);
for (const char **ptr = reserved_filter_names; *ptr != NULL; ptr++) {
/* GHashTable has no key destructor so the cast is safe. */
g_hash_table_add(proto_reserved_filter_names, *(char **)ptr);
}
gpa_hfinfo.len = 0;
gpa_hfinfo.allocated_len = 0;
gpa_hfinfo.hfi = NULL;
gpa_name_map = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, save_same_name_hfinfo);
gpa_protocol_aliases = g_hash_table_new(g_str_hash, g_str_equal);
deregistered_fields = g_ptr_array_new();
deregistered_data = g_ptr_array_new();
deregistered_slice = g_ptr_array_new();
/* Initialize the ftype subsystem */
ftypes_initialize();
/* Initialize the addres type subsystem */
address_types_initialize();
/* Register one special-case FT_TEXT_ONLY field for use when
converting wireshark to new-style proto_tree. These fields
are merely strings on the GUI tree; they are not filterable */
hf_text_only = proto_register_field_init(&hfi_text_only, -1);
/* Register the pseudo-protocols used for exceptions. */
register_show_exception();
register_type_length_mismatch();
register_byte_array_string_decodinws_error();
register_date_time_string_decodinws_error();
register_string_errors();
ftypes_register_pseudofields();
col_register_protocol();
/* Have each built-in dissector register its protocols, fields,
dissector tables, and dissectors to be called through a
handle, and do whatever one-time initialization it needs to
do. */
register_all_protocols(cb, client_data);
/* Now call the registration routines for all epan plugins. */
for (GSList *l = register_all_plugin_protocols_list; l != NULL; l = l->next) {
((void (*)(register_cb, gpointer))l->data)(cb, client_data);
}
/* Now call the registration routines for all dissector plugins. */
if (cb)
(*cb)(RA_PLUGIN_REGISTER, NULL, client_data);
g_slist_foreach(dissector_plugins, call_plugin_register_protoinfo, NULL);
/* Now call the "handoff registration" routines of all built-in
dissectors; those routines register the dissector in other
dissectors' handoff tables, and fetch any dissector handles
they need. */
register_all_protocol_handoffs(cb, client_data);
/* Now do the same with epan plugins. */
for (GSList *l = register_all_plugin_handoffs_list; l != NULL; l = l->next) {
((void (*)(register_cb, gpointer))l->data)(cb, client_data);
}
/* Now do the same with dissector plugins. */
if (cb)
(*cb)(RA_PLUGIN_HANDOFF, NULL, client_data);
g_slist_foreach(dissector_plugins, call_plugin_register_handoff, NULL);
/* sort the protocols by protocol name */
protocols = g_list_sort(protocols, proto_compare_name);
/* We've assigned all the subtree type values; allocate the array
for them, and zero it out. */
tree_is_expanded = g_new0(guint32, (num_tree_types/32)+1);
}
static void
proto_cleanup_base(void)
{
protocol_t *protocol;
header_field_info *hfinfo;
/* Free the abbrev/ID hash table */
if (gpa_name_map) {
g_hash_table_destroy(gpa_name_map);
gpa_name_map = NULL;
}
if (gpa_protocol_aliases) {
g_hash_table_destroy(gpa_protocol_aliases);
gpa_protocol_aliases = NULL;
}
g_free(last_field_name);
last_field_name = NULL;
while (protocols) {
protocol = (protocol_t *)protocols->data;
PROTO_REGISTRAR_GET_NTH(protocol->proto_id, hfinfo);
DISSECTOR_ASSERT(protocol->proto_id == hfinfo->id);
g_slice_free(header_field_info, hfinfo);
if (protocol->parent_proto_id != -1) {
// pino protocol
DISSECTOR_ASSERT(protocol->fields == NULL); //helpers should not have any registered fields
DISSECTOR_ASSERT(protocol->heur_list == NULL); //helpers should not have a heuristic list
} else {
if (protocol->fields) {
g_ptr_array_free(protocol->fields, TRUE);
}
g_list_free(protocol->heur_list);
}
protocols = g_list_remove(protocols, protocol);
g_free(protocol);
}
if (proto_names) {
g_hash_table_destroy(proto_names);
proto_names = NULL;
}
if (proto_short_names) {
g_hash_table_destroy(proto_short_names);
proto_short_names = NULL;
}
if (proto_filter_names) {
g_hash_table_destroy(proto_filter_names);
proto_filter_names = NULL;
}
if (proto_reserved_filter_names) {
g_hash_table_destroy(proto_reserved_filter_names);
proto_reserved_filter_names = NULL;
}
if (gpa_hfinfo.allocated_len) {
gpa_hfinfo.len = 0;
gpa_hfinfo.allocated_len = 0;
g_free(gpa_hfinfo.hfi);
gpa_hfinfo.hfi = NULL;
}
if (deregistered_fields) {
g_ptr_array_free(deregistered_fields, TRUE);
deregistered_fields = NULL;
}
if (deregistered_data) {
g_ptr_array_free(deregistered_data, TRUE);
deregistered_data = NULL;
}
if (deregistered_slice) {
g_ptr_array_free(deregistered_slice, TRUE);
deregistered_slice = NULL;
}
g_free(tree_is_expanded);
tree_is_expanded = NULL;
if (prefixes)
g_hash_table_destroy(prefixes);
}
void
proto_cleanup(void)
{
proto_free_deregistered_fields();
proto_cleanup_base();
g_slist_free(dissector_plugins);
dissector_plugins = NULL;
}
static gboolean
proto_tree_traverse_pre_order(proto_tree *tree, proto_tree_traverse_func func,
gpointer data)
{
proto_node *pnode = tree;
proto_node *child;
proto_node *current;
if (func(pnode, data))
return TRUE;
child = pnode->first_child;
while (child != NULL) {
/*
* The routine we call might modify the child, e.g. by
* freeing it, so we get the child's successor before
* calling that routine.
*/
current = child;
child = current->next;
if (proto_tree_traverse_pre_order((proto_tree *)current, func, data))
return TRUE;
}
return FALSE;
}
void
proto_tree_children_foreach(proto_tree *tree, proto_tree_foreach_func func,
gpointer data)
{
proto_node *node = tree;
proto_node *current;
if (!node)
return;
node = node->first_child;
while (node != NULL) {
current = node;
node = current->next;
func((proto_tree *)current, data);
}
}
static void
free_GPtrArray_value(gpointer key, gpointer value, gpointer user_data _U_)
{
GPtrArray *ptrs = (GPtrArray *)value;
gint hfid = GPOINTER_TO_UINT(key);
header_field_info *hfinfo;
PROTO_REGISTRAR_GET_NTH(hfid, hfinfo);
if (hfinfo->ref_type != HF_REF_TYPE_NONE) {
/* when a field is referenced by a filter this also
affects the refcount for the parent protocol so we need
to adjust the refcount for the parent as well
*/
if (hfinfo->parent != -1) {
header_field_info *parent_hfinfo;
PROTO_REGISTRAR_GET_NTH(hfinfo->parent, parent_hfinfo);
parent_hfinfo->ref_type = HF_REF_TYPE_NONE;
}
hfinfo->ref_type = HF_REF_TYPE_NONE;
}
g_ptr_array_free(ptrs, TRUE);
}
static void
proto_tree_free_node(proto_node *node, gpointer data _U_)
{
field_info *finfo = PNODE_FINFO(node);
proto_tree_children_foreach(node, proto_tree_free_node, NULL);
fvalue_free(finfo->value);
finfo->value = NULL;
}
void
proto_tree_reset(proto_tree *tree)
{
tree_data_t *tree_data = PTREE_DATA(tree);
proto_tree_children_foreach(tree, proto_tree_free_node, NULL);
/* free tree data */
if (tree_data->interesting_hfids) {
/* Free all the GPtrArray's in the interesting_hfids hash. */
g_hash_table_foreach(tree_data->interesting_hfids,
free_GPtrArray_value, NULL);
/* And then remove all values. */
g_hash_table_remove_all(tree_data->interesting_hfids);
}
/* Reset track of the number of children */
tree_data->count = 0;
PROTO_NODE_INIT(tree);
}
/* frees the resources that the dissection a proto_tree uses */
void
proto_tree_free(proto_tree *tree)
{
tree_data_t *tree_data = PTREE_DATA(tree);
proto_tree_children_foreach(tree, proto_tree_free_node, NULL);
/* free tree data */
if (tree_data->interesting_hfids) {
/* Free all the GPtrArray's in the interesting_hfids hash. */
g_hash_table_foreach(tree_data->interesting_hfids,
free_GPtrArray_value, NULL);
/* And then destroy the hash. */
g_hash_table_destroy(tree_data->interesting_hfids);
}
g_slice_free(tree_data_t, tree_data);
g_slice_free(proto_tree, tree);
}
/* Is the parsing being done for a visible proto_tree or an invisible one?
* By setting this correctly, the proto_tree creation is sped up by not
* having to call vsnprintf and copy strings around.
*/
gboolean
proto_tree_set_visible(proto_tree *tree, gboolean visible)
{
gboolean old_visible = PTREE_DATA(tree)->visible;
PTREE_DATA(tree)->visible = visible;
return old_visible;
}
void
proto_tree_set_fake_protocols(proto_tree *tree, gboolean fake_protocols)
{
PTREE_DATA(tree)->fake_protocols = fake_protocols;
}
/* Assume dissector set only its protocol fields.
This function is called by dissectors and allows the speeding up of filtering
in wireshark; if this function returns FALSE it is safe to reset tree to NULL
and thus skip calling most of the expensive proto_tree_add_...()
functions.
If the tree is visible we implicitly assume the field is referenced.
*/
gboolean
proto_field_is_referenced(proto_tree *tree, int proto_id)
{
register header_field_info *hfinfo;
if (!tree)
return FALSE;
if (PTREE_DATA(tree)->visible)
return TRUE;
PROTO_REGISTRAR_GET_NTH(proto_id, hfinfo);
if (hfinfo->ref_type != HF_REF_TYPE_NONE)
return TRUE;
if (hfinfo->type == FT_PROTOCOL && !PTREE_DATA(tree)->fake_protocols)
return TRUE;
return FALSE;
}
/* Finds a record in the hfinfo array by id. */
header_field_info *
proto_registrar_get_nth(guint hfindex)
{
register header_field_info *hfinfo;
PROTO_REGISTRAR_GET_NTH(hfindex, hfinfo);
return hfinfo;
}
/* Prefix initialization
* this allows for a dissector to register a display filter name prefix
* so that it can delay the initialization of the hf array as long as
* possible.
*/
/* compute a hash for the part before the dot of a display filter */
static guint
prefix_hash (gconstpointer key) {
/* end the string at the dot and compute its hash */
gchar* copy = g_strdup((const gchar *)key);
gchar* c = copy;
guint tmp;
for (; *c; c++) {
if (*c == '.') {
*c = 0;
break;
}
}
tmp = g_str_hash(copy);
g_free(copy);
return tmp;
}
/* are both strings equal up to the end or the dot? */
static gboolean
prefix_equal (gconstpointer ap, gconstpointer bp) {
const gchar* a = (const gchar *)ap;
const gchar* b = (const gchar *)bp;
do {
gchar ac = *a++;
gchar bc = *b++;
if ( (ac == '.' || ac == '\0') && (bc == '.' || bc == '\0') ) return TRUE;
if ( (ac == '.' || ac == '\0') && ! (bc == '.' || bc == '\0') ) return FALSE;
if ( (bc == '.' || bc == '\0') && ! (ac == '.' || ac == '\0') ) return FALSE;
if (ac != bc) return FALSE;
} while (1);
return FALSE;
}
/* Register a new prefix for "delayed" initialization of field arrays */
void
proto_register_prefix(const char *prefix, prefix_initializer_t pi ) {
if (! prefixes ) {
prefixes = g_hash_table_new(prefix_hash, prefix_equal);
}
g_hash_table_insert(prefixes, (gpointer)prefix, (gpointer)pi);
}
/* helper to call all prefix initializers */
static gboolean
initialize_prefix(gpointer k, gpointer v, gpointer u _U_) {
((prefix_initializer_t)v)((const char *)k);
return TRUE;
}
/** Initialize every remaining uninitialized prefix. */
void
proto_initialize_all_prefixes(void) {
g_hash_table_foreach_remove(prefixes, initialize_prefix, NULL);
}
/* Finds a record in the hfinfo array by name.
* If it fails to find it in the already registered fields,
* it tries to find and call an initializer in the prefixes
* table and if so it looks again.
*/
header_field_info *
proto_registrar_get_byname(const char *field_name)
{
header_field_info *hfinfo;
prefix_initializer_t pi;
if (!field_name)
return NULL;
if (g_strcmp0(field_name, last_field_name) == 0) {
return last_hfinfo;
}
hfinfo = (header_field_info *)g_hash_table_lookup(gpa_name_map, field_name);
if (hfinfo) {
g_free(last_field_name);
last_field_name = g_strdup(field_name);
last_hfinfo = hfinfo;
return hfinfo;
}
if (!prefixes)
return NULL;
if ((pi = (prefix_initializer_t)g_hash_table_lookup(prefixes, field_name) ) != NULL) {
pi(field_name);
g_hash_table_remove(prefixes, field_name);
} else {
return NULL;
}
hfinfo = (header_field_info *)g_hash_table_lookup(gpa_name_map, field_name);
if (hfinfo) {
g_free(last_field_name);
last_field_name = g_strdup(field_name);
last_hfinfo = hfinfo;
}
return hfinfo;
}
header_field_info*
proto_registrar_get_byalias(const char *alias_name)
{
if (!alias_name) {
return NULL;
}
/* Find our aliased protocol. */
char *an_copy = g_strdup(alias_name);
char *dot = strchr(an_copy, '.');
if (dot) {
*dot = '\0';
}
const char *proto_pfx = (const char *) g_hash_table_lookup(gpa_protocol_aliases, an_copy);
if (!proto_pfx) {
g_free(an_copy);
return NULL;
}
/* Construct our aliased field and look it up. */
GString *filter_name = g_string_new(proto_pfx);
if (dot) {
g_string_append_printf(filter_name, ".%s", dot+1);
}
header_field_info *hfinfo = proto_registrar_get_byname(filter_name->str);
g_free(an_copy);
g_string_free(filter_name, TRUE);
return hfinfo;
}
int
proto_registrar_get_id_byname(const char *field_name)
{
header_field_info *hfinfo;
hfinfo = proto_registrar_get_byname(field_name);
if (!hfinfo)
return -1;
return hfinfo->id;
}
static int
label_strcat_flags(const header_field_info *hfinfo)
{
if (FIELD_DISPLAY(hfinfo->display) & BASE_STR_WSP)
return FORMAT_LABEL_REPLACE_SPACE;
return 0;
}
static char *
format_bytes_hfinfo_maxlen(wmem_allocator_t *scope, const header_field_info *hfinfo,
const guint8 *bytes, guint length, size_t max_str_len)
{
char *str = NULL;
const guint8 *p;
gboolean is_printable;
if (bytes) {
if (hfinfo->display & BASE_SHOW_UTF_8_PRINTABLE) {
/*
* If all bytes are valid and printable UTF-8, show the
* bytes as a string - in quotes to indicate that it's
* a string.
*/
if (isprint_utf8_string(bytes, length)) {
str = wmem_strdup_printf(scope, "\"%.*s\"",
(int)length, bytes);
return str;
}
} else if (hfinfo->display & BASE_SHOW_ASCII_PRINTABLE) {
/*
* Check whether all bytes are printable.
*/
is_printable = TRUE;
for (p = bytes; p < bytes+length; p++) {
if (!g_ascii_isprint(*p)) {
/* Not printable. */
is_printable = FALSE;
break;
}
}
/*
* If all bytes are printable ASCII, show the bytes
* as a string - in quotes to indicate that it's
* a string.
*/
if (is_printable) {
str = wmem_strdup_printf(scope, "\"%.*s\"",
(int)length, bytes);
return str;
}
}
/*
* Either it's not printable ASCII, or we don't care whether
* it's printable ASCII; show it as hex bytes.
*/
switch (FIELD_DISPLAY(hfinfo->display)) {
case SEP_DOT:
str = bytes_to_str_punct_maxlen(scope, bytes, length, '.', max_str_len/3);
break;
case SEP_DASH:
str = bytes_to_str_punct_maxlen(scope, bytes, length, '-', max_str_len/3);
break;
case SEP_COLON:
str = bytes_to_str_punct_maxlen(scope, bytes, length, ':', max_str_len/3);
break;
case SEP_SPACE:
str = bytes_to_str_punct_maxlen(scope, bytes, length, ' ', max_str_len/3);
break;
case BASE_NONE:
default:
if (prefs.display_byte_fields_with_spaces) {
str = bytes_to_str_punct_maxlen(scope, bytes, length, ' ', max_str_len/3);
} else {
str = bytes_to_str_maxlen(scope, bytes, length, max_str_len/2);
}
break;
}
}
else {
if (hfinfo->display & BASE_ALLOW_ZERO) {
str = wmem_strdup(scope, "<none>");
} else {
str = wmem_strdup(scope, "<MISSING>");
}
}
return str;
}
static char *
format_bytes_hfinfo(wmem_allocator_t *scope, const header_field_info *hfinfo,
const guint8 *bytes, guint length)
{
return format_bytes_hfinfo_maxlen(scope, hfinfo, bytes, length, ITEM_LABEL_LENGTH);
}
static void
ptvcursor_new_subtree_levels(ptvcursor_t *ptvc)
{
subtree_lvl *pushed_tree;
DISSECTOR_ASSERT(ptvc->pushed_tree_max <= SUBTREE_MAX_LEVELS-SUBTREE_ONCE_ALLOCATION_NUMBER);
ptvc->pushed_tree_max += SUBTREE_ONCE_ALLOCATION_NUMBER;
pushed_tree = (subtree_lvl *)wmem_realloc(ptvc->scope, (void *)ptvc->pushed_tree, sizeof(subtree_lvl) * ptvc->pushed_tree_max);
DISSECTOR_ASSERT(pushed_tree != NULL);
ptvc->pushed_tree = pushed_tree;
}
static void
ptvcursor_free_subtree_levels(ptvcursor_t *ptvc)
{
ptvc->pushed_tree = NULL;
ptvc->pushed_tree_max = 0;
DISSECTOR_ASSERT(ptvc->pushed_tree_index == 0);
ptvc->pushed_tree_index = 0;
}
/* Allocates an initializes a ptvcursor_t with 3 variables:
* proto_tree, tvbuff, and offset. */
ptvcursor_t *
ptvcursor_new(wmem_allocator_t *scope, proto_tree *tree, tvbuff_t *tvb, gint offset)
{
ptvcursor_t *ptvc;
ptvc = wmem_new(scope, ptvcursor_t);
ptvc->scope = scope;
ptvc->tree = tree;
ptvc->tvb = tvb;
ptvc->offset = offset;
ptvc->pushed_tree = NULL;
ptvc->pushed_tree_max = 0;
ptvc->pushed_tree_index = 0;
return ptvc;
}
/* Frees memory for ptvcursor_t, but nothing deeper than that. */
void
ptvcursor_free(ptvcursor_t *ptvc)
{
ptvcursor_free_subtree_levels(ptvc);
/*g_free(ptvc);*/
}
/* Returns tvbuff. */
tvbuff_t *
ptvcursor_tvbuff(ptvcursor_t *ptvc)
{
return ptvc->tvb;
}
/* Returns current offset. */
gint
ptvcursor_current_offset(ptvcursor_t *ptvc)
{
return ptvc->offset;
}
proto_tree *
ptvcursor_tree(ptvcursor_t *ptvc)
{
if (!ptvc)
return NULL;
return ptvc->tree;
}
void
ptvcursor_set_tree(ptvcursor_t *ptvc, proto_tree *tree)
{
ptvc->tree = tree;
}
/* creates a subtree, sets it as the working tree and pushes the old working tree */
proto_tree *
ptvcursor_push_subtree(ptvcursor_t *ptvc, proto_item *it, gint ett_subtree)
{
subtree_lvl *subtree;
if (ptvc->pushed_tree_index >= ptvc->pushed_tree_max)
ptvcursor_new_subtree_levels(ptvc);
subtree = ptvc->pushed_tree + ptvc->pushed_tree_index;
subtree->tree = ptvc->tree;
subtree->it= NULL;
ptvc->pushed_tree_index++;
return ptvcursor_set_subtree(ptvc, it, ett_subtree);
}
/* pops a subtree */
void
ptvcursor_pop_subtree(ptvcursor_t *ptvc)
{
subtree_lvl *subtree;
if (ptvc->pushed_tree_index <= 0)
return;
ptvc->pushed_tree_index--;
subtree = ptvc->pushed_tree + ptvc->pushed_tree_index;
if (subtree->it != NULL)
proto_item_set_len(subtree->it, ptvcursor_current_offset(ptvc) - subtree->cursor_offset);
ptvc->tree = subtree->tree;
}
/* saves the current tvb offset and the item in the current subtree level */
static void
ptvcursor_subtree_set_item(ptvcursor_t *ptvc, proto_item *it)
{
subtree_lvl *subtree;
DISSECTOR_ASSERT(ptvc->pushed_tree_index > 0);
subtree = ptvc->pushed_tree + ptvc->pushed_tree_index - 1;
subtree->it = it;
subtree->cursor_offset = ptvcursor_current_offset(ptvc);
}
/* Creates a subtree and adds it to the cursor as the working tree but does not
* save the old working tree */
proto_tree *
ptvcursor_set_subtree(ptvcursor_t *ptvc, proto_item *it, gint ett_subtree)
{
ptvc->tree = proto_item_add_subtree(it, ett_subtree);
return ptvc->tree;
}
static proto_tree *
ptvcursor_add_subtree_item(ptvcursor_t *ptvc, proto_item *it, gint ett_subtree, gint length)
{
ptvcursor_push_subtree(ptvc, it, ett_subtree);
if (length == SUBTREE_UNDEFINED_LENGTH)
ptvcursor_subtree_set_item(ptvc, it);
return ptvcursor_tree(ptvc);
}
/* Add an item to the tree and create a subtree
* If the length is unknown, length may be defined as SUBTREE_UNDEFINED_LENGTH.
* In this case, when the subtree will be closed, the parent item length will
* be equal to the advancement of the cursor since the creation of the subtree.
*/
proto_tree *
ptvcursor_add_with_subtree(ptvcursor_t *ptvc, int hfindex, gint length,
const guint encoding, gint ett_subtree)
{
proto_item *it;
it = ptvcursor_add_no_advance(ptvc, hfindex, length, encoding);
return ptvcursor_add_subtree_item(ptvc, it, ett_subtree, length);
}
static proto_item *
proto_tree_add_text_node(proto_tree *tree, tvbuff_t *tvb, gint start, gint length);
/* Add a text node to the tree and create a subtree
* If the length is unknown, length may be defined as SUBTREE_UNDEFINED_LENGTH.
* In this case, when the subtree will be closed, the item length will be equal
* to the advancement of the cursor since the creation of the subtree.
*/
proto_tree *
ptvcursor_add_text_with_subtree(ptvcursor_t *ptvc, gint length,
gint ett_subtree, const char *format, ...)
{
proto_item *pi;
va_list ap;
header_field_info *hfinfo;
proto_tree *tree;
tree = ptvcursor_tree(ptvc);
CHECK_FOR_NULL_TREE(tree);
TRY_TO_FAKE_THIS_ITEM(tree, hf_text_only, hfinfo);
pi = proto_tree_add_text_node(tree, ptvcursor_tvbuff(ptvc),
ptvcursor_current_offset(ptvc), length);
TRY_TO_FAKE_THIS_REPR(pi);
va_start(ap, format);
proto_tree_set_representation(pi, format, ap);
va_end(ap);
return ptvcursor_add_subtree_item(ptvc, pi, ett_subtree, length);
}
/* Add a text-only node, leaving it to our caller to fill the text in */
static proto_item *
proto_tree_add_text_node(proto_tree *tree, tvbuff_t *tvb, gint start, gint length)
{
proto_item *pi;
if (tree == NULL)
return NULL;
pi = proto_tree_add_pi(tree, &hfi_text_only, tvb, start, &length);
return pi;
}
/* (INTERNAL USE ONLY) Add a text-only node to the proto_tree */
proto_item *
proto_tree_add_text_internal(proto_tree *tree, tvbuff_t *tvb, gint start, gint length,
const char *format, ...)
{
proto_item *pi;
va_list ap;
header_field_info *hfinfo;
if (length == -1) {
length = tvb_captured_length(tvb) ? tvb_ensure_captured_length_remaining(tvb, start) : 0;
} else {
tvb_ensure_bytes_exist(tvb, start, length);
}
CHECK_FOR_NULL_TREE(tree);
TRY_TO_FAKE_THIS_ITEM(tree, hf_text_only, hfinfo);
pi = proto_tree_add_text_node(tree, tvb, start, length);
TRY_TO_FAKE_THIS_REPR(pi);
va_start(ap, format);
proto_tree_set_representation(pi, format, ap);
va_end(ap);
return pi;
}
/* (INTERNAL USE ONLY) Add a text-only node to the proto_tree (va_list version) */
proto_item *
proto_tree_add_text_valist_internal(proto_tree *tree, tvbuff_t *tvb, gint start,
gint length, const char *format, va_list ap)
{
proto_item *pi;
header_field_info *hfinfo;
if (length == -1) {
length = tvb_captured_length(tvb) ? tvb_ensure_captured_length_remaining(tvb, start) : 0;
} else {
tvb_ensure_bytes_exist(tvb, start, length);
}
CHECK_FOR_NULL_TREE(tree);
TRY_TO_FAKE_THIS_ITEM(tree, hf_text_only, hfinfo);
pi = proto_tree_add_text_node(tree, tvb, start, length);
TRY_TO_FAKE_THIS_REPR(pi);
proto_tree_set_representation(pi, format, ap);
return pi;
}
/* Add a text-only node that creates a subtree underneath.
*/
proto_tree *
proto_tree_add_subtree(proto_tree *tree, tvbuff_t *tvb, gint start, gint length, gint idx, proto_item **tree_item, const char *text)
{
return proto_tree_add_subtree_format(tree, tvb, start, length, idx, tree_item, "%s", text);
}
/* Add a text-only node that creates a subtree underneath.
*/
proto_tree *
proto_tree_add_subtree_format(proto_tree *tree, tvbuff_t *tvb, gint start, gint length, gint idx, proto_item **tree_item, const char *format, ...)
{
proto_tree *pt;
proto_item *pi;
va_list ap;
va_start(ap, format);
pi = proto_tree_add_text_valist_internal(tree, tvb, start, length, format, ap);
va_end(ap);
if (tree_item != NULL)
*tree_item = pi;
pt = proto_item_add_subtree(pi, idx);
return pt;
}
/* Add a text-only node for debugging purposes. The caller doesn't need
* to worry about tvbuff, start, or length. Debug message gets sent to
* STDOUT, too */
proto_item *
proto_tree_add_debug_text(proto_tree *tree, const char *format, ...)
{
proto_item *pi;
va_list ap;
pi = proto_tree_add_text_node(tree, NULL, 0, 0);
if (pi) {
va_start(ap, format);
proto_tree_set_representation(pi, format, ap);
va_end(ap);
}
va_start(ap, format);
vprintf(format, ap);
va_end(ap);
printf("\n");
return pi;
}
proto_item *
proto_tree_add_format_text(proto_tree *tree, tvbuff_t *tvb, gint start, gint length)
{
proto_item *pi;
header_field_info *hfinfo;
CHECK_FOR_NULL_TREE(tree);
TRY_TO_FAKE_THIS_ITEM(tree, hf_text_only, hfinfo);
pi = proto_tree_add_text_node(tree, tvb, start, length);
TRY_TO_FAKE_THIS_REPR(pi);
proto_item_set_text(pi, "%s", tvb_format_text(tree->tree_data->pinfo->pool, tvb, start, length));
return pi;
}
proto_item *
proto_tree_add_format_wsp_text(proto_tree *tree, tvbuff_t *tvb, gint start, gint length)
{
proto_item *pi;
header_field_info *hfinfo;
gchar *str;
CHECK_FOR_NULL_TREE(tree);
TRY_TO_FAKE_THIS_ITEM(tree, hf_text_only, hfinfo);
pi = proto_tree_add_text_node(tree, tvb, start, length);
TRY_TO_FAKE_THIS_REPR(pi);
str = tvb_format_text_wsp(NULL, tvb, start, length);
proto_item_set_text(pi, "%s", str);
wmem_free(NULL, str);
return pi;
}
void proto_report_dissector_bug(const char *format, ...)
{
va_list args;
if (wireshark_abort_on_dissector_bug) {
/*
* Try to have the error message show up in the crash
* information.
*/
va_start(args, format);
ws_vadd_crash_info(format, args);
va_end(args);
/*
* Print the error message.
*/
va_start(args, format);
vfprintf(stderr, format, args);
va_end(args);
putc('\n', stderr);
/*
* And crash.
*/
abort();
} else {
va_start(args, format);
VTHROW_FORMATTED(DissectorError, format, args);
va_end(args);
}
}
/* We could probably get away with changing is_error to a minimum length value. */
static void
report_type_length_mismatch(proto_tree *tree, const gchar *descr, int length, gboolean is_error)
{
if (is_error) {
expert_add_info_format(NULL, tree, &ei_type_length_mismatch_error, "Trying to fetch %s with length %d", descr, length);
} else {
expert_add_info_format(NULL, tree, &ei_type_length_mismatch_warn, "Trying to fetch %s with length %d", descr, length);
}
if (is_error) {
THROW(ReportedBoundsError);
}
}
static guint32
get_uint_value(proto_tree *tree, tvbuff_t *tvb, gint offset, gint length, const guint encoding)
{
guint32 value;
gboolean length_error;
switch (length) {
case 1:
value = tvb_get_guint8(tvb, offset);
if (encoding & ENC_ZIGBEE) {
if (value == 0xFF) { /* Invalid Zigbee length, set to 0 */
value = 0;
}
}
break;
case 2:
value = (encoding & ENC_LITTLE_ENDIAN) ? tvb_get_letohs(tvb, offset)
: tvb_get_ntohs(tvb, offset);
if (encoding & ENC_ZIGBEE) {
if (value == 0xFFFF) { /* Invalid Zigbee length, set to 0 */
value = 0;
}
}
break;
case 3:
value = (encoding & ENC_LITTLE_ENDIAN) ? tvb_get_letoh24(tvb, offset)
: tvb_get_ntoh24(tvb, offset);
break;
case 4:
value = (encoding & ENC_LITTLE_ENDIAN) ? tvb_get_letohl(tvb, offset)
: tvb_get_ntohl(tvb, offset);
break;
default:
if (length < 1) {
length_error = TRUE;
value = 0;
} else {
length_error = FALSE;
value = (encoding & ENC_LITTLE_ENDIAN) ? tvb_get_letohl(tvb, offset)
: tvb_get_ntohl(tvb, offset);
}
report_type_length_mismatch(tree, "an unsigned integer", length, length_error);
break;
}
return value;
}
static inline guint64
get_uint64_value(proto_tree *tree, tvbuff_t *tvb, gint offset, guint length, const guint encoding)
{
guint64 value;
gboolean length_error;
switch (length) {
case 1:
value = tvb_get_guint8(tvb, offset);
break;
case 2:
value = (encoding & ENC_LITTLE_ENDIAN) ? tvb_get_letohs(tvb, offset)
: tvb_get_ntohs(tvb, offset);
break;
case 3:
value = (encoding & ENC_LITTLE_ENDIAN) ? tvb_get_letoh24(tvb, offset)
: tvb_get_ntoh24(tvb, offset);
break;
case 4:
value = (encoding & ENC_LITTLE_ENDIAN) ? tvb_get_letohl(tvb, offset)
: tvb_get_ntohl(tvb, offset);
break;
case 5:
value = (encoding & ENC_LITTLE_ENDIAN) ? tvb_get_letoh40(tvb, offset)
: tvb_get_ntoh40(tvb, offset);
break;
case 6:
value = (encoding & ENC_LITTLE_ENDIAN) ? tvb_get_letoh48(tvb, offset)
: tvb_get_ntoh48(tvb, offset);
break;
case 7:
value = (encoding & ENC_LITTLE_ENDIAN) ? tvb_get_letoh56(tvb, offset)
: tvb_get_ntoh56(tvb, offset);
break;
case 8:
value = (encoding & ENC_LITTLE_ENDIAN) ? tvb_get_letoh64(tvb, offset)
: tvb_get_ntoh64(tvb, offset);
break;
default:
if (length < 1) {
length_error = TRUE;
value = 0;
} else {
length_error = FALSE;
value = (encoding & ENC_LITTLE_ENDIAN) ? tvb_get_letoh64(tvb, offset)
: tvb_get_ntoh64(tvb, offset);
}
report_type_length_mismatch(tree, "an unsigned integer", length, length_error);
break;
}
return value;
}
static gint32
get_int_value(proto_tree *tree, tvbuff_t *tvb, gint offset, gint length, const guint encoding)
{
gint32 value;
gboolean length_error;
switch (length) {
case 1:
value = tvb_get_gint8(tvb, offset);
break;
case 2:
value = encoding ? tvb_get_letohis(tvb, offset)
: tvb_get_ntohis(tvb, offset);
break;
case 3:
value = encoding ? tvb_get_letohi24(tvb, offset)
: tvb_get_ntohi24(tvb, offset);
break;
case 4:
value = encoding ? tvb_get_letohil(tvb, offset)
: tvb_get_ntohil(tvb, offset);
break;
default:
if (length < 1) {
length_error = TRUE;
value = 0;
} else {
length_error = FALSE;
value = encoding ? tvb_get_letohil(tvb, offset)
: tvb_get_ntohil(tvb, offset);
}
report_type_length_mismatch(tree, "a signed integer", length, length_error);
break;
}
return value;
}
/* Note: this returns an unsigned int64, but with the appropriate bit(s) set to
* be cast-able as a gint64. This is weird, but what the code has always done.
*/
static inline guint64
get_int64_value(proto_tree *tree, tvbuff_t *tvb, gint start, guint length, const guint encoding)
{
guint64 value = get_uint64_value(tree, tvb, start, length, encoding);
switch (length) {
case 7:
value = ws_sign_ext64(value, 56);
break;
case 6:
value = ws_sign_ext64(value, 48);
break;
case 5:
value = ws_sign_ext64(value, 40);
break;
case 4:
value = ws_sign_ext64(value, 32);
break;
case 3:
value = ws_sign_ext64(value, 24);
break;
case 2:
value = ws_sign_ext64(value, 16);
break;
case 1:
value = ws_sign_ext64(value, 8);
break;
}
return value;
}
/* For FT_STRING */
static inline const guint8 *
get_string_value(wmem_allocator_t *scope, tvbuff_t *tvb, gint start,
gint length, gint *ret_length, const guint encoding)
{
if (length == -1) {
length = tvb_ensure_captured_length_remaining(tvb, start);
}
*ret_length = length;
return tvb_get_string_enc(scope, tvb, start, length, encoding);
}
/* For FT_STRINGZ */
static inline const guint8 *
get_stringz_value(wmem_allocator_t *scope, proto_tree *tree, tvbuff_t *tvb,
gint start, gint length, gint *ret_length, const guint encoding)
{
const guint8 *value;
if (length < -1) {
report_type_length_mismatch(tree, "a string", length, TRUE);
}
if (length == -1) {
/* This can throw an exception */
value = tvb_get_stringz_enc(scope, tvb, start, &length, encoding);
} else {
/* In this case, length signifies the length of the string.
*
* This could either be a null-padded string, which doesn't
* necessarily have a '\0' at the end, or a null-terminated
* string, with a trailing '\0'. (Yes, there are cases
* where you have a string that's both counted and null-
* terminated.)
*
* In the first case, we must allocate a buffer of length
* "length+1", to make room for a trailing '\0'.
*
* In the second case, we don't assume that there is a
* trailing '\0' there, as the packet might be malformed.
* (XXX - should we throw an exception if there's no
* trailing '\0'?) Therefore, we allocate a buffer of
* length "length+1", and put in a trailing '\0', just to
* be safe.
*
* (XXX - this would change if we made string values counted
* rather than null-terminated.)
*/
value = tvb_get_string_enc(scope, tvb, start, length, encoding);
}
*ret_length = length;
return value;
}
/* For FT_UINT_STRING */
static inline const guint8 *
get_uint_string_value(wmem_allocator_t *scope, proto_tree *tree,
tvbuff_t *tvb, gint start, gint length, gint *ret_length,
const guint encoding)
{
guint32 n;
const guint8 *value;
/* I believe it's ok if this is called with a NULL tree */
n = get_uint_value(tree, tvb, start, length, encoding & ~ENC_CHARENCODING_MASK);
value = tvb_get_string_enc(scope, tvb, start + length, n, encoding);
length += n;
*ret_length = length;
return value;
}
/* For FT_STRINGZPAD */
static inline const guint8 *
get_stringzpad_value(wmem_allocator_t *scope, tvbuff_t *tvb, gint start,
gint length, gint *ret_length, const guint encoding)
{
/*
* XXX - currently, string values are null-
* terminated, so a "zero-padded" string
* isn't special. If we represent string
* values as something that includes a counted
* array of bytes, we'll need to strip the
* trailing NULs.
*/
if (length == -1) {
length = tvb_ensure_captured_length_remaining(tvb, start);
}
*ret_length = length;
return tvb_get_string_enc(scope, tvb, start, length, encoding);
}
/* For FT_STRINGZTRUNC */
static inline const guint8 *
get_stringztrunc_value(wmem_allocator_t *scope, tvbuff_t *tvb, gint start,
gint length, gint *ret_length, const guint encoding)
{
/*
* XXX - currently, string values are null-
* terminated, so a "zero-truncated" string
* isn't special. If we represent string
* values as something that includes a counted
* array of bytes, we'll need to strip everything
* starting with the terminating NUL.
*/
if (length == -1) {
length = tvb_ensure_captured_length_remaining(tvb, start);
}
*ret_length = length;
return tvb_get_string_enc(scope, tvb, start, length, encoding);
}
/*
* Deltas between the epochs for various non-UN*X time stamp formats and
* the January 1, 1970, 00:00:00 (proleptic?) UTC epoch for the UN*X time
* stamp format.
*/
/*
* NTP Era 0: the epoch is January 1, 1900, 00:00:00 (proleptic?) UTC.
* XXX - if it's OK if this is unsigned, can we just use
* EPOCH_DELTA_1900_01_01_00_00_00_UTC?
*/
#define NTP_TIMEDIFF1900TO1970SEC G_GINT64_CONSTANT(2208988800)
/*
* NTP Era 1: the epoch is February 7, 2036, 06:28:16 UTC.
*/
#define NTP_TIMEDIFF1970TO2036SEC G_GINT64_CONSTANT(2085978496)
/* this can be called when there is no tree, so tree may be null */
static void
get_time_value(proto_tree *tree, tvbuff_t *tvb, const gint start,
const gint length, const guint encoding, nstime_t *time_stamp,
const gboolean is_relative)
{
guint32 tmpsecs;
guint64 tmp64secs;
guint64 todusecs;
switch (encoding) {
case ENC_TIME_SECS_NSECS|ENC_BIG_ENDIAN:
/*
* If the length is 16, 8-byte seconds, followed
* by 8-byte fractional time in nanoseconds,
* both big-endian.
*
* If the length is 12, 8-byte seconds, followed
* by 4-byte fractional time in nanoseconds,
* both big-endian.
*
* If the length is 8, 4-byte seconds, followed
* by 4-byte fractional time in nanoseconds,
* both big-endian.
*
* For absolute times, the seconds are seconds
* since the UN*X epoch.
*/
if (length == 16) {
time_stamp->secs = (time_t)tvb_get_ntoh64(tvb, start);
time_stamp->nsecs = (guint32)tvb_get_ntoh64(tvb, start+8);
} else if (length == 12) {
time_stamp->secs = (time_t)tvb_get_ntoh64(tvb, start);
time_stamp->nsecs = tvb_get_ntohl(tvb, start+8);
} else if (length == 8) {
time_stamp->secs = (time_t)tvb_get_ntohl(tvb, start);
time_stamp->nsecs = tvb_get_ntohl(tvb, start+4);
} else if (length == 4) {
/*
* Backwards compatibility.
* ENC_TIME_SECS_NSECS is 0; using
* ENC_BIG_ENDIAN by itself with a 4-byte
* time-in-seconds value was done in the
* past.
*/
time_stamp->secs = (time_t)tvb_get_ntohl(tvb, start);
time_stamp->nsecs = 0;
} else {
time_stamp->secs = 0;
time_stamp->nsecs = 0;
report_type_length_mismatch(tree, "a timespec", length, (length < 4));
}
break;
case ENC_TIME_SECS_NSECS|ENC_LITTLE_ENDIAN:
/*
* If the length is 16, 8-byte seconds, followed
* by 8-byte fractional time in nanoseconds,
* both little-endian.
*
* If the length is 12, 8-byte seconds, followed
* by 4-byte fractional time in nanoseconds,
* both little-endian.
*
* If the length is 8, 4-byte seconds, followed
* by 4-byte fractional time in nanoseconds,
* both little-endian.
*
* For absolute times, the seconds are seconds
* since the UN*X epoch.
*/
if (length == 16) {
time_stamp->secs = (time_t)tvb_get_letoh64(tvb, start);
time_stamp->nsecs = (guint32)tvb_get_letoh64(tvb, start+8);
} else if (length == 12) {
time_stamp->secs = (time_t)tvb_get_letoh64(tvb, start);
time_stamp->nsecs = tvb_get_letohl(tvb, start+8);
} else if (length == 8) {
time_stamp->secs = (time_t)tvb_get_letohl(tvb, start);
time_stamp->nsecs = tvb_get_letohl(tvb, start+4);
} else if (length == 4) {
/*
* Backwards compatibility.
* ENC_TIME_SECS_NSECS is 0; using
* ENC_LITTLE_ENDIAN by itself with a 4-byte
* time-in-seconds value was done in the
* past.
*/
time_stamp->secs = (time_t)tvb_get_letohl(tvb, start);
time_stamp->nsecs = 0;
} else {
time_stamp->secs = 0;
time_stamp->nsecs = 0;
report_type_length_mismatch(tree, "a timespec", length, (length < 4));
}
break;
case ENC_TIME_NTP|ENC_BIG_ENDIAN:
/*
* NTP time stamp, big-endian.
* Only supported for absolute times.
*/
DISSECTOR_ASSERT(!is_relative);
/* We need a temporary variable here so the unsigned math
* works correctly (for years > 2036 according to RFC 2030
* chapter 3).
*
* If bit 0 is set, the UTC time is in the range 1968-2036 and
* UTC time is reckoned from 0h 0m 0s UTC on 1 January 1900.
* If bit 0 is not set, the time is in the range 2036-2104 and
* UTC time is reckoned from 6h 28m 16s UTC on 7 February 2036.
*/
tmpsecs = tvb_get_ntohl(tvb, start);
if ((tmpsecs & 0x80000000) != 0)
time_stamp->secs = (time_t)((gint64)tmpsecs - NTP_TIMEDIFF1900TO1970SEC);
else
time_stamp->secs = (time_t)((gint64)tmpsecs + NTP_TIMEDIFF1970TO2036SEC);
if (length == 8) {
/*
* Convert 1/2^32s of a second to nanoseconds.
*/
time_stamp->nsecs = (int)(1000000000*(tvb_get_ntohl(tvb, start+4)/4294967296.0));
if ((time_stamp->nsecs == 0) && (tmpsecs == 0)) {
//This is "NULL" time
time_stamp->secs = 0;
}
} else if (length == 4) {
/*
* Backwards compatibility.
*/
if (tmpsecs == 0) {
//This is "NULL" time
time_stamp->secs = 0;
}
time_stamp->nsecs = 0;
} else {
time_stamp->secs = 0;
time_stamp->nsecs = 0;
report_type_length_mismatch(tree, "an NTP time stamp", length, (length < 4));
}
break;
case ENC_TIME_NTP|ENC_LITTLE_ENDIAN:
/*
* NTP time stamp, little-endian.
* Only supported for absolute times.
*/
DISSECTOR_ASSERT(!is_relative);
/* We need a temporary variable here so the unsigned math
* works correctly (for years > 2036 according to RFC 2030
* chapter 3).
*
* If bit 0 is set, the UTC time is in the range 1968-2036 and
* UTC time is reckoned from 0h 0m 0s UTC on 1 January 1900.
* If bit 0 is not set, the time is in the range 2036-2104 and
* UTC time is reckoned from 6h 28m 16s UTC on 7 February 2036.
*/
tmpsecs = tvb_get_letohl(tvb, start);
if ((tmpsecs & 0x80000000) != 0)
time_stamp->secs = (time_t)((gint64)tmpsecs - NTP_TIMEDIFF1900TO1970SEC);
else
time_stamp->secs = (time_t)((gint64)tmpsecs + NTP_TIMEDIFF1970TO2036SEC);
if (length == 8) {
/*
* Convert 1/2^32s of a second to nanoseconds.
*/
time_stamp->nsecs = (int)(1000000000*(tvb_get_letohl(tvb, start+4)/4294967296.0));
if ((time_stamp->nsecs == 0) && (tmpsecs == 0)) {
//This is "NULL" time
time_stamp->secs = 0;
}
} else if (length == 4) {
/*
* Backwards compatibility.
*/
if (tmpsecs == 0) {
//This is "NULL" time
time_stamp->secs = 0;
}
time_stamp->nsecs = 0;
} else {
time_stamp->secs = 0;
time_stamp->nsecs = 0;
report_type_length_mismatch(tree, "an NTP time stamp", length, (length < 4));
}
break;
case ENC_TIME_TOD|ENC_BIG_ENDIAN:
/*
* S/3x0 and z/Architecture TOD clock time stamp,
* big-endian. The epoch is January 1, 1900,
* 00:00:00 (proleptic?) UTC.
*
* Only supported for absolute times.
*/
DISSECTOR_ASSERT(!is_relative);
DISSECTOR_ASSERT(length == 8);
if (length == 8) {
todusecs = tvb_get_ntoh64(tvb, start) >> 12;
time_stamp->secs = (time_t)((todusecs / 1000000) - EPOCH_DELTA_1900_01_01_00_00_00_UTC);
time_stamp->nsecs = (int)((todusecs % 1000000) * 1000);
} else {
time_stamp->secs = 0;
time_stamp->nsecs = 0;
report_type_length_mismatch(tree, "a TOD clock time stamp", length, (length < 4));
}
break;
case ENC_TIME_TOD|ENC_LITTLE_ENDIAN:
/*
* S/3x0 and z/Architecture TOD clock time stamp,
* little-endian. The epoch is January 1, 1900,
* 00:00:00 (proleptic?) UTC.
*
* Only supported for absolute times.
*/
DISSECTOR_ASSERT(!is_relative);
if (length == 8) {
todusecs = tvb_get_letoh64(tvb, start) >> 12 ;
time_stamp->secs = (time_t)((todusecs / 1000000) - EPOCH_DELTA_1900_01_01_00_00_00_UTC);
time_stamp->nsecs = (int)((todusecs % 1000000) * 1000);
} else {
time_stamp->secs = 0;
time_stamp->nsecs = 0;
report_type_length_mismatch(tree, "a TOD clock time stamp", length, (length < 4));
}
break;
case ENC_TIME_RTPS|ENC_BIG_ENDIAN:
/*
* Time stamp using the same seconds/fraction format
* as NTP, but with the origin of the time stamp being
* the UNIX epoch rather than the NTP epoch; big-
* endian.
*
* Only supported for absolute times.
*/
DISSECTOR_ASSERT(!is_relative);
if (length == 8) {
time_stamp->secs = (time_t)tvb_get_ntohl(tvb, start);
/*
* Convert 1/2^32s of a second to nanoseconds.
*/
time_stamp->nsecs = (int)(1000000000*(tvb_get_ntohl(tvb, start+4)/4294967296.0));
} else {
time_stamp->secs = 0;
time_stamp->nsecs = 0;
report_type_length_mismatch(tree, "an RTPS time stamp", length, (length < 4));
}
break;
case ENC_TIME_RTPS|ENC_LITTLE_ENDIAN:
/*
* Time stamp using the same seconds/fraction format
* as NTP, but with the origin of the time stamp being
* the UNIX epoch rather than the NTP epoch; little-
* endian.
*
* Only supported for absolute times.
*/
DISSECTOR_ASSERT(!is_relative);
if (length == 8) {
time_stamp->secs = (time_t)tvb_get_letohl(tvb, start);
/*
* Convert 1/2^32s of a second to nanoseconds.
*/
time_stamp->nsecs = (int)(1000000000*(tvb_get_letohl(tvb, start+4)/4294967296.0));
} else {
time_stamp->secs = 0;
time_stamp->nsecs = 0;
report_type_length_mismatch(tree, "an RTPS time stamp", length, (length < 4));
}
break;
case ENC_TIME_MIP6 | ENC_BIG_ENDIAN:
/*
* MIP6 time stamp, big-endian.
* A 64-bit unsigned integer field containing a timestamp. The
* value indicates the number of seconds since January 1, 1970,
* 00:00 UTC, by using a fixed point format. In this format, the
* integer number of seconds is contained in the first 48 bits of
* the field, and the remaining 16 bits indicate the number of
* 1/65536 fractions of a second.
* Only supported for absolute times.
*/
DISSECTOR_ASSERT(!is_relative);
if (length == 8) {
/* We need a temporary variable here so the casting and fractions
* of a second work correctly.
*/
tmp64secs = tvb_get_ntoh48(tvb, start);
tmpsecs = tvb_get_ntohs(tvb, start + 6);
tmpsecs <<= 16;
if ((tmp64secs == 0) && (tmpsecs == 0)) {
//This is "NULL" time
time_stamp->secs = 0;
time_stamp->nsecs = 0;
} else {
time_stamp->secs = (time_t)tmp64secs;
time_stamp->nsecs = (int)((tmpsecs / 4294967296.0) * 1000000000);
}
} else {
time_stamp->secs = 0;
time_stamp->nsecs = 0;
report_type_length_mismatch(tree, "an NTP time stamp", length, (length != 8));
}
break;
case ENC_TIME_SECS_USECS|ENC_BIG_ENDIAN:
/*
* If the length is 16, 8-byte seconds, followed
* by 8-byte fractional time in microseconds,
* both big-endian.
*
* If the length is 12, 8-byte seconds, followed
* by 4-byte fractional time in microseconds,
* both big-endian.
*
* If the length is 8, 4-byte seconds, followed
* by 4-byte fractional time in microseconds,
* both big-endian.
*
* For absolute times, the seconds are seconds
* since the UN*X epoch.
*/
if (length == 16) {
time_stamp->secs = (time_t)tvb_get_ntoh64(tvb, start);
time_stamp->nsecs = (guint32)tvb_get_ntoh64(tvb, start+8)*1000;
} else if (length == 12) {
time_stamp->secs = (time_t)tvb_get_ntoh64(tvb, start);
time_stamp->nsecs = tvb_get_ntohl(tvb, start+8)*1000;
} else if (length == 8) {
time_stamp->secs = (time_t)tvb_get_ntohl(tvb, start);
time_stamp->nsecs = tvb_get_ntohl(tvb, start+4)*1000;
} else {
time_stamp->secs = 0;
time_stamp->nsecs = 0;
report_type_length_mismatch(tree, "a timeval", length, (length < 4));
}
break;
case ENC_TIME_SECS_USECS|ENC_LITTLE_ENDIAN:
/*
* If the length is 16, 8-byte seconds, followed
* by 8-byte fractional time in microseconds,
* both little-endian.
*
* If the length is 12, 8-byte seconds, followed
* by 4-byte fractional time in microseconds,
* both little-endian.
*
* If the length is 8, 4-byte seconds, followed
* by 4-byte fractional time in microseconds,
* both little-endian.
*
* For absolute times, the seconds are seconds
* since the UN*X epoch.
*/
if (length == 16) {
time_stamp->secs = (time_t)tvb_get_letoh64(tvb, start);
time_stamp->nsecs = (guint32)tvb_get_letoh64(tvb, start+8)*1000;
} else if (length == 12) {
time_stamp->secs = (time_t)tvb_get_letoh64(tvb, start);
time_stamp->nsecs = tvb_get_letohl(tvb, start+8)*1000;
} else if (length == 8) {
time_stamp->secs = (time_t)tvb_get_letohl(tvb, start);
time_stamp->nsecs = tvb_get_letohl(tvb, start+4)*1000;
} else {
time_stamp->secs = 0;
time_stamp->nsecs = 0;
report_type_length_mismatch(tree, "a timeval", length, (length < 4));
}
break;
case ENC_TIME_SECS|ENC_BIG_ENDIAN:
case ENC_TIME_SECS|ENC_LITTLE_ENDIAN:
/*
* Seconds, 1 to 8 bytes.
* For absolute times, it's seconds since the
* UN*X epoch.
*/
if (length >= 1 && length <= 8) {
time_stamp->secs = (time_t)get_uint64_value(tree, tvb, start, length, encoding);
time_stamp->nsecs = 0;
} else {
time_stamp->secs = 0;
time_stamp->nsecs = 0;
report_type_length_mismatch(tree, "a time-in-seconds time stamp", length, (length < 4));
}
break;
case ENC_TIME_MSECS|ENC_BIG_ENDIAN:
case ENC_TIME_MSECS|ENC_LITTLE_ENDIAN:
/*
* Milliseconds, 1 to 8 bytes.
* For absolute times, it's milliseconds since the
* UN*X epoch.
*/
if (length >= 1 && length <= 8) {
guint64 msecs;
msecs = get_uint64_value(tree, tvb, start, length, encoding);
time_stamp->secs = (time_t)(msecs / 1000);
time_stamp->nsecs = (int)(msecs % 1000)*1000000;
} else {
time_stamp->secs = 0;
time_stamp->nsecs = 0;
report_type_length_mismatch(tree, "a time-in-milliseconds time stamp", length, (length < 4));
}
break;
case ENC_TIME_USECS|ENC_BIG_ENDIAN:
case ENC_TIME_USECS|ENC_LITTLE_ENDIAN:
/*
* Microseconds, 1 to 8 bytes.
* For absolute times, it's microseconds since the
* UN*X epoch.
*/
if (length >= 1 && length <= 8) {
guint64 usecs;
usecs = get_uint64_value(tree, tvb, start, length, encoding);
time_stamp->secs = (time_t)(usecs / 1000000);
time_stamp->nsecs = (int)(usecs % 1000000)*1000;
} else {
time_stamp->secs = 0;
time_stamp->nsecs = 0;
report_type_length_mismatch(tree, "a time-in-microseconds time stamp", length, (length < 4));
}
break;
case ENC_TIME_NSECS|ENC_BIG_ENDIAN:
case ENC_TIME_NSECS|ENC_LITTLE_ENDIAN:
/*
* nanoseconds, 1 to 8 bytes.
* For absolute times, it's nanoseconds since the
* UN*X epoch.
*/
if (length >= 1 && length <= 8) {
guint64 nsecs;
nsecs = get_uint64_value(tree, tvb, start, length, encoding);
time_stamp->secs = (time_t)(nsecs / 1000000000);
time_stamp->nsecs = (int)(nsecs % 1000000000);
} else {
time_stamp->secs = 0;
time_stamp->nsecs = 0;
report_type_length_mismatch(tree, "a time-in-nanoseconds time stamp", length, (length < 4));
}
break;
case ENC_TIME_RFC_3971|ENC_BIG_ENDIAN:
/*
* 1/64ths of a second since the UN*X epoch,
* big-endian.
*
* Only supported for absolute times.
*/
DISSECTOR_ASSERT(!is_relative);
if (length == 8) {
/*
* The upper 48 bits are seconds since the
* UN*X epoch.
*/
time_stamp->secs = (time_t)tvb_get_ntoh48(tvb, start);
/*
* The lower 16 bits are 1/2^16s of a second;
* convert them to nanoseconds.
*
* XXX - this may give the impression of higher
* precision than you actually get.
*/
time_stamp->nsecs = (int)(1000000000*(tvb_get_ntohs(tvb, start+6)/65536.0));
} else {
time_stamp->secs = 0;
time_stamp->nsecs = 0;
report_type_length_mismatch(tree, "an RFC 3971-style time stamp", length, (length < 4));
}
break;
case ENC_TIME_RFC_3971|ENC_LITTLE_ENDIAN:
/*
* 1/64ths of a second since the UN*X epoch,
* little-endian.
*
* Only supported for absolute times.
*/
DISSECTOR_ASSERT(!is_relative);
if (length == 8) {
/*
* XXX - this is assuming that, if anybody
* were ever to use this format - RFC 3971
* doesn't, because that's an Internet
* protocol, and those use network byte
* order, i.e. big-endian - they'd treat it
* as a 64-bit count of 1/2^16s of a second,
* putting the upper 48 bits at the end.
*
* The lower 48 bits are seconds since the
* UN*X epoch.
*/
time_stamp->secs = (time_t)tvb_get_letoh48(tvb, start+2);
/*
* The upper 16 bits are 1/2^16s of a second;
* convert them to nanoseconds.
*
* XXX - this may give the impression of higher
* precision than you actually get.
*/
time_stamp->nsecs = (int)(1000000000*(tvb_get_letohs(tvb, start)/65536.0));
} else {
time_stamp->secs = 0;
time_stamp->nsecs = 0;
report_type_length_mismatch(tree, "an RFC 3971-style time stamp", length, (length < 4));
}
break;
case ENC_TIME_SECS_NTP|ENC_BIG_ENDIAN:
/*
* NTP time stamp, with 1-second resolution (i.e.,
* seconds since the NTP epoch), big-endian.
* Only supported for absolute times.
*/
DISSECTOR_ASSERT(!is_relative);
if (length == 4) {
/*
* We need a temporary variable here so the unsigned math
* works correctly (for years > 2036 according to RFC 2030
* chapter 3).
*
* If bit 0 is set, the UTC time is in the range 1968-2036 and
* UTC time is reckoned from 0h 0m 0s UTC on 1 January 1900.
* If bit 0 is not set, the time is in the range 2036-2104 and
* UTC time is reckoned from 6h 28m 16s UTC on 7 February 2036.
*/
tmpsecs = tvb_get_ntohl(tvb, start);
if ((tmpsecs & 0x80000000) != 0)
time_stamp->secs = (time_t)((gint64)tmpsecs - NTP_TIMEDIFF1900TO1970SEC);
else
time_stamp->secs = (time_t)((gint64)tmpsecs + NTP_TIMEDIFF1970TO2036SEC);
time_stamp->nsecs = 0;
} else {
time_stamp->secs = 0;
time_stamp->nsecs = 0;
report_type_length_mismatch(tree, "an NTP seconds-only time stamp", length, (length < 4));
}
break;
case ENC_TIME_SECS_NTP|ENC_LITTLE_ENDIAN:
/*
* NTP time stamp, with 1-second resolution (i.e.,
* seconds since the NTP epoch), little-endian.
* Only supported for absolute times.
*/
DISSECTOR_ASSERT(!is_relative);
/*
* We need a temporary variable here so the unsigned math
* works correctly (for years > 2036 according to RFC 2030
* chapter 3).
*
* If bit 0 is set, the UTC time is in the range 1968-2036 and
* UTC time is reckoned from 0h 0m 0s UTC on 1 January 1900.
* If bit 0 is not set, the time is in the range 2036-2104 and
* UTC time is reckoned from 6h 28m 16s UTC on 7 February 2036.
*/
if (length == 4) {
tmpsecs = tvb_get_letohl(tvb, start);
if ((tmpsecs & 0x80000000) != 0)
time_stamp->secs = (time_t)((gint64)tmpsecs - NTP_TIMEDIFF1900TO1970SEC);
else
time_stamp->secs = (time_t)((gint64)tmpsecs + NTP_TIMEDIFF1970TO2036SEC);
time_stamp->nsecs = 0;
} else {
time_stamp->secs = 0;
time_stamp->nsecs = 0;
report_type_length_mismatch(tree, "an NTP seconds-only time stamp", length, (length < 4));
}
break;
case ENC_TIME_MSEC_NTP | ENC_BIG_ENDIAN:
/*
* Milliseconds, 1 to 8 bytes.
* For absolute times, it's milliseconds since the
* NTP epoch.
*/
if (length >= 1 && length <= 8) {
guint64 msecs;
msecs = get_uint64_value(tree, tvb, start, length, encoding);
tmpsecs = (guint32)(msecs / 1000);
/*
* If bit 0 is set, the UTC time is in the range 1968-2036 and
* UTC time is reckoned from 0h 0m 0s UTC on 1 January 1900.
* If bit 0 is not set, the time is in the range 2036-2104 and
* UTC time is reckoned from 6h 28m 16s UTC on 7 February 2036.
*/
if ((tmpsecs & 0x80000000) != 0)
time_stamp->secs = (time_t)((gint64)tmpsecs - NTP_TIMEDIFF1900TO1970SEC);
else
time_stamp->secs = (time_t)((gint64)tmpsecs + NTP_TIMEDIFF1970TO2036SEC);
time_stamp->nsecs = (int)(msecs % 1000)*1000000;
}
else {
time_stamp->secs = 0;
time_stamp->nsecs = 0;
report_type_length_mismatch(tree, "a time-in-milliseconds NTP time stamp", length, (length < 4));
}
break;
case ENC_TIME_CLASSIC_MAC_OS_SECS|ENC_BIG_ENDIAN:
/*
* Classic Mac OS time stamps, big-endian.
* Only supported for absolute times.
*/
DISSECTOR_ASSERT(!is_relative);
if (length == 8) {
tmp64secs = tvb_get_ntoh64(tvb, start);
time_stamp->secs = (time_t)(gint64)(tmp64secs - EPOCH_DELTA_1904_01_01_00_00_00_UTC);
time_stamp->nsecs = 0;
} else if (length == 4) {
tmpsecs = tvb_get_ntohl(tvb, start);
time_stamp->secs = (time_t)(gint32)(tmpsecs - EPOCH_DELTA_1904_01_01_00_00_00_UTC);
time_stamp->nsecs = 0;
} else {
time_stamp->secs = 0;
time_stamp->nsecs = 0;
report_type_length_mismatch(tree, "an MP4 time stamp", length, (length < 4));
}
break;
default:
DISSECTOR_ASSERT_NOT_REACHED();
break;
}
}
static void
tree_data_add_maybe_interesting_field(tree_data_t *tree_data, field_info *fi)
{
const header_field_info *hfinfo = fi->hfinfo;
if (hfinfo->ref_type == HF_REF_TYPE_DIRECT) {
GPtrArray *ptrs = NULL;
if (tree_data->interesting_hfids == NULL) {
/* Initialize the hash because we now know that it is needed */
tree_data->interesting_hfids =
g_hash_table_new(g_direct_hash, NULL /* g_direct_equal */);
} else if (g_hash_table_size(tree_data->interesting_hfids)) {
ptrs = (GPtrArray *)g_hash_table_lookup(tree_data->interesting_hfids,
GINT_TO_POINTER(hfinfo->id));
}
if (!ptrs) {
/* First element triggers the creation of pointer array */
ptrs = g_ptr_array_new();
g_hash_table_insert(tree_data->interesting_hfids,
GINT_TO_POINTER(hfinfo->id), ptrs);
}
g_ptr_array_add(ptrs, fi);
}
}
/*
* Validates that field length bytes are available starting from
* start (pos/neg). Throws an exception if they aren't.
*/
static void
test_length(header_field_info *hfinfo, tvbuff_t *tvb,
gint start, gint length, const guint encoding)
{
gint size = length;
if (!tvb)
return;
if ((hfinfo->type == FT_STRINGZ) ||
((encoding & ENC_VARINT_MASK) &&
(FT_IS_UINT(hfinfo->type) || FT_IS_INT(hfinfo->type)))) {
/* If we're fetching until the end of the TVB, only validate
* that the offset is within range.
*/
if (length == -1)
size = 0;
}
tvb_ensure_bytes_exist(tvb, start, size);
}
static void
detect_trailing_stray_characters(guint encoding, const char *string, gint length, proto_item *pi)
{
gboolean found_stray_character = FALSE;
if (!string)
return;
switch (encoding & ENC_CHARENCODING_MASK) {
case ENC_ASCII:
case ENC_UTF_8:
for (gint i = (gint)strlen(string); i < length; i++) {
if (string[i] != '\0') {
found_stray_character = TRUE;
break;
}
}
break;
default:
break;
}
if (found_stray_character) {
expert_add_info(NULL, pi, &ei_string_trailing_characters);
}
}
static void
free_fvalue_cb(void *data)
{
fvalue_t *fv = (fvalue_t*)data;
fvalue_free(fv);
}
/* Add an item to a proto_tree, using the text label registered to that item;
the item is extracted from the tvbuff handed to it. */
static proto_item *
proto_tree_new_item(field_info *new_fi, proto_tree *tree,
tvbuff_t *tvb, gint start, gint length,
guint encoding)
{
proto_item *pi;
guint32 value, n;
guint64 value64;
ws_in4_addr ipv4_value;
float floatval;
double doubleval;
const char *stringval = NULL;
nstime_t time_stamp;
gboolean length_error;
/* Ensure that the newly created fvalue_t is freed if we throw an
* exception before adding it to the tree. (gcc creates clobbering
* when it optimizes the equivalent TRY..EXCEPT implementation.)
* XXX: Move the new_field_info() call inside here?
*/
CLEANUP_PUSH(free_fvalue_cb, new_fi->value);
switch (new_fi->hfinfo->type) {
case FT_NONE:
/* no value to set for FT_NONE */
break;
case FT_PROTOCOL:
proto_tree_set_protocol_tvb(new_fi, tvb, new_fi->hfinfo->name, length);
break;
case FT_BYTES:
proto_tree_set_bytes_tvb(new_fi, tvb, start, length);
break;
case FT_UINT_BYTES:
n = get_uint_value(tree, tvb, start, length, encoding);
proto_tree_set_bytes_tvb(new_fi, tvb, start + length, n);
/* Instead of calling proto_item_set_len(), since we don't yet
* have a proto_item, we set the field_info's length ourselves. */
new_fi->length = n + length;
break;
case FT_BOOLEAN:
/*
* Map all non-zero values to little-endian for
* backwards compatibility.
*/
if (encoding)
encoding = ENC_LITTLE_ENDIAN;
proto_tree_set_boolean(new_fi,
get_uint64_value(tree, tvb, start, length, encoding));
break;
case FT_CHAR:
/* XXX - make these just FT_UINT? */
case FT_UINT8:
case FT_UINT16:
case FT_UINT24:
case FT_UINT32:
if (encoding & ENC_VARINT_MASK) {
new_fi->length = tvb_get_varint(tvb, start, (length == -1) ? FT_VARINT_MAX_LEN : length, &value64, encoding);
value = (guint32)value64;
if (!(encoding & ENC_VARINT_QUIC)) {
new_fi->flags |= FI_VARINT;
}
}
else {
/*
* Map all non-zero values to little-endian for
* backwards compatibility.
*/
if (encoding)
encoding = ENC_LITTLE_ENDIAN;
value = get_uint_value(tree, tvb, start, length, encoding);
}
proto_tree_set_uint(new_fi, value);
break;
case FT_UINT40:
case FT_UINT48:
case FT_UINT56:
case FT_UINT64:
if (encoding & ENC_VARINT_MASK) {
new_fi->length = tvb_get_varint(tvb, start, (length == -1) ? FT_VARINT_MAX_LEN : length, &value64, encoding);
if (!(encoding & ENC_VARINT_QUIC)) {
new_fi->flags |= FI_VARINT;
}
}
else {
/*
* Map all other non-zero values to little-endian for
* backwards compatibility.
*/
if (encoding)
encoding = ENC_LITTLE_ENDIAN;
value64 = get_uint64_value(tree, tvb, start, length, encoding);
}
proto_tree_set_uint64(new_fi, value64);
break;
/* XXX - make these just FT_INT? */
case FT_INT8:
case FT_INT16:
case FT_INT24:
case FT_INT32:
/*
* Map all non-zero values to little-endian for
* backwards compatibility.
*/
if (encoding)
encoding = ENC_LITTLE_ENDIAN;
proto_tree_set_int(new_fi,
get_int_value(tree, tvb, start, length, encoding));
break;
case FT_INT40:
case FT_INT48:
case FT_INT56:
case FT_INT64:
/*
* Map all non-zero values to little-endian for
* backwards compatibility.
*/
if (encoding)
encoding = ENC_LITTLE_ENDIAN;
proto_tree_set_int64(new_fi,
get_int64_value(tree, tvb, start, length, encoding));
break;
case FT_IPv4:
/*
* Map all non-zero values to little-endian for
* backwards compatibility.
*/
if (encoding)
encoding = ENC_LITTLE_ENDIAN;
if (length != FT_IPv4_LEN) {
length_error = length < FT_IPv4_LEN ? TRUE : FALSE;
report_type_length_mismatch(tree, "an IPv4 address", length, length_error);
}
ipv4_value = tvb_get_ipv4(tvb, start);
/*
* NOTE: to support code written when
* proto_tree_add_item() took a gboolean as its
* last argument, with FALSE meaning "big-endian"
* and TRUE meaning "little-endian", we treat any
* non-zero value of "encoding" as meaning
* "little-endian".
*/
proto_tree_set_ipv4(new_fi, encoding ? GUINT32_SWAP_LE_BE(ipv4_value) : ipv4_value);
break;
case FT_IPXNET:
if (length != FT_IPXNET_LEN) {
length_error = length < FT_IPXNET_LEN ? TRUE : FALSE;
report_type_length_mismatch(tree, "an IPXNET address", length, length_error);
}
proto_tree_set_ipxnet(new_fi,
get_uint_value(tree, tvb, start, FT_IPXNET_LEN, ENC_BIG_ENDIAN));
break;
case FT_IPv6:
if (length != FT_IPv6_LEN) {
length_error = length < FT_IPv6_LEN ? TRUE : FALSE;
report_type_length_mismatch(tree, "an IPv6 address", length, length_error);
}
proto_tree_set_ipv6_tvb(new_fi, tvb, start, length);
break;
case FT_FCWWN:
if (length != FT_FCWWN_LEN) {
length_error = length < FT_FCWWN_LEN ? TRUE : FALSE;
report_type_length_mismatch(tree, "an FCWWN address", length, length_error);
}
proto_tree_set_fcwwn_tvb(new_fi, tvb, start, length);
break;
case FT_AX25:
if (length != 7) {
length_error = length < 7 ? TRUE : FALSE;
report_type_length_mismatch(tree, "an AX.25 address", length, length_error);
}
proto_tree_set_ax25_tvb(new_fi, tvb, start);
break;
case FT_VINES:
if (length != VINES_ADDR_LEN) {
length_error = length < VINES_ADDR_LEN ? TRUE : FALSE;
report_type_length_mismatch(tree, "a Vines address", length, length_error);
}
proto_tree_set_vines_tvb(new_fi, tvb, start);
break;
case FT_ETHER:
if (length != FT_ETHER_LEN) {
length_error = length < FT_ETHER_LEN ? TRUE : FALSE;
report_type_length_mismatch(tree, "a MAC address", length, length_error);
}
proto_tree_set_ether_tvb(new_fi, tvb, start);
break;
case FT_EUI64:
/*
* Map all non-zero values to little-endian for
* backwards compatibility.
*/
if (encoding)
encoding = ENC_LITTLE_ENDIAN;
if (length != FT_EUI64_LEN) {
length_error = length < FT_EUI64_LEN ? TRUE : FALSE;
report_type_length_mismatch(tree, "an EUI-64 address", length, length_error);
}
proto_tree_set_eui64_tvb(new_fi, tvb, start, encoding);
break;
case FT_GUID:
/*
* Map all non-zero values to little-endian for
* backwards compatibility.
*/
if (encoding)
encoding = ENC_LITTLE_ENDIAN;
if (length != FT_GUID_LEN) {
length_error = length < FT_GUID_LEN ? TRUE : FALSE;
report_type_length_mismatch(tree, "a GUID", length, length_error);
}
proto_tree_set_guid_tvb(new_fi, tvb, start, encoding);
break;
case FT_OID:
case FT_REL_OID:
proto_tree_set_oid_tvb(new_fi, tvb, start, length);
break;
case FT_SYSTEM_ID:
proto_tree_set_system_id_tvb(new_fi, tvb, start, length);
break;
case FT_FLOAT:
/*
* NOTE: to support code written when
* proto_tree_add_item() took a gboolean as its
* last argument, with FALSE meaning "big-endian"
* and TRUE meaning "little-endian", we treat any
* non-zero value of "encoding" as meaning
* "little-endian".
*
* At some point in the future, we might
* support non-IEEE-binary floating-point
* formats in the encoding as well
* (IEEE decimal, System/3x0, VAX).
*/
if (encoding)
encoding = ENC_LITTLE_ENDIAN;
if (length != 4) {
length_error = length < 4 ? TRUE : FALSE;
report_type_length_mismatch(tree, "a single-precision floating point number", length, length_error);
}
if (encoding)
floatval = tvb_get_letohieee_float(tvb, start);
else
floatval = tvb_get_ntohieee_float(tvb, start);
proto_tree_set_float(new_fi, floatval);
break;
case FT_DOUBLE:
/*
* NOTE: to support code written when
* proto_tree_add_item() took a gboolean as its
* last argument, with FALSE meaning "big-endian"
* and TRUE meaning "little-endian", we treat any
* non-zero value of "encoding" as meaning
* "little-endian".
*
* At some point in the future, we might
* support non-IEEE-binary floating-point
* formats in the encoding as well
* (IEEE decimal, System/3x0, VAX).
*/
if (encoding == TRUE)
encoding = ENC_LITTLE_ENDIAN;
if (length != 8) {
length_error = length < 8 ? TRUE : FALSE;
report_type_length_mismatch(tree, "a double-precision floating point number", length, length_error);
}
if (encoding)
doubleval = tvb_get_letohieee_double(tvb, start);
else
doubleval = tvb_get_ntohieee_double(tvb, start);
proto_tree_set_double(new_fi, doubleval);
break;
case FT_STRING:
stringval = get_string_value(PNODE_POOL(tree),
tvb, start, length, &length, encoding);
proto_tree_set_string(new_fi, stringval);
/* Instead of calling proto_item_set_len(), since we
* don't yet have a proto_item, we set the
* field_info's length ourselves.
*
* XXX - our caller can't use that length to
* advance an offset unless they arrange that
* there always be a protocol tree into which
* we're putting this item.
*/
new_fi->length = length;
break;
case FT_STRINGZ:
stringval = get_stringz_value(PNODE_POOL(tree),
tree, tvb, start, length, &length, encoding);
proto_tree_set_string(new_fi, stringval);
/* Instead of calling proto_item_set_len(),
* since we don't yet have a proto_item, we
* set the field_info's length ourselves.
*
* XXX - our caller can't use that length to
* advance an offset unless they arrange that
* there always be a protocol tree into which
* we're putting this item.
*/
new_fi->length = length;
break;
case FT_UINT_STRING:
/*
* NOTE: to support code written when
* proto_tree_add_item() took a gboolean as its
* last argument, with FALSE meaning "big-endian"
* and TRUE meaning "little-endian", if the
* encoding value is TRUE, treat that as
* ASCII with a little-endian length.
*
* This won't work for code that passes
* arbitrary non-zero values; that code
* will need to be fixed.
*/
if (encoding == TRUE)
encoding = ENC_ASCII|ENC_LITTLE_ENDIAN;
stringval = get_uint_string_value(PNODE_POOL(tree),
tree, tvb, start, length, &length, encoding);
proto_tree_set_string(new_fi, stringval);
/* Instead of calling proto_item_set_len(), since we
* don't yet have a proto_item, we set the
* field_info's length ourselves.
*
* XXX - our caller can't use that length to
* advance an offset unless they arrange that
* there always be a protocol tree into which
* we're putting this item.
*/
new_fi->length = length;
break;
case FT_STRINGZPAD:
stringval = get_stringzpad_value(PNODE_POOL(tree),
tvb, start, length, &length, encoding);
proto_tree_set_string(new_fi, stringval);
/* Instead of calling proto_item_set_len(), since we
* don't yet have a proto_item, we set the
* field_info's length ourselves.
*
* XXX - our caller can't use that length to
* advance an offset unless they arrange that
* there always be a protocol tree into which
* we're putting this item.
*/
new_fi->length = length;
break;
case FT_STRINGZTRUNC:
stringval = get_stringztrunc_value(PNODE_POOL(tree),
tvb, start, length, &length, encoding);
proto_tree_set_string(new_fi, stringval);
/* Instead of calling proto_item_set_len(), since we
* don't yet have a proto_item, we set the
* field_info's length ourselves.
*
* XXX - our caller can't use that length to
* advance an offset unless they arrange that
* there always be a protocol tree into which
* we're putting this item.
*/
new_fi->length = length;
break;
case FT_ABSOLUTE_TIME:
/*
* Absolute times can be in any of a number of
* formats, and they can be big-endian or
* little-endian.
*
* Historically FT_TIMEs were only timespecs;
* the only question was whether they were stored
* in big- or little-endian format.
*
* For backwards compatibility, we interpret an
* encoding of 1 as meaning "little-endian timespec",
* so that passing TRUE is interpreted as that.
*/
if (encoding == TRUE)
encoding = ENC_TIME_SECS_NSECS|ENC_LITTLE_ENDIAN;
get_time_value(tree, tvb, start, length, encoding, &time_stamp, FALSE);
proto_tree_set_time(new_fi, &time_stamp);
break;
case FT_RELATIVE_TIME:
/*
* Relative times can be in any of a number of
* formats, and they can be big-endian or
* little-endian.
*
* Historically FT_TIMEs were only timespecs;
* the only question was whether they were stored
* in big- or little-endian format.
*
* For backwards compatibility, we interpret an
* encoding of 1 as meaning "little-endian timespec",
* so that passing TRUE is interpreted as that.
*/
if (encoding == TRUE)
encoding = ENC_TIME_SECS_NSECS|ENC_LITTLE_ENDIAN;
get_time_value(tree, tvb, start, length, encoding, &time_stamp, TRUE);
proto_tree_set_time(new_fi, &time_stamp);
break;
case FT_IEEE_11073_SFLOAT:
if (encoding)
encoding = ENC_LITTLE_ENDIAN;
if (length != 2) {
length_error = length < 2 ? TRUE : FALSE;
report_type_length_mismatch(tree, "a IEEE 11073 SFLOAT", length, length_error);
}
fvalue_set_uinteger(new_fi->value, tvb_get_guint16(tvb, start, encoding));
break;
case FT_IEEE_11073_FLOAT:
if (encoding)
encoding = ENC_LITTLE_ENDIAN;
if (length != 4) {
length_error = length < 4 ? TRUE : FALSE;
report_type_length_mismatch(tree, "a IEEE 11073 FLOAT", length, length_error);
}
break;
default:
REPORT_DISSECTOR_BUG("field %s is of unknown type %d (%s)",
new_fi->hfinfo->abbrev,
new_fi->hfinfo->type,
ftype_name(new_fi->hfinfo->type));
break;
}
FI_SET_FLAG(new_fi, (encoding & ENC_LITTLE_ENDIAN) ? FI_LITTLE_ENDIAN : FI_BIG_ENDIAN);
/* Don't add new node to proto_tree until now so that any exceptions
* raised by a tvbuff access method doesn't leave junk in the proto_tree. */
/* XXX. wouldn't be better to add this item to tree, with some special
* flag (FI_EXCEPTION?) to know which item caused exception? For
* strings and bytes, we would have to set new_fi->value to something
* non-NULL, or otherwise ensure that proto_item_fill_display_label
* could handle NULL values. */
CLEANUP_POP
pi = proto_tree_add_node(tree, new_fi);
switch (new_fi->hfinfo->type) {
case FT_STRING:
/* XXX: trailing stray character detection should be done
* _before_ conversion to UTF-8, because conversion can change
* the length, or else get_string_length should return a value
* for the "length in bytes of the string after conversion
* including internal nulls." (Noting that we do, for other
* reasons, still need the "length in bytes in the field",
* especially for FT_STRINGZ.)
*
* This is true even for ASCII and UTF-8, because
* substituting REPLACEMENT CHARACTERS for illegal characters
* can also do so (and for UTF-8 possibly even make the
* string _shorter_).
*/
detect_trailing_stray_characters(encoding, stringval, length, pi);
break;
default:
break;
}
return pi;
}
proto_item *
proto_tree_add_item_ret_int(proto_tree *tree, int hfindex, tvbuff_t *tvb,
const gint start, gint length,
const guint encoding, gint32 *retval)
{
header_field_info *hfinfo;
field_info *new_fi;
gint32 value;
PROTO_REGISTRAR_GET_NTH(hfindex, hfinfo);
switch (hfinfo->type) {
case FT_INT8:
case FT_INT16:
case FT_INT24:
case FT_INT32:
break;
case FT_INT64:
REPORT_DISSECTOR_BUG("64-bit signed integer field %s used with proto_tree_add_item_ret_int()",
hfinfo->abbrev);
default:
REPORT_DISSECTOR_BUG("Non-signed-integer field %s used with proto_tree_add_item_ret_int()",
hfinfo->abbrev);
}
CHECK_FOR_ZERO_OR_MINUS_LENGTH_AND_CLEANUP(length,
{
if(retval)
{
*retval = 0;
}
} );
if (encoding & ENC_STRING) {
REPORT_DISSECTOR_BUG("wrong encoding");
}
/* I believe it's ok if this is called with a NULL tree */
value = get_int_value(tree, tvb, start, length, encoding);
if (retval) {
gint no_of_bits;
*retval = value;
if (hfinfo->bitmask) {
/* Mask out irrelevant portions */
*retval &= (guint32)(hfinfo->bitmask);
/* Shift bits */
*retval >>= hfinfo_bitshift(hfinfo);
}
no_of_bits = ws_count_ones(hfinfo->bitmask);
*retval = ws_sign_ext32(*retval, no_of_bits);
}
CHECK_FOR_NULL_TREE(tree);
TRY_TO_FAKE_THIS_ITEM(tree, hfinfo->id, hfinfo);
new_fi = new_field_info(tree, hfinfo, tvb, start, length);
proto_tree_set_int(new_fi, value);
new_fi->flags |= (encoding & ENC_LITTLE_ENDIAN) ? FI_LITTLE_ENDIAN : FI_BIG_ENDIAN;
return proto_tree_add_node(tree, new_fi);
}
proto_item *
proto_tree_add_item_ret_uint(proto_tree *tree, int hfindex, tvbuff_t *tvb,
const gint start, gint length,
const guint encoding, guint32 *retval)
{
header_field_info *hfinfo;
field_info *new_fi;
guint32 value;
PROTO_REGISTRAR_GET_NTH(hfindex, hfinfo);
switch (hfinfo->type) {
case FT_CHAR:
case FT_UINT8:
case FT_UINT16:
case FT_UINT24:
case FT_UINT32:
break;
default:
REPORT_DISSECTOR_BUG("field %s is not of type FT_CHAR, FT_UINT8, FT_UINT16, FT_UINT24, or FT_UINT32",
hfinfo->abbrev);
}
CHECK_FOR_ZERO_OR_MINUS_LENGTH_AND_CLEANUP(length,
{
if(retval)
{
*retval = 0;
}
} );
if (encoding & ENC_STRING) {
REPORT_DISSECTOR_BUG("wrong encoding");
}
/* I believe it's ok if this is called with a NULL tree */
/* XXX - modify if we ever support EBCDIC FT_CHAR */
if (encoding & ENC_VARINT_MASK) {
guint64 temp64;
tvb_get_varint(tvb, start, length, &temp64, encoding);
value = (guint32)temp64;
} else {
value = get_uint_value(tree, tvb, start, length, encoding);
}
if (retval) {
*retval = value;
if (hfinfo->bitmask) {
/* Mask out irrelevant portions */
*retval &= (guint32)(hfinfo->bitmask);
/* Shift bits */
*retval >>= hfinfo_bitshift(hfinfo);
}
}
CHECK_FOR_NULL_TREE(tree);
TRY_TO_FAKE_THIS_ITEM(tree, hfinfo->id, hfinfo);
new_fi = new_field_info(tree, hfinfo, tvb, start, length);
proto_tree_set_uint(new_fi, value);
new_fi->flags |= (encoding & ENC_LITTLE_ENDIAN) ? FI_LITTLE_ENDIAN : FI_BIG_ENDIAN;
if (encoding & (ENC_VARINT_PROTOBUF|ENC_VARINT_ZIGZAG|ENC_VARINT_SDNV)) {
new_fi->flags |= FI_VARINT;
}
return proto_tree_add_node(tree, new_fi);
}
/* Gets data from tvbuff, adds it to proto_tree, increments offset,
* and returns proto_item* and uint value retreived*/
proto_item *
ptvcursor_add_ret_uint(ptvcursor_t *ptvc, int hfindex, gint length,
const guint encoding, guint32 *retval)
{
field_info *new_fi;
header_field_info *hfinfo;
gint item_length;
int offset;
guint32 value;
offset = ptvc->offset;
PROTO_REGISTRAR_GET_NTH(hfindex, hfinfo);
switch (hfinfo->type) {
case FT_CHAR:
case FT_UINT8:
case FT_UINT16:
case FT_UINT24:
case FT_UINT32:
break;
default:
REPORT_DISSECTOR_BUG("field %s is not of type FT_CHAR, FT_UINT8, FT_UINT16, FT_UINT24, or FT_UINT32",
hfinfo->abbrev);
}
get_hfi_length(hfinfo, ptvc->tvb, offset, &length, &item_length, encoding);
test_length(hfinfo, ptvc->tvb, offset, item_length, encoding);
/* I believe it's ok if this is called with a NULL tree */
/* XXX - modify if we ever support EBCDIC FT_CHAR */
value = get_uint_value(ptvc->tree, ptvc->tvb, offset, item_length, encoding);
if (retval) {
*retval = value;
if (hfinfo->bitmask) {
/* Mask out irrelevant portions */
*retval &= (guint32)(hfinfo->bitmask);
/* Shift bits */
*retval >>= hfinfo_bitshift(hfinfo);
}
}
ptvc->offset += get_full_length(hfinfo, ptvc->tvb, offset, length,
item_length, encoding);
CHECK_FOR_NULL_TREE(ptvc->tree);
/* Coast clear. Try and fake it */
TRY_TO_FAKE_THIS_ITEM(ptvc->tree, hfindex, hfinfo);
new_fi = new_field_info(ptvc->tree, hfinfo, ptvc->tvb, offset, item_length);
return proto_tree_new_item(new_fi, ptvc->tree, ptvc->tvb,
offset, length, encoding);
}
/* Gets data from tvbuff, adds it to proto_tree, increments offset,
* and returns proto_item* and int value retreived*/
proto_item *
ptvcursor_add_ret_int(ptvcursor_t *ptvc, int hfindex, gint length,
const guint encoding, gint32 *retval)
{
field_info *new_fi;
header_field_info *hfinfo;
gint item_length;
int offset;
guint32 value;
offset = ptvc->offset;
PROTO_REGISTRAR_GET_NTH(hfindex, hfinfo);
switch (hfinfo->type) {
case FT_INT8:
case FT_INT16:
case FT_INT24:
case FT_INT32:
break;
default:
REPORT_DISSECTOR_BUG("field %s is not of type FT_INT8, FT_INT16, FT_INT24, or FT_INT32",
hfinfo->abbrev);
}
get_hfi_length(hfinfo, ptvc->tvb, offset, &length, &item_length, encoding);
test_length(hfinfo, ptvc->tvb, offset, item_length, encoding);
/* I believe it's ok if this is called with a NULL tree */
/* XXX - modify if we ever support EBCDIC FT_CHAR */
value = get_int_value(ptvc->tree, ptvc->tvb, offset, item_length, encoding);
if (retval) {
gint no_of_bits;
*retval = value;
if (hfinfo->bitmask) {
/* Mask out irrelevant portions */
*retval &= (guint32)(hfinfo->bitmask);
/* Shift bits */
*retval >>= hfinfo_bitshift(hfinfo);
}
no_of_bits = ws_count_ones(hfinfo->bitmask);
*retval = ws_sign_ext32(*retval, no_of_bits);
}
ptvc->offset += get_full_length(hfinfo, ptvc->tvb, offset, length,
item_length, encoding);
CHECK_FOR_NULL_TREE(ptvc->tree);
/* Coast clear. Try and fake it */
TRY_TO_FAKE_THIS_ITEM(ptvc->tree, hfindex, hfinfo);
new_fi = new_field_info(ptvc->tree, hfinfo, ptvc->tvb, offset, item_length);
return proto_tree_new_item(new_fi, ptvc->tree, ptvc->tvb,
offset, length, encoding);
}
/* Gets data from tvbuff, adds it to proto_tree, increments offset,
* and returns proto_item* and string value retreived */
proto_item*
ptvcursor_add_ret_string(ptvcursor_t* ptvc, int hf, gint length, const guint encoding, wmem_allocator_t *scope, const guint8 **retval)
{
header_field_info *hfinfo;
field_info *new_fi;
const guint8 *value;
gint item_length;
int offset;
offset = ptvc->offset;
PROTO_REGISTRAR_GET_NTH(hf, hfinfo);
switch (hfinfo->type) {
case FT_STRING:
value = get_string_value(scope, ptvc->tvb, offset, length, &item_length, encoding);
break;
case FT_STRINGZ:
value = get_stringz_value(scope, ptvc->tree, ptvc->tvb, offset, length, &item_length, encoding);
break;
case FT_UINT_STRING:
value = get_uint_string_value(scope, ptvc->tree, ptvc->tvb, offset, length, &item_length, encoding);
break;
case FT_STRINGZPAD:
value = get_stringzpad_value(scope, ptvc->tvb, offset, length, &item_length, encoding);
break;
case FT_STRINGZTRUNC:
value = get_stringztrunc_value(scope, ptvc->tvb, offset, length, &item_length, encoding);
break;
default:
REPORT_DISSECTOR_BUG("field %s is not of type FT_STRING, FT_STRINGZ, FT_UINT_STRING, FT_STRINGZPAD, or FT_STRINGZTRUNC",
hfinfo->abbrev);
}
if (retval)
*retval = value;
ptvc->offset += item_length;
CHECK_FOR_NULL_TREE(ptvc->tree);
TRY_TO_FAKE_THIS_ITEM(ptvc->tree, hfinfo->id, hfinfo);
new_fi = new_field_info(ptvc->tree, hfinfo, ptvc->tvb, offset, item_length);
return proto_tree_new_item(new_fi, ptvc->tree, ptvc->tvb,
offset, length, encoding);
}
/* Gets data from tvbuff, adds it to proto_tree, increments offset,
* and returns proto_item* and boolean value retreived */
proto_item*
ptvcursor_add_ret_boolean(ptvcursor_t* ptvc, int hfindex, gint length, const guint encoding, gboolean *retval)
{
header_field_info *hfinfo;
field_info *new_fi;
gint item_length;
int offset;
guint64 value, bitval;
offset = ptvc->offset;
PROTO_REGISTRAR_GET_NTH(hfindex, hfinfo);
if (hfinfo->type != FT_BOOLEAN) {
REPORT_DISSECTOR_BUG("field %s is not of type FT_BOOLEAN",
hfinfo->abbrev);
}
CHECK_FOR_ZERO_OR_MINUS_LENGTH_AND_CLEANUP(length,
{
if(retval)
{
*retval = FALSE;
}
} );
if (encoding & ENC_STRING) {
REPORT_DISSECTOR_BUG("wrong encoding");
}
get_hfi_length(hfinfo, ptvc->tvb, offset, &length, &item_length, encoding);
test_length(hfinfo, ptvc->tvb, offset, item_length, encoding);
/* I believe it's ok if this is called with a NULL tree */
value = get_uint64_value(ptvc->tree, ptvc->tvb, offset, length, encoding);
if (retval) {
bitval = value;
if (hfinfo->bitmask) {
/* Mask out irrelevant portions */
bitval &= hfinfo->bitmask;
}
*retval = (bitval != 0);
}
ptvc->offset += get_full_length(hfinfo, ptvc->tvb, offset, length,
item_length, encoding);
CHECK_FOR_NULL_TREE(ptvc->tree);
TRY_TO_FAKE_THIS_ITEM(ptvc->tree, hfinfo->id, hfinfo);
new_fi = new_field_info(ptvc->tree, hfinfo, ptvc->tvb, offset, item_length);
return proto_tree_new_item(new_fi, ptvc->tree, ptvc->tvb,
offset, length, encoding);
}
proto_item *
proto_tree_add_item_ret_uint64(proto_tree *tree, int hfindex, tvbuff_t *tvb,
const gint start, gint length, const guint encoding, guint64 *retval)
{
header_field_info *hfinfo;
field_info *new_fi;
guint64 value;
PROTO_REGISTRAR_GET_NTH(hfindex, hfinfo);
switch (hfinfo->type) {
case FT_UINT40:
case FT_UINT48:
case FT_UINT56:
case FT_UINT64:
break;
default:
REPORT_DISSECTOR_BUG("field %s is not of type FT_UINT40, FT_UINT48, FT_UINT56, or FT_UINT64",
hfinfo->abbrev);
}
CHECK_FOR_ZERO_OR_MINUS_LENGTH_AND_CLEANUP(length,
{
if(retval)
{
*retval = 0;
}
} );
if (encoding & ENC_STRING) {
REPORT_DISSECTOR_BUG("wrong encoding");
}
/* I believe it's ok if this is called with a NULL tree */
if (encoding & ENC_VARINT_MASK) {
tvb_get_varint(tvb, start, length, &value, encoding);
} else {
value = get_uint64_value(tree, tvb, start, length, encoding);
}
if (retval) {
*retval = value;
if (hfinfo->bitmask) {
/* Mask out irrelevant portions */
*retval &= hfinfo->bitmask;
/* Shift bits */
*retval >>= hfinfo_bitshift(hfinfo);
}
}
CHECK_FOR_NULL_TREE(tree);
TRY_TO_FAKE_THIS_ITEM(tree, hfinfo->id, hfinfo);
new_fi = new_field_info(tree, hfinfo, tvb, start, length);
proto_tree_set_uint64(new_fi, value);
new_fi->flags |= (encoding & ENC_LITTLE_ENDIAN) ? FI_LITTLE_ENDIAN : FI_BIG_ENDIAN;
if (encoding & (ENC_VARINT_PROTOBUF|ENC_VARINT_ZIGZAG|ENC_VARINT_SDNV)) {
new_fi->flags |= FI_VARINT;
}
return proto_tree_add_node(tree, new_fi);
}
proto_item *
proto_tree_add_item_ret_int64(proto_tree *tree, int hfindex, tvbuff_t *tvb,
const gint start, gint length, const guint encoding, gint64 *retval)
{
header_field_info *hfinfo;
field_info *new_fi;
gint64 value;
PROTO_REGISTRAR_GET_NTH(hfindex, hfinfo);
switch (hfinfo->type) {
case FT_INT40:
case FT_INT48:
case FT_INT56:
case FT_INT64:
break;
default:
REPORT_DISSECTOR_BUG("field %s is not of type FT_INT40, FT_INT48, FT_INT56, or FT_INT64",
hfinfo->abbrev);
}
CHECK_FOR_ZERO_OR_MINUS_LENGTH_AND_CLEANUP(length,
{
if(retval)
{
*retval = 0;
}
} );
if (encoding & ENC_STRING) {
REPORT_DISSECTOR_BUG("wrong encoding");
}
/* I believe it's ok if this is called with a NULL tree */
if (encoding & ENC_VARINT_MASK) {
tvb_get_varint(tvb, start, length, &value, encoding);
}
else {
value = get_int64_value(tree, tvb, start, length, encoding);
}
if (retval) {
*retval = value;
}
CHECK_FOR_NULL_TREE(tree);
TRY_TO_FAKE_THIS_ITEM(tree, hfinfo->id, hfinfo);
new_fi = new_field_info(tree, hfinfo, tvb, start, length);
proto_tree_set_int64(new_fi, value);
new_fi->flags |= (encoding & ENC_LITTLE_ENDIAN) ? FI_LITTLE_ENDIAN : FI_BIG_ENDIAN;
if (encoding & (ENC_VARINT_PROTOBUF|ENC_VARINT_ZIGZAG|ENC_VARINT_SDNV)) {
new_fi->flags |= FI_VARINT;
}
return proto_tree_add_node(tree, new_fi);
}
proto_item *
proto_tree_add_item_ret_varint(proto_tree *tree, int hfindex, tvbuff_t *tvb,
const gint start, gint length, const guint encoding, guint64 *retval, gint *lenretval)
{
header_field_info *hfinfo;
field_info *new_fi;
guint64 value;
PROTO_REGISTRAR_GET_NTH(hfindex, hfinfo);
if ((!FT_IS_INT(hfinfo->type)) && (!FT_IS_UINT(hfinfo->type))) {
REPORT_DISSECTOR_BUG("field %s is not of type FT_UINT or FT_INT",
hfinfo->abbrev);
}
/* length validation for native number encoding caught by get_uint64_value() */
/* length has to be -1 or > 0 regardless of encoding */
if (length == 0)
REPORT_DISSECTOR_BUG("Invalid length %d passed to proto_tree_add_item_ret_varint",
length);
if (encoding & ENC_STRING) {
REPORT_DISSECTOR_BUG("wrong encoding");
}
length = tvb_get_varint(tvb, start, (length == -1) ? FT_VARINT_MAX_LEN : length, &value, encoding);
if (retval) {
*retval = value;
if (hfinfo->bitmask) {
/* Mask out irrelevant portions */
*retval &= hfinfo->bitmask;
/* Shift bits */
*retval >>= hfinfo_bitshift(hfinfo);
}
}
if (lenretval) {
*lenretval = length;
}
CHECK_FOR_NULL_TREE(tree);
TRY_TO_FAKE_THIS_ITEM(tree, hfinfo->id, hfinfo);
new_fi = new_field_info(tree, hfinfo, tvb, start, length);
proto_tree_set_uint64(new_fi, value);
new_fi->flags |= (encoding & ENC_LITTLE_ENDIAN) ? FI_LITTLE_ENDIAN : FI_BIG_ENDIAN;
if (encoding & (ENC_VARINT_PROTOBUF|ENC_VARINT_ZIGZAG|ENC_VARINT_SDNV)) {
new_fi->flags |= FI_VARINT;
}
return proto_tree_add_node(tree, new_fi);
}
proto_item *
proto_tree_add_item_ret_boolean(proto_tree *tree, int hfindex, tvbuff_t *tvb,
const gint start, gint length,
const guint encoding, gboolean *retval)
{
header_field_info *hfinfo;
field_info *new_fi;
guint64 value, bitval;
PROTO_REGISTRAR_GET_NTH(hfindex, hfinfo);
if (hfinfo->type != FT_BOOLEAN) {
REPORT_DISSECTOR_BUG("field %s is not of type FT_BOOLEAN",
hfinfo->abbrev);
}
CHECK_FOR_ZERO_OR_MINUS_LENGTH_AND_CLEANUP(length,
{
if(retval)
{
*retval = FALSE;
}
} );
if (encoding & ENC_STRING) {
REPORT_DISSECTOR_BUG("wrong encoding");
}
/* I believe it's ok if this is called with a NULL tree */
value = get_uint64_value(tree, tvb, start, length, encoding);
if (retval) {
bitval = value;
if (hfinfo->bitmask) {
/* Mask out irrelevant portions */
bitval &= hfinfo->bitmask;
}
*retval = (bitval != 0);
}
CHECK_FOR_NULL_TREE(tree);
TRY_TO_FAKE_THIS_ITEM(tree, hfinfo->id, hfinfo);
new_fi = new_field_info(tree, hfinfo, tvb, start, length);
proto_tree_set_boolean(new_fi, value);
new_fi->flags |= (encoding & ENC_LITTLE_ENDIAN) ? FI_LITTLE_ENDIAN : FI_BIG_ENDIAN;
return proto_tree_add_node(tree, new_fi);
}
proto_item *
proto_tree_add_item_ret_float(proto_tree *tree, int hfindex, tvbuff_t *tvb,
const gint start, gint length,
const guint encoding, gfloat *retval)
{
header_field_info *hfinfo = proto_registrar_get_nth(hfindex);
field_info *new_fi;
gfloat value;
DISSECTOR_ASSERT_HINT(hfinfo != NULL, "Not passed hfi!");
if (hfinfo->type != FT_FLOAT) {
REPORT_DISSECTOR_BUG("field %s is not of type FT_FLOAT", hfinfo->abbrev);
}
if (length != 4) {
report_type_length_mismatch(tree, "a single-precision floating point number", length, TRUE);
}
/* treat any nonzero encoding as little endian for backwards compatibility */
value = encoding ? tvb_get_letohieee_float(tvb, start) : tvb_get_ntohieee_float(tvb, start);
if (retval) {
*retval = value;
}
CHECK_FOR_NULL_TREE(tree);
TRY_TO_FAKE_THIS_ITEM(tree, hfinfo->id, hfinfo);
new_fi = new_field_info(tree, hfinfo, tvb, start, length);
if (encoding) {
new_fi->flags |= FI_LITTLE_ENDIAN;
}
proto_tree_set_float(new_fi, value);
return proto_tree_add_node(tree, new_fi);
}
proto_item *
proto_tree_add_item_ret_double(proto_tree *tree, int hfindex, tvbuff_t *tvb,
const gint start, gint length,
const guint encoding, gdouble *retval)
{
header_field_info *hfinfo = proto_registrar_get_nth(hfindex);
field_info *new_fi;
gdouble value;
DISSECTOR_ASSERT_HINT(hfinfo != NULL, "Not passed hfi!");
if (hfinfo->type != FT_DOUBLE) {
REPORT_DISSECTOR_BUG("field %s is not of type FT_DOUBLE", hfinfo->abbrev);
}
if (length != 8) {
report_type_length_mismatch(tree, "a double-precision floating point number", length, TRUE);
}
/* treat any nonzero encoding as little endian for backwards compatibility */
value = encoding ? tvb_get_letohieee_double(tvb, start) : tvb_get_ntohieee_double(tvb, start);
if (retval) {
*retval = value;
}
CHECK_FOR_NULL_TREE(tree);
TRY_TO_FAKE_THIS_ITEM(tree, hfinfo->id, hfinfo);
new_fi = new_field_info(tree, hfinfo, tvb, start, length);
if (encoding) {
new_fi->flags |= FI_LITTLE_ENDIAN;
}
proto_tree_set_double(new_fi, value);
return proto_tree_add_node(tree, new_fi);
}
proto_item *
proto_tree_add_item_ret_ipv4(proto_tree *tree, int hfindex, tvbuff_t *tvb,
const gint start, gint length,
const guint encoding, ws_in4_addr *retval)
{
header_field_info *hfinfo;
field_info *new_fi;
ws_in4_addr value;
PROTO_REGISTRAR_GET_NTH(hfindex, hfinfo);
switch (hfinfo->type) {
case FT_IPv4:
break;
default:
REPORT_DISSECTOR_BUG("field %s is not of type FT_IPv4",
hfinfo->abbrev);
}
if (length != FT_IPv4_LEN)
REPORT_DISSECTOR_BUG("Invalid length %d passed to proto_tree_add_item_ret_ipv4",
length);
if (encoding & (ENC_STRING | ENC_VARINT_MASK)) {
REPORT_DISSECTOR_BUG("wrong encoding");
}
/*
* NOTE: to support code written when proto_tree_add_item() took
* a gboolean as its last argument, with FALSE meaning "big-endian"
* and TRUE meaning "little-endian", we treat any non-zero value
* of "encoding" as meaning "little-endian".
*/
value = tvb_get_ipv4(tvb, start);
if (encoding)
value = GUINT32_SWAP_LE_BE(value);
if (retval) {
*retval = value;
}
CHECK_FOR_NULL_TREE(tree);
TRY_TO_FAKE_THIS_ITEM(tree, hfinfo->id, hfinfo);
new_fi = new_field_info(tree, hfinfo, tvb, start, length);
proto_tree_set_ipv4(new_fi, value);
new_fi->flags |= encoding ? FI_LITTLE_ENDIAN : FI_BIG_ENDIAN;
return proto_tree_add_node(tree, new_fi);
}
proto_item *
proto_tree_add_item_ret_ipv6(proto_tree *tree, int hfindex, tvbuff_t *tvb,
const gint start, gint length,
const guint encoding, ws_in6_addr *addr)
{
header_field_info *hfinfo = proto_registrar_get_nth(hfindex);
field_info *new_fi;
DISSECTOR_ASSERT_HINT(hfinfo != NULL, "Not passed hfi!");
switch (hfinfo->type) {
case FT_IPv6:
break;
default:
REPORT_DISSECTOR_BUG("field %s is not of type FT_IPv6",
hfinfo->abbrev);
}
if (length != FT_IPv6_LEN)
REPORT_DISSECTOR_BUG("Invalid length %d passed to proto_tree_add_item_ret_ipv6",
length);
if (encoding) {
REPORT_DISSECTOR_BUG("Encodings not yet implemented for proto_tree_add_item_ret_ipv6");
}
tvb_get_ipv6(tvb, start, addr);
CHECK_FOR_NULL_TREE(tree);
TRY_TO_FAKE_THIS_ITEM(tree, hfinfo->id, hfinfo);
new_fi = new_field_info(tree, hfinfo, tvb, start, length);
proto_tree_set_ipv6(new_fi, addr);
return proto_tree_add_node(tree, new_fi);
}
proto_item *
proto_tree_add_item_ret_ether(proto_tree *tree, int hfindex, tvbuff_t *tvb,
const gint start, gint length, const guint encoding, guint8 *retval) {
header_field_info *hfinfo = proto_registrar_get_nth(hfindex);
field_info *new_fi;
DISSECTOR_ASSERT_HINT(hfinfo != NULL, "Not passed hfi!");
switch (hfinfo->type) {
case FT_ETHER:
break;
default:
REPORT_DISSECTOR_BUG("field %s is not of type FT_ETHER",
hfinfo->abbrev);
}
if (length != FT_ETHER_LEN)
REPORT_DISSECTOR_BUG("Invalid length %d passed to proto_tree_add_item_ret_ether",
length);
if (encoding) {
REPORT_DISSECTOR_BUG("Encodings not yet implemented for proto_tree_add_item_ret_ether");
}
tvb_memcpy(tvb, retval, start, length);
CHECK_FOR_NULL_TREE(tree);
TRY_TO_FAKE_THIS_ITEM(tree, hfinfo->id, hfinfo);
new_fi = new_field_info(tree, hfinfo, tvb, start, length);
proto_tree_set_ether(new_fi, retval);
return proto_tree_add_node(tree, new_fi);
}
proto_item *
proto_tree_add_item_ret_string_and_length(proto_tree *tree, int hfindex,
tvbuff_t *tvb,
const gint start, gint length,
const guint encoding,
wmem_allocator_t *scope,
const guint8 **retval,
gint *lenretval)
{
proto_item *pi;
header_field_info *hfinfo;
field_info *new_fi;
const guint8 *value;
PROTO_REGISTRAR_GET_NTH(hfindex, hfinfo);
switch (hfinfo->type) {
case FT_STRING:
value = get_string_value(scope, tvb, start, length, lenretval, encoding);
break;
case FT_STRINGZ:
value = get_stringz_value(scope, tree, tvb, start, length, lenretval, encoding);
break;
case FT_UINT_STRING:
value = get_uint_string_value(scope, tree, tvb, start, length, lenretval, encoding);
break;
case FT_STRINGZPAD:
value = get_stringzpad_value(scope, tvb, start, length, lenretval, encoding);
break;
case FT_STRINGZTRUNC:
value = get_stringztrunc_value(scope, tvb, start, length, lenretval, encoding);
break;
default:
REPORT_DISSECTOR_BUG("field %s is not of type FT_STRING, FT_STRINGZ, FT_UINT_STRING, FT_STRINGZPAD, or FT_STRINGZTRUNC",
hfinfo->abbrev);
}
if (retval)
*retval = value;
CHECK_FOR_NULL_TREE(tree);
TRY_TO_FAKE_THIS_ITEM(tree, hfinfo->id, hfinfo);
new_fi = new_field_info(tree, hfinfo, tvb, start, *lenretval);
proto_tree_set_string(new_fi, value);
new_fi->flags |= (encoding & ENC_LITTLE_ENDIAN) ? FI_LITTLE_ENDIAN : FI_BIG_ENDIAN;
pi = proto_tree_add_node(tree, new_fi);
switch (hfinfo->type) {
case FT_STRINGZ:
case FT_STRINGZPAD:
case FT_STRINGZTRUNC:
case FT_UINT_STRING:
break;
case FT_STRING:
detect_trailing_stray_characters(encoding, value, length, pi);
break;
default:
ws_assert_not_reached();
}
return pi;
}
proto_item *
proto_tree_add_item_ret_string(proto_tree *tree, int hfindex, tvbuff_t *tvb,
const gint start, gint length,
const guint encoding, wmem_allocator_t *scope,
const guint8 **retval)
{
return proto_tree_add_item_ret_string_and_length(tree, hfindex,
tvb, start, length, encoding, scope, retval, &length);
}
proto_item *
proto_tree_add_item_ret_display_string_and_length(proto_tree *tree, int hfindex,
tvbuff_t *tvb,
const gint start, gint length,
const guint encoding,
wmem_allocator_t *scope,
char **retval,
gint *lenretval)
{
proto_item *pi;
header_field_info *hfinfo;
field_info *new_fi;
const guint8 *value;
guint32 n = 0;
PROTO_REGISTRAR_GET_NTH(hfindex, hfinfo);
switch (hfinfo->type) {
case FT_STRING:
value = get_string_value(scope, tvb, start, length, lenretval, encoding);
*retval = wmem_alloc(scope, ITEM_LABEL_LENGTH);
ws_label_strcpy(*retval, ITEM_LABEL_LENGTH, 0, value, label_strcat_flags(hfinfo));
break;
case FT_STRINGZ:
value = get_stringz_value(scope, tree, tvb, start, length, lenretval, encoding);
*retval = wmem_alloc(scope, ITEM_LABEL_LENGTH);
ws_label_strcpy(*retval, ITEM_LABEL_LENGTH, 0, value, label_strcat_flags(hfinfo));
break;
case FT_UINT_STRING:
value = get_uint_string_value(scope, tree, tvb, start, length, lenretval, encoding);
*retval = wmem_alloc(scope, ITEM_LABEL_LENGTH);
ws_label_strcpy(*retval, ITEM_LABEL_LENGTH, 0, value, label_strcat_flags(hfinfo));
break;
case FT_STRINGZPAD:
value = get_stringzpad_value(scope, tvb, start, length, lenretval, encoding);
*retval = wmem_alloc(scope, ITEM_LABEL_LENGTH);
ws_label_strcpy(*retval, ITEM_LABEL_LENGTH, 0, value, label_strcat_flags(hfinfo));
break;
case FT_STRINGZTRUNC:
value = get_stringztrunc_value(scope, tvb, start, length, lenretval, encoding);
*retval = wmem_alloc(scope, ITEM_LABEL_LENGTH);
ws_label_strcpy(*retval, ITEM_LABEL_LENGTH, 0, value, label_strcat_flags(hfinfo));
break;
case FT_BYTES:
tvb_ensure_bytes_exist(tvb, start, length);
value = tvb_get_ptr(tvb, start, length);
*retval = format_bytes_hfinfo(scope, hfinfo, value, length);
*lenretval = length;
break;
case FT_UINT_BYTES:
n = get_uint_value(tree, tvb, start, length, encoding);
tvb_ensure_bytes_exist(tvb, start + length, n);
value = tvb_get_ptr(tvb, start + length, n);
*retval = format_bytes_hfinfo(scope, hfinfo, value, n);
*lenretval = length + n;
break;
default:
REPORT_DISSECTOR_BUG("field %s is not of type FT_STRING, FT_STRINGZ, FT_UINT_STRING, FT_STRINGZPAD, FT_STRINGZTRUNC, FT_BYTES, or FT_UINT_BYTES",
hfinfo->abbrev);
}
CHECK_FOR_NULL_TREE(tree);
TRY_TO_FAKE_THIS_ITEM(tree, hfinfo->id, hfinfo);
new_fi = new_field_info(tree, hfinfo, tvb, start, *lenretval);
switch (hfinfo->type) {
case FT_STRING:
case FT_STRINGZ:
case FT_UINT_STRING:
case FT_STRINGZPAD:
case FT_STRINGZTRUNC:
proto_tree_set_string(new_fi, value);
break;
case FT_BYTES:
proto_tree_set_bytes(new_fi, value, length);
break;
case FT_UINT_BYTES:
proto_tree_set_bytes(new_fi, value, n);
break;
default:
ws_assert_not_reached();
}
new_fi->flags |= (encoding & ENC_LITTLE_ENDIAN) ? FI_LITTLE_ENDIAN : FI_BIG_ENDIAN;
pi = proto_tree_add_node(tree, new_fi);
switch (hfinfo->type) {
case FT_STRINGZ:
case FT_STRINGZPAD:
case FT_STRINGZTRUNC:
case FT_UINT_STRING:
break;
case FT_STRING:
detect_trailing_stray_characters(encoding, value, length, pi);
break;
case FT_BYTES:
case FT_UINT_BYTES:
break;
default:
ws_assert_not_reached();
}
return pi;
}
proto_item *
proto_tree_add_item_ret_display_string(proto_tree *tree, int hfindex,
tvbuff_t *tvb,
const gint start, gint length,
const guint encoding,
wmem_allocator_t *scope,
char **retval)
{
return proto_tree_add_item_ret_display_string_and_length(tree, hfindex,
tvb, start, length, encoding, scope, retval, &length);
}
proto_item *
proto_tree_add_item_ret_time_string(proto_tree *tree, int hfindex,
tvbuff_t *tvb,
const gint start, gint length, const guint encoding,
wmem_allocator_t *scope, char **retval)
{
header_field_info *hfinfo;
field_info *new_fi;
nstime_t time_stamp;
PROTO_REGISTRAR_GET_NTH(hfindex, hfinfo);
switch (hfinfo->type) {
case FT_ABSOLUTE_TIME:
get_time_value(tree, tvb, start, length, encoding, &time_stamp, FALSE);
*retval = abs_time_to_str(scope, &time_stamp, hfinfo->display, TRUE);
break;
case FT_RELATIVE_TIME:
get_time_value(tree, tvb, start, length, encoding, &time_stamp, TRUE);
*retval = rel_time_to_secs_str(scope, &time_stamp);
break;
default:
REPORT_DISSECTOR_BUG("field %s is not of type FT_ABSOLUTE_TIME or FT_RELATIVE_TIME",
hfinfo->abbrev);
}
CHECK_FOR_NULL_TREE(tree);
TRY_TO_FAKE_THIS_ITEM(tree, hfinfo->id, hfinfo);
new_fi = new_field_info(tree, hfinfo, tvb, start, length);
switch (hfinfo->type) {
case FT_ABSOLUTE_TIME:
case FT_RELATIVE_TIME:
proto_tree_set_time(new_fi, &time_stamp);
break;
default:
ws_assert_not_reached();
}
new_fi->flags |= (encoding & ENC_LITTLE_ENDIAN) ? FI_LITTLE_ENDIAN : FI_BIG_ENDIAN;
return proto_tree_add_node(tree, new_fi);
}
/* Gets data from tvbuff, adds it to proto_tree, increments offset,
and returns proto_item* */
proto_item *
ptvcursor_add(ptvcursor_t *ptvc, int hfindex, gint length,
const guint encoding)
{
field_info *new_fi;
header_field_info *hfinfo;
gint item_length;
int offset;
offset = ptvc->offset;
PROTO_REGISTRAR_GET_NTH(hfindex, hfinfo);
get_hfi_length(hfinfo, ptvc->tvb, offset, &length, &item_length, encoding);
test_length(hfinfo, ptvc->tvb, offset, item_length, encoding);
ptvc->offset += get_full_length(hfinfo, ptvc->tvb, offset, length,
item_length, encoding);
CHECK_FOR_NULL_TREE(ptvc->tree);
/* Coast clear. Try and fake it */
TRY_TO_FAKE_THIS_ITEM(ptvc->tree, hfindex, hfinfo);
new_fi = new_field_info(ptvc->tree, hfinfo, ptvc->tvb, offset, item_length);
return proto_tree_new_item(new_fi, ptvc->tree, ptvc->tvb,
offset, length, encoding);
}
/* Add an item to a proto_tree, using the text label registered to that item;
the item is extracted from the tvbuff handed to it. */
proto_item *
proto_tree_add_item_new(proto_tree *tree, header_field_info *hfinfo, tvbuff_t *tvb,
const gint start, gint length, const guint encoding)
{
field_info *new_fi;
gint item_length;
DISSECTOR_ASSERT_HINT(hfinfo != NULL, "Not passed hfi!");
get_hfi_length(hfinfo, tvb, start, &length, &item_length, encoding);
test_length(hfinfo, tvb, start, item_length, encoding);
CHECK_FOR_NULL_TREE(tree);
TRY_TO_FAKE_THIS_ITEM(tree, hfinfo->id, hfinfo);
new_fi = new_field_info(tree, hfinfo, tvb, start, item_length);
return proto_tree_new_item(new_fi, tree, tvb, start, length, encoding);
}
proto_item *
proto_tree_add_item(proto_tree *tree, int hfindex, tvbuff_t *tvb,
const gint start, gint length, const guint encoding)
{
register header_field_info *hfinfo;
PROTO_REGISTRAR_GET_NTH(hfindex, hfinfo);
return proto_tree_add_item_new(tree, hfinfo, tvb, start, length, encoding);
}
/* Add an item to a proto_tree, using the text label registered to that item;
the item is extracted from the tvbuff handed to it.
Return the length of the item through the pointer. */
proto_item *
proto_tree_add_item_new_ret_length(proto_tree *tree, header_field_info *hfinfo,
tvbuff_t *tvb, const gint start,
gint length, const guint encoding,
gint *lenretval)
{
field_info *new_fi;
gint item_length;
proto_item *item;
DISSECTOR_ASSERT_HINT(hfinfo != NULL, "Not passed hfi!");
get_hfi_length(hfinfo, tvb, start, &length, &item_length, encoding);
test_length(hfinfo, tvb, start, item_length, encoding);
if (!tree) {
/*
* We need to get the correct item length here.
* That's normally done by proto_tree_new_item(),
* but we won't be calling it.
*/
*lenretval = get_full_length(hfinfo, tvb, start, length,
item_length, encoding);
return NULL;
}
TRY_TO_FAKE_THIS_ITEM_OR_FREE(tree, hfinfo->id, hfinfo, {
/*
* Even if the tree item is not referenced (and thus faked),
* the caller must still be informed of the actual length.
*/
*lenretval = get_full_length(hfinfo, tvb, start, length,
item_length, encoding);
});
new_fi = new_field_info(tree, hfinfo, tvb, start, item_length);
item = proto_tree_new_item(new_fi, tree, tvb, start, length, encoding);
*lenretval = new_fi->length;
return item;
}
proto_item *
proto_tree_add_item_ret_length(proto_tree *tree, int hfindex, tvbuff_t *tvb,
const gint start, gint length,
const guint encoding, gint *lenretval)
{
register header_field_info *hfinfo;
PROTO_REGISTRAR_GET_NTH(hfindex, hfinfo);
return proto_tree_add_item_new_ret_length(tree, hfinfo, tvb, start, length, encoding, lenretval);
}
/* which FT_ types can use proto_tree_add_bytes_item() */
static inline gboolean
validate_proto_tree_add_bytes_ftype(const enum ftenum type)
{
return (type == FT_BYTES ||
type == FT_UINT_BYTES ||
type == FT_OID ||
type == FT_REL_OID ||
type == FT_SYSTEM_ID );
}
/* Note: this does no validation that the byte array of an FT_OID or
FT_REL_OID is actually valid; and neither does proto_tree_add_item(),
so I think it's ok to continue not validating it?
*/
proto_item *
proto_tree_add_bytes_item(proto_tree *tree, int hfindex, tvbuff_t *tvb,
const gint start, gint length, const guint encoding,
GByteArray *retval, gint *endoff, gint *err)
{
field_info *new_fi;
GByteArray *bytes = retval;
GByteArray *created_bytes = NULL;
gboolean failed = FALSE;
guint32 n = 0;
header_field_info *hfinfo;
gboolean generate = (bytes || tree) ? TRUE : FALSE;
PROTO_REGISTRAR_GET_NTH(hfindex, hfinfo);
DISSECTOR_ASSERT_HINT(hfinfo != NULL, "Not passed hfi!");
DISSECTOR_ASSERT_HINT(validate_proto_tree_add_bytes_ftype(hfinfo->type),
"Called proto_tree_add_bytes_item but not a bytes-based FT_XXX type");
CHECK_FOR_ZERO_OR_MINUS_LENGTH(length);
if (encoding & ENC_STR_NUM) {
REPORT_DISSECTOR_BUG("Decoding number strings for byte arrays is not supported");
}
if (generate && (encoding & ENC_STR_HEX)) {
if (hfinfo->type == FT_UINT_BYTES) {
/* can't decode FT_UINT_BYTES from strings */
REPORT_DISSECTOR_BUG("proto_tree_add_bytes_item called for "
"FT_UINT_BYTES type, but as ENC_STR_HEX");
}
if (!bytes) {
/* caller doesn't care about return value, but we need it to
call tvb_get_string_bytes() and set the tree later */
bytes = created_bytes = g_byte_array_new();
}
/*
* bytes might be NULL after this, but can't add expert
* error until later; if it's NULL, just note that
* it failed.
*/
bytes = tvb_get_string_bytes(tvb, start, length, encoding, bytes, endoff);
if (bytes == NULL)
failed = TRUE;
}
else if (generate) {
tvb_ensure_bytes_exist(tvb, start, length);
if (hfinfo->type == FT_UINT_BYTES) {
n = length; /* n is now the "header" length */
length = get_uint_value(tree, tvb, start, n, encoding);
/* length is now the value's length; only store the value in the array */
tvb_ensure_bytes_exist(tvb, start + n, length);
if (!bytes) {
/* caller doesn't care about return value, but
* we may need it to set the tree later */
bytes = created_bytes = g_byte_array_new();
}
g_byte_array_append(bytes, tvb_get_ptr(tvb, start + n, length), length);
}
else if (length > 0) {
if (!bytes) {
/* caller doesn't care about return value, but
* we may need it to set the tree later */
bytes = created_bytes = g_byte_array_new();
}
g_byte_array_append(bytes, tvb_get_ptr(tvb, start, length), length);
}
if (endoff)
*endoff = start + n + length;
}
if (err)
*err = failed ? EINVAL : 0;
CHECK_FOR_NULL_TREE_AND_FREE(tree,
{
if (created_bytes)
g_byte_array_free(created_bytes, TRUE);
created_bytes = NULL;
bytes = NULL;
} );
TRY_TO_FAKE_THIS_ITEM_OR_FREE(tree, hfinfo->id, hfinfo,
{
if (created_bytes)
g_byte_array_free(created_bytes, TRUE);
created_bytes = NULL;
bytes = NULL;
} );
/* n will be zero except when it's a FT_UINT_BYTES */
new_fi = new_field_info(tree, hfinfo, tvb, start, n + length);
if (encoding & ENC_STRING) {
if (failed)
expert_add_info(NULL, tree, &ei_byte_array_string_decoding_failed_error);
if (bytes)
proto_tree_set_bytes_gbytearray(new_fi, bytes);
else
proto_tree_set_bytes(new_fi, NULL, 0);
if (created_bytes)
g_byte_array_free(created_bytes, TRUE);
}
else {
/* n will be zero except when it's a FT_UINT_BYTES */
proto_tree_set_bytes_tvb(new_fi, tvb, start + n, length);
/* XXX: If we have a non-NULL tree but NULL retval, we don't
* use the byte array created above in this case.
*/
if (created_bytes)
g_byte_array_free(created_bytes, TRUE);
FI_SET_FLAG(new_fi,
(encoding & ENC_LITTLE_ENDIAN) ? FI_LITTLE_ENDIAN : FI_BIG_ENDIAN);
}
return proto_tree_add_node(tree, new_fi);
}
proto_item *
proto_tree_add_time_item(proto_tree *tree, int hfindex, tvbuff_t *tvb,
const gint start, gint length, const guint encoding,
nstime_t *retval, gint *endoff, gint *err)
{
field_info *new_fi;
nstime_t time_stamp;
gint saved_err = 0;
header_field_info *hfinfo;
PROTO_REGISTRAR_GET_NTH(hfindex, hfinfo);
DISSECTOR_ASSERT_HINT(hfinfo != NULL, "Not passed hfi!");
CHECK_FOR_ZERO_OR_MINUS_LENGTH_AND_CLEANUP(length,
{
if(retval)
{
nstime_set_zero(retval);
}
} );
nstime_set_zero(&time_stamp);
if (encoding & ENC_STR_TIME_MASK) {
DISSECTOR_ASSERT_FIELD_TYPE(hfinfo, FT_ABSOLUTE_TIME);
/* The only string format that could be a relative time is
* ENC_ISO_8601_TIME, and that is treated as an absolute time
* relative to "now" currently.
*/
if (!tvb_get_string_time(tvb, start, length, encoding, &time_stamp, endoff))
saved_err = EINVAL;
}
else {
DISSECTOR_ASSERT_FIELD_TYPE_IS_TIME(hfinfo);
const gboolean is_relative = (hfinfo->type == FT_RELATIVE_TIME) ? TRUE : FALSE;
tvb_ensure_bytes_exist(tvb, start, length);
get_time_value(tree, tvb, start, length, encoding, &time_stamp, is_relative);
if (endoff) *endoff = start + length;
}
if (err) *err = saved_err;
if (retval) {
retval->secs = time_stamp.secs;
retval->nsecs = time_stamp.nsecs;
}
CHECK_FOR_NULL_TREE(tree);
TRY_TO_FAKE_THIS_ITEM(tree, hfinfo->id, hfinfo);
new_fi = new_field_info(tree, hfinfo, tvb, start, length);
proto_tree_set_time(new_fi, &time_stamp);
if (encoding & ENC_STRING) {
if (saved_err)
expert_add_info(NULL, tree, &ei_date_time_string_decoding_failed_error);
}
else {
FI_SET_FLAG(new_fi,
(encoding & ENC_LITTLE_ENDIAN) ? FI_LITTLE_ENDIAN : FI_BIG_ENDIAN);
}
return proto_tree_add_node(tree, new_fi);
}
/* Add a FT_NONE to a proto_tree */
proto_item *
proto_tree_add_none_format(proto_tree *tree, const int hfindex, tvbuff_t *tvb,
const gint start, gint length, const char *format,
...)
{
proto_item *pi;
va_list ap;
header_field_info *hfinfo;
CHECK_FOR_NULL_TREE(tree);
TRY_TO_FAKE_THIS_ITEM(tree, hfindex, hfinfo);
DISSECTOR_ASSERT_FIELD_TYPE(hfinfo, FT_NONE);
pi = proto_tree_add_pi(tree, hfinfo, tvb, start, &length);
TRY_TO_FAKE_THIS_REPR(pi);
va_start(ap, format);
proto_tree_set_representation(pi, format, ap);
va_end(ap);
/* no value to set for FT_NONE */
return pi;
}
/* Gets data from tvbuff, adds it to proto_tree, *DOES NOT* increment
* offset, and returns proto_item* */
proto_item *
ptvcursor_add_no_advance(ptvcursor_t* ptvc, int hf, gint length,
const guint encoding)
{
proto_item *item;
item = proto_tree_add_item(ptvc->tree, hf, ptvc->tvb, ptvc->offset,
length, encoding);
return item;
}
/* Advance the ptvcursor's offset within its tvbuff without
* adding anything to the proto_tree. */
void
ptvcursor_advance(ptvcursor_t* ptvc, gint length)
{
ptvc->offset += length;
}
static void
proto_tree_set_protocol_tvb(field_info *fi, tvbuff_t *tvb, const char* field_data, int length)
{
fvalue_set_protocol(fi->value, tvb, field_data, length);
}
/* Add a FT_PROTOCOL to a proto_tree */
proto_item *
proto_tree_add_protocol_format(proto_tree *tree, int hfindex, tvbuff_t *tvb,
gint start, gint length, const char *format, ...)
{
proto_item *pi;
tvbuff_t *protocol_tvb;
va_list ap;
header_field_info *hfinfo;
gchar* protocol_rep;
CHECK_FOR_NULL_TREE(tree);
TRY_TO_FAKE_THIS_ITEM(tree, hfindex, hfinfo);
DISSECTOR_ASSERT_FIELD_TYPE(hfinfo, FT_PROTOCOL);
/*
* This can throw an exception, so do it before we allocate anything.
*/
protocol_tvb = (start == 0 ? tvb : tvb_new_subset_length(tvb, start, length));
pi = proto_tree_add_pi(tree, hfinfo, tvb, start, &length);
va_start(ap, format);
protocol_rep = ws_strdup_vprintf(format, ap);
proto_tree_set_protocol_tvb(PNODE_FINFO(pi), protocol_tvb, protocol_rep, length);
g_free(protocol_rep);
va_end(ap);
TRY_TO_FAKE_THIS_REPR(pi);
va_start(ap, format);
proto_tree_set_representation(pi, format, ap);
va_end(ap);
return pi;
}
/* Add a FT_BYTES to a proto_tree */
proto_item *
proto_tree_add_bytes(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
gint length, const guint8 *start_ptr)
{
proto_item *pi;
header_field_info *hfinfo;
gint item_length;
PROTO_REGISTRAR_GET_NTH(hfindex, hfinfo);
get_hfi_length(hfinfo, tvb, start, &length, &item_length, ENC_NA);
test_length(hfinfo, tvb, start, item_length, ENC_NA);
CHECK_FOR_NULL_TREE(tree);
TRY_TO_FAKE_THIS_ITEM(tree, hfindex, hfinfo);
DISSECTOR_ASSERT_FIELD_TYPE(hfinfo, FT_BYTES);
pi = proto_tree_add_pi(tree, hfinfo, tvb, start, &length);
proto_tree_set_bytes(PNODE_FINFO(pi), start_ptr, length);
return pi;
}
/* Add a FT_BYTES to a proto_tree */
proto_item *
proto_tree_add_bytes_with_length(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
gint tvbuff_length, const guint8 *start_ptr, gint ptr_length)
{
proto_item *pi;
header_field_info *hfinfo;
gint item_length;
PROTO_REGISTRAR_GET_NTH(hfindex, hfinfo);
get_hfi_length(hfinfo, tvb, start, &tvbuff_length, &item_length, ENC_NA);
test_length(hfinfo, tvb, start, item_length, ENC_NA);
CHECK_FOR_NULL_TREE(tree);
TRY_TO_FAKE_THIS_ITEM(tree, hfindex, hfinfo);
DISSECTOR_ASSERT_FIELD_TYPE(hfinfo, FT_BYTES);
pi = proto_tree_add_pi(tree, hfinfo, tvb, start, &tvbuff_length);
proto_tree_set_bytes(PNODE_FINFO(pi), start_ptr, ptr_length);
return pi;
}
proto_item *
proto_tree_add_bytes_format_value(proto_tree *tree, int hfindex, tvbuff_t *tvb,
gint start, gint length,
const guint8 *start_ptr,
const char *format, ...)
{
proto_item *pi;
va_list ap;
if (start_ptr == NULL)
start_ptr = tvb_get_ptr(tvb, start, length);
pi = proto_tree_add_bytes(tree, hfindex, tvb, start, length, start_ptr);
TRY_TO_FAKE_THIS_REPR_NESTED(pi);
va_start(ap, format);
proto_tree_set_representation_value(pi, format, ap);
va_end(ap);
return pi;
}
proto_item *
proto_tree_add_bytes_format(proto_tree *tree, int hfindex, tvbuff_t *tvb,
gint start, gint length, const guint8 *start_ptr,
const char *format, ...)
{
proto_item *pi;
va_list ap;
if (start_ptr == NULL)
start_ptr = tvb_get_ptr(tvb, start, length);
pi = proto_tree_add_bytes(tree, hfindex, tvb, start, length, start_ptr);
TRY_TO_FAKE_THIS_REPR_NESTED(pi);
va_start(ap, format);
proto_tree_set_representation(pi, format, ap);
va_end(ap);
return pi;
}
static void
proto_tree_set_bytes(field_info *fi, const guint8* start_ptr, gint length)
{
DISSECTOR_ASSERT(length >= 0);
DISSECTOR_ASSERT(start_ptr != NULL || length == 0);
fvalue_set_bytes_data(fi->value, start_ptr, length);
}
static void
proto_tree_set_bytes_tvb(field_info *fi, tvbuff_t *tvb, gint offset, gint length)
{
tvb_ensure_bytes_exist(tvb, offset, length);
proto_tree_set_bytes(fi, tvb_get_ptr(tvb, offset, length), length);
}
static void
proto_tree_set_bytes_gbytearray(field_info *fi, const GByteArray *value)
{
GByteArray *bytes;
DISSECTOR_ASSERT(value != NULL);
bytes = byte_array_dup(value);
fvalue_set_byte_array(fi->value, bytes);
}
/* Add a FT_*TIME to a proto_tree */
proto_item *
proto_tree_add_time(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
gint length, const nstime_t *value_ptr)
{
proto_item *pi;
header_field_info *hfinfo;
CHECK_FOR_NULL_TREE(tree);
TRY_TO_FAKE_THIS_ITEM(tree, hfindex, hfinfo);
DISSECTOR_ASSERT_FIELD_TYPE_IS_TIME(hfinfo);
pi = proto_tree_add_pi(tree, hfinfo, tvb, start, &length);
proto_tree_set_time(PNODE_FINFO(pi), value_ptr);
return pi;
}
proto_item *
proto_tree_add_time_format_value(proto_tree *tree, int hfindex, tvbuff_t *tvb,
gint start, gint length, nstime_t *value_ptr,
const char *format, ...)
{
proto_item *pi;
va_list ap;
pi = proto_tree_add_time(tree, hfindex, tvb, start, length, value_ptr);
if (pi != tree) {
va_start(ap, format);
proto_tree_set_representation_value(pi, format, ap);
va_end(ap);
}
return pi;
}
proto_item *
proto_tree_add_time_format(proto_tree *tree, int hfindex, tvbuff_t *tvb,
gint start, gint length, nstime_t *value_ptr,
const char *format, ...)
{
proto_item *pi;
va_list ap;
pi = proto_tree_add_time(tree, hfindex, tvb, start, length, value_ptr);
if (pi != tree) {
TRY_TO_FAKE_THIS_REPR(pi);
va_start(ap, format);
proto_tree_set_representation(pi, format, ap);
va_end(ap);
}
return pi;
}
/* Set the FT_*TIME value */
static void
proto_tree_set_time(field_info *fi, const nstime_t *value_ptr)
{
DISSECTOR_ASSERT(value_ptr != NULL);
fvalue_set_time(fi->value, value_ptr);
}
/* Add a FT_IPXNET to a proto_tree */
proto_item *
proto_tree_add_ipxnet(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
gint length, guint32 value)
{
proto_item *pi;
header_field_info *hfinfo;
CHECK_FOR_NULL_TREE(tree);
TRY_TO_FAKE_THIS_ITEM(tree, hfindex, hfinfo);
DISSECTOR_ASSERT_FIELD_TYPE(hfinfo, FT_IPXNET);
pi = proto_tree_add_pi(tree, hfinfo, tvb, start, &length);
proto_tree_set_ipxnet(PNODE_FINFO(pi), value);
return pi;
}
proto_item *
proto_tree_add_ipxnet_format_value(proto_tree *tree, int hfindex, tvbuff_t *tvb,
gint start, gint length, guint32 value,
const char *format, ...)
{
proto_item *pi;
va_list ap;
pi = proto_tree_add_ipxnet(tree, hfindex, tvb, start, length, value);
if (pi != tree) {
va_start(ap, format);
proto_tree_set_representation_value(pi, format, ap);
va_end(ap);
}
return pi;
}
proto_item *
proto_tree_add_ipxnet_format(proto_tree *tree, int hfindex, tvbuff_t *tvb,
gint start, gint length, guint32 value,
const char *format, ...)
{
proto_item *pi;
va_list ap;
pi = proto_tree_add_ipxnet(tree, hfindex, tvb, start, length, value);
if (pi != tree) {
TRY_TO_FAKE_THIS_REPR(pi);
va_start(ap, format);
proto_tree_set_representation(pi, format, ap);
va_end(ap);
}
return pi;
}
/* Set the FT_IPXNET value */
static void
proto_tree_set_ipxnet(field_info *fi, guint32 value)
{
fvalue_set_uinteger(fi->value, value);
}
/* Add a FT_IPv4 to a proto_tree */
proto_item *
proto_tree_add_ipv4(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
gint length, ws_in4_addr value)
{
proto_item *pi;
header_field_info *hfinfo;
CHECK_FOR_NULL_TREE(tree);
TRY_TO_FAKE_THIS_ITEM(tree, hfindex, hfinfo);
DISSECTOR_ASSERT_FIELD_TYPE(hfinfo, FT_IPv4);
pi = proto_tree_add_pi(tree, hfinfo, tvb, start, &length);
proto_tree_set_ipv4(PNODE_FINFO(pi), value);
return pi;
}
proto_item *
proto_tree_add_ipv4_format_value(proto_tree *tree, int hfindex, tvbuff_t *tvb,
gint start, gint length, ws_in4_addr value,
const char *format, ...)
{
proto_item *pi;
va_list ap;
pi = proto_tree_add_ipv4(tree, hfindex, tvb, start, length, value);
if (pi != tree) {
va_start(ap, format);
proto_tree_set_representation_value(pi, format, ap);
va_end(ap);
}
return pi;
}
proto_item *
proto_tree_add_ipv4_format(proto_tree *tree, int hfindex, tvbuff_t *tvb,
gint start, gint length, ws_in4_addr value,
const char *format, ...)
{
proto_item *pi;
va_list ap;
pi = proto_tree_add_ipv4(tree, hfindex, tvb, start, length, value);
if (pi != tree) {
TRY_TO_FAKE_THIS_REPR(pi);
va_start(ap, format);
proto_tree_set_representation(pi, format, ap);
va_end(ap);
}
return pi;
}
/* Set the FT_IPv4 value */
static void
proto_tree_set_ipv4(field_info *fi, ws_in4_addr value)
{
fvalue_set_uinteger(fi->value, value);
}
/* Add a FT_IPv6 to a proto_tree */
proto_item *
proto_tree_add_ipv6(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
gint length, const ws_in6_addr *value)
{
proto_item *pi;
header_field_info *hfinfo;
CHECK_FOR_NULL_TREE(tree);
TRY_TO_FAKE_THIS_ITEM(tree, hfindex, hfinfo);
DISSECTOR_ASSERT_FIELD_TYPE(hfinfo, FT_IPv6);
pi = proto_tree_add_pi(tree, hfinfo, tvb, start, &length);
proto_tree_set_ipv6(PNODE_FINFO(pi), value);
return pi;
}
proto_item *
proto_tree_add_ipv6_format_value(proto_tree *tree, int hfindex, tvbuff_t *tvb,
gint start, gint length,
const ws_in6_addr *value_ptr,
const char *format, ...)
{
proto_item *pi;
va_list ap;
pi = proto_tree_add_ipv6(tree, hfindex, tvb, start, length, value_ptr);
if (pi != tree) {
va_start(ap, format);
proto_tree_set_representation_value(pi, format, ap);
va_end(ap);
}
return pi;
}
proto_item *
proto_tree_add_ipv6_format(proto_tree *tree, int hfindex, tvbuff_t *tvb,
gint start, gint length,
const ws_in6_addr *value_ptr,
const char *format, ...)
{
proto_item *pi;
va_list ap;
pi = proto_tree_add_ipv6(tree, hfindex, tvb, start, length, value_ptr);
if (pi != tree) {
TRY_TO_FAKE_THIS_REPR(pi);
va_start(ap, format);
proto_tree_set_representation(pi, format, ap);
va_end(ap);
}
return pi;
}
/* Set the FT_IPv6 value */
static void
proto_tree_set_ipv6(field_info *fi, const ws_in6_addr *value)
{
DISSECTOR_ASSERT(value != NULL);
fvalue_set_ipv6(fi->value, value);
}
static void
proto_tree_set_ipv6_tvb(field_info *fi, tvbuff_t *tvb, gint start, gint length)
{
proto_tree_set_ipv6(fi, (const ws_in6_addr *)tvb_get_ptr(tvb, start, length));
}
/* Set the FT_FCWWN value */
static void
proto_tree_set_fcwwn(field_info *fi, const guint8* value_ptr)
{
DISSECTOR_ASSERT(value_ptr != NULL);
fvalue_set_fcwwn(fi->value, value_ptr);
}
static void
proto_tree_set_fcwwn_tvb(field_info *fi, tvbuff_t *tvb, gint start, gint length)
{
proto_tree_set_fcwwn(fi, tvb_get_ptr(tvb, start, length));
}
/* Add a FT_GUID to a proto_tree */
proto_item *
proto_tree_add_guid(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
gint length, const e_guid_t *value_ptr)
{
proto_item *pi;
header_field_info *hfinfo;
CHECK_FOR_NULL_TREE(tree);
TRY_TO_FAKE_THIS_ITEM(tree, hfindex, hfinfo);
DISSECTOR_ASSERT_FIELD_TYPE(hfinfo, FT_GUID);
pi = proto_tree_add_pi(tree, hfinfo, tvb, start, &length);
proto_tree_set_guid(PNODE_FINFO(pi), value_ptr);
return pi;
}
proto_item *
proto_tree_add_guid_format_value(proto_tree *tree, int hfindex, tvbuff_t *tvb,
gint start, gint length,
const e_guid_t *value_ptr,
const char *format, ...)
{
proto_item *pi;
va_list ap;
pi = proto_tree_add_guid(tree, hfindex, tvb, start, length, value_ptr);
if (pi != tree) {
va_start(ap, format);
proto_tree_set_representation_value(pi, format, ap);
va_end(ap);
}
return pi;
}
proto_item *
proto_tree_add_guid_format(proto_tree *tree, int hfindex, tvbuff_t *tvb,
gint start, gint length, const e_guid_t *value_ptr,
const char *format, ...)
{
proto_item *pi;
va_list ap;
pi = proto_tree_add_guid(tree, hfindex, tvb, start, length, value_ptr);
if (pi != tree) {
TRY_TO_FAKE_THIS_REPR(pi);
va_start(ap, format);
proto_tree_set_representation(pi, format, ap);
va_end(ap);
}
return pi;
}
/* Set the FT_GUID value */
static void
proto_tree_set_guid(field_info *fi, const e_guid_t *value_ptr)
{
DISSECTOR_ASSERT(value_ptr != NULL);
fvalue_set_guid(fi->value, value_ptr);
}
static void
proto_tree_set_guid_tvb(field_info *fi, tvbuff_t *tvb, gint start,
const guint encoding)
{
e_guid_t guid;
tvb_get_guid(tvb, start, &guid, encoding);
proto_tree_set_guid(fi, &guid);
}
/* Add a FT_OID to a proto_tree */
proto_item *
proto_tree_add_oid(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
gint length, const guint8* value_ptr)
{
proto_item *pi;
header_field_info *hfinfo;
CHECK_FOR_NULL_TREE(tree);
TRY_TO_FAKE_THIS_ITEM(tree, hfindex, hfinfo);
DISSECTOR_ASSERT_FIELD_TYPE(hfinfo, FT_OID);
pi = proto_tree_add_pi(tree, hfinfo, tvb, start, &length);
proto_tree_set_oid(PNODE_FINFO(pi), value_ptr, length);
return pi;
}
proto_item *
proto_tree_add_oid_format_value(proto_tree *tree, int hfindex, tvbuff_t *tvb,
gint start, gint length,
const guint8* value_ptr,
const char *format, ...)
{
proto_item *pi;
va_list ap;
pi = proto_tree_add_oid(tree, hfindex, tvb, start, length, value_ptr);
if (pi != tree) {
va_start(ap, format);
proto_tree_set_representation_value(pi, format, ap);
va_end(ap);
}
return pi;
}
proto_item *
proto_tree_add_oid_format(proto_tree *tree, int hfindex, tvbuff_t *tvb,
gint start, gint length, const guint8* value_ptr,
const char *format, ...)
{
proto_item *pi;
va_list ap;
pi = proto_tree_add_oid(tree, hfindex, tvb, start, length, value_ptr);
if (pi != tree) {
TRY_TO_FAKE_THIS_REPR(pi);
va_start(ap, format);
proto_tree_set_representation(pi, format, ap);
va_end(ap);
}
return pi;
}
/* Set the FT_OID value */
static void
proto_tree_set_oid(field_info *fi, const guint8* value_ptr, gint length)
{
GByteArray *bytes;
DISSECTOR_ASSERT(value_ptr != NULL || length == 0);
bytes = g_byte_array_new();
if (length > 0) {
g_byte_array_append(bytes, value_ptr, length);
}
fvalue_set_byte_array(fi->value, bytes);
}
static void
proto_tree_set_oid_tvb(field_info *fi, tvbuff_t *tvb, gint start, gint length)
{
proto_tree_set_oid(fi, tvb_get_ptr(tvb, start, length), length);
}
/* Set the FT_SYSTEM_ID value */
static void
proto_tree_set_system_id(field_info *fi, const guint8* value_ptr, gint length)
{
GByteArray *bytes;
DISSECTOR_ASSERT(value_ptr != NULL || length == 0);
bytes = g_byte_array_new();
if (length > 0) {
g_byte_array_append(bytes, value_ptr, length);
}
fvalue_set_byte_array(fi->value, bytes);
}
static void
proto_tree_set_system_id_tvb(field_info *fi, tvbuff_t *tvb, gint start, gint length)
{
proto_tree_set_system_id(fi, tvb_get_ptr(tvb, start, length), length);
}
/* Add a FT_STRING, FT_STRINGZ, FT_STRINGZPAD, or FT_STRINGZTRUNC to a
* proto_tree. Creates own copy of string, and frees it when the proto_tree
* is destroyed. */
proto_item *
proto_tree_add_string(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
gint length, const char* value)
{
proto_item *pi;
header_field_info *hfinfo;
gint item_length;
PROTO_REGISTRAR_GET_NTH(hfindex, hfinfo);
get_hfi_length(hfinfo, tvb, start, &length, &item_length, ENC_NA);
/*
* Special case - if the length is 0, skip the test, so that
* we can have an empty string right after the end of the
* packet. (This handles URL-encoded forms where the last field
* has no value so the form ends right after the =.)
*/
if (item_length != 0)
test_length(hfinfo, tvb, start, item_length, ENC_NA);
CHECK_FOR_NULL_TREE(tree);
TRY_TO_FAKE_THIS_ITEM(tree, hfindex, hfinfo);
DISSECTOR_ASSERT_FIELD_TYPE_IS_STRING(hfinfo);
pi = proto_tree_add_pi(tree, hfinfo, tvb, start, &length);
DISSECTOR_ASSERT(length >= 0);
WS_UTF_8_CHECK(value, -1);
proto_tree_set_string(PNODE_FINFO(pi), value);
return pi;
}
proto_item *
proto_tree_add_string_format_value(proto_tree *tree, int hfindex, tvbuff_t *tvb,
gint start, gint length, const char* value,
const char *format,
...)
{
proto_item *pi;
va_list ap;
pi = proto_tree_add_string(tree, hfindex, tvb, start, length, value);
if (pi != tree) {
va_start(ap, format);
proto_tree_set_representation_value(pi, format, ap);
va_end(ap);
}
return pi;
}
proto_item *
proto_tree_add_string_format(proto_tree *tree, int hfindex, tvbuff_t *tvb,
gint start, gint length, const char* value,
const char *format, ...)
{
proto_item *pi;
va_list ap;
pi = proto_tree_add_string(tree, hfindex, tvb, start, length, value);
if (pi != tree) {
TRY_TO_FAKE_THIS_REPR(pi);
va_start(ap, format);
proto_tree_set_representation(pi, format, ap);
va_end(ap);
}
return pi;
}
/* Set the FT_STRING value */
static void
proto_tree_set_string(field_info *fi, const char* value)
{
if (value) {
fvalue_set_string(fi->value, value);
} else {
/*
* XXX - why is a null value for a string field
* considered valid?
*/
fvalue_set_string(fi->value, "[ Null ]");
}
}
/* Set the FT_AX25 value */
static void
proto_tree_set_ax25(field_info *fi, const guint8* value)
{
fvalue_set_ax25(fi->value, value);
}
static void
proto_tree_set_ax25_tvb(field_info *fi, tvbuff_t *tvb, gint start)
{
proto_tree_set_ax25(fi, tvb_get_ptr(tvb, start, 7));
}
/* Set the FT_VINES value */
static void
proto_tree_set_vines(field_info *fi, const guint8* value)
{
fvalue_set_vines(fi->value, value);
}
static void
proto_tree_set_vines_tvb(field_info *fi, tvbuff_t *tvb, gint start)
{
proto_tree_set_vines(fi, tvb_get_ptr(tvb, start, FT_VINES_ADDR_LEN));
}
/* Add a FT_ETHER to a proto_tree */
proto_item *
proto_tree_add_ether(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
gint length, const guint8* value)
{
proto_item *pi;
header_field_info *hfinfo;
CHECK_FOR_NULL_TREE(tree);
TRY_TO_FAKE_THIS_ITEM(tree, hfindex, hfinfo);
DISSECTOR_ASSERT_FIELD_TYPE(hfinfo, FT_ETHER);
pi = proto_tree_add_pi(tree, hfinfo, tvb, start, &length);
proto_tree_set_ether(PNODE_FINFO(pi), value);
return pi;
}
proto_item *
proto_tree_add_ether_format_value(proto_tree *tree, int hfindex, tvbuff_t *tvb,
gint start, gint length, const guint8* value,
const char *format, ...)
{
proto_item *pi;
va_list ap;
pi = proto_tree_add_ether(tree, hfindex, tvb, start, length, value);
if (pi != tree) {
va_start(ap, format);
proto_tree_set_representation_value(pi, format, ap);
va_end(ap);
}
return pi;
}
proto_item *
proto_tree_add_ether_format(proto_tree *tree, int hfindex, tvbuff_t *tvb,
gint start, gint length, const guint8* value,
const char *format, ...)
{
proto_item *pi;
va_list ap;
pi = proto_tree_add_ether(tree, hfindex, tvb, start, length, value);
if (pi != tree) {
TRY_TO_FAKE_THIS_REPR(pi);
va_start(ap, format);
proto_tree_set_representation(pi, format, ap);
va_end(ap);
}
return pi;
}
/* Set the FT_ETHER value */
static void
proto_tree_set_ether(field_info *fi, const guint8* value)
{
fvalue_set_ether(fi->value, value);
}
static void
proto_tree_set_ether_tvb(field_info *fi, tvbuff_t *tvb, gint start)
{
proto_tree_set_ether(fi, tvb_get_ptr(tvb, start, FT_ETHER_LEN));
}
/* Add a FT_BOOLEAN to a proto_tree */
proto_item *
proto_tree_add_boolean(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
gint length, guint32 value)
{
proto_item *pi;
header_field_info *hfinfo;
CHECK_FOR_NULL_TREE(tree);
TRY_TO_FAKE_THIS_ITEM(tree, hfindex, hfinfo);
DISSECTOR_ASSERT_FIELD_TYPE(hfinfo, FT_BOOLEAN);
pi = proto_tree_add_pi(tree, hfinfo, tvb, start, &length);
proto_tree_set_boolean(PNODE_FINFO(pi), value);
return pi;
}
proto_item *
proto_tree_add_boolean_format_value(proto_tree *tree, int hfindex,
tvbuff_t *tvb, gint start, gint length,
guint32 value, const char *format, ...)
{
proto_item *pi;
va_list ap;
pi = proto_tree_add_boolean(tree, hfindex, tvb, start, length, value);
if (pi != tree) {
va_start(ap, format);
proto_tree_set_representation_value(pi, format, ap);
va_end(ap);
}
return pi;
}
proto_item *
proto_tree_add_boolean_format(proto_tree *tree, int hfindex, tvbuff_t *tvb,
gint start, gint length, guint32 value,
const char *format, ...)
{
proto_item *pi;
va_list ap;
pi = proto_tree_add_boolean(tree, hfindex, tvb, start, length, value);
if (pi != tree) {
TRY_TO_FAKE_THIS_REPR(pi);
va_start(ap, format);
proto_tree_set_representation(pi, format, ap);
va_end(ap);
}
return pi;
}
static proto_item *
proto_tree_add_boolean64(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
gint length, guint64 value)
{
proto_item *pi;
header_field_info *hfinfo;
CHECK_FOR_NULL_TREE(tree);
TRY_TO_FAKE_THIS_ITEM(tree, hfindex, hfinfo);
DISSECTOR_ASSERT_FIELD_TYPE(hfinfo, FT_BOOLEAN);
pi = proto_tree_add_pi(tree, hfinfo, tvb, start, &length);
proto_tree_set_boolean(PNODE_FINFO(pi), value);
return pi;
}
/* Set the FT_BOOLEAN value */
static void
proto_tree_set_boolean(field_info *fi, guint64 value)
{
proto_tree_set_uint64(fi, value);
}
/* Generate, into "buf", a string showing the bits of a bitfield.
Return a pointer to the character after that string. */
static char *
other_decode_bitfield_value(char *buf, const guint64 val, const guint64 mask, const int width)
{
int i = 0;
guint64 bit;
char *p;
p = buf;
/* This is a devel error. It is safer to stop here. */
DISSECTOR_ASSERT(width >= 1);
bit = G_GUINT64_CONSTANT(1) << (width - 1);
for (;;) {
if (mask & bit) {
/* This bit is part of the field. Show its value. */
if (val & bit)
*p++ = '1';
else
*p++ = '0';
} else {
/* This bit is not part of the field. */
*p++ = '.';
}
bit >>= 1;
i++;
if (i >= width)
break;
if (i % 4 == 0)
*p++ = ' ';
}
*p = '\0';
return p;
}
static char *
decode_bitfield_value(char *buf, const guint64 val, const guint64 mask, const int width)
{
char *p;
p = other_decode_bitfield_value(buf, val, mask, width);
p = g_stpcpy(p, " = ");
return p;
}
static char *
other_decode_bitfield_varint_value(char *buf, guint64 val, guint64 mask, const int width)
{
int i = 0;
guint64 bit;
char *p;
p = buf;
/* This is a devel error. It is safer to stop here. */
DISSECTOR_ASSERT(width >= 1);
bit = G_GUINT64_CONSTANT(1) << (width - 1);
for (;;) {
if (((8-(i % 8)) != 8) && /* MSB is never used for value. */
(mask & bit)) {
/* This bit is part of the field. Show its value. */
if (val & bit)
*p++ = '1';
else
*p++ = '0';
} else {
/* This bit is not part of the field. */
*p++ = '.';
}
bit >>= 1;
i++;
if (i >= width)
break;
if (i % 4 == 0)
*p++ = ' ';
}
*p = '\0';
return p;
}
static char *
decode_bitfield_varint_value(char *buf, const guint64 val, const guint64 mask, const int width)
{
char *p;
p = other_decode_bitfield_varint_value(buf, val, mask, width);
p = g_stpcpy(p, " = ");
return p;
}
/* Add a FT_FLOAT to a proto_tree */
proto_item *
proto_tree_add_float(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
gint length, float value)
{
proto_item *pi;
header_field_info *hfinfo;
CHECK_FOR_NULL_TREE(tree);
TRY_TO_FAKE_THIS_ITEM(tree, hfindex, hfinfo);
DISSECTOR_ASSERT_FIELD_TYPE(hfinfo, FT_FLOAT);
pi = proto_tree_add_pi(tree, hfinfo, tvb, start, &length);
proto_tree_set_float(PNODE_FINFO(pi), value);
return pi;
}
proto_item *
proto_tree_add_float_format_value(proto_tree *tree, int hfindex, tvbuff_t *tvb,
gint start, gint length, float value,
const char *format, ...)
{
proto_item *pi;
va_list ap;
pi = proto_tree_add_float(tree, hfindex, tvb, start, length, value);
if (pi != tree) {
va_start(ap, format);
proto_tree_set_representation_value(pi, format, ap);
va_end(ap);
}
return pi;
}
proto_item *
proto_tree_add_float_format(proto_tree *tree, int hfindex, tvbuff_t *tvb,
gint start, gint length, float value,
const char *format, ...)
{
proto_item *pi;
va_list ap;
pi = proto_tree_add_float(tree, hfindex, tvb, start, length, value);
if (pi != tree) {
TRY_TO_FAKE_THIS_REPR(pi);
va_start(ap, format);
proto_tree_set_representation(pi, format, ap);
va_end(ap);
}
return pi;
}
/* Set the FT_FLOAT value */
static void
proto_tree_set_float(field_info *fi, float value)
{
fvalue_set_floating(fi->value, value);
}
/* Add a FT_DOUBLE to a proto_tree */
proto_item *
proto_tree_add_double(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
gint length, double value)
{
proto_item *pi;
header_field_info *hfinfo;
CHECK_FOR_NULL_TREE(tree);
TRY_TO_FAKE_THIS_ITEM(tree, hfindex, hfinfo);
DISSECTOR_ASSERT_FIELD_TYPE(hfinfo, FT_DOUBLE);
pi = proto_tree_add_pi(tree, hfinfo, tvb, start, &length);
proto_tree_set_double(PNODE_FINFO(pi), value);
return pi;
}
proto_item *
proto_tree_add_double_format_value(proto_tree *tree, int hfindex, tvbuff_t *tvb,
gint start, gint length, double value,
const char *format, ...)
{
proto_item *pi;
va_list ap;
pi = proto_tree_add_double(tree, hfindex, tvb, start, length, value);
if (pi != tree) {
va_start(ap, format);
proto_tree_set_representation_value(pi, format, ap);
va_end(ap);
}
return pi;
}
proto_item *
proto_tree_add_double_format(proto_tree *tree, int hfindex, tvbuff_t *tvb,
gint start, gint length, double value,
const char *format, ...)
{
proto_item *pi;
va_list ap;
pi = proto_tree_add_double(tree, hfindex, tvb, start, length, value);
if (pi != tree) {
TRY_TO_FAKE_THIS_REPR(pi);
va_start(ap, format);
proto_tree_set_representation(pi, format, ap);
va_end(ap);
}
return pi;
}
/* Set the FT_DOUBLE value */
static void
proto_tree_set_double(field_info *fi, double value)
{
fvalue_set_floating(fi->value, value);
}
/* Add FT_CHAR or FT_UINT{8,16,24,32} to a proto_tree */
proto_item *
proto_tree_add_uint(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
gint length, guint32 value)
{
proto_item *pi = NULL;
header_field_info *hfinfo;
CHECK_FOR_NULL_TREE(tree);
TRY_TO_FAKE_THIS_ITEM(tree, hfindex, hfinfo);
switch (hfinfo->type) {
case FT_CHAR:
case FT_UINT8:
case FT_UINT16:
case FT_UINT24:
case FT_UINT32:
case FT_FRAMENUM:
pi = proto_tree_add_pi(tree, hfinfo, tvb, start, &length);
proto_tree_set_uint(PNODE_FINFO(pi), value);
break;
default:
REPORT_DISSECTOR_BUG("field %s is not of type FT_CHAR, FT_UINT8, FT_UINT16, FT_UINT24, FT_UINT32, or FT_FRAMENUM",
hfinfo->abbrev);
}
return pi;
}
proto_item *
proto_tree_add_uint_format_value(proto_tree *tree, int hfindex, tvbuff_t *tvb,
gint start, gint length, guint32 value,
const char *format, ...)
{
proto_item *pi;
va_list ap;
pi = proto_tree_add_uint(tree, hfindex, tvb, start, length, value);
if (pi != tree) {
va_start(ap, format);
proto_tree_set_representation_value(pi, format, ap);
va_end(ap);
}
return pi;
}
proto_item *
proto_tree_add_uint_format(proto_tree *tree, int hfindex, tvbuff_t *tvb,
gint start, gint length, guint32 value,
const char *format, ...)
{
proto_item *pi;
va_list ap;
pi = proto_tree_add_uint(tree, hfindex, tvb, start, length, value);
if (pi != tree) {
TRY_TO_FAKE_THIS_REPR(pi);
va_start(ap, format);
proto_tree_set_representation(pi, format, ap);
va_end(ap);
}
return pi;
}
/* Set the FT_UINT{8,16,24,32} value */
static void
proto_tree_set_uint(field_info *fi, guint32 value)
{
header_field_info *hfinfo;
guint32 integer;
hfinfo = fi->hfinfo;
integer = value;
if (hfinfo->bitmask) {
/* Mask out irrelevant portions */
integer &= (guint32)(hfinfo->bitmask);
/* Shift bits */
integer >>= hfinfo_bitshift(hfinfo);
FI_SET_FLAG(fi, FI_BITS_OFFSET(hfinfo_bitoffset(hfinfo)));
FI_SET_FLAG(fi, FI_BITS_SIZE(hfinfo_mask_bitwidth(hfinfo)));
}
fvalue_set_uinteger(fi->value, integer);
}
/* Add FT_UINT{40,48,56,64} to a proto_tree */
proto_item *
proto_tree_add_uint64(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
gint length, guint64 value)
{
proto_item *pi = NULL;
header_field_info *hfinfo;
CHECK_FOR_NULL_TREE(tree);
TRY_TO_FAKE_THIS_ITEM(tree, hfindex, hfinfo);
switch (hfinfo->type) {
case FT_UINT40:
case FT_UINT48:
case FT_UINT56:
case FT_UINT64:
case FT_FRAMENUM:
pi = proto_tree_add_pi(tree, hfinfo, tvb, start, &length);
proto_tree_set_uint64(PNODE_FINFO(pi), value);
break;
default:
REPORT_DISSECTOR_BUG("field %s is not of type FT_UINT40, FT_UINT48, FT_UINT56, FT_UINT64, or FT_FRAMENUM",
hfinfo->abbrev);
}
return pi;
}
proto_item *
proto_tree_add_uint64_format_value(proto_tree *tree, int hfindex, tvbuff_t *tvb,
gint start, gint length, guint64 value,
const char *format, ...)
{
proto_item *pi;
va_list ap;
pi = proto_tree_add_uint64(tree, hfindex, tvb, start, length, value);
if (pi != tree) {
va_start(ap, format);
proto_tree_set_representation_value(pi, format, ap);
va_end(ap);
}
return pi;
}
proto_item *
proto_tree_add_uint64_format(proto_tree *tree, int hfindex, tvbuff_t *tvb,
gint start, gint length, guint64 value,
const char *format, ...)
{
proto_item *pi;
va_list ap;
pi = proto_tree_add_uint64(tree, hfindex, tvb, start, length, value);
if (pi != tree) {
TRY_TO_FAKE_THIS_REPR(pi);
va_start(ap, format);
proto_tree_set_representation(pi, format, ap);
va_end(ap);
}
return pi;
}
/* Set the FT_UINT{40,48,56,64} value */
static void
proto_tree_set_uint64(field_info *fi, guint64 value)
{
header_field_info *hfinfo;
guint64 integer;
hfinfo = fi->hfinfo;
integer = value;
if (hfinfo->bitmask) {
/* Mask out irrelevant portions */
integer &= hfinfo->bitmask;
/* Shift bits */
integer >>= hfinfo_bitshift(hfinfo);
FI_SET_FLAG(fi, FI_BITS_OFFSET(hfinfo_bitoffset(hfinfo)));
FI_SET_FLAG(fi, FI_BITS_SIZE(hfinfo_mask_bitwidth(hfinfo)));
}
fvalue_set_uinteger64(fi->value, integer);
}
/* Add FT_INT{8,16,24,32} to a proto_tree */
proto_item *
proto_tree_add_int(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
gint length, gint32 value)
{
proto_item *pi = NULL;
header_field_info *hfinfo;
CHECK_FOR_NULL_TREE(tree);
TRY_TO_FAKE_THIS_ITEM(tree, hfindex, hfinfo);
switch (hfinfo->type) {
case FT_INT8:
case FT_INT16:
case FT_INT24:
case FT_INT32:
pi = proto_tree_add_pi(tree, hfinfo, tvb, start, &length);
proto_tree_set_int(PNODE_FINFO(pi), value);
break;
default:
REPORT_DISSECTOR_BUG("field %s is not of type FT_INT8, FT_INT16, FT_INT24, or FT_INT32",
hfinfo->abbrev);
}
return pi;
}
proto_item *
proto_tree_add_int_format_value(proto_tree *tree, int hfindex, tvbuff_t *tvb,
gint start, gint length, gint32 value,
const char *format, ...)
{
proto_item *pi;
va_list ap;
pi = proto_tree_add_int(tree, hfindex, tvb, start, length, value);
if (pi != tree) {
va_start(ap, format);
proto_tree_set_representation_value(pi, format, ap);
va_end(ap);
}
return pi;
}
proto_item *
proto_tree_add_int_format(proto_tree *tree, int hfindex, tvbuff_t *tvb,
gint start, gint length, gint32 value,
const char *format, ...)
{
proto_item *pi;
va_list ap;
pi = proto_tree_add_int(tree, hfindex, tvb, start, length, value);
if (pi != tree) {
TRY_TO_FAKE_THIS_REPR(pi);
va_start(ap, format);
proto_tree_set_representation(pi, format, ap);
va_end(ap);
}
return pi;
}
/* Set the FT_INT{8,16,24,32} value */
static void
proto_tree_set_int(field_info *fi, gint32 value)
{
header_field_info *hfinfo;
guint32 integer;
gint no_of_bits;
hfinfo = fi->hfinfo;
integer = (guint32) value;
if (hfinfo->bitmask) {
/* Mask out irrelevant portions */
integer &= (guint32)(hfinfo->bitmask);
/* Shift bits */
integer >>= hfinfo_bitshift(hfinfo);
no_of_bits = ws_count_ones(hfinfo->bitmask);
integer = ws_sign_ext32(integer, no_of_bits);
FI_SET_FLAG(fi, FI_BITS_OFFSET(hfinfo_bitoffset(hfinfo)));
FI_SET_FLAG(fi, FI_BITS_SIZE(hfinfo_mask_bitwidth(hfinfo)));
}
fvalue_set_sinteger(fi->value, integer);
}
/* Add FT_INT{40,48,56,64} to a proto_tree */
proto_item *
proto_tree_add_int64(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
gint length, gint64 value)
{
proto_item *pi = NULL;
header_field_info *hfinfo;
CHECK_FOR_NULL_TREE(tree);
TRY_TO_FAKE_THIS_ITEM(tree, hfindex, hfinfo);
switch (hfinfo->type) {
case FT_INT40:
case FT_INT48:
case FT_INT56:
case FT_INT64:
pi = proto_tree_add_pi(tree, hfinfo, tvb, start, &length);
proto_tree_set_int64(PNODE_FINFO(pi), value);
break;
default:
REPORT_DISSECTOR_BUG("field %s is not of type FT_INT40, FT_INT48, FT_INT56, or FT_INT64",
hfinfo->abbrev);
}
return pi;
}
proto_item *
proto_tree_add_int64_format_value(proto_tree *tree, int hfindex, tvbuff_t *tvb,
gint start, gint length, gint64 value,
const char *format, ...)
{
proto_item *pi;
va_list ap;
pi = proto_tree_add_int64(tree, hfindex, tvb, start, length, value);
if (pi != tree) {
va_start(ap, format);
proto_tree_set_representation_value(pi, format, ap);
va_end(ap);
}
return pi;
}
/* Set the FT_INT{40,48,56,64} value */
static void
proto_tree_set_int64(field_info *fi, gint64 value)
{
header_field_info *hfinfo;
guint64 integer;
gint no_of_bits;
hfinfo = fi->hfinfo;
integer = value;
if (hfinfo->bitmask) {
/* Mask out irrelevant portions */
integer &= hfinfo->bitmask;
/* Shift bits */
integer >>= hfinfo_bitshift(hfinfo);
no_of_bits = ws_count_ones(hfinfo->bitmask);
integer = ws_sign_ext64(integer, no_of_bits);
FI_SET_FLAG(fi, FI_BITS_OFFSET(hfinfo_bitoffset(hfinfo)));
FI_SET_FLAG(fi, FI_BITS_SIZE(hfinfo_mask_bitwidth(hfinfo)));
}
fvalue_set_sinteger64(fi->value, integer);
}
proto_item *
proto_tree_add_int64_format(proto_tree *tree, int hfindex, tvbuff_t *tvb,
gint start, gint length, gint64 value,
const char *format, ...)
{
proto_item *pi;
va_list ap;
pi = proto_tree_add_int64(tree, hfindex, tvb, start, length, value);
if (pi != tree) {
TRY_TO_FAKE_THIS_REPR(pi);
va_start(ap, format);
proto_tree_set_representation(pi, format, ap);
va_end(ap);
}
return pi;
}
/* Add a FT_EUI64 to a proto_tree */
proto_item *
proto_tree_add_eui64(proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start,
gint length, const guint64 value)
{
proto_item *pi;
header_field_info *hfinfo;
CHECK_FOR_NULL_TREE(tree);
TRY_TO_FAKE_THIS_ITEM(tree, hfindex, hfinfo);
DISSECTOR_ASSERT_FIELD_TYPE(hfinfo, FT_EUI64);
pi = proto_tree_add_pi(tree, hfinfo, tvb, start, &length);
proto_tree_set_eui64(PNODE_FINFO(pi), value);
return pi;
}
proto_item *
proto_tree_add_eui64_format_value(proto_tree *tree, int hfindex, tvbuff_t *tvb,
gint start, gint length, const guint64 value,
const char *format, ...)
{
proto_item *pi;
va_list ap;
pi = proto_tree_add_eui64(tree, hfindex, tvb, start, length, value);
if (pi != tree) {
va_start(ap, format);
proto_tree_set_representation_value(pi, format, ap);
va_end(ap);
}
return pi;
}
proto_item *
proto_tree_add_eui64_format(proto_tree *tree, int hfindex, tvbuff_t *tvb,
gint start, gint length, const guint64 value,
const char *format, ...)
{
proto_item *pi;
va_list ap;
pi = proto_tree_add_eui64(tree, hfindex, tvb, start, length, value);
if (pi != tree) {
TRY_TO_FAKE_THIS_REPR(pi);
va_start(ap, format);
proto_tree_set_representation(pi, format, ap);
va_end(ap);
}
return pi;
}
/* Set the FT_EUI64 value */
static void
proto_tree_set_eui64(field_info *fi, const guint64 value)
{
fvalue_set_uinteger64(fi->value, value);
}
static void
proto_tree_set_eui64_tvb(field_info *fi, tvbuff_t *tvb, gint start, const guint encoding)
{
if (encoding)
{
proto_tree_set_eui64(fi, tvb_get_letoh64(tvb, start));
} else {
proto_tree_set_eui64(fi, tvb_get_ntoh64(tvb, start));
}
}
/* Add a field_info struct to the proto_tree, encapsulating it in a proto_node */
static proto_item *
proto_tree_add_node(proto_tree *tree, field_info *fi)
{
proto_node *pnode, *tnode, *sibling;
field_info *tfi;
guint depth = 1;
/*
* Restrict our depth. proto_tree_traverse_pre_order and
* proto_tree_traverse_post_order (and possibly others) are recursive
* so we need to be mindful of our stack size.
*/
if (tree->first_child == NULL) {
for (tnode = tree; tnode != NULL; tnode = tnode->parent) {
depth++;
if (G_UNLIKELY(depth > prefs.gui_max_tree_depth)) {
fvalue_free(fi->value);
fi->value = NULL;
THROW_MESSAGE(DissectorError, wmem_strdup_printf(PNODE_POOL(tree),
"Maximum tree depth %d exceeded for \"%s\" - \"%s\" (%s:%u) (Maximum depth can be increased in advanced preferences)",
prefs.gui_max_tree_depth,
fi->hfinfo->name, fi->hfinfo->abbrev, G_STRFUNC, __LINE__));
}
}
}
/*
* Make sure "tree" is ready to have subtrees under it, by
* checking whether it's been given an ett_ value.
*
* "PNODE_FINFO(tnode)" may be null; that's the case for the root
* node of the protocol tree. That node is not displayed,
* so it doesn't need an ett_ value to remember whether it
* was expanded.
*/
tnode = tree;
tfi = PNODE_FINFO(tnode);
if (tfi != NULL && (tfi->tree_type < 0 || tfi->tree_type >= num_tree_types)) {
/* Since we are not adding fi to a node, its fvalue won't get
* freed by proto_tree_free_node(), so free it now.
*/
fvalue_free(fi->value);
fi->value = NULL;
REPORT_DISSECTOR_BUG("\"%s\" - \"%s\" tfi->tree_type: %d invalid (%s:%u)",
fi->hfinfo->name, fi->hfinfo->abbrev, tfi->tree_type, __FILE__, __LINE__);
/* XXX - is it safe to continue here? */
}
pnode = wmem_new(PNODE_POOL(tree), proto_node);
PROTO_NODE_INIT(pnode);
pnode->parent = tnode;
PNODE_FINFO(pnode) = fi;
pnode->tree_data = PTREE_DATA(tree);
if (tnode->last_child != NULL) {
sibling = tnode->last_child;
DISSECTOR_ASSERT(sibling->next == NULL);
sibling->next = pnode;
} else
tnode->first_child = pnode;
tnode->last_child = pnode;
tree_data_add_maybe_interesting_field(pnode->tree_data, fi);
return (proto_item *)pnode;
}
/* Generic way to allocate field_info and add to proto_tree.
* Sets *pfi to address of newly-allocated field_info struct */
static proto_item *
proto_tree_add_pi(proto_tree *tree, header_field_info *hfinfo, tvbuff_t *tvb, gint start,
gint *length)
{
proto_item *pi;
field_info *fi;
gint item_length;
get_hfi_length(hfinfo, tvb, start, length, &item_length, ENC_NA);
fi = new_field_info(tree, hfinfo, tvb, start, item_length);
pi = proto_tree_add_node(tree, fi);
return pi;
}
static void
get_hfi_length(header_field_info *hfinfo, tvbuff_t *tvb, const gint start, gint *length,
gint *item_length, const guint encoding)
{
gint length_remaining;
/*
* We only allow a null tvbuff if the item has a zero length,
* i.e. if there's no data backing it.
*/
DISSECTOR_ASSERT(tvb != NULL || *length == 0);
/*
* XXX - in some protocols, there are 32-bit unsigned length
* fields, so lengths in protocol tree and tvbuff routines
* should really be unsigned. We should have, for those
* field types for which "to the end of the tvbuff" makes sense,
* additional routines that take no length argument and
* add fields that run to the end of the tvbuff.
*/
if (*length == -1) {
/*
* For FT_NONE, FT_PROTOCOL, FT_BYTES, FT_STRING,
* FT_STRINGZPAD, and FT_STRINGZTRUNC fields, a length
* of -1 means "set the length to what remains in the
* tvbuff".
*
* The assumption is either that
*
* 1) the length of the item can only be determined
* by dissection (typically true of items with
* subitems, which are probably FT_NONE or
* FT_PROTOCOL)
*
* or
*
* 2) if the tvbuff is "short" (either due to a short
* snapshot length or due to lack of reassembly of
* fragments/segments/whatever), we want to display
* what's available in the field (probably FT_BYTES
* or FT_STRING) and then throw an exception later
*
* or
*
* 3) the field is defined to be "what's left in the
* packet"
*
* so we set the length to what remains in the tvbuff so
* that, if we throw an exception while dissecting, it
* has what is probably the right value.
*
* For FT_STRINGZ, it means "the string is null-terminated,
* not null-padded; set the length to the actual length
* of the string", and if the tvbuff if short, we just
* throw an exception.
*
* For ENC_VARINT_PROTOBUF|ENC_VARINT_QUIC|ENC_VARIANT_ZIGZAG|ENC_VARINT_SDNV, it means "find the end of the string",
* and if the tvbuff if short, we just throw an exception.
*
* It's not valid for any other type of field. For those
* fields, we treat -1 the same way we treat other
* negative values - we assume the length is a Really
* Big Positive Number, and throw a ReportedBoundsError
* exception, under the assumption that the Really Big
* Length would run past the end of the packet.
*/
if ((FT_IS_INT(hfinfo->type)) || (FT_IS_UINT(hfinfo->type))) {
if (encoding & (ENC_VARINT_PROTOBUF|ENC_VARINT_ZIGZAG|ENC_VARINT_SDNV)) {
/*
* Leave the length as -1, so our caller knows
* it was -1.
*/
*item_length = *length;
return;
} else if (encoding & ENC_VARINT_QUIC) {
switch (tvb_get_guint8(tvb, start) >> 6)
{
case 0: /* 0b00 => 1 byte length (6 bits Usable) */
*item_length = 1;
break;
case 1: /* 0b01 => 2 bytes length (14 bits Usable) */
*item_length = 2;
break;
case 2: /* 0b10 => 4 bytes length (30 bits Usable) */
*item_length = 4;
break;
case 3: /* 0b11 => 8 bytes length (62 bits Usable) */
*item_length = 8;
break;
}
}
}
switch (hfinfo->type) {
case FT_PROTOCOL:
case FT_NONE:
case FT_BYTES:
case FT_STRING:
case FT_STRINGZPAD:
case FT_STRINGZTRUNC:
/*
* We allow FT_PROTOCOLs to be zero-length -
* for example, an ONC RPC NULL procedure has
* neither arguments nor reply, so the
* payload for that protocol is empty.
*
* We also allow the others to be zero-length -
* because that's the way the code has been for a
* long, long time.
*
* However, we want to ensure that the start
* offset is not *past* the byte past the end
* of the tvbuff: we throw an exception in that
* case.
*/
*length = tvb_captured_length(tvb) ? tvb_ensure_captured_length_remaining(tvb, start) : 0;
DISSECTOR_ASSERT(*length >= 0);
break;
case FT_STRINGZ:
/*
* Leave the length as -1, so our caller knows
* it was -1.
*/
break;
default:
THROW(ReportedBoundsError);
DISSECTOR_ASSERT_NOT_REACHED();
}
*item_length = *length;
} else {
*item_length = *length;
if (hfinfo->type == FT_PROTOCOL || hfinfo->type == FT_NONE) {
/*
* These types are for interior nodes of the
* tree, and don't have data associated with
* them; if the length is negative (XXX - see
* above) or goes past the end of the tvbuff,
* cut it short at the end of the tvbuff.
* That way, if this field is selected in
* Wireshark, we don't highlight stuff past
* the end of the data.
*/
/* XXX - what to do, if we don't have a tvb? */
if (tvb) {
length_remaining = tvb_captured_length_remaining(tvb, start);
if (*item_length < 0 ||
(*item_length > 0 &&
(length_remaining < *item_length)))
*item_length = length_remaining;
}
}
if (*item_length < 0) {
THROW(ReportedBoundsError);
}
}
}
static gint
get_full_length(header_field_info *hfinfo, tvbuff_t *tvb, const gint start,
gint length, guint item_length, const gint encoding)
{
guint32 n;
/*
* We need to get the correct item length here.
* That's normally done by proto_tree_new_item(),
* but we won't be calling it.
*/
switch (hfinfo->type) {
case FT_NONE:
case FT_PROTOCOL:
case FT_BYTES:
/*
* The length is the specified length.
*/
break;
case FT_UINT_BYTES:
n = get_uint_value(NULL, tvb, start, length, encoding);
item_length += n;
if ((int)item_length < length) {
THROW(ReportedBoundsError);
}
break;
/* XXX - make these just FT_UINT? */
case FT_UINT8:
case FT_UINT16:
case FT_UINT24:
case FT_UINT32:
case FT_UINT40:
case FT_UINT48:
case FT_UINT56:
case FT_UINT64:
/* XXX - make these just FT_INT? */
case FT_INT8:
case FT_INT16:
case FT_INT24:
case FT_INT32:
case FT_INT40:
case FT_INT48:
case FT_INT56:
case FT_INT64:
if (encoding & ENC_VARINT_MASK) {
if (length < -1) {
report_type_length_mismatch(NULL, "a FT_[U]INT", length, TRUE);
}
if (length == -1) {
guint64 dummy;
/* This can throw an exception */
/* XXX - do this without fetching the varint? */
length = tvb_get_varint(tvb, start, FT_VARINT_MAX_LEN, &dummy, encoding);
if (length == 0) {
THROW(ReportedBoundsError);
}
}
item_length = length;
break;
}
/*
* The length is the specified length.
*/
break;
case FT_BOOLEAN:
case FT_CHAR:
case FT_IPv4:
case FT_IPXNET:
case FT_IPv6:
case FT_FCWWN:
case FT_AX25:
case FT_VINES:
case FT_ETHER:
case FT_EUI64:
case FT_GUID:
case FT_OID:
case FT_REL_OID:
case FT_SYSTEM_ID:
case FT_FLOAT:
case FT_DOUBLE:
case FT_STRING:
/*
* The length is the specified length.
*/
break;
case FT_STRINGZ:
if (length < -1) {
report_type_length_mismatch(NULL, "a string", length, TRUE);
}
if (length == -1) {
/* This can throw an exception */
/* XXX - do this without fetching the string? */
wmem_free(NULL, tvb_get_stringz_enc(NULL, tvb, start, &length, encoding));
}
item_length = length;
break;
case FT_UINT_STRING:
n = get_uint_value(NULL, tvb, start, length, encoding & ~ENC_CHARENCODING_MASK);
item_length += n;
if ((int)item_length < length) {
THROW(ReportedBoundsError);
}
break;
case FT_STRINGZPAD:
case FT_STRINGZTRUNC:
case FT_ABSOLUTE_TIME:
case FT_RELATIVE_TIME:
case FT_IEEE_11073_SFLOAT:
case FT_IEEE_11073_FLOAT:
/*
* The length is the specified length.
*/
break;
default:
REPORT_DISSECTOR_BUG("field %s has type %d (%s) not handled in gset_full_length()",
hfinfo->abbrev,
hfinfo->type,
ftype_name(hfinfo->type));
break;
}
return item_length;
}
static field_info *
new_field_info(proto_tree *tree, header_field_info *hfinfo, tvbuff_t *tvb,
const gint start, const gint item_length)
{
field_info *fi;
FIELD_INFO_NEW(PNODE_POOL(tree), fi);
fi->hfinfo = hfinfo;
fi->start = start;
fi->start += (tvb)?tvb_raw_offset(tvb):0;
fi->length = item_length;
fi->tree_type = -1;
fi->flags = 0;
if (!PTREE_DATA(tree)->visible)
FI_SET_FLAG(fi, FI_HIDDEN);
fi->value = fvalue_new(fi->hfinfo->type);
fi->rep = NULL;
/* add the data source tvbuff */
fi->ds_tvb = tvb ? tvb_get_ds_tvb(tvb) : NULL;
fi->appendix_start = 0;
fi->appendix_length = 0;
fi->total_layer_num = tree->tree_data->pinfo->curr_layer_num;
fi->proto_layer_num = tree->tree_data->pinfo->curr_proto_layer_num;
return fi;
}
/* If the protocol tree is to be visible, set the representation of a
proto_tree entry with the name of the field for the item and with
the value formatted with the supplied printf-style format and
argument list. */
static void
proto_tree_set_representation_value(proto_item *pi, const char *format, va_list ap)
{
ws_assert(pi);
/* If the tree (GUI) or item isn't visible it's pointless for us to generate the protocol
* items string representation */
if (PTREE_DATA(pi)->visible && !proto_item_is_hidden(pi)) {
gsize name_pos, ret = 0;
char *str;
field_info *fi = PITEM_FINFO(pi);
header_field_info *hf;
DISSECTOR_ASSERT(fi);
hf = fi->hfinfo;
ITEM_LABEL_NEW(PNODE_POOL(pi), fi->rep);
if (hf->bitmask && (hf->type == FT_BOOLEAN || FT_IS_UINT(hf->type))) {
guint64 val;
char *p;
if (FT_IS_UINT32(hf->type))
val = fvalue_get_uinteger(fi->value);
else
val = fvalue_get_uinteger64(fi->value);
val <<= hfinfo_bitshift(hf);
p = decode_bitfield_value(fi->rep->representation, val, hf->bitmask, hfinfo_container_bitwidth(hf));
ret = (p - fi->rep->representation);
}
/* put in the hf name */
name_pos = ret = label_concat(fi->rep->representation, ret, hf->name);
ret = label_concat(fi->rep->representation, ret, ": ");
/* If possible, Put in the value of the string */
str = wmem_strdup_vprintf(PNODE_POOL(pi), format, ap);
WS_UTF_8_CHECK(str, -1);
ret = ws_label_strcpy(fi->rep->representation, ITEM_LABEL_LENGTH, ret, str, 0);
if (ret >= ITEM_LABEL_LENGTH) {
/* Uh oh, we don't have enough room. Tell the user
* that the field is truncated.
*/
label_mark_truncated(fi->rep->representation, name_pos);
}
}
}
/* If the protocol tree is to be visible, set the representation of a
proto_tree entry with the representation formatted with the supplied
printf-style format and argument list. */
static void
proto_tree_set_representation(proto_item *pi, const char *format, va_list ap)
{
gsize ret; /*tmp return value */
char *str;
field_info *fi = PITEM_FINFO(pi);
DISSECTOR_ASSERT(fi);
if (!proto_item_is_hidden(pi)) {
ITEM_LABEL_NEW(PNODE_POOL(pi), fi->rep);
str = wmem_strdup_vprintf(PNODE_POOL(pi), format, ap);
WS_UTF_8_CHECK(str, -1);
ret = ws_label_strcpy(fi->rep->representation, ITEM_LABEL_LENGTH, 0, str, 0);
if (ret >= ITEM_LABEL_LENGTH) {
/* Uh oh, we don't have enough room. Tell the user
* that the field is truncated.
*/
LABEL_MARK_TRUNCATED_START(fi->rep->representation);
}
}
}
static int
protoo_strlcpy(gchar *dest, const gchar *src, gsize dest_size)
{
if (dest_size == 0) return 0;
gsize res = g_strlcpy(dest, src, dest_size);
/* At most dest_size - 1 characters will be copied
* (unless dest_size is 0). */
if (res >= dest_size)
res = dest_size - 1;
return (int) res;
}
static header_field_info *
hfinfo_same_name_get_prev(const header_field_info *hfinfo)
{
header_field_info *dup_hfinfo;
if (hfinfo->same_name_prev_id == -1)
return NULL;
PROTO_REGISTRAR_GET_NTH(hfinfo->same_name_prev_id, dup_hfinfo);
return dup_hfinfo;
}
static void
hfinfo_remove_from_gpa_name_map(const header_field_info *hfinfo)
{
g_free(last_field_name);
last_field_name = NULL;
if (!hfinfo->same_name_next && hfinfo->same_name_prev_id == -1) {
/* No hfinfo with the same name */
g_hash_table_steal(gpa_name_map, hfinfo->abbrev);
return;
}
if (hfinfo->same_name_next) {
hfinfo->same_name_next->same_name_prev_id = hfinfo->same_name_prev_id;
}
if (hfinfo->same_name_prev_id != -1) {
header_field_info *same_name_prev = hfinfo_same_name_get_prev(hfinfo);
same_name_prev->same_name_next = hfinfo->same_name_next;
if (!hfinfo->same_name_next) {
/* It's always the latest added hfinfo which is stored in gpa_name_map */
g_hash_table_insert(gpa_name_map, (gpointer) (same_name_prev->abbrev), same_name_prev);
}
}
}
int
proto_item_fill_display_label(field_info *finfo, gchar *display_label_str, const int label_str_size)
{
header_field_info *hfinfo = finfo->hfinfo;
int label_len = 0;
char *tmp_str;
const char *str;
const guint8 *bytes;
guint32 number;
guint64 number64;
const char *hf_str_val;
char number_buf[48];
const char *number_out;
address addr;
ws_in4_addr ipv4;
const ws_in6_addr *ipv6;
switch (hfinfo->type) {
case FT_NONE:
case FT_PROTOCOL:
return protoo_strlcpy(display_label_str, UTF8_CHECK_MARK, label_str_size);
case FT_UINT_BYTES:
case FT_BYTES:
tmp_str = format_bytes_hfinfo_maxlen(NULL,
hfinfo,
fvalue_get_bytes_data(finfo->value),
(guint)fvalue_length2(finfo->value),
label_str_size);
label_len = protoo_strlcpy(display_label_str, tmp_str, label_str_size);
wmem_free(NULL, tmp_str);
break;
case FT_ABSOLUTE_TIME:
tmp_str = abs_time_to_str(NULL, fvalue_get_time(finfo->value), hfinfo->display, TRUE);
label_len = protoo_strlcpy(display_label_str, tmp_str, label_str_size);
wmem_free(NULL, tmp_str);
break;
case FT_RELATIVE_TIME:
tmp_str = rel_time_to_secs_str(NULL, fvalue_get_time(finfo->value));
label_len = protoo_strlcpy(display_label_str, tmp_str, label_str_size);
wmem_free(NULL, tmp_str);
break;
case FT_BOOLEAN:
number64 = fvalue_get_uinteger64(finfo->value);
label_len = protoo_strlcpy(display_label_str,
tfs_get_string(!!number64, hfinfo->strings), label_str_size);
break;
case FT_CHAR:
number = fvalue_get_uinteger(finfo->value);
if (FIELD_DISPLAY(hfinfo->display) == BASE_CUSTOM) {
gchar tmp[ITEM_LABEL_LENGTH];
custom_fmt_func_t fmtfunc = (custom_fmt_func_t)hfinfo->strings;
DISSECTOR_ASSERT(fmtfunc);
fmtfunc(tmp, number);
label_len = protoo_strlcpy(display_label_str, tmp, label_str_size);
} else if (hfinfo->strings) {
number_out = hf_try_val_to_str(number, hfinfo);
if (!number_out) {
number_out = hfinfo_char_value_format_display(BASE_HEX, number_buf, number);
}
label_len = protoo_strlcpy(display_label_str, number_out, label_str_size);
} else {
number_out = hfinfo_char_value_format(hfinfo, number_buf, number);
label_len = protoo_strlcpy(display_label_str, number_out, label_str_size);
}
break;
/* XXX - make these just FT_NUMBER? */
case FT_INT8:
case FT_INT16:
case FT_INT24:
case FT_INT32:
case FT_UINT8:
case FT_UINT16:
case FT_UINT24:
case FT_UINT32:
case FT_FRAMENUM:
hf_str_val = NULL;
number = FT_IS_INT(hfinfo->type) ?
(guint32) fvalue_get_sinteger(finfo->value) :
fvalue_get_uinteger(finfo->value);
if (FIELD_DISPLAY(hfinfo->display) == BASE_CUSTOM) {
gchar tmp[ITEM_LABEL_LENGTH];
custom_fmt_func_t fmtfunc = (custom_fmt_func_t)hfinfo->strings;
DISSECTOR_ASSERT(fmtfunc);
fmtfunc(tmp, number);
label_len = protoo_strlcpy(display_label_str, tmp, label_str_size);
} else if (hfinfo->strings && hfinfo->type != FT_FRAMENUM) {
if (hfinfo->display & BASE_UNIT_STRING) {
number_out = hfinfo_numeric_value_format(hfinfo, number_buf, number);
label_len = protoo_strlcpy(display_label_str, number_out, label_str_size);
hf_str_val = hf_try_val_to_str(number, hfinfo);
label_len += protoo_strlcpy(display_label_str+label_len, hf_str_val, label_str_size-label_len);
} else {
number_out = hf_try_val_to_str(number, hfinfo);
if (!number_out) {
number_out = hfinfo_number_value_format_display(hfinfo, hfinfo->display, number_buf, number);
}
label_len = protoo_strlcpy(display_label_str, number_out, label_str_size);
}
} else {
number_out = hfinfo_number_value_format(hfinfo, number_buf, number);
label_len = protoo_strlcpy(display_label_str, number_out, label_str_size);
}
break;
case FT_INT40:
case FT_INT48:
case FT_INT56:
case FT_INT64:
case FT_UINT40:
case FT_UINT48:
case FT_UINT56:
case FT_UINT64:
hf_str_val = NULL;
number64 = FT_IS_INT(hfinfo->type) ?
(guint64) fvalue_get_sinteger64(finfo->value) :
fvalue_get_uinteger64(finfo->value);
if (FIELD_DISPLAY(hfinfo->display) == BASE_CUSTOM) {
gchar tmp[ITEM_LABEL_LENGTH];
custom_fmt_func_64_t fmtfunc64 = (custom_fmt_func_64_t)hfinfo->strings;
DISSECTOR_ASSERT(fmtfunc64);
fmtfunc64(tmp, number64);
label_len = protoo_strlcpy(display_label_str, tmp, label_str_size);
} else if (hfinfo->strings) {
if (hfinfo->display & BASE_UNIT_STRING) {
number_out = hfinfo_numeric_value_format64(hfinfo, number_buf, number64);
label_len = protoo_strlcpy(display_label_str, number_out, label_str_size);
hf_str_val = hf_try_val64_to_str(number64, hfinfo);
label_len += protoo_strlcpy(display_label_str+label_len, hf_str_val, label_str_size-label_len);
} else {
number_out = hf_try_val64_to_str(number64, hfinfo);
if (!number_out)
number_out = hfinfo_number_value_format_display64(hfinfo, hfinfo->display, number_buf, number64);
label_len = protoo_strlcpy(display_label_str, number_out, label_str_size);
}
} else {
number_out = hfinfo_number_value_format64(hfinfo, number_buf, number64);
label_len = protoo_strlcpy(display_label_str, number_out, label_str_size);
}
break;
case FT_EUI64:
tmp_str = eui64_to_str(NULL, fvalue_get_uinteger64(finfo->value));
label_len = protoo_strlcpy(display_label_str, tmp_str, label_str_size);
wmem_free(NULL, tmp_str);
break;
case FT_IPv4:
ipv4 = fvalue_get_uinteger(finfo->value);
set_address (&addr, AT_IPv4, 4, &ipv4);
tmp_str = address_to_display(NULL, &addr);
label_len = protoo_strlcpy(display_label_str, tmp_str, label_str_size);
wmem_free(NULL, tmp_str);
break;
case FT_IPv6:
ipv6 = fvalue_get_ipv6(finfo->value);
set_address (&addr, AT_IPv6, sizeof(ws_in6_addr), ipv6);
tmp_str = address_to_display(NULL, &addr);
label_len = protoo_strlcpy(display_label_str, tmp_str, label_str_size);
wmem_free(NULL, tmp_str);
break;
case FT_FCWWN:
set_address (&addr, AT_FCWWN, FCWWN_ADDR_LEN, fvalue_get_bytes_data(finfo->value));
tmp_str = address_to_display(NULL, &addr);
label_len = protoo_strlcpy(display_label_str, tmp_str, label_str_size);
wmem_free(NULL, tmp_str);
break;
case FT_ETHER:
set_address (&addr, AT_ETHER, FT_ETHER_LEN, fvalue_get_bytes_data(finfo->value));
tmp_str = address_to_display(NULL, &addr);
label_len = protoo_strlcpy(display_label_str, tmp_str, label_str_size);
wmem_free(NULL, tmp_str);
break;
case FT_GUID:
tmp_str = guid_to_str(NULL, fvalue_get_guid(finfo->value));
label_len = protoo_strlcpy(display_label_str, tmp_str, label_str_size);
wmem_free(NULL, tmp_str);
break;
case FT_REL_OID:
bytes = fvalue_get_bytes_data(finfo->value);
tmp_str = rel_oid_resolved_from_encoded(NULL, bytes, (int)fvalue_length2(finfo->value));
label_len = protoo_strlcpy(display_label_str, tmp_str, label_str_size);
wmem_free(NULL, tmp_str);
break;
case FT_OID:
bytes = fvalue_get_bytes_data(finfo->value);
tmp_str = oid_resolved_from_encoded(NULL, bytes, (int)fvalue_length2(finfo->value));
label_len = protoo_strlcpy(display_label_str, tmp_str, label_str_size);
wmem_free(NULL, tmp_str);
break;
case FT_SYSTEM_ID:
bytes = fvalue_get_bytes_data(finfo->value);
tmp_str = print_system_id(NULL, bytes, (int)fvalue_length2(finfo->value));
label_len = protoo_strlcpy(display_label_str, tmp_str, label_str_size);
wmem_free(NULL, tmp_str);
break;
case FT_FLOAT:
case FT_DOUBLE:
label_len = (int)fill_display_label_float(finfo, display_label_str);
break;
case FT_STRING:
case FT_STRINGZ:
case FT_UINT_STRING:
case FT_STRINGZPAD:
case FT_STRINGZTRUNC:
str = fvalue_get_string(finfo->value);
label_len = (int)ws_label_strcpy(display_label_str, label_str_size, 0, str, label_strcat_flags(hfinfo));
if (label_len >= label_str_size) {
/* Truncation occured. Get the real length
* copied (not including '\0') */
label_len = label_str_size ? label_str_size - 1 : 0;
}
break;
default:
/* First try ftype string representation */
tmp_str = fvalue_to_string_repr(NULL, finfo->value, FTREPR_DISPLAY, hfinfo->display);
if (!tmp_str) {
/* Default to show as bytes */
bytes = fvalue_get_bytes_data(finfo->value);
tmp_str = bytes_to_str(NULL, bytes, fvalue_length2(finfo->value));
}
label_len = protoo_strlcpy(display_label_str, tmp_str, label_str_size);
wmem_free(NULL, tmp_str);
break;
}
return label_len;
}
/* -------------------------- */
/* Sets the text for a custom column from proto fields.
*
* @param[out] result The "resolved" column text (human readable, uses strings)
* @param[out] expr The "unresolved" column text (values, display repr)
* @return The filter (abbrev) for the field (XXX: Only the first if multifield)
*/
const gchar *
proto_custom_set(proto_tree* tree, GSList *field_ids, gint occurrence,
gchar *result, gchar *expr, const int size)
{
int len, prev_len, last, i, offset_r = 0, offset_e = 0;
GPtrArray *finfos;
field_info *finfo = NULL;
header_field_info* hfinfo;
const gchar *abbrev = NULL;
const char *hf_str_val;
char *str;
int *field_idx;
int field_id;
int ii = 0;
ws_assert(field_ids != NULL);
while ((field_idx = (int *) g_slist_nth_data(field_ids, ii++))) {
field_id = *field_idx;
PROTO_REGISTRAR_GET_NTH((guint)field_id, hfinfo);
/* do we need to rewind ? */
if (!hfinfo)
return "";
if (occurrence < 0) {
/* Search other direction */
while (hfinfo->same_name_prev_id != -1) {
PROTO_REGISTRAR_GET_NTH(hfinfo->same_name_prev_id, hfinfo);
}
}
prev_len = 0; /* Reset handled occurrences */
while (hfinfo) {
finfos = proto_get_finfo_ptr_array(tree, hfinfo->id);
if (!finfos || !(len = g_ptr_array_len(finfos))) {
if (occurrence < 0) {
hfinfo = hfinfo->same_name_next;
} else {
hfinfo = hfinfo_same_name_get_prev(hfinfo);
}
continue;
}
/* Are there enough occurrences of the field? */
if (((occurrence - prev_len) > len) || ((occurrence + prev_len) < -len)) {
if (occurrence < 0) {
hfinfo = hfinfo->same_name_next;
} else {
hfinfo = hfinfo_same_name_get_prev(hfinfo);
}
prev_len += len;
continue;
}
/* Calculate single index or set outer bounderies */
if (occurrence < 0) {
i = occurrence + len + prev_len;
last = i;
} else if (occurrence > 0) {
i = occurrence - 1 - prev_len;
last = i;
} else {
i = 0;
last = len - 1;
}
prev_len += len; /* Count handled occurrences */
while (i <= last) {
finfo = (field_info *)g_ptr_array_index(finfos, i);
if (offset_r && (offset_r < (size - 1)))
result[offset_r++] = ',';
switch (hfinfo->type) {
case FT_NONE:
case FT_PROTOCOL:
/* Prevent multiple check marks */
if (strstr(result, UTF8_CHECK_MARK ",") == NULL) {
offset_r += proto_item_fill_display_label(finfo, result+offset_r, size-offset_r);
} else {
result[--offset_r] = '\0'; /* Remove the added trailing ',' again */
}
break;
default:
offset_r += proto_item_fill_display_label(finfo, result+offset_r, size-offset_r);
break;
}
if (offset_e && (offset_e < (size - 1)))
expr[offset_e++] = ',';
if (hfinfo->strings && hfinfo->type != FT_FRAMENUM && FIELD_DISPLAY(hfinfo->display) == BASE_NONE && (FT_IS_INT(hfinfo->type) || FT_IS_UINT(hfinfo->type))) {
/* Integer types with BASE_NONE never get the numeric value. */
if (FT_IS_INT32(hfinfo->type)) {
hf_str_val = hf_try_val_to_str_const(fvalue_get_sinteger(finfo->value), hfinfo, "Unknown");
} else if (FT_IS_UINT32(hfinfo->type)) {
hf_str_val = hf_try_val_to_str_const(fvalue_get_uinteger(finfo->value), hfinfo, "Unknown");
} else if (FT_IS_INT64(hfinfo->type)) {
hf_str_val = hf_try_val64_to_str_const(fvalue_get_sinteger64(finfo->value), hfinfo, "Unknown");
} else { // if (FT_IS_UINT64(hfinfo->type)) {
hf_str_val = hf_try_val64_to_str_const(fvalue_get_uinteger64(finfo->value), hfinfo, "Unknown");
}
snprintf(expr+offset_e, size-offset_e, "\"%s\"", hf_str_val);
offset_e = (int)strlen(expr);
} else if (hfinfo->type == FT_NONE || hfinfo->type == FT_PROTOCOL) {
/* Prevent multiple check marks */
if (strstr(expr, UTF8_CHECK_MARK ",") == NULL) {
offset_e += proto_item_fill_display_label(finfo, expr+offset_e, size-offset_e);
} else {
expr[--offset_e] = '\0'; /* Remove the added trailing ',' again */
}
} else {
str = fvalue_to_string_repr(NULL, finfo->value, FTREPR_DISPLAY, finfo->hfinfo->display);
offset_e += protoo_strlcpy(expr+offset_e, str, size-offset_e);
wmem_free(NULL, str);
}
i++;
}
/* XXX: Why is only the first abbreviation returned for a multifield
* custom column? */
if (!abbrev) {
/* Store abbrev for return value */
abbrev = hfinfo->abbrev;
}
if (occurrence == 0) {
/* Fetch next hfinfo with same name (abbrev) */
hfinfo = hfinfo_same_name_get_prev(hfinfo);
} else {
hfinfo = NULL;
}
}
}
if (offset_r >= (size - 1)) {
mark_truncated(result, 0, size);
}
if (offset_e >= (size - 1)) {
mark_truncated(expr, 0, size);
}
return abbrev ? abbrev : "";
}
gchar *
proto_custom_get_filter(epan_dissect_t* edt, GSList *field_ids, gint occurrence)
{
int len, prev_len, last, i;
GPtrArray *finfos;
field_info *finfo = NULL;
header_field_info* hfinfo;
char *filter = NULL;
GPtrArray *filter_array;
int field_id;
ws_assert(field_ids != NULL);
filter_array = g_ptr_array_new_full(g_slist_length(field_ids), g_free);
for (GSList *iter = field_ids; iter; iter = iter->next) {
field_id = *(int *)iter->data;
PROTO_REGISTRAR_GET_NTH((guint)field_id, hfinfo);
/* do we need to rewind ? */
if (!hfinfo)
return NULL;
if (occurrence < 0) {
/* Search other direction */
while (hfinfo->same_name_prev_id != -1) {
PROTO_REGISTRAR_GET_NTH(hfinfo->same_name_prev_id, hfinfo);
}
}
prev_len = 0; /* Reset handled occurrences */
while (hfinfo) {
finfos = proto_get_finfo_ptr_array(edt->tree, hfinfo->id);
if (!finfos || !(len = g_ptr_array_len(finfos))) {
if (occurrence < 0) {
hfinfo = hfinfo->same_name_next;
} else {
hfinfo = hfinfo_same_name_get_prev(hfinfo);
}
continue;
}
/* Are there enough occurrences of the field? */
if (((occurrence - prev_len) > len) || ((occurrence + prev_len) < -len)) {
if (occurrence < 0) {
hfinfo = hfinfo->same_name_next;
} else {
hfinfo = hfinfo_same_name_get_prev(hfinfo);
}
prev_len += len;
continue;
}
/* Calculate single index or set outer bounderies */
if (occurrence < 0) {
i = occurrence + len + prev_len;
last = i;
} else if (occurrence > 0) {
i = occurrence - 1 - prev_len;
last = i;
} else {
i = 0;
last = len - 1;
}
prev_len += len; /* Count handled occurrences */
while (i <= last) {
finfo = (field_info *)g_ptr_array_index(finfos, i);
filter = proto_construct_match_selected_string(finfo, edt);
if (filter) {
/* Only add the same expression once (especially for FT_PROTOCOL).
* The ptr array doesn't have NULL entries so g_str_equal is fine.
*/
if (!g_ptr_array_find_with_equal_func(filter_array, filter, g_str_equal, NULL)) {
g_ptr_array_add(filter_array, filter);
}
}
i++;
}
if (occurrence == 0) {
/* Fetch next hfinfo with same name (abbrev) */
hfinfo = hfinfo_same_name_get_prev(hfinfo);
} else {
hfinfo = NULL;
}
}
}
g_ptr_array_add(filter_array, NULL);
/* XXX: Should this be || or && ? */
gchar *output = g_strjoinv(" || ", (char **)filter_array->pdata);
g_ptr_array_free(filter_array, TRUE);
return output;
}
/* Set text of proto_item after having already been created. */
void
proto_item_set_text(proto_item *pi, const char *format, ...)
{
field_info *fi = NULL;
va_list ap;
TRY_TO_FAKE_THIS_REPR_VOID(pi);
fi = PITEM_FINFO(pi);
if (fi == NULL)
return;
if (fi->rep) {
ITEM_LABEL_FREE(PNODE_POOL(pi), fi->rep);
fi->rep = NULL;
}
va_start(ap, format);
proto_tree_set_representation(pi, format, ap);
va_end(ap);
}
/* Append to text of proto_item after having already been created. */
void
proto_item_append_text(proto_item *pi, const char *format, ...)
{
field_info *fi = NULL;
size_t curlen;
char *str;
va_list ap;
TRY_TO_FAKE_THIS_REPR_VOID(pi);
fi = PITEM_FINFO(pi);
if (fi == NULL) {
return;
}
if (!proto_item_is_hidden(pi)) {
/*
* If we don't already have a representation,
* generate the default representation.
*/
if (fi->rep == NULL) {
ITEM_LABEL_NEW(PNODE_POOL(pi), fi->rep);
proto_item_fill_label(fi, fi->rep->representation);
}
if (fi->rep) {
curlen = strlen(fi->rep->representation);
/* curlen doesn't include the \0 byte.
* XXX: If curlen + 4 > ITEM_LABEL_LENGTH, we can't tell if
* the representation has already been truncated (of an up
* to 4 byte UTF-8 character) or is just at the maximum length
* unless we search for " [truncated]" (which may not be
* at the start.)
* It's safer to do nothing.
*/
if (ITEM_LABEL_LENGTH > (curlen + 4)) {
va_start(ap, format);
str = wmem_strdup_vprintf(PNODE_POOL(pi), format, ap);
va_end(ap);
WS_UTF_8_CHECK(str, -1);
curlen = ws_label_strcpy(fi->rep->representation, ITEM_LABEL_LENGTH, curlen, str, 0);
if (curlen >= ITEM_LABEL_LENGTH) {
/* Uh oh, we don't have enough room. Tell the user
* that the field is truncated.
*/
LABEL_MARK_TRUNCATED_START(fi->rep->representation);
}
}
}
}
}
/* Prepend to text of proto_item after having already been created. */
void
proto_item_prepend_text(proto_item *pi, const char *format, ...)
{
field_info *fi = NULL;
gsize pos;
char representation[ITEM_LABEL_LENGTH];
char *str;
va_list ap;
TRY_TO_FAKE_THIS_REPR_VOID(pi);
fi = PITEM_FINFO(pi);
if (fi == NULL) {
return;
}
if (!proto_item_is_hidden(pi)) {
/*
* If we don't already have a representation,
* generate the default representation.
*/
if (fi->rep == NULL) {
ITEM_LABEL_NEW(PNODE_POOL(pi), fi->rep);
proto_item_fill_label(fi, representation);
} else
(void) g_strlcpy(representation, fi->rep->representation, ITEM_LABEL_LENGTH);
va_start(ap, format);
str = wmem_strdup_vprintf(PNODE_POOL(pi), format, ap);
va_end(ap);
WS_UTF_8_CHECK(str, -1);
pos = ws_label_strcpy(fi->rep->representation, ITEM_LABEL_LENGTH, 0, str, 0);
pos = ws_label_strcpy(fi->rep->representation, ITEM_LABEL_LENGTH, pos, representation, 0);
/* XXX: As above, if the old representation is close to the label
* length, it might already be marked as truncated. */
if (pos >= ITEM_LABEL_LENGTH && (strlen(representation) + 4) <= ITEM_LABEL_LENGTH) {
/* Uh oh, we don't have enough room. Tell the user
* that the field is truncated.
*/
LABEL_MARK_TRUNCATED_START(fi->rep->representation);
}
}
}
static void
finfo_set_len(field_info *fi, const gint length)
{
gint length_remaining;
DISSECTOR_ASSERT_HINT(length >= 0, fi->hfinfo->abbrev);
length_remaining = tvb_captured_length_remaining(fi->ds_tvb, fi->start);
if (length > length_remaining)
fi->length = length_remaining;
else
fi->length = length;
/* If we have an FT_PROTOCOL we need to set the length of the fvalue tvbuff as well. */
if (fvalue_type_ftenum(fi->value) == FT_PROTOCOL) {
fvalue_set_protocol(fi->value, NULL, NULL, fi->length);
}
/*
* You cannot just make the "len" field of a GByteArray
* larger, if there's no data to back that length;
* you can only make it smaller.
*/
if (fvalue_type_ftenum(fi->value) == FT_BYTES && fi->length > 0) {
GBytes *bytes = fvalue_get_bytes(fi->value);
gsize size;
const void *data = g_bytes_get_data(bytes, &size);
if ((gsize)fi->length <= size) {
fvalue_set_bytes_data(fi->value, data, fi->length);
}
g_bytes_unref(bytes);
}
}
void
proto_item_set_len(proto_item *pi, const gint length)
{
field_info *fi;
/* XXX: We actually want to set the length of non visible proto_items
* that are protocols, if we're not faking protocols, so that
* ui/proto_hier_stats can work correctly. (#17877) But we don't
* want to set the length of the protocol accidentally when intending
* to set the length of a faked child. However, we can't tell whether
* this is called by the original item or the child when faking items.
*/
TRY_TO_FAKE_THIS_REPR_VOID(pi);
fi = PITEM_FINFO(pi);
if (fi == NULL)
return;
finfo_set_len(fi, length);
}
/*
* Sets the length of the item based on its start and on the specified
* offset, which is the offset past the end of the item; as the start
* in the item is relative to the beginning of the data source tvbuff,
* we need to pass in a tvbuff - the end offset is relative to the beginning
* of that tvbuff.
*/
void
proto_item_set_end(proto_item *pi, tvbuff_t *tvb, gint end)
{
field_info *fi;
gint length;
TRY_TO_FAKE_THIS_REPR_VOID(pi);
fi = PITEM_FINFO(pi);
if (fi == NULL)
return;
end += tvb_raw_offset(tvb);
DISSECTOR_ASSERT(end >= fi->start);
length = end - fi->start;
finfo_set_len(fi, length);
}
int
proto_item_get_len(const proto_item *pi)
{
field_info *fi;
if (!pi)
return -1;
fi = PITEM_FINFO(pi);
return fi ? fi->length : -1;
}
void
proto_item_set_bits_offset_len(proto_item *ti, int bits_offset, int bits_len) {
if (!ti) {
return;
}
FI_SET_FLAG(PNODE_FINFO(ti), FI_BITS_OFFSET(bits_offset));
FI_SET_FLAG(PNODE_FINFO(ti), FI_BITS_SIZE(bits_len));
}
char *
proto_item_get_display_repr(wmem_allocator_t *scope, proto_item *pi)
{
field_info *fi;
if (!pi)
return "";
fi = PITEM_FINFO(pi);
DISSECTOR_ASSERT(fi->hfinfo != NULL);
return fvalue_to_string_repr(scope, fi->value, FTREPR_DISPLAY, fi->hfinfo->display);
}
proto_tree *
proto_tree_create_root(packet_info *pinfo)
{
proto_node *pnode;
/* Initialize the proto_node */
pnode = g_slice_new(proto_tree);
PROTO_NODE_INIT(pnode);
pnode->parent = NULL;
PNODE_FINFO(pnode) = NULL;
pnode->tree_data = g_slice_new(tree_data_t);
/* Make sure we can access pinfo everywhere */
pnode->tree_data->pinfo = pinfo;
/* Don't initialize the tree_data_t. Wait until we know we need it */
pnode->tree_data->interesting_hfids = NULL;
/* Set the default to FALSE so it's easier to
* find errors; if we expect to see the protocol tree
* but for some reason the default 'visible' is not
* changed, then we'll find out very quickly. */
pnode->tree_data->visible = FALSE;
/* Make sure that we fake protocols (if possible) */
pnode->tree_data->fake_protocols = TRUE;
/* Keep track of the number of children */
pnode->tree_data->count = 0;
return (proto_tree *)pnode;
}
/* "prime" a proto_tree with a single hfid that a dfilter
* is interested in. */
void
proto_tree_prime_with_hfid(proto_tree *tree _U_, const gint hfid)
{
header_field_info *hfinfo;
PROTO_REGISTRAR_GET_NTH(hfid, hfinfo);
/* this field is referenced by a filter so increase the refcount.
also increase the refcount for the parent, i.e the protocol.
*/
hfinfo->ref_type = HF_REF_TYPE_DIRECT;
/* only increase the refcount if there is a parent.
if this is a protocol and not a field then parent will be -1
and there is no parent to add any refcounting for.
*/
if (hfinfo->parent != -1) {
header_field_info *parent_hfinfo;
PROTO_REGISTRAR_GET_NTH(hfinfo->parent, parent_hfinfo);
/* Mark parent as indirectly referenced unless it is already directly
* referenced, i.e. the user has specified the parent in a filter.
*/
if (parent_hfinfo->ref_type != HF_REF_TYPE_DIRECT)
parent_hfinfo->ref_type = HF_REF_TYPE_INDIRECT;
}
}
proto_tree *
proto_item_add_subtree(proto_item *pi, const gint idx) {
field_info *fi;
if (!pi)
return NULL;
DISSECTOR_ASSERT(idx >= 0 && idx < num_tree_types);
fi = PITEM_FINFO(pi);
if (!fi)
return (proto_tree *)pi;
fi->tree_type = idx;
return (proto_tree *)pi;
}
proto_tree *
proto_item_get_subtree(proto_item *pi) {
field_info *fi;
if (!pi)
return NULL;
fi = PITEM_FINFO(pi);
if ( (!fi) || (fi->tree_type == -1) )
return NULL;
return (proto_tree *)pi;
}
proto_item *
proto_item_get_parent(const proto_item *ti) {
/* XXX: If we're faking items, this will return the parent of the
* faked item, which may not be the logical parent expected.
* We have no way of knowing exactly which real item the fake
* item refers to here (the original item or one of its children
* using it as a fake), and thus whether the parent should be the
* faked item itself or the faked item's parent.
*
* In that case, there's a good chance we end up returning the
* root node of the protocol tree, which has "PNODE_FINFO()" null.
*
* If we later add items to _that_, they will not be faked even though
* they _should_ be, hurting performance (#8069). Also protocol
* hierarchy stats (which fakes everything but protocols) may
* behave oddly if unexpected items are added under the root node.
*/
if (!ti)
return NULL;
return ti->parent;
}
proto_item *
proto_item_get_parent_nth(proto_item *ti, int gen) {
/* XXX: Same issue as above, even more so. */
if (!ti)
return NULL;
while (gen--) {
ti = ti->parent;
if (!ti)
return NULL;
}
return ti;
}
proto_item *
proto_tree_get_parent(proto_tree *tree) {
if (!tree)
return NULL;
return (proto_item *)tree;
}
proto_tree *
proto_tree_get_parent_tree(proto_tree *tree) {
/* XXX: Same issue as proto_item_get_parent */
if (!tree)
return NULL;
/* we're the root tree, there's no parent
return ourselves so the caller has at least a tree to attach to */
if (!tree->parent)
return tree;
return (proto_tree *)tree->parent;
}
proto_tree *
proto_tree_get_root(proto_tree *tree) {
if (!tree)
return NULL;
while (tree->parent) {
tree = tree->parent;
}
return tree;
}
void
proto_tree_move_item(proto_tree *tree, proto_item *fixed_item,
proto_item *item_to_move)
{
/* This function doesn't generate any values. It only reorganizes the prococol tree
* so we can bail out immediately if it isn't visible. */
if (!tree || !PTREE_DATA(tree)->visible)
return;
DISSECTOR_ASSERT(item_to_move->parent == tree);
DISSECTOR_ASSERT(fixed_item->parent == tree);
/*** cut item_to_move out ***/
/* is item_to_move the first? */
if (tree->first_child == item_to_move) {
/* simply change first child to next */
tree->first_child = item_to_move->next;
DISSECTOR_ASSERT(tree->last_child != item_to_move);
} else {
proto_item *curr_item;
/* find previous and change it's next */
for (curr_item = tree->first_child; curr_item != NULL; curr_item = curr_item->next) {
if (curr_item->next == item_to_move) {
break;
}
}
DISSECTOR_ASSERT(curr_item);
curr_item->next = item_to_move->next;
/* fix last_child if required */
if (tree->last_child == item_to_move) {
tree->last_child = curr_item;
}
}
/*** insert to_move after fixed ***/
item_to_move->next = fixed_item->next;
fixed_item->next = item_to_move;
if (tree->last_child == fixed_item) {
tree->last_child = item_to_move;
}
}
void
proto_tree_set_appendix(proto_tree *tree, tvbuff_t *tvb, gint start,
const gint length)
{
field_info *fi;
if (tree == NULL)
return;
fi = PTREE_FINFO(tree);
if (fi == NULL)
return;
start += tvb_raw_offset(tvb);
DISSECTOR_ASSERT(start >= 0);
DISSECTOR_ASSERT(length >= 0);
fi->appendix_start = start;
fi->appendix_length = length;
}
static void
check_protocol_filter_name_or_fail(const char *filter_name)
{
/* Require at least two characters. */
if (filter_name[0] == '\0' || filter_name[1] == '\0') {
REPORT_DISSECTOR_BUG("Protocol filter name \"%s\" cannot have length less than two.", filter_name);
}
if (proto_check_field_name(filter_name) != '\0') {
REPORT_DISSECTOR_BUG("Protocol filter name \"%s\" has one or more invalid characters."
" Allowed are letters, digits, '-', '_' and non-repeating '.'."
" This might be caused by an inappropriate plugin or a development error.", filter_name);
}
/* Check that it doesn't match some very common numeric forms. */
if (filter_name[0] == '0' &&
(filter_name[1] == 'x' || filter_name[1] == 'X' ||
filter_name[1] == 'b' || filter_name[1] == 'B')) {
REPORT_DISSECTOR_BUG("Protocol filter name \"%s\" cannot start with \"%c%c\".",
filter_name, filter_name[0], filter_name[1]);
}
/* Names starting with a digit must not contain a minus sign (currently not checked at runtime). */
/* Check that it contains at least one letter. */
bool have_letter = false;
for (const char *s = filter_name; *s != '\0'; s++) {
if (g_ascii_isalpha(*s)) {
have_letter = true;
break;
}
}
if (!have_letter) {
REPORT_DISSECTOR_BUG("Protocol filter name \"%s\" must contain at least one letter a-z.",
filter_name);
}
/* Check for reserved keywords. */
if (g_hash_table_contains(proto_reserved_filter_names, filter_name)) {
REPORT_DISSECTOR_BUG("Protocol filter name \"%s\" is invalid because it is a reserved keyword."
" This might be caused by an inappropriate plugin or a development error.", filter_name);
}
}
int
proto_register_protocol(const char *name, const char *short_name,
const char *filter_name)
{
protocol_t *protocol;
header_field_info *hfinfo;
/*
* Make sure there's not already a protocol with any of those
* names. Crash if there is, as that's an error in the code
* or an inappropriate plugin.
* This situation has to be fixed to not register more than one
* protocol with the same name.
*/
if (g_hash_table_lookup(proto_names, name)) {
/* ws_error will terminate the program */
REPORT_DISSECTOR_BUG("Duplicate protocol name \"%s\"!"
" This might be caused by an inappropriate plugin or a development error.", name);
}
if (g_hash_table_lookup(proto_short_names, short_name)) {
REPORT_DISSECTOR_BUG("Duplicate protocol short_name \"%s\"!"
" This might be caused by an inappropriate plugin or a development error.", short_name);
}
check_protocol_filter_name_or_fail(filter_name);
if (g_hash_table_lookup(proto_filter_names, filter_name)) {
REPORT_DISSECTOR_BUG("Duplicate protocol filter_name \"%s\"!"
" This might be caused by an inappropriate plugin or a development error.", filter_name);
}
/*
* Add this protocol to the list of known protocols;
* the list is sorted by protocol short name.
*/
protocol = g_new(protocol_t, 1);
protocol->name = name;
protocol->short_name = short_name;
protocol->filter_name = filter_name;
protocol->fields = NULL; /* Delegate until actually needed */
protocol->is_enabled = TRUE; /* protocol is enabled by default */
protocol->enabled_by_default = TRUE; /* see previous comment */
protocol->can_toggle = TRUE;
protocol->parent_proto_id = -1;
protocol->heur_list = NULL;
/* List will be sorted later by name, when all protocols completed registering */
protocols = g_list_prepend(protocols, protocol);
g_hash_table_insert(proto_names, (gpointer)name, protocol);
g_hash_table_insert(proto_filter_names, (gpointer)filter_name, protocol);
g_hash_table_insert(proto_short_names, (gpointer)short_name, protocol);
/* Here we allocate a new header_field_info struct */
hfinfo = g_slice_new(header_field_info);
hfinfo->name = name;
hfinfo->abbrev = filter_name;
hfinfo->type = FT_PROTOCOL;
hfinfo->display = BASE_NONE;
hfinfo->strings = protocol;
hfinfo->bitmask = 0;
hfinfo->ref_type = HF_REF_TYPE_NONE;
hfinfo->blurb = NULL;
hfinfo->parent = -1; /* This field differentiates protos and fields */
protocol->proto_id = proto_register_field_init(hfinfo, hfinfo->parent);
return protocol->proto_id;
}
int
proto_register_protocol_in_name_only(const char *name, const char *short_name, const char *filter_name, int parent_proto, enum ftenum field_type)
{
protocol_t *protocol;
header_field_info *hfinfo;
/*
* Helper protocols don't need the strict rules as a "regular" protocol
* Just register it in a list and make a hf_ field from it
*/
if ((field_type != FT_PROTOCOL) && (field_type != FT_BYTES)) {
REPORT_DISSECTOR_BUG("Pino \"%s\" must be of type FT_PROTOCOL or FT_BYTES.", name);
}
if (parent_proto < 0) {
REPORT_DISSECTOR_BUG("Must have a valid parent protocol for helper protocol \"%s\"!"
" This might be caused by an inappropriate plugin or a development error.", name);
}
check_protocol_filter_name_or_fail(filter_name);
/* Add this protocol to the list of helper protocols (just so it can be properly freed) */
protocol = g_new(protocol_t, 1);
protocol->name = name;
protocol->short_name = short_name;
protocol->filter_name = filter_name;
protocol->fields = NULL; /* Delegate until actually needed */
/* Enabling and toggling is really determined by parent protocol,
but provide default values here */
protocol->is_enabled = TRUE;
protocol->enabled_by_default = TRUE;
protocol->can_toggle = TRUE;
protocol->parent_proto_id = parent_proto;
protocol->heur_list = NULL;
/* List will be sorted later by name, when all protocols completed registering */
protocols = g_list_prepend(protocols, protocol);
/* Here we allocate a new header_field_info struct */
hfinfo = g_slice_new(header_field_info);
hfinfo->name = name;
hfinfo->abbrev = filter_name;
hfinfo->type = field_type;
hfinfo->display = BASE_NONE;
if (field_type == FT_BYTES) {
hfinfo->display |= (BASE_NO_DISPLAY_VALUE|BASE_PROTOCOL_INFO);
}
hfinfo->strings = protocol;
hfinfo->bitmask = 0;
hfinfo->ref_type = HF_REF_TYPE_NONE;
hfinfo->blurb = NULL;
hfinfo->parent = -1; /* This field differentiates protos and fields */
protocol->proto_id = proto_register_field_init(hfinfo, hfinfo->parent);
return protocol->proto_id;
}
gboolean
proto_deregister_protocol(const char *short_name)
{
protocol_t *protocol;
header_field_info *hfinfo;
int proto_id;
guint i;
proto_id = proto_get_id_by_short_name(short_name);
protocol = find_protocol_by_id(proto_id);
if (protocol == NULL)
return FALSE;
g_hash_table_remove(proto_names, protocol->name);
g_hash_table_remove(proto_short_names, (gpointer)short_name);
g_hash_table_remove(proto_filter_names, (gpointer)protocol->filter_name);
if (protocol->fields) {
for (i = 0; i < protocol->fields->len; i++) {
hfinfo = (header_field_info *)g_ptr_array_index(protocol->fields, i);
hfinfo_remove_from_gpa_name_map(hfinfo);
expert_deregister_expertinfo(hfinfo->abbrev);
g_ptr_array_add(deregistered_fields, gpa_hfinfo.hfi[hfinfo->id]);
}
g_ptr_array_free(protocol->fields, TRUE);
protocol->fields = NULL;
}
g_list_free(protocol->heur_list);
/* Remove this protocol from the list of known protocols */
protocols = g_list_remove(protocols, protocol);
g_ptr_array_add(deregistered_fields, gpa_hfinfo.hfi[proto_id]);
g_hash_table_steal(gpa_name_map, protocol->filter_name);
g_free(last_field_name);
last_field_name = NULL;
return TRUE;
}
void
proto_register_alias(const int proto_id, const char *alias_name)
{
protocol_t *protocol;
protocol = find_protocol_by_id(proto_id);
if (alias_name && protocol) {
g_hash_table_insert(gpa_protocol_aliases, (gpointer) alias_name, (gpointer)protocol->filter_name);
}
}
/*
* Routines to use to iterate over the protocols.
* The argument passed to the iterator routines is an opaque cookie to
* their callers; it's the GList pointer for the current element in
* the list.
* The ID of the protocol is returned, or -1 if there is no protocol.
*/
int
proto_get_first_protocol(void **cookie)
{
protocol_t *protocol;
if (protocols == NULL)
return -1;
*cookie = protocols;
protocol = (protocol_t *)protocols->data;
return protocol->proto_id;
}
int
proto_get_data_protocol(void *cookie)
{
GList *list_item = (GList *)cookie;
protocol_t *protocol = (protocol_t *)list_item->data;
return protocol->proto_id;
}
int
proto_get_next_protocol(void **cookie)
{
GList *list_item = (GList *)*cookie;
protocol_t *protocol;
list_item = g_list_next(list_item);
if (list_item == NULL)
return -1;
*cookie = list_item;
protocol = (protocol_t *)list_item->data;
return protocol->proto_id;
}
header_field_info *
proto_get_first_protocol_field(const int proto_id, void **cookie)
{
protocol_t *protocol = find_protocol_by_id(proto_id);
if ((protocol == NULL) || (protocol->fields == NULL) || (protocol->fields->len == 0))
return NULL;
*cookie = GUINT_TO_POINTER(0);
return (header_field_info *)g_ptr_array_index(protocol->fields, 0);
}
header_field_info *
proto_get_next_protocol_field(const int proto_id, void **cookie)
{
protocol_t *protocol = find_protocol_by_id(proto_id);
guint i = GPOINTER_TO_UINT(*cookie);
i++;
if ((protocol->fields == NULL) || (i >= protocol->fields->len))
return NULL;
*cookie = GUINT_TO_POINTER(i);
return (header_field_info *)g_ptr_array_index(protocol->fields, i);
}
protocol_t *
find_protocol_by_id(const int proto_id)
{
header_field_info *hfinfo;
if (proto_id < 0)
return NULL;
PROTO_REGISTRAR_GET_NTH(proto_id, hfinfo);
if (hfinfo->type != FT_PROTOCOL) {
DISSECTOR_ASSERT(hfinfo->display & BASE_PROTOCOL_INFO);
}
return (protocol_t *)hfinfo->strings;
}
int
proto_get_id(const protocol_t *protocol)
{
return protocol->proto_id;
}
gboolean
proto_name_already_registered(const gchar *name)
{
DISSECTOR_ASSERT_HINT(name, "No name present");
if (g_hash_table_lookup(proto_names, name) != NULL)
return TRUE;
return FALSE;
}
int
proto_get_id_by_filter_name(const gchar *filter_name)
{
const protocol_t *protocol = NULL;
DISSECTOR_ASSERT_HINT(filter_name, "No filter name present");
protocol = (const protocol_t *)g_hash_table_lookup(proto_filter_names, filter_name);
if (protocol == NULL)
return -1;
return protocol->proto_id;
}
int
proto_get_id_by_short_name(const gchar *short_name)
{
const protocol_t *protocol = NULL;
DISSECTOR_ASSERT_HINT(short_name, "No short name present");
protocol = (const protocol_t *)g_hash_table_lookup(proto_short_names, short_name);
if (protocol == NULL)
return -1;
return protocol->proto_id;
}
const char *
proto_get_protocol_name(const int proto_id)
{
protocol_t *protocol;
protocol = find_protocol_by_id(proto_id);
if (protocol == NULL)
return NULL;
return protocol->name;
}
const char *
proto_get_protocol_short_name(const protocol_t *protocol)
{
if (protocol == NULL)
return "(none)";
return protocol->short_name;
}
const char *
proto_get_protocol_long_name(const protocol_t *protocol)
{
if (protocol == NULL)
return "(none)";
return protocol->name;
}
const char *
proto_get_protocol_filter_name(const int proto_id)
{
protocol_t *protocol;
protocol = find_protocol_by_id(proto_id);
if (protocol == NULL)
return "(none)";
return protocol->filter_name;
}
void proto_add_heuristic_dissector(protocol_t *protocol, const char *short_name)
{
heur_dtbl_entry_t* heuristic_dissector;
if (protocol == NULL)
return;
heuristic_dissector = find_heur_dissector_by_unique_short_name(short_name);
if (heuristic_dissector != NULL)
{
protocol->heur_list = g_list_prepend (protocol->heur_list, heuristic_dissector);
}
}
void proto_heuristic_dissector_foreach(const protocol_t *protocol, GFunc func, gpointer user_data)
{
if (protocol == NULL)
return;
g_list_foreach(protocol->heur_list, func, user_data);
}
void
proto_get_frame_protocols(const wmem_list_t *layers, gboolean *is_ip,
gboolean *is_tcp, gboolean *is_udp,
gboolean *is_sctp, gboolean *is_tls,
gboolean *is_rtp,
gboolean *is_lte_rlc)
{
wmem_list_frame_t *protos = wmem_list_head(layers);
int proto_id;
const char *proto_name;
/* Walk the list of a available protocols in the packet and
attempt to find "major" ones. */
/* It might make more sense to assemble and return a bitfield. */
while (protos != NULL)
{
proto_id = GPOINTER_TO_INT(wmem_list_frame_data(protos));
proto_name = proto_get_protocol_filter_name(proto_id);
if (is_ip && ((!strcmp(proto_name, "ip")) ||
(!strcmp(proto_name, "ipv6")))) {
*is_ip = TRUE;
} else if (is_tcp && !strcmp(proto_name, "tcp")) {
*is_tcp = TRUE;
} else if (is_udp && !strcmp(proto_name, "udp")) {
*is_udp = TRUE;
} else if (is_sctp && !strcmp(proto_name, "sctp")) {
*is_sctp = TRUE;
} else if (is_tls && !strcmp(proto_name, "tls")) {
*is_tls = TRUE;
} else if (is_rtp && !strcmp(proto_name, "rtp")) {
*is_rtp = TRUE;
} else if (is_lte_rlc && !strcmp(proto_name, "rlc-lte")) {
*is_lte_rlc = TRUE;
}
protos = wmem_list_frame_next(protos);
}
}
gboolean
proto_is_frame_protocol(const wmem_list_t *layers, const char* proto_name)
{
wmem_list_frame_t *protos = wmem_list_head(layers);
int proto_id;
const char *name;
/* Walk the list of a available protocols in the packet and
attempt to find the specified protocol. */
while (protos != NULL)
{
proto_id = GPOINTER_TO_INT(wmem_list_frame_data(protos));
name = proto_get_protocol_filter_name(proto_id);
if (!strcmp(name, proto_name))
{
return TRUE;
}
protos = wmem_list_frame_next(protos);
}
return FALSE;
}
gchar *
proto_list_layers(const packet_info *pinfo)
{
wmem_strbuf_t *buf;
wmem_list_frame_t *layers = wmem_list_head(pinfo->layers);
buf = wmem_strbuf_new_sized(pinfo->pool, 128);
/* Walk the list of layers in the packet and
return a string of all entries. */
while (layers != NULL)
{
wmem_strbuf_append(buf, proto_get_protocol_filter_name(GPOINTER_TO_UINT(wmem_list_frame_data(layers))));
layers = wmem_list_frame_next(layers);
if (layers != NULL) {
wmem_strbuf_append_c(buf, ':');
}
}
return wmem_strbuf_finalize(buf);
}
gboolean
proto_is_pino(const protocol_t *protocol)
{
return (protocol->parent_proto_id != -1);
}
gboolean
proto_is_protocol_enabled(const protocol_t *protocol)
{
if (protocol == NULL)
return FALSE;
//parent protocol determines enable/disable for helper dissectors
if (proto_is_pino(protocol))
return proto_is_protocol_enabled(find_protocol_by_id(protocol->parent_proto_id));
return protocol->is_enabled;
}
gboolean
proto_is_protocol_enabled_by_default(const protocol_t *protocol)
{
//parent protocol determines enable/disable for helper dissectors
if (proto_is_pino(protocol))
return proto_is_protocol_enabled_by_default(find_protocol_by_id(protocol->parent_proto_id));
return protocol->enabled_by_default;
}
gboolean
proto_can_toggle_protocol(const int proto_id)
{
protocol_t *protocol;
protocol = find_protocol_by_id(proto_id);
//parent protocol determines toggling for helper dissectors
if (proto_is_pino(protocol))
return proto_can_toggle_protocol(protocol->parent_proto_id);
return protocol->can_toggle;
}
void
proto_disable_by_default(const int proto_id)
{
protocol_t *protocol;
protocol = find_protocol_by_id(proto_id);
DISSECTOR_ASSERT(protocol->can_toggle);
DISSECTOR_ASSERT(proto_is_pino(protocol) == FALSE);
protocol->is_enabled = FALSE;
protocol->enabled_by_default = FALSE;
}
void
proto_set_decoding(const int proto_id, const gboolean enabled)
{
protocol_t *protocol;
protocol = find_protocol_by_id(proto_id);
DISSECTOR_ASSERT(protocol->can_toggle);
DISSECTOR_ASSERT(proto_is_pino(protocol) == FALSE);
protocol->is_enabled = enabled;
}
void
proto_disable_all(void)
{
/* This doesn't explicitly disable heuristic protocols,
* but the heuristic doesn't get called if the parent
* protocol isn't enabled.
*/
protocol_t *protocol;
GList *list_item = protocols;
if (protocols == NULL)
return;
while (list_item) {
protocol = (protocol_t *)list_item->data;
if (protocol->can_toggle) {
protocol->is_enabled = FALSE;
}
list_item = g_list_next(list_item);
}
}
static void
heur_reenable_cb(void *data, void *user_data _U_)
{
heur_dtbl_entry_t *heur = (heur_dtbl_entry_t*)data;
heur->enabled = heur->enabled_by_default;
}
void
proto_reenable_all(void)
{
protocol_t *protocol;
GList *list_item = protocols;
if (protocols == NULL)
return;
while (list_item) {
protocol = (protocol_t *)list_item->data;
if (protocol->can_toggle)
protocol->is_enabled = protocol->enabled_by_default;
proto_heuristic_dissector_foreach(protocol, heur_reenable_cb, NULL);
list_item = g_list_next(list_item);
}
}
void
proto_set_cant_toggle(const int proto_id)
{
protocol_t *protocol;
protocol = find_protocol_by_id(proto_id);
protocol->can_toggle = FALSE;
}
static int
proto_register_field_common(protocol_t *proto, header_field_info *hfi, const int parent)
{
if (proto != NULL) {
g_ptr_array_add(proto->fields, hfi);
}
return proto_register_field_init(hfi, parent);
}
/* for use with static arrays only, since we don't allocate our own copies
of the header_field_info struct contained within the hf_register_info struct */
void
proto_register_field_array(const int parent, hf_register_info *hf, const int num_records)
{
hf_register_info *ptr = hf;
protocol_t *proto;
int i;
proto = find_protocol_by_id(parent);
if (proto->fields == NULL) {
proto->fields = g_ptr_array_sized_new(num_records);
}
for (i = 0; i < num_records; i++, ptr++) {
/*
* Make sure we haven't registered this yet.
* Most fields have variables associated with them
* that are initialized to -1; some have array elements,
* or possibly uninitialized variables, so we also allow
* 0 (which is unlikely to be the field ID we get back
* from "proto_register_field_init()").
*/
if (*ptr->p_id != -1 && *ptr->p_id != 0) {
REPORT_DISSECTOR_BUG(
"Duplicate field detected in call to proto_register_field_array: %s is already registered",
ptr->hfinfo.abbrev);
return;
}
*ptr->p_id = proto_register_field_common(proto, &ptr->hfinfo, parent);
}
}
/* deregister already registered fields */
void
proto_deregister_field (const int parent, gint hf_id)
{
header_field_info *hfi;
protocol_t *proto;
guint i;
g_free(last_field_name);
last_field_name = NULL;
if (hf_id == -1 || hf_id == 0)
return;
proto = find_protocol_by_id (parent);
if (!proto || proto->fields == NULL) {
return;
}
for (i = 0; i < proto->fields->len; i++) {
hfi = (header_field_info *)g_ptr_array_index(proto->fields, i);
if (hfi->id == hf_id) {
/* Found the hf_id in this protocol */
g_hash_table_steal(gpa_name_map, hfi->abbrev);
g_ptr_array_remove_index_fast(proto->fields, i);
g_ptr_array_add(deregistered_fields, gpa_hfinfo.hfi[hf_id]);
return;
}
}
}
void
proto_add_deregistered_data (void *data)
{
g_ptr_array_add(deregistered_data, data);
}
void
proto_add_deregistered_slice (gsize block_size, gpointer mem_block)
{
struct g_slice_data *slice_data = g_slice_new(struct g_slice_data);
slice_data->block_size = block_size;
slice_data->mem_block = mem_block;
g_ptr_array_add(deregistered_slice, slice_data);
}
void proto_free_field_strings (ftenum_t field_type, unsigned int field_display, const void *field_strings)
{
if (field_strings == NULL) {
return;
}
switch (field_type) {
case FT_FRAMENUM:
/* This is just an integer represented as a pointer */
break;
case FT_PROTOCOL: {
protocol_t *protocol = (protocol_t *)field_strings;
g_free((gchar *)protocol->short_name);
break;
}
case FT_BOOLEAN: {
true_false_string *tf = (true_false_string *)field_strings;
g_free((gchar *)tf->true_string);
g_free((gchar *)tf->false_string);
break;
}
case FT_UINT40:
case FT_INT40:
case FT_UINT48:
case FT_INT48:
case FT_UINT56:
case FT_INT56:
case FT_UINT64:
case FT_INT64: {
if (field_display & BASE_UNIT_STRING) {
unit_name_string *unit = (unit_name_string *)field_strings;
g_free((gchar *)unit->singular);
g_free((gchar *)unit->plural);
} else if (field_display & BASE_RANGE_STRING) {
range_string *rs = (range_string *)field_strings;
while (rs->strptr) {
g_free((gchar *)rs->strptr);
rs++;
}
} else if (field_display & BASE_EXT_STRING) {
val64_string_ext *vse = (val64_string_ext *)field_strings;
val64_string *vs = (val64_string *)vse->_vs_p;
while (vs->strptr) {
g_free((gchar *)vs->strptr);
vs++;
}
val64_string_ext_free(vse);
field_strings = NULL;
} else if (field_display == BASE_CUSTOM) {
/* this will be a pointer to a function, don't free that */
field_strings = NULL;
} else {
val64_string *vs64 = (val64_string *)field_strings;
while (vs64->strptr) {
g_free((gchar *)vs64->strptr);
vs64++;
}
}
break;
}
case FT_CHAR:
case FT_UINT8:
case FT_INT8:
case FT_UINT16:
case FT_INT16:
case FT_UINT24:
case FT_INT24:
case FT_UINT32:
case FT_INT32:
case FT_FLOAT:
case FT_DOUBLE: {
if (field_display & BASE_UNIT_STRING) {
unit_name_string *unit = (unit_name_string *)field_strings;
g_free((gchar *)unit->singular);
g_free((gchar *)unit->plural);
} else if (field_display & BASE_RANGE_STRING) {
range_string *rs = (range_string *)field_strings;
while (rs->strptr) {
g_free((gchar *)rs->strptr);
rs++;
}
} else if (field_display & BASE_EXT_STRING) {
value_string_ext *vse = (value_string_ext *)field_strings;
value_string *vs = (value_string *)vse->_vs_p;
while (vs->strptr) {
g_free((gchar *)vs->strptr);
vs++;
}
value_string_ext_free(vse);
field_strings = NULL;
} else if (field_display == BASE_CUSTOM) {
/* this will be a pointer to a function, don't free that */
field_strings = NULL;
} else {
value_string *vs = (value_string *)field_strings;
while (vs->strptr) {
g_free((gchar *)vs->strptr);
vs++;
}
}
break;
default:
break;
}
}
if (field_type != FT_FRAMENUM) {
g_free((void *)field_strings);
}
}
static void
free_deregistered_field (gpointer data, gpointer user_data _U_)
{
header_field_info *hfi = (header_field_info *) data;
gint hf_id = hfi->id;
g_free((char *)hfi->name);
g_free((char *)hfi->abbrev);
g_free((char *)hfi->blurb);
proto_free_field_strings(hfi->type, hfi->display, hfi->strings);
if (hfi->parent == -1)
g_slice_free(header_field_info, hfi);
gpa_hfinfo.hfi[hf_id] = NULL; /* Invalidate this hf_id / proto_id */
}
static void
free_deregistered_data (gpointer data, gpointer user_data _U_)
{
g_free (data);
}
static void
free_deregistered_slice (gpointer data, gpointer user_data _U_)
{
struct g_slice_data *slice_data = (struct g_slice_data *)data;
g_slice_free1(slice_data->block_size, slice_data->mem_block);
g_slice_free(struct g_slice_data, slice_data);
}
/* free deregistered fields and data */
void
proto_free_deregistered_fields (void)
{
expert_free_deregistered_expertinfos();
g_ptr_array_foreach(deregistered_fields, free_deregistered_field, NULL);
g_ptr_array_free(deregistered_fields, TRUE);
deregistered_fields = g_ptr_array_new();
g_ptr_array_foreach(deregistered_data, free_deregistered_data, NULL);
g_ptr_array_free(deregistered_data, TRUE);
deregistered_data = g_ptr_array_new();
g_ptr_array_foreach(deregistered_slice, free_deregistered_slice, NULL);
g_ptr_array_free(deregistered_slice, TRUE);
deregistered_slice = g_ptr_array_new();
}
static const value_string hf_display[] = {
{ BASE_NONE, "BASE_NONE" },
{ BASE_DEC, "BASE_DEC" },
{ BASE_HEX, "BASE_HEX" },
{ BASE_OCT, "BASE_OCT" },
{ BASE_DEC_HEX, "BASE_DEC_HEX" },
{ BASE_HEX_DEC, "BASE_HEX_DEC" },
{ BASE_CUSTOM, "BASE_CUSTOM" },
{ BASE_NONE|BASE_RANGE_STRING, "BASE_NONE|BASE_RANGE_STRING" },
{ BASE_DEC|BASE_RANGE_STRING, "BASE_DEC|BASE_RANGE_STRING" },
{ BASE_HEX|BASE_RANGE_STRING, "BASE_HEX|BASE_RANGE_STRING" },
{ BASE_OCT|BASE_RANGE_STRING, "BASE_OCT|BASE_RANGE_STRING" },
{ BASE_DEC_HEX|BASE_RANGE_STRING, "BASE_DEC_HEX|BASE_RANGE_STRING" },
{ BASE_HEX_DEC|BASE_RANGE_STRING, "BASE_HEX_DEC|BASE_RANGE_STRING" },
{ BASE_CUSTOM|BASE_RANGE_STRING, "BASE_CUSTOM|BASE_RANGE_STRING" },
{ BASE_NONE|BASE_VAL64_STRING, "BASE_NONE|BASE_VAL64_STRING" },
{ BASE_DEC|BASE_VAL64_STRING, "BASE_DEC|BASE_VAL64_STRING" },
{ BASE_HEX|BASE_VAL64_STRING, "BASE_HEX|BASE_VAL64_STRING" },
{ BASE_OCT|BASE_VAL64_STRING, "BASE_OCT|BASE_VAL64_STRING" },
{ BASE_DEC_HEX|BASE_VAL64_STRING, "BASE_DEC_HEX|BASE_VAL64_STRING" },
{ BASE_HEX_DEC|BASE_VAL64_STRING, "BASE_HEX_DEC|BASE_VAL64_STRING" },
{ BASE_CUSTOM|BASE_VAL64_STRING, "BASE_CUSTOM|BASE_VAL64_STRING" },
{ ABSOLUTE_TIME_LOCAL, "ABSOLUTE_TIME_LOCAL" },
{ ABSOLUTE_TIME_UTC, "ABSOLUTE_TIME_UTC" },
{ ABSOLUTE_TIME_DOY_UTC, "ABSOLUTE_TIME_DOY_UTC" },
{ BASE_PT_UDP, "BASE_PT_UDP" },
{ BASE_PT_TCP, "BASE_PT_TCP" },
{ BASE_PT_DCCP, "BASE_PT_DCCP" },
{ BASE_PT_SCTP, "BASE_PT_SCTP" },
{ BASE_OUI, "BASE_OUI" },
{ 0, NULL } };
const char* proto_field_display_to_string(int field_display)
{
return val_to_str_const(field_display, hf_display, "Unknown");
}
static inline port_type
display_to_port_type(field_display_e e)
{
switch (e) {
case BASE_PT_UDP:
return PT_UDP;
case BASE_PT_TCP:
return PT_TCP;
case BASE_PT_DCCP:
return PT_DCCP;
case BASE_PT_SCTP:
return PT_SCTP;
default:
break;
}
return PT_NONE;
}
/* temporary function containing assert part for easier profiling */
static void
tmp_fld_check_assert(header_field_info *hfinfo)
{
gchar* tmp_str;
/* The field must have a name (with length > 0) */
if (!hfinfo->name || !hfinfo->name[0]) {
if (hfinfo->abbrev)
/* Try to identify the field */
REPORT_DISSECTOR_BUG("Field (abbrev='%s') does not have a name",
hfinfo->abbrev);
else
/* Hum, no luck */
REPORT_DISSECTOR_BUG("Field does not have a name (nor an abbreviation)");
}
/* fields with an empty string for an abbreviation aren't filterable */
if (!hfinfo->abbrev || !hfinfo->abbrev[0])
REPORT_DISSECTOR_BUG("Field '%s' does not have an abbreviation", hfinfo->name);
/* These types of fields are allowed to have value_strings,
* true_false_strings or a protocol_t struct
*/
if (hfinfo->strings != NULL && FIELD_DISPLAY(hfinfo->display) != BASE_CUSTOM) {
switch (hfinfo->type) {
/*
* These types are allowed to support display value_strings,
* value64_strings, the extended versions of the previous
* two, range strings, or unit strings.
*/
case FT_CHAR:
case FT_UINT8:
case FT_UINT16:
case FT_UINT24:
case FT_UINT32:
case FT_UINT40:
case FT_UINT48:
case FT_UINT56:
case FT_UINT64:
case FT_INT8:
case FT_INT16:
case FT_INT24:
case FT_INT32:
case FT_INT40:
case FT_INT48:
case FT_INT56:
case FT_INT64:
case FT_BOOLEAN:
case FT_PROTOCOL:
break;
/*
* This is allowed to have a value of type
* enum ft_framenum_type to indicate what relationship
* the frame in question has to the frame in which
* the field is put.
*/
case FT_FRAMENUM:
break;
/*
* These types are allowed to support only unit strings.
*/
case FT_FLOAT:
case FT_DOUBLE:
if (!(hfinfo->display & BASE_UNIT_STRING)) {
REPORT_DISSECTOR_BUG("Field '%s' (%s) has a non-unit-strings 'strings' value but is of type %s"
" (which is only allowed to have unit strings)",
hfinfo->name, hfinfo->abbrev, ftype_name(hfinfo->type));
}
break;
/*
* This type is only allowed to support a string if it's
* a protocol (for pinos).
*/
case FT_BYTES:
if (!(hfinfo->display & BASE_PROTOCOL_INFO)) {
REPORT_DISSECTOR_BUG("Field '%s' (%s) has a non-protocol-info 'strings' value but is of type %s"
" (which is only allowed to have protocol-info strings)",
hfinfo->name, hfinfo->abbrev, ftype_name(hfinfo->type));
}
break;
default:
REPORT_DISSECTOR_BUG("Field '%s' (%s) has a 'strings' value but is of type %s"
" (which is not allowed to have strings)",
hfinfo->name, hfinfo->abbrev, ftype_name(hfinfo->type));
}
}
/* TODO: This check may slow down startup, and output quite a few warnings.
It would be good to be able to enable this (and possibly other checks?)
in non-release builds. */
#ifdef ENABLE_CHECK_FILTER
/* Check for duplicate value_string values.
There are lots that have the same value *and* string, so for now only
report those that have same value but different string. */
if ((hfinfo->strings != NULL) &&
!(hfinfo->display & BASE_RANGE_STRING) &&
!(hfinfo->display & BASE_UNIT_STRING) &&
!((hfinfo->display & FIELD_DISPLAY_E_MASK) == BASE_CUSTOM) &&
(
(hfinfo->type == FT_CHAR) ||
(hfinfo->type == FT_UINT8) ||
(hfinfo->type == FT_UINT16) ||
(hfinfo->type == FT_UINT24) ||
(hfinfo->type == FT_UINT32) ||
(hfinfo->type == FT_INT8) ||
(hfinfo->type == FT_INT16) ||
(hfinfo->type == FT_INT24) ||
(hfinfo->type == FT_INT32) )) {
if (hfinfo->display & BASE_EXT_STRING) {
if (hfinfo->display & BASE_VAL64_STRING) {
const val64_string *start_values = VAL64_STRING_EXT_VS_P((const val64_string_ext*)hfinfo->strings);
CHECK_HF_VALUE(val64_string, PRIu64, start_values);
} else {
const value_string *start_values = VALUE_STRING_EXT_VS_P((const value_string_ext*)hfinfo->strings);
CHECK_HF_VALUE(value_string, "u", start_values);
}
} else {
const value_string *start_values = (const value_string*)hfinfo->strings;
CHECK_HF_VALUE(value_string, "u", start_values);
}
}
if (hfinfo->type == FT_BOOLEAN) {
const true_false_string *tfs = (const true_false_string*)hfinfo->strings;
if (tfs) {
if (strcmp(tfs->false_string, tfs->true_string) == 0) {
ws_warning("Field '%s' (%s) has identical true and false strings (\"%s\", \"%s\")",
hfinfo->name, hfinfo->abbrev,
tfs->false_string, tfs->true_string);
}
}
}
if (hfinfo->display & BASE_RANGE_STRING) {
const range_string *rs = (const range_string*)(hfinfo->strings);
if (rs) {
const range_string *this_it = rs;
do {
if (this_it->value_max < this_it->value_min) {
ws_warning("value_range_string error: %s (%s) entry for \"%s\" - max(%"PRIu64" 0x%"PRIx64") is less than min(%"PRIu64" 0x%"PRIx64")",
hfinfo->name, hfinfo->abbrev,
this_it->strptr,
this_it->value_max, this_it->value_max,
this_it->value_min, this_it->value_min);
++this_it;
continue;
}
for (const range_string *prev_it=rs; prev_it < this_it; ++prev_it) {
/* Not OK if this one is completely hidden by an earlier one! */
if ((prev_it->value_min <= this_it->value_min) && (prev_it->value_max >= this_it->value_max)) {
ws_warning("value_range_string error: %s (%s) hidden by earlier entry "
"(prev=\"%s\": %"PRIu64" 0x%"PRIx64" -> %"PRIu64" 0x%"PRIx64") (this=\"%s\": %"PRIu64" 0x%"PRIx64" -> %"PRIu64" 0x%"PRIx64")",
hfinfo->name, hfinfo->abbrev,
prev_it->strptr, prev_it->value_min, prev_it->value_min,
prev_it->value_max, prev_it->value_max,
this_it->strptr, this_it->value_min, this_it->value_min,
this_it->value_max, this_it->value_max);
}
}
++this_it;
} while (this_it->strptr);
}
}
#endif
switch (hfinfo->type) {
case FT_CHAR:
/* Require the char type to have BASE_HEX, BASE_OCT,
* BASE_CUSTOM, or BASE_NONE as its base.
*
* If the display value is BASE_NONE and there is a
* strings conversion then the dissector writer is
* telling us that the field's numerical value is
* meaningless; we'll avoid showing the value to the
* user.
*/
switch (FIELD_DISPLAY(hfinfo->display)) {
case BASE_HEX:
case BASE_OCT:
case BASE_CUSTOM: /* hfinfo_numeric_value_format() treats this as decimal */
break;
case BASE_NONE:
if (hfinfo->strings == NULL)
REPORT_DISSECTOR_BUG("Field '%s' (%s) is an integral value (%s)"
" but is being displayed as BASE_NONE but"
" without a strings conversion",
hfinfo->name, hfinfo->abbrev,
ftype_name(hfinfo->type));
break;
default:
tmp_str = val_to_str_wmem(NULL, hfinfo->display, hf_display, "(Unknown: 0x%x)");
REPORT_DISSECTOR_BUG("Field '%s' (%s) is a character value (%s)"
" but is being displayed as %s",
hfinfo->name, hfinfo->abbrev,
ftype_name(hfinfo->type), tmp_str);
//wmem_free(NULL, tmp_str);
}
if (hfinfo->display & BASE_UNIT_STRING) {
REPORT_DISSECTOR_BUG("Field '%s' (%s) is a character value (%s) but has a unit string",
hfinfo->name, hfinfo->abbrev,
ftype_name(hfinfo->type));
}
break;
case FT_INT8:
case FT_INT16:
case FT_INT24:
case FT_INT32:
case FT_INT40:
case FT_INT48:
case FT_INT56:
case FT_INT64:
/* Hexadecimal and octal are, in printf() and everywhere
* else, unsigned so don't allow dissectors to register a
* signed field to be displayed unsigned. (Else how would
* we display negative values?)
*/
switch (FIELD_DISPLAY(hfinfo->display)) {
case BASE_HEX:
case BASE_OCT:
case BASE_DEC_HEX:
case BASE_HEX_DEC:
tmp_str = val_to_str_wmem(NULL, hfinfo->display, hf_display, "(Bit count: %d)");
REPORT_DISSECTOR_BUG("Field '%s' (%s) is signed (%s) but is being displayed unsigned (%s)",
hfinfo->name, hfinfo->abbrev,
ftype_name(hfinfo->type), tmp_str);
//wmem_free(NULL, tmp_str);
}
/* FALL THROUGH */
case FT_UINT8:
case FT_UINT16:
case FT_UINT24:
case FT_UINT32:
case FT_UINT40:
case FT_UINT48:
case FT_UINT56:
case FT_UINT64:
if (IS_BASE_PORT(hfinfo->display)) {
tmp_str = val_to_str_wmem(NULL, hfinfo->display, hf_display, "(Unknown: 0x%x)");
if (hfinfo->type != FT_UINT16) {
REPORT_DISSECTOR_BUG("Field '%s' (%s) has 'display' value %s but it can only be used with FT_UINT16, not %s",
hfinfo->name, hfinfo->abbrev,
tmp_str, ftype_name(hfinfo->type));
}
if (hfinfo->strings != NULL) {
REPORT_DISSECTOR_BUG("Field '%s' (%s) is an %s (%s) but has a strings value",
hfinfo->name, hfinfo->abbrev,
ftype_name(hfinfo->type), tmp_str);
}
if (hfinfo->bitmask != 0) {
REPORT_DISSECTOR_BUG("Field '%s' (%s) is an %s (%s) but has a bitmask",
hfinfo->name, hfinfo->abbrev,
ftype_name(hfinfo->type), tmp_str);
}
wmem_free(NULL, tmp_str);
break;
}
if (hfinfo->display == BASE_OUI) {
tmp_str = val_to_str_wmem(NULL, hfinfo->display, hf_display, "(Unknown: 0x%x)");
if (hfinfo->type != FT_UINT24) {
REPORT_DISSECTOR_BUG("Field '%s' (%s) has 'display' value %s but it can only be used with FT_UINT24, not %s",
hfinfo->name, hfinfo->abbrev,
tmp_str, ftype_name(hfinfo->type));
}
if (hfinfo->strings != NULL) {
REPORT_DISSECTOR_BUG("Field '%s' (%s) is an %s (%s) but has a strings value",
hfinfo->name, hfinfo->abbrev,
ftype_name(hfinfo->type), tmp_str);
}
if (hfinfo->bitmask != 0) {
REPORT_DISSECTOR_BUG("Field '%s' (%s) is an %s (%s) but has a bitmask",
hfinfo->name, hfinfo->abbrev,
ftype_name(hfinfo->type), tmp_str);
}
wmem_free(NULL, tmp_str);
break;
}
/* Require integral types (other than frame number,
* which is always displayed in decimal) to have a
* number base.
*
* If the display value is BASE_NONE and there is a
* strings conversion then the dissector writer is
* telling us that the field's numerical value is
* meaningless; we'll avoid showing the value to the
* user.
*/
switch (FIELD_DISPLAY(hfinfo->display)) {
case BASE_DEC:
case BASE_HEX:
case BASE_OCT:
case BASE_DEC_HEX:
case BASE_HEX_DEC:
case BASE_CUSTOM: /* hfinfo_numeric_value_format() treats this as decimal */
break;
case BASE_NONE:
if (hfinfo->strings == NULL) {
REPORT_DISSECTOR_BUG("Field '%s' (%s) is an integral value (%s)"
" but is being displayed as BASE_NONE but"
" without a strings conversion",
hfinfo->name, hfinfo->abbrev,
ftype_name(hfinfo->type));
}
if (hfinfo->display & BASE_SPECIAL_VALS) {
REPORT_DISSECTOR_BUG("Field '%s' (%s) is an integral value (%s)"
" that is being displayed as BASE_NONE but"
" with BASE_SPECIAL_VALS",
hfinfo->name, hfinfo->abbrev,
ftype_name(hfinfo->type));
}
break;
default:
tmp_str = val_to_str_wmem(NULL, hfinfo->display, hf_display, "(Unknown: 0x%x)");
REPORT_DISSECTOR_BUG("Field '%s' (%s) is an integral value (%s)"
" but is being displayed as %s",
hfinfo->name, hfinfo->abbrev,
ftype_name(hfinfo->type), tmp_str);
//wmem_free(NULL, tmp_str);
}
break;
case FT_BYTES:
case FT_UINT_BYTES:
/* Require bytes to have a "display type" that could
* add a character between displayed bytes.
*/
switch (FIELD_DISPLAY(hfinfo->display)) {
case BASE_NONE:
case SEP_DOT:
case SEP_DASH:
case SEP_COLON:
case SEP_SPACE:
break;
default:
tmp_str = val_to_str_wmem(NULL, hfinfo->display, hf_display, "(Bit count: %d)");
REPORT_DISSECTOR_BUG("Field '%s' (%s) is an byte array but is being displayed as %s instead of BASE_NONE, SEP_DOT, SEP_DASH, SEP_COLON, or SEP_SPACE",
hfinfo->name, hfinfo->abbrev, tmp_str);
//wmem_free(NULL, tmp_str);
}
if (hfinfo->bitmask != 0)
REPORT_DISSECTOR_BUG("Field '%s' (%s) is an %s but has a bitmask",
hfinfo->name, hfinfo->abbrev,
ftype_name(hfinfo->type));
//allowed to support string if its a protocol (for pinos)
if ((hfinfo->strings != NULL) && (!(hfinfo->display & BASE_PROTOCOL_INFO)))
REPORT_DISSECTOR_BUG("Field '%s' (%s) is an %s but has a strings value",
hfinfo->name, hfinfo->abbrev,
ftype_name(hfinfo->type));
break;
case FT_PROTOCOL:
case FT_FRAMENUM:
if (hfinfo->display != BASE_NONE) {
tmp_str = val_to_str_wmem(NULL, hfinfo->display, hf_display, "(Bit count: %d)");
REPORT_DISSECTOR_BUG("Field '%s' (%s) is an %s but is being displayed as %s instead of BASE_NONE",
hfinfo->name, hfinfo->abbrev,
ftype_name(hfinfo->type), tmp_str);
//wmem_free(NULL, tmp_str);
}
if (hfinfo->bitmask != 0)
REPORT_DISSECTOR_BUG("Field '%s' (%s) is an %s but has a bitmask",
hfinfo->name, hfinfo->abbrev,
ftype_name(hfinfo->type));
break;
case FT_BOOLEAN:
break;
case FT_ABSOLUTE_TIME:
if (!FIELD_DISPLAY_IS_ABSOLUTE_TIME(hfinfo->display)) {
tmp_str = val_to_str_wmem(NULL, hfinfo->display, hf_display, "(Bit count: %d)");
REPORT_DISSECTOR_BUG("Field '%s' (%s) is a %s but is being displayed as %s instead of as a time",
hfinfo->name, hfinfo->abbrev, ftype_name(hfinfo->type), tmp_str);
//wmem_free(NULL, tmp_str);
}
if (hfinfo->bitmask != 0)
REPORT_DISSECTOR_BUG("Field '%s' (%s) is an %s but has a bitmask",
hfinfo->name, hfinfo->abbrev,
ftype_name(hfinfo->type));
break;
case FT_STRING:
case FT_STRINGZ:
case FT_UINT_STRING:
case FT_STRINGZPAD:
case FT_STRINGZTRUNC:
switch (hfinfo->display) {
case BASE_NONE:
case BASE_STR_WSP:
break;
default:
tmp_str = val_to_str_wmem(NULL, hfinfo->display, hf_display, "(Unknown: 0x%x)");
REPORT_DISSECTOR_BUG("Field '%s' (%s) is an string value (%s)"
" but is being displayed as %s",
hfinfo->name, hfinfo->abbrev,
ftype_name(hfinfo->type), tmp_str);
//wmem_free(NULL, tmp_str);
}
if (hfinfo->bitmask != 0)
REPORT_DISSECTOR_BUG("Field '%s' (%s) is an %s but has a bitmask",
hfinfo->name, hfinfo->abbrev,
ftype_name(hfinfo->type));
if (hfinfo->strings != NULL)
REPORT_DISSECTOR_BUG("Field '%s' (%s) is an %s but has a strings value",
hfinfo->name, hfinfo->abbrev,
ftype_name(hfinfo->type));
break;
case FT_IPv4:
switch (hfinfo->display) {
case BASE_NONE:
case BASE_NETMASK:
break;
default:
tmp_str = val_to_str_wmem(NULL, hfinfo->display, hf_display, "(Unknown: 0x%x)");
REPORT_DISSECTOR_BUG("Field '%s' (%s) is an IPv4 value (%s)"
" but is being displayed as %s",
hfinfo->name, hfinfo->abbrev,
ftype_name(hfinfo->type), tmp_str);
//wmem_free(NULL, tmp_str);
break;
}
break;
case FT_FLOAT:
case FT_DOUBLE:
switch (FIELD_DISPLAY(hfinfo->display)) {
case BASE_NONE:
case BASE_DEC:
case BASE_HEX:
case BASE_EXP:
case BASE_CUSTOM:
break;
default:
tmp_str = val_to_str_wmem(NULL, hfinfo->display, hf_display, "(Unknown: 0x%x)");
REPORT_DISSECTOR_BUG("Field '%s' (%s) is a float value (%s)"
" but is being displayed as %s",
hfinfo->name, hfinfo->abbrev,
ftype_name(hfinfo->type), tmp_str);
//wmem_free(NULL, tmp_str);
}
if (hfinfo->bitmask != 0)
REPORT_DISSECTOR_BUG("Field '%s' (%s) is an %s but has a bitmask",
hfinfo->name, hfinfo->abbrev,
ftype_name(hfinfo->type));
if (FIELD_DISPLAY(hfinfo->display) != BASE_CUSTOM && (hfinfo->strings != NULL) && !(hfinfo->display & BASE_UNIT_STRING))
REPORT_DISSECTOR_BUG("Field '%s' (%s) is an %s but has a strings value",
hfinfo->name, hfinfo->abbrev,
ftype_name(hfinfo->type));
break;
default:
if (hfinfo->display != BASE_NONE) {
tmp_str = val_to_str_wmem(NULL, hfinfo->display, hf_display, "(Bit count: %d)");
REPORT_DISSECTOR_BUG("Field '%s' (%s) is an %s but is being displayed as %s instead of BASE_NONE",
hfinfo->name, hfinfo->abbrev,
ftype_name(hfinfo->type),
tmp_str);
//wmem_free(NULL, tmp_str);
}
if (hfinfo->bitmask != 0)
REPORT_DISSECTOR_BUG("Field '%s' (%s) is an %s but has a bitmask",
hfinfo->name, hfinfo->abbrev,
ftype_name(hfinfo->type));
if (hfinfo->strings != NULL)
REPORT_DISSECTOR_BUG("Field '%s' (%s) is an %s but has a strings value",
hfinfo->name, hfinfo->abbrev,
ftype_name(hfinfo->type));
break;
}
}
#ifdef ENABLE_CHECK_FILTER
static enum ftenum
_ftype_common(enum ftenum type)
{
switch (type) {
case FT_INT8:
case FT_INT16:
case FT_INT24:
case FT_INT32:
return FT_INT32;
case FT_CHAR:
case FT_UINT8:
case FT_UINT16:
case FT_UINT24:
case FT_UINT32:
case FT_IPXNET:
case FT_FRAMENUM:
return FT_UINT32;
case FT_UINT64:
case FT_EUI64:
return FT_UINT64;
case FT_STRING:
case FT_STRINGZ:
case FT_UINT_STRING:
return FT_STRING;
case FT_FLOAT:
case FT_DOUBLE:
return FT_DOUBLE;
case FT_BYTES:
case FT_UINT_BYTES:
case FT_ETHER:
case FT_OID:
return FT_BYTES;
case FT_ABSOLUTE_TIME:
case FT_RELATIVE_TIME:
return FT_ABSOLUTE_TIME;
default:
return type;
}
}
#endif
static void
register_type_length_mismatch(void)
{
static ei_register_info ei[] = {
{ &ei_type_length_mismatch_error, { "_ws.type_length.mismatch", PI_MALFORMED, PI_ERROR, "Trying to fetch X with length Y", EXPFILL }},
{ &ei_type_length_mismatch_warn, { "_ws.type_length.mismatch", PI_MALFORMED, PI_WARN, "Trying to fetch X with length Y", EXPFILL }},
};
expert_module_t* expert_type_length_mismatch;
proto_type_length_mismatch = proto_register_protocol("Type Length Mismatch", "Type length mismatch", "_ws.type_length");
expert_type_length_mismatch = expert_register_protocol(proto_type_length_mismatch);
expert_register_field_array(expert_type_length_mismatch, ei, array_length(ei));
/* "Type Length Mismatch" isn't really a protocol, it's an error indication;
disabling them makes no sense. */
proto_set_cant_toggle(proto_type_length_mismatch);
}
static void
register_byte_array_string_decodinws_error(void)
{
static ei_register_info ei[] = {
{ &ei_byte_array_string_decoding_failed_error,
{ "_ws.byte_array_string.decoding_error.failed", PI_MALFORMED, PI_ERROR,
"Failed to decode byte array from string", EXPFILL
}
},
};
expert_module_t* expert_byte_array_string_decoding_error;
proto_byte_array_string_decoding_error =
proto_register_protocol("Byte Array-String Decoding Error",
"Byte Array-string decoding error",
"_ws.byte_array_string.decoding_error");
expert_byte_array_string_decoding_error =
expert_register_protocol(proto_byte_array_string_decoding_error);
expert_register_field_array(expert_byte_array_string_decoding_error, ei, array_length(ei));
/* "Byte Array-String Decoding Error" isn't really a protocol, it's an error indication;
disabling them makes no sense. */
proto_set_cant_toggle(proto_byte_array_string_decoding_error);
}
static void
register_date_time_string_decodinws_error(void)
{
static ei_register_info ei[] = {
{ &ei_date_time_string_decoding_failed_error,
{ "_ws.date_time_string.decoding_error.failed", PI_MALFORMED, PI_ERROR,
"Failed to decode date and time from string", EXPFILL
}
},
};
expert_module_t* expert_date_time_string_decoding_error;
proto_date_time_string_decoding_error =
proto_register_protocol("Date and Time-String Decoding Error",
"Date and Time-string decoding error",
"_ws.date_time_string.decoding_error");
expert_date_time_string_decoding_error =
expert_register_protocol(proto_date_time_string_decoding_error);
expert_register_field_array(expert_date_time_string_decoding_error, ei, array_length(ei));
/* "Date and Time-String Decoding Error" isn't really a protocol, it's an error indication;
disabling them makes no sense. */
proto_set_cant_toggle(proto_date_time_string_decoding_error);
}
static void
register_string_errors(void)
{
static ei_register_info ei[] = {
{ &ei_string_trailing_characters,
{ "_ws.string.trailing_stray_characters", PI_UNDECODED, PI_WARN, "Trailing stray characters", EXPFILL }
},
};
expert_module_t* expert_string_errors;
proto_string_errors = proto_register_protocol("String Errors", "String errors", "_ws.string");
expert_string_errors = expert_register_protocol(proto_string_errors);
expert_register_field_array(expert_string_errors, ei, array_length(ei));
/* "String Errors" isn't really a protocol, it's an error indication;
disabling them makes no sense. */
proto_set_cant_toggle(proto_string_errors);
}
#define PROTO_PRE_ALLOC_HF_FIELDS_MEM (270000+PRE_ALLOC_EXPERT_FIELDS_MEM)
static int
proto_register_field_init(header_field_info *hfinfo, const int parent)
{
tmp_fld_check_assert(hfinfo);
hfinfo->parent = parent;
hfinfo->same_name_next = NULL;
hfinfo->same_name_prev_id = -1;
/* if we always add and never delete, then id == len - 1 is correct */
if (gpa_hfinfo.len >= gpa_hfinfo.allocated_len) {
if (!gpa_hfinfo.hfi) {
gpa_hfinfo.allocated_len = PROTO_PRE_ALLOC_HF_FIELDS_MEM;
gpa_hfinfo.hfi = (header_field_info **)g_malloc(sizeof(header_field_info *)*PROTO_PRE_ALLOC_HF_FIELDS_MEM);
} else {
gpa_hfinfo.allocated_len += 1000;
gpa_hfinfo.hfi = (header_field_info **)g_realloc(gpa_hfinfo.hfi,
sizeof(header_field_info *)*gpa_hfinfo.allocated_len);
/*ws_warning("gpa_hfinfo.allocated_len %u", gpa_hfinfo.allocated_len);*/
}
}
gpa_hfinfo.hfi[gpa_hfinfo.len] = hfinfo;
gpa_hfinfo.len++;
hfinfo->id = gpa_hfinfo.len - 1;
/* if we have real names, enter this field in the name tree */
if ((hfinfo->name[0] != 0) && (hfinfo->abbrev[0] != 0 )) {
header_field_info *same_name_next_hfinfo;
guchar c;
/* Check that the filter name (abbreviation) is legal;
* it must contain only alphanumerics, '-', "_", and ".". */
c = proto_check_field_name(hfinfo->abbrev);
if (c) {
if (c == '.') {
REPORT_DISSECTOR_BUG("Invalid leading, duplicated or trailing '.' found in filter name '%s'", hfinfo->abbrev);
} else if (g_ascii_isprint(c)) {
REPORT_DISSECTOR_BUG("Invalid character '%c' in filter name '%s'", c, hfinfo->abbrev);
} else {
REPORT_DISSECTOR_BUG("Invalid byte \\%03o in filter name '%s'", c, hfinfo->abbrev);
}
}
/* We allow multiple hfinfo's to be registered under the same
* abbreviation. This was done for X.25, as, depending
* on whether it's modulo-8 or modulo-128 operation,
* some bitfield fields may be in different bits of
* a byte, and we want to be able to refer to that field
* with one name regardless of whether the packets
* are modulo-8 or modulo-128 packets. */
same_name_hfinfo = NULL;
g_hash_table_insert(gpa_name_map, (gpointer) (hfinfo->abbrev), hfinfo);
/* GLIB 2.x - if it is already present
* the previous hfinfo with the same name is saved
* to same_name_hfinfo by value destroy callback */
if (same_name_hfinfo) {
/* There's already a field with this name.
* Put the current field *before* that field
* in the list of fields with this name, Thus,
* we end up with an effectively
* doubly-linked-list of same-named hfinfo's,
* with the head of the list (stored in the
* hash) being the last seen hfinfo.
*/
same_name_next_hfinfo =
same_name_hfinfo->same_name_next;
hfinfo->same_name_next = same_name_next_hfinfo;
if (same_name_next_hfinfo)
same_name_next_hfinfo->same_name_prev_id = hfinfo->id;
same_name_hfinfo->same_name_next = hfinfo;
hfinfo->same_name_prev_id = same_name_hfinfo->id;
#ifdef ENABLE_CHECK_FILTER
while (same_name_hfinfo) {
if (_ftype_common(hfinfo->type) != _ftype_common(same_name_hfinfo->type))
ws_warning("'%s' exists multiple times with incompatible types: %s and %s", hfinfo->abbrev, ftype_name(hfinfo->type), ftype_name(same_name_hfinfo->type));
same_name_hfinfo = same_name_hfinfo->same_name_next;
}
#endif
}
}
return hfinfo->id;
}
void
proto_register_subtree_array(gint * const *indices, const int num_indices)
{
int i;
gint *const *ptr = indices;
/*
* If we've already allocated the array of tree types, expand
* it; this lets plugins such as mate add tree types after
* the initial startup. (If we haven't already allocated it,
* we don't allocate it; on the first pass, we just assign
* ett values and keep track of how many we've assigned, and
* when we're finished registering all dissectors we allocate
* the array, so that we do only one allocation rather than
* wasting CPU time and memory by growing the array for each
* dissector that registers ett values.)
*/
if (tree_is_expanded != NULL) {
tree_is_expanded = (guint32 *)g_realloc(tree_is_expanded, (1+((num_tree_types + num_indices)/32)) * sizeof(guint32));
/* set new items to 0 */
/* XXX, slow!!! optimize when needed (align 'i' to 32, and set rest of guint32 to 0) */
for (i = num_tree_types; i < num_tree_types + num_indices; i++)
tree_is_expanded[i >> 5] &= ~(1U << (i & 31));
}
/*
* Assign "num_indices" subtree numbers starting at "num_tree_types",
* returning the indices through the pointers in the array whose
* first element is pointed to by "indices", and update
* "num_tree_types" appropriately.
*/
for (i = 0; i < num_indices; i++, ptr++, num_tree_types++) {
if (**ptr != -1) {
REPORT_DISSECTOR_BUG("register_subtree_array: subtree item type (ett_...) not -1 !"
" This is a development error:"
" Either the subtree item type has already been assigned or"
" was not initialized to -1.");
}
**ptr = num_tree_types;
}
}
static void
mark_truncated(char *label_str, gsize name_pos, const size_t size)
{
static const char trunc_str[] = " [truncated]";
const size_t trunc_len = sizeof(trunc_str)-1;
gchar *last_char;
/* ..... field_name: dataaaaaaaaaaaaa
* |
* ^^^^^ name_pos
*
* ..... field_name [truncated]: dataaaaaaaaaaaaa
*
* name_pos==0 means that we have only data or only a field_name
*/
if (name_pos < size - trunc_len) {
memmove(label_str + name_pos + trunc_len, label_str + name_pos, size - name_pos - trunc_len);
memcpy(label_str + name_pos, trunc_str, trunc_len);
/* in general, label_str is UTF-8
we can truncate it only at the beginning of a new character
we go backwards from the byte right after our buffer and
find the next starting byte of a UTF-8 character, this is
where we cut
there's no need to use g_utf8_find_prev_char(), the search
will always succeed since we copied trunc_str into the
buffer */
/* g_utf8_prev_char does not deference the memory address
* passed in (until after decrementing it, so it is perfectly
* legal to pass in a pointer one past the last element.
*/
last_char = g_utf8_prev_char(label_str + size);
*last_char = '\0';
} else if (name_pos < size)
(void) g_strlcpy(label_str + name_pos, trunc_str, size - name_pos);
}
static void
label_mark_truncated(char *label_str, gsize name_pos)
{
mark_truncated(label_str, name_pos, ITEM_LABEL_LENGTH);
}
static gsize
label_fill(char *label_str, gsize pos, const header_field_info *hfinfo, const char *text)
{
gsize name_pos;
/* "%s: %s", hfinfo->name, text */
name_pos = pos = label_concat(label_str, pos, hfinfo->name);
if (!(hfinfo->display & BASE_NO_DISPLAY_VALUE)) {
pos = label_concat(label_str, pos, ": ");
pos = ws_label_strcpy(label_str, ITEM_LABEL_LENGTH, pos, text ? text : "(null)", label_strcat_flags(hfinfo));
}
if (pos >= ITEM_LABEL_LENGTH) {
/* Uh oh, we don't have enough room. Tell the user that the field is truncated. */
label_mark_truncated(label_str, name_pos);
}
return pos;
}
static gsize
label_fill_descr(char *label_str, gsize pos, const header_field_info *hfinfo, const char *text, const char *descr)
{
gsize name_pos;
/* "%s: %s (%s)", hfinfo->name, text, descr */
name_pos = pos = label_concat(label_str, pos, hfinfo->name);
if (!(hfinfo->display & BASE_NO_DISPLAY_VALUE)) {
pos = label_concat(label_str, pos, ": ");
if (hfinfo->display & BASE_UNIT_STRING) {
pos = label_concat(label_str, pos, descr ? descr : "(null)");
pos = label_concat(label_str, pos, text ? text : "(null)");
} else {
pos = label_concat(label_str, pos, text ? text : "(null)");
pos = label_concat(label_str, pos, " (");
pos = label_concat(label_str, pos, descr ? descr : "(null)");
pos = label_concat(label_str, pos, ")");
}
}
if (pos >= ITEM_LABEL_LENGTH) {
/* Uh oh, we don't have enough room. Tell the user that the field is truncated. */
label_mark_truncated(label_str, name_pos);
}
return pos;
}
void
proto_item_fill_label(field_info *fi, gchar *label_str)
{
header_field_info *hfinfo;
const char *str;
const guint8 *bytes;
guint32 integer;
guint64 integer64;
ws_in4_addr ipv4;
const ws_in6_addr *ipv6;
const e_guid_t *guid;
gchar *name;
address addr;
char *addr_str;
char *tmp;
if (!label_str) {
ws_warning("NULL label_str passed to proto_item_fill_label.");
return;
}
label_str[0]= '\0';
if (!fi) {
return;
}
hfinfo = fi->hfinfo;
switch (hfinfo->type) {
case FT_NONE:
case FT_PROTOCOL:
(void) g_strlcpy(label_str, hfinfo->name, ITEM_LABEL_LENGTH);
break;
case FT_BOOLEAN:
fill_label_boolean(fi, label_str);
break;
case FT_BYTES:
case FT_UINT_BYTES:
tmp = format_bytes_hfinfo(NULL, hfinfo,
fvalue_get_bytes_data(fi->value),
(guint)fvalue_length2(fi->value));
label_fill(label_str, 0, hfinfo, tmp);
wmem_free(NULL, tmp);
break;
case FT_CHAR:
if (hfinfo->bitmask) {
fill_label_bitfield_char(fi, label_str);
} else {
fill_label_char(fi, label_str);
}
break;
/* Four types of integers to take care of:
* Bitfield, with val_string
* Bitfield, w/o val_string
* Non-bitfield, with val_string
* Non-bitfield, w/o val_string
*/
case FT_UINT8:
case FT_UINT16:
case FT_UINT24:
case FT_UINT32:
if (hfinfo->bitmask) {
fill_label_bitfield(fi, label_str, FALSE);
} else {
fill_label_number(fi, label_str, FALSE);
}
break;
case FT_FRAMENUM:
fill_label_number(fi, label_str, FALSE);
break;
case FT_UINT40:
case FT_UINT48:
case FT_UINT56:
case FT_UINT64:
if (hfinfo->bitmask) {
fill_label_bitfield64(fi, label_str, FALSE);
} else {
fill_label_number64(fi, label_str, FALSE);
}
break;
case FT_INT8:
case FT_INT16:
case FT_INT24:
case FT_INT32:
if (hfinfo->bitmask) {
fill_label_bitfield(fi, label_str, TRUE);
} else {
fill_label_number(fi, label_str, TRUE);
}
break;
case FT_INT40:
case FT_INT48:
case FT_INT56:
case FT_INT64:
if (hfinfo->bitmask) {
fill_label_bitfield64(fi, label_str, TRUE);
} else {
fill_label_number64(fi, label_str, TRUE);
}
break;
case FT_FLOAT:
case FT_DOUBLE:
fill_label_float(fi, label_str);
break;
case FT_ABSOLUTE_TIME:
tmp = abs_time_to_str(NULL, fvalue_get_time(fi->value), hfinfo->display, TRUE);
label_fill(label_str, 0, hfinfo, tmp);
wmem_free(NULL, tmp);
break;
case FT_RELATIVE_TIME:
tmp = rel_time_to_secs_str(NULL, fvalue_get_time(fi->value));
snprintf(label_str, ITEM_LABEL_LENGTH,
"%s: %s seconds", hfinfo->name, tmp);
wmem_free(NULL, tmp);
break;
case FT_IPXNET:
integer = fvalue_get_uinteger(fi->value);
tmp = get_ipxnet_name(NULL, integer);
snprintf(label_str, ITEM_LABEL_LENGTH,
"%s: %s (0x%08X)", hfinfo->name,
tmp, integer);
wmem_free(NULL, tmp);
break;
case FT_AX25:
addr.type = AT_AX25;
addr.len = AX25_ADDR_LEN;
addr.data = fvalue_get_bytes_data(fi->value);
addr_str = (char*)address_to_str(NULL, &addr);
snprintf(label_str, ITEM_LABEL_LENGTH,
"%s: %s", hfinfo->name, addr_str);
wmem_free(NULL, addr_str);
break;
case FT_VINES:
addr.type = AT_VINES;
addr.len = VINES_ADDR_LEN;
addr.data = fvalue_get_bytes_data(fi->value);
addr_str = (char*)address_to_str(NULL, &addr);
snprintf(label_str, ITEM_LABEL_LENGTH,
"%s: %s", hfinfo->name, addr_str);
wmem_free(NULL, addr_str);
break;
case FT_ETHER:
bytes = fvalue_get_bytes_data(fi->value);
addr.type = AT_ETHER;
addr.len = 6;
addr.data = bytes;
addr_str = (char*)address_with_resolution_to_str(NULL, &addr);
snprintf(label_str, ITEM_LABEL_LENGTH,
"%s: %s", hfinfo->name, addr_str);
wmem_free(NULL, addr_str);
break;
case FT_IPv4:
ipv4 = fvalue_get_uinteger(fi->value);
addr.type = AT_IPv4;
addr.len = 4;
addr.data = &ipv4;
if (hfinfo->display == BASE_NETMASK) {
addr_str = (char*)address_to_str(NULL, &addr);
} else {
addr_str = (char*)address_with_resolution_to_str(NULL, &addr);
}
snprintf(label_str, ITEM_LABEL_LENGTH,
"%s: %s", hfinfo->name, addr_str);
wmem_free(NULL, addr_str);
break;
case FT_IPv6:
ipv6 = fvalue_get_ipv6(fi->value);
addr.type = AT_IPv6;
addr.len = 16;
addr.data = ipv6;
addr_str = (char*)address_with_resolution_to_str(NULL, &addr);
snprintf(label_str, ITEM_LABEL_LENGTH,
"%s: %s", hfinfo->name, addr_str);
wmem_free(NULL, addr_str);
break;
case FT_FCWWN:
bytes = fvalue_get_bytes_data(fi->value);
addr.type = AT_FCWWN;
addr.len = FCWWN_ADDR_LEN;
addr.data = bytes;
addr_str = (char*)address_with_resolution_to_str(NULL, &addr);
snprintf(label_str, ITEM_LABEL_LENGTH,
"%s: %s", hfinfo->name, addr_str);
wmem_free(NULL, addr_str);
break;
case FT_GUID:
guid = fvalue_get_guid(fi->value);
tmp = guid_to_str(NULL, guid);
label_fill(label_str, 0, hfinfo, tmp);
wmem_free(NULL, tmp);
break;
case FT_OID:
bytes = fvalue_get_bytes_data(fi->value);
name = oid_resolved_from_encoded(NULL, bytes, (gint)fvalue_length2(fi->value));
tmp = oid_encoded2string(NULL, bytes, (guint)fvalue_length2(fi->value));
if (name) {
label_fill_descr(label_str, 0, hfinfo, tmp, name);
wmem_free(NULL, name);
} else {
label_fill(label_str, 0, hfinfo, tmp);
}
wmem_free(NULL, tmp);
break;
case FT_REL_OID:
bytes = fvalue_get_bytes_data(fi->value);
name = rel_oid_resolved_from_encoded(NULL, bytes, (gint)fvalue_length2(fi->value));
tmp = rel_oid_encoded2string(NULL, bytes, (guint)fvalue_length2(fi->value));
if (name) {
label_fill_descr(label_str, 0, hfinfo, tmp, name);
wmem_free(NULL, name);
} else {
label_fill(label_str, 0, hfinfo, tmp);
}
wmem_free(NULL, tmp);
break;
case FT_SYSTEM_ID:
bytes = fvalue_get_bytes_data(fi->value);
tmp = print_system_id(NULL, bytes, (int)fvalue_length2(fi->value));
label_fill(label_str, 0, hfinfo, tmp);
wmem_free(NULL, tmp);
break;
case FT_EUI64:
integer64 = fvalue_get_uinteger64(fi->value);
addr_str = eui64_to_str(NULL, integer64);
tmp = (char*)eui64_to_display(NULL, integer64);
label_fill_descr(label_str, 0, hfinfo, tmp, addr_str);
wmem_free(NULL, tmp);
wmem_free(NULL, addr_str);
break;
case FT_STRING:
case FT_STRINGZ:
case FT_UINT_STRING:
case FT_STRINGZPAD:
case FT_STRINGZTRUNC:
str = fvalue_get_string(fi->value);
label_fill(label_str, 0, hfinfo, str);
break;
case FT_IEEE_11073_SFLOAT:
case FT_IEEE_11073_FLOAT:
tmp = fvalue_to_string_repr(NULL, fi->value, FTREPR_DISPLAY, hfinfo->display);
snprintf(label_str, ITEM_LABEL_LENGTH,
"%s: %s",
hfinfo->name, tmp);
wmem_free(NULL, tmp);
break;
default:
REPORT_DISSECTOR_BUG("field %s has type %d (%s) not handled in proto_item_fill_label()",
hfinfo->abbrev,
hfinfo->type,
ftype_name(hfinfo->type));
break;
}
}
static void
fill_label_boolean(field_info *fi, gchar *label_str)
{
char *p;
int bitfield_byte_length = 0, bitwidth;
guint64 unshifted_value;
guint64 value;
header_field_info *hfinfo = fi->hfinfo;
value = fvalue_get_uinteger64(fi->value);
if (hfinfo->bitmask) {
/* Figure out the bit width */
bitwidth = hfinfo_container_bitwidth(hfinfo);
/* Un-shift bits */
unshifted_value = value;
unshifted_value <<= hfinfo_bitshift(hfinfo);
/* Create the bitfield first */
p = decode_bitfield_value(label_str, unshifted_value, hfinfo->bitmask, bitwidth);
bitfield_byte_length = (int) (p - label_str);
}
/* Fill in the textual info */
label_fill(label_str, bitfield_byte_length, hfinfo, tfs_get_string(!!value, hfinfo->strings));
}
static const char *
hf_try_val_to_str(guint32 value, const header_field_info *hfinfo)
{
if (hfinfo->display & BASE_RANGE_STRING)
return try_rval_to_str(value, (const range_string *) hfinfo->strings);
if (hfinfo->display & BASE_EXT_STRING) {
if (hfinfo->display & BASE_VAL64_STRING)
return try_val64_to_str_ext(value, (val64_string_ext *) hfinfo->strings);
else
return try_val_to_str_ext(value, (value_string_ext *) hfinfo->strings);
}
if (hfinfo->display & BASE_VAL64_STRING)
return try_val64_to_str(value, (const val64_string *) hfinfo->strings);
if (hfinfo->display & BASE_UNIT_STRING)
return unit_name_string_get_value(value, (const struct unit_name_string*) hfinfo->strings);
return try_val_to_str(value, (const value_string *) hfinfo->strings);
}
static const char *
hf_try_val64_to_str(guint64 value, const header_field_info *hfinfo)
{
if (hfinfo->display & BASE_VAL64_STRING) {
if (hfinfo->display & BASE_EXT_STRING)
return try_val64_to_str_ext(value, (val64_string_ext *) hfinfo->strings);
else
return try_val64_to_str(value, (const val64_string *) hfinfo->strings);
}
if (hfinfo->display & BASE_RANGE_STRING)
return try_rval64_to_str(value, (const range_string *) hfinfo->strings);
if (hfinfo->display & BASE_UNIT_STRING)
return unit_name_string_get_value64(value, (const struct unit_name_string*) hfinfo->strings);
/* If this is reached somebody registered a 64-bit field with a 32-bit
* value-string, which isn't right. */
REPORT_DISSECTOR_BUG("field %s is a 64-bit field with a 32-bit value_string",
hfinfo->abbrev);
/* This is necessary to squelch MSVC errors; is there
any way to tell it that DISSECTOR_ASSERT_NOT_REACHED()
never returns? */
return NULL;
}
static const char *
hf_try_double_val_to_str(gdouble value, const header_field_info *hfinfo)
{
if (hfinfo->display & BASE_UNIT_STRING)
return unit_name_string_get_double(value, (const struct unit_name_string*)hfinfo->strings);
REPORT_DISSECTOR_BUG("field %s (FT_DOUBLE) has no base_unit_string", hfinfo->abbrev);
/* This is necessary to squelch MSVC errors; is there
any way to tell it that DISSECTOR_ASSERT_NOT_REACHED()
never returns? */
return NULL;
}
static const char *
hf_try_val_to_str_const(guint32 value, const header_field_info *hfinfo, const char *unknown_str)
{
const char *str = hf_try_val_to_str(value, hfinfo);
return (str) ? str : unknown_str;
}
static const char *
hf_try_val64_to_str_const(guint64 value, const header_field_info *hfinfo, const char *unknown_str)
{
const char *str = hf_try_val64_to_str(value, hfinfo);
return (str) ? str : unknown_str;
}
/* Fills data for bitfield chars with val_strings */
static void
fill_label_bitfield_char(field_info *fi, gchar *label_str)
{
char *p;
int bitfield_byte_length, bitwidth;
guint32 unshifted_value;
guint32 value;
char buf[32];
const char *out;
header_field_info *hfinfo = fi->hfinfo;
/* Figure out the bit width */
bitwidth = hfinfo_container_bitwidth(hfinfo);
/* Un-shift bits */
value = fvalue_get_uinteger(fi->value);
unshifted_value = value;
if (hfinfo->bitmask) {
unshifted_value <<= hfinfo_bitshift(hfinfo);
}
/* Create the bitfield first */
p = decode_bitfield_value(label_str, unshifted_value, hfinfo->bitmask, bitwidth);
bitfield_byte_length = (int) (p - label_str);
/* Fill in the textual info using stored (shifted) value */
if (hfinfo->display == BASE_CUSTOM) {
gchar tmp[ITEM_LABEL_LENGTH];
const custom_fmt_func_t fmtfunc = (const custom_fmt_func_t)hfinfo->strings;
DISSECTOR_ASSERT(fmtfunc);
fmtfunc(tmp, value);
label_fill(label_str, bitfield_byte_length, hfinfo, tmp);
}
else if (hfinfo->strings) {
const char *val_str = hf_try_val_to_str_const(value, hfinfo, "Unknown");
out = hfinfo_char_vals_format(hfinfo, buf, value);
if (out == NULL) /* BASE_NONE so don't put integer in descr */
label_fill(label_str, bitfield_byte_length, hfinfo, val_str);
else
label_fill_descr(label_str, bitfield_byte_length, hfinfo, val_str, out);
}
else {
out = hfinfo_char_value_format(hfinfo, buf, value);
label_fill(label_str, bitfield_byte_length, hfinfo, out);
}
}
/* Fills data for bitfield ints with val_strings */
static void
fill_label_bitfield(field_info *fi, gchar *label_str, gboolean is_signed)
{
char *p;
int bitfield_byte_length, bitwidth;
guint32 value, unshifted_value;
char buf[32];
const char *out;
header_field_info *hfinfo = fi->hfinfo;
/* Figure out the bit width */
if (fi->flags & FI_VARINT)
bitwidth = fi->length*8;
else
bitwidth = hfinfo_container_bitwidth(hfinfo);
/* Un-shift bits */
if (is_signed)
value = fvalue_get_sinteger(fi->value);
else
value = fvalue_get_uinteger(fi->value);
unshifted_value = value;
if (hfinfo->bitmask) {
unshifted_value <<= hfinfo_bitshift(hfinfo);
}
/* Create the bitfield first */
if (fi->flags & FI_VARINT)
p = decode_bitfield_varint_value(label_str, unshifted_value, hfinfo->bitmask, bitwidth);
else
p = decode_bitfield_value(label_str, unshifted_value, hfinfo->bitmask, bitwidth);
bitfield_byte_length = (int) (p - label_str);
/* Fill in the textual info using stored (shifted) value */
if (hfinfo->display == BASE_CUSTOM) {
gchar tmp[ITEM_LABEL_LENGTH];
const custom_fmt_func_t fmtfunc = (const custom_fmt_func_t)hfinfo->strings;
DISSECTOR_ASSERT(fmtfunc);
fmtfunc(tmp, value);
label_fill(label_str, bitfield_byte_length, hfinfo, tmp);
}
else if (hfinfo->strings) {
const char *val_str = hf_try_val_to_str(value, hfinfo);
out = hfinfo_number_vals_format(hfinfo, buf, value);
if (hfinfo->display & BASE_SPECIAL_VALS) {
/*
* Unique values only display value_string string
* if there is a match. Otherwise it's just a number
*/
if (val_str) {
label_fill_descr(label_str, bitfield_byte_length, hfinfo, val_str, out);
} else {
label_fill(label_str, bitfield_byte_length, hfinfo, out);
}
} else {
if (val_str == NULL)
val_str = "Unknown";
if (out == NULL) /* BASE_NONE so don't put integer in descr */
label_fill(label_str, bitfield_byte_length, hfinfo, val_str);
else
label_fill_descr(label_str, bitfield_byte_length, hfinfo, val_str, out);
}
}
else {
out = hfinfo_number_value_format(hfinfo, buf, value);
label_fill(label_str, bitfield_byte_length, hfinfo, out);
}
}
static void
fill_label_bitfield64(field_info *fi, gchar *label_str, gboolean is_signed)
{
char *p;
int bitfield_byte_length, bitwidth;
guint64 value, unshifted_value;
char buf[48];
const char *out;
header_field_info *hfinfo = fi->hfinfo;
/* Figure out the bit width */
if (fi->flags & FI_VARINT)
bitwidth = fi->length*8;
else
bitwidth = hfinfo_container_bitwidth(hfinfo);
/* Un-shift bits */
if (is_signed)
value = fvalue_get_sinteger64(fi->value);
else
value = fvalue_get_uinteger64(fi->value);
unshifted_value = value;
if (hfinfo->bitmask) {
unshifted_value <<= hfinfo_bitshift(hfinfo);
}
/* Create the bitfield first */
if (fi->flags & FI_VARINT)
p = decode_bitfield_varint_value(label_str, unshifted_value, hfinfo->bitmask, bitwidth);
else
p = decode_bitfield_value(label_str, unshifted_value, hfinfo->bitmask, bitwidth);
bitfield_byte_length = (int) (p - label_str);
/* Fill in the textual info using stored (shifted) value */
if (hfinfo->display == BASE_CUSTOM) {
gchar tmp[ITEM_LABEL_LENGTH];
const custom_fmt_func_64_t fmtfunc64 = (const custom_fmt_func_64_t)hfinfo->strings;
DISSECTOR_ASSERT(fmtfunc64);
fmtfunc64(tmp, value);
label_fill(label_str, bitfield_byte_length, hfinfo, tmp);
}
else if (hfinfo->strings) {
const char *val_str = hf_try_val64_to_str(value, hfinfo);
out = hfinfo_number_vals_format64(hfinfo, buf, value);
if (hfinfo->display & BASE_SPECIAL_VALS) {
/*
* Unique values only display value_string string
* if there is a match. Otherwise it's just a number
*/
if (val_str) {
label_fill_descr(label_str, bitfield_byte_length, hfinfo, val_str, out);
} else {
label_fill(label_str, bitfield_byte_length, hfinfo, out);
}
} else {
if (val_str == NULL)
val_str = "Unknown";
if (out == NULL) /* BASE_NONE so don't put integer in descr */
label_fill(label_str, bitfield_byte_length, hfinfo, val_str);
else
label_fill_descr(label_str, bitfield_byte_length, hfinfo, val_str, out);
}
}
else {
out = hfinfo_number_value_format64(hfinfo, buf, value);
label_fill(label_str, bitfield_byte_length, hfinfo, out);
}
}
static void
fill_label_char(field_info *fi, gchar *label_str)
{
header_field_info *hfinfo = fi->hfinfo;
guint32 value;
char buf[32];
const char *out;
value = fvalue_get_uinteger(fi->value);
/* Fill in the textual info */
if (hfinfo->display == BASE_CUSTOM) {
gchar tmp[ITEM_LABEL_LENGTH];
const custom_fmt_func_t fmtfunc = (const custom_fmt_func_t)hfinfo->strings;
DISSECTOR_ASSERT(fmtfunc);
fmtfunc(tmp, value);
label_fill(label_str, 0, hfinfo, tmp);
}
else if (hfinfo->strings) {
const char *val_str = hf_try_val_to_str_const(value, hfinfo, "Unknown");
out = hfinfo_char_vals_format(hfinfo, buf, value);
label_fill_descr(label_str, 0, hfinfo, val_str, out);
}
else {
out = hfinfo_char_value_format(hfinfo, buf, value);
label_fill(label_str, 0, hfinfo, out);
}
}
static void
fill_label_number(field_info *fi, gchar *label_str, gboolean is_signed)
{
header_field_info *hfinfo = fi->hfinfo;
guint32 value;
char buf[32];
const char *out;
if (is_signed)
value = fvalue_get_sinteger(fi->value);
else
value = fvalue_get_uinteger(fi->value);
/* Fill in the textual info */
if (hfinfo->display == BASE_CUSTOM) {
gchar tmp[ITEM_LABEL_LENGTH];
const custom_fmt_func_t fmtfunc = (const custom_fmt_func_t)hfinfo->strings;
DISSECTOR_ASSERT(fmtfunc);
fmtfunc(tmp, value);
label_fill(label_str, 0, hfinfo, tmp);
}
else if (hfinfo->strings && hfinfo->type != FT_FRAMENUM) {
/*
* It makes no sense to have a value-string table for a
* frame-number field - they're just integers giving
* the ordinal frame number.
*/
const char *val_str = hf_try_val_to_str(value, hfinfo);
out = hfinfo_number_vals_format(hfinfo, buf, value);
if (hfinfo->display & BASE_SPECIAL_VALS) {
/*
* Unique values only display value_string string
* if there is a match. Otherwise it's just a number
*/
if (val_str) {
label_fill_descr(label_str, 0, hfinfo, val_str, out);
} else {
label_fill(label_str, 0, hfinfo, out);
}
} else {
if (val_str == NULL)
val_str = "Unknown";
if (out == NULL) /* BASE_NONE so don't put integer in descr */
label_fill(label_str, 0, hfinfo, val_str);
else
label_fill_descr(label_str, 0, hfinfo, val_str, out);
}
}
else if (IS_BASE_PORT(hfinfo->display)) {
gchar tmp[ITEM_LABEL_LENGTH];
port_with_resolution_to_str_buf(tmp, sizeof(tmp),
display_to_port_type((field_display_e)hfinfo->display), value);
label_fill(label_str, 0, hfinfo, tmp);
}
else {
out = hfinfo_number_value_format(hfinfo, buf, value);
label_fill(label_str, 0, hfinfo, out);
}
}
static void
fill_label_number64(field_info *fi, gchar *label_str, gboolean is_signed)
{
header_field_info *hfinfo = fi->hfinfo;
guint64 value;
char buf[48];
const char *out;
if (is_signed)
value = fvalue_get_sinteger64(fi->value);
else
value = fvalue_get_uinteger64(fi->value);
/* Fill in the textual info */
if (hfinfo->display == BASE_CUSTOM) {
gchar tmp[ITEM_LABEL_LENGTH];
const custom_fmt_func_64_t fmtfunc64 = (const custom_fmt_func_64_t)hfinfo->strings;
DISSECTOR_ASSERT(fmtfunc64);
fmtfunc64(tmp, value);
label_fill(label_str, 0, hfinfo, tmp);
}
else if (hfinfo->strings) {
const char *val_str = hf_try_val64_to_str(value, hfinfo);
out = hfinfo_number_vals_format64(hfinfo, buf, value);
if (hfinfo->display & BASE_SPECIAL_VALS) {
/*
* Unique values only display value_string string
* if there is a match. Otherwise it's just a number
*/
if (val_str) {
label_fill_descr(label_str, 0, hfinfo, val_str, out);
} else {
label_fill(label_str, 0, hfinfo, out);
}
} else {
if (val_str == NULL)
val_str = "Unknown";
if (out == NULL) /* BASE_NONE so don't put integer in descr */
label_fill(label_str, 0, hfinfo, val_str);
else
label_fill_descr(label_str, 0, hfinfo, val_str, out);
}
}
else {
out = hfinfo_number_value_format64(hfinfo, buf, value);
label_fill(label_str, 0, hfinfo, out);
}
}
static size_t
fill_display_label_float(field_info *fi, gchar *label_str)
{
int display;
int digits;
int n;
double value;
display = FIELD_DISPLAY(fi->hfinfo->display);
value = fvalue_get_floating(fi->value);
if (display == BASE_CUSTOM) {
const custom_fmt_func_double_t fmtfunc = (const custom_fmt_func_double_t)fi->hfinfo->strings;
DISSECTOR_ASSERT(fmtfunc);
fmtfunc(label_str, value);
return strlen(label_str);
}
switch (display) {
case BASE_NONE:
if (fi->hfinfo->type == FT_FLOAT)
digits = FLT_DIG;
else
digits = DBL_DIG;
n = snprintf(label_str, ITEM_LABEL_LENGTH, "%.*g", digits, value);
break;
case BASE_DEC:
n = snprintf(label_str, ITEM_LABEL_LENGTH, "%f", value);
break;
case BASE_HEX:
n = snprintf(label_str, ITEM_LABEL_LENGTH, "%a", value);
break;
case BASE_EXP:
n = snprintf(label_str, ITEM_LABEL_LENGTH, "%e", value);
break;
default:
ws_assert_not_reached();
}
if (n < 0) {
return 0; /* error */
}
if ((fi->hfinfo->strings) && (fi->hfinfo->display & BASE_UNIT_STRING)) {
const char *hf_str_val;
hf_str_val = hf_try_double_val_to_str(value, fi->hfinfo);
n += protoo_strlcpy(label_str + n, hf_str_val, ITEM_LABEL_LENGTH - n);
}
if (n > ITEM_LABEL_LENGTH) {
ws_warning("label length too small");
return strlen(label_str);
}
return n;
}
void
fill_label_float(field_info *fi, gchar *label_str)
{
gchar tmp[ITEM_LABEL_LENGTH];
fill_display_label_float(fi, tmp);
label_fill(label_str, 0, fi->hfinfo, tmp);
}
int
hfinfo_bitshift(const header_field_info *hfinfo)
{
return ws_ctz(hfinfo->bitmask);
}
static int
hfinfo_bitoffset(const header_field_info *hfinfo)
{
if (!hfinfo->bitmask) {
return 0;
}
/* ilog2 = first set bit, counting 0 as the last bit; we want 0
* as the first bit */
return hfinfo_container_bitwidth(hfinfo) - 1 - ws_ilog2(hfinfo->bitmask);
}
static int
hfinfo_mask_bitwidth(const header_field_info *hfinfo)
{
if (!hfinfo->bitmask) {
return 0;
}
/* ilog2 = first set bit, ctz = last set bit */
return ws_ilog2(hfinfo->bitmask) - ws_ctz(hfinfo->bitmask) + 1;
}
static int
hfinfo_type_bitwidth(enum ftenum type)
{
int bitwidth = 0;
switch (type) {
case FT_CHAR:
case FT_UINT8:
case FT_INT8:
bitwidth = 8;
break;
case FT_UINT16:
case FT_INT16:
bitwidth = 16;
break;
case FT_UINT24:
case FT_INT24:
bitwidth = 24;
break;
case FT_UINT32:
case FT_INT32:
bitwidth = 32;
break;
case FT_UINT40:
case FT_INT40:
bitwidth = 40;
break;
case FT_UINT48:
case FT_INT48:
bitwidth = 48;
break;
case FT_UINT56:
case FT_INT56:
bitwidth = 56;
break;
case FT_UINT64:
case FT_INT64:
bitwidth = 64;
break;
default:
DISSECTOR_ASSERT_NOT_REACHED();
;
}
return bitwidth;
}
static int
hfinfo_container_bitwidth(const header_field_info *hfinfo)
{
if (!hfinfo->bitmask) {
return 0;
}
if (hfinfo->type == FT_BOOLEAN) {
return hfinfo->display; /* hacky? :) */
}
return hfinfo_type_bitwidth(hfinfo->type);
}
static int
hfinfo_hex_digits(const header_field_info *hfinfo)
{
int bitwidth;
/* If we have a bitmask, hfinfo->type is the width of the container, so not
* appropriate to determine the number of hex digits for the field.
* So instead, we compute it from the bitmask.
*/
if (hfinfo->bitmask != 0) {
bitwidth = hfinfo_mask_bitwidth(hfinfo);
} else {
bitwidth = hfinfo_type_bitwidth(hfinfo->type);
}
/* Divide by 4, rounding up, to get number of hex digits. */
return (bitwidth + 3) / 4;
}
const char *
hfinfo_char_value_format_display(int display, char buf[7], guint32 value)
{
char *ptr = &buf[6];
static const gchar hex_digits[16] =
{ '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
*ptr = '\0';
*(--ptr) = '\'';
/* Properly format value */
if (g_ascii_isprint(value)) {
/*
* Printable, so just show the character, and, if it needs
* to be escaped, escape it.
*/
*(--ptr) = value;
if (value == '\\' || value == '\'')
*(--ptr) = '\\';
} else {
/*
* Non-printable; show it as an escape sequence.
*/
switch (value) {
case '\0':
/*
* Show a NUL with only one digit.
*/
*(--ptr) = '0';
break;
case '\a':
*(--ptr) = 'a';
break;
case '\b':
*(--ptr) = 'b';
break;
case '\f':
*(--ptr) = 'f';
break;
case '\n':
*(--ptr) = 'n';
break;
case '\r':
*(--ptr) = 'r';
break;
case '\t':
*(--ptr) = 't';
break;
case '\v':
*(--ptr) = 'v';
break;
default:
switch (FIELD_DISPLAY(display)) {
case BASE_OCT:
*(--ptr) = (value & 0x7) + '0';
value >>= 3;
*(--ptr) = (value & 0x7) + '0';
value >>= 3;
*(--ptr) = (value & 0x7) + '0';
break;
case BASE_HEX:
*(--ptr) = hex_digits[value & 0x0F];
value >>= 4;
*(--ptr) = hex_digits[value & 0x0F];
*(--ptr) = 'x';
break;
default:
REPORT_DISSECTOR_BUG("Invalid base: %d", FIELD_DISPLAY(display));
}
}
*(--ptr) = '\\';
}
*(--ptr) = '\'';
return ptr;
}
static const char *
hfinfo_number_value_format_display(const header_field_info *hfinfo, int display, char buf[32], guint32 value)
{
char *ptr = &buf[31];
gboolean isint = FT_IS_INT(hfinfo->type);
*ptr = '\0';
/* Properly format value */
switch (FIELD_DISPLAY(display)) {
case BASE_DEC:
return isint ? int_to_str_back(ptr, (gint32) value) : uint_to_str_back(ptr, value);
case BASE_DEC_HEX:
*(--ptr) = ')';
ptr = hex_to_str_back_len(ptr, value, hfinfo_hex_digits(hfinfo));
*(--ptr) = '(';
*(--ptr) = ' ';
ptr = isint ? int_to_str_back(ptr, (gint32) value) : uint_to_str_back(ptr, value);
return ptr;
case BASE_OCT:
return oct_to_str_back(ptr, value);
case BASE_HEX:
return hex_to_str_back_len(ptr, value, hfinfo_hex_digits(hfinfo));
case BASE_HEX_DEC:
*(--ptr) = ')';
ptr = isint ? int_to_str_back(ptr, (gint32) value) : uint_to_str_back(ptr, value);
*(--ptr) = '(';
*(--ptr) = ' ';
ptr = hex_to_str_back_len(ptr, value, hfinfo_hex_digits(hfinfo));
return ptr;
case BASE_PT_UDP:
case BASE_PT_TCP:
case BASE_PT_DCCP:
case BASE_PT_SCTP:
port_with_resolution_to_str_buf(buf, 32,
display_to_port_type((field_display_e)display), value);
return buf;
case BASE_OUI:
{
guint8 p_oui[3];
const gchar *manuf_name;
p_oui[0] = value >> 16 & 0xFF;
p_oui[1] = value >> 8 & 0xFF;
p_oui[2] = value & 0xFF;
/* Attempt an OUI lookup. */
manuf_name = uint_get_manuf_name_if_known(value);
if (manuf_name == NULL) {
/* Could not find an OUI. */
snprintf(buf, 32, "%02x:%02x:%02x", p_oui[0], p_oui[1], p_oui[2]);
}
else {
/* Found an address string. */
snprintf(buf, 32, "%02x:%02x:%02x (%s)", p_oui[0], p_oui[1], p_oui[2], manuf_name);
}
return buf;
}
default:
REPORT_DISSECTOR_BUG("Invalid base: %d", FIELD_DISPLAY(display));
}
return ptr;
}
static const char *
hfinfo_number_value_format_display64(const header_field_info *hfinfo, int display, char buf[48], guint64 value)
{
char *ptr = &buf[47];
gboolean isint = FT_IS_INT(hfinfo->type);
*ptr = '\0';
/* Properly format value */
switch (FIELD_DISPLAY(display)) {
case BASE_DEC:
return isint ? int64_to_str_back(ptr, (gint64) value) : uint64_to_str_back(ptr, value);
case BASE_DEC_HEX:
*(--ptr) = ')';
ptr = hex64_to_str_back_len(ptr, value, hfinfo_hex_digits(hfinfo));
*(--ptr) = '(';
*(--ptr) = ' ';
ptr = isint ? int64_to_str_back(ptr, (gint64) value) : uint64_to_str_back(ptr, value);
return ptr;
case BASE_OCT:
return oct64_to_str_back(ptr, value);
case BASE_HEX:
return hex64_to_str_back_len(ptr, value, hfinfo_hex_digits(hfinfo));
case BASE_HEX_DEC:
*(--ptr) = ')';
ptr = isint ? int64_to_str_back(ptr, (gint64) value) : uint64_to_str_back(ptr, value);
*(--ptr) = '(';
*(--ptr) = ' ';
ptr = hex64_to_str_back_len(ptr, value, hfinfo_hex_digits(hfinfo));
return ptr;
default:
REPORT_DISSECTOR_BUG("Invalid base: %d", FIELD_DISPLAY(display));
}
return ptr;
}
static const char *
hfinfo_number_value_format(const header_field_info *hfinfo, char buf[32], guint32 value)
{
int display = hfinfo->display;
if (hfinfo->type == FT_FRAMENUM) {
/*
* Frame numbers are always displayed in decimal.
*/
display = BASE_DEC;
}
return hfinfo_number_value_format_display(hfinfo, display, buf, value);
}
static const char *
hfinfo_number_value_format64(const header_field_info *hfinfo, char buf[48], guint64 value)
{
int display = hfinfo->display;
if (hfinfo->type == FT_FRAMENUM) {
/*
* Frame numbers are always displayed in decimal.
*/
display = BASE_DEC;
}
return hfinfo_number_value_format_display64(hfinfo, display, buf, value);
}
static const char *
hfinfo_char_value_format(const header_field_info *hfinfo, char buf[32], guint32 value)
{
/* Get the underlying BASE_ value */
int display = FIELD_DISPLAY(hfinfo->display);
return hfinfo_char_value_format_display(display, buf, value);
}
static const char *
hfinfo_numeric_value_format(const header_field_info *hfinfo, char buf[32], guint32 value)
{
/* Get the underlying BASE_ value */
int display = FIELD_DISPLAY(hfinfo->display);
if (hfinfo->type == FT_FRAMENUM) {
/*
* Frame numbers are always displayed in decimal.
*/
display = BASE_DEC;
}
if (IS_BASE_PORT(display)) {
display = BASE_DEC;
} else if (display == BASE_OUI) {
display = BASE_HEX;
}
switch (display) {
case BASE_NONE:
/* case BASE_DEC: */
case BASE_DEC_HEX:
case BASE_OCT: /* XXX, why we're changing BASE_OCT to BASE_DEC? */
case BASE_CUSTOM:
display = BASE_DEC;
break;
/* case BASE_HEX: */
case BASE_HEX_DEC:
display = BASE_HEX;
break;
}
return hfinfo_number_value_format_display(hfinfo, display, buf, value);
}
static const char *
hfinfo_numeric_value_format64(const header_field_info *hfinfo, char buf[48], guint64 value)
{
/* Get the underlying BASE_ value */
int display = FIELD_DISPLAY(hfinfo->display);
if (hfinfo->type == FT_FRAMENUM) {
/*
* Frame numbers are always displayed in decimal.
*/
display = BASE_DEC;
}
switch (display) {
case BASE_NONE:
/* case BASE_DEC: */
case BASE_DEC_HEX:
case BASE_OCT: /* XXX, why we're changing BASE_OCT to BASE_DEC? */
case BASE_CUSTOM:
display = BASE_DEC;
break;
/* case BASE_HEX: */
case BASE_HEX_DEC:
display = BASE_HEX;
break;
}
return hfinfo_number_value_format_display64(hfinfo, display, buf, value);
}
static const char *
hfinfo_char_vals_format(const header_field_info *hfinfo, char buf[32], guint32 value)
{
/* Get the underlying BASE_ value */
int display = FIELD_DISPLAY(hfinfo->display);
return hfinfo_char_value_format_display(display, buf, value);
}
static const char *
hfinfo_number_vals_format(const header_field_info *hfinfo, char buf[32], guint32 value)
{
/* Get the underlying BASE_ value */
int display = FIELD_DISPLAY(hfinfo->display);
if (display == BASE_NONE)
return NULL;
if (display == BASE_DEC_HEX)
display = BASE_DEC;
if (display == BASE_HEX_DEC)
display = BASE_HEX;
return hfinfo_number_value_format_display(hfinfo, display, buf, value);
}
static const char *
hfinfo_number_vals_format64(const header_field_info *hfinfo, char buf[48], guint64 value)
{
/* Get the underlying BASE_ value */
int display = FIELD_DISPLAY(hfinfo->display);
if (display == BASE_NONE)
return NULL;
if (display == BASE_DEC_HEX)
display = BASE_DEC;
if (display == BASE_HEX_DEC)
display = BASE_HEX;
return hfinfo_number_value_format_display64(hfinfo, display, buf, value);
}
const char *
proto_registrar_get_name(const int n)
{
header_field_info *hfinfo;
PROTO_REGISTRAR_GET_NTH(n, hfinfo);
return hfinfo->name;
}
const char *
proto_registrar_get_abbrev(const int n)
{
header_field_info *hfinfo;
PROTO_REGISTRAR_GET_NTH(n, hfinfo);
return hfinfo->abbrev;
}
enum ftenum
proto_registrar_get_ftype(const int n)
{
header_field_info *hfinfo;
PROTO_REGISTRAR_GET_NTH(n, hfinfo);
return hfinfo->type;
}
int
proto_registrar_get_parent(const int n)
{
header_field_info *hfinfo;
PROTO_REGISTRAR_GET_NTH(n, hfinfo);
return hfinfo->parent;
}
gboolean
proto_registrar_is_protocol(const int n)
{
header_field_info *hfinfo;
PROTO_REGISTRAR_GET_NTH(n, hfinfo);
return (((hfinfo->id != hf_text_only) && (hfinfo->parent == -1)) ? TRUE : FALSE);
}
/* Returns length of field in packet (not necessarily the length
* in our internal representation, as in the case of IPv4).
* 0 means undeterminable at time of registration
* -1 means the field is not registered. */
gint
proto_registrar_get_length(const int n)
{
header_field_info *hfinfo;
PROTO_REGISTRAR_GET_NTH(n, hfinfo);
return ftype_wire_size(hfinfo->type);
}
/* Looks for a protocol or a field in a proto_tree. Returns TRUE if
* it exists anywhere, or FALSE if it exists nowhere. */
gboolean
proto_check_for_protocol_or_field(const proto_tree* tree, const int id)
{
GPtrArray *ptrs = proto_get_finfo_ptr_array(tree, id);
if (g_ptr_array_len(ptrs) > 0) {
return TRUE;
}
else {
return FALSE;
}
}
/* Return GPtrArray* of field_info pointers for all hfindex that appear in tree.
* This only works if the hfindex was "primed" before the dissection
* took place, as we just pass back the already-created GPtrArray*.
* The caller should *not* free the GPtrArray*; proto_tree_free_node()
* handles that. */
GPtrArray *
proto_get_finfo_ptr_array(const proto_tree *tree, const int id)
{
if (!tree)
return NULL;
if (PTREE_DATA(tree)->interesting_hfids != NULL)
return (GPtrArray *)g_hash_table_lookup(PTREE_DATA(tree)->interesting_hfids,
GINT_TO_POINTER(id));
else
return NULL;
}
gboolean
proto_tracking_interesting_fields(const proto_tree *tree)
{
GHashTable *interesting_hfids;
if (!tree)
return FALSE;
interesting_hfids = PTREE_DATA(tree)->interesting_hfids;
return (interesting_hfids != NULL) && g_hash_table_size(interesting_hfids);
}
/* Helper struct for proto_find_info() and proto_all_finfos() */
typedef struct {
GPtrArray *array;
int id;
} ffdata_t;
/* Helper function for proto_find_info() */
static gboolean
find_finfo(proto_node *node, gpointer data)
{
field_info *fi = PNODE_FINFO(node);
if (fi && fi->hfinfo) {
if (fi->hfinfo->id == ((ffdata_t*)data)->id) {
g_ptr_array_add(((ffdata_t*)data)->array, fi);
}
}
/* Don't stop traversing. */
return FALSE;
}
/* Helper function for proto_find_first_info() */
static gboolean
find_first_finfo(proto_node *node, gpointer data)
{
field_info *fi = PNODE_FINFO(node);
if (fi && fi->hfinfo) {
if (fi->hfinfo->id == ((ffdata_t*)data)->id) {
g_ptr_array_add(((ffdata_t*)data)->array, fi);
/* Stop traversing. */
return TRUE;
}
}
/* Continue traversing. */
return FALSE;
}
/* Return GPtrArray* of field_info pointers for all hfindex that appear in a tree.
* This works on any proto_tree, primed or unprimed, but actually searches
* the tree, so it is slower than using proto_get_finfo_ptr_array on a primed tree.
* The caller does need to free the returned GPtrArray with
* g_ptr_array_free(<array>, TRUE).
*/
GPtrArray *
proto_find_finfo(proto_tree *tree, const int id)
{
ffdata_t ffdata;
ffdata.array = g_ptr_array_new();
ffdata.id = id;
proto_tree_traverse_pre_order(tree, find_finfo, &ffdata);
return ffdata.array;
}
/* Return GPtrArray* of first field_info pointers for the searched hfindex that appear in a tree.
* This works on any proto_tree, primed or unprimed, but actually searches
* the tree, so it is slower than using proto_get_finfo_ptr_array on a primed tree.
* The caller does need to free the returned GPtrArray with
* g_ptr_array_free(<array>, TRUE).
*/
GPtrArray *
proto_find_first_finfo(proto_tree *tree, const int id)
{
ffdata_t ffdata;
ffdata.array = g_ptr_array_new();
ffdata.id = id;
proto_tree_traverse_pre_order(tree, find_first_finfo, &ffdata);
return ffdata.array;
}
/* Helper function for proto_all_finfos() */
static gboolean
every_finfo(proto_node *node, gpointer data)
{
field_info *fi = PNODE_FINFO(node);
if (fi && fi->hfinfo) {
g_ptr_array_add(((ffdata_t*)data)->array, fi);
}
/* Don't stop traversing. */
return FALSE;
}
/* Return GPtrArray* of field_info pointers containing all hfindexes that appear in a tree. */
GPtrArray *
proto_all_finfos(proto_tree *tree)
{
ffdata_t ffdata;
/* Pre allocate enough space to hold all fields in most cases */
ffdata.array = g_ptr_array_sized_new(512);
ffdata.id = 0;
proto_tree_traverse_pre_order(tree, every_finfo, &ffdata);
return ffdata.array;
}
typedef struct {
guint offset;
field_info *finfo;
tvbuff_t *tvb;
} offset_search_t;
static gboolean
check_for_offset(proto_node *node, gpointer data)
{
field_info *fi = PNODE_FINFO(node);
offset_search_t *offsearch = (offset_search_t *)data;
/* !fi == the top most container node which holds nothing */
if (fi && !proto_item_is_hidden(node) && !proto_item_is_generated(node) && fi->ds_tvb && offsearch->tvb == fi->ds_tvb) {
if (offsearch->offset >= (guint) fi->start &&
offsearch->offset < (guint) (fi->start + fi->length)) {
offsearch->finfo = fi;
return FALSE; /* keep traversing */
}
}
return FALSE; /* keep traversing */
}
/* Search a proto_tree backwards (from leaves to root) looking for the field
* whose start/length occupies 'offset' */
/* XXX - I couldn't find an easy way to search backwards, so I search
* forwards, w/o stopping. Therefore, the last finfo I find will the be
* the one I want to return to the user. This algorithm is inefficient
* and could be re-done, but I'd have to handle all the children and
* siblings of each node myself. When I have more time I'll do that.
* (yeah right) */
field_info *
proto_find_field_from_offset(proto_tree *tree, guint offset, tvbuff_t *tvb)
{
offset_search_t offsearch;
offsearch.offset = offset;
offsearch.finfo = NULL;
offsearch.tvb = tvb;
proto_tree_traverse_pre_order(tree, check_for_offset, &offsearch);
return offsearch.finfo;
}
typedef struct {
gint length;
gchar *buf;
} decoded_data_t;
static gboolean
check_for_undecoded(proto_node *node, gpointer data)
{
field_info *fi = PNODE_FINFO(node);
decoded_data_t* decoded = (decoded_data_t*)data;
gint i;
guint byte;
guint bit;
if (fi && fi->hfinfo->type != FT_PROTOCOL) {
for (i = fi->start; i < fi->start + fi->length && i < decoded->length; i++) {
byte = i / 8;
bit = i % 8;
decoded->buf[byte] |= (1 << bit);
}
}
return FALSE;
}
gchar*
proto_find_undecoded_data(proto_tree *tree, guint length)
{
decoded_data_t decoded;
decoded.length = length;
decoded.buf = (gchar*)wmem_alloc0(PNODE_POOL(tree), length / 8 + 1);
proto_tree_traverse_pre_order(tree, check_for_undecoded, &decoded);
return decoded.buf;
}
/* Dumps the protocols in the registration database to stdout. An independent
* program can take this output and format it into nice tables or HTML or
* whatever.
*
* There is one record per line. The fields are tab-delimited.
*
* Field 1 = protocol name
* Field 2 = protocol short name
* Field 3 = protocol filter name
* Field 4 = protocol enabled
* Field 5 = protocol enabled by default
* Field 6 = protocol can toggle
*/
void
proto_registrar_dump_protocols(void)
{
protocol_t *protocol;
int i;
void *cookie = NULL;
i = proto_get_first_protocol(&cookie);
while (i != -1) {
protocol = find_protocol_by_id(i);
printf("%s\t%s\t%s\t%c\t%c\t%c\n",
protocol->name,
protocol->short_name,
protocol->filter_name,
(proto_is_protocol_enabled_by_default(protocol) ? 'T' : 'F'),
(proto_is_protocol_enabled(protocol) ? 'T' : 'F'),
(proto_can_toggle_protocol(protocol->proto_id) ? 'T' : 'F'));
i = proto_get_next_protocol(&cookie);
}
}
/* Dumps the value_strings, extended value string headers, range_strings
* or true/false strings for fields that have them.
* There is one record per line. Fields are tab-delimited.
* There are four types of records: Value String, Extended Value String Header,
* Range String and True/False String. The first field, 'V', 'E', 'R' or 'T', indicates
* the type of record.
*
* Note that a record will be generated only if the value_string,... is referenced
* in a registered hfinfo entry.
*
*
* Value Strings
* -------------
* Field 1 = 'V'
* Field 2 = Field abbreviation to which this value string corresponds
* Field 3 = Integer value
* Field 4 = String
*
* Extended Value String Headers
* -----------------------------
* Field 1 = 'E'
* Field 2 = Field abbreviation to which this extended value string header corresponds
* Field 3 = Extended Value String "Name"
* Field 4 = Number of entries in the associated value_string array
* Field 5 = Access Type: "Linear Search", "Binary Search", "Direct (indexed) Access"
*
* Range Strings
* -------------
* Field 1 = 'R'
* Field 2 = Field abbreviation to which this range string corresponds
* Field 3 = Integer value: lower bound
* Field 4 = Integer value: upper bound
* Field 5 = String
*
* True/False Strings
* ------------------
* Field 1 = 'T'
* Field 2 = Field abbreviation to which this true/false string corresponds
* Field 3 = True String
* Field 4 = False String
*/
void
proto_registrar_dump_values(void)
{
header_field_info *hfinfo;
int i, len, vi;
const value_string *vals;
const val64_string *vals64;
const range_string *range;
const true_false_string *tfs;
const unit_name_string *units;
len = gpa_hfinfo.len;
for (i = 0; i < len ; i++) {
if (gpa_hfinfo.hfi[i] == NULL)
continue; /* This is a deregistered protocol or field */
PROTO_REGISTRAR_GET_NTH(i, hfinfo);
if (hfinfo->id == hf_text_only) {
continue;
}
/* ignore protocols */
if (proto_registrar_is_protocol(i)) {
continue;
}
/* process header fields */
#if 0 /* XXX: We apparently allow fields with the same name but with differing "strings" content */
/*
* If this field isn't at the head of the list of
* fields with this name, skip this field - all
* fields with the same name are really just versions
* of the same field stored in different bits, and
* should have the same type/radix/value list, and
* just differ in their bit masks. (If a field isn't
* a bitfield, but can be, say, 1 or 2 bytes long,
* it can just be made FT_UINT16, meaning the
* *maximum* length is 2 bytes, and be used
* for all lengths.)
*/
if (hfinfo->same_name_prev_id != -1)
continue;
#endif
vals = NULL;
vals64 = NULL;
range = NULL;
tfs = NULL;
units = NULL;
if (hfinfo->strings != NULL) {
if (FIELD_DISPLAY(hfinfo->display) != BASE_CUSTOM &&
(hfinfo->type == FT_CHAR ||
hfinfo->type == FT_UINT8 ||
hfinfo->type == FT_UINT16 ||
hfinfo->type == FT_UINT24 ||
hfinfo->type == FT_UINT32 ||
hfinfo->type == FT_UINT40 ||
hfinfo->type == FT_UINT48 ||
hfinfo->type == FT_UINT56 ||
hfinfo->type == FT_UINT64 ||
hfinfo->type == FT_INT8 ||
hfinfo->type == FT_INT16 ||
hfinfo->type == FT_INT24 ||
hfinfo->type == FT_INT32 ||
hfinfo->type == FT_INT40 ||
hfinfo->type == FT_INT48 ||
hfinfo->type == FT_INT56 ||
hfinfo->type == FT_INT64 ||
hfinfo->type == FT_FLOAT ||
hfinfo->type == FT_DOUBLE)) {
if (hfinfo->display & BASE_RANGE_STRING) {
range = (const range_string *)hfinfo->strings;
} else if (hfinfo->display & BASE_EXT_STRING) {
if (hfinfo->display & BASE_VAL64_STRING) {
vals64 = VAL64_STRING_EXT_VS_P((const val64_string_ext *)hfinfo->strings);
} else {
vals = VALUE_STRING_EXT_VS_P((const value_string_ext *)hfinfo->strings);
}
} else if (hfinfo->display & BASE_VAL64_STRING) {
vals64 = (const val64_string *)hfinfo->strings;
} else if (hfinfo->display & BASE_UNIT_STRING) {
units = (const unit_name_string *)hfinfo->strings;
} else {
vals = (const value_string *)hfinfo->strings;
}
}
else if (hfinfo->type == FT_BOOLEAN) {
tfs = (const struct true_false_string *)hfinfo->strings;
}
}
/* Print value strings? */
if (vals) {
if (hfinfo->display & BASE_EXT_STRING) {
if (hfinfo->display & BASE_VAL64_STRING) {
val64_string_ext *vse_p = (val64_string_ext *)hfinfo->strings;
if (!val64_string_ext_validate(vse_p)) {
ws_warning("Invalid val64_string_ext ptr for: %s", hfinfo->abbrev);
continue;
}
try_val64_to_str_ext(0, vse_p); /* "prime" the extended val64_string */
printf("E\t%s\t%u\t%s\t%s\n",
hfinfo->abbrev,
VAL64_STRING_EXT_VS_NUM_ENTRIES(vse_p),
VAL64_STRING_EXT_VS_NAME(vse_p),
val64_string_ext_match_type_str(vse_p));
} else {
value_string_ext *vse_p = (value_string_ext *)hfinfo->strings;
if (!value_string_ext_validate(vse_p)) {
ws_warning("Invalid value_string_ext ptr for: %s", hfinfo->abbrev);
continue;
}
try_val_to_str_ext(0, vse_p); /* "prime" the extended value_string */
printf("E\t%s\t%u\t%s\t%s\n",
hfinfo->abbrev,
VALUE_STRING_EXT_VS_NUM_ENTRIES(vse_p),
VALUE_STRING_EXT_VS_NAME(vse_p),
value_string_ext_match_type_str(vse_p));
}
}
vi = 0;
while (vals[vi].strptr) {
/* Print in the proper base */
if (hfinfo->type == FT_CHAR) {
if (g_ascii_isprint(vals[vi].value)) {
printf("V\t%s\t'%c'\t%s\n",
hfinfo->abbrev,
vals[vi].value,
vals[vi].strptr);
} else {
if (hfinfo->display == BASE_HEX) {
printf("V\t%s\t'\\x%02x'\t%s\n",
hfinfo->abbrev,
vals[vi].value,
vals[vi].strptr);
}
else {
printf("V\t%s\t'\\%03o'\t%s\n",
hfinfo->abbrev,
vals[vi].value,
vals[vi].strptr);
}
}
} else {
if (hfinfo->display == BASE_HEX) {
printf("V\t%s\t0x%x\t%s\n",
hfinfo->abbrev,
vals[vi].value,
vals[vi].strptr);
}
else {
printf("V\t%s\t%u\t%s\n",
hfinfo->abbrev,
vals[vi].value,
vals[vi].strptr);
}
}
vi++;
}
}
else if (vals64) {
vi = 0;
while (vals64[vi].strptr) {
printf("V64\t%s\t%" PRIu64 "\t%s\n",
hfinfo->abbrev,
vals64[vi].value,
vals64[vi].strptr);
vi++;
}
}
/* print range strings? */
else if (range) {
vi = 0;
while (range[vi].strptr) {
/* Print in the proper base */
if (FIELD_DISPLAY(hfinfo->display) == BASE_HEX) {
printf("R\t%s\t0x%"PRIx64"\t0x%"PRIx64"\t%s\n",
hfinfo->abbrev,
range[vi].value_min,
range[vi].value_max,
range[vi].strptr);
}
else {
printf("R\t%s\t%"PRIu64"\t%"PRIu64"\t%s\n",
hfinfo->abbrev,
range[vi].value_min,
range[vi].value_max,
range[vi].strptr);
}
vi++;
}
}
/* Print true/false strings? */
else if (tfs) {
printf("T\t%s\t%s\t%s\n", hfinfo->abbrev,
tfs->true_string, tfs->false_string);
}
/* Print unit strings? */
else if (units) {
printf("U\t%s\t%s\t%s\n", hfinfo->abbrev,
units->singular, units->plural ? units->plural : "(no plural)");
}
}
}
/* Prints the number of registered fields.
* Useful for determining an appropriate value for
* PROTO_PRE_ALLOC_HF_FIELDS_MEM.
*
* Returns FALSE if PROTO_PRE_ALLOC_HF_FIELDS_MEM is larger than or equal to
* the number of fields, TRUE otherwise.
*/
gboolean
proto_registrar_dump_fieldcount(void)
{
guint32 i;
header_field_info *hfinfo;
guint32 deregistered_count = 0;
guint32 same_name_count = 0;
guint32 protocol_count = 0;
for (i = 0; i < gpa_hfinfo.len; i++) {
if (gpa_hfinfo.hfi[i] == NULL) {
deregistered_count++;
continue; /* This is a deregistered protocol or header field */
}
PROTO_REGISTRAR_GET_NTH(i, hfinfo);
if (proto_registrar_is_protocol(i))
protocol_count++;
if (hfinfo->same_name_prev_id != -1)
same_name_count++;
}
printf("There are %u header fields registered, of which:\n"
"\t%u are deregistered\n"
"\t%u are protocols\n"
"\t%u have the same name as another field\n\n",
gpa_hfinfo.len, deregistered_count, protocol_count,
same_name_count);
printf("%d fields were pre-allocated.\n%s", PROTO_PRE_ALLOC_HF_FIELDS_MEM,
(gpa_hfinfo.allocated_len > PROTO_PRE_ALLOC_HF_FIELDS_MEM) ?
"* * Please increase PROTO_PRE_ALLOC_HF_FIELDS_MEM (in epan/proto.c)! * *\n\n" :
"\n");
printf("The header field table consumes %u KiB of memory.\n",
(unsigned int)(gpa_hfinfo.allocated_len * sizeof(header_field_info *) / 1024));
printf("The fields themselves consume %u KiB of memory.\n",
(unsigned int)(gpa_hfinfo.len * sizeof(header_field_info) / 1024));
return (gpa_hfinfo.allocated_len > PROTO_PRE_ALLOC_HF_FIELDS_MEM);
}
static void
elastic_add_base_mapping(json_dumper *dumper)
{
json_dumper_set_member_name(dumper, "settings");
json_dumper_begin_object(dumper);
json_dumper_set_member_name(dumper, "index.mapping.total_fields.limit");
json_dumper_value_anyf(dumper, "%d", 1000000);
json_dumper_end_object(dumper);
}
static gchar*
ws_type_to_elastic(guint type _U_)
{
switch(type) {
case FT_UINT16:
case FT_INT16:
case FT_INT32:
case FT_UINT24:
case FT_INT24:
return "integer";
case FT_INT8:
case FT_UINT8:
return "short";
case FT_FRAMENUM:
case FT_UINT32:
case FT_UINT40:
case FT_UINT48:
case FT_UINT56:
case FT_UINT64: // Actually it's not handled by 'long' elastic type.
case FT_INT48:
case FT_INT64:
return "long";
case FT_FLOAT:
case FT_DOUBLE:
return "float";
case FT_IPv6:
case FT_IPv4:
return "ip";
case FT_ABSOLUTE_TIME:
case FT_RELATIVE_TIME:
return "date";
case FT_BYTES:
case FT_UINT_BYTES:
return "byte";
case FT_BOOLEAN:
return "boolean";
default:
return NULL;
}
}
static gchar*
dot_to_underscore(gchar* str)
{
unsigned i;
for (i = 0; i < strlen(str); i++) {
if (str[i] == '.')
str[i] = '_';
}
return str;
}
/* Dumps a mapping file for ElasticSearch
*/
void
proto_registrar_dump_elastic(const gchar* filter)
{
header_field_info *hfinfo;
header_field_info *parent_hfinfo;
guint i;
gboolean open_object = TRUE;
const char* prev_proto = NULL;
gchar* str;
gchar** protos = NULL;
gchar* proto;
gboolean found;
guint j;
gchar* type;
gchar* prev_item = NULL;
/* We have filtering protocols. Extract them. */
if (filter) {
protos = g_strsplit(filter, ",", -1);
}
/*
* To help tracking down the json tree, objects have been appended with a comment:
* n.label -> where n is the indentation level and label the name of the object
*/
json_dumper dumper = {
.output_file = stdout,
.flags = JSON_DUMPER_FLAGS_PRETTY_PRINT,
};
json_dumper_begin_object(&dumper); // 1.root
elastic_add_base_mapping(&dumper);
json_dumper_set_member_name(&dumper, "mappings");
json_dumper_begin_object(&dumper); // 2.mappings
json_dumper_set_member_name(&dumper, "dynamic");
json_dumper_value_anyf(&dumper, "false");
json_dumper_set_member_name(&dumper, "properties");
json_dumper_begin_object(&dumper); // 3.properties
json_dumper_set_member_name(&dumper, "timestamp");
json_dumper_begin_object(&dumper); // 4.timestamp
json_dumper_set_member_name(&dumper, "type");
json_dumper_value_string(&dumper, "date");
json_dumper_end_object(&dumper); // 4.timestamp
json_dumper_set_member_name(&dumper, "layers");
json_dumper_begin_object(&dumper); // 4.layers
json_dumper_set_member_name(&dumper, "properties");
json_dumper_begin_object(&dumper); // 5.properties
for (i = 0; i < gpa_hfinfo.len; i++) {
if (gpa_hfinfo.hfi[i] == NULL)
continue; /* This is a deregistered protocol or header field */
PROTO_REGISTRAR_GET_NTH(i, hfinfo);
/*
* Skip the pseudo-field for "proto_tree_add_text()" since
* we don't want it in the list of filterable protocols.
*/
if (hfinfo->id == hf_text_only)
continue;
if (!proto_registrar_is_protocol(i)) {
PROTO_REGISTRAR_GET_NTH(hfinfo->parent, parent_hfinfo);
/*
* Skip the field if filter protocols have been set and this one's
* parent is not listed.
*/
if (protos) {
found = FALSE;
j = 0;
proto = protos[0];
while(proto) {
if (!g_strcmp0(proto, parent_hfinfo->abbrev)) {
found = TRUE;
break;
}
j++;
proto = protos[j];
}
if (!found)
continue;
}
if (prev_proto && g_strcmp0(parent_hfinfo->abbrev, prev_proto)) {
json_dumper_end_object(&dumper); // 7.properties
json_dumper_end_object(&dumper); // 8.parent_hfinfo->abbrev
open_object = TRUE;
}
prev_proto = parent_hfinfo->abbrev;
if (open_object) {
json_dumper_set_member_name(&dumper, parent_hfinfo->abbrev);
json_dumper_begin_object(&dumper); // 6.parent_hfinfo->abbrev
json_dumper_set_member_name(&dumper, "properties");
json_dumper_begin_object(&dumper); // 7.properties
open_object = FALSE;
}
/* Skip the fields that would map into string. This is the default in elasticsearch. */
type = ws_type_to_elastic(hfinfo->type);
/* when type is NULL, we have the default mapping: string */
if (type) {
str = ws_strdup_printf("%s_%s", prev_proto, hfinfo->abbrev);
dot_to_underscore(str);
if (g_strcmp0(prev_item, str)) {
json_dumper_set_member_name(&dumper, str);
json_dumper_begin_object(&dumper); // 8.hfinfo->abbrev
json_dumper_set_member_name(&dumper, "type");
json_dumper_value_string(&dumper, type);
json_dumper_end_object(&dumper); // 8.hfinfo->abbrev
}
g_free(prev_item);
prev_item = str;
}
}
}
g_free(prev_item);
if (prev_proto) {
json_dumper_end_object(&dumper); // 7.properties
json_dumper_end_object(&dumper); // 6.parent_hfinfo->abbrev
}
json_dumper_end_object(&dumper); // 5.properties
json_dumper_end_object(&dumper); // 4.layers
json_dumper_end_object(&dumper); // 3.properties
json_dumper_end_object(&dumper); // 2.mappings
json_dumper_end_object(&dumper); // 1.root
gboolean ret = json_dumper_finish(&dumper);
DISSECTOR_ASSERT(ret);
g_strfreev(protos);
}
/* Dumps the contents of the registration database to stdout. An independent
* program can take this output and format it into nice tables or HTML or
* whatever.
*
* There is one record per line. Each record is either a protocol or a header
* field, differentiated by the first field. The fields are tab-delimited.
*
* Protocols
* ---------
* Field 1 = 'P'
* Field 2 = descriptive protocol name
* Field 3 = protocol abbreviation
*
* Header Fields
* -------------
* Field 1 = 'F'
* Field 2 = descriptive field name
* Field 3 = field abbreviation
* Field 4 = type ( textual representation of the ftenum type )
* Field 5 = parent protocol abbreviation
* Field 6 = base for display (for integer types); "parent bitfield width" for FT_BOOLEAN
* Field 7 = bitmask: format: hex: 0x....
* Field 8 = blurb describing field
*/
void
proto_registrar_dump_fields(void)
{
header_field_info *hfinfo, *parent_hfinfo;
int i, len;
const char *enum_name;
const char *base_name;
const char *blurb;
char width[5];
len = gpa_hfinfo.len;
for (i = 0; i < len ; i++) {
if (gpa_hfinfo.hfi[i] == NULL)
continue; /* This is a deregistered protocol or header field */
PROTO_REGISTRAR_GET_NTH(i, hfinfo);
/*
* Skip the pseudo-field for "proto_tree_add_text()" since
* we don't want it in the list of filterable fields.
*/
if (hfinfo->id == hf_text_only)
continue;
/* format for protocols */
if (proto_registrar_is_protocol(i)) {
printf("P\t%s\t%s\n", hfinfo->name, hfinfo->abbrev);
}
/* format for header fields */
else {
/*
* If this field isn't at the head of the list of
* fields with this name, skip this field - all
* fields with the same name are really just versions
* of the same field stored in different bits, and
* should have the same type/radix/value list, and
* just differ in their bit masks. (If a field isn't
* a bitfield, but can be, say, 1 or 2 bytes long,
* it can just be made FT_UINT16, meaning the
* *maximum* length is 2 bytes, and be used
* for all lengths.)
*/
if (hfinfo->same_name_prev_id != -1)
continue;
PROTO_REGISTRAR_GET_NTH(hfinfo->parent, parent_hfinfo);
enum_name = ftype_name(hfinfo->type);
base_name = "";
if (hfinfo->type == FT_CHAR ||
hfinfo->type == FT_UINT8 ||
hfinfo->type == FT_UINT16 ||
hfinfo->type == FT_UINT24 ||
hfinfo->type == FT_UINT32 ||
hfinfo->type == FT_UINT40 ||
hfinfo->type == FT_UINT48 ||
hfinfo->type == FT_UINT56 ||
hfinfo->type == FT_UINT64 ||
hfinfo->type == FT_INT8 ||
hfinfo->type == FT_INT16 ||
hfinfo->type == FT_INT24 ||
hfinfo->type == FT_INT32 ||
hfinfo->type == FT_INT40 ||
hfinfo->type == FT_INT48 ||
hfinfo->type == FT_INT56 ||
hfinfo->type == FT_INT64) {
switch (FIELD_DISPLAY(hfinfo->display)) {
case BASE_NONE:
case BASE_DEC:
case BASE_HEX:
case BASE_OCT:
case BASE_DEC_HEX:
case BASE_HEX_DEC:
case BASE_CUSTOM:
case BASE_PT_UDP:
case BASE_PT_TCP:
case BASE_PT_DCCP:
case BASE_PT_SCTP:
case BASE_OUI:
base_name = val_to_str_const(FIELD_DISPLAY(hfinfo->display), hf_display, "????");
break;
default:
base_name = "????";
break;
}
} else if (hfinfo->type == FT_BOOLEAN) {
/* For FT_BOOLEAN: 'display' can be "parent bitfield width" */
snprintf(width, sizeof(width), "%d", hfinfo->display);
base_name = width;
}
blurb = hfinfo->blurb;
if (blurb == NULL)
blurb = "";
else if (strlen(blurb) == 0)
blurb = "\"\"";
printf("F\t%s\t%s\t%s\t%s\t%s\t0x%" PRIx64 "\t%s\n",
hfinfo->name, hfinfo->abbrev, enum_name,
parent_hfinfo->abbrev, base_name,
hfinfo->bitmask, blurb);
}
}
}
/* Dumps all abbreviated field and protocol completions of the given string to
* stdout. An independent program may use this for command-line tab completion
* of fields.
*/
gboolean
proto_registrar_dump_field_completions(char *prefix)
{
header_field_info *hfinfo;
int i, len;
size_t prefix_len;
gboolean matched = FALSE;
prefix_len = strlen(prefix);
len = gpa_hfinfo.len;
for (i = 0; i < len ; i++) {
if (gpa_hfinfo.hfi[i] == NULL)
continue; /* This is a deregistered protocol or header field */
PROTO_REGISTRAR_GET_NTH(i, hfinfo);
/*
* Skip the pseudo-field for "proto_tree_add_text()" since
* we don't want it in the list of filterable fields.
*/
if (hfinfo->id == hf_text_only)
continue;
/* format for protocols */
if (proto_registrar_is_protocol(i)) {
if(0 == strncmp(hfinfo->abbrev, prefix, prefix_len)) {
matched = TRUE;
printf("%s\t%s\n", hfinfo->abbrev, hfinfo->name);
}
}
/* format for header fields */
else {
/*
* If this field isn't at the head of the list of
* fields with this name, skip this field - all
* fields with the same name are really just versions
* of the same field stored in different bits, and
* should have the same type/radix/value list, and
* just differ in their bit masks. (If a field isn't
* a bitfield, but can be, say, 1 or 2 bytes long,
* it can just be made FT_UINT16, meaning the
* *maximum* length is 2 bytes, and be used
* for all lengths.)
*/
if (hfinfo->same_name_prev_id != -1)
continue;
if(0 == strncmp(hfinfo->abbrev, prefix, prefix_len)) {
matched = TRUE;
printf("%s\t%s\n", hfinfo->abbrev, hfinfo->name);
}
}
}
return matched;
}
/* Dumps field types and descriptive names to stdout. An independent
* program can take this output and format it into nice tables or HTML or
* whatever.
*
* There is one record per line. The fields are tab-delimited.
*
* Field 1 = field type name, e.g. FT_UINT8
* Field 2 = descriptive name, e.g. "Unsigned, 1 byte"
*/
void
proto_registrar_dump_ftypes(void)
{
int fte;
for (fte = 0; fte < FT_NUM_TYPES; fte++) {
printf("%s\t%s\n", ftype_name((ftenum_t)fte), ftype_pretty_name((ftenum_t)fte));
}
}
/* This function indicates whether it's possible to construct a
* "match selected" display filter string for the specified field,
* returns an indication of whether it's possible, and, if it's
* possible and "filter" is non-null, constructs the filter and
* sets "*filter" to point to it.
* You do not need to [g_]free() this string since it will be automatically
* freed once the next packet is dissected.
*/
static gboolean
construct_match_selected_string(field_info *finfo, epan_dissect_t *edt,
char **filter)
{
header_field_info *hfinfo;
char *ptr;
int buf_len;
int i;
gint start, length, length_remaining;
guint8 c;
if (!finfo)
return FALSE;
hfinfo = finfo->hfinfo;
DISSECTOR_ASSERT(hfinfo);
/* If we have BASE_NONE and strings (a non-NULL FIELDCONVERT),
* then "the numeric value ... is not used when preparing
* filters for the field in question." If it's any other
* base, we'll generate the filter normally (which will
* be numeric, even though the human-readable string does
* work for filtering.)
*
* XXX - It might be nice to use fvalue_to_string_repr() in
* "proto_item_fill_label()" as well, although, there, you'd
* have to deal with the base *and* with resolved values for
* addresses.
*
* Perhaps in addition to taking the repr type (DISPLAY
* or DFILTER) and the display (base), fvalue_to_string_repr()
* should have the the "strings" values in the header_field_info
* structure for the field as a parameter, so it can have
* if the field is Boolean or an enumerated integer type,
* the tables used to generate human-readable values.
*/
if (hfinfo->strings && FIELD_DISPLAY(hfinfo->display) == BASE_NONE) {
const gchar *str = NULL;
switch (hfinfo->type) {
case FT_INT8:
case FT_INT16:
case FT_INT24:
case FT_INT32:
str = hf_try_val_to_str(fvalue_get_sinteger(finfo->value), hfinfo);
break;
case FT_CHAR:
case FT_UINT8:
case FT_UINT16:
case FT_UINT24:
case FT_UINT32:
str = hf_try_val_to_str(fvalue_get_uinteger(finfo->value), hfinfo);
break;
default:
break;
}
if (str != NULL && filter != NULL) {
*filter = wmem_strdup_printf(NULL, "%s == \"%s\"", hfinfo->abbrev, str);
return TRUE;
}
}
switch (hfinfo->type) {
case FT_PROTOCOL:
if (filter != NULL)
*filter = wmem_strdup(NULL, finfo->hfinfo->abbrev);
break;
case FT_NONE:
/*
* If the length is 0, just match the name of the
* field.
*
* (Also check for negative values, just in case,
* as we'll cast it to an unsigned value later.)
*/
length = finfo->length;
if (length == 0) {
if (filter != NULL)
*filter = wmem_strdup(NULL, finfo->hfinfo->abbrev);
break;
}
if (length < 0)
return FALSE;
/*
* This doesn't have a value, so we'd match
* on the raw bytes at this address.
*
* Should we be allowed to access to the raw bytes?
* If "edt" is NULL, the answer is "no".
*/
if (edt == NULL)
return FALSE;
/*
* Is this field part of the raw frame tvbuff?
* If not, we can't use "frame[N:M]" to match
* it.
*
* XXX - should this be frame-relative, or
* protocol-relative?
*
* XXX - does this fallback for non-registered
* fields even make sense?
*/
if (finfo->ds_tvb != edt->tvb)
return FALSE; /* you lose */
/*
* Don't go past the end of that tvbuff.
*/
length_remaining = tvb_captured_length_remaining(finfo->ds_tvb, finfo->start);
if (length > length_remaining)
length = length_remaining;
if (length <= 0)
return FALSE;
if (filter != NULL) {
start = finfo->start;
buf_len = 32 + length * 3;
*filter = (char *)wmem_alloc0(NULL, buf_len);
ptr = *filter;
ptr += snprintf(ptr, buf_len-(ptr-*filter),
"frame[%d:%d] == ", finfo->start, length);
for (i=0; i<length; i++) {
c = tvb_get_guint8(finfo->ds_tvb, start);
start++;
if (i == 0 ) {
ptr += snprintf(ptr, buf_len-(ptr-*filter), "%02x", c);
}
else {
ptr += snprintf(ptr, buf_len-(ptr-*filter), ":%02x", c);
}
}
}
break;
/* By default, use the fvalue's "to_string_repr" method. */
default:
if (filter != NULL) {
char *str = fvalue_to_string_repr(NULL, finfo->value, FTREPR_DFILTER, finfo->hfinfo->display);
*filter = wmem_strdup_printf(NULL, "%s == %s", hfinfo->abbrev, str);
wmem_free(NULL, str);
}
break;
}
return TRUE;
}
/*
* Returns TRUE if we can do a "match selected" on the field, FALSE
* otherwise.
*/
gboolean
proto_can_match_selected(field_info *finfo, epan_dissect_t *edt)
{
return construct_match_selected_string(finfo, edt, NULL);
}
/* This function attempts to construct a "match selected" display filter
* string for the specified field; if it can do so, it returns a pointer
* to the string, otherwise it returns NULL.
*
* The string is wmem allocated and must be freed with "wmem_free(NULL, ...)".
*/
char *
proto_construct_match_selected_string(field_info *finfo, epan_dissect_t *edt)
{
char *filter = NULL;
if (!construct_match_selected_string(finfo, edt, &filter))
{
wmem_free(NULL, filter);
return NULL;
}
return filter;
}
/* This function is common code for all proto_tree_add_bitmask... functions.
*/
static gboolean
proto_item_add_bitmask_tree(proto_item *item, tvbuff_t *tvb, const int offset,
const int len, const gint ett, int * const *fields,
const int flags, gboolean first,
gboolean use_parent_tree,
proto_tree* tree, guint64 value)
{
guint64 available_bits = G_MAXUINT64;
guint64 bitmask = 0;
guint64 tmpval;
header_field_info *hf;
guint32 integer32;
gint bit_offset;
gint no_of_bits;
if (!*fields)
REPORT_DISSECTOR_BUG("Illegal call of proto_item_add_bitmask_tree without fields");
if (len < 0 || len > 8)
REPORT_DISSECTOR_BUG("Invalid len: %d", len);
/**
* packet-frame.c uses len=0 since the value is taken from the packet
* metadata, not the packet bytes. In that case, assume that all bits
* in the provided value are valid.
*/
if (len > 0) {
available_bits >>= (8 - (guint)len)*8;
}
if (use_parent_tree == FALSE)
tree = proto_item_add_subtree(item, ett);
while (*fields) {
guint64 present_bits;
PROTO_REGISTRAR_GET_NTH(**fields,hf);
DISSECTOR_ASSERT_HINT(hf->bitmask != 0, hf->abbrev);
bitmask |= hf->bitmask;
/* Skip fields that aren't fully present */
present_bits = available_bits & hf->bitmask;
if (present_bits != hf->bitmask) {
fields++;
continue;
}
switch (hf->type) {
case FT_CHAR:
case FT_UINT8:
case FT_UINT16:
case FT_UINT24:
case FT_UINT32:
proto_tree_add_uint(tree, **fields, tvb, offset, len, (guint32)value);
break;
case FT_INT8:
case FT_INT16:
case FT_INT24:
case FT_INT32:
proto_tree_add_int(tree, **fields, tvb, offset, len, (gint32)value);
break;
case FT_UINT40:
case FT_UINT48:
case FT_UINT56:
case FT_UINT64:
proto_tree_add_uint64(tree, **fields, tvb, offset, len, value);
break;
case FT_INT40:
case FT_INT48:
case FT_INT56:
case FT_INT64:
proto_tree_add_int64(tree, **fields, tvb, offset, len, (gint64)value);
break;
case FT_BOOLEAN:
proto_tree_add_boolean64(tree, **fields, tvb, offset, len, value);
break;
default:
REPORT_DISSECTOR_BUG("field %s has type %d (%s) not handled in proto_item_add_bitmask_tree()",
hf->abbrev,
hf->type,
ftype_name(hf->type));
break;
}
if (flags & BMT_NO_APPEND) {
fields++;
continue;
}
tmpval = (value & hf->bitmask) >> hfinfo_bitshift(hf);
/* XXX: README.developer and the comments have always defined
* BMT_NO_INT as "only boolean flags are added to the title /
* don't add non-boolean (integral) fields", but the
* implementation has always added BASE_CUSTOM and fields with
* value_strings, though not fields with unit_strings.
* Possibly this is because some dissectors use a FT_UINT8
* with a value_string for fields that should be a FT_BOOLEAN.
*/
switch (hf->type) {
case FT_CHAR:
if (hf->display == BASE_CUSTOM) {
gchar lbl[ITEM_LABEL_LENGTH];
const custom_fmt_func_t fmtfunc = (const custom_fmt_func_t)hf->strings;
DISSECTOR_ASSERT(fmtfunc);
fmtfunc(lbl, (guint32) tmpval);
proto_item_append_text(item, "%s%s: %s", first ? "" : ", ",
hf->name, lbl);
first = FALSE;
}
else if (hf->strings) {
proto_item_append_text(item, "%s%s: %s", first ? "" : ", ",
hf->name, hf_try_val_to_str_const((guint32) tmpval, hf, "Unknown"));
first = FALSE;
}
else if (!(flags & BMT_NO_INT)) {
char buf[32];
const char *out;
if (!first) {
proto_item_append_text(item, ", ");
}
out = hfinfo_char_value_format(hf, buf, (guint32) tmpval);
proto_item_append_text(item, "%s: %s", hf->name, out);
first = FALSE;
}
break;
case FT_UINT8:
case FT_UINT16:
case FT_UINT24:
case FT_UINT32:
if (hf->display == BASE_CUSTOM) {
gchar lbl[ITEM_LABEL_LENGTH];
const custom_fmt_func_t fmtfunc = (const custom_fmt_func_t)hf->strings;
DISSECTOR_ASSERT(fmtfunc);
fmtfunc(lbl, (guint32) tmpval);
proto_item_append_text(item, "%s%s: %s", first ? "" : ", ",
hf->name, lbl);
first = FALSE;
}
else if ((hf->strings) &&(!(hf->display & (BASE_UNIT_STRING|BASE_SPECIAL_VALS)))) {
proto_item_append_text(item, "%s%s: %s", first ? "" : ", ",
hf->name, hf_try_val_to_str_const((guint32) tmpval, hf, "Unknown"));
first = FALSE;
}
else if (!(flags & BMT_NO_INT)) {
char buf[32];
const char *out = NULL;
if (!first) {
proto_item_append_text(item, ", ");
}
if (hf->strings && hf->display & BASE_SPECIAL_VALS) {
out = hf_try_val_to_str((guint32) tmpval, hf);
}
if (out == NULL) {
out = hfinfo_number_value_format(hf, buf, (guint32) tmpval);
}
proto_item_append_text(item, "%s: %s", hf->name, out);
if (hf->strings && hf->display & BASE_UNIT_STRING) {
proto_item_append_text(item, "%s", unit_name_string_get_value((guint32) tmpval, (const unit_name_string*)hf->strings));
}
first = FALSE;
}
break;
case FT_INT8:
case FT_INT16:
case FT_INT24:
case FT_INT32:
integer32 = (guint32) tmpval;
if (hf->bitmask) {
no_of_bits = ws_count_ones(hf->bitmask);
integer32 = ws_sign_ext32(integer32, no_of_bits);
}
if (hf->display == BASE_CUSTOM) {
gchar lbl[ITEM_LABEL_LENGTH];
const custom_fmt_func_t fmtfunc = (const custom_fmt_func_t)hf->strings;
DISSECTOR_ASSERT(fmtfunc);
fmtfunc(lbl, (gint32) integer32);
proto_item_append_text(item, "%s%s: %s", first ? "" : ", ",
hf->name, lbl);
first = FALSE;
}
else if ((hf->strings) &&(!(hf->display & (BASE_UNIT_STRING|BASE_SPECIAL_VALS)))) {
proto_item_append_text(item, "%s%s: %s", first ? "" : ", ",
hf->name, hf_try_val_to_str_const((gint32) integer32, hf, "Unknown"));
first = FALSE;
}
else if (!(flags & BMT_NO_INT)) {
char buf[32];
const char *out = NULL;
if (!first) {
proto_item_append_text(item, ", ");
}
if (hf->strings && hf->display & BASE_SPECIAL_VALS) {
out = hf_try_val_to_str((gint32) integer32, hf);
}
if (out == NULL) {
out = hfinfo_number_value_format(hf, buf, (gint32) integer32);
}
proto_item_append_text(item, "%s: %s", hf->name, out);
if (hf->display & BASE_UNIT_STRING) {
proto_item_append_text(item, "%s", unit_name_string_get_value((guint32) tmpval, (const unit_name_string*)hf->strings));
}
first = FALSE;
}
break;
case FT_UINT40:
case FT_UINT48:
case FT_UINT56:
case FT_UINT64:
if (hf->display == BASE_CUSTOM) {
gchar lbl[ITEM_LABEL_LENGTH];
const custom_fmt_func_64_t fmtfunc = (const custom_fmt_func_64_t)hf->strings;
DISSECTOR_ASSERT(fmtfunc);
fmtfunc(lbl, tmpval);
proto_item_append_text(item, "%s%s: %s", first ? "" : ", ",
hf->name, lbl);
first = FALSE;
}
else if ((hf->strings) &&(!(hf->display & (BASE_UNIT_STRING|BASE_SPECIAL_VALS)))) {
proto_item_append_text(item, "%s%s: %s", first ? "" : ", ",
hf->name, hf_try_val64_to_str_const(tmpval, hf, "Unknown"));
first = FALSE;
}
else if (!(flags & BMT_NO_INT)) {
char buf[48];
const char *out = NULL;
if (!first) {
proto_item_append_text(item, ", ");
}
if (hf->strings && hf->display & BASE_SPECIAL_VALS) {
out = hf_try_val64_to_str(tmpval, hf);
}
if (out == NULL) {
out = hfinfo_number_value_format64(hf, buf, tmpval);
}
proto_item_append_text(item, "%s: %s", hf->name, out);
if (hf->strings && hf->display & BASE_UNIT_STRING) {
proto_item_append_text(item, "%s", unit_name_string_get_value64(tmpval, (const unit_name_string*)hf->strings));
}
first = FALSE;
}
break;
case FT_INT40:
case FT_INT48:
case FT_INT56:
case FT_INT64:
if (hf->bitmask) {
no_of_bits = ws_count_ones(hf->bitmask);
tmpval = ws_sign_ext64(tmpval, no_of_bits);
}
if (hf->display == BASE_CUSTOM) {
gchar lbl[ITEM_LABEL_LENGTH];
const custom_fmt_func_64_t fmtfunc = (const custom_fmt_func_64_t)hf->strings;
DISSECTOR_ASSERT(fmtfunc);
fmtfunc(lbl, (gint64) tmpval);
proto_item_append_text(item, "%s%s: %s", first ? "" : ", ",
hf->name, lbl);
first = FALSE;
}
else if ((hf->strings) &&(!(hf->display & (BASE_UNIT_STRING|BASE_SPECIAL_VALS)))) {
proto_item_append_text(item, "%s%s: %s", first ? "" : ", ",
hf->name, hf_try_val64_to_str_const((gint64) tmpval, hf, "Unknown"));
first = FALSE;
}
else if (!(flags & BMT_NO_INT)) {
char buf[48];
const char *out = NULL;
if (!first) {
proto_item_append_text(item, ", ");
}
if (hf->strings && hf->display & BASE_SPECIAL_VALS) {
out = hf_try_val64_to_str((gint64) tmpval, hf);
}
if (out == NULL) {
out = hfinfo_number_value_format64(hf, buf, (gint64) tmpval);
}
proto_item_append_text(item, "%s: %s", hf->name, out);
if (hf->strings && hf->display & BASE_UNIT_STRING) {
proto_item_append_text(item, "%s", unit_name_string_get_value64(tmpval, (const unit_name_string*)hf->strings));
}
first = FALSE;
}
break;
case FT_BOOLEAN:
if (hf->strings && !(flags & BMT_NO_TFS)) {
/* If we have true/false strings, emit full - otherwise messages
might look weird */
const struct true_false_string *tfs =
(const struct true_false_string *)hf->strings;
if (tmpval) {
proto_item_append_text(item, "%s%s: %s", first ? "" : ", ",
hf->name, tfs->true_string);
first = FALSE;
} else if (!(flags & BMT_NO_FALSE)) {
proto_item_append_text(item, "%s%s: %s", first ? "" : ", ",
hf->name, tfs->false_string);
first = FALSE;
}
} else if (hf->bitmask & value) {
/* If the flag is set, show the name */
proto_item_append_text(item, "%s%s", first ? "" : ", ", hf->name);
first = FALSE;
}
break;
default:
REPORT_DISSECTOR_BUG("field %s has type %d (%s) not handled in proto_item_add_bitmask_tree()",
hf->abbrev,
hf->type,
ftype_name(hf->type));
break;
}
fields++;
}
/* XXX: We don't pass the hfi into this function. Perhaps we should,
* but then again most dissectors don't set the bitmask field for
* the higher level bitmask hfi, so calculate the bitmask from the
* fields present. */
if (item) {
bit_offset = len*8 - 1 - ws_ilog2(bitmask);
no_of_bits = ws_ilog2(bitmask) - ws_ctz(bitmask) + 1;
FI_SET_FLAG(PNODE_FINFO(item), FI_BITS_OFFSET(bit_offset));
FI_SET_FLAG(PNODE_FINFO(item), FI_BITS_SIZE(no_of_bits));
}
return first;
}
/* This function will dissect a sequence of bytes that describe a
* bitmask and supply the value of that sequence through a pointer.
* hf_hdr is a 8/16/24/32/40/48/56/64 bit integer that describes the bitmask
* to be dissected.
* This field will form an expansion under which the individual fields of the
* bitmask is dissected and displayed.
* This field must be of the type FT_[U]INT{8|16|24|32|40|48|56|64}.
*
* fields is an array of pointers to int that lists all the fields of the
* bitmask. These fields can be either of the type FT_BOOLEAN for flags
* or another integer of the same type/size as hf_hdr with a mask specified.
* This array is terminated by a NULL entry.
*
* FT_BOOLEAN bits that are set to 1 will have the name added to the expansion.
* FT_integer fields that have a value_string attached will have the
* matched string displayed on the expansion line.
*/
proto_item *
proto_tree_add_bitmask_ret_uint64(proto_tree *parent_tree, tvbuff_t *tvb,
const guint offset, const int hf_hdr,
const gint ett, int * const *fields,
const guint encoding, guint64 *retval)
{
return proto_tree_add_bitmask_with_flags_ret_uint64(parent_tree, tvb, offset, hf_hdr, ett, fields, encoding, BMT_NO_INT|BMT_NO_TFS, retval);
}
/* This function will dissect a sequence of bytes that describe a
* bitmask.
* hf_hdr is a 8/16/24/32/40/48/56/64 bit integer that describes the bitmask
* to be dissected.
* This field will form an expansion under which the individual fields of the
* bitmask is dissected and displayed.
* This field must be of the type FT_[U]INT{8|16|24|32|40|48|56|64}.
*
* fields is an array of pointers to int that lists all the fields of the
* bitmask. These fields can be either of the type FT_BOOLEAN for flags
* or another integer of the same type/size as hf_hdr with a mask specified.
* This array is terminated by a NULL entry.
*
* FT_BOOLEAN bits that are set to 1 will have the name added to the expansion.
* FT_integer fields that have a value_string attached will have the
* matched string displayed on the expansion line.
*/
proto_item *
proto_tree_add_bitmask(proto_tree *parent_tree, tvbuff_t *tvb,
const guint offset, const int hf_hdr,
const gint ett, int * const *fields,
const guint encoding)
{
return proto_tree_add_bitmask_with_flags(parent_tree, tvb, offset, hf_hdr, ett, fields, encoding, BMT_NO_INT|BMT_NO_TFS);
}
/* The same as proto_tree_add_bitmask_ret_uint64(), but uses user-supplied flags to determine
* what data is appended to the header.
*/
proto_item *
proto_tree_add_bitmask_with_flags_ret_uint64(proto_tree *parent_tree, tvbuff_t *tvb, const guint offset,
const int hf_hdr, const gint ett, int * const *fields, const guint encoding, const int flags,
guint64 *retval)
{
proto_item *item = NULL;
header_field_info *hf;
int len;
guint64 value;
PROTO_REGISTRAR_GET_NTH(hf_hdr,hf);
DISSECTOR_ASSERT_FIELD_TYPE_IS_INTEGRAL(hf);
len = ftype_wire_size(hf->type);
value = get_uint64_value(parent_tree, tvb, offset, len, encoding);
if (parent_tree) {
item = proto_tree_add_item(parent_tree, hf_hdr, tvb, offset, len, encoding);
proto_item_add_bitmask_tree(item, tvb, offset, len, ett, fields,
flags, FALSE, FALSE, NULL, value);
}
*retval = value;
if (hf->bitmask) {
/* Mask out irrelevant portions */
*retval &= hf->bitmask;
/* Shift bits */
*retval >>= hfinfo_bitshift(hf);
}
return item;
}
/* The same as proto_tree_add_bitmask_ret_uint64(), but uses user-supplied flags to determine
* what data is appended to the header.
*/
proto_item *
proto_tree_add_bitmask_with_flags(proto_tree *parent_tree, tvbuff_t *tvb, const guint offset,
const int hf_hdr, const gint ett, int * const *fields, const guint encoding, const int flags)
{
proto_item *item = NULL;
header_field_info *hf;
int len;
guint64 value;
PROTO_REGISTRAR_GET_NTH(hf_hdr,hf);
DISSECTOR_ASSERT_FIELD_TYPE_IS_INTEGRAL(hf);
if (parent_tree) {
len = ftype_wire_size(hf->type);
item = proto_tree_add_item(parent_tree, hf_hdr, tvb, offset, len, encoding);
value = get_uint64_value(parent_tree, tvb, offset, len, encoding);
proto_item_add_bitmask_tree(item, tvb, offset, len, ett, fields,
flags, FALSE, FALSE, NULL, value);
}
return item;
}
/* Similar to proto_tree_add_bitmask(), but with a passed in value (presumably because it
can't be retrieved directly from tvb) */
proto_item *
proto_tree_add_bitmask_value(proto_tree *parent_tree, tvbuff_t *tvb, const guint offset,
const int hf_hdr, const gint ett, int * const *fields, const guint64 value)
{
return proto_tree_add_bitmask_value_with_flags(parent_tree, tvb, offset,
hf_hdr, ett, fields, value, BMT_NO_INT|BMT_NO_TFS);
}
/* Similar to proto_tree_add_bitmask_value(), but with control of flag values */
WS_DLL_PUBLIC proto_item *
proto_tree_add_bitmask_value_with_flags(proto_tree *parent_tree, tvbuff_t *tvb, const guint offset,
const int hf_hdr, const gint ett, int * const *fields, const guint64 value, const int flags)
{
proto_item *item = NULL;
header_field_info *hf;
int len;
PROTO_REGISTRAR_GET_NTH(hf_hdr,hf);
DISSECTOR_ASSERT_FIELD_TYPE_IS_INTEGRAL(hf);
/* the proto_tree_add_uint/_uint64() calls below
will fail if tvb==NULL and len!=0 */
len = tvb ? ftype_wire_size(hf->type) : 0;
if (parent_tree) {
if (len <= 4)
item = proto_tree_add_uint(parent_tree, hf_hdr, tvb, offset, len, (guint32)value);
else
item = proto_tree_add_uint64(parent_tree, hf_hdr, tvb, offset, len, value);
proto_item_add_bitmask_tree(item, tvb, offset, len, ett, fields,
flags, FALSE, FALSE, NULL, value);
}
return item;
}
/* Similar to proto_tree_add_bitmask(), but with no "header" item to group all of the fields */
void
proto_tree_add_bitmask_list(proto_tree *tree, tvbuff_t *tvb, const guint offset,
const int len, int * const *fields, const guint encoding)
{
guint64 value;
if (tree) {
value = get_uint64_value(tree, tvb, offset, len, encoding);
proto_item_add_bitmask_tree(NULL, tvb, offset, len, -1, fields,
BMT_NO_APPEND, FALSE, TRUE, tree, value);
}
}
WS_DLL_PUBLIC void
proto_tree_add_bitmask_list_ret_uint64(proto_tree *tree, tvbuff_t *tvb, const guint offset,
const int len, int * const *fields, const guint encoding, guint64 *retval)
{
guint64 value;
value = get_uint64_value(tree, tvb, offset, len, encoding);
if (tree) {
proto_item_add_bitmask_tree(NULL, tvb, offset, len, -1, fields,
BMT_NO_APPEND, FALSE, TRUE, tree, value);
}
if (retval) {
*retval = value;
}
}
WS_DLL_PUBLIC void
proto_tree_add_bitmask_list_value(proto_tree *tree, tvbuff_t *tvb, const guint offset,
const int len, int * const *fields, const guint64 value)
{
if (tree) {
proto_item_add_bitmask_tree(NULL, tvb, offset, len, -1, fields,
BMT_NO_APPEND, FALSE, TRUE, tree, value);
}
}
/* The same as proto_tree_add_bitmask(), but using a caller-supplied length.
* This is intended to support bitmask fields whose lengths can vary, perhaps
* as the underlying standard evolves over time.
* With this API there is the possibility of being called to display more or
* less data than the dissector was coded to support.
* In such cases, it is assumed that bitmasks are extended on the MSb end.
* Thus when presented with "too much" or "too little" data, MSbits will be
* ignored or MSfields sacrificed.
*
* Only fields for which all defined bits are available are displayed.
*/
proto_item *
proto_tree_add_bitmask_len(proto_tree *parent_tree, tvbuff_t *tvb,
const guint offset, const guint len, const int hf_hdr,
const gint ett, int * const *fields, struct expert_field* exp,
const guint encoding)
{
proto_item *item = NULL;
header_field_info *hf;
guint decodable_len;
guint decodable_offset;
guint32 decodable_value;
guint64 value;
PROTO_REGISTRAR_GET_NTH(hf_hdr, hf);
DISSECTOR_ASSERT_FIELD_TYPE_IS_INTEGRAL(hf);
decodable_offset = offset;
decodable_len = MIN(len, (guint) ftype_wire_size(hf->type));
/* If we are ftype_wire_size-limited,
* make sure we decode as many LSBs as possible.
*/
if (encoding == ENC_BIG_ENDIAN) {
decodable_offset += (len - decodable_len);
}
if (parent_tree) {
decodable_value = get_uint_value(parent_tree, tvb, decodable_offset,
decodable_len, encoding);
/* The root item covers all the bytes even if we can't decode them all */
item = proto_tree_add_uint(parent_tree, hf_hdr, tvb, offset, len,
decodable_value);
}
if (decodable_len < len) {
/* Dissector likely requires updating for new protocol revision */
expert_add_info_format(NULL, item, exp,
"Only least-significant %d of %d bytes decoded",
decodable_len, len);
}
if (item) {
value = get_uint64_value(parent_tree, tvb, decodable_offset, decodable_len, encoding);
proto_item_add_bitmask_tree(item, tvb, decodable_offset, decodable_len,
ett, fields, BMT_NO_INT|BMT_NO_TFS, FALSE, FALSE, NULL, value);
}
return item;
}
/* The same as proto_tree_add_bitmask(), but using an arbitrary text as a top-level item */
proto_item *
proto_tree_add_bitmask_text(proto_tree *parent_tree, tvbuff_t *tvb,
const guint offset, const guint len,
const char *name, const char *fallback,
const gint ett, int * const *fields,
const guint encoding, const int flags)
{
proto_item *item = NULL;
guint64 value;
if (parent_tree) {
item = proto_tree_add_text_internal(parent_tree, tvb, offset, len, "%s", name ? name : "");
value = get_uint64_value(parent_tree, tvb, offset, len, encoding);
if (proto_item_add_bitmask_tree(item, tvb, offset, len, ett, fields,
flags, TRUE, FALSE, NULL, value) && fallback) {
/* Still at first item - append 'fallback' text if any */
proto_item_append_text(item, "%s", fallback);
}
}
return item;
}
proto_item *
proto_tree_add_bits_item(proto_tree *tree, const int hfindex, tvbuff_t *tvb,
const guint bit_offset, const gint no_of_bits,
const guint encoding)
{
header_field_info *hfinfo;
gint octet_length;
gint octet_offset;
PROTO_REGISTRAR_GET_NTH(hfindex, hfinfo);
if (no_of_bits < 0) {
THROW(ReportedBoundsError);
}
octet_length = (no_of_bits + 7) >> 3;
octet_offset = bit_offset >> 3;
test_length(hfinfo, tvb, octet_offset, octet_length, encoding);
/* Yes, we try to fake this item again in proto_tree_add_bits_ret_val()
* but only after doing a bunch more work (which we can, in the common
* case, shortcut here).
*/
CHECK_FOR_NULL_TREE(tree);
TRY_TO_FAKE_THIS_ITEM(tree, hfindex, hfinfo);
return proto_tree_add_bits_ret_val(tree, hfindex, tvb, bit_offset, no_of_bits, NULL, encoding);
}
/*
* This function will dissect a sequence of bits that does not need to be byte aligned; the bits
* set will be shown in the tree as ..10 10.. and the integer value returned if return_value is set.
* Offset should be given in bits from the start of the tvb.
*/
static proto_item *
_proto_tree_add_bits_ret_val(proto_tree *tree, const int hfindex, tvbuff_t *tvb,
const guint bit_offset, const gint no_of_bits,
guint64 *return_value, const guint encoding)
{
gint offset;
guint length;
guint8 tot_no_bits;
char *bf_str;
char lbl_str[ITEM_LABEL_LENGTH];
guint64 value = 0;
guint8 *bytes = NULL;
size_t bytes_length = 0;
proto_item *pi;
header_field_info *hf_field;
/* We can't fake it just yet. We have to fill in the 'return_value' parameter */
PROTO_REGISTRAR_GET_NTH(hfindex, hf_field);
if (hf_field->bitmask != 0) {
REPORT_DISSECTOR_BUG("Incompatible use of proto_tree_add_bits_ret_val"
" with field '%s' (%s) with bitmask != 0",
hf_field->abbrev, hf_field->name);
}
if (no_of_bits < 0) {
THROW(ReportedBoundsError);
} else if (no_of_bits == 0) {
REPORT_DISSECTOR_BUG("field %s passed to proto_tree_add_bits_ret_val() has a bit width of 0",
hf_field->abbrev);
}
/* Byte align offset */
offset = bit_offset>>3;
/*
* Calculate the number of octets used to hold the bits
*/
tot_no_bits = ((bit_offset&0x7) + no_of_bits);
length = (tot_no_bits + 7) >> 3;
if (no_of_bits < 65) {
value = tvb_get_bits64(tvb, bit_offset, no_of_bits, encoding);
} else if (hf_field->type != FT_BYTES) {
REPORT_DISSECTOR_BUG("field %s passed to proto_tree_add_bits_ret_val() has a bit width of %u > 65",
hf_field->abbrev, no_of_bits);
return NULL;
}
/* Sign extend for signed types */
switch (hf_field->type) {
case FT_INT8:
case FT_INT16:
case FT_INT24:
case FT_INT32:
case FT_INT40:
case FT_INT48:
case FT_INT56:
case FT_INT64:
value = ws_sign_ext64(value, no_of_bits);
break;
default:
break;
}
if (return_value) {
*return_value = value;
}
/* Coast clear. Try and fake it */
CHECK_FOR_NULL_TREE(tree);
TRY_TO_FAKE_THIS_ITEM(tree, hfindex, hf_field);
bf_str = decode_bits_in_field(PNODE_POOL(tree), bit_offset, no_of_bits, value, encoding);
switch (hf_field->type) {
case FT_BOOLEAN:
/* Boolean field */
return proto_tree_add_boolean_format(tree, hfindex, tvb, offset, length, (guint32)value,
"%s = %s: %s",
bf_str, hf_field->name, tfs_get_string(!!value, hf_field->strings));
break;
case FT_CHAR:
pi = proto_tree_add_uint(tree, hfindex, tvb, offset, length, (guint32)value);
fill_label_char(PITEM_FINFO(pi), lbl_str);
break;
case FT_UINT8:
case FT_UINT16:
case FT_UINT24:
case FT_UINT32:
pi = proto_tree_add_uint(tree, hfindex, tvb, offset, length, (guint32)value);
fill_label_number(PITEM_FINFO(pi), lbl_str, FALSE);
break;
case FT_INT8:
case FT_INT16:
case FT_INT24:
case FT_INT32:
pi = proto_tree_add_int(tree, hfindex, tvb, offset, length, (gint32)value);
fill_label_number(PITEM_FINFO(pi), lbl_str, TRUE);
break;
case FT_UINT40:
case FT_UINT48:
case FT_UINT56:
case FT_UINT64:
pi = proto_tree_add_uint64(tree, hfindex, tvb, offset, length, value);
fill_label_number64(PITEM_FINFO(pi), lbl_str, FALSE);
break;
case FT_INT40:
case FT_INT48:
case FT_INT56:
case FT_INT64:
pi = proto_tree_add_int64(tree, hfindex, tvb, offset, length, (gint64)value);
fill_label_number64(PITEM_FINFO(pi), lbl_str, TRUE);
break;
case FT_BYTES:
bytes = tvb_get_bits_array(PNODE_POOL(tree), tvb, bit_offset, no_of_bits, &bytes_length, encoding);
pi = proto_tree_add_bytes_with_length(tree, hfindex, tvb, offset, length, bytes, (gint) bytes_length);
proto_item_fill_label(PITEM_FINFO(pi), lbl_str);
proto_item_set_text(pi, "%s", lbl_str);
return pi;
break;
/* TODO: should handle FT_UINT_BYTES ? */
default:
REPORT_DISSECTOR_BUG("field %s has type %d (%s) not handled in proto_tree_add_bits_ret_val()",
hf_field->abbrev,
hf_field->type,
ftype_name(hf_field->type));
return NULL;
break;
}
proto_item_set_text(pi, "%s = %s", bf_str, lbl_str);
return pi;
}
proto_item *
proto_tree_add_split_bits_item_ret_val(proto_tree *tree, const int hfindex, tvbuff_t *tvb,
const guint bit_offset, const crumb_spec_t *crumb_spec,
guint64 *return_value)
{
proto_item *pi;
gint no_of_bits;
gint octet_offset;
guint mask_initial_bit_offset;
guint mask_greatest_bit_offset;
guint octet_length;
guint8 i;
char bf_str[256];
char lbl_str[ITEM_LABEL_LENGTH];
guint64 value;
guint64 composite_bitmask;
guint64 composite_bitmap;
header_field_info *hf_field;
/* We can't fake it just yet. We have to fill in the 'return_value' parameter */
PROTO_REGISTRAR_GET_NTH(hfindex, hf_field);
if (hf_field->bitmask != 0) {
REPORT_DISSECTOR_BUG("Incompatible use of proto_tree_add_split_bits_item_ret_val"
" with field '%s' (%s) with bitmask != 0",
hf_field->abbrev, hf_field->name);
}
mask_initial_bit_offset = bit_offset % 8;
no_of_bits = 0;
value = 0;
i = 0;
mask_greatest_bit_offset = 0;
composite_bitmask = 0;
composite_bitmap = 0;
while (crumb_spec[i].crumb_bit_length != 0) {
guint64 crumb_mask, crumb_value;
guint8 crumb_end_bit_offset;
crumb_value = tvb_get_bits64(tvb,
bit_offset + crumb_spec[i].crumb_bit_offset,
crumb_spec[i].crumb_bit_length,
ENC_BIG_ENDIAN);
value += crumb_value;
no_of_bits += crumb_spec[i].crumb_bit_length;
DISSECTOR_ASSERT_HINT(no_of_bits <= 64, "a value larger than 64 bits cannot be represented");
/* The bitmask is 64 bit, left-aligned, starting at the first bit of the
octet containing the initial offset.
If the mask is beyond 32 bits, then give up on bit map display.
This could be improved in future, probably showing a table
of 32 or 64 bits per row */
if (mask_greatest_bit_offset < 32) {
crumb_end_bit_offset = mask_initial_bit_offset
+ crumb_spec[i].crumb_bit_offset
+ crumb_spec[i].crumb_bit_length;
crumb_mask = (G_GUINT64_CONSTANT(1) << crumb_spec[i].crumb_bit_length) - 1;
if (crumb_end_bit_offset > mask_greatest_bit_offset) {
mask_greatest_bit_offset = crumb_end_bit_offset;
}
/* Currently the bitmap of the crumbs are only shown if
* smaller than 32 bits. Do not bother calculating the
* mask if it is larger than that. */
if (crumb_end_bit_offset <= 32) {
composite_bitmask |= (crumb_mask << (64 - crumb_end_bit_offset));
composite_bitmap |= (crumb_value << (64 - crumb_end_bit_offset));
}
}
/* Shift left for the next segment */
value <<= crumb_spec[++i].crumb_bit_length;
}
/* Sign extend for signed types */
switch (hf_field->type) {
case FT_INT8:
case FT_INT16:
case FT_INT24:
case FT_INT32:
case FT_INT40:
case FT_INT48:
case FT_INT56:
case FT_INT64:
value = ws_sign_ext64(value, no_of_bits);
break;
default:
break;
}
if (return_value) {
*return_value = value;
}
/* Coast clear. Try and fake it */
CHECK_FOR_NULL_TREE(tree);
TRY_TO_FAKE_THIS_ITEM(tree, hfindex, hf_field);
/* initialise the format string */
bf_str[0] = '\0';
octet_offset = bit_offset >> 3;
/* Round up mask length to nearest octet */
octet_length = ((mask_greatest_bit_offset + 7) >> 3);
mask_greatest_bit_offset = octet_length << 3;
/* As noted above, we currently only produce a bitmap if the crumbs span less than 4 octets of the tvb.
It would be a useful enhancement to eliminate this restriction. */
if (mask_greatest_bit_offset > 0 && mask_greatest_bit_offset <= 32) {
other_decode_bitfield_value(bf_str,
(guint32)(composite_bitmap >> (64 - mask_greatest_bit_offset)),
(guint32)(composite_bitmask >> (64 - mask_greatest_bit_offset)),
mask_greatest_bit_offset);
} else {
/* If the bitmask is too large, try to describe its contents. */
snprintf(bf_str, sizeof(bf_str), "%d bits", no_of_bits);
}
switch (hf_field->type) {
case FT_BOOLEAN: /* it is a bit odd to have a boolean encoded as split-bits, but possible, I suppose? */
/* Boolean field */
return proto_tree_add_boolean_format(tree, hfindex,
tvb, octet_offset, octet_length, (guint32)value,
"%s = %s: %s",
bf_str, hf_field->name, tfs_get_string(!!value, hf_field->strings));
break;
case FT_CHAR:
pi = proto_tree_add_uint(tree, hfindex, tvb, octet_offset, octet_length, (guint32)value);
fill_label_char(PITEM_FINFO(pi), lbl_str);
break;
case FT_UINT8:
case FT_UINT16:
case FT_UINT24:
case FT_UINT32:
pi = proto_tree_add_uint(tree, hfindex, tvb, octet_offset, octet_length, (guint32)value);
fill_label_number(PITEM_FINFO(pi), lbl_str, FALSE);
break;
case FT_INT8:
case FT_INT16:
case FT_INT24:
case FT_INT32:
pi = proto_tree_add_int(tree, hfindex, tvb, octet_offset, octet_length, (gint32)value);
fill_label_number(PITEM_FINFO(pi), lbl_str, TRUE);
break;
case FT_UINT40:
case FT_UINT48:
case FT_UINT56:
case FT_UINT64:
pi = proto_tree_add_uint64(tree, hfindex, tvb, octet_offset, octet_length, value);
fill_label_number64(PITEM_FINFO(pi), lbl_str, FALSE);
break;
case FT_INT40:
case FT_INT48:
case FT_INT56:
case FT_INT64:
pi = proto_tree_add_int64(tree, hfindex, tvb, octet_offset, octet_length, (gint64)value);
fill_label_number64(PITEM_FINFO(pi), lbl_str, TRUE);
break;
default:
REPORT_DISSECTOR_BUG("field %s has type %d (%s) not handled in proto_tree_add_split_bits_item_ret_val()",
hf_field->abbrev,
hf_field->type,
ftype_name(hf_field->type));
return NULL;
break;
}
proto_item_set_text(pi, "%s = %s", bf_str, lbl_str);
return pi;
}
void
proto_tree_add_split_bits_crumb(proto_tree *tree, const int hfindex, tvbuff_t *tvb, const guint bit_offset,
const crumb_spec_t *crumb_spec, guint16 crumb_index)
{
header_field_info *hfinfo;
gint start = bit_offset >> 3;
gint length = ((bit_offset + crumb_spec[crumb_index].crumb_bit_length - 1) >> 3) - (bit_offset >> 3) + 1;
/* We have to duplicate this length check from proto_tree_add_text_internal in order to check for a null tree
* so that we can use the tree's memory scope in calculating the string */
if (length == -1) {
tvb_captured_length(tvb) ? tvb_ensure_captured_length_remaining(tvb, start) : 0;
} else {
tvb_ensure_bytes_exist(tvb, start, length);
}
if (!tree) return;
PROTO_REGISTRAR_GET_NTH(hfindex, hfinfo);
proto_tree_add_text_internal(tree, tvb, start, length,
"%s crumb %d of %s (decoded above)",
decode_bits_in_field(PNODE_POOL(tree), bit_offset, crumb_spec[crumb_index].crumb_bit_length,
tvb_get_bits(tvb,
bit_offset,
crumb_spec[crumb_index].crumb_bit_length,
ENC_BIG_ENDIAN),
ENC_BIG_ENDIAN),
crumb_index,
hfinfo->name);
}
proto_item *
proto_tree_add_bits_ret_val(proto_tree *tree, const int hfindex, tvbuff_t *tvb,
const guint bit_offset, const gint no_of_bits,
guint64 *return_value, const guint encoding)
{
proto_item *item;
if ((item = _proto_tree_add_bits_ret_val(tree, hfindex, tvb,
bit_offset, no_of_bits,
return_value, encoding))) {
FI_SET_FLAG(PNODE_FINFO(item), FI_BITS_OFFSET(bit_offset));
FI_SET_FLAG(PNODE_FINFO(item), FI_BITS_SIZE(no_of_bits));
}
return item;
}
static proto_item *
_proto_tree_add_bits_format_value(proto_tree *tree, const int hfindex,
tvbuff_t *tvb, const guint bit_offset,
const gint no_of_bits, void *value_ptr,
const guint encoding, gchar *value_str)
{
gint offset;
guint length;
guint8 tot_no_bits;
char *str;
guint64 value = 0;
header_field_info *hf_field;
/* We do not have to return a value, try to fake it as soon as possible */
CHECK_FOR_NULL_TREE(tree);
TRY_TO_FAKE_THIS_ITEM(tree, hfindex, hf_field);
if (hf_field->bitmask != 0) {
REPORT_DISSECTOR_BUG("Incompatible use of proto_tree_add_bits_format_value"
" with field '%s' (%s) with bitmask != 0",
hf_field->abbrev, hf_field->name);
}
if (no_of_bits < 0) {
THROW(ReportedBoundsError);
} else if (no_of_bits == 0) {
REPORT_DISSECTOR_BUG("field %s passed to proto_tree_add_bits_format_value() has a bit width of 0",
hf_field->abbrev);
}
/* Byte align offset */
offset = bit_offset>>3;
/*
* Calculate the number of octets used to hold the bits
*/
tot_no_bits = ((bit_offset&0x7) + no_of_bits);
length = tot_no_bits>>3;
/* If we are using part of the next octet, increase length by 1 */
if (tot_no_bits & 0x07)
length++;
if (no_of_bits < 65) {
value = tvb_get_bits64(tvb, bit_offset, no_of_bits, encoding);
} else {
REPORT_DISSECTOR_BUG("field %s passed to proto_tree_add_bits_format_value() has a bit width of %u > 65",
hf_field->abbrev, no_of_bits);
return NULL;
}
str = decode_bits_in_field(PNODE_POOL(tree), bit_offset, no_of_bits, value, encoding);
(void) g_strlcat(str, " = ", 256+64);
(void) g_strlcat(str, hf_field->name, 256+64);
/*
* This function does not receive an actual value but a dimensionless pointer to that value.
* For this reason, the type of the header field is examined in order to determine
* what kind of value we should read from this address.
* The caller of this function must make sure that for the specific header field type the address of
* a compatible value is provided.
*/
switch (hf_field->type) {
case FT_BOOLEAN:
return proto_tree_add_boolean_format(tree, hfindex, tvb, offset, length, *(guint32 *)value_ptr,
"%s: %s", str, value_str);
break;
case FT_CHAR:
case FT_UINT8:
case FT_UINT16:
case FT_UINT24:
case FT_UINT32:
return proto_tree_add_uint_format(tree, hfindex, tvb, offset, length, *(guint32 *)value_ptr,
"%s: %s", str, value_str);
break;
case FT_UINT40:
case FT_UINT48:
case FT_UINT56:
case FT_UINT64:
return proto_tree_add_uint64_format(tree, hfindex, tvb, offset, length, *(guint64 *)value_ptr,
"%s: %s", str, value_str);
break;
case FT_INT8:
case FT_INT16:
case FT_INT24:
case FT_INT32:
return proto_tree_add_int_format(tree, hfindex, tvb, offset, length, *(gint32 *)value_ptr,
"%s: %s", str, value_str);
break;
case FT_INT40:
case FT_INT48:
case FT_INT56:
case FT_INT64:
return proto_tree_add_int64_format(tree, hfindex, tvb, offset, length, *(gint64 *)value_ptr,
"%s: %s", str, value_str);
break;
case FT_FLOAT:
return proto_tree_add_float_format(tree, hfindex, tvb, offset, length, *(float *)value_ptr,
"%s: %s", str, value_str);
break;
default:
REPORT_DISSECTOR_BUG("field %s has type %d (%s) not handled in proto_tree_add_bits_format_value()",
hf_field->abbrev,
hf_field->type,
ftype_name(hf_field->type));
return NULL;
break;
}
}
static proto_item *
proto_tree_add_bits_format_value(proto_tree *tree, const int hfindex,
tvbuff_t *tvb, const guint bit_offset,
const gint no_of_bits, void *value_ptr,
const guint encoding, gchar *value_str)
{
proto_item *item;
if ((item = _proto_tree_add_bits_format_value(tree, hfindex,
tvb, bit_offset, no_of_bits,
value_ptr, encoding, value_str))) {
FI_SET_FLAG(PNODE_FINFO(item), FI_BITS_OFFSET(bit_offset));
FI_SET_FLAG(PNODE_FINFO(item), FI_BITS_SIZE(no_of_bits));
}
return item;
}
#define CREATE_VALUE_STRING(tree,dst,format,ap) \
va_start(ap, format); \
dst = wmem_strdup_vprintf(PNODE_POOL(tree), format, ap); \
va_end(ap);
proto_item *
proto_tree_add_uint_bits_format_value(proto_tree *tree, const int hfindex,
tvbuff_t *tvb, const guint bit_offset,
const gint no_of_bits, guint32 value,
const guint encoding,
const char *format, ...)
{
va_list ap;
gchar *dst;
header_field_info *hf_field;
CHECK_FOR_NULL_TREE(tree);
TRY_TO_FAKE_THIS_ITEM(tree, hfindex, hf_field);
switch (hf_field->type) {
case FT_UINT8:
case FT_UINT16:
case FT_UINT24:
case FT_UINT32:
break;
default:
REPORT_DISSECTOR_BUG("field %s is not of type FT_UINT8, FT_UINT16, FT_UINT24, or FT_UINT32",
hf_field->abbrev);
return NULL;
break;
}
CREATE_VALUE_STRING(tree, dst, format, ap);
return proto_tree_add_bits_format_value(tree, hfindex, tvb, bit_offset, no_of_bits, &value, encoding, dst);
}
proto_item *
proto_tree_add_uint64_bits_format_value(proto_tree *tree, const int hfindex,
tvbuff_t *tvb, const guint bit_offset,
const gint no_of_bits, guint64 value,
const guint encoding,
const char *format, ...)
{
va_list ap;
gchar *dst;
header_field_info *hf_field;
CHECK_FOR_NULL_TREE(tree);
TRY_TO_FAKE_THIS_ITEM(tree, hfindex, hf_field);
switch (hf_field->type) {
case FT_UINT40:
case FT_UINT48:
case FT_UINT56:
case FT_UINT64:
break;
default:
REPORT_DISSECTOR_BUG("field %s is not of type FT_UINT40, FT_UINT48, FT_UINT56, or FT_UINT64",
hf_field->abbrev);
return NULL;
break;
}
CREATE_VALUE_STRING(tree, dst, format, ap);
return proto_tree_add_bits_format_value(tree, hfindex, tvb, bit_offset, no_of_bits, &value, encoding, dst);
}
proto_item *
proto_tree_add_float_bits_format_value(proto_tree *tree, const int hfindex,
tvbuff_t *tvb, const guint bit_offset,
const gint no_of_bits, float value,
const guint encoding,
const char *format, ...)
{
va_list ap;
gchar *dst;
header_field_info *hf_field;
CHECK_FOR_NULL_TREE(tree);
TRY_TO_FAKE_THIS_ITEM(tree, hfindex, hf_field);
DISSECTOR_ASSERT_FIELD_TYPE(hf_field, FT_FLOAT);
CREATE_VALUE_STRING(tree, dst, format, ap);
return proto_tree_add_bits_format_value(tree, hfindex, tvb, bit_offset, no_of_bits, &value, encoding, dst);
}
proto_item *
proto_tree_add_int_bits_format_value(proto_tree *tree, const int hfindex,
tvbuff_t *tvb, const guint bit_offset,
const gint no_of_bits, gint32 value,
const guint encoding,
const char *format, ...)
{
va_list ap;
gchar *dst;
header_field_info *hf_field;
CHECK_FOR_NULL_TREE(tree);
TRY_TO_FAKE_THIS_ITEM(tree, hfindex, hf_field);
switch (hf_field->type) {
case FT_INT8:
case FT_INT16:
case FT_INT24:
case FT_INT32:
break;
default:
REPORT_DISSECTOR_BUG("field %s is not of type FT_INT8, FT_INT16, FT_INT24, or FT_INT32",
hf_field->abbrev);
return NULL;
break;
}
CREATE_VALUE_STRING(tree, dst, format, ap);
return proto_tree_add_bits_format_value(tree, hfindex, tvb, bit_offset, no_of_bits, &value, encoding, dst);
}
proto_item *
proto_tree_add_int64_bits_format_value(proto_tree *tree, const int hfindex,
tvbuff_t *tvb, const guint bit_offset,
const gint no_of_bits, gint64 value,
const guint encoding,
const char *format, ...)
{
va_list ap;
gchar *dst;
header_field_info *hf_field;
CHECK_FOR_NULL_TREE(tree);
TRY_TO_FAKE_THIS_ITEM(tree, hfindex, hf_field);
switch (hf_field->type) {
case FT_INT40:
case FT_INT48:
case FT_INT56:
case FT_INT64:
break;
default:
REPORT_DISSECTOR_BUG("field %s is not of type FT_INT40, FT_INT48, FT_INT56, or FT_INT64",
hf_field->abbrev);
return NULL;
break;
}
CREATE_VALUE_STRING(tree, dst, format, ap);
return proto_tree_add_bits_format_value(tree, hfindex, tvb, bit_offset, no_of_bits, &value, encoding, dst);
}
proto_item *
proto_tree_add_boolean_bits_format_value(proto_tree *tree, const int hfindex,
tvbuff_t *tvb, const guint bit_offset,
const gint no_of_bits, guint32 value,
const guint encoding,
const char *format, ...)
{
va_list ap;
gchar *dst;
header_field_info *hf_field;
CHECK_FOR_NULL_TREE(tree);
TRY_TO_FAKE_THIS_ITEM(tree, hfindex, hf_field);
DISSECTOR_ASSERT_FIELD_TYPE(hf_field, FT_BOOLEAN);
CREATE_VALUE_STRING(tree, dst, format, ap);
return proto_tree_add_bits_format_value(tree, hfindex, tvb, bit_offset, no_of_bits, &value, encoding, dst);
}
proto_item *
proto_tree_add_boolean_bits_format_value64(proto_tree *tree, const int hfindex,
tvbuff_t *tvb, const guint bit_offset,
const gint no_of_bits, guint64 value,
const guint encoding,
const char *format, ...)
{
va_list ap;
gchar *dst;
header_field_info *hf_field;
CHECK_FOR_NULL_TREE(tree);
TRY_TO_FAKE_THIS_ITEM(tree, hfindex, hf_field);
DISSECTOR_ASSERT_FIELD_TYPE(hf_field, FT_BOOLEAN);
CREATE_VALUE_STRING(tree, dst, format, ap);
return proto_tree_add_bits_format_value(tree, hfindex, tvb, bit_offset, no_of_bits, &value, encoding, dst);
}
proto_item *
proto_tree_add_ts_23_038_7bits_packed_item(proto_tree *tree, const int hfindex, tvbuff_t *tvb,
const guint bit_offset, const gint no_of_chars)
{
proto_item *pi;
header_field_info *hfinfo;
gint byte_length;
gint byte_offset;
gchar *string;
CHECK_FOR_NULL_TREE(tree);
TRY_TO_FAKE_THIS_ITEM(tree, hfindex, hfinfo);
DISSECTOR_ASSERT_FIELD_TYPE(hfinfo, FT_STRING);
byte_length = (((no_of_chars + 1) * 7) + (bit_offset & 0x07)) >> 3;
byte_offset = bit_offset >> 3;
string = tvb_get_ts_23_038_7bits_string_packed(PNODE_POOL(tree), tvb, bit_offset, no_of_chars);
pi = proto_tree_add_pi(tree, hfinfo, tvb, byte_offset, &byte_length);
DISSECTOR_ASSERT(byte_length >= 0);
proto_tree_set_string(PNODE_FINFO(pi), string);
return pi;
}
proto_item *
proto_tree_add_ascii_7bits_item(proto_tree *tree, const int hfindex, tvbuff_t *tvb,
const guint bit_offset, const gint no_of_chars)
{
proto_item *pi;
header_field_info *hfinfo;
gint byte_length;
gint byte_offset;
gchar *string;
CHECK_FOR_NULL_TREE(tree);
TRY_TO_FAKE_THIS_ITEM(tree, hfindex, hfinfo);
DISSECTOR_ASSERT_FIELD_TYPE(hfinfo, FT_STRING);
byte_length = (((no_of_chars + 1) * 7) + (bit_offset & 0x07)) >> 3;
byte_offset = bit_offset >> 3;
string = tvb_get_ascii_7bits_string(PNODE_POOL(tree), tvb, bit_offset, no_of_chars);
pi = proto_tree_add_pi(tree, hfinfo, tvb, byte_offset, &byte_length);
DISSECTOR_ASSERT(byte_length >= 0);
proto_tree_set_string(PNODE_FINFO(pi), string);
return pi;
}
const value_string proto_checksum_vals[] = {
{ PROTO_CHECKSUM_E_BAD, "Bad" },
{ PROTO_CHECKSUM_E_GOOD, "Good" },
{ PROTO_CHECKSUM_E_UNVERIFIED, "Unverified" },
{ PROTO_CHECKSUM_E_NOT_PRESENT, "Not present" },
{ PROTO_CHECKSUM_E_ILLEGAL, "Illegal" },
{ 0, NULL }
};
proto_item *
proto_tree_add_checksum(proto_tree *tree, tvbuff_t *tvb, const guint offset,
const int hf_checksum, const int hf_checksum_status, struct expert_field* bad_checksum_expert,
packet_info *pinfo, guint32 computed_checksum, const guint encoding, const guint flags)
{
header_field_info *hfinfo;
guint32 checksum;
guint32 len;
proto_item* ti = NULL;
proto_item* ti2;
gboolean incorrect_checksum = TRUE;
PROTO_REGISTRAR_GET_NTH(hf_checksum, hfinfo);
switch (hfinfo->type) {
case FT_UINT8:
len = 1;
break;
case FT_UINT16:
len = 2;
break;
case FT_UINT24:
len = 3;
break;
case FT_UINT32:
len = 4;
break;
default:
REPORT_DISSECTOR_BUG("field %s is not of type FT_UINT8, FT_UINT16, FT_UINT24, or FT_UINT32",
hfinfo->abbrev);
}
if (flags & PROTO_CHECKSUM_NOT_PRESENT) {
ti = proto_tree_add_uint_format_value(tree, hf_checksum, tvb, offset, len, 0, "[missing]");
proto_item_set_generated(ti);
if (hf_checksum_status != -1) {
ti2 = proto_tree_add_uint(tree, hf_checksum_status, tvb, offset, len, PROTO_CHECKSUM_E_NOT_PRESENT);
proto_item_set_generated(ti2);
}
return ti;
}
if (flags & PROTO_CHECKSUM_GENERATED) {
ti = proto_tree_add_uint(tree, hf_checksum, tvb, offset, len, computed_checksum);
proto_item_set_generated(ti);
} else {
ti = proto_tree_add_item_ret_uint(tree, hf_checksum, tvb, offset, len, encoding, &checksum);
if (flags & PROTO_CHECKSUM_VERIFY) {
if (flags & (PROTO_CHECKSUM_IN_CKSUM|PROTO_CHECKSUM_ZERO)) {
if (computed_checksum == 0) {
proto_item_append_text(ti, " [correct]");
if (hf_checksum_status != -1) {
ti2 = proto_tree_add_uint(tree, hf_checksum_status, tvb, offset, 0, PROTO_CHECKSUM_E_GOOD);
proto_item_set_generated(ti2);
}
incorrect_checksum = FALSE;
} else if (flags & PROTO_CHECKSUM_IN_CKSUM) {
computed_checksum = in_cksum_shouldbe(checksum, computed_checksum);
}
} else {
if (checksum == computed_checksum) {
proto_item_append_text(ti, " [correct]");
if (hf_checksum_status != -1) {
ti2 = proto_tree_add_uint(tree, hf_checksum_status, tvb, offset, 0, PROTO_CHECKSUM_E_GOOD);
proto_item_set_generated(ti2);
}
incorrect_checksum = FALSE;
}
}
if (incorrect_checksum) {
if (hf_checksum_status != -1) {
ti2 = proto_tree_add_uint(tree, hf_checksum_status, tvb, offset, 0, PROTO_CHECKSUM_E_BAD);
proto_item_set_generated(ti2);
}
if (flags & PROTO_CHECKSUM_ZERO) {
proto_item_append_text(ti, " [incorrect]");
if (bad_checksum_expert != NULL)
expert_add_info_format(pinfo, ti, bad_checksum_expert, "%s", expert_get_summary(bad_checksum_expert));
} else {
proto_item_append_text(ti, " incorrect, should be 0x%0*x", len*2, computed_checksum);
if (bad_checksum_expert != NULL)
expert_add_info_format(pinfo, ti, bad_checksum_expert, "%s [should be 0x%0*x]", expert_get_summary(bad_checksum_expert), len * 2, computed_checksum);
}
}
} else {
if (hf_checksum_status != -1) {
proto_item_append_text(ti, " [unverified]");
ti2 = proto_tree_add_uint(tree, hf_checksum_status, tvb, offset, 0, PROTO_CHECKSUM_E_UNVERIFIED);
proto_item_set_generated(ti2);
}
}
}
return ti;
}
proto_item *
proto_tree_add_checksum_bytes(proto_tree *tree, tvbuff_t *tvb, const guint offset,
const int hf_checksum, const int hf_checksum_status, struct expert_field* bad_checksum_expert,
packet_info *pinfo, const uint8_t *computed_checksum, size_t checksum_len, const guint flags)
{
header_field_info *hfinfo;
uint8_t *checksum = NULL;
proto_item* ti = NULL;
proto_item* ti2;
gboolean incorrect_checksum = TRUE;
PROTO_REGISTRAR_GET_NTH(hf_checksum, hfinfo);
if (hfinfo->type != FT_BYTES) {
REPORT_DISSECTOR_BUG("field %s is not of type FT_BYTES",
hfinfo->abbrev);
}
if (flags & PROTO_CHECKSUM_NOT_PRESENT) {
ti = proto_tree_add_bytes_format_value(tree, hf_checksum, tvb, offset, (gint)checksum_len, 0, "[missing]");
proto_item_set_generated(ti);
if (hf_checksum_status != -1) {
ti2 = proto_tree_add_uint(tree, hf_checksum_status, tvb, offset, (gint)checksum_len, PROTO_CHECKSUM_E_NOT_PRESENT);
proto_item_set_generated(ti2);
}
return ti;
}
if (flags & PROTO_CHECKSUM_GENERATED) {
ti = proto_tree_add_bytes(tree, hf_checksum, tvb, offset, (gint)checksum_len, computed_checksum);
proto_item_set_generated(ti);
} else {
checksum = (uint8_t*)wmem_alloc0_array(wmem_packet_scope(), uint8_t, checksum_len);
tvb_memcpy(tvb, checksum, offset, checksum_len);
ti = proto_tree_add_bytes(tree, hf_checksum, tvb, offset, (gint)checksum_len, checksum);
if (flags & PROTO_CHECKSUM_VERIFY) {
if (flags & (PROTO_CHECKSUM_IN_CKSUM|PROTO_CHECKSUM_ZERO)) {
if (computed_checksum == 0) {
proto_item_append_text(ti, " [correct]");
if (hf_checksum_status != -1) {
ti2 = proto_tree_add_uint(tree, hf_checksum_status, tvb, offset, 0, PROTO_CHECKSUM_E_GOOD);
proto_item_set_generated(ti2);
}
incorrect_checksum = FALSE;
}
} else {
if (memcmp(computed_checksum, checksum, checksum_len) == 0) {
proto_item_append_text(ti, " [correct]");
if (hf_checksum_status != -1) {
ti2 = proto_tree_add_uint(tree, hf_checksum_status, tvb, offset, 0, PROTO_CHECKSUM_E_GOOD);
proto_item_set_generated(ti2);
}
incorrect_checksum = FALSE;
}
}
if (incorrect_checksum) {
if (hf_checksum_status != -1) {
ti2 = proto_tree_add_uint(tree, hf_checksum_status, tvb, offset, 0, PROTO_CHECKSUM_E_BAD);
proto_item_set_generated(ti2);
}
if (flags & PROTO_CHECKSUM_ZERO) {
proto_item_append_text(ti, " [incorrect]");
if (bad_checksum_expert != NULL)
expert_add_info_format(pinfo, ti, bad_checksum_expert, "%s", expert_get_summary(bad_checksum_expert));
} else {
size_t computed_checksum_str_len = (2 * checksum_len * sizeof(char)) + 1;
char *computed_checksum_str = (char*)wmem_alloc0_array(wmem_packet_scope(), char, computed_checksum_str_len);
for (size_t counter = 0; counter < checksum_len; ++counter) {
snprintf(
/* On ecah iteration inserts two characters */
(char*)&computed_checksum_str[counter << 1],
computed_checksum_str_len - (counter << 1),
"%02x",
computed_checksum[counter]);
}
proto_item_append_text(ti, " incorrect, should be 0x%s", computed_checksum_str);
if (bad_checksum_expert != NULL)
expert_add_info_format(pinfo, ti, bad_checksum_expert, "%s [should be 0x%s]", expert_get_summary(bad_checksum_expert), computed_checksum_str);
}
}
} else {
if (hf_checksum_status != -1) {
proto_item_append_text(ti, " [unverified]");
ti2 = proto_tree_add_uint(tree, hf_checksum_status, tvb, offset, 0, PROTO_CHECKSUM_E_UNVERIFIED);
proto_item_set_generated(ti2);
}
}
}
return ti;
}
guchar
proto_check_field_name(const gchar *field_name)
{
return module_check_valid_name(field_name, FALSE);
}
guchar
proto_check_field_name_lower(const gchar *field_name)
{
return module_check_valid_name(field_name, TRUE);
}
gboolean
tree_expanded(int tree_type)
{
if (tree_type == -1) {
return FALSE;
}
ws_assert(tree_type >= 0 && tree_type < num_tree_types);
return tree_is_expanded[tree_type >> 5] & (1U << (tree_type & 31));
}
void
tree_expanded_set(int tree_type, gboolean value)
{
ws_assert(tree_type >= 0 && tree_type < num_tree_types);
if (value)
tree_is_expanded[tree_type >> 5] |= (1U << (tree_type & 31));
else
tree_is_expanded[tree_type >> 5] &= ~(1U << (tree_type & 31));
}
/*
* Editor modelines - https://www.wireshark.org/tools/modelines.html
*
* Local variables:
* c-basic-offset: 8
* tab-width: 8
* indent-tabs-mode: t
* End:
*
* vi: set shiftwidth=8 tabstop=8 noexpandtab:
* :indentSize=8:tabSize=8:noTabs=false:
*/
|