]> git.ipfire.org Git - thirdparty/hostap.git/commitdiff
wpa_supplicant: Move wpa_config_get_line() into utils
authorPatrick Steinhardt <ps@pks.im>
Sun, 14 Feb 2021 11:16:28 +0000 (12:16 +0100)
committerJouni Malinen <j@w1.fi>
Mon, 15 Feb 2021 22:47:43 +0000 (00:47 +0200)
The function wpa_config_get_line() is used by the wpa_supplicant config
file parser to retrieve the next non-comment non-blank line. We'll need
the same kind of functionality to implement the file-based external
password backend, so as a preparatory step this commit extracts the
function into its own standalone file in the utils package.

No functional changes are expected from this commit.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
src/utils/Makefile
src/utils/config.c [new file with mode: 0644]
src/utils/config.h [new file with mode: 0644]
tests/fuzzing/wnm/wnm.c
wpa_supplicant/Android.mk
wpa_supplicant/Makefile
wpa_supplicant/config_file.c
wpa_supplicant/vs2005/eapol_test/eapol_test.vcproj
wpa_supplicant/vs2005/wpa_supplicant/wpa_supplicant.vcproj
wpa_supplicant/vs2005/wpasvc/wpasvc.vcproj

index e8ad997eed76ddc049c8e04faed29f9d5e25ad6f..d995b8178a1dd94d3077803b1816c5f746bd7315 100644 (file)
@@ -6,6 +6,7 @@ LIB_OBJS= \
        base64.o \
        bitfield.o \
        common.o \
+       config.o \
        crc32.o \
        ip_addr.o \
        json.o \
diff --git a/src/utils/config.c b/src/utils/config.c
new file mode 100644 (file)
index 0000000..22aa221
--- /dev/null
@@ -0,0 +1,97 @@
+/*
+ * Configuration parsing
+ * Copyright (c) 2003-2019, Jouni Malinen <j@w1.fi>
+ *
+ * This software may be distributed under the terms of the BSD license.
+ * See README for more details.
+ */
+
+#include "includes.h"
+
+#include "utils/config.h"
+#include "common.h"
+
+
+static int newline_terminated(const char *buf, size_t buflen)
+{
+       size_t len = os_strlen(buf);
+       if (len == 0)
+               return 0;
+       if (len == buflen - 1 && buf[buflen - 1] != '\r' &&
+           buf[len - 1] != '\n')
+               return 0;
+       return 1;
+}
+
+
+static void skip_line_end(FILE *stream)
+{
+       char buf[100];
+       while (fgets(buf, sizeof(buf), stream)) {
+               buf[sizeof(buf) - 1] = '\0';
+               if (newline_terminated(buf, sizeof(buf)))
+                       return;
+       }
+}
+
+
+char * wpa_config_get_line(char *s, int size, FILE *stream, int *line,
+                          char **_pos)
+{
+       char *pos, *end, *sstart;
+
+       while (fgets(s, size, stream)) {
+               (*line)++;
+               s[size - 1] = '\0';
+               if (!newline_terminated(s, size)) {
+                       /*
+                        * The line was truncated - skip rest of it to avoid
+                        * confusing error messages.
+                        */
+                       wpa_printf(MSG_INFO, "Long line in configuration file "
+                                  "truncated");
+                       skip_line_end(stream);
+               }
+               pos = s;
+
+               /* Skip white space from the beginning of line. */
+               while (*pos == ' ' || *pos == '\t' || *pos == '\r')
+                       pos++;
+
+               /* Skip comment lines and empty lines */
+               if (*pos == '#' || *pos == '\n' || *pos == '\0')
+                       continue;
+
+               /*
+                * Remove # comments unless they are within a double quoted
+                * string.
+                */
+               sstart = os_strchr(pos, '"');
+               if (sstart)
+                       sstart = os_strrchr(sstart + 1, '"');
+               if (!sstart)
+                       sstart = pos;
+               end = os_strchr(sstart, '#');
+               if (end)
+                       *end-- = '\0';
+               else
+                       end = pos + os_strlen(pos) - 1;
+
+               /* Remove trailing white space. */
+               while (end > pos &&
+                      (*end == '\n' || *end == ' ' || *end == '\t' ||
+                       *end == '\r'))
+                       *end-- = '\0';
+
+               if (*pos == '\0')
+                       continue;
+
+               if (_pos)
+                       *_pos = pos;
+               return pos;
+       }
+
+       if (_pos)
+               *_pos = NULL;
+       return NULL;
+}
diff --git a/src/utils/config.h b/src/utils/config.h
new file mode 100644 (file)
index 0000000..074a88a
--- /dev/null
@@ -0,0 +1,29 @@
+/*
+ * Configuration parsing
+ * Copyright (c) 2003-2019, Jouni Malinen <j@w1.fi>
+ *
+ * This software may be distributed under the terms of the BSD license.
+ * See README for more details.
+ */
+
+#ifndef UTILS_CONFIG_H
+#define UTILS_CONFIG_H
+
+/**
+ * wpa_config_get_line - Read the next configuration file line
+ * @s: Buffer for the line
+ * @size: The buffer length
+ * @stream: File stream to read from
+ * @line: Pointer to a variable storing the file line number
+ * @_pos: Buffer for the pointer to the beginning of data on the text line or
+ * %NULL if not needed (returned value used instead)
+ * Returns: Pointer to the beginning of data on the text line or %NULL if no
+ * more text lines are available.
+ *
+ * This function reads the next non-empty line from the configuration file and
+ * removes comments. The returned string is guaranteed to be null-terminated.
+ */
+char * wpa_config_get_line(char *s, int size, FILE *stream, int *line,
+                          char **_pos);
+
+#endif /* UTILS_CONFIG_H */
index 9c0d5418b7e4e63c1b447696dee818c09480320a..7afc648e442aff16f72a41c8b4d97cb968f866e2 100644 (file)
@@ -16,7 +16,7 @@
 #include "wpa_supplicant_i.h"
 #include "bss.h"
 #include "wnm_sta.h"
-#include "config.h"
+#include "../../../wpa_supplicant/config.h"
 #include "../fuzzer-common.h"
 
 
index 4cbce995c8507fb499f9b32cb5df791cffdb7d43..5b551d4df0c81b7956b6c651fdbd229730faeab8 100644 (file)
@@ -94,6 +94,7 @@ OBJS += notify.c
 OBJS += bss.c
 OBJS += eap_register.c
 OBJS += src/utils/common.c
+OBJS += src/utils/config.c
 OBJS += src/utils/wpa_debug.c
 OBJS += src/utils/wpabuf.c
 OBJS += src/utils/bitfield.c
index 59b054cd2a99b1f0109867e08f69767455e7fc64..5e9a696f6f2f569f3afea8ec4bcbad2726a2cace 100644 (file)
@@ -83,6 +83,7 @@ OBJS += notify.o
 OBJS += bss.o
 OBJS += eap_register.o
 OBJS += ../src/utils/common.o
+OBJS += ../src/utils/config.o
 OBJS += ../src/utils/wpa_debug.o
 OBJS += ../src/utils/wpabuf.o
 OBJS += ../src/utils/bitfield.o
index fd6275e225bca92aa774c2717ee4c65d0d058141..a535e3f08aadaab21a0570932d9d71c56d6b0435 100644 (file)
 #include "p2p/p2p.h"
 #include "eap_peer/eap_methods.h"
 #include "eap_peer/eap.h"
-
-
-static int newline_terminated(const char *buf, size_t buflen)
-{
-       size_t len = os_strlen(buf);
-       if (len == 0)
-               return 0;
-       if (len == buflen - 1 && buf[buflen - 1] != '\r' &&
-           buf[len - 1] != '\n')
-               return 0;
-       return 1;
-}
-
-
-static void skip_line_end(FILE *stream)
-{
-       char buf[100];
-       while (fgets(buf, sizeof(buf), stream)) {
-               buf[sizeof(buf) - 1] = '\0';
-               if (newline_terminated(buf, sizeof(buf)))
-                       return;
-       }
-}
-
-
-/**
- * wpa_config_get_line - Read the next configuration file line
- * @s: Buffer for the line
- * @size: The buffer length
- * @stream: File stream to read from
- * @line: Pointer to a variable storing the file line number
- * @_pos: Buffer for the pointer to the beginning of data on the text line or
- * %NULL if not needed (returned value used instead)
- * Returns: Pointer to the beginning of data on the text line or %NULL if no
- * more text lines are available.
- *
- * This function reads the next non-empty line from the configuration file and
- * removes comments. The returned string is guaranteed to be null-terminated.
- */
-static char * wpa_config_get_line(char *s, int size, FILE *stream, int *line,
-                                 char **_pos)
-{
-       char *pos, *end, *sstart;
-
-       while (fgets(s, size, stream)) {
-               (*line)++;
-               s[size - 1] = '\0';
-               if (!newline_terminated(s, size)) {
-                       /*
-                        * The line was truncated - skip rest of it to avoid
-                        * confusing error messages.
-                        */
-                       wpa_printf(MSG_INFO, "Long line in configuration file "
-                                  "truncated");
-                       skip_line_end(stream);
-               }
-               pos = s;
-
-               /* Skip white space from the beginning of line. */
-               while (*pos == ' ' || *pos == '\t' || *pos == '\r')
-                       pos++;
-
-               /* Skip comment lines and empty lines */
-               if (*pos == '#' || *pos == '\n' || *pos == '\0')
-                       continue;
-
-               /*
-                * Remove # comments unless they are within a double quoted
-                * string.
-                */
-               sstart = os_strchr(pos, '"');
-               if (sstart)
-                       sstart = os_strrchr(sstart + 1, '"');
-               if (!sstart)
-                       sstart = pos;
-               end = os_strchr(sstart, '#');
-               if (end)
-                       *end-- = '\0';
-               else
-                       end = pos + os_strlen(pos) - 1;
-
-               /* Remove trailing white space. */
-               while (end > pos &&
-                      (*end == '\n' || *end == ' ' || *end == '\t' ||
-                       *end == '\r'))
-                       *end-- = '\0';
-
-               if (*pos == '\0')
-                       continue;
-
-               if (_pos)
-                       *_pos = pos;
-               return pos;
-       }
-
-       if (_pos)
-               *_pos = NULL;
-       return NULL;
-}
+#include "utils/config.h"
 
 
 static int wpa_config_validate_network(struct wpa_ssid *ssid, int line)
index f9522fc1c47c5566d98ffe48a9eb0d7232c2fc3b..c92b8fd89d6cc04e6772165fe21f4fe846b6ed4e 100755 (executable)
                                RelativePath="..\..\..\src\utils\common.c"\r
                                >\r
                        </File>\r
+                       <File\r
+                               RelativePath="..\..\..\src\utils\config.c"\r
+                               >\r
+                       </File>\r
                        <File\r
                                RelativePath="..\..\config.c"\r
                                >\r
index 5867544d4fb6ca69c448d6cc7cc5e554b89eaeb4..10c05b56559743ecf0edc94ae0a43ac6acc2a8ae 100755 (executable)
                                RelativePath="..\..\..\src\utils\common.c"\r
                                >\r
                        </File>\r
+                       <File\r
+                               RelativePath="..\..\..\src\utils\config.c"\r
+                               >\r
+                       </File>\r
                        <File\r
                                RelativePath="..\..\config.c"\r
                                >\r
index 21d600b719bd69d91fd569b0acb80f570c70d7fd..82d9033ffe8edebee9e8c1eef2d2350802455f2a 100755 (executable)
                                RelativePath="..\..\..\src\utils\common.c"\r
                                >\r
                        </File>\r
+                       <File\r
+                               RelativePath="..\..\..\src\utils\config.c"\r
+                               >\r
+                       </File>\r
                        <File\r
                                RelativePath="..\..\config.c"\r
                                >\r