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

/*!
 * \author
 * Alexander Wenzel <alexander.aw.wenzel@bmw.de>
 * Markus Klein <Markus.Klein@esk.fraunhofer.de>
 * Mikko Rapeli <mikko.rapeli@bmw.de>
 *
 * \copyright Copyright © 2011-2015 BMW AG. \n
 * License MPL-2.0: Mozilla Public License version 2.0 http://mozilla.org/MPL/2.0/.
 *
 * \file dlt_common.c
 */

#include <stdio.h>
#include <stdlib.h> /* for malloc(), free() */
#include <string.h> /* for strlen(), memcmp(), memmove() */
#include <time.h>   /* for localtime(), strftime() */
#include <limits.h> /* for NAME_MAX */

#include "dlt_common.h"
#include "dlt_common_cfg.h"

#include "dlt_version.h"

#if defined (__WIN32__) || defined (_MSC_VER)
#include <winsock2.h> /* for socket(), connect(), send(), and recv() */
#else
#include <sys/socket.h> /* for socket(), connect(), send(), and recv() */
#include <syslog.h>
#include <time.h> /* for clock_gettime() */
#endif

#if defined (_MSC_VER)
#include <io.h>
#else
#include <unistd.h>     /* for read(), close() */
#include <sys/time.h>	/* for gettimeofday() */
#endif

#if defined (__MSDOS__) || defined (_MSC_VER)
#pragma warning(disable : 4996) /* Switch off C4996 warnings */
#include <windows.h>
#include <winbase.h>
#endif

static char str[DLT_COMMON_BUFFER_LENGTH];

const char dltSerialHeader[DLT_ID_SIZE] = { 'D','L','S',1 };
char dltSerialHeaderChar[DLT_ID_SIZE] = { 'D','L','S',1 };

/* internal logging parameters */
static int logging_mode = DLT_LOG_TO_CONSOLE;
static int logging_level = LOG_INFO;
static char logging_filename[NAME_MAX + 1] = "";
static FILE *logging_handle = 0;

char *message_type[] = {"log","app_trace","nw_trace","control","","","",""};
char *log_info[] = {"","fatal","error","warn","info","debug","verbose","","","","","","","","",""};
char *trace_type[] = {"","variable","func_in","func_out","state","vfb","","","","","","","","","",""};
char *nw_trace_type[] = {"","ipc","can","flexray","most","vfb","","","","","","","","","",""};
char *control_type[] = {"","request","response","time","","","","","","","","","","","",""};
static char *service_id[] = {"","set_log_level","set_trace_status","get_log_info","get_default_log_level","store_config","reset_to_factory_default",
                             "set_com_interface_status","set_com_interface_max_bandwidth","set_verbose_mode","set_message_filtering","set_timing_packets",
                             "get_local_time","use_ecu_id","use_session_id","use_timestamp","use_extended_header","set_default_log_level","set_default_trace_status",
                             "get_software_version","message_buffer_overflow"
                            };
static char *return_type[] = {"ok","not_supported","error","","","","","","no_matching_context_id"};

/* internal function definitions */
int dlt_buffer_get(DltBuffer *buf,unsigned char *data, int max_size,int delete);
int dlt_buffer_reset(DltBuffer *buf);
int dlt_buffer_increase_size(DltBuffer *buf);
int dlt_buffer_minimize_size(DltBuffer *buf);
void dlt_buffer_write_block(DltBuffer *buf,int *write, const unsigned char *data,unsigned int size);
void dlt_buffer_read_block(DltBuffer *buf,int *read,unsigned char *data,unsigned int size);	

void dlt_print_hex(uint8_t *ptr,int size)
{
    int num;

    if (ptr==0)
    {
        return;
    }

    for (num=0;num<size;num++)
    {
        if (num>0)
        {
            printf(" ");
        }

        printf("%.2x",((uint8_t*)ptr)[num]);
    }
}

int dlt_print_hex_string(char *text,int textlength,uint8_t *ptr,int size)
{
    int num;

    if ((ptr==0) || (text==0) || (textlength<=0) || (size<0))
    {
        return -1;
    }

    /* Length 3: AB_ , A is first digit of hex number, B is second digit of hex number, _ is space */
    if (textlength<(size*3))
    {
        char str[255];
        snprintf(str, 254, "String does not fit hex data (available=%d, required=%d) !\n", textlength, size*3);
        dlt_log(LOG_WARNING, str);
        return -1;
    }

    for (num=0;num<size;num++)
    {
        if (num>0)
        {
            snprintf(text,2," ");
            text++;
        }

        snprintf(text,3,"%.2x",((uint8_t*)ptr)[num]);
        text+=2; /* 2 chars */
    }

    return 0;
}

int dlt_print_mixed_string(char *text,int textlength,uint8_t *ptr,int size,int html)
{
    int required_size = 0;
    int lines, rest, i;

    if ((ptr==0) || (text==0) || (textlength<=0) || (size<0))
    {
        return -1;
    }

    /* Check maximum required size and do a length check */
    if (html==0)
    {
        required_size=(DLT_COMMON_HEX_LINELEN+(2*DLT_COMMON_HEX_CHARS+(DLT_COMMON_HEX_CHARS-1))+DLT_COMMON_CHARLEN+DLT_COMMON_HEX_CHARS+DLT_COMMON_CHARLEN) *
                      ((size/DLT_COMMON_HEX_CHARS) + 1);
        /* Example: (8 chars line number + (2*16 chars + 15 spaces) + space + 16 ascii chars + CR) *
           ((size/16) lines + extra line for the rest) */
    }
    else
    {
        required_size=(DLT_COMMON_HEX_LINELEN+(2*DLT_COMMON_HEX_CHARS+(DLT_COMMON_HEX_CHARS-1))+DLT_COMMON_CHARLEN+DLT_COMMON_HEX_CHARS+4*DLT_COMMON_CHARLEN) *
                      ((size/DLT_COMMON_HEX_CHARS) + 1);
        /* Example: (8 chars line number + (2*16 chars + 15 spaces) + space + 16 ascii chars + 4 [HTML CR: <BR>]) *
           ((size/16) lines + extra line for the rest) */
    }

    if (textlength<required_size)
    {
        snprintf(str,DLT_COMMON_BUFFER_LENGTH, "String does not fit mixed data (available=%d, required=%d) !\n", textlength, required_size);
        dlt_log(LOG_WARNING, str);
        return -1;
    }

    /* print full lines */
    for (lines=0; lines< (size / DLT_COMMON_HEX_CHARS); lines++)
    {
        /* Line number */
        snprintf(text,DLT_COMMON_HEX_LINELEN+1,"%.6x: ",lines * DLT_COMMON_HEX_CHARS);
        text+=DLT_COMMON_HEX_LINELEN; /* 'XXXXXX: ' */

        /* Hex-Output */
        /* It is not required to decrement textlength, as it was already checked, that
           there is enough space for the complete output */
        dlt_print_hex_string(text,textlength,(uint8_t*)(ptr+(lines*DLT_COMMON_HEX_CHARS)),DLT_COMMON_HEX_CHARS);
        text+=((2*DLT_COMMON_HEX_CHARS)+(DLT_COMMON_HEX_CHARS-1)); /* 32 characters + 15 spaces */

        snprintf(text,2," ");
        text+=DLT_COMMON_CHARLEN;

        /* Char-Output */
        /* It is not required to decrement textlength, as it was already checked, that
           there is enough space for the complete output */
        dlt_print_char_string(&text,textlength,(uint8_t*)(ptr+(lines*DLT_COMMON_HEX_CHARS)),DLT_COMMON_HEX_CHARS);

        if (html==0)
        {
            snprintf(text,2,"\n");
            text+=DLT_COMMON_CHARLEN;
        }
        else
        {
            snprintf(text,5,"<BR>");
            text+=(4*DLT_COMMON_CHARLEN);
        }
    }

    /* print partial line */
    rest = size % DLT_COMMON_HEX_CHARS;

    if (rest>0)
    {
        /* Line number */
        snprintf(text,9,"%.6x: ", (size / DLT_COMMON_HEX_CHARS) * DLT_COMMON_HEX_CHARS);
        text+=DLT_COMMON_HEX_LINELEN; /* 'XXXXXX: ' */

        /* Hex-Output */
        /* It is not required to decrement textlength, as it was already checked, that
           there is enough space for the complete output */
        dlt_print_hex_string(text,textlength,(uint8_t*)(ptr+ ((size / DLT_COMMON_HEX_CHARS) * DLT_COMMON_HEX_CHARS)),rest);
        text+=2*rest+(rest-1);

        for (i=0;i<(DLT_COMMON_HEX_CHARS-rest);i++)
        {
            snprintf(text,4," xx");
            text+=(3*DLT_COMMON_CHARLEN);
        }

        snprintf(text,2," ");
        text+=DLT_COMMON_CHARLEN;

        /* Char-Output */
        /* It is not required to decrement textlength, as it was already checked, that
           there is enough space for the complete output */
        dlt_print_char_string(&text,textlength,(uint8_t*)(ptr+ ((size / DLT_COMMON_HEX_CHARS) * DLT_COMMON_HEX_CHARS)),rest);
    }

    return 0;
}

int dlt_print_char_string(char **text,int textlength,uint8_t *ptr,int size)
{
    int num;

    if (text==0)
    {
        return -1;
    }

    if ((ptr==0) || (*text==0) || (textlength<=0) || (size<0))
    {
        return -1;
    }

    if (textlength< size)
    {
        char str[255];
        sprintf(str, 254, "String does not fit character data (available=%d, required=%d) !\n", textlength, size);
        dlt_log(LOG_WARNING, str);
        return -1;
    }

    for (num=0;num<size;num++)
    {
        if ( (((char*)ptr)[num]<DLT_COMMON_ASCII_CHAR_SPACE) || (((char*)ptr)[num]>DLT_COMMON_ASCII_CHAR_TILDE) )
        {
            snprintf(*text,2,".");
        }
        else
        {
            /* replace < with . */
            if (((char*)ptr)[num]!=DLT_COMMON_ASCII_CHAR_LT)
            {
                snprintf(*text,2,"%c",((char *)ptr)[num]);
            }
            else
            {
                snprintf(*text,2,".");
            }
        }
        (*text)++;
    }

    return 0;
}

void dlt_print_id(char *text,const char *id)
{
    int i, len;

    // check nullpointer
    if(!text || !id)
        return;

    if (text==0)
    {
        return;
    }

    /* Initialize text */
    for (i=0; i<DLT_ID_SIZE; i++)
    {
        text[i]='-';
    }

    text[DLT_ID_SIZE] = 0;

    len = ((strlen(id)<=DLT_ID_SIZE)?strlen(id):DLT_ID_SIZE);

    /* Check id*/
    for (i=0; i<len; i++)
        text[i] = id[i];
}

void dlt_set_id(char *id,const char *text)
{
    // check nullpointer
    if(!id || !text)
        return;

    id[0] = 0;
    id[1] = 0;
    id[2] = 0;
    id[3] = 0;

    if (text==0)
    {
        return;
    }

    if (text[0]!=0)
    {
        id[0] = text[0];
    }
    else
    {
        return;
    }

    if (text[1]!=0)
    {
        id[1] = text[1];
    }
    else
    {
        return;
    }

    if (text[2]!=0)
    {
        id[2] = text[2];
    }
    else
    {
        return;
    }

    if (text[3]!=0)
    {
        id[3] = text[3];
    }
    else
    {
        return;
    }
}

void dlt_clean_string(char *text,int length)
{
    int num;

    if (text==0)
    {
        return;
    }

    for (num=0;num<length;num++)
    {
        if (text[num]=='\r' || text[num]=='\n')
        {
            text[num] = ' ';
        }
    }
}

int dlt_filter_init(DltFilter *filter,int verbose)
{
    PRINT_FUNCTION_VERBOSE(verbose);

    if (filter==0)
    {
        return -1;
    }

    filter->counter = 0;

    return 0;
}

int dlt_filter_free(DltFilter *filter,int verbose)
{
    PRINT_FUNCTION_VERBOSE(verbose);

    if (filter==0)
    {
        return -1;
    }

    return 0;
}

int dlt_filter_load(DltFilter *filter,const char *filename,int verbose)
{
    FILE *handle;
    char str1[DLT_COMMON_BUFFER_LENGTH];
    char apid[DLT_ID_SIZE],ctid[DLT_ID_SIZE];

    PRINT_FUNCTION_VERBOSE(verbose);

    if (filter==0)
    {
        return -1;
    }

    handle = fopen(filename,"r");
    if (handle == 0)
    {
        snprintf(str,DLT_COMMON_BUFFER_LENGTH,"Filter file %s cannot be opened!\n",filename);
        dlt_log(LOG_WARNING, str);
        return -1;
    }

    /* Reset filters */
    filter->counter=0;

    while (!feof(handle))
    {
        str1[0]=0;
        if (fscanf(handle,"%s",str1)!=1)
        {
            break;
        }
        if (str1[0]==0)
        {
            break;
        }
        printf(" %s",str1);
        if (strcmp(str1,"----")==0)
        {
            dlt_set_id(apid,"");
        }
        else
        {
            dlt_set_id(apid,str1);
        }

        str1[0]=0;
        if (fscanf(handle,"%s",str1)!=1)
        {
            break;
        }
        if (str1[0]==0)
        {
            break;
        }
        printf(" %s\r\n",str1);
        if (strcmp(str1,"----")==0)
        {
            dlt_set_id(ctid,"");
        }
        else
        {
            dlt_set_id(ctid,str1);
        }

        if (filter->counter<DLT_FILTER_MAX)
        {
            dlt_filter_add(filter,apid,ctid,verbose);
        }
        else
        {
            snprintf(str,DLT_COMMON_BUFFER_LENGTH, "Maximum number (%d) of allowed filters reached, ignoring rest of filters!\n", DLT_FILTER_MAX);
            dlt_log(LOG_WARNING, str);
            return 0;
        }
    }

    fclose(handle);

    return 0;
}

int dlt_filter_save(DltFilter *filter,const char *filename,int verbose)
{
    FILE *handle;
    int num;
    char buf[DLT_COMMON_BUFFER_LENGTH];

    PRINT_FUNCTION_VERBOSE(verbose);

    if (filter==0)
    {
        return -1;
    }

    handle = fopen(filename,"w");
    if (handle == 0)
    {
        snprintf(str,DLT_COMMON_BUFFER_LENGTH,"Filter file %s cannot be opened!\n",filename);
        dlt_log(LOG_WARNING, str);
        return -1;
    }

    for (num=0;num<filter->counter;num++)
    {
        if (filter->apid[num][0]==0)
        {
            fprintf(handle,"---- ");
        }
        else
        {
            dlt_print_id(buf,filter->apid[num]);
            fprintf(handle,"%s ",buf);
        }
        if (filter->ctid[num][0]==0)
        {
            fprintf(handle,"---- ");
        }
        else
        {
            dlt_print_id(buf,filter->ctid[num]);
            fprintf(handle,"%s ",buf);
        }
    }

    fclose(handle);

    return 0;
}

int dlt_filter_find(DltFilter *filter,const char *apid,const char *ctid, int verbose)
{
    int num;

    PRINT_FUNCTION_VERBOSE(verbose);

    if ((filter==0) || (apid==0))
    {
        return -1;
    }

    for (num=0; num<filter->counter; num++)
    {
        if (memcmp(filter->apid[num],apid,DLT_ID_SIZE)==0)
        {
            /* apid matches, now check for ctid */
            if (ctid==0)
            {
                /* check if empty ctid matches */                
                //if (memcmp(filter->ctid[num],"",DLT_ID_SIZE)==0)//coverity complains here about Out-of-bounds access.
                char empty_ctid[DLT_ID_SIZE]="";
                if (memcmp(filter->ctid[num],empty_ctid,DLT_ID_SIZE)==0)
                {
                    return num;
                }
            }
            else
            {
                if (memcmp(filter->ctid[num],ctid,DLT_ID_SIZE)==0)
                {
                    return num;
                }
            }
        }
    }

    return -1; /* Not found */
}

int dlt_filter_add(DltFilter *filter,const char *apid,const char *ctid, int verbose)
{
    PRINT_FUNCTION_VERBOSE(verbose);

    if ((filter==0) || (apid==0))
    {
        return -1;
    }

    if (filter->counter >= DLT_FILTER_MAX)
    {
        sprintf(str, "Maximum number (%d) of allowed filters reached, ignoring filter!\n", DLT_FILTER_MAX);
        dlt_log(LOG_WARNING, str);
        return -1;
    }

    /* add each filter (apid, ctid) only once to filter array */
    if (dlt_filter_find(filter,apid,ctid,verbose)<0)
    {
        /* filter not found, so add it to filter array */
        if (filter->counter<DLT_FILTER_MAX)
        {
            dlt_set_id(filter->apid[filter->counter],apid);
            dlt_set_id(filter->ctid[filter->counter],(ctid?ctid:""));

            filter->counter++;

            return 0;
        }
    }

    return -1;
}

int dlt_filter_delete(DltFilter *filter,const char *apid,const char *ctid, int verbose)
{
    int j,k;
    int found=0;

    PRINT_FUNCTION_VERBOSE(verbose);

    if ((filter==0) || (apid==0))
    {
        return -1;
    }

    if (filter->counter>0)
    {
        /* Get first occurence of apid and ctid in filter array */
        for (j=0; j<filter->counter; j++)
        {
            if ((memcmp(filter->apid[j],apid,DLT_ID_SIZE)==0) &&
                (memcmp(filter->ctid[j],ctid,DLT_ID_SIZE)==0)
               )

            {
                found=1;
                break;
            }
        }

        if (found)
        {
            /* j is index */
            /* Copy from j+1 til end to j til end-1 */

            dlt_set_id(filter->apid[j],"");
            dlt_set_id(filter->ctid[j],"");

            for (k=j; k<(filter->counter-1); k++)
            {
                dlt_set_id(filter->apid[k],filter->apid[k+1]);
                dlt_set_id(filter->ctid[k],filter->ctid[k+1]);
            }

            filter->counter--;
            return 0;
        }
    }

    return -1;
}

int dlt_message_init(DltMessage *msg,int verbose)
{
    PRINT_FUNCTION_VERBOSE(verbose);

    if (msg==0)
    {
        return -1;
    }

    /* initalise structure parameters */
    msg->headersize = 0;
    msg->datasize = 0;

    msg->databuffer = 0;
    msg->databuffersize = 0;

    msg->storageheader = 0;
    msg->standardheader = 0;
    msg->extendedheader = 0;

    msg->found_serialheader = 0;

    return 0;
}

int dlt_message_free(DltMessage *msg,int verbose)
{
    PRINT_FUNCTION_VERBOSE(verbose);

    if (msg==0)
    {
        return -1;
    }
    /* delete databuffer if exists */
    if (msg->databuffer)
    {
        free(msg->databuffer);
        msg->databuffer = 0;
        msg->databuffersize = 0;
    }

    return 0;
}

int dlt_message_header(DltMessage *msg,char *text,int textlength,int verbose)
{
    return dlt_message_header_flags(msg,text,textlength,DLT_HEADER_SHOW_ALL,verbose);
}

int dlt_message_header_flags(DltMessage *msg,char *text,int textlength,int flags, int verbose)
{
    struct tm * timeinfo;
    char buffer [DLT_COMMON_BUFFER_LENGTH];

    PRINT_FUNCTION_VERBOSE(verbose);

    if ((msg==0) || (text==0) || (textlength<=0))
    {
        return -1;
    }

    if ((flags<DLT_HEADER_SHOW_NONE) || (flags>DLT_HEADER_SHOW_ALL))
    {
        return -1;
    }

    text[0] = 0;

    if ((flags & DLT_HEADER_SHOW_TIME) == DLT_HEADER_SHOW_TIME)
    {
        /* print received time */
    	time_t tt = msg->storageheader->seconds;
        timeinfo = localtime (&tt);

        if (timeinfo!=0)
        {
            strftime (buffer,sizeof(buffer),"%Y/%m/%d %H:%M:%S",timeinfo);
            snprintf(text,textlength,"%s.%.6d ",buffer,msg->storageheader->microseconds);
        }
    }

    if ((flags & DLT_HEADER_SHOW_TMSTP) == DLT_HEADER_SHOW_TMSTP)
    {
        /* print timestamp if available */
        if ( DLT_IS_HTYP_WTMS(msg->standardheader->htyp) )
        {
            snprintf(text+strlen(text),textlength-strlen(text),"%10u ",msg->headerextra.tmsp);
        }
        else
        {
            snprintf(text+strlen(text),textlength-strlen(text),"---------- ");
        }
    }

    if ((flags & DLT_HEADER_SHOW_MSGCNT) == DLT_HEADER_SHOW_MSGCNT)
    {
        /* print message counter */
        snprintf(text+strlen(text),textlength-strlen(text),"%.3d ",msg->standardheader->mcnt);
    }

    if ((flags & DLT_HEADER_SHOW_ECUID) == DLT_HEADER_SHOW_ECUID)
    {
        /* print ecu id, use header extra if available, else storage header value */
        if ( DLT_IS_HTYP_WEID(msg->standardheader->htyp) )
        {
            dlt_print_id(text+strlen(text),msg->headerextra.ecu);
        }
        else
        {
            dlt_print_id(text+strlen(text),msg->storageheader->ecu);
        }
    }

    /* print app id and context id if extended header available, else '----' */#
    if ((flags & DLT_HEADER_SHOW_APID) == DLT_HEADER_SHOW_APID)
    {
        snprintf(text+strlen(text),textlength-strlen(text)," ");
        if ((DLT_IS_HTYP_UEH(msg->standardheader->htyp)) && (msg->extendedheader->apid[0]!=0))
        {
            dlt_print_id(text+strlen(text),msg->extendedheader->apid);
        }
        else
        {
            snprintf(text+strlen(text),textlength-strlen(text),"----");
        }

        snprintf(text+strlen(text),textlength-strlen(text)," ");
    }

    if ((flags & DLT_HEADER_SHOW_CTID) == DLT_HEADER_SHOW_CTID)
    {
        if ((DLT_IS_HTYP_UEH(msg->standardheader->htyp)) && (msg->extendedheader->ctid[0]!=0))
        {
            dlt_print_id(text+strlen(text),msg->extendedheader->ctid);
        }
        else
        {
            snprintf(text+strlen(text),textlength-strlen(text),"----");
        }

        snprintf(text+strlen(text),textlength-strlen(text)," ");
    }

    /* print info about message type and length */
    if (DLT_IS_HTYP_UEH(msg->standardheader->htyp))
    {
        if ((flags & DLT_HEADER_SHOW_MSGTYPE) == DLT_HEADER_SHOW_MSGTYPE)
        {
            snprintf(text+strlen(text),textlength-strlen(text),"%s",message_type[DLT_GET_MSIN_MSTP(msg->extendedheader->msin)]);
            snprintf(text+strlen(text),textlength-strlen(text)," ");
        }

        if ((flags & DLT_HEADER_SHOW_MSGSUBTYPE) == DLT_HEADER_SHOW_MSGSUBTYPE)
        {
            if ((DLT_GET_MSIN_MSTP(msg->extendedheader->msin))==DLT_TYPE_LOG)
            {
                snprintf(text+strlen(text),textlength-strlen(text),"%s",log_info[DLT_GET_MSIN_MTIN(msg->extendedheader->msin)]);
            }

            if ((DLT_GET_MSIN_MSTP(msg->extendedheader->msin))==DLT_TYPE_APP_TRACE)
            {
                snprintf(text+strlen(text),textlength-strlen(text),"%s",trace_type[DLT_GET_MSIN_MTIN(msg->extendedheader->msin)]);
            }

            if ((DLT_GET_MSIN_MSTP(msg->extendedheader->msin))==DLT_TYPE_NW_TRACE)
            {
                snprintf(text+strlen(text),textlength-strlen(text),"%s",nw_trace_type[DLT_GET_MSIN_MTIN(msg->extendedheader->msin)]);
            }

            if ((DLT_GET_MSIN_MSTP(msg->extendedheader->msin))==DLT_TYPE_CONTROL)
            {
                snprintf(text+strlen(text),textlength-strlen(text),"%s",control_type[DLT_GET_MSIN_MTIN(msg->extendedheader->msin)]);
            }

            snprintf(text+strlen(text),textlength-strlen(text)," ");
        }

        if ((flags & DLT_HEADER_SHOW_VNVSTATUS) == DLT_HEADER_SHOW_VNVSTATUS)
        {
            /* print verbose status pf message */
            if (DLT_IS_MSIN_VERB(msg->extendedheader->msin))
            {
                snprintf(text+strlen(text),textlength-strlen(text),"V");
            }
            else
            {
                snprintf(text+strlen(text),textlength-strlen(text),"N");
            }

            snprintf(text+strlen(text),textlength-strlen(text)," ");
        }

        if ((flags & DLT_HEADER_SHOW_NOARG) == DLT_HEADER_SHOW_NOARG)
        {
            /* print number of arguments */
            snprintf(text+strlen(text),textlength-strlen(text),"%d", msg->extendedheader->noar);
        }

    }
    else
    {
        if ((flags & DLT_HEADER_SHOW_MSGTYPE) == DLT_HEADER_SHOW_MSGTYPE)
        {
            snprintf(text+strlen(text),textlength-strlen(text),"--- ");
        }

        if ((flags & DLT_HEADER_SHOW_MSGSUBTYPE) == DLT_HEADER_SHOW_MSGSUBTYPE)
        {
            snprintf(text+strlen(text),textlength-strlen(text),"--- ");
        }

        if ((flags & DLT_HEADER_SHOW_VNVSTATUS) == DLT_HEADER_SHOW_VNVSTATUS)
        {
            snprintf(text+strlen(text),textlength-strlen(text),"N ");
        }

        if ((flags & DLT_HEADER_SHOW_NOARG) == DLT_HEADER_SHOW_NOARG)
        {
            snprintf(text+strlen(text),textlength-strlen(text),"-");
        }
    }

    return 0;
}

int dlt_message_payload(DltMessage *msg,char *text,int textlength,int type,int verbose)
{
    uint32_t id=0,id_tmp=0;
    uint8_t retval=0;

    uint8_t *ptr;
    int32_t datalength;

    /* Pointer to ptr and datalength */
    uint8_t **pptr;
    int32_t *pdatalength;

    int ret=0;

    int num;
    uint32_t type_info=0,type_info_tmp=0;

    PRINT_FUNCTION_VERBOSE(verbose);

    if ((msg==0) || (text==0))
    {
        return -1;
    }

    if (textlength<=0)
    {
        dlt_log(LOG_WARNING, "String does not fit binary data!\n");
        return -1;
    }

    /* start with empty string */
    text[0] = 0;

    /* print payload only as hex */
    if (type==DLT_OUTPUT_HEX)
    {
        return dlt_print_hex_string(text,textlength,msg->databuffer,msg->datasize);
    }

    /* print payload as mixed */
    if (type==DLT_OUTPUT_MIXED_FOR_PLAIN)
    {
        return dlt_print_mixed_string(text,textlength,msg->databuffer,msg->datasize,0);
    }

    if (type==DLT_OUTPUT_MIXED_FOR_HTML)
    {
        return dlt_print_mixed_string(text,textlength,msg->databuffer,msg->datasize,1);
    }

    ptr = msg->databuffer;
    datalength = msg->datasize;

    /* Pointer to ptr and datalength */
    pptr = &ptr;
    pdatalength = &datalength;

    /* non-verbose mode */

    /* print payload as hex */
    if (DLT_MSG_IS_NONVERBOSE(msg))
    {

        DLT_MSG_READ_VALUE(id_tmp,ptr,datalength,uint32_t);
        id=DLT_ENDIAN_GET_32(msg->standardheader->htyp, id_tmp);

        if (textlength<((datalength*3)+20))
        {
            char str[255];
            sprintf(str, 254, "String does not fit binary data (available=%d, required=%d) !\n", textlength, (datalength*3)+20);
            dlt_log(LOG_WARNING, str);
            return -1;
        }

        /* process message id / service id */
        if (DLT_MSG_IS_CONTROL(msg))
        {
            if (id > 0 && id < DLT_SERVICE_ID_LAST_ENTRY)
            {
                snprintf(text+strlen(text),textlength-strlen(text),"%s",service_id[id]); /* service id */
            }
            else
            {
                if (!(DLT_MSG_IS_CONTROL_TIME(msg)))
                {
                    snprintf(text+strlen(text),textlength-strlen(text),"service(%u)",id); /* service id */
                }
            }

            if (datalength>0)
            {
                snprintf(text+strlen(text),textlength-strlen(text),", ");
            }
        }
        else
        {
            snprintf(text+strlen(text),textlength-strlen(text),"%u, ",id); /* message id */
        }

        /* process return value */
        if (DLT_MSG_IS_CONTROL_RESPONSE(msg))
        {
            if (datalength>0)
            {
                DLT_MSG_READ_VALUE(retval,ptr,datalength,uint8_t); /* No endian conversion necessary */
                if ( (retval<3) || (retval==8))
                {
                    snprintf(text+strlen(text),textlength-strlen(text),"%s",return_type[retval]);
                }
                else
                {
                    snprintf(text+strlen(text),textlength-strlen(text),"%.2x",retval);
                }

                if (datalength>=1)
                {
                    snprintf(text+strlen(text),textlength-strlen(text),", ");
                }
            }
        }

        if (type==DLT_OUTPUT_ASCII_LIMITED)
        {
            ret=dlt_print_hex_string(text+strlen(text),textlength-strlen(text),ptr,
                                     (datalength>DLT_COMMON_ASCII_LIMIT_MAX_CHARS?DLT_COMMON_ASCII_LIMIT_MAX_CHARS:datalength));
            if ((datalength>DLT_COMMON_ASCII_LIMIT_MAX_CHARS) &&
                    ((textlength-strlen(text))>4))
            {
                snprintf(text+strlen(text),textlength-strlen(text)," ...");
            }
        }
        else
        {
            ret=dlt_print_hex_string(text+strlen(text),textlength-strlen(text),ptr,datalength);
        }

        return ret;
    }

    /* At this point, it is ensured that a extended header is available */

    /* verbose mode */
    type_info=0;
    type_info_tmp=0;

    for (num=0;num<(int)(msg->extendedheader->noar);num++)
    {
        if (num!=0)
        {
            snprintf(text+strlen(text),textlength-strlen(text)," ");
        }

        /* first read the type info of the argument */
        DLT_MSG_READ_VALUE(type_info_tmp,ptr,datalength,uint32_t);
        type_info=DLT_ENDIAN_GET_32(msg->standardheader->htyp, type_info_tmp);

        /* print out argument */
        if (dlt_message_argument_print(msg, type_info, pptr, pdatalength, text, textlength, -1, 0)==-1)
        {
            return -1;
        }
    }

    return 0;
}

int dlt_message_filter_check(DltMessage *msg,DltFilter *filter,int verbose)
{
    /* check the filters if message is used */
    int num;
    int found = 0;

    PRINT_FUNCTION_VERBOSE(verbose);

    if ((msg==0) || (filter==0))
    {
        return -1;
    }

    if ((filter->counter==0) || (!(DLT_IS_HTYP_UEH(msg->standardheader->htyp))))
    {
        /* no filter is set, or no extended header is available, so do as filter is matching */
        return 1;
    }

    for (num=0;num<filter->counter;num++)
    {
        /* check each filter if it matches */
        if ((DLT_IS_HTYP_UEH(msg->standardheader->htyp)) &&
                (filter->apid[num][0]==0 || memcmp(filter->apid[num],msg->extendedheader->apid,DLT_ID_SIZE)==0) &&
                (filter->ctid[num][0]==0 || memcmp(filter->ctid[num],msg->extendedheader->ctid,DLT_ID_SIZE)==0) )
        {
            found = 1;
            break;
        }
    }

    return found;
}

int dlt_message_read(DltMessage *msg,uint8_t *buffer,unsigned int length,int resync, int verbose)
{
    int extra_size = 0;

    PRINT_FUNCTION_VERBOSE(verbose);

    if ((msg==0) || (buffer==0) || (length<=0))
    {
        return DLT_MESSAGE_ERROR_UNKNOWN;
    }

    /* initialize resync_offset */
    msg->resync_offset=0;

    /* check if message contains serial header, smaller than standard header */
    if (length<sizeof(dltSerialHeader))
    {
        /* dlt_log(LOG_ERR, "Length smaller than serial header!\n"); */
        return DLT_MESSAGE_ERROR_SIZE;
    }

    if (memcmp(buffer,dltSerialHeader,sizeof(dltSerialHeader)) == 0)
    {
        /* serial header found */
        msg->found_serialheader = 1;
        buffer += sizeof(dltSerialHeader);
        length -= sizeof(dltSerialHeader);
    }
    else
    {
        /* serial header not found */
        msg->found_serialheader = 0;
        if (resync)
        {
            /* resync if necessary */
            msg->resync_offset=0;

            do
            {
                if (memcmp(buffer+msg->resync_offset,dltSerialHeader,sizeof(dltSerialHeader)) == 0)
                {
                    /* serial header found */
                    msg->found_serialheader = 1;
                    buffer += sizeof(dltSerialHeader);
                    length -= sizeof(dltSerialHeader);
                    break;
                }

                msg->resync_offset++;
            }
            while ((sizeof(dltSerialHeader)+msg->resync_offset)<=length);

            /* Set new start offset */
            if (msg->resync_offset>0)
            {
                /* Resyncing connection */
                buffer+=msg->resync_offset;
                length-=msg->resync_offset;
            }
        }
    }

    /* check that standard header fits buffer */
    if (length<sizeof(DltStandardHeader))
    {
        /* dlt_log(LOG_ERR, "Length smaller than standard header!\n"); */
        return DLT_MESSAGE_ERROR_SIZE;
    }
    memcpy(msg->headerbuffer+sizeof(DltStorageHeader),buffer,sizeof(DltStandardHeader));

    /* set ptrs to structures */
    msg->storageheader = (DltStorageHeader*) msg->headerbuffer;
    msg->standardheader = (DltStandardHeader*) (msg->headerbuffer + sizeof(DltStorageHeader));

    /* calculate complete size of headers */
    extra_size = DLT_STANDARD_HEADER_EXTRA_SIZE(msg->standardheader->htyp)+(DLT_IS_HTYP_UEH(msg->standardheader->htyp) ? sizeof(DltExtendedHeader) : 0);
    msg->headersize = sizeof(DltStorageHeader) + sizeof(DltStandardHeader) + extra_size;
    msg->datasize =  DLT_BETOH_16(msg->standardheader->len) - (msg->headersize - sizeof(DltStorageHeader));

    if (verbose)
    {
        snprintf(str,DLT_COMMON_BUFFER_LENGTH,"BufferLength=%d, HeaderSize=%d, DataSize=%d\n",length, msg->headersize, msg->datasize);
        dlt_log(LOG_DEBUG, str);
    }

    /* check data size */
    if (msg->datasize < 0)
    {
        snprintf(str,DLT_COMMON_BUFFER_LENGTH,"Plausibility check failed. Complete message size too short (%d)!\n",msg->datasize);
        dlt_log(LOG_WARNING, str);
        return DLT_MESSAGE_ERROR_CONTENT;
    }

    /* load standard header extra parameters and Extended header if used */
    if (extra_size>0)
    {
        if (length  < (msg->headersize - sizeof(DltStorageHeader)))
        {
            return DLT_MESSAGE_ERROR_SIZE;
        }

        memcpy(msg->headerbuffer+sizeof(DltStorageHeader)+sizeof(DltStandardHeader),buffer+sizeof(DltStandardHeader),extra_size);

        /* set extended header ptr and get standard header extra parameters */
        if (DLT_IS_HTYP_UEH(msg->standardheader->htyp))
        {
            msg->extendedheader = (DltExtendedHeader*) (msg->headerbuffer + sizeof(DltStorageHeader) + sizeof(DltStandardHeader) +
                                  DLT_STANDARD_HEADER_EXTRA_SIZE(msg->standardheader->htyp));
        }
        else
        {
            msg->extendedheader = 0;
        }

        dlt_message_get_extraparameters(msg,verbose);
    }

    /* check if payload fits length */
    if (length  < (msg->headersize - sizeof(DltStorageHeader) + msg->datasize))
    {
        /* dlt_log(LOG_ERR,"length does not fit!\n"); */
        return DLT_MESSAGE_ERROR_SIZE;
    }

    /* free last used memory for buffer */
    if (msg->databuffer)
    {
    	if (msg->datasize>msg->databuffersize){
    		free(msg->databuffer);
    		msg->databuffer=(uint8_t *)malloc(msg->datasize);
    		msg->databuffersize = msg->datasize;
    	}
    }else{
    	/* get new memory for buffer */
    	msg->databuffer = (uint8_t *)malloc(msg->datasize);
    	msg->databuffersize = msg->datasize;
    }
    if (msg->databuffer == 0)
    {
        snprintf(str,DLT_COMMON_BUFFER_LENGTH,"Cannot allocate memory for payload buffer of size %d!\n",msg->datasize);
        dlt_log(LOG_WARNING, str);
        return DLT_MESSAGE_ERROR_UNKNOWN;
    }

    /* load payload data from buffer */
    memcpy(msg->databuffer,buffer+(msg->headersize-sizeof(DltStorageHeader)),msg->datasize);

    return DLT_MESSAGE_ERROR_OK;
}

int dlt_message_get_extraparameters(DltMessage *msg,int verbose)
{
    PRINT_FUNCTION_VERBOSE(verbose);

    if (msg==0)
    {
        return -1;
    }

    if (DLT_IS_HTYP_WEID(msg->standardheader->htyp))
    {
        memcpy(msg->headerextra.ecu,msg->headerbuffer + sizeof(DltStorageHeader) + sizeof(DltStandardHeader),DLT_ID_SIZE);
    }

    if (DLT_IS_HTYP_WSID(msg->standardheader->htyp))
    {
        memcpy(&(msg->headerextra.seid),msg->headerbuffer + sizeof(DltStorageHeader) + sizeof(DltStandardHeader)
               + (DLT_IS_HTYP_WEID(msg->standardheader->htyp) ? DLT_SIZE_WEID : 0), DLT_SIZE_WSID);
        msg->headerextra.seid = DLT_BETOH_32(msg->headerextra.seid);
    }

    if (DLT_IS_HTYP_WTMS(msg->standardheader->htyp))
    {
        memcpy(&(msg->headerextra.tmsp),msg->headerbuffer + sizeof(DltStorageHeader) + sizeof(DltStandardHeader)
               + (DLT_IS_HTYP_WEID(msg->standardheader->htyp) ? DLT_SIZE_WEID : 0)
               + (DLT_IS_HTYP_WSID(msg->standardheader->htyp) ? DLT_SIZE_WSID : 0),DLT_SIZE_WTMS);
        msg->headerextra.tmsp = DLT_BETOH_32(msg->headerextra.tmsp);
    }

    return 0;
}

int dlt_message_set_extraparameters(DltMessage *msg,int verbose)
{
    PRINT_FUNCTION_VERBOSE(verbose);

    if (msg==0)
    {
        return -1;
    }

    if (DLT_IS_HTYP_WEID(msg->standardheader->htyp))
    {
        memcpy(msg->headerbuffer + sizeof(DltStorageHeader) + sizeof(DltStandardHeader),msg->headerextra.ecu,DLT_ID_SIZE);
    }

    if (DLT_IS_HTYP_WSID(msg->standardheader->htyp))
    {
        msg->headerextra.seid = DLT_HTOBE_32(msg->headerextra.seid);
        memcpy(msg->headerbuffer + sizeof(DltStorageHeader) + sizeof(DltStandardHeader)
               + (DLT_IS_HTYP_WEID(msg->standardheader->htyp) ? DLT_SIZE_WEID : 0), &(msg->headerextra.seid),DLT_SIZE_WSID);
    }

    if (DLT_IS_HTYP_WTMS(msg->standardheader->htyp))
    {
        msg->headerextra.tmsp = DLT_HTOBE_32(msg->headerextra.tmsp);
        memcpy(msg->headerbuffer + sizeof(DltStorageHeader) + sizeof(DltStandardHeader)
               + (DLT_IS_HTYP_WEID(msg->standardheader->htyp) ? DLT_SIZE_WEID : 0)
               + (DLT_IS_HTYP_WSID(msg->standardheader->htyp) ? DLT_SIZE_WSID : 0), &(msg->headerextra.tmsp),DLT_SIZE_WTMS);
    }

    return 0;
}

int dlt_file_init(DltFile *file,int verbose)
{
    PRINT_FUNCTION_VERBOSE(verbose);

    if (file==0)
    {
        return -1;
    }

    /* initalise structure parameters */
    file->handle = 0;
    file->counter = 0;
    file->counter_total = 0;
    file->index = 0;

    file->filter = 0;
    file->filter_counter = 0;
    file->file_position = 0;

    file->position = 0;

    file->error_messages = 0;

    return dlt_message_init(&(file->msg),verbose);
}

int dlt_file_set_filter(DltFile *file,DltFilter *filter,int verbose)
{
    PRINT_FUNCTION_VERBOSE(verbose);

    if (file==0)
    {
        return -1;
    }

    /* set filter */
    file->filter = filter;

    return 0;
}

int dlt_file_read_header(DltFile *file,int verbose)
{
    PRINT_FUNCTION_VERBOSE(verbose);

    if (file==0)
    {
        return -1;
    }

    /* load header from file */
    if (fread(file->msg.headerbuffer,sizeof(DltStorageHeader)+sizeof(DltStandardHeader),1,file->handle)!=1)
    {
        if (!feof(file->handle))
        {
            dlt_log(LOG_WARNING, "Cannot read header from file!\n");
        }
        return -1;
    }

    /* set ptrs to structures */
    file->msg.storageheader = (DltStorageHeader*) file->msg.headerbuffer;
    file->msg.standardheader = (DltStandardHeader*) (file->msg.headerbuffer + sizeof(DltStorageHeader));

    /* check id of storage header */
    if (dlt_check_storageheader(file->msg.storageheader)==0)
    {
        dlt_log(LOG_WARNING, "DLT storage header pattern not found!\n");
        return -1;
    }

    /* calculate complete size of headers */
    file->msg.headersize = sizeof(DltStorageHeader) + sizeof(DltStandardHeader) +
                           DLT_STANDARD_HEADER_EXTRA_SIZE(file->msg.standardheader->htyp) + (DLT_IS_HTYP_UEH(file->msg.standardheader->htyp) ? sizeof(DltExtendedHeader) : 0);
    file->msg.datasize = DLT_BETOH_16(file->msg.standardheader->len) + sizeof(DltStorageHeader) - file->msg.headersize;
    if (verbose)
    {
        snprintf(str,DLT_COMMON_BUFFER_LENGTH,"HeaderSize=%d, DataSize=%d\n",file->msg.headersize, file->msg.datasize);
        dlt_log(LOG_DEBUG, str);
    }

    /* check data size */
    if (file->msg.datasize < 0)
    {
        snprintf(str,DLT_COMMON_BUFFER_LENGTH,"Plausibility check failed. Complete message size too short! (%d)\n", file->msg.datasize);
        dlt_log(LOG_WARNING, str);
        return -1;
    }

    return 0;
}

int dlt_file_read_header_raw(DltFile *file,int resync,int verbose)
{
    char dltSerialHeaderBuffer[DLT_ID_SIZE];

    PRINT_FUNCTION_VERBOSE(verbose);

    if (file==0)
    {
        return -1;
    }

    /* check if serial header exists, ignore if found */
    if (fread(dltSerialHeaderBuffer,sizeof(dltSerialHeaderBuffer),1,file->handle)!=1)
    {
        /* cannot read serial header, not enough data available in file */
        if (!feof(file->handle))
        {
            dlt_log(LOG_WARNING, "Cannot read header from file!\n");
        }
        return -1;
    }
    if (memcmp(dltSerialHeaderBuffer,dltSerialHeader,sizeof(dltSerialHeader)) == 0)
    {
        /* serial header found */
        /* nothing to do continue reading */

    }
    else
    {
        /* serial header not found */
        if(resync)
        {
            /* increase error counter */
            file->error_messages++;

            /* resync to serial header */
            do
            {
                memmove(dltSerialHeaderBuffer,dltSerialHeaderBuffer+1,sizeof(dltSerialHeader)-1);
                if (fread(dltSerialHeaderBuffer+3,1,1,file->handle)!=1)
                {
                    /* cannot read any data, perhaps end of file reached */
                    return -1;
                }
                if (memcmp(dltSerialHeaderBuffer,dltSerialHeader,sizeof(dltSerialHeader)) == 0)
                {
                    /* serial header synchronised */
                    break;
                }
            } while(1);
        }
        else
        {
            /* go back to last file position */
            if (0 != fseek(file->handle,file->file_position,SEEK_SET))
                return -1;
        }
    }

    /* load header from file */
    if (fread(file->msg.headerbuffer+sizeof(DltStorageHeader),sizeof(DltStandardHeader),1,file->handle)!=1)
    {
        if (!feof(file->handle))
        {
            dlt_log(LOG_WARNING, "Cannot read header from file!\n");
        }
        return -1;
    }

    /* set ptrs to structures */
    file->msg.storageheader = (DltStorageHeader*) file->msg.headerbuffer; // this points now to a empty storage header (filled with '0')
    file->msg.standardheader = (DltStandardHeader*) (file->msg.headerbuffer + sizeof(DltStorageHeader));

    /* Skip storage header field, fill this field with '0' */
    memset(file->msg.storageheader,0,sizeof(DltStorageHeader));

    /* Set storage header */
    dlt_set_storageheader(file->msg.storageheader,DLT_COMMON_DUMMY_ECUID);

    /* no check for storage header id*/

    /* calculate complete size of headers */
    file->msg.headersize = sizeof(DltStorageHeader) + sizeof(DltStandardHeader) +
                           DLT_STANDARD_HEADER_EXTRA_SIZE(file->msg.standardheader->htyp) + (DLT_IS_HTYP_UEH(file->msg.standardheader->htyp) ? sizeof(DltExtendedHeader) : 0);
    file->msg.datasize = DLT_BETOH_16(file->msg.standardheader->len) + sizeof(DltStorageHeader) - file->msg.headersize;
    if (verbose)
    {
        snprintf(str,DLT_COMMON_BUFFER_LENGTH,"HeaderSize=%d, DataSize=%d\n",file->msg.headersize, file->msg.datasize);
        dlt_log(LOG_DEBUG, str);
    }

    /* check data size */
    if (file->msg.datasize < 0)
    {
        snprintf(str,DLT_COMMON_BUFFER_LENGTH,"Plausibility check failed. Complete message size too short! (%d)\n", file->msg.datasize);
        dlt_log(LOG_WARNING, str);
        return -1;
    }

    return 0;
}

int dlt_file_read_header_extended(DltFile *file, int verbose)
{
    PRINT_FUNCTION_VERBOSE(verbose);

    if (file==0)
    {
        return -1;
    }

    /* load standard header extra parameters if used */
    if (DLT_STANDARD_HEADER_EXTRA_SIZE(file->msg.standardheader->htyp))
    {
        if (fread(file->msg.headerbuffer+sizeof(DltStorageHeader)+sizeof(DltStandardHeader),
                  DLT_STANDARD_HEADER_EXTRA_SIZE(file->msg.standardheader->htyp),
                  1,file->handle)!=1)
        {
            dlt_log(LOG_WARNING, "Cannot read standard header extra parameters from file!\n");
            return -1;
        }

        dlt_message_get_extraparameters(&(file->msg),verbose);
    }

    /* load Extended header if used */
    if (DLT_IS_HTYP_UEH(file->msg.standardheader->htyp)==0)
    {
        /* there is nothing to be loaded */
        return 0;
    }

    if (fread(file->msg.headerbuffer+sizeof(DltStorageHeader)+sizeof(DltStandardHeader)+DLT_STANDARD_HEADER_EXTRA_SIZE(file->msg.standardheader->htyp),
              (DLT_IS_HTYP_UEH(file->msg.standardheader->htyp) ? sizeof(DltExtendedHeader) : 0),
              1,file->handle)!=1)
    {
        dlt_log(LOG_WARNING, "Cannot read extended header from file!\n");
        return -1;
    }

    /* set extended header ptr */
    if (DLT_IS_HTYP_UEH(file->msg.standardheader->htyp))
    {
        file->msg.extendedheader = (DltExtendedHeader*) (file->msg.headerbuffer + sizeof(DltStorageHeader) + sizeof(DltStandardHeader) +
                                   DLT_STANDARD_HEADER_EXTRA_SIZE(file->msg.standardheader->htyp));
    }
    else
    {
        file->msg.extendedheader = 0;
    }

    return 0;
}

int dlt_file_read_data(DltFile *file, int verbose)
{
    PRINT_FUNCTION_VERBOSE(verbose);

    if (file==0)
    {
        return -1;
    }

    /* free last used memory for buffer */
    if (file->msg.databuffer && (file->msg.databuffersize < file->msg.datasize))
    {
        free(file->msg.databuffer);
        file->msg.databuffer=0;
    }

    if (file->msg.databuffer == 0){
    	/* get new memory for buffer */
    	file->msg.databuffer = (uint8_t *)malloc(file->msg.datasize);
    	file->msg.databuffersize = file->msg.datasize;
    }

    if (file->msg.databuffer == 0)
    {
        snprintf(str,DLT_COMMON_BUFFER_LENGTH,"Cannot allocate memory for payload buffer of size %d!\n",file->msg.datasize);
        dlt_log(LOG_WARNING, str);
        return -1;
    }

    /* load payload data from file */
    if (fread(file->msg.databuffer,file->msg.datasize,1,file->handle)!=1)
    {
        if (file->msg.datasize!=0)
        {
            snprintf(str,DLT_COMMON_BUFFER_LENGTH,"Cannot read payload data from file of size %d!\n",file->msg.datasize);
            dlt_log(LOG_WARNING, str);
            return -1;
        }
    }

    return 0;
}

int dlt_file_open(DltFile *file,const char *filename,int verbose)
{
    PRINT_FUNCTION_VERBOSE(verbose);

    if(!file || !filename)
        return -1;

    /* reset counters */
    file->counter = 0;
    file->counter_total = 0;
    file->position = 0;
    file->file_position = 0;
    file->file_length = 0;
    file->error_messages = 0;

    if (file->handle)
    {
        fclose(file->handle);
    }

    /* open dlt file */
    file->handle = fopen(filename,"rb");
    if (file->handle == 0)
    {
        snprintf(str, DLT_COMMON_BUFFER_LENGTH - 1 ,"File %s cannot be opened!\n",filename);
        dlt_log(LOG_WARNING, str);
        return -1;
    }

    if (0 != fseek(file->handle,0,SEEK_END))
    {
        snprintf(str,DLT_COMMON_BUFFER_LENGTH,"dlt_file_open: Seek failed to 0,SEEK_END");
        dlt_log(LOG_WARNING, str);
        return -1;
    }
    file->file_length = ftell(file->handle);

    if (0 != fseek(file->handle,0,SEEK_SET))
    {
        snprintf(str,DLT_COMMON_BUFFER_LENGTH,"dlt_file_open: Seek failed to 0,SEEK_SET");
        dlt_log(LOG_WARNING, str);
        return -1;
    }

    if (verbose)
    {
        /* print file length */
        snprintf(str,DLT_COMMON_BUFFER_LENGTH,"File is %lu bytes long\n",file->file_length);
        dlt_log(LOG_DEBUG, str);
    }
    return 0;
}

int dlt_file_read(DltFile *file,int verbose)
{
    long *ptr;
    int found = 0;

    if (verbose)
    {
        snprintf(str,DLT_COMMON_BUFFER_LENGTH,"%s: Message %d:\n",__func__, file->counter_total);
        dlt_log(LOG_DEBUG, str);
    }

    if (file==0)
    {
        return -1;
    }

    /* allocate new memory for index if number of messages exceeds a multiple of DLT_COMMON_INDEX_ALLOC (e.g.: 1000) */
    if (file->counter % DLT_COMMON_INDEX_ALLOC == 0)
    {
        ptr = (long *) malloc(((file->counter/DLT_COMMON_INDEX_ALLOC) + 1) * DLT_COMMON_INDEX_ALLOC * sizeof(long));

        if (ptr==0)
        {
            return -1;
        }

        if (file->index)
        {
            memcpy(ptr,file->index,file->counter * sizeof(long));
            free(file->index);
        }
        file->index = ptr;
    }

    /* set to end of last succesful read message, because of conflicting calls to dlt_file_read and dlt_file_message */
    if (0 !=  fseek(file->handle,file->file_position,SEEK_SET))
    {
        snprintf(str,DLT_COMMON_BUFFER_LENGTH,"Seek failed to file_position %ld \n",file->file_position);
        dlt_log(LOG_WARNING, str);
        return -1;
    }

    /* get file position at start of DLT message */
    if (verbose)
    {
        snprintf(str,DLT_COMMON_BUFFER_LENGTH,"Position in file: %ld\n",file->file_position);
        dlt_log(LOG_INFO, str);
    }

    /* read header */
    if (dlt_file_read_header(file,verbose)<0)
    {
        /* go back to last position in file */
        fseek(file->handle,file->file_position,SEEK_SET);
        return -1;
    }

    if (file->filter)
    {
        /* read the extended header if filter is enabled and extended header exists */
        if (dlt_file_read_header_extended(file, verbose)<0)
        {
            /* go back to last position in file */
            if (0 != fseek(file->handle,file->file_position,SEEK_SET))
            {
                snprintf(str,DLT_COMMON_BUFFER_LENGTH,"Seek to last file pos failed!\n");
                dlt_log(LOG_WARNING, str);
            }
            return-1;
        }

        /* check the filters if message is used */
        if (dlt_message_filter_check(&(file->msg),file->filter,verbose) == 1)
        {
            /* filter matched, consequently store current message */
            /* store index pointer to message position in DLT file */
            file->index[file->counter] = file->file_position;
            file->counter++;
            file->position = file->counter - 1;

            found = 1;
        }

        /* skip payload data */
        if (fseek(file->handle,file->msg.datasize,SEEK_CUR)!=0)
        {
            /* go back to last position in file */
            snprintf(str,DLT_COMMON_BUFFER_LENGTH,"Seek failed to skip payload data of size %d!\n",file->msg.datasize);
            dlt_log(LOG_WARNING, str);

            if (0 != fseek(file->handle,file->file_position,SEEK_SET))
            {
                snprintf(str,DLT_COMMON_BUFFER_LENGTH,"Seek back also failed!\n");
                dlt_log(LOG_WARNING, str);
            }

            return -1;
        }
    }
    else
    {
        /* filter is disabled */
        /* skip additional header parameters and payload data */
        if (fseek(file->handle,file->msg.headersize - sizeof(DltStorageHeader) - sizeof(DltStandardHeader) + file->msg.datasize,SEEK_CUR))
        {

            snprintf(str,DLT_COMMON_BUFFER_LENGTH,"Seek failed to skip extra header and payload data from file of size %d!\n",
                    file->msg.headersize - sizeof(DltStorageHeader) - sizeof(DltStandardHeader) + file->msg.datasize);
            dlt_log(LOG_WARNING, str);
            /* go back to last position in file */
            if (fseek(file->handle,file->file_position,SEEK_SET))
            {
                snprintf(str,DLT_COMMON_BUFFER_LENGTH,"Seek back also failed!\n");
                dlt_log(LOG_WARNING, str);
            }
            return -1;
        }

        /* store index pointer to message position in DLT file */
        file->index[file->counter] = file->file_position;
        file->counter++;
        file->position = file->counter - 1;

        found = 1;
    }

    /* increase total message counter */
    file->counter_total++;

    /* store position to next message */
    file->file_position = ftell(file->handle);

    return found;
}

int dlt_file_read_raw(DltFile *file,int resync, int verbose)
{
    int found = 0;
    long *ptr;

    if (verbose)
    {
        snprintf(str,DLT_COMMON_BUFFER_LENGTH,"%s: Message %d:\n",__func__, file->counter_total);
        dlt_log(LOG_DEBUG, str);
    }

    if (file==0)
        return -1;

    /* allocate new memory for index if number of messages exceeds a multiple of DLT_COMMON_INDEX_ALLOC (e.g.: 1000) */
    if (file->counter % DLT_COMMON_INDEX_ALLOC == 0)
    {
        ptr = (long *) malloc(((file->counter/DLT_COMMON_INDEX_ALLOC) + 1) * DLT_COMMON_INDEX_ALLOC * sizeof(long));

        if (ptr==0)
        {
            return -1;
        }

        if (file->index)
        {
            memcpy(ptr,file->index,file->counter * sizeof(long));
            free(file->index);
        }
        file->index = ptr;
    }

    /* set to end of last successful read message, because of conflicting calls to dlt_file_read and dlt_file_message */
    if (0 != fseek(file->handle,file->file_position,SEEK_SET))
        return -1;

    /* get file position at start of DLT message */
    if (verbose)
    {
        snprintf(str,DLT_COMMON_BUFFER_LENGTH,"Position in file: %ld\n",file->file_position);
        dlt_log(LOG_DEBUG, str);
    }

    /* read header */
    if (dlt_file_read_header_raw(file,resync,verbose)<0)
    {
        /* go back to last position in file */
        if (0!= fseek(file->handle,file->file_position,SEEK_SET))
        {
            snprintf(str,DLT_COMMON_BUFFER_LENGTH,"dlt_file_read_raw, fseek failed 1\n");
            dlt_log(LOG_WARNING, str);
        }
        return -1;
    }

    /* read the extended header if filter is enabled and extended header exists */
    if (dlt_file_read_header_extended(file, verbose)<0)
    {
        /* go back to last position in file */
        if (0 != fseek(file->handle,file->file_position,SEEK_SET))
        {
            snprintf(str,DLT_COMMON_BUFFER_LENGTH,"dlt_file_read_raw, fseek failed 2\n");
            dlt_log(LOG_WARNING, str);
        }
        return-1;
    }

    if (dlt_file_read_data(file,verbose)<0)
    {
        /* go back to last position in file */
        if (0 != fseek(file->handle,file->file_position,SEEK_SET))
        {
            snprintf(str,DLT_COMMON_BUFFER_LENGTH,"dlt_file_read_raw, fseek failed 3\n");
            dlt_log(LOG_WARNING, str);
        }
        return-1;
    }

    /* store index pointer to message position in DLT file */
    file->index[file->counter] = file->file_position;
    file->counter++;
    file->position = file->counter - 1;

    found = 1;

    /* increase total message counter */
    file->counter_total++;

    /* store position to next message */
    file->file_position = ftell(file->handle);

    return found;
}

int dlt_file_close(DltFile *file,int verbose)
{
    PRINT_FUNCTION_VERBOSE(verbose);

    if (file==0)
    {
        return -1;
    }

    if (file->handle)
    {
        fclose(file->handle);
    }

    file->handle = 0;

    return 0;
}

int dlt_file_message(DltFile *file,int index,int verbose)
{
    PRINT_FUNCTION_VERBOSE(verbose);

    if (file==0)
    {
        return -1;
    }

    /* check if message is in range */
    if (index >= file->counter)
    {
        snprintf(str,DLT_COMMON_BUFFER_LENGTH,"Message %d out of range!\r\n",index);
        dlt_log(LOG_WARNING, str);
        return -1;
    }

    /* seek to position in file */
    if (fseek(file->handle,file->index[index],SEEK_SET)!=0)
    {
        snprintf(str,DLT_COMMON_BUFFER_LENGTH,"Seek to message %d to position %ld failed!\r\n",index,file->index[index]);
        dlt_log(LOG_WARNING, str);
        return -1;
    }

    /* read all header and payload */
    if (dlt_file_read_header(file,verbose)<0)
    {
        return -1;
    }

    if (dlt_file_read_header_extended(file,verbose)<0)
    {
        return -1;
    }

    if (dlt_file_read_data(file,verbose)<0)
    {
        return -1;
    }

    /* set current position in file */
    file->position = index;

    return 0;
}

int dlt_file_free(DltFile *file,int verbose)
{
    PRINT_FUNCTION_VERBOSE(verbose);

    if (file==0)
    {
        return -1;
    }

    /* delete index lost if exists */
    if (file->index)
    {
        free(file->index);
    }
    file->index = 0;

    /* close file */
    if (file->handle)
    {
        fclose(file->handle);
    }
    file->handle = 0;

    return dlt_message_free(&(file->msg),verbose);
}

void dlt_log_set_level(int level)
{
	logging_level = level;
}

int get_test_dlt_log_set_level()
{
    return logging_level;
}

void dlt_log_set_filename(const char *filename)
{
    // check nullpointer
    if(!filename)
        return;

    strncpy(logging_filename,filename,NAME_MAX);
    logging_filename[NAME_MAX]=0;

}

char * get_test_dlt_log_set_filename()
{
    return logging_filename;
}

void dlt_log_init(int mode)
{
    logging_mode = mode;
    
	if(logging_mode == DLT_LOG_TO_FILE)
	{
		/* internal logging to file */
		logging_handle = fopen(logging_filename,"w");
		if (logging_handle == 0)
		{
			printf("Internal log file %s cannot be opened!\n",logging_filename);
			return;
		}
	}
}

int get_test_dlt_log_init()
{
    return logging_mode;
}

void dlt_log_free(void)
{
	if(logging_mode == DLT_LOG_TO_FILE) {
		fclose(logging_handle);
	}
}

int get_test_dlt_log_free()
{
    if(logging_mode == DLT_LOG_TO_FILE)
        return logging_handle;
    else
        return logging_mode;
}

int dlt_log(int prio, char *s)
{
    static const char asSeverity[LOG_DEBUG+2][11] = {"EMERGENCY", "ALERT    ", "CRITICAL ", "ERROR    ", "WARNING  ", "NOTICE   ", "INFO     ", "DEBUG    ", "         "};
    static const char sFormatString[] = "[%5d.%06d]~DLT~%5d~%s~%s";
    struct timespec sTimeSpec;

    if (s==0)
    {
        return -1;
    }
	if(logging_level<prio)
	{
		return 0;
	}
    if ((prio < 0) || (prio > LOG_DEBUG))
    {
        prio = LOG_DEBUG + 1;
    }

    clock_gettime(CLOCK_MONOTONIC, &sTimeSpec);
	switch(logging_mode)
	{
		case DLT_LOG_TO_CONSOLE:
			/* log to stdout */
			printf(sFormatString, (unsigned int)sTimeSpec.tv_sec, (unsigned int)(sTimeSpec.tv_nsec/1000), getpid(), asSeverity[prio], s);
			break;
		case DLT_LOG_TO_SYSLOG:
			/* log to syslog */
#if !defined (__WIN32__) && !defined(_MSC_VER)
			openlog("DLT",LOG_PID,LOG_DAEMON);
			syslog(prio, sFormatString, (unsigned int)sTimeSpec.tv_sec, (unsigned int)(sTimeSpec.tv_nsec/1000), getpid(), getpid(), asSeverity[prio], s);
			closelog();
#endif
			break;
		case DLT_LOG_TO_FILE:
			/* log to file */
			if(logging_handle) {
				fprintf(logging_handle,sFormatString, (unsigned int)sTimeSpec.tv_sec, (unsigned int)(sTimeSpec.tv_nsec/1000), getpid(), getpid(), asSeverity[prio], s);
				fflush(logging_handle);
			}
			break;
		case DLT_LOG_DROPPED:
		default:
			break;
	}

    return 0;
}

int dlt_receiver_init(DltReceiver *receiver,int fd, int buffersize)
{
    if (receiver==0)
    {
        return -1;
    }

    receiver->lastBytesRcvd = 0;
    receiver->bytesRcvd = 0;
    receiver->totalBytesRcvd = 0;
    receiver->buffersize = buffersize;
    receiver->fd = fd;
    receiver->buffer = (char*)malloc(receiver->buffersize);

    if (receiver->buffer == 0)
    {
        receiver->buf = 0;
        return -1;
    }
    else
    {
        receiver->buf = receiver->buffer;
    }

    return 0;
}

int dlt_receiver_free(DltReceiver *receiver)
{

    if (receiver==0)
    {
        return -1;
    }

    if (receiver->buffer)
    {
        free(receiver->buffer);
    }

    receiver->buffer = 0;
    receiver->buf = 0;

    return 0;
}

#ifndef QT_VIEWER
int dlt_receiver_receive_socket(DltReceiver *receiver)
{
    if (receiver==0)
    {
        return -1;
    }

    if (receiver->buffer==0)
    {
        return -1;
    }

    receiver->buf = (char *)receiver->buffer;
    receiver->lastBytesRcvd = receiver->bytesRcvd;

    /* wait for data from socket */
    if ((receiver->bytesRcvd = recv(receiver->fd, receiver->buf + receiver->lastBytesRcvd, receiver->buffersize - receiver->lastBytesRcvd , 0)) <= 0)
    {
        receiver->bytesRcvd = 0;

        return receiver->bytesRcvd;
    } /* if */

    receiver->totalBytesRcvd += receiver->bytesRcvd;
    receiver->bytesRcvd += receiver->lastBytesRcvd;

    return receiver->bytesRcvd;
}
#endif

int dlt_receiver_receive_fd(DltReceiver *receiver)
{
    if (receiver==0)
    {
        return -1;
    }

    if (receiver->buffer==0)
    {
        return -1;
    }

    receiver->buf = (char *)receiver->buffer;
    receiver->lastBytesRcvd = receiver->bytesRcvd;

    /* wait for data from fd */
    if ((receiver->bytesRcvd = read(receiver->fd, receiver->buf + receiver->lastBytesRcvd, receiver->buffersize - receiver->lastBytesRcvd)) <= 0)
    {
        receiver->bytesRcvd = 0;

        return receiver->bytesRcvd;
    } /* if */

    receiver->totalBytesRcvd += receiver->bytesRcvd;
    receiver->bytesRcvd += receiver->lastBytesRcvd;

    return receiver->bytesRcvd;
}

int dlt_receiver_remove(DltReceiver *receiver,int size)
{
    if (receiver==0)
    {
        return -1;
    }

    if (receiver->buf==0)
    {
        return -1;
    }

    if (size > receiver->bytesRcvd || size <= 0)
    {
    	receiver->buf = receiver->buf + receiver->bytesRcvd;
    	receiver->bytesRcvd=0;
    	return -1;
    }

    receiver->bytesRcvd = receiver->bytesRcvd - size;
    receiver->buf = receiver->buf + size;

    return 0;
}

int dlt_receiver_move_to_begin(DltReceiver *receiver)
{
    if (receiver==0)
    {
        return -1;
    }

    if ((receiver->buffer==0) || (receiver->buf==0))
    {
        return -1;
    }

    if ((receiver->buffer!=receiver->buf) && (receiver->bytesRcvd!=0))
    {
        memmove(receiver->buffer,receiver->buf,receiver->bytesRcvd);
    }

    return 0;
}

int dlt_set_storageheader(DltStorageHeader *storageheader, const char *ecu)
{

#if !defined(_MSC_VER)
    struct timeval tv;
#endif

    if (storageheader==0)
    {
        return -1;
    }

    /* get time of day */
#if defined(_MSC_VER)
    time(&(storageheader->seconds));
#else
    gettimeofday(&tv, NULL);
#endif

    /* prepare storage header */
    storageheader->pattern[0] = 'D';
    storageheader->pattern[1] = 'L';
    storageheader->pattern[2] = 'T';
    storageheader->pattern[3] = 0x01;

    dlt_set_id(storageheader->ecu,ecu);

    /* Set current time */
#if defined(_MSC_VER)
    storageheader->microseconds = 0;
#else
    storageheader->seconds = (time_t)tv.tv_sec; /* value is long */
    storageheader->microseconds = (int32_t)tv.tv_usec; /* value is long */
#endif

    return 0;
}

int dlt_check_storageheader(DltStorageHeader *storageheader)
{
    if (storageheader==0)
    {
        return -1;
    }

    return  ((storageheader->pattern[0] == 'D') &&
             (storageheader->pattern[1] == 'L') &&
             (storageheader->pattern[2] == 'T') &&
             (storageheader->pattern[3] == 1));
}












int dlt_buffer_init_static_server(DltBuffer *buf, const unsigned char *ptr, uint32_t size)
{
	char str[256];
	DltBufferHead *head;

	// Init parameters
	buf->shm = (unsigned char *)ptr;
	buf->min_size = size;
	buf->max_size = size;
	buf->step_size = 0;
	
	// Init pointers
 	head = (DltBufferHead*)buf->shm;
	head->read = 0;
	head->write = 0;
	head->count = 0;
    buf->mem = (unsigned char *)(buf->shm + sizeof(DltBufferHead));
	buf->size = buf->min_size - sizeof(DltBufferHead);

	// clear memory
	memset(buf->mem,0,buf->size);
    
	snprintf(str,sizeof(str),"Buffer: Size %d\n",buf->size);
	dlt_log(LOG_DEBUG, str);

	return 0; /* OK */
}

int dlt_buffer_init_static_client(DltBuffer *buf, const unsigned char *ptr, uint32_t size)
{
	char str[256];

	// Init parameters
	buf->shm = (unsigned char *)ptr;
	buf->min_size = size;
	buf->max_size = size;
	buf->step_size = 0;
	
	// Init pointers
    buf->mem = (unsigned char *)(buf->shm + sizeof(DltBufferHead));
    buf->size = buf->min_size - sizeof(DltBufferHead);

	snprintf(str,sizeof(str),"Buffer: Size %d\n",buf->size);
	dlt_log(LOG_DEBUG, str);

	return 0; /* OK */
}

int dlt_buffer_init_dynamic(DltBuffer *buf, uint32_t min_size, uint32_t max_size,uint32_t step_size)
{
    //Do not DLT_SEM_LOCK inside here!
	char str[256];
	DltBufferHead *head;

    // catch null pointer
    if(!buf)
        return -1;
    // catch 0 logical errors
    if(min_size == 0 || max_size == 0 || step_size == 0)
        return -1;

	// Init parameters
	buf->min_size = min_size;
	buf->max_size = max_size;
	buf->step_size = step_size;

	// allocat memory
	buf->shm = malloc(buf->min_size);
	if(buf->shm == NULL) {
		snprintf(str,sizeof(str),"Buffer: Cannot allocate %d bytes\n",buf->min_size);
		dlt_log(LOG_EMERG, str);
		return -1;
	}
	
	// Init pointers
	head = (DltBufferHead*)buf->shm;
	head->read = 0;
	head->write = 0;
	head->count = 0;
    buf->mem = (unsigned char *)(buf->shm + sizeof(DltBufferHead));
    buf->size = buf->min_size - sizeof(DltBufferHead);

	// clear memory
	memset(buf->mem,0,buf->size);
    
	//snprintf(str,sizeof(str),"Buffer: Size %d bytes\n",buf->size);
	//dlt_log(LOG_INFO, str);

	return 0; /* OK */
}

int dlt_buffer_free_static(DltBuffer *buf)
{
	if(!buf->mem) {
		// buffer not initialised
		dlt_log(LOG_WARNING,"Buffer: Buffer not initialised\n");
		return -1; /* ERROR */
	}

	return 0;
}

int dlt_buffer_free_dynamic(DltBuffer *buf)
{
    // catch null pointer
    if(!buf)
        return -1;

	if(!buf->shm) {
		// buffer not initialised
		dlt_log(LOG_WARNING,"Buffer: Buffer not initialised\n");
		return -1; /* ERROR */
	}

    free(buf->shm);
	buf->shm = 0;
	buf->mem = 0;
	
	return 0;
}

void dlt_buffer_write_block(DltBuffer *buf,int *write, const unsigned char *data,unsigned int size)
{
    // catch null pointer
    if(buf && write && data) {
        if((int)(*write+size) <= buf->size) {
            // write one block
            memcpy(buf->mem+*write,data,size);
            *write += size;
        }
        else {
            // write two blocks
            memcpy(buf->mem+*write, data, buf->size-*write);
            memcpy(buf->mem, data+buf->size-*write, size-buf->size+*write);
            *write += size-buf->size;
        }
    }
}

int get_test_dlt_buffer_write_block(DltBuffer *buf, int *write, const unsigned char *data, unsigned int size)
{
    // Get-Method for gtest. Test dlt_buffer_write_block
    dlt_buffer_write_block(buf, write, data, size);
    if (!write)
        return -1;
    else
        return *write;
}

void dlt_buffer_read_block(DltBuffer *buf,int *read,unsigned char *data,unsigned int size)
{
    // catch nullpointer
    if(buf && read && data) {
        if((int)(*read+size) <= buf->size) {
            // read one block
            memcpy(data,buf->mem+*read,size);
            *read += size;
        }
        else {
            // read two blocks
            memcpy(data, buf->mem+*read, buf->size-*read);
            memcpy(data+buf->size-*read, buf->mem, size-buf->size+*read);
            *read += size-buf->size;
        }
    }
}

int get_test_dlt_buffer_read_block(DltBuffer *buf, int *read,unsigned char *data,unsigned int size)
{
    // Get-Method for gtest. Test dlt_buffer_read_block
    dlt_buffer_read_block(buf, read, data, size);
    if(!read)
        return -1;
    else
        return *read;
}

int dlt_buffer_increase_size(DltBuffer *buf)
{
	DltBufferHead *head,*new_head;
	unsigned char *new_ptr;

    // catch null pointer
    if(!buf)
        return -1;

	/* check size */
	if(buf->step_size==0) {
		/* cannot increase size */
		return -1;
	}

	/* check size */
	if((buf->size + sizeof(DltBufferHead) + buf->step_size) > buf->max_size) {
		/* max size reached, do not increase */
		return -1;
	}

	/* allocate new buffer */
	new_ptr = malloc(buf->size + sizeof(DltBufferHead) + buf->step_size);
	if(new_ptr == NULL) {
		snprintf(str,sizeof(str),"Buffer: Cannot increase size because allocate %d bytes failed\n",buf->min_size);
		dlt_log(LOG_WARNING, str);
		return -1;
	}
		
	/* copy data */
	head = (DltBufferHead*)buf->shm;
	new_head = (DltBufferHead*)new_ptr;
	if(head->read < head->write) {
		memcpy(new_ptr+sizeof(DltBufferHead) , buf->mem+head->read , head->write-head->read);
		new_head->read = 0;	
		new_head->write = head->write-head->read;	
		new_head->count = head->count;	
	}
	else {
		memcpy(new_ptr+sizeof(DltBufferHead) , buf->mem+head->read , buf->size-head->read);
		memcpy(new_ptr+sizeof(DltBufferHead)+buf->size-head->read , buf->mem , head->write);
		new_head->read = 0;	
		new_head->write = buf->size-head->read+head->write;	
		new_head->count = head->count;			
	}
	
	/* free old data */
	free(buf->shm);
	
	/* update data */
	buf->shm = new_ptr;
	buf->mem = new_ptr+sizeof(DltBufferHead);
	buf->size += buf->step_size;
	
	snprintf(str,sizeof(str),"Buffer: Size increased to %d bytes\n",buf->size+sizeof(DltBufferHead));
	dlt_log(LOG_DEBUG, str);

	return 0; // OK		
}

int dlt_buffer_minimize_size(DltBuffer *buf)
{
	unsigned char *new_ptr;

    // catch null pointer
    if(!buf)
        return -1;

	if((buf->size + sizeof(DltBufferHead)) == buf->min_size)
	{
		/* already minimized */
		return 0;
	}

	/* allocate new buffer */
	new_ptr = malloc(buf->min_size);
	if(new_ptr == NULL) {
		snprintf(str,sizeof(str),"Buffer: Cannot set to min size of %d bytes\n",buf->min_size);
		dlt_log(LOG_WARNING, str);
		return -1;
	}

	/* free old data */
	free(buf->shm);
	
	/* update data */
	buf->shm = new_ptr;
	buf->mem = new_ptr+sizeof(DltBufferHead);
	buf->size = buf->min_size - sizeof(DltBufferHead);

	/* reset pointers and counters */	
	((int*)(buf->shm))[0] = 0;  // pointer to write memory  
	((int*)(buf->shm))[1] = 0;  // pointer to read memory
	((int*)(buf->shm))[2] = 0;  // number of packets

	// clear memory
	memset(buf->mem,0,buf->size);

	dlt_log(LOG_DEBUG,"Buffer: Buffer minimized.\n");

	return 0; /* OK */	
}

int dlt_buffer_reset(DltBuffer *buf)
{
    // catch null pointer
    if(!buf)
        return -1;

	dlt_log(LOG_WARNING,"Buffer: Buffer reset triggered.\n");

	/* reset pointers and counters */	
	((int*)(buf->shm))[0] = 0;  // pointer to write memory  
	((int*)(buf->shm))[1] = 0;  // pointer to read memory
	((int*)(buf->shm))[2] = 0;  // number of packets

	// clear memory
    memset(buf->mem,0,buf->size);

	return 0; /* OK */
}

int dlt_buffer_push(DltBuffer *buf,const unsigned char *data,unsigned int size)
{
    return dlt_buffer_push3(buf,data,size,0,0,0,0);
}

int dlt_buffer_push3(DltBuffer *buf,const unsigned char *data1,unsigned int size1,const unsigned char *data2,unsigned int size2,const unsigned char *data3,unsigned int size3)
{
	int free_size;	
	int write, read, count;
	DltBufferBlockHead head;

    // catch null pointer
    if(!buf)
        return -1;
	
	if(!buf->shm) {
		// buffer not initialised
		dlt_log(LOG_ERR,"Buffer: Buffer not initialised\n");
		return -1; /* ERROR */
	}

	// get current write pointer
	write = ((int*)(buf->shm))[0];
	read = ((int*)(buf->shm))[1];
	count = ((int*)(buf->shm))[2];

	// check pointers
	if((read>buf->size) || (write>buf->size))
	{
		dlt_log(LOG_ERR,"Buffer: Pointer out of range\n");
		dlt_buffer_reset(buf);
		return -1; // ERROR		
	}

	// calculate free size
	if(read>write)
		free_size = read - write;
	else if(count && (write == read))
		free_size = 0;	
	else
		free_size = buf->size - write + read;
	
	// check size
	if(free_size < (int)(sizeof(DltBufferBlockHead)+size1+size2+size3)) {
		// try to increase size if possible
		if(dlt_buffer_increase_size(buf)) {
			/* increase size is not possible */
			//dlt_log(LOG_ERR,"Buffer: Buffer is full\n");
			return -1; // ERROR
		}
		// update pointers
		write = ((int*)(buf->shm))[0];
		read = ((int*)(buf->shm))[1];
	}

	// set header
	strncpy(head.head,DLT_BUFFER_HEAD,3);
	head.head[3]=0;
	head.status = 2;
	head.size = size1+size2+size3;

	// write data
	dlt_buffer_write_block(buf,&write,(unsigned char*)&head,sizeof(DltBufferBlockHead));
    if(size1) dlt_buffer_write_block(buf,&write,data1,size1);
    if(size2) dlt_buffer_write_block(buf,&write,data2,size2);
    if(size3) dlt_buffer_write_block(buf,&write,data3,size3);

	// update global shm pointers
	((int*)(buf->shm))[0] = write; // set new write pointer 	
	((int*)(buf->shm))[2] += 1; // increase counter

	return 0; // OK
	
}

int dlt_buffer_get(DltBuffer *buf,unsigned char *data, int max_size,int delete)
{
	int used_size;	
	int write, read, count;
	char head_compare[] = DLT_BUFFER_HEAD;
	DltBufferBlockHead head;

    // catch null pointer
    if(!buf)
        return -1;
	
	if(!buf->shm) {
		// shm not initialised
		dlt_log(LOG_ERR,"Buffer: SHM not initialised\n");
		return -1; /* ERROR */
	}

	// get current write pointer
	write = ((int*)(buf->shm))[0];
	read = ((int*)(buf->shm))[1];
	count = ((int*)(buf->shm))[2];

	// check pointers
	if((read>buf->size) || (write>buf->size) || (count<0))
	{
		dlt_log(LOG_ERR,"Buffer: Pointer out of range\n");
		dlt_buffer_reset(buf);
		return -1; // ERROR		
	}

	// check if data is in there
	if(count==0) {
		if(write!=read)
		{
			dlt_log(LOG_ERR,"Buffer: SHM should be empty, but is not\n");
			dlt_buffer_reset(buf);
		}
		return -1; // ERROR		
	}

	// calculate used size
	if(write>read)
		used_size = write - read;
	else	
		used_size = buf->size - read + write;

	// first check size
	if(used_size < (int)(sizeof(DltBufferBlockHead))) {
		dlt_log(LOG_ERR,"Buffer: Size check 1 failed\n");
		dlt_buffer_reset(buf);
		return -1; // ERROR
	}

	// read header
	dlt_buffer_read_block(buf,&read,(unsigned char*)&head,sizeof(DltBufferBlockHead));

	// check header
	if(memcmp((unsigned char*)(head.head),head_compare,sizeof(head_compare))!=0)
	{
		dlt_log(LOG_ERR,"Buffer: Header head check failed\n");
		dlt_buffer_reset(buf);
		return -1; // ERROR
	}
	if(head.status != 2)
	{
		dlt_log(LOG_ERR,"Buffer: Header status check failed\n");
		dlt_buffer_reset(buf);
		return -1; // ERROR
	}

	// second check size
	if(used_size < (int)(sizeof(DltBufferBlockHead)+head.size)) {
		dlt_log(LOG_ERR,"Buffer: Size check 2 failed\n");
		dlt_buffer_reset(buf);
		return -1; // ERROR
	}

	// third check size
	if(max_size && (head.size > max_size)) {
		dlt_log(LOG_ERR,"Buffer: Size check 3 failed\n");
		// nothing to do but data does not fit provided buffer
	}

	if(data && max_size)
	{
		// read data
		dlt_buffer_read_block(buf,&read,data,head.size);

		if(delete)
		{
			// update buffer pointers
			((int*)(buf->shm))[1] = read; // set new read pointer 	
		}
	}
	else
	{
		if(delete) {
			if( (read+head.size) <= buf->size)
				((int*)(buf->shm))[1] = read+head.size; // set new read pointer 	
			else
				((int*)(buf->shm))[1] = read+head.size-buf->size; // set new read pointer 		
		}
	}
	if(delete) {
		((int*)(buf->shm))[2] -= 1; // decrease counter
	
		if(((int*)(buf->shm))[2] == 0)
		{
			// try to minimize size
			dlt_buffer_minimize_size(buf);
		}
	}
	
	return head.size; // OK	
}

int dlt_buffer_pull(DltBuffer *buf,unsigned char *data, int max_size)
{
	return dlt_buffer_get(buf,data,max_size,1);
}

int dlt_buffer_copy(DltBuffer *buf,unsigned char *data, int max_size)
{
	return dlt_buffer_get(buf,data,max_size,0);
}

int dlt_buffer_remove(DltBuffer *buf)
{
	return dlt_buffer_get(buf,0,0,1);
}

void dlt_buffer_info(DltBuffer *buf)
{
	char str[256];

	snprintf(str,sizeof(str),"Buffer: Available size: %d\n",buf->size);
	dlt_log(LOG_DEBUG, str);
	snprintf(str,sizeof(str),"Buffer: Buffer full start address: %lX\n",(unsigned long)buf->shm);
	dlt_log(LOG_DEBUG, str);
	snprintf(str,sizeof(str),"Buffer: Buffer start address: %lX\n",(unsigned long)buf->mem);
	dlt_log(LOG_DEBUG, str);
	
}

int get_test_dlt_buffer_info(DltBuffer *buf)
{
    // Get-Method for gtest. Test dlt_buffer_info
    dlt_buffer_info(buf);
    return 0;
}

void dlt_buffer_status(DltBuffer *buf)
{
	int write, read, count;
	char str[256];

    // check nullpointer
    if(!buf)
        return;

    /* check if buffer available */
    if(!buf->shm)
        return;

	write = ((int*)(buf->shm))[0];
	read = ((int*)(buf->shm))[1];
	count = ((int*)(buf->shm))[2];

	snprintf(str,sizeof(str),"Buffer: Write: %d\n",write);
	dlt_log(LOG_DEBUG, str);
	snprintf(str,sizeof(str),"Buffer: Read: %d\n",read);
	dlt_log(LOG_DEBUG, str);
	snprintf(str,sizeof(str),"Buffer: Count: %d\n",count);
	dlt_log(LOG_DEBUG, str);	
}

int dlt_buffer_get_total_size(DltBuffer *buf)
{
    // catch null pointer
    if(!buf)
        return -1;

	return buf->max_size;	
}

int dlt_buffer_get_used_size(DltBuffer *buf)
{
	int write, read, count;

    // catch null pointer
    if(!buf)
        return -1;

    /* check if buffer available */
    if(!buf->shm)
        return 0;

	write = ((int*)(buf->shm))[0];
	read = ((int*)(buf->shm))[1];
	count = ((int*)(buf->shm))[2];

	if(count == 0)
		return 0;
		
	if(write>read)
		return (write - read);
	
	return (buf->size - read + write);	
}

int dlt_buffer_get_message_count(DltBuffer *buf)
{
    // catch null pointer
    if(!buf)
        return -1;

    /* check if buffer available */
    if(!buf->shm)
        return 0;

    return ((int*)(buf->shm))[2];
}

#if !defined (__WIN32__)

int dlt_setup_serial(int fd, speed_t speed)
{
#if !defined (__WIN32__) && !defined(_MSC_VER)
    struct termios config;

    if (isatty(fd)==0)
    {
        return -1;
    }

    if (tcgetattr(fd, &config) < 0)
    {
        return -1;
    }

    /* Input flags - Turn off input processing
       convert break to null byte, no CR to NL translation,
       no NL to CR translation, don't mark parity errors or breaks
       no input parity check, don't strip high bit off,
       no XON/XOFF software flow control
    */
    config.c_iflag &= ~(IGNBRK | BRKINT | ICRNL |
                        INLCR | PARMRK | INPCK | ISTRIP | IXON);

    /* Output flags - Turn off output processing
       no CR to NL translation, no NL to CR-NL translation,
       no NL to CR translation, no column 0 CR suppression,
       no Ctrl-D suppression, no fill characters, no case mapping,
       no local output processing

       config.c_oflag &= ~(OCRNL | ONLCR | ONLRET |
                           ONOCR | ONOEOT| OFILL | OLCUC | OPOST);
    */
    config.c_oflag = 0;

    /* No line processing:
       echo off, echo newline off, canonical mode off,
       extended input processing off, signal chars off
    */
    config.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN | ISIG);

    /* Turn off character processing
       clear current char size mask, no parity checking,
       no output processing, force 8 bit input
    */
    config.c_cflag &= ~(CSIZE | PARENB);
    config.c_cflag |= CS8;

    /* One input byte is enough to return from read()
       Inter-character timer off
    */
    config.c_cc[VMIN]  = 1;
    config.c_cc[VTIME] = 0;

    /* Communication speed (simple version, using the predefined
       constants)
    */
    if (cfsetispeed(&config, speed) < 0 || cfsetospeed(&config, speed) < 0)
    {
        return -1;
    }

    /* Finally, apply the configuration
    */
    if (tcsetattr(fd, TCSAFLUSH, &config) < 0)
    {
        return -1;
    }

    return 0;
#else
    return -1;
#endif
}

speed_t dlt_convert_serial_speed(int baudrate)
{
#if !defined (__WIN32__) && !defined(_MSC_VER) && !defined(__CYGWIN__)
    speed_t ret;

    switch (baudrate)
    {
    case  50:
    {
        ret = B50;
        break;
    }
    case  75:
    {
        ret = B75;
        break;
    }
    case  110:
    {
        ret = B110;
        break;
    }
    case  134:
    {
        ret = B134;
        break;
    }
    case  150:
    {
        ret = B150;
        break;
    }
    case  200:
    {
        ret = B200;
        break;
    }
    case  300:
    {
        ret = B300;
        break;
    }
    case  600:
    {
        ret = B600;
        break;
    }
    case  1200:
    {
        ret = B1200;
        break;
    }
    case  1800:
    {
        ret = B1800;
        break;
    }
    case  2400:
    {
        ret = B2400;
        break;
    }
    case  4800:
    {
        ret = B4800;
        break;
    }
    case  9600:
    {
        ret = B9600;
        break;
    }
    case  19200:
    {
        ret = B19200;
        break;
    }
    case  38400:
    {
        ret = B38400;
        break;
    }
    case  57600:
    {
        ret = B57600;
        break;
    }
    case  115200:
    {
        ret = B115200;
        break;
    }
    case 230400:
    {
        ret = B230400;
        break;
    }
    case 460800:
    {
        ret = B460800;
        break;
    }
    case  500000:
    {
        ret = B500000;
        break;
    }
    case  576000:
    {
        ret = B576000;
        break;
    }
    case  921600:
    {
        ret = B921600;
        break;
    }
    case  1000000:
    {
        ret = B1000000;
        break;
    }
    case  1152000:
    {
        ret = B1152000;
        break;
    }
    case  1500000:
    {
        ret = B1500000;
        break;
    }
    case  2000000:
    {
        ret = B2000000;
        break;
    }
    case  2500000:
    {
        ret = B2500000;
        break;
    }
    case  3000000:
    {
        ret = B3000000;
        break;
    }
    case  3500000:
    {
        ret = B3500000;
        break;
    }
    case  4000000:
    {
        ret = B4000000;
        break;
    }
    default:
    {
        ret = B115200;
        break;
    }
    }

    return ret;
#else
    return 0;
#endif
}

#endif

void dlt_get_version(char *buf, size_t size)
{
    snprintf(buf,size,"DLT Package Version: %s %s, Package Revision: %s, build on %s %s\n%s %s %s %s\n",
            _DLT_PACKAGE_VERSION, _DLT_PACKAGE_VERSION_STATE, _DLT_PACKAGE_REVISION, __DATE__ , __TIME__,_DLT_SYSTEMD_ENABLE,_DLT_SYSTEMD_WATCHDOG_ENABLE,_DLT_TEST_ENABLE,_DLT_SHM_ENABLE );
}

void dlt_get_major_version(char *buf, size_t size)
{
	snprintf(buf,size,"%s",_DLT_PACKAGE_MAJOR_VERSION);
}

void dlt_get_minor_version(char *buf, size_t size)
{
    snprintf(buf,size,"%s",_DLT_PACKAGE_MINOR_VERSION);
}


uint32_t dlt_uptime(void)
{

#if defined (__WIN32__) || defined(_MSC_VER)

    return (uint32_t)(GetTickCount()*10); /* GetTickCount() return DWORD */

#else
    struct timespec ts;

    if (clock_gettime(CLOCK_MONOTONIC,&ts)==0)
    {        
        return ( (uint32_t)ts.tv_sec*10000 + (uint32_t)ts.tv_nsec/100000 );// in 0.1 ms = 100 us
    }
    else
    {
        return 0;
    }

#endif

}

int dlt_message_print_header(DltMessage *message, char *text, uint32_t size, int verbose)
{
    if ((message==0) || (text==0))
    {
        return -1;
    }

    dlt_message_header(message,text,size,verbose);
    printf("%s\n",text);

    return 0;
}

int dlt_message_print_hex(DltMessage *message, char *text, uint32_t size, int verbose)
{
    if ((message==0) || (text==0))
    {
        return -1;
    }

    dlt_message_header(message,text,size,verbose);
    printf("%s ",text);
    dlt_message_payload(message,text,size,DLT_OUTPUT_HEX,verbose);
    printf("[%s]\n",text);

    return 0;
}

int dlt_message_print_ascii(DltMessage *message, char *text, uint32_t size, int verbose)
{
    if ((message==0) || (text==0))
    {
        return -1;
    }

    dlt_message_header(message,text,size,verbose);
    printf("%s ",text);
    dlt_message_payload(message,text,size,DLT_OUTPUT_ASCII,verbose);
    printf("[%s]\n",text);

    return 0;
}

int dlt_message_print_mixed_plain(DltMessage *message, char *text, uint32_t size, int verbose)
{
    if ((message==0) || (text==0))
    {
        return -1;
    }

    dlt_message_header(message,text,size,verbose);
    printf("%s \n",text);
    dlt_message_payload(message,text,size,DLT_OUTPUT_MIXED_FOR_PLAIN,verbose);
    printf("[%s]\n",text);

    return 0;
}

int dlt_message_print_mixed_html(DltMessage *message, char *text, uint32_t size, int verbose)
{
    if ((message==0) || (text==0))
    {
        return -1;
    }

    dlt_message_header(message,text,size,verbose);
    printf("%s \n",text);
    dlt_message_payload(message,text,size,DLT_OUTPUT_MIXED_FOR_HTML,verbose);
    printf("[%s]\n",text);

    return 0;
}

int dlt_message_argument_print(DltMessage *msg,uint32_t type_info,uint8_t **ptr,int32_t *datalength,char *text,int textlength,int byteLength,int __attribute__((unused)) verbose)
{
    int16_t length=0,length_tmp=0; /* the macro can set this variable to -1 */
    uint16_t length2=0,length2_tmp=0,length3=0,length3_tmp=0;

    uint8_t value8u=0;
    uint16_t value16u=0,value16u_tmp=0;
    uint32_t value32u=0,value32u_tmp=0;
    uint64_t value64u=0,value64u_tmp=0;

    int8_t  value8i=0;
    int16_t value16i=0,value16i_tmp=0;
    int32_t value32i=0,value32i_tmp=0;
    int64_t value64i=0,value64i_tmp=0;

    float32_t value32f=0,value32f_tmp=0;
    int32_t value32f_tmp_int32i=0,value32f_tmp_int32i_swaped=0;
    float64_t value64f=0,value64f_tmp=0;
    int64_t value64f_tmp_int64i=0,value64f_tmp_int64i_swaped=0;

    uint32_t quantisation_tmp = 0;
    if ( (type_info & DLT_TYPE_INFO_STRG) && (((type_info & DLT_TYPE_INFO_SCOD) == DLT_SCOD_ASCII) || ((type_info & DLT_TYPE_INFO_SCOD) == DLT_SCOD_UTF8)) )
    {

        /* string type or utf8-encoded string type */
        if (byteLength<0)
        {
            DLT_MSG_READ_VALUE(length_tmp,*ptr,*datalength,uint16_t);
            if((*datalength)<0)
                return -1;
            length=DLT_ENDIAN_GET_16(msg->standardheader->htyp, length_tmp);
        }
        else
        {
            length=(int16_t)byteLength;
        }

        if (type_info & DLT_TYPE_INFO_VARI)
        {
            DLT_MSG_READ_VALUE(length2_tmp,*ptr,*datalength,uint16_t);
            if((*datalength)<0)
                return -1;
            length2=DLT_ENDIAN_GET_16(msg->standardheader->htyp, length2_tmp);
            if((*datalength)<length2)
                return -1;
            *ptr += length2;
            *datalength-=length2;
        }

        DLT_MSG_READ_STRING((text+strlen(text)),*ptr,*datalength,length);
        if((*datalength)<0)
            return -1;

    }
    else if (type_info & DLT_TYPE_INFO_BOOL)
    {
        /* Boolean type */
        if (type_info & DLT_TYPE_INFO_VARI)
        {
            DLT_MSG_READ_VALUE(length2_tmp,*ptr,*datalength,uint16_t);
            if((*datalength)<0)
                return -1;
            length2=DLT_ENDIAN_GET_16(msg->standardheader->htyp, length2_tmp);
            if((*datalength)<length2)
                return -1;
            *ptr += length2;
            *datalength-=length2;
        }
        value8u=0;
        DLT_MSG_READ_VALUE(value8u,*ptr,*datalength,uint8_t); /* No endian conversion necessary */
        if((*datalength)<0)
            return -1;
        snprintf(text+strlen(text),textlength-strlen(text),"%d",value8u);
    }
    else if ((type_info & DLT_TYPE_INFO_UINT) && (DLT_SCOD_BIN == (type_info & DLT_TYPE_INFO_SCOD)))
    {
        if (DLT_TYLE_8BIT == (type_info & DLT_TYPE_INFO_TYLE))
        {
            DLT_MSG_READ_VALUE(value8u,*ptr,*datalength,uint8_t); /* No endian conversion necessary */
            if((*datalength)<0)
                return -1;
            char binary[10] = {'\0'}; // e.g.: "0b1100 0010"
            int i;
            for (i = (1 << 7); i > 0; i >>= 1)
            {
                if ((1<<3) == i)
                    strcat(binary, " ");
                strcat(binary, (i == (value8u & i)) ? "1" : "0");
            }

            snprintf(text+strlen(text),textlength-strlen(text),"0b%s",binary);
        }
        if (DLT_TYLE_16BIT == (type_info & DLT_TYPE_INFO_TYLE))
        {
            DLT_MSG_READ_VALUE(value16u,*ptr,*datalength,uint16_t);
            if((*datalength)<0)
                return -1;
            char binary[20] = {'\0'}; // e.g.: "0b1100 0010 0011 0110"
            int i;
            for (i = (1 << 15); i > 0; i >>= 1)
            {
                if (((1<<3) == i) || ((1<<7) == i) || ((1<<11) == i))
                    strcat(binary, " ");
                strcat(binary, (i == (value16u & i)) ? "1" : "0");
            }

            snprintf(text+strlen(text),textlength-strlen(text),"0b%s",binary);
        }
    }
    else if ((type_info & DLT_TYPE_INFO_UINT) && (DLT_SCOD_HEX == (type_info & DLT_TYPE_INFO_SCOD)))
    {
        if (DLT_TYLE_8BIT == (type_info & DLT_TYPE_INFO_TYLE))
        {
            DLT_MSG_READ_VALUE(value8u,*ptr,*datalength,uint8_t); /* No endian conversion necessary */
            if((*datalength)<0)
                return -1;
            snprintf(text+strlen(text),textlength-strlen(text),"0x%02x",value8u);
        }
        if (DLT_TYLE_16BIT == (type_info & DLT_TYPE_INFO_TYLE))
        {
            DLT_MSG_READ_VALUE(value16u,*ptr,*datalength,uint16_t);
            if((*datalength)<0)
                return -1;
            snprintf(text+strlen(text),textlength-strlen(text),"0x%04x",value16u);
        }
        if (DLT_TYLE_32BIT == (type_info & DLT_TYPE_INFO_TYLE))
        {
            DLT_MSG_READ_VALUE(value32u,*ptr,*datalength,uint32_t);
            if((*datalength)<0)
                return -1;
            snprintf(text+strlen(text),textlength-strlen(text),"0x%08x",value32u);
        }
        if (DLT_TYLE_64BIT == (type_info & DLT_TYPE_INFO_TYLE))
        {
            *ptr += 4;
            DLT_MSG_READ_VALUE(value32u,*ptr,*datalength,uint32_t);
            if((*datalength)<0)
                return -1;
            snprintf(text+strlen(text),textlength-strlen(text),"0x%08x",value32u);
            *ptr -= 8;
            DLT_MSG_READ_VALUE(value32u,*ptr,*datalength,uint32_t);
            if((*datalength)<0)
                return -1;
            snprintf(text+strlen(text),textlength-strlen(text),"%08x",value32u);
            *ptr += 4;
        }
    }
    else if ((type_info & DLT_TYPE_INFO_SINT) || (type_info & DLT_TYPE_INFO_UINT))
    {
        /* signed or unsigned argument received */
        if (type_info & DLT_TYPE_INFO_VARI)
        {
            DLT_MSG_READ_VALUE(length2_tmp,*ptr,*datalength,uint16_t);
            if((*datalength)<0)
                return -1;
            length2=DLT_ENDIAN_GET_16(msg->standardheader->htyp, length2_tmp);
            DLT_MSG_READ_VALUE(length3_tmp,*ptr,*datalength,uint16_t);
            if((*datalength)<0)
                return -1;
            length3=DLT_ENDIAN_GET_16(msg->standardheader->htyp, length3_tmp);
            if((*datalength)<length2)
                return -1;
            *ptr += length2;
            *datalength-=length2;
            if((*datalength)<length3)
                return -1;
            *ptr += length3;
            *datalength-=length3;
        }
        if (type_info & DLT_TYPE_INFO_FIXP)
        {
        	quantisation_tmp=quantisation_tmp; // prevent compiler warning
        	DLT_MSG_READ_VALUE(quantisation_tmp,*ptr,*datalength,uint32_t);

            if((*datalength)<0)
                return -1;
            switch (	type_info & DLT_TYPE_INFO_TYLE)
            {
				case DLT_TYLE_8BIT:
				case DLT_TYLE_16BIT:
				case DLT_TYLE_32BIT:
				{
                    if((*datalength)<4)
                        return -1;
                    *ptr += 4;
					*datalength-=4;
					break;
				}
				case DLT_TYLE_64BIT:
				{
                    if((*datalength)<8)
                        return -1;
                    *ptr += 8;
					*datalength-=8;
					break;
				}
				case DLT_TYLE_128BIT:
				{
                    if((*datalength)<16)
                        return -1;
                    *ptr += 16;
					*datalength-=16;
					break;
				}
				default:
				{
					return -1;
				}
            }
        }
        switch (	type_info & DLT_TYPE_INFO_TYLE)
        {
			case DLT_TYLE_8BIT:
			{
				if (type_info & DLT_TYPE_INFO_SINT)
				{
					value8i=0;
					DLT_MSG_READ_VALUE(value8i,*ptr,*datalength,int8_t); /* No endian conversion necessary */
                    if((*datalength)<0)
                        return -1;
                    snprintf(text+strlen(text),textlength-strlen(text),"%d",value8i);
				}
				else
				{
					value8u=0;
					DLT_MSG_READ_VALUE(value8u,*ptr,*datalength,uint8_t); /* No endian conversion necessary */
                    if((*datalength)<0)
                        return -1;
                    snprintf(text+strlen(text),textlength-strlen(text),"%d",value8u);
				}
				break;
			}
			case DLT_TYLE_16BIT:
			{
				if (type_info & DLT_TYPE_INFO_SINT)
				{
					value16i=0;
					value16i_tmp=0;
					DLT_MSG_READ_VALUE(value16i_tmp,*ptr,*datalength,int16_t);
                    if((*datalength)<0)
                        return -1;
                    value16i=DLT_ENDIAN_GET_16(msg->standardheader->htyp, value16i_tmp);
					snprintf(text+strlen(text),textlength-strlen(text),"%hd",value16i);
				}
				else
				{
					value16u=0;
					value16u_tmp=0;
					DLT_MSG_READ_VALUE(value16u_tmp,*ptr,*datalength,uint16_t);
                    if((*datalength)<0)
                        return -1;
                    value16u=DLT_ENDIAN_GET_16(msg->standardheader->htyp, value16u_tmp);
					snprintf(text+strlen(text),textlength-strlen(text),"%hu",value16u);
				}
				break;
			}
			case DLT_TYLE_32BIT:
			{
				if (type_info & DLT_TYPE_INFO_SINT)
				{
					value32i=0;
					value32i_tmp=0;
					DLT_MSG_READ_VALUE(value32i_tmp,*ptr,*datalength,int32_t);
                    if((*datalength)<0)
                        return -1;
                    value32i=DLT_ENDIAN_GET_32(msg->standardheader->htyp, (uint32_t)value32i_tmp);
					snprintf(text+strlen(text),textlength-strlen(text),"%d",value32i);
				}
				else
				{
					value32u=0;
					value32u_tmp=0;
					DLT_MSG_READ_VALUE(value32u_tmp,*ptr,*datalength,uint32_t);
                    if((*datalength)<0)
                        return -1;
                    value32u=DLT_ENDIAN_GET_32(msg->standardheader->htyp, value32u_tmp);
					snprintf(text+strlen(text),textlength-strlen(text),"%u",value32u);
				}
				break;
			}
			case DLT_TYLE_64BIT:
			{
				if (type_info & DLT_TYPE_INFO_SINT)
				{
					value64i=0;
					value64i_tmp=0;
					DLT_MSG_READ_VALUE(value64i_tmp,*ptr,*datalength,int64_t);
                    if((*datalength)<0)
                        return -1;
                    value64i=DLT_ENDIAN_GET_64(msg->standardheader->htyp, (uint64_t)value64i_tmp);
	#if defined (__WIN32__) && !defined(_MSC_VER)
					snprintf(text+strlen(text),textlength-strlen(text),"%I64d",value64i);
	#else
					snprintf(text+strlen(text),textlength-strlen(text),"%lld",value64i);
	#endif
				}
				else
				{
					value64u=0;
					value64u_tmp=0;
					DLT_MSG_READ_VALUE(value64u_tmp,*ptr,*datalength,uint64_t);
                    if((*datalength)<0)
                        return -1;
                    value64u=DLT_ENDIAN_GET_64(msg->standardheader->htyp, value64u_tmp);
	#if defined (__WIN32__) && !defined(_MSC_VER)
					snprintf(text+strlen(text),textlength-strlen(text),"%I64u",value64u);
	#else
					snprintf(text+strlen(text),textlength-strlen(text),"%llu",value64u);
	#endif
				}
				break;
			}
			case DLT_TYLE_128BIT:
			{
				if (*datalength>=16)
					dlt_print_hex_string(text+strlen(text),textlength,*ptr,16);
                if((*datalength)<16)
                    return -1;
                *ptr += 16;
				*datalength-=16;
				break;
			}
			default:
			{
				return -1;
			}
        }
    }
    else if (type_info & DLT_TYPE_INFO_FLOA)
    {
        /* float data argument */
        if (type_info & DLT_TYPE_INFO_VARI)
        {
            DLT_MSG_READ_VALUE(length2_tmp,*ptr,*datalength,uint16_t);
            if((*datalength)<0)
                return -1;
            length2=DLT_ENDIAN_GET_16(msg->standardheader->htyp, length2_tmp);
            DLT_MSG_READ_VALUE(length3_tmp,*ptr,*datalength,uint16_t);
            if((*datalength)<0)
                return -1;
            length3=DLT_ENDIAN_GET_16(msg->standardheader->htyp, length3_tmp);
            if((*datalength)<length2)
                return -1;
            *ptr += length2;
            *datalength-=length2;
            if((*datalength)<length3)
                return -1;
            *ptr += length3;
            *datalength-=length3;
        }
        switch (	type_info & DLT_TYPE_INFO_TYLE)
        {
			case DLT_TYLE_8BIT:
			{
				if (*datalength>=1)
					dlt_print_hex_string(text+strlen(text),textlength,*ptr,1);
                if((*datalength)<1)
                    return -1;
                *ptr += 1;
				*datalength-=1;
				break;
			}
			case DLT_TYLE_16BIT:
			{
				if (*datalength>=2)
					dlt_print_hex_string(text+strlen(text),textlength,*ptr,2);
                if((*datalength)<2)
                    return -1;
                *ptr += 2;
				*datalength-=2;
				break;
			}
			case DLT_TYLE_32BIT:
			{
				if (sizeof(float32_t)==4)
				{
					value32f=0;
					value32f_tmp=0;
					value32f_tmp_int32i=0;
					value32f_tmp_int32i_swaped=0;
					DLT_MSG_READ_VALUE(value32f_tmp,*ptr,*datalength,float32_t);
                    if((*datalength)<0)
                        return -1;
                    memcpy(&value32f_tmp_int32i,&value32f_tmp,sizeof(float32_t));
					value32f_tmp_int32i_swaped=DLT_ENDIAN_GET_32(msg->standardheader->htyp, (uint32_t)value32f_tmp_int32i);
					memcpy(&value32f,&value32f_tmp_int32i_swaped,sizeof(float32_t));
					snprintf(text+strlen(text),textlength-strlen(text),"%g",value32f);
				}
				else
				{
					dlt_log(LOG_ERR, "Invalid size of float32_t\n");
					return -1;
				}
				break;
			}
			case DLT_TYLE_64BIT:
			{
				if (sizeof(float64_t)==8)
				{
					value64f=0;
					value64f_tmp=0;
					value64f_tmp_int64i=0;
					value64f_tmp_int64i_swaped=0;
					DLT_MSG_READ_VALUE(value64f_tmp,*ptr,*datalength,float64_t);
                    if((*datalength)<0)
                        return -1;
                    memcpy(&value64f_tmp_int64i,&value64f_tmp,sizeof(float64_t));
					value64f_tmp_int64i_swaped=DLT_ENDIAN_GET_64(msg->standardheader->htyp, (uint64_t)value64f_tmp_int64i);
					memcpy(&value64f,&value64f_tmp_int64i_swaped,sizeof(float64_t));
#ifdef __arm__
					snprintf(text+strlen(text),textlength-strlen(text),"ILLEGAL");
#else
					snprintf(text+strlen(text),textlength-strlen(text),"%g",value64f);
#endif
				}
				else
				{
					dlt_log(LOG_ERR, "Invalid size of float64_t\n");
					return -1;
				}
				break;
			}
			case DLT_TYLE_128BIT:
			{
				if (*datalength>=16)
					dlt_print_hex_string(text+strlen(text),textlength,*ptr,16);
                if((*datalength)<16)
                    return -1;
                *ptr += 16;
				*datalength-=16;
				break;
			}
			default:
			{
				return -1;
			}
        }

    }
    else if (type_info & DLT_TYPE_INFO_RAWD)
    {
        /* raw data argument */
        DLT_MSG_READ_VALUE(length_tmp,*ptr,*datalength,uint16_t);
        if((*datalength)<0)
            return -1;
        length=DLT_ENDIAN_GET_16(msg->standardheader->htyp, length_tmp);
        if (type_info & DLT_TYPE_INFO_VARI)
        {
            DLT_MSG_READ_VALUE(length2_tmp,*ptr,*datalength,uint16_t);
            if((*datalength)<0)
                return -1;
            length2=DLT_ENDIAN_GET_16(msg->standardheader->htyp, length2_tmp);
            if((*datalength)<length2)
                return -1;
            *ptr += length2;
            *datalength-=length2;
        }
        if((*datalength)<length)
            return -1;
        dlt_print_hex_string(text+strlen(text),textlength,*ptr,length);
        *ptr+=length;
        *datalength-=length;
    }
    else if (type_info & DLT_TYPE_INFO_TRAI)
    {
        /* trace info argument */
        DLT_MSG_READ_VALUE(length_tmp,*ptr,*datalength,uint16_t);
        if((*datalength)<0)
            return -1;
        length=DLT_ENDIAN_GET_16(msg->standardheader->htyp, length_tmp);
        DLT_MSG_READ_STRING((text+strlen(text)),*ptr,*datalength,length);
        if((*datalength)<0)
            return -1;
    }
    else
    {
        return -1;
    }

    if (*datalength<0)
    {
        dlt_log(LOG_ERR, "Payload of DLT message corrupted\n");
        return -1;
    }

    return 0;
}

void dlt_check_envvar()
{
    char* env_log_filename = getenv("DLT_LOG_FILENAME");

    if( env_log_filename != NULL )
    {
        dlt_log_set_filename(env_log_filename);
    }

    char* env_log_level_str = getenv("DLT_LOG_LEVEL");

	if( env_log_level_str != NULL )
	{
		int level = 0;
		if( sscanf(env_log_level_str, "%d", &level) )
		{
			dlt_log_set_level(level);
		}
	}

    char* env_log_mode = getenv("DLT_LOG_MODE");

	if( env_log_mode != NULL )
	{
		int mode = 0;
		if( sscanf(env_log_mode, "%d", &mode) )
		{
			dlt_log_init(mode);
		}
	}


}