From f3538d9117b2cc67e7229321900e1b31286c835a Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sat, 30 Jun 2018 10:41:04 +1000 Subject: [PATCH] Add example/crc32 --- build-all.sh | 7 ++++- example/crc32/crc32.cc | 71 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 example/crc32/crc32.cc diff --git a/build-all.sh b/build-all.sh index 1693c64c..5f7adb4b 100755 --- a/build-all.sh +++ b/build-all.sh @@ -45,7 +45,12 @@ wuffs bench -skipgen -mimic -reps=1 -iterscale=1 for f in example/*; do echo Building $f - if [ "$f" = "example/library" ]; then + if [ "$f" = "example/crc32" ]; then + # example/crc32 is unusual in that it's C++, not C. + g++ $f/*.cc -o $f/a.out + elif [ "$f" = "example/library" ]; then + # example/library is unusual in that it uses separately compiled libraries + # instead of directly #include'ing Wuffs' .c files. gcc -Wall -Werror -static -I.. $f/*.c gen/lib/c/gcc-static/libwuffs.a -o $f/a.out elif [ -e $f/*.c ]; then gcc -Wall -Werror $f/*.c -o $f/a.out diff --git a/example/crc32/crc32.cc b/example/crc32/crc32.cc new file mode 100644 index 00000000..76eab10c --- /dev/null +++ b/example/crc32/crc32.cc @@ -0,0 +1,71 @@ +// 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. + +/* +crc32 prints the CRC-32 checksum (using the IEEE polynomial) of stdin. It is +similar to the standard /usr/bin/crc32 program, except that this example +program only reads from stdin. + +This example program differs from the other example Wuffs programs in that it +is written in C++, not C. + +$cxx crc32.cc && ./a.out < ../../README.md; rm -f a.out + +for a C++ compiler $cxx, such as clang++ or g++. +*/ + +#include +#include +#include +#include + +// 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/base.c" +#include "../../gen/c/std/crc32.c" + +#ifndef SRC_BUFFER_SIZE +#define SRC_BUFFER_SIZE (16 * 1024) +#endif + +uint8_t src_buffer[SRC_BUFFER_SIZE]; + +int main(int argc, char** argv) { + wuffs_crc32__ieee_hasher h = ((wuffs_crc32__ieee_hasher){}); + wuffs_crc32__ieee_hasher__check_wuffs_version(&h, sizeof h, WUFFS_VERSION); + + while (true) { + const int stdin_fd = 0; + ssize_t n = read(stdin_fd, src_buffer, SRC_BUFFER_SIZE); + if (n < 0) { + if (errno != EINTR) { + fprintf(stderr, "%s\n", strerror(errno)); + return 1; + } + continue; + } + + uint32_t checksum = + wuffs_crc32__ieee_hasher__update(&h, ((wuffs_base__slice_u8){ + .ptr = src_buffer, + .len = static_cast(n), + })); + + if (n == 0) { + printf("%08" PRIx32 "\n", checksum); + return 0; + } + } +}