OpenPGP: refactor do_dump_do()
- limit length of data to write even in raw mode to the real length - cluster variuable definitions - restrict scope of variables - introduce a variable length to make the purpose more obious - start preprocessor directives at column one - add comments where needed - harmonize coding style: space after "if" and casts
This commit is contained in:
parent
4a1bf9fb21
commit
ba9eebceaf
|
@ -391,11 +391,10 @@ static int do_userinfo(sc_card_t *card)
|
||||||
|
|
||||||
static int do_dump_do(sc_card_t *card, unsigned int tag)
|
static int do_dump_do(sc_card_t *card, unsigned int tag)
|
||||||
{
|
{
|
||||||
int r, tmp;
|
int r;
|
||||||
FILE *fp;
|
size_t length;
|
||||||
|
unsigned char buffer[254]; // Private DO are specified up to 254 bytes
|
||||||
|
|
||||||
// Private DO are specified up to 254 bytes
|
|
||||||
unsigned char buffer[254];
|
|
||||||
memset(buffer, '\0', sizeof(buffer));
|
memset(buffer, '\0', sizeof(buffer));
|
||||||
|
|
||||||
if (tag < 0x101 || tag > 0x104) {
|
if (tag < 0x101 || tag > 0x104) {
|
||||||
|
@ -406,36 +405,40 @@ static int do_dump_do(sc_card_t *card, unsigned int tag)
|
||||||
r = sc_get_data(card, tag, buffer, sizeof(buffer));
|
r = sc_get_data(card, tag, buffer, sizeof(buffer));
|
||||||
if (r < 0) {
|
if (r < 0) {
|
||||||
printf("Failed to get data object: %s\n", sc_strerror(r));
|
printf("Failed to get data object: %s\n", sc_strerror(r));
|
||||||
if(SC_ERROR_SECURITY_STATUS_NOT_SATISFIED == r) {
|
if (SC_ERROR_SECURITY_STATUS_NOT_SATISFIED == r) {
|
||||||
printf("Make sure the 'verify' and 'pin' parameters are correct.\n");
|
printf("Make sure the 'verify' and 'pin' parameters are correct.\n");
|
||||||
}
|
}
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
length = (size_t) r; /* r is guaranteed to be non-negative */
|
||||||
|
|
||||||
if(opt_raw) {
|
if (opt_raw) {
|
||||||
r = 0;
|
int tmp;
|
||||||
#ifndef _WIN32
|
FILE *fp;
|
||||||
|
|
||||||
|
#ifndef _WIN32
|
||||||
tmp = dup(fileno(stdout));
|
tmp = dup(fileno(stdout));
|
||||||
#else
|
#else
|
||||||
tmp = _dup(_fileno(stdout));
|
tmp = _dup(_fileno(stdout));
|
||||||
#endif
|
#endif
|
||||||
if (tmp < 0)
|
if (tmp < 0)
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
fp = freopen(NULL, "wb", stdout);
|
fp = freopen(NULL, "wb", stdout);
|
||||||
if (fp) {
|
if (fp) {
|
||||||
r = (int)fwrite(buffer, sizeof(char), sizeof(buffer), fp);
|
r = (int) fwrite(buffer, sizeof(char), length, fp);
|
||||||
}
|
}
|
||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
dup2(tmp, fileno(stdout));
|
dup2(tmp, fileno(stdout));
|
||||||
#else
|
#else
|
||||||
_dup2(tmp, _fileno(stdout));
|
_dup2(tmp, _fileno(stdout));
|
||||||
#endif
|
#endif
|
||||||
clearerr(stdout);
|
clearerr(stdout);
|
||||||
close(tmp);
|
close(tmp);
|
||||||
if (sizeof(buffer) != r)
|
|
||||||
|
if (length != r) /* fail on write errors */
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
} else {
|
} else {
|
||||||
util_hex_dump_asc(stdout, buffer, r, -1);
|
util_hex_dump_asc(stdout, buffer, length, -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
|
|
Loading…
Reference in New Issue