Mixe for Privacy and Anonymity in the Internet
CABase64.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2000, The JAP-Team
3  All rights reserved.
4  Redistribution and use in source and binary forms, with or without modification,
5  are permitted provided that the following conditions are met:
6 
7  - Redistributions of source code must retain the above copyright notice,
8  this list of conditions and the following disclaimer.
9 
10  - Redistributions in binary form must reproduce the above copyright notice,
11  this list of conditions and the following disclaimer in the documentation and/or
12  other materials provided with the distribution.
13 
14  - Neither the name of the University of Technology Dresden, Germany nor the names of its contributors
15  may be used to endorse or promote products derived from this software without specific
16  prior written permission.
17 
18 
19  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS
20  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
21  AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS
22  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24  OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
27  */
28 #include "StdAfx.h"
29 #include "CABase64.hpp"
30 
41 SINT32 CABase64::decode(const UINT8*in, UINT32 inlen, UINT8*out, UINT32*outlen)
42  {
43  if (outlen == NULL)
44  return E_UNKNOWN;
45  if (in == NULL || inlen == 0)
46  {
47  *outlen = 0;
48  return E_SUCCESS;
49  }
50  EVP_ENCODE_CTX* pCTX = NULL;
51 #if OPENSSL_VERSION_NUMBER > 0x100020cfL
52  pCTX=EVP_ENCODE_CTX_new();
53 #else
54  pCTX = new EVP_ENCODE_CTX;
55 #endif
56  EVP_DecodeInit(pCTX);
57  int len = 0;
58  *outlen = 0;
59  SINT32 ret = -1;
60  //ensure that in and out are disjunct - otherwise copy in
61  if (((out >= in) && (in + inlen > out)) || ((out < in) && (out + *outlen > in)))
62  {
63  UINT8*tmpIn = new UINT8[inlen];
64  memcpy(tmpIn, in, inlen);
65  ret = EVP_DecodeUpdate(pCTX, out, (int*) outlen, tmpIn, (int) inlen);
66  delete[] tmpIn;
67  tmpIn = NULL;
68  }
69  else
70  {
71  ret = EVP_DecodeUpdate(pCTX, out, (int*) outlen, (unsigned char*) in, (int) inlen);
72  }
73  if (ret < 0 || EVP_DecodeFinal(pCTX, out + (*outlen), &len) < 0)
74  {
75  ret=E_UNKNOWN;
76  }
77  else
78  {
79  (*outlen) += len;
80  ret = E_SUCCESS;
81  }
82 #if OPENSSL_VERSION_NUMBER > 0x100020cfL
83  EVP_ENCODE_CTX_free(pCTX);
84 #else
85  delete pCTX;
86 #endif
87 
88  return ret;
89  }
90 
102 SINT32 CABase64::encode(const UINT8*in, UINT32 inlen, UINT8*out, UINT32*outlen)
103  {
104  if (outlen == NULL)
105  return E_UNKNOWN;
106  if (in == NULL || inlen == 0)
107  {
108  *outlen = 0;
109  return E_SUCCESS;
110  }
111  if (inlen > *outlen)
112  return E_UNKNOWN;
113  EVP_ENCODE_CTX* pCTX = NULL;
114 #if OPENSSL_VERSION_NUMBER > 0x100020cfL
115  pCTX=EVP_ENCODE_CTX_new();
116 #else
117  pCTX = new EVP_ENCODE_CTX;
118 #endif
119  EVP_EncodeInit(pCTX);
120  UINT32 len = 0;
121  *outlen = 0;
122 
123  //ensure that in and out are disjunct - otherwise copy in
124  if (((out >= in) && (in + inlen > out)) || ((out < in) && (out + *outlen > in)))
125  {
126  UINT8*tmpIn = new UINT8[inlen];
127  memcpy(tmpIn, in, inlen);
128  EVP_EncodeUpdate(pCTX, out, (int*) outlen, tmpIn, (int) inlen);
129  delete[] tmpIn;
130  tmpIn = NULL;
131  }
132  else
133  {
134  EVP_EncodeUpdate(pCTX, out, (int*) outlen, (unsigned char*) in, (int) inlen);
135  }
136  EVP_EncodeFinal(pCTX, out + (*outlen), (int*) &len);
137  (*outlen) += len;
138 #if OPENSSL_VERSION_NUMBER > 0x100020cfL
139  EVP_ENCODE_CTX_free(pCTX);
140 #else
141  delete pCTX;
142 #endif
143  return E_SUCCESS;
144  }
signed int SINT32
Definition: basetypedefs.h:132
unsigned char UINT8
Definition: basetypedefs.h:135
unsigned int UINT32
Definition: basetypedefs.h:131
static SINT32 encode(const UINT8 *in, UINT32 len, UINT8 *out, UINT32 *outlen)
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff...
Definition: CABase64.cpp:102
static SINT32 decode(const UINT8 *in, UINT32 len, UINT8 *out, UINT32 *outlen)
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff...
Definition: CABase64.cpp:41
const SINT32 E_SUCCESS
Definition: errorcodes.hpp:2
#define E_UNKNOWN
Definition: errorcodes.hpp:3
UINT16 len
Definition: typedefs.hpp:0