Fix issues detected by static analysis tool
[platform/upstream/libxkbcommon.git] / src / scanner-utils.h
1 /*
2  * Copyright © 2012 Ran Benita <ran234@gmail.com>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  */
23
24 #ifndef XKBCOMP_SCANNER_UTILS_H
25 #define XKBCOMP_SCANNER_UTILS_H
26
27 /* Point to some substring in the file; used to avoid copying. */
28 struct sval {
29     const char *start;
30     unsigned int len;
31 };
32 typedef darray(struct sval) darray_sval;
33
34 static inline bool
35 svaleq(struct sval s1, struct sval s2)
36 {
37     return s1.len == s2.len && memcmp(s1.start, s2.start, s1.len) == 0;
38 }
39
40 static inline bool
41 svaleq_prefix(struct sval s1, struct sval s2)
42 {
43     return s1.len <= s2.len && memcmp(s1.start, s2.start, s1.len) == 0;
44 }
45
46 struct scanner {
47     const char *s;
48     size_t pos;
49     size_t len;
50     char buf[1024];
51     size_t buf_pos;
52     size_t line, column;
53     /* The line/column of the start of the current token. */
54     size_t token_line, token_column;
55     const char *file_name;
56     struct xkb_context *ctx;
57     void *priv;
58 };
59
60 #define scanner_log_with_code(scanner, level, log_msg_id, fmt, ...) \
61     xkb_log_with_code((scanner)->ctx, (level), 0, log_msg_id, \
62                     "%s:%zu:%zu: " fmt "\n", \
63                     (scanner)->file_name, \
64                     (scanner)->token_line, \
65                     (scanner)->token_column, ##__VA_ARGS__)
66
67 #define scanner_log(scanner, level, fmt, ...) \
68     xkb_log((scanner)->ctx, (level), 0, \
69             "%s:%zu:%zu: " fmt "\n", \
70             (scanner)->file_name, \
71             (scanner)->token_line, (scanner)->token_column, ##__VA_ARGS__)
72
73 #define scanner_err_with_code(scanner, id, fmt, ...) \
74     scanner_log_with_code(scanner, XKB_LOG_LEVEL_ERROR, id, fmt, ##__VA_ARGS__)
75
76 #define scanner_err(scanner, fmt, ...) \
77     scanner_log(scanner, XKB_LOG_LEVEL_ERROR, fmt, ##__VA_ARGS__)
78
79 #define scanner_warn_with_code(scanner, id, fmt, ...) \
80     scanner_log_with_code(scanner, XKB_LOG_LEVEL_WARNING, id, fmt, ##__VA_ARGS__)
81
82 #define scanner_warn(scanner, fmt, ...) \
83     scanner_log(scanner, XKB_LOG_LEVEL_WARNING, fmt, ##__VA_ARGS__)
84
85 static inline void
86 scanner_init(struct scanner *s, struct xkb_context *ctx,
87              const char *string, size_t len, const char *file_name,
88              void *priv)
89 {
90     s->s = string;
91     s->len = len;
92     s->pos = 0;
93     s->line = s->column = 1;
94     s->token_line = s->token_column = 1;
95     s->file_name = file_name;
96     s->ctx = ctx;
97     s->priv = priv;
98 }
99
100 static inline char
101 scanner_peek(struct scanner *s)
102 {
103     if (unlikely(s->pos >= s->len))
104         return '\0';
105     return s->s[s->pos];
106 }
107
108 static inline bool
109 scanner_eof(struct scanner *s)
110 {
111     return s->pos >= s->len;
112 }
113
114 static inline bool
115 scanner_eol(struct scanner *s)
116 {
117     return scanner_peek(s) == '\n';
118 }
119
120 static inline void
121 scanner_skip_to_eol(struct scanner *s)
122 {
123     const char *nl = memchr(s->s + s->pos, '\n', s->len - s->pos);
124     const size_t new_pos = nl ? (size_t) (nl - s->s) : s->len;
125     s->column += new_pos - s->pos;
126     s->pos = new_pos;
127 }
128
129 static inline char
130 scanner_next(struct scanner *s)
131 {
132     if (unlikely(scanner_eof(s)))
133         return '\0';
134     if (unlikely(scanner_eol(s))) {
135         s->line++;
136         s->column = 1;
137     }
138     else {
139         s->column++;
140     }
141     return s->s[s->pos++];
142 }
143
144 static inline bool
145 scanner_chr(struct scanner *s, char ch)
146 {
147     if (likely(scanner_peek(s) != ch))
148         return false;
149     s->pos++; s->column++;
150     return true;
151 }
152
153 static inline bool
154 scanner_str(struct scanner *s, const char *string, size_t len)
155 {
156     if (s->len - s->pos < len)
157         return false;
158     if (memcmp(s->s + s->pos, string, len) != 0)
159         return false;
160     s->pos += len; s->column += len;
161     return true;
162 }
163
164 #define scanner_lit(s, literal) scanner_str(s, literal, sizeof(literal) - 1)
165
166 static inline bool
167 scanner_buf_append(struct scanner *s, char ch)
168 {
169     if (s->buf_pos + 1 >= sizeof(s->buf))
170         return false;
171     s->buf[s->buf_pos++] = ch;
172     return true;
173 }
174
175 static inline bool
176 scanner_buf_appends(struct scanner *s, const char *str)
177 {
178     int ret;
179     ret = snprintf(s->buf + s->buf_pos, sizeof(s->buf) - s->buf_pos, "%s", str);
180     if (ret < 0 || (size_t) ret >= sizeof(s->buf) - s->buf_pos)
181         return false;
182     s->buf_pos += ret;
183     return true;
184 }
185
186 static inline bool
187 scanner_oct(struct scanner *s, uint8_t *out)
188 {
189     int i;
190     for (i = 0, *out = 0; scanner_peek(s) >= '0' && scanner_peek(s) <= '7' && i < 3; i++)
191         /* Test overflow */
192         if (*out < 040) {
193             *out = *out * 8 + scanner_next(s) - '0';
194         } else {
195             /* Consume valid digit, but mark result as invalid */
196             scanner_next(s);
197             return false;
198         }
199     return i > 0;
200 }
201
202 static inline bool
203 scanner_hex(struct scanner *s, uint8_t *out)
204 {
205     int i;
206     for (i = 0, *out = 0; is_xdigit(scanner_peek(s)) && i < 2; i++) {
207         const char c = scanner_next(s);
208         const char offset = (c >= '0' && c <= '9' ? '0' :
209                              c >= 'a' && c <= 'f' ? 'a' - 10 : 'A' - 10);
210
211         if (*out * 16 + c >= offset)
212             *out = *out * 16 + c - offset;
213     }
214     return i > 0;
215 }
216
217 #endif