00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include "StdAfx.h"
00029 #include "CABase64.hpp"
00030
00041 SINT32 CABase64::decode(const UINT8*in, UINT32 inlen, UINT8*out, UINT32*outlen)
00042 {
00043 if(outlen==NULL)
00044 return E_UNKNOWN;
00045 if(in==NULL||inlen==0)
00046 {
00047 *outlen=0;
00048 return E_SUCCESS;
00049 }
00050 EVP_ENCODE_CTX oCTX;
00051 EVP_DecodeInit(&oCTX);
00052 int len=0;
00053 *outlen=0;
00054 SINT32 ret=-1;
00055
00056 if(((out>=in)&&(in+inlen>out))||((out<in)&&(out+*outlen>in)))
00057 {
00058 UINT8*tmpIn=new UINT8[inlen];
00059 memcpy(tmpIn, in, inlen);
00060 ret=EVP_DecodeUpdate(&oCTX, out, (int*) outlen, tmpIn, (int)inlen);
00061 delete[] tmpIn;
00062 tmpIn = NULL;
00063 }
00064 else
00065 {
00066 ret=EVP_DecodeUpdate(&oCTX, out, (int*) outlen, (unsigned char*) in, (int)inlen);
00067 }
00068 if(ret<0||EVP_DecodeFinal(&oCTX, out+(*outlen), &len)<0)
00069 {
00070 return E_UNKNOWN;
00071 }
00072 (*outlen)+=len;
00073 return E_SUCCESS;
00074 }
00075
00087 SINT32 CABase64::encode(const UINT8*in, UINT32 inlen, UINT8*out, UINT32*outlen)
00088 {
00089 if(outlen==NULL)
00090 return E_UNKNOWN;
00091 if(in==NULL||inlen==0)
00092 {
00093 *outlen=0;
00094 return E_SUCCESS;
00095 }
00096 if(inlen>*outlen)
00097 return E_UNKNOWN;
00098
00099 EVP_ENCODE_CTX oCTX;
00100 EVP_EncodeInit(&oCTX);
00101 UINT32 len=0;
00102 *outlen=0;
00103
00104
00105 if(((out>=in)&&(in+inlen>out))||((out<in)&&(out+*outlen>in)))
00106 {
00107 UINT8*tmpIn=new UINT8[inlen];
00108 memcpy(tmpIn, in, inlen);
00109 EVP_EncodeUpdate(&oCTX, out, (int*) outlen, tmpIn, (int)inlen);
00110 delete[] tmpIn;
00111 tmpIn = NULL;
00112 }
00113 else
00114 {
00115 EVP_EncodeUpdate(&oCTX, out, (int*) outlen, (unsigned char*) in, (int)inlen);
00116 }
00117 EVP_EncodeFinal(&oCTX, out+(*outlen), (int*)&len);
00118 (*outlen)+=len;
00119 return E_SUCCESS;
00120 }