From b9673f64f10197b4c2742331e0cccb9c1352c7bc Mon Sep 17 00:00:00 2001 From: knu Date: Thu, 5 Oct 2006 11:09:42 +0000 Subject: * ext/digest/digest.[ch]: Since the argument order of hash_final_func_t was inconsistent with others, change it and rename to hash_finish_func_t to avoid confusion. * ext/digest/digest.[ch]: Remove and eliminate the use of hash_end_func_t. Implement hexdigest conversion in the base class. * ext/digest/md5/md5.c, ext/digest/md5/md5.h, ext/digest/md5/md5init.c, ext/digest/md5/md5ossl.c, ext/digest/md5/md5ossl.h: Remove MD5_End() and change MD5_Final() to MD5_Finish(). * ext/digest/rmd160/depend, ext/digest/rmd160/extconf.rb, ext/digest/rmd160/rmd160.c, ext/digest/rmd160/rmd160.h, ext/digest/rmd160/rmd160hl.c, ext/digest/rmd160/rmd160init.c, ext/digest/rmd160/rmd160ossl.c, ext/digest/rmd160/rmd160ossl.h: Remove unused functions RMD160_End(), RMD160_File(), RMD160_Data() and change RMD160_Final() to RMD160_Finish(). * ext/digest/sha1/extconf.rb, ext/digest/sha1/sha1.c, ext/digest/sha1/sha1.h, ext/digest/sha1/sha1hl.c, ext/digest/sha1/sha1init.c, ext/digest/sha1/sha1ossl.c, ext/digest/sha1/sha1ossl.h: Likewise. * ext/digest/sha2/extconf.rb, ext/digest/sha2/sha2.c, ext/digest/sha2/sha2.h, ext/digest/sha2/sha2hl.c, ext/digest/sha2/sha2init.c: Likewise. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11086 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ext/digest/digest.c | 64 +++++++++++++++++++++++++++++++++++------------------ 1 file changed, 43 insertions(+), 21 deletions(-) (limited to 'ext/digest/digest.c') diff --git a/ext/digest/digest.c b/ext/digest/digest.c index a2e0d55c91..f56a1921f8 100644 --- a/ext/digest/digest.c +++ b/ext/digest/digest.c @@ -39,6 +39,26 @@ get_digest_base_metadata(VALUE klass) return algo; } +static VALUE +hexdigest_str_new(const unsigned char *digest, size_t digest_len) +{ + int i; + VALUE str; + char *p; + static const char hex[] = "0123456789abcdef"; + + str = rb_str_new(0, digest_len * 2); + + for (i = 0, p = RSTRING_PTR(str); i < digest_len; i++) { + unsigned char byte = digest[i]; + + p[i + i] = hex[byte >> 4]; + p[i + i + 1] = hex[byte & 0x0f]; + } + + return str; +} + static VALUE rb_digest_base_alloc(VALUE klass) { @@ -52,7 +72,7 @@ rb_digest_base_alloc(VALUE klass) algo = get_digest_base_metadata(klass); - /* XXX: An uninitialized buffer leads ALGO_Equal() to fail */ + /* XXX: An uninitialized buffer may lead ALGO_Equal() to fail */ pctx = xcalloc(algo->ctx_size, 1); algo->init_func(pctx); @@ -75,7 +95,7 @@ rb_digest_base_s_digest(VALUE klass, VALUE str) algo->update_func(pctx, RSTRING_PTR(str), RSTRING_LEN(str)); str = rb_str_new(0, algo->digest_len); - algo->final_func(RSTRING_PTR(str), pctx); + algo->finish_func(pctx, RSTRING_PTR(str)); return str; } @@ -85,6 +105,8 @@ rb_digest_base_s_hexdigest(VALUE klass, VALUE str) { algo_t *algo; void *pctx; + void *digest; + size_t len; volatile VALUE obj = rb_digest_base_alloc(klass); algo = get_digest_base_metadata(klass); @@ -93,10 +115,11 @@ rb_digest_base_s_hexdigest(VALUE klass, VALUE str) StringValue(str); algo->update_func(pctx, RSTRING_PTR(str), RSTRING_LEN(str)); - str = rb_str_new(0, algo->digest_len * 2); - algo->end_func(pctx, RSTRING_PTR(str)); + len = algo->digest_len; + digest = xmalloc(len); + algo->finish_func(pctx, digest); - return str; + return hexdigest_str_new(digest, len); } static VALUE @@ -150,19 +173,18 @@ rb_digest_base_digest(VALUE self) { algo_t *algo; void *pctx1, *pctx2; - size_t len; + size_t ctx_size; VALUE str; algo = get_digest_base_metadata(rb_obj_class(self)); Data_Get_Struct(self, void, pctx1); - str = rb_str_new(0, algo->digest_len); - - len = algo->ctx_size; - pctx2 = xmalloc(len); - memcpy(pctx2, pctx1, len); + ctx_size = algo->ctx_size; + pctx2 = xmalloc(ctx_size); + memcpy(pctx2, pctx1, ctx_size); - algo->final_func(RSTRING_PTR(str), pctx2); + str = rb_str_new(0, algo->digest_len); + algo->finish_func(pctx2, RSTRING_PTR(str)); free(pctx2); return str; @@ -173,22 +195,22 @@ rb_digest_base_hexdigest(VALUE self) { algo_t *algo; void *pctx1, *pctx2; - size_t len; - VALUE str; + void *digest; + size_t ctx_size, len; algo = get_digest_base_metadata(rb_obj_class(self)); Data_Get_Struct(self, void, pctx1); - str = rb_str_new(0, algo->digest_len * 2); + ctx_size = algo->ctx_size; + pctx2 = xmalloc(ctx_size); + memcpy(pctx2, pctx1, ctx_size); - len = algo->ctx_size; - pctx2 = xmalloc(len); - memcpy(pctx2, pctx1, len); - - algo->end_func(pctx2, RSTRING_PTR(str)); + len = algo->digest_len; + digest = xmalloc(len); + algo->finish_func(pctx2, digest); free(pctx2); - return str; + return hexdigest_str_new(digest, len); } static VALUE -- cgit v1.2.3