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_H
25 #include "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"
39 #include "common/_error.h"
40
41 #ifdef BZIP2_FOUND
42
43 #include <bzlib.h>
44
45 struct bz2_file {
46 FILE *f;
47 BZFILE *file;
48 bool eof;
49 };
50
51 static struct bz2_file *bz2_fd_open(int fd)
52 {
53 struct bz2_file *b = NULL;
54 FILE* f;
55 int bzerror;
56
57 f = fdopen (fd, "r" );
58 if (f) {
59 b = malloc(sizeof(struct bz2_file));
60 b->f = f;
61 b->file = BZ2_bzReadOpen(&bzerror, f, 0, 0, NULL, 0);
62 b->eof = false;
63 if (bzerror != BZ_OK) {
64 oscap_seterr(OSCAP_EFAMILY_OSCAP, "Could not build BZ2FILE: %s",
65 BZ2_bzerror(b->file, &bzerror));
66 BZ2_bzReadClose(&bzerror, b->file);
67 free(b);
68 b = NULL;
69 }
70 }
71 return b;
72 }
73
74 //xmlInputReadCallback
75 static int bz2_file_read(struct bz2_file *bzfile, char *buffer, int len)
76 {
77 int bzerror;
78 if (bzfile->eof) {
79 // If we run bzRead on closed file we will get SEQUENCE_ERROR
80 return 0;
81 }
82 int size = BZ2_bzRead(&bzerror, (bzfile)->file, buffer, len);
83 if (bzerror == BZ_STREAM_END) {
84 bzfile->eof = true;
85 }
86 if (bzerror == BZ_OK || bzerror == BZ_STREAM_END)
87 return size;
88 else {
89 oscap_seterr(OSCAP_EFAMILY_OSCAP, "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_close(void *bzfile)
97 {
98 int bzerror;
99 BZ2_bzReadClose(&bzerror, ((struct bz2_file *)bzfile)->file);
100 fclose(((struct bz2_file *)bzfile)->f);
101 free(bzfile);
102 return bzerror == BZ_OK ? 0 : -1;
103 }
104
105 xmlDoc *bz2_fd_read_doc(int fd)
106 {
107 struct bz2_file *bzfile = bz2_fd_open(fd);
108 if (bzfile == NULL) {
109 return NULL;
110 }
111 return xmlReadIO((xmlInputReadCallback) bz2_file_read, bz2_file_close, bzfile, "url", NULL, XML_PARSE_PEDANTIC);
112 }
113
114 struct bz2_mem {
115 bz_stream *stream;
116 bool eof;
117 };
118
119 static void bz2_mem_free(struct bz2_mem *bzmem)
120 {
121 free(bzmem->stream);
122 free(bzmem);
123 }
124
125 static struct bz2_mem *bz2_mem_open(const char *buffer, size_t size)
126 {
127 struct bz2_mem *b = calloc(1, sizeof(struct bz2_mem));
128 b->stream = calloc(1, sizeof(bz_stream));
129 // next_in should point at the compressed data
130 b->stream->next_in = (char *) buffer;
131 // and avail_in should indicate how many bytes the library may read
132 b->stream->avail_in = size;
133 int bzerror = BZ2_bzDecompressInit(b->stream, 0, 0);
134 if (bzerror != BZ_OK) {
135 oscap_seterr(OSCAP_EFAMILY_OSCAP, "Could not build bz_stream from memory buffer: BZ2_bzDecompressInit returns %d", bzerror);
136 bz2_mem_free(b);
137 return NULL;
138 }
139 return b;
140 }
141
142 // xmlInputReadCallback
143 static int bz2_mem_read(struct 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->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->stream->next_out = buffer;
155 // with avail_out indicating how much output space is available.
156 bzmem->stream->avail_out = len;
157 int bzerror = BZ2_bzDecompress(bzmem->stream);
158 if (bzerror == BZ_STREAM_END) {
159 bzmem->eof = true;
160 }
161 if (bzerror == BZ_OK || bzerror == BZ_STREAM_END)
162 return (len - bzmem->stream->avail_out);
163 else {
164 oscap_seterr(OSCAP_EFAMILY_OSCAP, "Could not read from bz_stream: BZ2_bzDecompress returns %d", bzerror);
165 return -1;
166 }
167 }
168
169 // xmlInputCloseCallback
170 static int bz2_mem_close(void *bzmem) {
171 int bzerror = BZ2_bzDecompressEnd(((struct bz2_mem *)bzmem)->stream);
172 bz2_mem_free(bzmem);
173 if (bzerror != BZ_OK) {
174 oscap_seterr(OSCAP_EFAMILY_OSCAP, "Could not close bz_stream: BZ2_bzDecompressEnd returns %d", bzerror);
175 }
176 return bzerror == BZ_OK ? 0 : -1;
177 }
178
179 xmlDoc *bz2_mem_read_doc(const char *buffer, size_t size)
180 {
181 struct bz2_mem *bzmem = bz2_mem_open(buffer, size);
182 if (bzmem == NULL) {
183 return NULL;
184 }
185 return xmlReadIO((xmlInputReadCallback) bz2_mem_read, bz2_mem_close, bzmem, "url", NULL, XML_PARSE_PEDANTIC);
186 }
187
188 #endif
189
190 static const char magic_number[] = {'B','Z'};
191
192 bool bz2_memory_is_bzip(const char* memory, const size_t size){
193 if (size < 2){
194 return false; // Cannot read magic number
195 }
196
197 // compare memory header with reference magic_number of bz2
198 return ((memory[0] == magic_number[0]) && (memory[1] == magic_number[1]));
199 }
200
201 bool bz2_fd_is_bzip(int fd)
202 {
203 int fd_dup = dup(fd);
204 if (fd_dup == -1) {
205 return false;
206 }
207 FILE* file = fdopen(fd_dup, "r");
208 bool is_bzip;
209 if (file == NULL) {
210 return false; // 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 = (fgetc(file) == (int)magic_number[0]) && (fgetc(file) == (int)magic_number[1]);
214 }
215
216 fclose(file);
217 lseek(fd, 0, SEEK_SET);
218 return is_bzip;
219
220 }