fargo 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,61 @@
1
+ #include <stdlib.h>
2
+
3
+ #include "base32.h"
4
+
5
+ char base32_alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
6
+
7
+ char* base32_encode(const unsigned char* buffer, int len)
8
+ {
9
+ unsigned char* digest = NULL;
10
+ if (len > 0) {
11
+ int i, j = 0;
12
+ int bits_remain = 0;
13
+ unsigned short value = 0;
14
+ unsigned long digest_len = len*8;
15
+ digest_len = 1 + ((digest_len % 5) == 0 ? (digest_len / 5) : (digest_len / 5) + 1);
16
+
17
+ digest = (unsigned char*)malloc(digest_len);
18
+ if (digest != NULL) {
19
+ for (i = 0; i < len; i++) {
20
+ value = (value << 8) | buffer[i];
21
+ bits_remain += 8;
22
+ while (bits_remain > 5 && j < 1023) {
23
+ int idx = (value >> (bits_remain-5)) & 0x1F;
24
+ digest[j++] = base32_alphabet[idx];
25
+ bits_remain -= 5;
26
+ }
27
+ }
28
+ if (bits_remain > 0) {
29
+ int idx = (value << (5-bits_remain)) & 0x1F;
30
+ digest[j++] = base32_alphabet[idx];
31
+ }
32
+ digest[j] = '\0';
33
+ }
34
+ }
35
+ return digest;
36
+ }
37
+
38
+ #if 0
39
+ char* base32_decode(const unsigned char* buffer, int len)
40
+ {
41
+ int i, j = 0;
42
+ int bits_remain = 0;
43
+ unsigned short value = 0;
44
+
45
+ for (i = 0; i < len; i++) {
46
+ value |= (buffer[i] << bits_remain);
47
+ bits_remain += 8;
48
+ while (bits_remain > 5 && j < 1023) {
49
+ base32_digest[j++] = (value & 0x1F) < 26 ? 'A'+(value & 0x1F) : '2' + (value & 0x1F);
50
+ value >>= 5;
51
+ bits_remain -= 5;
52
+ }
53
+ }
54
+ if (bits_remain > 0) {
55
+ base32_digest[j++] = (value & 0x1F) < 26 ? 'A'+(value & 0x1F) : '2' + (value & 0x1F);
56
+ }
57
+ base32_digest[j] = '\0';
58
+ return base32_digest;
59
+ }
60
+
61
+ #endif
@@ -0,0 +1,16 @@
1
+ #ifndef __BASE32_H
2
+ #define __BASE32_H
3
+
4
+ #if defined(__cplusplus)
5
+ extern "C" {
6
+ #endif
7
+
8
+ // user must call free() funtion on pointer returned from these functions
9
+
10
+ char* base32_encode(const unsigned char* in, int inlen);
11
+
12
+ #if defined(__cplusplus)
13
+ }
14
+ #endif
15
+
16
+ #endif // ifndef __BASE32_H
@@ -0,0 +1,8 @@
1
+ # This uses mkmf
2
+ require 'mkmf'
3
+
4
+ if RUBY_VERSION =~ /1.9/
5
+ $CFLAGS << ' -DRUBY_19'
6
+ end
7
+
8
+ create_makefile('tth')
@@ -0,0 +1,34 @@
1
+ #include <ruby.h>
2
+ #include <string.h>
3
+ #include "tth.h"
4
+
5
+ #ifndef RUBY_19
6
+ # define RSTRING_LEN(s) (RSTRING(s)->len)
7
+ # define RSTRING_PTR(s) (RSTRING(s)->ptr)
8
+ # define RARRAY_LEN(s) (RARRAY(s)->len)
9
+ # define RARRAY_PTR(s) (RARRAY(s)->ptr)
10
+ #endif
11
+
12
+ VALUE rb_tth_file(VALUE self, VALUE filename) {
13
+ char *file = RSTRING_PTR(filename);
14
+
15
+ char* tthl = NULL;
16
+ size_t tthl_size;
17
+ char* hash = tth(file, &tthl, &tthl_size);
18
+ if (tthl != NULL) {
19
+ free(tthl);
20
+ }
21
+
22
+ if (hash != NULL) {
23
+ return rb_str_new(hash, strlen(hash));
24
+ } else {
25
+ return Qnil;
26
+ }
27
+ }
28
+
29
+ Init_tth() {
30
+ VALUE cFargo = rb_define_module("Fargo");
31
+ VALUE cTTH = rb_define_module_under(cFargo, "TTH");
32
+
33
+ rb_define_method(cTTH, "file_tth", rb_tth_file, 1);
34
+ }