Annotation of embedaddon/libpdel/structs/structs_generic.c, revision 1.1.1.1
1.1 misho 1:
2: /*
3: * Copyright (c) 2001-2002 Packet Design, LLC.
4: * All rights reserved.
5: *
6: * Subject to the following obligations and disclaimer of warranty,
7: * use and redistribution of this software, in source or object code
8: * forms, with or without modifications are expressly permitted by
9: * Packet Design; provided, however, that:
10: *
11: * (i) Any and all reproductions of the source or object code
12: * must include the copyright notice above and the following
13: * disclaimer of warranties; and
14: * (ii) No rights are granted, in any manner or form, to use
15: * Packet Design trademarks, including the mark "PACKET DESIGN"
16: * on advertising, endorsements, or otherwise except as such
17: * appears in the above copyright notice or in the software.
18: *
19: * THIS SOFTWARE IS BEING PROVIDED BY PACKET DESIGN "AS IS", AND
20: * TO THE MAXIMUM EXTENT PERMITTED BY LAW, PACKET DESIGN MAKES NO
21: * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING
22: * THIS SOFTWARE, INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED
23: * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
24: * OR NON-INFRINGEMENT. PACKET DESIGN DOES NOT WARRANT, GUARANTEE,
25: * OR MAKE ANY REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS
26: * OF THE USE OF THIS SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY,
27: * RELIABILITY OR OTHERWISE. IN NO EVENT SHALL PACKET DESIGN BE
28: * LIABLE FOR ANY DAMAGES RESULTING FROM OR ARISING OUT OF ANY USE
29: * OF THIS SOFTWARE, INCLUDING WITHOUT LIMITATION, ANY DIRECT,
30: * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, PUNITIVE, OR CONSEQUENTIAL
31: * DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, LOSS OF
32: * USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY THEORY OF
33: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
35: * THE USE OF THIS SOFTWARE, EVEN IF PACKET DESIGN IS ADVISED OF
36: * THE POSSIBILITY OF SUCH DAMAGE.
37: *
38: * Author: Archie Cobbs <archie@freebsd.org>
39: */
40:
41: #include <sys/types.h>
42: #ifdef __linux__
43: #include <endian.h>
44: #else
45: #include <machine/endian.h>
46: #endif
47:
48: #include <stdio.h>
49: #include <stdlib.h>
50: #include <stdarg.h>
51: #include <string.h>
52: #include <errno.h>
53:
54: #include "structs/structs.h"
55: #include "structs/type/array.h"
56: #include "util/typed_mem.h"
57:
58: #ifndef BYTE_ORDER
59: #error BYTE_ORDER is undefined
60: #endif
61:
62: /*********************************************************************
63: GENERIC FUNCTIONS
64: *********************************************************************/
65:
66: int
67: structs_region_init(const struct structs_type *type, void *data)
68: {
69: memset(data, 0, type->size);
70: return (0);
71: }
72:
73: int
74: structs_region_copy(const struct structs_type *type, const void *from, void *to)
75: {
76: memcpy(to, from, type->size);
77: return (0);
78: }
79:
80: int
81: structs_region_equal(const struct structs_type *type,
82: const void *v1, const void *v2)
83: {
84: return (memcmp(v1, v2, type->size) == 0);
85: }
86:
87: int
88: structs_region_encode(const struct structs_type *type, const char *mtype,
89: struct structs_data *code, const void *data)
90: {
91: if ((code->data = MALLOC(mtype, type->size)) == NULL)
92: return (-1);
93: memcpy(code->data, data, type->size);
94: code->length = type->size;
95: return (0);
96: }
97:
98: int
99: structs_region_decode(const struct structs_type *type,
100: const u_char *code, size_t cmax, void *data, char *ebuf, size_t emax)
101: {
102: if (cmax < type->size) {
103: strlcpy(ebuf, "encoded data is truncated", emax);
104: errno = EINVAL;
105: return (-1);
106: }
107: memcpy(data, code, type->size);
108: return (type->size);
109: }
110:
111: int
112: structs_region_encode_netorder(const struct structs_type *type,
113: const char *mtype, struct structs_data *code, const void *data)
114: {
115: if (structs_region_encode(type, mtype, code, data) == -1)
116: return (-1);
117: #if BYTE_ORDER == LITTLE_ENDIAN
118: {
119: u_char temp;
120: u_int i;
121:
122: for (i = 0; i < code->length / 2; i++) {
123: temp = code->data[i];
124: code->data[i] = code->data[code->length - 1 - i];
125: code->data[code->length - 1 - i] = temp;
126: }
127: }
128: #endif
129: return (0);
130: }
131:
132: int
133: structs_region_decode_netorder(const struct structs_type *type,
134: const u_char *code, size_t cmax, void *data, char *ebuf, size_t emax)
135: {
136: #if BYTE_ORDER == LITTLE_ENDIAN
137: u_char buf[16];
138: u_char temp;
139: u_int i;
140:
141: if (type->size > sizeof(buf)) {
142: errno = ERANGE; /* XXX oops fixed buffer is too small */
143: return (-1);
144: }
145: if (cmax > type->size)
146: cmax = type->size;
147: memcpy(buf, code, cmax);
148: for (i = 0; i < type->size / 2; i++) {
149: temp = buf[i];
150: buf[i] = buf[type->size - 1 - i];
151: buf[type->size - 1 - i] = temp;
152: }
153: code = buf;
154: #endif
155: return (structs_region_decode(type, code, cmax, data, ebuf, emax));
156: }
157:
158: char *
159: structs_notsupp_ascify(const struct structs_type *type,
160: const char *mtype, const void *data)
161: {
162: errno = EOPNOTSUPP;
163: return (NULL);
164: }
165:
166: int
167: structs_notsupp_init(const struct structs_type *type, void *data)
168: {
169: errno = EOPNOTSUPP;
170: return (-1);
171: }
172:
173: int
174: structs_notsupp_copy(const struct structs_type *type,
175: const void *from, void *to)
176: {
177: errno = EOPNOTSUPP;
178: return (-1);
179: }
180:
181: int
182: structs_notsupp_equal(const struct structs_type *type,
183: const void *v1, const void *v2)
184: {
185: errno = EOPNOTSUPP;
186: return (-1);
187: }
188:
189:
190: int
191: structs_notsupp_binify(const struct structs_type *type,
192: const char *ascii, void *data, char *ebuf, size_t emax)
193: {
194: strlcpy(ebuf,
195: "parsing from ASCII is not supported by this structs type", emax);
196: errno = EOPNOTSUPP;
197: return (-1);
198: }
199:
200: int
201: structs_notsupp_encode(const struct structs_type *type, const char *mtype,
202: struct structs_data *code, const void *data)
203: {
204: errno = EOPNOTSUPP;
205: return (-1);
206: }
207:
208: int
209: structs_notsupp_decode(const struct structs_type *type,
210: const u_char *code, size_t cmax, void *data, char *ebuf, size_t emax)
211: {
212: strlcpy(ebuf,
213: "binary decoding is not supported by this structs type", emax);
214: errno = EOPNOTSUPP;
215: return (-1);
216: }
217:
218: void
219: structs_nothing_free(const struct structs_type *type, void *data)
220: {
221: return;
222: }
223:
224: int
225: structs_ascii_copy(const struct structs_type *type, const void *from, void *to)
226: {
227: char *ascii;
228: int rtn;
229:
230: if ((ascii = (*type->ascify)(type, TYPED_MEM_TEMP, from)) == NULL)
231: return (-1);
232: rtn = (*type->binify)(type, ascii, to, NULL, 0);
233: FREE(TYPED_MEM_TEMP, ascii);
234: return (rtn);
235: }
236:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>