summaryrefslogtreecommitdiff
path: root/SWIG/_util.i
blob: bc2ee61700887606c10e99589834eee737a525ab (plain)
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
/* Copyright (c) 1999-2002 Ng Pheng Siong. All rights reserved.
 * Copyright (c) 2009-2010 Heikki Toivonen. All rights reserved.
*/
/* $Id$ */

%{
#include <openssl/x509v3.h>
%}

%warnfilter(454) _util_err;
%inline %{
static PyObject *_util_err;

void util_init(PyObject *util_err) {
    Py_INCREF(util_err);
    _util_err = util_err;
}
    
PyObject *util_hex_to_string(PyObject *blob) {
    PyObject *obj;
    const void *buf;
    char *ret;
    Py_ssize_t len;

    if (PyObject_AsReadBuffer(blob, &buf, &len) == -1)
        return NULL;

    ret = hex_to_string((unsigned char *)buf, len);
    if (!ret) {
        m2_PyErr_Msg(_util_err);
        return NULL;
    }

    obj = PyBytes_FromString(ret);

    OPENSSL_free(ret);
    return obj;
}

PyObject *util_string_to_hex(PyObject *blob) {
    PyObject *obj;
    const void *buf;
    unsigned char *ret;
    Py_ssize_t len0;
    long len;

    if (PyObject_AsReadBuffer(blob, &buf, &len0) == -1)
        return NULL;

    len = len0;
    ret = string_to_hex((char *)buf, &len);
    if (ret == NULL) {
        m2_PyErr_Msg(_util_err);
        return NULL;
    }
    obj = PyBytes_FromStringAndSize((char*)ret, len);
    OPENSSL_free(ret);
    return obj;
}
%}