This repository has been archived by the owner on Jun 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQUICUtils.fst
306 lines (278 loc) · 9.88 KB
/
QUICUtils.fst
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
module QUICUtils
open FStar
open FStar.HyperStack
open FStar.HyperStack.ST
open FStar.UInt32
open FStar.Int.Cast
open FStar.Printf
open C
open LowStar.Buffer
open LowStar.BufferOps
open C.Failure
open C.String
open QUICTypes
module U64 = FStar.UInt64
module U32 = FStar.UInt32
module U16 = FStar.UInt16
module U8 = FStar.UInt8
module I64 = FStar.Int64
module I32 = FStar.Int32
module Cast = FStar.Int.Cast
module B = LowStar.Buffer
(** Append a uint64 value to the buffer. Returns the next write offset. *)
let append64 (b:buffer_t) (offset:U32.t) (value:U64.t): ST U32.t
(requires (fun _ -> (UInt32.v offset < (B.length b - 8))))
(ensures (fun _ _ _ -> true))
=
store64_be (B.offset b offset) value;
offset +^ 8ul
(** Append a uint32 value to the buffer. Returns the next write offset. *)
let append32 (b:buffer_t) (offset:U32.t) (value:U32.t): ST U32.t
(requires (fun _ -> (UInt32.v offset < (B.length b - 4))))
(ensures (fun _ _ _ -> true))
=
store32_be (B.offset b offset) value;
offset +^ 4ul
(** Append a uint16 value to the buffer. Returns the next write offset. *)
let append16 (b:buffer_t) (offset:U32.t) (value:U16.t): ST U32.t
(requires (fun _ -> (UInt32.v offset < (B.length b - 2))))
(ensures (fun _ _ _ -> true))
=
store16_be (B.offset b offset) value;
offset +^ 2ul
(** Append a uint8 value to the buffer. Returns the next write offset. *)
let append8 (b:buffer_t) (offset:U32.t) (value:U8.t): ST U32.t
(requires (fun _ -> (UInt32.v offset < (B.length b - 1))))
(ensures (fun _ _ _ -> true))
=
B.upd b offset value;
offset +^ 1ul
// No polymorphic "append()" in F*... depends on "match value with" followed by type names
(** Append raw bytes to a buffer. Returns the next write offset. *)
let appendbytes (b:buffer_t) (offset:U32.t) (value:buffer_t) (valuelength:U32.t): ST U32.t
(requires (fun _ -> (UInt32.v offset < (B.length b - U32.v valuelength))))
(ensures (fun _ _ _ -> true))
=
B.blit value 0ul b offset valuelength;
offset+^valuelength
(** getbytes "safe" - read out of buffer 'b' with length 'l' at offset 'offset' into value/valuelength.
Returns the next write offset. *)
let getbytes_s (b:buffer_t) (l:U32.t) (offset:U32.t) (value:buffer_t) (valuelength:U32.t): ST (err U32.t)
(requires (fun _ -> true))
(ensures (fun _ _ _ -> true))
=
if U32.(offset+^valuelength >^ l) then
fail !$"Insufficient buffer"
else (
B.blit b offset value 0ul valuelength;
return U32.(offset +^ valuelength)
)
(** Read a uint64 from the buffer *)
let getu64 (b:buffer_t) (offset:U32.t): ST U64.t
(requires (fun _ -> (UInt32.v offset < (B.length b - 8))))
(ensures (fun _ _ _ -> true))
=
load64_be (B.offset b offset)
(** Read a uint32 from the buffer *)
let getu32 (b:buffer_t) (offset:U32.t): ST U32.t
(requires (fun _ -> (UInt32.v offset < (B.length b - 4))))
(ensures (fun _ _ _ -> true))
=
load32_be (B.offset b offset)
(** Read "safely" a uint32 from the buffer. Returns the value or an
error if the read is past the end of the buffer. *)
let getu32_s (b:buffer_t) (l:U32.t) (offset:U32.t): ST (err U32.t)
(requires (fun _ -> (UInt32.v offset < (B.length b - 4))))
(ensures (fun _ _ _ -> true))
=
if U32.(offset+^4ul >^ l) then
fail !$"Insufficient buffer"
else
return (load32_be (B.offset b offset))
(** Read a uint16 from the buffer *)
let getu16 (b:buffer_t) (offset:U32.t): ST U16.t
(requires (fun _ -> (UInt32.v offset < (B.length b - 2))))
(ensures (fun _ _ _ -> true))
=
load16_be (B.offset b offset)
(** Read "safely" a uint16 from the buffer. Returns the value or an
error if the read is past the end of the buffer. *)
let getu16_s (b:buffer_t) (l:U32.t) (offset:U32.t): ST (err U16.t)
(requires (fun _ -> (UInt32.v offset < (B.length b - 2))))
(ensures (fun _ _ _ -> true))
=
if U32.(offset+^2ul >^ l) then
fail !$"Insufficient buffer"
else
return (load16_be (B.offset b offset))
(** Read a uint8 from the buffer *)
let getu8 (b:buffer_t) (offset:U32.t): ST U8.t
(requires (fun _ -> (UInt32.v offset < (B.length b - 1))))
(ensures (fun _ _ _ -> true))
=
B.index b offset
(** Read "safely" a uint8 from the buffer. Returns the value or an
error if the read is past the end of the buffer. *)
let getu8_s (b:buffer_t) (l:U32.t) (offset:U32.t): ST (err U8.t)
(requires (fun _ -> (UInt32.v offset < (B.length b - 1))))
(ensures (fun _ _ _ -> true))
=
if U32.(offset+^1ul >^ l) then
fail !$"Insufficient buffer"
else
return (B.index b offset)
(* Compute max of two values *)
let maxi64 (x:I64.t) (y:I64.t): I64.t =
if I64.gt x y then x else y
(* Compute max of two values *)
let maxu64 (x:U64.t) (y:U64.t): U64.t =
if U64.gt x y then x else y
(* Compute min of two values *)
let minu32 (x:U32.t) (y:U32.t): U32.t =
if U32.gt x y then y else x
(* Compute min of two values *)
let minu64 (x:U64.t) (y:U64.t): U64.t =
if U64.gt x y then y else x
(** Write a variable-length U64.t. The top 2 bits of the first byte will indicate the length of the full value. *)
let appendvar (b:buffer_t) (offset:U32.t) (value:U64.t): ST U32.t
(requires (fun _ -> (UInt32.v offset < (B.length b - 8))))
(ensures (fun _ _ _ -> true))
=
push_frame();
let ret =
if U64.(value <^ 0x40UL) then (
let v8 = Cast.uint64_to_uint8 value in
append8 b offset v8
) else if U64.(value <^ 0x4000UL) then (
let v16 = Cast.uint64_to_uint16 value in
let v16 = U16.(v16 |^ 0x4000us) in
append16 b offset v16
) else if U64.(value <^ 0x40000000UL) then (
let v32 = Cast.uint64_to_uint32 value in
let v32 = U32.(v32 |^ 0x80000000ul) in
append32 b offset v32
) else if U64.(value <^ 0x4000000000000000UL) then (
let v64 = U64.(value |^ 0xc000000000000000UL) in
append64 b offset v64
) else failwith (of_literal "Value must be < 2^62")
in
pop_frame();
ret
(** Given a value, determine the number of bytes its encoding requires *)
let encodedsize (value:U64.t): ST U32.t
(requires (fun _ -> true))
(ensures (fun _ _ _ -> true))
=
if U64.(value <^ 0x40UL) then 1ul
else if U64.(value <^ 0x4000UL) then 2ul
else if U64.(value <^ 0x40000000UL) then 4ul
else if U64.(value <^ 0x4000000000000000UL) then 8ul
else failwith (of_literal "Value must be < 2^62")
(** Read a variable-length U64.t. The top 2 bits of the first byte indicate the length of the full value. *)
let getvar (b:buffer_t) (offset:U32.t): ST (U64.t * U32.t)
(requires (fun _ -> true))
(ensures (fun _ _ _ -> true))
=
push_frame();
let firstbyte:U8.t = getu8 b offset in
let len:U8.t = U8.(firstbyte &^ 0xc0uy) in
let ret = (
if len = 0x00uy then (
let t64 = Cast.uint8_to_uint64 firstbyte in
(t64, U32.(offset+^1ul))
) else if len = 0x40uy then (
let t16 = getu16 b offset in
let t16 = U16.(t16 &^ 0x3fffus) in
let t64 = Cast.uint16_to_uint64 t16 in
(t64, U32.(offset+^2ul))
) else if len = 0x80uy then (
let t32 = getu32 b offset in
let t32 = U32.(t32 &^ 0x3ffffffful) in
let t64 = Cast.uint32_to_uint64 t32 in
(t64, U32.(offset+^4ul))
) else (
let t64 = getu64 b offset in
let t64 = U64.(t64 &^ 0x3fffffffffffffffUL) in
(t64, U32.(offset+^8ul))
)
) in
pop_frame();
ret
(** Get a variable-length integer, or return failure if the read crosses the
end of the buffer *)
let getvar_s (b:buffer_t) (length:U32.t) (offset:U32.t): ST (err (U64.t * U32.t))
(requires (fun _ -> true))
(ensures (fun _ _ _ -> true))
=
firstbyte <-- (if U32.(offset <^ length) then
return (getu8 b offset)
else
fail !$"Insufficient buffer"
);
let len:U8.t = U8.(firstbyte &^ 0xc0uy) in
match len with
| 0x00uy ->
let nextoffset = U32.(offset+^1ul) in
if U32.(nextoffset >^ length) then
fail !$"Insufficient buffer"
else (
let t64 = Cast.uint8_to_uint64 firstbyte in
return (t64, nextoffset)
)
| 0x40uy ->
let nextoffset = U32.(offset+^2ul) in
if U32.(nextoffset >^ length) then
fail !$"Insufficient buffer"
else (
let t16 = getu16 b offset in
let t16 = U16.(t16 &^ 0x3fffus) in
let t64 = Cast.uint16_to_uint64 t16 in
return (t64, nextoffset)
)
| 0x80uy ->
let nextoffset = U32.(offset+^4ul) in
if U32.(nextoffset >^ length) then
fail !$"Insufficient buffer"
else (
let t32 = getu32 b offset in
let t32 = U32.(t32 &^ 0x3ffffffful) in
let t64 = Cast.uint32_to_uint64 t32 in
return (t64, nextoffset)
)
| _ ->
let nextoffset = U32.(offset+^8ul) in
if U32.(nextoffset >^ length) then
fail !$"Insufficient buffer"
else (
let t64 = getu64 b offset in
let t64 = U64.(t64 &^ 0x3fffffffffffffffUL) in
return (t64, nextoffset)
)
(** Convert a C.String to a Prims.string. This leaks onto the heap. Do not use outside of
debug logging code *)
let rec cstring_to_string_internal (src:C.String.t) (offset:U32.t) (len:U32.t) (dst:string): Tot string
=
if offset = len then
dst
else (
let c = C.String.get src offset in
let cu8 = C.uint8_of_char c in
let cf = FStar.Char.char_of_u32 (Cast.uint8_to_uint32 cu8) in
let cs = FStar.String.string_of_char cf in
let newdst = FStar.String.strcat dst cs in
cstring_to_string_internal src U32.(offset+^1ul) len newdst
)
(** Convert a C.String to a Prims.string. This leaks onto the heap. Do not use outside of
debug logging code *)
let cstring_to_string (src:C.String.t): Tot string
=
let len = strlen src in
cstring_to_string_internal src 0ul len ""
(** Extension to FStar.Printf to print C.String.t types *)
let parse_cstring : extension_parser =
function
| 'C' :: rest -> Some (MkExtension cstring_to_string, rest)
| _ -> None
(** Extend FStar.Printf.sprintf to support %XC, which is a C.String.t type *)
inline_for_extraction
let csprintf = ext_sprintf parse_cstring