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.

165 lines
7.0 KiB

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