Debian Codegraph @

Source/lineparse.cpp

raw
1 /*
2 * lineparse.cpp
3 *
4 * This file is a part of NSIS.
5 *
6 * Copyright (C) 1999-2026 Nullsoft and Contributors
7 *
8 * Licensed under the zlib/libpng license (the "License");
9 * you may not use this file except in compliance with the License.
10 *
11 * Licence details can be found in the file COPYING.
12 *
13 * This software is provided 'as-is', without any express or implied
14 * warranty.
15 */
16
17 #include "lineparse.h"
18 #include "Platform.h"
19 #include "tchar.h"
20 #include <cstdlib>
21 #include <cstring>
22 #include "tstring.h"
23 #include "util.h"
24
25 #ifndef _istspace
26 #define _istspace my_istspace
27 template<class T> static int my_istspace(T c) { return c == 0x20 || (c >= 0x09 && c <= 0x0D); }
28 #endif
29 template<class T> static T skipspace(T s) { while(*s && _istspace(*s)) ++s; return s; }
30
31
32 LineParser::LineParser(bool bCommentBlock)
33 {
34 m_incommentblock=bCommentBlock;
35 m_incomment=false;
36 m_nt=m_eat=0;
37 m_tokens=0;
38 }
39
40 LineParser::~LineParser()
41 {
42 freetokens();
43 }
44
45 bool LineParser::inComment()
46 {
47 return m_incomment;
48 }
49
50 bool LineParser::inCommentBlock()
51 {
52 return m_incommentblock;
53 }
54
55 int LineParser::parse(const TCHAR *line, int ignore_escaping/*=0*/, NStreamEncoding*pEncChange) // returns -1 on error
56 {
57 freetokens();
58 bool bPrevCB=m_incommentblock;
59 int n=doline(line, ignore_escaping, pEncChange);
60 if (n) return n;
61 if (m_nt)
62 {
63 m_incommentblock=bPrevCB;
64 m_tokens=(TCHAR**)malloc(sizeof(TCHAR*)*m_nt);
65 n=doline(line, ignore_escaping);
66 if (n)
67 {
68 freetokens();
69 return -1;
70 }
71 }
72 return 0;
73 }
74
75 int LineParser::getnumtokens()
76 {
77 return m_nt-m_eat;
78 }
79
80 void LineParser::eattoken()
81 {
82 m_eat++;
83 }
84
85 inline int LineParser::validate_token_index(int token, int *success/*=0*/) const
86 {
87 token += m_eat;
88 if (token < 0 || token >= m_nt || !m_tokens[token][0])
89 {
90 if (success) *success = 0;
91 return -1;
92 }
93 return token;
94 }
95
96
97 static unsigned int get_base_from_prefix(const TCHAR *str)
98 {
99 unsigned int hasbase = 0;
100 if (_T('-') == *str || _T('+') == *str) ++str;
101 if (_T('0') == str[0])
102 {
103 if (_T('x') == (str[1]|32)) hasbase = 16;
104 // Special support for 0n, 0y and 0t MASM style and 0b and 0o Python style radix prefix:
105 if (_T('n') == (str[1]|32)) hasbase = 10;
106 if (_T('b') == (str[1]|32) || _T('y') == (str[1]|32)) hasbase = 2;
107 if (_T('o') == (str[1]|32) || _T('t') == (str[1]|32)) hasbase = 8;
108 }
109 return hasbase;
110 }
111 static unsigned int number_has_base_prefix(const TCHAR *str) { return get_base_from_prefix(str); }
112
113 static int parse_int_expression(const TCHAR *str, bool expressions, int *success=0)
114 {
115 enum { XOP_NONE = 0, XOP_BITAND, XOP_BITXOR, XOP_BITOR, XOP_LOGAND, XOP_LOGOR };
116 int tot=0, xop=XOP_NONE, hasfailed=0;
117 nextnum:
118 const TCHAR *p=str, *parse=p, *xoperandpfixstart, *xoperandpfixend;
119 TCHAR *end;
120 int neg=0, base=0, num;
121 #ifndef _MSC_VER
122 xoperandpfixstart=xoperandpfixend=0; // Avoid GCC maybe-uninitialized warning
123 #endif
124
125 if (expressions)
126 {
127 xoperandpfixstart=p=skipspace(p);
128 while (*p == '~' || *p == '!') p=skipspace(++p);
129 xoperandpfixend=parse=p;
130 }
131
132 if (_T('+') == *p)
133 ++p;
134 else if (_T('-') == *p)
135 ++p, ++neg;
136 if (_T('0') == p[0])
137 {
138 if ((base=get_base_from_prefix(p))) parse=&p[2];
139 }
140 if (neg)
141 {
142 num=(int)(long)_tcstol(parse,&end,base);
143 if (base) num*=-1; // Input was "-0n012" but we have only parsed "012" and need to fix the sign
144 }
145 else
146 {
147 num=(int)(long)_tcstoul(parse,&end,base);
148 }
149 const bool parseddigits=end > parse;
150 if (!parseddigits) ++hasfailed; // "0x", "1 | ~" etc.
151
152 if (expressions)
153 {
154 for (p=xoperandpfixstart; p < xoperandpfixend; p=skipspace(++p))
155 switch(*p)
156 {
157 case '~': num=~num; break;
158 case '!': num=!num; break;
159 default: assert(!"op");
160 }
161
162 // Simple left-to-right parsing, no operator precedence
163 switch(xop)
164 {
165 case XOP_NONE: tot=num; break;
166 case XOP_BITAND: tot&=num; break;
167 case XOP_BITXOR: tot^=num; break;
168 case XOP_BITOR: tot|=num; break;
169 case XOP_LOGAND: tot=tot && num; break;
170 case XOP_LOGOR: tot=tot || num; break;
171 default: assert(!"op");
172 }
173
174 str=end=skipspace(end);
175 switch(*str)
176 {
177 case '&': xop=XOP_BITAND; if (*++str == '&') xop=XOP_LOGAND, ++str; goto nextnum;
178 case '^': xop=XOP_BITXOR, ++str; goto nextnum;
179 case '|': xop=XOP_BITOR; if (*++str == '|') xop=XOP_LOGOR, ++str; goto nextnum;
180 default: xop=XOP_NONE; // Done
181 }
182 }
183 else
184 {
185 tot=num;
186 }
187
188 if (success) *success=!(int)(*end) && !hasfailed;
189 return tot;
190 }
191
192 int LineParser::parse_int(const TCHAR *str, int *success/*=0*/)
193 {
194 return parse_int_expression(str, false, success);
195 }
196
197 int LineParser::parse_intx(const TCHAR *str, int *success/*=0*/)
198 {
199 return parse_int_expression(str, true, success);
200 }
201
202 int LineParser::gettoken_int(int token, int *success/*=0*/) const
203 {
204 if ((token = validate_token_index(token,success)) < 0) return 0;
205 return parse_int(m_tokens[token],success);
206 }
207
208 int LineParser::gettoken_intx(int token, int *success/*=0*/) const
209 {
210 if ((token = validate_token_index(token,success)) < 0) return 0;
211 return parse_intx(m_tokens[token],success);
212 }
213
214 double LineParser::parse_float(const TCHAR *str, int *success/*=0*/)
215 {
216 if (success)
217 {
218 const TCHAR *t=str;
219 if (_T('-') == *t || _T('+') == *t) ++t;
220 *success=*t ? 1 : 0;
221 unsigned int dotcount = 0;
222 while (*t)
223 {
224 if (_T('.') == *t && ++dotcount > 1) *success=0;
225 if ((*t < _T('0') || *t > _T('9')) && *t != _T('.')) *success=0;
226 t++;
227 }
228 }
229 return _tstof(str);
230 }
231
232 double LineParser::gettoken_float(int token, int *success/*=0*/) const
233 {
234 if ((token = validate_token_index(token,success)) < 0) return 0.0;
235 return parse_float(m_tokens[token],success);
236 }
237
238 double LineParser::parse_number(const TCHAR *str, int *success/*=0*/)
239 {
240 const unsigned int forceint=number_has_base_prefix(str);
241 return forceint ? parse_int(str,success) : parse_float(str,success);
242 }
243
244 double LineParser::gettoken_number(int token, int *success/*=0*/) const
245 {
246 if ((token = validate_token_index(token,success)) < 0) return 0.0;
247 return parse_number(m_tokens[token],success);
248 }
249
250 int LineParser::gettoken_binstrdata(int token, char*buffer, int bufcap) const
251 {
252 const TCHAR*p=gettoken_str(token);
253 int a,b,c,d=0;
254 while (*p)
255 {
256 a=*p;
257 if (a >= _T('0') && a <= _T('9')) a-=_T('0');
258 else if (a >= _T('a') && a <= _T('f')) a-=_T('a')-10;
259 else if (a >= _T('A') && a <= _T('F')) a-=_T('A')-10;
260 else if (a == _T(',')) { ++p; continue; } // Allow comma separator (for Regedit5 .reg format)
261 else break;
262 b=*++p;
263 if (b >= _T('0') && b <= _T('9')) b-=_T('0');
264 else if (b >= _T('a') && b <= _T('f')) b-=_T('a')-10;
265 else if (b >= _T('A') && b <= _T('F')) b-=_T('A')-10;
266 else break;
267 c=(a<<4)|b, p++;
268 if (d >= bufcap) return -1; // Buffer too small
269 buffer[d++]=c;
270 }
271 if (*p) return -2; // Did not parse the entire buffer
272 return d;
273 }
274
275 TCHAR* LineParser::gettoken_str(int token) const
276 {
277 token+=m_eat;
278 if (token < 0 || token >= m_nt) return const_cast<TCHAR*>(_T(""));
279 return m_tokens[token];
280 }
281
282 int LineParser::gettoken_enum(int token, const TCHAR *strlist) // null separated list
283 {
284 int x=0;
285 TCHAR *tt=gettoken_str(token);
286 if (tt && *tt) while (*strlist)
287 {
288 if (!_tcsicmp(tt,strlist)) return x;
289 strlist+=_tcslen(strlist)+1;
290 x++;
291 }
292 return -1;
293 }
294
295 void LineParser::freetokens()
296 {
297 if (m_tokens)
298 {
299 int x;
300 for (x = 0; x < m_nt; x ++)
301 free(m_tokens[x]);
302 free(m_tokens);
303 }
304 m_tokens=0;
305 m_nt=0;
306 }
307
308 static bool IsPEP263EncodingCharacter(TCHAR c)
309 {
310 TCHAR low = S7ChLwr(c);
311 return ('a' <= low && low <= 'z') || ('0' <= c && c <= '9') || c == '.' || c == '-' || c == '_';
312 }
313
314 int LineParser::doline(const TCHAR *line, int ignore_escaping/*=0*/, NStreamEncoding*pEncChange)
315 {
316 m_nt=0;
317 m_incomment = false;
318 while (*line == _T(' ') || *line == _T('\t')) line++;
319 while (*line)
320 {
321 if ( m_incommentblock )
322 {
323 while ( *line )
324 {
325 if ( *line == _T('*') && *(line+1) == _T('/') )
326 {
327 m_incommentblock=false; // Found end of comment block
328 line+=2;
329 while (*line == _T(' ') || *line == _T('\t')) line++;
330 break;
331 }
332 else line++;
333 }
334 }
335 else
336 {
337 int lstate=0; // 1=", 2=`, 4='
338 if (*line == _T(';') || *line == _T('#'))
339 {
340 m_incomment = true;
341 TCHAR *p = const_cast<TCHAR*>(line);
342 if (pEncChange && *++p) // Check for Python PEP 263 magic encoding comment
343 {
344 p = _tcsstr(p, L"coding");
345 if (p) p += sizeof("coding") - 1; else break;
346 if (*p == ':' || *p == '=') ++p; else break;
347 while (*p == _T(' ') || *p == _T('\t')) ++p;
348 TCHAR buf[200];
349 if (strtrycpy(buf, p, COUNTOF(buf)))
350 {
351 for (p = buf; IsPEP263EncodingCharacter(*p);) ++p;
352 *p = '\0';
353 pEncChange->SetCodepage(GetEncodingFromString(buf));
354 }
355 }
356 break;
357 }
358 if (*line == _T('/') && *(line+1) == _T('*'))
359 {
360 m_incommentblock = true;
361 line+=2;
362 }
363 else
364 {
365 if (*line == _T('\"')) lstate=1;
366 else if (*line == _T('\'')) lstate=2;
367 else if (*line == _T('`')) lstate=4;
368 if (lstate) line++;
369 int nc=0;
370 const TCHAR *p = line;
371 while (*line)
372 {
373 if (line[0] == _T('$') && line[1] == _T('\\'))
374 {
375 switch (line[2])
376 {
377 case _T('"'):
378 case _T('\''):
379 case _T('`'):
380 nc += ignore_escaping ? 3 : 1;
381 line += 3;
382 continue;
383 }
384 }
385 if (lstate==1 && *line ==_T('\"')) break;
386 if (lstate==2 && *line ==_T('\'')) break;
387 if (lstate==4 && *line ==_T('`')) break;
388 if (!lstate && (*line == _T(' ') || *line == _T('\t'))) break;
389 #ifdef NSIS_FIX_COMMENT_HANDLING
390 if (!lstate && (*line == _T(';') || *line == _T('#') || (*line == _T('/') && *(line+1) == _T('*')))) break;
391 #endif
392 line++;
393 nc++;
394 }
395 if (m_tokens)
396 {
397 int i;
398 m_tokens[m_nt]=(TCHAR*)malloc((nc+1)*sizeof(TCHAR));
399 for (i = 0; p < line; i++, p++) {
400 if (!ignore_escaping && p[0] == _T('$') && p[1] == _T('\\'))
401 {
402 switch (p[2])
403 {
404 case _T('"'):
405 case _T('\''):
406 case _T('`'):
407 p += 2;
408 }
409 }
410 m_tokens[m_nt][i] = *p;
411 }
412 m_tokens[m_nt][nc]=0;
413 }
414 m_nt++;
415 if (lstate)
416 {
417 if (*line) line++;
418 else return -2;
419 }
420 while (*line == _T(' ') || *line == _T('\t')) line++;
421 }
422 }
423 }
424 return 0;
425 }