From 92837ec1862f50f8acdfcc17023b646f4c80fc71 Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 15 Jun 2014 19:08:18 +0100 Subject: [PATCH] Renamed sha1 functions to avoid naming clashes --- Makefile.am | 2 ++ src/common.c | 12 ++++---- src/common.h | 2 +- src/tools/{sha1.c => p_sha1.c} | 56 +++++++++++++++++----------------- src/tools/{sha1.h => p_sha1.h} | 16 +++++----- src/xmpp/capabilities.c | 2 +- 6 files changed, 46 insertions(+), 44 deletions(-) rename src/tools/{sha1.c => p_sha1.c} (87%) rename src/tools/{sha1.h => p_sha1.h} (50%) diff --git a/Makefile.am b/Makefile.am index 566f4739..534001bf 100644 --- a/Makefile.am +++ b/Makefile.am @@ -21,6 +21,7 @@ core_sources = \ src/command/commands.h src/command/commands.c \ src/command/history.h src/tools/parser.c \ src/tools/parser.h \ + src/tools/p_sha1.h src/tools/p_sha1.c \ src/tools/autocomplete.c src/tools/autocomplete.h \ src/tools/history.c src/tools/history.h \ src/tools/tinyurl.c src/tools/tinyurl.h \ @@ -42,6 +43,7 @@ tests_sources = \ src/command/commands.h src/command/commands.c \ src/command/history.h src/tools/parser.c \ src/tools/parser.h \ + src/tools/p_sha1.h src/tools/p_sha1.c \ src/tools/autocomplete.c src/tools/autocomplete.h \ src/tools/history.c src/tools/history.h \ src/tools/tinyurl.c src/tools/tinyurl.h \ diff --git a/src/common.c b/src/common.c index 431fb8ac..d52c388e 100644 --- a/src/common.c +++ b/src/common.c @@ -33,7 +33,7 @@ #include #include -#include "tools/sha1.h" +#include "tools/p_sha1.h" #include "log.h" #include "common.h" @@ -416,16 +416,16 @@ generate_unique_id(char *prefix) } char * -sha1_hash(char *str) +p_sha1_hash(char *str) { - SHA1_CTX ctx; + P_SHA1_CTX ctx; uint8_t digest[20]; uint8_t *input = (uint8_t*)malloc(strlen(str) + 1); memcpy(input, str, strlen(str) + 1); - SHA1_Init(&ctx); - SHA1_Update(&ctx, input, strlen(str)); - SHA1_Final(&ctx, digest); + P_SHA1_Init(&ctx); + P_SHA1_Update(&ctx, input, strlen(str)); + P_SHA1_Final(&ctx, digest); free(input); return g_base64_encode(digest, sizeof(digest)); diff --git a/src/common.h b/src/common.h index d22992fd..7b98a2bc 100644 --- a/src/common.h +++ b/src/common.h @@ -98,7 +98,7 @@ const char * string_from_resource_presence(resource_presence_t presence); resource_presence_t resource_presence_from_string(const char * const str); contact_presence_t contact_presence_from_resource_presence(resource_presence_t resource_presence); -char * sha1_hash(char *str); +char * p_sha1_hash(char *str); char * generate_unique_id(char *prefix); int cmp_win_num(gconstpointer a, gconstpointer b); diff --git a/src/tools/sha1.c b/src/tools/p_sha1.c similarity index 87% rename from src/tools/sha1.c rename to src/tools/p_sha1.c index 69ad2a6e..7c059c97 100644 --- a/src/tools/sha1.c +++ b/src/tools/p_sha1.c @@ -14,10 +14,10 @@ Still 100% Public Domain Corrected a problem which generated improper hash values on 16 bit machines Routine SHA1Update changed from - void SHA1Update(SHA1_CTX* context, unsigned char* data, unsigned int + void SHA1Update(P_SHA1_CTX* context, unsigned char* data, unsigned int len) to - void SHA1Update(SHA1_CTX* context, unsigned char* data, unsigned + void SHA1Update(P_SHA1_CTX* context, unsigned char* data, unsigned long len) The 'len' parameter was declared an int which works fine on 32 bit machines. @@ -99,9 +99,9 @@ A million repetitions of "a" #include #endif -#include "sha1.h" +#include "p_sha1.h" -void SHA1_Transform(uint32_t state[5], const uint8_t buffer[64]); +void P_SHA1_Transform(uint32_t state[5], const uint8_t buffer[64]); #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits)))) @@ -126,7 +126,7 @@ void SHA1_Transform(uint32_t state[5], const uint8_t buffer[64]); #ifdef VERBOSE /* SAK */ -void SHAPrintContext(SHA1_CTX *context, char *msg){ +void SHAPrintContext(P_P_SHA1_CTX *context, char *msg){ printf("%s (%d,%d) %x %x %x %x %x\n", msg, context->count[0], context->count[1], @@ -139,7 +139,7 @@ void SHAPrintContext(SHA1_CTX *context, char *msg){ #endif /* VERBOSE */ /* Hash a single 512-bit block. This is the core of the algorithm. */ -void SHA1_Transform(uint32_t state[5], const uint8_t buffer[64]) +void P_SHA1_Transform(uint32_t state[5], const uint8_t buffer[64]) { uint32_t a, b, c, d, e; typedef union { @@ -198,7 +198,7 @@ void SHA1_Transform(uint32_t state[5], const uint8_t buffer[64]) /* SHA1Init - Initialize new context */ -void SHA1_Init(SHA1_CTX* context) +void P_SHA1_Init(P_SHA1_CTX* context) { /* SHA1 initialization constants */ context->state[0] = 0x67452301; @@ -211,7 +211,7 @@ void SHA1_Init(SHA1_CTX* context) /* Run your data through this. */ -void SHA1_Update(SHA1_CTX* context, const uint8_t* data, const size_t len) +void P_SHA1_Update(P_SHA1_CTX* context, const uint8_t* data, const size_t len) { size_t i, j; @@ -224,9 +224,9 @@ void SHA1_Update(SHA1_CTX* context, const uint8_t* data, const size_t len) context->count[1] += (len >> 29); if ((j + len) > 63) { memcpy(&context->buffer[j], data, (i = 64-j)); - SHA1_Transform(context->state, context->buffer); + P_SHA1_Transform(context->state, context->buffer); for ( ; i + 63 < len; i += 64) { - SHA1_Transform(context->state, data + i); + P_SHA1_Transform(context->state, data + i); } j = 0; } @@ -240,7 +240,7 @@ void SHA1_Update(SHA1_CTX* context, const uint8_t* data, const size_t len) /* Add padding and return the message digest. */ -void SHA1_Final(SHA1_CTX* context, uint8_t digest[SHA1_DIGEST_SIZE]) +void P_SHA1_Final(P_SHA1_CTX* context, uint8_t digest[P_SHA1_DIGEST_SIZE]) { uint32_t i; uint8_t finalcount[8]; @@ -249,12 +249,12 @@ void SHA1_Final(SHA1_CTX* context, uint8_t digest[SHA1_DIGEST_SIZE]) finalcount[i] = (unsigned char)((context->count[(i >= 4 ? 0 : 1)] >> ((3-(i & 3)) * 8) ) & 255); /* Endian independent */ } - SHA1_Update(context, (uint8_t *)"\200", 1); + P_SHA1_Update(context, (uint8_t *)"\200", 1); while ((context->count[0] & 504) != 448) { - SHA1_Update(context, (uint8_t *)"\0", 1); + P_SHA1_Update(context, (uint8_t *)"\0", 1); } - SHA1_Update(context, finalcount, 8); /* Should cause a SHA1_Transform() */ - for (i = 0; i < SHA1_DIGEST_SIZE; i++) { + P_SHA1_Update(context, finalcount, 8); /* Should cause a SHA1_Transform() */ + for (i = 0; i < P_SHA1_DIGEST_SIZE; i++) { digest[i] = (uint8_t) ((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255); } @@ -267,7 +267,7 @@ void SHA1_Final(SHA1_CTX* context, uint8_t digest[SHA1_DIGEST_SIZE]) memset(finalcount, 0, 8); /* SWR */ #ifdef SHA1HANDSOFF /* make SHA1Transform overwrite its own static vars */ - SHA1_Transform(context->state, context->buffer); + P_SHA1_Transform(context->state, context->buffer); #endif } @@ -277,8 +277,8 @@ void SHA1_Final(SHA1_CTX* context, uint8_t digest[SHA1_DIGEST_SIZE]) int main(int argc, char** argv) { int i, j; -SHA1_CTX context; -unsigned char digest[SHA1_DIGEST_SIZE], buffer[16384]; +P_SHA1_CTX context; +unsigned char digest[P_SHA1_DIGEST_SIZE], buffer[16384]; FILE* file; if (argc > 2) { @@ -303,7 +303,7 @@ FILE* file; } SHA1_Final(&context, digest); fclose(file); - for (i = 0; i < SHA1_DIGEST_SIZE/4; i++) { + for (i = 0; i < P_SHA1_DIGEST_SIZE/4; i++) { for (j = 0; j < 4; j++) { printf("%02X", digest[i*4+j]); } @@ -327,12 +327,12 @@ static char *test_results[] = { "84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1", "34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F"}; -void digest_to_hex(const uint8_t digest[SHA1_DIGEST_SIZE], char *output) +void digest_to_hex(const uint8_t digest[P_SHA1_DIGEST_SIZE], char *output) { int i,j; char *c = output; - for (i = 0; i < SHA1_DIGEST_SIZE/4; i++) { + for (i = 0; i < P_SHA1_DIGEST_SIZE/4; i++) { for (j = 0; j < 4; j++) { sprintf(c,"%02X", digest[i*4+j]); c += 2; @@ -346,16 +346,16 @@ void digest_to_hex(const uint8_t digest[SHA1_DIGEST_SIZE], char *output) int main(int argc, char** argv) { int k; - SHA1_CTX context; + P_SHA1_CTX context; uint8_t digest[20]; char output[80]; fprintf(stdout, "verifying SHA-1 implementation... "); for (k = 0; k < 2; k++){ - SHA1_Init(&context); - SHA1_Update(&context, (uint8_t*)test_data[k], strlen(test_data[k])); - SHA1_Final(&context, digest); + P_SHA1_Init(&context); + P_SHA1_Update(&context, (uint8_t*)test_data[k], strlen(test_data[k])); + P_SHA1_Final(&context, digest); digest_to_hex(digest, output); if (strcmp(output, test_results[k])) { @@ -367,10 +367,10 @@ int main(int argc, char** argv) } } /* million 'a' vector we feed separately */ - SHA1_Init(&context); + P_SHA1_Init(&context); for (k = 0; k < 1000000; k++) - SHA1_Update(&context, (uint8_t*)"a", 1); - SHA1_Final(&context, digest); + P_SHA1_Update(&context, (uint8_t*)"a", 1); + P_SHA1_Final(&context, digest); digest_to_hex(digest, output); if (strcmp(output, test_results[2])) { fprintf(stdout, "FAIL\n"); diff --git a/src/tools/sha1.h b/src/tools/p_sha1.h similarity index 50% rename from src/tools/sha1.h rename to src/tools/p_sha1.h index b637f662..75443b01 100644 --- a/src/tools/sha1.h +++ b/src/tools/p_sha1.h @@ -5,8 +5,8 @@ * SHA-1 hash API. */ -#ifndef __SHA1_H -#define __SHA1_H +#ifndef __P_SHA1_H +#define __P_SHA1_H #ifdef __cplusplus extern "C" { @@ -16,16 +16,16 @@ typedef struct { uint32_t state[5]; uint32_t count[2]; uint8_t buffer[64]; -} SHA1_CTX; +} P_SHA1_CTX; -#define SHA1_DIGEST_SIZE 20 +#define P_SHA1_DIGEST_SIZE 20 -void SHA1_Init(SHA1_CTX* context); -void SHA1_Update(SHA1_CTX* context, const uint8_t* data, const size_t len); -void SHA1_Final(SHA1_CTX* context, uint8_t digest[SHA1_DIGEST_SIZE]); +void P_SHA1_Init(P_SHA1_CTX* context); +void P_SHA1_Update(P_SHA1_CTX* context, const uint8_t* data, const size_t len); +void P_SHA1_Final(P_SHA1_CTX* context, uint8_t digest[P_SHA1_DIGEST_SIZE]); #ifdef __cplusplus } #endif -#endif /* __SHA1_H */ +#endif /* __P_SHA1_H */ diff --git a/src/xmpp/capabilities.c b/src/xmpp/capabilities.c index cd2e3fce..5f2ad422 100644 --- a/src/xmpp/capabilities.c +++ b/src/xmpp/capabilities.c @@ -202,7 +202,7 @@ caps_create_sha1_str(xmpp_stanza_t * const query) curr = g_slist_next(curr); } - char *result = sha1_hash(s->str); + char *result = p_sha1_hash(s->str); g_string_free(s, TRUE); g_slist_free_full(identities, g_free);