regex_block_lib.h 5.2 KB

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