Fix memory leaks

Indent lines


git-svn-id: https://www.opensc-project.org/svnp/opensc/trunk@369 c6295689-39f2-0310-b995-f0e70906c6a9
This commit is contained in:
aet 2002-03-22 00:13:25 +00:00
parent 6e3a09fa7b
commit 50e266567a
8 changed files with 112 additions and 115 deletions

View File

@ -1,5 +1,5 @@
#include <opensc.h>
#include <stdio.h> #include <stdio.h>
#include <opensc.h>
#include <sc-asn1.h> #include <sc-asn1.h>
int main(int argc, char *argv[]) int main(int argc, char *argv[])

View File

@ -2,30 +2,28 @@
* All rights reserved. * All rights reserved.
*/ */
#include "sc-test.h"
#include "opensc.h"
#include <unistd.h>
#include <stdio.h> #include <stdio.h>
#include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/time.h> #include <sys/time.h>
#include <opensc.h>
#include "sc-test.h"
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
int i, c, r; int i, c, r, freq[39];
int freq[39];
struct timeval tv1, tv2; struct timeval tv1, tv2;
u8 buf[14]; u8 buf[14];
i = sc_test_init(&argc, argv); i = sc_test_init(&argc, argv);
for (i = 0; i < 39; i++) for (i = 0; i < 39; i++)
freq[i] = 0; freq[i] = 0;
c = 0; c = 0;
while (1) { while (1) {
u8 alkiot[39]; u8 nbuf[39];
for (i = 0; i < 39; i++) { for (i = 0; i < 39; i++) {
alkiot[i] = i + 1; nbuf[i] = i + 1;
} }
if (c == 0) if (c == 0)
gettimeofday(&tv1, NULL); gettimeofday(&tv1, NULL);
@ -33,38 +31,37 @@ int main(int argc, char *argv[])
r = sc_get_challenge(card, buf, 14); r = sc_get_challenge(card, buf, 14);
sc_unlock(card); sc_unlock(card);
if (r == 0) { if (r == 0) {
int i, jaljella = 39; int i, left = 39;
printf("Lottorivi: "); printf("Lottery: ");
for (i = 0; i < 7; i++) { for (i = 0; i < 7; i++) {
unsigned short s = buf[2*i] + (buf[2*i+1] << 8); unsigned short s = buf[2 * i] + (buf[2 * i + 1] << 8);
int lot = s % (jaljella+1); int lot = s % (left + 1);
int num = alkiot[lot]; int num = nbuf[lot];
alkiot[lot] = alkiot[jaljella-1];
jaljella--;
freq[num-1]++;
nbuf[lot] = nbuf[left - 1];
left--;
freq[num - 1]++;
printf("%3d ", num); printf("%3d ", num);
} }
printf("\n"); printf("\n");
} else { } else {
fprintf(stderr, "get_random() failed: %s\n", sc_strerror(r)); fprintf(stderr, "sc_get_challenge() failed: %s\n", sc_strerror(r));
sc_test_cleanup(); sc_test_cleanup();
return 1; return 1;
} }
c++; c++;
if (c == 50) { if (c == 50) {
unsigned long long foo, foo2; unsigned long long foo, foo2;
gettimeofday(&tv2, NULL); gettimeofday(&tv2, NULL);
foo = tv2.tv_sec * 1000 + tv2.tv_usec / 1000; foo = tv2.tv_sec * 1000 + tv2.tv_usec / 1000;
foo2 = tv1.tv_sec * 1000 + tv1.tv_usec / 1000; foo2 = tv1.tv_sec * 1000 + tv1.tv_usec / 1000;
printf("Time per one: %lld ms\n", (foo - foo2)/50); printf("Time per one: %lld ms\n", (foo - foo2) / 50);
printf("Frequencies:\n"); printf("Frequencies:\n");
for (i = 0; i < 39; i++) { for (i = 0; i < 39; i++) {
printf("%3d: %-5d", i+1, freq[i]); printf("%3d: %-5d", i + 1, freq[i]);
if (((i+1) % 10) == 0) if (((i + 1) % 10) == 0)
printf("\n"); printf("\n");
} }
printf("\n"); printf("\n");

View File

@ -1,19 +1,18 @@
/* Copyright (C) 2001 Juha Yrjölä <juha.yrjola@iki.fi> /* Copyright (C) 2001 Juha Yrjölä <juha.yrjola@iki.fi>
* All rights reserved. * All rights reserved.
* *
* PKCS#15 PIN code test * PKCS#15 objects test
*/ */
#include "sc-test.h"
#include "opensc.h"
#include "opensc-pkcs15.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <opensc.h>
#include <opensc-pkcs15.h>
#include "sc-test.h"
struct sc_pkcs15_card *p15card; struct sc_pkcs15_card *p15card;
static int static int dump_objects(const char *what, int type)
dump_objects(const char *what, int type)
{ {
struct sc_pkcs15_object **objs; struct sc_pkcs15_object **objs;
int count, i; int count, i;
@ -47,11 +46,10 @@ dump_objects(const char *what, int type)
} }
free(objs); free(objs);
sc_unlock(card); sc_unlock(card);
return (count < 0)? 1 : 0; return (count < 0) ? 1 : 0;
} }
int int main(int argc, char *argv[])
main(int argc, char *argv[])
{ {
int i; int i;
@ -75,6 +73,7 @@ main(int argc, char *argv[])
dump_objects("RSA public keys", SC_PKCS15_TYPE_PUBKEY_RSA); dump_objects("RSA public keys", SC_PKCS15_TYPE_PUBKEY_RSA);
dump_objects("X.509 certificates", SC_PKCS15_TYPE_CERT_X509); dump_objects("X.509 certificates", SC_PKCS15_TYPE_CERT_X509);
sc_pkcs15_unbind(p15card);
sc_test_cleanup(); sc_test_cleanup();
return 0; return 0;
} }

View File

@ -4,13 +4,13 @@
* PKCS#15 PIN code test * PKCS#15 PIN code test
*/ */
#include "sc-test.h"
#include "opensc.h"
#include "opensc-pkcs15.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <string.h> #include <string.h>
#include <opensc.h>
#include <opensc-pkcs15.h>
#include "sc-test.h"
struct sc_pkcs15_card *p15card; struct sc_pkcs15_card *p15card;
@ -38,7 +38,6 @@ int enum_pins(struct sc_pkcs15_object ***ret)
return n; return n;
} }
int ask_and_verify_pin(struct sc_pkcs15_object *obj) int ask_and_verify_pin(struct sc_pkcs15_object *obj)
{ {
struct sc_pkcs15_pin_info *pin; struct sc_pkcs15_pin_info *pin;
@ -96,6 +95,7 @@ int main(int argc, char *argv[])
sc_unlock(card); sc_unlock(card);
if (i) { if (i) {
fprintf(stderr, "failed: %s\n", sc_strerror(i)); fprintf(stderr, "failed: %s\n", sc_strerror(i));
sc_test_cleanup();
return 1; return 1;
} }
printf("found.\n"); printf("found.\n");
@ -103,11 +103,15 @@ int main(int argc, char *argv[])
sc_lock(card); sc_lock(card);
count = enum_pins(&objs); count = enum_pins(&objs);
sc_unlock(card); sc_unlock(card);
if (count < 0) if (count < 0) {
sc_pkcs15_unbind(p15card);
sc_test_cleanup();
return 1; return 1;
}
for (i = 0; i < count; i++) { for (i = 0; i < count; i++) {
ask_and_verify_pin(objs[i]); ask_and_verify_pin(objs[i]);
} }
sc_pkcs15_unbind(p15card);
sc_test_cleanup(); sc_test_cleanup();
return 0; return 0;
} }

View File

@ -4,18 +4,18 @@
* PKCS#15 PIN code test * PKCS#15 PIN code test
*/ */
#include "sc-test.h"
#include "opensc.h"
#include "opensc-pkcs15.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <opensc.h>
#include <opensc-pkcs15.h>
#include "sc-test.h"
struct sc_pkcs15_card *p15card; struct sc_pkcs15_card *p15card;
static void static void print_pin(const struct sc_pkcs15_object *obj)
print_pin(const struct sc_pkcs15_object *obj)
{ {
const char *pin_flags[] = { const char *pin_flags[] =
{
"case-sensitive", "local", "change-disabled", "case-sensitive", "local", "change-disabled",
"unblock-disabled", "initialized", "needs-padding", "unblock-disabled", "initialized", "needs-padding",
"unblockingPin", "soPin", "disable_allowed", "unblockingPin", "soPin", "disable_allowed",
@ -23,7 +23,7 @@ print_pin(const struct sc_pkcs15_object *obj)
"exchangeRefData" "exchangeRefData"
}; };
struct sc_pkcs15_pin_info *pin; struct sc_pkcs15_pin_info *pin;
const int pf_count = sizeof(pin_flags)/sizeof(pin_flags[0]); const int pf_count = sizeof(pin_flags) / sizeof(pin_flags[0]);
int i; int i;
char *p; char *p;
@ -49,21 +49,22 @@ print_pin(const struct sc_pkcs15_object *obj)
printf("\n"); printf("\n");
} }
static void static void print_prkey_rsa(const struct sc_pkcs15_object *obj)
print_prkey_rsa(const struct sc_pkcs15_object *obj)
{ {
int i; int i;
const char *usages[] = { const char *usages[] =
{
"encrypt", "decrypt", "sign", "signRecover", "encrypt", "decrypt", "sign", "signRecover",
"wrap", "unwrap", "verify", "verifyRecover", "wrap", "unwrap", "verify", "verifyRecover",
"derive", "nonRepudiation" "derive", "nonRepudiation"
}; };
const int usage_count = sizeof(usages)/sizeof(usages[0]); const int usage_count = sizeof(usages) / sizeof(usages[0]);
const char *access_flags[] = { const char *access_flags[] =
{
"sensitive", "extract", "alwaysSensitive", "sensitive", "extract", "alwaysSensitive",
"neverExtract", "local" "neverExtract", "local"
}; };
const int af_count = sizeof(access_flags)/sizeof(access_flags[0]); const int af_count = sizeof(access_flags) / sizeof(access_flags[0]);
struct sc_pkcs15_prkey_info *prkey; struct sc_pkcs15_prkey_info *prkey;
prkey = (struct sc_pkcs15_prkey_info *) obj->data; prkey = (struct sc_pkcs15_prkey_info *) obj->data;
@ -92,21 +93,22 @@ print_prkey_rsa(const struct sc_pkcs15_object *obj)
printf("\n"); printf("\n");
} }
static void static void print_pubkey_rsa(const struct sc_pkcs15_object *obj)
print_pubkey_rsa(const struct sc_pkcs15_object *obj)
{ {
int i; int i;
const char *usages[] = { const char *usages[] =
{
"encrypt", "decrypt", "sign", "signRecover", "encrypt", "decrypt", "sign", "signRecover",
"wrap", "unwrap", "verify", "verifyRecover", "wrap", "unwrap", "verify", "verifyRecover",
"derive", "nonRepudiation" "derive", "nonRepudiation"
}; };
const int usage_count = sizeof(usages)/sizeof(usages[0]); const int usage_count = sizeof(usages) / sizeof(usages[0]);
const char *access_flags[] = { const char *access_flags[] =
{
"sensitive", "extract", "alwaysSensitive", "sensitive", "extract", "alwaysSensitive",
"neverExtract", "local" "neverExtract", "local"
}; };
const int af_count = sizeof(access_flags)/sizeof(access_flags[0]); const int af_count = sizeof(access_flags) / sizeof(access_flags[0]);
struct sc_pkcs15_pubkey_info *pubkey; struct sc_pkcs15_pubkey_info *pubkey;
pubkey = (struct sc_pkcs15_pubkey_info *) obj->data; pubkey = (struct sc_pkcs15_pubkey_info *) obj->data;
@ -135,8 +137,7 @@ print_pubkey_rsa(const struct sc_pkcs15_object *obj)
printf("\n"); printf("\n");
} }
static void static void print_cert_x509(const struct sc_pkcs15_object *obj)
print_cert_x509(const struct sc_pkcs15_object *obj)
{ {
struct sc_pkcs15_cert_info *cert; struct sc_pkcs15_cert_info *cert;
int i; int i;
@ -155,12 +156,10 @@ print_cert_x509(const struct sc_pkcs15_object *obj)
* and dump the label */ * and dump the label */
} }
void sc_test_print_object(const struct sc_pkcs15_object *obj)
void
sc_test_print_object(const struct sc_pkcs15_object *obj)
{ {
const char *kind; const char *kind;
void (*printer)(const struct sc_pkcs15_object *); void (*printer) (const struct sc_pkcs15_object *);
switch (obj->type) { switch (obj->type) {
case SC_PKCS15_TYPE_AUTH_PIN: case SC_PKCS15_TYPE_AUTH_PIN:

View File

@ -4,24 +4,22 @@
* Pseudo-random number generator test program * Pseudo-random number generator test program
*/ */
#include "sc-test.h"
#include "opensc.h"
#include <unistd.h>
#include <stdio.h> #include <stdio.h>
#include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/time.h> #include <sys/time.h>
#include <opensc.h>
#include "sc-test.h"
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
int i, c;
int freq[256];
struct timeval tv1, tv2; struct timeval tv1, tv2;
int i, c, freq[256];
u8 buf[8]; u8 buf[8];
i = sc_test_init(&argc, argv); i = sc_test_init(&argc, argv);
if (i < 0) if (i < 0)
return 1; return 1;
for (i = 0; i < 256; i++) for (i = 0; i < 256; i++)
freq[i] = 0; freq[i] = 0;
c = 0; c = 0;
@ -34,7 +32,7 @@ int main(int argc, char *argv[])
gettimeofday(&tv1, NULL); gettimeofday(&tv1, NULL);
i = sc_get_challenge(card, buf, 8); i = sc_get_challenge(card, buf, 8);
if (i != 0) { if (i != 0) {
fprintf(stderr, "sc_get_random() failed: %s\n", sc_strerror(i)); fprintf(stderr, "sc_get_challenge() failed: %s\n", sc_strerror(i));
sc_test_cleanup(); sc_test_cleanup();
return 1; return 1;
} }
@ -47,7 +45,7 @@ int main(int argc, char *argv[])
foo = tv2.tv_sec * 1000 + tv2.tv_usec / 1000; foo = tv2.tv_sec * 1000 + tv2.tv_usec / 1000;
foo2 = tv1.tv_sec * 1000 + tv1.tv_usec / 1000; foo2 = tv1.tv_sec * 1000 + tv1.tv_usec / 1000;
printf("\nTime to generate 64 bits of randomness: %lld ms\n", printf("\nTime to generate 64 bits of randomness: %lld ms\n",
(foo - foo2)/100); (foo - foo2) / 100);
printf("Frequencies:\n"); printf("Frequencies:\n");
for (i = 0; i < 256; i++) { for (i = 0; i < 256; i++) {
if (i && (i & 0x07) == 0) if (i && (i & 0x07) == 0)

View File

@ -55,7 +55,7 @@ int sc_test_init(int *argc, char *argv[])
printf("Connecting to card failed\n"); printf("Connecting to card failed\n");
return i; return i;
} }
printf("connected. ATR = "); printf("connected.\nATR = ");
for (i = 0; i < card->atr_len; i++) { for (i = 0; i < card->atr_len; i++) {
if (i) if (i)
printf(":"); printf(":");

View File

@ -1,5 +1,5 @@
#ifndef _SC_TEST_H #ifndef _SC_TEST_H
#define _SC_TEST_H
extern struct sc_context *ctx; extern struct sc_context *ctx;
extern struct sc_card *card; extern struct sc_card *card;