source: mainline/uspace/app/bithenge/blob.h@ 23db8aa

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 23db8aa was 978ccaf1, checked in by Sean Bartell <wingedtachikoma@…>, 13 years ago

Bithenge: various cleanup and tweaks

  • Property mode set to 100644
File size: 7.0 KB
Line 
1/*
2 * Copyright (c) 2012 Sean Bartell
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup bithenge
30 * @{
31 */
32/**
33 * @file
34 * Raw binary blobs.
35 */
36
37#ifndef BITHENGE_BLOB_H_
38#define BITHENGE_BLOB_H_
39
40#include <sys/types.h>
41#include "tree.h"
42
43/** A blob of raw binary data.
44 * @implements bithenge_node_t */
45typedef struct {
46 /** @privatesection */
47 struct bithenge_node_t base;
48} bithenge_blob_t;
49
50/** Operations providing random access to binary data.
51 * @todo Should these be thread-safe? */
52typedef struct bithenge_random_access_blob_ops_t {
53 /** @copydoc bithenge_blob_t::bithenge_blob_size */
54 int (*size)(bithenge_blob_t *self, aoff64_t *size);
55 /** @copydoc bithenge_blob_t::bithenge_blob_read */
56 int (*read)(bithenge_blob_t *self, aoff64_t offset, char *buffer,
57 aoff64_t *size);
58 /** Destroy the blob.
59 * @param blob The blob. */
60 void (*destroy)(bithenge_blob_t *self);
61} bithenge_random_access_blob_ops_t;
62
63/** A blob built from an object that supports only sequential reading.
64 * @implements bithenge_blob_t */
65typedef struct {
66 /** @privatesection */
67 /** The base random-access blob. */
68 bithenge_blob_t base;
69 /** Operations providing sequential access. */
70 const struct bithenge_sequential_blob_ops_t *ops;
71 /** Buffer containing all data read. */
72 char *buffer;
73 /** Size of buffer. */
74 aoff64_t buffer_size;
75 /** Amount of data actually in buffer. */
76 aoff64_t data_size;
77} bithenge_sequential_blob_t;
78
79/** Operations providing sequential access to binary data.
80 * @memberof bithenge_sequential_blob_t */
81typedef struct bithenge_sequential_blob_ops_t {
82
83 /** Get the total size of the blob. If the total size cannot be
84 * determined easily, this field may be null or return an error,
85 * forcing the entire blob to be read to determine its size.
86 *
87 * @memberof bithenge_blob_t
88 * @param self The blob.
89 * @param[out] size Total size of the blob.
90 * @return EOK on success or an error code from errno.h.
91 */
92 int (*size)(bithenge_sequential_blob_t *self, aoff64_t *size);
93
94 /** Read the next part of the blob. If the requested data extends
95 * beyond the end of the blob, the data up until the end of the blob
96 * will be read.
97 *
98 * @param self The blob.
99 * @param[out] buffer Buffer to read into. If an error occurs, the contents are
100 * undefined.
101 * @param[in,out] size Number of bytes to read; may be 0. If not enough
102 * data is left in the blob, the actual number of bytes read should be
103 * stored here. If an error occurs, the contents are undefined.
104 * @return EOK on success or an error code from errno.h.
105 */
106 int (*read)(bithenge_sequential_blob_t *self, char *buffer,
107 aoff64_t *size);
108
109 /** Destroy the blob.
110 * @param self The blob. */
111 void (*destroy)(bithenge_sequential_blob_t *self);
112} bithenge_sequential_blob_ops_t;
113
114/** Get the total size of the blob.
115 *
116 * @memberof bithenge_blob_t
117 * @param self The blob.
118 * @param[out] size Total size of the blob.
119 * @return EOK on success or an error code from errno.h.
120 */
121static inline int bithenge_blob_size(bithenge_blob_t *self, aoff64_t *size)
122{
123 assert(self);
124 assert(self->base.blob_ops);
125 return self->base.blob_ops->size(self, size);
126}
127
128/** Read part of the blob. If the requested data extends beyond the end of the
129 * blob, the data up until the end of the blob will be read. If the offset is
130 * beyond the end of the blob, even if the size is zero, an error will be
131 * returned.
132 *
133 * @memberof bithenge_blob_t
134 * @param self The blob.
135 * @param offset Byte offset within the blob.
136 * @param[out] buffer Buffer to read into. If an error occurs, the contents are
137 * undefined.
138 * @param[in,out] size Number of bytes to read; may be 0. If the requested
139 * range extends beyond the end of the blob, the actual number of bytes read
140 * should be stored here. If an error occurs, the contents are undefined.
141 * @return EOK on success or an error code from errno.h.
142 */
143static inline int bithenge_blob_read(bithenge_blob_t *self, aoff64_t offset,
144 char *buffer, aoff64_t *size)
145{
146 assert(self);
147 assert(self->base.blob_ops);
148 return self->base.blob_ops->read(self, offset, buffer, size);
149}
150
151/** Cast a blob node to a generic node.
152 * @memberof bithenge_blob_t
153 * @param blob The blob to cast.
154 * @return The blob node as a generic node. */
155static inline bithenge_node_t *bithenge_blob_as_node(bithenge_blob_t *blob)
156{
157 return &blob->base;
158}
159
160/** Cast a generic node to a blob node.
161 * @memberof bithenge_blob_t
162 * @param node The node to cast, which must be a blob node.
163 * @return The generic node as a blob node. */
164static inline bithenge_blob_t *bithenge_node_as_blob(bithenge_node_t *node)
165{
166 assert(node->type == BITHENGE_NODE_BLOB);
167 return (bithenge_blob_t *)node;
168}
169
170/** Increment a blob's reference count.
171 * @param blob The blob to reference. */
172static inline void bithenge_blob_inc_ref(bithenge_blob_t *blob)
173{
174 bithenge_node_inc_ref(bithenge_blob_as_node(blob));
175}
176
177/** Decrement a blob's reference count.
178 * @param blob The blob to dereference, or NULL. */
179static inline void bithenge_blob_dec_ref(bithenge_blob_t *blob)
180{
181 if (blob)
182 bithenge_node_dec_ref(bithenge_blob_as_node(blob));
183}
184
185int bithenge_init_random_access_blob(bithenge_blob_t *,
186 const bithenge_random_access_blob_ops_t *);
187int bithenge_init_sequential_blob(bithenge_sequential_blob_t *,
188 const bithenge_sequential_blob_ops_t *);
189int bithenge_new_blob_from_data(bithenge_node_t **, const void *, size_t);
190int bithenge_new_blob_from_buffer(bithenge_node_t **, const void *, size_t,
191 bool);
192int bithenge_new_offset_blob(bithenge_node_t **, bithenge_blob_t *, aoff64_t);
193int bithenge_new_subblob(bithenge_node_t **, bithenge_blob_t *, aoff64_t,
194 aoff64_t);
195bool bithenge_blob_equal(bithenge_blob_t *, bithenge_blob_t *);
196
197#endif
198
199/** @}
200 */
Note: See TracBrowser for help on using the repository browser.