a few more test suites

escapecharc now supports non-ascii
master
leitner 8 years ago
parent 607801f2fd
commit 660c314341

@ -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 */ /* MIME quoted-printable escaping with UTF-8: 'ö' -> '=c3=b6', characters > 0x7fffffff not supported */
size_t fmt_escapecharquotedprintableutf8(char* dest,uint32_t ch) __pure__; 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__; size_t fmt_escapecharc(char* dest,uint32_t ch) __pure__;
/* internal functions, may be independently useful */ /* internal functions, may be independently useful */

@ -11,3 +11,13 @@ size_t fmt_8long(char *dest,unsigned long i) {
} }
return len; return len;
} }
#ifdef UNITTEST
#include <assert.h>
#include <string.h>
int main() {
char buf[100];
assert(fmt_8long(buf,012345)==5 && !memcmp(buf,"12345",5));
}
#endif

@ -12,3 +12,13 @@ size_t fmt_8longlong(char *dest,unsigned long long i) {
} }
return len; return len;
} }
#ifdef UNITTEST
#include <assert.h>
#include <string.h>
int main() {
char buf[100];
assert(fmt_8longlong(buf,0123456701234567)==15 && !memcmp(buf,"123456701234567",15));
}
#endif

@ -23,3 +23,20 @@ size_t fmt_asn1derlength(char* dest,unsigned long long l) {
} }
return i+1; return i+1;
} }
#ifdef UNITTEST
#include <assert.h>
#include <string.h>
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

@ -13,3 +13,19 @@ size_t fmt_asn1dertag(char* dest,unsigned long long l) {
} }
return i; return i;
} }
#ifdef UNITTEST
#include <assert.h>
#include <string.h>
char buf[100];
void zap() { size_t i; for (i=0; i<sizeof(buf); ++i) buf[i]='_'; }
int main() {
assert(fmt_asn1dertag(NULL,0)==1);
zap(); assert(fmt_asn1dertag(buf,0)==1 && !memcmp(buf,"\x00_",2));
assert(fmt_asn1dertag(NULL,0xc2)==2);
zap(); assert(fmt_asn1dertag(buf,0xc2)==2 && !memcmp(buf,"\x81\x42_",3));
return 0;
}
#endif

@ -1,5 +1,9 @@
#include "fmt.h" #include "fmt.h"
#ifdef UNITTEST
#include "fmt_tohex.c"
#endif
static void fmt_oct3(char* dest,uint8_t w) { static void fmt_oct3(char* dest,uint8_t w) {
dest[2]=(char)((w&7)+'0'); w>>=3; dest[2]=(char)((w&7)+'0'); w>>=3;
dest[1]=(char)((w&7)+'0'); w>>=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) { size_t fmt_escapecharc(char* dest,uint32_t ch) {
char c; 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) { switch (ch) {
case '\a': c='a'; goto doescape; case '\a': c='a'; goto doescape;
case '\b': c='b'; goto doescape; case '\b': c='b'; goto doescape;
@ -34,3 +59,27 @@ size_t fmt_escapecharc(char* dest,uint32_t ch) {
return 4; return 4;
} }
} }
#ifdef UNITTEST
#include <assert.h>
#include <string.h>
#include <stdio.h>
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

@ -9,3 +9,13 @@ size_t fmt_ulong(char *dest,unsigned long i) {
*--dest = (char)((tmp%10)+'0'); *--dest = (char)((tmp%10)+'0');
return len; return len;
} }
#ifdef UNITTEST
#include <assert.h>
#include <string.h>
int main() {
char buf[100];
assert(fmt_ulong(buf,12345)==5 && !memcmp(buf,"12345",5));
}
#endif

@ -16,3 +16,13 @@ size_t fmt_xlong(char *dest,unsigned long i) {
} }
return len; return len;
} }
#ifdef UNITTEST
#include <assert.h>
#include <string.h>
int main() {
char buf[100];
assert(fmt_xlong(buf,0x12345)==5 && !memcmp(buf,"12345",5));
}
#endif

@ -49,5 +49,6 @@ int main() {
size_t i,l; 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)==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("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 #endif

@ -72,6 +72,7 @@ abort:
#ifdef UNITTEST #ifdef UNITTEST
#include <assert.h> #include <assert.h>
#include <string.h>
int main() { int main() {
char buf[100]; char buf[100];

Loading…
Cancel
Save