package pkcs11

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file p11_helpers.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
(** Miscellaneous helpers *)

let string_of_record
    ?(newlines = false)
    ?(indent = "")
    (fields : (string * string) list) : string =
  let first_separator =
    if newlines then
      ""
    else
      "{ "
  in
  let separator =
    if newlines then
      "\n"
    else
      "; "
  in
  let last_separator =
    if newlines then
      ""
    else
      " }"
  in
  let first = ref true in
  [ List.flatten
      (List.map
         (fun (name, value) ->
           [ (if !first then (
               first := false;
               first_separator
             ) else
               separator)
           ; indent
           ; name
           ; ": "
           ; value ])
         fields)
  ; [last_separator] ]
  |> List.flatten
  |> String.concat ""

let strings_of_record =
  List.map (fun (name, value) -> Printf.sprintf "%s: %s" name value)

let of_json_string ~typename of_string json =
  let err msg = Error (Printf.sprintf "(while parsing %s): %s" typename msg) in
  match json with
  | `String s -> (
    try Ok (of_string s) with
    | Invalid_argument _ -> err "of_string failed")
  | _ -> err "Not a JSON string"

(* Remove trailing zeros and spaces, and quote the result to prevent
   the DLL from injecting stuff into our tool. *)
let trim_and_quote string =
  let len = String.length string in
  let rec new_len i =
    if i < 0 then
      0
    else
      match string.[i] with
      | '\000'
      | ' ' ->
        new_len (i - 1)
      | _ -> i + 1
  in
  let new_len = new_len (len - 1) in
  Printf.sprintf "%S" (Str.first_chars string new_len)
OCaml

Innovation. Community. Security.