Debian Codegraph @

src/source/bz2.c

raw symbols SCIP
1 /*
2 * Copyright 2014 Red Hat Inc., Durham, North Carolina.
3 * All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors:
20 * Šimon Lukašík
21 *
22 */
23
24 #ifdef HAVE_CONFIG_Hcxx . . $ `<built-in>:409:9`!
25 #include "config.h"cxx . . $ `<file>/build/config.h`/
26 #endif
27
28 #include <libxml/tree.h>
29 #include <libxml/parser.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #ifdef OS_WINDOWS
33 #include <io.h>
34 #else
35 #include <unistd.h>
36 #endif
37
38 #include "bz2_priv.h"cxx . . $ `<file>/src/source/bz2_priv.h`/
39 #include "common/_error.h"cxx . . $ `<file>/src/common/_error.h`/
40
41 #ifdef BZIP2_FOUNDcxx . . $ `build/config.h:50:9`!
42
43 #include <bzlib.h>
44
45 struct bz2_filecxx . . $ bz2_file# {
46 FILEcxx . . $ FILE# *fcxx . . $ bz2_file#f.;
47 BZFILEcxx . . $ BZFILE# *filecxx . . $ bz2_file#file.;
48 boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! eofcxx . . $ bz2_file#eof.;
49 };
50
51 static struct bz2_filecxx . . $ bz2_file# *bz2_fd_opencxx . . $ bz2_fd_open(aecd54dbe25b3eb7).(int fd)
52 {
53 struct bz2_filecxx . . $ bz2_file# *b = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
54 FILEcxx . . $ FILE#* f;
55 int bzerror;
56
57 f = fdopencxx . . $ fdopen(8f4e2d7350ebd077). (fd, "r" );
58 if (f) {
59 b = malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(sizeof(struct bz2_filecxx . . $ bz2_file#));
60 b->fcxx . . $ bz2_file#f. = f;
61 b->filecxx . . $ bz2_file#file. = BZ2_bzReadOpencxx . . $ BZ2_bzReadOpen(8ced67d232a1169b).(&bzerror, f, 0, 0, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, 0);
62 b->eofcxx . . $ bz2_file#eof. = falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!;
63 if (bzerror != BZ_OKcxx . . $ `<external>/6165568682737807376/bzlib.h:33:9`!) {
64 oscap_seterrcxx . . $ `src/common/_error.h:52:9`!(OSCAP_EFAMILY_OSCAPcxx . . $ `src/common/public/oscap_error.h:60:9`!, "Could not build BZ2FILE: %s",
65 BZ2_bzerror(b->file, &bzerror));
66 BZ2_bzReadClosecxx . . $ BZ2_bzReadClose(4328bed083d2e232).(&bzerror, b->filecxx . . $ bz2_file#file.);
67 freecxx . . $ free(5d22bdacc48458e8).(b);
68 b = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
69 }
70 }
71 return b;
72 }
73
74 //xmlInputReadCallback
75 static int bz2_file_readcxx . . $ bz2_file_read(8e5ab7ed3ab840fd).(struct bz2_filecxx . . $ bz2_file# *bzfile, char *buffer, int len)
76 {
77 int bzerror;
78 if (bzfile->eofcxx . . $ bz2_file#eof.) {
79 // If we run bzRead on closed file we will get SEQUENCE_ERROR
80 return 0;
81 }
82 int size = BZ2_bzReadcxx . . $ BZ2_bzRead(4db0a25201600cac).(&bzerror, (bzfile)->filecxx . . $ bz2_file#file., buffer, len);
83 if (bzerror == BZ_STREAM_ENDcxx . . $ `<external>/6165568682737807376/bzlib.h:37:9`!) {
84 bzfile->eofcxx . . $ bz2_file#eof. = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!;
85 }
86 if (bzerror == BZ_OKcxx . . $ `<external>/6165568682737807376/bzlib.h:33:9`! || bzerror == BZ_STREAM_ENDcxx . . $ `<external>/6165568682737807376/bzlib.h:37:9`!)
87 return size;
88 else {
89 oscap_seterrcxx . . $ `src/common/_error.h:52:9`!(OSCAP_EFAMILY_OSCAPcxx . . $ `src/common/public/oscap_error.h:60:9`!, "Could not read from bZ2FILE: %s",
90 BZ2_bzerror(bzfile->file, &bzerror));
91 return -1;
92 }
93 }
94
95 // xmlInputCloseCallback
96 static int bz2_file_closecxx . . $ bz2_file_close(dedf1e9b1c614a07).(void *bzfile)
97 {
98 int bzerror;
99 BZ2_bzReadClosecxx . . $ BZ2_bzReadClose(4328bed083d2e232).(&bzerror, ((struct bz2_filecxx . . $ bz2_file# *)bzfile)->filecxx . . $ bz2_file#file.);
100 fclosecxx . . $ fclose(89bc1efcdac0ca22).(((struct bz2_filecxx . . $ bz2_file# *)bzfile)->fcxx . . $ bz2_file#f.);
101 freecxx . . $ free(5d22bdacc48458e8).(bzfile);
102 return bzerror == BZ_OKcxx . . $ `<external>/6165568682737807376/bzlib.h:33:9`! ? 0 : -1;
103 }
104
105 xmlDoccxx . . $ xmlDoc# *bz2_fd_read_doccxx . . $ bz2_fd_read_doc(d2eb9631c5e91c58).(int fd)
106 {
107 struct bz2_filecxx . . $ bz2_file# *bzfile = bz2_fd_opencxx . . $ bz2_fd_open(aecd54dbe25b3eb7).(fd);
108 if (bzfile == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
109 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
110 }
111 return xmlReadIOcxx . . $ xmlReadIO(9173fa3a434095a6).((xmlInputReadCallbackcxx . . $ xmlInputReadCallback#) bz2_file_readcxx . . $ bz2_file_read(8e5ab7ed3ab840fd)., bz2_file_closecxx . . $ bz2_file_close(dedf1e9b1c614a07)., bzfile, "url", NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, XML_PARSE_PEDANTICcxx . . $ XML_PARSE_PEDANTIC.);
112 }
113
114 struct bz2_memcxx . . $ bz2_mem# {
115 bz_streamcxx . . $ bz_stream# *streamcxx . . $ bz2_mem#stream.;
116 boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! eofcxx . . $ bz2_mem#eof.;
117 };
118
119 static void bz2_mem_freecxx . . $ bz2_mem_free(595ee8715e549a2a).(struct bz2_memcxx . . $ bz2_mem# *bzmem)
120 {
121 freecxx . . $ free(5d22bdacc48458e8).(bzmem->streamcxx . . $ bz2_mem#stream.);
122 freecxx . . $ free(5d22bdacc48458e8).(bzmem);
123 }
124
125 static struct bz2_memcxx . . $ bz2_mem# *bz2_mem_opencxx . . $ bz2_mem_open(b01b130208673dcb).(const char *buffer, size_t size)
126 {
127 struct bz2_memcxx . . $ bz2_mem# *b = calloccxx . . $ calloc(63a7bf2941bfcd7a).(1, sizeof(struct bz2_memcxx . . $ bz2_mem#));
128 b->streamcxx . . $ bz2_mem#stream. = calloccxx . . $ calloc(63a7bf2941bfcd7a).(1, sizeof(bz_streamcxx . . $ bz_stream#));
129 // next_in should point at the compressed data
130 b->streamcxx . . $ bz2_mem#stream.->next_incxx . . $ $anonymous_type_3d9b658c97b16b00_0#next_in. = (char *) buffer;
131 // and avail_in should indicate how many bytes the library may read
132 b->streamcxx . . $ bz2_mem#stream.->avail_incxx . . $ $anonymous_type_3d9b658c97b16b00_0#avail_in. = size;
133 int bzerror = BZ2_bzDecompressInitcxx . . $ BZ2_bzDecompressInit(4bb5dd11623dbf9c).(b->streamcxx . . $ bz2_mem#stream., 0, 0);
134 if (bzerror != BZ_OKcxx . . $ `<external>/6165568682737807376/bzlib.h:33:9`!) {
135 oscap_seterrcxx . . $ `src/common/_error.h:52:9`!(OSCAP_EFAMILY_OSCAPcxx . . $ `src/common/public/oscap_error.h:60:9`!, "Could not build bz_stream from memory buffer: BZ2_bzDecompressInit returns %d", bzerror);
136 bz2_mem_freecxx . . $ bz2_mem_free(595ee8715e549a2a).(b);
137 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
138 }
139 return b;
140 }
141
142 // xmlInputReadCallback
143 static int bz2_mem_readcxx . . $ bz2_mem_read(79fe0d022fab85f9).(struct bz2_memcxx . . $ bz2_mem# *bzmem, char *buffer, int len)
144 {
145 if (len < 1) {
146 // ensure that at least one byte of output space is available at each BZ2_bzDecompress call.
147 return 0;
148 }
149 if (bzmem->eofcxx . . $ bz2_mem#eof.) {
150 // If we run BZ2_bzDecompress on processed buffer we will get -1 (SEQUENCE_ERROR)
151 return 0;
152 }
153 // next_out should point to a buffer in which the uncompressed output is to be placed
154 bzmem->streamcxx . . $ bz2_mem#stream.->next_outcxx . . $ $anonymous_type_3d9b658c97b16b00_0#next_out. = buffer;
155 // with avail_out indicating how much output space is available.
156 bzmem->streamcxx . . $ bz2_mem#stream.->avail_outcxx . . $ $anonymous_type_3d9b658c97b16b00_0#avail_out. = len;
157 int bzerror = BZ2_bzDecompresscxx . . $ BZ2_bzDecompress(71159293f31575f9).(bzmem->streamcxx . . $ bz2_mem#stream.);
158 if (bzerror == BZ_STREAM_ENDcxx . . $ `<external>/6165568682737807376/bzlib.h:37:9`!) {
159 bzmem->eofcxx . . $ bz2_mem#eof. = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!;
160 }
161 if (bzerror == BZ_OKcxx . . $ `<external>/6165568682737807376/bzlib.h:33:9`! || bzerror == BZ_STREAM_ENDcxx . . $ `<external>/6165568682737807376/bzlib.h:37:9`!)
162 return (len - bzmem->streamcxx . . $ bz2_mem#stream.->avail_outcxx . . $ $anonymous_type_3d9b658c97b16b00_0#avail_out.);
163 else {
164 oscap_seterrcxx . . $ `src/common/_error.h:52:9`!(OSCAP_EFAMILY_OSCAPcxx . . $ `src/common/public/oscap_error.h:60:9`!, "Could not read from bz_stream: BZ2_bzDecompress returns %d", bzerror);
165 return -1;
166 }
167 }
168
169 // xmlInputCloseCallback
170 static int bz2_mem_closecxx . . $ bz2_mem_close(dedf1e9b1c614a07).(void *bzmem) {
171 int bzerror = BZ2_bzDecompressEndcxx . . $ BZ2_bzDecompressEnd(71159293f31575f9).(((struct bz2_memcxx . . $ bz2_mem# *)bzmem)->streamcxx . . $ bz2_mem#stream.);
172 bz2_mem_freecxx . . $ bz2_mem_free(595ee8715e549a2a).(bzmem);
173 if (bzerror != BZ_OKcxx . . $ `<external>/6165568682737807376/bzlib.h:33:9`!) {
174 oscap_seterrcxx . . $ `src/common/_error.h:52:9`!(OSCAP_EFAMILY_OSCAPcxx . . $ `src/common/public/oscap_error.h:60:9`!, "Could not close bz_stream: BZ2_bzDecompressEnd returns %d", bzerror);
175 }
176 return bzerror == BZ_OKcxx . . $ `<external>/6165568682737807376/bzlib.h:33:9`! ? 0 : -1;
177 }
178
179 xmlDoccxx . . $ xmlDoc# *bz2_mem_read_doccxx . . $ bz2_mem_read_doc(a702256848c0d94e).(const char *buffer, size_t size)
180 {
181 struct bz2_memcxx . . $ bz2_mem# *bzmem = bz2_mem_opencxx . . $ bz2_mem_open(b01b130208673dcb).(buffer, size);
182 if (bzmem == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
183 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
184 }
185 return xmlReadIOcxx . . $ xmlReadIO(9173fa3a434095a6).((xmlInputReadCallbackcxx . . $ xmlInputReadCallback#) bz2_mem_readcxx . . $ bz2_mem_read(79fe0d022fab85f9)., bz2_mem_closecxx . . $ bz2_mem_close(dedf1e9b1c614a07)., bzmem, "url", NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, XML_PARSE_PEDANTICcxx . . $ XML_PARSE_PEDANTIC.);
186 }
187
188 #endif
189
190 static const char magic_numbercxx . . $ magic_number.[] = {'B','Z'};
191
192 boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! bz2_memory_is_bzipcxx . . $ bz2_memory_is_bzip(4be6f7de8d2c8d82).(const char* memory, const size_t size){
193 if (size < 2){
194 return falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!; // Cannot read magic number
195 }
196
197 // compare memory header with reference magic_number of bz2
198 return ((memory[0] == magic_numbercxx . . $ magic_number.[0]) && (memory[1] == magic_numbercxx . . $ magic_number.[1]));
199 }
200
201 boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! bz2_fd_is_bzipcxx . . $ bz2_fd_is_bzip(83c267b20bac841a).(int fd)
202 {
203 int fd_dup = dupcxx . . $ dup(7864480464b09eea).(fd);
204 if (fd_dup == -1) {
205 return falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!;
206 }
207 FILEcxx . . $ FILE#* file = fdopencxx . . $ fdopen(8f4e2d7350ebd077).(fd_dup, "r");
208 boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! is_bzip;
209 if (file == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
210 return falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!; // cannot open/determine file type
211 } else {
212 // Compare magic number with file header. Type casting to integer solve EOF (-1) returned by fgetc()
213 is_bzip = (fgetccxx . . $ fgetc(89bc1efcdac0ca22).(file) == (int)magic_numbercxx . . $ magic_number.[0]) && (fgetccxx . . $ fgetc(89bc1efcdac0ca22).(file) == (int)magic_numbercxx . . $ magic_number.[1]);
214 }
215
216 fclosecxx . . $ fclose(89bc1efcdac0ca22).(file);
217 lseekcxx . . $ lseek(f1afe674ac17c82e).(fd, 0, SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:110:9`!);
218 return is_bzip;
219
220 }