archive_util.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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. static void
  41. errmsg(const char *m)
  42. {
  43. size_t s = strlen(m);
  44. ssize_t written;
  45. while (s > 0) {
  46. written = write(2, m, strlen(m));
  47. if (written <= 0)
  48. return;
  49. m += written;
  50. s -= written;
  51. }
  52. }
  53. #if ARCHIVE_VERSION_NUMBER < 3000000
  54. /* These disappear in libarchive 3.0 */
  55. /* Deprecated. */
  56. int
  57. archive_api_feature(void)
  58. {
  59. return (ARCHIVE_API_FEATURE);
  60. }
  61. /* Deprecated. */
  62. int
  63. archive_api_version(void)
  64. {
  65. return (ARCHIVE_API_VERSION);
  66. }
  67. /* Deprecated synonym for archive_version_number() */
  68. int
  69. archive_version_stamp(void)
  70. {
  71. return (archive_version_number());
  72. }
  73. /* Deprecated synonym for archive_version_string() */
  74. const char *
  75. archive_version(void)
  76. {
  77. return (archive_version_string());
  78. }
  79. #endif
  80. int
  81. archive_version_number(void)
  82. {
  83. return (ARCHIVE_VERSION_NUMBER);
  84. }
  85. const char *
  86. archive_version_string(void)
  87. {
  88. return (ARCHIVE_VERSION_STRING);
  89. }
  90. int
  91. archive_errno(struct archive *a)
  92. {
  93. return (a->archive_error_number);
  94. }
  95. const char *
  96. archive_error_string(struct archive *a)
  97. {
  98. if (a->error != NULL && *a->error != '\0')
  99. return (a->error);
  100. else
  101. return ("(Empty error message)");
  102. }
  103. int
  104. archive_file_count(struct archive *a)
  105. {
  106. return (a->file_count);
  107. }
  108. int
  109. archive_format(struct archive *a)
  110. {
  111. return (a->archive_format);
  112. }
  113. const char *
  114. archive_format_name(struct archive *a)
  115. {
  116. return (a->archive_format_name);
  117. }
  118. int
  119. archive_compression(struct archive *a)
  120. {
  121. return (a->compression_code);
  122. }
  123. const char *
  124. archive_compression_name(struct archive *a)
  125. {
  126. return (a->compression_name);
  127. }
  128. /*
  129. * Return a count of the number of compressed bytes processed.
  130. */
  131. int64_t
  132. archive_position_compressed(struct archive *a)
  133. {
  134. return (a->raw_position);
  135. }
  136. /*
  137. * Return a count of the number of uncompressed bytes processed.
  138. */
  139. int64_t
  140. archive_position_uncompressed(struct archive *a)
  141. {
  142. return (a->file_position);
  143. }
  144. void
  145. archive_clear_error(struct archive *a)
  146. {
  147. archive_string_empty(&a->error_string);
  148. a->error = NULL;
  149. a->archive_error_number = 0;
  150. }
  151. void
  152. archive_set_error(struct archive *a, int error_number, const char *fmt, ...)
  153. {
  154. va_list ap;
  155. a->archive_error_number = error_number;
  156. if (fmt == NULL) {
  157. a->error = NULL;
  158. return;
  159. }
  160. va_start(ap, fmt);
  161. archive_string_vsprintf(&(a->error_string), fmt, ap);
  162. va_end(ap);
  163. a->error = a->error_string.s;
  164. }
  165. void
  166. archive_copy_error(struct archive *dest, struct archive *src)
  167. {
  168. dest->archive_error_number = src->archive_error_number;
  169. archive_string_copy(&dest->error_string, &src->error_string);
  170. dest->error = dest->error_string.s;
  171. }
  172. void
  173. __archive_errx(int retvalue, const char *msg)
  174. {
  175. static const char *msg1 = "Fatal Internal Error in libarchive: ";
  176. errmsg(msg1);
  177. errmsg(msg);
  178. errmsg("\n");
  179. exit(retvalue);
  180. }
  181. /*
  182. * Parse option strings
  183. * Detail of option format.
  184. * - The option can accept:
  185. * "opt-name", "!opt-name", "opt-name=value".
  186. *
  187. * - The option entries are separated by comma.
  188. * e.g "compression=9,opt=XXX,opt-b=ZZZ"
  189. *
  190. * - The name of option string consist of '-' and alphabet
  191. * but character '-' cannot be used for the first character.
  192. * (Regular expression is [a-z][-a-z]+)
  193. *
  194. * - For a specific format/filter, using the format name with ':'.
  195. * e.g "zip:compression=9"
  196. * (This "compression=9" option entry is for "zip" format only)
  197. *
  198. * If another entries follow it, those are not for
  199. * the specfic format/filter.
  200. * e.g handle "zip:compression=9,opt=XXX,opt-b=ZZZ"
  201. * "zip" format/filter handler will get "compression=9"
  202. * all format/filter handler will get "opt=XXX"
  203. * all format/filter handler will get "opt-b=ZZZ"
  204. *
  205. * - Whitespace and tab are bypassed.
  206. *
  207. */
  208. int
  209. __archive_parse_options(const char *p, const char *fn, int keysize, char *key,
  210. int valsize, char *val)
  211. {
  212. const char *p_org;
  213. int apply;
  214. int kidx, vidx;
  215. int negative;
  216. enum {
  217. /* Requested for initialization. */
  218. INIT,
  219. /* Finding format/filter-name and option-name. */
  220. F_BOTH,
  221. /* Finding option-name only.
  222. * (already detected format/filter-name) */
  223. F_NAME,
  224. /* Getting option-value. */
  225. G_VALUE,
  226. } state;
  227. p_org = p;
  228. state = INIT;
  229. kidx = vidx = negative = 0;
  230. apply = 1;
  231. while (*p) {
  232. switch (state) {
  233. case INIT:
  234. kidx = vidx = 0;
  235. negative = 0;
  236. apply = 1;
  237. state = F_BOTH;
  238. break;
  239. case F_BOTH:
  240. case F_NAME:
  241. if ((*p >= 'a' && *p <= 'z') ||
  242. (*p >= '0' && *p <= '9') || *p == '-') {
  243. if (kidx == 0 && !(*p >= 'a' && *p <= 'z'))
  244. /* Illegal sequence. */
  245. return (-1);
  246. if (kidx >= keysize -1)
  247. /* Too many characters. */
  248. return (-1);
  249. key[kidx++] = *p++;
  250. } else if (*p == '!') {
  251. if (kidx != 0)
  252. /* Illegal sequence. */
  253. return (-1);
  254. negative = 1;
  255. ++p;
  256. } else if (*p == ',') {
  257. if (kidx == 0)
  258. /* Illegal sequence. */
  259. return (-1);
  260. if (!negative)
  261. val[vidx++] = '1';
  262. /* We have got boolean option data. */
  263. ++p;
  264. if (apply)
  265. goto complete;
  266. else
  267. /* This option does not apply to the
  268. * format which the fn variable
  269. * indicate. */
  270. state = INIT;
  271. } else if (*p == ':') {
  272. /* obuf data is format name */
  273. if (state == F_NAME)
  274. /* We already found it. */
  275. return (-1);
  276. if (kidx == 0)
  277. /* Illegal sequence. */
  278. return (-1);
  279. if (negative)
  280. /* We cannot accept "!format-name:". */
  281. return (-1);
  282. key[kidx] = '\0';
  283. if (strcmp(fn, key) != 0)
  284. /* This option does not apply to the
  285. * format which the fn variable
  286. * indicate. */
  287. apply = 0;
  288. kidx = 0;
  289. ++p;
  290. state = F_NAME;
  291. } else if (*p == '=') {
  292. if (kidx == 0)
  293. /* Illegal sequence. */
  294. return (-1);
  295. if (negative)
  296. /* We cannot accept "!opt-name=value". */
  297. return (-1);
  298. ++p;
  299. state = G_VALUE;
  300. } else if (*p == ' ') {
  301. /* Pass the space character */
  302. ++p;
  303. } else {
  304. /* Illegal character. */
  305. return (-1);
  306. }
  307. break;
  308. case G_VALUE:
  309. if (*p == ',') {
  310. if (vidx == 0)
  311. /* Illegal sequence. */
  312. return (-1);
  313. /* We have got option data. */
  314. ++p;
  315. if (apply)
  316. goto complete;
  317. else
  318. /* This option does not apply to the
  319. * format which the fn variable
  320. * indicate. */
  321. state = INIT;
  322. } else if (*p == ' ') {
  323. /* Pass the space character */
  324. ++p;
  325. } else {
  326. if (vidx >= valsize -1)
  327. /* Too many characters. */
  328. return (-1);
  329. val[vidx++] = *p++;
  330. }
  331. break;
  332. }
  333. }
  334. switch (state) {
  335. case F_BOTH:
  336. case F_NAME:
  337. if (kidx != 0) {
  338. if (!negative)
  339. val[vidx++] = '1';
  340. /* We have got boolean option. */
  341. if (apply)
  342. /* This option apply to the format which the
  343. * fn variable indicate. */
  344. goto complete;
  345. }
  346. break;
  347. case G_VALUE:
  348. if (vidx == 0)
  349. /* Illegal sequence. */
  350. return (-1);
  351. /* We have got option value. */
  352. if (apply)
  353. /* This option apply to the format which the fn
  354. * variable indicate. */
  355. goto complete;
  356. break;
  357. case INIT:/* nothing */
  358. break;
  359. }
  360. /* End of Option string. */
  361. return (0);
  362. complete:
  363. key[kidx] = '\0';
  364. val[vidx] = '\0';
  365. /* Return a size which we've consumed for detecting option */
  366. return ((int)(p - p_org));
  367. }