1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/ext/digest/bubblebabble/bubblebabble.c
knu b2c7fe1bbf * ext/digest: Prefix C constants with RUBY_ and C type names with
rb_ to avoid name clash in writing extensions.

* ext/digest: Introduce Digest::Class and Digest::Instance for
  ease of implementing subclasses and add-ons, inspried by
  gotoyuzo.

* ext/digest: The Digest::Instance module now requires and assumes
  that any instance be resettable and clonable, and add some
  convenient instance methods such as "new()", for creating a new
  copy, parameter taking "digest()" and "hexdigest()", for instant
  calculation.  These methods make digest instances work just like
  digest classes.

* ext/digest/sha2/lib/digest/sha2.rb:
  Add the Digest::SHA2 class to wrap up SHA2 variants: SHA256,
  SHA384 and SHA512, hoping this module would make a decent
  example of a digest subclass written in Ruby.

* ext/digest/lib/digest.rb: Adjust autoload entries for SHA2
  classes.

* ext/digest/lib/digest/hmac.rb: Follow the framework updates.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11197 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2006-10-20 12:48:35 +00:00

142 lines
3.3 KiB
C

/************************************************
bubblebabble.c - BubbleBabble encoding support
$Author$
created at: Fri Oct 13 18:31:42 JST 2006
Copyright (C) 2006 Akinori MUSHA
$Id$
************************************************/
#include "ruby.h"
#include "digest.h"
static ID id_digest;
static VALUE
bubblebabble_str_new(VALUE str_digest)
{
char *digest;
size_t digest_len;
VALUE str;
char *p;
int i, j, seed = 1;
static const char vowels[] = {
'a', 'e', 'i', 'o', 'u', 'y'
};
static const char consonants[] = {
'b', 'c', 'd', 'f', 'g', 'h', 'k', 'l', 'm', 'n',
'p', 'r', 's', 't', 'v', 'z', 'x'
};
StringValue(str_digest);
digest = RSTRING_PTR(str_digest);
digest_len = RSTRING_LEN(str_digest);
if ((LONG_MAX - 2) / 3 < (digest_len | 1)) {
rb_raise(rb_eRuntimeError, "digest string too long");
}
str = rb_str_new(0, (digest_len | 1) * 3 + 2);
p = RSTRING_PTR(str);
i = j = 0;
p[j++] = 'x';
for (;;) {
unsigned char byte1, byte2;
if (i >= digest_len) {
p[j++] = vowels[seed % 6];
p[j++] = consonants[16];
p[j++] = vowels[seed / 6];
break;
}
byte1 = digest[i++];
p[j++] = vowels[(((byte1 >> 6) & 3) + seed) % 6];
p[j++] = consonants[(byte1 >> 2) & 15];
p[j++] = vowels[((byte1 & 3) + (seed / 6)) % 6];
if (i >= digest_len) {
break;
}
byte2 = digest[i++];
p[j++] = consonants[(byte2 >> 4) & 15];
p[j++] = '-';
p[j++] = consonants[byte2 & 15];
seed = (seed * 5 + byte1 * 7 + byte2) % 36;
}
p[j] = 'x';
return str;
}
/*
* call-seq:
* Digest.bubblebabble(string) -> bubblebabble_string
*
* Returns a BubbleBabble encoded version of a given _string_.
*/
static VALUE
rb_digest_s_bubblebabble(VALUE klass, VALUE str)
{
return bubblebabble_str_new(str);
}
/*
* call-seq:
* Digest::Class.bubblebabble(string, ...) -> hash_string
*
* Returns the BubbleBabble encoded hash value of a given _string_.
*/
static VALUE
rb_digest_class_s_bubblebabble(int argc, VALUE *argv, VALUE klass)
{
return bubblebabble_str_new(rb_funcall2(klass, id_digest, argc, argv));
}
/*
* call-seq:
* digest_obj.bubblebabble -> hash_string
*
* Returns the resulting hash value in a Bubblebabble encoded form.
*/
static VALUE
rb_digest_instance_bubblebabble(VALUE self)
{
return bubblebabble_str_new(rb_funcall(self, id_digest, 0));
}
/*
* This module adds some methods to Digest classes to perform
* BubbleBabble encoding.
*/
void
Init_bubblebabble(void)
{
VALUE mDigest, mDigest_Instance, cDigest_Class;
rb_require("digest");
mDigest = rb_path2class("Digest");
mDigest_Instance = rb_path2class("Digest::Instance");
cDigest_Class = rb_path2class("Digest::Class");
/* Digest::bubblebabble() */
rb_define_module_function(mDigest, "bubblebabble", rb_digest_s_bubblebabble, 1);
/* Digest::Class::bubblebabble() */
rb_define_singleton_method(cDigest_Class, "bubblebabble", rb_digest_class_s_bubblebabble, -1);
/* Digest::Instance#bubblebabble() */
rb_define_method(mDigest_Instance, "bubblebabble", rb_digest_instance_bubblebabble, 0);
id_digest = rb_intern("digest");
}