1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
|
/*-
* Copyright 2021 Vsevolod Stakhov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "css_tokeniser.hxx"
#include "css_util.hxx"
#include "css.hxx"
#include <charconv>
#include <string>
namespace rspamd::css {
/* Helpers to create tokens */
/*
* This helper is intended to create tokens either with a tag and value
* or with just a tag.
*/
template<css_parser_token::token_type T, typename ...Args>
auto make_token(const Args&... args) -> css_parser_token;
template<>
auto make_token<css_parser_token::token_type::string_token, std::string_view>(const std::string_view &s)
-> css_parser_token
{
return css_parser_token{css_parser_token::token_type::string_token, s};
}
template<>
auto make_token<css_parser_token::token_type::ident_token, std::string_view>(const std::string_view &s)
-> css_parser_token
{
return css_parser_token{css_parser_token::token_type::ident_token, s};
}
template<>
auto make_token<css_parser_token::token_type::function_token, std::string_view>(const std::string_view &s)
-> css_parser_token
{
return css_parser_token{css_parser_token::token_type::function_token, s};
}
template<>
auto make_token<css_parser_token::token_type::url_token, std::string_view>(const std::string_view &s)
-> css_parser_token
{
return css_parser_token{css_parser_token::token_type::url_token, s};
}
template<>
auto make_token<css_parser_token::token_type::whitespace_token, std::string_view>(const std::string_view &s)
-> css_parser_token
{
return css_parser_token{css_parser_token::token_type::whitespace_token, s};
}
template<>
auto make_token<css_parser_token::token_type::delim_token, char>(const char &c)
-> css_parser_token
{
return css_parser_token{css_parser_token::token_type::delim_token, c};
}
template<>
auto make_token<css_parser_token::token_type::number_token, double>(const double &d)
-> css_parser_token
{
return css_parser_token{css_parser_token::token_type::number_token, d};
}
/*
* Generic tokens with no value (non-terminals)
*/
template<css_parser_token::token_type T>
auto make_token(void) -> css_parser_token
{
return css_parser_token{T, css_parser_token_placeholder()};
}
static constexpr inline auto is_plain_ident(char c) -> bool
{
if ((c & 0x80) || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_') {
return true;
}
return false;
};
auto
css_parser_token::adjust_dim(const css_parser_token &dim_token) -> bool
{
if (!std::holds_alternative<double>(value) ||
!std::holds_alternative<std::string_view>(dim_token.value)) {
/* Invalid tokens */
return false;
}
auto num = std::get<double>(value);
auto sv = std::get<std::string_view>(dim_token.value);
if (sv == "px") {
dim_type = css_parser_token::dim_type::dim_px;
flags |= css_parser_token::number_dimension;
num = (unsigned)num; /* Round to number */
}
else if (sv == "em") {
dim_type = css_parser_token::dim_type::dim_em;
flags |= css_parser_token::number_dimension;
/* EM is 16 px, so multiply and round */
num = (unsigned)(num * 16.0);
}
else if (sv == "rem") {
/* equal to EM in our case */
dim_type = css_parser_token::dim_type::dim_rem;
flags |= css_parser_token::number_dimension;
num = (unsigned)(num * 16.0);
}
else if (sv == "ex") {
/*
* Represents the x-height of the element's font.
* On fonts with the "x" letter, this is generally the height
* of lowercase letters in the font; 1ex = 0.5em in many fonts.
*/
dim_type = css_parser_token::dim_type::dim_ex;
flags |= css_parser_token::number_dimension;
num = (unsigned)(num * 8.0);
}
else if (sv == "wv") {
/*
* Vewport width in percentages:
* we assume 1% of viewport width as 8px
*/
dim_type = css_parser_token::dim_type::dim_wv;
flags |= css_parser_token::number_dimension;
num = (unsigned)(num * 8.0);
}
else if (sv == "wh") {
/*
* Vewport height in percentages
* we assume 1% of viewport width as 6px
*/
dim_type = css_parser_token::dim_type::dim_wh;
flags |= css_parser_token::number_dimension;
num = (unsigned)(num * 6.0);
}
else if (sv == "vmax") {
/*
* Vewport width in percentages
* we assume 1% of viewport width as 6px
*/
dim_type = css_parser_token::dim_type::dim_vmax;
flags |= css_parser_token::number_dimension;
num = (unsigned)(num * 8.0);
}
else if (sv == "vmin") {
/*
* Vewport height in percentages
* we assume 1% of viewport width as 6px
*/
dim_type = css_parser_token::dim_type::dim_vmin;
flags |= css_parser_token::number_dimension;
num = (unsigned)(num * 6.0);
}
else if (sv == "pt") {
dim_type = css_parser_token::dim_type::dim_pt;
flags |= css_parser_token::number_dimension;
num = (num * 96.0 / 72.0); /* One point. 1pt = 1/72nd of 1in */
}
else if (sv == "cm") {
dim_type = css_parser_token::dim_type::dim_cm;
flags |= css_parser_token::number_dimension;
num = (num * 96.0 / 2.54); /* 96px/2.54 */
}
else if (sv == "mm") {
dim_type = css_parser_token::dim_type::dim_mm;
flags |= css_parser_token::number_dimension;
num = (num * 9.6 / 2.54); /* 9.6px/2.54 */
}
else if (sv == "in") {
dim_type = css_parser_token::dim_type::dim_in;
flags |= css_parser_token::number_dimension;
num = (num * 96.0); /* 96px */
}
else if (sv == "pc") {
dim_type = css_parser_token::dim_type::dim_pc;
flags |= css_parser_token::number_dimension;
num = (num * 96.0 / 6.0); /* 1pc = 12pt = 1/6th of 1in. */
}
else {
flags |= css_parser_token::flag_bad_dimension;
return false;
}
value = num;
return true;
}
/*
* Consume functions: return a token and advance lexer offset
*/
auto css_tokeniser::consume_ident() -> struct css_parser_token
{
auto i = offset;
auto need_escape = false;
auto maybe_escape_sv = [&](auto cur_pos, auto tok_type) -> auto {
if (need_escape) {
auto escaped = rspamd::css::unescape_css(pool, {&input[offset],
cur_pos - offset});
offset = cur_pos;
return css_parser_token{tok_type, escaped};
}
auto result = std::string_view{&input[offset], cur_pos - offset};
offset = cur_pos;
return css_parser_token{tok_type, result};
};
/* Ident token can start from `-` or `--` */
if (input[i] == '-') {
i ++;
if (i < input.size() && input[i] == '-') {
i ++;
}
}
while (i < input.size()) {
auto c = input[i];
if (!is_plain_ident(c)) {
if (c == '\\' && i + 1 < input.size ()) {
/* Escape token */
need_escape = true;
auto nhex = 0;
/* Need to find an escape end */
do {
c = input[++i];
if (g_ascii_isxdigit(c)) {
nhex++;
if (nhex > 6) {
/* End of the escape */
break;
}
}
else if (nhex > 0 && c == ' ') {
/* \[hex]{1,6} */
i++; /* Skip one space */
break;
}
else {
/* Single \ + char */
break;
}
} while (i < input.size ());
}
else if (c == '(') {
/* Function or url token */
auto j = i + 1;
while (j < input.size() && g_ascii_isspace(input[j])) {
j ++;
}
if (input[j] == '"' || input[j] == '\'') {
/* Function token */
return maybe_escape_sv(j + 1,
css_parser_token::token_type::function_token);
}
else {
/* Consume URL token */
while (j < input.size() && input[j] != ')') {
j ++;
}
if (input[j] == ')') {
/* Valid url token */
return maybe_escape_sv(j + 1,
css_parser_token::token_type::url_token);
}
else {
/* Incomplete url token */
auto ret = maybe_escape_sv(j,
css_parser_token::token_type::url_token);
ret.flags |= css_parser_token::flag_bad_string;
return ret;
}
}
}
else {
i --; /* Push token back */
break; /* Not an ident token */
}
} /* !plain ident */
i ++;
}
return maybe_escape_sv(i, css_parser_token::token_type::ident_token);
}
auto css_tokeniser::consume_number() -> struct css_parser_token
{
auto i = offset;
auto seen_dot = false, seen_exp = false;
if (input[i] == '-') {
i ++;
}
if (input[i] == '.' && i < input.size()) {
seen_dot = true;
i ++;
}
while (i < input.size()) {
auto c = input[i];
if (!g_ascii_isdigit(c)) {
if (c == '.') {
if (!seen_dot) {
seen_dot = true;
}
else {
i --; /* Push back */
break;
}
}
else if (c == 'e' || c == 'E') {
if (!seen_exp) {
seen_exp = true;
seen_dot = true; /* dots are not allowed after e */
if (i + 1 < input.size()) {
auto next_c = input[i + 1];
if (next_c == '+' || next_c == '-') {
i ++;
}
}
}
else {
i --; /* Push back */
break;
}
}
}
i ++;
}
if (i > offset) {
double num;
/* I wish it was supported properly */
//auto conv_res = std::from_chars(&input[offset], &input[i], num);
std::string numbuf{&input[offset], (i - offset)};
num = std::stod(numbuf);
auto ret = make_token<css_parser_token::token_type::number_token>(num);
if (i < input.size()) {
if (input[i] == '%') {
ret.flags |= css_parser_token::number_percent;
i ++;
offset = i;
}
else if (is_plain_ident(input[i])) {
auto dim_token = consume_ident();
if (dim_token.type == css_parser_token::token_type::ident_token) {
if (!dim_token.adjust_dim(dim_token)) {
auto sv = std::get<std::string_view>(dim_token.value);
msg_debug_css("cannot apply dimension from the token %*s; number value = %.1f",
(int)sv.size(), sv.begin(), num);
}
}
else {
/* We have no option but to uncosume ident token in this case */
msg_debug_css("got invalid ident like token after number, unconsume it");
offset = i;
}
}
}
else {
offset = i;
}
return ret;
}
else {
msg_err_css("internal error: invalid number, empty token");
i ++;
}
offset = i;
/* Should not happen */
return make_token<css_parser_token::token_type::delim_token>(input[i - 1]);
}
/*
* Main routine to produce lexer tokens
*/
auto css_tokeniser::next_token(void) -> struct css_parser_token
{
/* Helpers */
/*
* This lambda eats comment handling nested comments;
* offset is set to the next character after a comment (or eof)
* Nothing is returned
*/
auto consume_comment = [this]() {
auto i = offset;
auto nested = 0;
/* We handle nested comments just because they can exist... */
while (i < input.size() - 1) {
auto c = input[i];
if (c == '*' && input[i + 1] == '/') {
if (nested == 0) {
offset = i + 2;
return;
}
else {
nested--;
i += 2;
continue;
}
}
else if (c == '/' && input[i + 1] == '*') {
nested++;
i += 2;
continue;
}
i++;
}
offset = i;
};
/*
* Consume quoted string, returns a string_view over a string, offset
* is set one character after the string. Css unescaping is done automatically
* Accepts a quote char to find end of string
*/
auto consume_string = [this](auto quote_char) -> auto {
auto i = offset;
bool need_unescape = false;
while (i < input.size()) {
auto c = input[i];
if (c == '\\') {
if (i + 1 < input.size()) {
need_unescape = true;
}
else {
/* \ at the end -> ignore */
}
}
else if (c == quote_char) {
/* End of string */
std::string_view res{&input[offset], i - offset};
if (need_unescape) {
res = rspamd::css::unescape_css(pool, res);
}
offset = i + 1;
return res;
}
else if (c == '\n') {
/* Should be a error, but we ignore it for now */
}
i ++;
}
/* EOF with no quote character, consider it fine */
std::string_view res{&input[offset], i - offset};
if (need_unescape) {
res = rspamd::css::unescape_css(pool, res);
}
offset = i;
return res;
};
/* Main tokenisation loop */
for (auto i = offset; i < input.size(); ++i) {
auto c = input[i];
switch (c) {
case '/':
if (i + 1 < input.size() && input[i + 1] == '*') {
offset = i + 2;
consume_comment(); /* Consume comment and go forward */
return next_token(); /* Tail call */
}
else {
offset = i + 1;
return make_token<css_parser_token::token_type::delim_token>(c);
}
break;
case ' ':
case '\t':
case '\n':
case '\r':
case '\v': {
/* Consume as much space as we can */
do {
c = input[++i];
} while (i < input.size() && g_ascii_isspace(c));
auto ret = make_token<css_parser_token::token_type::whitespace_token>(
std::string_view(&input[offset], i - offset));
offset = i;
return ret;
}
case '"':
case '\'':
offset = i + 1;
return make_token<css_parser_token::token_type::string_token>(consume_string(c));
case '(':
offset = i + 1;
return make_token<css_parser_token::token_type::obrace_token>();
case ')':
offset = i + 1;
return make_token<css_parser_token::token_type::ebrace_token>();
case '[':
offset = i + 1;
return make_token<css_parser_token::token_type::osqbrace_token>();
case ']':
offset = i + 1;
return make_token<css_parser_token::token_type::esqbrace_token>();
case '{':
offset = i + 1;
return make_token<css_parser_token::token_type::ocurlbrace_token>();
case '}':
offset = i + 1;
return make_token<css_parser_token::token_type::ecurlbrace_token>();
case ',':
return make_token<css_parser_token::token_type::comma_token>();
case ';':
return make_token<css_parser_token::token_type::semicolon_token>();
case '<':
/* Maybe an xml like comment */
if (i + 3 < input.size () && input[i + 1] == '!'
&& input[i + 2] == '-' && input[i + 3] == '-') {
offset += 3;
return make_token<css_parser_token::token_type::cdo_token>();
}
else {
offset = i + 1;
return make_token<css_parser_token::token_type::delim_token>(c);
}
break;
case '-':
if (i + 1 < input.size()) {
auto next_c = input[i + 1];
if (g_ascii_isdigit(next_c)) {
/* negative number */
return consume_number();
}
else if (next_c == '-') {
if (i + 2 < input.size() && input[i + 2] == '>') {
/* XML like comment */
return make_token<css_parser_token::token_type::cdc_token>();
}
}
}
/* No other options, a delimiter - */
offset = i + 1;
return make_token<css_parser_token::token_type::delim_token>(c);
break;
case '+':
case '.':
/* Maybe number */
if (i + 1 < input.size()) {
auto next_c = input[i + 1];
if (g_ascii_isdigit(next_c)) {
/* Numeric token */
return consume_number();
}
else {
offset = i + 1;
return make_token<css_parser_token::token_type::delim_token>(c);
}
}
/* No other options, a delimiter - */
offset = i + 1;
return make_token<css_parser_token::token_type::delim_token>(c);
break;
case '\\':
if (i + 1 < input.size()) {
if (input[i + 1] == '\n' || input[i + 1] == '\r') {
offset = i + 1;
return make_token<css_parser_token::token_type::delim_token>(c);
}
else {
/* Valid escape, assume ident */
return consume_ident();
}
}
else {
offset = i + 1;
return make_token<css_parser_token::token_type::delim_token>(c);
}
break;
case '@':
if (i + 3 < input.size()) {
if (is_plain_ident(input[i + 1]) &&
is_plain_ident(input[i + 2]) && is_plain_ident(input[i + 3])) {
offset = i + 1;
auto ident_token = consume_ident();
if (ident_token.type == css_parser_token::token_type::ident_token) {
/* Update type */
ident_token.type = css_parser_token::token_type::at_keyword_token;
}
return ident_token;
}
else {
offset = i + 1;
return make_token<css_parser_token::token_type::delim_token>(c);
}
}
else {
offset = i + 1;
return make_token<css_parser_token::token_type::delim_token>(c);
}
break;
case '#':
/* TODO: make it more conformant */
if (i + 2 < input.size()) {
if (is_plain_ident(input[i + 1]) &&
is_plain_ident(input[i + 2])) {
offset = i + 1;
auto ident_token = consume_ident();
if (ident_token.type == css_parser_token::token_type::ident_token) {
/* Update type */
ident_token.type = css_parser_token::token_type::hash_token;
}
return ident_token;
}
else {
offset = i + 1;
return make_token<css_parser_token::token_type::delim_token>(c);
}
}
else {
offset = i + 1;
return make_token<css_parser_token::token_type::delim_token>(c);
}
break;
default:
/* Generic parsing code */
if (g_ascii_isdigit(c)) {
return consume_number();
}
else if (is_plain_ident(c)) {
return consume_ident();
}
else {
offset = i + 1;
return make_token<css_parser_token::token_type::delim_token>(c);
}
break;
}
}
return make_token<css_parser_token::token_type::eof_token>();
}
constexpr auto css_parser_token::get_token_type() -> const char *
{
const char *ret = "unknown";
switch(type) {
case token_type::whitespace_token:
ret = "whitespace";
break;
case token_type::ident_token:
ret = "ident";
break;
case token_type::function_token:
ret = "function";
break;
case token_type::at_keyword_token:
ret = "atkeyword";
break;
case token_type::hash_token:
ret = "hash";
break;
case token_type::string_token:
ret = "string";
break;
case token_type::number_token:
ret = "number";
break;
case token_type::url_token:
ret = "url";
break;
case token_type::cdo_token: /* xml open comment */
ret = "cdo";
break;
case token_type::cdc_token: /* xml close comment */
ret = "cdc";
break;
case token_type::delim_token:
ret = "delim";
break;
case token_type::obrace_token: /* ( */
ret = "obrace";
break;
case token_type::ebrace_token: /* ) */
ret = "ebrace";
break;
case token_type::osqbrace_token: /* [ */
ret = "osqbrace";
break;
case token_type::esqbrace_token: /* ] */
ret = "esqbrace";
break;
case token_type::ocurlbrace_token: /* { */
ret = "ocurlbrace";
break;
case token_type::ecurlbrace_token: /* } */
ret = "ecurlbrace";
break;
case token_type::comma_token:
ret = "comma";
break;
case token_type::colon_token:
ret = "colon";
break;
case token_type::semicolon_token:
ret = "semicolon";
break;
case token_type::eof_token:
ret = "eof";
break;
}
return ret;
}
auto css_parser_token::debug_token_str() -> std::string
{
const auto *token_type_str = get_token_type();
std::string ret = token_type_str;
if (std::holds_alternative<std::string_view>(value)) {
ret += "; value=";
ret += std::get<std::string_view>(value);
}
else if (std::holds_alternative<double>(value)) {
ret += "; value=";
ret += std::to_string(std::get<double>(value));
}
else if (std::holds_alternative<char>(value)) {
ret += "; value=";
ret += std::get<char>(value);
}
if ((flags & (~number_dimension)) != default_flags) {
ret += "; flags=" + std::to_string(flags);
}
if (flags & number_dimension) {
ret += "; dim=" + std::to_string(static_cast<int>(dim_type));
}
return ret; /* Copy elision */
}
}
|