| | 1 | #include <Python.h> |
| | 2 | |
| | 3 | #include "util.h" |
| | 4 | |
| | 5 | #include <cStringIO.h> |
| | 6 | #include <stdio.h> |
| | 7 | #include <structmember.h> |
| | 8 | |
| | 9 | /* Useful Forward Declarations */ |
| | 10 | static PyObject * BufferedByteStream_tell(BufferedByteStream *); |
| | 11 | static PyObject * BufferedByteStream_at_eof(BufferedByteStream *); |
| | 12 | static PyObject * BufferedByteStream_remaining(BufferedByteStream *); |
| | 13 | static PyObject * BufferedByteStream_seek(BufferedByteStream *, PyObject *, PyObject *); |
| | 14 | static PyObject * BufferedByteStream_peek(BufferedByteStream *, PyObject *, PyObject *); |
| | 15 | static PyObject * BufferedByteStream_read(BufferedByteStream *, PyObject *, PyObject *); |
| | 16 | static PyObject * BufferedByteStream_readline(BufferedByteStream *); |
| | 17 | static PyObject * BufferedByteStream_readlines(BufferedByteStream *, PyObject *, PyObject *); |
| | 18 | static PyObject * BufferedByteStream_write(BufferedByteStream *, PyObject *); |
| | 19 | static PyObject * BufferedByteStream_writelines(BufferedByteStream *, PyObject *); |
| | 20 | static PyObject * BufferedByteStream_getvalue(BufferedByteStream *); |
| | 21 | static PyObject * BufferedByteStream_truncate(BufferedByteStream *, PyObject *, PyObject *); |
| | 22 | static PyObject * BufferedByteStream_flush(BufferedByteStream *); |
| | 23 | static PyObject * BufferedByteStream_close(BufferedByteStream *); |
| | 24 | static int BufferedByteStream_init_(BufferedByteStream *, PyObject *, int); |
| | 25 | |
| | 26 | staticforward PyTypeObject BufferedByteStreamType; |
| | 27 | #undef BufferedByteStream_Check |
| | 28 | #define BufferedByteStream_Check(O) ((O)->ob_type==&BufferedByteStreamType) |
| | 29 | |
| | 30 | /** |
| | 31 | * Utility Functions |
| | 32 | **/ |
| | 33 | |
| | 34 | static int |
| | 35 | get_long(PyObject *v, long *p) |
| | 36 | { |
| | 37 | long x; |
| | 38 | |
| | 39 | x = PyLong_AsLong(v); |
| | 40 | if (x == -1 && PyErr_Occurred()) { |
| | 41 | return -1; |
| | 42 | } |
| | 43 | *p = x; |
| | 44 | return 0; |
| | 45 | } |
| | 46 | |
| | 47 | static int |
| | 48 | get_ulong(PyObject *v, unsigned long *p) |
| | 49 | { |
| | 50 | unsigned long x; |
| | 51 | |
| | 52 | x = PyLong_AsUnsignedLong(v); |
| | 53 | if (PyErr_Occurred()) { |
| | 54 | return -1; |
| | 55 | } |
| | 56 | *p = x; |
| | 57 | return 0; |
| | 58 | } |
| | 59 | |
| | 60 | static PyObject * |
| | 61 | unpack_uint(BufferedByteStream *self, const char *buf, long num_bytes) |
| | 62 | { |
| | 63 | unsigned long x; |
| | 64 | const unsigned char *bytes; |
| | 65 | |
| | 66 | x = 0; |
| | 67 | bytes = (const unsigned char *) buf; |
| | 68 | do |
| | 69 | { |
| | 70 | if ( (self->endian==ENDIAN_BIG) || |
| | 71 | (self->endian==ENDIAN_NETWORK) ) |
| | 72 | { |
| | 73 | x = (x<<8) | *bytes++; |
| | 74 | } |
| | 75 | else |
| | 76 | { |
| | 77 | x = (x<<8) | bytes[num_bytes-1]; |
| | 78 | } |
| | 79 | } while (--num_bytes > 0); |
| | 80 | return PyLong_FromUnsignedLong(x); |
| | 81 | } |
| | 82 | |
| | 83 | static PyObject * |
| | 84 | unpack_int(BufferedByteStream *self, const char *buf, long num_bytes) |
| | 85 | { |
| | 86 | long x; |
| | 87 | Py_ssize_t bytes_left; |
| | 88 | const unsigned char *bytes; |
| | 89 | |
| | 90 | x = 0; |
| | 91 | bytes_left = num_bytes; |
| | 92 | bytes = (const unsigned char *) buf; |
| | 93 | do |
| | 94 | { |
| | 95 | if ( (self->endian==ENDIAN_BIG) || |
| | 96 | (self->endian==ENDIAN_NETWORK) ) |
| | 97 | { |
| | 98 | x = (x<<8) | *bytes++; |
| | 99 | } |
| | 100 | else |
| | 101 | { |
| | 102 | x = (x<<8) | bytes[bytes_left-1]; |
| | 103 | } |
| | 104 | } while (--bytes_left > 0); |
| | 105 | /* Extend the sign bit. */ |
| | 106 | if (SIZEOF_LONG > num_bytes) |
| | 107 | { |
| | 108 | x |= -(x & (1L << ((8 *num_bytes) - 1))); |
| | 109 | } |
| | 110 | return PyLong_FromLong(x); |
| | 111 | } |
| | 112 | |
| | 113 | static int |
| | 114 | pack_uint(BufferedByteStream *self, unsigned long x, Py_ssize_t num_bytes) |
| | 115 | { |
| | 116 | char buf[num_bytes]; |
| | 117 | |
| | 118 | if (num_bytes != SIZEOF_LONG) { |
| | 119 | unsigned long maxint = 1; |
| | 120 | maxint <<= (unsigned long)(num_bytes * 8); |
| | 121 | if (x >= maxint) |
| | 122 | { |
| | 123 | PyErr_SetString(PyExc_OverflowError, "integer out of range"); |
| | 124 | return -1; |
| | 125 | } |
| | 126 | } |
| | 127 | Py_ssize_t i=num_bytes; |
| | 128 | while(i>0) |
| | 129 | { |
| | 130 | switch(self->endian) |
| | 131 | { |
| | 132 | case ENDIAN_BIG: |
| | 133 | case ENDIAN_NETWORK: |
| | 134 | buf[--i] = (char)x; |
| | 135 | break; |
| | 136 | case ENDIAN_LITTLE: |
| | 137 | buf[num_bytes-i] = (char)x; |
| | 138 | i--; |
| | 139 | break; |
| | 140 | case ENDIAN_NATIVE: |
| | 141 | buf[num_bytes-i] = ((char *)&x)[num_bytes-i]; |
| | 142 | i--; |
| | 143 | } |
| | 144 | x >>= 8; |
| | 145 | } |
| | 146 | PycStringIO->cwrite(self->buffer, buf, num_bytes); |
| | 147 | return 0; |
| | 148 | } |
| | 149 | |
| | 150 | static int |
| | 151 | pack_int(BufferedByteStream *self, long x, Py_ssize_t num_bytes) |
| | 152 | { |
| | 153 | char buf[num_bytes]; |
| | 154 | |
| | 155 | if (num_bytes != SIZEOF_LONG) { |
| | 156 | long maxint = 1; |
| | 157 | maxint <<= (unsigned long)(num_bytes * 8); |
| | 158 | if (x >= maxint) |
| | 159 | { |
| | 160 | PyErr_SetString(PyExc_OverflowError, "integer out of range"); |
| | 161 | return -1; |
| | 162 | } |
| | 163 | } |
| | 164 | Py_ssize_t i=num_bytes; |
| | 165 | while(i>0) |
| | 166 | { |
| | 167 | switch(self->endian) |
| | 168 | { |
| | 169 | case ENDIAN_BIG: |
| | 170 | case ENDIAN_NETWORK: |
| | 171 | buf[--i] = ((char *)&x)[0]; |
| | 172 | break; |
| | 173 | case ENDIAN_LITTLE: |
| | 174 | buf[num_bytes-i] = ((char *)&x)[0]; |
| | 175 | i--; |
| | 176 | break; |
| | 177 | case ENDIAN_NATIVE: |
| | 178 | buf[num_bytes-i] = ((char *)&x)[num_bytes-i]; |
| | 179 | i--; |
| | 180 | } |
| | 181 | x >>= 8; |
| | 182 | } |
| | 183 | PycStringIO->cwrite(self->buffer, buf, num_bytes); |
| | 184 | return 0; |
| | 185 | } |
| | 186 | |
| | 187 | /** |
| | 188 | * BufferedByteStream methods |
| | 189 | **/ |
| | 190 | static void |
| | 191 | BufferedByteStream_dealloc(BufferedByteStream *self) |
| | 192 | { |
| | 193 | Py_XDECREF(self->buffer); |
| | 194 | self->ob_type->tp_free((PyObject *)self); |
| | 195 | } |
| | 196 | |
| | 197 | static PyObject * |
| | 198 | BufferedByteStream_tell(BufferedByteStream *self) |
| | 199 | { |
| | 200 | if(!self->buffer) { |
| | 201 | PyErr_SetString(PyExc_ValueError, "buffer is closed"); |
| | 202 | return NULL; |
| | 203 | } |
| | 204 | |
| | 205 | return PyObject_CallMethod(self->buffer, "tell", NULL); |
| | 206 | } |
| | 207 | |
| | 208 | static PyObject * |
| | 209 | BufferedByteStream_at_eof(BufferedByteStream *self) |
| | 210 | { |
| | 211 | PyObject *obj_remaining, *result; |
| | 212 | int remaining; |
| | 213 | |
| | 214 | if(!self->buffer) { |
| | 215 | PyErr_SetString(PyExc_ValueError, "buffer is closed"); |
| | 216 | return NULL; |
| | 217 | } |
| | 218 | |
| | 219 | /* at_eof <=> remaining==0 */ |
| | 220 | |
| | 221 | obj_remaining = BufferedByteStream_remaining(self); |
| | 222 | remaining = PyInt_AsLong(obj_remaining); |
| | 223 | Py_XDECREF(obj_remaining); |
| | 224 | |
| | 225 | result = PyBool_FromLong(remaining==0); |
| | 226 | Py_INCREF(result); |
| | 227 | |
| | 228 | return result; |
| | 229 | } |
| | 230 | |
| | 231 | static PyObject * |
| | 232 | BufferedByteStream_remaining(BufferedByteStream *self) |
| | 233 | { |
| | 234 | PyObject *cur_pos, *end_pos, *tmp, *result; |
| | 235 | |
| | 236 | if(!self->buffer) { |
| | 237 | PyErr_SetString(PyExc_ValueError, "buffer is closed"); |
| | 238 | return NULL; |
| | 239 | } |
| | 240 | |
| | 241 | cur_pos = BufferedByteStream_tell(self); |
| | 242 | |
| | 243 | PyObject_CallMethod(self->buffer, "seek", "ii", 0, 2); |
| | 244 | end_pos = BufferedByteStream_tell(self); |
| | 245 | |
| | 246 | tmp = PyObject_CallMethod(self->buffer, "seek", "Oi", cur_pos, 0); |
| | 247 | Py_XDECREF(tmp); |
| | 248 | |
| | 249 | result = PyNumber_Subtract(end_pos, cur_pos); |
| | 250 | Py_XDECREF(end_pos); |
| | 251 | Py_XDECREF(cur_pos); |
| | 252 | |
| | 253 | return result; |
| | 254 | } |
| | 255 | |
| | 256 | static PyObject * |
| | 257 | BufferedByteStream_seek(BufferedByteStream *self, PyObject *args, PyObject *kwargs) |
| | 258 | { |
| | 259 | PyObject *obj_pos = NULL; |
| | 260 | PyObject *obj_mode = NULL; |
| | 261 | static char *kwlist[] = {"pos", "mode", NULL}; |
| | 262 | |
| | 263 | if(!self->buffer) { |
| | 264 | PyErr_SetString(PyExc_ValueError, "buffer is closed"); |
| | 265 | return NULL; |
| | 266 | } |
| | 267 | |
| | 268 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|O", kwlist, &obj_pos, &obj_mode)) |
| | 269 | return NULL; |
| | 270 | |
| | 271 | if (!obj_mode) |
| | 272 | obj_mode = PyInt_FromLong(0); |
| | 273 | |
| | 274 | return PyObject_CallMethod(self->buffer, "seek", "OO", obj_pos, obj_mode); |
| | 275 | } |
| | 276 | |
| | 277 | static PyObject * |
| | 278 | BufferedByteStream_peek(BufferedByteStream *self, PyObject *args, PyObject *kwargs) |
| | 279 | { |
| | 280 | PyObject *obj_size = NULL; |
| | 281 | static char *kwlist[] = {"size", NULL}; |
| | 282 | |
| | 283 | if(!self->buffer) { |
| | 284 | PyErr_SetString(PyExc_ValueError, "buffer is closed"); |
| | 285 | return NULL; |
| | 286 | } |
| | 287 | |
| | 288 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, &obj_size)) |
| | 289 | return NULL; |
| | 290 | |
| | 291 | Py_ssize_t size = 1; |
| | 292 | if (obj_size) |
| | 293 | size = PyInt_AsLong(obj_size); |
| | 294 | |
| | 295 | if (size==-1 && PyErr_Occurred()) |
| | 296 | return NULL; |
| | 297 | |
| | 298 | PyObject *old_pos = BufferedByteStream_tell(self); |
| | 299 | |
| | 300 | char *buf = NULL; |
| | 301 | size = PycStringIO->cread(self->buffer, &buf, size); |
| | 302 | PyObject *result = PyString_FromStringAndSize(buf, size); |
| | 303 | |
| | 304 | PyObject_CallMethod(self->buffer, "seek", "Oi", old_pos, 0); |
| | 305 | Py_XDECREF(old_pos); |
| | 306 | |
| | 307 | return result; |
| | 308 | } |
| | 309 | |
| | 310 | static PyObject * |
| | 311 | BufferedByteStream_read(BufferedByteStream *self, PyObject *args, PyObject *kwargs) |
| | 312 | { |
| | 313 | PyObject *obj_n = NULL; |
| | 314 | static char *kwlist[] = {"n", NULL}; |
| | 315 | Py_ssize_t n = -1; |
| | 316 | char *buf = NULL; |
| | 317 | int chars_received; |
| | 318 | |
| | 319 | if(!self->buffer) { |
| | 320 | PyErr_SetString(PyExc_ValueError, "buffer is closed"); |
| | 321 | return NULL; |
| | 322 | } |
| | 323 | |
| | 324 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, &obj_n)) |
| | 325 | return NULL; |
| | 326 | |
| | 327 | if (obj_n) |
| | 328 | n = PyInt_AsLong(obj_n); |
| | 329 | |
| | 330 | if (n==-1 && PyErr_Occurred()) |
| | 331 | return NULL; |
| | 332 | |
| | 333 | chars_received = PycStringIO->cread(self->buffer, &buf, n); |
| | 334 | if (n>chars_received) |
| | 335 | { |
| | 336 | if(chars_received>0) |
| | 337 | PyErr_SetNone(PyExc_IOError); |
| | 338 | else |
| | 339 | PyErr_SetNone(PyExc_EOFError); |
| | 340 | return NULL; |
| | 341 | } |
| | 342 | return PyString_FromStringAndSize(buf, chars_received); |
| | 343 | } |
| | 344 | |
| | 345 | static PyObject * |
| | 346 | BufferedByteStream_readline(BufferedByteStream *self) |
| | 347 | { |
| | 348 | char *buf = NULL; |
| | 349 | int len; |
| | 350 | |
| | 351 | if(!self->buffer) { |
| | 352 | PyErr_SetString(PyExc_ValueError, "buffer is closed"); |
| | 353 | return NULL; |
| | 354 | } |
| | 355 | |
| | 356 | len = PycStringIO->creadline(self->buffer, &buf); |
| | 357 | return PyString_FromStringAndSize(buf, len); |
| | 358 | } |
| | 359 | |
| | 360 | static PyObject * |
| | 361 | BufferedByteStream_readlines(BufferedByteStream *self, PyObject *args, PyObject *kwargs) |
| | 362 | { |
| | 363 | PyObject *obj_sizehint = NULL; |
| | 364 | static char *kwlist[] = {"sizehint", NULL}; |
| | 365 | Py_ssize_t sizehint = 0; |
| | 366 | |
| | 367 | if(!self->buffer) { |
| | 368 | PyErr_SetString(PyExc_ValueError, "buffer is closed"); |
| | 369 | return NULL; |
| | 370 | } |
| | 371 | |
| | 372 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, &obj_sizehint)) |
| | 373 | return NULL; |
| | 374 | |
| | 375 | if (obj_sizehint) |
| | 376 | sizehint = PyInt_AsLong(obj_sizehint); |
| | 377 | |
| | 378 | if (sizehint==-1 && PyErr_Occurred()) |
| | 379 | return NULL; |
| | 380 | |
| | 381 | return PyObject_CallMethod(self->buffer, "readlines", "i", sizehint); |
| | 382 | } |
| | 383 | |
| | 384 | static PyObject * |
| | 385 | BufferedByteStream_write(BufferedByteStream *self, PyObject *obj) |
| | 386 | { |
| | 387 | char *buffer; |
| | 388 | Py_ssize_t length; |
| | 389 | |
| | 390 | if(!self->buffer) { |
| | 391 | PyErr_SetString(PyExc_ValueError, "buffer is closed"); |
| | 392 | return NULL; |
| | 393 | } |
| | 394 | |
| | 395 | if(PyString_AsStringAndSize(obj, &buffer, &length)) |
| | 396 | return NULL; |
| | 397 | |
| | 398 | PycStringIO->cwrite(self->buffer, buffer, length); |
| | 399 | |
| | 400 | Py_INCREF(Py_None); |
| | 401 | return Py_None; |
| | 402 | } |
| | 403 | |
| | 404 | static PyObject * |
| | 405 | BufferedByteStream_writelines(BufferedByteStream *self, PyObject *iterable) |
| | 406 | { |
| | 407 | PyObject *tmp; |
| | 408 | |
| | 409 | if(!self->buffer) { |
| | 410 | PyErr_SetString(PyExc_ValueError, "buffer is closed"); |
| | 411 | return NULL; |
| | 412 | } |
| | 413 | |
| | 414 | tmp = PyObject_CallMethod(self->buffer, "writelines", "O", iterable); |
| | 415 | Py_XDECREF(tmp); |
| | 416 | |
| | 417 | Py_INCREF(Py_None); |
| | 418 | return Py_None; |
| | 419 | } |
| | 420 | |
| | 421 | static PyObject * |
| | 422 | BufferedByteStream_getvalue(BufferedByteStream *self) |
| | 423 | { |
| | 424 | if(!self->buffer) { |
| | 425 | PyErr_SetString(PyExc_ValueError, "buffer is closed"); |
| | 426 | return NULL; |
| | 427 | } |
| | 428 | |
| | 429 | return PyObject_CallMethod(self->buffer, "getvalue", ""); |
| | 430 | } |
| | 431 | |
| | 432 | static PyObject * |
| | 433 | BufferedByteStream_truncate(BufferedByteStream *self, PyObject *args, PyObject * kwargs) |
| | 434 | { |
| | 435 | PyObject *obj_size = NULL, *tmp; |
| | 436 | static char *kwlist[] = {"size", NULL}; |
| | 437 | |
| | 438 | if(!self->buffer) { |
| | 439 | PyErr_SetString(PyExc_ValueError, "buffer is closed"); |
| | 440 | return NULL; |
| | 441 | } |
| | 442 | |
| | 443 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, &obj_size)) |
| | 444 | return NULL; |
| | 445 | |
| | 446 | if (!obj_size) |
| | 447 | obj_size = PyInt_FromLong(0); |
| | 448 | |
| | 449 | tmp = PyObject_CallMethod(self->buffer, "truncate", "O", obj_size); |
| | 450 | Py_XDECREF(tmp); |
| | 451 | |
| | 452 | Py_INCREF(Py_None); |
| | 453 | return Py_None; |
| | 454 | } |
| | 455 | |
| | 456 | static PyObject * |
| | 457 | BufferedByteStream_flush(BufferedByteStream *self) |
| | 458 | { |
| | 459 | PyObject *tmp; |
| | 460 | |
| | 461 | if(!self->buffer) { |
| | 462 | PyErr_SetString(PyExc_ValueError, "buffer is closed"); |
| | 463 | return NULL; |
| | 464 | } |
| | 465 | |
| | 466 | tmp = PyObject_CallMethod(self->buffer, "flush", NULL); |
| | 467 | Py_XDECREF(tmp); |
| | 468 | |
| | 469 | Py_INCREF(Py_None); |
| | 470 | return Py_None; |
| | 471 | } |
| | 472 | |
| | 473 | static PyObject * |
| | 474 | BufferedByteStream_close(BufferedByteStream *self) |
| | 475 | { |
| | 476 | if(self->buffer) |
| | 477 | { |
| | 478 | PyObject *tmp = PyObject_CallMethod(self->buffer, "close", NULL); |
| | 479 | Py_XDECREF(tmp); |
| | 480 | |
| | 481 | tmp = self->buffer; |
| | 482 | self->buffer = NULL; |
| | 483 | Py_XDECREF(tmp); |
| | 484 | } |
| | 485 | |
| | 486 | Py_INCREF(Py_None); |
| | 487 | return Py_None; |
| | 488 | } |
| | 489 | |
| | 490 | |
| | 491 | /** |
| | 492 | * BufferedByteStream Type Read/Write Methods |
| | 493 | **/ |
| | 494 | static PyObject * |
| | 495 | BufferedByteStream_read_uchar(BufferedByteStream *self) |
| | 496 | { |
| | 497 | char *buf = NULL; |
| | 498 | |
| | 499 | if(!self->buffer) { |
| | 500 | PyErr_SetString(PyExc_ValueError, "buffer is closed"); |
| | 501 | return NULL; |
| | 502 | } |
| | 503 | |
| | 504 | if (PycStringIO->cread(self->buffer, &buf, 1) != 1) |
| | 505 | { |
| | 506 | PyErr_SetNone(PyExc_EOFError); |
| | 507 | return NULL; |
| | 508 | } |
| | 509 | |
| | 510 | return PyInt_FromLong((long) *(unsigned char *)buf); |
| | 511 | } |
| | 512 | |
| | 513 | static PyObject * |
| | 514 | BufferedByteStream_write_uchar(BufferedByteStream *self, PyObject *c) |
| | 515 | { |
| | 516 | unsigned long val; |
| | 517 | |
| | 518 | if(!self->buffer) { |
| | 519 | PyErr_SetString(PyExc_ValueError, "buffer is closed"); |
| | 520 | return NULL; |
| | 521 | } |
| | 522 | |
| | 523 | if (get_ulong(c, &val)) |
| | 524 | return NULL; |
| | 525 | if (val < 0 || val > 255) |
| | 526 | { |
| | 527 | PyErr_SetString(PyExc_OverflowError, "uchar not in range"); |
| | 528 | return NULL; |
| | 529 | } |
| | 530 | |
| | 531 | PycStringIO->cwrite(self->buffer, (char *)&val, 1); |
| | 532 | Py_INCREF(Py_None); |
| | 533 | return Py_None; |
| | 534 | } |
| | 535 | |
| | 536 | static PyObject * |
| | 537 | BufferedByteStream_read_char(BufferedByteStream *self) |
| | 538 | { |
| | 539 | char *buf = NULL; |
| | 540 | |
| | 541 | if(!self->buffer) { |
| | 542 | PyErr_SetString(PyExc_ValueError, "buffer is closed"); |
| | 543 | return NULL; |
| | 544 | } |
| | 545 | |
| | 546 | if(PycStringIO->cread(self->buffer, &buf, 1) != 1) |
| | 547 | { |
| | 548 | PyErr_SetNone(PyExc_EOFError); |
| | 549 | return NULL; |
| | 550 | } |
| | 551 | return PyInt_FromLong((long) *(char *)buf); |
| | 552 | } |
| | 553 | |
| | 554 | static PyObject * |
| | 555 | BufferedByteStream_write_char(BufferedByteStream *self, PyObject *c) |
| | 556 | { |
| | 557 | long val; |
| | 558 | |
| | 559 | if(!self->buffer) { |
| | 560 | PyErr_SetString(PyExc_ValueError, "buffer is closed"); |
| | 561 | return NULL; |
| | 562 | } |
| | 563 | |
| | 564 | if (get_long(c, &val)) |
| | 565 | return NULL; |
| | 566 | if (val < -128 || val > 127) |
| | 567 | { |
| | 568 | PyErr_SetString(PyExc_OverflowError, "char not in range"); |
| | 569 | return NULL; |
| | 570 | } |
| | 571 | |
| | 572 | PycStringIO->cwrite(self->buffer, (char *)&val, 1); |
| | 573 | Py_INCREF(Py_None); |
| | 574 | return Py_None; |
| | 575 | } |
| | 576 | |
| | 577 | static PyObject * |
| | 578 | BufferedByteStream_read_ushort(BufferedByteStream *self) |
| | 579 | { |
| | 580 | char *buf = NULL; |
| | 581 | |
| | 582 | if(!self->buffer) { |
| | 583 | PyErr_SetString(PyExc_ValueError, "buffer is closed"); |
| | 584 | return NULL; |
| | 585 | } |
| | 586 | |
| | 587 | if (PycStringIO->cread(self->buffer, &buf, 2) != 2) |
| | 588 | { |
| | 589 | PyErr_SetNone(PyExc_EOFError); |
| | 590 | return NULL; |
| | 591 | } |
| | 592 | return unpack_uint(self, buf, 2); |
| | 593 | } |
| | 594 | |
| | 595 | static PyObject * |
| | 596 | BufferedByteStream_write_ushort(BufferedByteStream *self, PyObject *c) |
| | 597 | { |
| | 598 | unsigned long val; |
| | 599 | |
| | 600 | if(!self->buffer) { |
| | 601 | PyErr_SetString(PyExc_ValueError, "buffer is closed"); |
| | 602 | return NULL; |
| | 603 | } |
| | 604 | |
| | 605 | if (get_ulong(c, &val)) |
| | 606 | return NULL; |
| | 607 | if (val < 0 || val > 65535) |
| | 608 | { |
| | 609 | PyErr_SetString(PyExc_OverflowError, "ushort not in range"); |
| | 610 | return NULL; |
| | 611 | } |
| | 612 | pack_uint(self, val, 2); |
| | 613 | Py_INCREF(Py_None); |
| | 614 | return Py_None; |
| | 615 | } |
| | 616 | |
| | 617 | static PyObject * |
| | 618 | BufferedByteStream_read_short(BufferedByteStream *self) |
| | 619 | { |
| | 620 | char *buf = NULL; |
| | 621 | |
| | 622 | if(!self->buffer) { |
| | 623 | PyErr_SetString(PyExc_ValueError, "buffer is closed"); |
| | 624 | return NULL; |
| | 625 | } |
| | 626 | |
| | 627 | if (PycStringIO->cread(self->buffer, &buf, 2) != 2) |
| | 628 | { |
| | 629 | PyErr_SetNone(PyExc_EOFError); |
| | 630 | return NULL; |
| | 631 | } |
| | 632 | return unpack_int(self, buf, 2); |
| | 633 | } |
| | 634 | |
| | 635 | static PyObject * |
| | 636 | BufferedByteStream_write_short(BufferedByteStream *self, PyObject *c) |
| | 637 | { |
| | 638 | long val; |
| | 639 | |
| | 640 | if(!self->buffer) { |
| | 641 | PyErr_SetString(PyExc_ValueError, "buffer is closed"); |
| | 642 | return NULL; |
| | 643 | } |
| | 644 | |
| | 645 | if (get_long(c, &val)) |
| | 646 | return NULL; |
| | 647 | if (val < -32768 || val > 32767) |
| | 648 | { |
| | 649 | PyErr_SetString(PyExc_OverflowError, "short not in range"); |
| | 650 | return NULL; |
| | 651 | } |
| | 652 | pack_int(self, val, 2); |
| | 653 | Py_INCREF(Py_None); |
| | 654 | return Py_None; |
| | 655 | } |
| | 656 | |
| | 657 | static PyObject * |
| | 658 | BufferedByteStream_read_ulong(BufferedByteStream *self) |
| | 659 | { |
| | 660 | char *buf = NULL; |
| | 661 | |
| | 662 | if(!self->buffer) { |
| | 663 | PyErr_SetString(PyExc_ValueError, "buffer is closed"); |
| | 664 | return NULL; |
| | 665 | } |
| | 666 | |
| | 667 | if (PycStringIO->cread(self->buffer, &buf, 4) != 4) |
| | 668 | { |
| | 669 | PyErr_SetNone(PyExc_EOFError); |
| | 670 | return NULL; |
| | 671 | } |
| | 672 | |
| | 673 | return unpack_uint(self, buf, 4); |
| | 674 | } |
| | 675 | |
| | 676 | static PyObject * |
| | 677 | BufferedByteStream_write_ulong(BufferedByteStream *self, PyObject *c) |
| | 678 | { |
| | 679 | unsigned long val; |
| | 680 | |
| | 681 | if(!self->buffer) { |
| | 682 | PyErr_SetString(PyExc_ValueError, "buffer is closed"); |
| | 683 | return NULL; |
| | 684 | } |
| | 685 | |
| | 686 | if (get_ulong(c, &val)) |
| | 687 | return NULL; |
| | 688 | pack_uint(self, val, 4); |
| | 689 | Py_INCREF(Py_None); |
| | 690 | return Py_None; |
| | 691 | } |
| | 692 | |
| | 693 | static PyObject * |
| | 694 | BufferedByteStream_read_long(BufferedByteStream *self) |
| | 695 | { |
| | 696 | char *buf = NULL; |
| | 697 | |
| | 698 | if(!self->buffer) { |
| | 699 | PyErr_SetString(PyExc_ValueError, "buffer is closed"); |
| | 700 | return NULL; |
| | 701 | } |
| | 702 | |
| | 703 | PycStringIO->cread(self->buffer, &buf, 4); |
| | 704 | return unpack_int(self, buf, 4); |
| | 705 | } |
| | 706 | |
| | 707 | static PyObject * |
| | 708 | BufferedByteStream_write_long(BufferedByteStream *self, PyObject *c) |
| | 709 | { |
| | 710 | long val; |
| | 711 | |
| | 712 | if(!self->buffer) { |
| | 713 | PyErr_SetString(PyExc_ValueError, "buffer is closed"); |
| | 714 | return NULL; |
| | 715 | } |
| | 716 | |
| | 717 | if (get_long(c, &val)) |
| | 718 | return NULL; |
| | 719 | |
| | 720 | pack_int(self, val, 4); |
| | 721 | Py_INCREF(Py_None); |
| | 722 | return Py_None; |
| | 723 | } |
| | 724 | |
| | 725 | static PyObject * |
| | 726 | BufferedByteStream_read_float(BufferedByteStream *self) |
| | 727 | { |
| | 728 | float x; |
| | 729 | char *buf; |
| | 730 | |
| | 731 | if(!self->buffer) { |
| | 732 | PyErr_SetString(PyExc_ValueError, "buffer is closed"); |
| | 733 | return NULL; |
| | 734 | } |
| | 735 | |
| | 736 | if (PycStringIO->cread(self->buffer, &buf, 4) != 4) |
| | 737 | { |
| | 738 | PyErr_SetNone(PyExc_EOFError); |
| | 739 | return NULL; |
| | 740 | } |
| | 741 | |
| | 742 | switch(self->endian) |
| | 743 | { |
| | 744 | case ENDIAN_NATIVE: |
| | 745 | memcpy((char *)&x, buf, 4); |
| | 746 | break; |
| | 747 | case ENDIAN_NETWORK: |
| | 748 | case ENDIAN_BIG: |
| | 749 | x = _PyFloat_Unpack4((unsigned char *) buf, 0); |
| | 750 | break; |
| | 751 | case ENDIAN_LITTLE: |
| | 752 | x = _PyFloat_Unpack4((unsigned char *) buf, 1); |
| | 753 | break; |
| | 754 | } |
| | 755 | return PyFloat_FromDouble(x); |
| | 756 | } |
| | 757 | |
| | 758 | static PyObject * |
| | 759 | BufferedByteStream_write_float(BufferedByteStream *self, PyObject *c) |
| | 760 | { |
| | 761 | char buf[8]; |
| | 762 | double x; |
| | 763 | |
| | 764 | if(!self->buffer) { |
| | 765 | PyErr_SetString(PyExc_ValueError, "buffer is closed"); |
| | 766 | return NULL; |
| | 767 | } |
| | 768 | |
| | 769 | x = PyFloat_AsDouble(c); |
| | 770 | if(PyErr_Occurred()) |
| | 771 | { |
| | 772 | return NULL; |
| | 773 | } |
| | 774 | |
| | 775 | switch(self->endian) |
| | 776 | { |
| | 777 | case ENDIAN_NATIVE: |
| | 778 | memcpy(buf, (char *)&x, 4); |
| | 779 | break; |
| | 780 | case ENDIAN_NETWORK: |
| | 781 | case ENDIAN_BIG: |
| | 782 | _PyFloat_Pack4(x, (unsigned char *) buf, 0); |
| | 783 | break; |
| | 784 | case ENDIAN_LITTLE: |
| | 785 | _PyFloat_Pack4(x, (unsigned char *) buf, 1); |
| | 786 | break; |
| | 787 | } |
| | 788 | PycStringIO->cwrite(self->buffer, (char *)&buf, 4); |
| | 789 | Py_INCREF(Py_None); |
| | 790 | return Py_None; |
| | 791 | } |
| | 792 | |
| | 793 | static PyObject * |
| | 794 | BufferedByteStream_read_double(BufferedByteStream *self) |
| | 795 | { |
| | 796 | double x; |
| | 797 | char *buf; |
| | 798 | |
| | 799 | if(!self->buffer) { |
| | 800 | PyErr_SetString(PyExc_ValueError, "buffer is closed"); |
| | 801 | return NULL; |
| | 802 | } |
| | 803 | |
| | 804 | if (PycStringIO->cread(self->buffer, &buf, 8) != 8) |
| | 805 | { |
| | 806 | PyErr_SetNone(PyExc_EOFError); |
| | 807 | return NULL; |
| | 808 | } |
| | 809 | |
| | 810 | switch(self->endian) |
| | 811 | { |
| | 812 | case ENDIAN_NATIVE: |
| | 813 | memcpy((char *)&x, buf, 8); |
| | 814 | break; |
| | 815 | case ENDIAN_NETWORK: |
| | 816 | case ENDIAN_BIG: |
| | 817 | x = _PyFloat_Unpack8((unsigned char *) buf, 0); |
| | 818 | break; |
| | 819 | case ENDIAN_LITTLE: |
| | 820 | x = _PyFloat_Unpack8((unsigned char *) buf, 1); |
| | 821 | break; |
| | 822 | } |
| | 823 | return PyFloat_FromDouble(x); |