1299425Smm/*-
2299425Smm * Copyright (c) 2003-2007 Tim Kientzle
3299425Smm * Copyright (c) 2015 Okhotnikov Kirill
4299425Smm * All rights reserved.
5299425Smm *
6299425Smm * Redistribution and use in source and binary forms, with or without
7299425Smm * modification, are permitted provided that the following conditions
8299425Smm * are met:
9299425Smm * 1. Redistributions of source code must retain the above copyright
10299425Smm *    notice, this list of conditions and the following disclaimer.
11299425Smm * 2. Redistributions in binary form must reproduce the above copyright
12299425Smm *    notice, this list of conditions and the following disclaimer in the
13299425Smm *    documentation and/or other materials provided with the distribution.
14299425Smm *
15299425Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16299425Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17299425Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18299425Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19299425Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20299425Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21299425Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22299425Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23299425Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24299425Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25299425Smm */
26299425Smm
27299425Smm#include "archive_platform.h"
28299425Smm__FBSDID("$FreeBSD: stable/10/contrib/libarchive/libarchive/archive_write_set_format_filter_by_ext.c 316338 2017-03-31 20:17:30Z mm $");
29299425Smm
30299425Smm#ifdef HAVE_SYS_TYPES_H
31299425Smm#include <sys/types.h>
32299425Smm#endif
33299425Smm
34299425Smm#ifdef HAVE_ERRNO_H
35299425Smm#include <errno.h>
36299425Smm#endif
37299425Smm#ifdef HAVE_STRING_H
38299425Smm#include <string.h>
39299425Smm#endif
40299425Smm
41299425Smm#include "archive.h"
42299425Smm#include "archive_private.h"
43299425Smm
44299425Smm/* A table that maps names to functions. */
45316338Smmstatic const
46299425Smmstruct { const char *name; int (*format)(struct archive *); int (*filter)(struct archive *);  } names[] =
47299425Smm{
48299425Smm	{ ".7z",	archive_write_set_format_7zip,            archive_write_add_filter_none},
49299425Smm	{ ".zip",	archive_write_set_format_zip,             archive_write_add_filter_none},
50299425Smm	{ ".jar",	archive_write_set_format_zip,             archive_write_add_filter_none},
51299425Smm	{ ".cpio",	archive_write_set_format_cpio,            archive_write_add_filter_none},
52299425Smm	{ ".iso",	archive_write_set_format_iso9660,         archive_write_add_filter_none},
53299425Smm#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__)
54299425Smm	{ ".a",	        archive_write_set_format_ar_bsd,          archive_write_add_filter_none},
55299425Smm	{ ".ar",	archive_write_set_format_ar_bsd,          archive_write_add_filter_none},
56299425Smm#else
57299425Smm	{ ".a",	        archive_write_set_format_ar_svr4,         archive_write_add_filter_none},
58299425Smm	{ ".ar",	archive_write_set_format_ar_svr4,         archive_write_add_filter_none},
59299425Smm#endif
60299425Smm	{ ".tar",	archive_write_set_format_pax_restricted,  archive_write_add_filter_none},
61299425Smm	{ ".tgz",	archive_write_set_format_pax_restricted,  archive_write_add_filter_gzip},
62299425Smm	{ ".tar.gz",	archive_write_set_format_pax_restricted,  archive_write_add_filter_gzip},
63299425Smm	{ ".tar.bz2",	archive_write_set_format_pax_restricted,  archive_write_add_filter_bzip2},
64299425Smm	{ ".tar.xz",	archive_write_set_format_pax_restricted,  archive_write_add_filter_xz},
65299425Smm	{ NULL,		NULL,                             NULL }
66299425Smm};
67299425Smm
68299425Smmstatic
69299425Smmint cmpsuff(const char *str, const char *suffix)
70299425Smm{
71299425Smm  size_t length_str, length_suffix;
72299425Smm
73299425Smm  if ((str == NULL) || (suffix == NULL))
74299425Smm    return -1;
75299425Smm
76299425Smm  length_str = strlen(str);
77299425Smm  length_suffix = strlen(suffix);
78299425Smm
79299425Smm  if (length_str >= length_suffix) {
80299425Smm    return strcmp(str + (length_str - length_suffix), suffix);
81299425Smm  } else {
82299425Smm    return -1;
83299425Smm  }
84299425Smm}
85299425Smm
86299425Smmstatic int get_array_index(const char *name)
87299425Smm{
88299425Smm  int i;
89299425Smm
90299425Smm  for (i = 0; names[i].name != NULL; i++)
91299425Smm  {
92299425Smm    if (cmpsuff(name, names[i].name) == 0)
93299425Smm      return i;
94299425Smm  }
95299425Smm  return -1;
96299425Smm
97299425Smm}
98299425Smm
99299425Smmint
100299425Smmarchive_write_set_format_filter_by_ext(struct archive *a, const char *filename)
101299425Smm{
102299425Smm  int names_index = get_array_index(filename);
103299425Smm
104299425Smm  if (names_index >= 0)
105299425Smm  {
106299425Smm    int format_state = (names[names_index].format)(a);
107299425Smm    if (format_state == ARCHIVE_OK)
108299425Smm      return ((names[names_index].filter)(a));
109299425Smm    else
110299425Smm      return format_state;
111299425Smm  }
112299425Smm
113299425Smm  archive_set_error(a, EINVAL, "No such format '%s'", filename);
114299425Smm  a->state = ARCHIVE_STATE_FATAL;
115299425Smm  return (ARCHIVE_FATAL);
116299425Smm}
117299425Smm
118299425Smmint
119299425Smmarchive_write_set_format_filter_by_ext_def(struct archive *a, const char *filename, const char * def_ext)
120299425Smm{
121299425Smm  int names_index = get_array_index(filename);
122299425Smm
123299425Smm  if (names_index < 0)
124299425Smm    names_index = get_array_index(def_ext);
125299425Smm
126299425Smm  if (names_index >= 0)
127299425Smm  {
128299425Smm    int format_state = (names[names_index].format)(a);
129299425Smm    if (format_state == ARCHIVE_OK)
130299425Smm      return ((names[names_index].filter)(a));
131299425Smm    else
132299425Smm      return format_state;
133299425Smm  }
134299425Smm
135299425Smm  archive_set_error(a, EINVAL, "No such format '%s'", filename);
136299425Smm  a->state = ARCHIVE_STATE_FATAL;
137299425Smm  return (ARCHIVE_FATAL);
138299425Smm}
139299425Smm
140299425Smm
141299425Smm
142299425Smm
143