archive_string.h 9.3 KB

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