Merge branch 'master' of github.com:cyassl/cyassl

This commit is contained in:
Chris Conlon
2014-01-13 13:20:29 -07:00
2 changed files with 42 additions and 6 deletions

View File

@@ -147,15 +147,21 @@ const byte base64Encode[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
}; };
/* porting assistance from yaSSL by Raphael HUCK */ /* internal worker, handles both escaped and normal line endings */
int Base64_Encode(const byte* in, word32 inLen, byte* out, word32* outLen) static int DoBase64_Encode(const byte* in, word32 inLen, byte* out,
word32* outLen, int escaped)
{ {
word32 i = 0, word32 i = 0,
j = 0, j = 0,
n = 0; /* new line counter */ n = 0; /* new line counter */
word32 outSz = (inLen + 3 - 1) / 3 * 4; word32 outSz = (inLen + 3 - 1) / 3 * 4;
outSz += (outSz + PEM_LINE_SZ - 1) / PEM_LINE_SZ; /* new lines */ word32 addSz = (outSz + PEM_LINE_SZ - 1) / PEM_LINE_SZ; /* new lines */
if (escaped)
addSz *= 3; /* instead of just \n, we're doing %0A triplet */
outSz += addSz;
if (outSz > *outLen) return BAD_FUNC_ARG; if (outSz > *outLen) return BAD_FUNC_ARG;
@@ -178,9 +184,16 @@ int Base64_Encode(const byte* in, word32 inLen, byte* out, word32* outLen)
inLen -= 3; inLen -= 3;
if ((++n % (PEM_LINE_SZ / 4)) == 0 && inLen) if ((++n % (PEM_LINE_SZ / 4)) == 0 && inLen) {
if (escaped) {
out[i++] = '%';
out[i++] = '0';
out[i++] = 'A';
}
else
out[i++] = '\n'; out[i++] = '\n';
} }
}
/* last integral */ /* last integral */
if (inLen) { if (inLen) {
@@ -199,6 +212,12 @@ int Base64_Encode(const byte* in, word32 inLen, byte* out, word32* outLen)
out[i++] = PAD; out[i++] = PAD;
} }
if (escaped) {
out[i++] = '%';
out[i++] = '0';
out[i++] = 'A';
}
else
out[i++] = '\n'; out[i++] = '\n';
if (i != outSz) if (i != outSz)
return ASN_INPUT_E; return ASN_INPUT_E;
@@ -208,6 +227,20 @@ int Base64_Encode(const byte* in, word32 inLen, byte* out, word32* outLen)
} }
/* Base64 Encode, PEM style, with \n line endings */
int Base64_Encode(const byte* in, word32 inLen, byte* out, word32* outLen)
{
return DoBase64_Encode(in, inLen, out, outLen, 0);
}
/* Base64 Encode, with %0A esacped line endings instead of \n */
int Base64_EncodeEsc(const byte* in, word32 inLen, byte* out, word32* outLen)
{
return DoBase64_Encode(in, inLen, out, outLen, 1);
}
static static
const byte hexDecode[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, const byte hexDecode[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD,

View File

@@ -39,6 +39,9 @@ CYASSL_LOCAL int Base64_Decode(const byte* in, word32 inLen, byte* out,
CYASSL_API CYASSL_API
int Base64_Encode(const byte* in, word32 inLen, byte* out, int Base64_Encode(const byte* in, word32 inLen, byte* out,
word32* outLen); word32* outLen);
CYASSL_API
int Base64_EncodeEsc(const byte* in, word32 inLen, byte* out,
word32* outLen);
CYASSL_LOCAL CYASSL_LOCAL
int Base16_Decode(const byte* in, word32 inLen, byte* out, word32* outLen); int Base16_Decode(const byte* in, word32 inLen, byte* out, word32* outLen);
#endif #endif