Skip to content

Commit

Permalink
refactor(deps): Use only base64 routines under the BSD license
Browse files Browse the repository at this point in the history
The code from stackoverflow was (by default) under CC-BY-SA license.
This has raised some unclear points concerning copyleft.
  • Loading branch information
jpfr committed Aug 2, 2024
1 parent bd2e96f commit 8d7fabc
Showing 1 changed file with 61 additions and 55 deletions.
116 changes: 61 additions & 55 deletions deps/base64.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
/*
* Base64 encoding: Copyright (c) 2005-2011, Jouni Malinen <j@w1.fi>
* This software may be distributed under the terms of the BSD license.
* Base64 encoding/decoding (RFC1341)
* Copyright (c) 2005-2011, Jouni Malinen <j@w1.fi>
*
* Base64 decoding: Copyright (c) 2016, polfosol
* Posted at https://stackoverflow.com/a/37109258 under the CC-BY-SA Creative
* Commons license.
* This software may be distributed under the terms of the BSD license.
* See README for more details.
*/

#include "base64.h"
Expand Down Expand Up @@ -55,61 +54,68 @@ UA_base64(const unsigned char *src, size_t len, size_t *out_len) {
return out;
}

static const uint32_t from_b64[256] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62, 63, 62, 62, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0, 0, 0, 0, 0, 0,
0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0, 0, 0, 0, 63,
0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51};
static unsigned char dtable[256] = {
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 62 , 0x80, 62 , 0x80, 63 ,
52 , 53 , 54 , 55 , 56 , 57 , 58 , 59 , 60 , 61 , 0x80, 0x80, 0x80, 0, 0x80, 0x80,
0x80, 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 ,
15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 , 23 , 24 , 25 , 0x80, 0x80, 0x80, 0x80, 63 ,
0x80, 26 , 27 , 28 , 29 , 30 , 31 , 32 , 33 , 34 , 35 , 36 , 37 , 38 , 39 , 40 ,
41 , 42 , 43 , 44 , 45 , 46 , 47 , 48 , 49 , 50 , 51 , 0x80, 0x80, 0x80, 0x80, 0x80,
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80
};

unsigned char *
UA_unbase64(const unsigned char *src, size_t len, size_t *out_len) {
// we need a minimum length
if(len <= 2) {
*out_len = 0;
return (unsigned char*)UA_EMPTY_ARRAY_SENTINEL;
}

const unsigned char *p = src;
size_t pad1 = len % 4 || p[len - 1] == '=';
size_t pad2 = pad1 && (len % 4 > 2 || p[len - 2] != '=');
const size_t last = (len - pad1) / 4 << 2;
if(len == 0)
return NULL;

unsigned char *str = (unsigned char*)UA_malloc(last / 4 * 3 + pad1 + pad2);
if(!str)
return NULL;
unsigned char *out, *pos;
size_t olen = len / 4 * 3;
pos = out = (unsigned char*)UA_malloc(olen);
if(!out)
return NULL;

unsigned char *pos = str;
for(size_t i = 0; i < last; i += 4) {
uint32_t n = from_b64[p[i]] << 18 | from_b64[p[i + 1]] << 12 |
from_b64[p[i + 2]] << 6 | from_b64[p[i + 3]];
*pos++ = (unsigned char)(n >> 16);
*pos++ = (unsigned char)(n >> 8 & 0xFF);
*pos++ = (unsigned char)(n & 0xFF);
}
int pad = 0;
size_t count = 0;
unsigned char block[4];
for(size_t i = 0; i < len; i++) {
unsigned char tmp = dtable[src[i]];
if(tmp == 0x80)
continue;

if(pad1) {
if (last + 1 >= len) {
UA_free(str);
*out_len = 0;
return (unsigned char*)UA_EMPTY_ARRAY_SENTINEL;
}
uint32_t n = from_b64[p[last]] << 18 | from_b64[p[last + 1]] << 12;
*pos++ = (unsigned char)(n >> 16);
if(pad2) {
if (last + 2 >= len) {
UA_free(str);
*out_len = 0;
return (unsigned char*)UA_EMPTY_ARRAY_SENTINEL;
}
n |= from_b64[p[last + 2]] << 6;
*pos++ = (unsigned char)(n >> 8 & 0xFF);
}
}
if(src[i] == '=')
pad++;
block[count] = tmp;
count++;
if(count == 4) {
*pos++ = (block[0] << 2) | (block[1] >> 4);
*pos++ = (block[1] << 4) | (block[2] >> 2);
*pos++ = (block[2] << 6) | block[3];
count = 0;
if(pad) {
if(pad == 1)
pos--;
else if(pad == 2)
pos -= 2;
else {
/* Invalid padding */
UA_free(out);
return NULL;
}
break;
}
}
}

*out_len = (uintptr_t)(pos - str);
return str;
*out_len = (size_t)(pos - out);
return out;
}

0 comments on commit 8d7fabc

Please sign in to comment.