Updated the list of USB device IDs for uvisor(4) and needed routines.
[dragonfly.git] / contrib / libarchive-2 / libarchive / archive_string.c
blob7e43b360a4d0b4baf050e79f124452290fbbaf7c
1 /*-
2 * Copyright (c) 2003-2007 Tim Kientzle
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include "archive_platform.h"
27 __FBSDID("$FreeBSD: src/lib/libarchive/archive_string.c,v 1.11 2007/07/15 19:13:59 kientzle Exp $");
30 * Basic resizable string support, to simplify manipulating arbitrary-sized
31 * strings while minimizing heap activity.
34 #ifdef HAVE_STDLIB_H
35 #include <stdlib.h>
36 #endif
37 #ifdef HAVE_STRING_H
38 #include <string.h>
39 #endif
41 #include "archive_private.h"
42 #include "archive_string.h"
44 struct archive_string *
45 __archive_string_append(struct archive_string *as, const char *p, size_t s)
47 if (__archive_string_ensure(as, as->length + s + 1) == NULL)
48 __archive_errx(1, "Out of memory");
49 memcpy(as->s + as->length, p, s);
50 as->s[as->length + s] = 0;
51 as->length += s;
52 return (as);
55 void
56 __archive_string_copy(struct archive_string *dest, struct archive_string *src)
58 if (__archive_string_ensure(dest, src->length + 1) == NULL)
59 __archive_errx(1, "Out of memory");
60 memcpy(dest->s, src->s, src->length);
61 dest->length = src->length;
62 dest->s[dest->length] = 0;
65 void
66 __archive_string_free(struct archive_string *as)
68 as->length = 0;
69 as->buffer_length = 0;
70 if (as->s != NULL)
71 free(as->s);
74 /* Returns NULL on any allocation failure. */
75 struct archive_string *
76 __archive_string_ensure(struct archive_string *as, size_t s)
78 if (as->s && (s <= as->buffer_length))
79 return (as);
81 if (as->buffer_length < 32)
82 as->buffer_length = 32;
83 while (as->buffer_length < s)
84 as->buffer_length *= 2;
85 as->s = (char *)realloc(as->s, as->buffer_length);
86 if (as->s == NULL)
87 return (NULL);
88 return (as);
91 struct archive_string *
92 __archive_strncat(struct archive_string *as, const char *p, size_t n)
94 size_t s;
95 const char *pp;
97 /* Like strlen(p), except won't examine positions beyond p[n]. */
98 s = 0;
99 pp = p;
100 while (*pp && s < n) {
101 pp++;
102 s++;
104 return (__archive_string_append(as, p, s));
107 struct archive_string *
108 __archive_strappend_char(struct archive_string *as, char c)
110 return (__archive_string_append(as, &c, 1));
113 struct archive_string *
114 __archive_strappend_int(struct archive_string *as, int d, int base)
116 static const char *digits = "0123456789abcdef";
118 if (d < 0) {
119 __archive_strappend_char(as, '-');
120 d = -d;
122 if (d >= base)
123 __archive_strappend_int(as, d/base, base);
124 __archive_strappend_char(as, digits[d % base]);
125 return (as);