2 This file is part of GNUnet.
3 (C) 2009 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
23 * @brief Merkle-tree-ish-CHK file encoding for GNUnet
24 * @see https://gnunet.org/encoding
25 * @author Krista Bennett
26 * @author Christian Grothoff
29 * - decide if this API should be made public (gnunet_fs_service.h)
30 * or remain "internal" (but with exported symbols?)
32 #ifndef GNUNET_FS_TREE_H
33 #define GNUNET_FS_TREE_H
38 * Compute the depth of the CHK tree.
40 * @param flen file length for which to compute the depth
41 * @return depth of the tree, always > 0. A depth of 1 means only a DBLOCK.
44 GNUNET_FS_compute_depth (uint64_t flen);
48 * Calculate how many bytes of payload a block tree of the given
49 * depth MAY correspond to at most (this function ignores the fact that
50 * some blocks will only be present partially due to the total file
51 * size cutting some blocks off at the end).
53 * @param depth depth of the block. depth==0 is a DBLOCK.
54 * @return number of bytes of payload a subtree of this depth may correspond to
57 GNUNET_FS_tree_compute_tree_size (unsigned int depth);
61 * Compute how many bytes of data should be stored in
62 * the specified block.
64 * @param fsize overall file size, must be > 0.
65 * @param offset offset in the original data corresponding
66 * to the beginning of the tree induced by the block;
68 * @param depth depth of the node in the tree, 0 for DBLOCK
69 * @return number of bytes stored in this node
72 GNUNET_FS_tree_calculate_block_size (uint64_t fsize,
78 * Context for an ECRS-based file encoder that computes
79 * the Merkle-ish-CHK tree.
81 struct GNUNET_FS_TreeEncoder;
85 * Function called asking for the current (encoded)
86 * block to be processed. After processing the
87 * client should either call "GNUNET_FS_tree_encode_next"
88 * or (on error) "GNUNET_FS_tree_encode_finish".
91 * @param chk content hash key for the block
92 * @param offset offset of the block
93 * @param depth depth of the block, 0 for DBLOCKs
94 * @param type type of the block (IBLOCK or DBLOCK)
95 * @param block the (encrypted) block
96 * @param block_size size of block (in bytes)
98 typedef void (*GNUNET_FS_TreeBlockProcessor)(void *cls,
99 const struct ContentHashKey *chk,
102 enum GNUNET_BLOCK_Type type,
104 uint16_t block_size);
108 * Function called with information about our
109 * progress in computing the tree encoding.
112 * @param offset where are we in the file
113 * @param pt_block plaintext of the currently processed block
114 * @param pt_size size of pt_block
115 * @param depth depth of the block in the tree, 0 for DBLOCKS
117 typedef void (*GNUNET_FS_TreeProgressCallback)(void *cls,
119 const void *pt_block,
125 * Initialize a tree encoder. This function will call "proc" and
126 * "progress" on each block in the tree. Once all blocks have been
127 * processed, "cont" will be scheduled. The "reader" will be called
128 * to obtain the (plaintext) blocks for the file. Note that this
129 * function will actually never call "proc"; the "proc" function must
130 * be triggered by calling "GNUNET_FS_tree_encoder_next" to trigger
131 * encryption (and calling of "proc") for each block.
133 * @param h the global FS context
134 * @param size overall size of the file to encode
135 * @param cls closure for reader, proc, progress and cont
136 * @param reader function to call to read plaintext data
137 * @param proc function to call on each encrypted block
138 * @param progress function to call with progress information
139 * @param cont function to call when done
140 * @return tree encoder context
142 struct GNUNET_FS_TreeEncoder *
143 GNUNET_FS_tree_encoder_create (struct GNUNET_FS_Handle *h,
146 GNUNET_FS_DataReader reader,
147 GNUNET_FS_TreeBlockProcessor proc,
148 GNUNET_FS_TreeProgressCallback progress,
149 GNUNET_SCHEDULER_Task cont);
153 * Encrypt the next block of the file (and
154 * call proc and progress accordingly; or
155 * of course "cont" if we have already completed
156 * encoding of the entire file).
158 * @param te tree encoder to use
160 void GNUNET_FS_tree_encoder_next (struct GNUNET_FS_TreeEncoder * te);
164 * Clean up a tree encoder and return information
165 * about the resulting URI or an error message.
167 * @param te the tree encoder to clean up
168 * @param uri set to the resulting URI (if encoding finished)
169 * @param emsg set to an error message (if an error occured
170 * within the tree encoder; if this function is called
171 * prior to completion and prior to an internal error,
172 * both "*uri" and "*emsg" will be set to NULL).
174 void GNUNET_FS_tree_encoder_finish (struct GNUNET_FS_TreeEncoder *te,
175 struct GNUNET_FS_Uri **uri,
180 /* the functions below will be needed for persistence
181 but are not yet implemented -- FIXME... */
183 * Get data that would be needed to resume
184 * the encoding later.
186 * @param te encoding to resume
187 * @param data set to the resume data
188 * @param size set to the size of the resume data
190 void GNUNET_FS_tree_encoder_resume_get_data (const struct GNUNET_FS_TreeEncoder *te,
196 * Reset tree encoder to point previously
197 * obtained for resuming.
199 * @param te encoding to resume
200 * @param data the resume data
201 * @param size the size of the resume data
203 void GNUNET_FS_tree_encoder_resume (struct GNUNET_FS_TreeEncoder *te,
210 /* end of fs_tree.h */