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.4 KiB

  1. /* this header file comes from libowfat, http://www.fefe.de/libowfat/ */
  2. #ifndef SCAN_H
  3. #define SCAN_H
  4. /* for size_t: */
  5. #include <stddef.h>
  6. /* for uint32_t: */
  7. #include <stdint.h>
  8. /* for time_t: */
  9. #include <sys/types.h>
  10. /* for struct timespec: */
  11. #include <time.h>
  12. #ifdef __cplusplus
  13. extern "C" {
  14. #endif
  15. /* dietlibc defines these in sys/cdefs.h, which is included from stddef.h */
  16. #ifndef __pure__
  17. #define __pure__
  18. #endif
  19. #ifndef __writememsz__
  20. #define __writememsz__(a,b)
  21. #define __readmemsz__(a,b)
  22. #endif
  23. /* This file declared functions used to decode / scan / unmarshal
  24. * integer or string values from a buffer.
  25. * The first argument is always the source buffer, the second argument
  26. * is a pointer to the destination (where to store the result). The
  27. * return value is number of bytes scanned successfully. */
  28. /* Interpret src as ASCII decimal number, write number to dest and
  29. * return the number of bytes that were parsed.
  30. * scan_ulong("23",&i) -> i=23, return 2
  31. * NB: leading + or - or space not accepted:
  32. * scan_ulong("+23",&i) -> return 0
  33. * scan_ulong("-23",&i) -> return 0
  34. * scan_ulong(" 23",&i) -> return 0
  35. * scan_ulong("23,42",&i) -> i=23, return 2
  36. * NB: 023 for octal or 0x23 for hex are not supported!
  37. * scan_ulong("0x23",&i) -> i=0, return 1
  38. * NB: will detect integer overflow and abort on excessively large
  39. * values, i.e. on a 32-bit system:
  40. * scan_ulong("4294967296",&i" -> i=429496729, return 9 */
  41. size_t scan_ulong(const char *src,unsigned long *dest);
  42. __readmemsz__(1,2)
  43. size_t scan_ulongn(const char* src,size_t n,unsigned long* dest);
  44. /* Interpret src as ASCII hexadecimal number, write number to dest and
  45. * return the number of bytes that were parsed.
  46. * Note: leading '+' or '-' not accepted! */
  47. size_t scan_xlong(const char *src,unsigned long *dest);
  48. __readmemsz__(1,2)
  49. size_t scan_xlongn(const char *src,size_t n,unsigned long *dest);
  50. /* interpret src as ASCII octal number, write number to dest and
  51. * return the number of bytes that were parsed.
  52. * Note: leading '+' or '-' not accepted! */
  53. size_t scan_8long(const char *src,unsigned long *dest);
  54. __readmemsz__(1,2)
  55. size_t scan_8longn(const char *src,size_t n,unsigned long *dest);
  56. /* interpret src as signed ASCII decimal number, write number to dest
  57. * and return the number of bytes that were parsed.
  58. * Note: leading spaces not accepted! */
  59. size_t scan_long(const char *src,signed long *dest);
  60. __readmemsz__(1,2)
  61. size_t scan_longn(const char *src,size_t n,signed long *dest);
  62. size_t scan_longlong(const char *src,signed long long *dest);
  63. size_t scan_ulonglong(const char *src,unsigned long long *dest);
  64. size_t scan_xlonglong(const char *src,unsigned long long *dest);
  65. size_t scan_8longlong(const char *src,unsigned long long *dest);
  66. size_t scan_uint(const char *src,unsigned int *dest);
  67. size_t scan_xint(const char *src,unsigned int *dest);
  68. size_t scan_8int(const char *src,unsigned int *dest);
  69. size_t scan_int(const char *src,signed int *dest);
  70. size_t scan_ushort(const char *src,unsigned short *dest);
  71. size_t scan_xshort(const char *src,unsigned short *dest);
  72. size_t scan_8short(const char *src,unsigned short *dest);
  73. size_t scan_short(const char *src,signed short *dest);
  74. /* interpret src as double precision floating point number,
  75. * write number to dest and return the number of bytes that were parsed */
  76. size_t scan_double(const char *in, double *dest);
  77. /* if *src=='-', set *dest to -1 and return 1.
  78. * if *src=='+', set *dest to 1 and return 1.
  79. * otherwise set *dest to 1 return 0. */
  80. size_t scan_plusminus(const char *src,signed int *dest);
  81. /* return the highest integer n<=limit so that isspace(in[i]) for all 0<=i<=n */
  82. __readmemsz__(1,2)
  83. size_t scan_whitenskip(const char *in,size_t limit) __pure__;
  84. /* return the highest integer n<=limit so that !isspace(in[i]) for all 0<=i<=n */
  85. __readmemsz__(1,2)
  86. size_t scan_nonwhitenskip(const char *in,size_t limit) __pure__;
  87. /* return the highest integer n<=limit so that in[i] is element of
  88. * charset (ASCIIZ string) for all 0<=i<=n */
  89. __readmemsz__(1,3)
  90. size_t scan_charsetnskip(const char *in,const char *charset,size_t limit) __pure__;
  91. /* return the highest integer n<=limit so that in[i] is not element of
  92. * charset (ASCIIZ string) for all 0<=i<=n */
  93. __readmemsz__(1,3)
  94. size_t scan_noncharsetnskip(const char *in,const char *charset,size_t limit) __pure__;
  95. /* try to parse ASCII GMT date; does not understand time zones. */
  96. /* example dates:
  97. * "Sun, 06 Nov 1994 08:49:37 GMT"
  98. * "Sunday, 06-Nov-94 08:49:37 GMT"
  99. * "Sun Nov 6 08:49:37 1994"
  100. */
  101. size_t scan_httpdate(const char *in,time_t *t) __pure__;
  102. /* try to parse ASCII ISO-8601 date; does not understand time zones. */
  103. /* example date: "2014-05-27T19:22:16Z" */
  104. size_t scan_iso8601(const char* in,struct timespec* t) __pure__;
  105. /* some variable length encodings for integers */
  106. size_t scan_utf8(const char* in,size_t len,uint32_t* n) __pure__;
  107. size_t scan_utf8_sem(const char* in,size_t len,uint32_t* n) __pure__;
  108. size_t scan_asn1derlength(const char* in,size_t len,unsigned long long* n) __pure__;
  109. size_t scan_asn1dertag(const char* in,size_t len,unsigned long long* n) __pure__;
  110. /* Google protocol buffers */
  111. /* A protocol buffer is a sequence of (tag,value).
  112. * Parse each tag with scan_pb_tag, then look at the field number to see
  113. * which field in your struct is being sent. Integers must have type
  114. * 0, double type 1, strings type 2 and floats type 5. However, you
  115. * have to check this yourself.
  116. */
  117. size_t scan_varint(const char* in,size_t len, unsigned long long* n) __pure__; /* internal */
  118. size_t scan_pb_tag(const char* in,size_t len, size_t* fieldno,unsigned char* type) __pure__;
  119. /* Then, depending on the field number, validate the type and call the
  120. * corresponding of these functions to parse the value */
  121. size_t scan_pb_type0_int(const char* in,size_t len,unsigned long long* l) __pure__;
  122. size_t scan_pb_type0_sint(const char* in,size_t len,signed long long* l) __pure__;
  123. size_t scan_pb_type1_double(const char* in,size_t len,double* d) __pure__;
  124. size_t scan_pb_type1_fixed64(const char* in,size_t len,uint64_t* b) __pure__;
  125. /* NOTE: scan_pb_type2_stringlen only parses the length of the string,
  126. * not the string itself. It will return the number of bytes parsed in
  127. * the length, then set slen to the value of the length integer it just
  128. * read, and let string point to the next byte (where the actual string
  129. * starts). To advance in the protocol buffer, you'll have to skip the
  130. * return value of this function + slen bytes.
  131. * This is done so you can detect too large strings and abort the
  132. * parsing early without having to read and allocate memory for the rest
  133. * (potentially gigabytes) of the data announced by one unreasonable
  134. * string length value. */
  135. size_t scan_pb_type2_stringlen(const char* in,size_t len,const char** string, size_t* slen) __pure__;
  136. size_t scan_pb_type5_float(const char* in,size_t len,float* f) __pure__;
  137. size_t scan_pb_type5_fixed32(const char* in,size_t len,uint32_t* b) __pure__;
  138. /* parse a netstring, input buffer is in (len bytes).
  139. * if parsing is successful:
  140. * *dest points to string and *slen is size of string
  141. * return number of bytes parsed
  142. * else
  143. * return 0
  144. * Note: *dest will point inside the input buffer!
  145. */
  146. __readmemsz__(1,2)
  147. size_t scan_netstring(const char* in,size_t len,char** dest,size_t* slen) __pure__;
  148. /* internal function that might be useful independently */
  149. /* convert from hex ASCII, return 0 to 15 for success or -1 for failure */
  150. int scan_fromhex(unsigned char c);
  151. #ifdef __cplusplus
  152. }
  153. #endif
  154. #endif