1 /*
2  *
3  * Copyright (c) 2009, 2010, 2011
4  * Phillip Lougher <phillip@squashfs.org.uk>
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 
69 static struct compressor unknown_comp_ops = {
70 	0, "unknown"
71 };
72 
73 
74 struct compressor *compressor[] = {
75 	&gzip_comp_ops,
76 	&lzma_comp_ops,
77 	&lzo_comp_ops,
78 	&lz4_comp_ops,
79 	&xz_comp_ops,
80 	&unknown_comp_ops
81 };
82 
83 
lookup_compressor(char * name)84 struct compressor *lookup_compressor(char *name)
85 {
86 	int i;
87 
88 	for(i = 0; compressor[i]->id; i++)
89 		if(strcmp(compressor[i]->name, name) == 0)
90 			break;
91 
92 	return compressor[i];
93 }
94 
95 
lookup_compressor_id(int id)96 struct compressor *lookup_compressor_id(int id)
97 {
98 	int i;
99 
100 	for(i = 0; compressor[i]->id; i++)
101 		if(id == compressor[i]->id)
102 			break;
103 
104 	return compressor[i];
105 }
106 
107 
display_compressors(char * indent,char * def_comp)108 void display_compressors(char *indent, char *def_comp)
109 {
110 	int i;
111 
112 	for(i = 0; compressor[i]->id; i++)
113 		if(compressor[i]->supported)
114 			fprintf(stderr, "%s\t%s%s\n", indent,
115 				compressor[i]->name,
116 				strcmp(compressor[i]->name, def_comp) == 0 ?
117 				" (default)" : "");
118 }
119 
120 
display_compressor_usage(char * def_comp)121 void display_compressor_usage(char *def_comp)
122 {
123 	int i;
124 
125 	for(i = 0; compressor[i]->id; i++)
126 		if(compressor[i]->supported) {
127 			char *str = strcmp(compressor[i]->name, def_comp) == 0 ?
128 				" (default)" : "";
129 			if(compressor[i]->usage) {
130 				fprintf(stderr, "\t%s%s\n",
131 					compressor[i]->name, str);
132 				compressor[i]->usage();
133 			} else
134 				fprintf(stderr, "\t%s (no options)%s\n",
135 					compressor[i]->name, str);
136 		}
137 }
138