xref: /minix3/external/bsd/libarchive/dist/libarchive/archive_string.h (revision 543adbed3a3a783ed36434adafbc258b6bde442d)
1*543adbedSBen Gras /*-
2*543adbedSBen Gras  * Copyright (c) 2003-2007 Tim Kientzle
3*543adbedSBen Gras  * All rights reserved.
4*543adbedSBen Gras  *
5*543adbedSBen Gras  * Redistribution and use in source and binary forms, with or without
6*543adbedSBen Gras  * modification, are permitted provided that the following conditions
7*543adbedSBen Gras  * are met:
8*543adbedSBen Gras  * 1. Redistributions of source code must retain the above copyright
9*543adbedSBen Gras  *    notice, this list of conditions and the following disclaimer.
10*543adbedSBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
11*543adbedSBen Gras  *    notice, this list of conditions and the following disclaimer in the
12*543adbedSBen Gras  *    documentation and/or other materials provided with the distribution.
13*543adbedSBen Gras  *
14*543adbedSBen Gras  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15*543adbedSBen Gras  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16*543adbedSBen Gras  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17*543adbedSBen Gras  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18*543adbedSBen Gras  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19*543adbedSBen Gras  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20*543adbedSBen Gras  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21*543adbedSBen Gras  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22*543adbedSBen Gras  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23*543adbedSBen Gras  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24*543adbedSBen Gras  *
25*543adbedSBen Gras  * $FreeBSD: head/lib/libarchive/archive_string.h 201092 2009-12-28 02:26:06Z kientzle $
26*543adbedSBen Gras  *
27*543adbedSBen Gras  */
28*543adbedSBen Gras 
29*543adbedSBen Gras #ifndef __LIBARCHIVE_BUILD
30*543adbedSBen Gras #error This header is only to be used internally to libarchive.
31*543adbedSBen Gras #endif
32*543adbedSBen Gras 
33*543adbedSBen Gras #ifndef ARCHIVE_STRING_H_INCLUDED
34*543adbedSBen Gras #define	ARCHIVE_STRING_H_INCLUDED
35*543adbedSBen Gras 
36*543adbedSBen Gras #include <stdarg.h>
37*543adbedSBen Gras #ifdef HAVE_STDLIB_H
38*543adbedSBen Gras #include <stdlib.h>  /* required for wchar_t on some systems */
39*543adbedSBen Gras #endif
40*543adbedSBen Gras #ifdef HAVE_STRING_H
41*543adbedSBen Gras #include <string.h>
42*543adbedSBen Gras #endif
43*543adbedSBen Gras #ifdef HAVE_WCHAR_H
44*543adbedSBen Gras #include <wchar.h>
45*543adbedSBen Gras #endif
46*543adbedSBen Gras 
47*543adbedSBen Gras /*
48*543adbedSBen Gras  * Basic resizable/reusable string support a la Java's "StringBuffer."
49*543adbedSBen Gras  *
50*543adbedSBen Gras  * Unlike sbuf(9), the buffers here are fully reusable and track the
51*543adbedSBen Gras  * length throughout.
52*543adbedSBen Gras  *
53*543adbedSBen Gras  * Note that all visible symbols here begin with "__archive" as they
54*543adbedSBen Gras  * are internal symbols not intended for anyone outside of this library
55*543adbedSBen Gras  * to see or use.
56*543adbedSBen Gras  */
57*543adbedSBen Gras 
58*543adbedSBen Gras struct archive_string {
59*543adbedSBen Gras 	char	*s;  /* Pointer to the storage */
60*543adbedSBen Gras 	size_t	 length; /* Length of 's' */
61*543adbedSBen Gras 	size_t	 buffer_length; /* Length of malloc-ed storage */
62*543adbedSBen Gras };
63*543adbedSBen Gras 
64*543adbedSBen Gras /* Initialize an archive_string object on the stack or elsewhere. */
65*543adbedSBen Gras #define	archive_string_init(a)	\
66*543adbedSBen Gras 	do { (a)->s = NULL; (a)->length = 0; (a)->buffer_length = 0; } while(0)
67*543adbedSBen Gras 
68*543adbedSBen Gras /* Append a C char to an archive_string, resizing as necessary. */
69*543adbedSBen Gras struct archive_string *
70*543adbedSBen Gras __archive_strappend_char(struct archive_string *, char);
71*543adbedSBen Gras #define	archive_strappend_char __archive_strappend_char
72*543adbedSBen Gras 
73*543adbedSBen Gras /* Convert a wide-char string to UTF-8 and append the result. */
74*543adbedSBen Gras struct archive_string *
75*543adbedSBen Gras __archive_strappend_w_utf8(struct archive_string *, const wchar_t *);
76*543adbedSBen Gras #define	archive_strappend_w_utf8	__archive_strappend_w_utf8
77*543adbedSBen Gras 
78*543adbedSBen Gras /* Convert a wide-char string to current locale and append the result. */
79*543adbedSBen Gras /* Returns NULL if conversion fails. */
80*543adbedSBen Gras struct archive_string *
81*543adbedSBen Gras __archive_strappend_w_mbs(struct archive_string *, const wchar_t *);
82*543adbedSBen Gras #define	archive_strappend_w_mbs	__archive_strappend_w_mbs
83*543adbedSBen Gras 
84*543adbedSBen Gras /* Basic append operation. */
85*543adbedSBen Gras struct archive_string *
86*543adbedSBen Gras __archive_string_append(struct archive_string *as, const char *p, size_t s);
87*543adbedSBen Gras 
88*543adbedSBen Gras /* Copy one archive_string to another */
89*543adbedSBen Gras void
90*543adbedSBen Gras __archive_string_copy(struct archive_string *dest, struct archive_string *src);
91*543adbedSBen Gras #define archive_string_copy(dest, src) \
92*543adbedSBen Gras 	__archive_string_copy(dest, src)
93*543adbedSBen Gras 
94*543adbedSBen Gras /* Concatenate one archive_string to another */
95*543adbedSBen Gras void
96*543adbedSBen Gras __archive_string_concat(struct archive_string *dest, struct archive_string *src);
97*543adbedSBen Gras #define archive_string_concat(dest, src) \
98*543adbedSBen Gras 	__archive_string_concat(dest, src)
99*543adbedSBen Gras 
100*543adbedSBen Gras /* Ensure that the underlying buffer is at least as large as the request. */
101*543adbedSBen Gras struct archive_string *
102*543adbedSBen Gras __archive_string_ensure(struct archive_string *, size_t);
103*543adbedSBen Gras #define	archive_string_ensure __archive_string_ensure
104*543adbedSBen Gras 
105*543adbedSBen Gras /* Append C string, which may lack trailing \0. */
106*543adbedSBen Gras /* The source is declared void * here because this gets used with
107*543adbedSBen Gras  * "signed char *", "unsigned char *" and "char *" arguments.
108*543adbedSBen Gras  * Declaring it "char *" as with some of the other functions just
109*543adbedSBen Gras  * leads to a lot of extra casts. */
110*543adbedSBen Gras struct archive_string *
111*543adbedSBen Gras __archive_strncat(struct archive_string *, const void *, size_t);
112*543adbedSBen Gras #define	archive_strncat  __archive_strncat
113*543adbedSBen Gras 
114*543adbedSBen Gras /* Append a C string to an archive_string, resizing as necessary. */
115*543adbedSBen Gras #define	archive_strcat(as,p) __archive_string_append((as),(p),strlen(p))
116*543adbedSBen Gras 
117*543adbedSBen Gras /* Copy a C string to an archive_string, resizing as necessary. */
118*543adbedSBen Gras #define	archive_strcpy(as,p) \
119*543adbedSBen Gras 	((as)->length = 0, __archive_string_append((as), (p), p == NULL ? 0 : strlen(p)))
120*543adbedSBen Gras 
121*543adbedSBen Gras /* Copy a C string to an archive_string with limit, resizing as necessary. */
122*543adbedSBen Gras #define	archive_strncpy(as,p,l) \
123*543adbedSBen Gras 	((as)->length=0, archive_strncat((as), (p), (l)))
124*543adbedSBen Gras 
125*543adbedSBen Gras /* Return length of string. */
126*543adbedSBen Gras #define	archive_strlen(a) ((a)->length)
127*543adbedSBen Gras 
128*543adbedSBen Gras /* Set string length to zero. */
129*543adbedSBen Gras #define	archive_string_empty(a) ((a)->length = 0)
130*543adbedSBen Gras 
131*543adbedSBen Gras /* Release any allocated storage resources. */
132*543adbedSBen Gras void	__archive_string_free(struct archive_string *);
133*543adbedSBen Gras #define	archive_string_free  __archive_string_free
134*543adbedSBen Gras 
135*543adbedSBen Gras /* Like 'vsprintf', but resizes the underlying string as necessary. */
136*543adbedSBen Gras void	__archive_string_vsprintf(struct archive_string *, const char *,
137*543adbedSBen Gras 	    va_list);
138*543adbedSBen Gras #define	archive_string_vsprintf	__archive_string_vsprintf
139*543adbedSBen Gras 
140*543adbedSBen Gras void	__archive_string_sprintf(struct archive_string *, const char *, ...);
141*543adbedSBen Gras #define	archive_string_sprintf	__archive_string_sprintf
142*543adbedSBen Gras 
143*543adbedSBen Gras /* Allocates a fresh buffer and converts as (assumed to be UTF-8) into it.
144*543adbedSBen Gras  * Returns NULL if conversion failed in any way. */
145*543adbedSBen Gras wchar_t *__archive_string_utf8_w(struct archive_string *as);
146*543adbedSBen Gras 
147*543adbedSBen Gras 
148*543adbedSBen Gras #endif
149