2 * Dump mozilla style lz4json files.
4 * Copyright (c) 2014 Intel Corporation
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that: (1) source code distributions
9 * retain the above copyright notice and this paragraph in its entirety, (2)
10 * distributions including binary code include the above copyright notice and
11 * this paragraph in its entirety in the documentation or other materials
12 * provided with the distribution
14 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
16 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 /* File format reference:
20 http://mxr.mozilla.org/mozilla-central/source/toolkit/components/workerlz4/lz4.js
24 #include <sys/fcntl.h>
33 /* Assumes little endian host for the length */
35 int main(int ac, char **av)
38 int fd = open(*av, O_RDONLY);
44 if (fstat(fd, &st) < 0) {
48 if (st.st_size < 12) {
49 fprintf(stderr, "%s: file too short\n", *av);
53 unsigned ps = sysconf(_SC_PAGE_SIZE);
54 char *map = mmap(NULL, (st.st_size + ps - 1) & ~(ps - 1), PROT_READ,
56 if (map == (char *)-1) {
60 if (memcmp(map, "mozLz40", 8)) {
61 fprintf(stderr, "%s: not a mozLZ4a file\n", *av);
64 size_t outsz = *(uint32_t *) (map + 8);
65 char *out = malloc(outsz);
67 fprintf(stderr, "Cannot allocate memory\n");
70 if (LZ4_decompress_safe_partial(map + 12, out, st.st_size - 12, outsz, outsz) < 0) {
71 fprintf(stderr, "%s: decompression error\n", *av);
74 if (write(1, out, outsz) < outsz) {
79 munmap(map, (st.st_size + ps - 1) & ~(ps - 1));