xmlsecurityengine/xmlseccrypto/src/xmlsecc_bio.cpp
changeset 0 e35f40988205
child 1 29dae20d06bf
equal deleted inserted replaced
-1:000000000000 0:e35f40988205
       
     1 /*
       
     2 * Copyright (c) 2005-2006 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  A wrapper of OpenSSL bio.c functions to Symbian.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 /** A wrapper of OpenSSL bio.c functions to Symbian **/
       
    20 
       
    21 #include <stdlib.h>
       
    22 #include <stdio.h>
       
    23 #include <string.h>
       
    24 #include <errno.h>
       
    25 #include <e32base.h>
       
    26 #include "xmlsec_error_flag.h"
       
    27 #include "xmlsecc_config.h"
       
    28 #include "xmlsecc_bio.h"
       
    29 
       
    30 BIO* BIO_new_buffer(const char *aData, unsigned int aSize, const char *aName)
       
    31     {
       
    32     BIO	*bio;
       
    33     char *tmpname = NULL;
       
    34     			
       
    35     char* buf = (char *)malloc(aSize + 1);
       
    36     if(!buf){	   
       
    37 	    xmlSecSetErrorFlag( KErrNoMemory );
       
    38 	    return(NULL);
       
    39 	}
       
    40 	memcpy(buf, aData, aSize);
       
    41 	
       
    42     if (aName)
       
    43 	{
       
    44 		tmpname = (char *)malloc(sizeof(char)*(strlen(aName)+1));
       
    45 		if (!tmpname)
       
    46 		{
       
    47 		    xmlSecSetErrorFlag( KErrNoMemory );
       
    48 			free(buf);
       
    49 			return (NULL);
       
    50 		}		
       
    51 		strcpy(tmpname, aName);
       
    52 	}
       
    53 
       
    54 	bio = (BIO *)malloc(sizeof(BIO));
       
    55 	if (!bio)
       
    56 	{
       
    57 		xmlSecSetErrorFlag( KErrNoMemory );
       
    58 	    free(buf);
       
    59 	    free(tmpname);
       
    60 	    return(NULL);
       
    61 	}
       
    62 	
       
    63 	bio->mem = buf;
       
    64 	bio->len = aSize;
       
    65 	bio->name = tmpname;
       
    66 	
       
    67 	return bio;
       
    68     }
       
    69 
       
    70 BIO* BIO_new_file(const char *aFilename, const char *aMode, const char *name)
       
    71 {
       
    72     BIO *bio;
       
    73 	FILE *fp;
       
    74     char *buf;
       
    75     long fileLen;
       
    76     int byteRead;
       
    77     char *tmpname = NULL;
       
    78 
       
    79     //Read file to memory
       
    80 	fp = fopen(aFilename, aMode);
       
    81 	if(!fp)
       
    82 	    {
       
    83 	    if ( errno == ENOMEM )
       
    84 	        {
       
    85 		    xmlSecSetErrorFlag( KErrNoMemory );
       
    86 	        }
       
    87 	    return(NULL);
       
    88 	    }
       
    89 	fseek(fp, 0, SEEK_END);
       
    90 	fileLen = ftell(fp);
       
    91     fseek ( fp , 0L , SEEK_SET );
       
    92 	
       
    93 	buf = (char *)malloc(sizeof(char)*(fileLen+1));
       
    94     if(!buf) {	   
       
    95 		xmlSecSetErrorFlag( KErrNoMemory );
       
    96 	    return(NULL);
       
    97 	}    
       
    98 				
       
    99 	byteRead = fread(buf, sizeof(char), fileLen, fp);
       
   100     if(byteRead != fileLen) {
       
   101 	    free(buf);
       
   102 	    return(NULL);
       
   103 	}   
       
   104 	
       
   105 	if (name)
       
   106 	{
       
   107 		tmpname = (char *)malloc(sizeof(char)*(strlen(name)+1));
       
   108 		if (!tmpname)
       
   109 		{
       
   110 		    xmlSecSetErrorFlag( KErrNoMemory );
       
   111 			free(buf);
       
   112 			return (NULL);
       
   113 		}		
       
   114 		strcpy(tmpname, name);
       
   115 	}
       
   116 
       
   117 	bio = (BIO *)malloc(sizeof(BIO));
       
   118 	if (!bio)
       
   119 	{
       
   120 		xmlSecSetErrorFlag( KErrNoMemory );
       
   121 		if (tmpname)
       
   122 		    {
       
   123 		    free(tmpname);
       
   124 		    }
       
   125 	    free(buf);
       
   126 	    return(NULL);
       
   127 	}
       
   128 	
       
   129 	bio->mem = buf;
       
   130 	bio->len = fileLen;
       
   131 	bio->name = tmpname;
       
   132 
       
   133 	return bio; 	
       
   134 
       
   135 }
       
   136 
       
   137 void BIO_free(BIO *bio)
       
   138 {
       
   139 	if (bio)
       
   140 	{
       
   141 		if (bio->mem)
       
   142 		{
       
   143 			memset(bio->mem, 0, bio->len);
       
   144 			free(bio->mem);			
       
   145 		}
       
   146 		if (bio->name)
       
   147 		{
       
   148 		    free(bio->name);        
       
   149 		}
       
   150 		free(bio);
       
   151 	}
       
   152 }
       
   153 
       
   154 BIO* BIO_new()
       
   155 {
       
   156 	BIO *bio = (BIO *)malloc(sizeof(BIO));
       
   157 	if ( !bio ) 
       
   158 	    {
       
   159 	    xmlSecSetErrorFlag( KErrNoMemory );
       
   160 	    return NULL;
       
   161 	    }
       
   162 	bio->mem = NULL;
       
   163 	bio->len = 0;
       
   164 	bio->name = NULL;
       
   165 	return bio;
       
   166 }
       
   167 
       
   168 int BIO_write(BIO *bio, const unsigned char *buf, unsigned int size)
       
   169 {
       
   170 	bio->mem = (char *)malloc(size *sizeof(char));
       
   171 	if (!bio->mem)
       
   172 	    {
       
   173 	    xmlSecSetErrorFlag( KErrNoMemory );
       
   174 	    return -1;    
       
   175 	    }
       
   176 	
       
   177 	memcpy((unsigned char*)bio->mem, buf, size);
       
   178 	bio->len = size;
       
   179 	
       
   180 	return 0;
       
   181 }