fs_tree.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. This file is part of GNUnet.
  3. (C) 2009 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. * @file fs/fs_tree.h
  19. * @brief Merkle-tree-ish-CHK file encoding for GNUnet
  20. * @see https://gnunet.org/encoding
  21. * @author Krista Bennett
  22. * @author Christian Grothoff
  23. *
  24. * TODO:
  25. * - decide if this API should be made public (gnunet_fs_service.h)
  26. * or remain "internal" (but with exported symbols?)
  27. */
  28. #ifndef GNUNET_FS_TREE_H
  29. #define GNUNET_FS_TREE_H
  30. #include "fs_api.h"
  31. /**
  32. * Compute the depth of the CHK tree.
  33. *
  34. * @param flen file length for which to compute the depth
  35. * @return depth of the tree, always > 0. A depth of 1 means only a DBLOCK.
  36. */
  37. unsigned int
  38. GNUNET_FS_compute_depth (uint64_t flen);
  39. /**
  40. * Calculate how many bytes of payload a block tree of the given
  41. * depth MAY correspond to at most (this function ignores the fact that
  42. * some blocks will only be present partially due to the total file
  43. * size cutting some blocks off at the end).
  44. *
  45. * @param depth depth of the block. depth==0 is a DBLOCK.
  46. * @return number of bytes of payload a subtree of this depth may correspond to
  47. */
  48. uint64_t
  49. GNUNET_FS_tree_compute_tree_size (unsigned int depth);
  50. /**
  51. * Compute how many bytes of data should be stored in
  52. * the specified block.
  53. *
  54. * @param fsize overall file size, must be > 0.
  55. * @param offset offset in the original data corresponding
  56. * to the beginning of the tree induced by the block;
  57. * must be < fsize
  58. * @param depth depth of the node in the tree, 0 for DBLOCK
  59. * @return number of bytes stored in this node
  60. */
  61. size_t
  62. GNUNET_FS_tree_calculate_block_size (uint64_t fsize, uint64_t offset,
  63. unsigned int depth);
  64. /**
  65. * Context for an ECRS-based file encoder that computes
  66. * the Merkle-ish-CHK tree.
  67. */
  68. struct GNUNET_FS_TreeEncoder;
  69. /**
  70. * Function called asking for the current (encoded)
  71. * block to be processed. After processing the
  72. * client should either call "GNUNET_FS_tree_encode_next"
  73. * or (on error) "GNUNET_FS_tree_encode_finish".
  74. *
  75. * @param cls closure
  76. * @param chk content hash key for the block
  77. * @param offset offset of the block
  78. * @param depth depth of the block, 0 for DBLOCKs
  79. * @param type type of the block (IBLOCK or DBLOCK)
  80. * @param block the (encrypted) block
  81. * @param block_size size of block (in bytes)
  82. */
  83. typedef void (*GNUNET_FS_TreeBlockProcessor) (void *cls,
  84. const struct ContentHashKey * chk,
  85. uint64_t offset,
  86. unsigned int depth,
  87. enum GNUNET_BLOCK_Type type,
  88. const void *block,
  89. uint16_t block_size);
  90. /**
  91. * Function called with information about our
  92. * progress in computing the tree encoding.
  93. *
  94. * @param cls closure
  95. * @param offset where are we in the file
  96. * @param pt_block plaintext of the currently processed block
  97. * @param pt_size size of pt_block
  98. * @param depth depth of the block in the tree, 0 for DBLOCKS
  99. */
  100. typedef void (*GNUNET_FS_TreeProgressCallback) (void *cls, uint64_t offset,
  101. const void *pt_block,
  102. size_t pt_size,
  103. unsigned int depth);
  104. /**
  105. * Initialize a tree encoder. This function will call "proc" and
  106. * "progress" on each block in the tree. Once all blocks have been
  107. * processed, "cont" will be scheduled. The "reader" will be called
  108. * to obtain the (plaintext) blocks for the file. Note that this
  109. * function will actually never call "proc"; the "proc" function must
  110. * be triggered by calling "GNUNET_FS_tree_encoder_next" to trigger
  111. * encryption (and calling of "proc") for each block.
  112. *
  113. * @param h the global FS context
  114. * @param size overall size of the file to encode
  115. * @param cls closure for reader, proc, progress and cont
  116. * @param reader function to call to read plaintext data
  117. * @param proc function to call on each encrypted block
  118. * @param progress function to call with progress information
  119. * @param cont function to call when done
  120. * @return tree encoder context
  121. */
  122. struct GNUNET_FS_TreeEncoder *
  123. GNUNET_FS_tree_encoder_create (struct GNUNET_FS_Handle *h, uint64_t size,
  124. void *cls, GNUNET_FS_DataReader reader,
  125. GNUNET_FS_TreeBlockProcessor proc,
  126. GNUNET_FS_TreeProgressCallback progress,
  127. GNUNET_SCHEDULER_Task cont);
  128. /**
  129. * Encrypt the next block of the file (and
  130. * call proc and progress accordingly; or
  131. * of course "cont" if we have already completed
  132. * encoding of the entire file).
  133. *
  134. * @param te tree encoder to use
  135. */
  136. void
  137. GNUNET_FS_tree_encoder_next (struct GNUNET_FS_TreeEncoder *te);
  138. /**
  139. * Get the resulting URI from the encoding.
  140. *
  141. * @param te the tree encoder to clean up
  142. * @return uri set to the resulting URI (if encoding finished), NULL otherwise
  143. */
  144. struct GNUNET_FS_Uri *
  145. GNUNET_FS_tree_encoder_get_uri (struct GNUNET_FS_TreeEncoder *te);
  146. /**
  147. * Clean up a tree encoder and return information
  148. * about possible errors.
  149. *
  150. * @param te the tree encoder to clean up
  151. * @param emsg set to an error message (if an error occured
  152. * within the tree encoder; if this function is called
  153. * prior to completion and prior to an internal error,
  154. * both "*emsg" will be set to NULL).
  155. */
  156. void
  157. GNUNET_FS_tree_encoder_finish (struct GNUNET_FS_TreeEncoder *te,
  158. char **emsg);
  159. #if 0
  160. /* the functions below will be needed for persistence
  161. but are not yet implemented -- FIXME... */
  162. /**
  163. * Get data that would be needed to resume
  164. * the encoding later.
  165. *
  166. * @param te encoding to resume
  167. * @param data set to the resume data
  168. * @param size set to the size of the resume data
  169. */
  170. void
  171. GNUNET_FS_tree_encoder_resume_get_data (const struct GNUNET_FS_TreeEncoder *te,
  172. void **data, size_t * size);
  173. /**
  174. * Reset tree encoder to point previously
  175. * obtained for resuming.
  176. *
  177. * @param te encoding to resume
  178. * @param data the resume data
  179. * @param size the size of the resume data
  180. */
  181. void
  182. GNUNET_FS_tree_encoder_resume (struct GNUNET_FS_TreeEncoder *te,
  183. const void *data, size_t size);
  184. #endif
  185. #endif
  186. /* end of fs_tree.h */