package zlib

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

Source file zlib.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
(*
 * Copyright (c) 2015, Christopher Zimmermann
 *
 * Permission to use, copy, modify, and/or 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.
 *)


type status = 
  | Ok                   (* 0 *)
  | Stream_end           (* 1 *)
  | Need_dict            (* 2 *)
  | Buf_error            (* 3 (zlib -5) *)
  | Data_error of string (* 0 (zlib -3) *)

type algo = Deflated

type strategy =
  | Default_strategy  (* 0 *)
  | Filtered          (* 1 *)
  | Huffman_only      (* 2 *)
  | RLE               (* 3 *)
  | Fixed             (* 4 *)

type flush =
  | No_flush          (* 0 *)
  | Partial_flush     (* 1 *)
  | Sync_flush        (* 2 *)
  | Full_flush        (* 3 *)
  | Finish            (* 4 *)
  | Block             (* 5 *)
  | Trees             (* 6 *)

type data_type =
  | Binary            (* 0 *)
  | Text              (* 1 *)
  | Unknown           (* 2 *)

type deflate
type inflate

type 'a state
type bigstring =
  (char, Bigarray.int8_unsigned_elt, Bigarray.c_layout) Bigarray.Array1.t
type 'a t =
  { state :'a state
  ; mutable in_buf    :((char, Bigarray.int8_unsigned_elt, Bigarray.c_layout) Bigarray.Array1.t)
  ; mutable out_buf   :((char, Bigarray.int8_unsigned_elt, Bigarray.c_layout) Bigarray.Array1.t)
  ; mutable in_ofs    :int
  ; mutable out_ofs   :int
  ; mutable in_len    :int
  ; mutable out_len   :int
  ; mutable in_total  :int
  ; mutable out_total :int
  ; mutable data_type :int
  ; mutable cksum     :int32
  }

type header =
  { text        :bool
  ; mtime       :int32
  ; os          :int
  ; xflags      :int
  ; extra       :string option
  ; name        :string option
  ; comment     :string option
  }

external inflate_init : window_bits:int -> inflate state = "zlib_inflate_init"
external deflate_init :
  level:int -> algo:algo -> window_bits:int -> memory:int -> strategy:strategy
  -> deflate state
  = "zlib_deflate_init"

(* calculate upper bound on deflated stream. *)
external deflate_bound : deflate state -> int -> int
  = "zlib_deflate_bound"

(* flate handle flush *)
external flate : 'a t -> flush -> status = "zlib_flate"

(* set dictionary *)
external deflate_set_dictionary : deflate state -> string -> int32
  = "zlib_deflate_set_dictionary"
external inflate_set_dictionary : inflate state -> string -> status
  = "zlib_inflate_set_dictionary"

(* set/get header *)
external set_header : deflate state -> header -> unit = "zlib_set_header"
external get_header : inflate state -> header         = "zlib_get_header"

(* reset *)
external reset : 'a t -> unit = "zlib_reset"

(* adler32 *)
external adler32 : int32 -> string -> int32 = "zlib_adler32"
let adler32_empty = Int32.one

let get_data_type (mlstate :deflate t) =
  match mlstate.data_type with
  |0 -> Binary
  |1 -> Text
  |2 -> Unknown
  |_ -> assert(false)
;;

(* create caml record wrapping zlib state and bigarray buffers *)0
let create_deflate, create_inflate =
  let dummy_buf = Bigarray.(Array1.create char c_layout 0) in
  let wrap state =
    { state
    ; in_buf    = dummy_buf
    ; out_buf   = dummy_buf
    ; in_ofs    = 0
    ; out_ofs   = 0
    ; in_len    = -1
    ; out_len   = -1
    ; in_total  = 0
    ; out_total = 0
    ; cksum     = Int32.zero
    ; data_type = 2
    }
  in
  let create_deflate
      ?(level=(-1))
      ?(algo=Deflated)
      ?(window_bits=15)
      ?(memory=8)
      ?(strategy=Default_strategy)
      () =
    wrap (deflate_init ~level ~algo ~window_bits ~memory ~strategy)
  in
  let create_inflate ?(window_bits=15) () =
    wrap (inflate_init ~window_bits)
  in
  create_deflate, create_inflate
OCaml

Innovation. Community. Security.