6 #define CD_FRAMESIZE_RAW 2352
11 } __attribute__((packed));
13 int main(int argc, char *argv[])
15 unsigned char outbuf[CD_FRAMESIZE_RAW * 2];
16 unsigned char inbuf[CD_FRAMESIZE_RAW];
17 struct ztab_entry *ztable;
18 char *out_basename, *out_fname, *out_tfname;
20 long in_bytes, out_bytes;
21 long s, total_sectors;
25 fprintf(stderr, "usage:\n%s <cd_img> [out_basename]\n", argv[0]);
29 fin = fopen(argv[1], "rb");
31 fprintf(stderr, "fopen %s: ", argv[1]);
37 out_basename = argv[2];
39 out_basename = argv[1];
41 len = strlen(out_basename) + 3;
42 out_fname = malloc(len);
43 if (out_fname == NULL) {
44 fprintf(stderr, "OOM\n");
47 snprintf(out_fname, len, "%s.Z", out_basename);
49 fout = fopen(out_fname, "wb");
51 fprintf(stderr, "fopen %s: ", out_fname);
56 if (fseek(fin, 0, SEEK_END) != 0) {
57 fprintf(stderr, "fseek failed: ");
62 in_bytes = ftell(fin);
63 if (in_bytes % CD_FRAMESIZE_RAW) {
64 fprintf(stderr, "warning: input size %ld is not "
65 "multiple of sector size\n", in_bytes);
67 total_sectors = in_bytes / CD_FRAMESIZE_RAW;
68 fseek(fin, 0, SEEK_SET);
70 ztable = calloc(total_sectors, sizeof(ztable[0]));
72 fprintf(stderr, "OOM\n");
77 for (s = 0; s < total_sectors; s++) {
78 uLongf dest_len = sizeof(outbuf);
80 ret = fread(inbuf, 1, sizeof(inbuf), fin);
81 if (ret != sizeof(inbuf)) {
83 fprintf(stderr, "fread returned %d\n", ret);
87 ret = compress2(outbuf, &dest_len, inbuf, sizeof(inbuf), 9);
90 fprintf(stderr, "compress2 failed: %d\n", ret);
94 ret = fwrite(outbuf, 1, dest_len, fout);
95 if (ret != dest_len) {
97 fprintf(stderr, "fwrite returned %d\n", ret);
101 ztable[s].offset = out_bytes;
102 ztable[s].size = dest_len;
103 out_bytes += dest_len;
106 if ((s & 0x1ff) == 0) {
107 printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
108 printf("%3ld%% %ld/%ld", s * 100 / total_sectors, s, total_sectors);
117 len = strlen(out_fname) + 7;
118 out_tfname = malloc(len);
119 if (out_tfname == NULL) {
121 fprintf(stderr, "OOM\n");
124 snprintf(out_tfname, len, "%s.table", out_fname);
126 fout = fopen(out_tfname, "wb");
128 fprintf(stderr, "fopen %s: ", out_tfname);
133 ret = fwrite(ztable, sizeof(ztable[0]), total_sectors, fout);
134 if (ret != total_sectors) {
136 fprintf(stderr, "fwrite returned %d\n", ret);
141 printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
142 printf("%3ld%% %ld/%ld\n", s * 100 / total_sectors, s, total_sectors);
143 printf("%ld bytes from %ld (%.1f%%)\n", out_bytes, in_bytes,
144 (double)out_bytes * 100.0 / in_bytes);