Mixe for Privacy and Anonymity in the Internet
CABase64.cpp
Go to the documentation of this file.
00001 /*
00002 Copyright (c) 2000, The JAP-Team
00004 Redistribution and use in source and binary forms, with or without modification,
00005 are permitted provided that the following conditions are met:
00006
00007 - Redistributions of source code must retain the above copyright notice,
00008 this list of conditions and the following disclaimer.
00009
00010 - Redistributions in binary form must reproduce the above copyright notice,
00011 this list of conditions and the following disclaimer in the documentation and/or
00012 other materials provided with the distribution.
00013
00014 - Neither the name of the University of Technology Dresden, Germany nor the names of its contributors
00015 may be used to endorse or promote products derived from this software without specific
00016 prior written permission.
00017
00018
00019 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS'' AND ANY EXPRESS
00020 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
00021 AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS
00022 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00023 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00024 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
00025 IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
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   //ensure that in and out are disjunct - otherwise copy in
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   //ensure that in and out are disjunct - otherwise copy in
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 }