xref: /aosp_15_r20/external/open-dice/include/dice/cbor_writer.h (revision 60b67249c2e226f42f35cc6cfe66c6048e0bae6b)
1*60b67249SAndroid Build Coastguard Worker // Copyright 2021 Google LLC
2*60b67249SAndroid Build Coastguard Worker //
3*60b67249SAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4*60b67249SAndroid Build Coastguard Worker // use this file except in compliance with the License. You may obtain a copy of
5*60b67249SAndroid Build Coastguard Worker // the License at
6*60b67249SAndroid Build Coastguard Worker //
7*60b67249SAndroid Build Coastguard Worker //     https://www.apache.org/licenses/LICENSE-2.0
8*60b67249SAndroid Build Coastguard Worker //
9*60b67249SAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*60b67249SAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11*60b67249SAndroid Build Coastguard Worker // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12*60b67249SAndroid Build Coastguard Worker // License for the specific language governing permissions and limitations under
13*60b67249SAndroid Build Coastguard Worker // the License.
14*60b67249SAndroid Build Coastguard Worker 
15*60b67249SAndroid Build Coastguard Worker #ifndef DICE_CBOR_WRITER_H_
16*60b67249SAndroid Build Coastguard Worker #define DICE_CBOR_WRITER_H_
17*60b67249SAndroid Build Coastguard Worker 
18*60b67249SAndroid Build Coastguard Worker #include <stdbool.h>
19*60b67249SAndroid Build Coastguard Worker #include <stddef.h>
20*60b67249SAndroid Build Coastguard Worker #include <stdint.h>
21*60b67249SAndroid Build Coastguard Worker 
22*60b67249SAndroid Build Coastguard Worker #ifdef __cplusplus
23*60b67249SAndroid Build Coastguard Worker extern "C" {
24*60b67249SAndroid Build Coastguard Worker #endif
25*60b67249SAndroid Build Coastguard Worker 
26*60b67249SAndroid Build Coastguard Worker struct CborOut {
27*60b67249SAndroid Build Coastguard Worker   uint8_t* buffer;
28*60b67249SAndroid Build Coastguard Worker   size_t buffer_size;
29*60b67249SAndroid Build Coastguard Worker   size_t cursor;
30*60b67249SAndroid Build Coastguard Worker };
31*60b67249SAndroid Build Coastguard Worker 
32*60b67249SAndroid Build Coastguard Worker // Initializes an output stream for writing CBOR tokens.
CborOutInit(uint8_t * buffer,size_t buffer_size,struct CborOut * out)33*60b67249SAndroid Build Coastguard Worker static inline void CborOutInit(uint8_t* buffer, size_t buffer_size,
34*60b67249SAndroid Build Coastguard Worker                                struct CborOut* out) {
35*60b67249SAndroid Build Coastguard Worker   out->buffer = buffer;
36*60b67249SAndroid Build Coastguard Worker   out->buffer_size = buffer_size;
37*60b67249SAndroid Build Coastguard Worker   out->cursor = 0;
38*60b67249SAndroid Build Coastguard Worker }
39*60b67249SAndroid Build Coastguard Worker 
40*60b67249SAndroid Build Coastguard Worker // Returns the number of bytes of encoded data. If |CborOutOverflowed()|
41*60b67249SAndroid Build Coastguard Worker // returns false, this number of bytes have been written, otherwise, this is the
42*60b67249SAndroid Build Coastguard Worker // number of bytes that that would have been written had there been space.
CborOutSize(const struct CborOut * out)43*60b67249SAndroid Build Coastguard Worker static inline size_t CborOutSize(const struct CborOut* out) {
44*60b67249SAndroid Build Coastguard Worker   return out->cursor;
45*60b67249SAndroid Build Coastguard Worker }
46*60b67249SAndroid Build Coastguard Worker 
47*60b67249SAndroid Build Coastguard Worker // Returns whether the |out| buffer contains the encoded tokens written to it or
48*60b67249SAndroid Build Coastguard Worker // whether the encoded tokens did not fit and the contents of the buffer should
49*60b67249SAndroid Build Coastguard Worker // be considered invalid.
CborOutOverflowed(const struct CborOut * out)50*60b67249SAndroid Build Coastguard Worker static inline bool CborOutOverflowed(const struct CborOut* out) {
51*60b67249SAndroid Build Coastguard Worker   return out->cursor == SIZE_MAX || out->cursor > out->buffer_size;
52*60b67249SAndroid Build Coastguard Worker }
53*60b67249SAndroid Build Coastguard Worker 
54*60b67249SAndroid Build Coastguard Worker // These functions write simple deterministically encoded CBOR tokens to an
55*60b67249SAndroid Build Coastguard Worker // output buffer. The offset is always increased, even if there is not enough
56*60b67249SAndroid Build Coastguard Worker // space in the output buffer to allow for measurement of the encoded data.
57*60b67249SAndroid Build Coastguard Worker // Use |CborOutOverflowed()| to check whether or not the buffer successfully
58*60b67249SAndroid Build Coastguard Worker // contains all of the of the encoded data.
59*60b67249SAndroid Build Coastguard Worker //
60*60b67249SAndroid Build Coastguard Worker // Complex types are constructed from these simple types, see RFC 8949. The
61*60b67249SAndroid Build Coastguard Worker // caller is responsible for correct and deterministic encoding of complex
62*60b67249SAndroid Build Coastguard Worker // types.
63*60b67249SAndroid Build Coastguard Worker void CborWriteInt(int64_t val, struct CborOut* out);
64*60b67249SAndroid Build Coastguard Worker void CborWriteUint(uint64_t val, struct CborOut* out);
65*60b67249SAndroid Build Coastguard Worker void CborWriteBstr(size_t data_size, const uint8_t* data, struct CborOut* out);
66*60b67249SAndroid Build Coastguard Worker void CborWriteTstr(const char* str, struct CborOut* out);
67*60b67249SAndroid Build Coastguard Worker void CborWriteArray(size_t num_elements, struct CborOut* out);
68*60b67249SAndroid Build Coastguard Worker void CborWriteMap(size_t num_pairs, struct CborOut* out);
69*60b67249SAndroid Build Coastguard Worker void CborWriteTag(uint64_t tag, struct CborOut* out);
70*60b67249SAndroid Build Coastguard Worker void CborWriteFalse(struct CborOut* out);
71*60b67249SAndroid Build Coastguard Worker void CborWriteTrue(struct CborOut* out);
72*60b67249SAndroid Build Coastguard Worker void CborWriteNull(struct CborOut* out);
73*60b67249SAndroid Build Coastguard Worker 
74*60b67249SAndroid Build Coastguard Worker // These functions write the type header and reserve space for the caller to
75*60b67249SAndroid Build Coastguard Worker // populate. The reserved space is left uninitialized. Returns NULL if space
76*60b67249SAndroid Build Coastguard Worker // could not be reserved in the output buffer.
77*60b67249SAndroid Build Coastguard Worker uint8_t* CborAllocBstr(size_t data_size, struct CborOut* out);
78*60b67249SAndroid Build Coastguard Worker char* CborAllocTstr(size_t size, struct CborOut* out);
79*60b67249SAndroid Build Coastguard Worker 
80*60b67249SAndroid Build Coastguard Worker #ifdef __cplusplus
81*60b67249SAndroid Build Coastguard Worker }  // extern "C"
82*60b67249SAndroid Build Coastguard Worker #endif
83*60b67249SAndroid Build Coastguard Worker 
84*60b67249SAndroid Build Coastguard Worker #endif  // DICE_CBOR_WRITER_H_
85