-
Notifications
You must be signed in to change notification settings - Fork 117
/
misc.c
184 lines (163 loc) · 4.6 KB
/
misc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**********************************************************************
*
* This file is part of Cardpeek, the smart card reader utility.
*
* Copyright 2009-2013 by Alain Pannetrat <[email protected]>
*
* Cardpeek is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Cardpeek 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Cardpeek. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <time.h>
#include "misc.h"
#include "pathconfig.h"
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <glib/gstdio.h>
#ifdef _WIN32
const char ANSI_RESET[] = "";
const char ANSI_RED[] = "";
const char ANSI_GREEN[] = "";
const char ANSI_YELLOW[] = "";
const char ANSI_BLUE[] = "";
const char ANSI_MAGENTA[] = "";
const char ANSI_CYAN[] = "";
const char ANSI_WHITE[] = "";
#else
const char ANSI_RESET[] = "\x1b[0m";
const char ANSI_RED[] = "\x1b[31m";
const char ANSI_GREEN[] = "\x1b[32m";
const char ANSI_YELLOW[] = "\x1b[33m";
const char ANSI_BLUE[] = "\x1b[34m";
const char ANSI_MAGENTA[] = "\x1b[35m";
const char ANSI_CYAN[] = "\x1b[36m";
const char ANSI_WHITE[] = "\x1b[37m";
#endif
const char *filename_extension(const char *fname)
{
const char* dot = strrchr(fname, '.');
if (!dot || dot==fname) return "";
return dot;
}
const char *filename_base(const char *fname)
{
const char* slash = strrchr(fname, '/');
if (!slash) slash = strrchr(fname, '\\');
if (!slash) return fname;
return slash+1;
}
void logstring_default(int,const char *);
unsigned logpos=0;
logfunc_t LOGFUNCTION = logstring_default;
FILE* LOGFILE=NULL;
void logstring_default(int level, const char *str)
{
if (level==LOG_DEBUG)
fprintf(stderr,"%s",str);
if (level==LOG_INFO)
fprintf(stderr,"%s%s",ANSI_GREEN,str);
if (level==LOG_WARNING)
fprintf(stderr,"%s%s",ANSI_MAGENTA,str);
if (level==LOG_ERROR)
fprintf(stderr,"%s%s",ANSI_RED,str);
fprintf(stderr,"%s",ANSI_RESET);
}
int log_printf(int level, const char *format, ...)
{
va_list al;
char *buf=NULL;
unsigned len_buf;
va_start(al,format);
len_buf = vsnprintf(buf,0,format,al);
va_end(al);
buf = (char *)malloc(len_buf+24);
if (level==LOG_DEBUG)
sprintf(buf,"%04i DEBUG ",logpos++);
else if (level==LOG_INFO)
sprintf(buf,"%04i INFO ",logpos++);
else if (level==LOG_WARNING)
sprintf(buf,"%04i WARNING ",logpos++);
else if (level==LOG_ERROR)
sprintf(buf,"%04i ERROR ",logpos++);
va_start(al,format);
vsprintf(buf+strlen(buf),format,al);
va_end(al);
strcat(buf,"\n");
if (LOGFUNCTION)
LOGFUNCTION(level,buf);
if (LOGFILE)
fprintf(LOGFILE,"%s",buf);
free(buf);
return len_buf;
}
void log_set_function(logfunc_t logfunc)
{
LOGFUNCTION=logfunc;
}
void log_open_file(void)
{
time_t now = time(NULL);
// changed mode from "w+"" to "w" (LOGFILE is never read, and "w+" is buggy
// on mingw glib2 versions 2.44.1-2 to 2.58.1-1 (at least) due to patch
// "0001-Use-CreateFile-on-Win32-to-make-sure-g_unlink-always.patch")
LOGFILE = g_fopen(path_config_get_string(PATH_CONFIG_FILE_CARDPEEK_LOG),"w");
if (LOGFILE)
fprintf(LOGFILE,"cardpeek log start: %s",ctime(&now));
else
fprintf(stderr,"Could not open %s for output. Proceeding without a log file.\n",path_config_get_string(PATH_CONFIG_FILE_CARDPEEK_LOG));
}
void log_close_file(void)
{
time_t now = time(NULL);
if (LOGFILE)
{
fprintf(LOGFILE,"cardpeek log ends: %s",ctime(&now));
fclose(LOGFILE);
}
LOGFILE = NULL;
}
/*********************************************************/
guint cstring_hash(gconstpointer str)
{
const unsigned char *s = str;
guint res=0;
while (*s)
{
res = (res*27)+(*s);
s++;
}
return res;
}
gint cstring_equal(gconstpointer a, gconstpointer b)
{
return (strcmp(a,b)==0);
}
/*********************************************************/
unsigned version_to_bcd(const char *version)
{
unsigned v[3];
v[0]=v[1]=v[2]=0;
sscanf(version,"%u.%u.%u",v,v+1,v+2);
return ((v[0]/10)<<28)|
((v[0]%10)<<24)|
((v[1]/10)<<20)|
((v[1]%10)<<16)|
((v[2]/1000)<<12)|
(((v[2]/100)%10)<<8)|
(((v[2]/10)%10)<<4)|
(v[2]%10);
}