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.

300 lines
11 KiB

21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
6 years ago
6 years ago
14 years ago
14 years ago
21 years ago
  1. /* this header file comes from libowfat, http://www.fefe.de/libowfat/ */
  2. #ifndef FMT_H
  3. #define FMT_H
  4. /* for size_t: */
  5. #include <stddef.h>
  6. /* for uint32_t */
  7. #include <stdint.h>
  8. /* for time_t: */
  9. #ifdef _WIN32
  10. #include <time.h>
  11. #else
  12. #include <sys/types.h>
  13. #endif
  14. /* for byte_copy */
  15. #include <libowfat/byte.h>
  16. #include <libowfat/compiler.h>
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. #define FMT_LONG 41 /* enough space to hold -2^127 in decimal, plus \0 */
  21. #define FMT_ULONG 40 /* enough space to hold 2^128 - 1 in decimal, plus \0 */
  22. #define FMT_8LONG 44 /* enough space to hold 2^128 - 1 in octal, plus \0 */
  23. #define FMT_XLONG 33 /* enough space to hold 2^128 - 1 in hexadecimal, plus \0 */
  24. #define FMT_LEN ((char *) 0) /* convenient abbreviation */
  25. /* This file declares routines for formatting strings and numbers in
  26. * various forms or encodings, either binary or text based.
  27. *
  28. * The first argument is always the destination buffer, the second
  29. * argument is always the value to be formatted/encoded. The return
  30. * value is always the number of bytes written in the output buffer.
  31. *
  32. * If you don't know how large the output will be, call the function
  33. * first with NULL as destination buffer and use the length it returns
  34. * to allocate the destination buffer.
  35. *
  36. * Note that none of these routines append a 0 terminator. If you need
  37. * 0 termination, call the functions like this:
  38. buf[fmt_ulong(buf,number)]=0;
  39. */
  40. /* convert signed src integer -23 to ASCII '-','2','3', return number of
  41. * bytes of value in output format (3 in this example).
  42. * If dest is not NULL, write result to dest */
  43. att_write(1)
  44. size_t fmt_long(char *dest,signed long src) noexcept;
  45. /* convert unsigned src integer 23 to ASCII '2','3', return number of
  46. * bytes of value in output format (2 in this example).
  47. * If dest is not NULL, write result to dest */
  48. att_write(1)
  49. size_t fmt_ulong(char *dest,unsigned long src) noexcept;
  50. /* convert unsigned src integer 0x23 to ASCII '2','3', return number of
  51. * bytes of value in output format (2 in this example).
  52. * If dest is not NULL, write result to dest */
  53. att_write(1)
  54. size_t fmt_xlong(char *dest,unsigned long src) noexcept;
  55. /* convert unsigned src integer 023 to ASCII '2','3', return number of
  56. * bytes of value in output format (2 in this example).
  57. * If dest is not NULL, write result to dest */
  58. att_write(1)
  59. size_t fmt_8long(char *dest,unsigned long src) noexcept;
  60. /* like fmt_long but for long long */
  61. att_write(1)
  62. size_t fmt_longlong(char *dest,signed long long src) noexcept;
  63. /* like fmt_ulong but for unsigned long long */
  64. att_write(1)
  65. size_t fmt_ulonglong(char *dest,unsigned long long src) noexcept;
  66. /* like fmt_xlong but for unsigned long long */
  67. att_write(1)
  68. size_t fmt_xlonglong(char *dest,unsigned long long src) noexcept;
  69. #define fmt_uint(dest,src) fmt_ulong(dest,src)
  70. #define fmt_int(dest,src) fmt_long(dest,src)
  71. #define fmt_xint(dest,src) fmt_xlong(dest,src)
  72. #define fmt_8int(dest,src) fmt_8long(dest,src)
  73. /* Like fmt_ulong, but prepend '0' while length is smaller than padto.
  74. * Does not truncate! */
  75. /* fmt_ulong0(buf,23,4) -> '0','0','2','3' return 4 */
  76. /* fmt_ulong0(buf,234,2) -> '2','3','4', return 3 */
  77. att_write(1)
  78. size_t fmt_ulong0(char *,unsigned long src,size_t padto) noexcept;
  79. #define fmt_uint0(buf,src,padto) fmt_ulong0(buf,src,padto)
  80. /* convert src double 1.7 to ASCII '1','.','7', return length.
  81. * If dest is not NULL, write result to dest */
  82. att_write(1)
  83. size_t fmt_double(char *dest, double d,int max,int prec) noexcept;
  84. /* if src is negative, write '-' and return 1.
  85. * if src is positive, write '+' and return 1.
  86. * otherwise return 0 */
  87. att_write(1)
  88. size_t fmt_plusminus(char *dest,int src) noexcept;
  89. /* if src is negative, write '-' and return 1.
  90. * otherwise return 0. */
  91. att_write(1)
  92. size_t fmt_minus(char *dest,int src) noexcept;
  93. /* copy str to dest until \0 byte, return number of copied bytes. */
  94. /* fmt_str(NULL,str) == strlen(str) */
  95. /* fmt_str(buf,str) == strcpy(buf,str), return strlen(str) */
  96. /* strcat(strcpy(buf,"foo"),"bar") can be written as
  97. * i=fmt_str(buf,"foo");
  98. * i+=fmt_str(buf+i,"bar");
  99. * buf[i]=0;
  100. * This is more efficient because strcat needs to scan the string to
  101. * find the end and append.
  102. */
  103. att_write(1)
  104. size_t fmt_str(char *dest,const char *src) noexcept;
  105. /* copy str to dest until \0 byte or limit bytes copied.
  106. * return number of copied bytes. */
  107. att_writen(1,3)
  108. att_readn(2,3)
  109. size_t fmt_strn(char *dest,const char *src,size_t limit) noexcept;
  110. /* copy n bytes from src to dest, return n */
  111. att_writen(1,3)
  112. static inline size_t fmt_copybytes(char* dest,const char* src,size_t n) {
  113. if (dest) byte_copy(dest,n,src);
  114. return n;
  115. }
  116. /* copy sizeof(src)-1 bytes from src to dest, return sizeof(src)-1 */
  117. /* this is for
  118. * fmt_copybytes_sizeof_minus1(dest, "\x01\x02\x03\x04");
  119. * since we are technically passing a string, sizeof will include the
  120. * finishing 0 byte which we neither need nor want */
  121. #define fmt_copybytes_sizeof_minus1(dest,src) fmt_copybytes(dest,src,sizeof(src)-1)
  122. /* "foo" -> " foo"
  123. * write padlen-srclen spaces, if that is >= 0. Then copy srclen
  124. * characters from src. Truncate only if total length is larger than
  125. * maxlen. Return number of characters written. */
  126. att_writen(1,5)
  127. att_readn(2,3)
  128. size_t fmt_pad(char* dest,const char* src,size_t srclen,size_t padlen,size_t maxlen) noexcept;
  129. /* "foo" -> "foo "
  130. * append padlen-srclen spaces after dest, if that is >= 0. Truncate
  131. * only if total length is larger than maxlen. Return number of
  132. * characters written. */
  133. att_writen(1,4)
  134. size_t fmt_fill(char* dest,size_t srclen,size_t padlen,size_t maxlen) noexcept;
  135. /* 1 -> "1", 4900 -> "4.9k", 2300000 -> "2.3M" */
  136. att_write(1)
  137. size_t fmt_human(char* dest,unsigned long long l) noexcept;
  138. /* 1 -> "1", 4900 -> "4.8k", 2300000 -> "2.2M" */
  139. att_write(1)
  140. size_t fmt_humank(char* dest,unsigned long long l) noexcept;
  141. /* "Sun, 06 Nov 1994 08:49:37 GMT" */
  142. att_write(1)
  143. size_t fmt_httpdate(char* dest,time_t t) noexcept; /* not marked pure because it calls gmtime */
  144. /* "2014-05-27T19:22:16.247Z" */
  145. att_write(1)
  146. size_t fmt_iso8601(char* dest,time_t t) noexcept;
  147. #define FMT_UTF8 5
  148. #define FMT_ASN1LENGTH 17 /* enough space to hold 2^128-1 */
  149. #define FMT_ASN1TAG 19 /* enough space to hold 2^128-1 */
  150. /* some variable length encodings for integers */
  151. att_write(1)
  152. size_t fmt_utf8(char* dest,uint32_t n) noexcept; /* can store 0-0x7fffffff */
  153. att_write(1)
  154. size_t fmt_utf8_scratch(char* dest,uint32_t n) noexcept; /* same, but is allowed to overwrite up to 7 additional bytes */
  155. att_write(1)
  156. size_t fmt_asn1derlength(char* dest,unsigned long long l) noexcept; /* 0-0x7f: 1 byte, above that 1+bytes_needed bytes */
  157. att_write(1)
  158. size_t fmt_asn1derlength_scratch(char* dest,unsigned long long l) noexcept; /* same, but is allowed to overwrite up to 7 additional bytes */
  159. att_write(1)
  160. size_t fmt_asn1dertag(char* dest,unsigned long long l) noexcept; /* 1 byte for each 7 bits; upper bit = more bytes coming */
  161. att_write(1)
  162. size_t fmt_asn1dertag_scratch(char* dest,unsigned long long l) noexcept;/* same, but is allowed to overwrite up to 7 additional bytes */
  163. /* Google Protocol Buffers, https://developers.google.com/protocol-buffers/docs/encoding */
  164. att_write(1)
  165. size_t fmt_varint(char* dest,unsigned long long l) noexcept; /* protocol buffers encoding; like asn1dertag but little endian */
  166. att_write(1)
  167. size_t fmt_pb_tag(char* dest,size_t fieldno,unsigned char type) noexcept; /* protocol buffer tag */
  168. att_write(1)
  169. size_t fmt_pb_type0_int(char* dest,unsigned long long l) noexcept; /* protocol buffers encoding: type 0 bool/enum/int32/uint32/int64/uint64 */
  170. att_write(1)
  171. size_t fmt_pb_type0_sint(char* dest,signed long long l) noexcept;/* protocol buffers encoding: type 0 sint32/sint64 */
  172. att_write(1)
  173. size_t fmt_pb_type1_double(char* dest,double d) noexcept; /* protocol buffers encoding: double (64-bit little endian blob) */
  174. att_write(1)
  175. size_t fmt_pb_type1_fixed64(char* dest,uint64_t l) noexcept; /* protocol buffers encoding: 64-bit little endian blob */
  176. /* fmt_pb_type2_string can return 0 if (s,l) is clearly invalid */
  177. att_write(1)
  178. size_t fmt_pb_type2_string(char* dest,const char* s,size_t l) noexcept; /* protocol buffers encoding: varint length + blob */
  179. att_write(1)
  180. size_t fmt_pb_type5_float(char* dest,float f) noexcept; /* protocol buffers encoding: float (32-bit little endian blob) */
  181. att_write(1)
  182. size_t fmt_pb_type5_fixed32(char* dest,uint32_t l) noexcept; /* protocol buffers encoding: 32-bit little endian blob */
  183. att_write(1)
  184. size_t fmt_pb_int(char* dest,size_t fieldno,unsigned long long l) noexcept;
  185. att_write(1)
  186. size_t fmt_pb_sint(char* dest,size_t fieldno,signed long long l) noexcept;
  187. att_write(1)
  188. size_t fmt_pb_double(char* dest,size_t fieldno,double d) noexcept;
  189. att_write(1)
  190. size_t fmt_pb_float(char* dest,size_t fieldno,float f) noexcept;
  191. att_writen(1,4)
  192. size_t fmt_pb_string(char* dest,size_t fieldno,const char* s,size_t l) noexcept;
  193. /* fmt_netstring can return 0 if (src,len) is clearly invalid */
  194. att_writen(1,3)
  195. size_t fmt_netstring(char* dest,const char* src,size_t len) noexcept;
  196. /* Marshaling helper functions.
  197. * Escape one character, no matter if it needs escaping or not.
  198. * The functions will reject characters that cannot be represented but
  199. * not characters that the standard says should never occur. The idea
  200. * is to make these functions useful for creating bad encoding for
  201. * penetration testing.
  202. * Depending on the escaping method, the input character (uint32_t, a
  203. * unicode codepoint) may be limited to 0x7f, 0xff or 0x10ffff. */
  204. /* XML escaping: '&' -> '&amp;', '<' -> '&lt;', 'ö' -> '&#xf6;' */
  205. att_write(1)
  206. size_t fmt_escapecharxml(char* dest,uint32_t ch) noexcept;
  207. /* HTML escaping is the same as XML escaping. */
  208. att_write(1)
  209. size_t fmt_escapecharhtml(char* dest,uint32_t ch) noexcept;
  210. /* JSON escaping: '\' -> '\\', '"' -> '\"', 'ö' -> '\u00f6' */
  211. att_write(1)
  212. size_t fmt_escapecharjson(char* dest,uint32_t ch) noexcept;
  213. /* MIME quoted-printable escaping: 'ö' -> '=f6', characters > 0xff not supported */
  214. att_write(1)
  215. size_t fmt_escapecharquotedprintable(char* dest,uint32_t ch) noexcept;
  216. /* MIME quoted-printable escaping with UTF-8: 'ö' -> '=c3=b6', characters > 0x7fffffff not supported */
  217. att_write(1)
  218. size_t fmt_escapecharquotedprintableutf8(char* dest,uint32_t ch) noexcept;
  219. /* C99 style escaping: '\' -> '\\', newline -> '\n', 0xc2 -> '\302' */
  220. att_write(1)
  221. size_t fmt_escapecharc(char* dest,uint32_t ch) noexcept;
  222. /* internal functions, may be independently useful */
  223. att_const
  224. char fmt_tohex(char c);
  225. #define fmt_strm(b,...) fmt_strm_internal(b,__VA_ARGS__,(char*)0)
  226. att_write(1)
  227. size_t fmt_strm_internal(char* dest,...) noexcept;
  228. #ifndef MAX_ALLOCA
  229. #define MAX_ALLOCA 100000
  230. #endif
  231. #define fmt_strm_alloca(a,...) ({ size_t len=fmt_strm((char*)0,a,__VA_ARGS__)+1; char* c=(len<MAX_ALLOCA?alloca(len):0); if (c) c[fmt_strm(c,a,__VA_ARGS__)]=0; c;})
  232. #define fmt_strm_malloc(a,...) ({ size_t len=fmt_strm((char*)0,a,__VA_ARGS__)+1; char* c=(len<MAX_ALLOCA?malloc(len):0); if (c) c[fmt_strm(c,a,__VA_ARGS__)]=0; c;})
  233. #ifdef __cplusplus
  234. }
  235. #endif
  236. #endif