2961
|
1 static int getdwordle(unsigned char *pData)
|
|
2 {
|
|
3 return pData[0] | ((pData[1]) << 8) | ((pData[2]) << 16) | ((pData[3]) << 24);
|
|
4 }
|
|
5
|
|
6 static int xsf_tagsearchraw(const char *pData, int dwSize)
|
|
7 {
|
|
8 int dwPos;
|
|
9 int dwReservedAreaSize;
|
|
10 int dwProgramLength;
|
|
11 int dwProgramCRC;
|
|
12 if (dwSize < 16 + 5 + 1) return 0;
|
|
13 if (pData[0] != 'P') return 0;
|
|
14 if (pData[1] != 'S') return 0;
|
|
15 if (pData[2] != 'F') return 0;
|
|
16 dwReservedAreaSize = getdwordle(pData + 4);
|
|
17 dwProgramLength = getdwordle(pData + 8);
|
|
18 dwProgramCRC = getdwordle(pData + 12);
|
|
19 dwPos = 16 + dwReservedAreaSize + dwProgramLength;
|
|
20 if (dwPos >= dwSize) return 0;
|
|
21 return dwPos;
|
|
22 }
|
|
23 static int xsf_tagsearch(int *pdwRet, const char *pData, int dwSize)
|
|
24 {
|
|
25 int dwPos = xsf_tagsearchraw(pData, dwSize);
|
|
26 if (dwSize < dwPos + 5) return 0;
|
|
27 if (memcmp(pData + dwPos, "[TAG]", 5)) return 0;
|
|
28 *pdwRet = dwPos + 5;
|
|
29 return 1;
|
|
30 }
|
|
31
|
|
32 enum xsf_tagenum_callback_returnvalue
|
|
33 {
|
|
34 xsf_tagenum_callback_returnvaluecontinue = 0,
|
|
35 xsf_tagenum_callback_returnvaluebreak = 1
|
|
36 };
|
|
37 typedef int (*pfnxsf_tagenum_callback_t)(void *pWork, const char *pNameTop, const char *pNameEnd, const char *pValueTop, const char *pValueEnd);
|
|
38 static int xsf_tagenumraw(pfnxsf_tagenum_callback_t pCallBack, void *pWork, const char *pData, int dwSize)
|
|
39 {
|
|
40 int dwPos = 0;
|
|
41 while (dwPos < dwSize)
|
|
42 {
|
|
43 int dwNameTop;
|
|
44 int dwNameEnd;
|
|
45 int dwValueTop;
|
|
46 int dwValueEnd;
|
|
47 if (dwPos < dwSize && pData[dwPos] == 0x0a) dwPos++;
|
|
48 while (dwPos < dwSize && pData[dwPos] != 0x0a && 0x01 <= pData[dwPos] && pData[dwPos] <= 0x20)
|
|
49 dwPos++;
|
|
50 if (dwPos >= dwSize || pData[dwPos] == 0x0a) continue;
|
|
51 dwNameTop = dwPos;
|
|
52 while (dwPos < dwSize && pData[dwPos] != 0x0a && pData[dwPos] != '=')
|
|
53 dwPos++;
|
|
54 if (dwPos >= dwSize || pData[dwPos] == 0x0a) continue;
|
|
55 dwNameEnd = dwPos;
|
|
56 while (dwNameTop < dwNameEnd && 0x01 <= pData[dwNameEnd - 1] && pData[dwNameEnd - 1] <= 0x20)
|
|
57 dwNameEnd--;
|
|
58 if (dwPos < dwSize && pData[dwPos] == '=') dwPos++;
|
|
59 while (dwPos < dwSize && pData[dwPos] != 0x0a && 0x01 <= pData[dwPos] && pData[dwPos] <= 0x20)
|
|
60 dwPos++;
|
|
61 dwValueTop = dwPos;
|
|
62 while (dwPos < dwSize && pData[dwPos] != 0x0a)
|
|
63 dwPos++;
|
|
64 dwValueEnd = dwPos;
|
|
65 while (dwValueTop < dwValueEnd && 0x01 <= pData[dwValueEnd - 1] && pData[dwValueEnd - 1] <= 0x20)
|
|
66 dwValueEnd--;
|
|
67
|
|
68 if (pCallBack)
|
|
69 {
|
|
70 if (xsf_tagenum_callback_returnvaluecontinue != pCallBack(pWork, (const char *)pData + dwNameTop, (const char *)pData + dwNameEnd, (const char *)pData + dwValueTop, (const char *)pData + dwValueEnd))
|
|
71 return -1;
|
|
72 }
|
|
73 }
|
|
74 return 1;
|
|
75 }
|
|
76
|
|
77 static int xsf_tagenum(pfnxsf_tagenum_callback_t pCallBack, void *pWork, const char *pData, int dwSize)
|
|
78 {
|
|
79 int dwPos = 0;
|
|
80 if (!xsf_tagsearch(&dwPos, pData, dwSize))
|
|
81 return 0;
|
|
82 return xsf_tagenumraw(pCallBack, pWork, pData + dwPos, dwSize - dwPos);
|
|
83 }
|
|
84
|
|
85 typedef struct
|
|
86 {
|
|
87 int taglen;
|
|
88 const char *tag;
|
|
89 char *ret;
|
|
90 } xsf_tagget_work_t;
|
|
91
|
|
92 static int xsf_tagenum_callback_tagget(void *pWork, const char *pNameTop, const char *pNameEnd, const char *pValueTop, const char *pValueEnd)
|
|
93 {
|
|
94 xsf_tagget_work_t *pwork = (xsf_tagget_work_t *)pWork;
|
|
95 if (pwork->taglen == pNameEnd - pNameTop && !strncasecmp(pNameTop, pwork->tag, pwork->taglen))
|
|
96 {
|
|
97 char *ret = (char *)malloc(pValueEnd - pValueTop + 1);
|
|
98 if (!ret) return xsf_tagenum_callback_returnvaluecontinue;
|
|
99 memcpy(ret, pValueTop, pValueEnd - pValueTop);
|
|
100 ret[pValueEnd - pValueTop] = 0;
|
|
101 pwork->ret = ret;
|
|
102 return xsf_tagenum_callback_returnvaluebreak;
|
|
103 }
|
|
104 return xsf_tagenum_callback_returnvaluecontinue;
|
|
105 }
|
|
106
|
|
107 static char *xsf_taggetraw(const char *tag, const char *pData, int dwSize)
|
|
108 {
|
|
109 xsf_tagget_work_t work;
|
|
110 work.ret = 0;
|
|
111 work.tag = tag;
|
|
112 work.taglen = (int)strlen(tag);
|
|
113 xsf_tagenumraw(xsf_tagenum_callback_tagget, &work, pData, dwSize);
|
|
114 return work.ret;
|
|
115 }
|
|
116
|
|
117 static char *xsf_tagget(const char *tag, const char *pData, int dwSize)
|
|
118 {
|
|
119 xsf_tagget_work_t work;
|
|
120 work.ret = 0;
|
|
121 work.tag = tag;
|
|
122 work.taglen = (int)strlen(tag);
|
|
123 xsf_tagenum(xsf_tagenum_callback_tagget, &work, pData, dwSize);
|
|
124 return work.ret;
|
|
125 }
|
|
126
|
|
127 static int xsf_tagget_exist(const char *tag, const char *pData, int dwSize)
|
|
128 {
|
|
129 int exists;
|
|
130 char *value = xsf_tagget(tag, pData, dwSize);
|
|
131 if (value)
|
|
132 {
|
|
133 exists = 1;
|
|
134 free(value);
|
|
135 }
|
|
136 else
|
|
137 {
|
|
138 exists = 0;
|
|
139 }
|
|
140 return exists;
|
|
141 }
|
|
142
|
|
143 static int xsf_tagget_int(const char *tag, const char *pData, int dwSize, int value_default)
|
|
144 {
|
|
145 int ret = value_default;
|
|
146 char *value = xsf_tagget(tag, pData, dwSize);
|
|
147 if (value)
|
|
148 {
|
|
149 if (*value) ret = atoi(value);
|
|
150 free(value);
|
|
151 }
|
|
152 return ret;
|
|
153 }
|
|
154
|
|
155 static double xsf_tagget_float(const char *tag, const char *pData, int dwSize, double value_default)
|
|
156 {
|
|
157 double ret = value_default;
|
|
158 char *value = xsf_tagget(tag, pData, dwSize);
|
|
159 if (value)
|
|
160 {
|
|
161 if (*value) ret = atof(value);
|
|
162 free(value);
|
|
163 }
|
|
164 return ret;
|
|
165 }
|
|
166
|
|
167 static int tag2ms(const char *p)
|
|
168 {
|
|
169 int f = 0;
|
|
170 int b = 0;
|
|
171 int r = 0;
|
|
172 for (;*p; p++)
|
|
173 {
|
|
174 if (*p >= '0' && *p <= '9')
|
|
175 {
|
|
176 if (f < 1000)
|
|
177 {
|
|
178 r = r * 10 + *p - '0';
|
|
179 if (f) f *= 10;
|
|
180 continue;
|
|
181 }
|
|
182 break;
|
|
183 }
|
|
184 if (*p == '.')
|
|
185 {
|
|
186 f = 1;
|
|
187 continue;
|
|
188 }
|
|
189 if (*p == ':')
|
|
190 {
|
|
191 b = (b + r) * 60;
|
|
192 r = 0;
|
|
193 continue;
|
|
194 }
|
|
195 break;
|
|
196 }
|
|
197 if (f < 10)
|
|
198 r *= 1000;
|
|
199 else if (f == 10)
|
|
200 r *= 100;
|
|
201 else if (f == 100)
|
|
202 r *= 10;
|
|
203 r += b * 1000;
|
|
204 return r;
|
|
205 }
|