2002-04-30 17:55:41 +00:00
|
|
|
#include "textcode.h"
|
|
|
|
#include "haveinline.h"
|
|
|
|
|
|
|
|
static inline int dec(unsigned char x) {
|
|
|
|
if (x>='A' && x<='Z') return x-'A';
|
|
|
|
if (x>='a' && x<='z') return x-'a'+26;
|
|
|
|
if (x>='0' && x<='9') return x-'0'+26+26;
|
|
|
|
switch (x) {
|
|
|
|
case '+': return 62;
|
|
|
|
case '/': return 63;
|
|
|
|
default: return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-11-07 17:56:05 +00:00
|
|
|
size_t scan_base64(const char *src,char *dest,size_t *destlen) {
|
2002-04-30 17:55:41 +00:00
|
|
|
unsigned short tmp=0,bits=0;
|
|
|
|
register const unsigned char* s=(const unsigned char*) src;
|
2017-03-13 17:50:19 +00:00
|
|
|
const char* orig=src;
|
|
|
|
size_t i,j=0;
|
|
|
|
for (i=0;;) {
|
2002-04-30 17:55:41 +00:00
|
|
|
int a=dec(*s);
|
|
|
|
if (a<0) {
|
2017-03-13 17:50:19 +00:00
|
|
|
int equal=(*s=='=');
|
2016-02-28 12:24:47 +00:00
|
|
|
while (*s=='=' && ((s-(const unsigned char*)src)&3)) ++s;
|
2017-03-13 17:50:19 +00:00
|
|
|
tmp &= ((1<<bits)-1);
|
2017-03-15 10:36:02 +00:00
|
|
|
if (!tmp || equal) { j=i; orig=(const char*)s; }
|
2002-04-30 17:55:41 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
tmp=(tmp<<6)|a; bits+=6;
|
|
|
|
++s;
|
|
|
|
if (bits>=8) {
|
2017-03-13 17:50:19 +00:00
|
|
|
bits-=8;
|
|
|
|
if (dest) dest[i]=(tmp>>bits);
|
|
|
|
++i;
|
2017-03-15 10:36:02 +00:00
|
|
|
if (!bits) { j=i; orig=(const char*)s; }
|
2002-04-30 17:55:41 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-13 17:50:19 +00:00
|
|
|
if (destlen) *destlen=j;
|
|
|
|
return orig-src;
|
2002-04-30 17:55:41 +00:00
|
|
|
}
|
2017-03-13 17:50:19 +00:00
|
|
|
|
|
|
|
#ifdef UNITTEST
|
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
char buf[100];
|
2020-10-30 15:06:17 +00:00
|
|
|
size_t l;
|
2017-03-13 17:50:19 +00:00
|
|
|
memset(buf,0,10); assert(scan_base64("Zm5vcmQ=",buf,&l)==8 && l==5 && !memcmp(buf,"fnord",6));
|
2017-07-30 13:41:43 +00:00
|
|
|
/* check that we don't insist on the padding */
|
2017-03-13 17:50:19 +00:00
|
|
|
memset(buf,0,10); assert(scan_base64("Zm5vcmQ",buf,&l)==7 && l==5 && !memcmp(buf,"fnord",6));
|
2017-07-30 13:41:43 +00:00
|
|
|
/* check the special non-isalnum chars :) */
|
|
|
|
memset(buf,0,10); assert(scan_base64("/+8=",buf,&l)==4 && l==2 && !memcmp(buf,"\xff\xef",3));
|
2017-05-13 22:52:21 +00:00
|
|
|
return 0;
|
2017-03-13 17:50:19 +00:00
|
|
|
}
|
|
|
|
#endif
|