Blame lang/python/gpgme.i

Packit Service 672cf4
/*
Packit Service 672cf4
# Copyright (C) 2016 g10 Code GmbH
Packit Service 672cf4
# Copyright (C) 2004,2008 Igor Belyi <belyi@users.sourceforge.net>
Packit Service 672cf4
# Copyright (C) 2002 John Goerzen <jgoerzen@complete.org>
Packit Service 672cf4
#
Packit Service 672cf4
#    This library is free software; you can redistribute it and/or
Packit Service 672cf4
#    modify it under the terms of the GNU Lesser General Public
Packit Service 672cf4
#    License as published by the Free Software Foundation; either
Packit Service 672cf4
#    version 2.1 of the License, or (at your option) any later version.
Packit Service 672cf4
#
Packit Service 672cf4
#    This library is distributed in the hope that it will be useful,
Packit Service 672cf4
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 672cf4
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 672cf4
#    Lesser General Public License for more details.
Packit Service 672cf4
#
Packit Service 672cf4
#    You should have received a copy of the GNU Lesser General Public
Packit Service 672cf4
#    License along with this library; if not, write to the Free Software
Packit Service 672cf4
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
Packit Service 672cf4
*/
Packit Service 672cf4
%module gpgme
Packit Service 672cf4
%include "cpointer.i"
Packit Service 672cf4
%include "cstring.i"
Packit Service 672cf4
Packit Service 0ef63b
/* no need to record whether GPGME's c++ bindings were built
Packit Service 0ef63b
   concurrently with the python bindings */
Packit Service 0ef63b
%ignore HAVE_CXX11;
Packit Service 0ef63b
Packit Service 0ef63b
%{
Packit Service 0ef63b
/* We use public symbols (eg. "_obsolete_class") which are marked as
Packit Service 0ef63b
 * deprecated but we need to keep them.  Silence the warning.  */
Packit Service 0ef63b
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
Packit Service 0ef63b
%}
Packit Service 0ef63b
Packit Service 672cf4
/* Generate doc strings for all methods.
Packit Service 672cf4
Packit Service 672cf4
   This will generate docstrings of the form
Packit Service 672cf4
Packit Service 672cf4
     gpgme_op_encrypt(ctx, recp, flags, plain, cipher) -> gpgme_error_t
Packit Service 672cf4
Packit Service 672cf4
   which we transform into
Packit Service 672cf4
Packit Service 672cf4
     ctx.op_encrypt(recp, flags, plain, cipher) -> gpgme_error_t
Packit Service 672cf4
Packit Service 672cf4
   for automagically wrapped functions.  */
Packit Service 672cf4
%feature("autodoc", "0");
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
/* Allow use of Unicode objects, bytes, and None for strings.  */
Packit Service 672cf4
%typemap(in) const char *(PyObject *encodedInput = NULL) {
Packit Service 672cf4
  if ($input == Py_None)
Packit Service 672cf4
    $1 = NULL;
Packit Service 672cf4
  else if (PyUnicode_Check($input))
Packit Service 672cf4
    {
Packit Service 672cf4
      encodedInput = PyUnicode_AsUTF8String($input);
Packit Service 672cf4
      if (encodedInput == NULL)
Packit Service 61ff77
        return NULL;
Packit Service 672cf4
      $1 = PyBytes_AsString(encodedInput);
Packit Service 672cf4
    }
Packit Service 672cf4
  else if (PyBytes_Check($input))
Packit Service 672cf4
    $1 = PyBytes_AsString($input);
Packit Service 672cf4
  else {
Packit Service 672cf4
    PyErr_Format(PyExc_TypeError,
Packit Service 672cf4
                 "arg %d: expected str, bytes, or None, got %s",
Packit Service 672cf4
		 $argnum, $input->ob_type->tp_name);
Packit Service 61ff77
    return NULL;
Packit Service 672cf4
  }
Packit Service 672cf4
}
Packit Service 672cf4
%typemap(freearg) const char * {
Packit Service 672cf4
  Py_XDECREF(encodedInput$argnum);
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
/* Likewise for a list of strings.  */
Packit Service 61ff77
%typemap(in) const char *[] (void *vector = NULL,
Packit Service 61ff77
                             size_t size,
Packit Service 672cf4
                             PyObject **pyVector = NULL) {
Packit Service 672cf4
  /* Check if is a list */
Packit Service 672cf4
  if (PyList_Check($input)) {
Packit Service 672cf4
    size_t i, j;
Packit Service 672cf4
    size = PyList_Size($input);
Packit Service 61ff77
    $1 = (char **) (vector = malloc((size+1) * sizeof(char *)));
Packit Service 672cf4
    pyVector = calloc(sizeof *pyVector, size);
Packit Service 672cf4
Packit Service 672cf4
    for (i = 0; i < size; i++) {
Packit Service 672cf4
      PyObject *o = PyList_GetItem($input,i);
Packit Service 672cf4
      if (PyUnicode_Check(o))
Packit Service 672cf4
        {
Packit Service 672cf4
          pyVector[i] = PyUnicode_AsUTF8String(o);
Packit Service 672cf4
          if (pyVector[i] == NULL)
Packit Service 61ff77
            {
Packit Service 61ff77
              free(vector);
Packit Service 61ff77
              for (j = 0; j < i; j++)
Packit Service 61ff77
                Py_XDECREF(pyVector[j]);
Packit Service 61ff77
              return NULL;
Packit Service 61ff77
            }
Packit Service 672cf4
          $1[i] = PyBytes_AsString(pyVector[i]);
Packit Service 672cf4
        }
Packit Service 672cf4
      else if (PyString_Check(o))
Packit Service 672cf4
	$1[i] = PyString_AsString(o);
Packit Service 672cf4
      else {
Packit Service 672cf4
	PyErr_Format(PyExc_TypeError,
Packit Service 672cf4
                     "arg %d: list must contain only str or bytes, got %s "
Packit Service 672cf4
                     "at position %d",
Packit Service 672cf4
                     $argnum, o->ob_type->tp_name, i);
Packit Service 61ff77
	free($1);
Packit Service 61ff77
	return NULL;
Packit Service 672cf4
      }
Packit Service 672cf4
    }
Packit Service 672cf4
    $1[i] = NULL;
Packit Service 672cf4
  } else {
Packit Service 672cf4
    PyErr_Format(PyExc_TypeError,
Packit Service 672cf4
                 "arg %d: expected a list of str or bytes, got %s",
Packit Service 672cf4
                 $argnum, $input->ob_type->tp_name);
Packit Service 61ff77
    return NULL;
Packit Service 672cf4
  }
Packit Service 672cf4
}
Packit Service 672cf4
%typemap(freearg) const char *[] {
Packit Service 61ff77
  size_t i;
Packit Service 61ff77
  free(vector$argnum);
Packit Service 61ff77
  for (i = 0; i < size$argnum; i++)
Packit Service 61ff77
    Py_XDECREF(pyVector$argnum[i]);
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
/* Release returned buffers as necessary.  */
Packit Service 672cf4
%typemap(newfree) char * "gpgme_free($1);";
Packit Service 672cf4
%newobject gpgme_data_release_and_get_mem;
Packit Service 672cf4
%newobject gpgme_pubkey_algo_string;
Packit Service 672cf4
%newobject gpgme_addrspec_from_uid;
Packit Service 672cf4
Packit Service 672cf4
%typemap(arginit) gpgme_key_t [] {
Packit Service 672cf4
  $1 = NULL;
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
%typemap(in) gpgme_key_t [] {
Packit Service 672cf4
  int i, numb = 0;
Packit Service 672cf4
  if (!PySequence_Check($input)) {
Packit Service 672cf4
    PyErr_Format(PyExc_ValueError, "arg %d: Expected a list of gpgme_key_t",
Packit Service 672cf4
		 $argnum);
Packit Service 61ff77
    return NULL;
Packit Service 672cf4
  }
Packit Service 672cf4
  if((numb = PySequence_Length($input)) != 0) {
Packit Service 672cf4
    $1 = (gpgme_key_t*)malloc((numb+1)*sizeof(gpgme_key_t));
Packit Service 672cf4
    for(i=0; i
Packit Service 672cf4
      PyObject *pypointer = PySequence_GetItem($input, i);
Packit Service 672cf4
Packit Service 672cf4
      /* input = $input, 1 = $1, 1_descriptor = $1_descriptor */
Packit Service 672cf4
      /* &1_descriptor = $&1_descriptor *1_descriptor = $*1_descriptor */
Packit Service 672cf4
Packit Service 672cf4
      /* Following code is from swig's python.swg.  */
Packit Service 672cf4
      if ((SWIG_ConvertPtr(pypointer,(void **) &$1[i], $*1_descriptor,SWIG_POINTER_EXCEPTION | $disown )) == -1) {
Packit Service 672cf4
        Py_DECREF(pypointer);
Packit Service 672cf4
	PyErr_Format(PyExc_TypeError,
Packit Service 672cf4
                     "arg %d: list must contain only gpgme_key_ts, got %s "
Packit Service 672cf4
                     "at position %d",
Packit Service 672cf4
                     $argnum, pypointer->ob_type->tp_name, i);
Packit Service 61ff77
        free($1);
Packit Service 61ff77
	return NULL;
Packit Service 672cf4
      }
Packit Service 672cf4
      Py_DECREF(pypointer);
Packit Service 672cf4
    }
Packit Service 672cf4
    $1[numb] = NULL;
Packit Service 672cf4
  }
Packit Service 672cf4
}
Packit Service 672cf4
%typemap(freearg) gpgme_key_t [] {
Packit Service 672cf4
  if ($1) free($1);
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
/* Special handling for references to our objects.  */
Packit Service 672cf4
%typemap(in) gpgme_data_t DATAIN (gpgme_data_t wrapper = NULL,
Packit Service 672cf4
                                  PyObject *bytesio = NULL,
Packit Service 672cf4
                                  Py_buffer view, int have_view = 0) {
Packit Service 672cf4
  /* If we create a temporary wrapper object, we will store it in
Packit Service 672cf4
     wrapperN, where N is $argnum.  Here in this fragment, SWIG will
Packit Service 672cf4
     automatically append $argnum.  */
Packit Service 672cf4
  memset(&view, 0, sizeof view);
Packit Service 672cf4
  if ($input == Py_None)
Packit Service 672cf4
    $1 = NULL;
Packit Service 672cf4
  else {
Packit Service 672cf4
    PyObject *pypointer;
Packit Service 672cf4
    pypointer = _gpg_obj2gpgme_data_t($input, $argnum, &wrapper,
Packit Service 672cf4
                                       &bytesio, &view);
Packit Service 672cf4
    if (pypointer == NULL)
Packit Service 61ff77
      return NULL;
Packit Service 672cf4
    have_view = !! view.obj;
Packit Service 672cf4
Packit Service 672cf4
    /* input = $input, 1 = $1, 1_descriptor = $1_descriptor */
Packit Service 672cf4
Packit Service 672cf4
    /* Following code is from swig's python.swg.  */
Packit Service 672cf4
Packit Service 672cf4
    if ((SWIG_ConvertPtr(pypointer,(void **) &$1, $1_descriptor,
Packit Service 672cf4
         SWIG_POINTER_EXCEPTION | $disown )) == -1) {
Packit Service 672cf4
      Py_DECREF(pypointer);
Packit Service 61ff77
      return NULL;
Packit Service 672cf4
    }
Packit Service 672cf4
    Py_DECREF(pypointer);
Packit Service 672cf4
  }
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
#if HAVE_DATA_H
Packit Service 672cf4
/* If we are doing an in-tree build, we can use the internal
Packit Service 672cf4
   representation of struct gpgme_data for an very efficient check if
Packit Service 672cf4
   the buffer has been modified.  */
Packit Service 672cf4
%{
Packit Service 672cf4
#include "data.h"	/* For struct gpgme_data.  */
Packit Service 672cf4
%}
Packit Service 672cf4
#endif
Packit Service 672cf4
Packit Service 672cf4
%typemap(freearg) gpgme_data_t DATAIN {
Packit Service 672cf4
  /* See whether we need to update the Python buffer.  */
Packit Service 672cf4
  if (resultobj && wrapper$argnum && view$argnum.buf)
Packit Service 672cf4
    {
Packit Service 672cf4
      int dirty;
Packit Service 672cf4
      char *new_data = NULL;
Packit Service 672cf4
      size_t new_size;
Packit Service 672cf4
Packit Service 672cf4
#if HAVE_DATA_H
Packit Service 672cf4
      new_data = wrapper$argnum->data.mem.buffer;
Packit Service 672cf4
      new_size = wrapper$argnum->data.mem.length;
Packit Service 672cf4
      dirty = new_data != NULL;
Packit Service 672cf4
#else
Packit Service 672cf4
      new_data = gpgme_data_release_and_get_mem (wrapper$argnum, &new_size);
Packit Service 672cf4
      wrapper$argnum = NULL;
Packit Service 672cf4
      dirty = new_size != view$argnum.len
Packit Service 672cf4
        || memcmp (new_data, view$argnum.buf, view$argnum.len);
Packit Service 672cf4
#endif
Packit Service 672cf4
Packit Service 672cf4
      if (dirty)
Packit Service 672cf4
        {
Packit Service 672cf4
          /* The buffer is dirty.  */
Packit Service 672cf4
          if (view$argnum.readonly)
Packit Service 672cf4
            {
Packit Service 672cf4
              Py_XDECREF(resultobj);
Packit Service 672cf4
              resultobj = NULL;
Packit Service 672cf4
              PyErr_SetString(PyExc_ValueError,
Packit Service 672cf4
                              "cannot update read-only buffer");
Packit Service 672cf4
            }
Packit Service 672cf4
Packit Service 672cf4
          /* See if we need to truncate the buffer.  */
Packit Service 672cf4
          if (resultobj && view$argnum.len != new_size)
Packit Service 672cf4
            {
Packit Service 672cf4
              if (bytesio$argnum == NULL)
Packit Service 672cf4
                {
Packit Service 672cf4
                  Py_XDECREF(resultobj);
Packit Service 672cf4
                  resultobj = NULL;
Packit Service 672cf4
                  PyErr_SetString(PyExc_ValueError, "cannot resize buffer");
Packit Service 672cf4
                }
Packit Service 672cf4
              else
Packit Service 672cf4
                {
Packit Service 672cf4
                  PyObject *retval;
Packit Service 672cf4
                  PyBuffer_Release(&view$argnum);
Packit Service 672cf4
                  assert(view$argnum.obj == NULL);
Packit Service 672cf4
                  retval = PyObject_CallMethod(bytesio$argnum, "truncate",
Packit Service 672cf4
                                               "l", (long) new_size);
Packit Service 672cf4
                  if (retval == NULL)
Packit Service 672cf4
                    {
Packit Service 672cf4
                      Py_XDECREF(resultobj);
Packit Service 672cf4
                      resultobj = NULL;
Packit Service 672cf4
                    }
Packit Service 672cf4
                  else
Packit Service 672cf4
                    {
Packit Service 672cf4
                      Py_DECREF(retval);
Packit Service 672cf4
Packit Service 672cf4
                      retval = PyObject_CallMethod(bytesio$argnum,
Packit Service 672cf4
                                                   "getbuffer", NULL);
Packit Service 672cf4
                      if (retval == NULL
Packit Service 672cf4
                          || PyObject_GetBuffer(retval, &view$argnum,
Packit Service 672cf4
                                           PyBUF_SIMPLE|PyBUF_WRITABLE) < 0)
Packit Service 672cf4
                        {
Packit Service 672cf4
                          Py_XDECREF(resultobj);
Packit Service 672cf4
                          resultobj = NULL;
Packit Service 672cf4
                        }
Packit Service 672cf4
Packit Service 672cf4
                      Py_XDECREF(retval);
Packit Service 672cf4
Packit Service 672cf4
                      if (resultobj && view$argnum.len
Packit Service 672cf4
                          != new_size)
Packit Service 672cf4
                        {
Packit Service 672cf4
                          Py_XDECREF(resultobj);
Packit Service 672cf4
                          resultobj = NULL;
Packit Service 672cf4
                          PyErr_Format(PyExc_ValueError,
Packit Service 672cf4
                                       "Expected buffer of length %zu, got %zi",
Packit Service 672cf4
                                       new_size,
Packit Service 672cf4
                                       view$argnum.len);
Packit Service 672cf4
                        }
Packit Service 672cf4
                    }
Packit Service 672cf4
                }
Packit Service 672cf4
            }
Packit Service 672cf4
          if (resultobj)
Packit Service 672cf4
            memcpy(view$argnum.buf, new_data, new_size);
Packit Service 672cf4
        }
Packit Service 672cf4
#if ! HAVE_DATA_H
Packit Service 672cf4
      free (new_data);
Packit Service 672cf4
#endif
Packit Service 672cf4
    }
Packit Service 672cf4
Packit Service 672cf4
  /* Free the temporary wrapper, if any.  */
Packit Service 672cf4
  if (wrapper$argnum)
Packit Service 672cf4
    gpgme_data_release(wrapper$argnum);
Packit Service 672cf4
  Py_XDECREF (bytesio$argnum);
Packit Service 672cf4
  if (have_view$argnum && view$argnum.buf)
Packit Service 672cf4
    PyBuffer_Release(&view$argnum);
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
%apply gpgme_data_t DATAIN {gpgme_data_t plain, gpgme_data_t cipher,
Packit Service 672cf4
			gpgme_data_t sig, gpgme_data_t signed_text,
Packit Service 672cf4
			gpgme_data_t plaintext, gpgme_data_t keydata,
Packit Service 672cf4
			gpgme_data_t pubkey, gpgme_data_t seckey,
Packit Service 672cf4
			gpgme_data_t out, gpgme_data_t data};
Packit Service 672cf4
Packit Service 672cf4
/* SWIG has problems interpreting ssize_t, off_t or gpgme_error_t in
Packit Service 672cf4
   gpgme.h.  */
Packit Service 672cf4
%typemap(out) ssize_t, gpgme_error_t, gpgme_err_code_t, gpgme_err_source_t, gpg_error_t {
Packit Service 672cf4
  $result = PyLong_FromLong($1);
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
%typemap(in) ssize_t, gpgme_error_t, gpgme_err_code_t, gpgme_err_source_t, gpg_error_t {
Packit Service 672cf4
  if (PyLong_Check($input))
Packit Service 672cf4
    $1 = PyLong_AsLong($input);
Packit Service 672cf4
#if PY_MAJOR_VERSION < 3
Packit Service 672cf4
  else if (PyInt_Check($input))
Packit Service 672cf4
    $1 = PyInt_AsLong($input);
Packit Service 672cf4
#endif
Packit Service 672cf4
  else
Packit Service 672cf4
    PyErr_SetString(PyExc_TypeError, "Numeric argument expected");
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
%typemap(out) off_t {
Packit Service 672cf4
#if _FILE_OFFSET_BITS == 64
Packit Service 672cf4
  $result = PyLong_FromLongLong($1);
Packit Service 672cf4
#else
Packit Service 672cf4
  $result = PyLong_FromLong($1);
Packit Service 672cf4
#endif
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
%typemap(in) off_t {
Packit Service 672cf4
  if (PyLong_Check($input))
Packit Service 672cf4
#if _FILE_OFFSET_BITS == 64
Packit Service 672cf4
    $1 = PyLong_AsLongLong($input);
Packit Service 672cf4
#else
Packit Service 672cf4
    $1 = PyLong_AsLong($input);
Packit Service 672cf4
#endif
Packit Service 672cf4
#if PY_MAJOR_VERSION < 3
Packit Service 672cf4
  else if (PyInt_Check($input))
Packit Service 672cf4
    $1 = PyInt_AsLong($input);
Packit Service 672cf4
#endif
Packit Service 672cf4
  else
Packit Service 672cf4
    PyErr_SetString(PyExc_TypeError, "Numeric argument expected");
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
/* Those are for gpgme_data_read() and gpgme_strerror_r().  */
Packit Service 672cf4
%typemap(in) (void *buffer, size_t size), (char *buf, size_t buflen) {
Packit Service 672cf4
  {
Packit Service 672cf4
    long tmp$argnum;
Packit Service 672cf4
    if (PyLong_Check($input))
Packit Service 672cf4
      tmp$argnum = PyLong_AsLong($input);
Packit Service 672cf4
#if PY_MAJOR_VERSION < 3
Packit Service 672cf4
    else if (PyInt_Check($input))
Packit Service 672cf4
      tmp$argnum = PyInt_AsLong($input);
Packit Service 672cf4
#endif
Packit Service 672cf4
    else
Packit Service 672cf4
      {
Packit Service 672cf4
        PyErr_SetString(PyExc_TypeError, "Numeric argument expected");
Packit Service 61ff77
        return NULL;
Packit Service 672cf4
      }
Packit Service 672cf4
Packit Service 672cf4
    if (tmp$argnum < 0) {
Packit Service 672cf4
      PyErr_SetString(PyExc_ValueError, "Positive integer expected");
Packit Service 61ff77
      return NULL;
Packit Service 672cf4
    }
Packit Service 672cf4
    $2 = (size_t) tmp$argnum;
Packit Service 672cf4
    $1 = ($1_ltype) malloc($2+1);
Packit Service 672cf4
  }
Packit Service 672cf4
}
Packit Service 672cf4
%typemap(argout) (void *buffer, size_t size), (char *buf, size_t buflen) {
Packit Service 672cf4
  Py_XDECREF($result);   /* Blow away any previous result */
Packit Service 672cf4
  if (result < 0) {      /* Check for I/O error */
Packit Service 61ff77
    free($1);
Packit Service 672cf4
    return PyErr_SetFromErrno(PyExc_RuntimeError);
Packit Service 672cf4
  }
Packit Service 672cf4
  $result = PyBytes_FromStringAndSize($1,result);
Packit Service 61ff77
  free($1);
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
/* For gpgme_data_write, but should be universal.  */
Packit Service 672cf4
%typemap(in) (const void *buffer, size_t size)(PyObject *encodedInput = NULL) {
Packit Service 672cf4
  Py_ssize_t ssize;
Packit Service 672cf4
Packit Service 672cf4
  if ($input == Py_None)
Packit Service 672cf4
    $1 = NULL, $2 = 0;
Packit Service 672cf4
  else if (PyUnicode_Check($input))
Packit Service 672cf4
    {
Packit Service 672cf4
      encodedInput = PyUnicode_AsUTF8String($input);
Packit Service 672cf4
      if (encodedInput == NULL)
Packit Service 61ff77
        return NULL;
Packit Service 672cf4
      if (PyBytes_AsStringAndSize(encodedInput, (char **) &$1, &ssize) == -1)
Packit Service 672cf4
        {
Packit Service 672cf4
          Py_DECREF(encodedInput);
Packit Service 61ff77
          return NULL;
Packit Service 672cf4
        }
Packit Service 672cf4
    }
Packit Service 672cf4
  else if (PyBytes_Check($input))
Packit Service 672cf4
    PyBytes_AsStringAndSize($input, (char **) &$1, &ssize);
Packit Service 672cf4
  else {
Packit Service 672cf4
    PyErr_Format(PyExc_TypeError,
Packit Service 672cf4
                 "arg %d: expected str, bytes, or None, got %s",
Packit Service 672cf4
		 $argnum, $input->ob_type->tp_name);
Packit Service 61ff77
    return NULL;
Packit Service 672cf4
  }
Packit Service 672cf4
Packit Service 672cf4
  if (! $1)
Packit Service 672cf4
    $2 = 0;
Packit Service 672cf4
  else
Packit Service 672cf4
    {
Packit Service 672cf4
      assert (ssize >= 0);
Packit Service 672cf4
      $2 = (size_t) ssize;
Packit Service 672cf4
    }
Packit Service 672cf4
}
Packit Service 672cf4
%typemap(freearg) (const void *buffer, size_t size) {
Packit Service 672cf4
  Py_XDECREF(encodedInput$argnum);
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
/* Make types containing 'next' field to be lists.  */
Packit Service 672cf4
%ignore next;
Packit Service 672cf4
%typemap(out) gpgme_sig_notation_t, gpgme_subkey_t,
Packit Service 672cf4
   gpgme_key_sig_t, gpgme_user_id_t, gpgme_invalid_key_t,
Packit Service 672cf4
   gpgme_recipient_t, gpgme_new_signature_t, gpgme_signature_t,
Packit Service 672cf4
   gpgme_import_status_t, gpgme_conf_arg_t, gpgme_conf_opt_t,
Packit Service 672cf4
   gpgme_conf_comp_t, gpgme_tofu_info_t {
Packit Service 672cf4
  int i;
Packit Service 672cf4
  int size = 0;
Packit Service 672cf4
  $1_ltype curr;
Packit Service 672cf4
  for (curr = $1; curr != NULL; curr = curr->next) {
Packit Service 672cf4
    size++;
Packit Service 672cf4
  }
Packit Service 672cf4
  $result = PyList_New(size);
Packit Service 672cf4
  for (i=0,curr=$1; i<size; i++,curr=curr->next) {
Packit Service 672cf4
    PyObject *o = SWIG_NewPointerObj(SWIG_as_voidptr(curr), $1_descriptor, %newpointer_flags);
Packit Service 672cf4
    PyList_SetItem($result, i, o);
Packit Service 672cf4
  }
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4

Packit Service 672cf4
Packit Service 672cf4
/* Wrap the fragile result objects into robust Python ones.  */
Packit Service 672cf4
%define wrapresult(cls, name)
Packit Service 672cf4
%typemap(out) cls {
Packit Service 672cf4
  PyObject *fragile;
Packit Service 672cf4
  fragile = SWIG_NewPointerObj(SWIG_as_voidptr($1), $1_descriptor,
Packit Service 672cf4
                               %newpointer_flags);
Packit Service 672cf4
  $result = _gpg_wrap_result(fragile, name);
Packit Service 672cf4
  Py_DECREF(fragile);
Packit Service 672cf4
}
Packit Service 672cf4
%enddef
Packit Service 672cf4
Packit Service 672cf4
wrapresult(gpgme_encrypt_result_t, "EncryptResult")
Packit Service 672cf4
wrapresult(gpgme_decrypt_result_t, "DecryptResult")
Packit Service 672cf4
wrapresult(gpgme_sign_result_t, "SignResult")
Packit Service 672cf4
wrapresult(gpgme_verify_result_t, "VerifyResult")
Packit Service 672cf4
wrapresult(gpgme_import_result_t, "ImportResult")
Packit Service 672cf4
wrapresult(gpgme_genkey_result_t, "GenkeyResult")
Packit Service 672cf4
wrapresult(gpgme_keylist_result_t, "KeylistResult")
Packit Service 672cf4
wrapresult(gpgme_vfs_mount_result_t, "VFSMountResult")
Packit Service 672cf4
Packit Service 672cf4
%typemap(out) gpgme_engine_info_t {
Packit Service 672cf4
  int i;
Packit Service 672cf4
  int size = 0;
Packit Service 672cf4
  $1_ltype curr;
Packit Service 672cf4
  for (curr = $1; curr != NULL; curr = curr->next) {
Packit Service 672cf4
    size++;
Packit Service 672cf4
  }
Packit Service 672cf4
  $result = PyList_New(size);
Packit Service 672cf4
  if ($result == NULL)
Packit Service 61ff77
    return NULL;	/* raise */
Packit Service 672cf4
  for (i=0,curr=$1; i<size; i++,curr=curr->next) {
Packit Service 672cf4
    PyObject *fragile, *o;
Packit Service 672cf4
    fragile = SWIG_NewPointerObj(SWIG_as_voidptr(curr), $1_descriptor,
Packit Service 672cf4
                                 %newpointer_flags);
Packit Service 672cf4
    if (fragile == NULL)
Packit Service 672cf4
      {
Packit Service 672cf4
        Py_DECREF($result);
Packit Service 61ff77
        return NULL;	/* raise */
Packit Service 672cf4
      }
Packit Service 672cf4
    o = _gpg_wrap_result(fragile, "EngineInfo");
Packit Service 672cf4
    Py_DECREF(fragile);
Packit Service 672cf4
    if (o == NULL)
Packit Service 672cf4
      {
Packit Service 672cf4
        Py_DECREF($result);
Packit Service 61ff77
        return NULL;	/* raise */
Packit Service 672cf4
      }
Packit Service 672cf4
    PyList_SetItem($result, i, o);
Packit Service 672cf4
  }
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4

Packit Service 672cf4
Packit Service 672cf4
/* Include mapper for interact callbacks.  */
Packit Service 672cf4
%typemap(in) (gpgme_interact_cb_t fnc, void *fnc_value) {
Packit Service 672cf4
  if (! PyTuple_Check($input))
Packit Service 672cf4
    return PyErr_Format(PyExc_TypeError, "interact callback must be a tuple");
Packit Service 672cf4
  if (PyTuple_Size($input) != 2 && PyTuple_Size($input) != 3)
Packit Service 672cf4
    return PyErr_Format(PyExc_TypeError,
Packit Service 672cf4
                        "interact callback must be a tuple of size 2 or 3");
Packit Service 672cf4
Packit Service 672cf4
  $1 = (gpgme_interact_cb_t) _gpg_interact_cb;
Packit Service 672cf4
  $2 = $input;
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4

Packit Service 672cf4
Packit Service 672cf4
/* The assuan protocol callbacks.  */
Packit Service 672cf4
%typemap(in) (gpgme_assuan_data_cb_t data_cb, void *data_cb_value) {
Packit Service 672cf4
  if ($input == Py_None)
Packit Service 672cf4
    $1 = $2 = NULL;
Packit Service 672cf4
  else
Packit Service 672cf4
    {
Packit Service 672cf4
      if (! PyTuple_Check($input))
Packit Service 672cf4
        return PyErr_Format(PyExc_TypeError, "callback must be a tuple");
Packit Service 672cf4
      if (PyTuple_Size($input) != 2)
Packit Service 672cf4
        return PyErr_Format(PyExc_TypeError,
Packit Service 672cf4
                            "callback must be a tuple of size 2");
Packit Service 672cf4
      if (! PyCallable_Check(PyTuple_GetItem($input, 1)))
Packit Service 672cf4
        return PyErr_Format(PyExc_TypeError, "second item must be callable");
Packit Service 672cf4
      $1 = _gpg_assuan_data_cb;
Packit Service 672cf4
      $2 = $input;
Packit Service 672cf4
    }
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
%typemap(in) (gpgme_assuan_inquire_cb_t inq_cb, void *inq_cb_value) {
Packit Service 672cf4
  if ($input == Py_None)
Packit Service 672cf4
    $1 = $2 = NULL;
Packit Service 672cf4
  else
Packit Service 672cf4
    {
Packit Service 672cf4
      if (! PyTuple_Check($input))
Packit Service 672cf4
        return PyErr_Format(PyExc_TypeError, "callback must be a tuple");
Packit Service 672cf4
      if (PyTuple_Size($input) != 2)
Packit Service 672cf4
        return PyErr_Format(PyExc_TypeError,
Packit Service 672cf4
                            "callback must be a tuple of size 2");
Packit Service 672cf4
      if (! PyCallable_Check(PyTuple_GetItem($input, 1)))
Packit Service 672cf4
        return PyErr_Format(PyExc_TypeError, "second item must be callable");
Packit Service 672cf4
      $1 = _gpg_assuan_inquire_cb;
Packit Service 672cf4
      $2 = $input;
Packit Service 672cf4
    }
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
%typemap(in) (gpgme_assuan_status_cb_t stat_cb, void *stat_cb_value) {
Packit Service 672cf4
  if ($input == Py_None)
Packit Service 672cf4
    $1 = $2 = NULL;
Packit Service 672cf4
  else
Packit Service 672cf4
    {
Packit Service 672cf4
      if (! PyTuple_Check($input))
Packit Service 672cf4
        return PyErr_Format(PyExc_TypeError, "callback must be a tuple");
Packit Service 672cf4
      if (PyTuple_Size($input) != 2)
Packit Service 672cf4
        return PyErr_Format(PyExc_TypeError,
Packit Service 672cf4
                            "callback must be a tuple of size 2");
Packit Service 672cf4
      if (! PyCallable_Check(PyTuple_GetItem($input, 1)))
Packit Service 672cf4
        return PyErr_Format(PyExc_TypeError, "second item must be callable");
Packit Service 672cf4
      $1 = _gpg_assuan_status_cb;
Packit Service 672cf4
      $2 = $input;
Packit Service 672cf4
    }
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
/* With SWIG, you can define default arguments for parameters.
Packit Service 672cf4
 * While it's legal in C++ it is not in C, so we cannot change the
Packit Service 672cf4
 * already existing gpgme.h. We need, however, to declare the function
Packit Service 672cf4
 * *before* SWIG loads it from gpgme.h. Hence, we define it here.     */
Packit Service 672cf4
gpgme_error_t gpgme_op_keylist_start (gpgme_ctx_t ctx,
Packit Service 672cf4
                      const char *pattern="",
Packit Service 672cf4
                      int secret_only=0);
Packit Service 672cf4
Packit Service 672cf4
/* The whence argument is surprising in Python-land,
Packit Service 672cf4
   because BytesIO or StringIO objects do not require it.
Packit Service 672cf4
   It defaults to SEEK_SET. Let's do that for Data objects, too */
Packit Service 672cf4
off_t gpgme_data_seek (gpgme_data_t dh, off_t offset, int whence=SEEK_SET);
Packit Service 672cf4
Packit Service 672cf4
/* Include the unmodified <gpgme.h> for cc, and the cleaned-up local
Packit Service 672cf4
   version for SWIG.  We do, however, want to hide certain fields on
Packit Service 672cf4
   some structs, which we provide prior to including the version for
Packit Service 672cf4
   SWIG.  */
Packit Service 672cf4
%{
Packit Service 672cf4
#ifdef HAVE_CONFIG_H
Packit Service 672cf4
#include "config.h"
Packit Service 672cf4
#endif
Packit Service 672cf4
Packit Service 672cf4
#include <gpgme.h>
Packit Service 672cf4
%}
Packit Service 672cf4
Packit Service 672cf4
/* This is for notations, where we want to hide the length fields, and
Packit Service 0ef63b
 * the unused bit field block.  We silence the warning.  */
Packit Service 0ef63b
%warnfilter(302) _gpgme_sig_notation;
Packit Service 672cf4
struct _gpgme_sig_notation
Packit Service 672cf4
{
Packit Service 672cf4
  struct _gpgme_sig_notation *next;
Packit Service 672cf4
Packit Service 672cf4
  /* If NAME is a null pointer, then VALUE contains a policy URL
Packit Service 672cf4
     rather than a notation.  */
Packit Service 672cf4
  char *name;
Packit Service 672cf4
Packit Service 672cf4
  /* The value of the notation data.  */
Packit Service 672cf4
  char *value;
Packit Service 672cf4
Packit Service 672cf4
  /* The accumulated flags.  */
Packit Service 672cf4
  gpgme_sig_notation_flags_t flags;
Packit Service 672cf4
Packit Service 672cf4
  /* Notation data is human-readable.  */
Packit Service 672cf4
  unsigned int human_readable : 1;
Packit Service 672cf4
Packit Service 672cf4
  /* Notation data is critical.  */
Packit Service 672cf4
  unsigned int critical : 1;
Packit Service 672cf4
};
Packit Service 672cf4
Packit Service 672cf4
/* Now include our local modified version.  Any structs defined above
Packit Service 672cf4
   are ignored.  */
Packit Service 672cf4
#ifdef HAVE_CONFIG_H
Packit Service 672cf4
%include "config.h"
Packit Service 672cf4
#endif
Packit Service 672cf4
Packit Service 672cf4
%include "gpgme.h"
Packit Service 672cf4
Packit Service 672cf4
%include "errors.i"
Packit Service 672cf4
Packit Service 672cf4
/* Generating and handling pointers-to-pointers.  */
Packit Service 672cf4
Packit Service 672cf4
%pointer_functions(gpgme_ctx_t, gpgme_ctx_t_p);
Packit Service 672cf4
%pointer_functions(gpgme_data_t, gpgme_data_t_p);
Packit Service 672cf4
%pointer_functions(gpgme_key_t, gpgme_key_t_p);
Packit Service 672cf4
%pointer_functions(gpgme_error_t, gpgme_error_t_p);
Packit Service 672cf4
%pointer_functions(gpgme_trust_item_t, gpgme_trust_item_t_p);
Packit Service 672cf4
%pointer_functions(gpgme_engine_info_t, gpgme_engine_info_t_p);
Packit Service 672cf4
Packit Service 672cf4
/* Helper functions.  */
Packit Service 672cf4
Packit Service 672cf4
%{
Packit Service 672cf4
#include <stdio.h>
Packit Service 672cf4
%}
Packit Service 672cf4
FILE *fdopen(int fildes, const char *mode);
Packit Service 672cf4
Packit Service 672cf4
/* We include both headers in the generated c code...  */
Packit Service 672cf4
%{
Packit Service 672cf4
#include "helpers.h"
Packit Service 672cf4
#include "private.h"
Packit Service 672cf4
Packit Service 672cf4
/* SWIG runtime support for helpers.c  */
Packit Service 672cf4
PyObject *
Packit Service 672cf4
_gpg_wrap_gpgme_data_t(gpgme_data_t data)
Packit Service 672cf4
{
Packit Service 672cf4
  /*
Packit Service 672cf4
   * If SWIG is invoked without -builtin, the macro SWIG_NewPointerObj
Packit Service 672cf4
   * expects a variable named "self".
Packit Service 672cf4
   *
Packit Service 672cf4
   * XXX: It is not quite clear why passing NULL as self is okay, but
Packit Service 672cf4
   * it works with -builtin, and it seems to work just fine without
Packit Service 672cf4
   * it too.
Packit Service 672cf4
   */
Packit Service 672cf4
  PyObject* self = NULL;
Packit Service 672cf4
  (void) self;
Packit Service 672cf4
  return SWIG_NewPointerObj(data, SWIGTYPE_p_gpgme_data, 0);
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
gpgme_ctx_t
Packit Service 672cf4
_gpg_unwrap_gpgme_ctx_t(PyObject *wrapped)
Packit Service 672cf4
{
Packit Service 672cf4
  gpgme_ctx_t result;
Packit Service 672cf4
  if (SWIG_ConvertPtr(wrapped,
Packit Service 672cf4
                      (void **) &result,
Packit Service 672cf4
                      SWIGTYPE_p_gpgme_context,
Packit Service 672cf4
                      SWIG_POINTER_EXCEPTION) == -1)
Packit Service 672cf4
    return NULL;
Packit Service 672cf4
  return result;
Packit Service 672cf4
}
Packit Service 672cf4
%}
Packit Service 672cf4
Packit Service 672cf4
/* ... but only the public definitions here.  They will be exposed to
Packit Service 672cf4
   the Python world, so let's be careful.  */
Packit Service 672cf4
%include "helpers.h"
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
%define genericrepr(cls)
Packit Service 672cf4
%pythoncode %{
Packit Service 672cf4
    def __repr__(self):
Packit Service 672cf4
        names = [name for name in dir(self)
Packit Service 672cf4
            if not name.startswith("_") and name != "this"]
Packit Service 672cf4
        props = ", ".join(("{}={!r}".format(name, getattr(self, name))
Packit Service 672cf4
            for name in names)
Packit Service 672cf4
        )
Packit Service 672cf4
        return "cls({})".format(props)
Packit Service 672cf4
%}
Packit Service 672cf4
Packit Service 672cf4
%enddef
Packit Service 672cf4
Packit Service 672cf4
%extend _gpgme_key {
Packit Service 672cf4
  genericrepr(Key)
Packit Service 672cf4
};
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
%extend _gpgme_subkey {
Packit Service 672cf4
  genericrepr(SubKey)
Packit Service 672cf4
};
Packit Service 672cf4
Packit Service 672cf4
%extend _gpgme_key_sig {
Packit Service 672cf4
  genericrepr(KeySig)
Packit Service 672cf4
};
Packit Service 672cf4
Packit Service 672cf4
%extend _gpgme_user_id {
Packit Service 672cf4
  genericrepr(UID)
Packit Service 672cf4
};
Packit Service 672cf4
Packit Service 672cf4
%extend _gpgme_tofu_info {
Packit Service 672cf4
  genericrepr(TofuInfo)
Packit Service 672cf4
};