| 1 |
/* |
| 2 |
* SPDX-FileCopyrightText: (C) 1999-2008 Novell, Inc. (www.novell.com) |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "evolution-ews-config.h" |
| 7 |
|
| 8 |
#include <sys/types.h> |
| 9 |
#include <stdlib.h> |
| 10 |
#include <stdio.h> |
| 11 |
#include <math.h> |
| 12 |
#include <unistd.h> |
| 13 |
#include <glib.h> |
| 14 |
#include <glib/gstdio.h> |
| 15 |
#include "lzx.h" |
| 16 |
#include "../ews-oab-decompress.h" |
| 17 |
|
| 18 |
/* endian-neutral reading of little-endian data */ |
| 19 |
#define __egi32(a,n) ( ((((unsigned char *) a)[n+3]) << 24) | \ |
| 20 |
((((guchar *) a)[n + 2]) << 16) | \ |
| 21 |
((((guchar *) a)[n + 1]) << 8) | \ |
| 22 |
((((guchar *) a)[n + 0]))) |
| 23 |
#define EndGetI64(a) ((((unsigned long long int) __egi32(a,4)) << 32) | \ |
| 24 |
((guint) __egi32 (a,0))) |
| 25 |
#define EndGetI32(a) __egi32(a,0) |
| 26 |
#define EndGetI16(a) ((((a)[1])<<8)|((a)[0])) |
| 27 |
|
| 28 |
typedef struct { |
| 29 |
guint32 h_version; |
| 30 |
guint32 l_version; |
| 31 |
guint32 max_block_size; |
| 32 |
guint32 target_size; |
| 33 |
} LzxHeader; |
| 34 |
|
| 35 |
typedef struct { |
| 36 |
guint32 flags; |
| 37 |
guint32 comp_size; |
| 38 |
guint32 ucomp_size; |
| 39 |
guint32 crc; |
| 40 |
} LzxBlockHeader; |
| 41 |
|
| 42 |
static gboolean |
| 43 |
read_uint32 (FILE *input, |
| 44 |
guint32 *val) |
| 45 |
{ |
| 46 |
gchar buf[4]; |
| 47 |
|
| 48 |
if (fread (buf, 1, 4, input) == 4) { |
| 49 |
*val = EndGetI32 (buf); |
| 50 |
return TRUE; |
| 51 |
} else |
| 52 |
return FALSE; |
| 53 |
} |
| 54 |
|
| 55 |
static LzxHeader * |
| 56 |
read_headers (FILE *input, |
| 57 |
GError **error) |
| 58 |
{ |
| 59 |
LzxHeader *lzx_h; |
| 60 |
gboolean success; |
| 61 |
|
| 62 |
lzx_h = g_new0 (LzxHeader, 1); |
| 63 |
|
| 64 |
success = read_uint32 (input, &lzx_h->h_version); |
| 65 |
if (!success) |
| 66 |
goto exit; |
| 67 |
success = read_uint32 (input, &lzx_h->l_version); |
| 68 |
if (!success) |
| 69 |
goto exit; |
| 70 |
|
| 71 |
if (lzx_h->h_version != 0x00000003 || lzx_h->l_version != 0x00000001) { |
| 72 |
g_free (lzx_h); |
| 73 |
/* set the right domain later */ |
| 74 |
g_set_error_literal (error, g_quark_from_string ("lzx"), 1, "wrong version header"); |
| 75 |
return NULL; |
| 76 |
} |
| 77 |
|
| 78 |
success = read_uint32 (input, &lzx_h->max_block_size); |
| 79 |
if (!success) |
| 80 |
goto exit; |
| 81 |
success = read_uint32 (input, &lzx_h->target_size); |
| 82 |
if (!success) |
| 83 |
goto exit; |
| 84 |
|
| 85 |
exit: |
| 86 |
if (!success) { |
| 87 |
/* set the right domain later */ |
| 88 |
g_set_error_literal (error, g_quark_from_string ("lzx"), 1, "Unable to read lzx main header"); |
| 89 |
|
| 90 |
g_free (lzx_h); |
| 91 |
lzx_h = NULL; |
| 92 |
} |
| 93 |
|
| 94 |
return lzx_h; |
| 95 |
} |
| 96 |
|
| 97 |
static LzxBlockHeader * |
| 98 |
read_block_header (FILE *input, |
| 99 |
GError **error) |
| 100 |
{ |
| 101 |
LzxBlockHeader *lzx_b; |
| 102 |
gboolean success; |
| 103 |
|
| 104 |
lzx_b = g_new0 (LzxBlockHeader, 1); |
| 105 |
|
| 106 |
success = read_uint32 (input, &lzx_b->flags); |
| 107 |
if (!success) |
| 108 |
goto exit; |
| 109 |
|
| 110 |
success = read_uint32 (input, &lzx_b->comp_size); |
| 111 |
if (!success) |
| 112 |
goto exit; |
| 113 |
|
| 114 |
success = read_uint32 (input, &lzx_b->ucomp_size); |
| 115 |
if (!success) |
| 116 |
goto exit; |
| 117 |
|
| 118 |
success = read_uint32 (input, &lzx_b->crc); |
| 119 |
|
| 120 |
exit: |
| 121 |
if (!success) { |
| 122 |
/* set the right domain later */ |
| 123 |
g_set_error_literal (error, g_quark_from_string ("lzx"), 1, "Unable to read lzx block header"); |
| 124 |
|
| 125 |
g_free (lzx_b); |
| 126 |
lzx_b = NULL; |
| 127 |
} |
| 128 |
|
| 129 |
return lzx_b; |
| 130 |
} |
| 131 |
|
| 132 |
gboolean |
| 133 |
ews_oab_decompress_full (const gchar *filename, const gchar *output_filename, |
| 134 |
GError **error) |
| 135 |
{ |
| 136 |
LzxHeader *lzx_h = NULL; |
| 137 |
guint total_decomp_size = 0; |
| 138 |
FILE *input, *output = NULL; |
| 139 |
gboolean ret = TRUE; |
| 140 |
GError *err = NULL; |
| 141 |
|
| 142 |
input = fopen (filename, "rb"); |
| 143 |
if (!input) { |
| 144 |
g_set_error_literal (&err, g_quark_from_string ("lzx"), 1, "unable to open the input file"); |
| 145 |
ret = FALSE; |
| 146 |
goto exit; |
| 147 |
} |
| 148 |
|
| 149 |
output = fopen (output_filename, "wb"); |
| 150 |
if (!output) { |
| 151 |
g_set_error_literal (&err, g_quark_from_string ("lzx"), 1, "unable to open the output file"); |
| 152 |
ret = FALSE; |
| 153 |
goto exit; |
| 154 |
} |
| 155 |
|
| 156 |
lzx_h = read_headers (input, &err); |
| 157 |
if (!lzx_h) { |
| 158 |
ret = FALSE; |
| 159 |
goto exit; |
| 160 |
} |
| 161 |
|
| 162 |
/* TODO decompressing multiple lzx_blocks has not been tested yet. Will need to get a setup and test it. */ |
| 163 |
do { |
| 164 |
LzxBlockHeader *lzx_b; |
| 165 |
struct lzxd_stream *lzs; |
| 166 |
goffset offset; |
| 167 |
|
| 168 |
lzx_b = read_block_header (input, &err); |
| 169 |
if (err) { |
| 170 |
ret = FALSE; |
| 171 |
goto exit; |
| 172 |
} |
| 173 |
|
| 174 |
/* note the file offset */ |
| 175 |
offset = ftell (input); |
| 176 |
|
| 177 |
/* lzx_b points to 1, write it directly to file */ |
| 178 |
if (lzx_b->flags == 0) { |
| 179 |
gchar *buffer = g_malloc0 (lzx_b->ucomp_size); |
| 180 |
|
| 181 |
if (!(fread (buffer, 1, lzx_b->ucomp_size, input) == lzx_b->ucomp_size && |
| 182 |
fwrite (buffer, 1, lzx_b->ucomp_size, output) == lzx_b->ucomp_size)) { |
| 183 |
g_set_error_literal (&err, g_quark_from_string ("lzx"), 1, "failed to write data in output file"); |
| 184 |
g_free (buffer); |
| 185 |
ret = FALSE; |
| 186 |
goto exit; |
| 187 |
} |
| 188 |
g_free (buffer); |
| 189 |
} else { |
| 190 |
/* The window size should be the smallest power of two between 2^17 and 2^25 that is |
| 191 |
greater than or equal to the sum of the size of the reference data rounded up to |
| 192 |
a multiple of 32768 and the size of the subject data. Since we have no reference |
| 193 |
data, forget that and the rounding. Just the smallest power of two which is large |
| 194 |
enough to cover the subject data (lzx_b->ucomp_size). */ |
| 195 |
|
| 196 |
guint window_bits = g_bit_nth_msf(lzx_b->ucomp_size - 1, -1) + 1; |
| 197 |
|
| 198 |
if (window_bits < 17) |
| 199 |
window_bits = 17; |
| 200 |
else if (window_bits > 25) |
| 201 |
window_bits = 25; |
| 202 |
|
| 203 |
lzs = ews_lzxd_init (input, output, window_bits, |
| 204 |
0, 4096, lzx_b->ucomp_size, 1); |
| 205 |
if (!lzs) { |
| 206 |
g_set_error_literal (&err, g_quark_from_string ("lzx"), 1, "decompression failed (lzxd_init)"); |
| 207 |
ret = FALSE; |
| 208 |
goto exit; |
| 209 |
} |
| 210 |
if (ews_lzxd_decompress (lzs, lzx_b->ucomp_size) != LZX_ERR_OK) { |
| 211 |
g_set_error_literal (&err, g_quark_from_string ("lzx"), 1, "decompression failed (lzxd_decompress)"); |
| 212 |
ret = FALSE; |
| 213 |
goto exit; |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
/* Set the fp to beggining of next block. This is a HACK, looks like decompress reads beyond the block. |
| 218 |
* Since we can identify the next block start from block header, we just reset the offset */ |
| 219 |
offset += lzx_b->comp_size; |
| 220 |
fseek (input, offset, SEEK_SET); |
| 221 |
|
| 222 |
total_decomp_size += lzx_b->ucomp_size; |
| 223 |
g_free (lzx_b); |
| 224 |
} while (total_decomp_size < lzx_h->target_size); |
| 225 |
|
| 226 |
exit: |
| 227 |
if (input) |
| 228 |
fclose (input); |
| 229 |
|
| 230 |
if (output) |
| 231 |
fclose (output); |
| 232 |
|
| 233 |
if (err) { |
| 234 |
ret = FALSE; |
| 235 |
g_propagate_error (error, err); |
| 236 |
g_unlink (output_filename); |
| 237 |
} |
| 238 |
|
| 239 |
g_free (lzx_h); |
| 240 |
|
| 241 |
return ret; |
| 242 |
} |
| 243 |
|
| 244 |
typedef struct { |
| 245 |
guint32 h_version; |
| 246 |
guint32 l_version; |
| 247 |
guint32 max_block_size; |
| 248 |
guint32 source_size; |
| 249 |
guint32 target_size; |
| 250 |
guint32 source_crc; |
| 251 |
guint32 target_crc; |
| 252 |
} LzxPatchHeader; |
| 253 |
|
| 254 |
typedef struct { |
| 255 |
guint32 patch_size; |
| 256 |
guint32 target_size; |
| 257 |
guint32 source_size; |
| 258 |
guint32 crc; |
| 259 |
} LzxPatchBlockHeader; |
| 260 |
|
| 261 |
|
| 262 |
static LzxPatchHeader * |
| 263 |
read_patch_headers (FILE *input, |
| 264 |
GError **error) |
| 265 |
{ |
| 266 |
LzxPatchHeader *lzx_h; |
| 267 |
gboolean success; |
| 268 |
|
| 269 |
lzx_h = g_new0 (LzxPatchHeader, 1); |
| 270 |
|
| 271 |
success = read_uint32 (input, &lzx_h->h_version); |
| 272 |
if (!success) |
| 273 |
goto exit; |
| 274 |
success = read_uint32 (input, &lzx_h->l_version); |
| 275 |
if (!success) |
| 276 |
goto exit; |
| 277 |
|
| 278 |
if (lzx_h->h_version != 0x00000003 || lzx_h->l_version != 0x00000002) { |
| 279 |
g_free (lzx_h); |
| 280 |
/* set the right domain later */ |
| 281 |
g_set_error_literal (error, g_quark_from_string ("lzx"), 1, "wrong version header"); |
| 282 |
return NULL; |
| 283 |
} |
| 284 |
|
| 285 |
success = read_uint32 (input, &lzx_h->max_block_size); |
| 286 |
if (!success) |
| 287 |
goto exit; |
| 288 |
success = read_uint32 (input, &lzx_h->source_size); |
| 289 |
if (!success) |
| 290 |
goto exit; |
| 291 |
success = read_uint32 (input, &lzx_h->target_size); |
| 292 |
if (!success) |
| 293 |
goto exit; |
| 294 |
success = read_uint32 (input, &lzx_h->source_crc); |
| 295 |
if (!success) |
| 296 |
goto exit; |
| 297 |
success = read_uint32 (input, &lzx_h->target_crc); |
| 298 |
if (!success) |
| 299 |
goto exit; |
| 300 |
|
| 301 |
exit: |
| 302 |
if (!success) { |
| 303 |
/* set the right domain later */ |
| 304 |
g_set_error_literal (error, g_quark_from_string ("lzx"), 1, "Unable to read lzx main header"); |
| 305 |
|
| 306 |
g_free (lzx_h); |
| 307 |
lzx_h = NULL; |
| 308 |
} |
| 309 |
|
| 310 |
return lzx_h; |
| 311 |
} |
| 312 |
|
| 313 |
static LzxPatchBlockHeader * |
| 314 |
read_patch_block_header (FILE *input, |
| 315 |
GError **error) |
| 316 |
{ |
| 317 |
LzxPatchBlockHeader *lzx_b; |
| 318 |
gboolean success; |
| 319 |
|
| 320 |
lzx_b = g_new0 (LzxPatchBlockHeader, 1); |
| 321 |
|
| 322 |
success = read_uint32 (input, &lzx_b->patch_size); |
| 323 |
if (!success) |
| 324 |
goto exit; |
| 325 |
|
| 326 |
success = read_uint32 (input, &lzx_b->target_size); |
| 327 |
if (!success) |
| 328 |
goto exit; |
| 329 |
|
| 330 |
success = read_uint32 (input, &lzx_b->source_size); |
| 331 |
if (!success) |
| 332 |
goto exit; |
| 333 |
|
| 334 |
success = read_uint32 (input, &lzx_b->crc); |
| 335 |
|
| 336 |
exit: |
| 337 |
if (!success) { |
| 338 |
/* set the right domain later */ |
| 339 |
g_set_error_literal (error, g_quark_from_string ("lzx"), 1, "Unable to read lzx block header"); |
| 340 |
|
| 341 |
g_free (lzx_b); |
| 342 |
lzx_b = NULL; |
| 343 |
} |
| 344 |
|
| 345 |
return lzx_b; |
| 346 |
} |
| 347 |
|
| 348 |
gboolean |
| 349 |
ews_oab_decompress_patch (const gchar *filename, const gchar *orig_filename, |
| 350 |
const gchar *output_filename, GError **error) |
| 351 |
{ |
| 352 |
LzxPatchHeader *lzx_h = NULL; |
| 353 |
guint total_decomp_size = 0; |
| 354 |
FILE *input = NULL, *output = NULL, *orig_input = NULL; |
| 355 |
gboolean ret = TRUE; |
| 356 |
GError *err = NULL; |
| 357 |
|
| 358 |
input = fopen (filename, "rb"); |
| 359 |
if (!input) { |
| 360 |
g_set_error_literal (&err, g_quark_from_string ("lzx"), 1, "unable to open the input file"); |
| 361 |
ret = FALSE; |
| 362 |
goto exit; |
| 363 |
} |
| 364 |
|
| 365 |
orig_input = fopen (orig_filename, "rb"); |
| 366 |
if (!orig_input) { |
| 367 |
g_set_error_literal (&err, g_quark_from_string ("lzx"), 1, "unable to open the reference input file"); |
| 368 |
ret = FALSE; |
| 369 |
goto exit; |
| 370 |
} |
| 371 |
|
| 372 |
output = fopen (output_filename, "wb"); |
| 373 |
if (!output) { |
| 374 |
g_set_error_literal (&err, g_quark_from_string ("lzx"), 1, "unable to open the output file"); |
| 375 |
ret = FALSE; |
| 376 |
goto exit; |
| 377 |
} |
| 378 |
|
| 379 |
lzx_h = read_patch_headers (input, &err); |
| 380 |
if (!lzx_h) { |
| 381 |
ret = FALSE; |
| 382 |
goto exit; |
| 383 |
} |
| 384 |
|
| 385 |
/* TODO decompressing multiple lzx_blocks has not been tested yet. Will need to get a setup and test it. */ |
| 386 |
do { |
| 387 |
LzxPatchBlockHeader *lzx_b; |
| 388 |
struct lzxd_stream *lzs; |
| 389 |
goffset offset; |
| 390 |
guint ref_size, window_bits; |
| 391 |
|
| 392 |
lzx_b = read_patch_block_header (input, &err); |
| 393 |
if (err) { |
| 394 |
printf("err block header\n"); |
| 395 |
ret = FALSE; |
| 396 |
goto exit; |
| 397 |
} |
| 398 |
|
| 399 |
/* note the file offset */ |
| 400 |
offset = ftell(input); |
| 401 |
|
| 402 |
/* The window size should be the smallest power of two |
| 403 |
between 2^17 and 2^25 that is greater than or equal |
| 404 |
to the sum of the size of the reference data |
| 405 |
rounded up to a multiple of 32768 and the size of |
| 406 |
the subject data. */ |
| 407 |
ref_size = (lzx_b->source_size + 32767) & ~32767; |
| 408 |
window_bits = g_bit_nth_msf(ref_size + lzx_b->target_size - 1, -1) + 1; |
| 409 |
|
| 410 |
if (window_bits < 17) |
| 411 |
window_bits = 17; |
| 412 |
else if (window_bits > 25) |
| 413 |
window_bits = 25; |
| 414 |
|
| 415 |
lzs = ews_lzxd_init (input, output, window_bits, |
| 416 |
0, 4096, lzx_b->target_size, 1); |
| 417 |
if (!lzs) { |
| 418 |
g_set_error_literal (&err, g_quark_from_string ("lzx"), 1, "decompression failed (lzxd_init)"); |
| 419 |
ret = FALSE; |
| 420 |
goto exit; |
| 421 |
} |
| 422 |
if (ews_lzxd_set_reference_data(lzs, orig_input, lzx_b->source_size)) { |
| 423 |
g_set_error_literal (&err, g_quark_from_string ("lzx"), 1, "decompression failed (lzxd_set_reference_data)"); |
| 424 |
ret = FALSE; |
| 425 |
goto exit; |
| 426 |
} |
| 427 |
if (ews_lzxd_decompress (lzs, lzs->length) != LZX_ERR_OK) { |
| 428 |
g_set_error_literal (&err, g_quark_from_string ("lzx"), 1, "decompression failed (lzxd_decompress)"); |
| 429 |
ret = FALSE; |
| 430 |
goto exit; |
| 431 |
} |
| 432 |
|
| 433 |
/* Set the fp to beggining of next block. This is a HACK, looks like decompress reads beyond the block. |
| 434 |
* Since we can identify the next block start from block header, we just reset the offset */ |
| 435 |
offset += lzx_b->patch_size; |
| 436 |
fseek (input, offset, SEEK_SET); |
| 437 |
|
| 438 |
total_decomp_size += lzx_b->target_size; |
| 439 |
g_free (lzx_b); |
| 440 |
} while (total_decomp_size < lzx_h->target_size); |
| 441 |
|
| 442 |
exit: |
| 443 |
if (input) |
| 444 |
fclose (input); |
| 445 |
|
| 446 |
if (orig_input) |
| 447 |
fclose (orig_input); |
| 448 |
|
| 449 |
if (output) |
| 450 |
fclose (output); |
| 451 |
|
| 452 |
if (err) { |
| 453 |
ret = FALSE; |
| 454 |
g_propagate_error (error, err); |
| 455 |
g_unlink (output_filename); |
| 456 |
} |
| 457 |
|
| 458 |
g_free (lzx_h); |
| 459 |
|
| 460 |
return ret; |
| 461 |
} |
| 462 |
|