2 * Copyright (c) 2010 Your File System Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 #include <afsconfig.h>
26 #include <afs/param.h>
31 # include "afs/sysincludes.h"
32 # include "afsincludes.h"
36 #include <rx/rx_opaque.h>
40 * Generate a new opaque object
42 * Allocate a new opaque object, and copy datalen bytes from data into it.
43 * The caller should dispose of the resulting object using rx_opaque_free or
47 * A pointer to the data to copy into the object
49 * The number of bytes of data to copy
51 * A pointer to the allocated opaque object.
55 rx_opaque_new(void *data, size_t datalen)
57 struct rx_opaque *opaque = rxi_Alloc(sizeof(*opaque));
59 rx_opaque_populate(opaque, data, datalen);
64 * Allocate space within an existing opaque object
66 * Allocate length bytes of data within an existing opaque object. This will
67 * overwrite (without freeing) any data that is already held within the
73 * The number of bytes to allocate
75 * 0 on success, ENOMEM if the memory cannot be allocated.
79 rx_opaque_alloc(struct rx_opaque *buf, size_t length)
81 void *mem = rxi_Alloc(length);
86 memset(buf->val, 0, buf->len);
92 * Populate an existing opaque object
94 * Copy datalen bytes from data into an existing opaque object. This allocates
95 * new data space within the object, and will replace (without freeing) any
96 * data that is already held within the object.
99 * The opaque object to populate
101 * The number of bytes to allocate
103 * 0 on sucess, ENOMEM if memory cannot be allocated
107 rx_opaque_populate(struct rx_opaque *to, const void *data, size_t datalen)
113 if (data == NULL || datalen == 0)
116 code = rx_opaque_alloc(to, datalen);
119 memcpy(to->val, data, datalen);
124 * Copy data from one opaque object to another
126 * Make a copy of the data held within one existing opaque object into
127 * another. This allocates new data space within the destination object,
128 * and will replace (without freeing) any data that is already held within
132 * To object to copy to
134 * The object to copy data from
136 * 0 on success, ENOMEM if memory cannot be allocated
140 rx_opaque_copy(struct rx_opaque *to, const struct rx_opaque *from)
142 return rx_opaque_populate(to, from->val, from->len);
146 * Free the contents of an opaque object
150 rx_opaque_freeContents(struct rx_opaque *buf)
153 rxi_Free(buf->val, buf->len);
160 * Zero, then free, the contents of an opaque object
163 rx_opaque_zeroFreeContents(struct rx_opaque *buf)
166 memset(buf->val, 0, buf->len);
167 rx_opaque_freeContents(buf);
171 * Free an opaque object
173 * This frees the contents of the object, then frees the object itself
177 rx_opaque_free(struct rx_opaque **a_buf)
179 struct rx_opaque *buf = *a_buf;
185 rx_opaque_freeContents(buf);
186 rxi_Free(buf, sizeof(*buf));
190 * Zero, then free an opaque object
192 * This zeros the contents of an opaque object, frees those contents,
193 * then frees the object itself.
197 rx_opaque_zeroFree(struct rx_opaque **a_buf)
199 struct rx_opaque *buf = *a_buf;
205 rx_opaque_zeroFreeContents(buf);
206 rxi_Free(buf, sizeof(*buf));