From 12ff0ec89ad42a37935b76f1eb3c6acb0d6b0cdb Mon Sep 17 00:00:00 2001 From: heck Date: Tue, 25 Jan 2022 17:07:55 +0100 Subject: [PATCH] JNI-167: add Message.encodeASN1XER() --- src/codegen/gen_cpp_Message.ysl2 | 56 +++++++++++++++++++++++++++++++ src/codegen/gen_java_Message.ysl2 | 6 ++++ 2 files changed, 62 insertions(+) diff --git a/src/codegen/gen_cpp_Message.ysl2 b/src/codegen/gen_cpp_Message.ysl2 index a951771..9939c13 100644 --- a/src/codegen/gen_cpp_Message.ysl2 +++ b/src/codegen/gen_cpp_Message.ysl2 @@ -17,6 +17,8 @@ tstylesheet { || #include #include + #include + #include #include #include "jniutils.hh" #include "throw_pEp_exception.hh" @@ -113,6 +115,60 @@ tstylesheet { return result; } + + JNIEXPORT jbyteArray JNICALL Java_foundation_pEp_jniadapter_Message__1encodeASN1XER(JNIEnv *env, jobject obj) + { + pEpLog("called"); + message* _obj = nullptr; + try { + _obj = message_ptr(env, obj); + } catch(...) { + jclass ex = env->FindClass("java/lang/IllegalStateException"); + assert(ex); + env->ThrowNew(ex, nullptr); + return static_cast(NULL); + } + + // create ASN1Message + ASN1Message_t *pm = ::ASN1Message_from_message(_obj, NULL, true, 0); + if (pm == nullptr) { + jclass ex = env->FindClass("java/lang/IllegalStateException"); + assert(ex); + env->ThrowNew(ex, nullptr); + return static_cast(NULL); + } + + // encode PER + char *data_per = nullptr; + size_t data_size_per = 0; + PEP_STATUS status = ::encode_ASN1Message_message(pm, &data_per, &data_size_per); + if (status) { + throw_pEp_Exception(env, status); + } + if (data_per == nullptr) { + jclass ex = env->FindClass("java/lang/IllegalStateException"); + assert(ex); + env->ThrowNew(ex, nullptr); + return static_cast(NULL); + } + + // encode PER to XER + char *data_xer = nullptr; + status = ::PER_to_XER_ASN1Message_msg(data_per, data_size_per, &data_xer); + if (status) { + throw_pEp_Exception(env, status); + } + if (data_xer == nullptr) { + jclass ex = env->FindClass("java/lang/IllegalStateException"); + assert(ex); + env->ThrowNew(ex, nullptr); + return static_cast(NULL); + } + + jbyteArray result = from_string(env, data_xer); + return result; + } + || apply "*[name(.)!='enum']", 0, mode=entry { with "name", "@name"; diff --git a/src/codegen/gen_java_Message.ysl2 b/src/codegen/gen_java_Message.ysl2 index 8b13da8..a2b1e34 100644 --- a/src/codegen/gen_java_Message.ysl2 +++ b/src/codegen/gen_java_Message.ysl2 @@ -64,6 +64,12 @@ tstylesheet { return Utils.toUTF16(_encodeMIME()); } + private native byte[] _encodeASN1XER() throws pEpException; + + public String encodeASN1XER() { + return Utils.toUTF16(_encodeASN1XER()); + } + private «$cname»(long h) { handle = h; instanceCount.getAndIncrement();