Embedded SDK
Embedded SDK
Loading...
Searching...
No Matches
parser.c
Go to the documentation of this file.
1/*****************************************************************************
2 * Ledger App Boilerplate.
3 * (c) 2020 Ledger SAS.
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 <stddef.h> // size_t
19#include <stdint.h> // uint*_t
20#include <stdbool.h> // bool
21
22#include "parser.h"
23#include "offsets.h"
24
25bool apdu_parser(command_t *cmd, uint8_t *buf, size_t buf_len)
26{
27 // Check minimum length, CLA / INS / P1 and P2 are mandatory
28 if (buf_len < OFFSET_LC) {
29 return false;
30 }
31
32 if (buf_len == OFFSET_LC) {
33 // Lc field not specified, implies lc = 0
34 cmd->lc = 0;
35 }
36 else {
37 // Lc field specified, check value against received length
38 cmd->lc = buf[OFFSET_LC];
39 if (buf_len - OFFSET_CDATA != cmd->lc) {
40 return false;
41 }
42 }
43
44 cmd->cla = buf[OFFSET_CLA];
45 cmd->ins = buf[OFFSET_INS];
46 cmd->p1 = buf[OFFSET_P1];
47 cmd->p2 = buf[OFFSET_P2];
48 cmd->data = (cmd->lc > 0) ? buf + OFFSET_CDATA : NULL;
49
50 return true;
51}
#define OFFSET_LC
Definition offsets.h:22
#define OFFSET_P1
Definition offsets.h:14
#define OFFSET_P2
Definition offsets.h:18
#define OFFSET_INS
Definition offsets.h:10
#define OFFSET_CLA
Definition offsets.h:6
#define OFFSET_CDATA
Definition offsets.h:26
bool apdu_parser(command_t *cmd, uint8_t *buf, size_t buf_len)
Definition parser.c:25
uint8_t ins
Instruction class.
Definition parser.h:12
uint8_t lc
Instruction parameter 2.
Definition parser.h:15
uint8_t cla
Definition parser.h:11
uint8_t p1
Instruction code.
Definition parser.h:13
uint8_t p2
Instruction parameter 1.
Definition parser.h:14
uint8_t * data
Length of command data.
Definition parser.h:16
unsigned char uint8_t
Definition usbd_conf.h:53