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