⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 block.c

📁 This little Program allows you to send commands to the CardReader (CT-BCS) or to the Card itself. F
💻 C
字号:
/* Copyright (C) 1998  Gregor A. Panstruga   see ctapi.h for details*/#include <stdlib.h>#include <stdio.h>#include <sys/time.h>#include <unistd.h>#include "scio.h"#include "tools.h"struct TLVObject{   unsigned char Tag;   unsigned int Length;   unsigned char Value[255];};int GetBlock (unsigned short fd,               unsigned char *STS,              unsigned char *LEN,              unsigned char *APDU,              int TimeOut){      int 			i;   int 			retval;   int			TOTAL_LEN = 0;   unsigned char 	CRC;      unsigned char 	chksum = 0;     if ((retval = scgetc(fd, STS, TimeOut)) != 0)   {      return -1;   }//   printf("STS = %xh\n", *STS);   if ((retval = scgetc(fd, LEN, 100)) != 0)   {         return -2;   }//   printf("LEN = %d\n", *LEN);   if ((retval = scgetc(fd, &CRC, 100)) != 0)   {      return -3;   }       if (*STS & 0x80 == 0x80)      TOTAL_LEN = 0x100 + *LEN;   else      TOTAL_LEN = *LEN;   for (i=0 ; i<TOTAL_LEN; i++)   {      if ((retval = scgetc(fd, &APDU[i], 100)) != 0)      {         return -4;      }      chksum = chksum ^ APDU[i];//      printf ("APDU[%d] = %xh\n", i, APDU[i]);   }   if ((chksum & 0xff) != (CRC & 0xff))   {      return -6;   }      return 0;   //everything ok!}   int SendBlock(unsigned short fd,              unsigned char CTL,              unsigned char LEN,              unsigned char *APDU){   int i;   int TOTAL_LEN;   int retval;   unsigned char CRC = 0;   if ((CTL & 0x20) == 0x20)      TOTAL_LEN = 0x100 + LEN;   else      TOTAL_LEN = LEN;    // Calculating Checksum//   printf("TOTAL_LEN = %d\n", TOTAL_LEN);   for (i=0; i < TOTAL_LEN; i++)   {      CRC = CRC ^ APDU[i];   }   retval = scputc(fd, CTL);//   printf("CTL = %x\n", CTL);   if (retval != 0)   {      return -1;   }   retval = scputc(fd, TOTAL_LEN);   if (retval != 0)   {      return -1;   }   retval = scputc(fd, CRC);   if (retval != 0)   {      return -1;   }       for (i=0; i < TOTAL_LEN; i++)   {      retval = scputc(fd, APDU[i]);//      printf("APDU [%d] = %x\n", i, APDU[i]);      if (retval != 0)      {         return -1;      }   }   return 0;}int DecodeTLV(struct TLVObject *TLV, int Lc, char *TLVString){   int i;   int counter = 0;   int ObjCounter = 0;   while  (counter < Lc)   {      TLV[ObjCounter].Tag = TLVString[counter];      counter++;      TLV[ObjCounter].Length = TLVString[counter];      counter++;      for (i=0; i<TLV[ObjCounter].Length; i++)      {         TLV[ObjCounter].Value[i] = TLVString[counter] & 0xff;         counter++;      }      ObjCounter++;   }   return ObjCounter;}   

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -