SCLogDebug("line: '%s'", line);
uint8_t decoded[strlen(line)];
- uint32_t len = DecodeBase64(decoded, (const uint8_t *)line, strlen(line), 1);
+ uint32_t len =
+ DecodeBase64(decoded, (const uint8_t *)line, strlen(line), BASE64_MODE_STRICT);
if (len == 0)
FatalError(SC_ERR_FATAL, "bad base64 encoding %s/%s",
set->name, set->load);
*r = '\0';
uint8_t decoded[strlen(line)];
- uint32_t len = DecodeBase64(decoded, (const uint8_t *)line, strlen(line), 1);
+ uint32_t len =
+ DecodeBase64(decoded, (const uint8_t *)line, strlen(line), BASE64_MODE_STRICT);
if (len == 0)
FatalError(SC_ERR_FATAL, "bad base64 encoding %s/%s",
set->name, set->load);
switch (set->type) {
case DATASET_TYPE_STRING: {
uint8_t decoded[strlen(string)];
- uint32_t len = DecodeBase64(decoded, (const uint8_t *)string, strlen(string), 1);
+ uint32_t len = DecodeBase64(
+ decoded, (const uint8_t *)string, strlen(string), BASE64_MODE_STRICT);
if (len == 0) {
return -2;
}
switch (set->type) {
case DATASET_TYPE_STRING: {
uint8_t decoded[strlen(string)];
- uint32_t len = DecodeBase64(decoded, (const uint8_t *)string, strlen(string), 1);
+ uint32_t len = DecodeBase64(
+ decoded, (const uint8_t *)string, strlen(string), BASE64_MODE_STRICT);
if (len == 0) {
return -2;
}
PrintRawDataFp(stdout, payload, decode_len);
#endif
- det_ctx->base64_decoded_len = DecodeBase64(det_ctx->base64_decoded,
- payload, decode_len, 0);
+ det_ctx->base64_decoded_len =
+ DecodeBase64(det_ctx->base64_decoded, payload, decode_len, BASE64_MODE_RELAX);
SCLogDebug("Decoded %d bytes from base64 data.",
det_ctx->base64_decoded_len);
#if 0
ThreadMacrosRegisterTests();
UtilSpmSearchRegistertests();
UtilActionRegisterTests();
+ Base64RegisterTests();
SCClassConfRegisterTests();
SCThresholdConfRegisterTests();
SCRConfRegisterTests();
*/
#include "util-base64.h"
-
+#include "util-unittest.h"
/* Constants */
#define BASE64_TABLE_MAX 122
*
* \return Number of bytes decoded, or 0 if no data is decoded or it fails
*/
-uint32_t DecodeBase64(uint8_t *dest, const uint8_t *src, uint32_t len,
- int strict)
+uint32_t DecodeBase64(uint8_t *dest, const uint8_t *src, uint32_t len, Base64Mode mode)
{
int val;
uint32_t padding = 0, numDecoded = 0, bbidx = 0, valid = 1, i;
/* Get decimal representation */
val = GetBase64Value(src[i]);
if (val < 0) {
-
+ if (mode == BASE64_MODE_RFC2045 && src[i] == ' ') {
+ continue;
+ }
/* Invalid character found, so decoding fails */
if (src[i] != '=') {
valid = 0;
- if (strict) {
+ if (mode != BASE64_MODE_RELAX) {
numDecoded = 0;
}
break;
return numDecoded;
}
+
+#ifdef UNITTESTS
+
+static int DecodeString(void)
+{
+ /*
+ * SGV sbG8= : Hello
+ * SGVsbG8gV29ybGQ= : Hello World
+ * */
+
+ const char *src = "SGVs bG8 gV29y bGQ=";
+ uint8_t *dst = SCMalloc(sizeof(src) * 30);
+ int res = DecodeBase64(dst, (const uint8_t *)src, 30, 1);
+ printf("%d\n", res);
+ printf("dst str = \"%s\"", (const char *)dst);
+ FAIL_IF(res <= 0);
+ SCFree(dst);
+ PASS;
+}
+
+void Base64RegisterTests(void)
+{
+ UtRegisterTest("DecodeString", DecodeString);
+}
+#endif
#define ASCII_BLOCK 3
#define B64_BLOCK 4
+typedef enum {
+ BASE64_MODE_RELAX,
+ BASE64_MODE_RFC2045, /* SPs are allowed during transfer but must be skipped by Decoder */
+ BASE64_MODE_STRICT,
+} Base64Mode;
+
/* Function prototypes */
-uint32_t DecodeBase64(uint8_t *dest, const uint8_t *src, uint32_t len,
- int strict);
+uint32_t DecodeBase64(uint8_t *dest, const uint8_t *src, uint32_t len, Base64Mode mode);
+
+#endif
+#ifdef UNITTESTS
+void Base64RegisterTests(void);
#endif
/* Only decode if divisible by 4 */
if (state->bvr_len == B64_BLOCK || force) {
- remdec = DecodeBase64(state->data_chunk + state->data_chunk_len,
- state->bvremain, state->bvr_len, 1);
+ remdec = DecodeBase64(state->data_chunk + state->data_chunk_len, state->bvremain,
+ state->bvr_len, BASE64_MODE_RFC2045);
if (remdec > 0) {
/* Track decoded length */
SCLogDebug("Decoding: %u", len - rem1 - rem2);
- numDecoded = DecodeBase64(state->data_chunk + state->data_chunk_len,
- buf + offset, tobuf, 1);
+ numDecoded = DecodeBase64(state->data_chunk + state->data_chunk_len, buf + offset,
+ tobuf, BASE64_MODE_RFC2045);
if (numDecoded > 0) {
/* Track decoded length */
if (dst == NULL)
return 0;
- ret = DecodeBase64(dst, (const uint8_t *)base64msg, strlen(base64msg), 1);
+ ret = DecodeBase64(dst, (const uint8_t *)base64msg, strlen(base64msg), BASE64_MODE_RFC2045);
if (memcmp(dst, msg, strlen(msg)) == 0) {
ret = 1;