Skip to content

Commit

Permalink
Check padding char
Browse files Browse the repository at this point in the history
  • Loading branch information
ifduyue committed Dec 15, 2015
1 parent 3c0c17f commit 1e68267
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from setuptools import setup, Extension
import os

VERSION = "1.0.1"
VERSION = "1.0.2"

if os.name == 'posix':
extra_compile_args = [
Expand Down
30 changes: 21 additions & 9 deletions xxtea.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ static int bytes2longs(const char *in, int inlen, uint32_t *out, int padding)

static int longs2bytes(uint32_t *in, int inlen, char *out, int padding)
{
int i, pad = 0;
int i, outlen, pad;
unsigned char *s;

s = (unsigned char *)out;
Expand All @@ -141,21 +141,33 @@ static int longs2bytes(uint32_t *in, int inlen, char *out, int padding)
s[4 * i + 3] = (in[i] >> 24) & 0xFF;
}

i <<= 2;
outlen = inlen * 4;

/* PKCS#7 unpadding */
if (padding) {
pad = s[i - 1];
i -= pad;
}
pad = s[outlen - 1];
outlen -= pad;

if (pad < 1 || pad > 8) {
/* invalid padding */
return -1;
}

if (outlen < 0) {
return -2;
}

if (i >= 0 && i <= inlen << 2) {
s[i] = '\0';
for (i = outlen; i < inlen * 4; i++) {
if (s[i] != pad) {
return -3;
}
}
}

s[outlen] = '\0';

/* How many bytes we've got */
/* Negative means errors */
return pad < 0 ? pad : i;
return outlen;
}

/*****************************************************************************
Expand Down

0 comments on commit 1e68267

Please sign in to comment.