@@ -40,7 +40,6 @@ cdef class OutputStream(object):
4040 self .pos += blen
4141
4242 cpdef write_byte(self , unsigned char val):
43- assert 0 <= val <= 0xFF
4443 if self .size <= self .pos:
4544 self .extend(1 )
4645 self .data[self .pos] = val
@@ -59,6 +58,33 @@ cdef class OutputStream(object):
5958 if not v:
6059 break
6160
61+ cpdef write_bigendian_int64(self , libc.stdint.int64_t signed_v):
62+ cdef libc.stdint.uint64_t v = signed_v
63+ if self .size < self .pos - 8 :
64+ self .extend(8 )
65+ self .data[self .pos ] = < unsigned char > (v >> 56 )
66+ self .data[self .pos + 1 ] = < unsigned char > (v >> 48 )
67+ self .data[self .pos + 2 ] = < unsigned char > (v >> 40 )
68+ self .data[self .pos + 3 ] = < unsigned char > (v >> 32 )
69+ self .data[self .pos + 4 ] = < unsigned char > (v >> 24 )
70+ self .data[self .pos + 5 ] = < unsigned char > (v >> 16 )
71+ self .data[self .pos + 6 ] = < unsigned char > (v >> 8 )
72+ self .data[self .pos + 7 ] = < unsigned char > (v )
73+ self .pos += 8
74+
75+ cpdef write_bigendian_int32(self , libc.stdint.int32_t signed_v):
76+ cdef libc.stdint.uint32_t v = signed_v
77+ if self .size < self .pos - 4 :
78+ self .extend(4 )
79+ self .data[self .pos ] = < unsigned char > (v >> 24 )
80+ self .data[self .pos + 1 ] = < unsigned char > (v >> 16 )
81+ self .data[self .pos + 2 ] = < unsigned char > (v >> 8 )
82+ self .data[self .pos + 3 ] = < unsigned char > (v )
83+ self .pos += 4
84+
85+ cpdef write_bigendian_double(self , double d):
86+ self .write_bigendian_int64((< libc.stdint.int64_t* >< char * > & d)[0 ])
87+
6288 cpdef bytes get(self ):
6389 return self .data[:self .pos]
6490
@@ -111,3 +137,25 @@ cdef class InputStream(object):
111137 if not (byte & 0x80 ):
112138 break
113139 return result
140+
141+ cpdef libc.stdint.int64_t read_bigendian_int64(self ) except ? - 1 :
142+ self .pos += 8
143+ return (< unsigned char > self .allc[self .pos - 1 ]
144+ | < libc.stdint.uint64_t>< unsigned char > self .allc[self .pos - 2 ] << 8
145+ | < libc.stdint.uint64_t>< unsigned char > self .allc[self .pos - 3 ] << 16
146+ | < libc.stdint.uint64_t>< unsigned char > self .allc[self .pos - 4 ] << 24
147+ | < libc.stdint.uint64_t>< unsigned char > self .allc[self .pos - 5 ] << 32
148+ | < libc.stdint.uint64_t>< unsigned char > self .allc[self .pos - 6 ] << 40
149+ | < libc.stdint.uint64_t>< unsigned char > self .allc[self .pos - 7 ] << 48
150+ | < libc.stdint.uint64_t>< unsigned char > self .allc[self .pos - 8 ] << 56 )
151+
152+ cpdef libc.stdint.int32_t read_bigendian_int32(self ) except ? - 1 :
153+ self .pos += 4
154+ return (< unsigned char > self .allc[self .pos - 1 ]
155+ | < libc.stdint.uint32_t>< unsigned char > self .allc[self .pos - 2 ] << 8
156+ | < libc.stdint.uint32_t>< unsigned char > self .allc[self .pos - 3 ] << 16
157+ | < libc.stdint.uint32_t>< unsigned char > self .allc[self .pos - 4 ] << 24 )
158+
159+ cpdef double read_bigendian_double(self ) except ? - 1 :
160+ cdef libc.stdint.int64_t as_long = self .read_bigendian_int64()
161+ return (< double * >< char * > & as_long)[0 ]
0 commit comments