···6565 can raise and avoid appearance of unknown exceptions like an ex-nihilo
6666 magic rabbit (or magic money?). *)
6767exception Out_of_bounds
6868+exception Too_much_input
68696970let get_uint8 t off =
7071 if off < 0 || off >= String.length t then raise Out_of_bounds ;
···153154 let n = (len // 4) * 4 in
154155 let n' = (n // 4) * 3 in
155156 let res = Bytes.create n' in
157157+ let invalid_pad_overflow = pad in
156158157159 let get_uint8_or_padding =
158160 if pad then (fun t i -> if i >= len then raise Out_of_bounds ; get_uint8 t (off + i) )
···233235 if i + 4 = n
234236 (* end of input in anyway *)
235237 then match pad with
236236- | 0 -> 0
237237- | 4 -> 3
238238+ | 0 ->
239239+ 0
240240+ | 4 ->
241241+ (* assert (invalid_pad_overflow = false) ; *)
242242+ 3
238243 (* [get_uint8] lies and if we get [4], that mean we got one or more (at
239244 most 4) padding character. In this situation, because we round length
240245 of [res] (see [n // 4]), we need to delete 3 bytes. *)
241241- | pad -> pad
246246+ | pad ->
247247+ pad
242248 else match pad with
243249 | 0 -> dec (j + 3) (i + 4)
244244- | 4 -> only_padding 3 (i + 4)
250250+ | 4 ->
251251+ (* assert (invalid_pad_overflow = false) ; *)
252252+ only_padding 3 (i + 4)
245253 (* Same situation than above but we should get only more padding
246254 characters then. *)
247247- | pad -> only_padding pad (i + 4) end in
255255+ | pad ->
256256+ if invalid_pad_overflow = true then raise Too_much_input ;
257257+ only_padding pad (i + 4) end in
248258249259 match dec 0 0 with
250260 | 0 -> Ok (Bytes.unsafe_to_string res, 0, n')
···254264 | exception Not_found ->
255265 (* appear when one character of [input] ∉ [alphabet] and this character <> '=' *)
256266 error_msgf "Malformed input"
267267+ | exception Too_much_input ->
268268+ error_msgf "Too much input"
257269258270let decode ?pad ?(alphabet = default_alphabet) ?off ?len input =
259271 match decode_sub ?pad alphabet ?off ?len input with