diff options
author | Philipp Gesang <phg@phi-gamma.net> | 2018-11-03 19:58:42 +0100 |
---|---|---|
committer | Philipp Gesang <phg@phi-gamma.net> | 2018-11-03 20:23:23 +0100 |
commit | 55f50ddaf31fe1cb4cda4d15e387280e8f7a8242 (patch) | |
tree | 45b2ad303bc8a925aabc43f1910e0565db410f90 | |
parent | 2a999d7988d886b5e887988c68095addbbf9a794 (diff) | |
download | ocaml-sid-55f50ddaf31fe1cb4cda4d15e387280e8f7a8242.tar.gz |
sid: add channel handlers for binary representation
-rw-r--r-- | sid.ml | 26 | ||||
-rw-r--r-- | sid.mli | 12 |
2 files changed, 38 insertions, 0 deletions
@@ -221,6 +221,32 @@ module PacketRep = struct (* [MS-DTYP] 2.4.22 *) ; sid_sub_auths = sas } + let from_channel ?(endian=Little) ic = + try + let v = input_byte ic in + if v <> 0x01 then + Error (Printf.sprintf + "input malformed: expected SID version=0x01, got 0x%0.2x" v) else + let nsa = input_byte ic in + if max_subauth_count < nsa then + Error (Printf.sprintf + "input malformed: up to %d subAuthority elements permitted, \ + %d specified" + max_subauth_count nsa) else + let n = sizeof_ident_auth + nsa * sizeof_sub_auth in + let b = Bytes.make (2 + n) '\x00' in + Bytes.set b 0 '\x01'; + Bytes.set b 1 (char_of_int nsa); + really_input ic b 2 n; + decode ~endian b + with End_of_file -> + Error (Printf.sprintf + "input malformed: unexpected end of file at offset %d \ + parsing SID" (pos_in ic)) + + let to_channel ?(endian=Little) oc s = + encode ~endian s |> output_bytes oc + end (* [module PacketRep] *) module WellKnown = struct @@ -40,12 +40,24 @@ module StringFmt : module PacketRep : sig type endian = Big | Little + (** Specify the endianness when internalizing integers. Only relevant for + subauthorities as the identifier authority is specified as big endian. + The default is always [Little]. *) + val decode : ?endian:endian -> bytes -> (t, string) result (** [decode endian b] decode the byte buffer [b] as a SID. *) val encode : ?endian:endian -> t -> bytes (** [encode endian s] convert SID [s] to the packet representation encoding subauthorities in endianness [endian]. *) + + val from_channel : ?endian:endian -> in_channel -> (t, string) result + (** [from_channel endian ic] read binary SID from [ic] with endianness + [endian]. *) + + val to_channel : ?endian:endian -> out_channel -> t -> unit + (** [to_channel endian oc s] write SID [s] in packet representation + to channel [oc] with endianness [endian]. *) end (** Pre-defined SID constansts and constructors with fixed identifier |