2011-07-01 03:25:11 +04:00
|
|
|
/**
|
|
|
|
* FreeRDP: A Remote Desktop Protocol Client
|
2011-07-01 12:06:53 +04:00
|
|
|
* Stream Utils
|
2011-07-01 03:25:11 +04:00
|
|
|
*
|
|
|
|
* Copyright 2009-2011 Jay Sorg
|
2011-07-01 12:06:53 +04:00
|
|
|
* Copyright 2011 Vic Lee
|
2011-07-01 03:25:11 +04:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __STREAM_UTILS_H
|
|
|
|
#define __STREAM_UTILS_H
|
|
|
|
|
2011-07-01 12:06:53 +04:00
|
|
|
#include <freerdp/types/base.h>
|
2011-07-01 03:25:11 +04:00
|
|
|
|
2011-07-01 12:06:53 +04:00
|
|
|
struct _STREAM
|
|
|
|
{
|
|
|
|
uint8 * ptr;
|
2011-07-02 22:40:03 +04:00
|
|
|
uint8 * buffer;
|
2011-07-02 19:38:50 +04:00
|
|
|
int capacity;
|
2011-07-01 12:06:53 +04:00
|
|
|
};
|
2011-07-02 22:40:03 +04:00
|
|
|
typedef struct _STREAM STREAM;
|
2011-07-01 12:06:53 +04:00
|
|
|
|
|
|
|
STREAM *
|
2011-07-02 19:38:50 +04:00
|
|
|
stream_new(int capacity);
|
2011-07-01 12:06:53 +04:00
|
|
|
void
|
|
|
|
stream_free(STREAM * stream);
|
|
|
|
|
2011-07-02 19:38:50 +04:00
|
|
|
void
|
2011-07-02 21:58:23 +04:00
|
|
|
stream_extend(STREAM * stream);
|
2011-07-02 19:38:50 +04:00
|
|
|
#define stream_check_capacity(_s,_n) \
|
|
|
|
while (_s->ptr - _s->buffer + (_n) > _s->capacity) \
|
2011-07-02 21:58:23 +04:00
|
|
|
stream_extend(_s)
|
2011-07-02 19:38:50 +04:00
|
|
|
|
|
|
|
#define stream_get_pos(_s) (_s->ptr - _s->buffer)
|
|
|
|
#define stream_set_pos(_s,_m) _s->ptr = _s->buffer + (_m)
|
|
|
|
#define stream_seek(_s,_offset) _s->ptr += (_offset)
|
2011-07-03 12:37:36 +04:00
|
|
|
#define stream_get_head(_s) _s->buffer
|
|
|
|
#define stream_get_tail(_s) _s->ptr
|
2011-07-01 12:06:53 +04:00
|
|
|
|
2011-07-02 19:38:50 +04:00
|
|
|
#define stream_read_uint8(_s, _v) do { _v = *_s->ptr++; } while (0)
|
|
|
|
#define stream_read_uint16(_s, _v) do { _v = \
|
2011-07-01 12:06:53 +04:00
|
|
|
(uint16)(*_s->ptr) + \
|
2011-07-02 19:38:50 +04:00
|
|
|
(((uint16)(*(_s->ptr + 1))) << 8); \
|
|
|
|
_s->ptr += 2; } while (0)
|
|
|
|
#define stream_read_uint32(_s, _v) do { _v = \
|
|
|
|
(uint32)(*_s->ptr) + \
|
|
|
|
(((uint32)(*(_s->ptr + 1))) << 8) + \
|
|
|
|
(((uint32)(*(_s->ptr + 2))) << 16) + \
|
|
|
|
(((uint32)(*(_s->ptr + 3))) << 24); \
|
|
|
|
_s->ptr += 4; } while (0)
|
|
|
|
#define stream_read_uint64(_s, _v) do { _v = \
|
|
|
|
(uint64)(*_s->ptr) + \
|
|
|
|
(((uint64)(*(_s->ptr + 1))) << 8) + \
|
|
|
|
(((uint64)(*(_s->ptr + 2))) << 16) + \
|
|
|
|
(((uint64)(*(_s->ptr + 3))) << 24) + \
|
|
|
|
(((uint64)(*(_s->ptr + 4))) << 32) + \
|
|
|
|
(((uint64)(*(_s->ptr + 5))) << 40) + \
|
|
|
|
(((uint64)(*(_s->ptr + 6))) << 48) + \
|
|
|
|
(((uint64)(*(_s->ptr + 7))) << 56); \
|
|
|
|
_s->ptr += 8; } while (0)
|
2011-07-01 12:06:53 +04:00
|
|
|
|
2011-07-02 19:38:50 +04:00
|
|
|
#define stream_write_uint8(_s, _v) do { \
|
|
|
|
*_s->ptr++ = (uint8)(_v); } while (0)
|
|
|
|
#define stream_write_uint16(_s, _v) do { \
|
|
|
|
*_s->ptr++ = (_v) & 0xFF; \
|
|
|
|
*_s->ptr++ = ((_v) >> 8) & 0xFF; } while (0)
|
|
|
|
#define stream_write_uint32(_s, _v) do { \
|
|
|
|
*_s->ptr++ = (_v) & 0xFF; \
|
|
|
|
*_s->ptr++ = ((_v) >> 8) & 0xFF; \
|
|
|
|
*_s->ptr++ = ((_v) >> 16) & 0xFF; \
|
|
|
|
*_s->ptr++ = ((_v) >> 24) & 0xFF; } while (0)
|
|
|
|
#define stream_write_uint64(_s, _v) do { \
|
2011-07-01 12:06:53 +04:00
|
|
|
*_s->ptr++ = (_v) & 0xFF; \
|
2011-07-02 19:38:50 +04:00
|
|
|
*_s->ptr++ = ((_v) >> 8) & 0xFF; \
|
|
|
|
*_s->ptr++ = ((_v) >> 16) & 0xFF; \
|
|
|
|
*_s->ptr++ = ((_v) >> 24) & 0xFF; \
|
|
|
|
*_s->ptr++ = ((_v) >> 32) & 0xFF; \
|
|
|
|
*_s->ptr++ = ((_v) >> 40) & 0xFF; \
|
|
|
|
*_s->ptr++ = ((_v) >> 48) & 0xFF; \
|
|
|
|
*_s->ptr++ = ((_v) >> 56) & 0xFF; } while (0)
|
2011-07-03 13:07:32 +04:00
|
|
|
#define stream_write_buffer(_s, _b, _n) do { \
|
|
|
|
memcpy(_s->ptr, (_b), (_n)); \
|
|
|
|
_s->ptr += (_n); \
|
|
|
|
} while (0)
|
2011-07-01 03:25:11 +04:00
|
|
|
|
2011-07-02 22:40:03 +04:00
|
|
|
#define stream_peek_uint8(_s, _v) do { _v = *_s->ptr; } while (0)
|
|
|
|
#define stream_peek_uint16(_s, _v) do { _v = \
|
|
|
|
(uint16)(*_s->ptr) + \
|
|
|
|
(((uint16)(*(_s->ptr + 1))) << 8); \
|
|
|
|
} while (0)
|
|
|
|
#define stream_peek_uint32(_s, _v) do { _v = \
|
|
|
|
(uint32)(*_s->ptr) + \
|
|
|
|
(((uint32)(*(_s->ptr + 1))) << 8) + \
|
|
|
|
(((uint32)(*(_s->ptr + 2))) << 16) + \
|
|
|
|
(((uint32)(*(_s->ptr + 3))) << 24); \
|
|
|
|
} while (0)
|
|
|
|
#define stream_peek_uint64(_s, _v) do { _v = \
|
|
|
|
(uint64)(*_s->ptr) + \
|
|
|
|
(((uint64)(*(_s->ptr + 1))) << 8) + \
|
|
|
|
(((uint64)(*(_s->ptr + 2))) << 16) + \
|
|
|
|
(((uint64)(*(_s->ptr + 3))) << 24) + \
|
|
|
|
(((uint64)(*(_s->ptr + 4))) << 32) + \
|
|
|
|
(((uint64)(*(_s->ptr + 5))) << 40) + \
|
|
|
|
(((uint64)(*(_s->ptr + 6))) << 48) + \
|
|
|
|
(((uint64)(*(_s->ptr + 7))) << 56); \
|
|
|
|
} while (0)
|
|
|
|
|
2011-07-03 14:01:37 +04:00
|
|
|
#define stream_read_uint16_be(_s, _v) do { _v = \
|
|
|
|
(((uint16)(*_s->ptr)) << 8) + \
|
|
|
|
(uint16)(*(_s->ptr + 1)); \
|
|
|
|
_s->ptr += 2; } while (0)
|
|
|
|
|
|
|
|
#define stream_write_uint16_be(_s, _v) do { \
|
|
|
|
*_s->ptr++ = ((_v) >> 8) & 0xFF; \
|
|
|
|
*_s->ptr++ = (_v) & 0xFF; } while (0)
|
|
|
|
|
2011-07-01 03:25:11 +04:00
|
|
|
#endif /* __STREAM_UTILS_H */
|
|
|
|
|