NCDConfigTokenizer.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 (l = data_begins_with(str, left, "{")) {
  53. token = NCD_TOKEN_CURLY_OPEN;
  54. }
  55. else if (l = data_begins_with(str, left, "}")) {
  56. token = NCD_TOKEN_CURLY_CLOSE;
  57. }
  58. else if (l = data_begins_with(str, left, ";")) {
  59. token = NCD_TOKEN_SEMICOLON;
  60. }
  61. else if (l = data_begins_with(str, left, ".")) {
  62. token = NCD_TOKEN_DOT;
  63. }
  64. else if (is_name_first_char(*str)) {
  65. l = 1;
  66. while (l < left) {
  67. if (!is_name_char(str[l])) {
  68. break;
  69. }
  70. l++;
  71. }
  72. // check size
  73. if (l > NCD_MAX_SIZE) {
  74. error = 1;
  75. goto out;
  76. }
  77. // copy and terminate
  78. memcpy(dec, str, l);
  79. dec[l] = '\0';
  80. if (!strcmp(dec, "interface")) {
  81. token = NCD_TOKEN_INTERFACE;
  82. }
  83. else {
  84. token = NCD_TOKEN_NAME;
  85. token_val = dec;
  86. }
  87. }
  88. else if (*str == '"') {
  89. size_t dec_len = 0;
  90. // decode string on the fly
  91. l = 1;
  92. while (l < left) {
  93. char dec_ch;
  94. if (str[l] == '\\') {
  95. if (!(l + 1 < left)) {
  96. error = 1;
  97. goto out;
  98. }
  99. dec_ch = str[l + 1];
  100. l += 2;
  101. }
  102. else if (str[l] == '"') {
  103. break;
  104. }
  105. else {
  106. dec_ch = str[l];
  107. l++;
  108. }
  109. // check size
  110. if (dec_len == NCD_MAX_SIZE) {
  111. error = 1;
  112. goto out;
  113. }
  114. // append decoded char
  115. dec[dec_len] = dec_ch;
  116. dec_len++;
  117. }
  118. // make sure closing quote was found
  119. if (l == left) {
  120. error = 1;
  121. goto out;
  122. }
  123. l++;
  124. // terminate
  125. dec[dec_len] = '\0';
  126. token = NCD_TOKEN_STRING;
  127. token_val = dec;
  128. }
  129. else if (is_space_char(*str)) {
  130. token = 0;
  131. l = 1;
  132. }
  133. else {
  134. error = 1;
  135. }
  136. out:
  137. // report error
  138. if (error) {
  139. output(user, NCD_ERROR, NULL, position);
  140. return;
  141. }
  142. // output token
  143. if (token) {
  144. if (!output(user, token, token_val, position)) {
  145. return;
  146. }
  147. }
  148. str += l;
  149. left -= l;
  150. position += l;
  151. }
  152. output(user, NCD_EOF, NULL, position);
  153. }