regex_block_lib.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2012,2013 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @author Bartlomiej Polot
  18. * @file regex/regex_block_lib.h
  19. * @brief common function to manipulate blocks stored by regex in the DHT
  20. */
  21. #ifndef REGEX_BLOCK_LIB_H_
  22. #define REGEX_BLOCK_LIB_H_
  23. #ifdef __cplusplus
  24. extern "C"
  25. {
  26. #if 0
  27. /* keep Emacsens' auto-indent happy */
  28. }
  29. #endif
  30. #endif
  31. #include "platform.h"
  32. #include "block_regex.h"
  33. /**
  34. * Representation of a Regex node (and edges) in the DHT.
  35. */
  36. struct RegexBlock;
  37. /**
  38. * Edge representation.
  39. */
  40. struct REGEX_BLOCK_Edge
  41. {
  42. /**
  43. * Label of the edge. FIXME: might want to not consume exactly
  44. * multiples of 8 bits, need length!
  45. */
  46. const char *label;
  47. /**
  48. * Destionation of the edge.
  49. */
  50. struct GNUNET_HashCode destination;
  51. };
  52. /**
  53. * Check if the given 'proof' matches the given 'key'.
  54. *
  55. * @param proof partial regex of a state
  56. * @param proof_len number of bytes in @a proof
  57. * @param key hash of a state.
  58. * @return #GNUNET_OK if the proof is valid for the given key.
  59. */
  60. int
  61. REGEX_BLOCK_check_proof (const char *proof,
  62. size_t proof_len,
  63. const struct GNUNET_HashCode *key);
  64. /**
  65. * Check if the regex block is well formed, including all edges.
  66. *
  67. * @param block The start of the block.
  68. * @param size The size of the @a block.
  69. * @param query the query for the @a block
  70. * @param xquery String describing the edge we are looking for.
  71. * Can be NULL in case this is a put block.
  72. * @return #GNUNET_OK in case it's fine.
  73. * #GNUNET_NO in case the xquery exists and is not found (IRRELEVANT).
  74. * #GNUNET_SYSERR if the block is invalid.
  75. */
  76. int
  77. REGEX_BLOCK_check (const struct RegexBlock *block,
  78. size_t size,
  79. const struct GNUNET_HashCode *query,
  80. const char *xquery);
  81. /* FIXME: might want to use 'struct REGEX_BLOCK_Edge' here instead of 3 arguments! */
  82. /**
  83. * Iterator over edges in a block.
  84. *
  85. * @param cls Closure.
  86. * @param token Token that follows to next state.
  87. * @param len Length of token.
  88. * @param key Hash of next state.
  89. * @return #GNUNET_YES if should keep iterating, #GNUNET_NO otherwise.
  90. */
  91. typedef int
  92. (*REGEX_INTERNAL_EgdeIterator)(void *cls,
  93. const char *token,
  94. size_t len,
  95. const struct GNUNET_HashCode *key);
  96. /**
  97. * Iterate over all edges of a block of a regex state.
  98. *
  99. * @param block Block to iterate over.
  100. * @param size Size of block.
  101. * @param iterator Function to call on each edge in the block.
  102. * @param iter_cls Closure for the @a iterator.
  103. * @return #GNUNET_SYSERR if an error has been encountered.
  104. * #GNUNET_OK if no error has been encountered.
  105. * Note that if the iterator stops the iteration by returning
  106. * #GNUNET_NO, the block will no longer be checked for further errors.
  107. * The return value will be #GNUNET_OK meaning that no errors were
  108. * found until the edge last notified to the iterator, but there might
  109. * be errors in further edges.
  110. */
  111. int
  112. REGEX_BLOCK_iterate (const struct RegexBlock *block,
  113. size_t size,
  114. REGEX_INTERNAL_EgdeIterator iterator,
  115. void *iter_cls);
  116. /**
  117. * Obtain the key that a particular block is to be stored under.
  118. *
  119. * @param block block to get the key from
  120. * @param block_len number of bytes in @a block
  121. * @param key where to store the key
  122. * @return #GNUNET_OK on success, #GNUNET_SYSERR if the block is malformed
  123. */
  124. int
  125. REGEX_BLOCK_get_key (const struct RegexBlock *block,
  126. size_t block_len,
  127. struct GNUNET_HashCode *key);
  128. /**
  129. * Test if this block is marked as being an accept state.
  130. *
  131. * @param block block to test
  132. * @param size number of bytes in block
  133. * @return #GNUNET_YES if the block is accepting, #GNUNET_NO if not
  134. */
  135. int
  136. GNUNET_BLOCK_is_accepting (const struct RegexBlock *block,
  137. size_t block_len);
  138. /**
  139. * Construct a regex block to be stored in the DHT.
  140. *
  141. * @param proof proof string for the block
  142. * @param num_edges number of edges in the block
  143. * @param edges the edges of the block
  144. * @param accepting is this an accepting state
  145. * @param rsize set to the size of the returned block (OUT-only)
  146. * @return the regex block, NULL on error
  147. */
  148. struct RegexBlock *
  149. REGEX_BLOCK_create (const char *proof,
  150. unsigned int num_edges,
  151. const struct REGEX_BLOCK_Edge *edges,
  152. int accepting,
  153. size_t *rsize);
  154. #if 0 /* keep Emacsens' auto-indent happy */
  155. {
  156. #endif
  157. #ifdef __cplusplus
  158. }
  159. #endif
  160. /* ifndef REGEX_BLOCK_LIB_H */
  161. #endif
  162. /* end of regex_block_lib.h */