FreeRDP/libfreerdp-utils/stream.c

77 lines
1.7 KiB
C
Raw Normal View History

/**
* 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 size)
{
STREAM* stream;
2011-07-13 18:23:55 +04:00
stream = xnew(STREAM);
if (stream != NULL)
{
if (size != 0)
{
size = size > 0 ? size : 0x400;
2011-10-05 00:05:09 +04:00
stream->data = (uint8*) xzalloc(size);
stream->p = stream->data;
stream->size = size;
}
}
return stream;
}
void stream_free(STREAM* stream)
{
if (stream != NULL)
{
2012-01-14 02:11:34 +04:00
if (stream->data != NULL)
xfree(stream->data);
xfree(stream);
}
}
void stream_extend(STREAM* stream, int request_size)
{
2011-10-05 00:05:09 +04:00
int pos;
int original_size;
int increased_size;
pos = stream_get_pos(stream);
original_size = stream->size;
increased_size = (request_size > original_size ? request_size : original_size);
stream->size += increased_size;
2012-02-02 06:11:46 +04:00
if (original_size == 0)
stream->data = (uint8*) xmalloc(stream->size);
else
stream->data = (uint8*) xrealloc(stream->data, stream->size);
memset(stream->data + original_size, 0, increased_size);
stream_set_pos(stream, pos);
}