Skip to content
This repository
tag: v552
Fetching contributors…

Cannot retrieve contributors at this time

file 133 lines (115 sloc) 4.358 kb
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
(*
Copyright © 2011 MLstate

This file is part of OPA.

OPA is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License, version 3, as published by
the Free Software Foundation.

OPA is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
more details.

You should have received a copy of the GNU Affero General Public License
along with OPA. If not, see <http://www.gnu.org/licenses/>.
*)
(*
@author Frederic Ye
**)

(* Same as Gzip (Libzip) but uses a string instead of a stream *)


exception Error of string

let buffer_size = 1024

let char_buffer = String.create 1

type out_channel =
  { out_buffer: string;
    mutable out_pos: int;
    mutable out_avail: int;
    out_stream: Zlib.stream;
    mutable out_string: string;
    mutable out_size: int32;
    mutable out_crc: int32;
    only_deflate : bool }

let output_byte oz b =
  oz.out_string <- Printf.sprintf "%s%c" oz.out_string (Char.unsafe_chr b)

let open_out ?(level = 6) ?(only_deflate=false) () =
  if level < 1 || level > 9 then invalid_arg "Gzip.open_out: bad level";
  let oz = {
    out_buffer = String.create buffer_size;
    out_pos = 0;
    out_avail = buffer_size;
    out_stream = Zlib.deflate_init level false;
    out_string = "";
    out_size = Int32.zero;
    out_crc = Int32.zero;
    only_deflate = only_deflate } in
  (* Write minimal header *)
  if not only_deflate then (
    output_byte oz 0x1F; (* ID1 *)
    output_byte oz 0x8B; (* ID2 *)
    output_byte oz 8; (* compression method *)
    output_byte oz 0; (* flags *)
    for i = 1 to 4 do output_byte oz 0 done; (* mtime *)
    output_byte oz 0; (* xflags *)
    output_byte oz 0xFF; (* OS (unknown) *)
  );
  oz

let rec output oz buf pos len =
  if pos < 0 || len < 0 || pos + len > String.length buf then
    invalid_arg "Gzip.output";
  (* If output buffer is full, flush it *)
  if oz.out_avail = 0 then begin
    oz.out_string <- Printf.sprintf "%s%s" oz.out_string (String.sub oz.out_buffer 0 oz.out_pos);
    oz.out_pos <- 0;
    oz.out_avail <- String.length oz.out_buffer
  end;
  let (_, used_in, used_out) =
    try
      Zlib.deflate oz.out_stream buf pos len
                                 oz.out_buffer oz.out_pos oz.out_avail
                                 Zlib.Z_NO_FLUSH
    with Zlib.Error(_, _) ->
      raise (Error("error during compression")) in
  oz.out_pos <- oz.out_pos + used_out;
  oz.out_avail <- oz.out_avail - used_out;
  if not oz.only_deflate then (
    oz.out_size <- Int32.add oz.out_size (Int32.of_int used_in);
    oz.out_crc <- Zlib.update_crc oz.out_crc buf pos used_in;
  );
  if used_in < len then output oz buf (pos + used_in) (len - used_in)

let output_char oz c =
  char_buffer.[0] <- c;
  output oz char_buffer 0 1

let output_byte oz b =
  output_char oz (Char.unsafe_chr b)

let write_int32 oz n =
  let r = ref n in
  for i = 1 to 4 do
    oz.out_string <- Printf.sprintf "%s%c" oz.out_string (Char.unsafe_chr (Int32.to_int !r));
    r := Int32.shift_right_logical !r 8
  done

let flush oz =
  let rec do_flush () =
    (* If output buffer is full, flush it *)
    if oz.out_avail = 0 then begin
      oz.out_string <- Printf.sprintf "%s%s" oz.out_string (String.sub oz.out_buffer 0 oz.out_pos);
      oz.out_pos <- 0;
      oz.out_avail <- String.length oz.out_buffer
    end;
    let (finished, _, used_out) =
      Zlib.deflate oz.out_stream oz.out_buffer 0 0
                                 oz.out_buffer oz.out_pos oz.out_avail
                                 Zlib.Z_FINISH in
    oz.out_pos <- oz.out_pos + used_out;
    oz.out_avail <- oz.out_avail - used_out;
    if not finished then do_flush() in
  do_flush();
  (* Final data flush *)
  if oz.out_pos > 0 then
    oz.out_string <- Printf.sprintf "%s%s" oz.out_string (String.sub oz.out_buffer 0 oz.out_pos);
  (* Write CRC and size *)
  if not oz.only_deflate then (
    write_int32 oz oz.out_crc;
    write_int32 oz oz.out_size;
  );
  (* Dispose of stream *)
  Zlib.deflate_end oz.out_stream

let close_out oz =
  flush oz
Something went wrong with that request. Please try again.