-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |