source: mainline/uspace/app/bithenge/blob.h@ 4b16422

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

bithenge: add blobs created from memory buffers

  • Property mode set to 100644
File size: 6.1 KB
RevLine 
[a54bd98]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
42/** A blob of raw binary data. */
43typedef struct {
44 /** @privatesection */
45 /** Operations providing random access. */
46 const struct bithenge_random_access_blob_ops_t *ops;
47} bithenge_blob_t;
48
49/** Operations providing random access to binary data.
50 * @todo Should these be thread-safe? */
51typedef struct bithenge_random_access_blob_ops_t {
52 /** @copydoc bithenge_blob_t::bithenge_blob_size */
53 int (*size)(bithenge_blob_t *blob, aoff64_t *size);
54 /** @copydoc bithenge_blob_t::bithenge_blob_read */
55 int (*read)(bithenge_blob_t *blob, aoff64_t offset, char *buffer,
56 aoff64_t *size);
57 /** @copydoc bithenge_blob_t::bithenge_blob_destroy */
58 int (*destroy)(bithenge_blob_t *blob);
59} bithenge_random_access_blob_ops_t;
60
61/** A blob built from an object that supports only sequential reading.
62 * @implements bithenge_blob_t */
63typedef struct {
64 /** @privatesection */
65 /** The base random-access blob. */
66 bithenge_blob_t base;
67 /** Operations providing sequential access. */
68 const struct bithenge_sequential_blob_ops_t *ops;
69 /** Buffer containing all data read. */
70 char *buffer;
71 /** Size of buffer. */
72 aoff64_t buffer_size;
73 /** Amount of data actually in buffer. */
74 aoff64_t data_size;
75} bithenge_sequential_blob_t;
76
77/** Operations providing sequential access to binary data.
78 * @memberof bithenge_sequential_blob_t */
79typedef struct bithenge_sequential_blob_ops_t {
[743ce51]80
81 /** Get the total size of the blob. If the total size cannot be
82 * determined easily, this field may be null or return an error,
83 * forcing the entire blob to be read to determine its size.
84 *
85 * @memberof bithenge_blob_t
86 * @param blob The blob.
87 * @param[out] size Total size of the blob.
88 * @return EOK on success or an error code from errno.h.
89 */
[a54bd98]90 int (*size)(bithenge_sequential_blob_t *blob, aoff64_t *size);
[743ce51]91
92 /** Read the next part of the blob. If the requested data extends
93 * beyond the end of the blob, the data up until the end of the blob
94 * will be read.
95 *
96 * @param blob The blob.
97 * @param[out] buffer Buffer to read into. If an error occurs, the contents are
98 * undefined.
99 * @param[in,out] size Number of bytes to read; may be 0. If not enough
100 * data is left in the blob, the actual number of bytes read should be
101 * stored here. If an error occurs, the contents are undefined.
102 * @return EOK on success or an error code from errno.h.
103 */
[a54bd98]104 int (*read)(bithenge_sequential_blob_t *blob, char *buffer,
105 aoff64_t *size);
[743ce51]106
107 /** Destroy the blob.
108 * @param blob The blob.
109 * @return EOK on success or an error code from errno.h. */
[a54bd98]110 int (*destroy)(bithenge_sequential_blob_t *blob);
111} bithenge_sequential_blob_ops_t;
112
113/** Get the total size of the blob.
114 *
115 * @memberof bithenge_blob_t
116 * @param blob The blob.
117 * @param[out] size Total size of the blob.
118 * @return EOK on success or an error code from errno.h.
119 */
[743ce51]120static inline int bithenge_blob_size(bithenge_blob_t *blob, aoff64_t *size)
121{
122 assert(blob);
123 assert(blob->ops);
[a54bd98]124 return blob->ops->size(blob, size);
125}
126
127/** Read part of the blob. If the requested data extends beyond the end of the
128 * blob, the data up until the end of the blob will be read. If the offset is
129 * beyond the end of the blob, even if the size is zero, an error will be
130 * returned.
131 *
132 * @memberof bithenge_blob_t
133 * @param blob The blob.
134 * @param offset Byte offset within the blob.
135 * @param[out] buffer Buffer to read into. If an error occurs, the contents are
136 * undefined.
137 * @param[in,out] size Number of bytes to read; may be 0. If the requested
138 * range extends beyond the end of the blob, the actual number of bytes read
139 * should be stored here. If an error occurs, the contents are undefined.
140 * @return EOK on success or an error code from errno.h.
141 */
[743ce51]142static inline int bithenge_blob_read(bithenge_blob_t *blob, aoff64_t offset,
143 char *buffer, aoff64_t *size)
144{
145 assert(blob);
146 assert(blob->ops);
[a54bd98]147 return blob->ops->read(blob, offset, buffer, size);
148}
149
150/** Destroy the blob.
151 * @memberof bithenge_blob_t
152 * @param blob The blob.
153 * @return EOK on success or an error code from errno.h.
154 */
[743ce51]155static inline int bithenge_blob_destroy(bithenge_blob_t *blob)
156{
157 assert(blob);
158 assert(blob->ops);
[a54bd98]159 return blob->ops->destroy(blob);
160}
161
162int bithenge_new_random_access_blob(bithenge_blob_t *blob,
163 const bithenge_random_access_blob_ops_t *ops);
164
165int bithenge_new_sequential_blob(bithenge_sequential_blob_t *blob,
166 const bithenge_sequential_blob_ops_t *ops);
167
[ce683ed3]168int bithenge_new_blob_from_data(bithenge_blob_t **out, const void *data,
169 size_t len);
170
171int bithenge_new_blob_from_buffer(bithenge_blob_t **out, const void *buffer,
172 size_t len, bool needs_free);
173
[a54bd98]174#endif
175
176/** @}
177 */
Note: See TracBrowser for help on using the repository browser.