Initial commit - from Precise source
[freerdp-ubuntu-pcb-backport.git] / include / freerdp / utils / stream.h
1 /**
2  * FreeRDP: A Remote Desktop Protocol Client
3  * Stream Utils
4  *
5  * Copyright 2009-2011 Jay Sorg
6  * Copyright 2011 Vic Lee
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20
21 #ifndef __STREAM_UTILS_H
22 #define __STREAM_UTILS_H
23
24 #include <string.h>
25 #include <freerdp/api.h>
26 #include <freerdp/types.h>
27
28 struct _STREAM
29 {
30         int size;
31         uint8* p;
32         uint8* data;
33 };
34 typedef struct _STREAM STREAM;
35
36 FREERDP_API STREAM* stream_new(int size);
37 FREERDP_API void stream_free(STREAM* stream);
38
39 #define stream_attach(_s, _buf, _size) do { \
40         _s->size = _size; \
41         _s->data = _buf; \
42         _s->p = _buf; } while (0)
43 #define stream_detach(_s) memset(_s, 0, sizeof(STREAM))
44 #define stream_clear(_s) memset(_s->data, 0, _s->size)
45
46 FREERDP_API void stream_extend(STREAM* stream, int request_size);
47 #define stream_check_size(_s, _n) \
48         while (_s->p - _s->data + (_n) > _s->size) \
49                 stream_extend(_s, _n)
50
51 #define stream_get_pos(_s) (_s->p - _s->data)
52 #define stream_set_pos(_s,_m) _s->p = _s->data + (_m)
53 #define stream_seek(_s,_offset) _s->p += (_offset)
54 #define stream_rewind(_s,_offset) _s->p -= (_offset)
55 #define stream_seal(_s) _s->size = (_s->p - _s->data)
56 #define stream_get_mark(_s,_mark) _mark = _s->p
57 #define stream_set_mark(_s,_mark) _s->p = _mark
58 #define stream_get_head(_s) _s->data
59 #define stream_get_tail(_s) _s->p
60 #define stream_get_length(_s) (_s->p - _s->data)
61 #define stream_get_data(_s) (_s->data)
62 #define stream_get_size(_s) (_s->size)
63 #define stream_get_left(_s) (_s->size - (_s->p - _s->data))
64
65 #define stream_read_uint8(_s, _v) do { _v = *_s->p++; } while (0)
66 #define stream_read_uint16(_s, _v) do { _v = \
67         (uint16)(*_s->p) + \
68         (((uint16)(*(_s->p + 1))) << 8); \
69         _s->p += 2; } while (0)
70 #define stream_read_uint32(_s, _v) do { _v = \
71         (uint32)(*_s->p) + \
72         (((uint32)(*(_s->p + 1))) << 8) + \
73         (((uint32)(*(_s->p + 2))) << 16) + \
74         (((uint32)(*(_s->p + 3))) << 24); \
75         _s->p += 4; } while (0)
76 #define stream_read_uint64(_s, _v) do { _v = \
77         (uint64)(*_s->p) + \
78         (((uint64)(*(_s->p + 1))) << 8) + \
79         (((uint64)(*(_s->p + 2))) << 16) + \
80         (((uint64)(*(_s->p + 3))) << 24) + \
81         (((uint64)(*(_s->p + 4))) << 32) + \
82         (((uint64)(*(_s->p + 5))) << 40) + \
83         (((uint64)(*(_s->p + 6))) << 48) + \
84         (((uint64)(*(_s->p + 7))) << 56); \
85         _s->p += 8; } while (0)
86 #define stream_read(_s, _b, _n) do { \
87         memcpy(_b, (_s->p), (_n)); \
88         _s->p += (_n); \
89         } while (0)
90
91 #define stream_write_uint8(_s, _v) do { \
92         *_s->p++ = (uint8)(_v); } while (0)
93 #define stream_write_uint16(_s, _v) do { \
94         *_s->p++ = (_v) & 0xFF; \
95         *_s->p++ = ((_v) >> 8) & 0xFF; } while (0)
96 #define stream_write_uint32(_s, _v) do { \
97         *_s->p++ = (_v) & 0xFF; \
98         *_s->p++ = ((_v) >> 8) & 0xFF; \
99         *_s->p++ = ((_v) >> 16) & 0xFF; \
100         *_s->p++ = ((_v) >> 24) & 0xFF; } while (0)
101 #define stream_write_uint64(_s, _v) do { \
102         *_s->p++ = (uint64)(_v) & 0xFF; \
103         *_s->p++ = ((uint64)(_v) >> 8) & 0xFF; \
104         *_s->p++ = ((uint64)(_v) >> 16) & 0xFF; \
105         *_s->p++ = ((uint64)(_v) >> 24) & 0xFF; \
106         *_s->p++ = ((uint64)(_v) >> 32) & 0xFF; \
107         *_s->p++ = ((uint64)(_v) >> 40) & 0xFF; \
108         *_s->p++ = ((uint64)(_v) >> 48) & 0xFF; \
109         *_s->p++ = ((uint64)(_v) >> 56) & 0xFF; } while (0)
110 #define stream_write(_s, _b, _n) do { \
111         memcpy(_s->p, (_b), (_n)); \
112         _s->p += (_n); \
113         } while (0)
114 #define stream_write_zero(_s, _n) do { \
115         memset(_s->p, '\0', (_n)); \
116         _s->p += (_n); \
117         } while (0)
118 #define stream_set_byte(_s, _v, _n) do { \
119         memset(_s->p, _v, (_n)); \
120         _s->p += (_n); \
121         } while (0)
122
123 #define stream_peek_uint8(_s, _v) do { _v = *_s->p; } while (0)
124 #define stream_peek_uint16(_s, _v) do { _v = \
125         (uint16)(*_s->p) + \
126         (((uint16)(*(_s->p + 1))) << 8); \
127         } while (0)
128 #define stream_peek_uint32(_s, _v) do { _v = \
129         (uint32)(*_s->p) + \
130         (((uint32)(*(_s->p + 1))) << 8) + \
131         (((uint32)(*(_s->p + 2))) << 16) + \
132         (((uint32)(*(_s->p + 3))) << 24); \
133         } while (0)
134 #define stream_peek_uint64(_s, _v) do { _v = \
135         (uint64)(*_s->p) + \
136         (((uint64)(*(_s->p + 1))) << 8) + \
137         (((uint64)(*(_s->p + 2))) << 16) + \
138         (((uint64)(*(_s->p + 3))) << 24) + \
139         (((uint64)(*(_s->p + 4))) << 32) + \
140         (((uint64)(*(_s->p + 5))) << 40) + \
141         (((uint64)(*(_s->p + 6))) << 48) + \
142         (((uint64)(*(_s->p + 7))) << 56); \
143         } while (0)
144
145 #define stream_seek_uint8(_s)   stream_seek(_s, 1)
146 #define stream_seek_uint16(_s)  stream_seek(_s, 2)
147 #define stream_seek_uint32(_s)  stream_seek(_s, 4)
148 #define stream_seek_uint64(_s)  stream_seek(_s, 8)
149
150 #define stream_read_uint16_be(_s, _v) do { _v = \
151         (((uint16)(*_s->p)) << 8) + \
152         (uint16)(*(_s->p + 1)); \
153         _s->p += 2; } while (0)
154 #define stream_read_uint32_be(_s, _v) do { _v = \
155         (((uint32)(*(_s->p))) << 8) + \
156         (((uint32)(*(_s->p + 1)))) + \
157         (((uint32)(*(_s->p + 2))) << 24) + \
158         (((uint32)(*(_s->p + 3))) << 16); \
159         _s->p += 4; } while (0)
160
161 #define stream_write_uint16_be(_s, _v) do { \
162         *_s->p++ = ((_v) >> 8) & 0xFF; \
163         *_s->p++ = (_v) & 0xFF; } while (0)
164 #define stream_write_uint32_be(_s, _v) do { \
165         stream_write_uint16_be(_s, ((_v) >> 16 & 0xFFFF)); \
166         stream_write_uint16_be(_s, ((_v) & 0xFFFF)); \
167         } while (0)
168
169 #define stream_copy(_dst, _src, _n) do { \
170         memcpy(_dst->p, _src->p, _n); \
171         _dst->p += _n; \
172         _src->p += _n; \
173         } while (0)
174
175 #endif /* __STREAM_UTILS_H */
176