Skip to content

Commit 02221d7

Browse files
committed
added PolarSSL::Entropy
1 parent f8c13fc commit 02221d7

File tree

2 files changed

+69
-12
lines changed

2 files changed

+69
-12
lines changed

src/polarssl.c

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,63 @@
11
#include "mruby.h"
2-
#include <stdio.h>
3-
#include <string.h>
4-
#include "mruby/array.h"
5-
#include "mruby/class.h"
62
#include "mruby/data.h"
7-
#include "mruby/string.h"
8-
#include "mruby/variable.h"
3+
#include "polarssl/entropy.h"
94

10-
#include <err.h>
5+
static void lib_entropy_free(mrb_state *mrb, void *ptr) {
6+
entropy_context *entropy = ptr;
117

12-
void mrb_mruby_polarssl_gem_init(mrb_state *mrb) {
13-
struct RClass *d;
14-
d = mrb_define_module(mrb, "PolarSSL");
8+
if (entropy != NULL) {
9+
mrb_free(mrb, entropy);
10+
}
11+
}
12+
13+
static struct mrb_data_type mrb_entropy_type = { "ENTROPY", lib_entropy_free };
14+
15+
static void entropycheck(mrb_state *mrb, mrb_value self, entropy_context **entropyp) {
16+
entropy_context *entropy;
17+
18+
entropy = (entropy_context *)DATA_PTR(self);
19+
if (!entropy) {
20+
mrb_raise(mrb, E_RUNTIME_ERROR, "no entropy found (BUG?)");
21+
}
22+
if (entropyp) *entropyp = entropy;
23+
}
24+
25+
static mrb_value mrb_entropy_gather(mrb_state *mrb, mrb_value self) {
26+
entropy_context *entropy;
27+
28+
entropycheck(mrb, self, &entropy);
29+
30+
if( entropy_gather( entropy ) == 0 ) {
31+
return mrb_true_value();
32+
} else {
33+
return mrb_false_value();
34+
}
1535
}
1636

17-
void mrb_mruby_polarssl_gem_final(mrb_state *mrb) {
37+
static mrb_value mrb_entropy_init(mrb_state *mrb, mrb_value self) {
38+
entropy_context *entropy;
39+
40+
entropy = (entropy_context *)DATA_PTR(self);
41+
if (entropy) {
42+
lib_entropy_free(mrb, entropy);
43+
}
44+
DATA_TYPE(self) = &mrb_entropy_type;
45+
DATA_PTR(self) = NULL;
46+
47+
entropy = (entropy_context *)mrb_malloc(mrb, sizeof(*entropy));
48+
DATA_PTR(self) = entropy;
49+
entropy_init(entropy);
50+
return self;
51+
}
52+
53+
void mrb_mruby_polarssl_gem_init(mrb_state *mrb) {
54+
struct RClass *p, *e;
1855

19-
}
56+
p = mrb_define_module(mrb, "PolarSSL");
57+
e = mrb_define_class_under(mrb, p, "Entropy", mrb->object_class);
58+
mrb_define_method(mrb, e, "initialize", mrb_entropy_init, MRB_ARGS_NONE());
59+
mrb_define_method(mrb, e, "gather", mrb_entropy_gather, MRB_ARGS_NONE());
60+
}
61+
62+
void mrb_mruby_polarssl_gem_final(mrb_state *mrb) {
63+
}

test/polarssl.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,17 @@
44
assert("PolarSSL") do
55
PolarSSL.class == Module
66
end
7+
8+
assert('PolarSSL::Entropy') do
9+
PolarSSL::Entropy.class == Class
10+
end
11+
12+
assert('PolarSSL::Entropy#new') do
13+
entropy = PolarSSL::Entropy.new
14+
end
15+
16+
assert('PolarSSL::Entropy#gather') do
17+
entropy = PolarSSL::Entropy.new
18+
entropy.gather() == true
19+
end
720
end

0 commit comments

Comments
 (0)