From 935275bb5952b59b65c6a038a4b825aea95562ae Mon Sep 17 00:00:00 2001 From: nobu Date: Wed, 11 Feb 2015 02:09:23 +0000 Subject: [PATCH] digest: define Finish func from Final func * ext/digest/digest.h (DEFINE_FINISH_FUNC_FROM_FINAL): macro for finish functions, by inverting arguments order. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@49564 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 5 ++++- ext/digest/digest.h | 19 +++++++++++++++++++ ext/digest/digest_conf.rb | 1 - ext/digest/md5/md5ossl.c | 9 --------- ext/digest/md5/md5ossl.h | 4 +++- ext/digest/rmd160/rmd160ossl.c | 8 -------- ext/digest/rmd160/rmd160ossl.h | 3 ++- ext/digest/sha1/sha1ossl.c | 10 ---------- ext/digest/sha1/sha1ossl.h | 4 +++- ext/digest/sha2/sha2ossl.c | 13 ------------- ext/digest/sha2/sha2ossl.h | 16 +++++++++++++--- 11 files changed, 44 insertions(+), 48 deletions(-) delete mode 100644 ext/digest/md5/md5ossl.c delete mode 100644 ext/digest/rmd160/rmd160ossl.c delete mode 100644 ext/digest/sha1/sha1ossl.c delete mode 100644 ext/digest/sha2/sha2ossl.c diff --git a/ChangeLog b/ChangeLog index c7952d3997..51152286e2 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,7 @@ -Wed Feb 11 11:08:48 2015 Nobuyoshi Nakada +Wed Feb 11 11:09:21 2015 Nobuyoshi Nakada + + * ext/digest/digest.h (DEFINE_FINISH_FUNC_FROM_FINAL): macro for + finish functions, by inverting arguments order. * ext/digest/digest_conf.rb (digest_conf): extract common configurations. diff --git a/ext/digest/digest.h b/ext/digest/digest.h index 85848d5682..30359ad348 100644 --- a/ext/digest/digest.h +++ b/ext/digest/digest.h @@ -30,3 +30,22 @@ typedef struct { rb_digest_hash_update_func_t update_func; rb_digest_hash_finish_func_t finish_func; } rb_digest_metadata_t; + +#define DEFINE_UPDATE_FUNC_FOR_UINT(name) \ +void \ +rb_digest_##name##_update(void *ctx, unsigned char *ptr, size_t size) \ +{ \ + const unsigned int stride = 16384; \ + \ + for (; size > stride; size -= stride, ptr += stride) { \ + name##_Update(ctx, ptr, stride); \ + } \ + if (size > 0) name##_Update(ctx, ptr, size); \ +} + +#define DEFINE_FINISH_FUNC_FROM_FINAL(name) \ +int \ +rb_digest_##name##_finish(void *ctx, unsigned char *ptr) \ +{ \ + return name##_Final(ptr, ctx); \ +} diff --git a/ext/digest/digest_conf.rb b/ext/digest/digest_conf.rb index f2cb1f36e8..4b8b6f4c58 100644 --- a/ext/digest/digest_conf.rb +++ b/ext/digest/digest_conf.rb @@ -10,7 +10,6 @@ def digest_conf(name, hdr = name, funcs = nil) if funcs.all? {|func| OpenSSL.check_func("#{func}_Transform", hdr)} && funcs.all? {|func| have_type("#{func}_CTX", hdr)} $defs << "-D#{name.upcase}_USE_OPENSSL" - $objs << "#{name}ossl.#{$OBJEXT}" return :ossl end end diff --git a/ext/digest/md5/md5ossl.c b/ext/digest/md5/md5ossl.c deleted file mode 100644 index d94ae2cd2f..0000000000 --- a/ext/digest/md5/md5ossl.c +++ /dev/null @@ -1,9 +0,0 @@ -/* $Id$ */ - -#include "md5ossl.h" - -void -MD5_Finish(MD5_CTX *pctx, unsigned char *digest) -{ - MD5_Final(digest, pctx); -} diff --git a/ext/digest/md5/md5ossl.h b/ext/digest/md5/md5ossl.h index 1680c4f5c9..2e9347ed94 100644 --- a/ext/digest/md5/md5ossl.h +++ b/ext/digest/md5/md5ossl.h @@ -8,6 +8,8 @@ #define MD5_BLOCK_LENGTH MD5_CBLOCK -void MD5_Finish(MD5_CTX *pctx, unsigned char *digest); +static DEFINE_FINISH_FUNC_FROM_FINAL(MD5); +#undef MD5_Finish +#define MD5_Finish rb_digest_MD5_finish #endif diff --git a/ext/digest/rmd160/rmd160ossl.c b/ext/digest/rmd160/rmd160ossl.c deleted file mode 100644 index f24e63e3d8..0000000000 --- a/ext/digest/rmd160/rmd160ossl.c +++ /dev/null @@ -1,8 +0,0 @@ -/* $Id$ */ - -#include "defs.h" -#include "rmd160ossl.h" - -void RMD160_Finish(RMD160_CTX *ctx, char *buf) { - RIPEMD160_Final((unsigned char *)buf, ctx); -} diff --git a/ext/digest/rmd160/rmd160ossl.h b/ext/digest/rmd160/rmd160ossl.h index 3df38a01c0..e6bf5ea8d0 100644 --- a/ext/digest/rmd160/rmd160ossl.h +++ b/ext/digest/rmd160/rmd160ossl.h @@ -14,6 +14,7 @@ #define RMD160_BLOCK_LENGTH RIPEMD160_CBLOCK #define RMD160_DIGEST_LENGTH RIPEMD160_DIGEST_LENGTH -void RMD160_Finish(RMD160_CTX *ctx, char *buf); +static DEFINE_FINISH_FUNC_FROM_FINAL(RIPEMD160) +#define RMD160_Finish rb_digest_RIPEMD160_finish #endif diff --git a/ext/digest/sha1/sha1ossl.c b/ext/digest/sha1/sha1ossl.c deleted file mode 100644 index 452cf35084..0000000000 --- a/ext/digest/sha1/sha1ossl.c +++ /dev/null @@ -1,10 +0,0 @@ -/* $Id$ */ - -#include "defs.h" -#include "sha1ossl.h" - -void -SHA1_Finish(SHA1_CTX *ctx, char *buf) -{ - SHA1_Final((unsigned char *)buf, ctx); -} diff --git a/ext/digest/sha1/sha1ossl.h b/ext/digest/sha1/sha1ossl.h index 8f9984cc64..80580b6460 100644 --- a/ext/digest/sha1/sha1ossl.h +++ b/ext/digest/sha1/sha1ossl.h @@ -15,6 +15,8 @@ #endif #define SHA1_DIGEST_LENGTH SHA_DIGEST_LENGTH -void SHA1_Finish(SHA1_CTX *ctx, char *buf); +static DEFINE_FINISH_FUNC_FROM_FINAL(SHA1); +#undef SHA1_Finish +#define SHA1_Finish rb_digest_SHA1_finish #endif diff --git a/ext/digest/sha2/sha2ossl.c b/ext/digest/sha2/sha2ossl.c deleted file mode 100644 index 34353be8b8..0000000000 --- a/ext/digest/sha2/sha2ossl.c +++ /dev/null @@ -1,13 +0,0 @@ -#include "defs.h" -#include "sha2ossl.h" - -#define SHA_Finish(bit) \ - void SHA##bit##_Finish(SHA##bit##_CTX *ctx, char *buf) \ - { SHA##bit##_Final((unsigned char *)buf, ctx);} -#ifndef __DragonFly__ -#define SHA384_Final SHA512_Final -#endif - -SHA_Finish(256) -SHA_Finish(384) -SHA_Finish(512) diff --git a/ext/digest/sha2/sha2ossl.h b/ext/digest/sha2/sha2ossl.h index 4229d14cf9..f7258ee64a 100644 --- a/ext/digest/sha2/sha2ossl.h +++ b/ext/digest/sha2/sha2ossl.h @@ -8,10 +8,20 @@ #define SHA384_BLOCK_LENGTH SHA512_CBLOCK #define SHA512_BLOCK_LENGTH SHA512_CBLOCK +#ifndef __DragonFly__ +#define SHA384_Final SHA512_Final +#endif + typedef SHA512_CTX SHA384_CTX; -void SHA256_Finish(SHA256_CTX *ctx, char *buf); -void SHA384_Finish(SHA384_CTX *ctx, char *buf); -void SHA512_Finish(SHA512_CTX *ctx, char *buf); +#undef SHA256_Finish +#undef SHA384_Finish +#undef SHA512_Finish +#define SHA256_Finish rb_digest_SHA256_finish +#define SHA384_Finish rb_digest_SHA384_finish +#define SHA512_Finish rb_digest_SHA512_finish +static DEFINE_FINISH_FUNC_FROM_FINAL(SHA256); +static DEFINE_FINISH_FUNC_FROM_FINAL(SHA384); +static DEFINE_FINISH_FUNC_FROM_FINAL(SHA512); #endif