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.
This commit is contained in:
parent
2ec9f6dc9f
commit
60f3320ae0
13
t.c
13
t.c
@ -37,11 +37,13 @@ int64 writecb(int64 fd,const void* buf,uint64 n) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc,char* argv[]) {
|
int main(int argc,char* argv[]) {
|
||||||
|
#if 0
|
||||||
io_batch* b=iob_new(1234);
|
io_batch* b=iob_new(1234);
|
||||||
int64 fd=open("t.c",0);
|
int64 fd=open("t.c",0);
|
||||||
iob_addbuf(b,"fnord",5);
|
iob_addbuf(b,"fnord",5);
|
||||||
iob_addfile_close(b,fd,0,7365);
|
iob_addfile_close(b,fd,0,7365);
|
||||||
iob_write(1,b,writecb);
|
iob_write(1,b,writecb);
|
||||||
|
#endif
|
||||||
#if 0
|
#if 0
|
||||||
char dest[1024];
|
char dest[1024];
|
||||||
unsigned long len;
|
unsigned long len;
|
||||||
@ -310,5 +312,16 @@ int main(int argc,char* argv[]) {
|
|||||||
rdtscl(c);
|
rdtscl(c);
|
||||||
printf("%lu %lu\n",b-a,c-b);
|
printf("%lu %lu\n",b-a,c-b);
|
||||||
#endif
|
#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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
16
textcode.h
16
textcode.h
@ -6,13 +6,16 @@
|
|||||||
unsigned long fmt_uuencoded(char* dest,const char* src,unsigned long len);
|
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_base64(char* dest,const char* src,unsigned long len);
|
||||||
unsigned long fmt_quotedprintable(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_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_yenc(char* dest,const char* src,unsigned long len);
|
||||||
unsigned long fmt_hexdump(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 '&' */
|
/* this changes '<' to '<' and '&' to '&' */
|
||||||
unsigned long fmt_html(char* dest,const char* src,unsigned long len);
|
unsigned long fmt_html(char* dest,const char* src,unsigned long len);
|
||||||
/* change '\' to "\\", '\n' to "\n", ^A to "\x01" etc */
|
/* change '\' to "\\", '\n' to "\n", ^A to "\x01" etc */
|
||||||
unsigned long fmt_cescape(char* dest,const char* src,unsigned long len);
|
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
|
/* 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 */
|
* 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);
|
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),
|
int fmt_to_sa(unsigned long (*func)(char*,const char*,unsigned long),
|
||||||
stralloc* sa,const char* src,unsigned long len);
|
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 */
|
/* arg 1 is one of the scan_* functions from above */
|
||||||
/* return number of bytes scanned */
|
/* return number of bytes scanned */
|
||||||
unsigned long scan_to_sa(unsigned long (*func)(const char*,char*,unsigned long*),
|
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_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_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_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_base64_sa(src,sa) scan_to_sa(scan_base64,src,sa)
|
||||||
#define scan_quotedprintable_sa(src,sa) scan_to_sa(scan_quotedprintable,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),
|
void fmt_tofrom_array(unsigned long (*func)(char*,const char*,unsigned long),
|
||||||
array* dest,array* src);
|
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*),
|
unsigned long scan_to_array(unsigned long (*func)(const char*,char*,unsigned long*),
|
||||||
const char* src,array* dest);
|
const char* src,array* dest);
|
||||||
|
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
#include "str.h"
|
#include "str.h"
|
||||||
#include "haveinline.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;
|
register const unsigned char* s=(const unsigned char*) src;
|
||||||
unsigned long written=0,i;
|
unsigned long written=0,i;
|
||||||
char c;
|
char c;
|
||||||
@ -27,7 +27,7 @@ unsigned long fmt_cescape(char* dest,const char* src,unsigned long len) {
|
|||||||
written+=2;
|
written+=2;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
if (s[i]<' ') {
|
if (s[i]<' ' || escapeme[str_chr(escapeme,s[i])]==s[i]) {
|
||||||
if (dest) {
|
if (dest) {
|
||||||
dest[written]='\\';
|
dest[written]='\\';
|
||||||
dest[written+1]='x';
|
dest[written+1]='x';
|
||||||
@ -44,3 +44,7 @@ unsigned long fmt_cescape(char* dest,const char* src,unsigned long len) {
|
|||||||
}
|
}
|
||||||
return written;
|
return written;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unsigned long fmt_cescape(char* dest,const char* src,unsigned long len) {
|
||||||
|
return fmt_cescape2(dest,src,len,"");
|
||||||
|
}
|
||||||
|
@ -1,12 +1,13 @@
|
|||||||
#include "fmt.h"
|
#include "fmt.h"
|
||||||
#include "textcode.h"
|
#include "textcode.h"
|
||||||
#include "haveinline.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;
|
register const unsigned char* s=(const unsigned char*) src;
|
||||||
unsigned long written=0,i;
|
unsigned long written=0,i;
|
||||||
for (i=0; i<len; ++i) {
|
for (i=0; i<len; ++i) {
|
||||||
if (s[i]&0x80 || s[i]=='=') {
|
if (s[i]&0x80 || s[i]<' ' || s[i]=='=' || escapeme[str_chr(escapeme,s[i])]==s[i]) {
|
||||||
if (dest) {
|
if (dest) {
|
||||||
dest[written]='=';
|
dest[written]='=';
|
||||||
dest[written+1]=fmt_tohex(s[i]>>4);
|
dest[written+1]=fmt_tohex(s[i]>>4);
|
||||||
@ -19,3 +20,7 @@ unsigned long fmt_quotedprintable(char* dest,const char* src,unsigned long len)
|
|||||||
}
|
}
|
||||||
return written;
|
return written;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unsigned long fmt_quotedprintable(char* dest,const char* src,unsigned long len) {
|
||||||
|
return fmt_quotedprintable2(dest,src,len,"");
|
||||||
|
}
|
||||||
|
@ -11,11 +11,11 @@ static inline int issafe(unsigned char c) {
|
|||||||
safe[str_chr(safe,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;
|
register const unsigned char* s=(const unsigned char*) src;
|
||||||
unsigned long written=0,i;
|
unsigned long written=0,i;
|
||||||
for (i=0; i<len; ++i) {
|
for (i=0; i<len; ++i) {
|
||||||
if (!issafe(s[i])) {
|
if (!issafe(s[i]) || escapeme[str_chr(escapeme,s[i])]==s[i]) {
|
||||||
if (dest) {
|
if (dest) {
|
||||||
dest[written]='%';
|
dest[written]='%';
|
||||||
dest[written+1]=fmt_tohex(s[i]>>4);
|
dest[written+1]=fmt_tohex(s[i]>>4);
|
||||||
@ -28,3 +28,7 @@ unsigned long fmt_urlencoded(char* dest,const char* src,unsigned long len) {
|
|||||||
}
|
}
|
||||||
return written;
|
return written;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unsigned long fmt_urlencoded(char* dest,const char* src,unsigned long len) {
|
||||||
|
return fmt_urlencoded2(dest,src,len,"");
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user