1*27685a8dSFam Zheng /*
2*27685a8dSFam Zheng * DMG bzip2 uncompression
3*27685a8dSFam Zheng *
4*27685a8dSFam Zheng * Copyright (c) 2004 Johannes E. Schindelin
5*27685a8dSFam Zheng * Copyright (c) 2016 Red Hat, Inc.
6*27685a8dSFam Zheng *
7*27685a8dSFam Zheng * Permission is hereby granted, free of charge, to any person obtaining a copy
8*27685a8dSFam Zheng * of this software and associated documentation files (the "Software"), to deal
9*27685a8dSFam Zheng * in the Software without restriction, including without limitation the rights
10*27685a8dSFam Zheng * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11*27685a8dSFam Zheng * copies of the Software, and to permit persons to whom the Software is
12*27685a8dSFam Zheng * furnished to do so, subject to the following conditions:
13*27685a8dSFam Zheng *
14*27685a8dSFam Zheng * The above copyright notice and this permission notice shall be included in
15*27685a8dSFam Zheng * all copies or substantial portions of the Software.
16*27685a8dSFam Zheng *
17*27685a8dSFam Zheng * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18*27685a8dSFam Zheng * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19*27685a8dSFam Zheng * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20*27685a8dSFam Zheng * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21*27685a8dSFam Zheng * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22*27685a8dSFam Zheng * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23*27685a8dSFam Zheng * THE SOFTWARE.
24*27685a8dSFam Zheng */
25*27685a8dSFam Zheng #include "qemu/osdep.h"
26*27685a8dSFam Zheng #include "dmg.h"
27*27685a8dSFam Zheng #include <bzlib.h>
28*27685a8dSFam Zheng
dmg_uncompress_bz2_do(char * next_in,unsigned int avail_in,char * next_out,unsigned int avail_out)29*27685a8dSFam Zheng static int dmg_uncompress_bz2_do(char *next_in, unsigned int avail_in,
30*27685a8dSFam Zheng char *next_out, unsigned int avail_out)
31*27685a8dSFam Zheng {
32*27685a8dSFam Zheng int ret;
33*27685a8dSFam Zheng uint64_t total_out;
34*27685a8dSFam Zheng bz_stream bzstream = {};
35*27685a8dSFam Zheng
36*27685a8dSFam Zheng ret = BZ2_bzDecompressInit(&bzstream, 0, 0);
37*27685a8dSFam Zheng if (ret != BZ_OK) {
38*27685a8dSFam Zheng return -1;
39*27685a8dSFam Zheng }
40*27685a8dSFam Zheng bzstream.next_in = next_in;
41*27685a8dSFam Zheng bzstream.avail_in = avail_in;
42*27685a8dSFam Zheng bzstream.next_out = next_out;
43*27685a8dSFam Zheng bzstream.avail_out = avail_out;
44*27685a8dSFam Zheng ret = BZ2_bzDecompress(&bzstream);
45*27685a8dSFam Zheng total_out = ((uint64_t)bzstream.total_out_hi32 << 32) +
46*27685a8dSFam Zheng bzstream.total_out_lo32;
47*27685a8dSFam Zheng BZ2_bzDecompressEnd(&bzstream);
48*27685a8dSFam Zheng if (ret != BZ_STREAM_END ||
49*27685a8dSFam Zheng total_out != avail_out) {
50*27685a8dSFam Zheng return -1;
51*27685a8dSFam Zheng }
52*27685a8dSFam Zheng return 0;
53*27685a8dSFam Zheng }
54*27685a8dSFam Zheng
55*27685a8dSFam Zheng __attribute__((constructor))
dmg_bz2_init(void)56*27685a8dSFam Zheng static void dmg_bz2_init(void)
57*27685a8dSFam Zheng {
58*27685a8dSFam Zheng assert(!dmg_uncompress_bz2);
59*27685a8dSFam Zheng dmg_uncompress_bz2 = dmg_uncompress_bz2_do;
60*27685a8dSFam Zheng }
61