NCDConfigTokenizer.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /**
  2. * @file NCDConfigTokenizer.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. #include <string.h>
  23. #include <stddef.h>
  24. #include <stdlib.h>
  25. #include <misc/debug.h>
  26. #include <misc/string_begins_with.h>
  27. #include <misc/balloc.h>
  28. #include <misc/expstring.h>
  29. #include <base/BLog.h>
  30. #include <ncd/NCDConfigTokenizer.h>
  31. #include <generated/blog_channel_NCDConfigTokenizer.h>
  32. static int is_name_char (char c)
  33. {
  34. return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_');
  35. }
  36. static int is_name_first_char (char c)
  37. {
  38. return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_');
  39. }
  40. static int is_space_char (char c)
  41. {
  42. return (c == ' ' || c == '\t' || c == '\n' || c == '\r');
  43. }
  44. static int string_equals (char *str, int str_len, char *needle)
  45. {
  46. return (str_len == strlen(needle) && !memcmp(str, needle, str_len));
  47. }
  48. void NCDConfigTokenizer_Tokenize (char *str, size_t left, NCDConfigTokenizer_output output, void *user)
  49. {
  50. size_t line = 1;
  51. size_t line_char = 1;
  52. while (left > 0) {
  53. size_t l;
  54. int error = 0;
  55. int token;
  56. void *token_val = NULL;
  57. if (*str == '#') {
  58. l = 1;
  59. while (l < left && str[l] != '\n') {
  60. l++;
  61. }
  62. token = 0;
  63. }
  64. else if (l = data_begins_with(str, left, "{")) {
  65. token = NCD_TOKEN_CURLY_OPEN;
  66. }
  67. else if (l = data_begins_with(str, left, "}")) {
  68. token = NCD_TOKEN_CURLY_CLOSE;
  69. }
  70. else if (l = data_begins_with(str, left, "(")) {
  71. token = NCD_TOKEN_ROUND_OPEN;
  72. }
  73. else if (l = data_begins_with(str, left, ")")) {
  74. token = NCD_TOKEN_ROUND_CLOSE;
  75. }
  76. else if (l = data_begins_with(str, left, ";")) {
  77. token = NCD_TOKEN_SEMICOLON;
  78. }
  79. else if (l = data_begins_with(str, left, ".")) {
  80. token = NCD_TOKEN_DOT;
  81. }
  82. else if (l = data_begins_with(str, left, ",")) {
  83. token = NCD_TOKEN_COMMA;
  84. }
  85. else if (l = data_begins_with(str, left, "->")) {
  86. token = NCD_TOKEN_ARROW;
  87. }
  88. else if (is_name_first_char(*str)) {
  89. l = 1;
  90. while (l < left && is_name_char(str[l])) {
  91. l++;
  92. }
  93. // allocate buffer
  94. bsize_t bufsize = bsize_add(bsize_fromsize(l), bsize_fromint(1));
  95. char *buf;
  96. if (bufsize.is_overflow || !(buf = malloc(bufsize.value))) {
  97. BLog(BLOG_ERROR, "malloc failed");
  98. error = 1;
  99. goto out;
  100. }
  101. // copy and terminate
  102. memcpy(buf, str, l);
  103. buf[l] = '\0';
  104. if (!strcmp(buf, "process")) {
  105. token = NCD_TOKEN_PROCESS;
  106. free(buf);
  107. }
  108. else if (!strcmp(buf, "template")) {
  109. token = NCD_TOKEN_TEMPLATE;
  110. free(buf);
  111. }
  112. else {
  113. token = NCD_TOKEN_NAME;
  114. token_val = buf;
  115. }
  116. }
  117. else if (*str == '"') do {
  118. // init string
  119. ExpString estr;
  120. if (!ExpString_Init(&estr)) {
  121. BLog(BLOG_ERROR, "ExpString_Init failed");
  122. goto string_fail0;
  123. }
  124. // skip start quote
  125. l = 1;
  126. // decode string
  127. while (l < left) {
  128. char dec_ch;
  129. // get character
  130. if (str[l] == '\\') {
  131. if (left - l < 2) {
  132. BLog(BLOG_ERROR, "escape character found in string but nothing follows");
  133. goto string_fail1;
  134. }
  135. dec_ch = str[l + 1];
  136. l += 2;
  137. }
  138. else if (str[l] == '"') {
  139. break;
  140. }
  141. else {
  142. dec_ch = str[l];
  143. l++;
  144. }
  145. // string cannot contain zeros bytes
  146. if (dec_ch == '\0') {
  147. BLog(BLOG_ERROR, "string contains zero byte");
  148. goto string_fail1;
  149. }
  150. // append character to string
  151. if (!ExpString_AppendChar(&estr, dec_ch)) {
  152. BLog(BLOG_ERROR, "ExpString_AppendChar failed");
  153. goto string_fail1;
  154. }
  155. }
  156. // make sure ending quote was found
  157. if (l == left) {
  158. BLog(BLOG_ERROR, "missing ending quote for string");
  159. goto string_fail1;
  160. }
  161. // skip ending quote
  162. l++;
  163. token = NCD_TOKEN_STRING;
  164. token_val = ExpString_Get(&estr);
  165. break;
  166. string_fail1:
  167. ExpString_Free(&estr);
  168. string_fail0:
  169. error = 1;
  170. } while (0);
  171. else if (is_space_char(*str)) {
  172. token = 0;
  173. l = 1;
  174. }
  175. else {
  176. BLog(BLOG_ERROR, "unrecognized character");
  177. error = 1;
  178. }
  179. out:
  180. // report error
  181. if (error) {
  182. output(user, NCD_ERROR, NULL, line, line_char);
  183. return;
  184. }
  185. // output token
  186. if (token) {
  187. if (!output(user, token, token_val, line, line_char)) {
  188. return;
  189. }
  190. }
  191. // update line/char counters
  192. for (size_t i = 0; i < l; i++) {
  193. if (str[i] == '\n') {
  194. line++;
  195. line_char = 1;
  196. } else {
  197. line_char++;
  198. }
  199. }
  200. str += l;
  201. left -= l;
  202. }
  203. output(user, NCD_EOF, NULL, line, line_char);
  204. }