Created
April 3, 2018 18:06
-
-
Save datenwolf/2b8ce5a893eff5514212064dce8ad59e to your computer and use it in GitHub Desktop.
Modified base64 variant that is order preserving
This file contains hidden or 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
/* | |
Sorting order preserving base64 implementation. | |
Based on the original implementation of | |
picoweb / litheweb -- a web server and application framework | |
for resource constraint systems. | |
Copyright (C) 2012 - 2018 Wolfgang Draxinger | |
This program is free software; you can redistribute it and/or modify | |
it under the terms of the GNU General Public License as published by | |
the Free Software Foundation; either version 2 of the License, or | |
(at your option) any later version. | |
This program is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
GNU General Public License for more details. | |
You should have received a copy of the GNU General Public License | |
along with this program; if not, write to the Free Software | |
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
*/ | |
#include <stddef.h> | |
#include <stdint.h> | |
typedef uint8_t b64raw_t[3]; | |
typedef char b64enc_t[4]; | |
typedef uint32_t b64state_t; | |
void b64op_encode( | |
b64raw_t const raw, | |
size_t count, | |
b64enc_t enc) | |
{ | |
enc[1] = 0; | |
enc[3] = 0xff; | |
enc[2] = (count > 1) ? 0 : 0xff; | |
switch(count) { | |
default: | |
return; | |
case 3: | |
enc[3] = ((raw[2] & 0x3f)); | |
enc[2] = ((raw[2] & 0xc0) >> 6); | |
case 2: | |
enc[2] |= ((raw[1] & 0x0f) << 2); | |
enc[1] = ((raw[1] & 0xf0) >> 4); | |
case 1: | |
enc[1] |= ((raw[0] & 0x03) << 4); | |
enc[0] = ((raw[0] & 0xfc) >> 2); | |
} | |
for(int i = 0; i < 4; i++) { | |
switch( enc[i] ){ | |
case 0: enc[i] = '+'; break; | |
case 1: enc[i] = '='; break; | |
default: | |
if( 12 > enc[i] ){ | |
enc[i] = (enc[i]-2) + '0'; | |
} else | |
if( 38 > enc[i] ) { | |
enc[i] = (enc[i]-12) + 'A'; | |
} else | |
if( 64 > enc[i] ) { | |
enc[i] = (enc[i]-38) + 'a'; | |
} else { | |
enc[i] = '~'; | |
} | |
break; | |
} | |
} | |
} | |
size_t b64opdecode( | |
b64enc_t const enc, | |
b64raw_t raw) | |
{ | |
size_t count = 3; | |
b64enc_t v; | |
for(int i = 0; i < 4; i++) { | |
switch( enc[i] ){ | |
case '+': v[i] = 0; break; | |
case '=': v[i] = 1; break; | |
default: | |
if( '0' <= enc[i] && '9' >= enc[i] ) { | |
v[i] = enc[i] - '0' + 2; | |
} else | |
if( 'A' <= enc[i] && 'Z' >= enc[i] ) { | |
v[i] = enc[i] - 'A' + 12; | |
} else | |
if( 'a' <= enc[i] && 'z' >= enc[i] ) { | |
v[i] = enc[i] - 'a' + 38; | |
} else { | |
case '~': | |
v[i] = 0x3f; | |
count--; | |
} | |
} | |
} | |
raw[0] = ((v[0] & 0x3f) << 2) | ((v[1] & 0x30) >> 4); | |
raw[1] = ((v[1] & 0x0f) << 4) | ((v[2] & 0x3c) >> 2); | |
raw[2] = ((v[2] & 0x03) << 6) | ( v[3] & 0x3f ); | |
return count; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment