2006-10-13 07:46:03 -04:00
|
|
|
/************************************************
|
|
|
|
|
|
|
|
bubblebabble.c - BubbleBabble encoding support
|
|
|
|
|
|
|
|
$Author$
|
|
|
|
created at: Fri Oct 13 18:31:42 JST 2006
|
|
|
|
|
|
|
|
Copyright (C) 2006 Akinori MUSHA
|
|
|
|
|
|
|
|
$Id$
|
|
|
|
|
|
|
|
************************************************/
|
|
|
|
|
2015-11-20 23:35:57 -05:00
|
|
|
#include <ruby/ruby.h>
|
|
|
|
#include "../digest.h"
|
2006-10-13 07:46:03 -04:00
|
|
|
|
|
|
|
static ID id_digest;
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
bubblebabble_str_new(VALUE str_digest)
|
|
|
|
{
|
|
|
|
char *digest;
|
|
|
|
size_t digest_len;
|
|
|
|
VALUE str;
|
|
|
|
char *p;
|
2009-10-16 00:36:31 -04:00
|
|
|
size_t i, j, seed = 1;
|
2006-10-13 07:46:03 -04:00
|
|
|
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;
|
2010-04-22 04:04:13 -04:00
|
|
|
}
|
2006-10-13 07:46:03 -04:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-11-21 02:53:39 -05:00
|
|
|
/* Document-method: Digest::bubblebabble
|
|
|
|
*
|
2006-10-13 07:46:03 -04:00
|
|
|
* call-seq:
|
|
|
|
* Digest.bubblebabble(string) -> bubblebabble_string
|
|
|
|
*
|
2006-10-13 15:40:01 -04:00
|
|
|
* Returns a BubbleBabble encoded version of a given _string_.
|
|
|
|
*/
|
2006-10-13 07:46:03 -04:00
|
|
|
static VALUE
|
|
|
|
rb_digest_s_bubblebabble(VALUE klass, VALUE str)
|
|
|
|
{
|
|
|
|
return bubblebabble_str_new(str);
|
|
|
|
}
|
|
|
|
|
2013-11-21 02:53:39 -05:00
|
|
|
/* Document-method: Digest::Class::bubblebabble
|
|
|
|
*
|
2006-10-13 07:46:03 -04:00
|
|
|
* call-seq:
|
2006-10-20 08:48:35 -04:00
|
|
|
* Digest::Class.bubblebabble(string, ...) -> hash_string
|
2006-10-13 07:46:03 -04:00
|
|
|
*
|
|
|
|
* Returns the BubbleBabble encoded hash value of a given _string_.
|
|
|
|
*/
|
|
|
|
static VALUE
|
2006-10-20 08:48:35 -04:00
|
|
|
rb_digest_class_s_bubblebabble(int argc, VALUE *argv, VALUE klass)
|
2006-10-13 07:46:03 -04:00
|
|
|
{
|
2016-07-29 07:57:14 -04:00
|
|
|
return bubblebabble_str_new(rb_funcallv(klass, id_digest, argc, argv));
|
2006-10-13 07:46:03 -04:00
|
|
|
}
|
|
|
|
|
2013-11-21 02:53:39 -05:00
|
|
|
/* Document-method: Digest::Instance#bubblebabble
|
|
|
|
*
|
2006-10-13 07:46:03 -04:00
|
|
|
* call-seq:
|
|
|
|
* digest_obj.bubblebabble -> hash_string
|
|
|
|
*
|
|
|
|
* Returns the resulting hash value in a Bubblebabble encoded form.
|
|
|
|
*/
|
|
|
|
static VALUE
|
2006-10-20 08:48:35 -04:00
|
|
|
rb_digest_instance_bubblebabble(VALUE self)
|
2006-10-13 07:46:03 -04:00
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
2013-11-21 02:53:39 -05:00
|
|
|
VALUE rb_mDigest, rb_mDigest_Instance, rb_cDigest_Class;
|
2006-10-20 08:48:35 -04:00
|
|
|
|
|
|
|
rb_require("digest");
|
|
|
|
|
2013-11-21 02:53:39 -05:00
|
|
|
rb_mDigest = rb_path2class("Digest");
|
|
|
|
rb_mDigest_Instance = rb_path2class("Digest::Instance");
|
|
|
|
rb_cDigest_Class = rb_path2class("Digest::Class");
|
2006-10-13 07:46:03 -04:00
|
|
|
|
2013-11-21 02:53:39 -05:00
|
|
|
#if 0
|
|
|
|
rb_mDigest = rb_define_module("Digest");
|
|
|
|
rb_mDigest_Instance = rb_define_module_under(rb_mDigest, "Instance");
|
|
|
|
rb_cDigest_Class = rb_define_class_under(rb_mDigest, "Class", rb_cObject);
|
|
|
|
#endif
|
2006-10-13 07:46:03 -04:00
|
|
|
|
2013-11-21 02:53:39 -05:00
|
|
|
rb_define_module_function(rb_mDigest, "bubblebabble", rb_digest_s_bubblebabble, 1);
|
|
|
|
rb_define_singleton_method(rb_cDigest_Class, "bubblebabble", rb_digest_class_s_bubblebabble, -1);
|
|
|
|
rb_define_method(rb_mDigest_Instance, "bubblebabble", rb_digest_instance_bubblebabble, 0);
|
2006-10-13 07:46:03 -04:00
|
|
|
|
|
|
|
id_digest = rb_intern("digest");
|
|
|
|
}
|