/*
 * Generated by asn1c-0.9.29 (http://lionet.info/asn1c)
 * From ASN.1 module "CryptographicMessageSyntax2004"
 * 	found in "rfc5652-12.1.asn1"
 * 	`asn1c -Werror -fcompound-names -fwide-types -D asn1/asn1c -no-gen-PER -no-gen-example`
 */

#include "asn1/asn1c/CMSAttribute.h"

#include <openssl/obj_mac.h>

#include "asn1/asn1c/ContentType.h"
#include "asn1/asn1c/MessageDigest.h"
#include "asn1/asn1c/SigningTime.h"
#include "json_util.h"

static json_t *
CMSAttribute_encode_json(const asn_TYPE_descriptor_t *td, const void *sptr)
{
	struct CMSAttribute const *cattr = sptr;
	json_t *root;
	json_t *array;
	json_t *tmp;
	int a;

	if (!cattr)
		return json_null();

	root = json_obj_new();
	if (root == NULL)
		return NULL;

	tmp = OBJECT_IDENTIFIER_encode_json(NULL, &cattr->attrType);
	if (json_object_add(root, "attrType", tmp))
		goto fail;
	if (json_object_add(root, "attrValues", array = json_array_new()))
		goto fail;

	switch (OBJECT_IDENTIFIER_to_nid(&cattr->attrType)) {
	case NID_pkcs9_contentType:
		td = &asn_DEF_ContentType;
		break;
	case NID_pkcs9_messageDigest:
		td = &asn_DEF_MessageDigest;
		break;
	case NID_pkcs9_signingTime:
		td = &asn_DEF_SigningTime;
		break;
	default:
		td = &asn_DEF_ANY;
		break;
	}

	for (a = 0; a < cattr->attrValues.list.count; a++) {
		tmp = ANY_to_json(td, cattr->attrValues.list.array[a]);
		if (json_array_add(array, tmp))
			goto fail;
	}

	return root;

fail:	json_decref(root);
	return NULL;
}

static asn_TYPE_operation_t asn_OP_CMSAttribute = {
	SEQUENCE_free,
	SEQUENCE_print,
	SEQUENCE_compare,
	SEQUENCE_decode_ber,
	SEQUENCE_encode_der,
	CMSAttribute_encode_json,
	SEQUENCE_encode_xer,
	NULL	/* Use generic outmost tag fetcher */
};

static asn_TYPE_member_t asn_MBR_attrValues_3[] = {
	{ ATF_ANY_TYPE | ATF_POINTER, 0, 0,
		-1 /* Ambiguous tag (ANY?) */,
		0,
		&asn_DEF_CMSAttributeValue,
		NULL,
		{ NULL, NULL, NULL },
		NULL, NULL, /* No default value */
		""
		},
};
static const ber_tlv_tag_t asn_DEF_attrValues_tags_3[] = {
	(ASN_TAG_CLASS_UNIVERSAL | (17 << 2))
};
static asn_SET_OF_specifics_t asn_SPC_attrValues_specs_3 = {
	sizeof(struct CMSAttribute__attrValues),
	offsetof(struct CMSAttribute__attrValues, _asn_ctx),
	0,	/* XER encoding is XMLDelimitedItemList */
};
static /* Use -fall-defs-global to expose */
asn_TYPE_descriptor_t asn_DEF_attrValues_3 = {
	"attrValues",
	"attrValues",
	&asn_OP_SET_OF,
	asn_DEF_attrValues_tags_3,
	sizeof(asn_DEF_attrValues_tags_3)
		/sizeof(asn_DEF_attrValues_tags_3[0]), /* 1 */
	asn_DEF_attrValues_tags_3,	/* Same as above */
	sizeof(asn_DEF_attrValues_tags_3)
		/sizeof(asn_DEF_attrValues_tags_3[0]), /* 1 */
	{ NULL, NULL, SET_OF_constraint },
	asn_MBR_attrValues_3,
	1,	/* Single element */
	&asn_SPC_attrValues_specs_3	/* Additional specs */
};

asn_TYPE_member_t asn_MBR_CMSAttribute_1[] = {
	{ ATF_NOFLAGS, 0, offsetof(struct CMSAttribute, attrType),
		(ASN_TAG_CLASS_UNIVERSAL | (6 << 2)),
		0,
		&asn_DEF_OBJECT_IDENTIFIER,
		NULL,
		{ NULL, NULL, NULL },
		NULL, NULL, /* No default value */
		"attrType"
		},
	{ ATF_NOFLAGS, 0, offsetof(struct CMSAttribute, attrValues),
		(ASN_TAG_CLASS_UNIVERSAL | (17 << 2)),
		0,
		&asn_DEF_attrValues_3,
		NULL,
		{ NULL, NULL, NULL },
		NULL, NULL, /* No default value */
		"attrValues"
		},
};
static const ber_tlv_tag_t asn_DEF_CMSAttribute_tags_1[] = {
	(ASN_TAG_CLASS_UNIVERSAL | (16 << 2))
};
static const asn_TYPE_tag2member_t asn_MAP_CMSAttribute_tag2el_1[] = {
    { (ASN_TAG_CLASS_UNIVERSAL | (6 << 2)), 0, 0, 0 }, /* attrType */
    { (ASN_TAG_CLASS_UNIVERSAL | (17 << 2)), 1, 0, 0 } /* attrValues */
};
asn_SEQUENCE_specifics_t asn_SPC_CMSAttribute_specs_1 = {
	sizeof(struct CMSAttribute),
	offsetof(struct CMSAttribute, _asn_ctx),
	asn_MAP_CMSAttribute_tag2el_1,
	2,	/* Count of tags in the map */
	-1,	/* First extension addition */
};
asn_TYPE_descriptor_t asn_DEF_CMSAttribute = {
	"CMSAttribute",
	"CMSAttribute",
	&asn_OP_CMSAttribute,
	asn_DEF_CMSAttribute_tags_1,
	sizeof(asn_DEF_CMSAttribute_tags_1)
		/sizeof(asn_DEF_CMSAttribute_tags_1[0]), /* 1 */
	asn_DEF_CMSAttribute_tags_1,	/* Same as above */
	sizeof(asn_DEF_CMSAttribute_tags_1)
		/sizeof(asn_DEF_CMSAttribute_tags_1[0]), /* 1 */
	{ NULL, NULL, SEQUENCE_constraint },
	asn_MBR_CMSAttribute_1,
	2,	/* Elements count */
	&asn_SPC_CMSAttribute_specs_1	/* Additional specs */
};
