/
blf.c
2113 lines (1754 loc) · 79.7 KB
/
blf.c
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
/* blf.c
*
* Wiretap Library
* Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
*
* File format support for the Binary Log File (BLF) file format from
* Vector Informatik decoder
* Copyright (c) 2021-2022 by Dr. Lars Voelker <lars.voelker@technica-engineering.de>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
/*
* The following was used as a reference for the file format:
* https://bitbucket.org/tobylorenz/vector_blf
* The repo above includes multiple examples files as well.
*/
#include <config.h>
#include "blf.h"
#include <epan/dissectors/packet-socketcan.h>
#include <string.h>
#include <errno.h>
#include <epan/value_string.h>
#include <wsutil/wslog.h>
#include "file_wrappers.h"
#include "wtap-int.h"
#ifdef HAVE_ZLIB
#define ZLIB_CONST
#include <zlib.h>
#endif /* HAVE_ZLIB */
static const guint8 blf_magic[] = { 'L', 'O', 'G', 'G' };
static const guint8 blf_obj_magic[] = { 'L', 'O', 'B', 'J' };
static int blf_file_type_subtype = -1;
void register_blf(void);
static gboolean blf_read(wtap *wth, wtap_rec *rec, Buffer *buf, int *err, gchar **err_info, gint64 *data_offset);
static gboolean blf_seek_read(wtap *wth, gint64 seek_off, wtap_rec* rec, Buffer *buf, int *err, gchar **err_info);
static void blf_close(wtap *wth);
/*
* The virtual buffer looks like this (skips all headers):
* uncompressed log container data
* uncompressed log container data
* ...
*
* The "real" positions, length, etc. reference this layout and not the file.
* When no compression is used the file is accessed directly.
*/
typedef struct blf_log_container {
gint64 infile_start_pos; /* start position of log container in file */
guint64 infile_length; /* length of log container in file */
guint64 infile_data_start; /* start position of data in log container in file */
gint64 real_start_pos; /* decompressed (virtual) start position including header */
guint64 real_length; /* decompressed length */
gint64 real_first_object_pos; /* where does the first obj start? */
guint64 real_leftover_bytes; /* how many bytes are left over for the next container? */
guint16 compression_method; /* 0: uncompressed, 2: zlib */
unsigned char *real_data; /* cache for decompressed data */
} blf_log_container_t;
typedef struct blf_data {
gint64 start_of_last_obj;
gint64 current_real_seek_pos;
guint64 start_offset_ns;
guint current_log_container;
GArray *log_containers;
GHashTable *channel_to_iface_ht;
guint32 next_interface_id;
} blf_t;
typedef struct blf_params {
wtap *wth;
wtap_rec *rec;
Buffer *buf;
FILE_T fh;
blf_t *blf_data;
} blf_params_t;
typedef struct blf_channel_to_iface_entry {
int pkt_encap;
guint32 channel;
guint32 interface_id;
} blf_channel_to_iface_entry_t;
static void
blf_free_key(gpointer key) {
g_free(key);
}
static void
blf_free_channel_to_iface_entry(gpointer data) {
g_free(data);
}
static gint64
blf_calc_key_value(int pkt_encap, guint32 channel) {
return ((gint64)pkt_encap << 32) | channel;
}
static void add_interface_name(wtap_block_t *int_data, int pkt_encap, guint32 channel, gchar *name) {
if (name != NULL) {
wtap_block_add_string_option_format(*int_data, OPT_IDB_NAME, name, channel);
} else {
switch (pkt_encap) {
case WTAP_ENCAP_ETHERNET:
wtap_block_add_string_option_format(*int_data, OPT_IDB_NAME, "ETH-%d", channel);
break;
case WTAP_ENCAP_IEEE_802_11:
wtap_block_add_string_option_format(*int_data, OPT_IDB_NAME, "WLAN-%d", channel);
break;
case WTAP_ENCAP_FLEXRAY:
wtap_block_add_string_option_format(*int_data, OPT_IDB_NAME, "FR-%d", channel);
break;
case WTAP_ENCAP_LIN:
wtap_block_add_string_option_format(*int_data, OPT_IDB_NAME, "LIN-%d", channel);
break;
case WTAP_ENCAP_SOCKETCAN:
wtap_block_add_string_option_format(*int_data, OPT_IDB_NAME, "CAN-%d", channel);
break;
default:
wtap_block_add_string_option_format(*int_data, OPT_IDB_NAME, "0x%04x-%d", pkt_encap, channel);
}
}
}
static guint32
blf_add_interface(blf_params_t *params, int pkt_encap, guint32 channel, gchar *name) {
wtap_block_t int_data = wtap_block_create(WTAP_BLOCK_IF_ID_AND_INFO);
wtapng_if_descr_mandatory_t *if_descr_mand = (wtapng_if_descr_mandatory_t*)wtap_block_get_mandatory_data(int_data);
blf_channel_to_iface_entry_t *item = NULL;
if_descr_mand->wtap_encap = pkt_encap;
add_interface_name(&int_data, pkt_encap, channel, name);
if_descr_mand->time_units_per_second = 1000 * 1000 * 1000;
if_descr_mand->tsprecision = WTAP_TSPREC_NSEC;
wtap_block_add_uint8_option(int_data, OPT_IDB_TSRESOL, 9);
if_descr_mand->snap_len = WTAP_MAX_PACKET_SIZE_STANDARD;
if_descr_mand->num_stat_entries = 0;
if_descr_mand->interface_statistics = NULL;
wtap_add_idb(params->wth, int_data);
if (params->wth->file_encap == WTAP_ENCAP_UNKNOWN) {
params->wth->file_encap = if_descr_mand->wtap_encap;
} else {
if (params->wth->file_encap != if_descr_mand->wtap_encap) {
params->wth->file_encap = WTAP_ENCAP_PER_PACKET;
}
}
gint64 *key = NULL;
key = g_new(gint64, 1);
*key = blf_calc_key_value(pkt_encap, channel);
item = g_new(blf_channel_to_iface_entry_t, 1);
item->channel = channel;
item->pkt_encap = pkt_encap;
item->interface_id = params->blf_data->next_interface_id++;
g_hash_table_insert(params->blf_data->channel_to_iface_ht, key, item);
return item->interface_id;
}
static guint32
blf_lookup_interface(blf_params_t *params, int pkt_encap, guint32 channel, gchar *name) {
gint64 key = blf_calc_key_value(pkt_encap, channel);
blf_channel_to_iface_entry_t *item = NULL;
if (params->blf_data->channel_to_iface_ht == NULL) {
return 0;
}
item = (blf_channel_to_iface_entry_t *)g_hash_table_lookup(params->blf_data->channel_to_iface_ht, &key);
if (item != NULL) {
return item->interface_id;
} else {
return blf_add_interface(params, pkt_encap, channel, name);
}
}
static void
fix_endianness_blf_date(blf_date_t *date) {
date->year = GUINT16_FROM_LE(date->year);
date->month = GUINT16_FROM_LE(date->month);
date->dayofweek = GUINT16_FROM_LE(date->dayofweek);
date->day = GUINT16_FROM_LE(date->day);
date->hour = GUINT16_FROM_LE(date->hour);
date->mins = GUINT16_FROM_LE(date->mins);
date->sec = GUINT16_FROM_LE(date->sec);
date->ms = GUINT16_FROM_LE(date->ms);
}
static void
fix_endianness_blf_fileheader(blf_fileheader_t *header) {
header->header_length = GUINT32_FROM_LE(header->header_length);
header->len_compressed = GUINT64_FROM_LE(header->len_compressed);
header->len_uncompressed = GUINT64_FROM_LE(header->len_uncompressed);
header->obj_count = GUINT32_FROM_LE(header->obj_count);
header->obj_read = GUINT32_FROM_LE(header->obj_read);
fix_endianness_blf_date(&(header->start_date));
fix_endianness_blf_date(&(header->end_date));
header->length3 = GUINT32_FROM_LE(header->length3);
}
static void
fix_endianness_blf_blockheader(blf_blockheader_t *header) {
header->header_length = GUINT16_FROM_LE(header->header_length);
header->header_type = GUINT16_FROM_LE(header->header_type);
header->object_length = GUINT32_FROM_LE(header->object_length);
header->object_type = GUINT32_FROM_LE(header->object_type);
}
static void
fix_endianness_blf_logcontainerheader(blf_logcontainerheader_t *header) {
header->compression_method = GUINT16_FROM_LE(header->compression_method);
header->res1 = GUINT16_FROM_LE(header->res1);
header->res2 = GUINT32_FROM_LE(header->res2);
header->uncompressed_size = GUINT32_FROM_LE(header->uncompressed_size);
header->res4 = GUINT32_FROM_LE(header->res4);
}
static void
fix_endianness_blf_logobjectheader(blf_logobjectheader_t *header) {
header->flags = GUINT32_FROM_LE(header->flags);
header->client_index = GUINT16_FROM_LE(header->client_index);
header->object_version = GUINT16_FROM_LE(header->object_version);
header->object_timestamp = GUINT64_FROM_LE(header->object_timestamp);
}
static void
fix_endianness_blf_logobjectheader2(blf_logobjectheader2_t *header) {
header->flags = GUINT32_FROM_LE(header->flags);
header->object_version = GUINT16_FROM_LE(header->object_version);
header->object_timestamp = GUINT64_FROM_LE(header->object_timestamp);
header->original_timestamp = GUINT64_FROM_LE(header->object_timestamp);
}
static void
fix_endianness_blf_logobjectheader3(blf_logobjectheader3_t *header) {
header->flags = GUINT32_FROM_LE(header->flags);
header->static_size = GUINT16_FROM_LE(header->static_size);
header->object_version = GUINT16_FROM_LE(header->object_version);
header->object_timestamp = GUINT64_FROM_LE(header->object_timestamp);
}
static void
fix_endianness_blf_ethernetframeheader(blf_ethernetframeheader_t *header) {
header->channel = GUINT16_FROM_LE(header->channel);
header->direction = GUINT16_FROM_LE(header->direction);
header->ethtype = GUINT16_FROM_LE(header->ethtype);
header->tpid = GUINT16_FROM_LE(header->tpid);
header->tci = GUINT16_FROM_LE(header->tci);
header->payloadlength = GUINT16_FROM_LE(header->payloadlength);
}
static void
fix_endianness_blf_ethernetframeheader_ex(blf_ethernetframeheader_ex_t *header) {
header->struct_length = GUINT16_FROM_LE(header->struct_length);
header->flags = GUINT16_FROM_LE(header->flags);
header->channel = GUINT16_FROM_LE(header->channel);
header->hw_channel = GUINT16_FROM_LE(header->hw_channel);
header->frame_duration = GUINT64_FROM_LE(header->frame_duration);
header->frame_checksum = GUINT32_FROM_LE(header->frame_checksum);
header->direction = GUINT16_FROM_LE(header->direction);
header->frame_length = GUINT16_FROM_LE(header->frame_length);
header->frame_handle = GUINT32_FROM_LE(header->frame_handle);
header->error = GUINT32_FROM_LE(header->error);
}
static void
fix_endianness_blf_wlanframeheader(blf_wlanframeheader_t* header) {
header->channel = GUINT16_FROM_LE(header->channel);
header->flags = GUINT16_FROM_LE(header->flags);
header->signal_strength = GUINT16_FROM_LE(header->signal_strength);
header->signal_quality = GUINT16_FROM_LE(header->signal_quality);
header->frame_length = GUINT16_FROM_LE(header->frame_length);
}
static void
fix_endianness_blf_canmessage(blf_canmessage_t *header) {
header->channel = GUINT16_FROM_LE(header->channel);
header->id = GUINT32_FROM_LE(header->id);
}
static void
fix_endianness_blf_canmessage2_trailer(blf_canmessage2_trailer_t *header) {
header->frameLength_in_ns = GUINT32_FROM_LE(header->frameLength_in_ns);
header->reserved2 = GUINT16_FROM_LE(header->reserved1);
}
static void
fix_endianness_blf_canfdmessage(blf_canfdmessage_t *header) {
header->channel = GUINT16_FROM_LE(header->channel);
header->id = GUINT32_FROM_LE(header->id);
header->frameLength_in_ns = GUINT32_FROM_LE(header->frameLength_in_ns);
header->reservedCanFdMessage2 = GUINT32_FROM_LE(header->reservedCanFdMessage2);
}
static void
fix_endianness_blf_canfdmessage64(blf_canfdmessage64_t *header) {
header->id = GUINT32_FROM_LE(header->id);
header->frameLength_in_ns = GUINT32_FROM_LE(header->frameLength_in_ns);
header->flags = GUINT32_FROM_LE(header->flags);
header->btrCfgArb = GUINT32_FROM_LE(header->btrCfgArb);
header->btrCfgData = GUINT32_FROM_LE(header->btrCfgData);
header->timeOffsetBrsNs = GUINT32_FROM_LE(header->timeOffsetBrsNs);
header->timeOffsetCrcDelNs = GUINT32_FROM_LE(header->timeOffsetCrcDelNs);
header->bitCount = GUINT16_FROM_LE(header->bitCount);
header->crc = GUINT32_FROM_LE(header->crc);
}
static void
fix_endianness_blf_flexraydata(blf_flexraydata_t *header) {
header->channel = GUINT16_FROM_LE(header->channel);
header->messageId = GUINT16_FROM_LE(header->messageId);
header->crc = GUINT16_FROM_LE(header->crc);
header->reservedFlexRayData2 = GUINT16_FROM_LE(header->reservedFlexRayData2);
}
static void
fix_endianness_blf_flexraymessage(blf_flexraymessage_t *header) {
header->channel = GUINT16_FROM_LE(header->channel);
header->fpgaTick = GUINT32_FROM_LE(header->fpgaTick);
header->fpgaTickOverflow = GUINT32_FROM_LE(header->fpgaTickOverflow);
header->clientIndexFlexRayV6Message = GUINT32_FROM_LE(header->clientIndexFlexRayV6Message);
header->clusterTime = GUINT32_FROM_LE(header->clusterTime);
header->frameId = GUINT16_FROM_LE(header->frameId);
header->headerCrc = GUINT16_FROM_LE(header->headerCrc);
header->frameState = GUINT16_FROM_LE(header->frameState);
header->reservedFlexRayV6Message2 = GUINT16_FROM_LE(header->reservedFlexRayV6Message2);
}
static void
fix_endianness_blf_flexrayrcvmessage(blf_flexrayrcvmessage_t *header) {
header->channel = GUINT16_FROM_LE(header->channel);
header->version = GUINT16_FROM_LE(header->version);
header->channelMask = GUINT16_FROM_LE(header->channelMask);
header->dir = GUINT16_FROM_LE(header->dir);
header->clientIndex = GUINT32_FROM_LE(header->clientIndex);
header->clusterNo = GUINT32_FROM_LE(header->clusterNo);
header->frameId = GUINT16_FROM_LE(header->frameId);
header->headerCrc1 = GUINT16_FROM_LE(header->headerCrc1);
header->headerCrc2 = GUINT16_FROM_LE(header->headerCrc2);
header->payloadLength = GUINT16_FROM_LE(header->payloadLength);
header->payloadLengthValid = GUINT16_FROM_LE(header->payloadLengthValid);
header->cycle = GUINT16_FROM_LE(header->cycle);
header->tag = GUINT32_FROM_LE(header->tag);
header->data = GUINT32_FROM_LE(header->data);
header->frameFlags = GUINT32_FROM_LE(header->frameFlags);
header->appParameter = GUINT32_FROM_LE(header->appParameter);
/* this would be extra for ext format:
header->frameCRC = GUINT32_FROM_LE(header->frameCRC);
header->frameLengthInNs = GUINT32_FROM_LE(header->frameLengthInNs);
header->frameId1 = GUINT16_FROM_LE(header->frameId1);
header->pduOffset = GUINT16_FROM_LE(header->pduOffset);
header->blfLogMask = GUINT16_FROM_LE(header->blfLogMask);
*/
}
static void
fix_endianness_blf_linmessage(blf_linmessage_t *header) {
header->channel = GUINT16_FROM_LE(header->channel);
}
static void
fix_endianness_blf_linmessage_trailer(blf_linmessage_trailer_t *header) {
header->crc = GUINT16_FROM_LE(header->crc);
header->res2 = GUINT32_FROM_LE(header->res2);
}
static void
fix_endianness_blf_apptext_header(blf_apptext_t *header) {
header->source = GUINT32_FROM_LE(header->source);
header->reservedAppText1 = GUINT32_FROM_LE(header->reservedAppText1);
header->textLength = GUINT32_FROM_LE(header->textLength);
header->reservedAppText2 = GUINT32_FROM_LE(header->reservedAppText2);
}
static guint64
blf_timestamp_to_ns(guint32 flags, guint64 timestamp) {
switch (flags) {
case BLF_TIMESTAMP_RESOLUTION_10US:
return 10000 * timestamp;
break;
case BLF_TIMESTAMP_RESOLUTION_1NS:
return timestamp;
break;
default:
/*
* XXX - report this as an error?
*
* Or provide a mechanism to allow file readers to report
* a warning (an error that the the reader tries to work
* around and that the caller should report)?
*/
ws_debug("I don't understand the flags 0x%x", flags);
return 0;
break;
}
}
static void
blf_init_logcontainer(blf_log_container_t *tmp) {
tmp->infile_start_pos = 0;
tmp->infile_length = 0;
tmp->infile_data_start = 0;
tmp->real_start_pos = 0;
tmp->real_length = 0;
tmp->real_first_object_pos = -1;
tmp->real_leftover_bytes = G_MAXUINT64;
tmp->real_data = NULL;
tmp->compression_method = 0;
}
static void
blf_add_logcontainer(blf_t *blf_data, blf_log_container_t log_container) {
if (blf_data->log_containers == NULL) {
blf_data->log_containers = g_array_sized_new(FALSE, FALSE, sizeof(blf_log_container_t), 1);
blf_data->current_log_container = 0;
} else {
blf_data->current_log_container++;
}
g_array_append_val(blf_data->log_containers, log_container);
}
static gboolean
blf_get_logcontainer_by_index(blf_t *blf_data, guint container_index, blf_log_container_t **ret) {
if (blf_data == NULL || blf_data->log_containers == NULL || container_index >= blf_data->log_containers->len) {
return FALSE;
}
*ret = &g_array_index(blf_data->log_containers, blf_log_container_t, container_index);
return TRUE;
}
static gboolean
blf_find_logcontainer_for_address(blf_t *blf_data, gint64 pos, blf_log_container_t **container, gint *container_index) {
blf_log_container_t *tmp;
if (blf_data == NULL || blf_data->log_containers == NULL) {
return FALSE;
}
for (guint i = 0; i < blf_data->log_containers->len; i++) {
tmp = &g_array_index(blf_data->log_containers, blf_log_container_t, i);
if (tmp->real_start_pos <= pos && pos < tmp->real_start_pos + (gint64)tmp->real_length) {
*container = tmp;
*container_index = i;
return TRUE;
}
}
return FALSE;
}
static gboolean
blf_pull_logcontainer_into_memory(blf_params_t *params, guint index_log_container, int *err, gchar **err_info) {
blf_t *blf_data = params->blf_data;
blf_log_container_t tmp;
if (index_log_container >= blf_data->log_containers->len) {
/*
* XXX - does this represent a bug (WTAP_ERR_INTERNAL) or a
* malformed file (WTAP_ERR_BAD_FILE)?
*/
*err = WTAP_ERR_INTERNAL;
*err_info = ws_strdup_printf("blf_pull_logcontainer_into_memory: index_log_container (%u) >= blf_data->log_containers->len (%u)",
index_log_container, blf_data->log_containers->len);
return FALSE;
}
tmp = g_array_index(blf_data->log_containers, blf_log_container_t, index_log_container);
if (tmp.real_data != NULL) {
return TRUE;
}
if (tmp.compression_method == BLF_COMPRESSION_ZLIB) {
#ifdef HAVE_ZLIB
if (file_seek(params->fh, tmp.infile_data_start, SEEK_SET, err) == -1) {
return FALSE;
}
/* pull compressed data into buffer */
unsigned char *compressed_data = g_try_malloc0((gsize)tmp.infile_length);
if (tmp.infile_start_pos < 0) {
/*
* XXX - does this represent a bug (WTAP_ERR_INTERNAL) or a
* malformed file (WTAP_ERR_BAD_FILE)?
*/
*err = WTAP_ERR_INTERNAL;
*err_info = ws_strdup_printf("blf_pull_logcontainer_into_memory: tmp.infile_start_pos (%" G_GINT64_FORMAT ") < 0",
tmp.infile_start_pos);
return FALSE;
}
if (tmp.infile_data_start < (guint64)tmp.infile_start_pos) {
/*
* XXX - does this represent a bug (WTAP_ERR_INTERNAL) or a
* malformed file (WTAP_ERR_BAD_FILE)?
*/
*err = WTAP_ERR_INTERNAL;
*err_info = ws_strdup_printf("blf_pull_logcontainer_into_memory: tmp.infile_data_start (%" G_GUINT64_FORMAT ") < tmp.infile_start_pos (%" G_GINT64_FORMAT ")",
tmp.infile_data_start, tmp.infile_start_pos);
return FALSE;
}
if (tmp.infile_length < tmp.infile_data_start - (guint64)tmp.infile_start_pos) {
/*
* XXX - does this represent a bug (WTAP_ERR_INTERNAL) or a
* malformed file (WTAP_ERR_BAD_FILE)?
*/
*err = WTAP_ERR_INTERNAL;
*err_info = ws_strdup_printf("blf_pull_logcontainer_into_memory: tmp.infile_length (%" G_GUINT64_FORMAT ") < (tmp.infile_data_start (%" G_GUINT64_FORMAT ") - tmp.infile_start_pos (%" G_GINT64_FORMAT ")) = %" G_GUINT64_FORMAT,
tmp.infile_length,
tmp.infile_data_start, tmp.infile_start_pos,
tmp.infile_data_start - (guint64)tmp.infile_start_pos);
return FALSE;
}
guint64 data_length = tmp.infile_length - (tmp.infile_data_start - (guint64)tmp.infile_start_pos);
if (data_length > UINT_MAX) {
/*
* XXX - does this represent a bug (WTAP_ERR_INTERNAL) or a
* malformed file (WTAP_ERR_BAD_FILE)?
*/
*err = WTAP_ERR_INTERNAL;
*err_info = ws_strdup_printf("blf_pull_logcontainer_into_memory: data_length (%" G_GUINT64_FORMAT ") > UINT_MAX",
data_length);
return FALSE;
}
if (!wtap_read_bytes_or_eof(params->fh, compressed_data, (unsigned int)data_length, err, err_info)) {
return FALSE;
}
unsigned char *buf = g_try_malloc0((gsize)tmp.real_length);
z_stream infstream = {0};
infstream.avail_in = (unsigned int)data_length;
infstream.next_in = compressed_data;
infstream.avail_out = (unsigned int)tmp.real_length;
infstream.next_out = buf;
/* the actual DE-compression work. */
if (Z_OK != inflateInit(&infstream)) {
/*
* XXX - check the error code and handle this appropriately.
*/
*err = WTAP_ERR_INTERNAL;
if (infstream.msg != NULL) {
*err_info = ws_strdup_printf("blf_pull_logcontainer_into_memory: inflateInit failed for LogContainer %d, message\"%s\"",
index_log_container,
infstream.msg);
} else {
*err_info = ws_strdup_printf("blf_pull_logcontainer_into_memory: inflateInit failed for LogContainer %d",
index_log_container);
}
ws_debug("inflateInit failed for LogContainer %d", index_log_container);
if (infstream.msg != NULL) {
ws_debug("inflateInit returned: \"%s\"", infstream.msg);
}
return FALSE;
}
int ret = inflate(&infstream, Z_NO_FLUSH);
/* Z_OK should not happen here since we know how big the buffer should be */
if (Z_STREAM_END != ret) {
switch (ret) {
case Z_NEED_DICT:
*err = WTAP_ERR_DECOMPRESS;
*err_info = ws_strdup("preset dictionary needed");
break;
case Z_STREAM_ERROR:
*err = WTAP_ERR_DECOMPRESS;
*err_info = (infstream.msg != NULL) ? ws_strdup(infstream.msg) : NULL;
break;
case Z_MEM_ERROR:
/* This means "not enough memory". */
*err = ENOMEM;
*err_info = NULL;
break;
case Z_DATA_ERROR:
/* This means "deflate stream invalid" */
*err = WTAP_ERR_DECOMPRESS;
*err_info = (infstream.msg != NULL) ? ws_strdup(infstream.msg) : NULL;
break;
case Z_BUF_ERROR:
/* XXX - this is recoverable; what should we do here? */
*err = WTAP_ERR_INTERNAL;
*err_info = ws_strdup_printf("blf_pull_logcontainer_into_memory: Z_BUF_ERROR from inflate(), message \"%s\"",
(infstream.msg != NULL) ? infstream.msg : "(none)");
break;
case Z_VERSION_ERROR:
*err = WTAP_ERR_INTERNAL;
*err_info = ws_strdup_printf("blf_pull_logcontainer_into_memory: Z_VERSION_ERROR from inflate(), message \"%s\"",
(infstream.msg != NULL) ? infstream.msg : "(none)");
break;
default:
*err = WTAP_ERR_INTERNAL;
*err_info = ws_strdup_printf("blf_pull_logcontainer_into_memory: unexpected error %d from inflate(), message \"%s\"",
ret,
(infstream.msg != NULL) ? infstream.msg : "(none)");
break;
}
ws_debug("inflate failed (return code %d) for LogContainer %d", ret, index_log_container);
if (infstream.msg != NULL) {
ws_debug("inflate returned: \"%s\"", infstream.msg);
}
return FALSE;
}
if (Z_OK != inflateEnd(&infstream)) {
/* Returns either Z_OK or Z_STREAM_ERROR. */
*err = WTAP_ERR_DECOMPRESS;
*err_info = (infstream.msg != NULL) ? ws_strdup(infstream.msg) : NULL;
ws_debug("inflateEnd failed for LogContainer %d", index_log_container);
if (infstream.msg != NULL) {
ws_debug("inflateEnd returned: \"%s\"", infstream.msg);
}
return FALSE;
}
tmp.real_data = buf;
g_array_index(blf_data->log_containers, blf_log_container_t, index_log_container) = tmp;
return TRUE;
#else
*err = WTAP_ERR_DECOMPRESSION_NOT_SUPPORTED;
*err_info = NULL;
return FALSE;
#endif
}
return FALSE;
}
static gboolean
blf_read_bytes_or_eof(blf_params_t *params, guint64 real_pos, void *target_buffer, unsigned int count, int *err, gchar **err_info) {
blf_log_container_t *start_container;
blf_log_container_t *end_container;
blf_log_container_t *current_container;
gint start_container_index = -1;
gint end_container_index = -1;
gint current_container_index = -1;
unsigned int copied = 0;
unsigned int data_left;
unsigned int start_in_buf;
unsigned char *buf = (unsigned char *)target_buffer;
if (!blf_find_logcontainer_for_address(params->blf_data, real_pos, &start_container, &start_container_index)) {
/*
* XXX - why is this treated as an EOF rather than an error?
* *err appears to be 0, which means our caller treats it as an
* EOF, at least when reading the log object header.
*/
ws_debug("cannot read data because start position cannot be mapped");
return FALSE;
}
if (!blf_find_logcontainer_for_address(params->blf_data, real_pos + count - 1, &end_container, &end_container_index)) {
/*
* XXX - why is this treated as an EOF rather than an error?
* *err appears to be 0, which means our caller treats it as an
* EOF, at least when reading the log object header.
*/
ws_debug("cannot read data because end position cannot be mapped");
return FALSE;
}
current_container_index = start_container_index;
current_container = start_container;
start_in_buf = (unsigned int)real_pos - (unsigned int)start_container->real_start_pos;
switch (start_container->compression_method) {
case BLF_COMPRESSION_NONE:
while (current_container_index <= end_container_index) {
if (!blf_get_logcontainer_by_index(params->blf_data, current_container_index, ¤t_container)) {
/*
* XXX - does this represent a bug (WTAP_ERR_INTERNAL) or a
* malformed file (WTAP_ERR_BAD_FILE)?
*/
*err = WTAP_ERR_INTERNAL;
*err_info = ws_strdup_printf("blf_read_bytes_or_eof: cannot refresh container");
ws_debug("cannot refresh container");
return FALSE;
}
data_left = (unsigned int)(current_container->real_length - start_in_buf);
if (file_seek(params->fh, current_container->infile_data_start + start_in_buf, SEEK_SET, err) < 0) {
ws_debug("cannot seek data");
return FALSE;
}
if (data_left < (count - copied)) {
if (!wtap_read_bytes_or_eof(params->fh, buf + copied, data_left, err, err_info)) {
ws_debug("cannot read data");
return FALSE;
}
copied += data_left;
current_container_index++;
start_in_buf = 0;
} else {
if (!wtap_read_bytes_or_eof(params->fh, buf + copied, count - copied, err, err_info)) {
ws_debug("cannot read data");
return FALSE;
}
return TRUE;
}
}
break;
case BLF_COMPRESSION_ZLIB:
while (current_container_index <= end_container_index) {
if (!blf_pull_logcontainer_into_memory(params, current_container_index, err, err_info)) {
return FALSE;
}
if (!blf_get_logcontainer_by_index(params->blf_data, current_container_index, ¤t_container)) {
/*
* XXX - does this represent a bug (WTAP_ERR_INTERNAL) or a
* malformed file (WTAP_ERR_BAD_FILE)?
*/
*err = WTAP_ERR_INTERNAL;
*err_info = ws_strdup_printf("blf_read_bytes_or_eof: cannot refresh container");
ws_debug("cannot refresh container");
return FALSE;
}
if (current_container->real_data == NULL) {
/*
* XXX - does this represent a bug (WTAP_ERR_INTERNAL) or a
* malformed file (WTAP_ERR_BAD_FILE)?
*/
*err = WTAP_ERR_INTERNAL;
*err_info = ws_strdup_printf("blf_read_bytes_or_eof: pulling in container failed hard");
ws_debug("pulling in container failed hard");
return FALSE;
}
data_left = (unsigned int)(current_container->real_length - start_in_buf);
if (data_left < (count - copied)) {
memcpy(buf + copied, current_container->real_data + start_in_buf, (unsigned int)data_left);
copied += data_left;
current_container_index++;
start_in_buf = 0;
} else {
memcpy(buf + copied, current_container->real_data + start_in_buf, count - copied);
return TRUE;
}
}
/*
* XXX - does this represent a bug (WTAP_ERR_INTERNAL) or a
* malformed file (WTAP_ERR_BAD_FILE)?
*/
*err = WTAP_ERR_INTERNAL;
*err_info = ws_strdup_printf("blf_read_bytes_or_eof: ran out of items in container");
return FALSE;
break;
default:
*err = WTAP_ERR_UNSUPPORTED;
*err_info = ws_strdup_printf("blf: unknown compression method %u",
start_container->compression_method);
ws_debug("unknown compression method");
return FALSE;
}
return FALSE;
}
static gboolean
blf_read_bytes(blf_params_t *params, guint64 real_pos, void *target_buffer, unsigned int count, int *err, gchar **err_info) {
if (!blf_read_bytes_or_eof(params, real_pos, target_buffer, count, err, err_info)) {
if (*err == 0) {
*err = WTAP_ERR_SHORT_READ;
}
return FALSE;
}
return TRUE;
}
/* this is only called once on open to figure out the layout of the file */
static gboolean
blf_scan_file_for_logcontainers(blf_params_t *params) {
blf_blockheader_t header;
blf_logcontainerheader_t logcontainer_header;
blf_log_container_t tmp;
int err;
gchar *err_info;
guint64 current_start_pos;
guint64 current_real_start = 0;
while (1) {
current_start_pos = file_tell(params->fh);
/* Find Object */
while (1) {
if (!wtap_read_bytes_or_eof(params->fh, &header, sizeof header, &err, &err_info)) {
ws_debug("we found end of file");
/* lets ignore some bytes at the end since some implementations think it is ok to add a few zero bytes */
if (err == WTAP_ERR_SHORT_READ) {
err = 0;
}
return TRUE;
}
fix_endianness_blf_blockheader(&header);
if (memcmp(header.magic, blf_obj_magic, sizeof(blf_obj_magic))) {
ws_debug("object magic is not LOBJ (pos: 0x%" PRIx64 ")", current_start_pos);
} else {
break;
}
/* we are moving back and try again but 1 byte later */
/* TODO: better understand how this paddings works... */
current_start_pos++;
if (file_seek(params->fh, current_start_pos, SEEK_SET, &err) < 0) {
return FALSE;
}
}
if (header.header_type != BLF_HEADER_TYPE_DEFAULT) {
ws_debug("unknown header type, I know only BLF_HEADER_TYPE_DEFAULT (1)");
return FALSE;
}
switch (header.object_type) {
case BLF_OBJTYPE_LOG_CONTAINER:
if (header.header_length < sizeof(blf_blockheader_t)) {
ws_debug("log container header length too short");
return FALSE;
}
/* skip unknown header part if needed */
if (header.header_length - sizeof(blf_blockheader_t) > 0) {
/* seek over unknown header part */
if (file_seek(params->fh, current_start_pos + header.header_length, SEEK_SET, &err) < 0) {
ws_debug("cannot seek file for skipping unknown header bytes in log container");
return FALSE;
}
}
if (!wtap_read_bytes_or_eof(params->fh, &logcontainer_header, sizeof(blf_logcontainerheader_t), &err, &err_info)) {
ws_debug("not enough bytes for log container header");
return FALSE;
}
fix_endianness_blf_logcontainerheader(&logcontainer_header);
blf_init_logcontainer(&tmp);
tmp.infile_start_pos = current_start_pos;
tmp.infile_data_start = file_tell(params->fh);
tmp.infile_length = header.object_length;
tmp.real_start_pos = current_real_start;
tmp.real_length = logcontainer_header.uncompressed_size;
tmp.compression_method = logcontainer_header.compression_method;
/* set up next start position */
current_real_start += logcontainer_header.uncompressed_size;
if (file_seek(params->fh, current_start_pos + MAX(MAX(16, header.object_length), header.header_length), SEEK_SET, &err) < 0) {
ws_debug("cannot seek file for skipping log container bytes");
return FALSE;
}
blf_add_logcontainer(params->blf_data, tmp);
break;
default:
ws_debug("we found a non BLF log container on top level. this is unexpected.");
/* TODO: maybe create "fake Log Container" for this */
if (file_seek(params->fh, current_start_pos + MAX(MAX(16, header.object_length), header.header_length), SEEK_SET, &err) < 0) {
return FALSE;
}
}
}
return TRUE;
}
static void
blf_init_rec(blf_params_t *params, guint64 object_timestamp, int pkt_encap, guint32 channel, guint caplen, guint len) {
params->rec->rec_type = REC_TYPE_PACKET;
params->rec->block = wtap_block_create(WTAP_BLOCK_PACKET);
params->rec->presence_flags = WTAP_HAS_TS | WTAP_HAS_CAP_LEN | WTAP_HAS_INTERFACE_ID;
params->rec->tsprec = WTAP_TSPREC_NSEC; /* there is no 10us, maybe we should update this */
params->rec->ts.secs = object_timestamp / (1000 * 1000 * 1000);
params->rec->ts.nsecs = object_timestamp % (1000 * 1000 * 1000);
params->rec->rec_header.packet_header.caplen = caplen;
params->rec->rec_header.packet_header.len = len;
params->rec->rec_header.packet_header.pkt_encap = pkt_encap;
params->rec->rec_header.packet_header.interface_id = blf_lookup_interface(params, pkt_encap, channel, NULL);
/* TODO: before we had to remove comments and verdict here to not leak memory but APIs have changed ... */
}
static void
blf_add_direction_option(blf_params_t *params, guint16 direction) {
guint32 tmp = 0; /* dont care */
switch (direction) {
case BLF_DIR_RX:
tmp = 1; /* inbound */
break;
case BLF_DIR_TX:
case BLF_DIR_TX_RQ:
tmp = 2; /* outbound */
break;
}
/* pcapng.c: #define OPT_EPB_FLAGS 0x0002 */
wtap_block_add_uint32_option(params->rec->block, 0x0002, tmp);
}
static gboolean
blf_read_log_object_header(blf_params_t *params, int *err, gchar **err_info, gint64 header2_start, gint64 data_start, blf_logobjectheader_t *logheader) {
if (data_start - header2_start < (gint64)sizeof(blf_logobjectheader_t)) {
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("blf: not enough bytes for log object header");
ws_debug("not enough bytes for timestamp header");
return FALSE;
}
if (!blf_read_bytes_or_eof(params, header2_start, logheader, sizeof(*logheader), err, err_info)) {
ws_debug("not enough bytes for logheader");
return FALSE;
}
fix_endianness_blf_logobjectheader(logheader);
return TRUE;
}
static gboolean
blf_read_log_object_header2(blf_params_t *params, int *err, gchar **err_info, gint64 header2_start, gint64 data_start, blf_logobjectheader2_t *logheader) {
if (data_start - header2_start < (gint64)sizeof(blf_logobjectheader2_t)) {
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("blf: not enough bytes for log object header");
ws_debug("not enough bytes for timestamp header");
return FALSE;
}
if (!blf_read_bytes_or_eof(params, header2_start, logheader, sizeof(*logheader), err, err_info)) {
ws_debug("not enough bytes for logheader");
return FALSE;
}
fix_endianness_blf_logobjectheader2(logheader);
return TRUE;
}
static gboolean
blf_read_log_object_header3(blf_params_t *params, int *err, gchar **err_info, gint64 header2_start, gint64 data_start, blf_logobjectheader3_t *logheader) {
if (data_start - header2_start < (gint64)sizeof(blf_logobjectheader3_t)) {
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("blf: not enough bytes for log object header");
ws_debug("not enough bytes for timestamp header");
return FALSE;
}
if (!blf_read_bytes_or_eof(params, header2_start, logheader, sizeof(*logheader), err, err_info)) {
ws_debug("not enough bytes for logheader");
return FALSE;
}
fix_endianness_blf_logobjectheader3(logheader);
return TRUE;
}
static gboolean
blf_read_ethernetframe(blf_params_t *params, int *err, gchar **err_info, gint64 block_start, gint64 data_start, gint64 object_length, guint64 timestamp) {
blf_ethernetframeheader_t ethheader;