Annotation of embedaddon/mpd/src/contrib/libpdel/structs/type/structs_type_string.c, revision 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:
! 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;
! 69: const char *const s1 = *((const char **)v1);
! 70: const char *const s2 = *((const char **)v2);
! 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;
! 90: const char *s = *((char **)data);
! 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:
! 105: if (as_null && *ascii == '\0')
! 106: s = NULL;
! 107: else if ((s = STRDUP(mtype, ascii)) == NULL)
! 108: return (-1);
! 109: *((char **)data) = s;
! 110: return (0);
! 111: }
! 112:
! 113: /*
! 114: * This can be used by any type that wishes to encode its
! 115: * value using its ASCII string representation.
! 116: */
! 117: int
! 118: structs_string_encode(const struct structs_type *type, const char *mtype,
! 119: struct structs_data *code, const void *data)
! 120: {
! 121: if ((code->data = structs_get_string(type,
! 122: NULL, data, mtype)) == NULL)
! 123: return (-1);
! 124: code->length = strlen((char *)code->data) + 1;
! 125: return (0);
! 126: }
! 127:
! 128: /*
! 129: * This can be used by any type that wishes to encode its
! 130: * value using its ASCII string representation.
! 131: */
! 132: int
! 133: structs_string_decode(const struct structs_type *type,
! 134: const u_char *code, size_t cmax, void *data, char *ebuf, size_t emax)
! 135: {
! 136: size_t slen;
! 137:
! 138: /* Determine length of string */
! 139: for (slen = 0; slen < cmax && ((char *)code)[slen] != '\0'; slen++);
! 140: if (slen == cmax) {
! 141: strlcpy(ebuf, "encoded string is truncated", emax);
! 142: errno = EINVAL;
! 143: return (-1);
! 144: }
! 145:
! 146: /* Set string value */
! 147: if ((*type->binify)(type, (const char *)code, data, ebuf, emax) == -1)
! 148: return (-1);
! 149:
! 150: /* Done */
! 151: return (slen + 1);
! 152: }
! 153:
! 154: void
! 155: structs_string_free(const struct structs_type *type, void *data)
! 156: {
! 157: const char *mtype = type->args[0].s;
! 158: char *const s = *((char **)data);
! 159:
! 160: if (s != NULL) {
! 161: FREE(mtype, s);
! 162: *((char **)data) = NULL;
! 163: }
! 164: }
! 165:
! 166: const struct structs_type structs_type_string
! 167: = STRUCTS_STRING_TYPE(STRUCTS_TYPE_STRING_MTYPE, 0);
! 168: const struct structs_type structs_type_string_null
! 169: = STRUCTS_STRING_TYPE(STRUCTS_TYPE_STRING_MTYPE, 1);
! 170:
! 171: /*********************************************************************
! 172: BOUNDED LENGTH STRING TYPE
! 173: *********************************************************************/
! 174:
! 175: int
! 176: structs_bstring_equal(const struct structs_type *type,
! 177: const void *v1, const void *v2)
! 178: {
! 179: const char *const s1 = v1;
! 180: const char *const s2 = v2;
! 181:
! 182: return (strcmp(s1, s2) == 0);
! 183: }
! 184:
! 185: char *
! 186: structs_bstring_ascify(const struct structs_type *type,
! 187: const char *mtype, const void *data)
! 188: {
! 189: const char *const s = data;
! 190:
! 191: return (STRDUP(mtype, s));
! 192: }
! 193:
! 194: int
! 195: structs_bstring_binify(const struct structs_type *type,
! 196: const char *ascii, void *data, char *ebuf, size_t emax)
! 197: {
! 198: const size_t alen = strlen(ascii);
! 199:
! 200: if (alen + 1 > type->size) {
! 201: strlcpy(ebuf,
! 202: "string is too long for bounded length buffer", emax);
! 203: return (-1);
! 204: }
! 205: memcpy(data, ascii, alen + 1);
! 206: return (0);
! 207: }
! 208:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>