From 60f3320ae0ec161fb2465f23a6097cdd2045522c Mon Sep 17 00:00:00 2001 From: leitner Date: Wed, 30 Mar 2005 13:22:17 +0000 Subject: [PATCH] allow specifying more characters to encode for subset encoders (for example, you can say that fmt_quotedprintable should also escape ':', if you want to use ':' as separator in a data file. --- t.c | 13 +++++++++++++ textcode.h | 16 ++++++++++++++++ textcode/fmt_cescape.c | 8 ++++++-- textcode/fmt_quotedprintable.c | 9 +++++++-- textcode/fmt_urlencoded.c | 8 ++++++-- 5 files changed, 48 insertions(+), 6 deletions(-) diff --git a/t.c b/t.c index fbb28ab..5b7129a 100644 --- a/t.c +++ b/t.c @@ -37,11 +37,13 @@ int64 writecb(int64 fd,const void* buf,uint64 n) { } int main(int argc,char* argv[]) { +#if 0 io_batch* b=iob_new(1234); int64 fd=open("t.c",0); iob_addbuf(b,"fnord",5); iob_addfile_close(b,fd,0,7365); iob_write(1,b,writecb); +#endif #if 0 char dest[1024]; unsigned long len; @@ -310,5 +312,16 @@ int main(int argc,char* argv[]) { rdtscl(c); printf("%lu %lu\n",b-a,c-b); #endif +#if 1 + unsigned long size; + char* buf=mmap_read(argv[1],&size); + if (buf) { + unsigned int x=fmt_urlencoded2(0,buf,size,"x"); + unsigned int y; + char* tmp=malloc(x+1); + y=fmt_urlencoded2(tmp,buf,size,"x"); + write(1,tmp,x); + } +#endif } diff --git a/textcode.h b/textcode.h index 96c1abf..e38c806 100644 --- a/textcode.h +++ b/textcode.h @@ -6,13 +6,16 @@ unsigned long fmt_uuencoded(char* dest,const char* src,unsigned long len); unsigned long fmt_base64(char* dest,const char* src,unsigned long len); unsigned long fmt_quotedprintable(char* dest,const char* src,unsigned long len); +unsigned long fmt_quotedprintable2(char* dest,const char* src,unsigned long len,const char* escapeme); unsigned long fmt_urlencoded(char* dest,const char* src,unsigned long len); +unsigned long fmt_urlencoded2(char* dest,const char* src,unsigned long len,const char* escapeme); unsigned long fmt_yenc(char* dest,const char* src,unsigned long len); unsigned long fmt_hexdump(char* dest,const char* src,unsigned long len); /* this changes '<' to '<' and '&' to '&' */ unsigned long fmt_html(char* dest,const char* src,unsigned long len); /* change '\' to "\\", '\n' to "\n", ^A to "\x01" etc */ unsigned long fmt_cescape(char* dest,const char* src,unsigned long len); +unsigned long fmt_cescape2(char* dest,const char* src,unsigned long len,const char* escapeme); /* fold awk whitespace to '_'; this is great for writing fields with * white spaces to a log file and still allow awk to do log analysis */ unsigned long fmt_foldwhitespace(char* dest,const char* src,unsigned long len); @@ -37,6 +40,9 @@ unsigned long scan_cescape(const char *src,char *dest,unsigned long *destlen); int fmt_to_sa(unsigned long (*func)(char*,const char*,unsigned long), stralloc* sa,const char* src,unsigned long len); +int fmt_to_sa2(unsigned long (*func)(char*,const char*,unsigned long,const char*), + stralloc* sa,const char* src,unsigned long len,const char* escapeme); + /* arg 1 is one of the scan_* functions from above */ /* return number of bytes scanned */ unsigned long scan_to_sa(unsigned long (*func)(const char*,char*,unsigned long*), @@ -51,6 +57,10 @@ unsigned long scan_to_sa(unsigned long (*func)(const char*,char*,unsigned long*) #define fmt_html_sa(sa,src,len) fmt_to_sa(fmt_html,sa,src,len) #define fmt_cescape_sa(sa,src,len) fmt_to_sa(fmt_cescape,sa,src,len) +#define fmt_quotedprintable2_sa(sa,src,len,escapeme) fmt_to_sa2(fmt_quotedprintable2,sa,src,len,escapeme) +#define fmt_urlencoded2_sa(sa,src,len,escapeme) fmt_to_sa2(fmt_urlencoded2,sa,src,len,escapeme) +#define fmt_cescape2_sa(sa,src,len,escapeme) fmt_to_sa2(fmt_cescape2,sa,src,len,escapeme) + #define scan_uuencoded_sa(src,sa) scan_to_sa(scan_uuencoded,src,sa) #define scan_base64_sa(src,sa) scan_to_sa(scan_base64,src,sa) #define scan_quotedprintable_sa(src,sa) scan_to_sa(scan_quotedprintable,src,sa) @@ -68,6 +78,12 @@ void fmt_to_array(unsigned long (*func)(char*,const char*,unsigned long), void fmt_tofrom_array(unsigned long (*func)(char*,const char*,unsigned long), array* dest,array* src); +void fmt_to_array2(unsigned long (*func)(char*,const char*,unsigned long,const char*), + array* a,const char* src,unsigned long len,const char* escapeme); + +void fmt_tofrom_array2(unsigned long (*func)(char*,const char*,unsigned long,const char*), + array* dest,array* src,const char* escapeme); + unsigned long scan_to_array(unsigned long (*func)(const char*,char*,unsigned long*), const char* src,array* dest); diff --git a/textcode/fmt_cescape.c b/textcode/fmt_cescape.c index 369e478..cc6efe2 100644 --- a/textcode/fmt_cescape.c +++ b/textcode/fmt_cescape.c @@ -3,7 +3,7 @@ #include "str.h" #include "haveinline.h" -unsigned long fmt_cescape(char* dest,const char* src,unsigned long len) { +unsigned long fmt_cescape2(char* dest,const char* src,unsigned long len,const char* escapeme) { register const unsigned char* s=(const unsigned char*) src; unsigned long written=0,i; char c; @@ -27,7 +27,7 @@ unsigned long fmt_cescape(char* dest,const char* src,unsigned long len) { written+=2; break; default: - if (s[i]<' ') { + if (s[i]<' ' || escapeme[str_chr(escapeme,s[i])]==s[i]) { if (dest) { dest[written]='\\'; dest[written+1]='x'; @@ -44,3 +44,7 @@ unsigned long fmt_cescape(char* dest,const char* src,unsigned long len) { } return written; } + +unsigned long fmt_cescape(char* dest,const char* src,unsigned long len) { + return fmt_cescape2(dest,src,len,""); +} diff --git a/textcode/fmt_quotedprintable.c b/textcode/fmt_quotedprintable.c index c330947..eb7d81c 100644 --- a/textcode/fmt_quotedprintable.c +++ b/textcode/fmt_quotedprintable.c @@ -1,12 +1,13 @@ #include "fmt.h" #include "textcode.h" #include "haveinline.h" +#include "str.h" -unsigned long fmt_quotedprintable(char* dest,const char* src,unsigned long len) { +unsigned long fmt_quotedprintable2(char* dest,const char* src,unsigned long len,const char* escapeme) { register const unsigned char* s=(const unsigned char*) src; unsigned long written=0,i; for (i=0; i>4); @@ -19,3 +20,7 @@ unsigned long fmt_quotedprintable(char* dest,const char* src,unsigned long len) } return written; } + +unsigned long fmt_quotedprintable(char* dest,const char* src,unsigned long len) { + return fmt_quotedprintable2(dest,src,len,""); +} diff --git a/textcode/fmt_urlencoded.c b/textcode/fmt_urlencoded.c index 8f2dcd8..32a0dd5 100644 --- a/textcode/fmt_urlencoded.c +++ b/textcode/fmt_urlencoded.c @@ -11,11 +11,11 @@ static inline int issafe(unsigned char c) { safe[str_chr(safe,c)]); } -unsigned long fmt_urlencoded(char* dest,const char* src,unsigned long len) { +unsigned long fmt_urlencoded2(char* dest,const char* src,unsigned long len,const char* escapeme) { register const unsigned char* s=(const unsigned char*) src; unsigned long written=0,i; for (i=0; i>4); @@ -28,3 +28,7 @@ unsigned long fmt_urlencoded(char* dest,const char* src,unsigned long len) { } return written; } + +unsigned long fmt_urlencoded(char* dest,const char* src,unsigned long len) { + return fmt_urlencoded2(dest,src,len,""); +}