]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-json: Define syntax character classes
authorStephan Bosch <stephan.bosch@open-xchange.com>
Wed, 7 Aug 2019 18:20:18 +0000 (20:20 +0200)
committeraki.tuomi <aki.tuomi@open-xchange.com>
Sat, 18 Nov 2023 18:58:04 +0000 (18:58 +0000)
src/lib-json/Makefile.am
src/lib-json/json-syntax.c [new file with mode: 0644]
src/lib-json/json-syntax.h [new file with mode: 0644]

index cf30aa1b2c546ce1e28063d2820dd777e4a17a97..71589c657331277c9e4f4f9e2d885eab5cfc3a80 100644 (file)
@@ -4,10 +4,12 @@ AM_CPPFLAGS = \
        -I$(top_srcdir)/src/lib \
        -I$(top_srcdir)/src/lib-test
 
-libjson_la_SOURCES =
+libjson_la_SOURCES = \
+       json-syntax.c
 libjson_la_LIBADD = -lm
 
-headers =
+headers = \
+       json-syntax.h
 
 test_programs =
 
diff --git a/src/lib-json/json-syntax.c b/src/lib-json/json-syntax.c
new file mode 100644 (file)
index 0000000..215a0bb
--- /dev/null
@@ -0,0 +1,37 @@
+/* Copyright (c) 2017-2023 Dovecot authors, see the included COPYING file */
+
+#include "lib.h"
+#include "json-syntax.h"
+
+/* Character bit mappings:
+
+   (1<<0) =>  %x20-21 / %x23-5B / %x5D-10FFFF ; 'uchar'
+   (1<<1) =>  %x00-08 / %x0b-0c / %x0e-1F     ; 'control'
+   (1<<2) =>  %x09 / %x0A / %x0D / %x20       ; 'ws'
+   (1<<3) =>  "0"-"9"                         ; 'digit'
+ */
+
+const unsigned char json_uchar_char_mask   = (1<<0);
+const unsigned char json_control_char_mask = (1<<1);
+const unsigned char json_ws_char_mask      = (1<<2);
+const unsigned char json_digit_char_mask   = (1<<3);
+
+const unsigned char json_char_lookup[128] = {
+       0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, // 0x00
+       0x02, 0x04, 0x04, 0x02, 0x02, 0x04, 0x02, 0x02, // 0x08
+       0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, // 0x10
+       0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, // 0x18
+       0x05, 0x01, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, // 0x20
+       0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, // 0x28
+       0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, // 0x30
+       0x09, 0x09, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, // 0x38
+       0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, // 0x40
+       0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, // 0x48
+       0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, // 0x50
+       0x01, 0x01, 0x01, 0x01, 0x00, 0x01, 0x01, 0x01, // 0x58
+       0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, // 0x60
+       0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, // 0x68
+       0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, // 0x70
+       0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, // 0x78
+};
+
diff --git a/src/lib-json/json-syntax.h b/src/lib-json/json-syntax.h
new file mode 100644 (file)
index 0000000..16789d0
--- /dev/null
@@ -0,0 +1,41 @@
+#ifndef JSON_SYNTAX_H
+#define JSON_SYNTAX_H
+
+#include "unichar.h"
+
+extern const unsigned char json_uchar_char_mask;
+extern const unsigned char json_control_char_mask;
+extern const unsigned char json_ws_char_mask;
+extern const unsigned char json_digit_char_mask;
+
+extern const unsigned char json_char_lookup[128];
+
+static inline bool json_unichar_is_uchar(unichar_t ch)
+{
+       if (ch > 0x7F)
+               return (ch <= 0x10FFFF);
+       return ((json_char_lookup[ch] & json_uchar_char_mask) != 0);
+}
+
+static inline bool json_unichar_is_control(unichar_t ch)
+{
+       if (ch > 0x7F)
+               return FALSE;
+       return ((json_char_lookup[ch] & json_control_char_mask) != 0);
+}
+
+static inline bool json_unichar_is_ws(unichar_t ch)
+{
+       if (ch > 0x7F)
+               return FALSE;
+       return ((json_char_lookup[ch] & json_ws_char_mask) != 0);
+}
+
+static inline bool json_unichar_is_digit(unichar_t ch)
+{
+       if (ch > 0x7F)
+               return FALSE;
+       return ((json_char_lookup[ch] & json_digit_char_mask) != 0);
+}
+
+#endif