globalplatform
util.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2005-2026, Karsten Ohme
3  * This file is part of GlobalPlatform.
4  *
5  * GlobalPlatform is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * GlobalPlatform is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with GlobalPlatform. If not, see <https://www.gnu.org/licenses/>.
17  *
18  * In addition, as a special exception, the copyright holders give
19  * permission to link the code of portions of this program with the
20  * OpenSSL library under certain conditions as described in each
21  * individual source file, and distribute linked combinations
22  * including the two.
23  * You must obey the GNU Lesser General Public License in all respects
24  * for all of the code used other than OpenSSL. If you modify
25  * file(s) with this exception, you may extend this exception to your
26  * version of the file(s), but you are not obligated to do so. If you
27  * do not wish to do so, delete this exception statement from your
28  * version.
29  */
30 
35 #ifndef OPGP_UTIL_H
36 #define OPGP_UTIL_H
37 
38 #ifdef __cplusplus
39 extern "C"
40 {
41 #endif
42 
43 #ifdef WIN32
44 #include "stdafx.h"
45 #endif
46 
47 #include "globalplatform/types.h"
48 #include "globalplatform/library.h"
49 #include "globalplatform/error.h"
50 
54 typedef struct {
55  USHORT tag;
57  BYTE value[256];
60 } TLV;
61 
65 typedef struct {
66  USHORT tag;
67  const BYTE *value;
71 
73 OPGP_NO_API
74 LONG read_TLV(const BYTE *buffer, DWORD length, TLV *tlv);
75 
77 OPGP_NO_API
78 LONG parse_simple_tlv(const BYTE *buffer, DWORD length, GP_SIMPLE_TLV *tlv);
79 
81 OPGP_NO_API
82 OPGP_ERROR_STATUS append_tlv(PBYTE buffer, DWORD bufferSize, PDWORD offset, USHORT tag, const BYTE *value, DWORD valueLength);
83 
85 OPGP_NO_API
87 
89 OPGP_NO_API
90 DWORD get_short(PBYTE buf, DWORD offset);
91 
93 OPGP_NO_API
94 DWORD get_int(PBYTE buf, DWORD offset);
95 
97 DWORD get_number(PBYTE buf, DWORD offset, BYTE numLength);
98 
100 OPGP_NO_API
101 LONG parse_apdu_case(PBYTE apduCommand, DWORD apduCommandLength, PBYTE caseAPDU, PDWORD lc, PDWORD le);
102 
104 OPGP_NO_API
105 LONG write_TLV_length(PBYTE buffer, DWORD offset, DWORD lengthLeft, USHORT length);
106 
108 OPGP_NO_API
109 LONG parse_OID_numeric_string(PBYTE oid, DWORD oidLength, char *out, DWORD outLength);
110 
111 #ifdef __cplusplus
112 }
113 #endif
114 
115 #endif
Definition: util.h:65
const BYTE * value
Pointer into the source buffer.
Definition: util.h:67
DWORD tlvLength
The length of the whole TLV.
Definition: util.h:69
USHORT tag
The Tag.
Definition: util.h:66
DWORD length
The length of the value.
Definition: util.h:68
Definition: error.h:58
Definition: util.h:54
DWORD tlvLength
The length of the whole TLV.
Definition: util.h:59
USHORT tag
The Tag.
Definition: util.h:55
PBYTE extendedValue
The extended value if length > 256. Must be manually allocated and deallocated.
Definition: util.h:58
DWORD length
The length of the value.
Definition: util.h:56
long LONG
A long value.
Definition: types.h:69
unsigned char BYTE
A Microsoft/Muscle BYTE definition.
Definition: types.h:67
unsigned char * PBYTE
A Microsoft/Muscle LPBYTE, pointer to unsigned char.
Definition: types.h:64
unsigned long * PDWORD
A Microsoft LPDWORD/Muscle pointer to a DWORD.
Definition: types.h:66
unsigned long DWORD
A Microsoft/Muscle DWORD definition.
Definition: types.h:68
OPGP_NO_API DWORD get_int(PBYTE buf, DWORD offset)
Returns an unsigned int from the given position.
Definition: util.c:51
OPGP_NO_API LONG parse_apdu_case(PBYTE apduCommand, DWORD apduCommandLength, PBYTE caseAPDU, PDWORD lc, PDWORD le)
Parse the APDU case.
Definition: util.c:256
OPGP_NO_API LONG read_TLV(const BYTE *buffer, DWORD length, TLV *tlv)
Reads a TLV struct from the given buffer.
Definition: util.c:93
OPGP_NO_API DWORD convert_byte(BYTE b)
Converts a ISO 7816-4 Le Byte into its value.
Definition: util.c:42
OPGP_NO_API LONG parse_simple_tlv(const BYTE *buffer, DWORD length, GP_SIMPLE_TLV *tlv)
Reads a TLV view from the given buffer without copying the value.
Definition: util.c:155
DWORD get_number(PBYTE buf, DWORD offset, BYTE numLength)
Returns an unsigned int from the given position by taking just numLength bytes starting at offset int...
Definition: util.c:61
OPGP_NO_API OPGP_ERROR_STATUS append_tlv(PBYTE buffer, DWORD bufferSize, PDWORD offset, USHORT tag, const BYTE *value, DWORD valueLength)
Appends a TLV to the given buffer.
Definition: util.c:208
OPGP_NO_API LONG write_TLV_length(PBYTE buffer, DWORD offset, DWORD lengthLeft, USHORT length)
Writes a TLV length field following DER BER rules.
Definition: util.c:322
OPGP_NO_API DWORD get_short(PBYTE buf, DWORD offset)
Returns an unsigned short int from the given position.
Definition: util.c:82
OPGP_NO_API LONG parse_OID_numeric_string(PBYTE oid, DWORD oidLength, char *out, DWORD outLength)
Parses a DER encoded OID into a numeric string (e.g. "1.2.840.113549").
Definition: util.c:352