Add example/zcat

This commit is contained in:
Nigel Tao committed 2018-03-28 13:51:01 +11:00
1 parent ef899b0492
commit 707965488c
3 files changed
+138 -20

No files matched your search

+17 -16
View File
@@ -36,23 +36,23 @@ for a C compiler $cc, such as clang or gcc.
//
// We keep the whole input in memory, instead of one-pass stream processing,
// because playing a looping animation requires re-winding the input.
#define SRC_BUF_SIZE (64 * 1024 * 1024)
#define SRC_BUFFER_SIZE (64 * 1024 * 1024)
#define MAX_DIMENSION (4096)
uint8_t src_buf[SRC_BUF_SIZE] = {0};
uint8_t src_buffer[SRC_BUFFER_SIZE] = {0};
size_t src_len = 0;
uint8_t* dst_buf = NULL;
uint8_t* dst_buffer = NULL;
size_t dst_len = 0;
uint8_t* print_buf = NULL;
uint8_t* print_buffer = NULL;
size_t print_len = 0;
bool seen_num_loops = false;
uint32_t num_loops_remaining = 0;
const char* read_stdin() {
while (src_len < SRC_BUF_SIZE) {
while (src_len < SRC_BUFFER_SIZE) {
const int stdin_fd = 0;
ssize_t n = read(stdin_fd, src_buf + src_len, SRC_BUF_SIZE - src_len);
ssize_t n = read(stdin_fd, src_buffer + src_len, SRC_BUFFER_SIZE - src_len);
if (n > 0) {
src_len += n;
} else if (n == 0) {
@@ -71,7 +71,7 @@ const char* play() {
wuffs_gif__decoder__initialize(&dec, WUFFS_VERSION, 0);
wuffs_base__buf1 src = {
.ptr = src_buf, .len = src_len, .wi = src_len, .closed = true};
.ptr = src_buffer, .len = src_len, .wi = src_len, .closed = true};
wuffs_base__reader1 src_reader = {.buf = &src};
wuffs_base__image_config ic = {{0}};
@@ -88,18 +88,18 @@ const char* play() {
if ((width > MAX_DIMENSION) || (height > MAX_DIMENSION)) {
return "image dimensions are too large";
}
if (!dst_buf) {
if (!dst_buffer) {
dst_len = wuffs_base__image_config__pixbuf_size(&ic);
dst_buf = malloc(dst_len);
if (!dst_buf) {
dst_buffer = malloc(dst_len);
if (!dst_buffer) {
return "could not allocate dst buffer";
}
uint64_t plen = 1 + ((uint64_t)(width) + 1) * (uint64_t)(height);
if (plen <= (uint64_t)SIZE_MAX) {
print_len = (size_t)plen;
print_buf = malloc(print_len);
print_buffer = malloc(print_len);
}
if (!print_buf) {
if (!print_buffer) {
return "could not allocate print buffer";
}
}
@@ -111,7 +111,7 @@ const char* play() {
}
while (true) {
wuffs_base__buf1 dst = {.ptr = dst_buf, .len = dst_len};
wuffs_base__buf1 dst = {.ptr = dst_buffer, .len = dst_len};
wuffs_base__writer1 dst_writer = {.buf = &dst};
// TODO: provide API and support for when the frame rect is different from
// the image rect.
@@ -129,8 +129,8 @@ const char* play() {
// usleep arg should also take into account the decoding time.
usleep(100000);
uint8_t* d = dst_buf;
uint8_t* p = print_buf;
uint8_t* d = dst_buffer;
uint8_t* p = print_buffer;
*p++ = '\n';
uint32_t y;
for (y = 0; y < height; y++) {
@@ -143,7 +143,8 @@ const char* play() {
}
*p++ = '\n';
}
write(1, print_buf, print_len);
const int stdout_fd = 1;
write(stdout_fd, print_buffer, print_len);
}
return NULL;
}
+1 -4
View File
@@ -21,10 +21,7 @@ for a C compiler $cc, such as clang or gcc.
*/
// TODO: delete this program, as example/gifplayer is a more interesting
// example of Wuffs' GIF codec. This program's seccomp code should move
// somewhere before we delete it, though, as that's still a feature worth
// demonstrating. The gifplayer program can't use SECCOMP_MODE_STRICT, as it
// needs to sleep between animation frames.
// example of Wuffs' GIF codec.
#include <string.h>
#include <unistd.h>
+120
View File
@@ -0,0 +1,120 @@
// Copyright 2018 The Wuffs Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/*
zcat decodes gzip'ed data to stdout. It is similar to the standard /bin/zcat
program, except that this example program only reads from stdin. On Linux, it
also self-imposes a SECCOMP_MODE_STRICT sandbox. To run:
$cc zcat.c && ./a.out < ../../test/data/romeo.txt.gz; rm -f a.out
for a C compiler $cc, such as clang or gcc.
*/
#include <errno.h>
#include <unistd.h>
// If building this program in an environment that doesn't easily accomodate
// relative includes, you can use the script/inline-c-relative-includes.go
// program to generate a stand-alone C file.
#include "../../gen/c/std/crc32.c"
#include "../../gen/c/std/deflate.c"
#include "../../gen/c/std/gzip.c"
#ifdef __linux__
#include <linux/prctl.h>
#include <linux/seccomp.h>
#include <sys/prctl.h>
#include <sys/syscall.h>
#define WUFFS_EXAMPLE_USE_SECCOMP
#endif
#ifndef DST_BUFFER_SIZE
#define DST_BUFFER_SIZE (16 * 1024)
#endif
#ifndef SRC_BUFFER_SIZE
#define SRC_BUFFER_SIZE (16 * 1024)
#endif
char dst_buffer[DST_BUFFER_SIZE];
char src_buffer[SRC_BUFFER_SIZE];
static const char* decode() {
wuffs_gzip__decoder dec;
wuffs_gzip__decoder__initialize(&dec, WUFFS_VERSION, 0);
while (true) {
const int stdin_fd = 0;
ssize_t n_src = read(stdin_fd, src_buffer, SRC_BUFFER_SIZE);
if (n_src < 0) {
if (errno != EINTR) {
return strerror(errno);
}
continue;
}
wuffs_base__buf1 src = {.ptr = src_buffer,
.len = SRC_BUFFER_SIZE,
.wi = n_src,
.closed = n_src == 0};
wuffs_base__reader1 src_reader = {.buf = &src};
while (true) {
wuffs_base__buf1 dst = {.ptr = dst_buffer, .len = DST_BUFFER_SIZE};
wuffs_base__writer1 dst_writer = {.buf = &dst};
wuffs_gzip__status s =
wuffs_gzip__decoder__decode(&dec, dst_writer, src_reader);
if (dst.wi) {
// TODO: handle EINTR and other write errors; see "man 2 write".
const int stdout_fd = 1;
write(stdout_fd, dst_buffer, dst.wi);
}
if (s == WUFFS_GZIP__STATUS_OK) {
return NULL;
}
if (s == WUFFS_GZIP__SUSPENSION_SHORT_READ) {
break;
}
if (s != WUFFS_GZIP__SUSPENSION_SHORT_WRITE) {
return wuffs_gzip__status__string(s);
}
}
}
}
int fail(const char* msg) {
const int stderr_fd = 2;
write(stderr_fd, msg, strnlen(msg, 4095));
write(stderr_fd, "\n", 1);
return 1;
}
int main(int argc, char** argv) {
#ifdef WUFFS_EXAMPLE_USE_SECCOMP
prctl(PR_SET_SECCOMP, SECCOMP_MODE_STRICT);
#endif
const char* msg = decode();
int status = msg ? fail(msg) : 0;
#ifdef WUFFS_EXAMPLE_USE_SECCOMP
// Call SYS_exit explicitly instead of SYS_exit_group implicitly.
// SECCOMP_MODE_STRICT allows only the former.
syscall(SYS_exit, status);
#endif
return status;
}