My working unpac space for OCaml projects in development
0
fork

Configure Feed

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

Merge opam/patches/ohex

+352
+8
vendor/opam/ohex/CHANGES.md
··· 1 + ## v0.2.0 (2024-03-18) 2 + 3 + * Rename pp to pp_hexdump 4 + * Add a pp which just prints the hex with some spaces, but no newlines 5 + 6 + ## v0.1.0 (2024-03-14) 7 + 8 + * Initial release
+23
vendor/opam/ohex/LICENSE.md
··· 1 + Copyright (c) 2024, Hannes Mehnert 2 + All rights reserved. 3 + 4 + Redistribution and use in source and binary forms, with or without modification, 5 + are permitted provided that the following conditions are met: 6 + 7 + * Redistributions of source code must retain the above copyright notice, this 8 + list of conditions and the following disclaimer. 9 + 10 + * Redistributions in binary form must reproduce the above copyright notice, this 11 + list of conditions and the following disclaimer in the documentation and/or 12 + other materials provided with the distribution. 13 + 14 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 + ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 18 + ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 21 + ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+10
vendor/opam/ohex/README.md
··· 1 + ## oHEX 2 + 3 + This package with minimal dependency cone provides functionality to decode and 4 + encode strings into hexadecimal representation. 5 + 6 + As example, `Ohex.decode "4142" = "AB"`. And `Ohex.encode "AB" = "4142"`. 7 + 8 + There's even the property, for all strings s: `Ohex.(decode (encode s)) = s`. 9 + 10 + A pretty-printer is provided as well.
+9
vendor/opam/ohex/dune
··· 1 + (library 2 + (name ohex) 3 + (public_name ohex) 4 + (modules ohex)) 5 + 6 + (test 7 + (name tests) 8 + (modules tests) 9 + (libraries alcotest ohex))
+3
vendor/opam/ohex/dune-project
··· 1 + (lang dune 2.7) 2 + (name ohex) 3 + (formatting disabled)
+121
vendor/opam/ohex/ohex.ml
··· 1 + 2 + let string_fold f acc str = 3 + let st = ref acc in 4 + String.iter (fun c -> st := f !st c) str; 5 + !st 6 + 7 + let is_space = function 8 + | ' ' | '\n' | '\r' | '\t' -> true 9 + | _ -> false 10 + 11 + let digit = function 12 + | '0'..'9' as c -> int_of_char c - 0x30 13 + | 'A'..'F' as c -> int_of_char c - 0x41 + 10 14 + | 'a'..'f' as c -> int_of_char c - 0x61 + 10 15 + | _ -> invalid_arg "bad character" 16 + 17 + let required_length ?(skip_whitespace = true) src = 18 + let req = 19 + string_fold (fun r c -> 20 + if skip_whitespace && is_space c then 21 + r 22 + else ( 23 + ignore (digit c); 24 + succ r)) 25 + 0 src 26 + in 27 + if req mod 2 = 0 then 28 + req / 2 29 + else 30 + invalid_arg "leftover byte in hex string" 31 + 32 + let decode_into ?(skip_whitespace = true) src tgt ?(off = 0) () = 33 + let fold f acc str = 34 + let st = ref acc in 35 + String.iter (fun c -> st := f !st c) str; 36 + !st 37 + in 38 + let chars, leftover = 39 + fold (fun (chars, leftover) c -> 40 + if skip_whitespace && is_space c then 41 + chars, leftover 42 + else 43 + let c = digit c in 44 + match leftover with 45 + | None -> chars, Some (c lsl 4) 46 + | Some c' -> (c' lor c) :: chars, None) 47 + ([], None) src 48 + in 49 + let chars = List.rev chars in 50 + if leftover <> None then 51 + invalid_arg "leftover byte in hex string"; 52 + List.iteri (fun idx c -> Bytes.set_uint8 tgt (off + idx) c) chars 53 + 54 + let decode ?(skip_whitespace = true) src = 55 + let len = required_length ~skip_whitespace src in 56 + let buf = Bytes.create len in 57 + decode_into ~skip_whitespace src buf (); 58 + Bytes.unsafe_to_string buf 59 + 60 + let hex_map = "0123456789abcdef" 61 + 62 + let encode_into src tgt ?(off = 0) () = 63 + String.iteri (fun idx c -> 64 + let hi, lo = 65 + let i = int_of_char c in 66 + hex_map.[i lsr 4], hex_map.[i land 0x0F] 67 + in 68 + Bytes.set tgt (idx * 2 + off) hi; 69 + Bytes.set tgt (idx * 2 + off + 1) lo) 70 + src 71 + 72 + let encode src = 73 + let buf = Bytes.create (String.length src * 2) in 74 + encode_into src buf (); 75 + Bytes.unsafe_to_string buf 76 + 77 + let printable_ascii c = 78 + let i = int_of_char c in 79 + not (i < 0x20 || i >= 0x7f) 80 + 81 + let pp ppf s = 82 + String.iteri (fun idx c -> 83 + Format.fprintf ppf "%02x" (int_of_char c); 84 + if idx mod 2 = 1 then 85 + Format.pp_print_string ppf " "; 86 + if idx mod 8 = 7 then 87 + Format.pp_print_string ppf " ") 88 + s 89 + 90 + let pp_hexdump ?(row_numbers = true) ?(chars = true) () ppf s = 91 + String.iteri (fun idx c -> 92 + if idx mod 16 = 0 && row_numbers then 93 + Format.fprintf ppf "%06x " idx; 94 + Format.fprintf ppf "%02x" (int_of_char c); 95 + if idx mod 2 = 1 then 96 + Format.pp_print_string ppf " "; 97 + if idx mod 8 = 7 then 98 + Format.pp_print_string ppf " "; 99 + if idx mod 16 = 15 && chars then 100 + String.iter (fun c -> 101 + Format.pp_print_char ppf (if printable_ascii c then c else '.')) 102 + (String.sub s (idx - 15) 16); 103 + if idx mod 16 = 15 then 104 + Format.pp_print_string ppf "\n") 105 + s; 106 + (if chars then 107 + let last_n, pad = 108 + let l = String.length s in 109 + let pad = 16 - (l mod 16) in 110 + let pad = if pad = 16 then 0 else pad in 111 + String.sub s (l - (l mod 16)) (l mod 16), 112 + pad 113 + in 114 + if pad > 0 then 115 + let pad_chars = pad * 2 + (pad + 1) / 2 + (if pad > 8 then 1 else 0) + 1 in 116 + Format.pp_print_string ppf (String.make pad_chars ' '); 117 + String.iter (fun c -> 118 + Format.pp_print_char ppf (if printable_ascii c then c else '.')) 119 + last_n); 120 + if String.length s mod 16 <> 0 then 121 + Format.pp_print_string ppf "\n"
+54
vendor/opam/ohex/ohex.mli
··· 1 + (** Convert from and to hexadecimal representation. *) 2 + 3 + val required_length : ?skip_whitespace:bool -> string -> int 4 + (** [required_length ~skip_whitespace s] returns the length needed when the 5 + hex string [s] would be decoded into a sequence of octets. The argument 6 + [skip_whitespace] defaults to [true], and skips any whitespace characters 7 + (' ', '\n', '\r', '\t'). This function is useful for estimating the space 8 + required for [decode_into]. 9 + 10 + @raise Invalid_argument if any character in [s] is not a hex character, or 11 + an odd amount of characters are present. *) 12 + 13 + val decode : ?skip_whitespace:bool -> string -> string 14 + (** [decode ~skip_whitespace s] decodes a hex string [s] into a sequence of 15 + octets. The argument [skip_whitespace] defaults to [true], and skips any 16 + whitespace characters in [s] (' ', '\n', '\r', '\t'). An example: 17 + [decode "4142" = "AB"]. 18 + 19 + @raise Invalid_argument if any character in [s] is not a hex character, or 20 + an odd amount of characters are present. *) 21 + 22 + val decode_into : ?skip_whitespace:bool -> string -> bytes -> ?off:int -> unit 23 + -> unit 24 + (** [decode_into ~skip_whitespace s dst ~off ()] decodes [s] into [dst] 25 + starting at [off] (defaults to 0). The argument [skip_whitespace] defaults 26 + to [true] and skips any whitespace characters. 27 + 28 + @raise Invalid_argument if any character in [s] is not a hex character, an 29 + odd amount of characters are present, or [dst] does not contain enough 30 + space. *) 31 + 32 + val encode : string -> string 33 + (** [encode s] encodes [s] into a freshly allocated string of double size, where 34 + each character in [s] is encoded as two hex digits in the returned string. 35 + An example: [encode "AB" = "4142"]. 36 + *) 37 + 38 + val encode_into : string -> bytes -> ?off:int -> unit -> unit 39 + (** [encode_into s dst ~off ()] encodes [s] into [dst] starting at [off] 40 + (defaults to 0). Each character is encoded as two hex digits in [dst]. 41 + 42 + @raise Invalid_argument if [dst] does not contain enough space. *) 43 + 44 + val pp : Format.formatter -> string -> unit 45 + (** [pp ppf s] pretty-prints the string [s] in hexadecimal. Some spaces are 46 + emitted for easier readability. No newline is emitted. *) 47 + 48 + val pp_hexdump : ?row_numbers:bool -> ?chars:bool -> unit -> 49 + Format.formatter -> string -> unit 50 + (** [pp_hexdump ~row_numbers ~chars () ppf s] pretty-prints the string [s] in 51 + hexadecimal (similar to [hexdump -C]). If [row_numbers] is provided 52 + (defaults to [true]), each output line is prefixed with the row number. 53 + If [chars] is provided (defaults to [true]), in the last column the ASCII 54 + string is printed (non-printable characters are printed as '.'). *)
+22
vendor/opam/ohex/ohex.opam
··· 1 + opam-version: "2.0" 2 + maintainer: "Hannes Mehnert <hannes@mehnert.org>" 3 + authors: "Hannes Mehnert <hannes@mehnert.org>" 4 + license: "BSD-2-Clause" 5 + homepage: "https://git.robur.coop/robur/ohex" 6 + doc: "https://robur-coop.github.io/ohex/doc" 7 + bug-reports: "https://git.robur.coop/robur/ohex/issues" 8 + depends: [ 9 + "ocaml" {>= "4.08.0"} 10 + "dune" {>= "2.7"} 11 + "alcotest" {with-test} 12 + ] 13 + build: [ 14 + ["dune" "subst"] {dev} 15 + ["dune" "build" "-p" name "-j" jobs] 16 + ["dune" "runtest" "-p" name "-j" jobs] {with-test} 17 + ] 18 + dev-repo: "git+https://git.robur.coop/robur/ohex.git" 19 + synopsis: "Hexadecimal encoding and decoding" 20 + description: """ 21 + A library to encode and decode hexadecimal byte sequences. 22 + """
+102
vendor/opam/ohex/tests.ml
··· 1 + 2 + let tests = [ 3 + "", 0, ""; 4 + "41", 1, "A"; 5 + "41 41", 2, "AA"; 6 + " 41 41 ", 2, "AA"; 7 + " 414 1", 2, "AA"; 8 + ] 9 + 10 + let len_dec_tests = 11 + List.mapi (fun i (s, len, v) -> 12 + string_of_int i ^ " is correct", `Quick, 13 + (fun () -> 14 + Alcotest.(check int "required length" len (Ohex.required_length s)); 15 + Alcotest.(check string "decode works fine" v (Ohex.decode s)))) 16 + tests 17 + 18 + let bad_char_input = [ "W" ; "AAWW" ; "WWAA" ] 19 + 20 + let leftover_input = [ "AAA" ; "A" ] 21 + 22 + let bad_input_ws = [ " "; " AA" ; "AA " ; "A A" ] 23 + 24 + let bad_len_dec_tests = 25 + (List.mapi (fun i s -> 26 + string_of_int i ^ " fails (bad character)", `Quick, 27 + (fun () -> 28 + Alcotest.(check_raises "required length raises" 29 + (Invalid_argument "bad character") 30 + (fun () -> ignore (Ohex.required_length s))); 31 + Alcotest.(check_raises "decode raises" 32 + (Invalid_argument "bad character") 33 + (fun () -> ignore (Ohex.decode s))))) 34 + bad_char_input) @ 35 + (List.mapi (fun i s -> 36 + string_of_int i ^ " fails (leftover)", `Quick, 37 + (fun () -> 38 + Alcotest.(check_raises "required length raises" 39 + (Invalid_argument "leftover byte in hex string") 40 + (fun () -> ignore (Ohex.required_length ~skip_whitespace:false s))); 41 + Alcotest.(check_raises "decode raises" 42 + (Invalid_argument "leftover byte in hex string") 43 + (fun () -> ignore (Ohex.decode ~skip_whitespace:false s))))) 44 + leftover_input) @ 45 + (List.mapi (fun i s -> 46 + string_of_int i ^ " fails (skip_whitespace = false)", `Quick, 47 + (fun () -> 48 + Alcotest.(check_raises "required length raises" 49 + (Invalid_argument "bad character") 50 + (fun () -> ignore (Ohex.required_length ~skip_whitespace:false s))); 51 + Alcotest.(check_raises "decode raises" 52 + (Invalid_argument "bad character") 53 + (fun () -> ignore (Ohex.decode ~skip_whitespace:false s))))) 54 + bad_input_ws) 55 + 56 + let enc_tests = [ 57 + "A", "41", 2; 58 + "AA", "4141", 4; 59 + "AAA", "414141", 6; 60 + ] 61 + 62 + let enc_tests = 63 + List.mapi (fun i (v, hex, l) -> 64 + string_of_int i ^ " is correct", `Quick, 65 + (fun () -> 66 + Alcotest.(check string "encode works" hex (Ohex.encode v)); 67 + let buf = Bytes.create l in 68 + Ohex.encode_into v buf ~off:0 (); 69 + Alcotest.(check string "encode_into works" hex (Bytes.unsafe_to_string buf)))) 70 + enc_tests 71 + 72 + let dec_enc () = 73 + let random_string () = 74 + let size = Random.int 128 in 75 + let buf = Bytes.create size in 76 + for i = 0 to size - 1 do 77 + Bytes.set_uint8 buf i (Random.int 256) 78 + done; 79 + Bytes.unsafe_to_string buf 80 + in 81 + for i = 0 to 10_000 do 82 + let input = random_string () in 83 + Alcotest.(check string ("dec (enc s) = s " ^ string_of_int i) 84 + input Ohex.(decode (encode input))); 85 + Alcotest.(check string ("dec ~skip_ws:false (enc s) = s " ^ string_of_int i) 86 + input Ohex.(decode ~skip_whitespace:false (encode input))); 87 + let buf = Bytes.create (String.length input * 2) in 88 + Ohex.encode_into input buf ~off:0 (); 89 + let out = Bytes.create (String.length input) in 90 + Ohex.decode_into (Bytes.unsafe_to_string buf) out ~off:0 (); 91 + Alcotest.(check string ("dec_into (enc_into s) = s " ^ string_of_int i) 92 + input (Bytes.unsafe_to_string out)) 93 + done 94 + 95 + let suites = [ 96 + "length and decode pass", len_dec_tests ; 97 + "bad input", bad_len_dec_tests ; 98 + "encode tests", enc_tests ; 99 + "decode encode", [ "decode (encode s) = s", `Quick, dec_enc ]; 100 + ] 101 + 102 + let () = Alcotest.run "hex tests" suites