Coverage Report

Created: 2025-11-24 06:43

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opensc/src/libopensc/aux-data.c
Line
Count
Source
1
/*
2
 * aux-data.c: Auxiliary data help functions
3
 *
4
 * Copyright (C) 2016 Viktor Tarasov <viktor.tarasov@gmail.com>
5
 *
6
 * This library is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
11
 * This library is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
 */
20
21
#ifdef HAVE_CONFIG_H
22
#include "config.h"
23
#endif
24
25
#include <stdio.h>
26
#include <stdlib.h>
27
#include <assert.h>
28
#include <string.h>
29
30
#include "internal.h"
31
#include "common/compat_strlcat.h"
32
33
#include <libopensc/errors.h>
34
#include <libopensc/types.h>
35
#include <libopensc/log.h>
36
#include <libopensc/aux-data.h>
37
38
39
int
40
sc_aux_data_allocate(struct sc_context *ctx, struct sc_auxiliary_data **dst, struct sc_auxiliary_data *src)
41
0
{
42
0
  int rv = SC_ERROR_INTERNAL;
43
44
0
  LOG_FUNC_CALLED(ctx);
45
46
0
  if (!dst)
47
0
    LOG_TEST_RET(ctx, SC_ERROR_INVALID_ARGUMENTS, "Cannot allocate auxiliary data");
48
49
0
  if (*dst == NULL)   {
50
0
    *dst = calloc(1, sizeof(struct sc_auxiliary_data));
51
0
    if (*dst == NULL)
52
0
      LOG_TEST_RET(ctx, SC_ERROR_OUT_OF_MEMORY, "Cannot allocate aux. data");
53
0
  }
54
55
0
  if ((src == NULL) || (src->type == SC_AUX_DATA_TYPE_NO_DATA))
56
0
    LOG_FUNC_RETURN(ctx, SC_SUCCESS);
57
58
0
  switch(src->type) {
59
0
  case SC_AUX_DATA_TYPE_MD_CMAP_RECORD:
60
0
    **dst = *src;
61
0
    rv = SC_SUCCESS;
62
0
    break;
63
0
  default:
64
0
    sc_log(ctx, "Invalid aux-data type %X", src->type);
65
0
    LOG_TEST_RET(ctx, SC_ERROR_INVALID_ARGUMENTS, "Unknown aux-data type");
66
0
  }
67
68
0
  LOG_FUNC_RETURN(ctx, rv);
69
0
}
70
71
72
int
73
sc_aux_data_set_md_guid(struct sc_context *ctx, struct sc_auxiliary_data *aux_data, char *guid)
74
0
{
75
0
  struct sc_md_cmap_record *rec;
76
0
  int rv = SC_ERROR_INTERNAL;
77
78
0
  LOG_FUNC_CALLED(ctx);
79
0
  if (!aux_data || !guid || strlen(guid) > SC_MD_MAX_CONTAINER_NAME_LEN)
80
0
    LOG_TEST_RET(ctx, SC_ERROR_INVALID_ARGUMENTS, "Cannot set guid for MD container");
81
82
0
  switch(aux_data->type) {
83
0
  case SC_AUX_DATA_TYPE_NO_DATA:
84
0
    memset(aux_data, 0, sizeof(*aux_data));
85
0
    aux_data->type = SC_AUX_DATA_TYPE_MD_CMAP_RECORD;
86
    /* fallthrough */
87
0
  case SC_AUX_DATA_TYPE_MD_CMAP_RECORD:
88
0
    rec = &aux_data->data.cmap_record;
89
0
    memcpy(rec->guid, guid, strlen(guid));
90
0
    rec->guid_len = strlen(guid);
91
0
    sc_log(ctx, "set MD container GUID '%s'", aux_data->data.cmap_record.guid);
92
0
    rv = SC_SUCCESS;
93
0
    break;
94
0
  default:
95
0
    sc_log(ctx, "Invalid aux-data type %X", aux_data->type);
96
0
    LOG_TEST_RET(ctx, SC_ERROR_INVALID_ARGUMENTS, "Unknown aux-data type");
97
0
  }
98
99
0
  LOG_FUNC_RETURN(ctx, rv);
100
0
  return rv;
101
0
}
102
103
104
int
105
sc_aux_data_set_md_flags(struct sc_context *ctx, struct sc_auxiliary_data *aux_data, unsigned char flags)
106
0
{
107
0
  int rv = SC_ERROR_INTERNAL;
108
109
0
  LOG_FUNC_CALLED(ctx);
110
0
  if (!aux_data)
111
0
    LOG_TEST_RET(ctx, SC_ERROR_INVALID_ARGUMENTS, "Cannot set flags of MD container");
112
113
0
  switch(aux_data->type) {
114
0
  case SC_AUX_DATA_TYPE_NO_DATA:
115
0
    memset(aux_data, 0, sizeof(*aux_data));
116
0
    aux_data->type = SC_AUX_DATA_TYPE_MD_CMAP_RECORD;
117
    /* fallthrough */
118
0
  case SC_AUX_DATA_TYPE_MD_CMAP_RECORD:
119
0
    aux_data->data.cmap_record.flags = flags;
120
0
    sc_log(ctx, "set MD container flags '0x%X'", flags);
121
0
    rv = SC_SUCCESS;
122
0
    break;
123
0
  default:
124
0
    sc_log(ctx, "Invalid aux-data type %X", aux_data->type);
125
0
    LOG_TEST_RET(ctx, SC_ERROR_INVALID_ARGUMENTS, "Unknown aux-data type");
126
0
  }
127
128
0
  LOG_FUNC_RETURN(ctx, rv);
129
0
}
130
131
132
int
133
sc_aux_data_get_md_guid(struct sc_context *ctx, struct sc_auxiliary_data *aux_data,
134
    unsigned flags, unsigned char *out, size_t *out_size)
135
0
{
136
0
  struct sc_md_cmap_record *cmap_record = NULL;
137
0
  char guid[SC_MD_MAX_CONTAINER_NAME_LEN + 3];
138
139
0
  LOG_FUNC_CALLED(ctx);
140
0
  if(!aux_data || !out || !out_size)
141
0
    LOG_FUNC_RETURN(ctx, SC_ERROR_INVALID_ARGUMENTS);
142
143
0
  if (aux_data->type != SC_AUX_DATA_TYPE_MD_CMAP_RECORD)
144
0
    LOG_FUNC_RETURN(ctx, SC_ERROR_NOT_SUPPORTED);
145
146
0
  cmap_record = &aux_data->data.cmap_record;
147
148
  /* Ignore silently request of '{}' frame is output buffer is too small */
149
0
  if (!flags && *out_size < strlen((char *)cmap_record->guid) + 2)
150
0
    flags = 1;
151
152
0
  *guid = '\0';
153
0
  if (!flags)
154
0
    strncpy(guid, "{", sizeof guid);
155
0
  strlcat(guid, (char *)cmap_record->guid, sizeof(guid)-1);
156
0
  if (!flags)
157
0
    strlcat(guid, "}", sizeof(guid));
158
159
0
  if (*out_size < strlen(guid))   {
160
0
    sc_log(ctx,
161
0
           "aux-data: buffer too small: out_size:%"SC_FORMAT_LEN_SIZE_T"u < guid-length:%"SC_FORMAT_LEN_SIZE_T"u",
162
0
           *out_size, strlen(guid));
163
0
    LOG_FUNC_RETURN(ctx, SC_ERROR_BUFFER_TOO_SMALL);
164
0
  }
165
166
0
  memset(out, 0, *out_size);
167
0
  memcpy(out, guid, strlen(guid));
168
0
  *out_size = strlen(guid);
169
170
0
  sc_log(ctx, "aux-data: returns guid '%s'", (char *)out);
171
0
  LOG_FUNC_RETURN(ctx, SC_SUCCESS);
172
0
}
173
174
175
int
176
sc_aux_data_get_md_flags(struct sc_context *ctx, struct sc_auxiliary_data *aux_data,
177
    unsigned char *flags)
178
0
{
179
0
  LOG_FUNC_CALLED(ctx);
180
0
  if(!aux_data || !flags)
181
0
    LOG_FUNC_RETURN(ctx, SC_ERROR_INVALID_ARGUMENTS);
182
183
0
  if (aux_data->type != SC_AUX_DATA_TYPE_MD_CMAP_RECORD)
184
0
    LOG_FUNC_RETURN(ctx, SC_ERROR_NOT_SUPPORTED);
185
186
0
  *flags = aux_data->data.cmap_record.flags;
187
188
0
  sc_log(ctx, "aux-data: returns flags '0x%X'", *flags);
189
0
  LOG_FUNC_RETURN(ctx, SC_SUCCESS);
190
0
}
191
192
193
void
194
sc_aux_data_free(struct sc_auxiliary_data **data)
195
2.27k
{
196
2.27k
  if (data == NULL || *data == NULL)
197
2.27k
    return;
198
199
0
  switch((*data)->type) {
200
0
  case SC_AUX_DATA_TYPE_MD_CMAP_RECORD:
201
0
    free(*data);
202
0
    break;
203
0
  default:
204
0
    break;
205
0
  }
206
207
0
  *data = NULL;
208
0
}
209