1 /*
2  * Copyright (c) 2019, The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include "cppbor.h"
20 
21 namespace cppbor {
22 
23 using ParseResult = std::tuple<std::unique_ptr<Item> /* result */, const uint8_t* /* newPos */,
24                                std::string /* errMsg */>;
25 
26 /**
27  * Parse the first CBOR data item (possibly compound) from the range [begin, end).
28  *
29  * Returns a tuple of Item pointer, buffer pointer and error message.  If parsing is successful, the
30  * Item pointer is non-null, the buffer pointer points to the first byte after the
31  * successfully-parsed item and the error message string is empty.  If parsing fails, the Item
32  * pointer is null, the buffer pointer points to the first byte that was unparseable (the first byte
33  * of a data item header that is malformed in some way, e.g. an invalid value, or a length that is
34  * too large for the remining buffer, etc.) and the string contains an error message describing the
35  * problem encountered.
36  */
37 ParseResult parse(const uint8_t* begin, const uint8_t* end);
38 
39 /**
40  * Parse the first CBOR data item (possibly compound) from the byte vector.
41  *
42  * Returns a tuple of Item pointer, buffer pointer and error message.  If parsing is successful, the
43  * Item pointer is non-null, the buffer pointer points to the first byte after the
44  * successfully-parsed item and the error message string is empty.  If parsing fails, the Item
45  * pointer is null, the buffer pointer points to the first byte that was unparseable (the first byte
46  * of a data item header that is malformed in some way, e.g. an invalid value, or a length that is
47  * too large for the remining buffer, etc.) and the string contains an error message describing the
48  * problem encountered.
49  */
parse(const std::vector<uint8_t> & encoding)50 inline ParseResult parse(const std::vector<uint8_t>& encoding) {
51     return parse(encoding.data(), encoding.data() + encoding.size());
52 }
53 
54 /**
55  * Parse the first CBOR data item (possibly compound) from the range [begin, begin + size).
56  *
57  * Returns a tuple of Item pointer, buffer pointer and error message.  If parsing is successful, the
58  * Item pointer is non-null, the buffer pointer points to the first byte after the
59  * successfully-parsed item and the error message string is empty.  If parsing fails, the Item
60  * pointer is null, the buffer pointer points to the first byte that was unparseable (the first byte
61  * of a data item header that is malformed in some way, e.g. an invalid value, or a length that is
62  * too large for the remining buffer, etc.) and the string contains an error message describing the
63  * problem encountered.
64  */
parse(const uint8_t * begin,size_t size)65 inline ParseResult parse(const uint8_t* begin, size_t size) {
66     return parse(begin, begin + size);
67 }
68 
69 class ParseClient;
70 
71 /**
72  * Parse the CBOR data in the range [begin, end) in streaming fashion, calling methods on the
73  * provided ParseClient when elements are found.
74  */
75 void parse(const uint8_t* begin, const uint8_t* end, ParseClient* parseClient);
76 
77 /**
78  * Parse the CBOR data in the vector in streaming fashion, calling methods on the
79  * provided ParseClient when elements are found.
80  */
parse(const std::vector<uint8_t> & encoding,ParseClient * parseClient)81 inline void parse(const std::vector<uint8_t>& encoding, ParseClient* parseClient) {
82     return parse(encoding.data(), encoding.data() + encoding.size(), parseClient);
83 }
84 
85 /**
86  * A pure interface that callers of the streaming parse functions must implement.
87  */
88 class ParseClient {
89   public:
~ParseClient()90     virtual ~ParseClient() {}
91 
92     /**
93      * Called when an item is found.  The Item pointer points to the found item; use type() and
94      * the appropriate as*() method to examine the value.  hdrBegin points to the first byte of the
95      * header, valueBegin points to the first byte of the value and end points one past the end of
96      * the item.  In the case of header-only items, such as integers, and compound items (ARRAY,
97      * MAP or SEMANTIC) whose end has not yet been found, valueBegin and end are equal and point to
98      * the byte past the header.
99      *
100      * Note that for compound types (ARRAY, MAP, and SEMANTIC), the Item will have no content.  For
101      * Map and Array items, the size() method will return a correct value, but the index operators
102      * are unsafe, and the object cannot be safely compared with another Array/Map.
103      *
104      * The method returns a ParseClient*.  In most cases "return this;" will be the right answer,
105      * but a different ParseClient may be returned, which the parser will begin using. If the method
106      * returns nullptr, parsing will be aborted immediately.
107      */
108     virtual ParseClient* item(std::unique_ptr<Item>& item, const uint8_t* hdrBegin,
109                               const uint8_t* valueBegin, const uint8_t* end) = 0;
110 
111     /**
112      * Called when the end of a compound item (MAP or ARRAY) is found.  The item argument will be
113      * the same one passed to the item() call -- and may be empty if item() moved its value out.
114      * hdrBegin, valueBegin and end point to the beginning of the item header, the beginning of the
115      * first contained value, and one past the end of the last contained value, respectively.
116      *
117      * Note that the Item will have no content.
118      *
119      * As with item(), itemEnd() can change the ParseClient by returning a different one, or end the
120      * parsing by returning nullptr;
121      */
122     virtual ParseClient* itemEnd(std::unique_ptr<Item>& item, const uint8_t* hdrBegin,
123                                  const uint8_t* valueBegin, const uint8_t* end) = 0;
124 
125     /**
126      * Called when parsing encounters an error.  position is set to the first unparsed byte (one
127      * past the last successfully-parsed byte) and errorMessage contains an message explaining what
128      * sort of error occurred.
129      */
130     virtual void error(const uint8_t* position, const std::string& errorMessage) = 0;
131 };
132 
133 }  // namespace cppbor
134