-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
47 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,36 @@ | ||
let decompress_base64_str ?(buffer_size_factor = 12) base64_str = | ||
open Bigarray | ||
|
||
let decode_base64_exn base64_str = | ||
let compressed_data = Base64.decode_exn base64_str in | ||
let compressed_len = String.length compressed_data in | ||
let compressed_ba = Bigarray.Array1.create Bigarray.char Bigarray.c_layout compressed_len in | ||
let compressed_ba = Array1.create char c_layout compressed_len in | ||
for i = 0 to compressed_len - 1 do | ||
Bigarray.Array1.set compressed_ba i compressed_data.[i] | ||
Array1.set compressed_ba i compressed_data.[i] | ||
done; | ||
compressed_ba, compressed_len | ||
;; | ||
|
||
let decompress_base64_str ?(buffer_size_factor = 12) base64_str = | ||
let compressed_ba, compressed_len = decode_base64_exn base64_str in | ||
let inflate_state = Zlib.create_inflate () in | ||
inflate_state.Zlib.in_buf <- compressed_ba; | ||
inflate_state.Zlib.in_len <- compressed_len; | ||
inflate_state.in_buf <- compressed_ba; | ||
inflate_state.in_len <- compressed_len; | ||
let output_len = compressed_len * buffer_size_factor in | ||
let output_ba = Bigarray.Array1.create Bigarray.char Bigarray.c_layout output_len in | ||
inflate_state.Zlib.out_buf <- output_ba; | ||
inflate_state.Zlib.out_len <- output_len; | ||
let status = Zlib.flate inflate_state Zlib.Finish in | ||
let output_ba = Array1.create char c_layout output_len in | ||
inflate_state.out_buf <- output_ba; | ||
inflate_state.out_len <- output_len; | ||
let status = Zlib.flate inflate_state Finish in | ||
match status with | ||
| Zlib.Ok | Zlib.Stream_end -> | ||
let result_len = output_len - inflate_state.Zlib.out_len in | ||
let result = String.init result_len (fun i -> Bigarray.Array1.get output_ba i) in | ||
Some result | ||
| Zlib.Need_dict -> | ||
| Ok | Stream_end -> | ||
let result_len = output_len - inflate_state.out_len in | ||
Some (String.init result_len (fun i -> Array1.get output_ba i)) | ||
| Need_dict -> | ||
Printf.eprintf "Decompression error: Need dictionary\n"; | ||
None | ||
| Zlib.Buf_error -> | ||
| Buf_error -> | ||
Printf.eprintf "Decompression error: Buffer error\n"; | ||
None | ||
| Zlib.Data_error msg -> | ||
| Data_error msg -> | ||
Printf.eprintf "Decompression error: Data error (%s)\n" msg; | ||
None | ||
;; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
type t = string list | ||
|
||
let from_zlib_b64_str compressed_input = | ||
match Compression.Domain.decompress_base64_str compressed_input with | ||
| None -> | ||
Printf.printf "Decompression failed: Input may be corrupted or invalid.\n"; | ||
None | ||
| Some input -> | ||
(try | ||
let no_quotes = String.concat "" (String.split_on_char '"' input) in | ||
match Base64.decode no_quotes with | ||
| Ok decoded_data -> Some (String.split_on_char ':' decoded_data) | ||
| Error _ -> | ||
Printf.printf "Base64 decoding failed: Data may be corrupted or improperly encoded.\n"; | ||
None | ||
with | ||
| ex -> | ||
Printf.printf "Unexpected error: %s\n" (Printexc.to_string ex); | ||
None) | ||
;; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters