upstream: https://github.com/mirage/ocaml-mbr
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

fix(lint): replace Format with Fmt (E205), use err_* helpers (E340), shorten identifier (E320)

+10 -7
+10 -7
lib/mbr.ml
··· 16 16 17 17 open Result.Syntax 18 18 19 + (* Error helpers *) 20 + let err_lba_max x = Error (Fmt.str "sector count exceeds LBA max: %Ld" x) 21 + 22 + let err_bad_signature s1 s2 = 23 + Error (Fmt.str "Invalid signature: %02x %02x <> 0x55 0xaa" s1 s2) 24 + 19 25 (* Binary reading helpers - little-endian *) 20 26 let u8 s off = Char.code (Bytes.get s off) 21 27 let u16_le s off = u8 s off lor (u8 s (off + 1) lsl 8) ··· 99 105 else if x < Int64.(mul 1008L mib) then Ok 64 100 106 else if x < Int64.(mul 4032L mib) then Ok 128 101 107 else if x < Int64.(add (mul 8032L mib) (mul 512L kib)) then Ok 255 102 - else Error (Fmt.str "sector count exceeds LBA max: %Ld" x) 108 + else err_lba_max x 103 109 in 104 110 let cylinders = 105 111 Int64.(to_int (div (div x (of_int sectors)) (of_int heads))) ··· 300 306 301 307 (* MBR layout constants *) 302 308 let sizeof = 512 309 + let err_too_small n = Error (Fmt.str "MBR too small: %d < %d" n sizeof) 303 310 let bootstrap_code1_offset = 0 304 311 let bootstrap_code1_len = 218 305 312 let original_physical_drive_offset = 220 ··· 322 329 (* Internal unmarshal from bytes buffer *) 323 330 let unmarshal_bytes (buf : bytes) : (t, string) result = 324 331 let* () = 325 - if Bytes.length buf < sizeof then 326 - Error (Fmt.str "MBR too small: %d < %d" (Bytes.length buf) sizeof) 332 + if Bytes.length buf < sizeof then err_too_small (Bytes.length buf) 327 333 else Ok () 328 334 in 329 335 let signature1 = u8 buf signature1_offset in 330 336 let signature2 = u8 buf signature2_offset in 331 337 let* () = 332 338 if signature1 = 0x55 && signature2 = 0xaa then Ok () 333 - else 334 - Error 335 - (Fmt.str "Invalid signature: %02x %02x <> 0x55 0xaa" signature1 336 - signature2) 339 + else err_bad_signature signature1 signature2 337 340 in 338 341 let bootstrap_code = 339 342 Bytes.sub_string buf bootstrap_code1_offset bootstrap_code1_len