Mirror of :pserver:cvs@cvs.fefe.de:/cvs libowfat https://www.fefe.de/libowfat/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
1.1 KiB

7 years ago
  1. #include "fmt.h"
  2. size_t fmt_xmlescape(char* dest,uint32_t ch) {
  3. char* x;
  4. size_t n;
  5. /*
  6. From http://en.wikipedia.org/wiki/XML#Valid_characters
  7. Unicode code points in the following ranges are valid in XML 1.0 documents:
  8. U+0009, U+000A, U+000D: these are the only C0 controls accepted in XML 1.0;
  9. U+0020U+D7FF, U+E000U+FFFD: this excludes some (not all) non-characters in the BMP (all surrogates, U+FFFE and U+FFFF are forbidden);
  10. U+10000U+10FFFF: this includes all code points in supplementary planes, including non-characters.
  11. */
  12. if (ch==0 || (ch>=0xd780 && ch<=0xdfff) || ch==0xfffe || ch==0xffff || ch>0x10ffff) return 0;
  13. if ((ch&0x7f)<20 && ch!=9 && ch!=0xa && ch!=0xd && ch!=0x85) {
  14. char buf[6];
  15. buf[0]='&';
  16. buf[1]='#';
  17. buf[2]='x';
  18. n=3+fmt_xlong(buf+3,ch);
  19. buf[n++]=';';
  20. x=buf;
  21. } else
  22. switch (ch) {
  23. case '&':
  24. x="&amp;"; n=5;
  25. break;
  26. case '<':
  27. x="&lt;"; n=4;
  28. break;
  29. default:
  30. return fmt_utf8(dest,ch);
  31. }
  32. if (dest) {
  33. size_t i;
  34. for (i=0; i<n; ++i)
  35. dest[i]=x[i];
  36. }
  37. return n;
  38. }