-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathzuncompress.c
134 lines (115 loc) · 2.66 KB
/
zuncompress.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <stdio.h>
#include <stdio_ext.h>
#include <stdlib.h>
#include <ctype.h>
#include <sys/zfs_context.h>
#include <sys/spa.h>
#include <sys/spa_impl.h>
#include <sys/dmu.h>
#include <sys/zap.h>
#include <sys/fs/zfs.h>
#include <sys/zfs_znode.h>
#include <sys/vdev.h>
#include <sys/vdev_impl.h>
#include <sys/metaslab_impl.h>
#include <sys/dmu_objset.h>
#include <sys/dsl_dir.h>
#include <sys/dsl_dataset.h>
#include <sys/dsl_pool.h>
#include <sys/dbuf.h>
#include <sys/zil.h>
#include <sys/zil_impl.h>
#include <sys/stat.h>
#include <sys/resource.h>
#include <sys/dmu_traverse.h>
#include <sys/zio_checksum.h>
#include <sys/zio_compress.h>
#include <sys/zfs_fuid.h>
#include <sys/arc.h>
#undef ZFS_MAXNAMELEN
#undef verify
#include <libzfs.h>
#include <sys/fcntl.h>
#include <umem.h>
#include <strings.h>
#define STARTOFFSET (0x400000)
int
main(int argc, char *argv[])
{
int fd, i;
uint64_t psize = 0, lsize = 0, offset = STARTOFFSET;
char *sbuf;
char *dbuf;
char *ca = "lzjb";
char *infile;
int copt;
enum zio_compress c;
while ((copt = getopt(argc, argv, "l:p:c:o:")) != -1) {
switch(copt) {
case 'l':
lsize = strtoull(optarg, &optarg, 16);
if (lsize == 0) {
fprintf(stderr, "Error: unknown lsize\n");
exit(1);
}
break;
case 'p':
psize = strtoull(optarg, &optarg, 16);
if (psize == 0) {
fprintf(stderr, "Error: unknown psize\n");
exit(1);
}
break;
case 'c':
ca = optarg;
break;
case 'o':
offset += strtoull(optarg, &optarg, 16);
break;
case '?':
fprintf(stderr, "Usage: %s -l lsize -p psize [-c compression_algorithm] [input_file]\n",
argv[0]);
exit(1);
}
}
if (optind < argc) {
infile = argv[optind];
if ((fd = open(infile, O_RDONLY)) < 0) {
perror("Cannot open input file\n");
exit(1);
}
} else
fd = 0; /* stdin */
if (lsize == 0 || psize == 0) {
fprintf(stderr, "Usage: %s -l lsize -p psize [-c compression_algorithm] [input_file]\n",
argv[0]);
exit(1);
}
for (i = 0; i < ZIO_COMPRESS_FUNCTIONS; i++) {
if (strncmp(ca, zio_compress_table[i].ci_name,
strlen(zio_compress_table[i].ci_name)) == 0)
break;
}
if (i >= ZIO_COMPRESS_FUNCTIONS) {
(void)fprintf(stderr, "***Unknown compression type: '%s'\n", ca);
exit(1);
}
sbuf = malloc(psize);
dbuf = malloc(lsize);
if (sbuf == NULL || dbuf == NULL) {
fprintf(stderr, "failed to allocate space for source/destination buffers\n");
exit(1);
}
if (pread(fd, sbuf, psize, offset) != psize) {
perror("error reading input file\n");
exit(1);
}
if (zio_decompress_data(i, sbuf, dbuf, psize, lsize) != 0) {
perror("decompress failed\n");
exit(1);
}
write(1, dbuf, lsize);
free(sbuf);
free(dbuf);
exit(0);
}