1 /*
2 *
3 * Copyright (c) 2009, 2010, 2011
4 * Phillip Lougher <[email protected]>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2,
9 * or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 *
20 * compressor.c
21 */
22
23 #include <stdio.h>
24 #include <string.h>
25 #include "compressor.h"
26 #include "squashfs_fs.h"
27
28 #ifndef GZIP_SUPPORT
29 static struct compressor gzip_comp_ops = {
30 ZLIB_COMPRESSION, "gzip"
31 };
32 #else
33 extern struct compressor gzip_comp_ops;
34 #endif
35
36 #ifndef LZMA_SUPPORT
37 static struct compressor lzma_comp_ops = {
38 LZMA_COMPRESSION, "lzma"
39 };
40 #else
41 extern struct compressor lzma_comp_ops;
42 #endif
43
44 #ifndef LZO_SUPPORT
45 static struct compressor lzo_comp_ops = {
46 LZO_COMPRESSION, "lzo"
47 };
48 #else
49 extern struct compressor lzo_comp_ops;
50 #endif
51
52 #ifndef LZ4_SUPPORT
53 static struct compressor lz4_comp_ops = {
54 LZ4_COMPRESSION, "lz4"
55 };
56 #else
57 extern struct compressor lz4_comp_ops;
58 #endif
59
60 #ifndef XZ_SUPPORT
61 static struct compressor xz_comp_ops = {
62 XZ_COMPRESSION, "xz"
63 };
64 #else
65 extern struct compressor xz_comp_ops;
66 #endif
67
68 #ifndef ZSTD_SUPPORT
69 static struct compressor zstd_comp_ops = {
70 ZSTD_COMPRESSION, "zstd"
71 };
72 #else
73 extern struct compressor zstd_comp_ops;
74 #endif
75
76 static struct compressor unknown_comp_ops = {
77 0, "unknown"
78 };
79
80
81 struct compressor *compressor[] = {
82 &gzip_comp_ops,
83 &lzma_comp_ops,
84 &lzo_comp_ops,
85 &lz4_comp_ops,
86 &xz_comp_ops,
87 &zstd_comp_ops,
88 &unknown_comp_ops
89 };
90
91
lookup_compressor(char * name)92 struct compressor *lookup_compressor(char *name)
93 {
94 int i;
95
96 for(i = 0; compressor[i]->id; i++)
97 if(strcmp(compressor[i]->name, name) == 0)
98 break;
99
100 return compressor[i];
101 }
102
103
lookup_compressor_id(int id)104 struct compressor *lookup_compressor_id(int id)
105 {
106 int i;
107
108 for(i = 0; compressor[i]->id; i++)
109 if(id == compressor[i]->id)
110 break;
111
112 return compressor[i];
113 }
114
115
display_compressors(char * indent,char * def_comp)116 void display_compressors(char *indent, char *def_comp)
117 {
118 int i;
119
120 for(i = 0; compressor[i]->id; i++)
121 if(compressor[i]->supported)
122 fprintf(stderr, "%s\t%s%s\n", indent,
123 compressor[i]->name,
124 strcmp(compressor[i]->name, def_comp) == 0 ?
125 " (default)" : "");
126 }
127
128
display_compressor_usage(char * def_comp)129 void display_compressor_usage(char *def_comp)
130 {
131 int i;
132
133 for(i = 0; compressor[i]->id; i++)
134 if(compressor[i]->supported) {
135 char *str = strcmp(compressor[i]->name, def_comp) == 0 ?
136 " (default)" : "";
137 if(compressor[i]->usage) {
138 fprintf(stderr, "\t%s%s\n",
139 compressor[i]->name, str);
140 compressor[i]->usage();
141 } else
142 fprintf(stderr, "\t%s (no options)%s\n",
143 compressor[i]->name, str);
144 }
145 }
146