1
0

message_file.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #define _XOPEN_SOURCE
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <memory.h>
  6. #include <time.h>
  7. #include <stdint.h>
  8. #include "message_file.h"
  9. #include "zstream.h"
  10. namespace
  11. {
  12. char decode[256];
  13. char encode[256][3];
  14. bool code_init_flag=false;
  15. void init_code()
  16. {
  17. memset(decode,0,sizeof(decode));
  18. memset(encode,0,sizeof(encode));
  19. for(int c='A';c<='F';c++)
  20. decode[c]=c-'A'+10;
  21. for(int c='0';c<='9';c++)
  22. decode[c]=c-'0';
  23. for(int i=0;i<256;i++)
  24. {
  25. sprintf(encode[i],"%02X",i);
  26. }
  27. decode['\r']=-1;
  28. decode['\n']=-1;
  29. decode[' ']=-1;
  30. }
  31. inline int ch2int(const char c)
  32. {
  33. return (int) (uint8_t) c;
  34. }
  35. inline int convert(const char*p,char*o)
  36. {
  37. char c;
  38. char*r=o;
  39. for(;*p;p++)
  40. {
  41. if(decode[ch2int(*p)]<0)
  42. continue;
  43. c=decode[ch2int(*p++)]<<4;
  44. *o++=c+decode[ch2int(*p)];
  45. }
  46. return o-r;
  47. }
  48. }
  49. message_file::message_file(FILE*fp)
  50. {
  51. m_last_line[0]=0;
  52. m_file=fp;
  53. m_owner=false;
  54. if(!code_init_flag)
  55. {
  56. init_code();
  57. code_init_flag=true;
  58. }
  59. }
  60. message_file::message_file(const char*fname)
  61. {
  62. m_last_line[0]=0;
  63. m_file=fopen(fname,"r");
  64. m_owner=true;
  65. if(!code_init_flag)
  66. {
  67. init_code();
  68. code_init_flag=true;
  69. }
  70. }
  71. message_file::~message_file()
  72. {
  73. if(m_owner)
  74. fclose(m_file);
  75. }
  76. inline const char* now(char*date_str,uint64_t time)
  77. {
  78. time_t ntime=time/1000;
  79. struct tm buff;
  80. const struct tm*t=localtime_r(&ntime, &buff);
  81. sprintf(date_str,"%d-%02d-%02d %02d:%02d:%02d.%03d" ,
  82. t->tm_year+1900,t->tm_mon+1,t->tm_mday,
  83. t->tm_hour,t->tm_min,t->tm_sec,(int)(time%1000));
  84. return date_str;
  85. }
  86. int message_file::put_line(FILE*fp,uint64_t time,char*buf,int buflen)
  87. {
  88. char b[8192];
  89. fprintf(fp,"[%s]\n",now(b,time));
  90. int n=0,t;
  91. for(int i=0;i<buflen;i++)
  92. {
  93. t=(int)(unsigned char)buf[i];
  94. n+=sprintf(&b[n],"%s ",encode[t]);
  95. if((i+1)%32 == 0)
  96. {
  97. n+=sprintf(&b[n],"\n");
  98. }
  99. }
  100. fprintf(fp,"%s\n",b);
  101. return 0;
  102. }
  103. int line_type(const char*s)
  104. {
  105. if(s[0]==0 || s[0]=='\n')
  106. return 0;
  107. if(strncmp("2019-",s,5)==0)
  108. return 1;
  109. return 2;
  110. }
  111. int message_file::get_line(uint64_t*time,char*buf,int buflen)
  112. {
  113. return get_line(time,buf,buflen,0);
  114. }
  115. int message_file::get_line(uint64_t*time,char*buf,int buflen,char*m)
  116. {
  117. int type=0;
  118. while(1)
  119. {
  120. if(fgets(m_last_line,sizeof(m_last_line),m_file)==nullptr)
  121. return 0;
  122. type=line_type(m_last_line);
  123. if(type==0)
  124. continue;
  125. if(type==1)
  126. {
  127. struct tm tm;
  128. memset(&tm, 0, sizeof(struct tm));
  129. const char*ms=strptime(&m_last_line[0], "%Y-%m-%d %H:%M:%S", &tm);
  130. *time=1000LL*mktime(&tm)+atoi(ms+1);
  131. if(m) strcpy(m,m_last_line);
  132. continue;
  133. }
  134. break;
  135. }
  136. char*o=buf;
  137. while(1)
  138. {
  139. if(line_type(m_last_line)<=1)
  140. break;
  141. o+=convert(m_last_line,o);
  142. if(fgets(m_last_line,sizeof(m_last_line),m_file)==nullptr)
  143. return 0;
  144. }
  145. return o-buf;
  146. }
  147. int message_file::get_card_message(uint32_t card_id,char*b)
  148. {
  149. int len;
  150. char buf[2048];
  151. uint64_t time=0;
  152. int ppos=0;
  153. while((len=get_line(&time,buf,sizeof(buf)))>0)
  154. {
  155. zistream is(buf,len-2);
  156. uint16_t cmd;
  157. is>>skip(2)>>cmd;
  158. if(cmd!=0x843b && cmd!=0x753b)
  159. continue;
  160. is>>skip(16);
  161. while(!is.eof())
  162. {
  163. uint32_t cid;
  164. uint32_t gpos=is.pos();
  165. is>>skip(1)>>cid;
  166. if(cid!=card_id) continue;
  167. if(ppos==0) memcpy(b,buf,ppos=20);
  168. memcpy(&b[ppos],&buf[gpos],20);
  169. ppos+=20;
  170. }
  171. if(ppos==0) continue;
  172. ppos+=2;
  173. }
  174. return -1;
  175. }