Browse Source

add fmt_html_tagarg, fmt_xml

master
Felix von Leitner 7 years ago
parent
commit
3f0fa50edc
  1. 1
      CHANGES
  2. 9
      textcode.h
  3. 26
      textcode/fmt_html_tagarg.c
  4. 23
      textcode/fmt_xml.c

1
CHANGES

@ -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

9
textcode.h

@ -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 */

26
textcode/fmt_html_tagarg.c

@ -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;
}

23
textcode/fmt_xml.c

@ -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