From f4eddd26e99e4964ed41d95f834ce1a87ae16ec7 Mon Sep 17 00:00:00 2001 From: Stephan Bosch Date: Wed, 7 Aug 2019 20:20:18 +0200 Subject: [PATCH] lib-json: Define syntax character classes --- src/lib-json/Makefile.am | 6 ++++-- src/lib-json/json-syntax.c | 37 ++++++++++++++++++++++++++++++++++ src/lib-json/json-syntax.h | 41 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 src/lib-json/json-syntax.c create mode 100644 src/lib-json/json-syntax.h diff --git a/src/lib-json/Makefile.am b/src/lib-json/Makefile.am index cf30aa1b2c..71589c6573 100644 --- a/src/lib-json/Makefile.am +++ b/src/lib-json/Makefile.am @@ -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 index 0000000000..215a0bb427 --- /dev/null +++ b/src/lib-json/json-syntax.c @@ -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 index 0000000000..16789d02fc --- /dev/null +++ b/src/lib-json/json-syntax.h @@ -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 -- 2.47.3