123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- #define _XOPEN_SOURCE
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <memory.h>
- #include <time.h>
- #include <stdint.h>
- #include "message_file.h"
- #include "zstream.h"
- namespace
- {
- char decode[256];
- char encode[256][3];
- bool code_init_flag=false;
- void init_code()
- {
- memset(decode,0,sizeof(decode));
- memset(encode,0,sizeof(encode));
- for(int c='A';c<='F';c++)
- decode[c]=c-'A'+10;
- for(int c='0';c<='9';c++)
- decode[c]=c-'0';
-
- for(int i=0;i<256;i++)
- {
- sprintf(encode[i],"%02X",i);
- }
-
- decode['\r']=-1;
- decode['\n']=-1;
- decode[' ']=-1;
- }
- inline int ch2int(const char c)
- {
- return (int) (uint8_t) c;
- }
- inline int convert(const char*p,char*o)
- {
- char c;
- char*r=o;
- for(;*p;p++)
- {
- if(decode[ch2int(*p)]<0)
- continue;
- c=decode[ch2int(*p++)]<<4;
- *o++=c+decode[ch2int(*p)];
- }
- return o-r;
- }
- }
- message_file::message_file(FILE*fp)
- {
- m_last_line[0]=0;
- m_file=fp;
- m_owner=false;
-
- if(!code_init_flag)
- {
- init_code();
- code_init_flag=true;
- }
- }
- message_file::message_file(const char*fname)
- {
- m_last_line[0]=0;
- m_file=fopen(fname,"r");
- m_owner=true;
-
- if(!code_init_flag)
- {
- init_code();
- code_init_flag=true;
- }
- }
- message_file::~message_file()
- {
- if(m_owner)
- fclose(m_file);
- }
- inline const char* now(char*date_str,uint64_t time)
- {
- time_t ntime=time/1000;
- struct tm buff;
- const struct tm*t=localtime_r(&ntime, &buff);
- sprintf(date_str,"%d-%02d-%02d %02d:%02d:%02d.%03d" ,
- t->tm_year+1900,t->tm_mon+1,t->tm_mday,
- t->tm_hour,t->tm_min,t->tm_sec,(int)(time%1000));
- return date_str;
- }
- int message_file::put_line(FILE*fp,uint64_t time,char*buf,int buflen)
- {
- char b[8192];
- fprintf(fp,"[%s]\n",now(b,time));
- int n=0,t;
- for(int i=0;i<buflen;i++)
- {
- t=(int)(unsigned char)buf[i];
- n+=sprintf(&b[n],"%s ",encode[t]);
- if((i+1)%32 == 0)
- {
- n+=sprintf(&b[n],"\n");
- }
- }
- fprintf(fp,"%s\n",b);
- return 0;
- }
- int line_type(const char*s)
- {
- if(s[0]==0 || s[0]=='\n')
- return 0;
-
- if(strncmp("2019-",s,5)==0)
- return 1;
-
- return 2;
- }
- int message_file::get_line(uint64_t*time,char*buf,int buflen)
- {
- return get_line(time,buf,buflen,0);
- }
- int message_file::get_line(uint64_t*time,char*buf,int buflen,char*m)
- {
- int type=0;
-
- while(1)
- {
- if(fgets(m_last_line,sizeof(m_last_line),m_file)==nullptr)
- return 0;
- type=line_type(m_last_line);
- if(type==0)
- continue;
- if(type==1)
- {
- struct tm tm;
- memset(&tm, 0, sizeof(struct tm));
- const char*ms=strptime(&m_last_line[0], "%Y-%m-%d %H:%M:%S", &tm);
- *time=1000LL*mktime(&tm)+atoi(ms+1);
- if(m) strcpy(m,m_last_line);
- continue;
- }
- break;
- }
- char*o=buf;
- while(1)
- {
- if(line_type(m_last_line)<=1)
- break;
- o+=convert(m_last_line,o);
- if(fgets(m_last_line,sizeof(m_last_line),m_file)==nullptr)
- return 0;
- }
- return o-buf;
- }
- int message_file::get_card_message(uint32_t card_id,char*b)
- {
- int len;
- char buf[2048];
- uint64_t time=0;
- int ppos=0;
- while((len=get_line(&time,buf,sizeof(buf)))>0)
- {
- zistream is(buf,len-2);
- uint16_t cmd;
- is>>skip(2)>>cmd;
- if(cmd!=0x843b && cmd!=0x753b)
- continue;
- is>>skip(16);
- while(!is.eof())
- {
- uint32_t cid;
- uint32_t gpos=is.pos();
- is>>skip(1)>>cid;
- if(cid!=card_id) continue;
- if(ppos==0) memcpy(b,buf,ppos=20);
- memcpy(&b[ppos],&buf[gpos],20);
- ppos+=20;
- }
- if(ppos==0) continue;
- ppos+=2;
- }
- return -1;
- }
|