Ticket #225: cpyamf.diff

File cpyamf.diff, 44.1 KB (added by gerard, 3 months ago)

First attempt at cpyamf

  • cpyamf/util.h

     
     1#ifndef CPYAMF_UTIL_H 
     2#define CPYAMF_UTIL_H 
     3 
     4#ifdef __cpluscplus 
     5extern "C" { 
     6#endif 
     7/** 
     8 * 
     9 * util.h 
     10 * 
     11 * Defines the BufferedByteStream. 
     12 * 
     13 **/ 
     14#include <Python.h> 
     15 
     16#define CPYAMF_UTIL_IMPORT                                                   \ 
     17        cPyAmf_BufferedByteStream = (struct cPyAmf_BufferedByteStream_CAPI*)     \ 
     18                                PyCObject_Import("cpyamf.util",              \ 
     19                                                 "BufferedByteStream_CAPI"); 
     20 
     21typedef struct { 
     22    PyObject_HEAD 
     23    PyObject* buffer; /* cStringIO buffer */ 
     24    char endian; 
     25} BufferedByteStream; 
     26 
     27static struct cPyAmf_BufferedByteStream_CAPI { 
     28        // TODO: Add more methods here as deemed necessary. 
     29        int (*init)(PyObject*, PyObject*, int); /* (self, buf_obj, rewind) */ 
     30        PyTypeObject* Type; 
     31} *cPyAmf_BufferedByteStream; 
     32 
     33 
     34#define ENDIAN_NETWORK '!' 
     35#define ENDIAN_NATIVE '@' 
     36#define ENDIAN_LITTLE '<' 
     37#define ENDIAN_BIG '>' 
     38 
     39 
     40 
     41#define BufferedByteStream_Check(O) ((O)->ob_type==cPyAmf_BufferedByteStream->Type) 
     42#ifdef __cpluscplus 
     43} 
     44#endif 
     45#endif /* !CPYAMF_UTIL_H */ 
  • cpyamf/util.c

     
     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 */ 
     10static PyObject * BufferedByteStream_tell(BufferedByteStream *); 
     11static PyObject * BufferedByteStream_at_eof(BufferedByteStream *); 
     12static PyObject * BufferedByteStream_remaining(BufferedByteStream *); 
     13static PyObject * BufferedByteStream_seek(BufferedByteStream *, PyObject *, PyObject *); 
     14static PyObject * BufferedByteStream_peek(BufferedByteStream *, PyObject *, PyObject *); 
     15static PyObject * BufferedByteStream_read(BufferedByteStream *, PyObject *, PyObject *); 
     16static PyObject * BufferedByteStream_readline(BufferedByteStream *); 
     17static PyObject * BufferedByteStream_readlines(BufferedByteStream *, PyObject *, PyObject *); 
     18static PyObject * BufferedByteStream_write(BufferedByteStream *, PyObject *); 
     19static PyObject * BufferedByteStream_writelines(BufferedByteStream *, PyObject *); 
     20static PyObject * BufferedByteStream_getvalue(BufferedByteStream *); 
     21static PyObject * BufferedByteStream_truncate(BufferedByteStream *, PyObject *, PyObject *); 
     22static PyObject * BufferedByteStream_flush(BufferedByteStream *); 
     23static PyObject * BufferedByteStream_close(BufferedByteStream *); 
     24static int BufferedByteStream_init_(BufferedByteStream *, PyObject *, int); 
     25 
     26staticforward PyTypeObject BufferedByteStreamType; 
     27#undef BufferedByteStream_Check 
     28#define BufferedByteStream_Check(O) ((O)->ob_type==&BufferedByteStreamType) 
     29 
     30/** 
     31 * Utility Functions 
     32 **/ 
     33 
     34static int 
     35get_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 
     47static int 
     48get_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 
     60static PyObject * 
     61unpack_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 
     83static PyObject * 
     84unpack_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 
     113static int 
     114pack_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 
     150static int 
     151pack_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 **/ 
     190static void 
     191BufferedByteStream_dealloc(BufferedByteStream *self) 
     192{ 
     193    Py_XDECREF(self->buffer); 
     194    self->ob_type->tp_free((PyObject *)self); 
     195} 
     196 
     197static PyObject * 
     198BufferedByteStream_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 
     208static PyObject * 
     209BufferedByteStream_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 
     231static PyObject * 
     232BufferedByteStream_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 
     256static PyObject * 
     257BufferedByteStream_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 
     277static PyObject * 
     278BufferedByteStream_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 
     310static PyObject * 
     311BufferedByteStream_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 
     345static PyObject * 
     346BufferedByteStream_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 
     360static PyObject * 
     361BufferedByteStream_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 
     384static PyObject * 
     385BufferedByteStream_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 
     404static PyObject * 
     405BufferedByteStream_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 
     421static PyObject * 
     422BufferedByteStream_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 
     432static PyObject * 
     433BufferedByteStream_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 
     456static PyObject * 
     457BufferedByteStream_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 
     473static PyObject * 
     474BufferedByteStream_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 **/ 
     494static PyObject * 
     495BufferedByteStream_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 
     513static PyObject * 
     514BufferedByteStream_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 
     536static PyObject * 
     537BufferedByteStream_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 
     554static PyObject * 
     555BufferedByteStream_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 
     577static PyObject * 
     578BufferedByteStream_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 
     595static PyObject * 
     596BufferedByteStream_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 
     617static PyObject * 
     618BufferedByteStream_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 
     635static PyObject * 
     636BufferedByteStream_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 
     657static PyObject * 
     658BufferedByteStream_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 
     676static PyObject * 
     677BufferedByteStream_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 
     693static PyObject * 
     694BufferedByteStream_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 
     707static PyObject * 
     708BufferedByteStream_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 
     725static PyObject * 
     726BufferedByteStream_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 
     758static PyObject * 
     759BufferedByteStream_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 
     793static PyObject * 
     794BufferedByteStream_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); </