add man page and unit tests for scan_base64url
This commit is contained in:
@@ -26,8 +26,8 @@ static const char* lookup(size_t ofs,const char* t) {
|
||||
}
|
||||
|
||||
enum htmlmode { /* <a href="http://example.com/"foo">libowfat<home</a> */
|
||||
OUTSIDE, /* ^^^^^^^^^^^^^^^^ -> `libowfat<home` */
|
||||
TAGARG, /* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -> `http://example.com/"foo´ */
|
||||
OUTSIDE, /* ^^^^^^^^^^^^^^^^ -> libowfat<home */
|
||||
TAGARG, /* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -> http://example.com/"foo */
|
||||
};
|
||||
|
||||
static size_t scan_html_inner(const char *src,char *dest,size_t *destlen,enum htmlmode mode) {
|
||||
@@ -42,49 +42,41 @@ static size_t scan_html_inner(const char *src,char *dest,size_t *destlen,enum ht
|
||||
size_t j;
|
||||
if ((s[i+2]&~32)=='X') {
|
||||
j=scan_xlong(src+i+3,&l);
|
||||
if (!j) j+=3;
|
||||
if (j) j+=3;
|
||||
} else {
|
||||
j=scan_ulong(src+i+2,&l);
|
||||
if (!j) j+=3;
|
||||
if (j) j+=2;
|
||||
}
|
||||
if (s[i+j]==';') {
|
||||
i+=j;
|
||||
written+=fmt_utf8(dest+written,l);
|
||||
written+=fmt_utf8(dest?dest+written:0,l);
|
||||
} else {
|
||||
dest[written++]='&';
|
||||
if (dest) dest[written]='&';
|
||||
++written;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
utf8=lookup(1,src+i+1);
|
||||
if (utf8) {
|
||||
size_t l=strlen(utf8);
|
||||
memcpy(dest+written,utf8,l);
|
||||
if (dest) memcpy(dest+written,utf8,l);
|
||||
written+=l;
|
||||
i+=2+str_chr(src+i+2,';');
|
||||
continue;
|
||||
} else
|
||||
dest[written]='&';
|
||||
if (dest) dest[written]='&';
|
||||
} else if (s[i]=='<') {
|
||||
if (mode == OUTSIDE) break;
|
||||
if (case_starts((const char*)s+i+1,"br>")) {
|
||||
dest[written]='\n';
|
||||
i+=3;
|
||||
} else if (case_starts((const char*)s+i+1,"p>")) {
|
||||
dest[written]='\n'; ++written;
|
||||
dest[written]='\n';
|
||||
i+=3;
|
||||
} else
|
||||
dest[written]=s[i];
|
||||
break;
|
||||
} else if (s[i]=='"' && mode==TAGARG) {
|
||||
if (i==0) { dq=1; continue; }
|
||||
break;
|
||||
} else if (mode==TAGARG && !dq && (s[i]==' ' || s[i]=='\t' || s[i]=='\n'))
|
||||
break;
|
||||
else
|
||||
dest[written]=s[i];
|
||||
if (dest) dest[written]=s[i];
|
||||
++written;
|
||||
}
|
||||
*destlen=written;
|
||||
if (destlen) *destlen=written;
|
||||
return i;
|
||||
}
|
||||
|
||||
@@ -98,13 +90,40 @@ size_t scan_html(const char *src,char *dest,size_t *destlen) {
|
||||
|
||||
#ifdef UNITTEST
|
||||
#include <assert.h>
|
||||
#undef UNITTEST
|
||||
#include <scan/scan_fromhex.c>
|
||||
#include <scan/scan_xlongn.c>
|
||||
#include <scan/scan_xlong.c>
|
||||
#include <scan/scan_ulongn.c>
|
||||
#include <scan/scan_ulong.c>
|
||||
#include <str/str_chr.c>
|
||||
#include <fmt/fmt_utf8.c>
|
||||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
char* html="<a href=\"http://example.com/"foo\">libowfat<home</a>";
|
||||
char buf[100];
|
||||
size_t destlen;
|
||||
/* check that we stop at < */
|
||||
assert(scan_html(html,buf,&destlen)==0 && destlen==0);
|
||||
assert(scan_html(strchr(html,'>')+1,buf,&destlen)==16 && destlen==13 && !memcmp(buf,"libowfat<home",13));
|
||||
assert(scan_html_tagarg(strchr(html,'"')+1,buf,&destlen)==28 && destlen==23 && !memcmp(buf,"http://example.com/\"foo",23));
|
||||
/* check that we properly decode < */
|
||||
memset(buf,'?',sizeof(buf));
|
||||
assert(scan_html(strchr(html,'>')+1,buf,&destlen)==16 && destlen==13 && !memcmp(buf,"libowfat<home?",14));
|
||||
/* check that we stop at " and properly decode " */
|
||||
memset(buf,'?',sizeof(buf));
|
||||
assert(scan_html_tagarg(strchr(html,'"')+1,buf,&destlen)==28 && destlen==23 && !memcmp(buf,"http://example.com/\"foo?",24));
|
||||
/* check that we pass through invalid escapes */
|
||||
memset(buf,'?',sizeof(buf));
|
||||
assert(scan_html("&fnord;",buf,&destlen)==7 && destlen==7 && !memcmp(buf,"&fnord;?",8));
|
||||
memset(buf,'?',sizeof(buf));
|
||||
assert(scan_html("&#x;",buf,&destlen)==4 && destlen==4 && !memcmp(buf,"&#x;?",5));
|
||||
memset(buf,'?',sizeof(buf));
|
||||
assert(scan_html("&#;",buf,&destlen)==3 && destlen==3 && !memcmp(buf,"&#;?",4));
|
||||
/* check that &#x[hex]; is decoded properly */
|
||||
memset(buf,'?',sizeof(buf));
|
||||
assert(scan_html("",buf,&destlen)==5 && destlen==1 && buf[0]==1 && buf[1]=='?');
|
||||
/* check that &#[decimal]; is decoded properly */
|
||||
memset(buf,'?',sizeof(buf));
|
||||
assert(scan_html("",buf,&destlen)==4 && destlen==1 && buf[0]==1 && buf[1]=='?');
|
||||
}
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user