|
1 | 1 | /* |
2 | 2 | * copyright 2009, James William Pye |
3 | 3 | * http://python.projects.postgresql.org |
4 | | - * |
5 | 4 | */ |
6 | 5 | #define include_typio_functions \ |
7 | 6 | mFUNC(process_tuple, METH_VARARGS, \ |
8 | 7 | "process the items in the second argument " \ |
9 | 8 | "with the corresponding items in the first argument.") \ |
| 9 | + mFUNC(int2_pack, METH_O, "PyInt to serialized, int2") \ |
| 10 | + mFUNC(int2_unpack, METH_O, "PyInt from serialized, int2") \ |
| 11 | + mFUNC(int4_pack, METH_O, "PyInt to serialized, int4") \ |
| 12 | + mFUNC(int4_unpack, METH_O, "PyInt from serialized, int4") \ |
| 13 | + mFUNC(swap_int2_pack, METH_O, "PyInt to swapped serialized, int2") \ |
| 14 | + mFUNC(swap_int2_unpack, METH_O, "PyInt from swapped serialized, int2") \ |
| 15 | + mFUNC(swap_int4_pack, METH_O, "PyInt to swapped serialized, int4") \ |
| 16 | + mFUNC(swap_int4_unpack, METH_O, "PyInt from swapped serialized, int4") \ |
| 17 | + |
| 18 | +#define SHORT_MAX ((2<<14)-1) |
| 19 | +#define SHORT_MIN (-(2<<14)) |
| 20 | + |
| 21 | +/* |
| 22 | + * Define the swap functionality. |
| 23 | + */ |
| 24 | +#define swap2(CP) do{register char c; \ |
| 25 | + c=CP[1];CP[1]=CP[0];CP[0]=c;\ |
| 26 | +}while(0) |
| 27 | +#define swap4(P) do{register char c; \ |
| 28 | + c=P[3];P[3]=P[0];P[0]=c;\ |
| 29 | + c=P[2];P[2]=P[1];P[1]=c;\ |
| 30 | +}while(0) |
| 31 | +#define swap8(P) do{register char c; \ |
| 32 | + c=P[7];P[7]=P[0];P[0]=c;\ |
| 33 | + c=P[6];P[6]=P[1];P[1]=c;\ |
| 34 | + c=P[5];P[5]=P[2];P[2]=c;\ |
| 35 | + c=P[4];P[4]=P[3];P[3]=c;\ |
| 36 | +}while(0) |
| 37 | + |
| 38 | +static long |
| 39 | +swap_long(long l) |
| 40 | +{ |
| 41 | + swap4(((char *) &l)); |
| 42 | + return(l); |
| 43 | +} |
| 44 | + |
| 45 | +static long |
| 46 | +return_long(long l) |
| 47 | +{ |
| 48 | + return(l); |
| 49 | +} |
| 50 | + |
| 51 | +static PyObject * |
| 52 | +int2_pack(PyObject *self, PyObject *arg) |
| 53 | +{ |
| 54 | + long l; |
| 55 | + short s; |
| 56 | + |
| 57 | + l = PyLong_AsLong(arg); |
| 58 | + if (PyErr_Occurred()) |
| 59 | + return(NULL); |
| 60 | + |
| 61 | + if (l > SHORT_MAX || l < SHORT_MIN) |
| 62 | + { |
| 63 | + PyErr_Format(PyExc_OverflowError, |
| 64 | + "long '%d' overflows int2", l |
| 65 | + ); |
| 66 | + return(NULL); |
| 67 | + } |
| 68 | + |
| 69 | + s = (short) l; |
| 70 | + return(PyBytes_FromStringAndSize((const char *) &s, 2)); |
| 71 | +} |
| 72 | +static PyObject * |
| 73 | +swap_int2_pack(PyObject *self, PyObject *arg) |
| 74 | +{ |
| 75 | + long l; |
| 76 | + short s; |
| 77 | + |
| 78 | + l = PyLong_AsLong(arg); |
| 79 | + if (PyErr_Occurred()) |
| 80 | + return(NULL); |
| 81 | + if (l > SHORT_MAX || l < SHORT_MIN) |
| 82 | + { |
| 83 | + PyErr_SetString(PyExc_OverflowError, "long too big or small for int2"); |
| 84 | + return(NULL); |
| 85 | + } |
| 86 | + |
| 87 | + s = (short) l; |
| 88 | + swap2(((char *) &s)); |
| 89 | + return(PyBytes_FromStringAndSize((const char *) &s, 2)); |
| 90 | +} |
| 91 | + |
| 92 | +static PyObject * |
| 93 | +int2_unpack(PyObject *self, PyObject *arg) |
| 94 | +{ |
| 95 | + char *c; |
| 96 | + short *i; |
| 97 | + long l; |
| 98 | + Py_ssize_t len; |
| 99 | + PyObject *rob; |
| 100 | + |
| 101 | + c = PyBytes_AsString(arg); |
| 102 | + if (PyErr_Occurred()) |
| 103 | + return(NULL); |
| 104 | + |
| 105 | + len = PyBytes_Size(arg); |
| 106 | + if (len != 2) |
| 107 | + { |
| 108 | + PyErr_SetString(PyExc_ValueError, "invalid size of data for int2_unpack"); |
| 109 | + return(NULL); |
| 110 | + } |
| 111 | + |
| 112 | + i = (short *) c; |
| 113 | + l = (long) *i; |
| 114 | + rob = PyLong_FromLong(l); |
| 115 | + return(rob); |
| 116 | +} |
| 117 | +static PyObject * |
| 118 | +swap_int2_unpack(PyObject *self, PyObject *arg) |
| 119 | +{ |
| 120 | + char *c; |
| 121 | + short s; |
| 122 | + long l; |
| 123 | + Py_ssize_t len; |
| 124 | + PyObject *rob; |
| 125 | + |
| 126 | + c = PyBytes_AsString(arg); |
| 127 | + if (PyErr_Occurred()) |
| 128 | + return(NULL); |
| 129 | + |
| 130 | + len = PyBytes_Size(arg); |
| 131 | + if (len != 2) |
| 132 | + { |
| 133 | + PyErr_SetString(PyExc_ValueError, "invalid size of data for int2_unpack"); |
| 134 | + return(NULL); |
| 135 | + } |
| 136 | + |
| 137 | + s = *((short *) c); |
| 138 | + swap2(((char *) &s)); |
| 139 | + l = (long) s; |
| 140 | + rob = PyLong_FromLong(l); |
| 141 | + return(rob); |
| 142 | +} |
| 143 | + |
| 144 | +static PyObject * |
| 145 | +int4_pack(PyObject *self, PyObject *arg) |
| 146 | +{ |
| 147 | + long l; |
| 148 | + l = PyLong_AsLong(arg); |
| 149 | + if (PyErr_Occurred()) |
| 150 | + return(NULL); |
| 151 | + return(PyBytes_FromStringAndSize((const char *) &l, 4)); |
| 152 | +} |
| 153 | +static PyObject * |
| 154 | +swap_int4_pack(PyObject *self, PyObject *arg) |
| 155 | +{ |
| 156 | + long l; |
| 157 | + l = PyLong_AsLong(arg); |
| 158 | + if (PyErr_Occurred()) |
| 159 | + return(NULL); |
| 160 | + swap4(((char *) &l)); |
| 161 | + return(PyBytes_FromStringAndSize((const char *) &l, 4)); |
| 162 | +} |
| 163 | + |
| 164 | +static PyObject * |
| 165 | +int4_unpack(PyObject *self, PyObject *arg) |
| 166 | +{ |
| 167 | + char *c; |
| 168 | + long l; |
| 169 | + Py_ssize_t len; |
| 170 | + |
| 171 | + len = PyBytes_Size(arg); |
| 172 | + if (len != 4) |
| 173 | + { |
| 174 | + PyErr_SetString(PyExc_ValueError, "invalid size of data for int4_unpack"); |
| 175 | + return(NULL); |
| 176 | + } |
| 177 | + c = PyBytes_AsString(arg); |
| 178 | + if (PyErr_Occurred()) |
| 179 | + return(NULL); |
| 180 | + l = *((long *) c); |
| 181 | + |
| 182 | + return(PyLong_FromLong(l)); |
| 183 | +} |
| 184 | +static PyObject * |
| 185 | +swap_int4_unpack(PyObject *self, PyObject *arg) |
| 186 | +{ |
| 187 | + char *c; |
| 188 | + long l; |
| 189 | + Py_ssize_t len; |
| 190 | + |
| 191 | + c = PyBytes_AsString(arg); |
| 192 | + if (PyErr_Occurred()) |
| 193 | + return(NULL); |
| 194 | + |
| 195 | + len = PyBytes_Size(arg); |
| 196 | + if (len != 4) |
| 197 | + { |
| 198 | + PyErr_SetString(PyExc_ValueError, "invalid size of data for int4_unpack"); |
| 199 | + return(NULL); |
| 200 | + } |
| 201 | + |
| 202 | + l = *((long *) c); |
| 203 | + swap4(((char *) &l)); |
| 204 | + return(PyLong_FromLong(l)); |
| 205 | +} |
10 | 206 |
|
11 | 207 | /* |
12 | 208 | * process the tuple with the associated callables while |
@@ -165,4 +361,3 @@ process_tuple(PyObject *self, PyObject *args) |
165 | 361 |
|
166 | 362 | return(rob); |
167 | 363 | } |
168 | | - |
|
0 commit comments