Guido van Rossum | 73363b5 | 1996-08-19 22:58:53 +0000 | [diff] [blame] | 1 | /* This module exports part of the C API to the XDR routines into Python. |
| 2 | * XDR is Sun's eXternal Data Representation, as described in RFC 1014. This |
| 3 | * module is used by xdrlib.py to support the float and double data types |
| 4 | * which are too much of a pain to support in Python directly. It is |
| 5 | * not required by xdrlib.py -- when not available, these types aren't |
| 6 | * supported at the Python layer. Note that representations that can be |
| 7 | * implemented solely in Python, are *not* reproduced here. |
| 8 | * |
| 9 | * Module version number: 1.0 |
| 10 | * |
| 11 | * See xdrlib.py for usage notes. |
| 12 | * |
Guido van Rossum | d62d8bc | 1996-08-20 19:47:44 +0000 | [diff] [blame^] | 13 | * Note: this has so far, only been tested on Solaris 2.5 and IRIX 5.3. On |
| 14 | * these systems, you will need to link with -lnsl for these symbols to be |
| 15 | * defined. |
Guido van Rossum | 73363b5 | 1996-08-19 22:58:53 +0000 | [diff] [blame] | 16 | */ |
| 17 | |
| 18 | #include "Python.h" |
| 19 | |
| 20 | #include <netconfig.h> |
| 21 | #include <rpc/rpc.h> |
| 22 | #include <rpc/xdr.h> |
| 23 | |
| 24 | static PyObject* xdr_error; |
| 25 | |
| 26 | |
| 27 | |
| 28 | static PyObject* |
| 29 | pack_float(self, args) |
Guido van Rossum | d62d8bc | 1996-08-20 19:47:44 +0000 | [diff] [blame^] | 30 | PyObject* self; |
| 31 | PyObject* args; |
Guido van Rossum | 73363b5 | 1996-08-19 22:58:53 +0000 | [diff] [blame] | 32 | { |
| 33 | XDR xdr; |
| 34 | float value; |
| 35 | union { /* guarantees proper alignment */ |
| 36 | long dummy; |
| 37 | char buffer[4]; |
| 38 | } addr; |
| 39 | PyObject* rtn = NULL; |
| 40 | |
| 41 | if (!PyArg_ParseTuple(args, "f", &value)) |
| 42 | return NULL; |
| 43 | |
Guido van Rossum | d62d8bc | 1996-08-20 19:47:44 +0000 | [diff] [blame^] | 44 | xdr.x_ops = NULL; |
Guido van Rossum | 73363b5 | 1996-08-19 22:58:53 +0000 | [diff] [blame] | 45 | xdrmem_create(&xdr, addr.buffer, 4, XDR_ENCODE); |
Guido van Rossum | d62d8bc | 1996-08-20 19:47:44 +0000 | [diff] [blame^] | 46 | if( xdr.x_ops == NULL ) |
| 47 | PyErr_SetString(xdr_error, "XDR stream initialization failed."); |
| 48 | else if (xdr_float(&xdr, &value)) |
Guido van Rossum | 73363b5 | 1996-08-19 22:58:53 +0000 | [diff] [blame] | 49 | rtn = PyString_FromStringAndSize(addr.buffer, 4); |
| 50 | else |
| 51 | PyErr_SetString(xdr_error, "conversion from float failed"); |
| 52 | |
| 53 | xdr_destroy(&xdr); |
| 54 | return rtn; |
| 55 | } |
| 56 | |
Guido van Rossum | d62d8bc | 1996-08-20 19:47:44 +0000 | [diff] [blame^] | 57 | |
| 58 | |
Guido van Rossum | 73363b5 | 1996-08-19 22:58:53 +0000 | [diff] [blame] | 59 | static PyObject* |
| 60 | pack_double(self, args) |
Guido van Rossum | d62d8bc | 1996-08-20 19:47:44 +0000 | [diff] [blame^] | 61 | PyObject* self; |
| 62 | PyObject* args; |
Guido van Rossum | 73363b5 | 1996-08-19 22:58:53 +0000 | [diff] [blame] | 63 | { |
| 64 | XDR xdr; |
| 65 | double value; |
| 66 | union { /* guarantees proper alignment */ |
| 67 | long dummy; |
| 68 | char buffer[8]; |
| 69 | } addr; |
| 70 | PyObject* rtn = NULL; |
| 71 | |
| 72 | if (!PyArg_ParseTuple(args, "d", &value)) |
| 73 | return NULL; |
| 74 | |
Guido van Rossum | d62d8bc | 1996-08-20 19:47:44 +0000 | [diff] [blame^] | 75 | xdr.x_ops = NULL; |
Guido van Rossum | 73363b5 | 1996-08-19 22:58:53 +0000 | [diff] [blame] | 76 | xdrmem_create(&xdr, addr.buffer, 8, XDR_ENCODE); |
Guido van Rossum | d62d8bc | 1996-08-20 19:47:44 +0000 | [diff] [blame^] | 77 | if( xdr.x_ops == NULL ) |
| 78 | PyErr_SetString(xdr_error, "XDR stream initialization failed."); |
| 79 | else if (xdr_double(&xdr, &value)) |
Guido van Rossum | 73363b5 | 1996-08-19 22:58:53 +0000 | [diff] [blame] | 80 | rtn = PyString_FromStringAndSize(addr.buffer, 8); |
| 81 | else |
| 82 | PyErr_SetString(xdr_error, "conversion from double failed"); |
| 83 | |
| 84 | xdr_destroy(&xdr); |
| 85 | return rtn; |
| 86 | } |
| 87 | |
| 88 | |
| 89 | |
| 90 | static PyObject* |
| 91 | unpack_float(self, args) |
Guido van Rossum | d62d8bc | 1996-08-20 19:47:44 +0000 | [diff] [blame^] | 92 | PyObject* self; |
| 93 | PyObject* args; |
Guido van Rossum | 73363b5 | 1996-08-19 22:58:53 +0000 | [diff] [blame] | 94 | { |
| 95 | XDR xdr; |
| 96 | float value; |
| 97 | char* string; |
| 98 | int strlen; |
| 99 | PyObject* rtn = NULL; |
| 100 | |
| 101 | if (!PyArg_ParseTuple(args, "s#", &string, &strlen)) |
| 102 | return NULL; |
| 103 | |
| 104 | if (strlen != 4) { |
| 105 | PyErr_SetString(PyExc_ValueError, "4 byte string expected"); |
| 106 | return NULL; |
| 107 | } |
| 108 | |
| 109 | /* Python guarantees that the string is 4 byte aligned */ |
Guido van Rossum | d62d8bc | 1996-08-20 19:47:44 +0000 | [diff] [blame^] | 110 | xdr.x_ops = NULL; |
Guido van Rossum | 73363b5 | 1996-08-19 22:58:53 +0000 | [diff] [blame] | 111 | xdrmem_create(&xdr, (caddr_t)string, 4, XDR_DECODE); |
Guido van Rossum | d62d8bc | 1996-08-20 19:47:44 +0000 | [diff] [blame^] | 112 | if( xdr.x_ops == NULL ) |
| 113 | PyErr_SetString(xdr_error, "XDR stream initialization failed."); |
| 114 | else if (xdr_float(&xdr, &value)) |
Guido van Rossum | 73363b5 | 1996-08-19 22:58:53 +0000 | [diff] [blame] | 115 | rtn = Py_BuildValue("f", value); |
| 116 | else |
| 117 | PyErr_SetString(xdr_error, "conversion to float failed"); |
| 118 | |
| 119 | xdr_destroy(&xdr); |
| 120 | return rtn; |
| 121 | } |
| 122 | |
| 123 | |
Guido van Rossum | d62d8bc | 1996-08-20 19:47:44 +0000 | [diff] [blame^] | 124 | |
Guido van Rossum | 73363b5 | 1996-08-19 22:58:53 +0000 | [diff] [blame] | 125 | static PyObject* |
| 126 | unpack_double(self, args) |
Guido van Rossum | d62d8bc | 1996-08-20 19:47:44 +0000 | [diff] [blame^] | 127 | PyObject* self; |
| 128 | PyObject* args; |
Guido van Rossum | 73363b5 | 1996-08-19 22:58:53 +0000 | [diff] [blame] | 129 | { |
| 130 | XDR xdr; |
| 131 | double value; |
| 132 | char* string; |
| 133 | int strlen; |
| 134 | PyObject* rtn; |
| 135 | |
| 136 | if (!PyArg_ParseTuple(args, "s#", &string, &strlen)) |
| 137 | return NULL; |
| 138 | |
| 139 | if (strlen != 8) { |
| 140 | PyErr_SetString(PyExc_ValueError, "8 byte string expected"); |
| 141 | return NULL; |
| 142 | } |
| 143 | |
| 144 | /* Python guarantees that the string is 4 byte aligned */ |
Guido van Rossum | d62d8bc | 1996-08-20 19:47:44 +0000 | [diff] [blame^] | 145 | xdr.x_ops = NULL; |
Guido van Rossum | 73363b5 | 1996-08-19 22:58:53 +0000 | [diff] [blame] | 146 | xdrmem_create(&xdr, (caddr_t)string, 8, XDR_DECODE); |
Guido van Rossum | d62d8bc | 1996-08-20 19:47:44 +0000 | [diff] [blame^] | 147 | if( xdr.x_ops == NULL ) |
| 148 | PyErr_SetString(xdr_error, "XDR stream initialization failed."); |
| 149 | else if (xdr_double(&xdr, &value)) |
Guido van Rossum | 73363b5 | 1996-08-19 22:58:53 +0000 | [diff] [blame] | 150 | rtn = Py_BuildValue("d", value); |
| 151 | else |
| 152 | PyErr_SetString(xdr_error, "conversion to double failed"); |
| 153 | |
| 154 | xdr_destroy(&xdr); |
| 155 | return rtn; |
| 156 | } |
| 157 | |
| 158 | |
| 159 | |
| 160 | static struct PyMethodDef |
| 161 | xdr_methods[] = { |
| 162 | {"pack_float", pack_float, 1}, |
| 163 | {"pack_double", pack_double, 1}, |
| 164 | {"unpack_float", unpack_float, 1}, |
| 165 | {"unpack_double", unpack_double, 1}, |
| 166 | {NULL, NULL, 0} /* sentinel */ |
| 167 | }; |
| 168 | |
| 169 | |
| 170 | |
| 171 | void |
| 172 | init_xdr() |
| 173 | { |
| 174 | PyObject* module; |
| 175 | PyObject* dict; |
| 176 | |
| 177 | module = Py_InitModule("_xdr", xdr_methods); |
| 178 | dict = PyModule_GetDict(module); |
| 179 | |
| 180 | xdr_error = PyString_FromString("_xdr.error"); |
| 181 | PyDict_SetItemString(dict, "error", xdr_error); |
| 182 | } |