Skip to content

Commit

Permalink
b64_decode: Drop unused code
Browse files Browse the repository at this point in the history
Signed-off-by: Jianhui Zhao <jianhuizhao329@gmail.com>
  • Loading branch information
Jianhui Zhao committed May 8, 2019
1 parent db5f7d9 commit 94a059d
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 139 deletions.
141 changes: 4 additions & 137 deletions src/base64.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@
#include <string.h>
#include <stdint.h>

static const char Base64[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

static const char Pad64 = '=';

/* (From RFC1521 and draft-ietf-dnssec-secext-03.txt)
Expand Down Expand Up @@ -98,6 +97,9 @@ static const char Pad64 = '=';
int b64_encode(const void *_src, size_t srclength,
void *dest, size_t targsize)
{
char Base64[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

const unsigned char *src = _src;
char *target = dest;
size_t datalength = 0;
Expand Down Expand Up @@ -151,138 +153,3 @@ int b64_encode(const void *_src, size_t srclength,
return (datalength);
}

/* skips all whitespace anywhere.
converts characters, four at a time, starting at (or after)
src from base - 64 numbers into three 8 bit bytes in the target area.
it returns the number of data bytes stored at the target, or -1 on error.
*/

int b64_decode(const void *_src, void *dest, size_t targsize)
{
const char *src = _src;
unsigned char *target = dest;
int tarindex, state, ch;
uint8_t nextbyte;
char *pos;

state = 0;
tarindex = 0;

while ((ch = (unsigned char)*src++) != '\0') {
if (isspace(ch)) /* Skip whitespace anywhere. */
continue;

if (ch == Pad64)
break;

pos = strchr(Base64, ch);
if (pos == 0) /* A non-base64 character. */
return (-1);

switch (state) {
case 0:
if (target) {
if (tarindex >= targsize)
return (-1);
target[tarindex] = (pos - Base64) << 2;
}
state = 1;
break;
case 1:
if (target) {
if (tarindex >= targsize)
return (-1);
target[tarindex] |= (pos - Base64) >> 4;
nextbyte = ((pos - Base64) & 0x0f) << 4;
if (tarindex + 1 < targsize)
target[tarindex+1] = nextbyte;
else if (nextbyte)
return (-1);
}
tarindex++;
state = 2;
break;
case 2:
if (target) {
if (tarindex >= targsize)
return (-1);
target[tarindex] |= (pos - Base64) >> 2;
nextbyte = ((pos - Base64) & 0x03) << 6;
if (tarindex + 1 < targsize)
target[tarindex+1] = nextbyte;
else if (nextbyte)
return (-1);
}
tarindex++;
state = 3;
break;
case 3:
if (target) {
if (tarindex >= targsize)
return (-1);
target[tarindex] |= (pos - Base64);
}
tarindex++;
state = 0;
break;
}
}

/*
* We are done decoding Base-64 chars. Let's see if we ended
* on a byte boundary, and/or with erroneous trailing characters.
*/

if (ch == Pad64) { /* We got a pad char. */
ch = (unsigned char)*src++; /* Skip it, get next. */
switch (state) {
case 0: /* Invalid = in first position */
case 1: /* Invalid = in second position */
return (-1);

case 2: /* Valid, means one byte of info */
/* Skip any number of spaces. */
for (; ch != '\0'; ch = (unsigned char)*src++)
if (!isspace(ch))
break;
/* Make sure there is another trailing = sign. */
if (ch != Pad64)
return (-1);
ch = (unsigned char)*src++; /* Skip the = */
/* Fall through to "single trailing =" case. */
/* FALLTHROUGH */

case 3: /* Valid, means two bytes of info */
/*
* We know this char is an =. Is there anything but
* whitespace after it?
*/
for (; ch != '\0'; ch = (unsigned char)*src++)
if (!isspace(ch))
return (-1);

/*
* Now make sure for cases 2 and 3 that the "extra"
* bits that slopped past the last full byte were
* zeros. If we don't check them, they become a
* subliminal channel.
*/
if (target && tarindex < targsize &&
target[tarindex] != 0)
return (-1);
}
} else {
/*
* We ended by seeing the end of the string. Make sure we
* have no partial bytes lying around.
*/
if (state != 0)
return (-1);
}

/* Null-terminate if we have room left */
if (tarindex < targsize)
target[tarindex] = 0;

return (tarindex);
}
2 changes: 0 additions & 2 deletions src/base64.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,4 @@
int b64_encode(const void *_src, size_t srclength,
void *dest, size_t targsize);

int b64_decode(const void *_src, void *dest, size_t targsize);

#endif

0 comments on commit 94a059d

Please sign in to comment.