File: | libxmlsec/unxlngi6.pro/misc/build/xmlsec1-1.2.14/src/buffer.c |
Location: | line 204, column 2 |
Description: | Null pointer passed as an argument to a 'nonnull' parameter |
1 | /** | |||
2 | * XML Security Library (http://www.aleksey.com/xmlsec). | |||
3 | * | |||
4 | * Memory buffer. | |||
5 | * | |||
6 | * This is free software; see Copyright file in the source | |||
7 | * distribution for preciese wording. | |||
8 | * | |||
9 | * Copyright (C) 2002-2003 Aleksey Sanin <aleksey@aleksey.com> | |||
10 | */ | |||
11 | #include "globals.h" | |||
12 | ||||
13 | #include <stdlib.h> | |||
14 | #include <string.h> | |||
15 | #include <ctype.h> | |||
16 | #include <errno(*__errno_location ()).h> | |||
17 | ||||
18 | #include <libxml/tree.h> | |||
19 | ||||
20 | #include <xmlsec/xmlsec.h> | |||
21 | #include <xmlsec/xmltree.h> | |||
22 | #include <xmlsec/base64.h> | |||
23 | #include <xmlsec/buffer.h> | |||
24 | #include <xmlsec/errors.h> | |||
25 | ||||
26 | /***************************************************************************** | |||
27 | * | |||
28 | * xmlSecBuffer | |||
29 | * | |||
30 | ****************************************************************************/ | |||
31 | static xmlSecAllocMode gAllocMode = xmlSecAllocModeDouble; | |||
32 | static xmlSecSizesize_t gInitialSize = 1024; | |||
33 | ||||
34 | /** | |||
35 | * xmlSecBufferSetDefaultAllocMode: | |||
36 | * @defAllocMode: the new default buffer allocation mode. | |||
37 | * @defInitialSize: the new default buffer minimal intial size. | |||
38 | * | |||
39 | * Sets new global default allocation mode and minimal intial size. | |||
40 | */ | |||
41 | void | |||
42 | xmlSecBufferSetDefaultAllocMode(xmlSecAllocMode defAllocMode, xmlSecSizesize_t defInitialSize) { | |||
43 | xmlSecAssert(defInitialSize > 0)if(!( defInitialSize > 0 ) ) { xmlSecError("buffer.c",43,__FUNCTION__ , ((void*)0), "defInitialSize > 0", 100, " "); return; }; | |||
44 | ||||
45 | gAllocMode = defAllocMode; | |||
46 | gInitialSize = defInitialSize; | |||
47 | } | |||
48 | ||||
49 | /** | |||
50 | * xmlSecBufferCreate: | |||
51 | * @size: the intial size. | |||
52 | * | |||
53 | * Allocates and initalizes new memory buffer with given size. | |||
54 | * Caller is responsible for calling #xmlSecBufferDestroy function | |||
55 | * to free the buffer. | |||
56 | * | |||
57 | * Returns: pointer to newly allocated buffer or NULL if an error occurs. | |||
58 | */ | |||
59 | xmlSecBufferPtr | |||
60 | xmlSecBufferCreate(xmlSecSizesize_t size) { | |||
61 | xmlSecBufferPtr buf; | |||
62 | int ret; | |||
63 | ||||
64 | buf = (xmlSecBufferPtr)xmlMalloc(sizeof(xmlSecBuffer)); | |||
65 | if(buf == NULL((void*)0)) { | |||
66 | xmlSecError(XMLSEC_ERRORS_HERE"buffer.c",66,__FUNCTION__, | |||
67 | NULL((void*)0), | |||
68 | NULL((void*)0), | |||
69 | XMLSEC_ERRORS_R_MALLOC_FAILED2, | |||
70 | "sizeof(xmlSecBuffer)=%d", sizeof(xmlSecBuffer)); | |||
71 | return(NULL((void*)0)); | |||
72 | } | |||
73 | ||||
74 | ret = xmlSecBufferInitialize(buf, size); | |||
75 | if(ret < 0) { | |||
76 | xmlSecError(XMLSEC_ERRORS_HERE"buffer.c",76,__FUNCTION__, | |||
77 | NULL((void*)0), | |||
78 | "xmlSecBufferInitialize", | |||
79 | XMLSEC_ERRORS_R_XMLSEC_FAILED1, | |||
80 | "size=%d", size); | |||
81 | xmlSecBufferDestroy(buf); | |||
82 | return(NULL((void*)0)); | |||
83 | } | |||
84 | return(buf); | |||
85 | } | |||
86 | ||||
87 | /** | |||
88 | * xmlSecBufferDestroy: | |||
89 | * @buf: the pointer to buffer object. | |||
90 | * | |||
91 | * Desrtoys buffer object created with #xmlSecBufferCreate function. | |||
92 | */ | |||
93 | void | |||
94 | xmlSecBufferDestroy(xmlSecBufferPtr buf) { | |||
95 | xmlSecAssert(buf != NULL)if(!( buf != ((void*)0) ) ) { xmlSecError("buffer.c",95,__FUNCTION__ , ((void*)0), "buf != NULL", 100, " "); return; }; | |||
96 | ||||
97 | xmlSecBufferFinalize(buf); | |||
98 | xmlFree(buf); | |||
99 | } | |||
100 | ||||
101 | /** | |||
102 | * xmlSecBufferInitialize: | |||
103 | * @buf: the pointer to buffer object. | |||
104 | * @size: the initial buffer size. | |||
105 | * | |||
106 | * Initializes buffer object @buf. Caller is responsible for calling | |||
107 | * #xmlSecBufferFinalize function to free allocated resources. | |||
108 | * | |||
109 | * Returns: 0 on success or a negative value if an error occurs. | |||
110 | */ | |||
111 | int | |||
112 | xmlSecBufferInitialize(xmlSecBufferPtr buf, xmlSecSizesize_t size) { | |||
113 | xmlSecAssert2(buf != NULL, -1)if(!( buf != ((void*)0) ) ) { xmlSecError("buffer.c",113,__FUNCTION__ , ((void*)0), "buf != NULL", 100, " "); return(-1); }; | |||
114 | ||||
115 | buf->data = NULL((void*)0); | |||
116 | buf->size = buf->maxSize = 0; | |||
117 | buf->allocMode = gAllocMode; | |||
118 | ||||
119 | return(xmlSecBufferSetMaxSize(buf, size)); | |||
120 | } | |||
121 | ||||
122 | /** | |||
123 | * xmlSecBufferFinalize: | |||
124 | * @buf: the pointer to buffer object. | |||
125 | * | |||
126 | * Frees allocated resource for a buffer intialized with #xmlSecBufferInitialize | |||
127 | * function. | |||
128 | */ | |||
129 | void | |||
130 | xmlSecBufferFinalize(xmlSecBufferPtr buf) { | |||
131 | xmlSecAssert(buf != NULL)if(!( buf != ((void*)0) ) ) { xmlSecError("buffer.c",131,__FUNCTION__ , ((void*)0), "buf != NULL", 100, " "); return; }; | |||
132 | ||||
133 | xmlSecBufferEmpty(buf); | |||
134 | if(buf->data != 0) { | |||
135 | xmlFree(buf->data); | |||
136 | } | |||
137 | buf->data = NULL((void*)0); | |||
138 | buf->size = buf->maxSize = 0; | |||
139 | } | |||
140 | ||||
141 | /** | |||
142 | * xmlSecBufferEmpty: | |||
143 | * @buf: the pointer to buffer object. | |||
144 | * | |||
145 | * Empties the buffer. | |||
146 | */ | |||
147 | void | |||
148 | xmlSecBufferEmpty(xmlSecBufferPtr buf) { | |||
149 | xmlSecAssert(buf != NULL)if(!( buf != ((void*)0) ) ) { xmlSecError("buffer.c",149,__FUNCTION__ , ((void*)0), "buf != NULL", 100, " "); return; }; | |||
150 | ||||
151 | if(buf->data != 0) { | |||
152 | xmlSecAssert(buf->maxSize > 0)if(!( buf->maxSize > 0 ) ) { xmlSecError("buffer.c",152 ,__FUNCTION__, ((void*)0), "buf->maxSize > 0", 100, " " ); return; }; | |||
153 | ||||
154 | memset(buf->data, 0, buf->maxSize); | |||
155 | } | |||
156 | buf->size = 0; | |||
157 | } | |||
158 | ||||
159 | /** | |||
160 | * xmlSecBufferGetData: | |||
161 | * @buf: the pointer to buffer object. | |||
162 | * | |||
163 | * Gets pointer to buffer's data. | |||
164 | * | |||
165 | * Returns: pointer to buffer's data. | |||
166 | */ | |||
167 | xmlSecByteunsigned char* | |||
168 | xmlSecBufferGetData(xmlSecBufferPtr buf) { | |||
169 | xmlSecAssert2(buf != NULL, NULL)if(!( buf != ((void*)0) ) ) { xmlSecError("buffer.c",169,__FUNCTION__ , ((void*)0), "buf != NULL", 100, " "); return(((void*)0)); }; | |||
170 | ||||
171 | return(buf->data); | |||
172 | } | |||
173 | ||||
174 | /** | |||
175 | * xmlSecBufferSetData: | |||
176 | * @buf: the pointer to buffer object. | |||
177 | * @data: the data. | |||
178 | * @size: the data size. | |||
179 | * | |||
180 | * Sets the value of the buffer to @data. | |||
181 | * | |||
182 | * Returns: 0 on success or a negative value if an error occurs. | |||
183 | */ | |||
184 | int | |||
185 | xmlSecBufferSetData(xmlSecBufferPtr buf, const xmlSecByteunsigned char* data, xmlSecSizesize_t size) { | |||
186 | int ret; | |||
187 | ||||
188 | xmlSecAssert2(buf != NULL, -1)if(!( buf != ((void*)0) ) ) { xmlSecError("buffer.c",188,__FUNCTION__ , ((void*)0), "buf != NULL", 100, " "); return(-1); }; | |||
189 | ||||
190 | xmlSecBufferEmpty(buf); | |||
191 | if(size > 0) { | |||
| ||||
192 | xmlSecAssert2(data != NULL, -1)if(!( data != ((void*)0) ) ) { xmlSecError("buffer.c",192,__FUNCTION__ , ((void*)0), "data != NULL", 100, " "); return(-1); }; | |||
193 | ||||
194 | ret = xmlSecBufferSetMaxSize(buf, size); | |||
195 | if(ret < 0) { | |||
196 | xmlSecError(XMLSEC_ERRORS_HERE"buffer.c",196,__FUNCTION__, | |||
197 | NULL((void*)0), | |||
198 | "xmlSecBufferSetMaxSize", | |||
199 | XMLSEC_ERRORS_R_XMLSEC_FAILED1, | |||
200 | "size=%d", size); | |||
201 | return(-1); | |||
202 | } | |||
203 | ||||
204 | memcpy(buf->data, data, size); | |||
| ||||
205 | } | |||
206 | ||||
207 | buf->size = size; | |||
208 | return(0); | |||
209 | } | |||
210 | ||||
211 | /** | |||
212 | * xmlSecBufferGetSize: | |||
213 | * @buf: the pointer to buffer object. | |||
214 | * | |||
215 | * Gets the current buffer data size. | |||
216 | * | |||
217 | * Returns: the current data size. | |||
218 | */ | |||
219 | xmlSecSizesize_t | |||
220 | xmlSecBufferGetSize(xmlSecBufferPtr buf) { | |||
221 | xmlSecAssert2(buf != NULL, 0)if(!( buf != ((void*)0) ) ) { xmlSecError("buffer.c",221,__FUNCTION__ , ((void*)0), "buf != NULL", 100, " "); return(0); }; | |||
222 | ||||
223 | return(buf->size); | |||
224 | } | |||
225 | ||||
226 | /** | |||
227 | * xmlSecBufferSetSize: | |||
228 | * @buf: the pointer to buffer object. | |||
229 | * @size: the new data size. | |||
230 | * | |||
231 | * Sets new buffer data size. If necessary, buffer grows to | |||
232 | * have at least @size bytes. | |||
233 | * | |||
234 | * Returns: 0 on success or a negative value if an error occurs. | |||
235 | */ | |||
236 | int | |||
237 | xmlSecBufferSetSize(xmlSecBufferPtr buf, xmlSecSizesize_t size) { | |||
238 | int ret; | |||
239 | ||||
240 | xmlSecAssert2(buf != NULL, -1)if(!( buf != ((void*)0) ) ) { xmlSecError("buffer.c",240,__FUNCTION__ , ((void*)0), "buf != NULL", 100, " "); return(-1); }; | |||
241 | ||||
242 | ret = xmlSecBufferSetMaxSize(buf, size); | |||
243 | if(ret < 0) { | |||
244 | xmlSecError(XMLSEC_ERRORS_HERE"buffer.c",244,__FUNCTION__, | |||
245 | NULL((void*)0), | |||
246 | "xmlSecBufferSetMaxSize", | |||
247 | XMLSEC_ERRORS_R_XMLSEC_FAILED1, | |||
248 | "size=%d", size); | |||
249 | return(-1); | |||
250 | } | |||
251 | ||||
252 | ||||
253 | buf->size = size; | |||
254 | return(0); | |||
255 | } | |||
256 | ||||
257 | /** | |||
258 | * xmlSecBufferGetMaxSize: | |||
259 | * @buf: the pointer to buffer object. | |||
260 | * | |||
261 | * Gets the maximum (allocated) buffer size. | |||
262 | * | |||
263 | * Returns: the maximum (allocated) buffer size. | |||
264 | */ | |||
265 | xmlSecSizesize_t | |||
266 | xmlSecBufferGetMaxSize(xmlSecBufferPtr buf) { | |||
267 | xmlSecAssert2(buf != NULL, 0)if(!( buf != ((void*)0) ) ) { xmlSecError("buffer.c",267,__FUNCTION__ , ((void*)0), "buf != NULL", 100, " "); return(0); }; | |||
268 | ||||
269 | return(buf->maxSize); | |||
270 | } | |||
271 | ||||
272 | /** | |||
273 | * xmlSecBufferSetMaxSize: | |||
274 | * @buf: the pointer to buffer object. | |||
275 | * @size: the new maximum size. | |||
276 | * | |||
277 | * Sets new buffer maximum size. If necessary, buffer grows to | |||
278 | * have at least @size bytes. | |||
279 | * | |||
280 | * Returns: 0 on success or a negative value if an error occurs. | |||
281 | */ | |||
282 | int | |||
283 | xmlSecBufferSetMaxSize(xmlSecBufferPtr buf, xmlSecSizesize_t size) { | |||
284 | xmlSecByteunsigned char* newData; | |||
285 | xmlSecSizesize_t newSize = 0; | |||
286 | ||||
287 | xmlSecAssert2(buf != NULL, -1)if(!( buf != ((void*)0) ) ) { xmlSecError("buffer.c",287,__FUNCTION__ , ((void*)0), "buf != NULL", 100, " "); return(-1); }; | |||
288 | if(size <= buf->maxSize) { | |||
289 | return(0); | |||
290 | } | |||
291 | ||||
292 | switch(buf->allocMode) { | |||
293 | case xmlSecAllocModeExact: | |||
294 | newSize = size + 8; | |||
295 | break; | |||
296 | case xmlSecAllocModeDouble: | |||
297 | newSize = 2 * size + 32; | |||
298 | break; | |||
299 | } | |||
300 | ||||
301 | if(newSize < gInitialSize) { | |||
302 | newSize = gInitialSize; | |||
303 | } | |||
304 | ||||
305 | ||||
306 | if(buf->data != NULL((void*)0)) { | |||
307 | newData = (xmlSecByteunsigned char*)xmlRealloc(buf->data, newSize); | |||
308 | } else { | |||
309 | newData = (xmlSecByteunsigned char*)xmlMalloc(newSize); | |||
310 | } | |||
311 | if(newData == NULL((void*)0)) { | |||
312 | xmlSecError(XMLSEC_ERRORS_HERE"buffer.c",312,__FUNCTION__, | |||
313 | NULL((void*)0), | |||
314 | NULL((void*)0), | |||
315 | XMLSEC_ERRORS_R_MALLOC_FAILED2, | |||
316 | "size=%d", newSize); | |||
317 | return(-1); | |||
318 | } | |||
319 | ||||
320 | buf->data = newData; | |||
321 | buf->maxSize = newSize; | |||
322 | ||||
323 | if(buf->size < buf->maxSize) { | |||
324 | xmlSecAssert2(buf->data != NULL, -1)if(!( buf->data != ((void*)0) ) ) { xmlSecError("buffer.c" ,324,__FUNCTION__, ((void*)0), "buf->data != NULL", 100, " " ); return(-1); }; | |||
325 | memset(buf->data + buf->size, 0, buf->maxSize - buf->size); | |||
326 | } | |||
327 | ||||
328 | return(0); | |||
329 | } | |||
330 | ||||
331 | /** | |||
332 | * xmlSecBufferAppend: | |||
333 | * @buf: the pointer to buffer object. | |||
334 | * @data: the data. | |||
335 | * @size: the data size. | |||
336 | * | |||
337 | * Appends the @data after the current data stored in the buffer. | |||
338 | * | |||
339 | * Returns: 0 on success or a negative value if an error occurs. | |||
340 | */ | |||
341 | int | |||
342 | xmlSecBufferAppend(xmlSecBufferPtr buf, const xmlSecByteunsigned char* data, xmlSecSizesize_t size) { | |||
343 | int ret; | |||
344 | ||||
345 | xmlSecAssert2(buf != NULL, -1)if(!( buf != ((void*)0) ) ) { xmlSecError("buffer.c",345,__FUNCTION__ , ((void*)0), "buf != NULL", 100, " "); return(-1); }; | |||
346 | ||||
347 | if(size > 0) { | |||
348 | xmlSecAssert2(data != NULL, -1)if(!( data != ((void*)0) ) ) { xmlSecError("buffer.c",348,__FUNCTION__ , ((void*)0), "data != NULL", 100, " "); return(-1); }; | |||
349 | ||||
350 | ret = xmlSecBufferSetMaxSize(buf, buf->size + size); | |||
351 | if(ret < 0) { | |||
352 | xmlSecError(XMLSEC_ERRORS_HERE"buffer.c",352,__FUNCTION__, | |||
353 | NULL((void*)0), | |||
354 | "xmlSecBufferSetMaxSize", | |||
355 | XMLSEC_ERRORS_R_XMLSEC_FAILED1, | |||
356 | "size=%d", buf->size + size); | |||
357 | return(-1); | |||
358 | } | |||
359 | ||||
360 | memcpy(buf->data + buf->size, data, size); | |||
361 | buf->size += size; | |||
362 | } | |||
363 | ||||
364 | return(0); | |||
365 | } | |||
366 | ||||
367 | /** | |||
368 | * xmlSecBufferPrepend: | |||
369 | * @buf: the pointer to buffer object. | |||
370 | * @data: the data. | |||
371 | * @size: the data size. | |||
372 | * | |||
373 | * Prepends the @data before the current data stored in the buffer. | |||
374 | * | |||
375 | * Returns: 0 on success or a negative value if an error occurs. | |||
376 | */ | |||
377 | int | |||
378 | xmlSecBufferPrepend(xmlSecBufferPtr buf, const xmlSecByteunsigned char* data, xmlSecSizesize_t size) { | |||
379 | int ret; | |||
380 | ||||
381 | xmlSecAssert2(buf != NULL, -1)if(!( buf != ((void*)0) ) ) { xmlSecError("buffer.c",381,__FUNCTION__ , ((void*)0), "buf != NULL", 100, " "); return(-1); }; | |||
382 | ||||
383 | if(size > 0) { | |||
384 | xmlSecAssert2(data != NULL, -1)if(!( data != ((void*)0) ) ) { xmlSecError("buffer.c",384,__FUNCTION__ , ((void*)0), "data != NULL", 100, " "); return(-1); }; | |||
385 | ||||
386 | ret = xmlSecBufferSetMaxSize(buf, buf->size + size); | |||
387 | if(ret < 0) { | |||
388 | xmlSecError(XMLSEC_ERRORS_HERE"buffer.c",388,__FUNCTION__, | |||
389 | NULL((void*)0), | |||
390 | "xmlSecBufferSetMaxSize", | |||
391 | XMLSEC_ERRORS_R_XMLSEC_FAILED1, | |||
392 | "size=%d", buf->size + size); | |||
393 | return(-1); | |||
394 | } | |||
395 | ||||
396 | memmove(buf->data + size, buf->data, buf->size); | |||
397 | memcpy(buf->data, data, size); | |||
398 | buf->size += size; | |||
399 | } | |||
400 | ||||
401 | return(0); | |||
402 | } | |||
403 | ||||
404 | /** | |||
405 | * xmlSecBufferRemoveHead: | |||
406 | * @buf: the pointer to buffer object. | |||
407 | * @size: the number of bytes to be removed. | |||
408 | * | |||
409 | * Removes @size bytes from the beginning of the current buffer. | |||
410 | * | |||
411 | * Returns: 0 on success or a negative value if an error occurs. | |||
412 | */ | |||
413 | int | |||
414 | xmlSecBufferRemoveHead(xmlSecBufferPtr buf, xmlSecSizesize_t size) { | |||
415 | xmlSecAssert2(buf != NULL, -1)if(!( buf != ((void*)0) ) ) { xmlSecError("buffer.c",415,__FUNCTION__ , ((void*)0), "buf != NULL", 100, " "); return(-1); }; | |||
416 | ||||
417 | if(size < buf->size) { | |||
418 | xmlSecAssert2(buf->data != NULL, -1)if(!( buf->data != ((void*)0) ) ) { xmlSecError("buffer.c" ,418,__FUNCTION__, ((void*)0), "buf->data != NULL", 100, " " ); return(-1); }; | |||
419 | ||||
420 | buf->size -= size; | |||
421 | memmove(buf->data, buf->data + size, buf->size); | |||
422 | } else { | |||
423 | buf->size = 0; | |||
424 | } | |||
425 | if(buf->size < buf->maxSize) { | |||
426 | xmlSecAssert2(buf->data != NULL, -1)if(!( buf->data != ((void*)0) ) ) { xmlSecError("buffer.c" ,426,__FUNCTION__, ((void*)0), "buf->data != NULL", 100, " " ); return(-1); }; | |||
427 | memset(buf->data + buf->size, 0, buf->maxSize - buf->size); | |||
428 | } | |||
429 | return(0); | |||
430 | } | |||
431 | ||||
432 | /** | |||
433 | * xmlSecBufferRemoveTail: | |||
434 | * @buf: the pointer to buffer object. | |||
435 | * @size: the number of bytes to be removed. | |||
436 | * | |||
437 | * Removes @size bytes from the end of current buffer. | |||
438 | * | |||
439 | * Returns: 0 on success or a negative value if an error occurs. | |||
440 | */ | |||
441 | int | |||
442 | xmlSecBufferRemoveTail(xmlSecBufferPtr buf, xmlSecSizesize_t size) { | |||
443 | xmlSecAssert2(buf != NULL, -1)if(!( buf != ((void*)0) ) ) { xmlSecError("buffer.c",443,__FUNCTION__ , ((void*)0), "buf != NULL", 100, " "); return(-1); }; | |||
444 | ||||
445 | if(size < buf->size) { | |||
446 | buf->size -= size; | |||
447 | } else { | |||
448 | buf->size = 0; | |||
449 | } | |||
450 | if(buf->size < buf->maxSize) { | |||
451 | xmlSecAssert2(buf->data != NULL, -1)if(!( buf->data != ((void*)0) ) ) { xmlSecError("buffer.c" ,451,__FUNCTION__, ((void*)0), "buf->data != NULL", 100, " " ); return(-1); }; | |||
452 | memset(buf->data + buf->size, 0, buf->maxSize - buf->size); | |||
453 | } | |||
454 | return(0); | |||
455 | } | |||
456 | ||||
457 | /** | |||
458 | * xmlSecBufferReadFile: | |||
459 | * @buf: the pointer to buffer object. | |||
460 | * @filename: the filename. | |||
461 | * | |||
462 | * Reads the content of the file @filename in the buffer. | |||
463 | * | |||
464 | * Returns: 0 on success or a negative value if an error occurs. | |||
465 | */ | |||
466 | int | |||
467 | xmlSecBufferReadFile(xmlSecBufferPtr buf, const char* filename) { | |||
468 | xmlSecByteunsigned char buffer[1024]; | |||
469 | FILE* f; | |||
470 | int ret, len; | |||
471 | ||||
472 | xmlSecAssert2(buf != NULL, -1)if(!( buf != ((void*)0) ) ) { xmlSecError("buffer.c",472,__FUNCTION__ , ((void*)0), "buf != NULL", 100, " "); return(-1); }; | |||
473 | xmlSecAssert2(filename != NULL, -1)if(!( filename != ((void*)0) ) ) { xmlSecError("buffer.c",473 ,__FUNCTION__, ((void*)0), "filename != NULL", 100, " "); return (-1); }; | |||
474 | ||||
475 | f = fopen(filename, "rb"); | |||
476 | if(f == NULL((void*)0)) { | |||
477 | xmlSecError(XMLSEC_ERRORS_HERE"buffer.c",477,__FUNCTION__, | |||
478 | NULL((void*)0), | |||
479 | "fopen", | |||
480 | XMLSEC_ERRORS_R_IO_FAILED7, | |||
481 | "filename=%s;errno=%d", | |||
482 | xmlSecErrorsSafeString(filename)(((filename) != ((void*)0)) ? ((char*)(filename)) : (char*)"NULL" ), | |||
483 | errno(*__errno_location ())); | |||
484 | return(-1); | |||
485 | } | |||
486 | ||||
487 | while(1) { | |||
488 | len = fread(buffer, 1, sizeof(buffer), f); | |||
489 | if(len == 0) { | |||
490 | break; | |||
491 | }else if(len < 0) { | |||
492 | xmlSecError(XMLSEC_ERRORS_HERE"buffer.c",492,__FUNCTION__, | |||
493 | NULL((void*)0), | |||
494 | "fread", | |||
495 | XMLSEC_ERRORS_R_IO_FAILED7, | |||
496 | "filename=%s;errno=%d", | |||
497 | xmlSecErrorsSafeString(filename)(((filename) != ((void*)0)) ? ((char*)(filename)) : (char*)"NULL" ), | |||
498 | errno(*__errno_location ())); | |||
499 | fclose(f); | |||
500 | return(-1); | |||
501 | } | |||
502 | ||||
503 | ret = xmlSecBufferAppend(buf, buffer, len); | |||
504 | if(ret < 0) { | |||
505 | xmlSecError(XMLSEC_ERRORS_HERE"buffer.c",505,__FUNCTION__, | |||
506 | NULL((void*)0), | |||
507 | "xmlSecBufferAppend", | |||
508 | XMLSEC_ERRORS_R_XMLSEC_FAILED1, | |||
509 | "size=%d", | |||
510 | len); | |||
511 | fclose(f); | |||
512 | return(-1); | |||
513 | } | |||
514 | } | |||
515 | ||||
516 | fclose(f); | |||
517 | return(0); | |||
518 | } | |||
519 | ||||
520 | /** | |||
521 | * xmlSecBufferBase64NodeContentRead: | |||
522 | * @buf: the pointer to buffer object. | |||
523 | * @node: the pointer to node. | |||
524 | * | |||
525 | * Reads the content of the @node, base64 decodes it and stores the | |||
526 | * result in the buffer. | |||
527 | * | |||
528 | * Returns: 0 on success or a negative value if an error occurs. | |||
529 | */ | |||
530 | int | |||
531 | xmlSecBufferBase64NodeContentRead(xmlSecBufferPtr buf, xmlNodePtr node) { | |||
532 | xmlChar* content; | |||
533 | xmlSecSizesize_t size; | |||
534 | int ret; | |||
535 | ||||
536 | xmlSecAssert2(buf != NULL, -1)if(!( buf != ((void*)0) ) ) { xmlSecError("buffer.c",536,__FUNCTION__ , ((void*)0), "buf != NULL", 100, " "); return(-1); }; | |||
537 | xmlSecAssert2(node != NULL, -1)if(!( node != ((void*)0) ) ) { xmlSecError("buffer.c",537,__FUNCTION__ , ((void*)0), "node != NULL", 100, " "); return(-1); }; | |||
538 | ||||
539 | content = xmlNodeGetContent(node); | |||
540 | if(content == NULL((void*)0)) { | |||
541 | xmlSecError(XMLSEC_ERRORS_HERE"buffer.c",541,__FUNCTION__, | |||
542 | NULL((void*)0), | |||
543 | xmlSecErrorsSafeString(xmlSecNodeGetName(node))((((((node)) ? ((const char*)((node)->name)) : ((void*)0)) ) != ((void*)0)) ? ((char*)((((node)) ? ((const char*)((node) ->name)) : ((void*)0)))) : (char*)"NULL"), | |||
544 | XMLSEC_ERRORS_R_INVALID_NODE_CONTENT22, | |||
545 | XMLSEC_ERRORS_NO_MESSAGE" "); | |||
546 | return(-1); | |||
547 | } | |||
548 | ||||
549 | /* base64 decode size is less than input size */ | |||
550 | ret = xmlSecBufferSetMaxSize(buf, xmlStrlen(content)); | |||
551 | if(ret < 0) { | |||
552 | xmlSecError(XMLSEC_ERRORS_HERE"buffer.c",552,__FUNCTION__, | |||
553 | NULL((void*)0), | |||
554 | "xmlSecBufferSetMaxSize", | |||
555 | XMLSEC_ERRORS_R_XMLSEC_FAILED1, | |||
556 | XMLSEC_ERRORS_NO_MESSAGE" "); | |||
557 | xmlFree(content); | |||
558 | return(-1); | |||
559 | } | |||
560 | ||||
561 | ret = xmlSecBase64Decode(content, xmlSecBufferGetData(buf), xmlSecBufferGetMaxSize(buf)); | |||
562 | if(ret < 0) { | |||
563 | xmlSecError(XMLSEC_ERRORS_HERE"buffer.c",563,__FUNCTION__, | |||
564 | NULL((void*)0), | |||
565 | "xmlSecBase64Decode", | |||
566 | XMLSEC_ERRORS_R_XMLSEC_FAILED1, | |||
567 | XMLSEC_ERRORS_NO_MESSAGE" "); | |||
568 | xmlFree(content); | |||
569 | return(-1); | |||
570 | } | |||
571 | size = ret; | |||
572 | ||||
573 | ret = xmlSecBufferSetSize(buf, size); | |||
574 | if(ret < 0) { | |||
575 | xmlSecError(XMLSEC_ERRORS_HERE"buffer.c",575,__FUNCTION__, | |||
576 | NULL((void*)0), | |||
577 | "xmlSecBufferSetSize", | |||
578 | XMLSEC_ERRORS_R_XMLSEC_FAILED1, | |||
579 | "size=%d", size); | |||
580 | xmlFree(content); | |||
581 | return(-1); | |||
582 | } | |||
583 | xmlFree(content); | |||
584 | ||||
585 | return(0); | |||
586 | } | |||
587 | ||||
588 | /** | |||
589 | * xmlSecBufferBase64NodeContentWrite: | |||
590 | * @buf: the pointer to buffer object. | |||
591 | * @node: the pointer to a node. | |||
592 | * @columns: the max line size fro base64 encoded data. | |||
593 | * | |||
594 | * Sets the content of the @node to the base64 encoded buffer data. | |||
595 | * | |||
596 | * Returns: 0 on success or a negative value if an error occurs. | |||
597 | */ | |||
598 | int | |||
599 | xmlSecBufferBase64NodeContentWrite(xmlSecBufferPtr buf, xmlNodePtr node, int columns) { | |||
600 | xmlChar* content; | |||
601 | ||||
602 | xmlSecAssert2(buf != NULL, -1)if(!( buf != ((void*)0) ) ) { xmlSecError("buffer.c",602,__FUNCTION__ , ((void*)0), "buf != NULL", 100, " "); return(-1); }; | |||
603 | xmlSecAssert2(node != NULL, -1)if(!( node != ((void*)0) ) ) { xmlSecError("buffer.c",603,__FUNCTION__ , ((void*)0), "node != NULL", 100, " "); return(-1); }; | |||
604 | ||||
605 | content = xmlSecBase64Encode(xmlSecBufferGetData(buf), xmlSecBufferGetSize(buf), columns); | |||
606 | if(content == NULL((void*)0)) { | |||
607 | xmlSecError(XMLSEC_ERRORS_HERE"buffer.c",607,__FUNCTION__, | |||
608 | NULL((void*)0), | |||
609 | "xmlSecBase64Encode", | |||
610 | XMLSEC_ERRORS_R_XMLSEC_FAILED1, | |||
611 | XMLSEC_ERRORS_NO_MESSAGE" "); | |||
612 | return(-1); | |||
613 | } | |||
614 | xmlNodeAddContent(node, content); | |||
615 | xmlFree(content); | |||
616 | ||||
617 | return(0); | |||
618 | } | |||
619 | ||||
620 | /************************************************************************ | |||
621 | * | |||
622 | * IO buffer | |||
623 | * | |||
624 | ************************************************************************/ | |||
625 | static int xmlSecBufferIOWrite (xmlSecBufferPtr buf, | |||
626 | const xmlSecByteunsigned char *data, | |||
627 | xmlSecSizesize_t size); | |||
628 | static int xmlSecBufferIOClose (xmlSecBufferPtr buf); | |||
629 | ||||
630 | /** | |||
631 | * xmlSecBufferCreateOutputBuffer: | |||
632 | * @buf: the pointer to buffer. | |||
633 | * | |||
634 | * Creates new LibXML output buffer to store data in the @buf. Caller is | |||
635 | * responsible for destroying @buf when processing is done. | |||
636 | * | |||
637 | * Returns: pointer to newly allocated output buffer or NULL if an error | |||
638 | * occurs. | |||
639 | */ | |||
640 | xmlOutputBufferPtr | |||
641 | xmlSecBufferCreateOutputBuffer(xmlSecBufferPtr buf) { | |||
642 | return(xmlOutputBufferCreateIO((xmlOutputWriteCallback)xmlSecBufferIOWrite, | |||
643 | (xmlOutputCloseCallback)xmlSecBufferIOClose, | |||
644 | buf, | |||
645 | NULL((void*)0))); | |||
646 | } | |||
647 | ||||
648 | static int | |||
649 | xmlSecBufferIOWrite(xmlSecBufferPtr buf, const xmlSecByteunsigned char *data, xmlSecSizesize_t size) { | |||
650 | int ret; | |||
651 | ||||
652 | xmlSecAssert2(buf != NULL, -1)if(!( buf != ((void*)0) ) ) { xmlSecError("buffer.c",652,__FUNCTION__ , ((void*)0), "buf != NULL", 100, " "); return(-1); }; | |||
653 | xmlSecAssert2(data != NULL, -1)if(!( data != ((void*)0) ) ) { xmlSecError("buffer.c",653,__FUNCTION__ , ((void*)0), "data != NULL", 100, " "); return(-1); }; | |||
654 | ||||
655 | ret = xmlSecBufferAppend(buf, data, size); | |||
656 | if(ret < 0) { | |||
657 | xmlSecError(XMLSEC_ERRORS_HERE"buffer.c",657,__FUNCTION__, | |||
658 | NULL((void*)0), | |||
659 | "xmlSecBufferAppend", | |||
660 | XMLSEC_ERRORS_R_XMLSEC_FAILED1, | |||
661 | "size=%d", size); | |||
662 | return(-1); | |||
663 | } | |||
664 | ||||
665 | return(size); | |||
666 | } | |||
667 | ||||
668 | static int | |||
669 | xmlSecBufferIOClose(xmlSecBufferPtr buf) { | |||
670 | xmlSecAssert2(buf != NULL, -1)if(!( buf != ((void*)0) ) ) { xmlSecError("buffer.c",670,__FUNCTION__ , ((void*)0), "buf != NULL", 100, " "); return(-1); }; | |||
671 | ||||
672 | /* just do nothing */ | |||
673 | return(0); | |||
674 | } |