diff options
| author | Jack O'Connor <[email protected]> | 2020-10-20 12:27:57 -0400 |
|---|---|---|
| committer | Jack O'Connor <[email protected]> | 2020-10-20 12:51:30 -0400 |
| commit | 09546a677d4949b1ea5a7d289581ca0141971021 (patch) | |
| tree | 9e7f55fe4d98e4237f768dc23ea38f7438048511 | |
| parent | dae5dc5ef3bc2e366f7747f2bd77c82ba24b6259 (diff) | |
include example.c
| -rw-r--r-- | .github/workflows/ci.yml | 5 | ||||
| -rw-r--r-- | c/.gitignore | 1 | ||||
| -rw-r--r-- | c/Makefile.testing | 3 | ||||
| -rw-r--r-- | c/README.md | 5 | ||||
| -rw-r--r-- | c/example.c | 27 |
5 files changed, 39 insertions, 2 deletions
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5b42932..464a411 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -201,3 +201,8 @@ jobs: working-directory: ./c - run: BLAKE3_NO_SSE2=1 BLAKE3_NO_SSE41=1 BLAKE3_NO_AVX2=1 BLAKE3_NO_AVX512=1 make -f Makefile.testing test_asm working-directory: ./c + # Restore the files we deleted above. + - run: git checkout . + # Build the example. + - run: make -f Makefile.testing example + working-directory: ./c diff --git a/c/.gitignore b/c/.gitignore index c7a8b28..0bf608c 100644 --- a/c/.gitignore +++ b/c/.gitignore @@ -1,2 +1,3 @@ blake3 +example *.o diff --git a/c/Makefile.testing b/c/Makefile.testing index b4db5c8..41e6b82 100644 --- a/c/Makefile.testing +++ b/c/Makefile.testing @@ -71,5 +71,8 @@ test_asm: CFLAGS += -DBLAKE3_TESTING -fsanitize=address,undefined test_asm: asm ./test.py +example: example.c blake3.c blake3_dispatch.c blake3_portable.c $(ASM_TARGETS) + $(CC) $(CFLAGS) $(EXTRAFLAGS) $^ -o $@ $(LDFLAGS) + clean: rm -f $(NAME) *.o diff --git a/c/README.md b/c/README.md index 6211243..5e8b4e6 100644 --- a/c/README.md +++ b/c/README.md @@ -35,8 +35,9 @@ int main() { } ``` -If you save the example code above as `example.c`, and you're on x86\_64 -with a Unix-like OS, you can compile a working binary like this: +The code above is included in this directory as `example.c`. If you're +on x86\_64 with a Unix-like OS, you can compile a working binary like +this: ```bash gcc -O3 -o example example.c blake3.c blake3_dispatch.c blake3_portable.c \ diff --git a/c/example.c b/c/example.c new file mode 100644 index 0000000..02fe3c3 --- /dev/null +++ b/c/example.c @@ -0,0 +1,27 @@ +#include "blake3.h" +#include <stdio.h> +#include <unistd.h> + +int main() { + // Initialize the hasher. + blake3_hasher hasher; + blake3_hasher_init(&hasher); + + // Read input bytes from stdin. + unsigned char buf[65536]; + ssize_t n; + while ((n = read(STDIN_FILENO, buf, sizeof(buf))) > 0) { + blake3_hasher_update(&hasher, buf, n); + } + + // Finalize the hash. BLAKE3_OUT_LEN is the default output length, 32 bytes. + uint8_t output[BLAKE3_OUT_LEN]; + blake3_hasher_finalize(&hasher, output, BLAKE3_OUT_LEN); + + // Print the hash as hexadecimal. + for (size_t i = 0; i < BLAKE3_OUT_LEN; i++) { + printf("%02x", output[i]); + } + printf("\n"); + return 0; +} |
