ia64/xen-unstable

view tools/vtpm_manager/util/buffer.c @ 8977:f84d5cdd9895

Clean up segment selector fixup and validation.

Signed-off-by: Keir Fraser <keir@xensource.com>
author kaf24@firebug.cl.cam.ac.uk
date Thu Feb 23 14:43:45 2006 +0100 (2006-02-23)
parents 06d84bf87159
children 5014fd2b5c5a
line source
1 // ===================================================================
2 //
3 // Copyright (c) 2005, Intel Corp.
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions
8 // are met:
9 //
10 // * Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
12 // * Redistributions in binary form must reproduce the above
13 // copyright notice, this list of conditions and the following
14 // disclaimer in the documentation and/or other materials provided
15 // with the distribution.
16 // * Neither the name of Intel Corporation nor the names of its
17 // contributors may be used to endorse or promote products derived
18 // from this software without specific prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31 // OF THE POSSIBILITY OF SUCH DAMAGE.
32 // ===================================================================
35 #include <stdarg.h>
36 #include <string.h>
37 #include <stdlib.h>
38 #include <stdio.h>
39 #include <sys/param.h>
41 #include "tcg.h"
42 #include "bsg.h"
43 #include "buffer.h"
45 static TPM_RESULT buffer_priv_realloc (buffer_t * buf, tpm_size_t newsize);
47 //
48 // buffer functions!
49 //
51 TPM_RESULT buffer_init (buffer_t * buf, tpm_size_t initsize, const BYTE* initval) {
52 if (initsize == 0) {
53 memset(buf, 0, sizeof(*buf));
54 return TPM_SUCCESS;
55 }
58 buf->bytes = (BYTE*) malloc (initsize);
59 if (buf->bytes == NULL)
60 return TPM_RESOURCES;
62 buf->size = initsize;
63 buf->alloc_size = initsize;
65 if (initval)
66 memcpy (buf->bytes, initval, initsize);
68 buf->is_owner = TRUE;
70 return TPM_SUCCESS;
71 }
73 TPM_RESULT buffer_init_convert (buffer_t * buf, tpm_size_t initsize, BYTE* initval) {
75 buf->size = initsize;
76 buf->alloc_size = initsize;
77 buf->bytes = initval;
79 buf->is_owner = TRUE;
81 return TPM_SUCCESS;
82 }
84 TPM_RESULT buffer_init_copy (buffer_t * buf, const buffer_t * src) {
85 TPM_RESULT status = buffer_init (buf, src->size, src->bytes);
86 buf->is_owner = TRUE;
88 return status;
89 }
93 // make an alias to a constant array
94 TPM_RESULT buffer_init_const (buffer_t * buf, tpm_size_t size, const BYTE* val) {
95 // TODO: try to enforce the const things somehow!
96 buf->bytes = (BYTE*) val;
97 buf->size = size;
98 buf->alloc_size = 0; // this field is now unneeded
100 buf->is_owner = FALSE;
102 return TPM_SUCCESS;
103 }
105 // make an alias into buf, with given offset and length
106 // if len = 0, make the alias go to the end of buf
107 TPM_RESULT buffer_init_alias (buffer_t * buf, const buffer_t * b,
108 tpm_size_t offset, tpm_size_t len) {
109 if (offset + len > b->size) {
110 return TPM_NOSPACE;
111 }
113 buf->bytes = b->bytes + offset;
114 buf->size = len > 0 ? len : b->size - offset;
116 //VS/ buf->alloc_size = 0;
117 if (len ==0)
118 buf->alloc_size = b->alloc_size - offset;
119 else
120 buf->alloc_size = MIN(b->alloc_size - offset, len);
123 buf->is_owner = FALSE;
125 return TPM_SUCCESS;
126 }
129 // copy into the start of dest
130 TPM_RESULT buffer_copy (buffer_t * dest, const buffer_t* src)
131 {
132 TPM_RESULT status = TPM_SUCCESS;
134 if (dest->alloc_size < src->size) {
135 status = buffer_priv_realloc (dest, src->size);
136 STATUSCHECK (status);
137 }
139 memcpy (dest->bytes, src->bytes, src->size);
140 dest->size = src->size;
142 //VS/ dest->is_owner = TRUE;
144 abort_egress:
146 return status;
147 }
151 BOOL buffer_eq (const buffer_t * a, const buffer_t * b) {
152 return (a->size == b->size && memcmp (a->bytes, b->bytes, a->size) == 0);
153 }
156 void buffer_memset (buffer_t * buf, BYTE b) {
157 memset (buf->bytes, b, buf->size);
158 }
161 TPM_RESULT buffer_append_raw (buffer_t * buf, tpm_size_t len, const BYTE* bytes) {
162 TPM_RESULT status = TPM_SUCCESS;
164 if (buf->alloc_size < buf->size + len) {
165 status = buffer_priv_realloc (buf, buf->size + len);
166 STATUSCHECK (status);
167 }
169 memcpy (buf->bytes + buf->size, bytes, len);
171 buf->size += len;
173 goto egress;
175 abort_egress:
177 egress:
179 return status;
180 }
182 tpm_size_t buffer_len (const buffer_t* buf) {
183 return buf->size;
184 }
186 TPM_RESULT buffer_free (buffer_t * buf) {
187 if (buf && buf->is_owner && buf->bytes != NULL) {
188 free (buf->bytes);
189 buf->bytes = NULL;
190 }
192 return TPM_SUCCESS;
193 }
195 TPM_RESULT buffer_priv_realloc (buffer_t * buf, tpm_size_t newsize) {
197 // we want to realloc to twice the size, or the new size, whichever
198 // bigger
200 BYTE * tmpbuf = NULL;
202 newsize = MAX (buf->alloc_size * 2, newsize);
204 tmpbuf = (BYTE*) realloc (buf->bytes, newsize);
205 if (tmpbuf == NULL)
206 return TPM_SIZE;
209 buf->bytes = tmpbuf;
210 buf->alloc_size = newsize;
212 return TPM_SUCCESS;
213 }