libfreerdp-utils: implement stream module.

This commit is contained in:
Vic Lee 2011-07-02 23:38:50 +08:00
parent cc6598de8f
commit 8c161b8a20
3 changed files with 113 additions and 12 deletions

View File

@ -28,28 +28,65 @@ struct _STREAM
{ {
uint8 * buffer; uint8 * buffer;
uint8 * ptr; uint8 * ptr;
int size; int capacity;
}; };
typedef uint8 * STREAM_MARK;
STREAM * STREAM *
stream_new(int size); stream_new(int capacity);
void void
stream_free(STREAM * stream); stream_free(STREAM * stream);
#define stream_get_mark(_s) _s->ptr void
#define stream_set_mark(_s,_m) _s->ptr = _m stream_increase_capacity(STREAM * stream);
#define stream_check_capacity(_s,_n) \
while (_s->ptr - _s->buffer + (_n) > _s->capacity) \
stream_increase_capacity(_s)
#define stream_get_uint8(_s, _v) do { _v = *_s->ptr++; } while (0) #define stream_get_pos(_s) (_s->ptr - _s->buffer)
#define stream_get_uint16(_s, _v) do { _v = \ #define stream_set_pos(_s,_m) _s->ptr = _s->buffer + (_m)
#define stream_seek(_s,_offset) _s->ptr += (_offset)
#define stream_read_uint8(_s, _v) do { _v = *_s->ptr++; } while (0)
#define stream_read_uint16(_s, _v) do { _v = \
(uint16)(*_s->ptr) + \ (uint16)(*_s->ptr) + \
(((uint16)(*(_s->ptr + 1))) << 8); } while (0) (((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)
#define stream_set_uint8(_s, _v) do { *_s->ptr++ = (uint8)(_v); } while (0) #define stream_write_uint8(_s, _v) do { \
#define stream_set_uint16(_s, _v) do { \ *_s->ptr++ = (uint8)(_v); } while (0)
#define stream_write_uint16(_s, _v) do { \
*_s->ptr++ = (_v) & 0xFF; \ *_s->ptr++ = (_v) & 0xFF; \
*_s->ptr++ = (_v) >> 8; } while (0) *_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 { \
*_s->ptr++ = (_v) & 0xFF; \
*_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)
#endif /* __STREAM_UTILS_H */ #endif /* __STREAM_UTILS_H */

View File

@ -8,6 +8,7 @@ set(FREERDP_UTILS_SRCS
hexdump.c hexdump.c
memory.c memory.c
semaphore.c semaphore.c
stream.c
unicode.c) unicode.c)
add_library(freerdp-utils SHARED ${FREERDP_UTILS_SRCS}) add_library(freerdp-utils SHARED ${FREERDP_UTILS_SRCS})

63
libfreerdp-utils/stream.c Normal file
View File

@ -0,0 +1,63 @@
/**
* FreeRDP: A Remote Desktop Protocol Client
* Stream Utils
*
* Copyright 2011 Vic Lee
*
* 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.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <freerdp/utils/memory.h>
#include <freerdp/utils/stream.h>
STREAM *
stream_new(int capacity)
{
STREAM * stream;
stream = (STREAM *) xmalloc(sizeof(STREAM));
if (stream != NULL)
{
capacity = capacity > 0 ? capacity : 0x400;
stream->buffer = (uint8 *) xmalloc(capacity);
stream->ptr = stream->buffer;
stream->capacity = capacity;
}
return stream;
}
void
stream_free(STREAM * stream)
{
if (stream != NULL)
{
xfree(stream->buffer);
xfree(stream);
}
}
void
stream_increase_capacity(STREAM * stream)
{
int pos;
pos = stream_get_pos(stream);
stream->capacity <<= 1;
stream->buffer = (uint8 *) realloc(stream->buffer, stream->capacity);
stream_set_pos(stream, pos);
}