add base64url support
parent
9ed7f82dc8
commit
7f829a36bc
@ -0,0 +1 @@
|
||||
const char base64url[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=";
|
@ -0,0 +1,24 @@
|
||||
#include "fmt.h"
|
||||
#include "textcode.h"
|
||||
#include "haveinline.h"
|
||||
|
||||
size_t fmt_base64url(char* dest,const char* src,size_t len) {
|
||||
register const unsigned char* s=(const unsigned char*) src;
|
||||
unsigned short bits=0,temp=0;
|
||||
size_t written=0,i;
|
||||
if (!dest) return (len>((size_t)-1)/2)?(size_t)-1:((len+2)/3)*4;
|
||||
for (i=0; i<len; ++i) {
|
||||
temp<<=8; temp+=s[i]; bits+=8;
|
||||
while (bits>6) {
|
||||
dest[written]=base64url[((temp>>(bits-6))&63)];
|
||||
++written; bits-=6;
|
||||
}
|
||||
}
|
||||
if (bits) {
|
||||
temp<<=(6-bits);
|
||||
dest[written]=base64url[temp&63];
|
||||
++written;
|
||||
}
|
||||
/* no padding for base64url */
|
||||
return written;
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
#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;
|
||||
}
|
||||
}
|
||||
|
||||
size_t scan_base64url(const char *src,char *dest,size_t *destlen) {
|
||||
unsigned short tmp=0,bits=0;
|
||||
register const unsigned char* s=(const unsigned char*) src;
|
||||
const char* orig=dest;
|
||||
for (;;) {
|
||||
int a=dec(*s);
|
||||
if (a<0) break;
|
||||
tmp=(tmp<<6)|a; bits+=6;
|
||||
++s;
|
||||
if (bits>=8) {
|
||||
*dest=(tmp>>(bits-=8));
|
||||
++dest;
|
||||
}
|
||||
}
|
||||
*destlen=dest-orig;
|
||||
return (const char*)s-src;
|
||||
}
|
Loading…
Reference in New Issue