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
type default = Default
type clear = Clear
type unsigned = { unsigned : bool }
type endian = [ `BE | `LE | `ME | `NE ]
type ('test, 'v) t =
| Default : (default, default) t
| Offset : (int64, int64) t
| Regex : {
case_insensitive : bool;
start : bool;
limit : int64;
kind : [ `Byte | `Line ];
}
-> (Re.t, Ropes.t) t
| Clear : (clear, clear) t
| Search : {
compact_whitespaces : bool;
optional_blank : bool;
lower_case_insensitive : bool;
upper_case_insensitive : bool;
text : bool;
binary : bool;
trim : bool;
range : int64;
pattern : string;
find : Kmp.finder;
}
-> (string, string) t
| Pascal_string : (string, string) t
| Unicode_string : [ `BE | `LE ] -> (string, string) t
| Byte : unsigned * char Arithmetic.t -> (char, char) t
| Short : unsigned * int Arithmetic.t * [ `BE | `LE | `NE ] -> (int, int) t
| Long : unsigned * int32 Arithmetic.t * endian -> (int32, int32) t
| Quad : unsigned * int64 Arithmetic.t * endian -> (int64, int64) t
| Float : unsigned * float Arithmetic.t * endian -> (float, float) t
| Double : unsigned * float Arithmetic.t * endian -> (float, float) t
| Indirect : [ `Rel | `Abs ] -> ('test, 'v) t
| Date :
[ `Local | `UTC | `Window ]
* [ `s32 | `s64 ]
* Ptime.span Arithmetic.t
* endian
-> (Ptime.t, string) t
let serialize_endian ppf = function
| `BE -> Format.pp_print_string ppf "`BE"
| `LE -> Format.pp_print_string ppf "`LE"
| `ME -> Format.pp_print_string ppf "`ME"
| `NE -> Format.pp_print_string ppf "`NE"
let serialize : type test v. Format.formatter -> (test, v) t -> unit =
fun ppf -> function
| Default -> Format.pp_print_string ppf "Conan.Ty.default"
| Offset -> Format.pp_print_string ppf "Conan.Ty.offset"
| Regex { case_insensitive; start; limit; kind } ->
let serialize_kind ppf = function
| `Byte -> Format.fprintf ppf "`Byte"
| `Line -> Format.fprintf ppf "`Line"
in
Format.fprintf ppf
"@[<2>Conan.Ty.regex@ ~case_insensitive:%b ~start:%b@ ~limit:%LdL@ %a@]"
case_insensitive start limit serialize_kind kind
| Clear -> Format.pp_print_string ppf "Conan.Ty.clear"
| Search
{
compact_whitespaces;
optional_blank;
lower_case_insensitive;
upper_case_insensitive;
text;
binary;
trim;
range;
pattern;
find = _;
} ->
let serialize_type ppf () =
match (text, binary) with
| true, false -> Format.pp_print_string ppf "`Text"
| false, true -> Format.pp_print_string ppf "`Binary"
| _ -> assert false
in
Format.fprintf ppf
"@[<2>Conan.Ty.search@ ~compact_whitespaces:%b@ ~optional_blank:%b@ \
~lower_case_insensitive:%b@ ~upper_case_insensitive:%b@ @[%a@]@ \
~trim:%b@ %LdL@ ~pattern:%S@]"
compact_whitespaces optional_blank lower_case_insensitive
upper_case_insensitive serialize_type () trim range pattern
| Pascal_string -> Format.pp_print_string ppf "Conan.Ty.pascal_string"
| Unicode_string `BE -> Format.fprintf ppf "@[<2>Conan.Ty.str_unicode@ `BE@]"
| Unicode_string `LE -> Format.fprintf ppf "@[<2>Conan.Ty.str_unicode@ `LE@]"
| Byte ({ unsigned }, arithmetic) ->
Format.fprintf ppf
"@[<2>Conan.Ty.numeric@ ~unsigned:%b@ Conan.Integer.byte@ @[%a@]@]"
unsigned
Serialize.(parens (Arithmetic.serialize char))
arithmetic
| Short ({ unsigned }, arithmetic, endian) ->
let serialize_endian ppf = function
| `BE -> Format.pp_print_string ppf "`BE"
| `LE -> Format.pp_print_string ppf "`LE"
| `NE -> Format.pp_print_string ppf "`NE"
in
Format.fprintf ppf
"@[<2>Conan.Ty.numeric@ ~unsigned:%b@ ~endian:%a@ Conan.Integer.short \
@[%a@]@]"
unsigned serialize_endian endian
Serialize.(parens (Arithmetic.serialize int))
arithmetic
| Long ({ unsigned }, arithmetic, endian) ->
Format.fprintf ppf
"@[<2>Conan.Ty.numeric@ ~unsigned:%b@ ~endian:%a@ Conan.Integer.int32 \
@[%a@]@]"
unsigned serialize_endian endian
Serialize.(parens (Arithmetic.serialize int32))
arithmetic
| Quad ({ unsigned }, arithmetic, endian) ->
Format.fprintf ppf
"@[<2>Conan.Ty.numeric@ ~unsigned:%b@ ~endian:%a@ Conan.Integer.int64 \
@[%a@]@]"
unsigned serialize_endian endian
Serialize.(parens (Arithmetic.serialize int64))
arithmetic
| Float ({ unsigned }, arithmetic, endian) ->
Format.fprintf ppf
"@[<2>Conan.Ty.float@ ~unsigned:%b@ ~endian:%a@ @[%a@]@]" unsigned
serialize_endian endian
Serialize.(parens (Arithmetic.serialize float))
arithmetic
| Double ({ unsigned }, arithmetic, endian) ->
Format.fprintf ppf
"@[<2>Conan.Ty.double@ ~unsigned:%b@ ~endian:%a@ @[%a@]@]" unsigned
serialize_endian endian
Serialize.(parens (Arithmetic.serialize float))
arithmetic
| Indirect `Rel -> Format.fprintf ppf "@[<2>Conan.Ty.indirect@ `Rel@]"
| Indirect `Abs -> Format.fprintf ppf "@[<2>Conan.Ty.indirect@ `Abs@]"
| Date (zone, size, arithmetic, endian) ->
let serialize_type ppf () =
match (zone, size) with
| `UTC, `s32 -> Format.pp_print_string ppf "`Date"
| `Local, `s32 -> Format.pp_print_string ppf "`Ldate"
| `UTC, `s64 -> Format.pp_print_string ppf "`Qdate"
| `Local, `s64 -> Format.pp_print_string ppf "`Qldate"
| `Window, `s64 -> Format.pp_print_string ppf "`Qwdate"
| `Window, `s32 -> assert false
in
Format.fprintf ppf
"@[<2>Conan.Ty.date@ @[%a@]@ @[<1>(Some@ %a)@]@ @[%a@]@]" serialize_type
() serialize_endian endian
Serialize.(parens (Arithmetic.serialize (parens ptime_span)))
arithmetic
let pf = Format.fprintf
let pp_unsigned ppf { unsigned } = if unsigned then pf ppf "u"
let pp_flag letter ppf = function true -> pf ppf "%c" letter | false -> ()
let pp_int ppf = pf ppf "0x%x"
let pp_int32 ppf = pf ppf "0x%lx"
let pp_int64 ppf = pf ppf "0x%Lx"
let pp_ptime ppf v =
match Ptime.Span.to_int_s v with
| Some v -> pf ppf "%d" v
| None -> pf ppf "%.0f" (Ptime.Span.to_float_s v)
let pp_float ppf = pf ppf "%f"
let pp_endian ppf = function
| `BE -> pf ppf "be"
| `LE -> pf ppf "le"
| `ME -> pf ppf "me"
| `NE -> pf ppf "ne"
let pp_date_size ppf = function `s32 -> () | `s64 -> pf ppf "q"
let pp_of_result : type test v. (test, v) t -> Format.formatter -> v -> unit =
function
| Default -> fun ppf Default -> Format.fprintf ppf "x"
| Offset -> fun ppf v -> Format.fprintf ppf "%Ld" v
| Regex _ ->
fun ppf ropes ->
let str, off, len = Ropes.to_string ropes in
Format.fprintf ppf "%S" (String.sub str off len)
| Clear -> fun ppf Clear -> Format.fprintf ppf "clear"
| Search _ -> fun ppf v -> Format.fprintf ppf "%S" v
| Pascal_string -> fun ppf v -> Format.fprintf ppf "%S" v
| Unicode_string _ -> fun ppf v -> Format.fprintf ppf "%S" v
| Byte _ -> fun ppf chr -> Format.fprintf ppf "%S" (String.make 1 chr)
| Short _ -> fun ppf v -> Format.fprintf ppf "%d" v
| Long _ -> fun ppf v -> Format.fprintf ppf "%ld" v
| Quad _ -> fun ppf v -> Format.fprintf ppf "%Ld" v
| Float _ -> fun ppf v -> Format.fprintf ppf "%f" v
| Double _ -> fun ppf v -> Format.fprintf ppf "%f" v
| Indirect _ -> fun ppf _ -> Format.fprintf ppf "#indirection"
| Date _ -> fun ppf v -> Format.fprintf ppf "%S" v
let pp : type test v. Format.formatter -> (test, v) t -> unit =
fun ppf -> function
| Default -> pf ppf "default"
| Clear -> pf ppf "clear"
| Offset -> pf ppf "offset"
| Regex
{ case_insensitive = false; start = false; limit = 8192L; kind = `Byte }
->
pf ppf "regex"
| Regex { case_insensitive; start; limit; kind } ->
let line = match kind with `Byte -> false | `Line -> true in
pf ppf "regex/%a%a%a/%Ld" (pp_flag 'c') case_insensitive (pp_flag 's')
start (pp_flag 'l') line limit
| Search
{
compact_whitespaces = false;
optional_blank = false;
lower_case_insensitive = false;
upper_case_insensitive = false;
text = false;
binary = false;
trim = false;
range = 0L;
pattern = "";
find = _;
} ->
pf ppf "search"
| Search
{
compact_whitespaces;
optional_blank;
lower_case_insensitive;
upper_case_insensitive;
text;
binary;
trim;
range;
pattern = _;
find = _;
} ->
pf ppf "search/%a%a%a%a%a%a%a/%Ld" (pp_flag 'W') compact_whitespaces
(pp_flag 'w') optional_blank (pp_flag 'c') lower_case_insensitive
(pp_flag 'C') upper_case_insensitive (pp_flag 'b') binary (pp_flag 't')
text (pp_flag 'T') trim range
| Pascal_string -> pf ppf "pstring"
| Unicode_string `BE -> pf ppf "bestring16"
| Unicode_string `LE -> pf ppf "lestring16"
| Byte (unsigned, arithmetic) ->
let pp_byte ppf v = pf ppf "%02x" (Char.code v) in
pf ppf "%abyte%a" pp_unsigned unsigned (Arithmetic.pp pp_byte) arithmetic
| Short (unsigned, arithmetic, endian) ->
pf ppf "%a%ashort%a" pp_unsigned unsigned pp_endian endian
(Arithmetic.pp pp_int) arithmetic
| Long (unsigned, arithmetic, endian) ->
pf ppf "%a%along%a" pp_unsigned unsigned pp_endian endian
(Arithmetic.pp pp_int32) arithmetic
| Quad (unsigned, arithmetic, endian) ->
pf ppf "%a%aquad%a" pp_unsigned unsigned pp_endian endian
(Arithmetic.pp pp_int64) arithmetic
| Float (unsigned, arithmetic, endian) ->
pf ppf "%a%afloat%a" pp_unsigned unsigned pp_endian endian
(Arithmetic.pp pp_float) arithmetic
| Double (unsigned, arithmetic, endian) ->
pf ppf "%a%adouble%a" pp_unsigned unsigned pp_endian endian
(Arithmetic.pp pp_float) arithmetic
| Indirect `Rel -> pf ppf "indirect/r"
| Indirect `Abs -> pf ppf "indirect"
| Date (`Local, size, arithmetic, endian) ->
pf ppf "%a%aldate%a" pp_endian endian pp_date_size size
(Arithmetic.pp pp_ptime) arithmetic
| Date (`UTC, size, arithmetic, endian) ->
pf ppf "%a%adate%a" pp_endian endian pp_date_size size
(Arithmetic.pp pp_ptime) arithmetic
| Date (`Window, size, arithmetic, endian) ->
pf ppf "%a%awdate%a" pp_endian endian pp_date_size size
(Arithmetic.pp pp_ptime) arithmetic
let default : (default, default) t = Default
let offset : (int64, int64) t = Offset
let clear : (clear, clear) t = Clear
let regex ?(case_insensitive = false) ?(start = false) ?(limit = 8192L) kind =
Regex { case_insensitive; start; limit; kind }
let pascal_string = Pascal_string
let search ?(compact_whitespaces = false) ?(optional_blank = false)
?(lower_case_insensitive = false) ?(upper_case_insensitive = false) kind
?(trim = false) range ~pattern =
if Int64.of_int (String.length pattern) > range then
invalid_arg "Pattern can not be larger than the range";
let text, binary =
match kind with `Text -> (true, false) | `Binary -> (false, true)
in
Search
{
compact_whitespaces;
optional_blank;
lower_case_insensitive;
upper_case_insensitive;
text;
binary;
trim;
range;
pattern;
find = Kmp.find_one ~pattern;
}
let with_range range = function Search v -> Search { v with range } | t -> t
let with_pattern pattern = function
| Search v -> Search { v with pattern }
| t -> t
let str_unicode endian = Unicode_string endian
let system_endian = if Sys.big_endian then `BE else `LE
let numeric :
type w.
?unsigned:bool ->
?endian:endian ->
w Integer.t ->
w Arithmetic.t ->
(w, w) t =
fun ?(unsigned = false) ?(endian = system_endian) w a ->
match w with
| Integer.Byte -> Byte ({ unsigned }, a)
| Integer.Short ->
let endian =
match endian with
| `BE -> `BE
| `LE -> `LE
| `NE -> `NE
| _ -> invalid_arg "Invalid endian for short"
in
Short ({ unsigned }, a, endian)
| Integer.Int32 -> Long ({ unsigned }, a, endian)
| Integer.Int64 -> Quad ({ unsigned }, a, endian)
let date kind endian a =
let endian =
match endian with
| Some `BE -> `BE
| Some `LE -> `LE
| Some `ME -> `ME
| None -> if Sys.big_endian then `BE else `LE
in
match kind with
| `Date -> Date (`UTC, `s32, a, endian)
| `Ldate -> Date (`Local, `s32, a, endian)
| `Qdate -> Date (`UTC, `s64, a, endian)
| `Qldate -> Date (`Local, `s64, a, endian)
| `Qwdate -> Date (`Window, `s64, a, endian)
let float ?(unsigned = false) ?(endian = system_endian) c =
Float ({ unsigned }, c, endian)
let double ?(unsigned = false) ?(endian = system_endian) c =
Double ({ unsigned }, c, endian)
let indirect v = Indirect v
open Sigs
let ( <.> ) f g x = f (g x)
let newline = "\n"
let ok x = Ok x
let of_option ~error = function Some x -> Ok x | None -> Error (error ())
let reword_error f = function Ok x -> Ok x | Error err -> Error (f err)
let id x = x
let read_float ({ bind; return } as scheduler) syscall fd endian =
let ( >>= ) = bind in
let size =
match endian with
| `LE -> Size.lelong
| `BE -> Size.belong
| `ME -> assert false
| `NE -> Size.long
in
Size.read scheduler syscall fd size >>= function
| Ok v ->
let v = Int64.to_int32 v in
return (Ok (Int32.float_of_bits v))
| Error _ as err -> return err
let read_double ({ bind; return } as scheduler) syscall fd endian =
let ( >>= ) = bind in
let size =
match endian with
| `LE -> Size.lequad
| `BE -> Size.bequad
| `ME -> assert false
| `NE -> Size.quad
in
Size.read scheduler syscall fd size >>= function
| Ok v -> return (Ok (Int64.float_of_bits v))
| Error _ as err -> return err
let process :
type s fd error test v.
s scheduler ->
(fd, error, s) syscall ->
fd ->
int64 ->
(test, v) t ->
((v, [> `Syscall of error | `Invalid_date | `Not_found ]) result, s) io =
fun ({ bind; return } as scheduler) syscall fd abs_offset ty ->
let ( >>= ) = bind in
let ( >?= ) x f =
x >>= function Ok x -> f x | Error err -> return (Error err)
in
let ( >|= ) x f = x >>= fun x -> return (f x) in
match ty with
| Default -> (return <.> ok) (Default : default)
| Offset -> (return <.> ok) abs_offset
| Clear -> (return <.> ok) (Clear : clear)
| Search { pattern = ""; _ } -> (return <.> ok) ""
| Search
{
pattern = _;
compact_whitespaces = _;
optional_blank = _;
lower_case_insensitive = _;
upper_case_insensitive = _;
text = _;
binary = _;
trim = _;
range;
find;
} -> (
let get fd ~pos =
syscall.seek fd (Int64.add abs_offset pos) SET >?= fun () ->
syscall.read_int8 fd >?= fun code -> (return <.> ok <.> Char.chr) code
in
find.Kmp.f scheduler ~get ~ln:range fd
>|= reword_error (fun err -> `Syscall err)
>?= fun results ->
let results = List.sort Int64.compare results in
match results with
| [] -> return (Error `Not_found)
| rel_offset :: _ ->
syscall.seek fd (Int64.add abs_offset rel_offset) SET
>|= reword_error (fun err -> `Syscall err)
>?= fun () ->
syscall.read fd (Int64.to_int range)
>|= reword_error (fun err -> `Syscall err))
| Regex { kind; limit; _ } -> (
match kind with
| `Byte ->
syscall.read fd (Int64.to_int limit)
>|= reword_error (fun err -> `Syscall err)
>?= fun raw -> return (Ok (Ropes.of_string raw))
| `Line ->
let rec go acc = function
| 0 ->
let ropes =
Ropes.append
(Ropes.concat ~sep:newline acc)
(Ropes.of_string newline)
in
return (Ok ropes)
| n ->
syscall.line fd >|= reword_error (fun err -> `Syscall err)
>?= fun (off, len, line) ->
go (Ropes.of_string ~off ~len line :: acc) (pred n)
in
go [] (max 0 (Int64.to_int limit)))
| Byte ({ unsigned }, c) ->
let size, converter, w =
(Size.byte, Char.chr <.> Int64.to_int, Integer.byte)
in
syscall.seek fd abs_offset SET >|= reword_error (fun err -> `Syscall err)
>?= fun () ->
Size.read scheduler syscall fd size
>?= (return <.> ok <.> converter)
>|= reword_error (fun err -> `Syscall err)
>?= fun v -> (return <.> ok) (Arithmetic.process ~unsigned w v c)
| Short ({ unsigned }, c, endian) ->
let size, converter, w =
match endian with
| `LE -> (Size.leshort, Int64.to_int, Integer.short)
| `BE -> (Size.beshort, Int64.to_int, Integer.short)
| `NE -> (Size.short, Int64.to_int, Integer.short)
in
syscall.seek fd abs_offset SET >|= reword_error (fun err -> `Syscall err)
>?= fun () ->
Size.read scheduler syscall fd size
>?= (return <.> ok <.> converter)
>|= reword_error (fun err -> `Syscall err)
>?= fun v -> (return <.> ok) (Arithmetic.process ~unsigned w v c)
| Long ({ unsigned }, c, endian) ->
let size, converter, w =
match endian with
| `LE -> (Size.lelong, Int64.to_int32, Integer.int32)
| `BE -> (Size.belong, Int64.to_int32, Integer.int32)
| `NE -> (Size.long, Int64.to_int32, Integer.int32)
| `ME -> failwith "Middle-endian not supported"
in
syscall.seek fd abs_offset SET >|= reword_error (fun err -> `Syscall err)
>?= fun () ->
Size.read scheduler syscall fd size
>?= (return <.> ok <.> converter)
>|= reword_error (fun err -> `Syscall err)
>?= fun v -> (return <.> ok) (Arithmetic.process ~unsigned w v c)
| Quad ({ unsigned }, c, endian) ->
let size, converter, w =
match endian with
| `LE -> (Size.lequad, id, Integer.int64)
| `BE -> (Size.bequad, id, Integer.int64)
| `NE -> (Size.quad, id, Integer.int64)
| `ME -> failwith "Middle-endian not supported"
in
syscall.seek fd abs_offset SET >|= reword_error (fun err -> `Syscall err)
>?= fun () ->
Size.read scheduler syscall fd size
>?= (return <.> ok <.> converter)
>|= reword_error (fun err -> `Syscall err)
>?= fun v -> (return <.> ok) (Arithmetic.process ~unsigned w v c)
| Float ({ unsigned = _ }, c, endian) ->
syscall.seek fd abs_offset SET >|= reword_error (fun err -> `Syscall err)
>?= fun () ->
read_float scheduler syscall fd endian
>|= reword_error (fun err -> `Syscall err)
>?= fun v -> (return <.> ok) (Arithmetic.process_float v c)
| Double ({ unsigned = _ }, c, endian) ->
syscall.seek fd abs_offset SET >|= reword_error (fun err -> `Syscall err)
>?= fun () ->
read_double scheduler syscall fd endian
>|= reword_error (fun err -> `Syscall err)
>?= fun v -> (return <.> ok) (Arithmetic.process_float v c)
| Date (_, `s32, c, endian) -> (
let size =
match endian with
| `BE -> Size.belong
| `LE -> Size.lelong
| `NE -> Size.long
| `ME -> assert false
in
let error () = `Invalid_date in
syscall.seek fd abs_offset SET >|= reword_error (fun err -> `Syscall err)
>?= fun () ->
Size.read scheduler syscall fd size
>|= reword_error (fun err -> `Syscall err)
>?= (return <.> of_option ~error <.> Ptime.Span.of_float_s
<.> Int64.to_float)
>?= fun v ->
match Ptime.of_span (Arithmetic.process_ptime v c) with
| Some v -> return (ok (Ptime.to_rfc3339 v))
| None -> return (Error `Invalid_date))
| Date (_, `s64, c, endian) -> (
let size =
match endian with
| `BE -> Size.bequad
| `LE -> Size.lequad
| `NE -> Size.quad
| `ME -> assert false
in
let error () = `Invalid_date in
syscall.seek fd abs_offset SET >|= reword_error (fun err -> `Syscall err)
>?= fun () ->
Size.read scheduler syscall fd size
>|= reword_error (fun err -> `Syscall err)
>?= (return <.> of_option ~error <.> Ptime.Span.of_float_s
<.> Int64.to_float)
>?= fun v ->
match Ptime.of_span (Arithmetic.process_ptime v c) with
| Some v -> return (ok (Ptime.to_rfc3339 v))
| None -> return (Error `Invalid_date))
| Unicode_string _ -> return (Ok "")
| Pascal_string -> assert false
| Indirect _ -> assert false