xref: /openbmc/qemu/block/dmg-bz2.c (revision fcf5ef2a)
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 "qemu-common.h"
27 #include "dmg.h"
28 #include <bzlib.h>
29 
30 static int dmg_uncompress_bz2_do(char *next_in, unsigned int avail_in,
31                                  char *next_out, unsigned int avail_out)
32 {
33     int ret;
34     uint64_t total_out;
35     bz_stream bzstream = {};
36 
37     ret = BZ2_bzDecompressInit(&bzstream, 0, 0);
38     if (ret != BZ_OK) {
39         return -1;
40     }
41     bzstream.next_in = next_in;
42     bzstream.avail_in = avail_in;
43     bzstream.next_out = next_out;
44     bzstream.avail_out = avail_out;
45     ret = BZ2_bzDecompress(&bzstream);
46     total_out = ((uint64_t)bzstream.total_out_hi32 << 32) +
47                 bzstream.total_out_lo32;
48     BZ2_bzDecompressEnd(&bzstream);
49     if (ret != BZ_STREAM_END ||
50         total_out != avail_out) {
51         return -1;
52     }
53     return 0;
54 }
55 
56 __attribute__((constructor))
57 static void dmg_bz2_init(void)
58 {
59     assert(!dmg_uncompress_bz2);
60     dmg_uncompress_bz2 = dmg_uncompress_bz2_do;
61 }
62