• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 #include "tlv.h"
18 
19 #include <android-base/logging.h>
20 
21 namespace android::hardware::radio::minimal::sim {
22 
makeTlv(uint32_t tag,std::span<uint8_t const> value)23 std::vector<uint8_t> makeTlv(uint32_t tag, std::span<uint8_t const> value) {
24     // If needed, implement ISO 7816 5.2.2.1
25     CHECK(tag <= 0xFFFF) << "3-byte tag numbers (" << tag << ") are not implemented";
26 
27     // If we end up needing more, implement ISO 7816 5.2.2.2
28     CHECK(value.size() <= 0x7F) << "Large tag lengths are not implemented: " << value.size()
29                                 << " for " << tag;
30 
31     std::vector<uint8_t> serialized;
32     if (tag <= 0xFF) {
33         serialized = {static_cast<uint8_t>(tag), static_cast<uint8_t>(value.size())};
34     } else {
35         serialized = {static_cast<uint8_t>(tag >> 8), static_cast<uint8_t>(tag & 0xFF),
36                       static_cast<uint8_t>(value.size())};
37     }
38 
39     serialized.insert(serialized.end(), value.begin(), value.end());
40     return serialized;
41 }
42 
43 namespace tlv_operators {
44 
operator +(std::span<uint8_t const> a,std::span<uint8_t const> b)45 std::vector<uint8_t> operator+(std::span<uint8_t const> a, std::span<uint8_t const> b) {
46     std::vector<uint8_t> concatenated;
47     concatenated.insert(concatenated.end(), a.begin(), a.end());
48     concatenated.insert(concatenated.end(), b.begin(), b.end());
49     return concatenated;
50 }
51 
52 }  // namespace tlv_operators
53 
54 }  // namespace android::hardware::radio::minimal::sim
55