File: oabd.c

package info (click to toggle)
libmspack 0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 3,916 kB
  • sloc: sh: 11,332; ansic: 7,879; perl: 131; makefile: 97
file content (405 lines) | stat: -rw-r--r-- 10,316 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/* This file is part of libmspack.
 * © 2013 Intel Corporation
 *
 * libmspack is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License (LGPL) version 2.1
 *
 * For further details, see the file COPYING.LIB distributed with libmspack
 */

/* The Exchange Online Addressbook (OAB or sometimes OAL) is distributed
 * as a .LZX file in one of two forms. Either a "full download" containing
 * the entire address list, or an incremental binary patch which should be
 * applied to a previous version of the full decompressed data.
 *
 * The contents and format of the decompressed OAB are not handled here.
 *
 * For a complete description of the format, see the MSDN site:
 *
 * http://msdn.microsoft.com/en-us/library/cc463914 - [MS-OXOAB].pdf
 * http://msdn.microsoft.com/en-us/library/cc483133 - [MS-PATCH].pdf
 */

/* OAB decompression implementation */

#include <system.h>
#include <oab.h>
#include <lzx.h>
#include <crc32.h>

/* prototypes */
static int oabd_decompress(struct msoab_decompressor *self, const char *input,
			   const char *output);
static int oabd_decompress_incremental(struct msoab_decompressor *self,
				       const char *input, const char *base,
				       const char *output);

struct msoab_decompressor *
  mspack_create_oab_decompressor(struct mspack_system *sys)
{
  struct msoab_decompressor_p *self = NULL;

  if (!sys) sys = mspack_default_system;
  if (!mspack_valid_system(sys)) return NULL;

  if ((self = (struct msoab_decompressor_p *) sys->alloc(sys, sizeof(struct msoab_decompressor_p)))) {
    self->base.decompress             = &oabd_decompress;
    self->base.decompress_incremental = &oabd_decompress_incremental;
    self->system                      = sys;
  }
  return (struct msoab_decompressor *) self;
}

void mspack_destroy_oab_decompressor(struct msoab_decompressor *base) {
  struct msoab_decompressor_p *self = (struct msoab_decompressor_p *)base;
  if (self) {
    struct mspack_system *sys = self->system;
    sys->free(self);
  }
}

struct oabd_file {
  struct mspack_system *orig_sys;
  struct mspack_file *orig_file;
  unsigned int crc;
  size_t available;
};


static int oabd_sys_read (struct mspack_file *base_file, void *buf, int size)
{
  struct oabd_file *file = (struct oabd_file *)base_file;
  int bytes_read;

  if ((size_t)size > file->available)
    size = file->available;

  bytes_read = file->orig_sys->read(file->orig_file, buf, size);
  if (bytes_read < 0)
    return bytes_read;

  file->available -= bytes_read;
  return bytes_read;
}

static int oabd_sys_write (struct mspack_file *base_file, void *buf, int size)
{
  struct oabd_file *file = (struct oabd_file *)base_file;
  int bytes_written = file->orig_sys->write(file->orig_file, buf, size);

  if (bytes_written > 0)
    file->crc = crc32(file->crc, buf, bytes_written);

  return bytes_written;
}

static int oabd_decompress(struct msoab_decompressor *_self, const char *input,
			   const char *output)
{
  struct msoab_decompressor_p *self = (struct msoab_decompressor_p *) _self;
  struct mspack_system *sys;
  struct mspack_file *infh = NULL;
  struct mspack_file *outfh = NULL;
  unsigned char *buf = NULL;
  unsigned char hdrbuf[oabhead_SIZEOF];
  unsigned int block_max, target_size;
  struct lzxd_stream *lzx = NULL;
  struct mspack_system oabd_sys;
  struct oabd_file in_ofh, out_ofh;
  unsigned int window_bits;
  int ret = MSPACK_ERR_OK;

  if (!self) return MSPACK_ERR_ARGS;
  sys = self->system;

  infh = sys->open(sys, input, MSPACK_SYS_OPEN_READ);
  if (!infh) {
    ret = MSPACK_ERR_OPEN;
    goto out;
  }

  if (sys->read(infh, hdrbuf, oabhead_SIZEOF) != oabhead_SIZEOF) {
    ret = MSPACK_ERR_READ;
    goto out;
  }

  if (EndGetI32(&hdrbuf[oabhead_VersionHi]) != 3 ||
      EndGetI32(&hdrbuf[oabhead_VersionLo]) != 1) {
    ret = MSPACK_ERR_SIGNATURE;
    goto out;
  }

  block_max   = EndGetI32(&hdrbuf[oabhead_BlockMax]);
  target_size = EndGetI32(&hdrbuf[oabhead_TargetSize]);

  /* We use it for reading block headers too */
  if (block_max < oabblk_SIZEOF)
    block_max = oabblk_SIZEOF;

  outfh = sys->open(sys, output, MSPACK_SYS_OPEN_WRITE);
  if (!outfh) {
    ret = MSPACK_ERR_OPEN;
    goto out;
  }

  buf = sys->alloc(sys, block_max);
  if (!buf) {
    ret = MSPACK_ERR_NOMEMORY;
    goto out;
  }

  oabd_sys = *sys;
  oabd_sys.read = oabd_sys_read;
  oabd_sys.write = oabd_sys_write;

  in_ofh.orig_sys = sys;
  in_ofh.orig_file = infh;

  out_ofh.orig_sys = sys;
  out_ofh.orig_file = outfh;

  while (target_size) {
    unsigned int blk_csize, blk_dsize, blk_crc, blk_flags;

    if (sys->read(infh, buf, oabblk_SIZEOF) != oabblk_SIZEOF) {
      ret = MSPACK_ERR_READ;
      goto out;
    }
    blk_flags = EndGetI32(&buf[oabblk_Flags]);
    blk_csize = EndGetI32(&buf[oabblk_CompSize]);
    blk_dsize = EndGetI32(&buf[oabblk_UncompSize]);
    blk_crc   = EndGetI32(&buf[oabblk_CRC]);

    if (blk_dsize > block_max || blk_dsize > target_size || blk_flags > 1) {
      ret = MSPACK_ERR_DATAFORMAT;
      goto out;
    }

    if (!blk_flags) {
      /* Uncompressed block */
      if (blk_dsize != blk_csize) {
	ret = MSPACK_ERR_DATAFORMAT;
	goto out;
      }
      if (sys->read(infh, buf, blk_dsize) != (int)blk_dsize) {
	ret = MSPACK_ERR_READ;
	goto out;
      }
      if (sys->write(outfh, buf, blk_dsize) != (int)blk_dsize) {
	ret = MSPACK_ERR_WRITE;
	goto out;
      }
    } else {
      /* LZX compressed block */
      window_bits = 17;

      while (window_bits < 25 && (1U << window_bits) < blk_dsize)
	window_bits++;

      in_ofh.available = blk_csize;
      out_ofh.crc = 0xffffffff;

      lzx = lzxd_init(&oabd_sys, (void *)&in_ofh, (void *)&out_ofh, window_bits,
		      0, 4096, blk_dsize, 1);
      if (!lzx) {
	ret = MSPACK_ERR_NOMEMORY;
	goto out;
      }

      ret = lzxd_decompress(lzx, blk_dsize);
      if (ret != MSPACK_ERR_OK)
	goto out;

      lzxd_free(lzx);
      lzx = NULL;

      /* Consume any trailing padding bytes before the next block */
      while (in_ofh.available) {
	int count = block_max;
	if ((size_t)count > in_ofh.available)
	  count = in_ofh.available;

	count = sys->read(infh, buf, count);
	if (count < 0) {
	  ret = MSPACK_ERR_READ;
	  goto out;
	}
	in_ofh.available -= count;
      }

      if (out_ofh.crc != blk_crc) {
	ret = MSPACK_ERR_CHECKSUM;
	goto out;
      }
    }
    target_size -= blk_dsize;
  }

 out:
  if (lzx)
    lzxd_free(lzx);
  if (buf)
    sys->free(buf);
  if (outfh)
    sys->close(outfh);
  if (infh)
    sys->close(infh);

  return ret;
}

static int oabd_decompress_incremental(struct msoab_decompressor *_self,
				       const char *input, const char *base,
				       const char *output)
{
  struct msoab_decompressor_p *self = (struct msoab_decompressor_p *) _self;
  struct mspack_system *sys;
  struct mspack_file *infh = NULL;
  struct mspack_file *basefh = NULL;
  struct mspack_file *outfh = NULL;
  unsigned char *buf = NULL;
  unsigned char hdrbuf[patchhead_SIZEOF];
  unsigned int block_max, target_size;
  struct lzxd_stream *lzx = NULL;
  struct mspack_system oabd_sys;
  struct oabd_file in_ofh, out_ofh;
  unsigned int window_bits, window_size;
  int ret = MSPACK_ERR_OK;

  if (!self) return MSPACK_ERR_ARGS;
  sys = self->system;

  infh = sys->open(sys, input, MSPACK_SYS_OPEN_READ);
  if (!infh) {
    ret = MSPACK_ERR_OPEN;
    goto out;
  }

  if (sys->read(infh, hdrbuf, patchhead_SIZEOF) != patchhead_SIZEOF) {
    ret = MSPACK_ERR_READ;
    goto out;
  }

  if (EndGetI32(&hdrbuf[patchhead_VersionHi]) != 3 ||
      EndGetI32(&hdrbuf[patchhead_VersionLo]) != 2) {
    ret = MSPACK_ERR_SIGNATURE;
    goto out;
  }

  block_max = EndGetI32(&hdrbuf[patchhead_BlockMax]);
  target_size = EndGetI32(&hdrbuf[patchhead_TargetSize]);

  /* We use it for reading block headers too */
  if (block_max < patchblk_SIZEOF)
    block_max = patchblk_SIZEOF;

  basefh = sys->open(sys, base, MSPACK_SYS_OPEN_READ);
  if (!basefh) {
    ret = MSPACK_ERR_OPEN;
    goto out;
  }

  outfh = sys->open(sys, output, MSPACK_SYS_OPEN_WRITE);
  if (!outfh) {
    ret = MSPACK_ERR_OPEN;
    goto out;
  }

  buf = sys->alloc(sys, block_max);
  if (!buf) {
    ret = MSPACK_ERR_NOMEMORY;
    goto out;
  }

  oabd_sys = *sys;
  oabd_sys.read = oabd_sys_read;
  oabd_sys.write = oabd_sys_write;

  in_ofh.orig_sys = sys;
  in_ofh.orig_file = infh;

  out_ofh.orig_sys = sys;
  out_ofh.orig_file = outfh;

  while (target_size) {
    unsigned int blk_csize, blk_dsize, blk_ssize, blk_crc;

    if (sys->read(infh, buf, patchblk_SIZEOF) != patchblk_SIZEOF) {
      ret = MSPACK_ERR_READ;
      goto out;
    }
    blk_csize = EndGetI32(&buf[patchblk_PatchSize]);
    blk_dsize = EndGetI32(&buf[patchblk_TargetSize]);
    blk_ssize = EndGetI32(&buf[patchblk_SourceSize]);
    blk_crc   = EndGetI32(&buf[patchblk_CRC]);

    if (blk_dsize > block_max || blk_dsize > target_size ||
	blk_ssize > block_max) {
      ret = MSPACK_ERR_DATAFORMAT;
      goto out;
    }


    window_size = (blk_ssize + 32767) & ~32767;
    window_size += blk_dsize;
    window_bits = 17;

    while (window_bits < 25 && (1U << window_bits) < window_size)
      window_bits++;

    in_ofh.available = blk_csize;
    out_ofh.crc = 0xffffffff;

    lzx = lzxd_init(&oabd_sys, (void *)&in_ofh, (void *)&out_ofh, window_bits,
		    0, 4096, blk_dsize, 1);
    if (!lzx) {
      ret = MSPACK_ERR_NOMEMORY;
      goto out;
    }
    ret = lzxd_set_reference_data(lzx, sys, basefh, blk_ssize);
    if (ret != MSPACK_ERR_OK)
      goto out;

    ret = lzxd_decompress(lzx, blk_dsize);
    if (ret != MSPACK_ERR_OK)
      goto out;

    lzxd_free(lzx);
    lzx = NULL;

    /* Consume any trailing padding bytes before the next block */
    while (in_ofh.available) {
      int count = block_max;
      if ((size_t)count > in_ofh.available)
	count = in_ofh.available;

      count = sys->read(infh, buf, count);
      if (count < 0) {
	ret = MSPACK_ERR_READ;
	goto out;
      }
      in_ofh.available -= count;
    }

    if (out_ofh.crc != blk_crc) {
      ret = MSPACK_ERR_CHECKSUM;
      goto out;
    }

    target_size -= blk_dsize;
  }

 out:
  if (lzx)
    lzxd_free(lzx);
  if (buf)
    sys->free(buf);
  if (outfh)
    sys->close(outfh);
  if (basefh)
    sys->close(basefh);
  if (infh)
    sys->close(infh);

  return ret;
}