Ruby 2.0.0p353(2013-11-22revision43784)

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

bubblebabble.c
Go to the documentation of this file.
1 /************************************************
2
3 bubblebabble.c - BubbleBabble encoding support
4
5 $Author: nobu $
6 created at: Fri Oct 13 18:31:42 JST 2006
7
8 Copyright (C) 2006 Akinori MUSHA
9
10 $Id: bubblebabble.c 27437 2010-04-22 08:04:13Z nobu $
11
12 ************************************************/
13
14 #include "ruby.h"
15 #include "digest.h"
16
17 static ID id_digest;
18
19 static VALUE
20 bubblebabble_str_new(VALUE str_digest)
21 {
22 char *digest;
23 size_t digest_len;
24 VALUE str;
25 char *p;
26 size_t i, j, seed = 1;
27 static const char vowels[] = {
28 'a', 'e', 'i', 'o', 'u', 'y'
29 };
30 static const char consonants[] = {
31 'b', 'c', 'd', 'f', 'g', 'h', 'k', 'l', 'm', 'n',
32 'p', 'r', 's', 't', 'v', 'z', 'x'
33 };
34
35 StringValue(str_digest);
36 digest = RSTRING_PTR(str_digest);
37 digest_len = RSTRING_LEN(str_digest);
38
39 if ((LONG_MAX - 2) / 3 < (digest_len | 1)) {
40 rb_raise(rb_eRuntimeError, "digest string too long");
41 }
42
43 str = rb_str_new(0, (digest_len | 1) * 3 + 2);
44 p = RSTRING_PTR(str);
45
46 i = j = 0;
47 p[j++] = 'x';
48
49 for (;;) {
50 unsigned char byte1, byte2;
51
52 if (i >= digest_len) {
53 p[j++] = vowels[seed % 6];
54 p[j++] = consonants[16];
55 p[j++] = vowels[seed / 6];
56 break;
57 }
58
59 byte1 = digest[i++];
60 p[j++] = vowels[(((byte1 >> 6) & 3) + seed) % 6];
61 p[j++] = consonants[(byte1 >> 2) & 15];
62 p[j++] = vowels[((byte1 & 3) + (seed / 6)) % 6];
63
64 if (i >= digest_len) {
65 break;
66 }
67
68 byte2 = digest[i++];
69 p[j++] = consonants[(byte2 >> 4) & 15];
70 p[j++] = '-';
71 p[j++] = consonants[byte2 & 15];
72
73 seed = (seed * 5 + byte1 * 7 + byte2) % 36;
74 }
75
76 p[j] = 'x';
77
78 return str;
79 }
80
81 /*
82 * call-seq:
83 * Digest.bubblebabble(string) -> bubblebabble_string
84 *
85 * Returns a BubbleBabble encoded version of a given _string_.
86 */
87 static VALUE
88 rb_digest_s_bubblebabble(VALUE klass, VALUE str)
89 {
90 return bubblebabble_str_new(str);
91 }
92
93 /*
94 * call-seq:
95 * Digest::Class.bubblebabble(string, ...) -> hash_string
96 *
97 * Returns the BubbleBabble encoded hash value of a given _string_.
98 */
99 static VALUE
100 rb_digest_class_s_bubblebabble(int argc, VALUE *argv, VALUE klass)
101 {
102 return bubblebabble_str_new(rb_funcall2(klass, id_digest, argc, argv));
103 }
104
105 /*
106 * call-seq:
107 * digest_obj.bubblebabble -> hash_string
108 *
109 * Returns the resulting hash value in a Bubblebabble encoded form.
110 */
111 static VALUE
112 rb_digest_instance_bubblebabble(VALUE self)
113 {
114 return bubblebabble_str_new(rb_funcall(self, id_digest, 0));
115 }
116
117 /*
118 * This module adds some methods to Digest classes to perform
119 * BubbleBabble encoding.
120 */
121 void
122 Init_bubblebabble(void)
123 {
124 VALUE mDigest, mDigest_Instance, cDigest_Class;
125
126 rb_require("digest");
127
128 mDigest = rb_path2class("Digest");
129 mDigest_Instance = rb_path2class("Digest::Instance");
130 cDigest_Class = rb_path2class("Digest::Class");
131
132 /* Digest::bubblebabble() */
133 rb_define_module_function(mDigest, "bubblebabble", rb_digest_s_bubblebabble, 1);
134
135 /* Digest::Class::bubblebabble() */
136 rb_define_singleton_method(cDigest_Class, "bubblebabble", rb_digest_class_s_bubblebabble, -1);
137
138 /* Digest::Instance#bubblebabble() */
139 rb_define_method(mDigest_Instance, "bubblebabble", rb_digest_instance_bubblebabble, 0);
140
141 id_digest = rb_intern("digest");
142 }
p
Win32OLEIDispatch * p
Definition: win32ole.c:786
rb_define_singleton_method
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a singleton method for obj.
Definition: class.c:1497
i
ssize_t i
Definition: bigdecimal.c:5655
Init_bubblebabble
void Init_bubblebabble(void)
Definition: bubblebabble.c:122
ruby.h
rb_require
VALUE rb_require(const char *)
Definition: load.c:1024
RSTRING_PTR
#define RSTRING_PTR(str)
rb_digest_s_bubblebabble
static VALUE rb_digest_s_bubblebabble(VALUE klass, VALUE str)
Definition: bubblebabble.c:88
rb_funcall
VALUE rb_funcall(VALUE, ID, int,...)
Calls a method.
Definition: vm_eval.c:774
digest.h
rb_raise
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:1780
rb_path2class
VALUE rb_path2class(const char *)
Definition: variable.c:371
bubblebabble_str_new
static VALUE bubblebabble_str_new(VALUE str_digest)
Definition: bubblebabble.c:20
rb_digest_class_s_bubblebabble
static VALUE rb_digest_class_s_bubblebabble(int argc, VALUE *argv, VALUE klass)
Definition: bubblebabble.c:100
rb_eRuntimeError
VALUE rb_eRuntimeError
Definition: error.c:510
id_digest
static ID id_digest
Definition: bubblebabble.c:17
str
static VALUE char * str
Definition: tcltklib.c:3547
ID
unsigned long ID
Definition: ripper.y:105
RSTRING_LEN
#define RSTRING_LEN(str)
rb_digest_instance_bubblebabble
static VALUE rb_digest_instance_bubblebabble(VALUE self)
Definition: bubblebabble.c:112
LONG_MAX
#define LONG_MAX
Definition: ruby.h:201
argv
VALUE * argv
Definition: tcltklib.c:1971
rb_define_module_function
void rb_define_module_function(VALUE module, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a module function for module.
Definition: class.c:1512
StringValue
#define StringValue(v)
argc
int argc
Definition: tcltklib.c:1970
klass
klass
Definition: tcltklib.c:3504
rb_str_new
VALUE rb_str_new(const char *, long)
Definition: string.c:425
rb_funcall2
VALUE rb_funcall2(VALUE, ID, int, const VALUE *)
Calls a method.
Definition: vm_eval.c:805
VALUE
unsigned long VALUE
Definition: ripper.y:104
rb_intern
#define rb_intern(str)
rb_define_method
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1344