]> git.ipfire.org Git - people/ms/libloc.git/commitdiff
location-export: Validate country codes
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 5 Dec 2019 15:47:16 +0000 (15:47 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 5 Dec 2019 15:47:16 +0000 (15:47 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/country.c
src/libloc.sym
src/loc/country.h
src/python/location-exporter.in
src/python/locationmodule.c

index 909895047f922eeb5578c016b21f29f51a7af5f2..45d26b82ef49dd156df61b6d66555185c37e5f33 100644 (file)
@@ -172,3 +172,22 @@ int loc_country_to_database_v0(struct loc_country* country,
 
        return 0;
 }
+
+LOC_EXPORT int loc_country_code_is_valid(const char* cc) {
+       // It cannot be NULL
+       if (!cc || !*cc)
+               return 0;
+
+       // It must be 2 characters long
+       if (strlen(cc) != 2)
+               return 0;
+
+       // It must only contain A-Z
+       for (unsigned int i = 0; i < 2; i++) {
+               if (cc[i] < 'A' || cc[i] > 'Z')
+                       return 0;
+       }
+
+       // Looks valid
+       return 1;
+}
index b3d5b51d25e136ea804c274d7f6ff2735eb3c301..e9e85494839baeb5a4c1fff18839d4ac0a6f1fdc 100644 (file)
@@ -39,6 +39,7 @@ global:
 
        # Country
        loc_country_cmp;
+       loc_country_code_is_valid;
        loc_country_get_code;
        loc_country_get_continent_code;
        loc_country_get_name;
index 6dfb47ba3204b0208e3349b3b007544d6ab4316a..9757b2a77d6ff64f03ed14345666ad775da66be6 100644 (file)
@@ -36,6 +36,8 @@ int loc_country_set_name(struct loc_country* country, const char* name);
 
 int loc_country_cmp(struct loc_country* country1, struct loc_country* country2);
 
+int loc_country_code_is_valid(const char* cc);
+
 #ifdef LIBLOC_PRIVATE
 
 #include <string.h>
@@ -45,25 +47,6 @@ int loc_country_new_from_database_v0(struct loc_ctx* ctx, struct loc_stringpool*
 int loc_country_to_database_v0(struct loc_country* country,
     struct loc_stringpool* pool, struct loc_database_country_v0* dbobj);
 
-static inline int loc_country_code_is_valid(const char* cc) {
-       // It cannot be NULL
-       if (!cc || !*cc)
-               return 0;
-
-       // It must be 2 characters long
-       if (strlen(cc) != 2)
-               return 0;
-
-       // It must only contain A-Z
-       for (unsigned int i = 0; i < 2; i++) {
-               if (cc[i] < 'A' || cc[i] > 'Z')
-                       return 0;
-       }
-
-       // Looks valid
-       return 1;
-}
-
 static inline void loc_country_code_copy(char* dst, const char* src) {
     for (unsigned int i = 0; i < 2; i++) {
         dst[i] = src[i];
index 1e3be265c4977465d9132ff8c954d9c659e02224..12bc29a1070e57d08b7bec7f0d8b79e507b0f91d 100644 (file)
@@ -280,12 +280,12 @@ class CLI(object):
 
                                asns.append(object)
 
-                       elif not len(object) == 2:
-                               log.error("Invalid argument: %s" % object)
-                               return 2
+                       elif location.country_code_is_valid(object):
+                               countries.append(object)
 
                        else:
-                               countries.append(object)
+                               log.error("Invalid argument: %s" % object)
+                               return 2
 
                # Open the database
                try:
index 78cf07539b60ee002f7e951b8f5cd3c7f550c360..6e7269ba0aae84bc0c4a86c6d6019c6b17718606 100644 (file)
@@ -65,7 +65,25 @@ static PyObject* discover_latest_version(PyObject* m, PyObject* args) {
        return PyLong_FromUnsignedLong(t);
 }
 
+static PyObject* country_code_is_valid(PyObject* m, PyObject* args) {
+       const char* country_code = NULL;
+
+       if (!PyArg_ParseTuple(args, "s", &country_code))
+               return NULL;
+
+       if (loc_country_code_is_valid(country_code))
+               Py_RETURN_TRUE;
+
+       Py_RETURN_FALSE;
+}
+
 static PyMethodDef location_module_methods[] = {
+       {
+               "country_code_is_valid",
+               (PyCFunction)country_code_is_valid,
+               METH_VARARGS,
+               NULL,
+       },
        {
                "discover_latest_version",
                (PyCFunction)discover_latest_version,