1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
#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;
size_t 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_s_bubblebabble(VALUE klass, VALUE str)
{
return bubblebabble_str_new(str);
}
static VALUE
rb_digest_class_s_bubblebabble(int argc, VALUE *argv, VALUE klass)
{
return bubblebabble_str_new(rb_funcall2(klass, id_digest, argc, argv));
}
static VALUE
rb_digest_instance_bubblebabble(VALUE self)
{
return bubblebabble_str_new(rb_funcall(self, id_digest, 0));
}
void
Init_bubblebabble(void)
{
VALUE rb_mDigest, rb_mDigest_Instance, rb_cDigest_Class;
rb_require("digest");
rb_mDigest = rb_path2class("Digest");
rb_mDigest_Instance = rb_path2class("Digest::Instance");
rb_cDigest_Class = rb_path2class("Digest::Class");
#if 0
#endif
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);
id_digest = rb_intern("digest");
}
|