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.
 
 
 
 

132 lines
5.9 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. /* Needs len/3*4 bytes */
  11. size_t fmt_uuencoded(char* dest,const char* src,size_t len);
  12. /* Needs len/3*4 bytes */
  13. size_t fmt_base64(char* dest,const char* src,size_t len);
  14. /* Worst case: len*3 */
  15. size_t fmt_quotedprintable(char* dest,const char* src,size_t len);
  16. /* Worst case: len*3 */
  17. size_t fmt_quotedprintable2(char* dest,const char* src,size_t len,const char* escapeme);
  18. /* Worst case: len*3 */
  19. size_t fmt_urlencoded(char* dest,const char* src,size_t len);
  20. /* Worst case: len*3 */
  21. size_t fmt_urlencoded2(char* dest,const char* src,size_t len,const char* escapeme);
  22. /* Worst case: len*2 */
  23. size_t fmt_yenc(char* dest,const char* src,size_t len);
  24. /* Needs len*2 bytes */
  25. size_t fmt_hexdump(char* dest,const char* src,size_t len);
  26. /* Change '<' to '&lt;' and '&' to '&amp;'; worst case: len*5 */
  27. size_t fmt_html(char* dest,const char* src,size_t len);
  28. /* Change '\' to "\\", '\n' to "\n", ^A to "\x01" etc; worst case: len*4 */
  29. size_t fmt_cescape(char* dest,const char* src,size_t len);
  30. /* Worst case: len*4 */
  31. size_t fmt_cescape2(char* dest,const char* src,size_t len,const char* escapeme);
  32. /* Fold whitespace to '_'; this is great for writing fields with
  33. * white spaces to a log file and still allow awk to do log analysis */
  34. /* Worst case: same size */
  35. size_t fmt_foldwhitespace(char* dest,const char* src,size_t len);
  36. /* Worst case: len*3 */
  37. size_t fmt_ldapescape(char* dest,const char* src,size_t len);
  38. size_t fmt_ldapescape2(char* dest,const char* src,size_t len,const char* escapeme);
  39. /* Encode JSON string from UTF-8; will backslash-escape the bare minimum.
  40. * Will not verify that the input is valid UTF-8!
  41. * Worst case: len*6 */
  42. size_t fmt_jsonescape(char* dest,const char* src,size_t len);
  43. /* These read one line from src, decoded it, and write the result to
  44. * dest. The number of decoded bytes is written to destlen. dest
  45. * should be able to hold strlen(src) bytes as a rule of thumb. */
  46. size_t scan_uuencoded(const char *src,char *dest,size_t *destlen);
  47. size_t scan_base64(const char *src,char *dest,size_t *destlen);
  48. size_t scan_quotedprintable(const char *src,char *dest,size_t *destlen);
  49. size_t scan_urlencoded(const char *src,char *dest,size_t *destlen);
  50. size_t scan_urlencoded2(const char *src,char *dest,size_t *destlen);
  51. size_t scan_yenc(const char *src,char *dest,size_t *destlen);
  52. size_t scan_hexdump(const char *src,char *dest,size_t *destlen);
  53. size_t scan_html(const char *src,char *dest,size_t *destlen);
  54. size_t scan_cescape(const char *src,char *dest,size_t *destlen);
  55. size_t scan_ldapescape(const char* src,char* dest,size_t *destlen);
  56. size_t scan_jsonescape(const char* src,char* dest,size_t *destlen);
  57. #ifdef STRALLOC_H
  58. /* WARNING: these functions _append_ to the stralloc, not overwrite! */
  59. /* stralloc wrappers; return 1 on success, 0 on failure */
  60. /* arg 1 is one of the fmt_* functions from above */
  61. int fmt_to_sa(size_t (*func)(char*,const char*,size_t),
  62. stralloc* sa,const char* src,size_t len);
  63. int fmt_to_sa2(size_t (*func)(char*,const char*,size_t,const char*),
  64. stralloc* sa,const char* src,size_t len,const char* escapeme);
  65. /* arg 1 is one of the scan_* functions from above */
  66. /* return number of bytes scanned */
  67. size_t scan_to_sa(size_t (*func)(const char*,char*,size_t*),
  68. const char* src,stralloc* sa);
  69. #define fmt_uuencoded_sa(sa,src,len) fmt_to_sa(fmt_uuencoded,sa,src,len)
  70. #define fmt_base64_sa(sa,src,len) fmt_to_sa(fmt_base64,sa,src,len)
  71. #define fmt_quotedprintable_sa(sa,src,len) fmt_to_sa(fmt_quotedprintable,sa,src,len)
  72. #define fmt_urlencoded_sa(sa,src,len) fmt_to_sa(fmt_urlencoded,sa,src,len)
  73. #define fmt_yenc_sa(sa,src,len) fmt_to_sa(fmt_yenc,sa,src,len)
  74. #define fmt_hexdump_sa(sa,src,len) fmt_to_sa(fmt_hexdump,sa,src,len)
  75. #define fmt_html_sa(sa,src,len) fmt_to_sa(fmt_html,sa,src,len)
  76. #define fmt_cescape_sa(sa,src,len) fmt_to_sa(fmt_cescape,sa,src,len)
  77. #define fmt_ldapescape_sa(sa,src,len) fmt_to_sa(fmt_ldapescape,sa,src,len)
  78. #define fmt_jsonescape_sa(sa,src,len) fmt_to_sa(fmt_jsonescape,sa,src,len)
  79. #define fmt_quotedprintable2_sa(sa,src,len,escapeme) fmt_to_sa2(fmt_quotedprintable2,sa,src,len,escapeme)
  80. #define fmt_urlencoded2_sa(sa,src,len,escapeme) fmt_to_sa2(fmt_urlencoded2,sa,src,len,escapeme)
  81. #define fmt_cescape2_sa(sa,src,len,escapeme) fmt_to_sa2(fmt_cescape2,sa,src,len,escapeme)
  82. #define scan_uuencoded_sa(src,sa) scan_to_sa(scan_uuencoded,src,sa)
  83. #define scan_base64_sa(src,sa) scan_to_sa(scan_base64,src,sa)
  84. #define scan_quotedprintable_sa(src,sa) scan_to_sa(scan_quotedprintable,src,sa)
  85. #define scan_urlencoded_sa(src,sa) scan_to_sa(scan_urlencoded,src,sa)
  86. #define scan_yenc_sa(src,sa) scan_to_sa(scan_yenc,src,sa)
  87. #define scan_hexdump_sa(src,sa) scan_to_sa(scan_hexdump,src,sa)
  88. #define scan_html_sa(src,sa) scan_to_sa(scan_html,src,sa)
  89. #define scan_cescape_sa(src,sa) scan_to_sa(scan_cescape,src,sa)
  90. #define scan_ldapescape_sa(src,sa) scan_to_sa(scan_ldapescape,src,sa)
  91. #define scan_jsonescape_sa(src,sa) scan_to_sa(scan_jsonescape,src,sa)
  92. #endif
  93. #ifdef ARRAY_H
  94. void fmt_to_array(size_t (*func)(char*,const char*,size_t),
  95. array* a,const char* src,size_t len);
  96. void fmt_tofrom_array(size_t (*func)(char*,const char*,size_t),
  97. array* dest,array* src);
  98. void fmt_to_array2(size_t (*func)(char*,const char*,size_t,const char*),
  99. array* a,const char* src,size_t len,const char* escapeme);
  100. void fmt_tofrom_array2(size_t (*func)(char*,const char*,size_t,const char*),
  101. array* dest,array* src,const char* escapeme);
  102. size_t scan_to_array(size_t (*func)(const char*,char*,size_t*),
  103. const char* src,array* dest);
  104. size_t scan_tofrom_array(size_t (*func)(const char*,char*,size_t*),
  105. array* src,array* dest);
  106. #endif
  107. extern const char base64[64];
  108. #ifdef __cplusplus
  109. }
  110. #endif
  111. #endif