1 /*- 2 * Copyright (c) 2003-2010 Tim Kientzle 3 * All rights reserved. 4 * 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. 13 * 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. 24 * 25 * $FreeBSD: head/lib/libarchive/archive_string.h 201092 2009-12-28 02:26:06Z kientzle $ 26 * 27 */ 28 29 #ifndef __LIBARCHIVE_BUILD 30 #ifndef __LIBARCHIVE_TEST 31 #error This header is only to be used internally to libarchive. 32 #endif 33 #endif 34 35 #ifndef ARCHIVE_STRING_H_INCLUDED 36 #define ARCHIVE_STRING_H_INCLUDED 37 38 #include <stdarg.h> 39 #ifdef HAVE_STDLIB_H 40 #include <stdlib.h> /* required for wchar_t on some systems */ 41 #endif 42 #ifdef HAVE_STRING_H 43 #include <string.h> 44 #endif 45 #ifdef HAVE_WCHAR_H 46 #include <wchar.h> 47 #endif 48 49 #include "archive.h" 50 51 /* 52 * Basic resizable/reusable string support similar to Java's "StringBuffer." 53 * 54 * Unlike sbuf(9), the buffers here are fully reusable and track the 55 * length throughout. 56 */ 57 58 struct archive_string { 59 char *s; /* Pointer to the storage */ 60 size_t length; /* Length of 's' in characters */ 61 size_t buffer_length; /* Length of malloc-ed storage in bytes. */ 62 }; 63 64 struct archive_wstring { 65 wchar_t *s; /* Pointer to the storage */ 66 size_t length; /* Length of 's' in characters */ 67 size_t buffer_length; /* Length of malloc-ed storage in bytes. */ 68 }; 69 70 struct archive_string_conv; 71 72 /* Initialize an archive_string object on the stack or elsewhere. */ 73 #define archive_string_init(a) \ 74 do { (a)->s = NULL; (a)->length = 0; (a)->buffer_length = 0; } while(0) 75 76 /* Append a C char to an archive_string, resizing as necessary. */ 77 struct archive_string * 78 archive_strappend_char(struct archive_string *, char); 79 80 /* Ditto for a wchar_t and an archive_wstring. */ 81 struct archive_wstring * 82 archive_wstrappend_wchar(struct archive_wstring *, wchar_t); 83 84 /* Convert a Unicode string to current locale and append the result. */ 85 /* Returns -1 if conversion fails. */ 86 int 87 archive_string_append_from_wcs(struct archive_string *, const wchar_t *, size_t); 88 89 90 /* Create a string conversion object. 91 * Return NULL and set a error message if the conversion is not supported 92 * on the platform. */ 93 struct archive_string_conv * 94 archive_string_conversion_to_charset(struct archive *, const char *, int); 95 struct archive_string_conv * 96 archive_string_conversion_from_charset(struct archive *, const char *, int); 97 /* Create the default string conversion object for reading/writing an archive. 98 * Return NULL if the conversion is unneeded. 99 * Note: On non Windows platform this always returns NULL. 100 */ 101 struct archive_string_conv * 102 archive_string_default_conversion_for_read(struct archive *); 103 struct archive_string_conv * 104 archive_string_default_conversion_for_write(struct archive *); 105 /* Dispose of a string conversion object. */ 106 void 107 archive_string_conversion_free(struct archive *); 108 const char * 109 archive_string_conversion_charset_name(struct archive_string_conv *); 110 void 111 archive_string_conversion_set_opt(struct archive_string_conv *, int); 112 #define SCONV_SET_OPT_UTF8_LIBARCHIVE2X 1 113 114 115 /* Copy one archive_string to another in locale conversion. 116 * Return -1 if conversion failes. */ 117 int 118 archive_strncpy_in_locale(struct archive_string *, const void *, size_t, 119 struct archive_string_conv *); 120 121 /* Copy one archive_string to another in locale conversion. 122 * Return -1 if conversion failes. */ 123 int 124 archive_strncat_in_locale(struct archive_string *, const void *, size_t, 125 struct archive_string_conv *); 126 127 128 /* Copy one archive_string to another */ 129 #define archive_string_copy(dest, src) \ 130 ((dest)->length = 0, archive_string_concat((dest), (src))) 131 #define archive_wstring_copy(dest, src) \ 132 ((dest)->length = 0, archive_wstring_concat((dest), (src))) 133 134 /* Concatenate one archive_string to another */ 135 void archive_string_concat(struct archive_string *dest, struct archive_string *src); 136 void archive_wstring_concat(struct archive_wstring *dest, struct archive_wstring *src); 137 138 /* Ensure that the underlying buffer is at least as large as the request. */ 139 struct archive_string * 140 archive_string_ensure(struct archive_string *, size_t); 141 struct archive_wstring * 142 archive_wstring_ensure(struct archive_wstring *, size_t); 143 144 /* Append C string, which may lack trailing \0. */ 145 /* The source is declared void * here because this gets used with 146 * "signed char *", "unsigned char *" and "char *" arguments. 147 * Declaring it "char *" as with some of the other functions just 148 * leads to a lot of extra casts. */ 149 struct archive_string * 150 archive_strncat(struct archive_string *, const void *, size_t); 151 struct archive_wstring * 152 archive_wstrncat(struct archive_wstring *, const wchar_t *, size_t); 153 154 /* Append a C string to an archive_string, resizing as necessary. */ 155 struct archive_string * 156 archive_strcat(struct archive_string *, const void *); 157 struct archive_wstring * 158 archive_wstrcat(struct archive_wstring *, const wchar_t *); 159 160 /* Copy a C string to an archive_string, resizing as necessary. */ 161 #define archive_strcpy(as,p) \ 162 archive_strncpy((as), (p), ((p) == NULL ? 0 : strlen(p))) 163 #define archive_wstrcpy(as,p) \ 164 archive_wstrncpy((as), (p), ((p) == NULL ? 0 : wcslen(p))) 165 #define archive_strcpy_in_locale(as,p,lo) \ 166 archive_strncpy_in_locale((as), (p), ((p) == NULL ? 0 : strlen(p)), (lo)) 167 168 /* Copy a C string to an archive_string with limit, resizing as necessary. */ 169 #define archive_strncpy(as,p,l) \ 170 ((as)->length=0, archive_strncat((as), (p), (l))) 171 #define archive_wstrncpy(as,p,l) \ 172 ((as)->length = 0, archive_wstrncat((as), (p), (l))) 173 174 /* Return length of string. */ 175 #define archive_strlen(a) ((a)->length) 176 177 /* Set string length to zero. */ 178 #define archive_string_empty(a) ((a)->length = 0) 179 #define archive_wstring_empty(a) ((a)->length = 0) 180 181 /* Release any allocated storage resources. */ 182 void archive_string_free(struct archive_string *); 183 void archive_wstring_free(struct archive_wstring *); 184 185 /* Like 'vsprintf', but resizes the underlying string as necessary. */ 186 /* Note: This only implements a small subset of standard printf functionality. */ 187 void archive_string_vsprintf(struct archive_string *, const char *, 188 va_list) __LA_PRINTF(2, 0); 189 void archive_string_sprintf(struct archive_string *, const char *, ...) 190 __LA_PRINTF(2, 3); 191 192 /* Translates from MBS to Unicode. */ 193 /* Returns non-zero if conversion failed in any way. */ 194 int archive_wstring_append_from_mbs(struct archive_wstring *dest, 195 const char *, size_t); 196 197 198 /* A "multistring" can hold Unicode, UTF8, or MBS versions of 199 * the string. If you set and read the same version, no translation 200 * is done. If you set and read different versions, the library 201 * will attempt to transparently convert. 202 */ 203 struct archive_mstring { 204 struct archive_string aes_mbs; 205 struct archive_string aes_utf8; 206 struct archive_wstring aes_wcs; 207 struct archive_string aes_mbs_in_locale; 208 /* Bitmap of which of the above are valid. Because we're lazy 209 * about malloc-ing and reusing the underlying storage, we 210 * can't rely on NULL pointers to indicate whether a string 211 * has been set. */ 212 int aes_set; 213 #define AES_SET_MBS 1 214 #define AES_SET_UTF8 2 215 #define AES_SET_WCS 4 216 }; 217 218 void archive_mstring_clean(struct archive_mstring *); 219 void archive_mstring_copy(struct archive_mstring *dest, struct archive_mstring *src); 220 int archive_mstring_get_mbs(struct archive *, struct archive_mstring *, const char **); 221 int archive_mstring_get_utf8(struct archive *, struct archive_mstring *, const char **); 222 int archive_mstring_get_wcs(struct archive *, struct archive_mstring *, const wchar_t **); 223 int archive_mstring_get_mbs_l(struct archive_mstring *, const char **, 224 size_t *, struct archive_string_conv *); 225 int archive_mstring_copy_mbs(struct archive_mstring *, const char *mbs); 226 int archive_mstring_copy_mbs_len(struct archive_mstring *, const char *mbs, 227 size_t); 228 int archive_mstring_copy_utf8(struct archive_mstring *, const char *utf8); 229 int archive_mstring_copy_wcs(struct archive_mstring *, const wchar_t *wcs); 230 int archive_mstring_copy_wcs_len(struct archive_mstring *, 231 const wchar_t *wcs, size_t); 232 int archive_mstring_copy_mbs_len_l(struct archive_mstring *, 233 const char *mbs, size_t, struct archive_string_conv *); 234 int archive_mstring_update_utf8(struct archive *, struct archive_mstring *aes, const char *utf8); 235 236 237 #endif 238