Annotation of embedaddon/mpd/src/contrib/libpdel/structs/type/structs_type_string.c, revision 1.1.1.2
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:
43: #include <stdlib.h>
44: #include <stdio.h>
45: #include <stdarg.h>
46: #include <string.h>
47: #include <errno.h>
48:
49: #include "structs/structs.h"
50: #include "structs/type/array.h"
51: #include "structs/type/string.h"
52: #include "util/typed_mem.h"
53:
54: /*********************************************************************
55: DYNAMICALLY ALLOCATED STRING TYPE
56: *********************************************************************/
57:
58: int
59: structs_string_init(const struct structs_type *type, void *data)
60: {
61: return (structs_string_binify(type, "", data, NULL, 0));
62: }
63:
64: int
65: structs_string_equal(const struct structs_type *type,
66: const void *v1, const void *v2)
67: {
68: const int as_null = type->args[1].i;
1.1.1.2 ! misho 69: const char *const s1 = *((const char *const *)v1);
! 70: const char *const s2 = *((const char *const *)v2);
1.1 misho 71: int empty1;
72: int empty2;
73:
74: if (!as_null)
75: return (strcmp(s1, s2) == 0);
76: empty1 = (s1 == NULL) || *s1 == '\0';
77: empty2 = (s2 == NULL) || *s2 == '\0';
78: if (empty1 ^ empty2)
79: return (0);
80: if (empty1)
81: return (1);
82: return (strcmp(s1, s2) == 0);
83: }
84:
85: char *
86: structs_string_ascify(const struct structs_type *type,
87: const char *mtype, const void *data)
88: {
89: const int as_null = type->args[1].i;
1.1.1.2 ! misho 90: const char *s = *((const char *const *)data);
1.1 misho 91:
92: if (as_null && s == NULL)
93: s = "";
94: return (STRDUP(mtype, s));
95: }
96:
97: int
98: structs_string_binify(const struct structs_type *type,
99: const char *ascii, void *data, char *ebuf, size_t emax)
100: {
101: const char *mtype = type->args[0].s;
102: const int as_null = type->args[1].i;
103: char *s;
104:
1.1.1.2 ! misho 105: (void)ebuf;
! 106: (void)emax;
1.1 misho 107: if (as_null && *ascii == '\0')
108: s = NULL;
109: else if ((s = STRDUP(mtype, ascii)) == NULL)
110: return (-1);
111: *((char **)data) = s;
112: return (0);
113: }
114:
115: /*
116: * This can be used by any type that wishes to encode its
117: * value using its ASCII string representation.
118: */
119: int
120: structs_string_encode(const struct structs_type *type, const char *mtype,
121: struct structs_data *code, const void *data)
122: {
1.1.1.2 ! misho 123: if ((code->data = (u_char *)structs_get_string(type,
1.1 misho 124: NULL, data, mtype)) == NULL)
125: return (-1);
126: code->length = strlen((char *)code->data) + 1;
127: return (0);
128: }
129:
130: /*
131: * This can be used by any type that wishes to encode its
132: * value using its ASCII string representation.
133: */
134: int
135: structs_string_decode(const struct structs_type *type,
136: const u_char *code, size_t cmax, void *data, char *ebuf, size_t emax)
137: {
138: size_t slen;
139:
140: /* Determine length of string */
1.1.1.2 ! misho 141: for (slen = 0; slen < cmax && code[slen] != '\0'; slen++);
1.1 misho 142: if (slen == cmax) {
143: strlcpy(ebuf, "encoded string is truncated", emax);
144: errno = EINVAL;
145: return (-1);
146: }
147:
148: /* Set string value */
149: if ((*type->binify)(type, (const char *)code, data, ebuf, emax) == -1)
150: return (-1);
151:
152: /* Done */
153: return (slen + 1);
154: }
155:
156: void
157: structs_string_free(const struct structs_type *type, void *data)
158: {
159: const char *mtype = type->args[0].s;
160: char *const s = *((char **)data);
161:
162: if (s != NULL) {
163: FREE(mtype, s);
164: *((char **)data) = NULL;
165: }
166: }
167:
168: const struct structs_type structs_type_string
169: = STRUCTS_STRING_TYPE(STRUCTS_TYPE_STRING_MTYPE, 0);
170: const struct structs_type structs_type_string_null
171: = STRUCTS_STRING_TYPE(STRUCTS_TYPE_STRING_MTYPE, 1);
172:
173: /*********************************************************************
174: BOUNDED LENGTH STRING TYPE
175: *********************************************************************/
176:
177: int
178: structs_bstring_equal(const struct structs_type *type,
179: const void *v1, const void *v2)
180: {
181: const char *const s1 = v1;
182: const char *const s2 = v2;
183:
1.1.1.2 ! misho 184: (void)type;
1.1 misho 185: return (strcmp(s1, s2) == 0);
186: }
187:
188: char *
189: structs_bstring_ascify(const struct structs_type *type,
190: const char *mtype, const void *data)
191: {
192: const char *const s = data;
193:
1.1.1.2 ! misho 194: (void)type;
1.1 misho 195: return (STRDUP(mtype, s));
196: }
197:
198: int
199: structs_bstring_binify(const struct structs_type *type,
200: const char *ascii, void *data, char *ebuf, size_t emax)
201: {
202: const size_t alen = strlen(ascii);
203:
204: if (alen + 1 > type->size) {
205: strlcpy(ebuf,
206: "string is too long for bounded length buffer", emax);
207: return (-1);
208: }
209: memcpy(data, ascii, alen + 1);
210: return (0);
211: }
212:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>