2013-07-13 15:28:29 +10:00
|
|
|
#include "zlib.hpp"
|
|
|
|
|
|
|
|
#include "debug.hpp"
|
|
|
|
|
|
|
|
const char *
|
|
|
|
util::zlib::version (void) {
|
|
|
|
return zlibVersion ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-30 01:41:54 +10:00
|
|
|
const char*
|
|
|
|
util::zlib::code_to_string (int code) {
|
|
|
|
static const int MIN_CODE = -6;
|
|
|
|
static const int MAX_CODE = 2;
|
|
|
|
|
|
|
|
if (code > MAX_CODE || code < MIN_CODE) {
|
|
|
|
unreachable ();
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char* CODES[] = {
|
|
|
|
"OK",
|
|
|
|
"STREAM_END",
|
|
|
|
"NEED_DICT",
|
|
|
|
"ERRNO",
|
|
|
|
"STREAM_ERROR",
|
|
|
|
"DATA_ERROR",
|
|
|
|
"MEM_ERROR",
|
|
|
|
"BUF_ERROR",
|
|
|
|
"VERSION_ERROR",
|
|
|
|
};
|
|
|
|
|
|
|
|
return CODES[code + MIN_CODE];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-13 15:28:29 +10:00
|
|
|
size_t
|
|
|
|
util::zlib::uncompress (uint8_t *dst, size_t dst_len,
|
|
|
|
const uint8_t *src, size_t src_len) {
|
|
|
|
size_t actual_len = dst_len;
|
2013-08-05 16:40:15 +10:00
|
|
|
uLongf zlib_actual = actual_len;
|
|
|
|
|
|
|
|
int err = ::uncompress (dst, &zlib_actual, src, src_len);
|
2013-07-13 15:28:29 +10:00
|
|
|
error::try_code (err);
|
2013-08-05 16:40:15 +10:00
|
|
|
|
|
|
|
actual_len = zlib_actual;
|
2013-07-13 15:28:29 +10:00
|
|
|
return actual_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
util::zlib::error::try_code (int code) {
|
|
|
|
if (Z_OK == code)
|
|
|
|
return;
|
|
|
|
|
|
|
|
throw_code(code);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
util::zlib::error::error (const std::string &_what):
|
|
|
|
runtime_error (_what)
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
util::zlib::error::throw_code (int code) {
|
|
|
|
CHECK_NEQ (code, Z_OK);
|
|
|
|
|
|
|
|
switch (code) {
|
|
|
|
#define do_code(c) case c: throw code_error<c> ();
|
|
|
|
|
|
|
|
do_code(Z_STREAM_ERROR);
|
|
|
|
do_code(Z_DATA_ERROR);
|
|
|
|
do_code(Z_MEM_ERROR);
|
|
|
|
do_code(Z_BUF_ERROR);
|
|
|
|
do_code(Z_VERSION_ERROR);
|
|
|
|
|
|
|
|
default:
|
|
|
|
panic ("Unknown code");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-30 01:42:46 +10:00
|
|
|
|
|
|
|
template class util::zlib::code_error<Z_STREAM_ERROR>;
|
|
|
|
template class util::zlib::code_error<Z_DATA_ERROR>;
|
|
|
|
template class util::zlib::code_error<Z_MEM_ERROR>;
|
|
|
|
template class util::zlib::code_error<Z_BUF_ERROR>;
|
|
|
|
template class util::zlib::code_error<Z_VERSION_ERROR>;
|