Initial commit - from Precise source
[freerdp-ubuntu-pcb-backport.git] / libfreerdp-utils / stream.c
1 /**
2  * FreeRDP: A Remote Desktop Protocol Client
3  * Stream Utils
4  *
5  * Copyright 2011 Vic Lee
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include <freerdp/utils/memory.h>
25 #include <freerdp/utils/stream.h>
26
27 STREAM* stream_new(int size)
28 {
29         STREAM* stream;
30
31         stream = xnew(STREAM);
32
33         if (stream != NULL)
34         {
35                 if (size != 0)
36                 {
37                         size = size > 0 ? size : 0x400;
38                         stream->data = (uint8*) xzalloc(size);
39                         stream->p = stream->data;
40                         stream->size = size;
41                 }
42         }
43
44         return stream;
45 }
46
47 void stream_free(STREAM* stream)
48 {
49         if (stream != NULL)
50         {
51                 if (stream->data != NULL)
52                         xfree(stream->data);
53
54                 xfree(stream);
55         }
56 }
57
58 void stream_extend(STREAM* stream, int request_size)
59 {
60         int pos;
61         int original_size;
62         int increased_size;
63
64         pos = stream_get_pos(stream);
65         original_size = stream->size;
66         increased_size = (request_size > original_size ? request_size : original_size);
67         stream->size += increased_size;
68
69         if (original_size == 0)
70                 stream->data = (uint8*) xmalloc(stream->size);
71         else
72                 stream->data = (uint8*) xrealloc(stream->data, stream->size);
73
74         memset(stream->data + original_size, 0, increased_size);
75         stream_set_pos(stream, pos);
76 }