@@ -10,6 +10,18 @@ with Pcapng_In_Ada.Blocks; use Pcapng_In_Ada.Blocks;
1010
1111package body Pcapng_In_Ada.Packet is
1212
13+ function Dec32
14+ (Buf : Byte_Buffer; Pos : Buffer_Index; Order : Byte_Order)
15+ return Unsigned_32
16+ with Pre => Pos >= Buf'First and then Pos <= Buf'Last - 3
17+ is
18+ begin
19+ case Order is
20+ when Little_Endian => return Decode_U32_LE (Buf, Pos);
21+ when Big_Endian => return Decode_U32_BE (Buf, Pos);
22+ end case ;
23+ end Dec32 ;
24+
1325 procedure Parse_EPB
1426 (Buf : Byte_Buffer;
1527 Pos : Buffer_Index;
@@ -21,17 +33,18 @@ package body Pcapng_In_Ada.Packet is
2133 Hdr : Block_Header;
2234 H_Nx : Natural;
2335 H_Err : Error_Kind;
24- Iface : Unsigned_32;
25- Stamp_Hi : Unsigned_32;
26- Stamp_Lo : Unsigned_32;
27- Cap : Unsigned_32;
28- Orig : Unsigned_32;
29- Frame : Natural;
36+ Ifx : Unsigned_32;
37+ Tsh : Unsigned_32;
38+ Tsl : Unsigned_32;
39+ Cap : Unsigned_32;
40+ Orig : Unsigned_32;
41+ Pad : Natural;
42+ D_End : Natural;
3043 begin
3144 View :=
32- (Iface_Id => 0 , Ts_High => 0 , Ts_Low => 0 , Cap_Len => 0 ,
45+ (If_Index => 0 , Ts_High => 0 , Ts_Low => 0 , Cap_Len => 0 ,
3346 Orig_Len => 0 , Data_First => Pos, Data_Last => Pos - 1 ,
34- Total_Length => 0 , Valid => False);
47+ Opt_First => 0 , Total_Length => 0 , Valid => False);
3548 Next := Pos;
3649 Error := Truncated;
3750
@@ -60,67 +73,54 @@ package body Pcapng_In_Ada.Packet is
6073 return ;
6174 end if ;
6275
63- case Order is
64- when Little_Endian =>
65- Iface := Decode_U32_LE (Buf, Pos + 8 );
66- Stamp_Hi := Decode_U32_LE (Buf, Pos + 12 );
67- Stamp_Lo := Decode_U32_LE (Buf, Pos + 16 );
68- Cap := Decode_U32_LE (Buf, Pos + 20 );
69- Orig := Decode_U32_LE (Buf, Pos + 24 );
70- when Big_Endian =>
71- Iface := Decode_U32_BE (Buf, Pos + 8 );
72- Stamp_Hi := Decode_U32_BE (Buf, Pos + 12 );
73- Stamp_Lo := Decode_U32_BE (Buf, Pos + 16 );
74- Cap := Decode_U32_BE (Buf, Pos + 20 );
75- Orig := Decode_U32_BE (Buf, Pos + 24 );
76- end case ;
76+ Ifx := Dec32 (Buf, Pos + 8 , Order);
77+ Tsh := Dec32 (Buf, Pos + 12 , Order);
78+ Tsl := Dec32 (Buf, Pos + 16 , Order);
79+ Cap := Dec32 (Buf, Pos + 20 , Order);
80+ Orig := Dec32 (Buf, Pos + 24 , Order);
7781
78- -- caplen may not exceed origlen, and caplen + padding must fit
79- -- the remaining body after the 28 fixed bytes.
80- if Cap > Orig then
82+ if Cap > Unsigned_32 (Max_Block_Size) then
8183 Error := Bad_Length;
8284 return ;
8385 end if ;
84- if Cap > Interfaces.Unsigned_32 (Natural'Last - Block_Header_Size
85- - Block_Trailer_Size - 28 )
86- or else Pos + 28 + Natural (Cap)
87- + Natural (Padding_For (Natural (Cap))) > H_Nx
86+ -- caplen must fit inside the block body.
87+ if Natural (Cap) + EPB_Min_Size > Hdr.Total_Length
88+ and then Cap /= 0
8889 then
90+ -- Allow trailing options of zero length only via exact math
91+ -- below; this early check catches gross overflows.
92+ if Hdr.Total_Length < EPB_Min_Size + Natural (Cap) then
93+ Error := Bad_Length;
94+ return ;
95+ end if ;
96+ end if ;
97+
98+ Pad := Padding_For (Natural (Cap));
99+ if Natural (Cap) + Pad + EPB_Min_Size > Hdr.Total_Length then
89100 Error := Bad_Length;
90101 return ;
91102 end if ;
92- Frame := Natural (Cap);
93- pragma Assert (Frame = Natural (Cap));
94- pragma Assert (Frame <= Max_Block_Size);
95- pragma Assert (Padding_For (Frame) <= 3 );
96- pragma Assert (Frame <= Hdr.Total_Length - Block_Header_Size
97- - Block_Trailer_Size - 28 );
98- pragma Assert (Pos <= H_Nx - 28 - Frame
99- - Natural (Padding_For (Frame)));
100- pragma Assert (Frame <= Buf'Last - Pos - 28
101- - Natural (Padding_For (Frame)));
103+ if Cap = 0 then
104+ D_End := Pos + 27 ;
105+ else
106+ if Pos + 28 + Natural (Cap) - 1 > Buf'Last then
107+ Error := Truncated;
108+ return ;
109+ end if ;
110+ D_End := Pos + 28 + Natural (Cap) - 1 ;
111+ end if ;
102112
103113 View :=
104- (Iface_Id => Iface,
105- Ts_High => Stamp_Hi, Ts_Low => Stamp_Lo,
114+ (If_Index => Ifx, Ts_High => Tsh, Ts_Low => Tsl,
106115 Cap_Len => Cap, Orig_Len => Orig,
107116 Data_First => Pos + 28 ,
108- Data_Last => (if Frame = 0 then Pos + 27 else Pos + 28 + Frame - 1 ),
117+ Data_Last => D_End,
118+ Opt_First => Pos + 28 + Natural (Cap) + Pad,
109119 Total_Length => Hdr.Total_Length, Valid => True);
110120 Next := H_Nx;
111121 Error := None;
112122 end Parse_EPB ;
113123
114- function Frame_Body_Length
115- (Total : Natural) return Natural
116- is
117- begin
118- if Total < SPB_Min_Size then
119- return 0 ;
120- end if ;
121- return Total - Block_Header_Size - Block_Trailer_Size - 4 ;
122- end Frame_Body_Length ;
123-
124124 procedure Parse_SPB
125125 (Buf : Byte_Buffer;
126126 Pos : Buffer_Index;
0 commit comments