···7171 | Not_found when x = padding && pad = 1 -> (0, 2) in
72727373 emit a b c d j ;
7474- match pad with 0 -> dec (j + 3) (i + 4) | _ -> pad
7474+ match pad with
7575+ | 0 -> dec (j + 3) (i + 4)
7676+ | _ when i + 4 <> n -> raise Not_found
7777+ | _ -> pad
7578 in
7679 try let pad = dec 0 0 in Some (sub cs' 0 (n' - pad))
7780 with Invalid_argument _ | Not_found -> None
+54
tests/testlib.ml
···142142 assert_cs_equal ~msg:"invert" a a1 ;
143143 assert_cs_equal ~msg:"commut" a1 a2
144144145145+let b64_selftest n =
146146+ "selftest" >:: times ~n @@ fun _ ->
147147+ let n = Rng.Int.gen 1024 in
148148+ let x = Rng.generate n in
149149+150150+ let enc = Base64.encode x in
151151+ match Base64.decode enc with
152152+ | Some dec -> assert_cs_equal ~msg:"b64 mismatch" dec x
153153+ | None -> assert_failure "couldn't decode b64"
154154+145155146156let gen_rsa ~bits =
147157 let e = Z.(if bits < 24 then ~$3 else ~$0x10001) in
···302312303313 "00", "00 01 02", "00" ;
304314 ]
315315+316316+(* B64 *)
317317+318318+let b64_enc_cases =
319319+ cases_of (f1_eq ~msg:"b64" Base64.encode) [
320320+ "66 6f 6f", "5a 6d 39 76" ;
321321+ "66 6f 6f 6f", "5a 6d 39 76 62 77 3d 3d" ;
322322+ "66 6f 6f 6f 6f", "5a 6d 39 76 62 32 38 3d" ;
323323+ "66 6f 6f 6f 6f 6f", "5a 6d 39 76 62 32 39 76" ;
324324+ ]
325325+326326+327327+let f1_opt_eq ?msg f (a, b) _ =
328328+ let maybe = function None -> None | Some h -> Some (Cs.of_hex h) in
329329+ let (a, b) = Cs.of_hex a, maybe b in
330330+ let eq_opt eq a b = match (a, b) with
331331+ | (Some x, Some y) -> eq x y
332332+ | (None , None ) -> true
333333+ | _ -> false
334334+ in
335335+ assert_equal
336336+ ~cmp:(eq_opt Cstruct.equal)
337337+ ~printer:(show_opt hex_of_cs)
338338+ ?msg (f a) b
339339+340340+let b64_dec_cases =
341341+ cases_of (f1_opt_eq ~msg:"b64" Base64.decode) [
342342+ "00 5a 6d 39 76", None ;
343343+ "5a 6d 39 76", Some "66 6f 6f" ;
344344+ "5a 6d 39 76 76", None ;
345345+ "5a 6d 39 76 76 76", None ;
346346+ "5a 6d 39 76 76 76 76", None ;
347347+ "5a 6d 39 76 00", None ;
348348+ "5a 6d 39 76 62 77 3d 3d", Some "66 6f 6f 6f" ;
349349+ "5a 6d 39 76 62 77 3d 3d 00", None ;
350350+ "5a 6d 39 76 62 77 3d 3d 00 01", None ;
351351+ "5a 6d 39 76 62 77 3d 3d 00 01 02", None ;
352352+ "5a 6d 39 76 62 77 3d 3d 00 01 02 03", None ;
353353+ "5a 6d 39 76 62 32 38 3d", Some "66 6f 6f 6f 6f" ;
354354+ "5a 6d 39 76 62 32 39 76", Some "66 6f 6f 6f 6f 6f" ;
355355+ ]
356356+305357306358let f1_blk_eq ?msg ?(n=1) f (x, y) _ =
307359 let (x, y) = Cs.(of_hex x, of_hex y) in
···877929 ] ;
878930879931 "XOR" >::: [ xor_selftest 300 ; "example" >::: xor_cases ];
932932+933933+ "B64" >::: [ b64_selftest 300 ; "encoding" >::: b64_enc_cases ; "decoding" >::: b64_dec_cases ];
880934881935 "MD5" >::: md5_cases ;
882936