Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line | 
|---|---|---|---|
| 99 | pmbaty | 1 | /* wrap.c */ | 
| 2 | |||
| 3 | /* | ||
| 4 | This Software is distributed with the following X11 License, | ||
| 5 | sometimes also known as MIT license. | ||
| 6 | |||
| 7 | Copyright (c) 2010 Miguel A. Ballicora | ||
| 8 | |||
| 9 |  Permission is hereby granted, free of charge, to any person | ||
| 10 |  obtaining a copy of this software and associated documentation | ||
| 11 |  files (the "Software"), to deal in the Software without | ||
| 12 |  restriction, including without limitation the rights to use, | ||
| 13 |  copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| 14 |  copies of the Software, and to permit persons to whom the | ||
| 15 |  Software is furnished to do so, subject to the following | ||
| 16 |  conditions: | ||
| 17 | |||
| 18 |  The above copyright notice and this permission notice shall be | ||
| 19 |  included in all copies or substantial portions of the Software. | ||
| 20 | |||
| 21 |  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
| 22 |  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | ||
| 23 |  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
| 24 |  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | ||
| 25 |  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | ||
| 26 |  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
| 27 |  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | ||
| 28 |  OTHER DEALINGS IN THE SOFTWARE. | ||
| 29 | */ | ||
| 30 | |||
| 31 | #include "wrap.h" | ||
| 32 | |||
| 33 | #if defined(LZMA86) | ||
| 34 | #include "Lzma86Enc.h" | ||
| 35 | #include "Lzma86Dec.h" | ||
| 36 | #endif | ||
| 37 | |||
| 38 | #if defined(ZLIB) | ||
| 39 | #include "zlib.h" | ||
| 40 | #endif | ||
| 41 | |||
| 42 | #if defined(HUFFMAN) | ||
| 43 | #include "hzip.h" | ||
| 44 | #endif | ||
| 45 | |||
| 46 | #if defined(LIBLZF) | ||
| 47 | #include "lzf.h" | ||
| 48 | #endif | ||
| 49 | |||
| 50 | #if defined(LIBBZIP2) | ||
| 51 | #include "bzlib.h" | ||
| 52 | #endif | ||
| 53 | |||
| 54 | #if !defined(NDEBUG) | ||
| 55 | #define NDEBUG | ||
| 56 | #endif | ||
| 57 | #ifdef DEBUG | ||
| 58 | #undef NDEBUG | ||
| 59 | #endif | ||
| 60 | #include "assert.h" | ||
| 61 | |||
| 62 | /* external, so the compiler can be silenced */ | ||
| 63 | size_t TB_DUMMY_unused; | ||
| 64 | |||
| 65 | /***********************************************************************************************************/ | ||
| 66 | |||
| 67 | #if defined(ZLIB) | ||
| 68 | extern int | ||
| 69 | zlib_encode | ||
| 70 | (const unsigned char *in_start, size_t in_len, unsigned char *out_start, size_t *pout_len, size_t out_max) | ||
| 71 | { | ||
| 72 | enum COMPRESSION_LEVELS {ZLIB_MAXIMUM_COMPRESSION = 9}; | ||
| 73 | int outcome; | ||
| 74 | unsigned long zz = (unsigned long)out_max; | ||
| 75 | outcome = compress2 (out_start, &zz, in_start, in_len, ZLIB_MAXIMUM_COMPRESSION); | ||
| 76 | *pout_len = (size_t) zz; | ||
| 77 | return outcome == Z_OK; | ||
| 78 | } | ||
| 79 | |||
| 80 | extern int | ||
| 81 | zlib_decode | ||
| 82 | (const unsigned char *in_start, size_t in_len, unsigned char *out_start, size_t *pout_len, size_t out_max) | ||
| 83 | { | ||
| 84 | int outcome; | ||
| 85 | unsigned long nn = (unsigned long) out_max /* *pout_len */; | ||
| 86 | outcome = uncompress (out_start, &nn, in_start, (unsigned long)in_len); | ||
| 87 | *pout_len = (size_t)nn; | ||
| 88 | return outcome == Z_OK; | ||
| 89 | } | ||
| 90 | #endif | ||
| 91 | |||
| 92 | /***********************************************************************************************************/ | ||
| 93 | |||
| 94 | #if defined(LIBLZF) | ||
| 95 | extern int | ||
| 96 | lzf_encode | ||
| 97 | (const unsigned char *in_start, size_t in_len, unsigned char *out_start, size_t *pout_len, size_t out_max) | ||
| 98 | { | ||
| 99 | size_t x = lzf_compress (in_start, (unsigned)in_len, out_start, (unsigned)(in_len-1) /* ensures best compression */); | ||
| 100 | TB_DUMMY_unused = out_max; | ||
| 101 | if (x != 0) | ||
| 102 | *pout_len = (size_t) x; | ||
| 103 | return x != 0; | ||
| 104 | } | ||
| 105 | |||
| 106 | extern int | ||
| 107 | lzf_decode | ||
| 108 | (const unsigned char *in_start, size_t in_len, unsigned char *out_start, size_t *pout_len, size_t out_max) | ||
| 109 | { | ||
| 110 | *pout_len = (size_t)lzf_decompress (in_start, (unsigned)in_len, out_start, (unsigned)out_max); | ||
| 111 | return *pout_len != 0; | ||
| 112 | } | ||
| 113 | #endif | ||
| 114 | |||
| 115 | /***********************************************************************************************************/ | ||
| 116 | |||
| 117 | #if defined(LZMA86) | ||
| 118 | extern int | ||
| 119 | lzma_encode | ||
| 120 | (const unsigned char *in_start, size_t in_len, unsigned char *out_start, size_t *pout_len, size_t out_max) | ||
| 121 | { | ||
| 122 | int level = 5; /* 5 => default compression level */ | ||
| 123 | unsigned int memory = 4096; /* dictionary size */ | ||
| 124 | int filter = SZ_FILTER_NO; /* => 0, use LZMA, do not try to optimize with x86 filter */ | ||
| 125 | size_t zz = out_max; /* maximum memory allowed, receives back the actual size */ | ||
| 126 | int x = Lzma86_Encode(out_start, &zz, in_start, in_len, level, memory, filter); | ||
| 127 | *pout_len = zz; | ||
| 128 | return x == 0; | ||
| 129 | } | ||
| 130 | |||
| 131 | extern int | ||
| 132 | lzma_decode | ||
| 133 | (const unsigned char *in_start, size_t in_len, unsigned char *out_start, size_t *pout_len, size_t out_max) | ||
| 134 | { | ||
| 135 | size_t nn = out_max; | ||
| 136 | int x = Lzma86_Decode(out_start, &nn, in_start, &in_len); | ||
| 137 | *pout_len = nn; | ||
| 138 | return x == SZ_OK; | ||
| 139 | } | ||
| 140 | #endif | ||
| 141 | |||
| 142 | /***********************************************************************************************************/ | ||
| 143 | |||
| 144 | #if defined (LIBBZIP2) | ||
| 145 | |||
| 146 | extern int | ||
| 147 | bzip2_encode | ||
| 148 | (const unsigned char *in_start, size_t in_len, unsigned char *out_start, size_t *pout_len, size_t out_max) | ||
| 149 | { | ||
| 150 | int blockSize100k = 9; | ||
| 151 | int verbosity = 0; | ||
| 152 | int workFactor = 30; | ||
| 153 | size_t destlen = out_max; | ||
| 154 | |||
| 155 | int x = BZ2_bzBuffToBuffCompress( (char*)out_start, &destlen, (char*)in_start, in_len, | ||
| 156 | blockSize100k, verbosity, workFactor); | ||
| 157 | *pout_len = destlen; | ||
| 158 | return x == BZ_OK; | ||
| 159 | } | ||
| 160 | |||
| 161 | extern int | ||
| 162 | bzip2_decode | ||
| 163 | (const unsigned char *in_start, size_t in_len, unsigned char *out_start, size_t *pout_len, size_t out_max) | ||
| 164 | { | ||
| 165 | int small = 1; | ||
| 166 | int verbosity = 0; | ||
| 167 | size_t destlen = n; | ||
| 168 | |||
| 169 | int x = BZ2_bzBuffToBuffDecompress( (char*)out_start, &destlen, (char*)in_start, in_len, | ||
| 170 | small, verbosity); | ||
| 171 | *pout_len = destlen; | ||
| 172 | return x == BZ_OK; | ||
| 173 | } | ||
| 174 | |||
| 175 | #endif | ||
| 176 | |||
| 177 | /***********************************************************************************************************/ | ||
| 178 | |||
| 179 | extern int | ||
| 180 | justcopy_encode | ||
| 181 | (const unsigned char *in_start, size_t in_len, unsigned char *out_start, size_t *pout_len, size_t out_max) | ||
| 182 | { | ||
| 183 | size_t i; | ||
| 184 | const unsigned char *in = in_start; | ||
| 185 | unsigned char *out = out_start; | ||
| 186 | |||
| 187 | if (in_len > out_max) | ||
| 188 | return 0; | ||
| 189 | |||
| 190 | for (i = 0; i < in_len; i++) { | ||
| 191 | *out++ = *in++; | ||
| 192 |         } | ||
| 193 | *pout_len = in_len; | ||
| 194 | return 1; | ||
| 195 | } | ||
| 196 | |||
| 197 | extern int | ||
| 198 | justcopy_decode | ||
| 199 | (const unsigned char *in_start, size_t in_len, unsigned char *out_start, size_t *pout_len, size_t out_max) | ||
| 200 | { | ||
| 201 | size_t i; | ||
| 202 | const unsigned char *in = in_start; | ||
| 203 | unsigned char *out = out_start; | ||
| 204 | |||
| 205 | if (in_len > out_max) | ||
| 206 | return 0; | ||
| 207 | |||
| 208 | for (i = 0; i < in_len; i++) { | ||
| 209 | *out++ = *in++; | ||
| 210 |         } | ||
| 211 | *pout_len = in_len; | ||
| 212 | return 1; | ||
| 213 | } | ||
| 214 | |||
| 215 | /***********************************************************************************************************/ | ||
| 216 | |||
| 217 | #define RLE_ESC 253 | ||
| 218 | #define RLE_TER 254 | ||
| 219 | #define RLE_MAX 252 | ||
| 220 | #define TRUE 1 | ||
| 221 | #define FALSE 0 | ||
| 222 | |||
| 223 | extern int | ||
| 224 | rle_encode | ||
| 225 | (const unsigned char *in_start, size_t in_len, unsigned char *out_start, size_t *pout_len, size_t out_max) | ||
| 226 | { | ||
| 227 | const unsigned char *p; | ||
| 228 | const unsigned char *in = in_start; | ||
| 229 | const unsigned char *in_end = in + in_len; | ||
| 230 | unsigned char *out = out_start; | ||
| 231 | int ok = TRUE; | ||
| 232 | int ch; | ||
| 233 |         ptrdiff_t out_len; | ||
| 234 | |||
| 235 | while (in < in_end) | ||
| 236 |         { | ||
| 237 | if (*in == RLE_ESC) { | ||
| 238 | |||
| 239 | *out++ = RLE_ESC; | ||
| 240 | *out++ = RLE_ESC; | ||
| 241 |                         in++; | ||
| 242 | |||
| 243 | } else { | ||
| 244 | |||
| 245 | ch = *in; | ||
| 246 | |||
| 247 | if ( (in_end-in) >= 3 /* enough space for a run */ | ||
| 248 | && ch == in[1] && ch == in[2] && ch == in[3] /* enough length */) { | ||
| 249 | |||
| 250 | p = in; | ||
| 251 | while (p < in_end && *p == ch && (p-in) < RLE_MAX) { | ||
| 252 |                                         p++; | ||
| 253 |                                 } | ||
| 254 | |||
| 255 | *out++ = RLE_ESC; | ||
| 256 | assert (RLE_MAX < 256); | ||
| 257 | *out++ = (unsigned char)(p - in); | ||
| 258 | *out++ = (unsigned char)ch; | ||
| 259 | in = p; | ||
| 260 | |||
| 261 | } else { | ||
| 262 | *out++ = *in++; | ||
| 263 |                         } | ||
| 264 |                 } | ||
| 265 |         } | ||
| 266 | |||
| 267 | if (ok) { | ||
| 268 |                 /*      *out++ = RLE_ESC; *out++ = RLE_TER; */ | ||
| 269 | out_len = out - out_start; | ||
| 270 | *pout_len = (size_t)out_len; | ||
| 271 | ok = (size_t)out_len <= out_max; | ||
| 272 |         } | ||
| 273 | |||
| 274 | return ok; | ||
| 275 | } | ||
| 276 | |||
| 277 | extern int | ||
| 278 | rle_decode | ||
| 279 | (const unsigned char *in_start, size_t in_len, unsigned char *out_start, size_t *pout_len, size_t out_max) | ||
| 280 | { | ||
| 281 | const unsigned char *in = in_start; | ||
| 282 | const unsigned char *in_end = in + in_len; | ||
| 283 | unsigned char *out = out_start; | ||
| 284 | unsigned char *out_end = out + *pout_len; | ||
| 285 | int ok = TRUE; | ||
| 286 | int ch; | ||
| 287 | int n; | ||
| 288 |         ptrdiff_t out_len; | ||
| 289 | |||
| 290 | while (in < in_end) | ||
| 291 |         { | ||
| 292 | if (in >= in_end) { ok = FALSE; break;} | ||
| 293 | if (out >= out_end) { ok = FALSE; break;} | ||
| 294 | |||
| 295 | if (*in == RLE_ESC) { | ||
| 296 | ++in; if (in >= in_end) { ok = FALSE; break;} | ||
| 297 | |||
| 298 | if (*in == RLE_ESC) { | ||
| 299 | *out++ = *in++; | ||
| 300 | } /*else if (*in == RLE_TER) {ok = TRUE;break;} */ else { | ||
| 301 | |||
| 302 |                                 /* rle */ | ||
| 303 | n = *in++; if (in >= in_end) { ok = FALSE; break;} | ||
| 304 | ch = *in++; | ||
| 305 | while (n-->0) { if (out >= out_end) { ok = FALSE; break;} | ||
| 306 | *out++ = (unsigned char)ch; | ||
| 307 |                                 } | ||
| 308 |                         } | ||
| 309 | } else { | ||
| 310 | *out++ = *in++; | ||
| 311 |                 } | ||
| 312 |         } | ||
| 313 | |||
| 314 | out_len = out - out_start; | ||
| 315 | |||
| 316 | if (ok) | ||
| 317 | *pout_len = (size_t)out_len; | ||
| 318 | |||
| 319 | ok = ok && (out_max >= (size_t)out_len); | ||
| 320 | |||
| 321 | return ok; | ||
| 322 | } | ||
| 323 | |||
| 324 | |||
| 325 | |||
| 326 | |||
| 327 | |||
| 328 | |||
| 329 | |||
| 330 | |||
| 331 | |||
| 332 | |||
| 333 | |||
| 334 | |||
| 335 | |||
| 336 | |||
| 337 | |||
| 338 |