remove special case stralloc textcode functions

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 textcode
This commit is contained in:
leitner
2003-09-19 19:08:13 +00:00
parent e86a457f5c
commit a8f6a1c121
40 changed files with 218 additions and 477 deletions

View File

@@ -2,7 +2,7 @@
#include "textcode.h"
#include "haveinline.h"
unsigned int fmt_base64(char* dest,const char* src,unsigned int len) {
unsigned long fmt_base64(char* dest,const char* src,unsigned long len) {
register const unsigned char* s=(const unsigned char*) src;
unsigned short bits=0,temp=0;
unsigned long written=0,i;

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -3,16 +3,12 @@
#include "str.h"
#include "haveinline.h"
static inline int tohex(char c) {
return c>9?c-10+'a':c+'0';
}
unsigned int fmt_hexdump(char* dest,const char* src,unsigned int len) {
unsigned long fmt_hexdump(char* dest,const char* src,unsigned long len) {
register const unsigned char* s=(const unsigned char*) src;
unsigned long written=0,i;
for (i=0; i<len; ++i) {
dest[written]=tohex(s[i]>>4);
dest[written+1]=tohex(s[i]&15);
dest[written]=fmt_tohex(s[i]>>4);
dest[written+1]=fmt_tohex(s[i]&15);
written+=2;
}
return written;

View File

@@ -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;
}

View File

@@ -3,7 +3,7 @@
#include "str.h"
#include "haveinline.h"
unsigned int fmt_html(char* dest,const char* src,unsigned int len) {
unsigned long fmt_html(char* dest,const char* src,unsigned long len) {
register const unsigned char* s=(const unsigned char*) src;
unsigned long written=0,i;
const char* seq;

View File

@@ -2,19 +2,15 @@
#include "textcode.h"
#include "haveinline.h"
static inline int tohex(char c) {
return c>9?c-10+'A':c+'0';
}
unsigned int fmt_quotedprintable(char* dest,const char* src,unsigned int len) {
unsigned long fmt_quotedprintable(char* dest,const char* src,unsigned long len) {
register const unsigned char* s=(const unsigned char*) src;
unsigned long written=0,i;
for (i=0; i<len; ++i) {
if (s[i]&0x80 || s[i]=='=') {
if (dest) {
dest[written]='=';
dest[written+1]=tohex(s[i]>>4);
dest[written+2]=tohex(s[i]&15);
dest[written+1]=fmt_tohex(s[i]>>4);
dest[written+2]=fmt_tohex(s[i]&15);
}
written+=3;
} else {

View File

@@ -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;
}

12
textcode/fmt_to_array.c Normal file
View File

@@ -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);
}

11
textcode/fmt_to_sa.c Normal file
View File

@@ -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;
}

View File

@@ -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);
}

View File

@@ -3,11 +3,7 @@
#include "str.h"
#include "haveinline.h"
static inline int tohex(char c) {
return c>9?c-10+'A':c+'0';
}
unsigned int fmt_urlencoded(char* dest,const char* src,unsigned int len) {
unsigned long fmt_urlencoded(char* dest,const char* src,unsigned long len) {
const char unsafe[]=" +%<>\"#{}|\\^~[]`;/?:@=&";
register const unsigned char* s=(const unsigned char*) src;
unsigned long written=0,i;
@@ -15,8 +11,8 @@ unsigned int fmt_urlencoded(char* dest,const char* src,unsigned int len) {
if (s[i]&0x80 || unsafe[str_chr(unsafe,s[i])]==s[i]) {
if (dest) {
dest[written]='%';
dest[written+1]=tohex(s[i]>>4);
dest[written+2]=tohex(s[i]&15);
dest[written+1]=fmt_tohex(s[i]>>4);
dest[written+2]=fmt_tohex(s[i]&15);
}
written+=3;
} else {

View File

@@ -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;
}

View File

@@ -6,7 +6,7 @@ static inline unsigned int enc(unsigned char x) {
return ((x-1)&077)+'!';
}
unsigned int fmt_uuencoded(char* dest,const char* src,unsigned int len) {
unsigned long fmt_uuencoded(char* dest,const char* src,unsigned long len) {
unsigned int i;
register const unsigned char* s=(const unsigned char*) src;
const char* orig=dest;

View File

@@ -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;
}

View File

@@ -1,7 +1,7 @@
#include "fmt.h"
#include "textcode.h"
unsigned int fmt_yenc(char* dest,const char* src,unsigned int len) {
unsigned long fmt_yenc(char* dest,const char* src,unsigned long len) {
register const unsigned char* s=(const unsigned char*) src;
unsigned long written=0,i;
int linelen=0;

View File

@@ -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;
}

View File

@@ -12,7 +12,7 @@ static inline int dec(unsigned char x) {
}
}
unsigned int scan_base64(const char *src,char *dest,unsigned int *destlen) {
unsigned long scan_base64(const char *src,char *dest,unsigned long *destlen) {
unsigned short tmp=0,bits=0;
register const unsigned char* s=(const unsigned char*) src;
const char* orig=dest;

View File

@@ -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;
}

View File

@@ -1,22 +1,15 @@
#include "fmt.h"
#include "textcode.h"
#include "haveinline.h"
#include "scan.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;
}
unsigned int scan_hexdump(const char *src,char *dest,unsigned int *destlen) {
unsigned long scan_hexdump(const char *src,char *dest,unsigned long *destlen) {
register const unsigned char* s=(const unsigned char*) src;
unsigned long written=0,i;
for (i=0; s[i]; ++i) {
int j=fromhex(s[i]);
int j=scan_fromhex(s[i]);
if (j<0) break;
dest[written]=j<<4;
j=fromhex(s[i+1]);
j=scan_fromhex(s[i+1]);
if (j<0) break;
dest[written]|=j;
++i;

View File

@@ -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;
}

View File

@@ -3,7 +3,7 @@
#include "haveinline.h"
#include "case.h"
unsigned int scan_html(const char *src,char *dest,unsigned int *destlen) {
unsigned long scan_html(const char *src,char *dest,unsigned long *destlen) {
register const unsigned char* s=(const unsigned char*) src;
unsigned long written=0,i;
for (i=0; s[i]; ++i) {

View File

@@ -1,23 +1,16 @@
#include "fmt.h"
#include "textcode.h"
#include "haveinline.h"
#include "scan.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;
}
unsigned int scan_quotedprintable(const char *src,char *dest,unsigned int *destlen) {
unsigned long scan_quotedprintable(const char *src,char *dest,unsigned long *destlen) {
register const unsigned char* s=(const unsigned char*) src;
unsigned long written=0,i;
for (i=0; s[i]; ++i) {
if (s[i]=='=') {
int j=fromhex(s[i+1]);
int j=scan_fromhex(s[i+1]);
if (j<0) break;
dest[written]=j<<4;
j=fromhex(s[i+2]);
j=scan_fromhex(s[i+2]);
if (j<0) break;
dest[written]|=j;
i+=2;

View File

@@ -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;
}

12
textcode/scan_to_array.c Normal file
View File

@@ -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);
}

13
textcode/scan_to_sa.c Normal file
View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -1,23 +1,16 @@
#include "fmt.h"
#include "textcode.h"
#include "haveinline.h"
#include "scan.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;
}
unsigned int scan_urlencoded(const char *src,char *dest,unsigned int *destlen) {
unsigned long scan_urlencoded(const char *src,char *dest,unsigned long *destlen) {
register const unsigned char* s=(const unsigned char*) src;
unsigned long written=0,i;
for (i=0; s[i]; ++i) {
if (s[i]=='%') {
int j=fromhex(s[i+1]);
int j=scan_fromhex(s[i+1]);
if (j<0) break;
dest[written]=j<<4;
j=fromhex(s[i+2]);
j=scan_fromhex(s[i+2]);
if (j<0) break;
dest[written]|=j;
i+=2;

View File

@@ -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;
}

View File

@@ -1,6 +1,6 @@
#include "textcode.h"
unsigned int scan_uuencoded(const char *src,char *dest,unsigned int *destlen) {
unsigned long scan_uuencoded(const char *src,char *dest,unsigned long *destlen) {
unsigned int len;
unsigned long tmp;
register const unsigned char* s=(const unsigned char*) src;

View File

@@ -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;
}

View File

@@ -1,7 +1,7 @@
#include "fmt.h"
#include "textcode.h"
unsigned int scan_yenc(const char *src,char *dest,unsigned int *destlen) {
unsigned long scan_yenc(const char *src,char *dest,unsigned long *destlen) {
register const unsigned char* s=(const unsigned char*) src;
unsigned long written=0,i;
for (i=0; s[i]; ++i) {

View File

@@ -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;
}