FreeRDP/winpr/libwinpr/utils/stream.c

192 lines
2.9 KiB
C
Raw Normal View History

/*
* WinPR: Windows Portable Runtime
* Stream Utils
*
* Copyright 2011 Vic Lee
* Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
*
* 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.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <winpr/crt.h>
#include <winpr/stream.h>
PStream PStreamAlloc(size_t size)
{
PStream s;
s = malloc(sizeof(Stream));
if (s != NULL)
{
if (size > 0)
{
2012-07-05 02:33:49 +04:00
s->buffer = (BYTE*) malloc(size);
s->pointer = s->buffer;
s->size = size;
2012-07-05 02:33:49 +04:00
s->length = size;
}
else
{
ZeroMemory(s, sizeof(Stream));
}
}
return s;
}
void StreamAlloc(PStream s, size_t size)
{
if (s != NULL)
{
if (size > 0)
{
2012-07-05 02:33:49 +04:00
s->buffer = (BYTE*) malloc(size);
s->pointer = s->buffer;
s->size = size;
2012-07-05 02:33:49 +04:00
s->length = size;
}
else
{
ZeroMemory(s, sizeof(Stream));
}
}
}
void StreamReAlloc(PStream s, size_t size)
{
if (s != NULL)
{
if (size > 0)
{
size_t old_size;
size_t offset_p;
old_size = s->size;
2012-07-05 02:33:49 +04:00
offset_p = s->pointer - s->buffer;
2012-07-05 02:33:49 +04:00
s->buffer = (BYTE*) realloc(s->buffer, size);
s->size = size;
if (old_size <= size)
2012-07-05 02:33:49 +04:00
s->pointer = s->buffer + offset_p;
else
2012-07-05 02:33:49 +04:00
s->pointer = s->buffer;
}
else
{
if (s->size > 0)
2012-07-05 02:33:49 +04:00
free(s->buffer);
ZeroMemory(s, sizeof(Stream));
}
}
}
2012-07-05 02:33:49 +04:00
PStream PStreamAllocAttach(BYTE* buffer, size_t size)
{
PStream s;
s = malloc(sizeof(Stream));
if (s != NULL)
{
2012-07-05 02:33:49 +04:00
s->buffer = buffer;
s->pointer = s->buffer;
s->size = size;
2012-07-05 02:33:49 +04:00
s->length = size;
}
return s;
}
2012-07-05 02:33:49 +04:00
void StreamAllocAttach(PStream s, BYTE* buffer, size_t size)
{
if (s != NULL)
{
2012-07-05 02:33:49 +04:00
s->buffer = buffer;
s->pointer = s->buffer;
s->size = size;
2012-07-05 02:33:49 +04:00
s->length = size;
}
}
void PStreamFree(PStream s)
{
if (s != NULL)
{
2012-07-05 02:33:49 +04:00
if (s->buffer != NULL)
free(s->buffer);
free(s);
}
}
void StreamFree(PStream s)
{
if (s != NULL)
{
2012-07-05 02:33:49 +04:00
if (s->buffer != NULL)
free(s->buffer);
}
}
void PStreamFreeDetach(PStream s)
{
if (s != NULL)
{
2012-07-05 02:33:49 +04:00
s->buffer = NULL;
s->pointer = s->buffer;
s->size = 0;
2012-07-05 02:33:49 +04:00
s->length = 0;
free(s);
}
}
void StreamFreeDetach(PStream s)
{
if (s != NULL)
{
2012-07-05 02:33:49 +04:00
s->buffer = NULL;
s->pointer = s->buffer;
s->size = 0;
2012-07-05 02:33:49 +04:00
s->length = 0;
}
}
2012-07-05 02:33:49 +04:00
void StreamAttach(PStream s, BYTE* buffer, size_t size)
{
if (s != NULL)
{
2012-07-05 02:33:49 +04:00
s->buffer = buffer;
s->pointer = s->buffer;
s->size = size;
2012-07-05 02:33:49 +04:00
s->length = size;
}
}
void StreamDetach(PStream s)
{
if (s != NULL)
{
2012-07-05 02:33:49 +04:00
s->buffer = NULL;
s->pointer = s->buffer;
s->size = 0;
2012-07-05 02:33:49 +04:00
s->length = 0;
}
}