Kaynağa Gözat

ncd: modules: use ncd_read_uintmax() to parse numeric arguments

ambrop7 13 yıl önce
ebeveyn
işleme
50af137e19

+ 11 - 7
ncd/modules/arithmetic.c

@@ -69,7 +69,6 @@
 #include <inttypes.h>
 #include <limits.h>
 
-#include <misc/parse_number.h>
 #include <ncd/NCDModule.h>
 #include <ncd/static_strings.h>
 #include <ncd/value_utils.h>
@@ -188,19 +187,19 @@ static void new_boolean_templ (void *vo, NCDModuleInst *i, const struct NCDModul
         ModuleLog(i, BLOG_ERROR, "wrong arity");
         goto fail0;
     }
-    if (!NCDVal_IsStringNoNulls(n1_arg) || !NCDVal_IsStringNoNulls(n2_arg)) {
+    if (!NCDVal_IsString(n1_arg) || !NCDVal_IsString(n2_arg)) {
         ModuleLog(o->i, BLOG_ERROR, "wrong type");
         goto fail0;
     }
     
     uintmax_t n1;
-    if (!parse_unsigned_integer_bin(NCDVal_StringValue(n1_arg), NCDVal_StringLength(n1_arg), &n1)) {
+    if (!ncd_read_uintmax(n1_arg, &n1)) {
         ModuleLog(o->i, BLOG_ERROR, "wrong first argument");
         goto fail0;
     }
     
     uintmax_t n2;
-    if (!parse_unsigned_integer_bin(NCDVal_StringValue(n2_arg), NCDVal_StringLength(n2_arg), &n2)) {
+    if (!ncd_read_uintmax(n2_arg, &n2)) {
         ModuleLog(o->i, BLOG_ERROR, "wrong second argument");
         goto fail0;
     }
@@ -241,15 +240,20 @@ static void new_number_templ (void *vo, NCDModuleInst *i, const struct NCDModule
         ModuleLog(i, BLOG_ERROR, "wrong arity");
         goto fail0;
     }
-    if (!NCDVal_IsStringNoNulls(n1_arg) || !NCDVal_IsStringNoNulls(n2_arg)) {
+    if (!NCDVal_IsString(n1_arg) || !NCDVal_IsString(n2_arg)) {
         ModuleLog(o->i, BLOG_ERROR, "wrong type");
         goto fail0;
     }
     
     uintmax_t n1;
+    if (!ncd_read_uintmax(n1_arg, &n1)) {
+        ModuleLog(o->i, BLOG_ERROR, "wrong first argument");
+        goto fail0;
+    }
+    
     uintmax_t n2;
-    if (!parse_unsigned_integer(NCDVal_StringValue(n1_arg), &n1) || !parse_unsigned_integer(NCDVal_StringValue(n2_arg), &n2)) {
-        ModuleLog(o->i, BLOG_ERROR, "wrong value");
+    if (!ncd_read_uintmax(n2_arg, &n2)) {
+        ModuleLog(o->i, BLOG_ERROR, "wrong second argument");
         goto fail0;
     }
     

+ 3 - 3
ncd/modules/exit.c

@@ -39,8 +39,8 @@
 
 #include <limits.h>
 
-#include <misc/parse_number.h>
 #include <ncd/NCDModule.h>
+#include <ncd/value_utils.h>
 
 #include <generated/blog_channel_ncd_exit.h>
 
@@ -54,14 +54,14 @@ static void func_new (void *unused, NCDModuleInst *i, const struct NCDModuleInst
         ModuleLog(i, BLOG_ERROR, "wrong arity");
         goto fail0;
     }
-    if (!NCDVal_IsStringNoNulls(exit_code_arg)) {
+    if (!NCDVal_IsString(exit_code_arg)) {
         ModuleLog(i, BLOG_ERROR, "wrong type");
         goto fail0;
     }
     
     // parse exit code
     uintmax_t exit_code;
-    if (!parse_unsigned_integer(NCDVal_StringValue(exit_code_arg), &exit_code) || exit_code >= INT_MAX) {
+    if (!ncd_read_uintmax(exit_code_arg, &exit_code) || exit_code >= INT_MAX) {
         ModuleLog(i, BLOG_ERROR, "wrong exit code value");
         goto fail0;
     }

+ 2 - 2
ncd/modules/explode.c

@@ -49,11 +49,11 @@
 
 #include <misc/exparray.h>
 #include <misc/string_begins_with.h>
-#include <misc/parse_number.h>
 #include <misc/substring.h>
 #include <misc/balloc.h>
 #include <ncd/NCDModule.h>
 #include <ncd/static_strings.h>
+#include <ncd/value_utils.h>
 
 #include <generated/blog_channel_ncd_explode.h>
 
@@ -91,7 +91,7 @@ static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new
     size_t limit = SIZE_MAX;
     if (!NCDVal_IsInvalid(limit_arg)) {
         uintmax_t n;
-        if (!parse_unsigned_integer_bin(NCDVal_StringValue(limit_arg), NCDVal_StringLength(limit_arg), &n) || n == 0) {
+        if (!ncd_read_uintmax(limit_arg, &n) || n == 0) {
             ModuleLog(i, BLOG_ERROR, "bad limit argument");
             goto fail0;
         }

+ 3 - 3
ncd/modules/imperative.c

@@ -52,9 +52,9 @@
 #include <stdlib.h>
 #include <string.h>
 
-#include <misc/parse_number.h>
 #include <misc/string_begins_with.h>
 #include <ncd/NCDModule.h>
+#include <ncd/value_utils.h>
 
 #include <generated/blog_channel_ncd_imperative.h>
 
@@ -246,7 +246,7 @@ static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new
     }
     if (!NCDVal_IsStringNoNulls(init_template_arg) || !NCDVal_IsList(init_args)  ||
         !NCDVal_IsStringNoNulls(deinit_template_arg) || !NCDVal_IsList(o->deinit_args) ||
-        !NCDVal_IsStringNoNulls(deinit_timeout_arg)) {
+        !NCDVal_IsString(deinit_timeout_arg)) {
         ModuleLog(i, BLOG_ERROR, "wrong type");
         goto fail0;
     }
@@ -255,7 +255,7 @@ static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new
     
     // read timeout
     uintmax_t timeout;
-    if (!parse_unsigned_integer(NCDVal_StringValue(deinit_timeout_arg), &timeout) || timeout > UINT64_MAX) {
+    if (!ncd_read_uintmax(deinit_timeout_arg, &timeout) || timeout > UINT64_MAX){
         ModuleLog(i, BLOG_ERROR, "wrong timeout");
         goto fail0;
     }

+ 3 - 3
ncd/modules/index.c

@@ -48,8 +48,8 @@
 #include <stddef.h>
 #include <stdio.h>
 
-#include <misc/parse_number.h>
 #include <ncd/NCDModule.h>
+#include <ncd/value_utils.h>
 
 #include <generated/blog_channel_ncd_index.h>
 
@@ -80,14 +80,14 @@ static void func_new_from_value (void *vo, NCDModuleInst *i, const struct NCDMod
         ModuleLog(i, BLOG_ERROR, "wrong arity");
         goto fail0;
     }
-    if (!NCDVal_IsStringNoNulls(arg_value)) {
+    if (!NCDVal_IsString(arg_value)) {
         ModuleLog(i, BLOG_ERROR, "wrong type");
         goto fail0;
     }
     
     // parse value
     uintmax_t value;
-    if (!parse_unsigned_integer(NCDVal_StringValue(arg_value), &value)) {
+    if (!ncd_read_uintmax(arg_value, &value)) {
         ModuleLog(i, BLOG_ERROR, "wrong value");
         goto fail0;
     }

+ 7 - 7
ncd/modules/list.c

@@ -92,10 +92,10 @@
 #include <stdio.h>
 #include <inttypes.h>
 
-#include <misc/parse_number.h>
 #include <misc/offset.h>
 #include <structure/IndexedList.h>
 #include <ncd/NCDModule.h>
+#include <ncd/value_utils.h>
 
 #include <generated/blog_channel_ncd_list.h>
 
@@ -517,12 +517,12 @@ static void get_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst
         ModuleLog(o->i, BLOG_ERROR, "wrong arity");
         goto fail0;
     }
-    if (!NCDVal_IsStringNoNulls(index_arg)) {
+    if (!NCDVal_IsString(index_arg)) {
         ModuleLog(o->i, BLOG_ERROR, "wrong type");
         goto fail0;
     }
     uintmax_t index;
-    if (!parse_unsigned_integer(NCDVal_StringValue(index_arg), &index)) {
+    if (!ncd_read_uintmax(index_arg, &index)) {
         ModuleLog(o->i, BLOG_ERROR, "wrong value");
         goto fail0;
     }
@@ -677,14 +677,14 @@ static void find_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleIns
         ModuleLog(o->i, BLOG_ERROR, "wrong arity");
         goto fail0;
     }
-    if (!NCDVal_IsStringNoNulls(start_pos_arg)) {
+    if (!NCDVal_IsString(start_pos_arg)) {
         ModuleLog(o->i, BLOG_ERROR, "wrong type");
         goto fail0;
     }
     
     // read start position
     uintmax_t start_pos;
-    if (!parse_unsigned_integer(NCDVal_StringValue(start_pos_arg), &start_pos) || start_pos > UINT64_MAX) {
+    if (!ncd_read_uintmax(start_pos_arg, &start_pos) || start_pos > UINT64_MAX) {
         ModuleLog(o->i, BLOG_ERROR, "wrong start pos");
         goto fail0;
     }
@@ -752,14 +752,14 @@ static void removeat_func_new (void *unused, NCDModuleInst *i, const struct NCDM
         ModuleLog(i, BLOG_ERROR, "wrong arity");
         goto fail0;
     }
-    if (!NCDVal_IsStringNoNulls(remove_pos_arg)) {
+    if (!NCDVal_IsString(remove_pos_arg)) {
         ModuleLog(i, BLOG_ERROR, "wrong type");
         goto fail0;
     }
     
     // read position
     uintmax_t remove_pos;
-    if (!parse_unsigned_integer(NCDVal_StringValue(remove_pos_arg), &remove_pos)) {
+    if (!ncd_read_uintmax(remove_pos_arg, &remove_pos)) {
         ModuleLog(i, BLOG_ERROR, "wrong pos");
         goto fail0;
     }

+ 8 - 14
ncd/modules/parse.c

@@ -49,7 +49,6 @@
 
 #include <stdio.h>
 #include <stdlib.h>
-#include <string.h>
 #include <inttypes.h>
 
 #include <misc/parse_number.h>
@@ -81,12 +80,12 @@ static struct NCD_string_request strings[] = {
     {"succeeded"}, {"addr"}, {"prefix"}, {NULL}
 };
 
-typedef int (*parse_func) (NCDModuleInst *i, const char *str, NCDValMem *mem, NCDValRef *out);
+typedef int (*parse_func) (NCDModuleInst *i, const char *str, size_t str_len, NCDValMem *mem, NCDValRef *out);
 
-static int parse_number (NCDModuleInst *i, const char *str, NCDValMem *mem, NCDValRef *out)
+static int parse_number (NCDModuleInst *i, const char *str, size_t str_len, NCDValMem *mem, NCDValRef *out)
 {
     uintmax_t n;
-    if (!parse_unsigned_integer(str, &n)) {
+    if (!parse_unsigned_integer_bin(str, str_len, &n)) {
         ModuleLog(i, BLOG_ERROR, "failed to parse number");
         return 0;
     }
@@ -103,9 +102,9 @@ static int parse_number (NCDModuleInst *i, const char *str, NCDValMem *mem, NCDV
     return 1;
 }
 
-static int parse_value (NCDModuleInst *i, const char *str, NCDValMem *mem, NCDValRef *out)
+static int parse_value (NCDModuleInst *i, const char *str, size_t str_len, NCDValMem *mem, NCDValRef *out)
 {
-    if (!NCDValParser_Parse(str, strlen(str), mem, out)) {
+    if (!NCDValParser_Parse(str, str_len, mem, out)) {
         ModuleLog(i, BLOG_ERROR, "failed to parse value");
         return 0;
     }
@@ -113,10 +112,10 @@ static int parse_value (NCDModuleInst *i, const char *str, NCDValMem *mem, NCDVa
     return 1;
 }
 
-static int parse_ipv4_addr (NCDModuleInst *i, const char *str, NCDValMem *mem, NCDValRef *out)
+static int parse_ipv4_addr (NCDModuleInst *i, const char *str, size_t str_len, NCDValMem *mem, NCDValRef *out)
 {
     uint32_t addr;
-    if (!ipaddr_parse_ipv4_addr((char *)str, &addr)) {
+    if (!ipaddr_parse_ipv4_addr_bin(str, str_len, &addr)) {
         ModuleLog(i, BLOG_ERROR, "failed to parse ipv4 addresss");
         return 0;
     }
@@ -153,12 +152,7 @@ static void new_templ (void *vo, NCDModuleInst *i, const struct NCDModuleInst_ne
     NCDValMem_Init(&o->mem);
     
     // parse
-    if (NCDVal_StringHasNulls(str_arg)) {
-        ModuleLog(o->i, BLOG_ERROR, "string has nulls");
-        o->succeeded = 0;
-    } else {
-        o->succeeded = pfunc(i, NCDVal_StringValue(str_arg), &o->mem, &o->value);
-    }
+    o->succeeded = pfunc(i, NCDVal_StringValue(str_arg), NCDVal_StringLength(str_arg), &o->mem, &o->value);
     
     // signal up
     NCDModuleInst_Backend_Up(i);

+ 3 - 3
ncd/modules/sys_request_client.c

@@ -86,12 +86,12 @@
 #include <limits.h>
 
 #include <misc/offset.h>
-#include <misc/parse_number.h>
 #include <structure/LinkedList0.h>
 #include <structure/LinkedList1.h>
 #include <system/BAddr.h>
 #include <ncd/NCDModule.h>
 #include <ncd/NCDRequestClient.h>
+#include <ncd/value_utils.h>
 
 #include <generated/blog_channel_ncd_sys_request_client.h>
 
@@ -505,7 +505,7 @@ static int get_connect_addr (struct instance *o, NCDValRef connect_addr_arg, str
             goto bad;
         }
         
-        if (!NCDVal_IsStringNoNulls(ip_address_arg) || !NCDVal_IsStringNoNulls(port_number_arg)) {
+        if (!NCDVal_IsStringNoNulls(ip_address_arg) || !NCDVal_IsString(port_number_arg)) {
             goto bad;
         }
         
@@ -515,7 +515,7 @@ static int get_connect_addr (struct instance *o, NCDValRef connect_addr_arg, str
         }
         
         uintmax_t port;
-        if (!parse_unsigned_integer(NCDVal_StringValue(port_number_arg), &port) || port > UINT16_MAX) {
+        if (!ncd_read_uintmax(port_number_arg, &port) || port > UINT16_MAX) {
             goto bad;
         }
         

+ 3 - 3
ncd/modules/sys_request_server.c

@@ -78,7 +78,6 @@
 #include <misc/offset.h>
 #include <misc/debug.h>
 #include <misc/byteorder.h>
-#include <misc/parse_number.h>
 #include <protocol/packetproto.h>
 #include <protocol/requestproto.h>
 #include <structure/LinkedList0.h>
@@ -90,6 +89,7 @@
 #include <ncd/NCDValParser.h>
 #include <ncd/NCDValGenerator.h>
 #include <ncd/NCDModule.h>
+#include <ncd/value_utils.h>
 
 #include <generated/blog_channel_ncd_sys_request_server.h>
 
@@ -699,7 +699,7 @@ static int init_listen (struct instance *o, NCDValRef listen_addr_arg)
             goto bad;
         }
         
-        if (!NCDVal_IsStringNoNulls(ip_address_arg) || !NCDVal_IsStringNoNulls(port_number_arg)) {
+        if (!NCDVal_IsStringNoNulls(ip_address_arg) || !NCDVal_IsString(port_number_arg)) {
             goto bad;
         }
         
@@ -709,7 +709,7 @@ static int init_listen (struct instance *o, NCDValRef listen_addr_arg)
         }
         
         uintmax_t port;
-        if (!parse_unsigned_integer(NCDVal_StringValue(port_number_arg), &port) || port > UINT16_MAX) {
+        if (!ncd_read_uintmax(port_number_arg, &port) || port > UINT16_MAX) {
             goto bad;
         }
         

+ 7 - 7
ncd/modules/value.c

@@ -142,13 +142,13 @@
 
 #include <misc/offset.h>
 #include <misc/debug.h>
-#include <misc/parse_number.h>
 #include <misc/balloc.h>
 #include <structure/LinkedList0.h>
 #include <structure/IndexedList.h>
 #include <structure/SAvl.h>
 #include <ncd/NCDModule.h>
 #include <ncd/static_strings.h>
+#include <ncd/value_utils.h>
 
 #include <generated/blog_channel_ncd_value.h>
 
@@ -682,7 +682,7 @@ static struct value * value_get (NCDModuleInst *i, struct value *v, NCDValRef wh
         
         case NCDVAL_LIST: {
             uintmax_t index;
-            if (!NCDVal_IsStringNoNulls(where) || !parse_unsigned_integer(NCDVal_StringValue(where), &index)) {
+            if (!NCDVal_IsString(where) || !ncd_read_uintmax(where, &index)) {
                 if (!no_error) ModuleLog(i, BLOG_ERROR, "index is not a valid number (resolving into list)");
                 goto fail;
             }
@@ -752,7 +752,7 @@ static struct value * value_insert (NCDModuleInst *i, struct value *v, NCDValRef
         
         case NCDVAL_LIST: {
             uintmax_t index;
-            if (!NCDVal_IsStringNoNulls(where) || !parse_unsigned_integer(NCDVal_StringValue(where), &index)) {
+            if (!NCDVal_IsString(where) || !ncd_read_uintmax(where, &index)) {
                 ModuleLog(i, BLOG_ERROR, "index is not a valid number (inserting into list)");
                 goto fail1;
             }
@@ -833,7 +833,7 @@ static int value_remove (NCDModuleInst *i, struct value *v, NCDValRef where)
         
         case NCDVAL_LIST: {
             uintmax_t index;
-            if (!NCDVal_IsStringNoNulls(where) || !parse_unsigned_integer(NCDVal_StringValue(where), &index)) {
+            if (!NCDVal_IsString(where) || !ncd_read_uintmax(where, &index)) {
                 ModuleLog(i, BLOG_ERROR, "index is not a valid number (removing from list)");
                 goto fail;
             }
@@ -1397,19 +1397,19 @@ static void func_new_substr (void *vo, NCDModuleInst *i, const struct NCDModuleI
         ModuleLog(i, BLOG_ERROR, "wrong arity");
         goto fail0;
     }
-    if (!NCDVal_IsStringNoNulls(start_arg) || (!NCDVal_IsInvalid(length_arg) && !NCDVal_IsStringNoNulls(length_arg))) {
+    if (!NCDVal_IsString(start_arg) || (!NCDVal_IsInvalid(length_arg) && !NCDVal_IsString(length_arg))) {
         ModuleLog(i, BLOG_ERROR, "wrong type");
         goto fail0;
     }
     
     uintmax_t start;
-    if (!parse_unsigned_integer(NCDVal_StringValue(start_arg), &start)) {
+    if (!ncd_read_uintmax(start_arg, &start)) {
         ModuleLog(i, BLOG_ERROR, "start is not a number");
         goto fail0;
     }
     
     uintmax_t length = UINTMAX_MAX;
-    if (!NCDVal_IsInvalid(length_arg) && !parse_unsigned_integer(NCDVal_StringValue(length_arg), &length)) {
+    if (!NCDVal_IsInvalid(length_arg) && !ncd_read_uintmax(length_arg, &length)) {
         ModuleLog(i, BLOG_ERROR, "length is not a number");
         goto fail0;
     }