aboutsummaryrefslogtreecommitdiffstats
path: root/ext/openssl
diff options
context:
space:
mode:
authorKazuki Yamaguchi <k@rhe.jp>2016-12-08 15:23:39 +0900
committerKazuki Yamaguchi <k@rhe.jp>2016-12-23 13:14:09 +0900
commitfaaa3021385b5432dc960dfc8ca55d1b2fc89d3b (patch)
tree18695137e7351a5830b882f5626ac433a1b526d1 /ext/openssl
parent9b30cc419be9e1485ce535463a159c9ce21189d4 (diff)
downloadruby-openssl-faaa3021385b5432dc960dfc8ca55d1b2fc89d3b.tar.gz
bn: implement unary {plus,minus} operators for OpenSSL::BN
For consistency with Numeric. Not sure why they aren't currently; maybe they were simply forgotten.
Diffstat (limited to 'ext/openssl')
-rw-r--r--ext/openssl/ossl_bn.c34
1 files changed, 34 insertions, 0 deletions
diff --git a/ext/openssl/ossl_bn.c b/ext/openssl/ossl_bn.c
index 4e371cb2..1afebf44 100644
--- a/ext/openssl/ossl_bn.c
+++ b/ext/openssl/ossl_bn.c
@@ -856,6 +856,37 @@ ossl_bn_copy(VALUE self, VALUE other)
return self;
}
+/*
+ * call-seq:
+ * +bn -> aBN
+ */
+static VALUE
+ossl_bn_uplus(VALUE self)
+{
+ return self;
+}
+
+/*
+ * call-seq:
+ * -bn -> aBN
+ */
+static VALUE
+ossl_bn_uminus(VALUE self)
+{
+ VALUE obj;
+ BIGNUM *bn1, *bn2;
+
+ GetBN(self, bn1);
+ obj = NewBN(cBN);
+ bn2 = BN_dup(bn1);
+ if (!bn2)
+ ossl_raise(eBNError, "BN_dup");
+ SetBN(obj, bn2);
+ BN_set_negative(bn2, !BN_is_negative(bn2));
+
+ return obj;
+}
+
#define BIGNUM_CMP(func) \
static VALUE \
ossl_bn_##func(VALUE self, VALUE other) \
@@ -1068,6 +1099,9 @@ Init_ossl_bn(void)
rb_define_method(cBN, "num_bits", ossl_bn_num_bits, 0);
/* num_bits_word */
+ rb_define_method(cBN, "+@", ossl_bn_uplus, 0);
+ rb_define_method(cBN, "-@", ossl_bn_uminus, 0);
+
rb_define_method(cBN, "+", ossl_bn_add, 1);
rb_define_method(cBN, "-", ossl_bn_sub, 1);
rb_define_method(cBN, "*", ossl_bn_mul, 1);