]>
git.saurik.com Git - apple/hfs.git/blob - tests/img-to-c.c
11 void *zalloc(__unused
void *opaque
, uInt items
, uInt size
)
13 return malloc(items
* size
);
16 void zfree(__unused
void *opaque
, void *ptr
)
21 int main(int argc
, char *argv
[])
23 const int fixed_args
= 4;
24 char *args
[argc
+ fixed_args
];
26 char template[] = "/tmp/img-to-c.XXXXXXXX";
27 char *dir
= mkdtemp(template);
30 err(1, "mkdtemp failed");
33 asprintf(&path
, "%s/img.sparseimage", dir
);
41 for (i
= 1; i
< argc
; ++i
)
42 args
[i
+ fixed_args
- 1] = argv
[i
];
43 args
[i
+ fixed_args
- 1] = NULL
;
46 posix_spawn(&pid
, "/usr/bin/hdiutil", NULL
, NULL
, args
, NULL
);
49 waitpid(pid
, &status
, 0);
51 if (!WIFEXITED(status
) || WEXITSTATUS(status
))
52 err(1, "hdiutil failed");
54 int fd
= open(path
, O_RDONLY
);
61 deflateInit(&zs
, Z_DEFAULT_COMPRESSION
);
63 const size_t buf_size
= 1024 * 1024;
65 unsigned char *out_buf
= malloc(buf_size
);
68 unsigned char *in_buf
= malloc(buf_size
);
70 printf("unsigned char data[] = {");
77 ssize_t amt
= read(fd
, zs
.next_in
, &in_buf
[buf_size
] - zs
.next_in
);
85 zs
.next_out
= out_buf
;
86 zs
.avail_out
= buf_size
;
90 memmove(in_buf
, zs
.next_in
, zs
.avail_in
);
93 for (unsigned i
= 0; i
< buf_size
- zs
.avail_out
; ++i
) {
96 printf("0x%02x, ", out_buf
[i
]);
99 } while (!flush
|| zs
.avail_in
);