summaryrefslogtreecommitdiffstats
path: root/Modules/_xdrmodule.c
blob: d2c94536a2980027d5dccf4a6f4c0cb76058fa7a (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
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
/* This module exports part of the C API to the XDR routines into Python.
 * XDR is Sun's eXternal Data Representation, as described in RFC 1014.  This
 * module is used by xdrlib.py to support the float and double data types
 * which are too much of a pain to support in Python directly.  It is
 * not required by xdrlib.py -- when not available, these types aren't
 * supported at the Python layer.  Note that representations that can be
 * implemented solely in Python, are *not* reproduced here.
 *
 * Module version number: 1.0
 *
 * See xdrlib.py for usage notes.
 *
 * Note: this has so far, only been tested on Solaris 2.5 and IRIX 5.3.  On
 * these systems, you will need to link with -lnsl for these symbols to be
 * defined.
 */

#include "Python.h"

#include <rpc/rpc.h>
#include <rpc/xdr.h>

static PyObject* xdr_error;



static PyObject*
pack_float(self, args)
     PyObject* self;
     PyObject* args;
{
    XDR xdr;
    float value;
    union {                                  /* guarantees proper alignment */
        long dummy;
        char buffer[4];
    } addr;
    PyObject* rtn = NULL;

    if (!PyArg_ParseTuple(args, "f", &value))
        return NULL;

    xdr.x_ops = NULL;
    xdrmem_create(&xdr, addr.buffer, 4, XDR_ENCODE);
    if( xdr.x_ops == NULL )
        PyErr_SetString(xdr_error, "XDR stream initialization failed.");
    else if (xdr_float(&xdr, &value))
        rtn = PyString_FromStringAndSize(addr.buffer, 4);
    else
        PyErr_SetString(xdr_error, "conversion from float failed");

    xdr_destroy(&xdr);
    return rtn;
}
    


static PyObject*
pack_double(self, args)
     PyObject* self;
     PyObject* args;
{
    XDR xdr;
    double value;
    union {                                  /* guarantees proper alignment */
        long dummy;
        char buffer[8];
    } addr;
    PyObject* rtn = NULL;

    if (!PyArg_ParseTuple(args, "d", &value))
        return NULL;

    xdr.x_ops = NULL;
    xdrmem_create(&xdr, addr.buffer, 8, XDR_ENCODE);
    if( xdr.x_ops == NULL )
        PyErr_SetString(xdr_error, "XDR stream initialization failed.");
    else if (xdr_double(&xdr, &value))
        rtn = PyString_FromStringAndSize(addr.buffer, 8);
    else
        PyErr_SetString(xdr_error, "conversion from double failed");

    xdr_destroy(&xdr);
    return rtn;
}



static PyObject*
unpack_float(self, args)
     PyObject* self;
     PyObject* args;
{
    XDR xdr;
    float value;
    char* string;
    int strlen;
    PyObject* rtn = NULL;

    if (!PyArg_ParseTuple(args, "s#", &string, &strlen))
        return NULL;

    if (strlen != 4) {
        PyErr_SetString(PyExc_ValueError, "4 byte string expected");
        return NULL;
    }

        /* Python guarantees that the string is 4 byte aligned */
    xdr.x_ops = NULL;
    xdrmem_create(&xdr, (caddr_t)string, 4, XDR_DECODE);
    if( xdr.x_ops == NULL )
        PyErr_SetString(xdr_error, "XDR stream initialization failed.");
    else if (xdr_float(&xdr, &value))
        rtn = Py_BuildValue("f", value);
    else
        PyErr_SetString(xdr_error, "conversion to float failed");

    xdr_destroy(&xdr);
    return rtn;
}

    

static PyObject*
unpack_double(self, args)
     PyObject* self;
     PyObject* args;
{
    XDR xdr;
    double value;
    char* string;
    int strlen;
    PyObject* rtn = NULL;

    if (!PyArg_ParseTuple(args, "s#", &string, &strlen))
        return NULL;

    if (strlen != 8) {
        PyErr_SetString(PyExc_ValueError, "8 byte string expected");
        return NULL;
    }

        /* Python guarantees that the string is 4 byte aligned */
    xdr.x_ops = NULL;
    xdrmem_create(&xdr, (caddr_t)string, 8, XDR_DECODE);
    if( xdr.x_ops == NULL )
        PyErr_SetString(xdr_error, "XDR stream initialization failed.");
    else if (xdr_double(&xdr, &value))
        rtn = Py_BuildValue("d", value);
    else
        PyErr_SetString(xdr_error, "conversion to double failed");

    xdr_destroy(&xdr);
    return rtn;
}



static struct PyMethodDef
xdr_methods[] = {
    {"pack_float",    pack_float,  1},
    {"pack_double",   pack_double, 1},
    {"unpack_float",  unpack_float, 1},
    {"unpack_double", unpack_double, 1},
    {NULL,          NULL,       0}           /* sentinel */
};



void
init_xdr()
{
    PyObject* module;
    PyObject* dict;

    module = Py_InitModule("_xdr", xdr_methods);
    dict = PyModule_GetDict(module);

    xdr_error = PyString_FromString("_xdr.error");
    PyDict_SetItemString(dict, "error", xdr_error);
}