import less(1)
[unleashed/tickless.git] / usr / src / lib / libndmp / common / libndmp_door_data.c
blob0c52bed1a5cea083cb543ff06102f3cb070ac106
1 /*
2 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
4 */
6 /*
7 * BSD 3 Clause License
9 * Copyright (c) 2007, The Storage Networking Industry Association.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * - Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
17 * - Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in
19 * the documentation and/or other materials provided with the
20 * distribution.
22 * - Neither the name of The Storage Networking Industry Association (SNIA)
23 * nor the names of its contributors may be used to endorse or promote
24 * products derived from this software without specific prior written
25 * permission.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
31 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
41 * Provides encode/decode routines for all door servers/clients.
44 #include <string.h>
45 #include <errno.h>
46 #include <stdlib.h>
47 #include <libndmp.h>
49 ndmp_door_ctx_t *
50 ndmp_door_decode_start(char *ptr, int size)
52 ndmp_door_ctx_t *ctx = malloc(sizeof (ndmp_door_ctx_t));
53 if (ctx) {
54 ctx->start_ptr = ctx->ptr = ptr;
55 ctx->end_ptr = ptr + size;
56 ctx->status = 0;
58 return (ctx);
61 int
62 ndmp_door_decode_finish(ndmp_door_ctx_t *ctx)
64 int status = ctx->status;
65 if ((status == 0) && (ctx->ptr != ctx->end_ptr)) {
66 status = ENOTEMPTY;
68 free(ctx);
69 return (status);
72 ndmp_door_ctx_t *
73 ndmp_door_encode_start(char *ptr, int size)
75 ndmp_door_ctx_t *ctx = malloc(sizeof (ndmp_door_ctx_t));
76 if (ctx) {
77 ctx->start_ptr = ctx->ptr = ptr;
78 ctx->end_ptr = ptr + size;
79 ctx->status = 0;
81 return (ctx);
84 int
85 ndmp_door_encode_finish(ndmp_door_ctx_t *ctx, unsigned int *used)
87 int status = ctx->status;
88 if (status == 0) {
89 if (ctx->ptr < ctx->end_ptr) {
90 /*LINTED E_PTRDIFF_OVERFLOW*/
91 *used = ctx->ptr - ctx->start_ptr;
92 } else {
93 status = ENOSPC;
96 free(ctx);
97 return (status);
100 int32_t
101 ndmp_door_get_int32(ndmp_door_ctx_t *ctx)
103 int32_t num = 0;
104 if (ctx->status == 0) {
105 if (ctx->ptr + sizeof (int32_t) <= ctx->end_ptr) {
106 (void) memcpy(&num, ctx->ptr, sizeof (int32_t));
107 ctx->ptr += sizeof (int32_t);
108 } else {
109 ctx->status = ENOSPC;
112 return (num);
115 uint32_t
116 ndmp_door_get_uint32(ndmp_door_ctx_t *ctx)
118 return ((uint32_t)ndmp_door_get_int32(ctx));
121 char *
122 ndmp_door_get_string(ndmp_door_ctx_t *ctx)
124 char *buf = NULL;
125 int len = ndmp_door_get_int32(ctx);
127 if (ctx->status == 0) {
128 if (len == -1)
129 return (buf);
131 if (ctx->ptr + len <= ctx->end_ptr) {
132 buf = malloc(len +1);
133 if (buf) {
134 if (len == 0) {
135 (void) strcpy(buf, "");
136 } else {
137 (void) memcpy(buf, ctx->ptr, len);
138 ctx->ptr += len;
139 *(buf + len) = '\0';
141 } else {
142 ctx->status = errno;
144 } else {
145 ctx->status = ENOSPC;
148 return (buf);
151 void
152 ndmp_door_put_int32(ndmp_door_ctx_t *ctx, int32_t num)
154 if (ctx->status == 0) {
155 if (ctx->ptr + sizeof (int32_t) <= ctx->end_ptr) {
156 (void) memcpy(ctx->ptr, &num, sizeof (int32_t));
157 ctx->ptr += sizeof (int32_t);
158 } else {
159 ctx->status = ENOSPC;
164 void
165 ndmp_door_put_uint32(ndmp_door_ctx_t *ctx, uint32_t num)
167 ndmp_door_put_int32(ctx, (int32_t)num);
170 void
171 ndmp_door_put_string(ndmp_door_ctx_t *ctx, char *buf)
173 int len;
175 if (!buf)
176 len = -1;
177 else
178 len = strlen(buf);
180 if (ctx->status == 0) {
181 ndmp_door_put_int32(ctx, len);
182 if (len <= 0)
183 return;
185 if (ctx->ptr + len <= ctx->end_ptr) {
186 (void) memcpy(ctx->ptr, buf, len);
187 ctx->ptr += len;
188 } else {
189 ctx->status = ENOSPC;
194 void
195 ndmp_door_free_string(char *buf)
197 free(buf);
200 int64_t
201 ndmp_door_get_int64(ndmp_door_ctx_t *ctx)
203 int64_t num = 0;
204 if (ctx->status == 0) {
205 if (ctx->ptr + sizeof (int64_t) <= ctx->end_ptr) {
206 (void) memcpy(&num, ctx->ptr, sizeof (int64_t));
207 ctx->ptr += sizeof (int64_t);
208 } else {
209 ctx->status = ENOSPC;
212 return (num);
215 uint64_t
216 ndmp_door_get_uint64(ndmp_door_ctx_t *ctx)
218 return ((uint64_t)ndmp_door_get_int64(ctx));
222 void
223 ndmp_door_put_int64(ndmp_door_ctx_t *ctx, int64_t num)
225 if (ctx->status == 0) {
226 if (ctx->ptr + sizeof (int64_t) <= ctx->end_ptr) {
227 (void) memcpy(ctx->ptr, &num, sizeof (int64_t));
228 ctx->ptr += sizeof (int64_t);
229 } else {
230 ctx->status = ENOSPC;
235 void
236 ndmp_door_put_uint64(ndmp_door_ctx_t *ctx, uint64_t num)
238 ndmp_door_put_int64(ctx, (int64_t)num);
241 void
242 ndmp_door_put_short(ndmp_door_ctx_t *ctx, short num)
244 if (ctx->status == 0) {
245 if (ctx->ptr + sizeof (short) <= ctx->end_ptr) {
246 (void) memcpy(ctx->ptr, &num, sizeof (short));
247 ctx->ptr += sizeof (short);
248 } else {
249 ctx->status = ENOSPC;
254 short
255 ndmp_door_get_short(ndmp_door_ctx_t *ctx)
257 short num = 0;
258 if (ctx->status == 0) {
259 if (ctx->ptr + sizeof (short) <= ctx->end_ptr) {
260 (void) memcpy(&num, ctx->ptr, sizeof (short));
261 ctx->ptr += sizeof (short);
262 } else {
263 ctx->status = ENOSPC;
266 return (num);
269 void
270 ndmp_door_put_ushort(ndmp_door_ctx_t *ctx, unsigned short num)
272 ndmp_door_put_short(ctx, (short)num);
275 unsigned short
276 ndmp_door_get_ushort(ndmp_door_ctx_t *ctx)
278 return ((unsigned short)ndmp_door_get_short(ctx));
281 void
282 ndmp_door_put_buf(ndmp_door_ctx_t *ctx, unsigned char *start, int len)
284 ndmp_door_put_int32(ctx, len);
285 if (ctx->status == 0) {
286 if (ctx->ptr + len <= ctx->end_ptr) {
287 (void) memcpy(ctx->ptr, start, len);
288 ctx->ptr += len;
289 } else {
290 ctx->status = ENOSPC;
296 ndmp_door_get_buf(ndmp_door_ctx_t *ctx, unsigned char *buf, int bufsize)
298 int len = -1;
300 if (!buf)
301 return (-1);
303 len = ndmp_door_get_int32(ctx);
304 if (ctx->status == 0) {
305 if (bufsize < len) {
306 ctx->status = ENOSPC;
307 return (-2);
310 if (ctx->ptr + len <= ctx->end_ptr) {
311 (void) memcpy(buf, ctx->ptr, len);
312 ctx->ptr += len;
313 } else {
314 ctx->status = ENOSPC;
315 return (-3);
319 return (len);