12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697(* https://github.com/mirage/ocaml-hex/blob/85fe242997b61dc2f07ca1b62600b4d01ac0da94/lib/hex.ml
*
* Copyright (c) 2015 Trevor Summers Smith <trevorsummerssmith@gmail.com>
* Copyright (c) 2014 Thomas Gazagnaire <thomas@gazagnaire.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
*)letinvalid_argfmt=Printf.ksprintf(funstr->raise(Invalid_argumentstr))fmtlethexa="0123456789abcdef"andhexa1="0000000000000000111111111111111122222222222222223333333333333333444444444444444455555555555555556666666666666666777777777777777788888888888888889999999999999999aaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbccccccccccccccccddddddddddddddddeeeeeeeeeeeeeeeeffffffffffffffff"andhexa2="0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"letof_charc=letx=Char.codecin(hexa.[xlsr4],hexa.[xland0xf])letto_charxy=letcodec=matchcwith|'0'..'9'->Char.codec-48(* Char.code '0' *)|'A'..'F'->Char.codec-55(* Char.code 'A' + 10 *)|'a'..'f'->Char.codec-87(* Char.code 'a' + 10 *)|_->invalid_arg"Hex.to_char: %d is an invalid char"(Char.codec)inChar.chr((codexlsl4)+codey)letof_string_fasts=letlen=String.lengthsinletbuf=Bytes.create(len*2)infori=0tolen-1doBytes.unsafe_setbuf(i*2)(String.unsafe_gethexa1(Char.code(String.unsafe_getsi)));Bytes.unsafe_setbuf(succ(i*2))(String.unsafe_gethexa2(Char.code(String.unsafe_getsi)))done;`Hex(Bytes.to_stringbuf)letof_helper~ignore(next:int->char)len=letbuf=Buffer.createleninfori=0tolen-1doletc=nextiinifList.memcignorethen()elseletx,y=of_charcinBuffer.add_charbufx;Buffer.add_charbufydone;`Hex(Buffer.contentsbuf)letof_string?(ignore=[])s=matchignorewith|[]->of_string_fasts|ignore->of_helper~ignore(funi->s.[i])(String.lengths)letof_bytes?ignoreb=of_string?ignore(Bytes.to_stringb)letto_helper~empty_return~create~set(`Hexs)=ifs=""thenempty_returnelseletn=String.lengthsinletbuf=create(n/2)inletrecauxij=ifi>=nthen()elseifj>=ntheninvalid_arg"Hex conversion: Hex string cannot have an odd number of characters."else(setbuf(i/2)(to_chars.[i]s.[j]);aux(j+1)(j+2))inaux01;bufletto_byteshex=to_helper~empty_return:Bytes.empty~create:Bytes.create~set:Bytes.sethexletto_stringhex=Bytes.to_string@@to_byteshexletshow(`Hexhex)=hex