[34] | 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 | // =================================================================== |
---|
| 33 | |
---|
| 34 | |
---|
| 35 | #include <stdarg.h> |
---|
| 36 | #include <string.h> |
---|
| 37 | #include <stdlib.h> |
---|
| 38 | #include <stdio.h> |
---|
| 39 | #include <sys/param.h> |
---|
| 40 | |
---|
| 41 | #include "tcg.h" |
---|
| 42 | #include "bsg.h" |
---|
| 43 | #include "buffer.h" |
---|
| 44 | |
---|
| 45 | static TPM_RESULT buffer_priv_realloc (buffer_t * buf, tpm_size_t newsize); |
---|
| 46 | |
---|
| 47 | // |
---|
| 48 | // buffer functions! |
---|
| 49 | // |
---|
| 50 | |
---|
| 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 | } |
---|
| 56 | |
---|
| 57 | |
---|
| 58 | buf->bytes = (BYTE*) malloc (initsize); |
---|
| 59 | if (buf->bytes == NULL) |
---|
| 60 | return TPM_RESOURCES; |
---|
| 61 | |
---|
| 62 | buf->size = initsize; |
---|
| 63 | buf->alloc_size = initsize; |
---|
| 64 | |
---|
| 65 | if (initval) |
---|
| 66 | memcpy (buf->bytes, initval, initsize); |
---|
| 67 | |
---|
| 68 | buf->is_owner = TRUE; |
---|
| 69 | |
---|
| 70 | return TPM_SUCCESS; |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | TPM_RESULT buffer_init_convert (buffer_t * buf, tpm_size_t initsize, BYTE* initval) { |
---|
| 74 | |
---|
| 75 | buf->size = initsize; |
---|
| 76 | buf->alloc_size = initsize; |
---|
| 77 | buf->bytes = initval; |
---|
| 78 | |
---|
| 79 | buf->is_owner = TRUE; |
---|
| 80 | |
---|
| 81 | return TPM_SUCCESS; |
---|
| 82 | } |
---|
| 83 | |
---|
| 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; |
---|
| 87 | |
---|
| 88 | return status; |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | |
---|
| 92 | |
---|
| 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 |
---|
| 99 | |
---|
| 100 | buf->is_owner = FALSE; |
---|
| 101 | |
---|
| 102 | return TPM_SUCCESS; |
---|
| 103 | } |
---|
| 104 | |
---|
| 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 | } |
---|
| 112 | |
---|
| 113 | buf->bytes = b->bytes + offset; |
---|
| 114 | buf->size = len > 0 ? len : b->size - offset; |
---|
| 115 | |
---|
| 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); |
---|
| 121 | |
---|
| 122 | |
---|
| 123 | buf->is_owner = FALSE; |
---|
| 124 | |
---|
| 125 | return TPM_SUCCESS; |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | // make an alias buffer_t into bytestream, with given length |
---|
| 129 | TPM_RESULT buffer_init_alias_convert (buffer_t * buf, tpm_size_t size, BYTE* val) { |
---|
| 130 | |
---|
| 131 | buf->size = size; |
---|
| 132 | buf->alloc_size = size; |
---|
| 133 | buf->bytes = val; |
---|
| 134 | |
---|
| 135 | buf->is_owner = FALSE; |
---|
| 136 | |
---|
| 137 | return TPM_SUCCESS; |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | |
---|
| 141 | |
---|
| 142 | // copy into the start of dest |
---|
| 143 | TPM_RESULT buffer_copy (buffer_t * dest, const buffer_t* src) |
---|
| 144 | { |
---|
| 145 | TPM_RESULT status = TPM_SUCCESS; |
---|
| 146 | |
---|
| 147 | if (dest->alloc_size < src->size) { |
---|
| 148 | TPMTRYRETURN( buffer_priv_realloc (dest, src->size) ); |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | memcpy (dest->bytes, src->bytes, src->size); |
---|
| 152 | dest->size = src->size; |
---|
| 153 | |
---|
| 154 | //VS/ dest->is_owner = TRUE; |
---|
| 155 | |
---|
| 156 | abort_egress: |
---|
| 157 | |
---|
| 158 | return status; |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | |
---|
| 162 | |
---|
| 163 | BOOL buffer_eq (const buffer_t * a, const buffer_t * b) { |
---|
| 164 | return (a->size == b->size && memcmp (a->bytes, b->bytes, a->size) == 0); |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | |
---|
| 168 | void buffer_memset (buffer_t * buf, BYTE b) { |
---|
| 169 | memset (buf->bytes, b, buf->size); |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | |
---|
| 173 | TPM_RESULT buffer_append_raw (buffer_t * buf, tpm_size_t len, const BYTE* bytes) { |
---|
| 174 | TPM_RESULT status = TPM_SUCCESS; |
---|
| 175 | |
---|
| 176 | if (buf->alloc_size < buf->size + len) { |
---|
| 177 | TPMTRYRETURN( buffer_priv_realloc (buf, buf->size + len) ); |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | memcpy (buf->bytes + buf->size, bytes, len); |
---|
| 181 | |
---|
| 182 | buf->size += len; |
---|
| 183 | |
---|
| 184 | goto egress; |
---|
| 185 | |
---|
| 186 | abort_egress: |
---|
| 187 | |
---|
| 188 | egress: |
---|
| 189 | |
---|
| 190 | return status; |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | tpm_size_t buffer_len (const buffer_t* buf) { |
---|
| 194 | return buf->size; |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | TPM_RESULT buffer_free (buffer_t * buf) { |
---|
| 198 | if (buf && buf->is_owner && buf->bytes != NULL) { |
---|
| 199 | free (buf->bytes); |
---|
| 200 | buf->bytes = NULL; |
---|
| 201 | buf->size = buf->alloc_size = 0; |
---|
| 202 | |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | return TPM_SUCCESS; |
---|
| 206 | } |
---|
| 207 | |
---|
| 208 | TPM_RESULT buffer_priv_realloc (buffer_t * buf, tpm_size_t newsize) { |
---|
| 209 | |
---|
| 210 | // we want to realloc to twice the size, or the new size, whichever |
---|
| 211 | // bigger |
---|
| 212 | |
---|
| 213 | BYTE * tmpbuf = NULL; |
---|
| 214 | |
---|
| 215 | newsize = MAX (buf->alloc_size * 2, newsize); |
---|
| 216 | |
---|
| 217 | tmpbuf = (BYTE*) realloc (buf->bytes, newsize); |
---|
| 218 | if (tmpbuf == NULL) |
---|
| 219 | return TPM_SIZE; |
---|
| 220 | |
---|
| 221 | |
---|
| 222 | buf->bytes = tmpbuf; |
---|
| 223 | buf->alloc_size = newsize; |
---|
| 224 | |
---|
| 225 | return TPM_SUCCESS; |
---|
| 226 | } |
---|