static PyObject *
PyBlosc_decompress(PyObject *self, PyObject *args)
{
    PyObject *result_str;
    void *input, *output;
    size_t nbytes, cbytes;

    if (!PyArg_ParseTuple(args, "s#:decompress", &input, &cbytes))
      return NULL;

    /*  fetch the uncompressed size into nbytes */
    if (!get_nbytes(input, cbytes, &nbytes))
      return NULL;

    /* Book memory for the result */
    if (!(result_str = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)nbytes)))
      return NULL;
    output = PyBytes_AS_STRING(result_str);

    /*  do decompression */
    if (!decompress_helper(input, nbytes, output)){
      Py_XDECREF(result_str);
      return NULL;
    }

    return result_str;
}
static PyObject *
PyBlosc_decompress_ptr(PyObject *self, PyObject *args)
{
    PyObject * pointer, * return_int;
    void * input, * output;
    size_t cbytes, nbytes;

    /* require a compressed string and a pointer  */
    if (!PyArg_ParseTuple(args, "s#O:decompress", &input, &cbytes, &pointer))
      return NULL;

    /*  convert the int or long Python object to a void * */
    output = PyLong_AsVoidPtr(pointer);
    if (output == NULL)
      return NULL;

    /*  fetch the uncompressed size into nbytes */
    if (!get_nbytes(input, cbytes, &nbytes))
      return NULL;

    /* do decompression */
    if (!decompress_helper(input, nbytes, output))
      return NULL;

    /*  Return nbytes as python integer. This is legitimate, because
     *  decompress_helper above has checked that the number of bytes written
     *  was indeed nbytes.
     *  */
    return_int = PyLong_FromSize_t(nbytes);
    Py_INCREF(return_int);
    return return_int;
}
Exemple #3
0
static PyObject *
PyBlosc_decompress(PyObject *self, PyObject *args)
{
    Py_buffer view;
    PyObject *result_str;
    void *input, *output;
    size_t nbytes, cbytes;
    char *format;
    /* Accept some kind of input */
    #if PY_MAJOR_VERSION <= 2
        /* s* : bytes like object including unicode and anything that supports
         * the buffer interface */
        format = "s*:decompress";
    #elif PY_MAJOR_VERSION >= 3
        /* y* :bytes like object EXCLUDING unicode and anything that supports
         * the buffer interface. This is the recommended way to accept binary
         * data in Python 3. */
        format = "y*:decompress";
    #endif

    if (!PyArg_ParseTuple(args, format, &view))
      return NULL;

    cbytes = view.len;
    input = view.buf;
    /*  fetch the uncompressed size into nbytes */
    if (!get_nbytes(input, cbytes, &nbytes)){
      PyBuffer_Release(&view);
      return NULL;
    }

    /* Book memory for the result */
    if (!(result_str = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)nbytes))){
      PyBuffer_Release(&view);
      return NULL;
    }

    output = PyBytes_AS_STRING(result_str);

    /*  do decompression */
    if (!decompress_helper(input, nbytes, output)){
      Py_XDECREF(result_str);
      PyBuffer_Release(&view);
      return NULL;
    }

    PyBuffer_Release(&view);
    return result_str;
}
Exemple #4
0
void decode(int srcfd, int destfd, struct rsa_key *private_key) {
    unsigned long buf;
    int portion, r;

    portion = get_nbytes(private_key->n);

    for(buf = 0; (r = read(srcfd, &buf, portion)) > 0; buf = 0) {
        if(r < portion) {
            ftruncate(destfd, lseek(destfd, 0, SEEK_END) - buf);
        } else {
            buf = rsa_convert(buf, private_key);
            write(destfd, &buf, portion - 1);
        }
    }
}
Exemple #5
0
void code(int srcfd, int destfd, struct rsa_key *public_key) {
    unsigned long buf;
    int portion, r;

    portion = get_nbytes(public_key->n);

    for(buf = 0; (r = read(srcfd, &buf, portion - 1)) > 0; buf = 0) {
        buf = rsa_convert(buf, public_key);
        write(destfd, &buf, portion);

        if(r < (portion - 1)) {
            r = portion - 1 - r;
            write(destfd, &r, 1);
        }
    }
}
Exemple #6
0
static PyObject *
PyBlosc_decompress(PyObject *self, PyObject *args)
{
  Py_buffer view;
  PyObject *result_str;
  void *input, *output;
  size_t nbytes, cbytes;
  int as_bytearray;
  /* Accept some kind of input */
#if PY_MAJOR_VERSION <= 2
  PyObject *as_bytearray_obj = NULL;
  /* s* : bytes like object including unicode and anything that supports
   * the buffer interface. We cannot use p in python 2 so we will
   * create an object to hold the predicate. */
  if (!PyArg_ParseTuple(args, "s*O:decompress", &view, &as_bytearray_obj))
    return NULL;

  if ((as_bytearray = PyObject_IsTrue(as_bytearray_obj)) < 0) {
    /* failed to convert predicate to bool */
    return NULL;
  }
#elif PY_MAJOR_VERSION >= 3
  /* y* :bytes like object EXCLUDING unicode and anything that supports
   * the buffer interface. This is the recommended way to accept binary
   * data in Python 3. */
  if (!PyArg_ParseTuple(args, "y*p:decompress", &view, &as_bytearray))
    return NULL;
#endif

  cbytes = view.len;
  input = view.buf;
  /*  fetch the uncompressed size into nbytes */
  if (!get_nbytes(input, cbytes, &nbytes)){
    PyBuffer_Release(&view);
    return NULL;
  }

#define branch(from_string_and_size, as_string)				 \
  /* Book memory for the result */					 \
    if (!(result_str = from_string_and_size(NULL, (Py_ssize_t)nbytes))){ \
      PyBuffer_Release(&view);						 \
      return NULL;							 \
    }									 \
                                                                         \
    output = as_string(result_str);					 \
    (void)NULL

  if (as_bytearray) {
    branch(PyByteArray_FromStringAndSize, PyByteArray_AS_STRING);
  }
  else {
    branch(PyBytes_FromStringAndSize, PyBytes_AS_STRING);
  }

#undef branch

  /*  do decompression */
  if (!decompress_helper(input, nbytes, output)){
    Py_XDECREF(result_str);
    PyBuffer_Release(&view);
    return NULL;
  }

  PyBuffer_Release(&view);
  return result_str;
}