[M120 Migration][VD] Enable direct rendering for TVPlus
[platform/framework/web/chromium-efl.git] / components / apdu / apdu_command.h
1 // Copyright 2017 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef COMPONENTS_APDU_APDU_COMMAND_H_
6 #define COMPONENTS_APDU_APDU_COMMAND_H_
7
8 #include <cinttypes>
9 #include <utility>
10 #include <vector>
11
12 #include "base/component_export.h"
13 #include "base/containers/span.h"
14 #include "base/gtest_prod_util.h"
15 #include "third_party/abseil-cpp/absl/types/optional.h"
16
17 namespace apdu {
18
19 // APDU commands are defined as part of ISO 7816-4. Commands can be serialized
20 // into either short length encodings, where the maximum data length is 256
21 // bytes, or an extended length encoding, where the maximum data length is 65536
22 // bytes. This class implements only the extended length encoding. Serialized
23 // commands consist of a CLA byte, denoting the class of instruction, an INS
24 // byte, denoting the instruction code, P1 and P2, each one byte denoting
25 // instruction parameters, a length field (Lc), a data field of length Lc, and
26 // a maximum expected response length (Le).
27 class COMPONENT_EXPORT(APDU) ApduCommand {
28  public:
29   // Constructs an APDU command from the serialized message data.
30   static absl::optional<ApduCommand> CreateFromMessage(
31       base::span<const uint8_t> message);
32
33   ApduCommand();
34   ApduCommand(uint8_t cla,
35               uint8_t ins,
36               uint8_t p1,
37               uint8_t p2,
38               size_t response_length,
39               std::vector<uint8_t> data);
40   ApduCommand(ApduCommand&& that);
41   ApduCommand& operator=(ApduCommand&& that);
42
43   ApduCommand(const ApduCommand&) = delete;
44   ApduCommand& operator=(const ApduCommand&) = delete;
45
46   ~ApduCommand();
47
48   // Returns serialized message data.
49   std::vector<uint8_t> GetEncodedCommand() const;
50
51   void set_cla(uint8_t cla) { cla_ = cla; }
52   void set_ins(uint8_t ins) { ins_ = ins; }
53   void set_p1(uint8_t p1) { p1_ = p1; }
54   void set_p2(uint8_t p2) { p2_ = p2; }
55   void set_data(std::vector<uint8_t> data) { data_ = std::move(data); }
56   void set_response_length(size_t response_length) {
57     response_length_ = response_length;
58   }
59
60   uint8_t cla() const { return cla_; }
61   uint8_t ins() const { return ins_; }
62   uint8_t p1() const { return p1_; }
63   uint8_t p2() const { return p2_; }
64   size_t response_length() const { return response_length_; }
65   const std::vector<uint8_t>& data() const { return data_; }
66
67   static constexpr size_t kApduMaxResponseLength = 65536;
68
69  private:
70   FRIEND_TEST_ALL_PREFIXES(ApduTest, TestDeserializeBasic);
71   FRIEND_TEST_ALL_PREFIXES(ApduTest, TestDeserializeComplex);
72   FRIEND_TEST_ALL_PREFIXES(ApduTest, TestSerializeEdgeCases);
73
74   static constexpr size_t kApduMinHeader = 4;
75   static constexpr size_t kApduMaxHeader = 7;
76   static constexpr size_t kApduCommandDataOffset = 7;
77   static constexpr size_t kApduCommandLengthOffset = 5;
78
79   // As defined in ISO7816-4, extended length APDU request data is limited to
80   // 16 bits in length with a maximum value of 65535. Response data length is
81   // also limited to 16 bits in length with a value of 0x0000 corresponding to
82   // a length of 65536.
83   static constexpr size_t kApduMaxDataLength = 65535;
84   static constexpr size_t kApduMaxLength =
85       kApduMaxDataLength + kApduMaxHeader + 2;
86
87   uint8_t cla_ = 0;
88   uint8_t ins_ = 0;
89   uint8_t p1_ = 0;
90   uint8_t p2_ = 0;
91   size_t response_length_ = 0;
92   std::vector<uint8_t> data_;
93 };
94
95 }  // namespace apdu
96
97 #endif  // COMPONENTS_APDU_APDU_COMMAND_H_