add base64url support
This commit is contained in:
parent
9ed7f82dc8
commit
7f829a36bc
@ -1,7 +1,10 @@
|
||||
#include "array.h"
|
||||
#include "textcode.h"
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
#include <unistd.h>
|
||||
#include <write12.h>
|
||||
|
||||
array a;
|
||||
|
||||
@ -26,5 +29,11 @@ int main() {
|
||||
buf[n]=0;
|
||||
puts(buf);
|
||||
|
||||
assert(fmt_base64(buf,"fnörd",6)==8 && !memcmp(buf,"Zm7DtnJk",8));
|
||||
assert(scan_base64("Zm7DtnJk",buf,&n)==8 && n==6 && !memcmp(buf,"fnörd",6));
|
||||
assert(fmt_base64(buf,"x",1)==4 && !memcmp(buf,"eA==",4));
|
||||
assert(scan_base64("eA====",buf,&n)==4 && n==1 && buf[0]=='x');
|
||||
assert(fmt_base64url(buf,"\xef\xbf\xbdx",4)==6 && !memcmp(buf,"77-9eA",6));
|
||||
assert(scan_base64url("77-9eA",buf,&n)==6 && n==4 && !memcmp(buf,"\xef\xbf\xbdx",4));
|
||||
return 0;
|
||||
}
|
||||
|
1
textcode/base64url.c
Normal file
1
textcode/base64url.c
Normal file
@ -0,0 +1 @@
|
||||
const char base64url[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=";
|
24
textcode/fmt_base64url.c
Normal file
24
textcode/fmt_base64url.c
Normal file
@ -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;
|
||||
}
|
@ -19,7 +19,7 @@ size_t scan_base64(const char *src,char *dest,size_t *destlen) {
|
||||
for (;;) {
|
||||
int a=dec(*s);
|
||||
if (a<0) {
|
||||
while (*s=='=') ++s;
|
||||
while (*s=='=' && ((s-(const unsigned char*)src)&3)) ++s;
|
||||
break;
|
||||
}
|
||||
tmp=(tmp<<6)|a; bits+=6;
|
||||
|
31
textcode/scan_base64url.c
Normal file
31
textcode/scan_base64url.c
Normal file
@ -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…
x
Reference in New Issue
Block a user