probe.c
Upload User: wstnjxml
Upload Date: 2014-04-03
Package Size: 7248k
Code Size: 6k
Category:

Windows CE

Development Platform:

C/C++

  1. /*****************************************************************************
  2.  *
  3.  * This program is free software ; you can redistribute it and/or modify
  4.  * it under the terms of the GNU General Public License as published by
  5.  * the Free Software Foundation; either version 2 of the License, or
  6.  * (at your option) any later version.
  7.  *
  8.  * This program is distributed in the hope that it will be useful,
  9.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11.  * GNU General Public License for more details.
  12.  *
  13.  * You should have received a copy of the GNU General Public License
  14.  * along with this program; if not, write to the Free Software
  15.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  16.  *
  17.  * $Id: probe.c 607 2006-01-22 20:58:29Z picard $
  18.  *
  19.  * The Core Pocket Media Player
  20.  * Copyright (c) 2004-2005 Gabor Kovacs
  21.  *
  22.  ****************************************************************************/
  23. #include "common.h"
  24. static NOINLINE void SkipSpace(const tchar_t** p)
  25. {
  26. while (IsSpace(**p)) ++(*p);
  27. }
  28. static NOINLINE bool_t IsToken(const tchar_t** p,const tchar_t* Name)
  29. {
  30. size_t Len = tcslen(Name);
  31. SkipSpace(p);
  32. if (tcsnicmp(*p,Name,Len)==0 && !IsAlpha((*p)[Len]))
  33. {
  34. (*p) += Len;
  35. return 1;
  36. }
  37. return 0;
  38. }
  39. static NOINLINE bool_t IsSymbol(const tchar_t** p,int ch)
  40. {
  41. SkipSpace(p);
  42. if (**p == ch)
  43. {
  44. ++(*p);
  45. return 1;
  46. }
  47. return 0;
  48. }
  49. static NOINLINE bool_t IsTokenWithParam(const tchar_t** p,const tchar_t* Name)
  50. {
  51. return IsToken(p,Name) && IsSymbol(p,'(');
  52. }
  53. static INLINE void ParamEnd(const tchar_t** p)
  54. {
  55. IsSymbol(p,')');
  56. }
  57. static NOINLINE bool_t ParamNext(const tchar_t** p)
  58. {
  59. if (IsSymbol(p,','))
  60. return 1;
  61. ParamEnd(p);
  62. return 0;
  63. }
  64. static int Run(const tchar_t** p, const uint8_t** Data, int* Length);
  65. static NOINLINE int GetOfs(const tchar_t** p, const uint8_t** Data, int* Length)
  66. {
  67. int Ofs = 0;
  68.   if (IsSymbol(p,'('))
  69. {
  70. Ofs = Run(p,Data,Length);
  71. ParamEnd(p);
  72. }
  73. return Ofs;
  74. }
  75. static int Run(const tchar_t** p, const uint8_t** Data, int* Length)
  76. {
  77. int a,b,Ofs,v = 0;
  78. assert(!IsSymbol(p,')') && !IsSymbol(p,',') && **p);
  79. if (IsTokenWithParam(p,T("scan")))
  80. {
  81. const uint8_t* SaveData = *Data;
  82. int Result = -1;
  83. int SaveLength = *Length;
  84. const tchar_t* Start;
  85. int End = *Length - Run(p,Data,Length); // limit
  86. if (End < 0)
  87. End = 0;
  88. ParamNext(p);
  89. Start = *p;
  90. while (*Length > End && Result<0)
  91. {
  92. *p = Start;
  93. if (Run(p,Data,Length)) //success
  94. Result = 1;
  95. ParamNext(p);
  96. if (Run(p,Data,Length) && Result<=0) //fail
  97. Result = 0;
  98. ParamNext(p);
  99. Run(p,Data,Length); //step
  100. ParamEnd(p);
  101. }
  102. if (Result>=0)
  103. v = Result;
  104. *Data = SaveData;
  105. *Length = SaveLength;
  106. }
  107. else
  108. if (IsToken(p,T("fwd")))
  109. {
  110. v = 1;
  111. if (IsSymbol(p,'('))
  112. {
  113. v = Run(p,Data,Length);
  114. ParamEnd(p);
  115. }
  116. *Length -= v;
  117. *Data += v;
  118. }
  119. else
  120. if (IsToken(p,T("le32")))
  121. {
  122. Ofs = GetOfs(p,Data,Length);
  123. if (*Length >= Ofs+4)
  124. v = LOAD32LE(*Data+Ofs);
  125. }
  126. else
  127. if (IsToken(p,T("be32")))
  128. {
  129. Ofs = GetOfs(p,Data,Length);
  130. if (*Length >= Ofs+4)
  131. v = LOAD32BE(*Data+Ofs);
  132. }
  133. else
  134. if (IsToken(p,T("le16")))
  135. {
  136. Ofs = GetOfs(p,Data,Length);
  137. if (*Length >= Ofs+2)
  138. v = LOAD16LE(*Data+Ofs);
  139. }
  140. else
  141. if (IsToken(p,T("be16")))
  142. {
  143. Ofs = GetOfs(p,Data,Length);
  144. if (*Length >= Ofs+2)
  145. v = LOAD16BE(*Data+Ofs);
  146. }
  147. else
  148. if (IsToken(p,T("text")))
  149. {
  150. int i;
  151. v = 1;
  152. for (i=0;*Length>i;++i)
  153. {
  154. uint8_t ch = (*Data)[i];
  155. if (ch<32 && ch!=10 && ch!=13 && ch!=9 && ch!=26)
  156. {
  157. v = 0;
  158. break;
  159. }
  160. }
  161. }
  162. else
  163. if (IsToken(p,T("lines")))
  164. {
  165. int i;
  166. v = 0;
  167. for (i=0;*Length>i;++i)
  168. if ((*Data)[i] == 10)
  169. ++v;
  170. }
  171. else
  172. if (IsToken(p,T("length")))
  173. {
  174. v = *Length;
  175. }
  176. else
  177. if (IsTokenWithParam(p,T("stri")))
  178. {
  179. v = 0;
  180. if (IsSymbol(p,'''))
  181. {
  182. int i;
  183. v = 1;
  184. for (i=0;**p && **p!=''';++i,++(*p))
  185. if (*Length<=i || toupper((*Data)[i])!=toupper(**p))
  186. v = 0;
  187. IsSymbol(p,''');
  188. ParamEnd(p);
  189. }
  190. }
  191. else
  192. if (IsTokenWithParam(p,T("gt")))
  193. {
  194. a = Run(p,Data,Length);
  195. ParamNext(p);
  196. b = Run(p,Data,Length);
  197. ParamEnd(p);
  198. v = a>b;
  199. }
  200. else
  201. if (IsTokenWithParam(p,T("eq")))
  202. {
  203. a = Run(p,Data,Length);
  204. ParamNext(p);
  205. b = Run(p,Data,Length);
  206. ParamEnd(p);
  207. v = a == b;
  208. }
  209. else
  210. if (IsTokenWithParam(p,T("ne")))
  211. {
  212. a = Run(p,Data,Length);
  213. ParamNext(p);
  214. b = Run(p,Data,Length);
  215. ParamEnd(p);
  216. v = a != b;
  217. }
  218. else
  219. if (IsTokenWithParam(p,T("mask")))
  220. {
  221. a = Run(p,Data,Length);
  222. ParamNext(p);
  223. b = Run(p,Data,Length);
  224. ParamEnd(p);
  225. v = a & b;
  226. }
  227. else
  228. if (IsTokenWithParam(p,T("add")))
  229. {
  230. do
  231. {
  232. v += Run(p,Data,Length);
  233. }
  234. while (ParamNext(p));
  235. }
  236. else
  237. if (IsTokenWithParam(p,T("or")))
  238. {
  239. do
  240. {
  241. if (Run(p,Data,Length))
  242. v = 1;
  243. }
  244. while (ParamNext(p));
  245. }
  246. else
  247. if (IsTokenWithParam(p,T("and")))
  248. {
  249. v = 1;
  250. do
  251. {
  252. if (!Run(p,Data,Length))
  253. v = 0;
  254. }
  255. while (ParamNext(p));
  256. }
  257. else
  258. if (IsSymbol(p,'!'))
  259. v = !Run(p,Data,Length);
  260. else
  261. if (IsSymbol(p,'~'))
  262. v = ~Run(p,Data,Length);
  263. else
  264. if (IsSymbol(p,'''))
  265. {
  266. int i,c[4];
  267. c[0] = c[1] = c[2] = c[3] = '_';
  268. for (i=0;**p && **p!=''';++i,++(*p))
  269. if (i<4) c[i] = **p;
  270. IsSymbol(p,''');
  271. v = FOURCCLE(c[0],c[1],c[2],c[3]);
  272. }
  273. else
  274. if (IsDigit(**p) || **p=='-')
  275. {
  276. bool_t Neg = IsSymbol(p,'-');
  277. if ((*p)[0] == '0' && (*p)[1] == 'x')
  278. {
  279. (*p)+=2;
  280. for (;**p;++(*p))
  281. {
  282. int h = Hex(**p);
  283. if (h<0) break;
  284. v=v*16+h;
  285. }
  286. }
  287. else
  288. {
  289. for (;IsDigit(**p);++(*p))
  290. v = v*10 + (**p-'0');
  291. }
  292. if (Neg)
  293. v = -v;
  294. }
  295. return v;
  296. }
  297. bool_t DataProbe(const void* Ptr, int Length,const tchar_t* Code)
  298. {
  299. const uint8_t* Data = (const uint8_t*)Ptr;
  300. if (!Code[0])
  301. return 0;
  302. return Run(&Code,&Data,&Length);
  303. }