Guest User

Untitled

a guest
Jul 15th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.27 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <string.h>
  5. #include <map>
  6.  
  7. using namespace std;
  8.  
  9. class StrBuilder
  10. {
  11. std::vector<char> m_data;
  12.  
  13. public:
  14. StrBuilder(){}
  15. ~StrBuilder() { m_data.clear();}
  16.  
  17. inline void Add(const char& c)
  18. {
  19. m_data.push_back(c);
  20. }
  21.  
  22. inline std::string toString()
  23. {
  24. static std::string err("");
  25. if (m_data.size() <= 0)
  26. err;
  27.  
  28. size_t size = m_data.size(); // for null;
  29. char* c = new char[size+1];
  30. char* t = c;
  31. memset(c, 0x00, size+1);
  32. int i=0;
  33. for(; i < size; i++)
  34. {
  35. c[i] = m_data[i];
  36. }
  37. c[i] = 0;
  38.  
  39. std::string fix(t);
  40. delete [] c;
  41. c = nullptr;
  42. return fix;
  43. }
  44.  
  45. };
  46.  
  47. struct BuilderList
  48. {
  49. static std::vector<StrBuilder> s_Builders;
  50. };
  51.  
  52. std::vector<StrBuilder> BuilderList::s_Builders;
  53.  
  54. struct CDFixForMSSQLLib
  55. {
  56.  
  57. int m_andCnt ;
  58. const char* pBeginAnd;
  59.  
  60. std::vector<std::string> m_remainings;
  61. std::vector<std::string> m_test;
  62. // carful here, we must append in a proper way
  63. struct
  64. {
  65. int index;
  66. std::map<int, std::vector<std::string> > data;
  67. } m_appendBuffer;
  68.  
  69. public:
  70.  
  71.  
  72. void CollectRemainings(const char* begin)
  73. {
  74. const char* orIt = begin;
  75. std::vector<std::string> rem;
  76. while ((orIt = strstr(orIt, " OR ")) != NULL)
  77. {
  78. StrBuilder sb;
  79.  
  80. while (*orIt != '\'')
  81. orIt++;
  82.  
  83. for(;;)
  84. {
  85. sb.Add(*orIt++);
  86. if (*orIt == '\'')
  87. {
  88. sb.Add(*orIt--);
  89. break;
  90. }
  91. }
  92. m_remainings.push_back(sb.toString());
  93. rem.push_back(sb.toString());
  94. }
  95.  
  96. for(int i=0; i < rem.size(); ++i)
  97. {
  98. m_appendBuffer.data[m_appendBuffer.index]
  99. .push_back(rem.at(i));
  100. }
  101.  
  102. m_appendBuffer.index++;
  103. }
  104.  
  105. std::string GetConstantString(const char* begin)
  106. {
  107. StrBuilder sb;
  108. const char* it = begin;
  109. const char* end = strstr(begin, " WHERE ");
  110. while (it != end)
  111. {
  112. sb.Add(*it++);
  113. }
  114.  
  115. for(int i=0; i < strlen(" WHERE "); ++i)
  116. {
  117. sb.Add(*end++);
  118. }
  119.  
  120. const char* orFirst = strstr(begin, " OR ");
  121. const char* andFirst = strstr(begin, " AND ");
  122. if (andFirst > orFirst)
  123. {
  124. while (end != orFirst)
  125. {
  126. sb.Add(*end++);
  127. }
  128. }
  129. else
  130. {
  131. while (end != andFirst)
  132. {
  133. sb.Add(*end++);
  134. }
  135. }
  136.  
  137.  
  138. std::string ret = sb.toString();
  139. return ret;
  140. }
  141.  
  142.  
  143. // new fix new logic
  144. std::string GetFirstItemBeforeOR(const char* begin, std::string** rem)
  145. {
  146. if (!begin)
  147. return std::string("");
  148.  
  149. const char* it = begin;
  150. const char* end = strstr(it, " OR ");
  151. StrBuilder sb;
  152.  
  153. if (!end)
  154. {
  155. return std::string(begin);
  156. }
  157. else
  158. {
  159. {
  160. const char* tmp = end;
  161. StrBuilder sb;
  162. while (*tmp != '\0')
  163. {
  164. sb.Add(*tmp++);
  165. }
  166. std::string* s = new std::string(sb.toString().c_str());
  167. (*rem) = s;
  168.  
  169. }
  170.  
  171.  
  172.  
  173. const char* openStr = strchr(it, '\'');
  174. while (it != openStr)
  175. {
  176. sb.Add(*it++);
  177. }
  178.  
  179. sb.Add(*openStr++);
  180. while (*openStr != '\'')
  181. {
  182. sb.Add(*openStr++);
  183. }
  184. sb.Add(*openStr++);
  185. }
  186. std::string ret = sb.toString();
  187.  
  188. return ret;
  189.  
  190. }
  191.  
  192. // simple split by AND buffers to be worked on
  193. void PrepareBuffers(char* next, const char* crit,
  194. std::vector<std::string> & buff)
  195. {
  196. char* it = (char*)strstr(next, crit);
  197. if (!it)
  198. return;
  199. it += strlen(crit);
  200. StrBuilder sb;
  201. for(int i=0; i < strlen(crit); ++i)
  202. {
  203. sb.Add(crit[i]);
  204. }
  205.  
  206. const char* end = strstr(it, crit);
  207. if (!end)
  208. {
  209. while (*it != '\0')
  210. {
  211. sb.Add(*it++);
  212. }
  213. }
  214. else
  215. {
  216. while (it != end)
  217. {
  218. sb.Add(*it++);
  219. }
  220. }
  221. buff.push_back(std::string(sb.toString()));
  222. PrepareBuffers(it, crit, buff);
  223.  
  224. }
  225.  
  226.  
  227. CDFixForMSSQLLib() : m_andCnt(0) , pBeginAnd(NULL),
  228. m_appendBuffer{0}
  229. {
  230. }
  231.  
  232. ~CDFixForMSSQLLib() { }
  233.  
  234.  
  235. bool NeedsFix(const std::string& query)
  236. {
  237. const char* andMatch = strstr(query.c_str(), " AND ");
  238. const char* orMatch = strstr(query.c_str(), " OR ");
  239.  
  240. return (andMatch != NULL) && (orMatch != NULL);
  241. }
  242.  
  243.  
  244.  
  245. std::string ApplySQLFix(const std::string & queryToFix)
  246. {
  247.  
  248. StrBuilder sb;
  249. static std::string error(""); // return error empty string
  250.  
  251. std::string retString;
  252. const char* begin = queryToFix.c_str();
  253. if (!begin)
  254. {
  255. return error;
  256.  
  257. }
  258. // find first OR in string (lowercase)
  259. const char* orIt = begin;
  260. {
  261. const char* original = strstr(begin, " OR ");
  262. const char* tmpOrig = original;
  263. // bug - crashesh if not handled
  264. // if not or - return original query
  265. if (original == NULL)
  266. {
  267. return queryToFix;
  268. }
  269.  
  270. // prevent out writing to some bad place
  271. if (*original == ' ')
  272. {
  273. while (*original == ' ')
  274. original--;
  275. original++;
  276. }
  277.  
  278. for (const char* it = begin; it != original; )
  279. {
  280. sb.Add(*it++);
  281. }
  282.  
  283. retString = std::string(sb.toString());
  284. }
  285.  
  286. while ((orIt = strstr(orIt, " OR ")) != NULL)
  287. {
  288. while (*orIt != '\'')
  289. orIt++;
  290.  
  291. StrBuilder sb;
  292.  
  293. for (;;)
  294. {
  295. sb.Add(*orIt++);
  296. if (*orIt == '\'')
  297. break;
  298. }
  299.  
  300. sb.Add(*orIt--);
  301. BuilderList::s_Builders.push_back(sb);
  302. }
  303.  
  304. printf("String builder size (%d)\n", (int)BuilderList::s_Builders.size());
  305. return retString;
  306. }
  307.  
  308. };
  309.  
  310.  
  311. static const char* gQueryOld =
  312. "SELECT name, time, severity.key, severity, userid, userlocation, message, user.resourceid, user.resourcename, user.resourcetype, user.resourcelocation, user.accessgranted, user.audittype, var.MigratedLogDescr "
  313. "FROM Audit "
  314. " WHERE "
  315. "user.resourcetype.key = 'AXIS P3367 Fixed Dome Network Camera (10.4.24.46) - FIRST 1' OR "
  316. "user.resourcetype.key = 'AXIS P3367 Fixed Dome Network Camera (10.4.24.46) - Camera 1' OR "
  317. "user.resourcetype.key = 'Arecont Single Camera (10.4.24.131) - Camera 1' OR "
  318. "user.resourcetype.key = 'Arecont Single Camera (10.4.24.131) - Microphone 1' OR "
  319. "user.resourcetype.key = 'My window and camera (10.4.24.131) - Camera 1' "
  320. "AND "
  321. "A > B";
  322.  
  323.  
  324. static const char *gQueryNew =
  325. "SELECT name, time, severity.key, severity, userid, userlocation, message, user.resourceid, user.resourcename, user.resourcetype, user.resourcelocation, user.accessgranted, user.audittype, var.MigratedLogDescr "
  326. "FROM Audit "
  327. " WHERE "
  328. " A1 > 300 "
  329. " AND "
  330. " B1 > 200 "
  331. " AND "
  332. " kur != hui "
  333. " AND "
  334. "user.resourcetype.key = '111111111111111111111111111111111111111' OR "
  335. "user.resourcetype.key = 'AXIS P3367 Fixed Dome Network Camera (10.4.24.46) - Camera 1' OR "
  336. "user.resourcetype.key = 'Arecont Single Camera (10.4.24.131) - Camera 1' OR "
  337. "user.resourcetype.key = 'Arecont Single Camera (10.4.24.131) - Microphone 1' OR "
  338. "user.resourcetype.key = 'My window and camera (10.4.24.131) - Camera 1'"
  339. " AND "
  340. " A > 300 "
  341. " AND "
  342. "user.resourcename = 'camera' OR "
  343. "user.resourcename = 'microphone' OR "
  344. "user.resourcename = '#########################' OR "
  345. "user.resourcename = 'oooooooooooo' OR "
  346. "user.resourcename = 'kor kor' OR "
  347. "user.resourcename = 'cccccccccc ' OR "
  348. "user.resourcename = 'mor mor ' "
  349. " AND "
  350. " B > 200 "
  351. " AND "
  352. " C > 100";
  353.  
  354.  
  355. int main()
  356. {
  357. FILE* fp = fopen("out.txt", "w");
  358. if (!fp)
  359. exit(-1);
  360.  
  361. std::string query (gQueryNew);
  362. CDFixForMSSQLLib fix;
  363.  
  364. fprintf(fp, "Original query before CD fix: \r\n");
  365. fprintf(fp, "[%s]\r\n", gQueryNew);
  366. std::string new_query ;
  367. if (fix.NeedsFix(query))
  368. {
  369. std::cout << "Needs fix... \n";
  370.  
  371. // std::cout << fix.GetFirstItemBeforeOR(query.c_str(), otherhalf);
  372. std::cout << "\r\n\r\n";
  373. std::string* otherString;
  374. std::vector<std::string> a, b;
  375. //
  376. // new_query += fix.GetConstantString(query.c_str());
  377. new_query += fix.GetFirstItemBeforeOR(query.c_str(), &otherString);
  378.  
  379. std::cout << new_query.c_str() << "\r\n\r\n";
  380.  
  381. fix.PrepareBuffers((char*)query.c_str(), " AND ", a);
  382. fix.PrepareBuffers((char*)query.c_str(), " OR ", b);
  383. std::map<int, std::vector<std::string> >remainings;
  384.  
  385. for(int i=0; i < a.size(); ++i)
  386. {
  387. const char* it = 0;
  388. if ((it = strstr(a.at(i).c_str(), " OR ")) != NULL)
  389. {
  390. const char* t = a.at(i).c_str();
  391. StrBuilder sb;
  392. while (t != it)
  393. {
  394. sb.Add(*t++);
  395. }
  396. a[i] = sb.toString();
  397.  
  398. std::vector<std::string> tmp;
  399. fix.PrepareBuffers((char*)b.at(i).c_str(), " OR ", tmp);
  400. remainings[i] = tmp;
  401. }
  402.  
  403. //fix.CollectRemainings(a.at(i).c_str());
  404. }
  405.  
  406.  
  407. for(int i=0; i < a.size(); ++i)
  408. {
  409. printf("[%d](%s)\r\n", i, a.at(i).c_str());
  410. }
  411.  
  412. #if 0
  413. for(int i=0; i < a.size(); ++i)
  414. {
  415. std::string tmp = fix.GetFirstItemBeforeOR(
  416. a.at(i).c_str());
  417. std::cout << tmp.c_str() << "\n";
  418. new_query += tmp;
  419. }
  420. #endif
  421. }
  422. else
  423. {
  424. // use old fix for simplicity
  425. std::string s = fix.ApplySQLFix(query);
  426. query = std::string(s);
  427. }
  428.  
  429.  
  430. fprintf(fp, "Fixed query before BOOST SQL generator is: \r\n");
  431. fprintf(fp, "[%s]\r\n", new_query.c_str());
  432. fprintf(fp, "\r\nREMAININGS TO BE APPENDED AS OR-s:\r\n");
  433. for(int i=0; i < fix.m_remainings.size(); ++i)
  434. {
  435. fprintf(fp, "fix.m_remainings[%d] = (%s)\r\n", i, fix.m_remainings.at(i).c_str());
  436. }
  437.  
  438. for(int i=0; i < fix.m_appendBuffer.index; ++i)
  439. {
  440. // a minor index bug for the map, however it's a map and unique is guaranted
  441.  
  442. puts("\r\n");
  443. for(int j=0; j < fix.m_appendBuffer.data[i].size(); ++j)
  444. {
  445. fprintf(fp, "Row(%d)Col(%d) : DATA: %s\r\n", i, j,
  446. fix.m_appendBuffer.data[i][j].c_str());
  447. }
  448. }
  449.  
  450. fflush(fp);
  451. fclose(fp);
  452.  
  453. return 0;
  454. }
Add Comment
Please, Sign In to add comment