[vlc-devel] [vlc-commits] JSON representation and Javascript unescaping

Thomas Guillem thomas at gllm.fr
Mon Sep 28 10:20:06 CEST 2020



On Sun, Sep 27, 2020, at 15:16, Rémi Denis-Courmont wrote:
> vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Sun Sep 
> 27 15:19:42 2020 +0300| [c4c1bc81e819cee65f673a10f58e40f56eab9704] | 
> committer: Rémi Denis-Courmont
> 
> JSON representation and Javascript unescaping
> 
> > http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=c4c1bc81e819cee65f673a10f58e40f56eab9704
> ---
> 
>  modules/demux/Makefile.am |   7 +++
>  modules/demux/json/json.c | 145 ++++++++++++++++++++++++++++++++++++++++++++++
>  modules/demux/json/json.h |  73 +++++++++++++++++++++++
>  3 files changed, 225 insertions(+)
> 
> diff --git a/modules/demux/Makefile.am b/modules/demux/Makefile.am
> index 675b2d10af..f46fcdb92e 100644
> --- a/modules/demux/Makefile.am
> +++ b/modules/demux/Makefile.am
> @@ -512,3 +512,10 @@ libdemux_mock_plugin_la_SOURCES = demux/mock.c
>  libdemux_mock_plugin_la_LDFLAGS = $(AM_LDFLAGS) -rpath '$(demuxdir)'
>  libdemux_mock_plugin_la_LIBADD = $(LIBM)
>  noinst_LTLIBRARIES += libdemux_mock_plugin.la
> +
> +libvlc_json_la_SOURCES = \
> +	demux/json/json.c demux/json/json.h
> +libvlc_json_la_CPPFLAGS = $(AM_CPPFLAGS) -I$(srcdir)/demux/json
> +libvlc_json_la_LIBADD = $(LTLIBVLCCORE) ../compat/libcompat.la
> +libvlc_json_la_LDFLAGS = -static
> +noinst_LTLIBRARIES += libvlc_json.la
> diff --git a/modules/demux/json/json.c b/modules/demux/json/json.c
> new file mode 100644
> index 0000000000..d0ab86120a
> --- /dev/null
> +++ b/modules/demux/json/json.c
> @@ -0,0 +1,145 @@
> +/*****************************************************************************
> + * json/json.c: JSON parsing library
> + *****************************************************************************
> + * Copyright © 2020 Rémi Denis-Courmont
> + *
> + * This program is free software; you can redistribute it and/or modify it
> + * under the terms of the GNU Lesser General Public License as published by
> + * the Free Software Foundation; either version 2.1 of the License, or
> + * (at your option) any later version.
> + *
> + * This program is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
> + * GNU Lesser General Public License for more details.
> + *
> + * You should have received a copy of the GNU Lesser General Public License
> + * along with this program; if not, write to the Free Software Foundation,
> + * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
> + *****************************************************************************/
> +
> +#ifdef HAVE_CONFIG_H
> +# include <config.h>
> +#endif
> +
> +#include <assert.h>
> +#include <math.h>
> +#include <stdint.h>
> +#include <stdlib.h>
> +#include <string.h>
> +#include <vlc_common.h>
> +#include <vlc_charset.h>
> +#include "json.h"
> +
> +#ifdef WORDS_BIGENDIAN
> +# define ENDIAN(x) x "BE"
> +#else
> +# define ENDIAN(x) x "LE"
> +#endif
> +
> +char *json_unescape(const char *in, size_t inlen)
> +{
> +    /* 1) Convert UTF-8 to UTF-16.
> +     * This will catch any invalid UTF-8 byte sequence.
> +     */
> +    size_t buflen = 2 * (inlen + 1);
> +    void *buf = malloc(buflen);
> +
> +    if (unlikely(buf == NULL))
> +        return NULL;
> +
> +    vlc_iconv_t hd = vlc_iconv_open(ENDIAN("UTF-16") , "UTF-8");
> +
> +    if (unlikely(hd == (vlc_iconv_t)-1)) {
> +        free(buf);
> +        return NULL;
> +    }
> +
> +    char *out = buf;
> +    size_t outlen = buflen;
> +    size_t val = vlc_iconv(hd, &in, &inlen, &out, &outlen);
> +
> +    vlc_iconv_close(hd);
> +
> +    if (val == (size_t)-1) {
> +        free(buf);
> +        return NULL;
> +    }
> +
> +    /* 2) Unescape in UTF-16 (in place).
> +     */
> +    const uint16_t *in2 = buf, *end2 = in2 + ((buflen - outlen) / 2);
> +    uint16_t *out2 = buf;
> +
> +    while (in2 < end2) {
> +        uint16_t c = *(in2++);
> +
> +        if (c == '\\') {
> +            switch (*(in2++)) {
> +                case 'b':
> +                    c = '\b';
> +                    break;
> +                case 'f':
> +                    c = '\f';
> +                    break;
> +                case 'n':
> +                    c = '\n';
> +                    break;
> +                case 'r':
> +                    c = '\r';
> +                    break;
> +                case 't':
> +                    c = '\t';
> +                    break;
> +                case 'u': {
> +                    char hex[5] = { in2[0], in2[1], in2[2], in2[3], 0 };
> +
> +                    /* Tokeniser requires 4 hex-digits, so this cannot fail. */
> +                    if (sscanf(hex, "%4"SCNx16, &c) != 1)
> +                        vlc_assert_unreachable();
> +
> +                    in2 += 4;
> +                    break;
> +                }
> +                default:
> +                    /* Invalid escape is not allowed by tokeniser. */
> +                    vlc_assert_unreachable();
> +            }
> +	}
> +
> +        assert(out2 < in2); /* Safely in place */
> +        *(out2++) = c;
> +    }
> +
> +    /* 3) Convert back to UTF-8.
> +     * This will catch any invalid sequence of escaped UTF-16 surrogates.
> +     */
> +    char *ret = FromCharset(ENDIAN("UTF-16"), buf, (char *)out2 - (char *)buf);
> +
> +    free(buf);
> +    return ret;
> +}
> +
> +const struct json_value *json_get(const struct json_object *obj,
> +                                  const char *name)
> +{
> +    for (size_t i = 0; i < obj->count; i++)
> +        if (!strcmp(obj->members[i].name, name))
> +            return &obj->members[i].value;
> +
> +    return NULL;
> +}
> +
> +const char *json_get_str(const struct json_object *obj, const char *name)
> +{
> +    const struct json_value *v = json_get(obj, name);
> +
> +    return (v != NULL && v->type == JSON_STRING) ? v->string : NULL;
> +}
> +
> +double json_get_num(const struct json_object *obj, const char *name)
> +{
> +    const struct json_value *v = json_get(obj, name);
> +
> +    return (v != NULL && v->type == JSON_NUMBER) ? v->number : NAN;
> +}
> diff --git a/modules/demux/json/json.h b/modules/demux/json/json.h
> new file mode 100644
> index 0000000000..e468ec871f
> --- /dev/null
> +++ b/modules/demux/json/json.h
> @@ -0,0 +1,73 @@
> +/*****************************************************************************
> + * json/json.h:
> + *****************************************************************************
> + * Copyright © 2020 Rémi Denis-Courmont
> + *
> + * This program is free software; you can redistribute it and/or modify it
> + * under the terms of the GNU Lesser General Public License as published by
> + * the Free Software Foundation; either version 2.1 of the License, or
> + * (at your option) any later version.
> + *
> + * This program is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
> + * GNU Lesser General Public License for more details.
> + *
> + * You should have received a copy of the GNU Lesser General Public License
> + * along with this program; if not, write to the Free Software Foundation,
> + * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
> + *****************************************************************************/
> +
> +#ifndef JSON_H
> +#define JSON_H
> +
> +#include <stdbool.h>
> +#include <stdio.h>
> +
> +enum json_type {
> +    JSON_NULL,
> +    JSON_BOOLEAN,
> +    JSON_NUMBER,
> +    JSON_STRING,
> +    JSON_ARRAY,
> +    JSON_OBJECT,
> +};
> +
> +struct json_array {
> +    size_t size;
> +    struct json_value *entries;
> +};
> +
> +struct json_object {
> +    size_t count;
> +    struct json_member *members;
> +};
> +
> +struct json_value {
> +    enum json_type type;
> +    union {
> +        bool boolean;
> +        double number;
> +        char *string;
> +        struct json_array array;
> +        struct json_object object;
> +    };
> +};
> +
> +struct json_member {
> +    char *name;
> +    struct json_value value;
> +};
> +
> +void json_parse_error(void *log, const char *msg);
> +char *json_unescape(const char *, size_t);
> +
> +int json_parse(void *log, FILE *in, struct json_object *result);
> +void json_free(struct json_object *);
> +
> +const struct json_value *json_get(const struct json_object *obj,
> +                                  const char *name);
> +const char *json_get_str(const struct json_object *obj, const char *name);
> +double json_get_num(const struct json_object *obj, const char *name);

Please prefix with *vlc_* for new APIs (even module-only ones).

Also: Why not passing by the mailing list?

> +
> +#endif
> 
> _______________________________________________
> vlc-commits mailing list
> vlc-commits at videolan.org
> https://mailman.videolan.org/listinfo/vlc-commits
>


More information about the vlc-devel mailing list