Ruby  3.1.0dev(2021-09-10revisionb76ad15ed0da636161de0243c547ee1e6fc95681)
bubblebabble.c
Go to the documentation of this file.
1 /************************************************
2 
3  bubblebabble.c - BubbleBabble encoding support
4 
5  $Author$
6  created at: Fri Oct 13 18:31:42 JST 2006
7 
8  Copyright (C) 2006 Akinori MUSHA
9 
10  $Id$
11 
12 ************************************************/
13 
14 #include <ruby/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 /* Document-method: Digest::bubblebabble
82  *
83  * call-seq:
84  * Digest.bubblebabble(string) -> bubblebabble_string
85  *
86  * Returns a BubbleBabble encoded version of a given _string_.
87  */
88 static VALUE
89 rb_digest_s_bubblebabble(VALUE klass, VALUE str)
90 {
91  return bubblebabble_str_new(str);
92 }
93 
94 /* Document-method: Digest::Class::bubblebabble
95  *
96  * call-seq:
97  * Digest::Class.bubblebabble(string, ...) -> hash_string
98  *
99  * Returns the BubbleBabble encoded hash value of a given _string_.
100  */
101 static VALUE
102 rb_digest_class_s_bubblebabble(int argc, VALUE *argv, VALUE klass)
103 {
104  return bubblebabble_str_new(rb_funcallv(klass, id_digest, argc, argv));
105 }
106 
107 /* Document-method: Digest::Instance#bubblebabble
108  *
109  * call-seq:
110  * digest_obj.bubblebabble -> hash_string
111  *
112  * Returns the resulting hash value in a Bubblebabble encoded form.
113  */
114 static VALUE
115 rb_digest_instance_bubblebabble(VALUE self)
116 {
117  return bubblebabble_str_new(rb_funcall(self, id_digest, 0));
118 }
119 
120 /*
121  * This module adds some methods to Digest classes to perform
122  * BubbleBabble encoding.
123  */
124 void
126 {
127 #undef rb_intern
128  VALUE rb_mDigest, rb_mDigest_Instance, rb_cDigest_Class;
129 
130  rb_require("digest");
131 
132  rb_mDigest = rb_path2class("Digest");
133  rb_mDigest_Instance = rb_path2class("Digest::Instance");
134  rb_cDigest_Class = rb_path2class("Digest::Class");
135 
136 #if 0
137  rb_mDigest = rb_define_module("Digest");
138  rb_mDigest_Instance = rb_define_module_under(rb_mDigest, "Instance");
139  rb_cDigest_Class = rb_define_class_under(rb_mDigest, "Class", rb_cObject);
140 #endif
141 
142  rb_define_module_function(rb_mDigest, "bubblebabble", rb_digest_s_bubblebabble, 1);
143  rb_define_singleton_method(rb_cDigest_Class, "bubblebabble", rb_digest_class_s_bubblebabble, -1);
144  rb_define_method(rb_mDigest_Instance, "bubblebabble", rb_digest_instance_bubblebabble, 0);
145 
146  id_digest = rb_intern("digest");
147 }
rb_define_singleton_method
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
Definition: cxxanyargs.hpp:670
StringValue
#define StringValue(v)
Definition: rstring.h:50
rb_define_module_under
VALUE rb_define_module_under(VALUE outer, const char *name)
Definition: class.c:914
LONG_MAX
#define LONG_MAX
Definition: limits.h:36
rb_intern
ID rb_intern(const char *)
Definition: symbol.c:784
Init_bubblebabble
void Init_bubblebabble(void)
Definition: bubblebabble.c:125
rb_define_module
VALUE rb_define_module(const char *name)
Definition: class.c:887
argv
char ** argv
Definition: ruby.c:243
ID
unsigned long ID
Definition: value.h:39
RSTRING_LEN
#define RSTRING_LEN(string)
Definition: fbuffer.h:22
rb_funcallv
#define rb_funcallv(...)
Definition: internal.h:77
ruby.h
rb_str_new
#define rb_str_new(str, len)
Definition: string.h:213
rb_raise
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:3022
rb_cObject
VALUE rb_cObject
Object class.
Definition: object.c:50
rb_eRuntimeError
VALUE rb_eRuntimeError
Definition: error.c:1091
VALUE
unsigned long VALUE
Definition: value.h:38
rb_funcall
VALUE rb_funcall(VALUE, ID, int,...)
Calls a method.
Definition: vm_eval.c:1113
RSTRING_PTR
#define RSTRING_PTR(string)
Definition: fbuffer.h:19
str
char str[HTML_ESCAPE_MAX_LEN+1]
Definition: escape.c:18
argc
int argc
Definition: ruby.c:242
rb_require
VALUE rb_require(const char *)
Definition: load.c:1186
rb_path2class
VALUE rb_path2class(const char *)
Definition: variable.c:289
rb_define_class_under
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
Definition: class.c:809
rb_define_method
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
Definition: cxxanyargs.hpp:655
rb_define_module_function
#define rb_define_module_function(klass, mid, func, arity)
Defines klass#mid and makes it a module function.
Definition: cxxanyargs.hpp:674