diff --git a/include/freerdp/utils/stream.h b/include/freerdp/utils/stream.h index 4c3fe51a5..1511d875b 100644 --- a/include/freerdp/utils/stream.h +++ b/include/freerdp/utils/stream.h @@ -28,28 +28,65 @@ struct _STREAM { uint8 * buffer; uint8 * ptr; - int size; + int capacity; }; -typedef uint8 * STREAM_MARK; - STREAM * -stream_new(int size); +stream_new(int capacity); void stream_free(STREAM * stream); -#define stream_get_mark(_s) _s->ptr -#define stream_set_mark(_s,_m) _s->ptr = _m +void +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_uint16(_s, _v) do { _v = \ +#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) + +#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 + 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_set_uint16(_s, _v) do { \ +#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; } 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 */ diff --git a/libfreerdp-utils/CMakeLists.txt b/libfreerdp-utils/CMakeLists.txt index 8132a8f3f..d7298340b 100644 --- a/libfreerdp-utils/CMakeLists.txt +++ b/libfreerdp-utils/CMakeLists.txt @@ -8,6 +8,7 @@ set(FREERDP_UTILS_SRCS hexdump.c memory.c semaphore.c + stream.c unicode.c) add_library(freerdp-utils SHARED ${FREERDP_UTILS_SRCS}) diff --git a/libfreerdp-utils/stream.c b/libfreerdp-utils/stream.c new file mode 100644 index 000000000..eda073bdc --- /dev/null +++ b/libfreerdp-utils/stream.c @@ -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 +#include +#include + +#include +#include + +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); +}