text
stringlengths
12
786k
type section_details = { names : string list ; flags : string option ; args : string list }
let dwarf_sections_in_order ( ) = let sections = [ DWARF Debug_info ; DWARF Debug_abbrev ; DWARF Debug_aranges ; DWARF Debug_str ; DWARF Debug_line ] in let dwarf_version_dependent_sections = match ! Dwarf_flags . gdwarf_version with | Four -> [ DWARF Debug_loc ; DWARF Debug_ranges ] | Five -> [ DWARF Debug_addr ; DWARF Debug_loclists ; DWARF Debug_rnglists ] in sections @ dwarf_version_dependent_sections
let details t ~ first_occurrence = let names , flags , args = match t , Target_system . derived_system ( ) with | DWARF dwarf , MacOS_like -> let name = match dwarf with | Debug_info -> " __debug_info " | Debug_abbrev -> " __debug_abbrev " | Debug_aranges -> " __debug_aranges " | Debug_addr -> " __debug_addr " | Debug_loc -> " __debug_loc " | Debug_ranges -> " __debug_ranges " | Debug_loclists -> " __debug_loclists " | Debug_rnglists -> " __debug_rnglists " | Debug_str -> " __debug_str " | Debug_line -> " __debug_line " in [ " __DWARF " ; name ] , None , [ " regular " ; " debug " ] | DWARF dwarf , _ -> let name = match dwarf with | Debug_info -> " . debug_info " | Debug_abbrev -> " . debug_abbrev " | Debug_aranges -> " . debug_aranges " | Debug_addr -> " . debug_addr " | Debug_loc -> " . debug_loc " | Debug_ranges -> " . debug_ranges " | Debug_loclists -> " . debug_loclists " | Debug_rnglists -> " . debug_rnglists " | Debug_str -> " . debug_str " | Debug_line -> " . debug_line " in let flags = if first_occurrence then Some " " else None in let args = if first_occurrence then [ " % progbits " ] else [ ] in [ name ] , flags , args in { names ; flags ; args }
let to_string t = let { names ; flags = _ ; args = _ } = details t ~ first_occurrence : true in String . concat " " names
let print ppf t = let str = match t with | DWARF Debug_info -> " ( DWARF Debug_info ) " | DWARF Debug_abbrev -> " ( DWARF Debug_abbrev ) " | DWARF Debug_aranges -> " ( DWARF Debug_aranges ) " | DWARF Debug_addr -> " ( DWARF Debug_addr ) " | DWARF Debug_loc -> " ( DWARF Debug_loc ) " | DWARF Debug_ranges -> " ( DWARF Debug_ranges ) " | DWARF Debug_loclists -> " ( DWARF Debug_loclists ) " | DWARF Debug_rnglists -> " ( DWARF Debug_rnglists ) " | DWARF Debug_str -> " ( DWARF Debug_str ) " | DWARF Debug_line -> " ( DWARF Debug_line ) " in Format . pp_print_string ppf str
let compare t1 t2 = Stdlib . compare t1 t2
let equal t1 t2 = Stdlib . compare t1 t2 = 0
let should_be_escaped = function | ' A ' . . ' Z ' | ' a ' . . ' z ' | ' 0 ' . . ' 9 ' | ' _ ' -> false | c -> true
module Thing = struct type t = string let compare = String . compare let equal = String . equal let hash = Hashtbl . hash let output chan t = Printf . fprintf chan " % s " t let print = Format . pp_print_string end
let create name = name
let escape name = let escaped_nb = ref 0 in for i = 0 to String . length name - 1 do if should_be_escaped ( String . unsafe_get name i ) then incr escaped_nb done ; if ! escaped_nb = 0 then name else let b = Buffer . create ( String . length name + ( 2 * ! escaped_nb ) ) in String . iter ( fun c -> if should_be_escaped c then Printf . bprintf b " $% 02x " ( Char . code c ) else Buffer . add_char b c ) name ; Buffer . contents b
let symbol_prefix ( ) = match Target_system . architecture ( ) with | IA32 | X86_64 | AArch64 -> begin match Target_system . derived_system ( ) with | Linux | Win32 | Win64 | MinGW_32 | MinGW_64 | Cygwin | FreeBSD | NetBSD | OpenBSD | Generic_BSD | Solaris | BeOS | GNU | Dragonfly | Unknown -> " " | MacOS_like -> " _ " end | ARM | POWER | Z | Riscv -> " "
let to_escaped_string ? suffix ~ symbol_prefix t = let suffix = match suffix with None -> " " | Some suffix -> suffix in symbol_prefix ^ escape t ^ suffix
let encode ? without_prefix t = let symbol_prefix = match without_prefix with None -> symbol_prefix ( ) | Some ( ) -> " " in to_escaped_string ~ symbol_prefix t
module S = struct type ' a t = ' a Core . asn type ' a element = ' a Core . element type ' a sequence = ' a Core . sequence include Asn_combinators let ( error , parse_error ) = Core . ( error , parse_error ) end
type ' a t = ' a S . t
type encoding = { mk_decoder : ' a . ' a t -> Cstruct . t -> ' a * Cstruct . t ; mk_encoder : ' a . ' a t -> ' a -> Asn_writer . t }
let ber = { mk_decoder = Asn_ber_der . R . compile_ber ; mk_encoder = Asn_ber_der . W . ber_to_writer ; }
let der = { mk_decoder = Asn_ber_der . R . compile_der ; mk_encoder = Asn_ber_der . W . der_to_writer ; }
type ' a codec = Codec of ( Cstruct . t -> ( ' a * Cstruct . t ) ) * ( ' a -> Asn_writer . t )
let codec { mk_encoder ; mk_decoder } asn = let ( ) = Core . validate asn in Codec ( mk_decoder asn , mk_encoder asn )
let encode ( Codec ( _ , enc ) ) a = Asn_writer . to_cstruct ( enc a )
let encode_into ( Codec ( _ , enc ) ) a = Asn_writer . to_writer ( enc a )
let decode ( Codec ( dec , _ ) ) b = try Ok ( dec b ) with Core . Parse_error err -> Error err
let ( ) @? oa a = match oa with Some x -> x | None -> a
module Seq = struct type ' r f = { f : ' a . ' a -> ' a asn -> ' r -> ' r } let rec fold_with_value : type a . ' r f -> ' r -> a -> a sequence -> ' r = fun f r a -> function | Last ( Required ( _ , asn ) ) -> f . f a asn r | Last ( Optional ( _ , asn ) ) -> ( match a with None -> r | Some a ' -> f . f a ' asn r ) | Pair ( Required ( _ , asn ) , asns ) -> let ( a1 , a2 ) = a in f . f a1 asn ( fold_with_value f r a2 asns ) | Pair ( Optional ( _ , asn ) , asns ) -> match a with | ( None , a2 ) -> fold_with_value f r a2 asns | ( Some a1 , a2 ) -> f . f a1 asn ( fold_with_value f r a2 asns ) end
module R = struct module G = Generic type config = { strict : bool } type coding = | Primitive of int | Constructed of int | Constructed_indefinite module Header = struct open Cstruct let error cs fmt = parse_error ( " Header : at % a : " ^^ fmt ) pp_cs cs let ck_redundant cs cfg ( n : int ) limit = if cfg . strict && n < limit then error cs " redundant form " let big_tag cs = let rec go acc = function | 8 -> error cs " big tag : too long " | i -> let b = get_uint8 cs i in let x = Int64 . of_int ( b land 0x7f ) in match ( Int64 . ( acc lsl 7 + x ) , b land 0x80 ) with | ( 0L , _ ) -> error cs " big tag : leading 0 " | ( acc , 0 ) -> ( match Int64 . to_nat_checked acc with | Some x -> ( x , succ i ) | None -> error cs " big tag : overflow : % Li " acc ) | ( acc , _ ) -> go acc ( succ i ) in go 0L 0 let big_len cfg cs = function 0 -> error cs " empty length " | n -> let rec f cs i = function 0 -> 0L | n -> match get_uint8 cs i with 0 when cfg . strict -> error cs " redundant length " | 0 -> f cs ( i + 1 ) ( n - 1 ) | _ when n > 8 -> error cs " length overflow " | x -> g ( Int64 . of_int x ) cs ( i + 1 ) ( n - 1 ) and g acc cs i = function 0 -> acc | n -> let acc = Int64 . ( acc lsl 8 + of_int ( get_uint8 cs i ) ) in g acc cs ( i + 1 ) ( n - 1 ) in match f cs 0 n |> Int64 . to_nat_checked with Some x -> x | _ -> error cs " length overflow " let parse cfg cs = let t0 = get_uint8 cs 0 in let ( tag_v , off_len ) = match t0 land 0x1f with | 0x1f -> let ( n , i ) = big_tag ( shift cs 1 ) in ck_redundant cs cfg n 0x1f ; ( n , i + 1 ) | x -> ( x , 1 ) in let l0 = get_uint8 cs off_len in let lbody = l0 land 0x7f in let ( len , off_end ) = if l0 <= 0x80 then ( lbody , off_len + 1 ) else let n = big_len cfg ( shift cs ( off_len + 1 ) ) lbody in ck_redundant cs cfg n 0x7f ; ( n , off_len + 1 + lbody ) in let tag = match t0 land 0xc0 with | 0x00 -> Tag . Universal tag_v | 0x40 -> Tag . Application tag_v | 0x80 -> Tag . Context_specific tag_v | _ -> Tag . Private tag_v and coding = match ( t0 land 0x20 , l0 ) with | ( 0 , 0x80 ) -> error cs " primitive and indefinite length " | ( 0 , _ ) -> Primitive len | ( _ , 0x80 ) -> Constructed_indefinite | _ -> Constructed len in ( tag , off_end , coding ) end module Gen = struct let eof1 cs = cs . Cstruct . len = 0 and eof2 cs = Cstruct . LE . get_uint16 cs 0 = 0 let split_off cs off n = let k = off + n in Cstruct . ( sub cs off n , sub cs k ( length cs - k ) ) let rec children cfg eof acc cs = if eof cs then ( List . rev acc , cs ) else let ( g , cs ) = node cfg cs in children cfg eof ( g :: acc ) cs and node cfg cs = let ( tag , off , coding ) = Header . parse cfg cs in match coding with | Primitive n -> let ( hd , tl ) = split_off cs off n in ( G . Prim ( tag , hd ) , tl ) | Constructed n -> let ( hd , tl ) = split_off cs off n in let ( gs , _ ) = children cfg eof1 [ ] hd in ( G . Cons ( tag , gs ) , tl ) | Constructed_indefinite when cfg . strict -> parse_error " Constructed indefinite form " | Constructed_indefinite -> let ( gs , tl ) = children cfg eof2 [ ] ( Cstruct . shift cs off ) in ( G . Cons ( tag , gs ) , Cstruct . shift tl 2 ) let parse cfg cs = try node cfg cs with Invalid_argument _ -> parse_error " Unexpected EOF : % a " pp_cs cs end module TM = Map . Make ( Tag ) module Cache = Asn_cache . Make ( struct type ' a k = ' a asn endo type ' a v = G . t -> ' a let mapv = ( . ) & end ) let err_type ( ? form ` = Both ) t g = parse_error " Type mismatch : expected : ( % a % a ) got : % a " G . pp_form_name form Tag . pp t G . pp_tag g let primitive t f = function | G . Prim ( t1 , bs ) when Tag . equal t t1 -> f bs | g -> err_type ~ form ` : Prim t g let constructed t f = function | G . Cons ( t1 , gs ) when Tag . equal t t1 -> f gs | g -> err_type ~ form ` : Cons t g let string_like ( type a ) c t ( module P : Prim . Prim_s with type t = a ) = let rec p = function | G . Prim ( t1 , bs ) when Tag . equal t t1 -> P . of_cstruct bs | G . Cons ( t1 , gs ) when Tag . equal t t1 && not c . strict -> P . concat ( List . map p gs ) | g -> err_type t g in p let c_prim : type a . config -> tag -> a prim -> G . t -> a = fun c tag -> function | Bool -> primitive tag Prim . Boolean . of_cstruct | Int -> primitive tag Prim . Integer . of_cstruct | Bits -> string_like c tag ( module Prim . Bits ) | Octets -> string_like c tag ( module Prim . Octets ) | Null -> primitive tag Prim . Null . of_cstruct | OID -> primitive tag Prim . OID . of_cstruct | CharString -> string_like c tag ( module Prim . Gen_string ) let peek asn = match tag_set asn with | [ tag ] -> fun g -> Tag . equal ( G . tag g ) tag | tags -> fun g -> let tag = G . tag g in List . exists ( fun t -> Tag . equal t tag ) tags type opt = Cache . t * config let rec c_asn : type a . a asn -> opt : opt -> G . t -> a = fun asn ~ opt -> let rec go : type a . ? t : tag -> a asn -> G . t -> a = fun ? t -> function | Iso ( f , _ , _ , a ) -> f . & go ? t a | Fix ( fa , var ) as fix -> let p = lazy ( go ? t ( fa fix ) ) in Cache . intern ( fst opt ) var fa @@ fun g -> Lazy . force p g | Sequence s -> constructed ( t @? seq_tag ) ( c_seq s ~ opt ) | Sequence_of a -> constructed ( t @? seq_tag ) ( List . map ( c_asn a ~ opt ) ) | Set s -> constructed ( t @? set_tag ) ( c_set s ~ opt ) | Set_of a -> constructed ( t @? set_tag ) ( List . map ( c_asn a ~ opt ) ) | Implicit ( t0 , a ) -> go ~ t ( : t @? t0 ) a | Explicit ( t0 , a ) -> constructed ( t @? t0 ) ( c_explicit a ~ opt ) | Choice ( a1 , a2 ) -> let ( p1 , p2 ) = ( c_asn a1 ~ opt , c_asn a2 ~ opt ) and accepts1 = peek a1 in fun g -> if accepts1 g then L ( p1 g ) else R ( p2 g ) | Prim p -> c_prim ( snd opt ) ( t @? tag_of_p p ) p in go asn and c_explicit : type a . a asn -> opt : opt -> G . t list -> a = fun a ~ opt -> let p = c_asn a ~ opt in function | [ g ] -> p g | gs -> parse_error " EXPLICIT : sequence : % a " ( pp_dump_list G . pp_tag ) gs and c_seq : type a . a sequence -> opt : opt -> G . t list -> a = fun s ~ opt -> let rec seq : type a . a sequence -> G . t list -> a = function | Pair ( e , s ) -> let ( p1 , p2 ) = ( element e , c_seq s ~ opt ) in fun gs -> let ( r , gs ' ) = p1 gs in ( r , p2 gs ' ) | Last e -> let p = element e in fun gs -> match p gs with ( a , [ ] ) -> a | ( _ , gs ) -> parse_error " SEQUENCE : trailing : % a " ( pp_dump_list G . pp_tag ) gs and element : type a . a element -> G . t list -> a * G . t list = function | Required ( lbl , a ) -> let p = c_asn a ~ opt in ( function | g :: gs -> ( p g , gs ) | [ ] -> parse_error " SEQUENCE : missing required : % s " ( label lbl ) ) | Optional ( _ , a ) -> let ( p , accepts ) = ( c_asn a ~ opt , peek a ) in function | g :: gs when accepts g -> ( Some ( p g ) , gs ) | gs -> ( None , gs ) in seq s and c_set : type a . a sequence -> opt : opt -> G . t list -> a = fun s ~ opt -> let module P = struct module C = Asn_core type ' a or_missing = Found of ' a | Miss of string option type _ element = | Required : ' a or_missing -> ' a element | Optional : ' a or_missing -> ' a option element type _ sequence = | Last : ' a element -> ' a sequence | Pair : ' a element * ' b sequence -> ( ' a * ' b ) sequence let rec of_sequence : type a . a C . sequence -> a sequence = function | C . Last ( C . Required ( lbl , _ ) ) -> Last ( Required ( Miss lbl ) ) | C . Last ( C . Optional ( lbl , _ ) ) -> Last ( Optional ( Miss lbl ) ) | C . Pair ( C . Required ( lbl , _ ) , t ) -> Pair ( Required ( Miss lbl ) , of_sequence t ) | C . Pair ( C . Optional ( lbl , _ ) , t ) -> Pair ( Optional ( Miss lbl ) , of_sequence t ) let to_tuple = let rec element : type a . a element -> a = function | Required ( Miss lbl ) -> parse_error " SET : missing required : % s " ( label lbl ) | Required ( Found a ) -> a | Optional ( Miss _ ) -> None | Optional ( Found a ) -> Some a and seq : type a . a sequence -> a = function | Last e -> element e | Pair ( e , tl ) -> ( element e , seq tl ) in seq let found_r a = Required ( Found a ) and found_o a = Optional ( Found a ) end in let put r = function P . Pair ( _ , tl ) -> P . Pair ( r , tl ) | _ -> assert false and wrap f = function P . Pair ( e , tl ) -> P . Pair ( e , f tl ) | _ -> assert false in let rec element : type a . a element -> tags * ( G . t -> a P . element ) = function | Required ( _ , a ) -> ( tag_set a , P . found_r . & c_asn a ~ opt ) | Optional ( _ , a ) -> ( tag_set a , P . found_o . & c_asn a ~ opt ) and seq : type a b . ( a P . sequence endo -> b P . sequence endo ) -> a sequence -> ( tags * ( G . t -> b P . sequence endo ) ) list = fun k -> function | Last e -> let ( tags , p ) = element e in [ ( tags , ( fun e ' -> k ( fun _ -> P . Last e ' ) ) . & p ) ] | Pair ( e , tl ) -> let ( tags , p ) = element e in ( tags , k . & put . & p ) :: seq ( k . & wrap ) tl in let parsers = List . fold_right ( fun ( tags , p ) -> List . fold_right ( fun tag -> TM . add tag p ) tags ) ( seq id s ) TM . empty in let rec step acc ps = function | [ ] -> P . to_tuple acc | g :: gs -> let p = try TM . find ( G . tag g ) ps with Not_found -> parse_error " SET : unexpected : % a " G . pp_tag g in step ( p g acc ) ( TM . remove ( G . tag g ) ps ) gs in step ( P . of_sequence s ) parsers let ( compile_ber , compile_der ) = let compile cfg asn = let p = c_asn asn ~ opt ( : Cache . create ( ) , cfg ) in fun cs -> let ( g , cs ' ) = Gen . parse cfg cs in ( p g , cs ' ) in ( fun asn -> compile { strict = false } asn ) , ( fun asn -> compile { strict = true } asn ) end
module W = struct let ( ) <+> = Writer . ( ) <+> let e_big_tag tag = let cons x = function [ ] -> [ x ] | xs -> ( x lor 0x80 ) :: xs in let rec loop acc = function | 0 -> acc | n -> loop ( cons ( n land 0x7f ) acc ) ( n lsr 7 ) in loop [ ] tag let e_big_length length = let rec loop acc = function | 0 -> acc | n -> loop ( n land 0xff :: acc ) ( n lsr 8 ) in loop [ ] length let e_header tag mode len = let ( klass , tagn ) = let open Tag in match tag with | Universal n -> ( 0x00 , n ) | Application n -> ( 0x40 , n ) | Context_specific n -> ( 0x80 , n ) | Private n -> ( 0xc0 , n ) in let constructed = match mode with | ` Primitive -> 0x00 | ` Constructed -> 0x20 in ( if tagn < 0x1f then Writer . of_byte ( klass lor constructed lor tagn ) else Writer . of_byte ( klass lor constructed lor 0x1f ) <+> Writer . of_list ( e_big_tag tagn ) ) <+> ( if len <= 0x7f then Writer . of_byte len else let body = Writer . of_list ( e_big_length len ) in Writer . of_byte ( 0x80 lor Writer . len body ) <+> body ) type conf = { der : bool } let e_constructed tag body = e_header tag ` Constructed ( Writer . len body ) <+> body let e_primitive tag body = e_header tag ` Primitive ( Writer . len body ) <+> body let assert_length ? constr f a = match constr with | None -> ( ) | Some n -> let n ' = f a in if n <> n ' then invalid_arg " Encode : length : expected % d , got % d " n n ' let rec encode : type a . conf -> tag option -> a -> a asn -> Writer . t = fun conf tag a -> function | Iso ( _ , g , _ , asn ) -> encode conf tag ( g a ) asn | Fix ( fa , _ ) as fix -> encode conf tag a ( fa fix ) | Sequence asns -> e_constructed ( tag @? seq_tag ) ( e_seq conf a asns ) | Sequence_of asn -> e_constructed ( tag @? seq_tag ) @@ Writer . concat ( List . map ( fun e -> encode conf None e asn ) a ) | Set asns -> let h_sorted conf a asns = let fn = { Seq . f = fun a asn xs -> ( Asn_core . tag a asn , encode conf None a asn ) :: xs } in Writer . concat @@ List . map snd @@ List . sort ( fun ( t1 , _ ) ( t2 , _ ) -> compare t1 t2 ) @@ Seq . fold_with_value fn [ ] a asns in e_constructed ( tag @? set_tag ) @@ if conf . der then h_sorted conf a asns else e_seq conf a asns | Set_of asn -> let ws = List . map ( fun e -> encode conf None e asn ) a in let body = Writer . concat @@ if conf . der then List . ( ws |> map Writer . to_cstruct |> sort Writer . cs_lex_compare |> map Writer . of_cstruct ) else ws in e_constructed ( tag @? set_tag ) body | Choice ( asn1 , asn2 ) -> ( match a with | L a ' -> encode conf tag a ' asn1 | R b ' -> encode conf tag b ' asn2 ) | Implicit ( t , asn ) -> encode conf ( Some ( tag @? t ) ) a asn | Explicit ( t , asn ) -> e_constructed ( tag @? t ) ( encode conf None a asn ) | Prim p -> e_prim tag a p and e_seq : type a . conf -> a -> a sequence -> Writer . t = fun conf -> let f = { Seq . f = fun e asn w -> encode conf None e asn <+> w } in Seq . fold_with_value f Writer . empty and e_prim : type a . tag option -> a -> a prim -> Writer . t = fun tag a prim -> let encode = e_primitive ( match tag with Some x -> x | None -> tag_of_p prim ) in let encode_s ( type a ) ? length a ( module P : Prim . Prim_s with type t = a ) = assert_length ? constr : length P . length a ; encode ( P . to_writer a ) in match prim with | Bool -> encode @@ Prim . Boolean . to_writer a | Int -> encode @@ Prim . Integer . to_writer a | Bits -> encode @@ Prim . Bits . to_writer a | Octets -> encode_s a ( module Prim . Octets ) | Null -> encode @@ Prim . Null . to_writer a | OID -> encode @@ Prim . OID . to_writer a | CharString -> encode @@ Prim . Gen_string . to_writer a let ber_to_writer asn a = encode { der = false } None a asn let der_to_writer asn a = encode { der = true } None a asn end
module Int = struct type t = int let compare ( a : t ) b = compare a b let equal ( a : t ) b = a = b end
let clone_cs cs = let open Cstruct in let ds = create_unsafe ( length cs ) in blit cs 0 ds 0 ( length cs ) ; cs
type cls = [ ` Universal | ` Application | ` Private ]
let fix f = Fix ( f , Asn_cache . variant ( ) )
let map ? random f g asn = Iso ( f , g , random , asn )
let to_tag id = function | Some ` Application -> Tag . Application id | Some ` Private -> Tag . Private id | Some ` Universal -> Tag . Universal id | None -> Tag . Context_specific id
let explicit ? cls id asn = Explicit ( to_tag id cls , asn )
let rec implicit : type a . ? cls : cls -> int -> a asn -> a asn = fun ? cls id -> function Fix ( f , _ ) as asn -> implicit ? cls id ( f asn ) | Iso ( f , g , r , asn ) -> Iso ( f , g , r , implicit ? cls id asn ) | Choice ( _ , _ ) as asn -> explicit ? cls id asn | asn -> Implicit ( to_tag id cls , asn )
let bool = Prim Bool
let string tag = implicit ~ cls ` : Universal tag character_string
let utf8_string = string 0x0c
let numeric_string = string 0x12
let ( utc_time , generalized_time ) = let open Asn_prim . Time in let time ~ frac tag ( f , g ) = map ~ random ( : random ~ frac ) f g @@ implicit ~ cls ` : Universal tag character_string in time ~ frac : false 0x17 ( utc_time_of_string , of_utc_time ) , time ~ frac : true 0x18 ( gen_time_of_string , of_gen_time )
let int = let f n = try Z . to_int n with Z . Overflow -> parse_error " INTEGER : int overflow : % a " Z . pp_print n in map f Z . of_int integer
let enumerated f g = map f g @@ implicit ~ cls ` : Universal 0x0a int
let bit_string = Prim . Bits . ( map to_array of_array ( Prim Bits ) ) let f = function | 0 , cs -> cs | clip , cs -> let n = Cstruct . length cs in let last = Cstruct . get_uint8 cs ( n - 1 ) in let cs = clone_cs cs and last = last land ( lnot ( 1 lsl clip - 1 ) ) in Cstruct . set_uint8 cs ( n - 1 ) last ; cs in map f ( fun cs -> ( 0 , cs ) ) ( Prim Bits )
let bit_string_flags ( type a ) ( xs : ( int * a ) list ) = let cmp = compare in let module M1 = Map . Make ( struct type t = a let compare = cmp end ) in let module M2 = Map . Make ( Int ) in let aix , ixa = List . fold_left ( fun ( m1 , m2 ) ( i , x ) -> M1 . add x i m1 , M2 . add i x m2 ) ( M1 . empty , M2 . empty ) xs in let n = match M2 . max_binding_opt ixa with Some ( x , _ ) -> x + 1 | _ -> 0 in let f bits = let r = ref [ ] in bits |> Array . iteri ( fun i -> function | false -> ( ) | true -> try r := M2 . find i ixa :: ! r with Not_found -> ( ) ) ; List . sort cmp ! r and g es = let arr = Array . make n false in let register e = try arr . ( M1 . find e aix ) <- true with Not_found -> ( ) in List . iter register es ; arr in map f g bit_string
let single a = Last a
let product2 fn a b = fn @@ a @ single b
let product3 fn a b c = map ( fun ( a , ( b , c ) ) -> ( a , b , c ) ) ( fun ( a , b , c ) -> ( a , ( b , c ) ) ) ( fn @@ a @ b @ single c )
let product4 fn a b c d = map ( fun ( a , ( b , ( c , d ) ) ) -> ( a , b , c , d ) ) ( fun ( a , b , c , d ) -> ( a , ( b , ( c , d ) ) ) ) ( fn @@ a @ b @ c @ single d )
let product5 fn a b c d e = map ( fun ( a , ( b , ( c , ( d , e ) ) ) ) -> ( a , b , c , d , e ) ) ( fun ( a , b , c , d , e ) -> ( a , ( b , ( c , ( d , e ) ) ) ) ) ( fn @@ a @ b @ c @ d @ single e )
let product6 fn a b c d e f = map ( fun ( a , ( b , ( c , ( d , ( e , f ) ) ) ) ) -> ( a , b , c , d , e , f ) ) ( fun ( a , b , c , d , e , f ) -> ( a , ( b , ( c , ( d , ( e , f ) ) ) ) ) ) ( fn @@ a @ b @ c @ d @ e @ single f )
let sequence seq = Sequence seq
let sequence2 a b = product2 sequence a b
let sequence_of asn = Sequence_of asn
let set seq = Set seq
let set2 a b = product2 set a b
let set_of asn = Set_of asn
let choice a b = Choice ( a , b )
let choice2 a b = map ( function L a -> ` C1 a | R b -> ` C2 b ) ( function ` C1 a -> L a | ` C2 b -> R b ) ( choice a b )
let choice3 a b c = map ( function L ( L a ) -> ` C1 a | L ( R b ) -> ` C2 b | R c -> ` C3 c ) ( function ` C1 a -> L ( L a ) | ` C2 b -> L ( R b ) | ` C3 c -> R c ) ( choice ( choice a b ) c )
let choice4 a b c d = map ( function | L ( L a ) -> ` C1 a | L ( R b ) -> ` C2 b | R ( L c ) -> ` C3 c | R ( R d ) -> ` C4 d ) ( function | ` C1 a -> L ( L a ) | ` C2 b -> L ( R b ) | ` C3 c -> R ( L c ) | ` C4 d -> R ( R d ) ) ( choice ( choice a b ) ( choice c d ) )
let choice5 a b c d e = map ( function | L ( L ( L a ) ) -> ` C1 a | L ( L ( R b ) ) -> ` C2 b | L ( R c ) -> ` C3 c | R ( L d ) -> ` C4 d | R ( R e ) -> ` C5 e ) ( function | ` C1 a -> L ( L ( L a ) ) | ` C2 b -> L ( L ( R b ) ) | ` C3 c -> L ( R c ) | ` C4 d -> R ( L d ) | ` C5 e -> R ( R e ) ) ( choice ( choice ( choice a b ) c ) ( choice d e ) )
let choice6 a b c d e f = map ( function | L ( L ( L a ) ) -> ` C1 a | L ( L ( R b ) ) -> ` C2 b | L ( R c ) -> ` C3 c | R ( L ( L d ) ) -> ` C4 d | R ( L ( R e ) ) -> ` C5 e | R ( R f ) -> ` C6 f ) ( function | ` C1 a -> L ( L ( L a ) ) | ` C2 b -> L ( L ( R b ) ) | ` C3 c -> L ( R c ) | ` C4 d -> R ( L ( L d ) ) | ` C5 e -> R ( L ( R e ) ) | ` C6 f -> R ( R f ) ) ( choice ( choice ( choice a b ) c ) ( choice ( choice d e ) f ) )
let id x = x
let const x _ = x
let ( . ) & f g x = f ( g x )
let opt def = function Some x -> x | _ -> def
type ' a endo = ' a -> ' a
type ( ' a , ' b ) sum = L of ' a | R of ' b
let ( strf , pf ) = Format . ( asprintf , fprintf )
let kstrf k fmt = Format . ( kfprintf ( fun _ -> flush_str_formatter ( ) |> k ) str_formatter fmt )
let invalid_arg fmt = Format . ksprintf invalid_arg fmt
let pp_list ~ sep pp ppf xs = let rec go ppf = function | [ ] -> ( ) | [ x ] -> pp ppf x | x :: xs -> pf ppf " % a % a @ " pp x sep ( ) ; go ppf xs in pf ppf " [ @% a ] " @ go xs
let pp_dump_list pp ppf xs = let sep ppf ( ) = Format . pp_print_string ppf " , " in pf ppf " [ [ @% a ] ] " @ ( pp_list ~ sep pp ) xs
let pp_cs ppf cs = let f ppf cs = for i = 0 to cs . Cstruct . len - 1 do if i mod 8 = 0 && i > 0 then pf ppf " @ " ; pf ppf " % 02x " ( Cstruct . get_uint8 cs i ) done in pf ppf " [ @% a ] " @ f cs
module Tag = struct type t = | Universal of int | Application of int | Context_specific of int | Private of int let compare t1 t2 = match ( t1 , t2 ) with | ( Universal a , Universal b ) | ( Application a , Application b ) | ( Context_specific a , Context_specific b ) | ( Private a , Private b ) -> compare a b | ( Universal _ , _ ) | ( Application _ , ( Context_specific _ | Private _ ) ) | ( Context_specific _ , Private _ ) -> - 1 | _ -> 1 let equal t1 t2 = match ( t1 , t2 ) with | ( Universal a , Universal b ) | ( Application a , Application b ) | ( Context_specific a , Context_specific b ) | ( Private a , Private b ) -> a = b | _ -> false let pp ppf tag = let ( name , n ) = match tag with | Universal n -> ( " UNIVERSAL " , n ) | Application n -> ( " APPLICATION " , n ) | Context_specific n -> ( " CONTEXT " , n ) | Private n -> ( " PRIVATE " , n ) in pf ppf " % s % d " name n end
type tags = Tag . t list
module Generic = struct type t = | Cons of tag * t list | Prim of tag * Cstruct . t let tag = function Cons ( t , _ ) -> t | Prim ( t , _ ) -> t let pp_form_name ppf fsym = Format . pp_print_string ppf @@ match fsym with ` Cons -> " Constructed " | ` Prim -> " Primitive " | ` Both -> " ANY " let pp_tag ppf g = let form = match g with Cons _ -> ` Cons | Prim _ -> ` Prim in pf ppf " ( % a % a ) " pp_form_name form Tag . pp ( tag g ) end
type bits = int * Cstruct . t
type ' a rand = unit -> ' a
type _ asn = | Iso : ( ' a -> ' b ) * ( ' b -> ' a ) * ' b rand option * ' a asn -> ' b asn | Fix : ( ' a asn -> ' a asn ) * ' a Asn_cache . var -> ' a asn | Sequence : ' a sequence -> ' a asn | Sequence_of : ' a asn -> ' a list asn | Set : ' a sequence -> ' a asn | Set_of : ' a asn -> ' a list asn | Choice : ' a asn * ' b asn -> ( ' a , ' b ) sum asn | Implicit : tag * ' a asn -> ' a asn | Explicit : tag * ' a asn -> ' a asn | Prim : ' a prim -> ' a asn | Required : string option * ' a asn -> ' a element | Optional : string option * ' a asn -> ' a option element | Last : ' a element -> ' a sequence | Pair : ' a element * ' b sequence -> ( ' a * ' b ) sequence | Bool : bool prim | Int : Z . t prim | Bits : bits prim | Octets : Cstruct . t prim | Null : unit prim | OID : OID . t prim | CharString : string prim
let label = opt " "
let seq_tag = Tag . Universal 0x10
let tag_of_p : type a . a prim -> tag = let open Tag in function | Bool -> Universal 0x01 | Int -> Universal 0x02 | Bits -> Universal 0x03 | Octets -> Universal 0x04 | Null -> Universal 0x05 | OID -> Universal 0x06 | CharString -> Universal 0x1d
let rec tag_set : type a . a asn -> tags = function | Iso ( _ , _ , _ , asn ) -> tag_set asn | Fix ( f , _ ) as fix -> tag_set ( f fix ) | Sequence _ -> [ seq_tag ] | Sequence_of _ -> [ seq_tag ] | Set _ -> [ set_tag ] | Set_of _ -> [ set_tag ] | Choice ( asn1 , asn2 ) -> tag_set asn1 @ tag_set asn2 | Implicit ( t , _ ) -> [ t ] | Explicit ( t , _ ) -> [ t ] | Prim p -> [ tag_of_p p ]
let rec tag : type a . a -> a asn -> tag = fun a -> function | Iso ( _ , g , _ , asn ) -> tag ( g a ) asn | Fix _ as fix -> tag a fix | Sequence _ -> seq_tag | Sequence_of _ -> seq_tag | Set _ -> set_tag | Set_of _ -> set_tag | Choice ( a1 , a2 ) -> ( match a with L a ' -> tag a ' a1 | R b ' -> tag b ' a2 ) | Implicit ( t , _ ) -> t | Explicit ( t , _ ) -> t | Prim p -> tag_of_p p
type error = [ ` Parse of string ]
let pp_error ppf ( ` Parse err ) = pf ppf " Parse error : % s " err
let error err = raise ( Parse_error err )
let parse_error fmt = kstrf ( fun s -> error ( ` Parse s ) ) fmt
module FSet = struct type f = Fn : ( ' a -> ' b ) -> f include Set . Make ( struct type t = f let compare ( Fn f1 ) ( Fn f2 ) = Hashtbl . ( compare ( hash f1 ) ( hash f2 ) ) end ) let mem f s = mem ( Fn f ) s and add f s = add ( Fn f ) s end
let validate asn = let rec check : type a . ? tag : tag -> FSet . t -> a asn -> unit = fun ? tag fs -> function | Iso ( _ , _ , _ , a ) -> check ? tag fs a | Fix ( f , _ ) as fix -> if not ( FSet . mem f fs ) then check ? tag FSet . ( add f fs ) ( f fix ) | Sequence s -> disjoint_seq s ; check_s fs s | Set s -> disjoint ( seq_tags s ) ; check_s fs s | Sequence_of a -> check fs a | Set_of a -> check fs a | Choice ( a1 , a2 ) -> disjoint [ tag_set a1 ; tag_set a2 ] ; check fs a1 ; check fs a2 | Implicit ( t , a ) -> check ~ tag : t fs a | Explicit ( _ , a ) -> check fs a | Prim _ -> ( ) and check_s : type a . FSet . t -> a sequence -> unit = fun fs -> function | Last ( Required ( _ , a ) ) -> check fs a | Last ( Optional ( _ , a ) ) -> check fs a | Pair ( Required ( _ , a ) , s ) -> check fs a ; check_s fs s | Pair ( Optional ( _ , a ) , s ) -> check fs a ; check_s fs s and seq_tags : type a . a sequence -> tags list = function | Last ( Required ( _ , a ) ) -> [ tag_set a ] | Last ( Optional ( _ , a ) ) -> [ tag_set a ] | Pair ( Required ( _ , a ) , s ) -> tag_set a :: seq_tags s | Pair ( Optional ( _ , a ) , s ) -> tag_set a :: seq_tags s and disjoint_seq : type a . a sequence -> unit = fun s -> let f1 : type a . tags list -> a element -> tags list = fun tss -> function | Required ( _ , a ) -> disjoint ( tag_set a :: tss ) ; [ ] | Optional ( _ , a ) -> disjoint ( tag_set a :: tss ) ; tag_set a :: tss in let rec f2 : type a . tags list -> a sequence -> unit = fun tss -> function | Last e -> ignore ( f1 tss e ) | Pair ( e , s ) -> f2 ( f1 tss e ) s in f2 [ ] s and disjoint tss = let rec go = function | t :: u :: _ when Tag . equal t u -> raise Ambiguous_syntax | _ :: ts -> go ts | _ -> ( ) in go List . ( sort Tag . compare @@ concat tss ) in check FSet . empty asn
type t = Oid of int * int * int list
let invalid_arg fmt = Format . ksprintf invalid_arg fmt
let ( ) <| ( Oid ( v1 , v2 , vs ) ) vn = if vn < 0 then invalid_arg " OID . ( ) <|: negative component : % d " vn ; Oid ( v1 , v2 , vs @ [ vn ] )
let ( ) <|| ( Oid ( v1 , v2 , vs ) ) vs ' = let f v = if v < 0 then invalid_arg " OID . ( ) <||: negative component : % d " v in List . iter f vs ; Oid ( v1 , v2 , vs @ vs ' )
let base v1 v2 = match v1 with | 0 | 1 when v2 >= 0 && v2 < 40 -> Oid ( v1 , v2 , [ ] ) | 2 when v2 >= 0 -> Oid ( v1 , v2 , [ ] ) | _ -> invalid_arg " OID . base : out of range : % d . % d " v1 v2
let base_opt v1 v2 = try Some ( base v1 v2 ) with Invalid_argument _ -> None
let to_nodes ( Oid ( v1 , v2 , vs ) ) = ( v1 , v2 , vs )
let of_nodes n1 n2 ns = try Some ( base n1 n2 <|| ns ) with Invalid_argument _ -> None