diff --git a/fmt.h b/fmt.h index 9261829..4ca896d 100644 --- a/fmt.h +++ b/fmt.h @@ -198,7 +198,7 @@ size_t fmt_escapecharquotedprintable(char* dest,uint32_t ch) __pure__; /* MIME quoted-printable escaping with UTF-8: 'รถ' -> '=c3=b6', characters > 0x7fffffff not supported */ size_t fmt_escapecharquotedprintableutf8(char* dest,uint32_t ch) __pure__; -/* C escaping: '\' -> '\\', newline -> '\n', 0xc2 -> '\302' */ +/* C99 style escaping: '\' -> '\\', newline -> '\n', 0xc2 -> '\302' */ size_t fmt_escapecharc(char* dest,uint32_t ch) __pure__; /* internal functions, may be independently useful */ diff --git a/fmt/fmt_8long.c b/fmt/fmt_8long.c index 2593f15..7a372f6 100644 --- a/fmt/fmt_8long.c +++ b/fmt/fmt_8long.c @@ -11,3 +11,13 @@ size_t fmt_8long(char *dest,unsigned long i) { } return len; } + +#ifdef UNITTEST +#include +#include + +int main() { + char buf[100]; + assert(fmt_8long(buf,012345)==5 && !memcmp(buf,"12345",5)); +} +#endif diff --git a/fmt/fmt_8longlong.c b/fmt/fmt_8longlong.c index bcb2ec7..97cb9bf 100644 --- a/fmt/fmt_8longlong.c +++ b/fmt/fmt_8longlong.c @@ -12,3 +12,13 @@ size_t fmt_8longlong(char *dest,unsigned long long i) { } return len; } + +#ifdef UNITTEST +#include +#include + +int main() { + char buf[100]; + assert(fmt_8longlong(buf,0123456701234567)==15 && !memcmp(buf,"123456701234567",15)); +} +#endif diff --git a/fmt/fmt_asn1derlength.c b/fmt/fmt_asn1derlength.c index 263d473..160a2d5 100644 --- a/fmt/fmt_asn1derlength.c +++ b/fmt/fmt_asn1derlength.c @@ -23,3 +23,20 @@ size_t fmt_asn1derlength(char* dest,unsigned long long l) { } return i+1; } + +#ifdef UNITTEST +#include +#include + +int main() { + char buf[100]; +#define zap() memset(buf,'_',sizeof buf) + assert(fmt_asn1derlength(NULL,0)==1); + zap(); assert(fmt_asn1derlength(buf,0)==1 && !memcmp(buf,"\x00_",2)); + assert(fmt_asn1derlength(NULL,0xc2)==2); + zap(); assert(fmt_asn1derlength(buf,0xc2)==2 && !memcmp(buf,"\x81\xc2_",3)); + assert(fmt_asn1derlength(NULL,0x1234)==3); + zap(); assert(fmt_asn1derlength(buf,0x1234)==3 && !memcmp(buf,"\x82\x12\x34_",3)); + return 0; +} +#endif diff --git a/fmt/fmt_asn1dertag.c b/fmt/fmt_asn1dertag.c index 9d2d0cc..3f50ea7 100644 --- a/fmt/fmt_asn1dertag.c +++ b/fmt/fmt_asn1dertag.c @@ -13,3 +13,19 @@ size_t fmt_asn1dertag(char* dest,unsigned long long l) { } return i; } + +#ifdef UNITTEST +#include +#include + +char buf[100]; +void zap() { size_t i; for (i=0; i>=3; dest[1]=(char)((w&7)+'0'); w>>=3; @@ -8,7 +12,28 @@ static void fmt_oct3(char* dest,uint8_t w) { size_t fmt_escapecharc(char* dest,uint32_t ch) { char c; - if (ch>0xff) return 0; + if (ch>0xff) { + if (dest) { + unsigned int i; + dest[0]='\\'; + if (ch>0xffff) { + dest[1]='U'; + dest[2]=fmt_tohex((ch>>28)&0xf); + dest[3]=fmt_tohex((ch>>24)&0xf); + dest[4]=fmt_tohex((ch>>20)&0xf); + dest[5]=fmt_tohex((ch>>16)&0xf); + i=6; + } else { + dest[1]='u'; + i=2; + } + dest[i]=fmt_tohex((ch>>12)&0xf); + dest[i+1]=fmt_tohex((ch>>8)&0xf); + dest[i+2]=fmt_tohex((ch>>4)&0xf); + dest[i+3]=fmt_tohex(ch&0xf); + } + return (ch>0xffff ? 10 : 6); + } switch (ch) { case '\a': c='a'; goto doescape; case '\b': c='b'; goto doescape; @@ -34,3 +59,27 @@ size_t fmt_escapecharc(char* dest,uint32_t ch) { return 4; } } + +#ifdef UNITTEST +#include +#include +#include + +int main() { + char buf[100]; + assert(fmt_escapecharc(buf,0)==4 && !memcmp(buf,"\\000",4)); + assert(fmt_escapecharc(buf,'\a')==2 && !memcmp(buf,"\\a",2)); + assert(fmt_escapecharc(buf,'\b')==2 && !memcmp(buf,"\\b",2)); + assert(fmt_escapecharc(buf,'\f')==2 && !memcmp(buf,"\\f",2)); + assert(fmt_escapecharc(buf,'\n')==2 && !memcmp(buf,"\\n",2)); + assert(fmt_escapecharc(buf,'\r')==2 && !memcmp(buf,"\\r",2)); + assert(fmt_escapecharc(buf,0x1b)==2 && !memcmp(buf,"\\e",2)); + assert(fmt_escapecharc(buf,'\t')==2 && !memcmp(buf,"\\t",2)); + assert(fmt_escapecharc(buf,'\v')==2 && !memcmp(buf,"\\v",2)); + assert(fmt_escapecharc(buf,'\\')==2 && !memcmp(buf,"\\\\",2)); + assert(fmt_escapecharc(buf,'1')==4 && !memcmp(buf,"\\001",2)); + assert(fmt_escapecharc(buf,0xfefe)==6 && !memcmp(buf,"\\ufefe",6)); + assert(fmt_escapecharc(buf,0xfefec0de)==10 && !memcmp(buf,"\\Ufefec0de",10)); +} + +#endif diff --git a/fmt/fmt_ulong.c b/fmt/fmt_ulong.c index d89fb5b..7b7bc4a 100644 --- a/fmt/fmt_ulong.c +++ b/fmt/fmt_ulong.c @@ -9,3 +9,13 @@ size_t fmt_ulong(char *dest,unsigned long i) { *--dest = (char)((tmp%10)+'0'); return len; } + +#ifdef UNITTEST +#include +#include + +int main() { + char buf[100]; + assert(fmt_ulong(buf,12345)==5 && !memcmp(buf,"12345",5)); +} +#endif diff --git a/fmt/fmt_xlong.c b/fmt/fmt_xlong.c index 44723c4..37a66f8 100644 --- a/fmt/fmt_xlong.c +++ b/fmt/fmt_xlong.c @@ -16,3 +16,13 @@ size_t fmt_xlong(char *dest,unsigned long i) { } return len; } + +#ifdef UNITTEST +#include +#include + +int main() { + char buf[100]; + assert(fmt_xlong(buf,0x12345)==5 && !memcmp(buf,"12345",5)); +} +#endif diff --git a/textcode/scan_base64.c b/textcode/scan_base64.c index 63f9698..8affddd 100644 --- a/textcode/scan_base64.c +++ b/textcode/scan_base64.c @@ -49,5 +49,6 @@ int main() { size_t i,l; memset(buf,0,10); assert(scan_base64("Zm5vcmQ=",buf,&l)==8 && l==5 && !memcmp(buf,"fnord",6)); memset(buf,0,10); assert(scan_base64("Zm5vcmQ",buf,&l)==7 && l==5 && !memcmp(buf,"fnord",6)); + memset(buf,0,10); assert(scan_base64("//8=",buf,&l)==4 && l==2 && !memcmp(buf,"\xff\xff",3)); } #endif diff --git a/textcode/scan_jsonescape.c b/textcode/scan_jsonescape.c index 1e611dc..ae3bc61 100644 --- a/textcode/scan_jsonescape.c +++ b/textcode/scan_jsonescape.c @@ -72,6 +72,7 @@ abort: #ifdef UNITTEST #include +#include int main() { char buf[100];