NCDConfigTokenizer.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 <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 (is_name_first_char(*str)) {
  81. l = 1;
  82. while (l < left) {
  83. if (!is_name_char(str[l])) {
  84. break;
  85. }
  86. l++;
  87. }
  88. // check size
  89. if (l > NCD_MAX_SIZE) {
  90. error = 1;
  91. goto out;
  92. }
  93. // copy and terminate
  94. memcpy(dec, str, l);
  95. dec[l] = '\0';
  96. if (!strcmp(dec, "process")) {
  97. token = NCD_TOKEN_PROCESS;
  98. }
  99. else {
  100. token = NCD_TOKEN_NAME;
  101. token_val = dec;
  102. }
  103. }
  104. else if (*str == '"') {
  105. size_t dec_len = 0;
  106. // decode string on the fly
  107. l = 1;
  108. while (l < left) {
  109. char dec_ch;
  110. if (str[l] == '\\') {
  111. if (!(l + 1 < left)) {
  112. error = 1;
  113. goto out;
  114. }
  115. dec_ch = str[l + 1];
  116. l += 2;
  117. }
  118. else if (str[l] == '"') {
  119. break;
  120. }
  121. else {
  122. dec_ch = str[l];
  123. l++;
  124. }
  125. // check size
  126. if (dec_len == NCD_MAX_SIZE) {
  127. error = 1;
  128. goto out;
  129. }
  130. // append decoded char
  131. dec[dec_len] = dec_ch;
  132. dec_len++;
  133. }
  134. // make sure closing quote was found
  135. if (l == left) {
  136. error = 1;
  137. goto out;
  138. }
  139. l++;
  140. // terminate
  141. dec[dec_len] = '\0';
  142. token = NCD_TOKEN_STRING;
  143. token_val = dec;
  144. }
  145. else if (is_space_char(*str)) {
  146. token = 0;
  147. l = 1;
  148. }
  149. else {
  150. error = 1;
  151. }
  152. out:
  153. // report error
  154. if (error) {
  155. output(user, NCD_ERROR, NULL, position);
  156. return;
  157. }
  158. // output token
  159. if (token) {
  160. if (!output(user, token, token_val, position)) {
  161. return;
  162. }
  163. }
  164. str += l;
  165. left -= l;
  166. position += l;
  167. }
  168. output(user, NCD_EOF, NULL, position);
  169. }