@@ -60,6 +60,9 @@ extern "C"
60
60
SWIGEXPORT int SWIGSTDCALL ByteVector_size (void *ptr) {
61
61
return static_cast <std::vector<unsigned char >*>(ptr)->size ();
62
62
}
63
+ SWIGEXPORT void SWIGSTDCALL ByteVector_free (void *ptr) {
64
+ delete static_cast <std::vector<unsigned char >*>(ptr);
65
+ }
63
66
SWIGEXPORT void * SWIGSTDCALL ByteVector_to (unsigned char *data, int size) {
64
67
return new std::vector<unsigned char >(data, data + size);
65
68
}
@@ -72,112 +75,93 @@ extern "C"
72
75
public static extern global::System.IntPtr ByteVector_data (global::System.IntPtr data);
73
76
[global::System.Runtime .InteropServices .DllImport (" $dllimport" , EntryPoint=" ByteVector_size" )]
74
77
public static extern int ByteVector_size (global::System.IntPtr data);
78
+ [global::System.Runtime .InteropServices .DllImport (" $dllimport" , EntryPoint=" ByteVector_free" )]
79
+ public static extern void ByteVector_free (global::System.IntPtr data);
75
80
[global::System.Runtime .InteropServices .DllImport (" $dllimport" , EntryPoint=" ByteVector_to" )]
76
81
public static extern global::System.IntPtr ByteVector_to (
77
- [global::System.Runtime .InteropServices .MarshalAs (global::System.Runtime .InteropServices .UnmanagedType .LPArray )]byte[] data, int size);
78
-
79
- public class UTF8Marshaler : global::System.Runtime.InteropServices.ICustomMarshaler {
80
- static UTF8Marshaler static_instance = new UTF8Marshaler();
81
-
82
- public global::System.IntPtr MarshalManagedToNative (object managedObj) {
83
- if (managedObj == null)
84
- return global::System.IntPtr .Zero ;
85
- if (!(managedObj is string))
86
- throw new global::System.Runtime .InteropServices .MarshalDirectiveException (
87
- " UTF8Marshaler must be used on a string." );
88
-
89
- // not null terminated
90
- byte[] strbuf = global::System.Text .Encoding .UTF8 .GetBytes ((string)managedObj);
91
- global::System.IntPtr buffer = global::System.Runtime .InteropServices .Marshal .AllocHGlobal (strbuf.Length + 1 );
92
- global::System.Runtime .InteropServices .Marshal .Copy (strbuf, 0 , buffer, strbuf.Length );
93
-
94
- // write the terminating null
95
- global::System.Runtime .InteropServices .Marshal .WriteByte (buffer + strbuf.Length , 0 );
96
- return buffer;
97
- }
98
-
99
- public unsafe object MarshalNativeToManaged (global::System.IntPtr pNativeData) {
100
- byte* walk = (byte*)pNativeData;
101
-
102
- // find the end of the string
103
- while (*walk != 0 ) {
104
- walk++;
105
- }
106
- int length = (int )(walk - (byte*)pNativeData);
107
-
108
- // should not be null terminated
109
- byte[] strbuf = new byte[length];
110
- // skip the trailing null
111
- global::System.Runtime .InteropServices .Marshal .Copy ((global::System.IntPtr )pNativeData, strbuf, 0 , length);
112
- return global::System.Text .Encoding .UTF8 .GetString (strbuf);
113
- }
114
-
115
- public void CleanUpNativeData (global::System.IntPtr pNativeData) {
116
- global::System.Runtime .InteropServices .Marshal .FreeHGlobal (pNativeData);
117
- }
118
-
119
- public void CleanUpManagedData (object managedObj) {
120
- }
121
-
122
- public int GetNativeDataSize () {
123
- return -1 ;
124
- }
125
-
126
- public static global::System.Runtime.InteropServices.ICustomMarshaler GetInstance (string cookie) {
127
- return static_instance;
128
- }
129
- }
82
+ [global::System.Runtime .InteropServices .MarshalAs (global::System.Runtime .InteropServices .UnmanagedType .LPArray )]byte[] data, int size);
130
83
%}
131
84
132
85
#ifdef SWIGJAVA
133
- %typemap (in) std::vector<unsigned char > %{
134
- jbyte *$input_ptr = jenv->GetByteArrayElements ($input, NULL );
135
- jsize $input_size = jenv->GetArrayLength ($input);
136
- std::vector<unsigned char > $1_data($input_ptr, $input_ptr+$input_size);
137
- $1 = &$1_data;
138
- jenv->ReleaseByteArrayElements ($input, $input_ptr, JNI_ABORT);
139
- %}
140
- %typemap (out) std::vector<unsigned char > %{
141
- jresult = jenv->NewByteArray ((&result)->size ());
142
- jenv->SetByteArrayRegion (jresult, 0 , (&result)->size (), (const jbyte*)(&result)->data ());
143
- %}
144
- %typemap (jtype) std::vector<unsigned char > " byte[]"
86
+ %fragment(" SWIG_VectorUnsignedCharToJavaArray" , " header" ) {
87
+ static jbyteArray SWIG_VectorUnsignedCharToJavaArray (JNIEnv *jenv, const std::vector<unsigned char > &data) {
88
+ jbyteArray jresult = JCALL1 (NewByteArray, jenv, data.size ());
89
+ if (!jresult)
90
+ return nullptr ;
91
+ JCALL4 (SetByteArrayRegion, jenv, jresult, 0 , data.size (), (const jbyte*)data.data ());
92
+ return jresult;
93
+ }}
94
+ %fragment(" SWIG_JavaArrayToVectorUnsignedChar" , " header" ) {
95
+ static std::vector<unsigned char >* SWIG_JavaArrayToVectorUnsignedChar (JNIEnv *jenv, jbyteArray data) {
96
+ std::vector<unsigned char > *result = new std::vector<unsigned char >(JCALL1 (GetArrayLength, jenv, data));
97
+ JCALL4 (GetByteArrayRegion, jenv, data, 0 , result->size (), (jbyte*)result->data ());
98
+ return result;
99
+ }}
100
+ %typemap(in, fragment=" SWIG_JavaArrayToVectorUnsignedChar" ) std::vector<unsigned char >
101
+ %{ $1 = SWIG_JavaArrayToVectorUnsignedChar (jenv, $input); %}
102
+ %typemap(out, fragment=" SWIG_VectorUnsignedCharToJavaArray" ) std::vector<unsigned char >, digidoc::X509Cert
103
+ %{ $result = SWIG_VectorUnsignedCharToJavaArray (jenv, $1 ); %}
104
+ %typemap(jtype) std::vector<unsigned char >, digidoc::X509Cert " byte[]"
145
105
%typemap(jstype) std::vector<unsigned char > " byte[]"
146
- %typemap (jni) std::vector<unsigned char > " jbyteArray"
147
- %typemap (javain) std::vector<unsigned char > " $javainput"
106
+ %typemap(jstype) digidoc::X509Cert " java.security.cert.X509Certificate"
107
+ %typemap(jni) std::vector<unsigned char >, digidoc::X509Cert " jbyteArray"
108
+ %typemap(javain) std::vector<unsigned char >, digidoc::X509Cert " $javainput"
148
109
%typemap(javaout) std::vector<unsigned char > {
149
110
return $jnicall;
150
111
}
112
+ %typemap(javaout, throws=" java.security.cert.CertificateException, java.io.IOException" ) digidoc::X509Cert {
113
+ byte[] der = $jnicall;
114
+ java.security .cert .CertificateFactory cf = java.security .cert .CertificateFactory .getInstance (" X509" );
115
+ try (java.io .ByteArrayInputStream is = new java.io .ByteArrayInputStream (der)) {
116
+ return (java.security .cert .X509Certificate ) cf.generateCertificate (is);
117
+ }
118
+ }
119
+
151
120
#elif defined(SWIGCSHARP)
152
121
%typemap(cstype) std::vector<unsigned char > " byte[]"
153
- %typemap (csin,
154
- pre= " global::System.IntPtr cPtr$csinput = digidocPINVOKE.ByteVector_to($csinput, $csinput.Length);
155
- global::System.Runtime.InteropServices.HandleRef handleRef$csinput = new global::System.Runtime.InteropServices.HandleRef(this, cPtr$csinput);"
122
+ %typemap(cstype) digidoc::X509Cert " System.Security.Cryptography.X509Certificates.X509Certificate2 "
123
+ %typemap(csin, pre= " global::System.IntPtr cPtr$csinput = digidocPINVOKE.ByteVector_to($csinput, $csinput.Length);
124
+ var handleRef$csinput = new global::System.Runtime.InteropServices.HandleRef(this, cPtr$csinput);"
156
125
) std::vector<unsigned char > " handleRef$csinput"
157
126
%typemap(csout, excode=SWIGEXCODE) std::vector<unsigned char > {
158
- global::System.IntPtr data = $imcall;$excode
159
- byte[] result = new byte[$modulePINVOKE.ByteVector_size (data)];
160
- global::System.Runtime .InteropServices .Marshal .Copy ($modulePINVOKE.ByteVector_data (data), result, 0 , result.Length );
161
- return result;
162
- }
163
- #elif defined(SWIGPYTHON)
164
- %typemap (in) std::vector<unsigned char > %{
165
- if (PyBytes_Check ($input)) {
166
- const char *data = PyBytes_AsString ($input);
167
- $1 = new std::vector<unsigned char >(data, data + PyBytes_Size ($input));
168
- } else if (PyString_Check ($input)) {
169
- const char *data = PyString_AsString ($input);
170
- $1 = new std::vector<unsigned char >(data, data + PyString_Size ($input));
171
- } else {
172
- PyErr_SetString (PyExc_TypeError, " not a bytes" );
173
- SWIG_fail;
127
+ global::System.IntPtr cPtr = $imcall;$excode
128
+ byte[] result = new byte[$modulePINVOKE.ByteVector_size (cPtr)];
129
+ global::System.Runtime .InteropServices .Marshal .Copy ($modulePINVOKE.ByteVector_data (cPtr), result, 0 , result.Length );
130
+ $modulePINVOKE.ByteVector_free (cPtr);
131
+ return result;
132
+ }
133
+ %typemap(csout, excode=SWIGEXCODE) digidoc::X509Cert {
134
+ global::System.IntPtr cPtr = $imcall;$excode
135
+ byte[] der = new byte[$modulePINVOKE.ByteVector_size (cPtr)];
136
+ global::System.Runtime .InteropServices .Marshal .Copy ($modulePINVOKE.ByteVector_data (cPtr), der, 0 , der.Length );
137
+ $modulePINVOKE.ByteVector_free (cPtr);
138
+ return new System.Security .Cryptography .X509Certificates .X509Certificate2 (der);
174
139
}
175
- %}
140
+ %typemap(out) std::vector<unsigned char > %{ $result = new std::vector<unsigned char >(std::move ($1 )); %}
141
+ %typemap(out) digidoc::X509Cert %{ $result = new std::vector<unsigned char >($1 ); %}
142
+
143
+ #elif defined(SWIGPYTHON)
144
+ %typemap(in) std::vector<unsigned char > %{
145
+ if (PyBytes_Check ($input)) {
146
+ const char *data = PyBytes_AsString ($input);
147
+ $1 = new std::vector<unsigned char >(data, data + PyBytes_Size ($input));
148
+ } else if (PyString_Check ($input)) {
149
+ const char *data = PyString_AsString ($input);
150
+ $1 = new std::vector<unsigned char >(data, data + PyString_Size ($input));
151
+ } else {
152
+ PyErr_SetString (PyExc_TypeError, " not a bytes" );
153
+ SWIG_fail;
154
+ }
155
+ %}
176
156
%typemap(out) std::vector<unsigned char >
177
157
%{ $result = PyBytes_FromStringAndSize ((const char *)(&result)->data (), (&result)->size ()); %}
158
+ %typemap(out) digidoc::X509Cert {
159
+ std::vector<unsigned char > temp = $1 ;
160
+ $result = PyBytes_FromStringAndSize ((const char *)temp.data (), temp.size ());
161
+ }
162
+ #endif
178
163
%typemap(freearg) std::vector<unsigned char >
179
164
%{ delete $1 ; %}
180
- #endif
181
165
%apply std::vector<unsigned char > { std::vector<unsigned char > const & };
182
166
183
167
%exception %{
@@ -208,11 +192,6 @@ extern "C"
208
192
%ignore digidoc::ConfV2::verifyServiceCert;
209
193
%ignore digidoc::ConfV4::verifyServiceCerts;
210
194
%ignore digidoc::ConfV5::TSCerts;
211
- %ignore digidoc::Signer::cert;
212
- %ignore digidoc::Signature::signingCertificate;
213
- %ignore digidoc::Signature::OCSPCertificate;
214
- %ignore digidoc::Signature::TimeStampCertificate;
215
- %ignore digidoc::Signature::ArchiveTimeStampCertificate;
216
195
// hide stream methods, swig cannot generate usable wrappers
217
196
%ignore digidoc::DataFile::saveAs (std::ostream &os) const ;
218
197
%ignore digidoc::Container::addAdESSignature (std::istream &signature);
@@ -260,15 +239,10 @@ def transfer(self):
260
239
%include " std_vector.i"
261
240
%include " std_map.i"
262
241
#ifdef SWIGCSHARP
263
- namespace std {
264
- %typemap(imtype,
265
- inattributes=" [global::System.Runtime.InteropServices.MarshalAs(global::System.Runtime.InteropServices.UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(UTF8Marshaler))]" ,
266
- outattributes=" [return: global::System.Runtime.InteropServices.MarshalAs(global::System.Runtime.InteropServices.UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(UTF8Marshaler))]" )
267
- string " string"
268
- %typemap(imtype,
269
- inattributes=" [global::System.Runtime.InteropServices.MarshalAs(global::System.Runtime.InteropServices.UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(UTF8Marshaler))]" ,
270
- outattributes=" [return: global::System.Runtime.InteropServices.MarshalAs(global::System.Runtime.InteropServices.UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(UTF8Marshaler))]" ) const string & " string"
271
- }
242
+ %typemap(imtype,
243
+ inattributes=" [global::System.Runtime.InteropServices.MarshalAs(global::System.Runtime.InteropServices.UnmanagedType.LPUTF8Str)]" ,
244
+ outattributes=" [return: global::System.Runtime.InteropServices.MarshalAs(global::System.Runtime.InteropServices.UnmanagedType.LPUTF8Str)]" )
245
+ std::string, const std::string & " string"
272
246
#endif
273
247
274
248
// Handle DigiDoc Export declarations
@@ -294,31 +268,6 @@ namespace std {
294
268
%template (DataFiles) std::vector<digidoc::DataFile*>;
295
269
%template (Signatures) std::vector<digidoc::Signature*>;
296
270
297
- // override X509Cert methods to return byte array
298
- %extend digidoc::Signer {
299
- std::vector<unsigned char > cert () const
300
- {
301
- return $self->cert ();
302
- }
303
- }
304
- %extend digidoc::Signature {
305
- std::vector<unsigned char > signingCertificateDer () const
306
- {
307
- return $self->signingCertificate ();
308
- }
309
- std::vector<unsigned char > OCSPCertificateDer () const
310
- {
311
- return $self->OCSPCertificate ();
312
- }
313
- std::vector<unsigned char > TimeStampCertificateDer () const
314
- {
315
- return $self->TimeStampCertificate ();
316
- }
317
- std::vector<unsigned char > ArchiveTimeStampCertificateDer () const
318
- {
319
- return $self->ArchiveTimeStampCertificate ();
320
- }
321
- }
322
271
%extend digidoc::Container {
323
272
static digidoc::Container* open (const std::string &path, digidoc::ContainerOpenCB *cb)
324
273
{
0 commit comments