Philip Tricca | 7360972 | 2017-02-10 10:50:06 -0800 | [diff] [blame] | 1 | //**********************************************************************; |
| 2 | // Copyright (c) 2015, Intel Corporation |
| 3 | // All rights reserved. |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without |
| 6 | // modification, are permitted provided that the following conditions are met: |
| 7 | // |
| 8 | // 1. Redistributions of source code must retain the above copyright notice, |
| 9 | // this list of conditions and the following disclaimer. |
| 10 | // |
| 11 | // 2. Redistributions in binary form must reproduce the above copyright notice, |
| 12 | // this list of conditions and the following disclaimer in the documentation |
| 13 | // and/or other materials provided with the distribution. |
| 14 | // |
| 15 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 16 | // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 18 | // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 19 | // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 20 | // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 21 | // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 22 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 23 | // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 24 | // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 25 | // THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | //**********************************************************************; |
| 27 | |
Philip Tricca | cd313e7 | 2017-02-04 08:17:19 -0800 | [diff] [blame] | 28 | #include "sapi/marshal.h" |
Philip Tricca | 7360972 | 2017-02-10 10:50:06 -0800 | [diff] [blame] | 29 | #include "sapi/tpm20.h" |
| 30 | |
Philip Tricca | cd313e7 | 2017-02-04 08:17:19 -0800 | [diff] [blame] | 31 | #include "base-types.h" |
Philip Tricca | 7360972 | 2017-02-10 10:50:06 -0800 | [diff] [blame] | 32 | |
Philip Tricca | cd313e7 | 2017-02-04 08:17:19 -0800 | [diff] [blame] | 33 | #define BASE_MARSHAL(type, marshal_func) \ |
| 34 | TSS2_RC \ |
| 35 | type##_Marshal ( \ |
| 36 | type const *src, \ |
| 37 | uint8_t buffer [], \ |
| 38 | size_t buffer_size, \ |
| 39 | size_t *offset \ |
| 40 | ) \ |
| 41 | { \ |
| 42 | size_t local_offset = 0; \ |
| 43 | \ |
| 44 | if (offset != NULL) \ |
| 45 | local_offset = *offset; \ |
| 46 | \ |
| 47 | if (src == NULL || (buffer == NULL && offset == NULL)) { \ |
| 48 | return TSS2_TYPES_RC_BAD_REFERENCE; \ |
| 49 | } else if (buffer == NULL && offset != NULL) { \ |
| 50 | *offset += sizeof (*src); \ |
| 51 | return TSS2_RC_SUCCESS; \ |
| 52 | } else if (buffer_size < local_offset || \ |
| 53 | buffer_size - local_offset < sizeof (*src)) \ |
| 54 | { \ |
| 55 | return TSS2_TYPES_RC_INSUFFICIENT_BUFFER; \ |
| 56 | } \ |
| 57 | \ |
| 58 | data_ptr = (type*)&buffer [local_offset]; \ |
| 59 | *data_ptr = marshal_func (*src); \ |
| 60 | if (offset != NULL) { \ |
| 61 | *offset = local_offset + sizeof (*src); \ |
| 62 | } \ |
| 63 | \ |
| 64 | return TSS2_RC_SUCCESS; \ |
Philip Tricca | 7360972 | 2017-02-10 10:50:06 -0800 | [diff] [blame] | 65 | } |
| 66 | |
Philip Tricca | cd313e7 | 2017-02-04 08:17:19 -0800 | [diff] [blame] | 67 | #define BASE_UNMARSHAL(type, unmarshal_func) \ |
| 68 | TSS2_RC \ |
| 69 | type##_Unmarshal ( \ |
| 70 | uint8_t const buffer[], \ |
| 71 | size_t buffer_size, \ |
| 72 | size_t *offset, \ |
| 73 | type *dest \ |
| 74 | ) \ |
| 75 | { \ |
| 76 | size_t local_offset = 0; \ |
| 77 | \ |
| 78 | if (offset != NULL) \ |
| 79 | local_offset = *offset; \ |
| 80 | \ |
| 81 | if (buffer == NULL || (dest == NULL && offset == NULL)) { \ |
| 82 | return TSS2_TYPES_RC_BAD_REFERENCE; \ |
| 83 | } else if (dest == NULL && offset != NULL) { \ |
| 84 | *offset += sizeof (type); \ |
| 85 | return TSS2_RC_SUCCESS; \ |
| 86 | } else if (buffer_size < local_offset || \ |
| 87 | sizeof (*dest) > buffer_size - local_offset) \ |
| 88 | { \ |
| 89 | return TSS2_TYPES_RC_INSUFFICIENT_BUFFER; \ |
| 90 | } \ |
| 91 | \ |
| 92 | data_ptr = (type*)&buffer [local_offset]; \ |
| 93 | *dest = unmarshal_func (*data_ptr); \ |
| 94 | if (offset != NULL) { \ |
| 95 | *offset = local_offset + sizeof (*dest); \ |
| 96 | } \ |
| 97 | \ |
| 98 | return TSS2_RC_SUCCESS; \ |
Philip Tricca | 7360972 | 2017-02-10 10:50:06 -0800 | [diff] [blame] | 99 | } |
Philip Tricca | 689c090 | 2017-02-03 21:10:53 -0800 | [diff] [blame] | 100 | |
Philip Tricca | cd313e7 | 2017-02-04 08:17:19 -0800 | [diff] [blame] | 101 | BASE_MARSHAL (UINT8, HOST_TO_BE_8); |
| 102 | BASE_UNMARSHAL (UINT8, BE_TO_HOST_8); |
| 103 | BASE_MARSHAL (UINT16, HOST_TO_BE_16); |
| 104 | BASE_UNMARSHAL (UINT16, BE_TO_HOST_16); |
Philip Tricca | bff7212 | 2017-02-04 07:53:07 -0800 | [diff] [blame^] | 105 | BASE_MARSHAL (UINT32, HOST_TO_BE_32); |
| 106 | BASE_UNMARSHAL (UINT32, BE_TO_HOST_32); |
Philip Tricca | 3f03841 | 2017-02-04 08:32:55 -0800 | [diff] [blame] | 107 | |
| 108 | /* |
| 109 | * If we don't have endian.h then we need to fake it with our own endianness |
| 110 | * conversion functions. |
| 111 | */ |
| 112 | #if !defined(HAVE_ENDIAN_H) && !defined(WORDS_BIGENDIAN) |
| 113 | UINT16 |
| 114 | endian_conv_16 (UINT16 value) |
| 115 | { |
| 116 | return ((value & (0xff)) << 8) | \ |
| 117 | ((value & (0xff << 8)) >> 8); |
| 118 | } |
Philip Tricca | bff7212 | 2017-02-04 07:53:07 -0800 | [diff] [blame^] | 119 | UINT32 |
| 120 | endian_conv_32 (UINT32 value) |
| 121 | { |
| 122 | return ((value & (0xff)) << 24) | \ |
| 123 | ((value & (0xff << 8)) << 8) | \ |
| 124 | ((value & (0xff << 16)) >> 8) | \ |
| 125 | ((value & (0xff << 24)) >> 24); |
| 126 | } |
Philip Tricca | 3f03841 | 2017-02-04 08:32:55 -0800 | [diff] [blame] | 127 | #endif /* HAVE_ENDIAN_H */ |