fs_tree.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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.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,
  63. uint64_t offset,
  64. unsigned int depth);
  65. /**
  66. * Context for an ECRS-based file encoder that computes
  67. * the Merkle-ish-CHK tree.
  68. */
  69. struct GNUNET_FS_TreeEncoder;
  70. /**
  71. * Function called asking for the current (encoded)
  72. * block to be processed. After processing the
  73. * client should either call "GNUNET_FS_tree_encode_next"
  74. * or (on error) "GNUNET_FS_tree_encode_finish".
  75. *
  76. * @param cls closure
  77. * @param chk content hash key for the block
  78. * @param offset offset of the block
  79. * @param depth depth of the block, 0 for DBLOCKs
  80. * @param type type of the block (IBLOCK or DBLOCK)
  81. * @param block the (encrypted) block
  82. * @param block_size size of block (in bytes)
  83. */
  84. typedef void (*GNUNET_FS_TreeBlockProcessor)(void *cls,
  85. const struct ContentHashKey *chk,
  86. uint64_t offset,
  87. unsigned int depth,
  88. enum GNUNET_BLOCK_Type type,
  89. const void *block,
  90. uint16_t block_size);
  91. /**
  92. * Function called with information about our
  93. * progress in computing the tree encoding.
  94. *
  95. * @param cls closure
  96. * @param offset where are we in the file
  97. * @param pt_block plaintext of the currently processed block
  98. * @param pt_size size of pt_block
  99. * @param depth depth of the block in the tree, 0 for DBLOCKS
  100. */
  101. typedef void (*GNUNET_FS_TreeProgressCallback)(void *cls,
  102. uint64_t offset,
  103. const void *pt_block,
  104. size_t pt_size,
  105. unsigned int depth);
  106. /**
  107. * Initialize a tree encoder. This function will call "proc" and
  108. * "progress" on each block in the tree. Once all blocks have been
  109. * processed, "cont" will be scheduled. The "reader" will be called
  110. * to obtain the (plaintext) blocks for the file. Note that this
  111. * function will actually never call "proc"; the "proc" function must
  112. * be triggered by calling "GNUNET_FS_tree_encoder_next" to trigger
  113. * encryption (and calling of "proc") for each block.
  114. *
  115. * @param h the global FS context
  116. * @param size overall size of the file to encode
  117. * @param cls closure for reader, proc, progress and cont
  118. * @param reader function to call to read plaintext data
  119. * @param proc function to call on each encrypted block
  120. * @param progress function to call with progress information
  121. * @param cont function to call when done
  122. * @return tree encoder context
  123. */
  124. struct GNUNET_FS_TreeEncoder *
  125. GNUNET_FS_tree_encoder_create (struct GNUNET_FS_Handle *h,
  126. uint64_t size,
  127. void *cls,
  128. GNUNET_FS_DataReader reader,
  129. GNUNET_FS_TreeBlockProcessor proc,
  130. GNUNET_FS_TreeProgressCallback progress,
  131. GNUNET_SCHEDULER_Task cont);
  132. /**
  133. * Encrypt the next block of the file (and
  134. * call proc and progress accordingly; or
  135. * of course "cont" if we have already completed
  136. * encoding of the entire file).
  137. *
  138. * @param te tree encoder to use
  139. */
  140. void GNUNET_FS_tree_encoder_next (struct GNUNET_FS_TreeEncoder * te);
  141. /**
  142. * Clean up a tree encoder and return information
  143. * about the resulting URI or an error message.
  144. *
  145. * @param te the tree encoder to clean up
  146. * @param uri set to the resulting URI (if encoding finished)
  147. * @param emsg set to an error message (if an error occured
  148. * within the tree encoder; if this function is called
  149. * prior to completion and prior to an internal error,
  150. * both "*uri" and "*emsg" will be set to NULL).
  151. */
  152. void GNUNET_FS_tree_encoder_finish (struct GNUNET_FS_TreeEncoder *te,
  153. struct GNUNET_FS_Uri **uri,
  154. char **emsg);
  155. #if 0
  156. /* the functions below will be needed for persistence
  157. but are not yet implemented -- FIXME... */
  158. /**
  159. * Get data that would be needed to resume
  160. * the encoding later.
  161. *
  162. * @param te encoding to resume
  163. * @param data set to the resume data
  164. * @param size set to the size of the resume data
  165. */
  166. void GNUNET_FS_tree_encoder_resume_get_data (const struct GNUNET_FS_TreeEncoder *te,
  167. void **data,
  168. size_t *size);
  169. /**
  170. * Reset tree encoder to point previously
  171. * obtained for resuming.
  172. *
  173. * @param te encoding to resume
  174. * @param data the resume data
  175. * @param size the size of the resume data
  176. */
  177. void GNUNET_FS_tree_encoder_resume (struct GNUNET_FS_TreeEncoder *te,
  178. const void *data,
  179. size_t size);
  180. #endif
  181. #endif
  182. /* end of fs_tree.h */