From 056760c5f191ac5cd073d0c262c753b980141783 Mon Sep 17 00:00:00 2001 From: leitner Date: Fri, 19 Sep 2003 19:08:51 +0000 Subject: [PATCH] add cescape stuff and two tests --- test/cescape.c | 25 ++++++++++++++++++++++ test/textcode.c | 21 +++++++++++++++++++ textcode/fmt_cescape.c | 46 +++++++++++++++++++++++++++++++++++++++++ textcode/scan_cescape.c | 42 +++++++++++++++++++++++++++++++++++++ 4 files changed, 134 insertions(+) create mode 100644 test/cescape.c create mode 100644 test/textcode.c create mode 100644 textcode/fmt_cescape.c create mode 100644 textcode/scan_cescape.c diff --git a/test/cescape.c b/test/cescape.c new file mode 100644 index 0000000..3df9cd9 --- /dev/null +++ b/test/cescape.c @@ -0,0 +1,25 @@ + +#include +#include "buffer.h" +#include "textcode.h" + +void cescape(const char* c) { + char* buf=alloca(strlen(c)*5+1); + buffer_put(buffer_1,buf,fmt_cescape(buf,c,strlen(c))); + buffer_putnlflush(buffer_1); +} + +main(int argc,char* argv[]) { + int i; + for (i=1; i + +array a; + +int main() { + char buf[256]; + int i; + for (i=0; i<256; ++i) buf[i]=i; + + fmt_to_array(fmt_uuencoded,&a,buf,256); + assert(!array_failed(&a)); + write(1,array_start(&a),array_bytes(&a)); + array_trunc(&a); + + fmt_to_array(fmt_base64,&a,buf,256); + assert(!array_failed(&a)); + write(1,array_start(&a),array_bytes(&a)); write(1,"\n",1); + array_trunc(&a); +} diff --git a/textcode/fmt_cescape.c b/textcode/fmt_cescape.c new file mode 100644 index 0000000..2ff9f12 --- /dev/null +++ b/textcode/fmt_cescape.c @@ -0,0 +1,46 @@ +#include "fmt.h" +#include "textcode.h" +#include "str.h" +#include "haveinline.h" + +unsigned long fmt_cescape(char* dest,const char* src,unsigned long len) { + register const unsigned char* s=(const unsigned char*) src; + unsigned long written=0,i; + char c; + for (i=0; i>4); + dest[written+3]=fmt_tohex(s[i]&0xf); + } + written+=4; + } else { + if (dest) dest[written]=s[i]; + ++written; + } + break; + } + } + return written; +} diff --git a/textcode/scan_cescape.c b/textcode/scan_cescape.c new file mode 100644 index 0000000..8f0cdc4 --- /dev/null +++ b/textcode/scan_cescape.c @@ -0,0 +1,42 @@ +#include "fmt.h" +#include "textcode.h" +#include "scan.h" + +unsigned long scan_cescape(const char *src,char *dest,unsigned long *destlen) { + register const unsigned char* s=(const unsigned char*) src; + unsigned long written=0,i; + char c; + for (i=0; s[i]; ++i) { + if ((c=s[i])=='\\') { + switch (s[i+1]) { + case 'a': c='\a'; break; + case 'b': c='\b'; break; + case 'e': c='\e'; break; + case 'f': c='\f'; break; + case 'n': c='\n'; break; + case 'r': c='\r'; break; + case 't': c='\t'; break; + case 'v': c='\v'; + case '\\': break; + case 'x': + { + unsigned char a,b; + a=scan_fromhex(s[i+2]); + b=scan_fromhex(s[i+3]); + if (a<16 && b<16) { + c=(a<<4)+b; + i+=2; + } + } + break; + default: + --i; + } + ++i; + } + dest[written]=c; + ++written; + } + *destlen=written; + return i; +}