]> Dogcows Code - chaz/yoink/blob - src/moof/compression.cc
fixed documentation about where to find licenses
[chaz/yoink] / src / moof / compression.cc
1
2 /*] Copyright (c) 2009-2011, Charles McGarvey [*****************************
3 **] All rights reserved.
4 *
5 * Distributable under the terms and conditions of the 2-clause BSD license;
6 * see the file COPYING for a complete text of the license.
7 *
8 *****************************************************************************/
9
10 #include <iostream>
11 #include <stdexcept>
12 #include <zlib.h>
13
14 #include "compression.hh"
15
16 #define ZLIB_BUF_SIZE 262114
17
18
19 namespace moof {
20
21
22 void inflate(std::istream& in, std::ostream& out)
23 {
24 char inbuf[ZLIB_BUF_SIZE];
25 char outbuf[ZLIB_BUF_SIZE];
26
27 z_stream zstream;
28
29 zstream.zalloc = Z_NULL;
30 zstream.zfree = Z_NULL;
31 zstream.opaque = Z_NULL;
32 zstream.avail_in = 0;
33 zstream.next_in = Z_NULL;
34
35 int result = inflateInit2(&zstream, 32 + MAX_WBITS);
36 if (result != Z_OK) throw std::runtime_error("zlib init error");
37
38 do {
39 in.read(inbuf, sizeof(inbuf));
40 zstream.next_in = (Bytef*)inbuf;
41 zstream.avail_in = in.gcount();
42
43 if (zstream.avail_in == 0) break;
44
45 do {
46 zstream.next_out = (Bytef*)outbuf;
47 zstream.avail_out = sizeof(outbuf);
48
49 result = inflate(&zstream, Z_NO_FLUSH);
50 switch (result)
51 {
52 case Z_NEED_DICT:
53 case Z_DATA_ERROR:
54 case Z_MEM_ERROR:
55 inflateEnd(&zstream);
56 throw std::runtime_error("zlib inflate error");
57 case Z_STREAM_ERROR:
58 throw std::runtime_error("zlib stream error");
59 }
60
61 int inflated = sizeof(outbuf) - zstream.avail_out;
62 out.write(outbuf, inflated);
63 }
64 while (zstream.avail_out == 0);
65 }
66 while(result != Z_STREAM_END);
67
68 inflateEnd(&zstream);
69 }
70
71 void inflate(const char* in, size_t size, std::ostream& out)
72 {
73 char buf[ZLIB_BUF_SIZE];
74
75 z_stream zstream;
76 zstream.zalloc = Z_NULL;
77 zstream.zfree = Z_NULL;
78 zstream.opaque = Z_NULL;
79 zstream.avail_in = size;
80 zstream.next_in = (Bytef*)in;
81
82 int result = inflateInit2(&zstream, 32 + MAX_WBITS);
83 if (result != Z_OK) throw std::runtime_error("zlib init error");
84
85 do {
86 zstream.next_out = (Bytef*)buf;
87 zstream.avail_out = sizeof(buf);
88
89 result = inflate(&zstream, Z_NO_FLUSH);
90 switch (result)
91 {
92 case Z_NEED_DICT:
93 case Z_DATA_ERROR:
94 case Z_MEM_ERROR:
95 inflateEnd(&zstream);
96 throw std::runtime_error("zlib inflate error");
97 case Z_STREAM_ERROR:
98 throw std::runtime_error("zlib stream error");
99 }
100
101 size_t inflated = sizeof(buf) - zstream.avail_out;
102 out.write(buf, inflated);
103 }
104 while (zstream.avail_out == 0);
105
106 inflateEnd(&zstream);
107 }
108
109
110 } // namespace moof
111
This page took 0.0339390000000001 seconds and 4 git commands to generate.