summaryrefslogtreecommitdiffstats
path: root/tools/ccidmon.c
blob: 4e99da54de9879243e036abf1ec72afaea7f03dd (plain)
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
/* ccidmon.c - CCID monitor for use with the Linux usbmon facility.
 *	Copyright (C) 2009 Free Software Foundation, Inc.
 *
 * This file is part of GnuPG.
 *
 * GnuPG 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.
 *
 * GnuPG 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 this program; if not, see <https://www.gnu.org/licenses/>.
 */


/* This utility takes the output of usbmon, filters out the bulk data
   and prints the CCID messages in a human friendly way.

*/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <errno.h>
#include <stdarg.h>
#include <assert.h>
#include <unistd.h>
#include <signal.h>


#ifndef PACKAGE_VERSION
# define PACKAGE_VERSION "[build on " __DATE__ " " __TIME__ "]"
#endif
#ifndef PACKAGE_BUGREPORT
# define PACKAGE_BUGREPORT "devnull@example.org"
#endif
#define PGM "ccidmon"
#ifndef GNUPG_NAME
# define GNUPG_NAME "GnuPG"
#endif

/* Option flags. */
static int verbose;
static int debug;
static int skip_escape;
static int usb_bus, usb_dev;
static int sniffusb;


/* Error counter.  */
static int any_error;

/* Data storage.  */
struct
{
  int is_bi;
  char timestamp[20];
  char address[50];
  int count;
  char data[16000];
} databuffer;


enum {
  RDR_to_PC_NotifySlotChange= 0x50,
  RDR_to_PC_HardwareError   = 0x51,

  PC_to_RDR_SetParameters   = 0x61,
  PC_to_RDR_IccPowerOn      = 0x62,
  PC_to_RDR_IccPowerOff     = 0x63,
  PC_to_RDR_GetSlotStatus   = 0x65,
  PC_to_RDR_Secure          = 0x69,
  PC_to_RDR_T0APDU          = 0x6a,
  PC_to_RDR_Escape          = 0x6b,
  PC_to_RDR_GetParameters   = 0x6c,
  PC_to_RDR_ResetParameters = 0x6d,
  PC_to_RDR_IccClock        = 0x6e,
  PC_to_RDR_XfrBlock        = 0x6f,
  PC_to_RDR_Mechanical      = 0x71,
  PC_to_RDR_Abort           = 0x72,
  PC_to_RDR_SetDataRate     = 0x73,

  RDR_to_PC_DataBlock       = 0x80,
  RDR_to_PC_SlotStatus      = 0x81,
  RDR_to_PC_Parameters      = 0x82,
  RDR_to_PC_Escape          = 0x83,
  RDR_to_PC_DataRate        = 0x84
};


#define digitp(p)   (*(p) >= '0' && *(p) <= '9')
#define hexdigitp(a) (digitp (a)                     \
                      || (*(a) >= 'A' && *(a) <= 'F')  \
                      || (*(a) >= 'a' && *(a) <= 'f'))
#define ascii_isspace(a) ((a)==' ' || (a)=='\n' || (a)=='\r' || (a)=='\t')
#define xtoi_1(p)   ((p) <= '9'? ((p)- '0'): \
                     (p) <= 'F'? ((p)-'A'+10):((p)-'a'+10))



/* Print diagnostic message and exit with failure. */
static void
die (const char *format, ...)
{
  va_list arg_ptr;

  fflush (stdout);
  fprintf (stderr, "%s: ", PGM);

  va_start (arg_ptr, format);
  vfprintf (stderr, format, arg_ptr);
  va_end (arg_ptr);
  putc ('\n', stderr);

  exit (1);
}


/* Print diagnostic message. */
static void
err (const char *format, ...)
{
  va_list arg_ptr;

  any_error = 1;

  fflush (stdout);
  fprintf (stderr, "%s: ", PGM);

  va_start (arg_ptr, format);
  vfprintf (stderr, format, arg_ptr);
  va_end (arg_ptr);
  putc ('\n', stderr);
}


/* Convert a little endian stored 4 byte value into an unsigned
   integer. */
static unsigned int
convert_le_u32 (const unsigned char *buf)
{
  return buf[0] | (buf[1] << 8) | (buf[2] << 16) | ((unsigned int)buf[3] << 24);
}


/* Convert a little endian stored 2 byte value into an unsigned
   integer. */
static unsigned int
convert_le_u16 (const unsigned char *buf)
{
  return buf[0] | (buf[1] << 8);
}




static void
print_pr_data (const unsigned char *data, size_t datalen, size_t off)
{
  int needlf = 0;
  int first = 1;

  for (; off < datalen; off++)
    {
      if (!(off % 16) || first)
        {
          if (needlf)
            putchar ('\n');
          printf ("  [%04lu] ", (unsigned long)off);
        }
      printf (" %02X", data[off]);
      needlf = 1;
      first = 0;
    }
  if (needlf)
    putchar ('\n');
}


static void
print_p2r_header (const char *name, const unsigned char *msg, size_t msglen)
{
  printf ("%s:\n", name);
  if (msglen < 7)
    return;
  printf ("  dwLength ..........: %u\n", convert_le_u32 (msg+1));
  printf ("  bSlot .............: %u\n", msg[5]);
  printf ("  bSeq ..............: %u\n", msg[6]);
}


static void
print_p2r_iccpoweron (const unsigned char *msg, size_t msglen)
{
  print_p2r_header ("PC_to_RDR_IccPowerOn", msg, msglen);
  if (msglen < 10)
    return;
  printf ("  bPowerSelect ......: 0x%02x (%s)\n", msg[7],
          msg[7] == 0? "auto":
          msg[7] == 1? "5.0 V":
          msg[7] == 2? "3.0 V":
          msg[7] == 3? "1.8 V":"");
  print_pr_data (msg, msglen, 8);
}


static void
print_p2r_iccpoweroff (const unsigned char *msg, size_t msglen)
{
  print_p2r_header ("PC_to_RDR_IccPowerOff", msg, msglen);
  print_pr_data (msg, msglen, 7);
}


static void
print_p2r_getslotstatus (const unsigned char *msg, size_t msglen)
{
  print_p2r_header ("PC_to_RDR_GetSlotStatus", msg, msglen);
  print_pr_data (msg, msglen, 7);
}


static void
print_p2r_xfrblock (const unsigned char *msg, size_t msglen)
{
  unsigned int val;

  print_p2r_header ("PC_to_RDR_XfrBlock", msg, msglen);
  if (msglen < 10)
    return;
  printf ("  bBWI ..............: 0x%02x\n", msg[7]);
  val = convert_le_u16 (msg+8);
  printf ("  wLevelParameter ...: 0x%04x%s\n", val,
          val == 1? " (continued)":
          val == 2? " (continues+ends)":
          val == 3? " (continues+continued)":
          val == 16? " (DataBlock-expected)":"");
  print_pr_data (msg, msglen, 10);
}


static void
print_p2r_getparameters (const unsigned char *msg, size_t msglen)
{
  print_p2r_header ("PC_to_RDR_GetParameters", msg, msglen);
  print_pr_data (msg, msglen, 7);
}


static void
print_p2r_resetparameters (const unsigned char *msg, size_t msglen)
{
  print_p2r_header ("PC_to_RDR_ResetParameters", msg, msglen);
  print_pr_data (msg, msglen, 7);
}


static void
print_p2r_setparameters (const unsigned char *msg, size_t msglen)
{
  print_p2r_header ("PC_to_RDR_SetParameters", msg, msglen);
  if (msglen < 10)
    return;
  printf ("  bProtocolNum ......: 0x%02x\n", msg[7]);
  print_pr_data (msg, msglen, 8);
}


static void
print_p2r_escape (const unsigned char *msg, size_t msglen)
{
  if (skip_escape)
    return;
  print_p2r_header ("PC_to_RDR_Escape", msg, msglen);
  print_pr_data (msg, msglen, 7);
}


static void
print_p2r_iccclock (const unsigned char *msg, size_t msglen)
{
  print_p2r_header ("PC_to_RDR_IccClock", msg, msglen);
  if (msglen < 10)
    return;
  printf ("  bClockCommand .....: 0x%02x\n", msg[7]);
  print_pr_data (msg, msglen, 8);
}


static void
print_p2r_to0apdu (const unsigned char *msg, size_t msglen)
{
  print_p2r_header ("PC_to_RDR_T0APDU", msg, msglen);
  if (msglen < 10)
    return;
  printf ("  bmChanges .........: 0x%02x\n", msg[7]);
  printf ("  bClassGetResponse .: 0x%02x\n", msg[8]);
  printf ("  bClassEnvelope ....: 0x%02x\n", msg[9]);
  print_pr_data (msg, msglen, 10);
}


static void
print_p2r_secure (const unsigned char *msg, size_t msglen)
{
  unsigned int val;

  print_p2r_header ("PC_to_RDR_Secure", msg, msglen);
  if (msglen < 10)
    return;
  printf ("  bBMI ..............: 0x%02x\n", msg[7]);
  val = convert_le_u16 (msg+8);
  printf ("  wLevelParameter ...: 0x%04x%s\n", val,
          val == 1? " (continued)":
          val == 2? " (continues+ends)":
          val == 3? " (continues+continued)":
          val == 16? " (DataBlock-expected)":"");
  print_pr_data (msg, msglen, 10);
}


static void
print_p2r_mechanical (const unsigned char *msg, size_t msglen)
{
  print_p2r_header ("PC_to_RDR_Mechanical", msg, msglen);
  if (msglen < 10)
    return;
  printf ("  bFunction .........: 0x%02x\n", msg[7]);
  print_pr_data (msg, msglen, 8);
}


static void
print_p2r_abort (const unsigned char *msg, size_t msglen)
{
  print_p2r_header ("PC_to_RDR_Abort", msg, msglen);
  print_pr_data (msg, msglen, 7);
}


static void
print_p2r_setdatarate (const unsigned char *msg, size_t msglen)
{
  print_p2r_header ("PC_to_RDR_SetDataRate", msg, msglen);
  if (msglen < 10)
    return;
  print_pr_data (msg, msglen, 7);
}


static void
print_p2r_unknown (const unsigned char *msg, size_t msglen)
{
  char buf[100];

  snprintf (buf, sizeof buf, "Unknown PC_to_RDR command 0x%02X",
            msglen? msg[0]:0);
  print_p2r_header (buf, msg, msglen);
  if (msglen < 10)
    return;
  print_pr_data (msg, msglen, 0);
}


static void
print_p2r (const unsigned char *msg, size_t msglen)
{
  switch (msglen? msg[0]:0)
    {
    case PC_to_RDR_IccPowerOn:
      print_p2r_iccpoweron (msg, msglen);
      break;
    case PC_to_RDR_IccPowerOff:
      print_p2r_iccpoweroff (msg, msglen);
      break;
    case PC_to_RDR_GetSlotStatus:
      print_p2r_getslotstatus (msg, msglen);
      break;
    case PC_to_RDR_XfrBlock:
      print_p2r_xfrblock (msg, msglen);
      break;
    case PC_to_RDR_GetParameters:
      print_p2r_getparameters (msg, msglen);
      break;
    case PC_to_RDR_ResetParameters:
      print_p2r_resetparameters (msg, msglen);
      break;
    case PC_to_RDR_SetParameters:
      print_p2r_setparameters (msg, msglen);
      break;
    case PC_to_RDR_Escape:
      print_p2r_escape (msg, msglen);
      break;
    case PC_to_RDR_IccClock:
      print_p2r_iccclock (msg, msglen);
      break;
    case PC_to_RDR_T0APDU:
      print_p2r_to0apdu (msg, msglen);
      break;
    case PC_to_RDR_Secure:
      print_p2r_secure (msg, msglen);
      break;
    case PC_to_RDR_Mechanical:
      print_p2r_mechanical (msg, msglen);
      break;
    case PC_to_RDR_Abort:
      print_p2r_abort (msg, msglen);
      break;
    case PC_to_RDR_SetDataRate:
      print_p2r_setdatarate (msg, msglen);
      break;
    default:
      print_p2r_unknown (msg, msglen);
      break;
    }
}


static void
print_r2p_header (const char *name, const unsigned char *msg, size_t msglen)
{
  printf ("%s:\n", name);
  if (msglen < 9)
    return;
  printf ("  dwLength ..........: %u\n", convert_le_u32 (msg+1));
  printf ("  bSlot .............: %u\n", msg[5]);
  printf ("  bSeq ..............: %u\n", msg[6]);
  printf ("  bStatus ...........: %u\n", msg[7]);
  if (msg[8])
    printf ("  bError ............: %u\n", msg[8]);
}


static void
print_r2p_datablock (const unsigned char *msg, size_t msglen)
{
  print_r2p_header ("RDR_to_PC_DataBlock", msg, msglen);
  if (msglen < 10)
    return;
  if (msg[9])
    printf ("  bChainParameter ...: 0x%02x%s\n", msg[9],
            msg[9] == 1? " (continued)":
            msg[9] == 2? " (continues+ends)":
            msg[9] == 3? " (continues+continued)":
            msg[9] == 16? " (XferBlock-expected)":"");
  print_pr_data (msg, msglen, 10);
}


static void
print_r2p_slotstatus (const unsigned char *msg, size_t msglen)
{
  print_r2p_header ("RDR_to_PC_SlotStatus", msg, msglen);
  if (msglen < 10)
    return;
  printf ("  bClockStatus ......: 0x%02x%s\n", msg[9],
          msg[9] == 0? " (running)":
          msg[9] == 1? " (stopped-L)":
          msg[9] == 2? " (stopped-H)":
          msg[9] == 3? " (stopped)":"");
  print_pr_data (msg, msglen, 10);
}


static void
print_r2p_parameters (const unsigned char *msg, size_t msglen)
{
  print_r2p_header ("RDR_to_PC_Parameters", msg, msglen);
  if (msglen < 10)
    return;

  printf ("  protocol ..........: T=%d\n", msg[9]);
  if (msglen == 17 && msg[9] == 1)
    {
      /* Protocol T=1.  */
      printf ("  bmFindexDindex ....: %02X\n", msg[10]);
      printf ("  bmTCCKST1 .........: %02X\n", msg[11]);
      printf ("  bGuardTimeT1 ......: %02X\n", msg[12]);
      printf ("  bmWaitingIntegersT1: %02X\n", msg[13]);
      printf ("  bClockStop ........: %02X\n", msg[14]);
      printf ("  bIFSC .............: %d\n", msg[15]);
      printf ("  bNadValue .........: %d\n", msg[16]);
    }
  else
    print_pr_data (msg, msglen, 10);
}


static void
print_r2p_escape (const unsigned char *msg, size_t msglen)
{
  if (skip_escape)
    return;
  print_r2p_header ("RDR_to_PC_Escape", msg, msglen);
  if (msglen < 10)
    return;
  printf ("  buffer[9] .........: %02X\n", msg[9]);
  print_pr_data (msg, msglen, 10);
}


static void
print_r2p_datarate (const unsigned char *msg, size_t msglen)
{
  print_r2p_header ("RDR_to_PC_DataRate", msg, msglen);
  if (msglen < 10)
    return;
  if (msglen >= 18)
    {
      printf ("  dwClockFrequency ..: %u\n", convert_le_u32 (msg+10));
      printf ("  dwDataRate ..... ..: %u\n", convert_le_u32 (msg+14));
      print_pr_data (msg, msglen, 18);
    }
  else
    print_pr_data (msg, msglen, 10);
}


static void
print_r2p_unknown (const unsigned char *msg, size_t msglen)
{
  char buf[100];

  snprintf (buf, sizeof buf, "Unknown RDR_to_PC command 0x%02X",
            msglen? msg[0]:0);
  print_r2p_header (buf, msg, msglen);
  if (msglen < 10)
    return;
  printf ("  bMessageType ......: %02X\n", msg[0]);
  printf ("  buffer[9] .........: %02X\n", msg[9]);
  print_pr_data (msg, msglen, 10);
}


static void
print_r2p (const unsigned char *msg, size_t msglen)
{
  switch (msglen? msg[0]:0)
    {
    case RDR_to_PC_DataBlock:
      print_r2p_datablock (msg, msglen);
      break;
    case RDR_to_PC_SlotStatus:
      print_r2p_slotstatus (msg, msglen);
      break;
    case RDR_to_PC_Parameters:
      print_r2p_parameters (msg, msglen);
      break;
    case RDR_to_PC_Escape:
      print_r2p_escape (msg, msglen);
      break;
    case RDR_to_PC_DataRate:
      print_r2p_datarate (msg, msglen);
      break;
    default:
      print_r2p_unknown (msg, msglen);
      break;
    }

}


static void
flush_data (void)
{
  if (!databuffer.count)
    return;

  if (verbose)
    {
      printf ("Timestamp: %s\n", databuffer.timestamp);
      printf ("Address..: %s\n", databuffer.address);
    }
  if (databuffer.is_bi)
    {
      print_r2p (databuffer.data, databuffer.count);
      if (verbose)
        putchar ('\n');
    }
  else
    print_p2r (databuffer.data, databuffer.count);

  databuffer.count = 0;
}

static void
collect_data (char *hexdata, const char *timestamp,
              const char *address, unsigned int lineno)
{
  size_t length;
  int is_bi;
  char *s;
  unsigned int value;

  is_bi = (*address && address[1] == 'i');

  if (databuffer.is_bi != is_bi || strcmp (databuffer.address, address))
    flush_data ();
  databuffer.is_bi = is_bi;
  if (strlen (timestamp) >= sizeof databuffer.timestamp)
    die ("timestamp field too long");
  strcpy (databuffer.timestamp, timestamp);
  if (strlen (address) >= sizeof databuffer.address)
    die ("address field too long");
  strcpy (databuffer.address, address);

  length = databuffer.count;
  for (s=hexdata; *s; s++ )
    {
      if (ascii_isspace (*s))
        continue;
      if (!hexdigitp (s))
        {
          err ("invalid hex digit in line %u - line skipped", lineno);
          break;
        }
      value = xtoi_1 (*s) * 16;
      s++;
      if (!hexdigitp (s))
        {
          err ("invalid hex digit in line %u - line skipped", lineno);
          break;
        }
      value += xtoi_1 (*s);

      if (length >= sizeof (databuffer.data))
        {
          err ("too much data at line %u - can handle only up to %zu bytes",
               lineno, sizeof (databuffer.data));
          break;
        }
      databuffer.data[length++] = value;
    }
  databuffer.count = length;
}


static void
parse_line (char *line, unsigned int lineno)
{
  char *p;
  char *timestamp, *event_type, *address, *data, *status, *datatag;

  if (*line == '#' || !*line)
    return;

  if (debug)
    printf ("line[%u] ='%s'\n", lineno, line);

  p = strtok (line, " ");
  if (!p)
    die ("invalid line %d (no URB)", lineno);
  timestamp = strtok (NULL, " ");
  if (!timestamp)
    die ("invalid line %d (no timestamp)", lineno);
  event_type = strtok (NULL, " ");
  if (!event_type)
    die ("invalid line %d (no event type)", lineno);
  address = strtok (NULL, " ");
  if (!address)
    die ("invalid line %d (no address", lineno);
  if (usb_bus || usb_dev)
    {
      int bus, dev;

      p = strchr (address, ':');
      if (!p)
        die ("invalid line %d (invalid address", lineno);
      p++;
      bus = atoi (p);
      p = strchr (p, ':');
      if (!p)
        die ("invalid line %d (invalid address", lineno);
      p++;
      dev = atoi (p);

      if ((usb_bus && usb_bus != bus) || (usb_dev && usb_dev != dev))
        return;  /* We don't want that one.  */
    }
  if (*address == 'B' && (address[1] == 'o' || address[1] == 'i'))
    ; /* We want block ind and out.  */
  else if (*address == 'C' && (address[1] == 'o' || address[1] == 'i'))
    ; /* We want control ind and out.  */
  else
    return; /* But nothing else.  */
  status = strtok (NULL, " ");
  if (!status)
    return;
  if (!strchr ("-0123456789", *status))
    return; /* Setup packet.  */
  /* We don't support "Z[io]" types thus we don't need to check here.  */
  p = strtok (NULL, " ");
  if (!p)
    return; /* No data length.  */

  datatag = strtok (NULL, " ");
  if (datatag && *datatag == '=')
    {
      data = strtok (NULL, "");
      collect_data (data?data:"", timestamp, address, lineno);
    }
}


static void
parse_line_sniffusb (char *line, unsigned int lineno)
{
  char *p;

  if (debug)
    printf ("line[%u] ='%s'\n", lineno, line);

  p = strtok (line, " \t");
  if (!p)
    return;
  p = strtok (NULL, " \t");
  if (!p)
    return;
  p = strtok (NULL, " \t");
  if (!p)
    return;

  if (hexdigitp (p+0) && hexdigitp (p+1)
      && hexdigitp (p+2) && hexdigitp (p+3)
      && p[4] == ':' && !p[5])
    {
      size_t length;
      unsigned int value;

      length = databuffer.count;
      while ((p=strtok (NULL, " \t")))
        {
          if (!hexdigitp (p+0) || !hexdigitp (p+1))
            {
              err ("invalid hex digit in line %u (%s)", lineno,p);
              break;
            }
          value = xtoi_1 (p[0]) * 16 + xtoi_1 (p[1]);

          if (length >= sizeof (databuffer.data))
            {
              err ("too much data at line %u - can handle only up to % bytes",
                   lineno, sizeof (databuffer.data));
              break;
            }
          databuffer.data[length++] = value;
        }
      databuffer.count = length;

    }
  else if (!strcmp (p, "TransferFlags"))
    {
      flush_data ();

      *databuffer.address = 0;
      while ((p=strtok (NULL, " \t(,)")))
        {
          if (!strcmp (p, "USBD_TRANSFER_DIRECTION_IN"))
            {
              databuffer.is_bi = 1;
              break;
            }
          else if (!strcmp (p, "USBD_TRANSFER_DIRECTION_OUT"))
            {
              databuffer.is_bi = 0;
              break;
            }
        }
    }

}


static void
parse_input (FILE *fp)
{
  char line[2000];
  size_t length;
  unsigned int lineno = 0;

  while (fgets (line, sizeof (line), fp))
    {
      lineno++;
      length = strlen (line);
      if (length && line[length - 1] == '\n')
	line[--length] = 0;
      else
        err ("line number %u too long or last line not terminated", lineno);
      if (length && line[length - 1] == '\r')
	line[--length] = 0;
      if (sniffusb)
        parse_line_sniffusb (line, lineno);
      else
        parse_line (line, lineno);
    }
  flush_data ();
  if (ferror (fp))
    err ("error reading input at line %u: %s", lineno, strerror (errno));
}


int
main (int argc, char **argv)
{
  int last_argc = -1;

  if (argc)
    {
      argc--; argv++;
    }
  while (argc && last_argc != argc )
    {
      last_argc = argc;
      if (!strcmp (*argv, "--"))
        {
          argc--; argv++;
          break;
        }
      else if (!strcmp (*argv, "--version"))
        {
          fputs (PGM " (" GNUPG_NAME ") " PACKAGE_VERSION "\n", stdout);
          exit (0);
        }
      else if (!strcmp (*argv, "--help"))
        {
          puts ("Usage: " PGM " [BUS:DEV]\n"
                "Parse the output of usbmod assuming it is CCID compliant.\n\n"
                "  --skip-escape  do not show escape packets\n"
                "  --sniffusb     Assume output from Sniffusb.exe\n"
                "  --verbose      enable extra informational output\n"
                "  --debug        enable additional debug output\n"
                "  --help         display this help and exit\n\n"
                "Report bugs to " PACKAGE_BUGREPORT ".");
          exit (0);
        }
      else if (!strcmp (*argv, "--verbose"))
        {
          verbose = 1;
          argc--; argv++;
        }
      else if (!strcmp (*argv, "--debug"))
        {
          verbose = debug = 1;
          argc--; argv++;
        }
      else if (!strcmp (*argv, "--skip-escape"))
        {
          skip_escape = 1;
          argc--; argv++;
        }
      else if (!strcmp (*argv, "--sniffusb"))
        {
          sniffusb = 1;
          argc--; argv++;
        }
    }

  if (argc && sniffusb)
    die ("no arguments expected when using --sniffusb\n");
  else if (argc > 1)
    die ("usage: " PGM " [BUS:DEV]  (try --help for more information)\n");

  if (argc == 1)
    {
      const char *s = strchr (argv[0], ':');

      usb_bus = atoi (argv[0]);
      if (s)
        usb_dev =  atoi (s+1);
      if (usb_bus < 1 || usb_bus > 999 || usb_dev < 1 || usb_dev > 999)
        die ("invalid bus:dev specified");
    }


  signal (SIGPIPE, SIG_IGN);

  parse_input (stdin);

  return any_error? 1:0;
}


/*
Local Variables:
compile-command: "gcc -Wall -Wno-pointer-sign -g -o ccidmon ccidmon.c"
End:
*/