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.
 
 
 
 

182 lines
7.8 KiB

  1. /* this header file comes from libowfat, http://www.fefe.de/libowfat/ */
  2. #ifndef TEXTCODE_H
  3. #define TEXTCODE_H
  4. #include <stddef.h>
  5. #ifdef __cplusplus
  6. extern "C" {
  7. #endif
  8. /* These take len bytes from src and write them in encoded form to
  9. * dest (if dest != NULL), returning the number of bytes written. */
  10. /* dietlibc defines these in sys/cdefs.h, which is included from stddef.h */
  11. #ifndef __writememsz__
  12. #define __readmemsz__(a,b)
  13. #endif
  14. /* Needs len/3*4 bytes */
  15. __readmemsz__(2,3)
  16. size_t fmt_uuencoded(char* dest,const char* src,size_t len);
  17. /* Needs len/3*4 bytes */
  18. __readmemsz__(2,3)
  19. size_t fmt_base64(char* dest,const char* src,size_t len);
  20. __readmemsz__(2,3)
  21. size_t fmt_base64url(char* dest,const char* src,size_t len);
  22. /* Worst case: len*3 */
  23. __readmemsz__(2,3)
  24. size_t fmt_quotedprintable(char* dest,const char* src,size_t len);
  25. /* Worst case: len*3 */
  26. __readmemsz__(2,3)
  27. size_t fmt_quotedprintable2(char* dest,const char* src,size_t len,const char* escapeme);
  28. /* Worst case: len*3 */
  29. __readmemsz__(2,3)
  30. size_t fmt_urlencoded(char* dest,const char* src,size_t len);
  31. /* Worst case: len*3 */
  32. __readmemsz__(2,3)
  33. size_t fmt_urlencoded2(char* dest,const char* src,size_t len,const char* escapeme);
  34. /* Worst case: len*2 */
  35. __readmemsz__(2,3)
  36. size_t fmt_yenc(char* dest,const char* src,size_t len);
  37. /* Needs len*2 bytes */
  38. __readmemsz__(2,3)
  39. size_t fmt_hexdump(char* dest,const char* src,size_t len);
  40. /* Change '<' to '&lt;' and '&' to '&amp;' and '\n' to '<br>'; worst case: len*5 */
  41. /* This is meant for outputting text that goes between tags */
  42. __readmemsz__(2,3)
  43. size_t fmt_html(char* dest,const char* src,size_t len);
  44. /* Change '<' to '&lt;' and '&' to '&amp;' and '"' to '&quot;'; worst case: len*6 */
  45. /* This is meant for outputting text that goes in a tag argument between double quotes*/
  46. __readmemsz__(2,3)
  47. size_t fmt_html_tagarg(char* dest,const char* src,size_t len);
  48. /* Change '<' to '&lt;' and '&' to '&amp;'; worst case: len*5 */
  49. __readmemsz__(2,3)
  50. size_t fmt_xml(char* dest,const char* src,size_t len);
  51. /* Change '\' to "\\", '\n' to "\n", ^A to "\x01" etc; worst case: len*4 */
  52. __readmemsz__(2,3)
  53. size_t fmt_cescape(char* dest,const char* src,size_t len);
  54. /* Worst case: len*4 */
  55. __readmemsz__(2,3)
  56. size_t fmt_cescape2(char* dest,const char* src,size_t len,const char* escapeme);
  57. /* Fold whitespace to '_'; this is great for writing fields with
  58. * white spaces to a log file and still allow awk to do log analysis */
  59. /* Worst case: same size */
  60. __readmemsz__(2,3)
  61. size_t fmt_foldwhitespace(char* dest,const char* src,size_t len);
  62. /* Worst case: len*3 */
  63. __readmemsz__(2,3)
  64. size_t fmt_ldapescape(char* dest,const char* src,size_t len);
  65. __readmemsz__(2,3)
  66. size_t fmt_ldapescape2(char* dest,const char* src,size_t len,const char* escapeme);
  67. /* Encode JSON string from UTF-8; will backslash-escape the bare minimum.
  68. * Invalid UTF-8 in input will output as valid UTF-8 for each byte
  69. * Worst case: len*6 */
  70. __readmemsz__(2,3)
  71. size_t fmt_jsonescape(char* dest,const char* src,size_t len);
  72. __readmemsz__(2,3)
  73. size_t fmt_base85(char* dest,const char* src,size_t len);
  74. /* These read one line from src, decode it, and write the result to
  75. * dest. The number of decoded bytes is written to destlen. dest
  76. * should be able to hold strlen(src) bytes as a rule of thumb. */
  77. size_t scan_uuencoded(const char* src,char* dest,size_t* destlen);
  78. size_t scan_base64(const char* src,char* dest,size_t* destlen);
  79. size_t scan_base64url(const char* src,char* dest,size_t* destlen);
  80. size_t scan_quotedprintable(const char* src,char* dest,size_t* destlen);
  81. size_t scan_urlencoded(const char* src,char* dest,size_t* destlen);
  82. size_t scan_urlencoded2(const char* src,char* dest,size_t* destlen);
  83. size_t scan_yenc(const char* src,char* dest,size_t* destlen);
  84. size_t scan_hexdump(const char* src,char* dest,size_t* destlen);
  85. /* decodes all html5-standardized &foo; escapes, and also
  86. * "<br>" to "\n" and "<p>" to "\n\n", leaves the rest of the tags alone */
  87. size_t scan_html(const char* src,char* dest,size_t* destlen);
  88. /* decodes all html5-standardized &foo; escapes, but leaves all tags
  89. * alone */
  90. size_t scan_html_tagarg(const char* src,char* dest,size_t* destlen);
  91. /* Decodes escaped C string text, turning e.g. \n into newline */
  92. size_t scan_cescape(const char* src,char* dest,size_t* destlen);
  93. /* Decodes escaped LDIF text, turning e.g. \5C into \ */
  94. size_t scan_ldapescape(const char* src,char* dest,size_t* destlen);
  95. /* Decodes escaped JSON strings (like \"\r\n\u0013), turns escaped
  96. * surrogate pairs into UTF-8. Expects input to be valid UTF-8. Ends at
  97. * \0 or unescaped double quote. */
  98. size_t scan_jsonescape(const char* src,char* dest,size_t* destlen);
  99. size_t scan_base85(const char* src,char* dest,size_t* destlen);
  100. #ifdef STRALLOC_H
  101. /* WARNING: these functions _append_ to the stralloc, not overwrite! */
  102. /* stralloc wrappers; return 1 on success, 0 on failure */
  103. /* arg 1 is one of the fmt_* functions from above */
  104. int fmt_to_sa(size_t (*func)(char*,const char*,size_t),
  105. stralloc* sa,const char* src,size_t len);
  106. int fmt_to_sa2(size_t (*func)(char*,const char*,size_t,const char*),
  107. stralloc* sa,const char* src,size_t len,const char* escapeme);
  108. /* arg 1 is one of the scan_* functions from above */
  109. /* return number of bytes scanned */
  110. size_t scan_to_sa(size_t (*func)(const char*,char*,size_t*),
  111. const char* src,stralloc* sa);
  112. #define fmt_uuencoded_sa(sa,src,len) fmt_to_sa(fmt_uuencoded,sa,src,len)
  113. #define fmt_base64_sa(sa,src,len) fmt_to_sa(fmt_base64,sa,src,len)
  114. #define fmt_base64url_sa(sa,src,len) fmt_to_sa(fmt_base64url,sa,src,len)
  115. #define fmt_quotedprintable_sa(sa,src,len) fmt_to_sa(fmt_quotedprintable,sa,src,len)
  116. #define fmt_urlencoded_sa(sa,src,len) fmt_to_sa(fmt_urlencoded,sa,src,len)
  117. #define fmt_yenc_sa(sa,src,len) fmt_to_sa(fmt_yenc,sa,src,len)
  118. #define fmt_hexdump_sa(sa,src,len) fmt_to_sa(fmt_hexdump,sa,src,len)
  119. #define fmt_html_sa(sa,src,len) fmt_to_sa(fmt_html,sa,src,len)
  120. #define fmt_cescape_sa(sa,src,len) fmt_to_sa(fmt_cescape,sa,src,len)
  121. #define fmt_ldapescape_sa(sa,src,len) fmt_to_sa(fmt_ldapescape,sa,src,len)
  122. #define fmt_jsonescape_sa(sa,src,len) fmt_to_sa(fmt_jsonescape,sa,src,len)
  123. #define fmt_quotedprintable2_sa(sa,src,len,escapeme) fmt_to_sa2(fmt_quotedprintable2,sa,src,len,escapeme)
  124. #define fmt_urlencoded2_sa(sa,src,len,escapeme) fmt_to_sa2(fmt_urlencoded2,sa,src,len,escapeme)
  125. #define fmt_cescape2_sa(sa,src,len,escapeme) fmt_to_sa2(fmt_cescape2,sa,src,len,escapeme)
  126. #define scan_uuencoded_sa(src,sa) scan_to_sa(scan_uuencoded,src,sa)
  127. #define scan_base64_sa(src,sa) scan_to_sa(scan_base64,src,sa)
  128. #define scan_base64url_sa(src,sa) scan_to_sa(scan_base64url,src,sa)
  129. #define scan_quotedprintable_sa(src,sa) scan_to_sa(scan_quotedprintable,src,sa)
  130. #define scan_urlencoded_sa(src,sa) scan_to_sa(scan_urlencoded,src,sa)
  131. #define scan_yenc_sa(src,sa) scan_to_sa(scan_yenc,src,sa)
  132. #define scan_hexdump_sa(src,sa) scan_to_sa(scan_hexdump,src,sa)
  133. #define scan_html_sa(src,sa) scan_to_sa(scan_html,src,sa)
  134. #define scan_cescape_sa(src,sa) scan_to_sa(scan_cescape,src,sa)
  135. #define scan_ldapescape_sa(src,sa) scan_to_sa(scan_ldapescape,src,sa)
  136. #define scan_jsonescape_sa(src,sa) scan_to_sa(scan_jsonescape,src,sa)
  137. #endif
  138. #ifdef ARRAY_H
  139. void fmt_to_array(size_t (*func)(char*,const char*,size_t),
  140. array* a,const char* src,size_t len);
  141. void fmt_tofrom_array(size_t (*func)(char*,const char*,size_t),
  142. array* dest,array* src);
  143. void fmt_to_array2(size_t (*func)(char*,const char*,size_t,const char*),
  144. array* a,const char* src,size_t len,const char* escapeme);
  145. void fmt_tofrom_array2(size_t (*func)(char*,const char*,size_t,const char*),
  146. array* dest,array* src,const char* escapeme);
  147. size_t scan_to_array(size_t (*func)(const char*,char*,size_t*),
  148. const char* src,array* dest);
  149. size_t scan_tofrom_array(size_t (*func)(const char*,char*,size_t*),
  150. array* src,array* dest);
  151. #endif
  152. extern const char base64[64];
  153. extern const char base64url[64];
  154. #ifdef __cplusplus
  155. }
  156. #endif
  157. #endif