archive_util.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /*-
  2. * Copyright (c) 2009 Michihiro NAKAJIMA
  3. * Copyright (c) 2003-2007 Tim Kientzle
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  16. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  18. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  19. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  24. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "archive_platform.h"
  27. __FBSDID("$FreeBSD: src/lib/libarchive/archive_util.c,v 1.19 2008/10/21 12:10:30 des Exp $");
  28. #ifdef HAVE_SYS_TYPES_H
  29. #include <sys/types.h>
  30. #endif
  31. #ifdef HAVE_STDLIB_H
  32. #include <stdlib.h>
  33. #endif
  34. #ifdef HAVE_STRING_H
  35. #include <string.h>
  36. #endif
  37. #include "archive.h"
  38. #include "archive_private.h"
  39. #include "archive_string.h"
  40. #if ARCHIVE_VERSION_NUMBER < 3000000
  41. /* These disappear in libarchive 3.0 */
  42. /* Deprecated. */
  43. int
  44. archive_api_feature(void)
  45. {
  46. return (ARCHIVE_API_FEATURE);
  47. }
  48. /* Deprecated. */
  49. int
  50. archive_api_version(void)
  51. {
  52. return (ARCHIVE_API_VERSION);
  53. }
  54. /* Deprecated synonym for archive_version_number() */
  55. int
  56. archive_version_stamp(void)
  57. {
  58. return (archive_version_number());
  59. }
  60. /* Deprecated synonym for archive_version_string() */
  61. const char *
  62. archive_version(void)
  63. {
  64. return (archive_version_string());
  65. }
  66. #endif
  67. int
  68. archive_version_number(void)
  69. {
  70. return (ARCHIVE_VERSION_NUMBER);
  71. }
  72. const char *
  73. archive_version_string(void)
  74. {
  75. return (ARCHIVE_VERSION_STRING);
  76. }
  77. int
  78. archive_errno(struct archive *a)
  79. {
  80. return (a->archive_error_number);
  81. }
  82. const char *
  83. archive_error_string(struct archive *a)
  84. {
  85. if (a->error != NULL && *a->error != '\0')
  86. return (a->error);
  87. else
  88. return ("(Empty error message)");
  89. }
  90. int
  91. archive_file_count(struct archive *a)
  92. {
  93. return (a->file_count);
  94. }
  95. int
  96. archive_format(struct archive *a)
  97. {
  98. return (a->archive_format);
  99. }
  100. const char *
  101. archive_format_name(struct archive *a)
  102. {
  103. return (a->archive_format_name);
  104. }
  105. int
  106. archive_compression(struct archive *a)
  107. {
  108. return (a->compression_code);
  109. }
  110. const char *
  111. archive_compression_name(struct archive *a)
  112. {
  113. return (a->compression_name);
  114. }
  115. /*
  116. * Return a count of the number of compressed bytes processed.
  117. */
  118. int64_t
  119. archive_position_compressed(struct archive *a)
  120. {
  121. return (a->raw_position);
  122. }
  123. /*
  124. * Return a count of the number of uncompressed bytes processed.
  125. */
  126. int64_t
  127. archive_position_uncompressed(struct archive *a)
  128. {
  129. return (a->file_position);
  130. }
  131. void
  132. archive_clear_error(struct archive *a)
  133. {
  134. archive_string_empty(&a->error_string);
  135. a->error = NULL;
  136. }
  137. void
  138. archive_set_error(struct archive *a, int error_number, const char *fmt, ...)
  139. {
  140. va_list ap;
  141. a->archive_error_number = error_number;
  142. if (fmt == NULL) {
  143. a->error = NULL;
  144. return;
  145. }
  146. va_start(ap, fmt);
  147. archive_string_vsprintf(&(a->error_string), fmt, ap);
  148. va_end(ap);
  149. a->error = a->error_string.s;
  150. }
  151. void
  152. archive_copy_error(struct archive *dest, struct archive *src)
  153. {
  154. dest->archive_error_number = src->archive_error_number;
  155. archive_string_copy(&dest->error_string, &src->error_string);
  156. dest->error = dest->error_string.s;
  157. }
  158. void
  159. __archive_errx(int retvalue, const char *msg)
  160. {
  161. static const char *msg1 = "Fatal Internal Error in libarchive: ";
  162. size_t s;
  163. s = write(2, msg1, strlen(msg1));
  164. s = write(2, msg, strlen(msg));
  165. s = write(2, "\n", 1);
  166. (void)s; /* UNUSED */
  167. exit(retvalue);
  168. }
  169. /*
  170. * Parse option strings
  171. * Detail of option format.
  172. * - The option can accept:
  173. * "opt-name", "!opt-name", "opt-name=value".
  174. *
  175. * - The option entries are separated by comma.
  176. * e.g "compression=9,opt=XXX,opt-b=ZZZ"
  177. *
  178. * - The name of option string consist of '-' and alphabet
  179. * but character '-' cannot be used for the first character.
  180. * (Regular expression is [a-z][-a-z]+)
  181. *
  182. * - For a specfic format/filter, using the format name with ':'.
  183. * e.g "zip:compression=9"
  184. * (This "compression=9" option entry is for "zip" format only)
  185. *
  186. * If another entries follow it, those are not for
  187. * the specfic format/filter.
  188. * e.g handle "zip:compression=9,opt=XXX,opt-b=ZZZ"
  189. * "zip" format/filter handler will get "compression=9"
  190. * all format/filter handler will get "opt=XXX"
  191. * all format/filter handler will get "opt-b=ZZZ"
  192. *
  193. * - Whitespace and tab are bypassed.
  194. *
  195. */
  196. int
  197. __archive_parse_options(const char *p, const char *fn, int keysize, char *key,
  198. int valsize, char *val)
  199. {
  200. const char *p_org;
  201. int apply;
  202. int kidx, vidx;
  203. int negative;
  204. enum {
  205. /* Requested for initialization. */
  206. INIT,
  207. /* Finding format/filter-name and option-name. */
  208. F_BOTH,
  209. /* Finding option-name only.
  210. * (already detected format/filter-name) */
  211. F_NAME,
  212. /* Getting option-value. */
  213. G_VALUE,
  214. } state;
  215. p_org = p;
  216. state = INIT;
  217. kidx = vidx = negative = 0;
  218. apply = 1;
  219. while (*p) {
  220. switch (state) {
  221. case INIT:
  222. kidx = vidx = 0;
  223. negative = 0;
  224. apply = 1;
  225. state = F_BOTH;
  226. break;
  227. case F_BOTH:
  228. case F_NAME:
  229. if ((*p >= 'a' && *p <= 'z') ||
  230. (*p >= '0' && *p <= '9') || *p == '-') {
  231. if (kidx == 0 && !(*p >= 'a' && *p <= 'z'))
  232. /* Illegal sequence. */
  233. return (-1);
  234. if (kidx >= keysize -1)
  235. /* Too many characters. */
  236. return (-1);
  237. key[kidx++] = *p++;
  238. } else if (*p == '!') {
  239. if (kidx != 0)
  240. /* Illegal sequence. */
  241. return (-1);
  242. negative = 1;
  243. ++p;
  244. } else if (*p == ',') {
  245. if (kidx == 0)
  246. /* Illegal sequence. */
  247. return (-1);
  248. if (!negative)
  249. val[vidx++] = '1';
  250. /* We have got boolean option data. */
  251. ++p;
  252. if (apply)
  253. goto complete;
  254. else
  255. /* This option does not apply to the
  256. * format which the fn variable
  257. * indicate. */
  258. state = INIT;
  259. } else if (*p == ':') {
  260. /* obuf data is format name */
  261. if (state == F_NAME)
  262. /* We already found it. */
  263. return (-1);
  264. if (kidx == 0)
  265. /* Illegal sequence. */
  266. return (-1);
  267. if (negative)
  268. /* We cannot accept "!format-name:". */
  269. return (-1);
  270. key[kidx] = '\0';
  271. if (strcmp(fn, key) != 0)
  272. /* This option does not apply to the
  273. * format which the fn variable
  274. * indicate. */
  275. apply = 0;
  276. kidx = 0;
  277. ++p;
  278. state = F_NAME;
  279. } else if (*p == '=') {
  280. if (kidx == 0)
  281. /* Illegal sequence. */
  282. return (-1);
  283. if (negative)
  284. /* We cannot accept "!opt-name=value". */
  285. return (-1);
  286. ++p;
  287. state = G_VALUE;
  288. } else if (*p == ' ') {
  289. /* Pass the space character */
  290. ++p;
  291. } else {
  292. /* Illegal character. */
  293. return (-1);
  294. }
  295. break;
  296. case G_VALUE:
  297. if (*p == ',') {
  298. if (vidx == 0)
  299. /* Illegal sequence. */
  300. return (-1);
  301. /* We have got option data. */
  302. ++p;
  303. if (apply)
  304. goto complete;
  305. else
  306. /* This option does not apply to the
  307. * format which the fn variable
  308. * indicate. */
  309. state = INIT;
  310. } else if (*p == ' ') {
  311. /* Pass the space character */
  312. ++p;
  313. } else {
  314. if (vidx >= valsize -1)
  315. /* Too many characters. */
  316. return (-1);
  317. val[vidx++] = *p++;
  318. }
  319. break;
  320. }
  321. }
  322. switch (state) {
  323. case F_BOTH:
  324. case F_NAME:
  325. if (kidx != 0) {
  326. if (!negative)
  327. val[vidx++] = '1';
  328. /* We have got boolean option. */
  329. if (apply)
  330. /* This option apply to the format which the
  331. * fn variable indicate. */
  332. goto complete;
  333. }
  334. break;
  335. case G_VALUE:
  336. if (vidx == 0)
  337. /* Illegal sequence. */
  338. return (-1);
  339. /* We have got option value. */
  340. if (apply)
  341. /* This option apply to the format which the fn
  342. * variable indicate. */
  343. goto complete;
  344. break;
  345. case INIT:/* nothing */
  346. break;
  347. }
  348. /* End of Option string. */
  349. return (0);
  350. complete:
  351. key[kidx] = '\0';
  352. val[vidx] = '\0';
  353. /* Return a size which we've consumed for detecting option */
  354. return ((int)(p - p_org));
  355. }