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