lib/extensions/openssl/ext/ossl_x509attr.c in rhodes-5.5.18 vs lib/extensions/openssl/ext/ossl_x509attr.c in rhodes-6.0.11
- old
+ new
@@ -1,73 +1,86 @@
/*
- * $Id: ossl_x509attr.c 27437 2010-04-22 08:04:13Z nobu $
* 'OpenSSL for Ruby' project
* Copyright (C) 2001 Michal Rokos <m.rokos@sh.cvut.cz>
* All rights reserved.
*/
/*
- * This program is licenced under the same licence as Ruby.
+ * This program is licensed under the same licence as Ruby.
* (See the file 'LICENCE'.)
*/
#include "ossl.h"
-#define WrapX509Attr(klass, obj, attr) do { \
- if (!attr) { \
+#define NewX509Attr(klass) \
+ TypedData_Wrap_Struct((klass), &ossl_x509attr_type, 0)
+#define SetX509Attr(obj, attr) do { \
+ if (!(attr)) { \
ossl_raise(rb_eRuntimeError, "ATTR wasn't initialized!"); \
} \
- obj = Data_Wrap_Struct(klass, 0, X509_ATTRIBUTE_free, attr); \
+ RTYPEDDATA_DATA(obj) = (attr); \
} while (0)
#define GetX509Attr(obj, attr) do { \
- Data_Get_Struct(obj, X509_ATTRIBUTE, attr); \
- if (!attr) { \
+ TypedData_Get_Struct((obj), X509_ATTRIBUTE, &ossl_x509attr_type, (attr)); \
+ if (!(attr)) { \
ossl_raise(rb_eRuntimeError, "ATTR wasn't initialized!"); \
} \
} while (0)
#define SafeGetX509Attr(obj, attr) do { \
- OSSL_Check_Kind(obj, cX509Attr); \
- GetX509Attr(obj, attr); \
+ OSSL_Check_Kind((obj), cX509Attr); \
+ GetX509Attr((obj), (attr)); \
} while (0)
/*
* Classes
*/
VALUE cX509Attr;
VALUE eX509AttrError;
+static void
+ossl_x509attr_free(void *ptr)
+{
+ X509_ATTRIBUTE_free(ptr);
+}
+
+static const rb_data_type_t ossl_x509attr_type = {
+ "OpenSSL/X509/ATTRIBUTE",
+ {
+ 0, ossl_x509attr_free,
+ },
+ 0, 0, RUBY_TYPED_FREE_IMMEDIATELY,
+};
+
/*
* Public
*/
VALUE
ossl_x509attr_new(X509_ATTRIBUTE *attr)
{
X509_ATTRIBUTE *new;
VALUE obj;
+ obj = NewX509Attr(cX509Attr);
if (!attr) {
new = X509_ATTRIBUTE_new();
} else {
new = X509_ATTRIBUTE_dup(attr);
}
if (!new) {
ossl_raise(eX509AttrError, NULL);
}
- WrapX509Attr(cX509Attr, obj, new);
+ SetX509Attr(obj, new);
return obj;
}
X509_ATTRIBUTE *
-DupX509AttrPtr(VALUE obj)
+GetX509AttrPtr(VALUE obj)
{
- X509_ATTRIBUTE *attr, *new;
+ X509_ATTRIBUTE *attr;
SafeGetX509Attr(obj, attr);
- if (!(new = X509_ATTRIBUTE_dup(attr))) {
- ossl_raise(eX509AttrError, NULL);
- }
- return new;
+ return attr;
}
/*
* Private
*/
@@ -75,13 +88,14 @@
ossl_x509attr_alloc(VALUE klass)
{
X509_ATTRIBUTE *attr;
VALUE obj;
+ obj = NewX509Attr(klass);
if (!(attr = X509_ATTRIBUTE_new()))
ossl_raise(eX509AttrError, NULL);
- WrapX509Attr(klass, obj, attr);
+ SetX509Attr(obj, attr);
return obj;
}
/*
@@ -122,16 +136,19 @@
{
X509_ATTRIBUTE *attr;
ASN1_OBJECT *obj;
char *s;
- s = StringValuePtr(oid);
+ GetX509Attr(self, attr);
+ s = StringValueCStr(oid);
obj = OBJ_txt2obj(s, 0);
- if(!obj) obj = OBJ_txt2obj(s, 1);
if(!obj) ossl_raise(eX509AttrError, NULL);
- GetX509Attr(self, attr);
- X509_ATTRIBUTE_set1_object(attr, obj);
+ if (!X509_ATTRIBUTE_set1_object(attr, obj)) {
+ ASN1_OBJECT_free(obj);
+ ossl_raise(eX509AttrError, "X509_ATTRIBUTE_set1_object");
+ }
+ ASN1_OBJECT_free(obj);
return oid;
}
/*
@@ -163,12 +180,12 @@
#if defined(HAVE_ST_X509_ATTRIBUTE_SINGLE) || defined(HAVE_ST_SINGLE)
# define OSSL_X509ATTR_IS_SINGLE(attr) ((attr)->single)
# define OSSL_X509ATTR_SET_SINGLE(attr) ((attr)->single = 1)
#else
-# define OSSL_X509ATTR_IS_SINGLE(attr) (!(attr)->set)
-# define OSSL_X509ATTR_SET_SINGLE(attr) ((attr)->set = 0)
+# define OSSL_X509ATTR_IS_SINGLE(attr) (!(attr)->value.set)
+# define OSSL_X509ATTR_SET_SINGLE(attr) ((attr)->value.set = 0)
#endif
/*
* call-seq:
* attr.value = asn1 => asn1
@@ -258,10 +275,10 @@
/*
* X509_ATTRIBUTE init
*/
void
-Init_ossl_x509attr()
+Init_ossl_x509attr(void)
{
eX509AttrError = rb_define_class_under(mX509, "AttributeError", eOSSLError);
cX509Attr = rb_define_class_under(mX509, "Attribute", rb_cObject);
rb_define_alloc_func(cX509Attr, ossl_x509attr_alloc);