1 /* $Id: r1r2.c,v 1.4 2001/08/02 16:41:32 rees Exp $ */
2 
3 /*
4 copyright 1999
5 the regents of the university of michigan
6 all rights reserved
7 
8 permission is granted to use, copy, create derivative works
9 and redistribute this software and such derivative works
10 for any purpose, so long as the name of the university of
11 michigan is not used in any advertising or publicity
12 pertaining to the use or distribution of this software
13 without specific, written prior authorization.  if the
14 above copyright notice or any other identification of the
15 university of michigan is included in any copy of any
16 portion of this software, then the disclaimer below must
17 also be included.
18 
19 this software is provided as is, without representation
20 from the university of michigan as to its fitness for any
21 purpose, and without warranty by the university of
22 michigan of any kind, either express or implied, including
23 without limitation the implied warranties of
24 merchantability and fitness for a particular purpose. the
25 regents of the university of michigan shall not be liable
26 for any damages, including special, indirect, incidental, or
27 consequential damages, with respect to any claim arising
28 out of or in connection with the use of the software, even
29 if it has been or is hereafter advised of the possibility of
30 such damages.
31 */
32 
33 /*
34  * Return text for a given pair of sw1/sw2 status bytes
35  */
36 
37 #ifdef __palmos__
38 #include <Common.h>
39 #include <System/SysAll.h>
40 #include <System/Unix/sys_types.h>
41 #include <System/Unix/unix_stdlib.h>
42 #include <System/Unix/unix_string.h>
43 #include <UI/UIAll.h>
44 #include "field.h"
45 #else
46 #include <stdio.h>
47 #endif
48 
49 #include "sectok.h"
50 
51 static char *scsws(int sw);
52 
53 static struct r1r2s {
54     int sw;
55     char *s;
56 } r1r2s[] = {
57     {0x9000, "ok"},
58 
59     /* sectok errors */
60     {0x0601, "no such tty"},
61     {0x0602, "out of memory"},
62     {0x0603, "timeout"},
63     {0x0604, "slag!"},
64     {0x0605, "card type not supported"},
65     {0x0606, "no card in reader"},
66     {0x0607, "not implemented"},
67     {0x0608, "error loading driver"},
68     {0x0609, "communications error"},
69     {0x060a, "reader not open"},
70     {0x060c, "config conflict"},
71     {0x060d, "unknown error"},
72 
73     /* card errors */
74     {0x61ff, "ok; response available %x"},
75     {0x6234, "no such method"},
76     {0x6239, "out of memory"},
77     {0x6255, "null pointer"},
78     {0x6257, "array index out of bounds"},
79     {0x6258, "index out of bounds"},
80     {0x6281, "rec is corrupt"},
81     {0x6283, "invalid file"},
82     {0x6300, "auth failed"},
83     {0x6381, "invalid key"},
84     {0x67ff, "invalid length; should be %x"},
85     {0x6980, "bad param"},
86     {0x6982, "permission denied"},
87     {0x6983, "auth method blocked"},
88     {0x6984, "data invalid"},
89     {0x6985, "no file selected"},
90     {0x6987, "busy/SM missing"},
91     {0x6988, "SM wrong"},
92     {0x6a80, "invalid file type"},
93     {0x6a81, "function not supported"},
94     {0x6a82, "file not found"},
95     {0x6a83, "no such rec"},
96     {0x6b00, "wrong mode"},
97     {0x6cff, "wrong length; should be %x"},
98     {0x6d00, "unknown instruction"},
99     {0x6e00, "wrong class"},
100     {0x6f14, "invalid applet state"},
101     {0x6f15, "invalid state"},
102     {0x6f19, "applet already running"},
103     {0x6fb0, "uninitialized key"},
104     {0x9481, "bad state"},
105     {0x0000, NULL}
106 };
107 
108 #ifdef TEST
main(int ac,char * av[])109 main(int ac, char *av[])
110 {
111     int sw;
112     char *s;
113 
114     if (ac != 2) {
115 	fprintf(stderr, "usage: %s sw (in hex, please)\n", av[0]);
116 	exit(1);
117     }
118     sscanf(av[1], "%x", &sw);
119     sectok_print_sw(sw);
120     exit(0);
121 }
122 #endif
123 
sectok_print_sw(int sw)124 void sectok_print_sw(int sw)
125 {
126     printf("%s\n", sectok_get_sw(sw));
127 }
128 
sectok_get_sw(int sw)129 char *sectok_get_sw(int sw)
130 {
131     char *s;
132     static char buf[64];
133 
134     s = scsws(sw);
135     if (s)
136 	sprintf(buf, "%04x %s", sw, s);
137     else
138 	sprintf(buf, "%04x", sw);
139     return buf;
140 }
141 
scsws(int sw)142 static char *scsws(int sw)
143 {
144     int i, r1 = sectok_r1(sw), r2 = sectok_r2(sw), tr1, tr2;
145     static char buf[64];
146 
147     for (i = 0; r1r2s[i].s; i++) {
148 	tr1 = sectok_r1(r1r2s[i].sw);
149 	tr2 = sectok_r2(r1r2s[i].sw);
150 	if (tr1 == r1 && (tr2 == r2 || tr2 == 0xff))
151 	    break;
152     }
153 
154     if (sectok_r2(r1r2s[i].sw) != 0xff)
155 	return r1r2s[i].s;
156     sprintf(buf, r1r2s[i].s, r2);
157     return buf;
158 }
159 
160 #ifndef __palmos__
161 int
sectok_fdump_reply(FILE * f,unsigned char * p,int n,int sw)162 sectok_fdump_reply(FILE *f, unsigned char *p, int n, int sw)
163 {
164     int i;
165 
166     for (i = 0; i < n; i++)
167 	fprintf(f, "%d:%x ", i + 1, p[i]);
168     if (n)
169 	fprintf(f, "\n");
170     if (sw)
171 	fprintf(f, "%s\n", sectok_get_sw(sw));
172     return n;
173 }
174 
175 int
sectok_dump_reply(unsigned char * p,int n,int sw)176 sectok_dump_reply(unsigned char *p, int n, int sw)
177 {
178     return sectok_fdump_reply(stdout, p, n, sw);
179 }
180 #else
181 int
sectok_dump_reply(unsigned char * p,int n,int sw)182 sectok_dump_reply(unsigned char *p, int n, int sw)
183 {
184     int i;
185 
186     hidefield(printfield->id);
187     for (i = 0; i < n; i++)
188 	palmprintf("%d:%x ", i + 1, p[i]);
189     if (n)
190 	palmprintf("\n");
191     if (sw)
192 	palmprintf("%s\n", sectok_get_sw(sw));
193     showfield(printfield->id);
194     return n;
195 }
196 #endif
197