From fdd79e734a8fc9dfd6f17c7decc67d824e901cdd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Storsj=C3=B6?= Date: Fri, 17 Jan 2014 10:33:04 +0200 Subject: [PATCH 1/9] Add an SHA1 implementation by Paul Jones --- test/sha1.c | 393 ++++++++++++++++++++++++++++++++++++++++++++++++ test/sha1.h | 76 ++++++++++ test/targets.mk | 1 + 3 files changed, 470 insertions(+) create mode 100644 test/sha1.c create mode 100644 test/sha1.h diff --git a/test/sha1.c b/test/sha1.c new file mode 100644 index 00000000..ae3f7fb4 --- /dev/null +++ b/test/sha1.c @@ -0,0 +1,393 @@ +/*! + * \copy + * Copyright (c) 1998, 2009 Paul E. Jones + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + */ + +/* + * Description: + * This file implements the Secure Hashing Standard as defined + * in FIPS PUB 180-1 published April 17, 1995. + * + * The Secure Hashing Standard, which uses the Secure Hashing + * Algorithm (SHA), produces a 160-bit message digest for a + * given data stream. In theory, it is highly improbable that + * two messages will produce the same message digest. Therefore, + * this algorithm can serve as a means of providing a "fingerprint" + * for a message. + * + * Portability Issues: + * SHA-1 is defined in terms of 32-bit "words". This code was + * written with the expectation that the processor has at least + * a 32-bit machine word size. If the machine word size is larger, + * the code should still function properly. One caveat to that + * is that the input functions taking characters and character + * arrays assume that only 8 bits of information are stored in each + * character. + * + * Caveats: + * SHA-1 is designed to work with messages less than 2^64 bits + * long. Although SHA-1 allows a message digest to be generated for + * messages of any number of bits less than 2^64, this + * implementation only works with messages with a length that is a + * multiple of the size of an 8-bit character. + * + */ + +#include "sha1.h" + +/* + * Define the circular shift macro + */ +#define SHA1CircularShift(bits,word) \ + ((((word) << (bits)) & 0xFFFFFFFF) | \ + ((word) >> (32-(bits)))) + +/* Function prototypes */ +void SHA1ProcessMessageBlock(SHA1Context *); +void SHA1PadMessage(SHA1Context *); + +/* + * SHA1Reset + * + * Description: + * This function will initialize the SHA1Context in preparation + * for computing a new message digest. + * + * Parameters: + * context: [in/out] + * The context to reset. + * + * Returns: + * Nothing. + * + * Comments: + * + */ +void SHA1Reset(SHA1Context *context) +{ + context->Length_Low = 0; + context->Length_High = 0; + context->Message_Block_Index = 0; + + context->Message_Digest[0] = 0x67452301; + context->Message_Digest[1] = 0xEFCDAB89; + context->Message_Digest[2] = 0x98BADCFE; + context->Message_Digest[3] = 0x10325476; + context->Message_Digest[4] = 0xC3D2E1F0; + + context->Computed = 0; + context->Corrupted = 0; +} + +/* + * SHA1Result + * + * Description: + * This function will return the 160-bit message digest into the + * Message_Digest array within the SHA1Context provided + * + * Parameters: + * context: [in/out] + * The context to use to calculate the SHA-1 hash. + * + * Returns: + * 1 if successful, 0 if it failed. + * + * Comments: + * + */ +int SHA1Result(SHA1Context *context) +{ + + if (context->Corrupted) + { + return 0; + } + + if (!context->Computed) + { + SHA1PadMessage(context); + context->Computed = 1; + } + + return 1; +} + +/* + * SHA1Input + * + * Description: + * This function accepts an array of octets as the next portion of + * the message. + * + * Parameters: + * context: [in/out] + * The SHA-1 context to update + * message_array: [in] + * An array of characters representing the next portion of the + * message. + * length: [in] + * The length of the message in message_array + * + * Returns: + * Nothing. + * + * Comments: + * + */ +void SHA1Input( SHA1Context *context, + const unsigned char *message_array, + unsigned length) +{ + if (!length) + { + return; + } + + if (context->Computed || context->Corrupted) + { + context->Corrupted = 1; + return; + } + + while(length-- && !context->Corrupted) + { + context->Message_Block[context->Message_Block_Index++] = + (*message_array & 0xFF); + + context->Length_Low += 8; + /* Force it to 32 bits */ + context->Length_Low &= 0xFFFFFFFF; + if (context->Length_Low == 0) + { + context->Length_High++; + /* Force it to 32 bits */ + context->Length_High &= 0xFFFFFFFF; + if (context->Length_High == 0) + { + /* Message is too long */ + context->Corrupted = 1; + } + } + + if (context->Message_Block_Index == 64) + { + SHA1ProcessMessageBlock(context); + } + + message_array++; + } +} + +/* + * SHA1ProcessMessageBlock + * + * Description: + * This function will process the next 512 bits of the message + * stored in the Message_Block array. + * + * Parameters: + * None. + * + * Returns: + * Nothing. + * + * Comments: + * Many of the variable names in the SHAContext, especially the + * single character names, were used because those were the names + * used in the publication. + * + * + */ +void SHA1ProcessMessageBlock(SHA1Context *context) +{ + const unsigned K[] = /* Constants defined in SHA-1 */ + { + 0x5A827999, + 0x6ED9EBA1, + 0x8F1BBCDC, + 0xCA62C1D6 + }; + int t; /* Loop counter */ + unsigned temp; /* Temporary word value */ + unsigned W[80]; /* Word sequence */ + unsigned A, B, C, D, E; /* Word buffers */ + + /* + * Initialize the first 16 words in the array W + */ + for(t = 0; t < 16; t++) + { + W[t] = ((unsigned) context->Message_Block[t * 4]) << 24; + W[t] |= ((unsigned) context->Message_Block[t * 4 + 1]) << 16; + W[t] |= ((unsigned) context->Message_Block[t * 4 + 2]) << 8; + W[t] |= ((unsigned) context->Message_Block[t * 4 + 3]); + } + + for(t = 16; t < 80; t++) + { + W[t] = SHA1CircularShift(1,W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16]); + } + + A = context->Message_Digest[0]; + B = context->Message_Digest[1]; + C = context->Message_Digest[2]; + D = context->Message_Digest[3]; + E = context->Message_Digest[4]; + + for(t = 0; t < 20; t++) + { + temp = SHA1CircularShift(5,A) + + ((B & C) | ((~B) & D)) + E + W[t] + K[0]; + temp &= 0xFFFFFFFF; + E = D; + D = C; + C = SHA1CircularShift(30,B); + B = A; + A = temp; + } + + for(t = 20; t < 40; t++) + { + temp = SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[1]; + temp &= 0xFFFFFFFF; + E = D; + D = C; + C = SHA1CircularShift(30,B); + B = A; + A = temp; + } + + for(t = 40; t < 60; t++) + { + temp = SHA1CircularShift(5,A) + + ((B & C) | (B & D) | (C & D)) + E + W[t] + K[2]; + temp &= 0xFFFFFFFF; + E = D; + D = C; + C = SHA1CircularShift(30,B); + B = A; + A = temp; + } + + for(t = 60; t < 80; t++) + { + temp = SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[3]; + temp &= 0xFFFFFFFF; + E = D; + D = C; + C = SHA1CircularShift(30,B); + B = A; + A = temp; + } + + context->Message_Digest[0] = + (context->Message_Digest[0] + A) & 0xFFFFFFFF; + context->Message_Digest[1] = + (context->Message_Digest[1] + B) & 0xFFFFFFFF; + context->Message_Digest[2] = + (context->Message_Digest[2] + C) & 0xFFFFFFFF; + context->Message_Digest[3] = + (context->Message_Digest[3] + D) & 0xFFFFFFFF; + context->Message_Digest[4] = + (context->Message_Digest[4] + E) & 0xFFFFFFFF; + + context->Message_Block_Index = 0; +} + +/* + * SHA1PadMessage + * + * Description: + * According to the standard, the message must be padded to an even + * 512 bits. The first padding bit must be a '1'. The last 64 + * bits represent the length of the original message. All bits in + * between should be 0. This function will pad the message + * according to those rules by filling the Message_Block array + * accordingly. It will also call SHA1ProcessMessageBlock() + * appropriately. When it returns, it can be assumed that the + * message digest has been computed. + * + * Parameters: + * context: [in/out] + * The context to pad + * + * Returns: + * Nothing. + * + * Comments: + * + */ +void SHA1PadMessage(SHA1Context *context) +{ + /* + * Check to see if the current message block is too small to hold + * the initial padding bits and length. If so, we will pad the + * block, process it, and then continue padding into a second + * block. + */ + if (context->Message_Block_Index > 55) + { + context->Message_Block[context->Message_Block_Index++] = 0x80; + while(context->Message_Block_Index < 64) + { + context->Message_Block[context->Message_Block_Index++] = 0; + } + + SHA1ProcessMessageBlock(context); + + while(context->Message_Block_Index < 56) + { + context->Message_Block[context->Message_Block_Index++] = 0; + } + } + else + { + context->Message_Block[context->Message_Block_Index++] = 0x80; + while(context->Message_Block_Index < 56) + { + context->Message_Block[context->Message_Block_Index++] = 0; + } + } + + /* + * Store the message length as the last 8 octets + */ + context->Message_Block[56] = (context->Length_High >> 24) & 0xFF; + context->Message_Block[57] = (context->Length_High >> 16) & 0xFF; + context->Message_Block[58] = (context->Length_High >> 8) & 0xFF; + context->Message_Block[59] = (context->Length_High) & 0xFF; + context->Message_Block[60] = (context->Length_Low >> 24) & 0xFF; + context->Message_Block[61] = (context->Length_Low >> 16) & 0xFF; + context->Message_Block[62] = (context->Length_Low >> 8) & 0xFF; + context->Message_Block[63] = (context->Length_Low) & 0xFF; + + SHA1ProcessMessageBlock(context); +} diff --git a/test/sha1.h b/test/sha1.h new file mode 100644 index 00000000..8f389777 --- /dev/null +++ b/test/sha1.h @@ -0,0 +1,76 @@ +/*! + * \copy + * Copyright (c) 1998, 2009 Paul E. Jones + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + */ + +/* + * Description: + * This class implements the Secure Hashing Standard as defined + * in FIPS PUB 180-1 published April 17, 1995. + * + * Many of the variable names in the SHA1Context, especially the + * single character names, were used because those were the names + * used in the publication. + * + * Please read the file sha1.c for more information. + * + */ + +#ifndef _SHA1_H_ +#define _SHA1_H_ + +/* + * This structure will hold context information for the hashing + * operation + */ +typedef struct SHA1Context +{ + unsigned Message_Digest[5]; /* Message Digest (output) */ + + unsigned Length_Low; /* Message length in bits */ + unsigned Length_High; /* Message length in bits */ + + unsigned char Message_Block[64]; /* 512-bit message blocks */ + int Message_Block_Index; /* Index into message block array */ + + int Computed; /* Is the digest computed? */ + int Corrupted; /* Is the message digest corruped? */ +} SHA1Context; + +/* + * Function Prototypes + */ +void SHA1Reset(SHA1Context *); +int SHA1Result(SHA1Context *); +void SHA1Input( SHA1Context *, + const unsigned char *, + unsigned); + +#endif diff --git a/test/targets.mk b/test/targets.mk index 23974ca5..f95aa7a1 100644 --- a/test/targets.mk +++ b/test/targets.mk @@ -12,6 +12,7 @@ CODEC_UNITTEST_OBJS += $(CODEC_UNITTEST_CPP_SRCS:.cpp=.o) CODEC_UNITTEST_C_SRCS=\ $(CODEC_UNITTEST_SRCDIR)/c_interface_test.c\ + $(CODEC_UNITTEST_SRCDIR)/sha1.c\ CODEC_UNITTEST_OBJS += $(CODEC_UNITTEST_C_SRCS:.c=.o) From 7a4a066469be598f999f238ffc451257d6387769 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Storsj=C3=B6?= Date: Mon, 17 Feb 2014 12:11:15 +0200 Subject: [PATCH 2/9] Make the SHA1 header compatible with C++ --- test/sha1.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/sha1.h b/test/sha1.h index 8f389777..3cc1bf59 100644 --- a/test/sha1.h +++ b/test/sha1.h @@ -46,6 +46,10 @@ #ifndef _SHA1_H_ #define _SHA1_H_ +#ifdef __cplusplus +extern "C" { +#endif + /* * This structure will hold context information for the hashing * operation @@ -73,4 +77,8 @@ void SHA1Input( SHA1Context *, const unsigned char *, unsigned); +#ifdef __cplusplus +} +#endif + #endif From e8a2cf6d1f84035a7af4766e6a7b2120ef5f16c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Storsj=C3=B6?= Date: Mon, 24 Feb 2014 12:00:17 +0200 Subject: [PATCH 3/9] Make the SHA1Result function write the output into a byte array --- test/sha1.c | 10 ++++++++-- test/sha1.h | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/test/sha1.c b/test/sha1.c index ae3f7fb4..08a93d1d 100644 --- a/test/sha1.c +++ b/test/sha1.c @@ -111,11 +111,13 @@ void SHA1Reset(SHA1Context *context) * * Description: * This function will return the 160-bit message digest into the - * Message_Digest array within the SHA1Context provided + * digest array provided as a parameter. * * Parameters: * context: [in/out] * The context to use to calculate the SHA-1 hash. + * digest: [out] + * An array of characters where the digest is written. * * Returns: * 1 if successful, 0 if it failed. @@ -123,8 +125,9 @@ void SHA1Reset(SHA1Context *context) * Comments: * */ -int SHA1Result(SHA1Context *context) +int SHA1Result(SHA1Context *context, unsigned char *digest) { + int i; if (context->Corrupted) { @@ -137,6 +140,9 @@ int SHA1Result(SHA1Context *context) context->Computed = 1; } + for (i = 0; i < SHA_DIGEST_LENGTH; i++) + digest[i] = context->Message_Digest[i / 4] >> (8 * (3 - (i % 4))); + return 1; } diff --git a/test/sha1.h b/test/sha1.h index 3cc1bf59..50caae68 100644 --- a/test/sha1.h +++ b/test/sha1.h @@ -72,7 +72,7 @@ typedef struct SHA1Context * Function Prototypes */ void SHA1Reset(SHA1Context *); -int SHA1Result(SHA1Context *); +int SHA1Result(SHA1Context *, unsigned char *); void SHA1Input( SHA1Context *, const unsigned char *, unsigned); From 4f5fd952b636e739faef2c76f74b21ee11bf6dd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Storsj=C3=B6?= Date: Fri, 17 Jan 2014 10:39:42 +0200 Subject: [PATCH 4/9] Use the bundled sha1 implementation instead of relying on openssl This simplifies running tests on platforms where OpenSSL isn't commonly available. --- Makefile | 2 +- test/sha1.h | 6 ++++++ test/utils/HashFunctions.h | 2 +- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index ccc0020a..aae99dbe 100644 --- a/Makefile +++ b/Makefile @@ -82,7 +82,7 @@ H264ENC_INCLUDES = $(ENCODER_INCLUDES) -Icodec/console/enc/inc H264ENC_LDFLAGS = -L. $(call LINK_LIB,encoder) $(call LINK_LIB,processing) $(call LINK_LIB,common) H264ENC_DEPS = $(LIBPREFIX)encoder.$(LIBSUFFIX) $(LIBPREFIX)processing.$(LIBSUFFIX) $(LIBPREFIX)common.$(LIBSUFFIX) -CODEC_UNITTEST_LDFLAGS = -L. -lgtest -ldecoder -lcrypto -lencoder -lprocessing -lcommon +CODEC_UNITTEST_LDFLAGS = -L. -lgtest -ldecoder -lencoder -lprocessing -lcommon CODEC_UNITTEST_DEPS = $(LIBPREFIX)gtest.$(LIBSUFFIX) $(LIBPREFIX)decoder.$(LIBSUFFIX) $(LIBPREFIX)encoder.$(LIBSUFFIX) $(LIBPREFIX)processing.$(LIBSUFFIX) $(LIBPREFIX)common.$(LIBSUFFIX) .PHONY: test gtest-bootstrap clean diff --git a/test/sha1.h b/test/sha1.h index 50caae68..ac3dd476 100644 --- a/test/sha1.h +++ b/test/sha1.h @@ -77,6 +77,12 @@ void SHA1Input( SHA1Context *, const unsigned char *, unsigned); +#define SHA_DIGEST_LENGTH 20 +#define SHA_CTX SHA1Context +#define SHA1_Init(ctx) SHA1Reset(ctx) +#define SHA1_Update(ctx, d, l) SHA1Input(ctx, d, l) +#define SHA1_Final(d, ctx) SHA1Result(ctx, d) + #ifdef __cplusplus } #endif diff --git a/test/utils/HashFunctions.h b/test/utils/HashFunctions.h index fa5d67ad..ef9b9b63 100644 --- a/test/utils/HashFunctions.h +++ b/test/utils/HashFunctions.h @@ -3,7 +3,7 @@ #include #include -#include +#include "../sha1.h" static bool CompareHash(const unsigned char* digest, const char* hashStr) { char hashStrCmp[SHA_DIGEST_LENGTH * 2 + 1]; From 0f3dfb33b0413385b48f3781a1eea92879faa7bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Storsj=C3=B6?= Date: Fri, 17 Jan 2014 10:43:26 +0200 Subject: [PATCH 5/9] Use the local sha1 function names directly without compatibility defines --- test/decode_encode_test.cpp | 10 +++++----- test/decoder_test.cpp | 10 +++++----- test/encoder_test.cpp | 10 +++++----- test/sha1.h | 4 ---- 4 files changed, 15 insertions(+), 19 deletions(-) diff --git a/test/decode_encode_test.cpp b/test/decode_encode_test.cpp index 82883109..3cc4a7c8 100644 --- a/test/decode_encode_test.cpp +++ b/test/decode_encode_test.cpp @@ -6,14 +6,14 @@ #include "BaseDecoderTest.h" #include "BaseEncoderTest.h" -static void UpdateHashFromFrame(const SFrameBSInfo& info, SHA_CTX* ctx) { +static void UpdateHashFromFrame(const SFrameBSInfo& info, SHA1Context* ctx) { for (int i = 0; i < info.iLayerNum; ++i) { const SLayerBSInfo& layerInfo = info.sLayerInfo[i]; int layerSize = 0; for (int j = 0; j < layerInfo.iNalCount; ++j) { layerSize += layerInfo.iNalLengthInByte[j]; } - SHA1_Update(ctx, layerInfo.pBsBuf, layerSize); + SHA1Input(ctx, layerInfo.pBsBuf, layerSize); } } @@ -49,7 +49,7 @@ class DecodeEncodeTest : public ::testing::TestWithParam, if (HasFatalFailure()) { return; } - SHA1_Init(&ctx_); + SHA1Reset(&ctx_); } virtual void TearDown() { @@ -87,7 +87,7 @@ class DecodeEncodeTest : public ::testing::TestWithParam, } protected: - SHA_CTX ctx_; + SHA1Context ctx_; BufferedData buf_; }; @@ -97,7 +97,7 @@ TEST_P(DecodeEncodeTest, CompareOutput) { ASSERT_TRUE(Open(p.fileName)); EncodeStream(this, p.width, p.height, p.frameRate, this); unsigned char digest[SHA_DIGEST_LENGTH]; - SHA1_Final(digest, &ctx_); + SHA1Result(&ctx_, digest); if (!HasFatalFailure()) { ASSERT_TRUE(CompareHash(digest, p.hashStr)); } diff --git a/test/decoder_test.cpp b/test/decoder_test.cpp index 49d19a5d..2d8f5b25 100644 --- a/test/decoder_test.cpp +++ b/test/decoder_test.cpp @@ -2,10 +2,10 @@ #include "utils/HashFunctions.h" #include "BaseDecoderTest.h" -static void UpdateHashFromPlane(SHA_CTX* ctx, const uint8_t* plane, +static void UpdateHashFromPlane(SHA1Context* ctx, const uint8_t* plane, int width, int height, int stride) { for (int i = 0; i < height; i++) { - SHA1_Update(ctx, plane, width); + SHA1Input(ctx, plane, width); plane += stride; } } @@ -36,7 +36,7 @@ class DecoderOutputTest : public ::testing::WithParamInterface, if (HasFatalFailure()) { return; } - SHA1_Init(&ctx_); + SHA1Reset(&ctx_); } virtual void onDecodeFrame(const Frame& frame) { const Plane& y = frame.y; @@ -47,7 +47,7 @@ class DecoderOutputTest : public ::testing::WithParamInterface, UpdateHashFromPlane(&ctx_, v.data, v.width, v.height, v.stride); } protected: - SHA_CTX ctx_; + SHA1Context ctx_; }; TEST_P(DecoderOutputTest, CompareOutput) { @@ -55,7 +55,7 @@ TEST_P(DecoderOutputTest, CompareOutput) { DecodeFile(p.fileName, this); unsigned char digest[SHA_DIGEST_LENGTH]; - SHA1_Final(digest, &ctx_); + SHA1Result(&ctx_, digest); if (!HasFatalFailure()) { ASSERT_TRUE(CompareHash(digest, p.hashStr)); } diff --git a/test/encoder_test.cpp b/test/encoder_test.cpp index 7439ddb2..d2671312 100644 --- a/test/encoder_test.cpp +++ b/test/encoder_test.cpp @@ -2,14 +2,14 @@ #include "utils/HashFunctions.h" #include "BaseEncoderTest.h" -static void UpdateHashFromFrame(const SFrameBSInfo& info, SHA_CTX* ctx) { +static void UpdateHashFromFrame(const SFrameBSInfo& info, SHA1Context* ctx) { for (int i = 0; i < info.iLayerNum; ++i) { const SLayerBSInfo& layerInfo = info.sLayerInfo[i]; int layerSize = 0; for (int j = 0; j < layerInfo.iNalCount; ++j) { layerSize += layerInfo.iNalLengthInByte[j]; } - SHA1_Update(ctx, layerInfo.pBsBuf, layerSize); + SHA1Input(ctx, layerInfo.pBsBuf, layerSize); } } @@ -41,13 +41,13 @@ class EncoderOutputTest : public ::testing::WithParamInterface, if (HasFatalFailure()) { return; } - SHA1_Init(&ctx_); + SHA1Reset(&ctx_); } virtual void onEncodeFrame(const SFrameBSInfo& frameInfo) { UpdateHashFromFrame(frameInfo, &ctx_); } protected: - SHA_CTX ctx_; + SHA1Context ctx_; }; @@ -56,7 +56,7 @@ TEST_P(EncoderOutputTest, CompareOutput) { EncodeFile(p.fileName, p.width, p.height, p.frameRate, this); unsigned char digest[SHA_DIGEST_LENGTH]; - SHA1_Final(digest, &ctx_); + SHA1Result(&ctx_, digest); if (!HasFatalFailure()) { ASSERT_TRUE(CompareHash(digest, p.hashStr)); } diff --git a/test/sha1.h b/test/sha1.h index ac3dd476..08114f20 100644 --- a/test/sha1.h +++ b/test/sha1.h @@ -78,10 +78,6 @@ void SHA1Input( SHA1Context *, unsigned); #define SHA_DIGEST_LENGTH 20 -#define SHA_CTX SHA1Context -#define SHA1_Init(ctx) SHA1Reset(ctx) -#define SHA1_Update(ctx, d, l) SHA1Input(ctx, d, l) -#define SHA1_Final(d, ctx) SHA1Result(ctx, d) #ifdef __cplusplus } From af2a3542025b34232a656d314f660ae6fc6c3d8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Storsj=C3=B6?= Date: Fri, 17 Jan 2014 11:13:52 +0200 Subject: [PATCH 6/9] Remove the now unused -Wno-deprecated-declarations on OS X This was only required for building with OpenSSL. --- build/platform-darwin.mk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build/platform-darwin.mk b/build/platform-darwin.mk index afbe0832..8ad6691f 100644 --- a/build/platform-darwin.mk +++ b/build/platform-darwin.mk @@ -2,7 +2,7 @@ include build/platform-x86-common.mk ASM = nasm SHAREDLIBSUFFIX = dylib SHARED = -dynamiclib -CFLAGS += -Wno-deprecated-declarations -Werror -fPIC -DMACOS -DMT_ENABLED -MMD -MP +CFLAGS += -Werror -fPIC -DMACOS -DMT_ENABLED -MMD -MP LDFLAGS += -lpthread ASMFLAGS += --prefix _ -DNOPREFIX ifeq ($(ENABLE64BIT), Yes) From 113fb8c104bbea9ec20c57ef13add1d9958048f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Storsj=C3=B6?= Date: Fri, 17 Jan 2014 10:58:13 +0200 Subject: [PATCH 7/9] Set GTEST_HAS_TR1_TUPLE to 0 on msvc This fixes building of gtest on msvc. See e.g. http://stackoverflow.com/questions/8274588 for more discussion on the matter. This is only required on MSVC 2012 - gtest itself (in the cmake files) adds -D_VARIADIC_MAX=10 to the build when building using MSVC 2012, but we don't know the MSVC version at the make stage here. Since the gtest tuple support isn't used at all, it's simpler to just disable it altogether than to try to add -D_VARIADIC_MAX=10. --- build/platform-msvc-common.mk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build/platform-msvc-common.mk b/build/platform-msvc-common.mk index ab0d83ab..18232c21 100644 --- a/build/platform-msvc-common.mk +++ b/build/platform-msvc-common.mk @@ -2,7 +2,7 @@ CC=cl CXX=cl AR=lib CXX_O=-Fo$@ -CFLAGS += -nologo -W3 -EHsc -fp:precise -Zc:wchar_t -Zc:forScope +CFLAGS += -nologo -W3 -EHsc -fp:precise -Zc:wchar_t -Zc:forScope -DGTEST_HAS_TR1_TUPLE=0 CXX_LINK_O=-nologo -Fe$@ AR_OPTS=-nologo -out:$@ CFLAGS_OPT=-O2 -Ob1 -Oy- -Zi -GF -Gm- -GS -Gy -MD -DNDEBUG From 56e5a110482c7f6358e9c0ae4ee1208e59acc56d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Storsj=C3=B6?= Date: Fri, 17 Jan 2014 10:59:52 +0200 Subject: [PATCH 8/9] Wrap the library names in codec_unittest LDFLAGS, for use with MSVC --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index aae99dbe..34ea60c8 100644 --- a/Makefile +++ b/Makefile @@ -82,7 +82,7 @@ H264ENC_INCLUDES = $(ENCODER_INCLUDES) -Icodec/console/enc/inc H264ENC_LDFLAGS = -L. $(call LINK_LIB,encoder) $(call LINK_LIB,processing) $(call LINK_LIB,common) H264ENC_DEPS = $(LIBPREFIX)encoder.$(LIBSUFFIX) $(LIBPREFIX)processing.$(LIBSUFFIX) $(LIBPREFIX)common.$(LIBSUFFIX) -CODEC_UNITTEST_LDFLAGS = -L. -lgtest -ldecoder -lencoder -lprocessing -lcommon +CODEC_UNITTEST_LDFLAGS = -L. $(call LINK_LIB,gtest) $(call LINK_LIB,decoder) $(call LINK_LIB,encoder) $(call LINK_LIB,processing) $(call LINK_LIB,common) CODEC_UNITTEST_DEPS = $(LIBPREFIX)gtest.$(LIBSUFFIX) $(LIBPREFIX)decoder.$(LIBSUFFIX) $(LIBPREFIX)encoder.$(LIBSUFFIX) $(LIBPREFIX)processing.$(LIBSUFFIX) $(LIBPREFIX)common.$(LIBSUFFIX) .PHONY: test gtest-bootstrap clean From e88348b76d917e03c4c38114456e32b328b8193c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Storsj=C3=B6?= Date: Fri, 24 Jan 2014 10:29:27 +0200 Subject: [PATCH 9/9] Allow building tests on MSVC versions where stdint.h is missing Of the supported MSVC versions, only 2008 actually is missing stdint.h, it is included in MSVC since 2010. --- test/BaseDecoderTest.h | 2 +- test/test_stdint.h | 22 ++++++++++++++++++++++ test/utils/BufferedData.h | 2 +- 3 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 test/test_stdint.h diff --git a/test/BaseDecoderTest.h b/test/BaseDecoderTest.h index 32923a15..91802ada 100644 --- a/test/BaseDecoderTest.h +++ b/test/BaseDecoderTest.h @@ -1,7 +1,7 @@ #ifndef __BASEDECODERTEST_H__ #define __BASEDECODERTEST_H__ -#include +#include "test_stdint.h" #include #include #include "codec_api.h" diff --git a/test/test_stdint.h b/test/test_stdint.h new file mode 100644 index 00000000..335b2e22 --- /dev/null +++ b/test/test_stdint.h @@ -0,0 +1,22 @@ +#ifndef TEST_STDINT_H +#define TEST_STDINT_H + +#ifndef _MSC_VER + +#include + +#else + +typedef signed char int8_t ; +typedef unsigned char uint8_t ; +typedef short int16_t ; +typedef unsigned short uint16_t; +typedef int int32_t ; +typedef unsigned int uint32_t; +typedef __int64 int64_t ; +typedef unsigned __int64 uint64_t; +typedef short int_least16_t; + +#endif + +#endif //TEST_STDINT_H diff --git a/test/utils/BufferedData.h b/test/utils/BufferedData.h index 2b383296..4c5d729c 100644 --- a/test/utils/BufferedData.h +++ b/test/utils/BufferedData.h @@ -3,7 +3,7 @@ #include #include -#include +#include "../test_stdint.h" #include class BufferedData {