From 405f9537d22e077b88967ee64dd11a8315ac7fca Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Tue, 12 Nov 2024 20:29:23 +0200 Subject: [PATCH] Add example/compile_time.cpp --- doc/hash2/examples.adoc | 12 ++++++++++ example/Jamfile | 1 + example/compile_time.cpp | 48 ++++++++++++++++++++++++++++++++++++++++ test/Jamfile | 1 + 4 files changed, 62 insertions(+) create mode 100644 example/compile_time.cpp diff --git a/doc/hash2/examples.adoc b/doc/hash2/examples.adoc index 0c89a26..439cfe9 100644 --- a/doc/hash2/examples.adoc +++ b/doc/hash2/examples.adoc @@ -43,6 +43,8 @@ of a list of files, using a specified hash algorithm. The hash algorithm is passed as the first command line argument. +This example requires {cpp}14. + [source] ---- include::../../example/hash2sum.cpp[lines=7..-1] @@ -64,3 +66,13 @@ b07e42587d10ec323a25fd8fc3eef2213fb0997beb7950350f4e8a4b *canada.json 4ceee5a83ad320fedb0dfddfb6f80af50b99677e87158e2d039aa168 *citm_catalog.json 854ebe0da98cadd426ea0fa3218d60bb52cf6494e435d2f385a37d48 *twitter.json ---- + +## Compile Time Hashing + +This example demonstrates calculating the MD5 digest of a data array, embedded in the program source, at compile time. It requires {cpp}14. + +[source] +---- +include::../../example/compile_time.cpp[lines=5..-1] +---- + diff --git a/example/Jamfile b/example/Jamfile index d584355..8001c2a 100644 --- a/example/Jamfile +++ b/example/Jamfile @@ -5,3 +5,4 @@ project : default-build release static ; exe md5sum : md5sum.cpp ; exe hash2sum : hash2sum.cpp ; +exe compile_time : compile_time.cpp ; diff --git a/example/compile_time.cpp b/example/compile_time.cpp new file mode 100644 index 0000000..cd4db31 --- /dev/null +++ b/example/compile_time.cpp @@ -0,0 +1,48 @@ +// Copyright 2024 Peter Dimov +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +#include +#include + +// xxd -i resource +constexpr unsigned char resource[] = { + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x03,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9e, + 0xa0, 0xb6, 0x1d, 0xcd, 0xc2, 0x21, 0x9c, 0xb5, 0x44, 0xe3, 0x71, 0x03, + 0x1f, 0xe8, 0xbc, 0xb6, 0x06, 0x0a, 0x96, 0x07, 0xd3, 0x55, 0x8d, 0x08, + 0x42, 0x4a, 0x3b, 0x8c, 0xc2, 0x2c, 0xf1, 0x86, 0x4e, 0xdf, 0xc2, 0xf4, + 0xeb, 0xe1, 0xf4, 0x56, 0x9f, 0xae, 0xc5, 0x35, 0x67, 0xf4, 0x4d, 0x60, + 0x5d, 0xf8, 0xcf, 0xb8, 0x80, 0xa0, 0x20, 0x89, 0xef, 0x7b, 0xe6, 0x7c, + 0x9e, 0x67, 0xd6, 0x44, 0x13, 0x66, 0x5d, 0xcf, 0xff, 0x29, 0xd6, 0x49, + 0x96, 0x85, 0x0b, 0x7e, 0x01, 0x36, 0x66, 0x95, 0x6b, 0x80, 0x01, 0x00, + 0x00 +}; + +template constexpr auto md5( unsigned char const(&a)[ N ] ) +{ + boost::hash2::md5_128 hash; + hash.update( a, N ); + return hash.result(); +} + +constexpr auto resource_digest = md5( resource ); + +int main() +{ + std::cout << "Resource digest: " << resource_digest << std::endl; +} diff --git a/test/Jamfile b/test/Jamfile index 8c28bdc..19895c1 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -148,3 +148,4 @@ link ../benchmark/keys.cpp ; link ../example/md5sum.cpp ; link ../example/hash2sum.cpp : 11:no msvc-14.0:no ; +link ../example/compile_time.cpp : 11:no msvc-14.0:no ;