| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- /*
- * Copyright (c) 2021 Clementine Computing LLC.
- *
- * This file is part of PopuFare.
- *
- * PopuFare is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * PopuFare is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with PopuFare. If not, see <https://www.gnu.org/licenses/>.
- *
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <sys/time.h>
- #include <unistd.h>
- #include <string.h>
- #include <ctype.h>
- #include <getopt.h>
- #include <errno.h>
- #include "../common/common_config.h"
- #include "../passdb/passdb.h"
- #define VERSION "0.1.0"
- void _version(FILE *fp) {
- fprintf(fp, "version: %s\n", VERSION);
- }
- void _usage(FILE *fp) {
- _version(fp);
- fprintf(fp, "usage:\n\n");
- fprintf(fp, " passdb_inspect [-h] [-v] [passdb]\n\n");
- fprintf(fp, " [passdb] passdb file (defualt '%s')\n", PASSES_FILE);
- fprintf(fp, " [-h] help (this screen)\n");
- fprintf(fp, " [-v] show version\n");
- fprintf(fp, "\n");
- }
- struct option g_long_option[] = {
- {"version", no_argument, 0, 'v'},
- {"help", no_argument, 0, 'h'},
- {0,0,0,0}
- };
- char *g_pass_file = NULL;
- double del_tv( struct timeval *tv_e, struct timeval *tv_s) {
- double d=0.0;
- d += (double)(tv_e->tv_sec - tv_s->tv_sec) * (1000000.0);
- d += (double)(tv_e->tv_usec - tv_s->tv_usec);
- return d;
- }
- int main(int argc, char **argv) {
- int i, j, k;
- extern char *optarg;
- extern int optind;
- int option_index;
- int ch;
- int retcode = 0;
- int fd;
- ssize_t del_s=0;
- size_t offset=0, pass_size=0;
- rider_record *passmem = NULL;
- struct timeval tv_s, tv_e;
- while ((ch = getopt_long(argc, argv, "hv", g_long_option, &option_index)) > 0) switch(ch) {
- case 'h':
- _usage(stdout);
- exit(0);
- break;
- case 'v':
- _version(stdout);
- exit(0);
- break;
- case 0:
- default:
- fprintf(stderr, "bad option (%i, %i)\n", option_index, (int)ch);
- _usage(stderr);
- exit(-2);
- break;
- }
- if (optind < argc) {
- g_pass_file = strdup(argv[optind]);
- } else {
- g_pass_file = strdup(PASSES_FILE);
- }
- //---
- gettimeofday(&tv_s, NULL);
- pass_size = sizeof(rider_record)*NUM_STORED_PASSES;
- passmem = (rider_record *)malloc(pass_size);
- fd = open(g_pass_file, O_RDONLY);
- if (fd<0) {
- perror(g_pass_file);
- retcode = -1;
- goto free_and_exit;
- }
- while (offset < pass_size) {
- del_s = read(fd, ((void *)passmem) + offset, pass_size - offset);
- if (del_s<0) {
- perror(g_pass_file);
- goto free_and_exit;
- }
- offset += del_s;
- }
- close(fd);
- gettimeofday(&tv_e, NULL);
- printf("## loaded in %f\n", del_tv(&tv_e, &tv_s));
- //---
- for (i=0; i<NUM_STORED_PASSES; i++) {
- if (passmem[i].seq == 0) { continue; }
- printf("[%4i] ", i);
- printf(" %8lli %8lli ", passmem[i].seq, passmem[i].id);
- for (j=0; j<CREDENTIAL_LEN; j++) {
- //printf("%2x", passmem[i].magstripe_value[j]);
- //continue;
- if (isprint(passmem[i].magstripe_value[j])) {
- printf("%c", passmem[i].magstripe_value[j]);
- }
- else {
- printf(".");
- }
- }
- printf(" ");
- for (j=0; j<CREDENTIAL_LEN; j++) {
- if (isprint(passmem[i].rfid_value[j])) {
- printf("%c", passmem[i].rfid_value[j]);
- }
- else {
- printf(".");
- }
- }
- printf(" ");
- for (j=0; j<RULENAME_LEN; j++) {
- if (isprint(passmem[i].rule_name[j])) {
- printf("%c", passmem[i].rule_name[j]);
- }
- else {
- printf(".");
- }
- }
- printf(" ");
- for (j=0; j<PARAM_LEN; j++) {
- if (isprint(passmem[i].rule_param[j])) {
- printf("%c", passmem[i].rule_param[j]);
- }
- else {
- printf(".");
- }
- }
- printf("\n");
- }
- //---
- free_and_exit:
- if (g_pass_file) {
- free(g_pass_file);
- }
- if (passmem) {
- free(passmem);
- }
- exit(retcode);
- }
|