NCDConfigTokenizer.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /**
  2. * @file NCDConfigTokenizer.c
  3. * @author Ambroz Bizjak <[email protected]>
  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 <misc/debug.h>
  25. #include <misc/string_begins_with.h>
  26. #include <ncdconfig/NCDConfigTokenizer.h>
  27. static int is_name_char (char c)
  28. {
  29. return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_');
  30. }
  31. static int is_name_first_char (char c)
  32. {
  33. return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_');
  34. }
  35. static int is_space_char (char c)
  36. {
  37. return (c == ' ' || c == '\t' || c == '\n' || c == '\r');
  38. }
  39. static int string_equals (char *str, int str_len, char *needle)
  40. {
  41. return (str_len == strlen(needle) && !memcmp(str, needle, str_len));
  42. }
  43. void NCDConfigTokenizer_Tokenize (char *str, size_t left, NCDConfigTokenizer_output output, void *user)
  44. {
  45. size_t position = 0;
  46. while (left > 0) {
  47. size_t l;
  48. int error = 0;
  49. int token;
  50. void *token_val = NULL;
  51. char dec[NCD_MAX_SIZE + 1];
  52. if (*str == '#') {
  53. l = 1;
  54. while (l < left && str[l] != '\n') {
  55. l++;
  56. }
  57. token = 0;
  58. }
  59. else if (l = data_begins_with(str, left, "{")) {
  60. token = NCD_TOKEN_CURLY_OPEN;
  61. }
  62. else if (l = data_begins_with(str, left, "}")) {
  63. token = NCD_TOKEN_CURLY_CLOSE;
  64. }
  65. else if (l = data_begins_with(str, left, "(")) {
  66. token = NCD_TOKEN_ROUND_OPEN;
  67. }
  68. else if (l = data_begins_with(str, left, ")")) {
  69. token = NCD_TOKEN_ROUND_CLOSE;
  70. }
  71. else if (l = data_begins_with(str, left, ";")) {
  72. token = NCD_TOKEN_SEMICOLON;
  73. }
  74. else if (l = data_begins_with(str, left, ".")) {
  75. token = NCD_TOKEN_DOT;
  76. }
  77. else if (l = data_begins_with(str, left, ",")) {
  78. token = NCD_TOKEN_COMMA;
  79. }
  80. else if (l = data_begins_with(str, left, "->")) {
  81. token = NCD_TOKEN_ARROW;
  82. }
  83. else if (is_name_first_char(*str)) {
  84. l = 1;
  85. while (l < left) {
  86. if (!is_name_char(str[l])) {
  87. break;
  88. }
  89. l++;
  90. }
  91. // check size
  92. if (l > NCD_MAX_SIZE) {
  93. error = 1;
  94. goto out;
  95. }
  96. // copy and terminate
  97. memcpy(dec, str, l);
  98. dec[l] = '\0';
  99. if (!strcmp(dec, "process")) {
  100. token = NCD_TOKEN_PROCESS;
  101. }
  102. else {
  103. token = NCD_TOKEN_NAME;
  104. token_val = dec;
  105. }
  106. }
  107. else if (*str == '"') {
  108. size_t dec_len = 0;
  109. // decode string on the fly
  110. l = 1;
  111. while (l < left) {
  112. char dec_ch;
  113. if (str[l] == '\\') {
  114. if (!(l + 1 < left)) {
  115. error = 1;
  116. goto out;
  117. }
  118. dec_ch = str[l + 1];
  119. l += 2;
  120. }
  121. else if (str[l] == '"') {
  122. break;
  123. }
  124. else {
  125. dec_ch = str[l];
  126. l++;
  127. }
  128. // check size
  129. if (dec_len == NCD_MAX_SIZE) {
  130. error = 1;
  131. goto out;
  132. }
  133. // append decoded char
  134. dec[dec_len] = dec_ch;
  135. dec_len++;
  136. }
  137. // make sure closing quote was found
  138. if (l == left) {
  139. error = 1;
  140. goto out;
  141. }
  142. l++;
  143. // terminate
  144. dec[dec_len] = '\0';
  145. token = NCD_TOKEN_STRING;
  146. token_val = dec;
  147. }
  148. else if (is_space_char(*str)) {
  149. token = 0;
  150. l = 1;
  151. }
  152. else {
  153. error = 1;
  154. }
  155. out:
  156. // report error
  157. if (error) {
  158. output(user, NCD_ERROR, NULL, position);
  159. return;
  160. }
  161. // output token
  162. if (token) {
  163. if (!output(user, token, token_val, position)) {
  164. return;
  165. }
  166. }
  167. str += l;
  168. left -= l;
  169. position += l;
  170. }
  171. output(user, NCD_EOF, NULL, position);
  172. }