]>
Dogcows Code - chaz/yoink/blob - src/moof/compression.cc
2 /*] Copyright (c) 2009-2011, Charles McGarvey [*****************************
3 **] All rights reserved.
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.
8 *****************************************************************************/
14 #include "compression.hh"
16 #define ZLIB_BUF_SIZE 262114
22 void inflate(std::istream
& in
, std::ostream
& out
)
24 char inbuf
[ZLIB_BUF_SIZE
];
25 char outbuf
[ZLIB_BUF_SIZE
];
29 zstream
.zalloc
= Z_NULL
;
30 zstream
.zfree
= Z_NULL
;
31 zstream
.opaque
= Z_NULL
;
33 zstream
.next_in
= Z_NULL
;
35 int result
= inflateInit2(&zstream
, 32 + MAX_WBITS
);
36 if (result
!= Z_OK
) throw std::runtime_error("zlib init error");
39 in
.read(inbuf
, sizeof(inbuf
));
40 zstream
.next_in
= (Bytef
*)inbuf
;
41 zstream
.avail_in
= in
.gcount();
43 if (zstream
.avail_in
== 0) break;
46 zstream
.next_out
= (Bytef
*)outbuf
;
47 zstream
.avail_out
= sizeof(outbuf
);
49 result
= inflate(&zstream
, Z_NO_FLUSH
);
56 throw std::runtime_error("zlib inflate error");
58 throw std::runtime_error("zlib stream error");
61 int inflated
= sizeof(outbuf
) - zstream
.avail_out
;
62 out
.write(outbuf
, inflated
);
64 while (zstream
.avail_out
== 0);
66 while(result
!= Z_STREAM_END
);
71 void inflate(const char* in
, size_t size
, std::ostream
& out
)
73 char buf
[ZLIB_BUF_SIZE
];
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
;
82 int result
= inflateInit2(&zstream
, 32 + MAX_WBITS
);
83 if (result
!= Z_OK
) throw std::runtime_error("zlib init error");
86 zstream
.next_out
= (Bytef
*)buf
;
87 zstream
.avail_out
= sizeof(buf
);
89 result
= inflate(&zstream
, Z_NO_FLUSH
);
96 throw std::runtime_error("zlib inflate error");
98 throw std::runtime_error("zlib stream error");
101 size_t inflated
= sizeof(buf
) - zstream
.avail_out
;
102 out
.write(buf
, inflated
);
104 while (zstream
.avail_out
== 0);
106 inflateEnd(&zstream
);
This page took 0.038008 seconds and 5 git commands to generate.