add fmt_html_tagarg, fmt_xml

master
leitner 10 years ago
parent 7c5d119f9a
commit 0aa50a19a4

@ -16,6 +16,7 @@
switch epoll from level triggering to edge triggering
introduce io_eagain_read and io_eagain_write (discontinue using io_eagain plz)
fix buffer_get
add fmt_html_tagarg, fmt_xml
0.29:
save 8 bytes in taia.h for 64-bit systems

@ -27,8 +27,15 @@ size_t fmt_urlencoded2(char* dest,const char* src,size_t len,const char* escapem
size_t fmt_yenc(char* dest,const char* src,size_t len);
/* Needs len*2 bytes */
size_t fmt_hexdump(char* dest,const char* src,size_t len);
/* Change '<' to '&lt;' and '&' to '&amp;'; worst case: len*5 */
/* Change '<' to '&lt;' and '&' to '&amp;' and '\n' to '<br>'; worst case: len*5 */
/* This is meant for outputting text that goes between tags */
size_t fmt_html(char* dest,const char* src,size_t len);
/* Change '<' to '&lt;' and '&' to '&amp;' and '"' to '&quot;'; worst case: len*6 */
/* This is meant for outputting text that goes in a tag argument between double quotes*/
size_t fmt_html_tagarg(char* dest,const char* src,size_t len);
/* Change '<' to '&lt;' and '&' to '&amp;'; worst case: len*5 */
size_t fmt_xml(char* dest,const char* src,size_t len);
/* Change '\' to "\\", '\n' to "\n", ^A to "\x01" etc; worst case: len*4 */
size_t fmt_cescape(char* dest,const char* src,size_t len);
/* Worst case: len*4 */

@ -0,0 +1,26 @@
#include "fmt.h"
#include "textcode.h"
#include "str.h"
#include "haveinline.h"
size_t fmt_html_tagarg(char* dest,const char* src,size_t len) {
register const unsigned char* s=(const unsigned char*) src;
size_t written=0,i;
const char* seq;
for (i=0; i<len; ++i) {
switch (s[i]) {
case '&': seq="&amp;"; goto doit;
case '<': seq="&lt;"; goto doit;
case '>': seq="&gt;"; goto doit;
case '"':
seq="&dquot;";
doit:
written+=fmt_str(dest?dest+written:0,seq);
break;
default: if (dest) dest[written]=s[i]; ++written; break;
}
/* in case someone gives us malicious input */
if (written>((size_t)-1)/2) return (size_t)-1;
}
return written;
}

@ -0,0 +1,23 @@
#include "fmt.h"
#include "textcode.h"
#include "str.h"
#include "haveinline.h"
size_t fmt_xml(char* dest,const char* src,size_t len) {
register const unsigned char* s=(const unsigned char*) src;
size_t written=0,i;
const char* seq;
for (i=0; i<len; ++i) {
switch (s[i]) {
case '&': seq="&amp;"; goto doit;
case '<': seq="&lt;";
doit:
written+=fmt_str(dest?dest+written:0,seq);
break;
default: if (dest) dest[written]=s[i]; ++written; break;
}
/* in case someone gives us malicious input */
if (written>((size_t)-1)/2) return (size_t)-1;
}
return written;
}
Loading…
Cancel
Save