diff --git a/cdatapack/cdatapack_get.c b/cdatapack/cdatapack_get.c --- a/cdatapack/cdatapack_get.c +++ b/cdatapack/cdatapack_get.c @@ -6,12 +6,12 @@ #include #include -#include #include #include +#include "cdatapack.h" +#include "clib/sha1.h" #include "convert.h" -#include "cdatapack.h" #define DATAIDX_EXT ".dataidx" #define DATAPACK_EXT ".datapack" @@ -81,10 +81,10 @@ for (int ix = 0; ix < chain.links_count; ix ++) { delta_chain_link_t *link = &chain.delta_chain_links[ix]; - SHA1_CTX ctx; - SHA1DCInit(&ctx); - SHA1DCUpdate(&ctx, link->delta, link->delta_sz); - SHA1DCFinal(sha, &ctx); + fbhg_sha1_ctx_t ctx; + fbhg_sha1_init(&ctx); + fbhg_sha1_update(&ctx, link->delta, link->delta_sz); + fbhg_sha1_final(sha, &ctx); if (last_filename_sz != link->filename_sz || memcmp(last_filename, link->filename, last_filename_sz) != 0) { diff --git a/cfastmanifest/checksum.c b/cfastmanifest/checksum.c --- a/cfastmanifest/checksum.c +++ b/cfastmanifest/checksum.c @@ -5,14 +5,13 @@ // // no-check-code -#include - +#include "clib/sha1.h" #include "node.h" #include "tree.h" static void update_checksum(node_t *node) { - SHA1_CTX ctx; - SHA1DCInit(&ctx); + fbhg_sha1_ctx_t ctx; + fbhg_sha1_init(&ctx); // find all the children and make sure their checksums are up-to-date. for (child_num_t ix = 0; ix < node->num_children; ix++) { @@ -21,12 +20,12 @@ update_checksum(child); } - SHA1DCUpdate(&ctx, (const unsigned char*) child->name, child->name_sz); - SHA1DCUpdate(&ctx, child->checksum, child->checksum_sz); - SHA1DCUpdate(&ctx, &child->flags, 1); + fbhg_sha1_update(&ctx, child->name, child->name_sz); + fbhg_sha1_update(&ctx, child->checksum, child->checksum_sz); + fbhg_sha1_update(&ctx, &child->flags, 1); } - SHA1DCFinal(node->checksum, &ctx); + fbhg_sha1_final(node->checksum, &ctx); node->checksum_sz = SHA1_BYTES; node->checksum_valid = true; } diff --git a/clib/sha1.h b/clib/sha1.h new file mode 100644 --- /dev/null +++ b/clib/sha1.h @@ -0,0 +1,38 @@ +// Copyright (c) 2017-present, Facebook, Inc. +// All Rights Reserved. +// +// This software may be used and distributed according to the terms of the +// GNU General Public License version 2 or any later version. + +// sha1.h - wrapper functions around the underlying SHA-1 implementation. +// +// no-check-code +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include + +typedef SHA1_CTX fbhg_sha1_ctx_t; + +static inline int fbhg_sha1_init(fbhg_sha1_ctx_t* ctx) { + SHA1DCInit(ctx); + return 0; +} + +static inline int +fbhg_sha1_update(fbhg_sha1_ctx_t* ctx, const void* data, unsigned long length) { + SHA1DCUpdate(ctx, (const unsigned char*)data, length); + return 0; +} + +static inline int fbhg_sha1_final(unsigned char* md, fbhg_sha1_ctx_t* ctx) { + return SHA1DCFinal(md, ctx); +} + +#ifdef __cplusplus +} /* extern C */ +#endif diff --git a/ctreemanifest/manifest.cpp b/ctreemanifest/manifest.cpp --- a/ctreemanifest/manifest.cpp +++ b/ctreemanifest/manifest.cpp @@ -7,9 +7,8 @@ // // no-check-code -#include - #include "manifest.h" +#include "clib/sha1.h" Manifest::Manifest(ConstantStringRef &rawobj, const char *node) : _rawobj(rawobj), @@ -232,19 +231,19 @@ std::string content; this->serialize(content); - SHA1_CTX ctx; - SHA1DCInit(&ctx); + fbhg_sha1_ctx_t ctx; + fbhg_sha1_init(&ctx); if (memcmp(p1, p2, BIN_NODE_SIZE) == -1) { - SHA1DCUpdate(&ctx, (const unsigned char *) p1, BIN_NODE_SIZE); - SHA1DCUpdate(&ctx, (const unsigned char *) p2, BIN_NODE_SIZE); + fbhg_sha1_update(&ctx, p1, BIN_NODE_SIZE); + fbhg_sha1_update(&ctx, p2, BIN_NODE_SIZE); } else { - SHA1DCUpdate(&ctx, (const unsigned char *)p2, BIN_NODE_SIZE); - SHA1DCUpdate(&ctx, (const unsigned char *)p1, BIN_NODE_SIZE); + fbhg_sha1_update(&ctx, p2, BIN_NODE_SIZE); + fbhg_sha1_update(&ctx, p1, BIN_NODE_SIZE); } - SHA1DCUpdate(&ctx, (const unsigned char *)content.c_str(), content.size()); + fbhg_sha1_update(&ctx, content.c_str(), content.size()); - SHA1DCFinal((unsigned char*)result, &ctx); + fbhg_sha1_final((unsigned char*)result, &ctx); } ManifestPtr::ManifestPtr() : diff --git a/setup.py b/setup.py --- a/setup.py +++ b/setup.py @@ -245,6 +245,7 @@ 'ctreemanifest/treemanifest.cpp', ], include_dirs=[ + '.', 'ctreemanifest', 'cdatapack', 'clib', @@ -280,6 +281,7 @@ 'cfastmanifest/tree_path.c', ], include_dirs=[ + '.', 'cfastmanifest', 'clib', 'third-party', diff --git a/tests/test-check-code-hg.t b/tests/test-check-code-hg.t --- a/tests/test-check-code-hg.t +++ b/tests/test-check-code-hg.t @@ -57,6 +57,7 @@ Skipping clib/buffer.c it has no-che?k-code (glob) Skipping clib/buffer.h it has no-che?k-code (glob) Skipping clib/null_test.c it has no-che?k-code (glob) + Skipping clib/sha1.h it has no-che?k-code (glob) Skipping cstore/datapackstore.cpp it has no-che?k-code (glob) Skipping cstore/datapackstore.h it has no-che?k-code (glob) Skipping cstore/key.h it has no-che?k-code (glob)