blob: 05ce7b6642a834e24ac7f978b3977d66ff539c5e [file] [log] [blame]
Philip Tricca73609722017-02-10 10:50:06 -08001//**********************************************************************;
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 Triccacd313e72017-02-04 08:17:19 -080028#include "sapi/marshal.h"
Philip Tricca73609722017-02-10 10:50:06 -080029#include "sapi/tpm20.h"
30
Philip Triccacd313e72017-02-04 08:17:19 -080031#include "base-types.h"
Philip Tricca73609722017-02-10 10:50:06 -080032
Philip Triccacd313e72017-02-04 08:17:19 -080033#define BASE_MARSHAL(type, marshal_func) \
34TSS2_RC \
35type##_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 Tricca73609722017-02-10 10:50:06 -080065}
66
Philip Triccacd313e72017-02-04 08:17:19 -080067#define BASE_UNMARSHAL(type, unmarshal_func) \
68TSS2_RC \
69type##_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 Tricca73609722017-02-10 10:50:06 -080099}
Philip Tricca689c0902017-02-03 21:10:53 -0800100
Philip Triccacd313e72017-02-04 08:17:19 -0800101BASE_MARSHAL (UINT8, HOST_TO_BE_8);
102BASE_UNMARSHAL (UINT8, BE_TO_HOST_8);
103BASE_MARSHAL (UINT16, HOST_TO_BE_16);
104BASE_UNMARSHAL (UINT16, BE_TO_HOST_16);
Philip Triccabff72122017-02-04 07:53:07 -0800105BASE_MARSHAL (UINT32, HOST_TO_BE_32);
106BASE_UNMARSHAL (UINT32, BE_TO_HOST_32);
Philip Tricca3f038412017-02-04 08:32:55 -0800107
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)
113UINT16
114endian_conv_16 (UINT16 value)
115{
116 return ((value & (0xff)) << 8) | \
117 ((value & (0xff << 8)) >> 8);
118}
Philip Triccabff72122017-02-04 07:53:07 -0800119UINT32
120endian_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 Tricca3f038412017-02-04 08:32:55 -0800127#endif /* HAVE_ENDIAN_H */