// Main entry to read file and decode into objects // xmlFile are command arg - relative or full path, dos path in windows int CommonAnalysis::ReadFile(const char *xmlFile,bool useWorkingDir) { // set directory of input file archiver->SetInputDirPath(xmlFile,useWorkingDir); // Initialize the XML4C2 system SAX2XMLReader* parser; try { XMLPlatformUtils::Initialize(); // Create a SAX parser object. parser=XMLReaderFactory::createXMLReader(); // Validation (first means yes or no, second means to skip if no DTD, even if yes) parser->setFeature(XMLString::transcode("http://xml.org/sax/features/validation"),GetValidate()); parser->setFeature(XMLString::transcode("http://apache.org/xml/features/validation/dynamic"),true); if(!GetValidate()) { parser->setFeature(XMLString::transcode("http://apache.org/xml/features/nonvalidating/load-external-dtd"), GetValidate()); } // default settings parser->setFeature(XMLString::transcode("http://apache.org/xml/features/validation/schema"),doSchema); parser->setFeature(XMLString::transcode("http://apache.org/xml/features/validation/schema-full-checking"),schemaFullChecking); } catch(const XMLException& toCatch) { cerr << "\nAn error occurred while initializing Xerces: " << StrX(toCatch.getMessage()) << endl; return XercesInitErr; } catch(std::bad_alloc&) { cerr << "\nMemory error initializing Xerces" << endl; return XercesInitErr; } catch( ... ) { cerr << "\nUnexpected error initializing Xerces" << endl; return XercesInitErr; } // parce the file #ifdef MPM_CODE MPMReadHandler *handler=new MPMReadHandler(); #else FEAReadHandler *handler=new FEAReadHandler(); #endif try { // create the handler parser->setContentHandler(handler); parser->setErrorHandler(handler); parser->parse(xmlFile); // Reading done, not clean up and exit #ifdef MPM_CODE delete parser; // Must be done prior to calling Terminate, below. #else // delete parser; // not sure why cannot delete the parser in FEA code #endif handler->FinishUp(); delete handler; XMLPlatformUtils::Terminate(); } catch(const XMLException& toCatch) { char *message = XMLString::transcode(toCatch.getMessage()); cerr << "\nParcing error: " << message << endl; return ReadFileErr; } catch(const SAXException& err) { char *message = XMLString::transcode(err.getMessage()); cerr << "\nInput file error: " << message << endl; return ReadFileErr; } catch(std::bad_alloc& ba) { cerr << "\nMemory error: " << ba.what() << endl; return ReadFileErr; } catch(const char *errMsg) { cerr << errMsg << endl; return ReadFileErr; } catch( ... ) { cerr << "Unexpected error occurred while reading XML file" << endl; return ReadFileErr; } return noErr; }
// 读取记录 bool CJMYPassPay::ReadRecords(const uint8 *readBuf, const int readLen, string &errcode) { static char temp[BUF_SIZE] = {0}; static uint8 initBuf[BUF_SIZE] = {0}; int initLen = g_readLen; memcpy(initBuf, g_readBuff, g_readLen); // 找出应用文件定位器,tag为94,取出短文件标识符读记录 int i = 0; for (; i < initLen; i++) { if (0X94 == initBuf[i]) { // 找到了短文件标识符,获取短文件标识符的长度 int sfiLen = initBuf[i + 1]; if (0 != sfiLen % 4) { // 短文件标识符长度有错 PrintLog("Find SFI length error!"); return false; } // 把短文件标识符读出 int iStart = i + 2; memset(temp, 0x00, sizeof(temp)); for (int l = 0; l < sfiLen; l++) { sprintf(&temp[l*3], "%02X ", initBuf[iStart++]); } PrintLog("SFI:%s", temp); // 短文件标识符每四个字节为一组,分别是:短文件标示,第一记录编号,最后一条记录编号,存放用于脱机数据认证的数据的连续记录个数 iStart = i + 2; // 指向第一组数据 int iGroup = sfiLen / 4; bool bRead9F74 = false; bool bRead5F25 = false; bool bRead5F24 = false; for (int j = 0; j < iGroup; j++) { // 开始处理一组数据 uint8 sfi = initBuf[iStart + (j * 4)]; // sfi uint8 first = initBuf[iStart + (j * 4) + 1]; // start record uint8 last = initBuf[iStart + (j * 4) + 2]; // end record uint8 otherRec = initBuf[iStart + (j * 4) + 3]; uint8 iReadType = sfi | 0X04; //读指定标识符的记录 0100 int readRecLen = 0; char hexBuf[4] = {0}; string readRecCmd; static uint8 readRecBuff[4096] = {0}; for(int k = first; k <= last; k++) { readRecCmd = "31 00 B2 "; memset(hexBuf,0X00, sizeof(hexBuf)); sprintf(hexBuf, "%02X ", k); // 指定要读取的记录编号 readRecCmd += hexBuf; memset(hexBuf,0X00, sizeof(hexBuf)); sprintf(hexBuf, "%02X ", iReadType); // 指定读取的方式为读取当前记录 readRecCmd += hexBuf; memset(g_readBuff, 0x00, sizeof(g_readBuff)); PrintLog("record cmd:%s", readRecCmd.c_str()); if ( false == WriteData(readRecCmd)) { PrintLog("Send cmd:%s filed", readRecCmd.c_str()); return false; } GetReadRes(); readRecLen = g_readLen; memset(temp, 0x00, sizeof(temp)); for (int i = 0; i < readRecLen; i++) { sprintf(temp + i * 3, "%02X ", g_readBuff[i]); } PrintLog("send cmd:%s\n recv:%s", readRecCmd.c_str(), temp); if (g_readLen > 3) { char sw1sw2[6] = {0}; snprintf(sw1sw2, sizeof(sw1sw2), "%02X%02X", g_readBuff[g_readLen-3], g_readBuff[g_readLen-2]); //PrintLog("************sw1sw = %s************", sw1sw2); //请求的记录不存在,卡片返回 SW1 SW2=“6A83” if(sw1sw2 == NO_RECORD) { PrintLog("命令:%s 读记录失败!", readRecCmd.c_str()); errcode = NO_RECORD; continue; } else if (sw1sw2 != SUCCESS_CODE) { PrintLog("发生了未知的错误:sw1sw2=%s", sw1sw2); errcode = UNKNOW_ERROR; continue; } } // 读取 卡行授权码 if (false == bRead9F74) { bRead9F74 = Get9F74Value(g_readBuff, readLen); } // 读取卡片生效日期 if (false == bRead5F25) { bRead5F25 = GetValidate(g_readBuff, readLen); if(bRead5F25) { int currDateValue = atoi(m_CurrDate.c_str()); // 生效日期检查 if (m_Validate.length() > 0) { int validateValue = atoi(m_Validate.c_str()); PrintLog("生效日期:%d", validateValue); if (validateValue > currDateValue) { PrintLog("卡片未生效"); return false; } } } } // 读取卡片失效日期 if (false == bRead5F24) { bRead5F24 = GetInvalidate(g_readBuff, readLen); if (bRead5F24) { // 失效日期检查 int currDateValue = atoi(m_CurrDate.c_str()); int invalidateValue = atoi(m_Invalidate.c_str()); PrintLog("失效日期:%d", invalidateValue); if (currDateValue > invalidateValue) { PrintLog("卡片已失效"); return false; } } } } } if (false == bRead9F74) { // 所有记录都读完毕了,还没有读9F74则认为是失败 PrintLog("Find tag9F74 value failed!"); return false; } break; } } if (i >= initLen) { PrintLog("读记录时,查找短文件标识符失败"); return false; } return true; }