1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
|
/*
# Copyright (C) 2016 g10 Code GmbH
# Copyright (C) 2004,2008 Igor Belyi <[email protected]>
# Copyright (C) 2002 John Goerzen <[email protected]>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
%module pygpgme
%include "cpointer.i"
%include "cstring.i"
/* Generate doc strings for all methods.
This will generate docstrings of the form
gpgme_op_encrypt(ctx, recp, flags, plain, cipher) -> gpgme_error_t
which we transform into
ctx.op_encrypt(recp, flags, plain, cipher) -> gpgme_error_t
for automagically wrapped functions. */
%feature("autodoc", "0");
/* Allow use of Unicode objects, bytes, and None for strings. */
%typemap(in) const char * {
if ($input == Py_None)
$1 = NULL;
else if (PyUnicode_Check($input))
$1 = PyUnicode_AsUTF8($input);
else if (PyBytes_Check($input))
$1 = PyBytes_AsString($input);
else {
PyErr_Format(PyExc_TypeError,
"arg %d: expected str, bytes, or None, got %s",
$argnum, $input->ob_type->tp_name);
return NULL;
}
}
%typemap(freearg) const char * "";
/* Likewise for a list of strings. */
%typemap(in) const char *[] (void *vector = NULL) {
/* Check if is a list */
if (PyList_Check($input)) {
size_t i, size = PyList_Size($input);
$1 = (char **) (vector = malloc((size+1) * sizeof(char *)));
for (i = 0; i < size; i++) {
PyObject *o = PyList_GetItem($input,i);
if (PyUnicode_Check(o))
$1[i] = PyUnicode_AsUTF8(o);
else if (PyString_Check(o))
$1[i] = PyString_AsString(o);
else {
PyErr_Format(PyExc_TypeError,
"arg %d: list must contain only str or bytes, got %s "
"at position %d",
$argnum, o->ob_type->tp_name, i);
free($1);
return NULL;
}
}
$1[i] = NULL;
} else {
PyErr_Format(PyExc_TypeError,
"arg %d: expected a list of str or bytes, got %s",
$argnum, $input->ob_type->tp_name);
return NULL;
}
}
%typemap(freearg) const char *[] {
free(vector$argnum);
}
// Release returned buffers as necessary.
%typemap(newfree) char * "free($1);";
%newobject gpgme_data_release_and_get_mem;
%typemap(arginit) gpgme_key_t [] {
$1 = NULL;
}
%typemap(in) gpgme_key_t [] {
int i, numb = 0;
if (!PySequence_Check($input)) {
PyErr_Format(PyExc_ValueError, "arg %d: Expected a list of gpgme_key_t",
$argnum);
return NULL;
}
if((numb = PySequence_Length($input)) != 0) {
$1 = (gpgme_key_t*)malloc((numb+1)*sizeof(gpgme_key_t));
for(i=0; i<numb; i++) {
PyObject *pypointer = PySequence_GetItem($input, i);
/* input = $input, 1 = $1, 1_descriptor = $1_descriptor */
/* &1_descriptor = $&1_descriptor *1_descriptor = $*1_descriptor */
// Following code is from swig's python.swg
if ((SWIG_ConvertPtr(pypointer,(void **) &$1[i], $*1_descriptor,SWIG_POINTER_EXCEPTION | $disown )) == -1) {
Py_DECREF(pypointer);
return NULL;
}
Py_DECREF(pypointer);
}
$1[numb] = NULL;
}
}
%typemap(freearg) gpgme_key_t [] {
if ($1) free($1);
}
// Special handling for references to our objects.
%typemap(in) gpgme_data_t DATAIN (gpgme_data_t wrapper = NULL,
PyObject *bytesio = NULL,
Py_buffer view, int have_view = 0) {
/* If we create a temporary wrapper object, we will store it in
wrapperN, where N is $argnum. Here in this fragment, SWIG will
automatically append $argnum. */
memset(&view, 0, sizeof view);
if ($input == Py_None)
$1 = NULL;
else {
PyObject *pypointer;
pypointer = object_to_gpgme_data_t($input, $argnum, &wrapper,
&bytesio, &view);
if (pypointer == NULL)
return NULL;
have_view = !! view.obj;
/* input = $input, 1 = $1, 1_descriptor = $1_descriptor */
// Following code is from swig's python.swg
if ((SWIG_ConvertPtr(pypointer,(void **) &$1, $1_descriptor,
SWIG_POINTER_EXCEPTION | $disown )) == -1) {
Py_DECREF(pypointer);
return NULL;
}
Py_DECREF(pypointer);
}
}
#if HAVE_DATA_H
/* If we are doing an in-tree build, we can use the internal
representation of struct gpgme_data for an very efficient check if
the buffer has been modified. */
%{
#include "src/data.h" /* For struct gpgme_data. */
%}
#endif
%typemap(freearg) gpgme_data_t DATAIN {
/* See whether we need to update the Python buffer. */
if (resultobj && wrapper$argnum && view$argnum.buf)
{
int dirty;
char *new_data = NULL;
size_t new_size;
#if HAVE_DATA_H
new_data = wrapper$argnum->data.mem.buffer;
new_size = wrapper$argnum->data.mem.length;
dirty = new_data != NULL;
#else
new_data = gpgme_data_release_and_get_mem (wrapper$argnum, &new_size);
wrapper$argnum = NULL;
dirty = new_size != view$argnum.len
|| memcmp (new_data, view$argnum.buf, view$argnum.len);
#endif
if (dirty)
{
/* The buffer is dirty. */
if (view$argnum.readonly)
{
Py_XDECREF(resultobj);
resultobj = NULL;
PyErr_SetString(PyExc_ValueError,
"cannot update read-only buffer");
}
/* See if we need to truncate the buffer. */
if (resultobj && view$argnum.len != new_size)
{
if (bytesio$argnum == NULL)
{
Py_XDECREF(resultobj);
resultobj = NULL;
PyErr_SetString(PyExc_ValueError, "cannot resize buffer");
}
else
{
PyObject *retval;
PyBuffer_Release(&view$argnum);
assert(view$argnum.obj == NULL);
retval = PyObject_CallMethod(bytesio$argnum, "truncate",
"l", (long) new_size);
if (retval == NULL)
{
Py_XDECREF(resultobj);
resultobj = NULL;
}
else
{
Py_DECREF(retval);
retval = PyObject_CallMethod(bytesio$argnum,
"getbuffer", NULL);
if (retval == NULL
|| PyObject_GetBuffer(retval, &view$argnum,
PyBUF_SIMPLE|PyBUF_WRITABLE) < 0)
{
Py_XDECREF(resultobj);
resultobj = NULL;
}
Py_XDECREF(retval);
if (resultobj && view$argnum.len
!= new_size)
{
Py_XDECREF(resultobj);
resultobj = NULL;
PyErr_Format(PyExc_ValueError,
"Expected buffer of length %zu, got %zi",
new_size,
view$argnum.len);
}
}
}
}
if (resultobj)
memcpy(view$argnum.buf, new_data, new_size);
}
#if ! HAVE_DATA_H
free (new_data);
#endif
}
/* Free the temporary wrapper, if any. */
if (wrapper$argnum)
gpgme_data_release(wrapper$argnum);
Py_XDECREF (bytesio$argnum);
if (have_view$argnum && view$argnum.buf)
PyBuffer_Release(&view$argnum);
}
%apply gpgme_data_t DATAIN {gpgme_data_t plain, gpgme_data_t cipher,
gpgme_data_t sig, gpgme_data_t signed_text,
gpgme_data_t plaintext, gpgme_data_t keydata,
gpgme_data_t pubkey, gpgme_data_t seckey,
gpgme_data_t out};
/* SWIG has problems interpreting ssize_t, off_t or gpgme_error_t in
gpgme.h. */
/* XXX: This is wrong at least for off_t if compiled with LFS. */
%typemap(out) ssize_t, off_t, gpgme_error_t, gpgme_err_code_t, gpgme_err_source_t, gpg_error_t {
$result = PyLong_FromLong($1);
}
/* XXX: This is wrong at least for off_t if compiled with LFS. */
%typemap(in) ssize_t, off_t, gpgme_error_t, gpgme_err_code_t, gpgme_err_source_t, gpg_error_t {
$1 = PyLong_AsLong($input);
}
// Those are for gpgme_data_read() and gpgme_strerror_r()
%typemap(in) (void *buffer, size_t size), (char *buf, size_t buflen) {
$2 = PyLong_AsLong($input);
if ($2 < 0) {
PyErr_SetString(PyExc_ValueError, "Positive integer expected");
return NULL;
}
$1 = ($1_ltype) malloc($2+1);
}
%typemap(argout) (void *buffer, size_t size), (char *buf, size_t buflen) {
Py_XDECREF($result); /* Blow away any previous result */
if (result < 0) { /* Check for I/O error */
free($1);
return PyErr_SetFromErrno(PyExc_RuntimeError);
}
$result = PyBytes_FromStringAndSize($1,result);
free($1);
}
/* For gpgme_data_write, but should be universal. */
%typemap(in) (const void *buffer, size_t size) {
Py_ssize_t ssize;
if ($input == Py_None)
$1 = NULL, $2 = 0;
else if (PyUnicode_Check($input))
$1 = PyUnicode_AsUTF8AndSize($input, &ssize);
else if (PyBytes_Check($input))
PyBytes_AsStringAndSize($input, (char **) &$1, &ssize);
else {
PyErr_Format(PyExc_TypeError,
"arg %d: expected str, bytes, or None, got %s",
$argnum, $input->ob_type->tp_name);
return NULL;
}
if (! $1)
$2 = 0;
else
{
assert (ssize >= 0);
$2 = (size_t) ssize;
}
}
%typemap(freearg) (const void *buffer, size_t size) "";
// Make types containing 'next' field to be lists
%ignore next;
%typemap(out) gpgme_sig_notation_t, gpgme_engine_info_t, gpgme_subkey_t,
gpgme_key_sig_t, gpgme_user_id_t, gpgme_invalid_key_t,
gpgme_recipient_t, gpgme_new_signature_t, gpgme_signature_t,
gpgme_import_status_t, gpgme_conf_arg_t, gpgme_conf_opt_t,
gpgme_conf_comp_t, gpgme_tofu_info_t {
int i;
int size = 0;
$1_ltype curr;
for (curr = $1; curr != NULL; curr = curr->next) {
size++;
}
$result = PyList_New(size);
for (i=0,curr=$1; i<size; i++,curr=curr->next) {
PyObject *o = SWIG_NewPointerObj(SWIG_as_voidptr(curr), $1_descriptor, %newpointer_flags);
PyList_SetItem($result, i, o);
}
}
/* Wrap the fragile result objects into robust Python ones. */
%typemap(out) gpgme_encrypt_result_t {
PyObject *fragile;
fragile = SWIG_NewPointerObj(SWIG_as_voidptr($1), $1_descriptor,
%newpointer_flags);
$result = pygpgme_wrap_fragile_result(fragile, "EncryptResult");
Py_DECREF(fragile);
}
%typemap(out) gpgme_decrypt_result_t {
PyObject *fragile;
fragile = SWIG_NewPointerObj(SWIG_as_voidptr($1), $1_descriptor,
%newpointer_flags);
$result = pygpgme_wrap_fragile_result(fragile, "DecryptResult");
Py_DECREF(fragile);
}
%typemap(out) gpgme_sign_result_t {
PyObject *fragile;
fragile = SWIG_NewPointerObj(SWIG_as_voidptr($1), $1_descriptor,
%newpointer_flags);
$result = pygpgme_wrap_fragile_result(fragile, "SignResult");
Py_DECREF(fragile);
}
%typemap(out) gpgme_verify_result_t {
PyObject *fragile;
fragile = SWIG_NewPointerObj(SWIG_as_voidptr($1), $1_descriptor,
%newpointer_flags);
$result = pygpgme_wrap_fragile_result(fragile, "VerifyResult");
Py_DECREF(fragile);
}
%typemap(out) gpgme_import_result_t {
PyObject *fragile;
fragile = SWIG_NewPointerObj(SWIG_as_voidptr($1), $1_descriptor,
%newpointer_flags);
$result = pygpgme_wrap_fragile_result(fragile, "ImportResult");
Py_DECREF(fragile);
}
%typemap(out) gpgme_genkey_result_t {
PyObject *fragile;
fragile = SWIG_NewPointerObj(SWIG_as_voidptr($1), $1_descriptor,
%newpointer_flags);
$result = pygpgme_wrap_fragile_result(fragile, "GenkeyResult");
Py_DECREF(fragile);
}
%typemap(out) gpgme_keylist_result_t {
PyObject *fragile;
fragile = SWIG_NewPointerObj(SWIG_as_voidptr($1), $1_descriptor,
%newpointer_flags);
$result = pygpgme_wrap_fragile_result(fragile, "KeylistResult");
Py_DECREF(fragile);
}
%typemap(out) gpgme_vfs_mount_result_t {
PyObject *fragile;
fragile = SWIG_NewPointerObj(SWIG_as_voidptr($1), $1_descriptor,
%newpointer_flags);
$result = pygpgme_wrap_fragile_result(fragile, "VFSMountResult");
Py_DECREF(fragile);
}
// Include mapper for edit callbacks
%typemap(in) (gpgme_edit_cb_t fnc, void *fnc_value) {
if (! PyTuple_Check($input))
return PyErr_Format(PyExc_TypeError, "edit callback must be a tuple");
if (PyTuple_Size($input) != 2 && PyTuple_Size($input) != 3)
return PyErr_Format(PyExc_TypeError,
"edit callback must be a tuple of size 2 or 3");
$1 = (gpgme_edit_cb_t) pyEditCb;
$2 = $input;
}
/* Include the unmodified <gpgme.h> for cc, and the cleaned-up local
version for SWIG. We do, however, want to hide certain fields on
some structs, which we provide prior to including the version for
SWIG. */
%{
#include <gpgme.h>
%}
/* This is for notations, where we want to hide the length fields, and
the unused bit field block. */
struct _gpgme_sig_notation
{
struct _gpgme_sig_notation *next;
/* If NAME is a null pointer, then VALUE contains a policy URL
rather than a notation. */
char *name;
/* The value of the notation data. */
char *value;
/* The accumulated flags. */
gpgme_sig_notation_flags_t flags;
/* Notation data is human-readable. */
unsigned int human_readable : 1;
/* Notation data is critical. */
unsigned int critical : 1;
};
/* Now include our local modified version. Any structs defined above
are ignored. */
%include "gpgme.h"
%include "errors.i"
// Generating and handling pointers-to-pointers.
%pointer_functions(gpgme_ctx_t, gpgme_ctx_t_p);
%pointer_functions(gpgme_data_t, gpgme_data_t_p);
%pointer_functions(gpgme_key_t, gpgme_key_t_p);
%pointer_functions(gpgme_error_t, gpgme_error_t_p);
%pointer_functions(gpgme_trust_item_t, gpgme_trust_item_t_p);
%pointer_functions(gpgme_engine_info_t, gpgme_engine_info_t_p);
// Helper functions.
%{
#include <stdio.h>
%}
FILE *fdopen(int fildes, const char *mode);
%{
#include "helpers.h"
/* SWIG support for helpers.c */
PyObject *
pygpgme_wrap_gpgme_data_t(gpgme_data_t data)
{
return SWIG_Python_NewPointerObj(NULL, data, SWIGTYPE_p_gpgme_data, 0);
}
gpgme_ctx_t
pygpgme_unwrap_gpgme_ctx_t(PyObject *wrapped)
{
gpgme_ctx_t result;
if (SWIG_ConvertPtr(wrapped,
(void **) &result,
SWIGTYPE_p_gpgme_context,
SWIG_POINTER_EXCEPTION) == -1)
return NULL;
return result;
}
%}
%include "helpers.h"
|