1 /*
2  *
3  * Copyright 2017, The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #include <teeui/cbor.h>
19 
20 namespace teeui {
21 namespace cbor {
22 
23 namespace {
24 
getByte(const uint64_t & v,const uint8_t index)25 inline uint8_t getByte(const uint64_t& v, const uint8_t index) {
26     return (v >> (index * 8)) & 0xff;
27 }
28 
writeBytes(WriteState state,uint64_t value,uint8_t size)29 WriteState writeBytes(WriteState state, uint64_t value, uint8_t size) {
30     auto pos = state.data_;
31     if (!(state += size)) return state;
32     switch (size) {
33     case 8:
34         *pos++ = getByte(value, 7);
35         *pos++ = getByte(value, 6);
36         *pos++ = getByte(value, 5);
37         *pos++ = getByte(value, 4);
38         [[fallthrough]];
39     case 4:
40         *pos++ = getByte(value, 3);
41         *pos++ = getByte(value, 2);
42         [[fallthrough]];
43     case 2:
44         *pos++ = getByte(value, 1);
45         [[fallthrough]];
46     case 1:
47         *pos++ = getByte(value, 0);
48         break;
49     default:
50         state.error_ = Error::MALFORMED;
51     }
52     return state;
53 }
54 
55 }  // anonymous namespace
56 
writeHeader(WriteState wState,Type type,const uint64_t value)57 WriteState writeHeader(WriteState wState, Type type, const uint64_t value) {
58     if (!wState) return wState;
59     uint8_t& header = *wState.data_;
60     if (!++wState) return wState;
61     header = static_cast<uint8_t>(type) << 5;
62     if (value < 24) {
63         header |= static_cast<uint8_t>(value);
64     } else if (value < 0x100) {
65         header |= 24;
66         wState = writeBytes(wState, value, 1);
67     } else if (value < 0x10000) {
68         header |= 25;
69         wState = writeBytes(wState, value, 2);
70     } else if (value < 0x100000000) {
71         header |= 26;
72         wState = writeBytes(wState, value, 4);
73     } else {
74         header |= 27;
75         wState = writeBytes(wState, value, 8);
76     }
77     return wState;
78 }
79 
byteCount(char c)80 static size_t byteCount(char c) {
81     if ((0xc0 & c) == 0x80) {
82         return 0;  // this is a multibyte payload byte
83     } else if (0x80 & c) {
84         /*
85          * CLZ - count leading zeroes.
86          * __builtin_clz promotes the argument to unsigned int.
87          * We invert c to turn leading ones into leading zeroes.
88          * We subtract additional leading zeroes due to the type promotion from the result.
89          */
90         return __builtin_clz((unsigned char)(~c)) - (sizeof(unsigned int) * 8 - 8);
91     } else {
92         return 1;
93     }
94 }
95 
checkUTF8Copy(const char * begin,const char * const end,uint8_t * out)96 bool checkUTF8Copy(const char* begin, const char* const end, uint8_t* out) {
97     while (begin != end) {
98         auto bc = byteCount(*begin);
99         // if the string ends in the middle of a multi byte char it is invalid
100         if (begin + bc > end) return false;
101         switch (bc) {
102         case 4:
103             if (out) *out++ = *reinterpret_cast<const uint8_t*>(begin++);
104             [[fallthrough]];
105         case 3:
106             if (out) *out++ = *reinterpret_cast<const uint8_t*>(begin++);
107             [[fallthrough]];
108         case 2:
109             if (out) *out++ = *reinterpret_cast<const uint8_t*>(begin++);
110             [[fallthrough]];
111         case 1:
112             if (out) *out++ = *reinterpret_cast<const uint8_t*>(begin++);
113             break;
114         default:
115             // case 0 means we encounted a payload byte when we expected a header.
116             // case > 4 is malformed.
117             return false;
118         }
119     }
120     return true;
121 }
122 
123 }  // namespace cbor
124 }  // namespace teeui
125