Skip to content

Commit

Permalink
Add base64 matcher functions
Browse files Browse the repository at this point in the history
  • Loading branch information
leecam committed Nov 20, 2024
1 parent e517e25 commit eb9c4e1
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
48 changes: 48 additions & 0 deletions matcher/base64.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

static int B64Lookup(char x) {
if (x >= 0x41 && x <= 0x5a) {
return x-0x41;
} else if (x >= 0x61 && x <= 0x7a) {
return x-0x61+26;
} else if (x >= 0x30 && x <= 0x39) {
return x-0x30+52;
} else if (x == 0x2d) {
return 62;
} else if (x == 0x5f) {
return 63;
} else {
return 0;
}
}

int B64DecodeURL(char* input, char** output) {
int b64len = strlen(input);
int output_len = (b64len/4) * 3;
char* buffer = malloc(output_len);

int count = 0;
for(int i=0; i<b64len; i+=4) {
uint32_t v = 0;
for(int j=0; j<4; j++) {
v = v << 6;
v += B64Lookup(input[i+j]);
}
buffer[count++] = (v >> 16);
buffer[count++] = (v >> 8) & 0xff;
buffer[count++] = v & 0xFF;
}
*output = buffer;

if (b64len > 0 && input[b64len-1] == '=') {
output_len--;
}
if (b64len > 1 && input[b64len-2] == '=') {
output_len--;
}


return output_len;
}
6 changes: 6 additions & 0 deletions matcher/base64.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef BASE64_H
#define BASE64_H

int B64DecodeURL(char* input, char** output);

#endif

0 comments on commit eb9c4e1

Please sign in to comment.