Skip to content

Commit

Permalink
openpgp-tool: issue-220: read and display OpenPGP data
Browse files Browse the repository at this point in the history
  • Loading branch information
viktorTarasov committed Jun 1, 2014
1 parent ee0566a commit 3f13f57
Showing 1 changed file with 23 additions and 29 deletions.
52 changes: 23 additions & 29 deletions src/tools/openpgp-tool.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ static void display_data(const struct ef_name_map *mapping, char *value)
} else {
const char *label = mapping->name;

printf("%s:%*s%s\n", label, 10-strlen(label), "", value);
printf("%s:%*s%s\n", label, (int)(10-strlen(label)), "", value);
}
}
}
Expand Down Expand Up @@ -304,44 +304,38 @@ static int do_userinfo(sc_card_t *card)
for (i = 0; openpgp_data[i].ef != NULL; i++) {
sc_path_t path;
sc_file_t *file;
size_t count;
size_t offset = 0;
int count;
int r;

sc_format_path(openpgp_data[i].ef, &path);
r = sc_select_file(card, &path, &file);

if (r) {
fprintf(stderr, "Failed to select EF %s: %s\n",
openpgp_data[i].ef, sc_strerror(r));
fprintf(stderr, "Failed to select EF %s: %s\n", openpgp_data[i].ef, sc_strerror(r));
return EXIT_FAILURE;
}

count = file->size;
while (count > 0) {
int c = count > sizeof(buf) ? sizeof(buf) : count;

r = sc_read_binary(card, offset, buf+offset, c, 0);
if (r < 0) {
fprintf(stderr, "%s: read failed - %s\n",
openpgp_data[i].ef, sc_strerror(r));
return EXIT_FAILURE;
}
if (r != c) {
fprintf(stderr, "%s: expecting %d, got only %d bytes\n",
openpgp_data[i].ef, c, r);
return EXIT_FAILURE;
}

offset += r;
count -= r;
}

buf[file->size] = '\0';

if (file->size > 0) {
display_data(openpgp_data + i, (char *) buf);
if (!count)
continue;

if (count > (int)sizeof(buf) - 1) {
fprintf(stderr, "Too small buffer to read the OpenPGP data\n");
return EXIT_FAILURE;
}

r = sc_read_binary(card, 0, buf, count, 0);
if (r < 0) {
fprintf(stderr, "%s: read failed - %s\n", openpgp_data[i].ef, sc_strerror(r));
return EXIT_FAILURE;
}
if (r != count) {
fprintf(stderr, "%s: expecting %d, got only %d bytes\n", openpgp_data[i].ef, count, r);
return EXIT_FAILURE;
}

buf[count] = '\0';

display_data(openpgp_data + i, (char *) buf);
}

return EXIT_SUCCESS;
Expand Down

0 comments on commit 3f13f57

Please sign in to comment.