mirror of /home/gitosis/repositories/libowfat.git
write generic stralloc and array textcode wrapper functions change textcode API to use long instead of int add cescape fmt and scan functions to textcode add fmt_foldwhitespace to textcodemaster
parent
2d27cd435b
commit
640edc1651
40 changed files with 218 additions and 477 deletions
@ -0,0 +1,6 @@ |
||||
#include "fmt.h" |
||||
|
||||
char fmt_tohex(char c) { |
||||
return c>=10?c-10+'a':c+'0'; |
||||
} |
||||
|
@ -0,0 +1,12 @@ |
||||
#include "scan.h" |
||||
|
||||
int scan_fromhex(unsigned char c) { |
||||
if (c>='0' && c<='9') |
||||
return c-'0'; |
||||
else if (c>='A' && c<='F') |
||||
return c-'A'+10; |
||||
else if (c>='a' && c<='f') |
||||
return c-'a'+10; |
||||
return -1; |
||||
} |
||||
|
@ -1,28 +0,0 @@ |
||||
#include "fmt.h" |
||||
#include "stralloc.h" |
||||
#include "textcode.h" |
||||
#include "haveinline.h" |
||||
|
||||
int fmt_base64_sa(stralloc *sa,const char* src,unsigned int len) { |
||||
register const unsigned char* s=(const unsigned char*) src; |
||||
unsigned short bits=0,temp=0; |
||||
unsigned long i; |
||||
char dest; |
||||
for (i=0; i<len; ++i) { |
||||
temp<<=8; temp+=s[i]; bits+=8; |
||||
while (bits>6) { |
||||
dest=base64[((temp>>(bits-6))&63)]; |
||||
if (!stralloc_catb(sa, &dest, 1)) return 0; |
||||
bits-=6; |
||||
} |
||||
} |
||||
if (bits) { |
||||
temp<<=(6-bits); |
||||
dest=base64[temp&63]; |
||||
if (!stralloc_catb(sa, &dest, 1)) return 0; |
||||
} |
||||
while (sa->len&3) {
|
||||
if (!stralloc_catb(sa, "=", 1)) return 0; |
||||
} |
||||
return 1; |
||||
} |
@ -0,0 +1,17 @@ |
||||
#include "fmt.h" |
||||
#include "textcode.h" |
||||
#include "str.h" |
||||
#include "haveinline.h" |
||||
|
||||
unsigned long fmt_foldwhitespace(char* dest,const char* src,unsigned long len) { |
||||
register const unsigned char* s=(const unsigned char*) src; |
||||
unsigned long i; |
||||
char c; |
||||
for (i=0; i<len; ++i) { |
||||
switch (c=s[i]) { |
||||
case ' ': case '\t': case '\n': c='_'; break; |
||||
} |
||||
if (dest) dest[i]=s[i]; |
||||
} |
||||
return len; |
||||
} |
@ -1,21 +0,0 @@ |
||||
#include "fmt.h" |
||||
#include "stralloc.h" |
||||
#include "textcode.h" |
||||
#include "str.h" |
||||
#include "haveinline.h" |
||||
|
||||
static inline int tohex(char c) { |
||||
return c>9?c-10+'a':c+'0'; |
||||
} |
||||
|
||||
int fmt_hexdump_sa(stralloc* sa,const char* src,unsigned int len) { |
||||
register const unsigned char* s=(const unsigned char*) src; |
||||
unsigned long i; |
||||
for (i=0; i<len; ++i) { |
||||
char dest[2]; |
||||
dest[0]=tohex(s[i]>>4); |
||||
dest[1]=tohex(s[i]&15); |
||||
if (!stralloc_catb(sa, dest, 2)) return 0; |
||||
} |
||||
return 1; |
||||
} |
@ -1,25 +0,0 @@ |
||||
#include "fmt.h" |
||||
#include "stralloc.h" |
||||
#include "textcode.h" |
||||
#include "haveinline.h" |
||||
|
||||
static inline int tohex(char c) { |
||||
return c>9?c-10+'A':c+'0'; |
||||
} |
||||
|
||||
int fmt_quotedprintable_sa(stralloc* sa,const char* src,unsigned int len) { |
||||
register const unsigned char* s=(const unsigned char*) src; |
||||
unsigned long i; |
||||
for (i=0; i<len; ++i) { |
||||
if (s[i]&0x80 || s[i]=='=') { |
||||
char dest[3]={'='}; |
||||
|
||||
dest[1]=tohex(s[i]>>4); |
||||
dest[2]=tohex(s[i]&15); |
||||
if (!stralloc_catb(sa, dest, 3)) return 0; |
||||
} else { |
||||
if (!stralloc_catb(sa, s + i, 1)) return 0; |
||||
} |
||||
} |
||||
return 1; |
||||
} |
@ -0,0 +1,12 @@ |
||||
#include "array.h" |
||||
#include "textcode.h" |
||||
|
||||
void fmt_to_array(unsigned long (*func)(char*,const char*,unsigned long), |
||||
array* a,const char* src,unsigned long len) { |
||||
unsigned long needed=func(0,src,len); |
||||
if (array_allocate(a,1,array_bytes(a)+needed-1)) { |
||||
char* x=array_start(a)+array_bytes(a)-needed; |
||||
func(x,src,len); |
||||
} else |
||||
array_fail(a); |
||||
} |
@ -0,0 +1,11 @@ |
||||
#include "stralloc.h" |
||||
#include "textcode.h" |
||||
|
||||
int fmt_to_sa(unsigned long (*func)(char*,const char*,unsigned long), |
||||
stralloc* sa,const char* src,unsigned long len) { |
||||
unsigned long needed=func(0,src,len); |
||||
if (!stralloc_readyplus(sa,needed)) return 0; |
||||
func(sa->s+sa->len,src,len); |
||||
sa->len+=needed; |
||||
return needed; |
||||
} |
@ -0,0 +1,15 @@ |
||||
#include "array.h" |
||||
#include "textcode.h" |
||||
|
||||
void fmt_tofrom_array(unsigned long (*func)(char*,const char*,unsigned long), |
||||
array* dest,array* src) { |
||||
unsigned long needed; |
||||
char* x; |
||||
if (array_failed(dest) || array_failed(src)) { array_fail(dest); return; } |
||||
needed=func(0,array_start(src),array_bytes(src)); |
||||
if (array_allocate(dest,1,array_bytes(dest)+needed-1)) { |
||||
x=array_start(dest)+array_bytes(dest)-needed; |
||||
func(x,array_start(src),array_bytes(src)); |
||||
} else |
||||
array_fail(dest); |
||||
} |
@ -1,26 +0,0 @@ |
||||
#include "fmt.h" |
||||
#include "stralloc.h" |
||||
#include "textcode.h" |
||||
#include "str.h" |
||||
#include "haveinline.h" |
||||
|
||||
static inline int tohex(char c) { |
||||
return c>9?c-10+'A':c+'0'; |
||||
} |
||||
|
||||
int fmt_urlencoded_sa(stralloc *sa,const char* src,unsigned int len) { |
||||
const char unsafe[]=" %<>\"#{}|\\^~[]`;/?:@=&"; |
||||
register const unsigned char* s=(const unsigned char*) src; |
||||
unsigned long i; |
||||
for (i=0; i<len; ++i) { |
||||
if (s[i]&0x80 || unsafe[str_chr(unsafe,s[i])]==s[i]) { |
||||
char dest[3] = {'%'}; |
||||
dest[1]=tohex(s[i]>>4); |
||||
dest[2]=tohex(s[i]&15); |
||||
if (!stralloc_catb(sa, dest, 3)) return 0; |
||||
} else { |
||||
if (!stralloc_catb(sa, s+i, 1)) return 0; |
||||
} |
||||
} |
||||
return 1; |
||||
} |
@ -1,38 +0,0 @@ |
||||
#include "fmt.h" |
||||
#include "stralloc.h" |
||||
#include "textcode.h" |
||||
#include "haveinline.h" |
||||
|
||||
static inline unsigned int enc(unsigned char x) { |
||||
return ((x-1)&077)+'!'; |
||||
} |
||||
|
||||
int fmt_uuencoded_sa(stralloc* sa,const char* src,unsigned int len) { |
||||
unsigned int i; |
||||
register const unsigned char* s=(const unsigned char*) src; |
||||
unsigned long tmp; |
||||
while (len) { |
||||
{ |
||||
register unsigned int diff; |
||||
char c; |
||||
if (len>45) { i=15; diff=45; } else { i=(len+2)/3; diff=len; } |
||||
c=enc(diff); |
||||
len-=diff; |
||||
if (!stralloc_catb(sa,&c,1)) return 0; |
||||
} |
||||
for (; i; --i) { |
||||
char dest[4]; |
||||
tmp=((unsigned long)s[0] << 16) + |
||||
((unsigned long)s[1] << 8) + |
||||
((unsigned long)s[2]); |
||||
dest[0]=enc((tmp>>(3*6))&077); |
||||
dest[1]=enc((tmp>>(2*6))&077); |
||||
dest[2]=enc((tmp>>(1*6))&077); |
||||
dest[3]=enc(tmp&077); |
||||
s+=3; |
||||
if (!stralloc_catb(sa,dest,4)) return 0; |
||||
} |
||||
if (!stralloc_catb(sa,"\n",1)) return 0; |
||||
} |
||||
return 1; |
||||
} |
@ -1,45 +0,0 @@ |
||||
#include "fmt.h" |
||||
#include "stralloc.h" |
||||
#include "textcode.h" |
||||
|
||||
int fmt_yenc_sa(stralloc *sa,const char* src,unsigned int len) { |
||||
register const unsigned char* s=(const unsigned char*) src; |
||||
unsigned long i; |
||||
int linelen=0; |
||||
for (i=0; i<len; ++i) { |
||||
unsigned char c=s[i]+42; |
||||
switch (c) { |
||||
case ' ': /* escape space at line ending */ |
||||
if (linelen==253) { |
||||
linelen=0; |
||||
if (!stralloc_catb(sa,"\n",1)) return 0; |
||||
} |
||||
goto dontescape; |
||||
case 'F': /* escape "^From " */ |
||||
if (s[i+1]+42!='r' || s[i+2]+42!='o' || s[i+3]+42!='m' || s[i+4]+42!=' ') goto dontescape; |
||||
case '.': /* dot at start of line needs to be escaped */ |
||||
if (!linelen) goto dontescape; |
||||
/* fall through */ |
||||
case 0: |
||||
case '\n': |
||||
case '\r': |
||||
case '=': |
||||
if (!stralloc_catb(sa,"=",1)) return 0; |
||||
c+=64; |
||||
/* fall through */ |
||||
default: |
||||
dontescape: |
||||
++linelen; |
||||
if (!stralloc_catb(sa,&c,1)) return 0; |
||||
if (linelen>253) { |
||||
linelen=0; |
||||
if (!stralloc_catb(sa,"\n",1)) return 0; |
||||
} |
||||
} |
||||
} |
||||
if (linelen) { |
||||
linelen=0; |
||||
if (!stralloc_catb(sa,"\n",1)) return 0; |
||||
} |
||||
return 1; |
||||
} |
@ -1,33 +0,0 @@ |
||||
#include "stralloc.h" |
||||
#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; |
||||
} |
||||
} |
||||
|
||||
int scan_base64_sa(const char *src,stralloc* sa) { |
||||
unsigned short tmp=0,bits=0; |
||||
register const unsigned char* s=(const unsigned char*) src; |
||||
for (;;) { |
||||
int a=dec(*s); |
||||
if (a<0) { |
||||
while (*s=='=') ++s; |
||||
break; |
||||
} |
||||
tmp=(tmp<<6)|a; bits+=6; |
||||
++s; |
||||
if (bits>=8) { |
||||
char dest=(tmp>>(bits-=8)); |
||||
if (!stralloc_catb(sa,&dest,1)) return 0; |
||||
} |
||||
} |
||||
return 1; |
||||
} |
@ -1,28 +0,0 @@ |
||||
#include "fmt.h" |
||||
#include "stralloc.h" |
||||
#include "textcode.h" |
||||
#include "haveinline.h" |
||||
|
||||
static inline int fromhex(char c) { |
||||
if (c>='0' && c<='9') return c-'0'; |
||||
if (c>='A' && c<='F') return c-'A'+10; |
||||
if (c>='a' && c<='f') return c-'a'+10; |
||||
return -1; |
||||
} |
||||
|
||||
int scan_hexdump_sa(const char *src,stralloc *sa) { |
||||
register const unsigned char* s=(const unsigned char*) src; |
||||
unsigned long i; |
||||
for (i=0; s[i]; ++i) { |
||||
char dest; |
||||
int j=fromhex(s[i]); |
||||
if (j<0) break; |
||||
dest=j<<4; |
||||
j=fromhex(s[i+1]); |
||||
if (j<0) break; |
||||
dest|=j; |
||||
++i; |
||||
if (!stralloc_catb(sa, &dest, 1)) return 0; |
||||
} |
||||
return 1; |
||||
} |
@ -1,32 +0,0 @@ |
||||
#include "fmt.h" |
||||
#include "stralloc.h" |
||||
#include "textcode.h" |
||||
#include "haveinline.h" |
||||
|
||||
static inline int fromhex(char c) { |
||||
if (c>='0' && c<='9') return c-'0'; |
||||
if (c>='A' && c<='F') return c-'A'+10; |
||||
if (c>='a' && c<='f') return c-'a'+10; |
||||
return -1; |
||||
} |
||||
|
||||
int scan_quotedprintable_sa(const char *src,stralloc* sa) { |
||||
register const unsigned char* s=(const unsigned char*) src; |
||||
unsigned long i; |
||||
for (i=0; s[i]; ++i) { |
||||
if (s[i]=='=') { |
||||
char dest; |
||||
int j=fromhex(s[i+1]); |
||||
if (j<0) break; |
||||
dest=j<<4; |
||||
j=fromhex(s[i+2]); |
||||
if (j<0) break; |
||||
dest|=j; |
||||
if (!stralloc_catb(sa, &dest, 1)) return 0; |
||||
i+=2; |
||||
} else { |
||||
if (!stralloc_catb(sa, s+i, 1)) return 0; |
||||
} |
||||
} |
||||
return 1; |
||||
} |
@ -0,0 +1,12 @@ |
||||
#include "str.h" |
||||
#include "array.h" |
||||
#include "textcode.h" |
||||
|
||||
unsigned long scan_to_array(unsigned long (*func)(const char*,char*,unsigned long*), |
||||
const char* src,array* dest) { |
||||
unsigned long scanned; |
||||
unsigned long needed=str_len(src); |
||||
char* x=array_start(dest)+array_bytes(dest); |
||||
if (!array_allocate(dest,1,array_bytes(dest)+needed-1)) return 0; |
||||
return func(src,x,&scanned); |
||||
} |
@ -0,0 +1,13 @@ |
||||
#include "str.h" |
||||
#include "stralloc.h" |
||||
#include "textcode.h" |
||||
|
||||
unsigned long scan_to_sa(unsigned long (*func)(const char*,char*,unsigned long*), |
||||
const char* src,stralloc* sa) { |
||||
unsigned long scanned; |
||||
unsigned long r; |
||||
if (!stralloc_readyplus(sa,str_len(src))) return 0; |
||||
if ((r=func(src,sa->s+sa->len,&scanned))) |
||||
sa->len+=r; |
||||
return r; |
||||
} |
@ -0,0 +1,18 @@ |
||||
#include "str.h" |
||||
#include "array.h" |
||||
#include "textcode.h" |
||||
|
||||
unsigned long scan_tofrom_array(unsigned long (*func)(const char*,char*,unsigned long*), |
||||
array* src,array* dest) { |
||||
unsigned long scanned; |
||||
unsigned long needed; |
||||
char* x; |
||||
array_cat0(src); |
||||
if (array_failed(src) || array_failed(dest)) return 0; |
||||
needed=array_bytes(src); |
||||
x=array_start(dest)+array_bytes(dest); |
||||
if (!array_allocate(dest,1,array_bytes(dest)+needed-1)) return 0; |
||||
needed=func(array_start(src),x,&scanned); |
||||
array_truncate(src,1,array_bytes(src)-1); |
||||
return needed; |
||||
} |
@ -1,33 +0,0 @@ |
||||
#include "fmt.h" |
||||
#include "stralloc.h" |
||||
#include "textcode.h" |
||||
#include "haveinline.h" |
||||
|
||||
static inline int fromhex(char c) { |
||||
if (c>='0' && c<='9') return c-'0'; |
||||
if (c>='A' && c<='F') return c-'A'+10; |
||||
if (c>='a' && c<='f') return c-'a'+10; |
||||
return -1; |
||||
} |
||||
|
||||
int scan_urlencoded_sa(const char *src, stralloc *sa) { |
||||
register const unsigned char* s=(const unsigned char*) src; |
||||
unsigned long i; |
||||
for (i=0; s[i]; ++i) { |
||||
if (s[i]=='%') { |
||||
char dest; |
||||
int j=fromhex(s[i+1]); |
||||
if (j<0) break; |
||||
dest=j<<4; |
||||
j=fromhex(s[i+2]); |
||||
if (j<0) break; |
||||
dest|=j; |
||||
i+=2; |
||||
if (!stralloc_catb(sa, &dest, 1)) return 0; |
||||
} else if (s[i]=='+') { |
||||
if (!stralloc_catb(sa," ",1)) return 0; |
||||
} else |
||||
if (!stralloc_catb(sa,s+i,1)) return 0; |
||||
} |
||||
return 1; |
||||
} |
@ -1,25 +0,0 @@ |
||||
#include "stralloc.h" |
||||
#include "textcode.h" |
||||
|
||||
int scan_uuencoded_sa(const char *src,stralloc *sa) { |
||||
unsigned int len; |
||||
unsigned long tmp; |
||||
register const unsigned char* s=(const unsigned char*) src; |
||||
if ((len=*s-' ')>64) return 0; len&=63; |
||||
++s; |
||||
while (len>0) { |
||||
char dest[3]; |
||||
unsigned int l=len; |
||||
if (s[0]-' '>64 || s[1]-' '>64 || s[2]-' '>64 || s[3]-' '>64) return 0; |
||||
tmp=(((s[0]-' ')&077) << (3*6)) + |
||||
(((s[1]-' ')&077) << (2*6)) + |
||||
(((s[2]-' ')&077) << (1*6)) + |
||||
(((s[3]-' ')&077)); |
||||
s+=4; |
||||
if (len) { dest[0]=tmp>>16; --len; } |
||||
if (len) { dest[1]=tmp>>8; --len; } |
||||
if (len) { dest[2]=tmp&0xff; --len; } |
||||
if (!stralloc_catb(sa,dest,l-len)) return 0; |
||||
} |
||||
return 1; |
||||
} |
@ -1,21 +0,0 @@ |
||||
#include "fmt.h" |
||||
#include "stralloc.h" |
||||
#include "textcode.h" |
||||
|
||||
int scan_yenc_sa(const char *src,stralloc *sa) { |
||||
register const unsigned char* s=(const unsigned char*) src; |
||||
unsigned long i; |
||||
for (i=0; s[i]; ++i) { |
||||
char dest; |
||||
if (s[i]=='=') { |
||||
++i; |
||||
if (s[i]=='y') break; |
||||
dest=s[i]-64-42; |
||||
} else if (s[i]=='\n' || s[i]=='\r' || s[i]=='\0') |
||||
break; |
||||
else |
||||
dest=s[i]-42; |
||||
if (!stralloc_catb(sa,&dest,1)) return 0; |
||||
} |
||||
return 1; |
||||
} |
Loading…
Reference in new issue