summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorknu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2006-10-13 11:46:03 +0000
committerknu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2006-10-13 11:46:03 +0000
commit2125aafc273147f183925556f1ee28b6568fd512 (patch)
treeda38b498e87893493f8faffd1826b2641c1a2b5e
parentb9dea008c7bea3457108875a306aee5508c3452e (diff)
* ext/digest/bubblebabble, ext/digest/digest.c: Rip BubbleBabble
support out of the base class and have a separate module named digest/bubblebabble. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11152 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog6
-rw-r--r--ext/digest/bubblebabble/bubblebabble.c136
-rw-r--r--ext/digest/bubblebabble/extconf.rb3
-rw-r--r--ext/digest/digest.c76
4 files changed, 145 insertions, 76 deletions
diff --git a/ChangeLog b/ChangeLog
index d70ff8e..a592531 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+Fri Oct 13 20:38:12 2006 Akinori MUSHA <knu@iDaemons.org>
+
+ * ext/digest/bubblebabble, ext/digest/digest.c: Rip BubbleBabble
+ support out of the base class and have a separate module named
+ digest/bubblebabble.
+
Fri Oct 13 19:53:59 2006 Akinori MUSHA <knu@iDaemons.org>
* ext/digest/digest.c (rb_digest_base_equal): Again, should call
diff --git a/ext/digest/bubblebabble/bubblebabble.c b/ext/digest/bubblebabble/bubblebabble.c
new file mode 100644
index 0000000..1e882cc
--- /dev/null
+++ b/ext/digest/bubblebabble/bubblebabble.c
@@ -0,0 +1,136 @@
+/************************************************
+
+ 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"
+
+static VALUE mDigest, cDigest_Base;
+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::ALGORITHM.bubblebabble(string) -> hash_string
+ *
+ * Returns the BubbleBabble encoded hash value of a given _string_.
+ */
+static VALUE
+rb_digest_base_s_bubblebabble(VALUE klass, VALUE str)
+{
+ return bubblebabble_str_new(rb_funcall(klass, id_digest, 1, str));
+}
+
+/*
+ * call-seq:
+ * digest_obj.bubblebabble -> hash_string
+ *
+ * Returns the resulting hash value in a Bubblebabble encoded form.
+ */
+static VALUE
+rb_digest_base_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)
+{
+ mDigest = rb_define_module("Digest");
+ cDigest_Base = rb_define_class_under(mDigest, "Base", rb_cObject);
+
+ /* Digest::bubblebabble() */
+ rb_define_module_function(mDigest, "bubblebabble", rb_digest_s_bubblebabble, 1);
+
+ /* Digest::Base::bubblebabble() */
+ rb_define_singleton_method(cDigest_Base, "bubblebabble", rb_digest_base_s_bubblebabble, 1);
+
+ /* Digest::Base#bubblebabble() */
+ rb_define_method(cDigest_Base, "bubblebabble", rb_digest_base_bubblebabble, 0);
+
+ id_digest = rb_intern("digest");
+}
diff --git a/ext/digest/bubblebabble/extconf.rb b/ext/digest/bubblebabble/extconf.rb
new file mode 100644
index 0000000..5b7f911
--- /dev/null
+++ b/ext/digest/bubblebabble/extconf.rb
@@ -0,0 +1,3 @@
+require 'mkmf'
+
+create_makefile('digest/bubblebabble')
diff --git a/ext/digest/digest.c b/ext/digest/digest.c
index 4c7cac1..8fa3c08 100644
--- a/ext/digest/digest.c
+++ b/ext/digest/digest.c
@@ -73,68 +73,6 @@ hexdigest_str_new(VALUE str_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;
-}
-
-static VALUE
rb_digest_base_alloc(VALUE klass)
{
algo_t *algo;
@@ -191,12 +129,6 @@ rb_digest_base_s_hexdigest(VALUE klass, VALUE str)
}
static VALUE
-rb_digest_base_s_bubblebabble(VALUE klass, VALUE str)
-{
- return bubblebabble_str_new(rb_funcall(klass, id_digest, 1, str));
-}
-
-static VALUE
rb_digest_base_copy(VALUE copy, VALUE obj)
{
algo_t *algo;
@@ -335,12 +267,6 @@ rb_digest_base_hexdigest(VALUE self)
}
static VALUE
-rb_digest_base_bubblebabble(VALUE self)
-{
- return bubblebabble_str_new(rb_funcall(self, id_digest, 0));
-}
-
-static VALUE
rb_digest_base_inspect(VALUE self)
{
algo_t *algo;
@@ -425,7 +351,6 @@ Init_digest(void)
rb_define_alloc_func(cDigest_Base, rb_digest_base_alloc);
rb_define_singleton_method(cDigest_Base, "digest", rb_digest_base_s_digest, 1);
rb_define_singleton_method(cDigest_Base, "hexdigest", rb_digest_base_s_hexdigest, 1);
- rb_define_singleton_method(cDigest_Base, "bubblebabble", rb_digest_base_s_bubblebabble, 1);
rb_define_method(cDigest_Base, "initialize", rb_digest_base_init, -1);
rb_define_method(cDigest_Base, "initialize_copy", rb_digest_base_copy, 1);
@@ -434,7 +359,6 @@ Init_digest(void)
rb_define_method(cDigest_Base, "<<", rb_digest_base_lshift, 1);
rb_define_method(cDigest_Base, "digest", rb_digest_base_digest, 0);
rb_define_method(cDigest_Base, "hexdigest", rb_digest_base_hexdigest, 0);
- rb_define_method(cDigest_Base, "bubblebabble", rb_digest_base_bubblebabble, 0);
rb_define_method(cDigest_Base, "to_s", rb_digest_base_hexdigest, 0);
rb_define_method(cDigest_Base, "inspect", rb_digest_base_inspect, 0);
rb_define_method(cDigest_Base, "==", rb_digest_base_equal, 1);