diff --git a/Makefile b/Makefile index 64a04828..611678e8 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ OUT=webdis HIREDIS_OBJ?=src/hiredis/hiredis.o src/hiredis/sds.o src/hiredis/net.o src/hiredis/async.o src/hiredis/read.o src/hiredis/dict.o src/hiredis/alloc.o src/hiredis/sockcompat.o -JANSSON_OBJ?=src/jansson/src/dump.o src/jansson/src/error.o src/jansson/src/hashtable.o src/jansson/src/load.o src/jansson/src/strbuffer.o src/jansson/src/utf.o src/jansson/src/value.o src/jansson/src/variadic.o +JANSSON_OBJ?=src/jansson/src/dump.o src/jansson/src/error.o src/jansson/src/hashtable.o src/jansson/src/hashtable_seed.o src/jansson/src/load.o src/jansson/src/memory.o src/jansson/src/pack_unpack.o src/jansson/src/strbuffer.o src/jansson/src/strconv.o src/jansson/src/utf.o src/jansson/src/value.o src/jansson/src/version.o B64_OBJS?=src/b64/cencode.o FORMAT_OBJS?=src/formats/json.o src/formats/raw.o src/formats/common.o src/formats/custom-type.o HTTP_PARSER_OBJS?=src/http-parser/http_parser.o diff --git a/src/jansson/CHANGES b/src/jansson/CHANGES index 0cadf5c3..cb6ff07c 100644 --- a/src/jansson/CHANGES +++ b/src/jansson/CHANGES @@ -1,3 +1,783 @@ +Version 2.14 +============ + +Released 2021-09-09 + +* New Features: + + - Add `json_object_getn`, `json_object_setn`, `json_object_deln`, and the + corresponding `nocheck` functions. (#520, by Maxim Zhukov) + +* Fixes: + + - Handle `sprintf` corner cases (#537, by Tobias Stoeckmann) + +* Build: + + - Symbol versioning for all exported symbols (#540, by Simon McVittie) + - Fix compiler warnings (#555, by Kelvin Lee) + +* Documentation: + + - Small fixes (#544, #546, by @i-ky) + - Sphinx 3 compatibility (#543, by Pierce Lopez) + + +Version 2.13.1 +============== + +Released 2020-05-07 + +* Build: + + - Include `jansson_version_str()` and `jansson_version_cmp()` in + shared library. (#534) + + - Include ``scripts/`` in tarball. (#535) + + +Version 2.13 +============ + +Released 2020-05-05 + +* New Features: + + - Add `jansson_version_str()` and `jansson_version_cmp()` for runtime + version checking (#465). + + - Add `json_object_update_new()`, `json_object_update_existing_new()` + and `json_object_update_missing_new()` functions (#499). + + - Add `json_object_update_recursive()` (#505). + +* Build: + + - Add ``-Wno-format-truncation`` to suppress format truncation warnings (#489). + +* Bug fixes: + + - Remove ``strtod`` macro definition for MinGW (#498). + + - Add infinite loop check in `json_deep_copy()` (#490). + + - Add ``pipe`` macro definition for MinGW (#500). + + - Enhance ``JANSSON_ATTRS`` macro to support earlier C standard(C89) (#501). + + - Update version detection for sphinx-build (#502). + +* Documentation: + + - Fix typos (#483, #494). + + - Document that call the custom free function to free the return value + of `json_dumps()` if you have a custom malloc/free (#490). + + - Add vcpkg installation instructions (#496). + + - Document that non-blocking file descriptor is not supported on + `json_loadfd()` (#503). + + +Version 2.12 +============ + +Released 2018-11-26 + +* Bug fixes: + + - Fix error message in `json_pack()` for NULL object (#409). + + - Avoid invalid memory read in `json_pack()` (#421). + + - Call va_end after va_copy in `json_vsprintf()` (#427). + + - Improve handling of formats with '?' and '*' in `json_pack()` (#438). + + - Remove inappropriate `jsonp_free()` which caused segmentation fault in + error handling (#444). + +* Build: + + - Add function attributes for GCC and CLANG to provide warnings on improper + use of jansson routines (#404). + + - Many CMake fixes (#408, #412, #415). + + - Enable -Bsymbolic-functions linker flag whenever possible. + + - Resolve various compiler warnings (#423, #430, #435, #436). + + - Fix code coverage ignored paths (#439). + +* Other: + + - Test coverage improvements (#398, #400). + + - Add VS 2017 to appveyor, update Visual Studio documentation (#417). + + - Update copyright for 2018 (#424). + + - Update install instructions in README (#401). + +Version 2.11 +============ + +Released 2018-02-09 + +* New features: + + - Add `json_pack()` format specifiers s*, o* and O* for values that + can be omitted if null (#339). + + - Add `json_error_code()` to retrieve numeric error codes (#365, #380, + #381). + + - Enable thread safety for `json_dump()` on all systems. Enable thread + safe `json_decref()` and `json_incref()` for modern compilers (#389). + + - Add `json_sprintf()` and `json_vsprintf()` (#393). + +* Bug Fixes: + + - Fix incorrect report of success from `json_dump_file()` when an error + is returned by `fclose()` (#359). + + - Make json_equal() const-correct (#344). + + - Fix incomplete stealing of references by `json_pack()` (#374). + +* Build: + + - Work around gcc's -Wimplicit-fallthrough. + + - Fix CMake detection of ``sys/types.h`` header (#375). + + - Fix `jansson.pc` generated by CMake to be more consistent with the one + generated using GNU Autotools (#368). + +* Other: + + - Miscellaneous documentation fixes (#356, #378, #395). + + - Remove unnecessary reference actions from parsers (#377). + +Version 2.10 +============ + +Released 2017-03-02 + +* New features: + + - Add JSON_EMBED encoding flag allowing arrays and objects to be encoded + into existing streams (#329). + + - Add `json_dumpb()` function for dumping to a pre-allocated buffer (#328). + + - Add `json_dumpfd()` and `json_loadfd()` functions for dumping to streaming + file descriptors (#328). + + - Add support for parsing buffers larger than 2GB (#309). + +* Build: + + - Fix CMake build when LONG_LONG_INT is defined as "" (#321) + +* Other: + + - Internal code cleanup (#311, #314) + +Version 2.9 +=========== + +Released 2016-09-18 + +* New features: + + - Add ``json_auto_t`` to automatically decref a value that goes out + of scope. Available only on GCC and Clang. (#301) + +* Build: + + - Fix CMake build (at least on Linux) by removing conflicting + jansson_config.h from the distribution (#306) + + - Change CMake install target generation to be optional (#305) + +* Documentation: + + - Small documentation fixes. + + +Version 2.8 +=========== + +Released 2016-08-30 + +* New features: + + - Always preserve insertion order of object items. + `json_object_iter()` and friends, `json_object_foreach()` and + `json_dumps()` and friends now always work in the insertion order of + object items (#293). + + - Add `json_object_foreach_safe()` macro that allows + `json_object_del()` calls during iteration (#230). + + - Add `json_get_alloc_funcs()` to allow reading the allocation + functions set by `json_set_alloc_funcs()` (#262, #264). + + - Add `json_pack()` format specifiers s?, o? and O? for values that + can be null (#261, #270). + +* Bug fixes: + + - Fix a crash when parsing inputs consisting of very deeply nested + arrays or objects (#282, #284). + + - Never convert numbers to integers in the parser when + JSON_DECODE_INT_AS_REAL is set. This fixes error messages for + overflowing numbers when JSON_DECODE_INT_AS_REAL is set (#212). + + - Fix a use-after-free in `json_pack()` error handling. + + - Fix subnormal number parsing on mingw32. + + - Handle out-of-memory situations gracefully in the hashtable + implementation (#298). + +* Build: + + - Fix build with CMake on all versions of Visual Studio up to 2015 + (#262, #289). + + - Fix pkgconfig libdir when using CMake (#268). + + - Fix CMake config for static CRT builds on Windows (#206). + + - Fix warnings on LLVM 6.0 targeting iOS arm64 (#208). + + - Add coverlls.io support via Travis for a nice test coverage badge + (#211). + + - Don't expect ``jansson_config.h`` to be in the compiler's include + path (#209). + + - Add a build-time option to set initial hashtable size (#213). + + - Use snprintf and strncpy in place of sprintf and strcpy to silence + linker warnings on OpenBSD (#233). + +* Documentation: + + - Fix various typos in documentation, and a broken link (#258). + + - Add an example program in ``examples/`` (#214, #217). + + - Fix building of documentation man pages (#207). + + - Document the fact that copying objects doesn't preserve the + insertion order of keys (#237). + +* Tests: + + - Don't use the nonstandard __FUNCTION__ macro in tests. + + - Use expr instead of $((...)) in shell scripts for Solaris 10 + compatibility. + + - Disable Visual Studio warning C4756 when triggered deliberately in + tests (#216). + + - Other minor fixes (#221, #248). + +* Other changes: + + - List all unrecognized object keys when strict unpacking fails + (#263). + + - Alter the order of the members of the hashtable_pair struct for + easier debugging. + + - Minor performance improvement to `json_dump()` and friends (#234). + + - Minor style fixes (#255, #257). + + +Version 2.7 +=========== + +Released 2014-10-02 + +* New features: + + - `json_pack()` and friends: Add format specifiers ``s%`` and ``+%`` + for a size_t string length (#141). + + - `json_unpack()` and friends: Add format specifier ``s%`` for + unpacking the string length along with the string itself (#141). + + - Add length-aware string constructors `json_stringn()` and + `json_stringn_nocheck()`, length-aware string mutators + `json_string_setn()` and `json_string_setn_nocheck()`, and a + function for getting string's length `json_string_length()` (#141, + #143). + + - Support ``\u0000`` escapes in the decoder. The support can be + enabled by using the ``JSON_ALLOW_NUL`` decoding flag (#141). + + - Add `json_boolean_value()` as an alias for `json_is_true()` + (#146). + + - Add JSON_REAL_PRECISION encoding flag/macro for controlling real + number precision (#178). + + - Define the maximum indentation as JSON_MAX_INDENT (#191). + +* Bug fixes: + + - Some malformed ``\uNNNN`` escapes could crash the decoder with an + assertion failure. + + - Avoid integer overflows with very long strings in UTF-8 decoder and + hashtable. + + - Check for *NULL* key in `json_object_get()` and + `json_object_del()` (#151). + + - Enhance hashtable seeding on Windows (#162). + + - `json_unpack()`: Allow mixing JSON_STRICT with optional keys + (#162, #163). + + - Fix int/int32 mismatch (#142). + + - Parse subnormal numbers correctly (#202). + +* Build: + + - Remove VS2010 build files. CMake should be used on Windows instead + (#165). + + - Fix CMake build flags for MinGW (#193). + + - Add CMake config files for find_package. Rename config.h to + jansson_private_config.h (#157, #159). + + - Make Valgrind checks work with CMake (#160). + + - Fix feature checks to use correct __ATOMIC flags. + + - Fix CMake checks for uint16_t and uint8_t support (#177). + + - Make Jansson build on SmartOS/Solaris (#171). + + - Work around a GCC bug on Solaris (#175). + + - Fix autoreconf on Debian (#182). + + - Don't use GNU make specific export for global AM_CFLAGS (#203, + #204). + + - Fix building on Android using the supplied Android.mk (#166, + #174). + + - Android.mk: Add -DHAVE_STDINT_H to LOCAL_CFLAGS (#200). + +* Documentation: + + - Document JANSSON_BUILD_SHARED_LIBS CMake option (#187). + +* Tests: + + - Close file handles correctly (#198). + +* Other changes: + + - ``\uNNNN`` escapes are now encoded in upper case for better + readability. + + - Enable usage of AddressSanitizer (#180). + + +Version 2.6 +=========== + +Released 2014-02-11 + +* Security: + + - CVE-2013-6401: The hash function used by the hashtable + implementation has been changed, and is automatically seeded with + random data when the first JSON object is created. This prevents + an attacker from causing large JSON objects with specially crafted + keys perform poorly. + +* New features: + + - `json_object_seed()`: Set the seed value of the hash function. + +* Bug fixes: + + - Include CMake specific files in the release tarball. + +* Documentation: + + - Fix tutorial source to send a User-Agent header, which is now + required by the GitHub API. + + - Set all memory to zero in secure_free() example. + + +Version 2.5 +=========== + +Released 2013-09-19 + +* New features: + + - `json_pack()` and friends: Add format specifiers ``s#``, ``+`` and + ``+#``. + + - Add ``JSON_DECODE_INT_AS_REAL`` decoding flag to treat all numbers + as real in the decoder (#123). + + - Add `json_array_foreach()`, paralleling `json_object_foreach()` + (#118). + +* Bug fixes: + + - `json_dumps()` and friends: Don't crash if json is *NULL* and + ``JSON_ENCODE_ANY`` is set. + + - Fix a theoretical integer overflow in `jsonp_strdup()`. + + - Fix `l_isxdigit()` macro (#97). + + - Fix an off-by-one error in `json_array_remove()`. + +* Build: + + - Support CMake in addition to GNU Autotools (#106, #107, #112, + #115, #120, #127). + + - Support building for Android (#109). + + - Don't use ``-Werror`` by default. + + - Support building and testing with VPATH (#93). + + - Fix compilation when ``NDEBUG`` is defined (#128) + +* Tests: + + - Fix a refleak in ``test/bin/json_process.c``. + +* Documentation: + + - Clarify the return value of `json_load_callback_t`. + + - Document how to circumvent problems with separate heaps on Windows. + + - Fix memory leaks and warnings in ``github_commits.c``. + + - Use `json_decref()` properly in tutorial. + +* Other: + + - Make it possible to forward declare ``struct json_t``. + + +Version 2.4 +=========== + +Released 2012-09-23 + +* New features: + + - Add `json_boolean()` macro that returns the JSON true or false + value based on its argument (#86). + + - Add `json_load_callback()` that calls a callback function + repeatedly to read the JSON input (#57). + + - Add JSON_ESCAPE_SLASH encoding flag to escape all occurences of + ``/`` with ``\/``. + +* Bug fixes: + + - Check for and reject NaN and Inf values for reals. Encoding these + values resulted in invalid JSON. + + - Fix `json_real_set()` to return -1 on error. + +* Build: + + - Jansson now builds on Windows with Visual Studio 2010, and + includes solution and project files in ``win32/vs2010/`` + directory. + + - Fix build warnings (#77, #78). + + - Add ``-no-undefined`` to LDFLAGS (#90). + +* Tests: + + - Fix the symbol exports test on Linux/PPC64 (#88). + +* Documentation: + + - Fix typos (#73, #84). + + +Version 2.3.1 +============= + +Released 2012-04-20 + +* Build issues: + + - Only use ``long long`` if ``strtoll()`` is also available. + +* Documentation: + + - Fix the names of library version constants in documentation. (#52) + + - Change the tutorial to use GitHub API v3. (#65) + +* Tests: + + - Make some tests locale independent. (#51) + + - Distribute the library exports test in the tarball. + + - Make test run on shells that don't support the ``export FOO=bar`` + syntax. + + +Version 2.3 +=========== + +Released 2012-01-27 + +* New features: + + - `json_unpack()` and friends: Add support for optional object keys + with the ``{s?o}`` syntax. + + - Add `json_object_update_existing()` and + `json_object_update_missing()`, for updating only existing keys or + only adding missing keys to an object. (#37) + + - Add `json_object_foreach()` for more convenient iteration over + objects. (#45, #46) + + - When decoding JSON, write the number of bytes that were read from + input to ``error.position`` also on success. This is handy with + ``JSON_DISABLE_EOF_CHECK``. + + - Add support for decoding any JSON value, not just arrays or + objects. The support is enabled with the new ``JSON_DECODE_ANY`` + flag. Patch by Andrea Marchesini. (#4) + +* Bug fixes + + - Avoid problems with object's serial number growing too big. (#40, + #41) + + - Decoding functions now return NULL if the first argument is NULL. + Patch by Andrea Marchesini. + + - Include ``jansson_config.h.win32`` in the distribution tarball. + + - Remove ``+`` and leading zeros from exponents in the encoder. + (#39) + + - Make Jansson build and work on MinGW. (#39, #38) + +* Documentation + + - Note that the same JSON values must not be encoded in parallel by + separate threads. (#42) + + - Document MinGW support. + + +Version 2.2.1 +============= + +Released 2011-10-06 + +* Bug fixes: + + - Fix real number encoding and decoding under non-C locales. (#32) + + - Fix identifier decoding under non-UTF-8 locales. (#35) + + - `json_load_file()`: Open the input file in binary mode for maximum + compatiblity. + +* Documentation: + + - Clarify the lifecycle of the result of the ``s`` fromat of + `json_unpack()`. (#31) + + - Add some portability info. (#36) + + - Little clarifications here and there. + +* Other: + + - Some style fixes, issues detected by static analyzers. + + +Version 2.2 +=========== + +Released 2011-09-03 + +* New features: + + - `json_dump_callback()`: Pass the encoder output to a callback + function in chunks. + +* Bug fixes: + + - `json_string_set()`: Check that target is a string and value is + not NULL. + +* Other: + + - Documentation typo fixes and clarifications. + + +Version 2.1 +=========== + +Released 2011-06-10 + +* New features: + + - `json_loadb()`: Decode a string with a given size, useful if the + string is not null terminated. + + - Add ``JSON_ENCODE_ANY`` encoding flag to allow encoding any JSON + value. By default, only arrays and objects can be encoded. (#19) + + - Add ``JSON_REJECT_DUPLICATES`` decoding flag to issue a decoding + error if any JSON object in the input contins duplicate keys. (#3) + + - Add ``JSON_DISABLE_EOF_CHECK`` decoding flag to stop decoding after a + valid JSON input. This allows other data after the JSON data. + +* Bug fixes: + + - Fix an additional memory leak when memory allocation fails in + `json_object_set()` and friends. + + - Clear errno before calling `strtod()` for better portability. (#27) + +* Building: + + - Avoid set-but-not-used warning/error in a test. (#20) + +* Other: + + - Minor clarifications to documentation. + + +Version 2.0.1 +============= + +Released 2011-03-31 + +* Bug fixes: + + - Replace a few `malloc()` and `free()` calls with their + counterparts that support custom memory management. + + - Fix object key hashing in json_unpack() strict checking mode. + + - Fix the parentheses in ``JANSSON_VERSION_HEX`` macro. + + - Fix `json_object_size()` return value. + + - Fix a few compilation issues. + +* Portability: + + - Enhance portability of `va_copy()`. + + - Test framework portability enhancements. + +* Documentation: + + - Distribute ``doc/upgrading.rst`` with the source tarball. + + - Build documentation in strict mode in ``make distcheck``. + + +Version 2.0 +=========== + +Released 2011-02-28 + +This release is backwards incompatible with the 1.x release series. +See the chapter "Upgrading from older versions" in documentation for +details. + +* Backwards incompatible changes: + + - Unify unsigned integer usage in the API: All occurences of + unsigned int and unsigned long have been replaced with size_t. + + - Change JSON integer's underlying type to the widest signed integer + type available, i.e. long long if it's supported, otherwise long. + Add a typedef json_int_t that defines the type. + + - Change the maximum indentation depth to 31 spaces in encoder. This + frees up bits from the flags parameter of encoding functions + `json_dumpf()`, `json_dumps()` and `json_dump_file()`. + + - For future needs, add a flags parameter to all decoding functions + `json_loadf()`, `json_loads()` and `json_load_file()`. + +* New features + + - `json_pack()`, `json_pack_ex()`, `json_vpack_ex()`: Create JSON + values based on a format string. + + - `json_unpack()`, `json_unpack_ex()`, `json_vunpack_ex()`: Simple + value extraction and validation functionality based on a format + string. + + - Add column, position and source fields to the ``json_error_t`` + struct. + + - Enhance error reporting in the decoder. + + - ``JANSSON_VERSION`` et al.: Preprocessor constants that define the + library version. + + - `json_set_alloc_funcs()`: Set custom memory allocation functions. + +* Fix many portability issues, especially on Windows. + +* Configuration + + - Add file ``jansson_config.h`` that contains site specific + configuration. It's created automatically by the configure script, + or can be created by hand if the configure script cannot be used. + The file ``jansson_config.h.win32`` can be used without + modifications on Windows systems. + + - Add a section to documentation describing how to build Jansson on + Windows. + + - Documentation now requires Sphinx 1.0 or newer. + + Version 1.3 =========== diff --git a/src/jansson/LICENSE b/src/jansson/LICENSE index 552b3498..483459ca 100644 --- a/src/jansson/LICENSE +++ b/src/jansson/LICENSE @@ -1,4 +1,4 @@ -Copyright (c) 2009, 2010 Petri Lehtinen +Copyright (c) 2009-2020 Petri Lehtinen Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/src/jansson/Makefile.am b/src/jansson/Makefile.am index 173c7de4..a6f9192d 100644 --- a/src/jansson/Makefile.am +++ b/src/jansson/Makefile.am @@ -1,8 +1,12 @@ -EXTRA_DIST = CHANGES LICENSE README.rst +EXTRA_DIST = CHANGES LICENSE README.rst CMakeLists.txt cmake android examples scripts SUBDIRS = doc src test -check-doc: +# "make distcheck" builds the dvi target, so use it to check that the +# documentation is built correctly. +dvi: $(MAKE) SPHINXOPTS_EXTRA=-W html pkgconfigdir = $(libdir)/pkgconfig pkgconfig_DATA = jansson.pc + +TESTS = scripts/clang-format-check diff --git a/src/jansson/README.rst b/src/jansson/README.rst index 27706dd5..83fc89a8 100644 --- a/src/jansson/README.rst +++ b/src/jansson/README.rst @@ -1,12 +1,21 @@ Jansson README ============== +.. image:: https://github.com/akheron/jansson/workflows/tests/badge.svg + :target: https://github.com/akheron/jansson/actions + +.. image:: https://ci.appveyor.com/api/projects/status/lmhkkc4q8cwc65ko + :target: https://ci.appveyor.com/project/akheron/jansson + +.. image:: https://coveralls.io/repos/akheron/jansson/badge.png?branch=master + :target: https://coveralls.io/r/akheron/jansson?branch=master + Jansson_ is a C library for encoding, decoding and manipulating JSON data. Its main features and design principles are: - Simple and intuitive API and data model -- Comprehensive documentation +- `Comprehensive documentation`_ - No dependencies on other libraries @@ -21,39 +30,52 @@ source distribution for details. Compilation and Installation ---------------------------- -If you obtained a source tarball, just use the standard autotools -commands:: +You can download and install Jansson using the `vcpkg `_ dependency manager: - $ ./configure && make && make install +.. code-block:: bash -If the source has been checked out from a Git repository, the -./configure script has to be generated fist. The easiest way is to use -autoreconf:: + git clone https://github.com/Microsoft/vcpkg.git + cd vcpkg + ./bootstrap-vcpkg.sh + ./vcpkg integrate install + vcpkg install jansson - $ autoreconf -i +The Jansson port in vcpkg is kept up to date by Microsoft team members and community contributors. If the version is out of date, please `create an issue or pull request `_ on the vcpkg repository. + +If you obtained a `source tarball`_ from the "Releases" section of the main +site just use the standard autotools commands:: + + $ ./configure + $ make + $ make install To run the test suite, invoke:: $ make check +If the source has been checked out from a Git repository, the +./configure script has to be generated first. The easiest way is to +use autoreconf:: + + $ autoreconf -i + Documentation ------------- -Documentation is in the ``doc/`` subdirectory. It's written in -reStructuredText_ with Sphinx_ annotations, so reading it in plain may -be inconvenient. For this reason, prebuilt HTML documentation is -available at http://www.digip.org/jansson/doc/. +Documentation is available at http://jansson.readthedocs.io/en/latest/. -To generate HTML documentation yourself, invoke:: +The documentation source is in the ``doc/`` subdirectory. To generate +HTML documentation, invoke:: - make html + $ make html -and point your browser to ``doc/_build/html/index.html``. Sphinx_ is -required to generate the documentation. +Then, point your browser to ``doc/_build/html/index.html``. Sphinx_ +1.0 or newer is required to generate the documentation. .. _Jansson: http://www.digip.org/jansson/ +.. _`Comprehensive documentation`: http://jansson.readthedocs.io/en/latest/ .. _`MIT license`: http://www.opensource.org/licenses/mit-license.php -.. _reStructuredText: http://docutils.sourceforge.net/rst.html +.. _`source tarball`: http://www.digip.org/jansson#releases .. _Sphinx: http://sphinx.pocoo.org/ diff --git a/src/jansson/configure.ac b/src/jansson/configure.ac index cabc7324..f022eb72 100644 --- a/src/jansson/configure.ac +++ b/src/jansson/configure.ac @@ -1,29 +1,29 @@ AC_PREREQ([2.60]) -AC_INIT([jansson], [2.0pre], [petri@digip.org]) +AC_INIT([jansson], [2.14], [https://github.com/akheron/jansson/issues]) +AC_CONFIG_AUX_DIR([.]) AM_INIT_AUTOMAKE([1.10 foreign]) AC_CONFIG_SRCDIR([src/value.c]) -AC_CONFIG_HEADERS([config.h]) +AC_CONFIG_HEADERS([jansson_private_config.h]) # Checks for programs. AC_PROG_CC +AC_PROG_CXX AC_PROG_LIBTOOL AM_CONDITIONAL([GCC], [test x$GCC = xyes]) # Checks for libraries. # Checks for header files. +AC_CHECK_HEADERS([endian.h fcntl.h locale.h sched.h unistd.h sys/param.h sys/stat.h sys/time.h sys/types.h]) # Checks for typedefs, structures, and compiler characteristics. AC_TYPE_INT32_T - +AC_TYPE_UINT32_T +AC_TYPE_UINT16_T +AC_TYPE_UINT8_T AC_TYPE_LONG_LONG_INT -case $ac_cv_type_long_long_int in - yes) json_have_long_long=1;; - *) json_have_long_long=0;; -esac -AC_SUBST([json_have_long_long]) AC_C_INLINE case $ac_cv_c_inline in @@ -34,6 +34,137 @@ esac AC_SUBST([json_inline]) # Checks for library functions. +AC_CHECK_FUNCS([close getpid gettimeofday localeconv open read sched_yield strtoll]) + +AC_MSG_CHECKING([for gcc __sync builtins]) +have_sync_builtins=no +AC_TRY_LINK( + [], [unsigned long val; __sync_bool_compare_and_swap(&val, 0, 1); __sync_add_and_fetch(&val, 1); __sync_sub_and_fetch(&val, 1);], + [have_sync_builtins=yes], +) +if test "x$have_sync_builtins" = "xyes"; then + AC_DEFINE([HAVE_SYNC_BUILTINS], [1], + [Define to 1 if gcc's __sync builtins are available]) + json_have_sync_builtins=1 +else + json_have_sync_builtins=0 +fi +AC_SUBST([json_have_sync_builtins]) +AC_MSG_RESULT([$have_sync_builtins]) + +AC_MSG_CHECKING([for gcc __atomic builtins]) +have_atomic_builtins=no +AC_TRY_LINK( + [], [char l; unsigned long v; __atomic_test_and_set(&l, __ATOMIC_RELAXED); __atomic_store_n(&v, 1, __ATOMIC_RELEASE); __atomic_load_n(&v, __ATOMIC_ACQUIRE); __atomic_add_fetch(&v, 1, __ATOMIC_ACQUIRE); __atomic_sub_fetch(&v, 1, __ATOMIC_RELEASE);], + [have_atomic_builtins=yes], +) +if test "x$have_atomic_builtins" = "xyes"; then + AC_DEFINE([HAVE_ATOMIC_BUILTINS], [1], + [Define to 1 if gcc's __atomic builtins are available]) + json_have_atomic_builtins=1 +else + json_have_atomic_builtins=0 +fi +AC_SUBST([json_have_atomic_builtins]) +AC_MSG_RESULT([$have_atomic_builtins]) + +case "$ac_cv_type_long_long_int$ac_cv_func_strtoll" in + yesyes) json_have_long_long=1;; + *) json_have_long_long=0;; +esac +AC_SUBST([json_have_long_long]) + +case "$ac_cv_header_locale_h$ac_cv_func_localeconv" in + yesyes) json_have_localeconv=1;; + *) json_have_localeconv=0;; +esac +AC_SUBST([json_have_localeconv]) + +# Features +AC_ARG_ENABLE([urandom], + [AS_HELP_STRING([--disable-urandom], + [Don't use /dev/urandom to seed the hash function])], + [use_urandom=$enableval], [use_urandom=yes]) + +if test "x$use_urandom" = xyes; then +AC_DEFINE([USE_URANDOM], [1], + [Define to 1 if /dev/urandom should be used for seeding the hash function]) +fi + +AC_ARG_ENABLE([windows-cryptoapi], + [AS_HELP_STRING([--disable-windows-cryptoapi], + [Don't use CryptGenRandom to seed the hash function])], + [use_windows_cryptoapi=$enableval], [use_windows_cryptoapi=yes]) + +if test "x$use_windows_cryptoapi" = xyes; then +AC_DEFINE([USE_WINDOWS_CRYPTOAPI], [1], + [Define to 1 if CryptGenRandom should be used for seeding the hash function]) +fi + +AC_ARG_ENABLE([initial-hashtable-order], + [AS_HELP_STRING([--enable-initial-hashtable-order=VAL], + [Number of buckets new object hashtables contain is 2 raised to this power. The default is 3, so empty hashtables contain 2^3 = 8 buckets.])], + [initial_hashtable_order=$enableval], [initial_hashtable_order=3]) +AC_DEFINE_UNQUOTED([INITIAL_HASHTABLE_ORDER], [$initial_hashtable_order], + [Number of buckets new object hashtables contain is 2 raised to this power. E.g. 3 -> 2^3 = 8.]) + +AC_ARG_ENABLE([Bsymbolic], + [AS_HELP_STRING([--disable-Bsymbolic], + [Avoid linking with -Bsymbolic-function])], + [], [with_Bsymbolic=check]) + +if test "x$with_Bsymbolic" != "xno" ; then + AC_MSG_CHECKING([for -Bsymbolic-functions linker flag]) + saved_LDFLAGS="${LDFLAGS}" + LDFLAGS=-Wl,-Bsymbolic-functions + AC_TRY_LINK( + [], [int main (void) { return 0; }], + [AC_MSG_RESULT([yes]) + have_Bsymbolic=yes], + [AC_MSG_RESULT([no]) + have_Bsymbolic=no] + ) + LDFLAGS="${saved_LDFLAGS}" + + if test "x$with_Bsymbolic" = "xcheck" ; then + with_Bsymbolic=$have_Bsymbolic; + fi + if test "x$with_Bsymbolic:x$have_Bsymbolic" = "xyes:xno" ; then + AC_MSG_ERROR([linker support is required for -Bsymbolic]) + fi +fi + +AS_IF([test "x$with_Bsymbolic" = "xyes"], [JSON_BSYMBOLIC_LDFLAGS=-Wl[,]-Bsymbolic-functions]) +AC_SUBST(JSON_BSYMBOLIC_LDFLAGS) + +# Enable symbol versioning on GNU libc +JSON_SYMVER_LDFLAGS= +AC_CHECK_DECL([__GLIBC__], [JSON_SYMVER_LDFLAGS=-Wl,--default-symver]) +AC_SUBST([JSON_SYMVER_LDFLAGS]) + +AC_ARG_ENABLE([ossfuzzers], + [AS_HELP_STRING([--enable-ossfuzzers], + [Whether to generate the fuzzers for OSS-Fuzz])], + [have_ossfuzzers=yes], [have_ossfuzzers=no]) +AM_CONDITIONAL([USE_OSSFUZZERS], [test "x$have_ossfuzzers" = "xyes"]) + + +AC_SUBST([LIB_FUZZING_ENGINE]) +AM_CONDITIONAL([USE_OSSFUZZ_FLAG], [test "x$LIB_FUZZING_ENGINE" = "x-fsanitize=fuzzer"]) +AM_CONDITIONAL([USE_OSSFUZZ_STATIC], [test -f "$LIB_FUZZING_ENGINE"]) + + +if test x$GCC = xyes; then + AC_MSG_CHECKING(for -Wno-format-truncation) + wnoformat_truncation="-Wno-format-truncation" + AS_IF([${CC} -Wno-format-truncation -Werror -S -o /dev/null -xc /dev/null > /dev/null 2>&1], + [AC_MSG_RESULT(yes)], + [AC_MSG_RESULT(no) + wnoformat_truncation=""]) + + AM_CFLAGS="-Wall -Wextra -Wdeclaration-after-statement -Wshadow ${wnoformat_truncation}" +fi +AC_SUBST([AM_CFLAGS]) AC_CONFIG_FILES([ jansson.pc @@ -43,6 +174,7 @@ AC_CONFIG_FILES([ src/jansson_config.h test/Makefile test/bin/Makefile + test/ossfuzz/Makefile test/suites/Makefile test/suites/api/Makefile ]) diff --git a/src/jansson/jansson.pc.in b/src/jansson/jansson.pc.in index d9bf4dad..69c9a43a 100644 --- a/src/jansson/jansson.pc.in +++ b/src/jansson/jansson.pc.in @@ -1,7 +1,7 @@ prefix=@prefix@ exec_prefix=@exec_prefix@ libdir=@libdir@ -includedir=${prefix}/include +includedir=@includedir@ Name: Jansson Description: Library for encoding, decoding and manipulating JSON data diff --git a/src/jansson/src/Makefile.am b/src/jansson/src/Makefile.am index be8fcb90..63eda321 100644 --- a/src/jansson/src/Makefile.am +++ b/src/jansson/src/Makefile.am @@ -1,4 +1,7 @@ -include_HEADERS = jansson.h jansson_config.h +EXTRA_DIST = jansson.def + +include_HEADERS = jansson.h +nodist_include_HEADERS = jansson_config.h lib_LTLIBRARIES = libjansson.la libjansson_la_SOURCES = \ @@ -6,19 +9,22 @@ libjansson_la_SOURCES = \ error.c \ hashtable.c \ hashtable.h \ + hashtable_seed.c \ jansson_private.h \ load.c \ + lookup3.h \ + memory.c \ + pack_unpack.c \ strbuffer.c \ strbuffer.h \ + strconv.c \ utf.c \ utf.h \ value.c \ - variadic.c + version.c libjansson_la_LDFLAGS = \ - -export-symbols-regex '^json_' \ - -version-info 3:0:3 - -if GCC -# These flags are gcc specific -AM_CFLAGS = -Wall -Wextra -Werror -endif + -no-undefined \ + -export-symbols-regex '^json_|^jansson_' \ + -version-info 18:0:14 \ + @JSON_SYMVER_LDFLAGS@ \ + @JSON_BSYMBOLIC_LDFLAGS@ diff --git a/src/jansson/src/dump.c b/src/jansson/src/dump.c index fb6d5458..c0fb06e7 100644 --- a/src/jansson/src/dump.c +++ b/src/jansson/src/dump.c @@ -1,141 +1,183 @@ /* - * Copyright (c) 2009, 2010 Petri Lehtinen + * Copyright (c) 2009-2016 Petri Lehtinen * * Jansson is free software; you can redistribute it and/or modify * it under the terms of the MIT license. See LICENSE for details. */ +#ifndef _GNU_SOURCE #define _GNU_SOURCE +#endif + +#include "jansson_private.h" + +#include #include #include #include -#include +#ifdef HAVE_UNISTD_H +#include +#endif -#include -#include "jansson_private.h" +#include "jansson.h" #include "strbuffer.h" #include "utf.h" -#define MAX_INTEGER_STR_LENGTH 100 -#define MAX_REAL_STR_LENGTH 100 +#define MAX_INTEGER_STR_LENGTH 100 +#define MAX_REAL_STR_LENGTH 100 -typedef int (*dump_func)(const char *buffer, int size, void *data); +#define FLAGS_TO_INDENT(f) ((f)&0x1F) +#define FLAGS_TO_PRECISION(f) (((f) >> 11) & 0x1F) -struct string -{ - char *buffer; - int length; - int size; +struct buffer { + const size_t size; + size_t used; + char *data; }; -static int dump_to_strbuffer(const char *buffer, int size, void *data) -{ +static int dump_to_strbuffer(const char *buffer, size_t size, void *data) { return strbuffer_append_bytes((strbuffer_t *)data, buffer, size); } -static int dump_to_file(const char *buffer, int size, void *data) -{ +static int dump_to_buffer(const char *buffer, size_t size, void *data) { + struct buffer *buf = (struct buffer *)data; + + if (buf->used + size <= buf->size) + memcpy(&buf->data[buf->used], buffer, size); + + buf->used += size; + return 0; +} + +static int dump_to_file(const char *buffer, size_t size, void *data) { FILE *dest = (FILE *)data; - if(fwrite(buffer, size, 1, dest) != 1) + if (fwrite(buffer, size, 1, dest) != 1) return -1; return 0; } +static int dump_to_fd(const char *buffer, size_t size, void *data) { +#ifdef HAVE_UNISTD_H + int *dest = (int *)data; + if (write(*dest, buffer, size) == (ssize_t)size) + return 0; +#endif + return -1; +} + /* 32 spaces (the maximum indentation size) */ -static char whitespace[] = " "; +static const char whitespace[] = " "; -static int dump_indent(size_t flags, int depth, int space, dump_func dump, void *data) -{ - if(JSON_INDENT(flags) > 0) - { - int i, ws_count = JSON_INDENT(flags); +static int dump_indent(size_t flags, int depth, int space, json_dump_callback_t dump, + void *data) { + if (FLAGS_TO_INDENT(flags) > 0) { + unsigned int ws_count = FLAGS_TO_INDENT(flags), n_spaces = depth * ws_count; - if(dump("\n", 1, data)) + if (dump("\n", 1, data)) return -1; - for(i = 0; i < depth; i++) - { - if(dump(whitespace, ws_count, data)) + while (n_spaces > 0) { + int cur_n = + n_spaces < sizeof whitespace - 1 ? n_spaces : sizeof whitespace - 1; + + if (dump(whitespace, cur_n, data)) return -1; + + n_spaces -= cur_n; } - } - else if(space && !(flags & JSON_COMPACT)) - { + } else if (space && !(flags & JSON_COMPACT)) { return dump(" ", 1, data); } return 0; } -static int dump_string(const char *str, int ascii, dump_func dump, void *data) -{ - const char *pos, *end; +static int dump_string(const char *str, size_t len, json_dump_callback_t dump, void *data, + size_t flags) { + const char *pos, *end, *lim; int32_t codepoint = 0; - if(dump("\"", 1, data)) + if (dump("\"", 1, data)) return -1; end = pos = str; - while(1) - { + lim = str + len; + while (1) { const char *text; - char seq[22]; + char seq[13]; int length; - while(*end) - { - end = utf8_iterate(pos, &codepoint); - if(!end) + while (end < lim) { + end = utf8_iterate(pos, lim - pos, &codepoint); + if (!end) return -1; /* mandatory escape or control char */ - if(codepoint == '\\' || codepoint == '"' || codepoint < 0x20) + if (codepoint == '\\' || codepoint == '"' || codepoint < 0x20) + break; + + /* slash */ + if ((flags & JSON_ESCAPE_SLASH) && codepoint == '/') break; /* non-ASCII */ - if(ascii && codepoint > 0x7F) + if ((flags & JSON_ENSURE_ASCII) && codepoint > 0x7F) break; pos = end; } - if(pos != str) { - if(dump(str, pos - str, data)) + if (pos != str) { + if (dump(str, pos - str, data)) return -1; } - if(end == pos) + if (end == pos) break; - /* handle \, ", and control codes */ + /* handle \, /, ", and control codes */ length = 2; - switch(codepoint) - { - case '\\': text = "\\\\"; break; - case '\"': text = "\\\""; break; - case '\b': text = "\\b"; break; - case '\f': text = "\\f"; break; - case '\n': text = "\\n"; break; - case '\r': text = "\\r"; break; - case '\t': text = "\\t"; break; - default: - { + switch (codepoint) { + case '\\': + text = "\\\\"; + break; + case '\"': + text = "\\\""; + break; + case '\b': + text = "\\b"; + break; + case '\f': + text = "\\f"; + break; + case '\n': + text = "\\n"; + break; + case '\r': + text = "\\r"; + break; + case '\t': + text = "\\t"; + break; + case '/': + text = "\\/"; + break; + default: { /* codepoint is in BMP */ - if(codepoint < 0x10000) - { - sprintf(seq, "\\u%04x", codepoint); + if (codepoint < 0x10000) { + snprintf(seq, sizeof(seq), "\\u%04X", (unsigned int)codepoint); length = 6; } /* not in BMP -> construct a UTF-16 surrogate pair */ - else - { + else { int32_t first, last; codepoint -= 0x10000; first = 0xD800 | ((codepoint & 0xffc00) >> 10); last = 0xDC00 | (codepoint & 0x003ff); - sprintf(seq, "\\u%04x\\u%04x", first, last); + snprintf(seq, sizeof(seq), "\\u%04X\\u%04X", (unsigned int)first, + (unsigned int)last); length = 12; } @@ -144,7 +186,7 @@ static int dump_string(const char *str, int ascii, dump_func dump, void *data) } } - if(dump(text, length, data)) + if (dump(text, length, data)) return -1; str = pos = end; @@ -153,24 +195,33 @@ static int dump_string(const char *str, int ascii, dump_func dump, void *data) return dump("\"", 1, data); } -static int object_key_compare_keys(const void *key1, const void *key2) -{ - return strcmp((*(const object_key_t **)key1)->key, - (*(const object_key_t **)key2)->key); -} +struct key_len { + const char *key; + int len; +}; + +static int compare_keys(const void *key1, const void *key2) { + const struct key_len *k1 = key1; + const struct key_len *k2 = key2; + const size_t min_size = k1->len < k2->len ? k1->len : k2->len; + int res = memcmp(k1->key, k2->key, min_size); -static int object_key_compare_serials(const void *key1, const void *key2) -{ - return (*(const object_key_t **)key1)->serial - - (*(const object_key_t **)key2)->serial; + if (res) + return res; + + return k1->len - k2->len; } -static int do_dump(const json_t *json, size_t flags, int depth, - dump_func dump, void *data) -{ - int ascii = flags & JSON_ENSURE_ASCII ? 1 : 0; +static int do_dump(const json_t *json, size_t flags, int depth, hashtable_t *parents, + json_dump_callback_t dump, void *data) { + int embed = flags & JSON_EMBED; + + flags &= ~JSON_EMBED; - switch(json_typeof(json)) { + if (!json) + return -1; + + switch (json_typeof(json)) { case JSON_NULL: return dump("null", 4, data); @@ -180,233 +231,190 @@ static int do_dump(const json_t *json, size_t flags, int depth, case JSON_FALSE: return dump("false", 5, data); - case JSON_INTEGER: - { + case JSON_INTEGER: { char buffer[MAX_INTEGER_STR_LENGTH]; int size; - size = snprintf(buffer, MAX_INTEGER_STR_LENGTH, - "%" JSON_INTEGER_FORMAT, + size = snprintf(buffer, MAX_INTEGER_STR_LENGTH, "%" JSON_INTEGER_FORMAT, json_integer_value(json)); - if(size >= MAX_INTEGER_STR_LENGTH) + if (size < 0 || size >= MAX_INTEGER_STR_LENGTH) return -1; return dump(buffer, size, data); } - case JSON_REAL: - { + case JSON_REAL: { char buffer[MAX_REAL_STR_LENGTH]; int size; + double value = json_real_value(json); - size = snprintf(buffer, MAX_REAL_STR_LENGTH, "%.17g", - json_real_value(json)); - if(size >= MAX_REAL_STR_LENGTH) + size = jsonp_dtostr(buffer, MAX_REAL_STR_LENGTH, value, + FLAGS_TO_PRECISION(flags)); + if (size < 0) return -1; - /* Make sure there's a dot or 'e' in the output. Otherwise - a real is converted to an integer when decoding */ - if(strchr(buffer, '.') == NULL && - strchr(buffer, 'e') == NULL) - { - if(size + 2 >= MAX_REAL_STR_LENGTH) { - /* No space to append ".0" */ - return -1; - } - buffer[size] = '.'; - buffer[size + 1] = '0'; - size += 2; - } - return dump(buffer, size, data); } case JSON_STRING: - return dump_string(json_string_value(json), ascii, dump, data); + return dump_string(json_string_value(json), json_string_length(json), dump, + data, flags); - case JSON_ARRAY: - { - int i; - int n; - json_array_t *array; + case JSON_ARRAY: { + size_t n; + size_t i; + /* Space for "0x", double the sizeof a pointer for the hex and a + * terminator. */ + char key[2 + (sizeof(json) * 2) + 1]; + size_t key_len; /* detect circular references */ - array = json_to_array(json); - if(array->visited) - goto array_error; - array->visited = 1; + if (jsonp_loop_check(parents, json, key, sizeof(key), &key_len)) + return -1; n = json_array_size(json); - if(dump("[", 1, data)) - goto array_error; - if(n == 0) { - array->visited = 0; - return dump("]", 1, data); + if (!embed && dump("[", 1, data)) + return -1; + if (n == 0) { + hashtable_del(parents, key, key_len); + return embed ? 0 : dump("]", 1, data); } - if(dump_indent(flags, depth + 1, 0, dump, data)) - goto array_error; - - for(i = 0; i < n; ++i) { - if(do_dump(json_array_get(json, i), flags, depth + 1, - dump, data)) - goto array_error; - - if(i < n - 1) - { - if(dump(",", 1, data) || - dump_indent(flags, depth + 1, 1, dump, data)) - goto array_error; - } - else - { - if(dump_indent(flags, depth, 0, dump, data)) - goto array_error; + if (dump_indent(flags, depth + 1, 0, dump, data)) + return -1; + + for (i = 0; i < n; ++i) { + if (do_dump(json_array_get(json, i), flags, depth + 1, parents, dump, + data)) + return -1; + + if (i < n - 1) { + if (dump(",", 1, data) || + dump_indent(flags, depth + 1, 1, dump, data)) + return -1; + } else { + if (dump_indent(flags, depth, 0, dump, data)) + return -1; } } - array->visited = 0; - return dump("]", 1, data); - - array_error: - array->visited = 0; - return -1; + hashtable_del(parents, key, key_len); + return embed ? 0 : dump("]", 1, data); } - case JSON_OBJECT: - { - json_object_t *object; + case JSON_OBJECT: { void *iter; const char *separator; int separator_length; + char loop_key[LOOP_KEY_LEN]; + size_t loop_key_len; - if(flags & JSON_COMPACT) { + if (flags & JSON_COMPACT) { separator = ":"; separator_length = 1; - } - else { + } else { separator = ": "; separator_length = 2; } /* detect circular references */ - object = json_to_object(json); - if(object->visited) - goto object_error; - object->visited = 1; + if (jsonp_loop_check(parents, json, loop_key, sizeof(loop_key), + &loop_key_len)) + return -1; iter = json_object_iter((json_t *)json); - if(dump("{", 1, data)) - goto object_error; - if(!iter) { - object->visited = 0; - return dump("}", 1, data); + if (!embed && dump("{", 1, data)) + return -1; + if (!iter) { + hashtable_del(parents, loop_key, loop_key_len); + return embed ? 0 : dump("}", 1, data); } - if(dump_indent(flags, depth + 1, 0, dump, data)) - goto object_error; + if (dump_indent(flags, depth + 1, 0, dump, data)) + return -1; - if(flags & JSON_SORT_KEYS || flags & JSON_PRESERVE_ORDER) - { - const object_key_t **keys; + if (flags & JSON_SORT_KEYS) { + struct key_len *keys; size_t size, i; - int (*cmp_func)(const void *, const void *); size = json_object_size(json); - keys = malloc(size * sizeof(object_key_t *)); - if(!keys) - goto object_error; + keys = jsonp_malloc(size * sizeof(struct key_len)); + if (!keys) + return -1; i = 0; - while(iter) - { - keys[i] = jsonp_object_iter_fullkey(iter); + while (iter) { + struct key_len *keylen = &keys[i]; + + keylen->key = json_object_iter_key(iter); + keylen->len = json_object_iter_key_len(iter); + iter = json_object_iter_next((json_t *)json, iter); i++; } assert(i == size); - if(flags & JSON_SORT_KEYS) - cmp_func = object_key_compare_keys; - else - cmp_func = object_key_compare_serials; - - qsort(keys, size, sizeof(object_key_t *), cmp_func); + qsort(keys, size, sizeof(struct key_len), compare_keys); - for(i = 0; i < size; i++) - { - const char *key; + for (i = 0; i < size; i++) { + const struct key_len *key; json_t *value; - key = keys[i]->key; - value = json_object_get(json, key); + key = &keys[i]; + value = json_object_getn(json, key->key, key->len); assert(value); - dump_string(key, ascii, dump, data); - if(dump(separator, separator_length, data) || - do_dump(value, flags, depth + 1, dump, data)) - { - free(keys); - goto object_error; + dump_string(key->key, key->len, dump, data, flags); + if (dump(separator, separator_length, data) || + do_dump(value, flags, depth + 1, parents, dump, data)) { + jsonp_free(keys); + return -1; } - if(i < size - 1) - { - if(dump(",", 1, data) || - dump_indent(flags, depth + 1, 1, dump, data)) - { - free(keys); - goto object_error; + if (i < size - 1) { + if (dump(",", 1, data) || + dump_indent(flags, depth + 1, 1, dump, data)) { + jsonp_free(keys); + return -1; } - } - else - { - if(dump_indent(flags, depth, 0, dump, data)) - { - free(keys); - goto object_error; + } else { + if (dump_indent(flags, depth, 0, dump, data)) { + jsonp_free(keys); + return -1; } } } - free(keys); - } - else - { + jsonp_free(keys); + } else { /* Don't sort keys */ - while(iter) - { + while (iter) { void *next = json_object_iter_next((json_t *)json, iter); - - dump_string(json_object_iter_key(iter), ascii, dump, data); - if(dump(separator, separator_length, data) || - do_dump(json_object_iter_value(iter), flags, depth + 1, - dump, data)) - goto object_error; - - if(next) - { - if(dump(",", 1, data) || - dump_indent(flags, depth + 1, 1, dump, data)) - goto object_error; - } - else - { - if(dump_indent(flags, depth, 0, dump, data)) - goto object_error; + const char *key = json_object_iter_key(iter); + const size_t key_len = json_object_iter_key_len(iter); + + dump_string(key, key_len, dump, data, flags); + if (dump(separator, separator_length, data) || + do_dump(json_object_iter_value(iter), flags, depth + 1, parents, + dump, data)) + return -1; + + if (next) { + if (dump(",", 1, data) || + dump_indent(flags, depth + 1, 1, dump, data)) + return -1; + } else { + if (dump_indent(flags, depth, 0, dump, data)) + return -1; } iter = next; } } - object->visited = 0; - return dump("}", 1, data); - - object_error: - object->visited = 0; - return -1; + hashtable_del(parents, loop_key, loop_key_len); + return embed ? 0 : dump("}", 1, data); } default: @@ -415,47 +423,68 @@ static int do_dump(const json_t *json, size_t flags, int depth, } } - -char *json_dumps(const json_t *json, size_t flags) -{ +char *json_dumps(const json_t *json, size_t flags) { strbuffer_t strbuff; char *result; - if(!json_is_array(json) && !json_is_object(json)) + if (strbuffer_init(&strbuff)) return NULL; - if(strbuffer_init(&strbuff)) - return NULL; + if (json_dump_callback(json, dump_to_strbuffer, (void *)&strbuff, flags)) + result = NULL; + else + result = jsonp_strdup(strbuffer_value(&strbuff)); - if(do_dump(json, flags, 0, dump_to_strbuffer, (void *)&strbuff)) { - strbuffer_close(&strbuff); - return NULL; - } - - result = strdup(strbuffer_value(&strbuff)); strbuffer_close(&strbuff); - return result; } -int json_dumpf(const json_t *json, FILE *output, size_t flags) -{ - if(!json_is_array(json) && !json_is_object(json)) - return -1; +size_t json_dumpb(const json_t *json, char *buffer, size_t size, size_t flags) { + struct buffer buf = {size, 0, buffer}; + + if (json_dump_callback(json, dump_to_buffer, (void *)&buf, flags)) + return 0; + + return buf.used; +} + +int json_dumpf(const json_t *json, FILE *output, size_t flags) { + return json_dump_callback(json, dump_to_file, (void *)output, flags); +} - return do_dump(json, flags, 0, dump_to_file, (void *)output); +int json_dumpfd(const json_t *json, int output, size_t flags) { + return json_dump_callback(json, dump_to_fd, (void *)&output, flags); } -int json_dump_file(const json_t *json, const char *path, size_t flags) -{ +int json_dump_file(const json_t *json, const char *path, size_t flags) { int result; FILE *output = fopen(path, "w"); - if(!output) + if (!output) return -1; result = json_dumpf(json, output, flags); - fclose(output); + if (fclose(output) != 0) + return -1; + return result; } + +int json_dump_callback(const json_t *json, json_dump_callback_t callback, void *data, + size_t flags) { + int res; + hashtable_t parents_set; + + if (!(flags & JSON_ENCODE_ANY)) { + if (!json_is_array(json) && !json_is_object(json)) + return -1; + } + + if (hashtable_init(&parents_set)) + return -1; + res = do_dump(json, flags, 0, &parents_set, callback, data); + hashtable_close(&parents_set); + + return res; +} diff --git a/src/jansson/src/error.c b/src/jansson/src/error.c index b8c78db1..ea87bba6 100644 --- a/src/jansson/src/error.c +++ b/src/jansson/src/error.c @@ -1,38 +1,60 @@ -#include -#include - #include "jansson_private.h" +#include -void jsonp_error_init(json_error_t *error, const char *source) -{ - if(error) - { +void jsonp_error_init(json_error_t *error, const char *source) { + if (error) { error->text[0] = '\0'; error->line = -1; error->column = -1; + error->position = 0; + if (source) + jsonp_error_set_source(error, source); + else + error->source[0] = '\0'; + } +} + +void jsonp_error_set_source(json_error_t *error, const char *source) { + size_t length; + + if (!error || !source) + return; + length = strlen(source); + if (length < JSON_ERROR_SOURCE_LENGTH) strncpy(error->source, source, JSON_ERROR_SOURCE_LENGTH); + else { + size_t extra = length - JSON_ERROR_SOURCE_LENGTH + 4; + memcpy(error->source, "...", 3); + strncpy(error->source + 3, source + extra, JSON_ERROR_SOURCE_LENGTH - 3); error->source[JSON_ERROR_SOURCE_LENGTH - 1] = '\0'; } } -void jsonp_error_set(json_error_t *error, int line, int column, - const char *msg, ...) -{ +void jsonp_error_set(json_error_t *error, int line, int column, size_t position, + enum json_error_code code, const char *msg, ...) { va_list ap; - if(!error) + va_start(ap, msg); + jsonp_error_vset(error, line, column, position, code, msg, ap); + va_end(ap); +} + +void jsonp_error_vset(json_error_t *error, int line, int column, size_t position, + enum json_error_code code, const char *msg, va_list ap) { + if (!error) return; - if(error->text[0] != '\0') { + if (error->text[0] != '\0') { /* error already set */ return; } error->line = line; error->column = column; + error->position = (int)position; - va_start(ap, msg); - vsnprintf(error->text, JSON_ERROR_TEXT_LENGTH, msg, ap); - va_end(ap); + vsnprintf(error->text, JSON_ERROR_TEXT_LENGTH - 1, msg, ap); + error->text[JSON_ERROR_TEXT_LENGTH - 2] = '\0'; + error->text[JSON_ERROR_TEXT_LENGTH - 1] = code; } diff --git a/src/jansson/src/hashtable.c b/src/jansson/src/hashtable.c index 158aa281..01506a41 100644 --- a/src/jansson/src/hashtable.c +++ b/src/jansson/src/hashtable.c @@ -1,91 +1,86 @@ /* - * Copyright (c) 2009, 2010 Petri Lehtinen + * Copyright (c) 2009-2016 Petri Lehtinen * * This library is free software; you can redistribute it and/or modify * it under the terms of the MIT license. See LICENSE for details. */ +#if HAVE_CONFIG_H +#include +#endif + #include -#include /* for JSON_INLINE */ -#include "jansson_private.h" /* for container_of() */ +#include +#include + #include "hashtable.h" +#include "jansson_private.h" /* for container_of() */ +#include /* for JSON_INLINE */ + +#ifndef INITIAL_HASHTABLE_ORDER +#define INITIAL_HASHTABLE_ORDER 3 +#endif typedef struct hashtable_list list_t; typedef struct hashtable_pair pair_t; typedef struct hashtable_bucket bucket_t; -#define list_to_pair(list_) container_of(list_, pair_t, list) +extern volatile uint32_t hashtable_seed; -static JSON_INLINE void list_init(list_t *list) -{ +/* Implementation of the hash function */ +#include "lookup3.h" + +#define list_to_pair(list_) container_of(list_, pair_t, list) +#define ordered_list_to_pair(list_) container_of(list_, pair_t, ordered_list) +#define hash_str(key, len) ((size_t)hashlittle((key), len, hashtable_seed)) + +static JSON_INLINE void list_init(list_t *list) { list->next = list; list->prev = list; } -static JSON_INLINE void list_insert(list_t *list, list_t *node) -{ +static JSON_INLINE void list_insert(list_t *list, list_t *node) { node->next = list; node->prev = list->prev; list->prev->next = node; list->prev = node; } -static JSON_INLINE void list_remove(list_t *list) -{ +static JSON_INLINE void list_remove(list_t *list) { list->prev->next = list->next; list->next->prev = list->prev; } -static JSON_INLINE int bucket_is_empty(hashtable_t *hashtable, bucket_t *bucket) -{ +static JSON_INLINE int bucket_is_empty(hashtable_t *hashtable, bucket_t *bucket) { return bucket->first == &hashtable->list && bucket->first == bucket->last; } -static void insert_to_bucket(hashtable_t *hashtable, bucket_t *bucket, - list_t *list) -{ - if(bucket_is_empty(hashtable, bucket)) - { +static void insert_to_bucket(hashtable_t *hashtable, bucket_t *bucket, list_t *list) { + if (bucket_is_empty(hashtable, bucket)) { list_insert(&hashtable->list, list); bucket->first = bucket->last = list; - } - else - { + } else { list_insert(bucket->first, list); bucket->first = list; } } -static size_t primes[] = { - 5, 13, 23, 53, 97, 193, 389, 769, 1543, 3079, 6151, 12289, 24593, - 49157, 98317, 196613, 393241, 786433, 1572869, 3145739, 6291469, - 12582917, 25165843, 50331653, 100663319, 201326611, 402653189, - 805306457, 1610612741 -}; - -static JSON_INLINE size_t num_buckets(hashtable_t *hashtable) -{ - return primes[hashtable->num_buckets]; -} - - static pair_t *hashtable_find_pair(hashtable_t *hashtable, bucket_t *bucket, - const void *key, size_t hash) -{ + const char *key, size_t key_len, size_t hash) { list_t *list; pair_t *pair; - if(bucket_is_empty(hashtable, bucket)) + if (bucket_is_empty(hashtable, bucket)) return NULL; list = bucket->first; - while(1) - { + while (1) { pair = list_to_pair(list); - if(pair->hash == hash && hashtable->cmp_keys(pair->key, key)) + if (pair->hash == hash && pair->key_len == key_len && + memcmp(pair->key, key, key_len) == 0) return pair; - if(list == bucket->last) + if (list == bucket->last) break; list = list->next; @@ -95,84 +90,75 @@ static pair_t *hashtable_find_pair(hashtable_t *hashtable, bucket_t *bucket, } /* returns 0 on success, -1 if key was not found */ -static int hashtable_do_del(hashtable_t *hashtable, - const void *key, size_t hash) -{ +static int hashtable_do_del(hashtable_t *hashtable, const char *key, size_t key_len, + size_t hash) { pair_t *pair; bucket_t *bucket; size_t index; - index = hash % num_buckets(hashtable); + index = hash & hashmask(hashtable->order); bucket = &hashtable->buckets[index]; - pair = hashtable_find_pair(hashtable, bucket, key, hash); - if(!pair) + pair = hashtable_find_pair(hashtable, bucket, key, key_len, hash); + if (!pair) return -1; - if(&pair->list == bucket->first && &pair->list == bucket->last) + if (&pair->list == bucket->first && &pair->list == bucket->last) bucket->first = bucket->last = &hashtable->list; - else if(&pair->list == bucket->first) + else if (&pair->list == bucket->first) bucket->first = pair->list.next; - else if(&pair->list == bucket->last) + else if (&pair->list == bucket->last) bucket->last = pair->list.prev; list_remove(&pair->list); + list_remove(&pair->ordered_list); + json_decref(pair->value); - if(hashtable->free_key) - hashtable->free_key(pair->key); - if(hashtable->free_value) - hashtable->free_value(pair->value); - - free(pair); + jsonp_free(pair); hashtable->size--; return 0; } -static void hashtable_do_clear(hashtable_t *hashtable) -{ +static void hashtable_do_clear(hashtable_t *hashtable) { list_t *list, *next; pair_t *pair; - for(list = hashtable->list.next; list != &hashtable->list; list = next) - { + for (list = hashtable->list.next; list != &hashtable->list; list = next) { next = list->next; pair = list_to_pair(list); - if(hashtable->free_key) - hashtable->free_key(pair->key); - if(hashtable->free_value) - hashtable->free_value(pair->value); - free(pair); + json_decref(pair->value); + jsonp_free(pair); } } -static int hashtable_do_rehash(hashtable_t *hashtable) -{ +static int hashtable_do_rehash(hashtable_t *hashtable) { list_t *list, *next; pair_t *pair; - size_t i, index, new_size; + size_t i, index, new_size, new_order; + struct hashtable_bucket *new_buckets; - free(hashtable->buckets); + new_order = hashtable->order + 1; + new_size = hashsize(new_order); - hashtable->num_buckets++; - new_size = num_buckets(hashtable); - - hashtable->buckets = malloc(new_size * sizeof(bucket_t)); - if(!hashtable->buckets) + new_buckets = jsonp_malloc(new_size * sizeof(bucket_t)); + if (!new_buckets) return -1; - for(i = 0; i < num_buckets(hashtable); i++) - { - hashtable->buckets[i].first = hashtable->buckets[i].last = - &hashtable->list; + jsonp_free(hashtable->buckets); + hashtable->buckets = new_buckets; + hashtable->order = new_order; + + for (i = 0; i < hashsize(hashtable->order); i++) { + hashtable->buckets[i].first = hashtable->buckets[i].last = &hashtable->list; } list = hashtable->list.next; list_init(&hashtable->list); - for(; list != &hashtable->list; list = next) { + for (; list != &hashtable->list; list = next) { next = list->next; pair = list_to_pair(list); index = pair->hash % new_size; @@ -182,190 +168,170 @@ static int hashtable_do_rehash(hashtable_t *hashtable) return 0; } +int hashtable_init(hashtable_t *hashtable) { + size_t i; -hashtable_t *hashtable_create(key_hash_fn hash_key, key_cmp_fn cmp_keys, - free_fn free_key, free_fn free_value) -{ - hashtable_t *hashtable = malloc(sizeof(hashtable_t)); - if(!hashtable) - return NULL; + hashtable->size = 0; + hashtable->order = INITIAL_HASHTABLE_ORDER; + hashtable->buckets = jsonp_malloc(hashsize(hashtable->order) * sizeof(bucket_t)); + if (!hashtable->buckets) + return -1; - if(hashtable_init(hashtable, hash_key, cmp_keys, free_key, free_value)) - { - free(hashtable); - return NULL; + list_init(&hashtable->list); + list_init(&hashtable->ordered_list); + + for (i = 0; i < hashsize(hashtable->order); i++) { + hashtable->buckets[i].first = hashtable->buckets[i].last = &hashtable->list; } - return hashtable; + return 0; } -void hashtable_destroy(hashtable_t *hashtable) -{ - hashtable_close(hashtable); - free(hashtable); +void hashtable_close(hashtable_t *hashtable) { + hashtable_do_clear(hashtable); + jsonp_free(hashtable->buckets); } -int hashtable_init(hashtable_t *hashtable, - key_hash_fn hash_key, key_cmp_fn cmp_keys, - free_fn free_key, free_fn free_value) -{ - size_t i; +static pair_t *init_pair(json_t *value, const char *key, size_t key_len, size_t hash) { + pair_t *pair; - hashtable->size = 0; - hashtable->num_buckets = 0; /* index to primes[] */ - hashtable->buckets = malloc(num_buckets(hashtable) * sizeof(bucket_t)); - if(!hashtable->buckets) - return -1; + /* offsetof(...) returns the size of pair_t without the last, + flexible member. This way, the correct amount is + allocated. */ - list_init(&hashtable->list); + if (key_len >= (size_t)-1 - offsetof(pair_t, key)) { + /* Avoid an overflow if the key is very long */ + return NULL; + } - hashtable->hash_key = hash_key; - hashtable->cmp_keys = cmp_keys; - hashtable->free_key = free_key; - hashtable->free_value = free_value; + pair = jsonp_malloc(offsetof(pair_t, key) + key_len + 1); - for(i = 0; i < num_buckets(hashtable); i++) - { - hashtable->buckets[i].first = hashtable->buckets[i].last = - &hashtable->list; - } + if (!pair) + return NULL; - return 0; -} + pair->hash = hash; + memcpy(pair->key, key, key_len); + pair->key[key_len] = '\0'; + pair->key_len = key_len; + pair->value = value; -void hashtable_close(hashtable_t *hashtable) -{ - hashtable_do_clear(hashtable); - free(hashtable->buckets); + list_init(&pair->list); + list_init(&pair->ordered_list); + + return pair; } -int hashtable_set(hashtable_t *hashtable, void *key, void *value) -{ +int hashtable_set(hashtable_t *hashtable, const char *key, size_t key_len, + json_t *value) { pair_t *pair; bucket_t *bucket; size_t hash, index; /* rehash if the load ratio exceeds 1 */ - if(hashtable->size >= num_buckets(hashtable)) - if(hashtable_do_rehash(hashtable)) + if (hashtable->size >= hashsize(hashtable->order)) + if (hashtable_do_rehash(hashtable)) return -1; - hash = hashtable->hash_key(key); - index = hash % num_buckets(hashtable); + hash = hash_str(key, key_len); + index = hash & hashmask(hashtable->order); bucket = &hashtable->buckets[index]; - pair = hashtable_find_pair(hashtable, bucket, key, hash); - - if(pair) - { - if(hashtable->free_key) - hashtable->free_key(key); - if(hashtable->free_value) - hashtable->free_value(pair->value); - pair->value = value; - } - else - { - pair = malloc(sizeof(pair_t)); - if(!pair) - return -1; + pair = hashtable_find_pair(hashtable, bucket, key, key_len, hash); - pair->key = key; + if (pair) { + json_decref(pair->value); pair->value = value; - pair->hash = hash; - list_init(&pair->list); + } else { + pair = init_pair(value, key, key_len, hash); + + if (!pair) + return -1; insert_to_bucket(hashtable, bucket, &pair->list); + list_insert(&hashtable->ordered_list, &pair->ordered_list); hashtable->size++; } return 0; } -void *hashtable_get(hashtable_t *hashtable, const void *key) -{ +void *hashtable_get(hashtable_t *hashtable, const char *key, size_t key_len) { pair_t *pair; size_t hash; bucket_t *bucket; - hash = hashtable->hash_key(key); - bucket = &hashtable->buckets[hash % num_buckets(hashtable)]; + hash = hash_str(key, key_len); + bucket = &hashtable->buckets[hash & hashmask(hashtable->order)]; - pair = hashtable_find_pair(hashtable, bucket, key, hash); - if(!pair) + pair = hashtable_find_pair(hashtable, bucket, key, key_len, hash); + if (!pair) return NULL; return pair->value; } -int hashtable_del(hashtable_t *hashtable, const void *key) -{ - size_t hash = hashtable->hash_key(key); - return hashtable_do_del(hashtable, key, hash); +int hashtable_del(hashtable_t *hashtable, const char *key, size_t key_len) { + size_t hash = hash_str(key, key_len); + return hashtable_do_del(hashtable, key, key_len, hash); } -void hashtable_clear(hashtable_t *hashtable) -{ +void hashtable_clear(hashtable_t *hashtable) { size_t i; hashtable_do_clear(hashtable); - for(i = 0; i < num_buckets(hashtable); i++) - { - hashtable->buckets[i].first = hashtable->buckets[i].last = - &hashtable->list; + for (i = 0; i < hashsize(hashtable->order); i++) { + hashtable->buckets[i].first = hashtable->buckets[i].last = &hashtable->list; } list_init(&hashtable->list); + list_init(&hashtable->ordered_list); hashtable->size = 0; } -void *hashtable_iter(hashtable_t *hashtable) -{ - return hashtable_iter_next(hashtable, &hashtable->list); +void *hashtable_iter(hashtable_t *hashtable) { + return hashtable_iter_next(hashtable, &hashtable->ordered_list); } -void *hashtable_iter_at(hashtable_t *hashtable, const void *key) -{ +void *hashtable_iter_at(hashtable_t *hashtable, const char *key, size_t key_len) { pair_t *pair; size_t hash; bucket_t *bucket; - hash = hashtable->hash_key(key); - bucket = &hashtable->buckets[hash % num_buckets(hashtable)]; + hash = hash_str(key, key_len); + bucket = &hashtable->buckets[hash & hashmask(hashtable->order)]; - pair = hashtable_find_pair(hashtable, bucket, key, hash); - if(!pair) + pair = hashtable_find_pair(hashtable, bucket, key, key_len, hash); + if (!pair) return NULL; - return &pair->list; + return &pair->ordered_list; } -void *hashtable_iter_next(hashtable_t *hashtable, void *iter) -{ +void *hashtable_iter_next(hashtable_t *hashtable, void *iter) { list_t *list = (list_t *)iter; - if(list->next == &hashtable->list) + if (list->next == &hashtable->ordered_list) return NULL; return list->next; } -void *hashtable_iter_key(void *iter) -{ - pair_t *pair = list_to_pair((list_t *)iter); +void *hashtable_iter_key(void *iter) { + pair_t *pair = ordered_list_to_pair((list_t *)iter); return pair->key; } -void *hashtable_iter_value(void *iter) -{ - pair_t *pair = list_to_pair((list_t *)iter); - return pair->value; +size_t hashtable_iter_key_len(void *iter) { + pair_t *pair = ordered_list_to_pair((list_t *)iter); + return pair->key_len; } -void hashtable_iter_set(hashtable_t *hashtable, void *iter, void *value) -{ - pair_t *pair = list_to_pair((list_t *)iter); +void *hashtable_iter_value(void *iter) { + pair_t *pair = ordered_list_to_pair((list_t *)iter); + return pair->value; +} - if(hashtable->free_value) - hashtable->free_value(pair->value); +void hashtable_iter_set(void *iter, json_t *value) { + pair_t *pair = ordered_list_to_pair((list_t *)iter); + json_decref(pair->value); pair->value = value; } diff --git a/src/jansson/src/hashtable.h b/src/jansson/src/hashtable.h index aba51348..03a1f5a9 100644 --- a/src/jansson/src/hashtable.h +++ b/src/jansson/src/hashtable.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2009, 2010 Petri Lehtinen + * Copyright (c) 2009-2016 Petri Lehtinen * * This library is free software; you can redistribute it and/or modify * it under the terms of the MIT license. See LICENSE for details. @@ -8,20 +8,24 @@ #ifndef HASHTABLE_H #define HASHTABLE_H -typedef size_t (*key_hash_fn)(const void *key); -typedef int (*key_cmp_fn)(const void *key1, const void *key2); -typedef void (*free_fn)(void *key); +#include "jansson.h" +#include struct hashtable_list { struct hashtable_list *prev; struct hashtable_list *next; }; +/* "pair" may be a bit confusing a name, but think of it as a + key-value pair. In this case, it just encodes some extra data, + too */ struct hashtable_pair { - void *key; - void *value; - size_t hash; struct hashtable_list list; + struct hashtable_list ordered_list; + size_t hash; + json_t *value; + size_t key_len; + char key[1]; }; struct hashtable_bucket { @@ -32,58 +36,25 @@ struct hashtable_bucket { typedef struct hashtable { size_t size; struct hashtable_bucket *buckets; - size_t num_buckets; /* index to primes[] */ + size_t order; /* hashtable has pow(2, order) buckets */ struct hashtable_list list; - - key_hash_fn hash_key; - key_cmp_fn cmp_keys; /* returns non-zero for equal keys */ - free_fn free_key; - free_fn free_value; + struct hashtable_list ordered_list; } hashtable_t; -/** - * hashtable_create - Create a hashtable object - * - * @hash_key: The key hashing function - * @cmp_keys: The key compare function. Returns non-zero for equal and - * zero for unequal unequal keys - * @free_key: If non-NULL, called for a key that is no longer referenced. - * @free_value: If non-NULL, called for a value that is no longer referenced. - * - * Returns a new hashtable object that should be freed with - * hashtable_destroy when it's no longer used, or NULL on failure (out - * of memory). - */ -hashtable_t *hashtable_create(key_hash_fn hash_key, key_cmp_fn cmp_keys, - free_fn free_key, free_fn free_value); - -/** - * hashtable_destroy - Destroy a hashtable object - * - * @hashtable: The hashtable - * - * Destroys a hashtable created with hashtable_create(). - */ -void hashtable_destroy(hashtable_t *hashtable); +#define hashtable_key_to_iter(key_) \ + (&(container_of(key_, struct hashtable_pair, key)->ordered_list)) /** * hashtable_init - Initialize a hashtable object * * @hashtable: The (statically allocated) hashtable object - * @hash_key: The key hashing function - * @cmp_keys: The key compare function. Returns non-zero for equal and - * zero for unequal unequal keys - * @free_key: If non-NULL, called for a key that is no longer referenced. - * @free_value: If non-NULL, called for a value that is no longer referenced. * * Initializes a statically allocated hashtable object. The object * should be cleared with hashtable_close when it's no longer used. * * Returns 0 on success, -1 on error (out of memory). */ -int hashtable_init(hashtable_t *hashtable, - key_hash_fn hash_key, key_cmp_fn cmp_keys, - free_fn free_key, free_fn free_value); +int hashtable_init(hashtable_t *hashtable) JANSSON_ATTRS((warn_unused_result)); /** * hashtable_close - Release all resources used by a hashtable object @@ -99,40 +70,40 @@ void hashtable_close(hashtable_t *hashtable); * * @hashtable: The hashtable object * @key: The key + * @key: The length of key + * @serial: For addition order of keys * @value: The value * * If a value with the given key already exists, its value is replaced - * with the new value. - * - * Key and value are "stealed" in the sense that hashtable frees them - * automatically when they are no longer used. The freeing is - * accomplished by calling free_key and free_value functions that were - * supplied to hashtable_new. In case one or both of the free - * functions is NULL, the corresponding item is not "stealed". + * with the new value. Value is "stealed" in the sense that hashtable + * doesn't increment its refcount but decreases the refcount when the + * value is no longer needed. * * Returns 0 on success, -1 on failure (out of memory). */ -int hashtable_set(hashtable_t *hashtable, void *key, void *value); +int hashtable_set(hashtable_t *hashtable, const char *key, size_t key_len, json_t *value); /** * hashtable_get - Get a value associated with a key * * @hashtable: The hashtable object * @key: The key + * @key: The length of key * * Returns value if it is found, or NULL otherwise. */ -void *hashtable_get(hashtable_t *hashtable, const void *key); +void *hashtable_get(hashtable_t *hashtable, const char *key, size_t key_len); /** * hashtable_del - Remove a value from the hashtable * * @hashtable: The hashtable object * @key: The key + * @key: The length of key * * Returns 0 on success, or -1 if the key was not found. */ -int hashtable_del(hashtable_t *hashtable, const void *key); +int hashtable_del(hashtable_t *hashtable, const char *key, size_t key_len); /** * hashtable_clear - Clear hashtable @@ -165,11 +136,12 @@ void *hashtable_iter(hashtable_t *hashtable); * * @hashtable: The hashtable object * @key: The key that the iterator should point to + * @key: The length of key * * Like hashtable_iter() but returns an iterator pointing to a * specific key. */ -void *hashtable_iter_at(hashtable_t *hashtable, const void *key); +void *hashtable_iter_at(hashtable_t *hashtable, const char *key, size_t key_len); /** * hashtable_iter_next - Advance an iterator @@ -189,6 +161,13 @@ void *hashtable_iter_next(hashtable_t *hashtable, void *iter); */ void *hashtable_iter_key(void *iter); +/** + * hashtable_iter_key_len - Retrieve the key length pointed by an iterator + * + * @iter: The iterator + */ +size_t hashtable_iter_key_len(void *iter); + /** * hashtable_iter_value - Retrieve the value pointed by an iterator * @@ -202,6 +181,6 @@ void *hashtable_iter_value(void *iter); * @iter: The iterator * @value: The value to set */ -void hashtable_iter_set(hashtable_t *hashtable, void *iter, void *value); +void hashtable_iter_set(void *iter, json_t *value); #endif diff --git a/src/jansson/src/hashtable_seed.c b/src/jansson/src/hashtable_seed.c new file mode 100644 index 00000000..37cc0729 --- /dev/null +++ b/src/jansson/src/hashtable_seed.c @@ -0,0 +1,274 @@ +/* Generate sizeof(uint32_t) bytes of as random data as possible to seed + the hash function. +*/ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include +#include + +#ifdef HAVE_FCNTL_H +#include +#endif + +#ifdef HAVE_SCHED_H +#include +#endif + +#ifdef HAVE_UNISTD_H +#include +#endif + +#ifdef HAVE_SYS_STAT_H +#include +#endif + +#ifdef HAVE_SYS_TIME_H +#include +#endif + +#ifdef HAVE_SYS_TYPES_H +#include +#endif + +#if defined(_WIN32) +/* For GetModuleHandle(), GetProcAddress() and GetCurrentProcessId() */ +#include +#endif + +#include "jansson.h" + +static uint32_t buf_to_uint32(char *data) { + size_t i; + uint32_t result = 0; + + for (i = 0; i < sizeof(uint32_t); i++) + result = (result << 8) | (unsigned char)data[i]; + + return result; +} + +/* /dev/urandom */ +#if !defined(_WIN32) && defined(USE_URANDOM) +static int seed_from_urandom(uint32_t *seed) { + /* Use unbuffered I/O if we have open(), close() and read(). Otherwise + fall back to fopen() */ + + char data[sizeof(uint32_t)]; + int ok; + +#if defined(HAVE_OPEN) && defined(HAVE_CLOSE) && defined(HAVE_READ) + int urandom; + urandom = open("/dev/urandom", O_RDONLY); + if (urandom == -1) + return 1; + + ok = read(urandom, data, sizeof(uint32_t)) == sizeof(uint32_t); + close(urandom); +#else + FILE *urandom; + + urandom = fopen("/dev/urandom", "rb"); + if (!urandom) + return 1; + + ok = fread(data, 1, sizeof(uint32_t), urandom) == sizeof(uint32_t); + fclose(urandom); +#endif + + if (!ok) + return 1; + + *seed = buf_to_uint32(data); + return 0; +} +#endif + +/* Windows Crypto API */ +#if defined(_WIN32) && defined(USE_WINDOWS_CRYPTOAPI) +#include + +typedef BOOL(WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv, LPCSTR pszContainer, + LPCSTR pszProvider, DWORD dwProvType, + DWORD dwFlags); +typedef BOOL(WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen, BYTE *pbBuffer); +typedef BOOL(WINAPI *CRYPTRELEASECONTEXT)(HCRYPTPROV hProv, DWORD dwFlags); + +static int seed_from_windows_cryptoapi(uint32_t *seed) { + HINSTANCE hAdvAPI32 = NULL; + CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL; + CRYPTGENRANDOM pCryptGenRandom = NULL; + CRYPTRELEASECONTEXT pCryptReleaseContext = NULL; + HCRYPTPROV hCryptProv = 0; + BYTE data[sizeof(uint32_t)]; + int ok; + + hAdvAPI32 = GetModuleHandle(TEXT("advapi32.dll")); + if (hAdvAPI32 == NULL) + return 1; + + pCryptAcquireContext = + (CRYPTACQUIRECONTEXTA)GetProcAddress(hAdvAPI32, "CryptAcquireContextA"); + if (!pCryptAcquireContext) + return 1; + + pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(hAdvAPI32, "CryptGenRandom"); + if (!pCryptGenRandom) + return 1; + + pCryptReleaseContext = + (CRYPTRELEASECONTEXT)GetProcAddress(hAdvAPI32, "CryptReleaseContext"); + if (!pCryptReleaseContext) + return 1; + + if (!pCryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, + CRYPT_VERIFYCONTEXT)) + return 1; + + ok = pCryptGenRandom(hCryptProv, sizeof(uint32_t), data); + pCryptReleaseContext(hCryptProv, 0); + + if (!ok) + return 1; + + *seed = buf_to_uint32((char *)data); + return 0; +} +#endif + +/* gettimeofday() and getpid() */ +static int seed_from_timestamp_and_pid(uint32_t *seed) { +#ifdef HAVE_GETTIMEOFDAY + /* XOR of seconds and microseconds */ + struct timeval tv; + gettimeofday(&tv, NULL); + *seed = (uint32_t)tv.tv_sec ^ (uint32_t)tv.tv_usec; +#else + /* Seconds only */ + *seed = (uint32_t)time(NULL); +#endif + + /* XOR with PID for more randomness */ +#if defined(_WIN32) + *seed ^= (uint32_t)GetCurrentProcessId(); +#elif defined(HAVE_GETPID) + *seed ^= (uint32_t)getpid(); +#endif + + return 0; +} + +static uint32_t generate_seed() { + uint32_t seed = 0; + int done = 0; + +#if !defined(_WIN32) && defined(USE_URANDOM) + if (seed_from_urandom(&seed) == 0) + done = 1; +#endif + +#if defined(_WIN32) && defined(USE_WINDOWS_CRYPTOAPI) + if (seed_from_windows_cryptoapi(&seed) == 0) + done = 1; +#endif + + if (!done) { + /* Fall back to timestamp and PID if no better randomness is + available */ + seed_from_timestamp_and_pid(&seed); + } + + /* Make sure the seed is never zero */ + if (seed == 0) + seed = 1; + + return seed; +} + +volatile uint32_t hashtable_seed = 0; + +#if defined(HAVE_ATOMIC_BUILTINS) && (defined(HAVE_SCHED_YIELD) || !defined(_WIN32)) +static volatile char seed_initialized = 0; + +void json_object_seed(size_t seed) { + uint32_t new_seed = (uint32_t)seed; + + if (hashtable_seed == 0) { + if (__atomic_test_and_set(&seed_initialized, __ATOMIC_RELAXED) == 0) { + /* Do the seeding ourselves */ + if (new_seed == 0) + new_seed = generate_seed(); + + __atomic_store_n(&hashtable_seed, new_seed, __ATOMIC_RELEASE); + } else { + /* Wait for another thread to do the seeding */ + do { +#ifdef HAVE_SCHED_YIELD + sched_yield(); +#endif + } while (__atomic_load_n(&hashtable_seed, __ATOMIC_ACQUIRE) == 0); + } + } +} +#elif defined(HAVE_SYNC_BUILTINS) && (defined(HAVE_SCHED_YIELD) || !defined(_WIN32)) +void json_object_seed(size_t seed) { + uint32_t new_seed = (uint32_t)seed; + + if (hashtable_seed == 0) { + if (new_seed == 0) { + /* Explicit synchronization fences are not supported by the + __sync builtins, so every thread getting here has to + generate the seed value. + */ + new_seed = generate_seed(); + } + + do { + if (__sync_bool_compare_and_swap(&hashtable_seed, 0, new_seed)) { + /* We were the first to seed */ + break; + } else { + /* Wait for another thread to do the seeding */ +#ifdef HAVE_SCHED_YIELD + sched_yield(); +#endif + } + } while (hashtable_seed == 0); + } +} +#elif defined(_WIN32) +static long seed_initialized = 0; +void json_object_seed(size_t seed) { + uint32_t new_seed = (uint32_t)seed; + + if (hashtable_seed == 0) { + if (InterlockedIncrement(&seed_initialized) == 1) { + /* Do the seeding ourselves */ + if (new_seed == 0) + new_seed = generate_seed(); + + hashtable_seed = new_seed; + } else { + /* Wait for another thread to do the seeding */ + do { + SwitchToThread(); + } while (hashtable_seed == 0); + } + } +} +#else +/* Fall back to a thread-unsafe version */ +void json_object_seed(size_t seed) { + uint32_t new_seed = (uint32_t)seed; + + if (hashtable_seed == 0) { + if (new_seed == 0) + new_seed = generate_seed(); + + hashtable_seed = new_seed; + } +} +#endif diff --git a/src/jansson/src/jansson.h b/src/jansson/src/jansson.h index 1e6fe6ea..391c85e9 100644 --- a/src/jansson/src/jansson.h +++ b/src/jansson/src/jansson.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2009, 2010 Petri Lehtinen + * Copyright (c) 2009-2016 Petri Lehtinen * * Jansson is free software; you can redistribute it and/or modify * it under the terms of the MIT license. See LICENSE for details. @@ -8,9 +8,11 @@ #ifndef JANSSON_H #define JANSSON_H +#include #include -#include /* for size_t */ -#include +#include /* for size_t */ + +#include "jansson_config.h" #ifdef __cplusplus extern "C" { @@ -18,19 +20,30 @@ extern "C" { /* version */ -#define JANSSON_MAJOR_VERSION 1 -#define JANSSON_MINOR_VERSION 3 -#define JANSSON_MICRO_VERSION 0 +#define JANSSON_MAJOR_VERSION 2 +#define JANSSON_MINOR_VERSION 14 +#define JANSSON_MICRO_VERSION 0 /* Micro version is omitted if it's 0 */ -#define JANSSON_VERSION "1.3" +#define JANSSON_VERSION "2.14" /* Version as a 3-byte hex number, e.g. 0x010201 == 1.2.1. Use this for numeric comparisons, e.g. #if JANSSON_VERSION_HEX >= ... */ -#define JANSSON_VERSION_HEX ((JANSSON_MAJOR_VERSION << 16) | \ - (JANSSON_MINOR_VERSION << 8) | \ - (JANSSON_MICRO_VERSION << 0))) +#define JANSSON_VERSION_HEX \ + ((JANSSON_MAJOR_VERSION << 16) | (JANSSON_MINOR_VERSION << 8) | \ + (JANSSON_MICRO_VERSION << 0)) + +/* If __atomic or __sync builtins are available the library is thread + * safe for all read-only functions plus reference counting. */ +#if JSON_HAVE_ATOMIC_BUILTINS || JSON_HAVE_SYNC_BUILTINS +#define JANSSON_THREAD_SAFE_REFCOUNT 1 +#endif +#if defined(__GNUC__) || defined(__clang__) +#define JANSSON_ATTRS(x) __attribute__(x) +#else +#define JANSSON_ATTRS(x) +#endif /* types */ @@ -45,111 +58,237 @@ typedef enum { JSON_NULL } json_type; -typedef struct { +typedef struct json_t { json_type type; - size_t refcount; + volatile size_t refcount; } json_t; +#ifndef JANSSON_USING_CMAKE /* disabled if using cmake */ #if JSON_INTEGER_IS_LONG_LONG +#ifdef _WIN32 +#define JSON_INTEGER_FORMAT "I64d" +#else #define JSON_INTEGER_FORMAT "lld" +#endif typedef long long json_int_t; #else #define JSON_INTEGER_FORMAT "ld" typedef long json_int_t; #endif /* JSON_INTEGER_IS_LONG_LONG */ +#endif -#define json_typeof(json) ((json)->type) -#define json_is_object(json) (json && json_typeof(json) == JSON_OBJECT) -#define json_is_array(json) (json && json_typeof(json) == JSON_ARRAY) -#define json_is_string(json) (json && json_typeof(json) == JSON_STRING) -#define json_is_integer(json) (json && json_typeof(json) == JSON_INTEGER) -#define json_is_real(json) (json && json_typeof(json) == JSON_REAL) -#define json_is_number(json) (json_is_integer(json) || json_is_real(json)) -#define json_is_true(json) (json && json_typeof(json) == JSON_TRUE) -#define json_is_false(json) (json && json_typeof(json) == JSON_FALSE) -#define json_is_boolean(json) (json_is_true(json) || json_is_false(json)) -#define json_is_null(json) (json && json_typeof(json) == JSON_NULL) +#define json_typeof(json) ((json)->type) +#define json_is_object(json) ((json) && json_typeof(json) == JSON_OBJECT) +#define json_is_array(json) ((json) && json_typeof(json) == JSON_ARRAY) +#define json_is_string(json) ((json) && json_typeof(json) == JSON_STRING) +#define json_is_integer(json) ((json) && json_typeof(json) == JSON_INTEGER) +#define json_is_real(json) ((json) && json_typeof(json) == JSON_REAL) +#define json_is_number(json) (json_is_integer(json) || json_is_real(json)) +#define json_is_true(json) ((json) && json_typeof(json) == JSON_TRUE) +#define json_is_false(json) ((json) && json_typeof(json) == JSON_FALSE) +#define json_boolean_value json_is_true +#define json_is_boolean(json) (json_is_true(json) || json_is_false(json)) +#define json_is_null(json) ((json) && json_typeof(json) == JSON_NULL) /* construction, destruction, reference counting */ json_t *json_object(void); json_t *json_array(void); json_t *json_string(const char *value); +json_t *json_stringn(const char *value, size_t len); json_t *json_string_nocheck(const char *value); +json_t *json_stringn_nocheck(const char *value, size_t len); json_t *json_integer(json_int_t value); json_t *json_real(double value); json_t *json_true(void); json_t *json_false(void); +#define json_boolean(val) ((val) ? json_true() : json_false()) json_t *json_null(void); -static JSON_INLINE -json_t *json_incref(json_t *json) -{ - if(json && json->refcount != (size_t)-1) - ++json->refcount; +/* do not call JSON_INTERNAL_INCREF or JSON_INTERNAL_DECREF directly */ +#if JSON_HAVE_ATOMIC_BUILTINS +#define JSON_INTERNAL_INCREF(json) \ + __atomic_add_fetch(&json->refcount, 1, __ATOMIC_ACQUIRE) +#define JSON_INTERNAL_DECREF(json) \ + __atomic_sub_fetch(&json->refcount, 1, __ATOMIC_RELEASE) +#elif JSON_HAVE_SYNC_BUILTINS +#define JSON_INTERNAL_INCREF(json) __sync_add_and_fetch(&json->refcount, 1) +#define JSON_INTERNAL_DECREF(json) __sync_sub_and_fetch(&json->refcount, 1) +#else +#define JSON_INTERNAL_INCREF(json) (++json->refcount) +#define JSON_INTERNAL_DECREF(json) (--json->refcount) +#endif + +static JSON_INLINE json_t *json_incref(json_t *json) { + if (json && json->refcount != (size_t)-1) + JSON_INTERNAL_INCREF(json); return json; } /* do not call json_delete directly */ void json_delete(json_t *json); -static JSON_INLINE -void json_decref(json_t *json) -{ - if(json && json->refcount != (size_t)-1 && --json->refcount == 0) +static JSON_INLINE void json_decref(json_t *json) { + if (json && json->refcount != (size_t)-1 && JSON_INTERNAL_DECREF(json) == 0) json_delete(json); } +#if defined(__GNUC__) || defined(__clang__) +static JSON_INLINE void json_decrefp(json_t **json) { + if (json) { + json_decref(*json); + *json = NULL; + } +} + +#define json_auto_t json_t __attribute__((cleanup(json_decrefp))) +#endif /* error reporting */ -#define JSON_ERROR_TEXT_LENGTH 160 -#define JSON_ERROR_SOURCE_LENGTH 80 +#define JSON_ERROR_TEXT_LENGTH 160 +#define JSON_ERROR_SOURCE_LENGTH 80 -typedef struct { - char text[JSON_ERROR_TEXT_LENGTH]; +typedef struct json_error_t { int line; int column; + int position; char source[JSON_ERROR_SOURCE_LENGTH]; + char text[JSON_ERROR_TEXT_LENGTH]; } json_error_t; +enum json_error_code { + json_error_unknown, + json_error_out_of_memory, + json_error_stack_overflow, + json_error_cannot_open_file, + json_error_invalid_argument, + json_error_invalid_utf8, + json_error_premature_end_of_input, + json_error_end_of_input_expected, + json_error_invalid_syntax, + json_error_invalid_format, + json_error_wrong_type, + json_error_null_character, + json_error_null_value, + json_error_null_byte_in_key, + json_error_duplicate_key, + json_error_numeric_overflow, + json_error_item_not_found, + json_error_index_out_of_range +}; + +static JSON_INLINE enum json_error_code json_error_code(const json_error_t *e) { + return (enum json_error_code)e->text[JSON_ERROR_TEXT_LENGTH - 1]; +} /* getters, setters, manipulation */ +void json_object_seed(size_t seed); size_t json_object_size(const json_t *object); -json_t *json_object_get(const json_t *object, const char *key); +json_t *json_object_get(const json_t *object, const char *key) + JANSSON_ATTRS((warn_unused_result)); +json_t *json_object_getn(const json_t *object, const char *key, size_t key_len) + JANSSON_ATTRS((warn_unused_result)); int json_object_set_new(json_t *object, const char *key, json_t *value); +int json_object_setn_new(json_t *object, const char *key, size_t key_len, json_t *value); int json_object_set_new_nocheck(json_t *object, const char *key, json_t *value); +int json_object_setn_new_nocheck(json_t *object, const char *key, size_t key_len, + json_t *value); int json_object_del(json_t *object, const char *key); +int json_object_deln(json_t *object, const char *key, size_t key_len); int json_object_clear(json_t *object); int json_object_update(json_t *object, json_t *other); +int json_object_update_existing(json_t *object, json_t *other); +int json_object_update_missing(json_t *object, json_t *other); +int json_object_update_recursive(json_t *object, json_t *other); void *json_object_iter(json_t *object); void *json_object_iter_at(json_t *object, const char *key); +void *json_object_key_to_iter(const char *key); void *json_object_iter_next(json_t *object, void *iter); const char *json_object_iter_key(void *iter); +size_t json_object_iter_key_len(void *iter); json_t *json_object_iter_value(void *iter); int json_object_iter_set_new(json_t *object, void *iter, json_t *value); -static JSON_INLINE -int json_object_set(json_t *object, const char *key, json_t *value) -{ +#define json_object_foreach(object, key, value) \ + for (key = json_object_iter_key(json_object_iter(object)); \ + key && (value = json_object_iter_value(json_object_key_to_iter(key))); \ + key = json_object_iter_key( \ + json_object_iter_next(object, json_object_key_to_iter(key)))) + +#define json_object_keylen_foreach(object, key, key_len, value) \ + for (key = json_object_iter_key(json_object_iter(object)), \ + key_len = json_object_iter_key_len(json_object_key_to_iter(key)); \ + key && (value = json_object_iter_value(json_object_key_to_iter(key))); \ + key = json_object_iter_key( \ + json_object_iter_next(object, json_object_key_to_iter(key))), \ + key_len = json_object_iter_key_len(json_object_key_to_iter(key))) + +#define json_object_foreach_safe(object, n, key, value) \ + for (key = json_object_iter_key(json_object_iter(object)), \ + n = json_object_iter_next(object, json_object_key_to_iter(key)); \ + key && (value = json_object_iter_value(json_object_key_to_iter(key))); \ + key = json_object_iter_key(n), \ + n = json_object_iter_next(object, json_object_key_to_iter(key))) + +#define json_object_keylen_foreach_safe(object, n, key, key_len, value) \ + for (key = json_object_iter_key(json_object_iter(object)), \ + n = json_object_iter_next(object, json_object_key_to_iter(key)), \ + key_len = json_object_iter_key_len(json_object_key_to_iter(key)); \ + key && (value = json_object_iter_value(json_object_key_to_iter(key))); \ + key = json_object_iter_key(n), key_len = json_object_iter_key_len(n), \ + n = json_object_iter_next(object, json_object_key_to_iter(key))) + +#define json_array_foreach(array, index, value) \ + for (index = 0; \ + index < json_array_size(array) && (value = json_array_get(array, index)); \ + index++) + +static JSON_INLINE int json_object_set(json_t *object, const char *key, json_t *value) { return json_object_set_new(object, key, json_incref(value)); } -static JSON_INLINE -int json_object_set_nocheck(json_t *object, const char *key, json_t *value) -{ +static JSON_INLINE int json_object_setn(json_t *object, const char *key, size_t key_len, + json_t *value) { + return json_object_setn_new(object, key, key_len, json_incref(value)); +} + +static JSON_INLINE int json_object_set_nocheck(json_t *object, const char *key, + json_t *value) { return json_object_set_new_nocheck(object, key, json_incref(value)); } -static JSON_INLINE -int json_object_iter_set(json_t *object, void *iter, json_t *value) -{ +static JSON_INLINE int json_object_setn_nocheck(json_t *object, const char *key, + size_t key_len, json_t *value) { + return json_object_setn_new_nocheck(object, key, key_len, json_incref(value)); +} + +static JSON_INLINE int json_object_iter_set(json_t *object, void *iter, json_t *value) { return json_object_iter_set_new(object, iter, json_incref(value)); } +static JSON_INLINE int json_object_update_new(json_t *object, json_t *other) { + int ret = json_object_update(object, other); + json_decref(other); + return ret; +} + +static JSON_INLINE int json_object_update_existing_new(json_t *object, json_t *other) { + int ret = json_object_update_existing(object, other); + json_decref(other); + return ret; +} + +static JSON_INLINE int json_object_update_missing_new(json_t *object, json_t *other) { + int ret = json_object_update_missing(object, other); + json_decref(other); + return ret; +} + size_t json_array_size(const json_t *array); -json_t *json_array_get(const json_t *array, size_t index); +json_t *json_array_get(const json_t *array, size_t index) + JANSSON_ATTRS((warn_unused_result)); int json_array_set_new(json_t *array, size_t index, json_t *value); int json_array_append_new(json_t *array, json_t *value); int json_array_insert_new(json_t *array, size_t index, json_t *value); @@ -157,63 +296,121 @@ int json_array_remove(json_t *array, size_t index); int json_array_clear(json_t *array); int json_array_extend(json_t *array, json_t *other); -static JSON_INLINE -int json_array_set(json_t *array, size_t index, json_t *value) -{ - return json_array_set_new(array, index, json_incref(value)); +static JSON_INLINE int json_array_set(json_t *array, size_t ind, json_t *value) { + return json_array_set_new(array, ind, json_incref(value)); } -static JSON_INLINE -int json_array_append(json_t *array, json_t *value) -{ +static JSON_INLINE int json_array_append(json_t *array, json_t *value) { return json_array_append_new(array, json_incref(value)); } -static JSON_INLINE -int json_array_insert(json_t *array, size_t index, json_t *value) -{ - return json_array_insert_new(array, index, json_incref(value)); +static JSON_INLINE int json_array_insert(json_t *array, size_t ind, json_t *value) { + return json_array_insert_new(array, ind, json_incref(value)); } const char *json_string_value(const json_t *string); +size_t json_string_length(const json_t *string); json_int_t json_integer_value(const json_t *integer); double json_real_value(const json_t *real); double json_number_value(const json_t *json); int json_string_set(json_t *string, const char *value); +int json_string_setn(json_t *string, const char *value, size_t len); int json_string_set_nocheck(json_t *string, const char *value); +int json_string_setn_nocheck(json_t *string, const char *value, size_t len); int json_integer_set(json_t *integer, json_int_t value); int json_real_set(json_t *real, double value); -json_t *json_pack(json_error_t *error, const char *fmt, ...); -int json_unpack(json_t *root, json_error_t *error, const char *fmt, ...); - -/* equality */ +/* pack, unpack */ -int json_equal(json_t *value1, json_t *value2); +json_t *json_pack(const char *fmt, ...) JANSSON_ATTRS((warn_unused_result)); +json_t *json_pack_ex(json_error_t *error, size_t flags, const char *fmt, ...) + JANSSON_ATTRS((warn_unused_result)); +json_t *json_vpack_ex(json_error_t *error, size_t flags, const char *fmt, va_list ap) + JANSSON_ATTRS((warn_unused_result)); +#define JSON_VALIDATE_ONLY 0x1 +#define JSON_STRICT 0x2 -/* copying */ +int json_unpack(json_t *root, const char *fmt, ...); +int json_unpack_ex(json_t *root, json_error_t *error, size_t flags, const char *fmt, ...); +int json_vunpack_ex(json_t *root, json_error_t *error, size_t flags, const char *fmt, + va_list ap); -json_t *json_copy(json_t *value); -json_t *json_deep_copy(json_t *value); +/* sprintf */ +json_t *json_sprintf(const char *fmt, ...) + JANSSON_ATTRS((warn_unused_result, format(printf, 1, 2))); +json_t *json_vsprintf(const char *fmt, va_list ap) + JANSSON_ATTRS((warn_unused_result, format(printf, 1, 0))); -/* loading, printing */ +/* equality */ -json_t *json_loads(const char *input, size_t flags, json_error_t *error); -json_t *json_loadf(FILE *input, size_t flags, json_error_t *error); -json_t *json_load_file(const char *path, size_t flags, json_error_t *error); +int json_equal(const json_t *value1, const json_t *value2); -#define JSON_INDENT(n) (n & 0x1F) -#define JSON_COMPACT 0x20 -#define JSON_ENSURE_ASCII 0x40 -#define JSON_SORT_KEYS 0x80 -#define JSON_PRESERVE_ORDER 0x100 +/* copying */ -char *json_dumps(const json_t *json, size_t flags); +json_t *json_copy(json_t *value) JANSSON_ATTRS((warn_unused_result)); +json_t *json_deep_copy(const json_t *value) JANSSON_ATTRS((warn_unused_result)); + +/* decoding */ + +#define JSON_REJECT_DUPLICATES 0x1 +#define JSON_DISABLE_EOF_CHECK 0x2 +#define JSON_DECODE_ANY 0x4 +#define JSON_DECODE_INT_AS_REAL 0x8 +#define JSON_ALLOW_NUL 0x10 + +typedef size_t (*json_load_callback_t)(void *buffer, size_t buflen, void *data); + +json_t *json_loads(const char *input, size_t flags, json_error_t *error) + JANSSON_ATTRS((warn_unused_result)); +json_t *json_loadb(const char *buffer, size_t buflen, size_t flags, json_error_t *error) + JANSSON_ATTRS((warn_unused_result)); +json_t *json_loadf(FILE *input, size_t flags, json_error_t *error) + JANSSON_ATTRS((warn_unused_result)); +json_t *json_loadfd(int input, size_t flags, json_error_t *error) + JANSSON_ATTRS((warn_unused_result)); +json_t *json_load_file(const char *path, size_t flags, json_error_t *error) + JANSSON_ATTRS((warn_unused_result)); +json_t *json_load_callback(json_load_callback_t callback, void *data, size_t flags, + json_error_t *error) JANSSON_ATTRS((warn_unused_result)); + +/* encoding */ + +#define JSON_MAX_INDENT 0x1F +#define JSON_INDENT(n) ((n)&JSON_MAX_INDENT) +#define JSON_COMPACT 0x20 +#define JSON_ENSURE_ASCII 0x40 +#define JSON_SORT_KEYS 0x80 +#define JSON_PRESERVE_ORDER 0x100 +#define JSON_ENCODE_ANY 0x200 +#define JSON_ESCAPE_SLASH 0x400 +#define JSON_REAL_PRECISION(n) (((n)&0x1F) << 11) +#define JSON_EMBED 0x10000 + +typedef int (*json_dump_callback_t)(const char *buffer, size_t size, void *data); + +char *json_dumps(const json_t *json, size_t flags) JANSSON_ATTRS((warn_unused_result)); +size_t json_dumpb(const json_t *json, char *buffer, size_t size, size_t flags); int json_dumpf(const json_t *json, FILE *output, size_t flags); +int json_dumpfd(const json_t *json, int output, size_t flags); int json_dump_file(const json_t *json, const char *path, size_t flags); +int json_dump_callback(const json_t *json, json_dump_callback_t callback, void *data, + size_t flags); + +/* custom memory allocation */ + +typedef void *(*json_malloc_t)(size_t); +typedef void (*json_free_t)(void *); + +void json_set_alloc_funcs(json_malloc_t malloc_fn, json_free_t free_fn); +void json_get_alloc_funcs(json_malloc_t *malloc_fn, json_free_t *free_fn); + +/* runtime version checking */ + +const char *jansson_version_str(void); +int jansson_version_cmp(int major, int minor, int micro); #ifdef __cplusplus } diff --git a/src/jansson/src/jansson_config.h b/src/jansson/src/jansson_config.h index ff0f43fe..b34baa55 100644 --- a/src/jansson/src/jansson_config.h +++ b/src/jansson/src/jansson_config.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010 Petri Lehtinen + * Copyright (c) 2010-2016 Petri Lehtinen * * Jansson is free software; you can redistribute it and/or modify * it under the terms of the MIT license. See LICENSE for details. @@ -27,8 +27,25 @@ #define JSON_INLINE inline #endif -/* If your compiler supports the `long long` type, - JSON_INTEGER_IS_LONG_LONG is defined to 1, otherwise to 0. */ +/* If your compiler supports the `long long` type and the strtoll() + library function, JSON_INTEGER_IS_LONG_LONG is defined to 1, + otherwise to 0. */ #define JSON_INTEGER_IS_LONG_LONG 1 +/* If locale.h and localeconv() are available, define to 1, + otherwise to 0. */ +#define JSON_HAVE_LOCALECONV 1 + +/* If __atomic builtins are available they will be used to manage + reference counts of json_t. */ +#define JSON_HAVE_ATOMIC_BUILTINS 1 + +/* If __atomic builtins are not available we try using __sync builtins + to manage reference counts of json_t. */ +#define JSON_HAVE_SYNC_BUILTINS 1 + +/* Maximum recursion depth for parsing JSON input. + This limits the depth of e.g. array-within-array constructions. */ +#define JSON_PARSER_MAX_DEPTH 2048 + #endif diff --git a/src/jansson/src/jansson_config.h.in b/src/jansson/src/jansson_config.h.in index d2a93926..fe692ab4 100644 --- a/src/jansson/src/jansson_config.h.in +++ b/src/jansson/src/jansson_config.h.in @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010 Petri Lehtinen + * Copyright (c) 2010-2016 Petri Lehtinen * * Jansson is free software; you can redistribute it and/or modify * it under the terms of the MIT license. See LICENSE for details. @@ -27,8 +27,25 @@ #define JSON_INLINE @json_inline@ #endif -/* If your compiler supports the `long long` type, - JSON_INTEGER_IS_LONG_LONG is defined to 1, otherwise to 0. */ +/* If your compiler supports the `long long` type and the strtoll() + library function, JSON_INTEGER_IS_LONG_LONG is defined to 1, + otherwise to 0. */ #define JSON_INTEGER_IS_LONG_LONG @json_have_long_long@ +/* If locale.h and localeconv() are available, define to 1, + otherwise to 0. */ +#define JSON_HAVE_LOCALECONV @json_have_localeconv@ + +/* If __atomic builtins are available they will be used to manage + reference counts of json_t. */ +#define JSON_HAVE_ATOMIC_BUILTINS @json_have_atomic_builtins@ + +/* If __atomic builtins are not available we try using __sync builtins + to manage reference counts of json_t. */ +#define JSON_HAVE_SYNC_BUILTINS @json_have_sync_builtins@ + +/* Maximum recursion depth for parsing JSON input. + This limits the depth of e.g. array-within-array constructions. */ +#define JSON_PARSER_MAX_DEPTH 2048 + #endif diff --git a/src/jansson/src/jansson_private.h b/src/jansson/src/jansson_private.h index 951780c6..ea2593cd 100644 --- a/src/jansson/src/jansson_private.h +++ b/src/jansson/src/jansson_private.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2009, 2010 Petri Lehtinen + * Copyright (c) 2009-2016 Petri Lehtinen * * Jansson is free software; you can redistribute it and/or modify * it under the terms of the MIT license. See LICENSE for details. @@ -8,23 +8,33 @@ #ifndef JANSSON_PRIVATE_H #define JANSSON_PRIVATE_H -#include -#include "jansson.h" #include "hashtable.h" +#include "jansson.h" +#include "jansson_private_config.h" +#include "strbuffer.h" +#include -#define container_of(ptr_, type_, member_) \ +#define container_of(ptr_, type_, member_) \ ((type_ *)((char *)ptr_ - offsetof(type_, member_))) /* On some platforms, max() may already be defined */ #ifndef max -#define max(a, b) ((a) > (b) ? (a) : (b)) +#define max(a, b) ((a) > (b) ? (a) : (b)) +#endif + +/* va_copy is a C99 feature. In C89 implementations, it's sometimes + available as __va_copy. If not, memcpy() should do the trick. */ +#ifndef va_copy +#ifdef __va_copy +#define va_copy __va_copy +#else +#define va_copy(a, b) memcpy(&(a), &(b), sizeof(va_list)) +#endif #endif typedef struct { json_t json; hashtable_t hashtable; - size_t serial; - int visited; } json_object_t; typedef struct { @@ -32,12 +42,12 @@ typedef struct { size_t size; size_t entries; json_t **table; - int visited; } json_array_t; typedef struct { json_t json; char *value; + size_t length; } json_string_t; typedef struct { @@ -53,18 +63,52 @@ typedef struct { #define json_to_object(json_) container_of(json_, json_object_t, json) #define json_to_array(json_) container_of(json_, json_array_t, json) #define json_to_string(json_) container_of(json_, json_string_t, json) -#define json_to_real(json_) container_of(json_, json_real_t, json) +#define json_to_real(json_) container_of(json_, json_real_t, json) #define json_to_integer(json_) container_of(json_, json_integer_t, json) -typedef struct { - size_t serial; - char key[1]; -} object_key_t; - -const object_key_t *jsonp_object_iter_fullkey(void *iter); +/* Create a string by taking ownership of an existing buffer */ +json_t *jsonp_stringn_nocheck_own(const char *value, size_t len); +/* Error message formatting */ void jsonp_error_init(json_error_t *error, const char *source); -void jsonp_error_set(json_error_t *error, int line, int column, - const char *msg, ...); +void jsonp_error_set_source(json_error_t *error, const char *source); +void jsonp_error_set(json_error_t *error, int line, int column, size_t position, + enum json_error_code code, const char *msg, ...); +void jsonp_error_vset(json_error_t *error, int line, int column, size_t position, + enum json_error_code code, const char *msg, va_list ap); + +/* Locale independent string<->double conversions */ +int jsonp_strtod(strbuffer_t *strbuffer, double *out); +int jsonp_dtostr(char *buffer, size_t size, double value, int prec); + +/* Wrappers for custom memory functions */ +void *jsonp_malloc(size_t size) JANSSON_ATTRS((warn_unused_result)); +void jsonp_free(void *ptr); +char *jsonp_strndup(const char *str, size_t length) JANSSON_ATTRS((warn_unused_result)); +char *jsonp_strdup(const char *str) JANSSON_ATTRS((warn_unused_result)); +char *jsonp_strndup(const char *str, size_t len) JANSSON_ATTRS((warn_unused_result)); + +/* Circular reference check*/ +/* Space for "0x", double the sizeof a pointer for the hex and a terminator. */ +#define LOOP_KEY_LEN (2 + (sizeof(json_t *) * 2) + 1) +int jsonp_loop_check(hashtable_t *parents, const json_t *json, char *key, size_t key_size, + size_t *key_len_out); + +/* Windows compatibility */ +#if defined(_WIN32) || defined(WIN32) +#if defined(_MSC_VER) /* MS compiller */ +#if (_MSC_VER < 1900) && \ + !defined(snprintf) /* snprintf not defined yet & not introduced */ +#define snprintf _snprintf +#endif +#if (_MSC_VER < 1500) && \ + !defined(vsnprintf) /* vsnprintf not defined yet & not introduced */ +#define vsnprintf(b, c, f, a) _vsnprintf(b, c, f, a) +#endif +#else /* Other Windows compiller, old definition */ +#define snprintf _snprintf +#define vsnprintf _vsnprintf +#endif +#endif #endif diff --git a/src/jansson/src/jansson_private_config.h b/src/jansson/src/jansson_private_config.h new file mode 100644 index 00000000..acbb36ad --- /dev/null +++ b/src/jansson/src/jansson_private_config.h @@ -0,0 +1,159 @@ +/* jansson_private_config.h. Generated from jansson_private_config.h.in by configure. */ +/* jansson_private_config.h.in. Generated from configure.ac by autoheader. */ + +/* Define to 1 if gcc's __atomic builtins are available */ +#define HAVE_ATOMIC_BUILTINS 1 + +/* Define to 1 if you have the `close' function. */ +#define HAVE_CLOSE 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_DLFCN_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_ENDIAN_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_FCNTL_H 1 + +/* Define to 1 if you have the `getpid' function. */ +#define HAVE_GETPID 1 + +/* Define to 1 if you have the `gettimeofday' function. */ +#define HAVE_GETTIMEOFDAY 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_INTTYPES_H 1 + +/* Define to 1 if you have the `localeconv' function. */ +#define HAVE_LOCALECONV 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_LOCALE_H 1 + +/* Define to 1 if the system has the type `long long int'. */ +#define HAVE_LONG_LONG_INT 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_MEMORY_H 1 + +/* Define to 1 if you have the `open' function. */ +#define HAVE_OPEN 1 + +/* Define to 1 if you have the `read' function. */ +#define HAVE_READ 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_SCHED_H 1 + +/* Define to 1 if you have the `sched_yield' function. */ +#define HAVE_SCHED_YIELD 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_STDINT_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_STDLIB_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_STRINGS_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_STRING_H 1 + +/* Define to 1 if you have the `strtoll' function. */ +#define HAVE_STRTOLL 1 + +/* Define to 1 if gcc's __sync builtins are available */ +#define HAVE_SYNC_BUILTINS 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_PARAM_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_STAT_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_TIME_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_TYPES_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_UNISTD_H 1 + +/* Define to 1 if the system has the type `unsigned long long int'. */ +#define HAVE_UNSIGNED_LONG_LONG_INT 1 + +/* Number of buckets new object hashtables contain is 2 raised to this power. + E.g. 3 -> 2^3 = 8. */ +#define INITIAL_HASHTABLE_ORDER 3 + +/* Define to the sub-directory where libtool stores uninstalled libraries. */ +#define LT_OBJDIR ".libs/" + +/* Name of package */ +#define PACKAGE "jansson" + +/* Define to the address where bug reports for this package should be sent. */ +#define PACKAGE_BUGREPORT "https://github.com/akheron/jansson/issues" + +/* Define to the full name of this package. */ +#define PACKAGE_NAME "jansson" + +/* Define to the full name and version of this package. */ +#define PACKAGE_STRING "jansson 2.14" + +/* Define to the one symbol short name of this package. */ +#define PACKAGE_TARNAME "jansson" + +/* Define to the home page for this package. */ +#define PACKAGE_URL "" + +/* Define to the version of this package. */ +#define PACKAGE_VERSION "2.14" + +/* Define to 1 if you have the ANSI C header files. */ +#define STDC_HEADERS 1 + +/* Define to 1 if /dev/urandom should be used for seeding the hash function */ +#define USE_URANDOM 1 + +/* Define to 1 if CryptGenRandom should be used for seeding the hash function + */ +#define USE_WINDOWS_CRYPTOAPI 1 + +/* Version number of package */ +#define VERSION "2.14" + +/* Define for Solaris 2.5.1 so the uint32_t typedef from , + , or is not used. If the typedef were allowed, the + #define below would cause a syntax error. */ +/* #undef _UINT32_T */ + +/* Define for Solaris 2.5.1 so the uint8_t typedef from , + , or is not used. If the typedef were allowed, the + #define below would cause a syntax error. */ +/* #undef _UINT8_T */ + +/* Define to `__inline__' or `__inline' if that's what the C compiler + calls it, or to nothing if 'inline' is not supported under any name. */ +#ifndef __cplusplus +/* #undef inline */ +#endif + +/* Define to the type of a signed integer type of width exactly 32 bits if + such a type exists and the standard includes do not define it. */ +/* #undef int32_t */ + +/* Define to the type of an unsigned integer type of width exactly 16 bits if + such a type exists and the standard includes do not define it. */ +/* #undef uint16_t */ + +/* Define to the type of an unsigned integer type of width exactly 32 bits if + such a type exists and the standard includes do not define it. */ +/* #undef uint32_t */ + +/* Define to the type of an unsigned integer type of width exactly 8 bits if + such a type exists and the standard includes do not define it. */ +/* #undef uint8_t */ diff --git a/src/jansson/src/load.c b/src/jansson/src/load.c index a4c8e934..8ae7abd1 100644 --- a/src/jansson/src/load.c +++ b/src/jansson/src/load.c @@ -1,305 +1,350 @@ /* - * Copyright (c) 2009, 2010 Petri Lehtinen + * Copyright (c) 2009-2016 Petri Lehtinen * * Jansson is free software; you can redistribute it and/or modify * it under the terms of the MIT license. See LICENSE for details. */ +#ifndef _GNU_SOURCE #define _GNU_SOURCE -#include +#endif + +#include "jansson_private.h" + +#include #include #include #include #include #include -#include -#include +#ifdef HAVE_UNISTD_H +#include +#endif -#include -#include "jansson_private.h" +#include "jansson.h" #include "strbuffer.h" #include "utf.h" -#define TOKEN_INVALID -1 -#define TOKEN_EOF 0 -#define TOKEN_STRING 256 -#define TOKEN_INTEGER 257 -#define TOKEN_REAL 258 -#define TOKEN_TRUE 259 -#define TOKEN_FALSE 260 -#define TOKEN_NULL 261 - -/* read one byte from stream, return EOF on end of file */ +#define STREAM_STATE_OK 0 +#define STREAM_STATE_EOF -1 +#define STREAM_STATE_ERROR -2 + +#define TOKEN_INVALID -1 +#define TOKEN_EOF 0 +#define TOKEN_STRING 256 +#define TOKEN_INTEGER 257 +#define TOKEN_REAL 258 +#define TOKEN_TRUE 259 +#define TOKEN_FALSE 260 +#define TOKEN_NULL 261 + +/* Locale independent versions of isxxx() functions */ +#define l_isupper(c) ('A' <= (c) && (c) <= 'Z') +#define l_islower(c) ('a' <= (c) && (c) <= 'z') +#define l_isalpha(c) (l_isupper(c) || l_islower(c)) +#define l_isdigit(c) ('0' <= (c) && (c) <= '9') +#define l_isxdigit(c) \ + (l_isdigit(c) || ('A' <= (c) && (c) <= 'F') || ('a' <= (c) && (c) <= 'f')) + +/* Read one byte from stream, convert to unsigned char, then int, and + return. return EOF on end of file. This corresponds to the + behaviour of fgetc(). */ typedef int (*get_func)(void *data); -/* return non-zero if end of file has been reached */ -typedef int (*eof_func)(void *data); - typedef struct { get_func get; - eof_func eof; void *data; - int stream_pos; char buffer[5]; - int buffer_pos; + size_t buffer_pos; + int state; + int line; + int column, last_column; + size_t position; } stream_t; - typedef struct { stream_t stream; strbuffer_t saved_text; + size_t flags; + size_t depth; int token; - int line, column; union { - char *string; + struct { + char *val; + size_t len; + } string; json_int_t integer; double real; } value; } lex_t; +#define stream_to_lex(stream) container_of(stream, lex_t, stream) /*** error reporting ***/ -/* disable format truncation warnings for this function. - it's not a security issue; very long strings could just be truncated on error - */ -#if (defined(__GNUC__) && !defined(__clang__)) -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wformat-truncation" -#endif -static void error_set(json_error_t *error, const lex_t *lex, - const char *msg, ...) -{ +static void error_set(json_error_t *error, const lex_t *lex, enum json_error_code code, + const char *msg, ...) { va_list ap; char msg_text[JSON_ERROR_TEXT_LENGTH]; + char msg_with_context[JSON_ERROR_TEXT_LENGTH]; int line = -1, col = -1; + size_t pos = 0; const char *result = msg_text; - if(!error) + if (!error) return; va_start(ap, msg); vsnprintf(msg_text, JSON_ERROR_TEXT_LENGTH, msg, ap); + msg_text[JSON_ERROR_TEXT_LENGTH - 1] = '\0'; va_end(ap); - if(lex) - { + if (lex) { const char *saved_text = strbuffer_value(&lex->saved_text); - char msg_with_context[JSON_ERROR_TEXT_LENGTH]; - line = lex->line; + line = lex->stream.line; + col = lex->stream.column; + pos = lex->stream.position; - if(saved_text && saved_text[0]) - { - if(lex->saved_text.length <= 20) { - snprintf(msg_with_context, JSON_ERROR_TEXT_LENGTH, - "%s near '%s'", msg_text, saved_text); + if (saved_text && saved_text[0]) { + if (lex->saved_text.length <= 20) { + snprintf(msg_with_context, JSON_ERROR_TEXT_LENGTH, "%s near '%s'", + msg_text, saved_text); + msg_with_context[JSON_ERROR_TEXT_LENGTH - 1] = '\0'; + result = msg_with_context; + } + } else { + if (code == json_error_invalid_syntax) { + /* More specific error code for premature end of file. */ + code = json_error_premature_end_of_input; + } + if (lex->stream.state == STREAM_STATE_ERROR) { + /* No context for UTF-8 decoding errors */ + result = msg_text; + } else { + snprintf(msg_with_context, JSON_ERROR_TEXT_LENGTH, "%s near end of file", + msg_text); + msg_with_context[JSON_ERROR_TEXT_LENGTH - 1] = '\0'; result = msg_with_context; } - } - else - { - snprintf(msg_with_context, JSON_ERROR_TEXT_LENGTH, - "%s near end of file", msg_text); - result = msg_with_context; } } - jsonp_error_set(error, line, col, "%s", result); + jsonp_error_set(error, line, col, pos, code, "%s", result); } -#if (defined(__GNUC__) && !defined(__clang__)) -#pragma GCC diagnostic pop -#endif - /*** lexical analyzer ***/ -static void -stream_init(stream_t *stream, get_func get, eof_func eof, void *data) -{ +static void stream_init(stream_t *stream, get_func get, void *data) { stream->get = get; - stream->eof = eof; stream->data = data; - stream->stream_pos = 0; stream->buffer[0] = '\0'; stream->buffer_pos = 0; + + stream->state = STREAM_STATE_OK; + stream->line = 1; + stream->column = 0; + stream->position = 0; } -static char stream_get(stream_t *stream, json_error_t *error) -{ - char c; +static int stream_get(stream_t *stream, json_error_t *error) { + int c; - if(!stream->buffer[stream->buffer_pos]) - { - stream->buffer[0] = stream->get(stream->data); - stream->buffer_pos = 0; + if (stream->state != STREAM_STATE_OK) + return stream->state; + + if (!stream->buffer[stream->buffer_pos]) { + c = stream->get(stream->data); + if (c == EOF) { + stream->state = STREAM_STATE_EOF; + return STREAM_STATE_EOF; + } - c = stream->buffer[0]; + stream->buffer[0] = c; + stream->buffer_pos = 0; - if((unsigned char)c >= 0x80 && c != (char)EOF) - { + if (0x80 <= c && c <= 0xFF) { /* multi-byte UTF-8 sequence */ - int i, count; + size_t i, count; count = utf8_check_first(c); - if(!count) + if (!count) goto out; assert(count >= 2); - for(i = 1; i < count; i++) + for (i = 1; i < count; i++) stream->buffer[i] = stream->get(stream->data); - if(!utf8_check_full(stream->buffer, count, NULL)) + if (!utf8_check_full(stream->buffer, count, NULL)) goto out; - stream->stream_pos += count; stream->buffer[count] = '\0'; - } - else { + } else stream->buffer[1] = '\0'; - stream->stream_pos++; - } } - return stream->buffer[stream->buffer_pos++]; + c = stream->buffer[stream->buffer_pos++]; + + stream->position++; + if (c == '\n') { + stream->line++; + stream->last_column = stream->column; + stream->column = 0; + } else if (utf8_check_first(c)) { + /* track the Unicode character column, so increment only if + this is the first character of a UTF-8 sequence */ + stream->column++; + } + + return c; out: - error_set(error, NULL, "unable to decode byte 0x%x at position %d", - (unsigned char)c, stream->stream_pos); + stream->state = STREAM_STATE_ERROR; + error_set(error, stream_to_lex(stream), json_error_invalid_utf8, + "unable to decode byte 0x%x", c); + return STREAM_STATE_ERROR; +} - stream->buffer[0] = EOF; - stream->buffer[1] = '\0'; - stream->buffer_pos = 1; +static void stream_unget(stream_t *stream, int c) { + if (c == STREAM_STATE_EOF || c == STREAM_STATE_ERROR) + return; - return EOF; -} + stream->position--; + if (c == '\n') { + stream->line--; + stream->column = stream->last_column; + } else if (utf8_check_first(c)) + stream->column--; -static void stream_unget(stream_t *stream, char c) -{ assert(stream->buffer_pos > 0); stream->buffer_pos--; assert(stream->buffer[stream->buffer_pos] == c); } - -static int lex_get(lex_t *lex, json_error_t *error) -{ +static int lex_get(lex_t *lex, json_error_t *error) { return stream_get(&lex->stream, error); } -static int lex_eof(lex_t *lex) -{ - return lex->stream.eof(lex->stream.data); -} - -static void lex_save(lex_t *lex, char c) -{ - strbuffer_append_byte(&lex->saved_text, c); -} +static void lex_save(lex_t *lex, int c) { strbuffer_append_byte(&lex->saved_text, c); } -static int lex_get_save(lex_t *lex, json_error_t *error) -{ - char c = stream_get(&lex->stream, error); - lex_save(lex, c); +static int lex_get_save(lex_t *lex, json_error_t *error) { + int c = stream_get(&lex->stream, error); + if (c != STREAM_STATE_EOF && c != STREAM_STATE_ERROR) + lex_save(lex, c); return c; } -static void lex_unget_unsave(lex_t *lex, char c) -{ - char d; - stream_unget(&lex->stream, c); - d = strbuffer_pop(&lex->saved_text); - assert(c == d); +static void lex_unget(lex_t *lex, int c) { stream_unget(&lex->stream, c); } + +static void lex_unget_unsave(lex_t *lex, int c) { + if (c != STREAM_STATE_EOF && c != STREAM_STATE_ERROR) { +/* Since we treat warnings as errors, when assertions are turned + * off the "d" variable would be set but never used. Which is + * treated as an error by GCC. + */ +#ifndef NDEBUG + char d; +#endif + stream_unget(&lex->stream, c); +#ifndef NDEBUG + d = +#endif + strbuffer_pop(&lex->saved_text); + assert(c == d); + } } -static void lex_save_cached(lex_t *lex) -{ - while(lex->stream.buffer[lex->stream.buffer_pos] != '\0') - { +static void lex_save_cached(lex_t *lex) { + while (lex->stream.buffer[lex->stream.buffer_pos] != '\0') { lex_save(lex, lex->stream.buffer[lex->stream.buffer_pos]); lex->stream.buffer_pos++; + lex->stream.position++; } } +static void lex_free_string(lex_t *lex) { + jsonp_free(lex->value.string.val); + lex->value.string.val = NULL; + lex->value.string.len = 0; +} + /* assumes that str points to 'u' plus at least 4 valid hex digits */ -static int32_t decode_unicode_escape(const char *str) -{ +static int32_t decode_unicode_escape(const char *str) { int i; int32_t value = 0; assert(str[0] == 'u'); - for(i = 1; i <= 4; i++) { + for (i = 1; i <= 4; i++) { char c = str[i]; value <<= 4; - if(isdigit(c)) + if (l_isdigit(c)) value += c - '0'; - else if(islower(c)) + else if (l_islower(c)) value += c - 'a' + 10; - else if(isupper(c)) + else if (l_isupper(c)) value += c - 'A' + 10; else - assert(0); + return -1; } return value; } -static void lex_scan_string(lex_t *lex, json_error_t *error) -{ - char c; +static void lex_scan_string(lex_t *lex, json_error_t *error) { + int c; const char *p; char *t; int i; - lex->value.string = NULL; + lex->value.string.val = NULL; lex->token = TOKEN_INVALID; c = lex_get_save(lex, error); - while(c != '"') { - if(c == (char)EOF) { - lex_unget_unsave(lex, c); - if(lex_eof(lex)) - error_set(error, lex, "premature end of input"); + while (c != '"') { + if (c == STREAM_STATE_ERROR) + goto out; + + else if (c == STREAM_STATE_EOF) { + error_set(error, lex, json_error_premature_end_of_input, + "premature end of input"); goto out; } - else if((unsigned char)c <= 0x1F) { + else if (0 <= c && c <= 0x1F) { /* control character */ lex_unget_unsave(lex, c); - if(c == '\n') - error_set(error, lex, "unexpected newline"); + if (c == '\n') + error_set(error, lex, json_error_invalid_syntax, "unexpected newline"); else - error_set(error, lex, "control character 0x%x", c); + error_set(error, lex, json_error_invalid_syntax, "control character 0x%x", + c); goto out; } - else if(c == '\\') { + else if (c == '\\') { c = lex_get_save(lex, error); - if(c == 'u') { + if (c == 'u') { c = lex_get_save(lex, error); - for(i = 0; i < 4; i++) { - if(!isxdigit(c)) { - lex_unget_unsave(lex, c); - error_set(error, lex, "invalid escape"); + for (i = 0; i < 4; i++) { + if (!l_isxdigit(c)) { + error_set(error, lex, json_error_invalid_syntax, + "invalid escape"); goto out; } c = lex_get_save(lex, error); } - } - else if(c == '"' || c == '\\' || c == '/' || c == 'b' || - c == 'f' || c == 'n' || c == 'r' || c == 't') + } else if (c == '"' || c == '\\' || c == '/' || c == 'b' || c == 'f' || + c == 'n' || c == 'r' || c == 't') c = lex_get_save(lex, error); else { - lex_unget_unsave(lex, c); - error_set(error, lex, "invalid escape"); + error_set(error, lex, json_error_invalid_syntax, "invalid escape"); goto out; } - } - else + } else c = lex_get_save(lex, error); } @@ -310,259 +355,263 @@ static void lex_scan_string(lex_t *lex, json_error_t *error) - two \uXXXX escapes (length 12) forming an UTF-16 surrogate pair are converted to 4 bytes */ - lex->value.string = malloc(lex->saved_text.length + 1); - if(!lex->value.string) { + t = jsonp_malloc(lex->saved_text.length + 1); + if (!t) { /* this is not very nice, since TOKEN_INVALID is returned */ goto out; } - - /* the target */ - t = lex->value.string; + lex->value.string.val = t; /* + 1 to skip the " */ p = strbuffer_value(&lex->saved_text) + 1; - while(*p != '"') { - if(*p == '\\') { + while (*p != '"') { + if (*p == '\\') { p++; - if(*p == 'u') { - char buffer[4]; - int length; + if (*p == 'u') { + size_t length; int32_t value; value = decode_unicode_escape(p); + if (value < 0) { + error_set(error, lex, json_error_invalid_syntax, + "invalid Unicode escape '%.6s'", p - 1); + goto out; + } p += 5; - if(0xD800 <= value && value <= 0xDBFF) { + if (0xD800 <= value && value <= 0xDBFF) { /* surrogate pair */ - if(*p == '\\' && *(p + 1) == 'u') { + if (*p == '\\' && *(p + 1) == 'u') { int32_t value2 = decode_unicode_escape(++p); + if (value2 < 0) { + error_set(error, lex, json_error_invalid_syntax, + "invalid Unicode escape '%.6s'", p - 1); + goto out; + } p += 5; - if(0xDC00 <= value2 && value2 <= 0xDFFF) { + if (0xDC00 <= value2 && value2 <= 0xDFFF) { /* valid second surrogate */ value = - ((value - 0xD800) << 10) + - (value2 - 0xDC00) + - 0x10000; - } - else { + ((value - 0xD800) << 10) + (value2 - 0xDC00) + 0x10000; + } else { /* invalid second surrogate */ - error_set(error, lex, - "invalid Unicode '\\u%04X\\u%04X'", - value, value2); + error_set(error, lex, json_error_invalid_syntax, + "invalid Unicode '\\u%04X\\u%04X'", value, value2); goto out; } - } - else { + } else { /* no second surrogate */ - error_set(error, lex, "invalid Unicode '\\u%04X'", - value); + error_set(error, lex, json_error_invalid_syntax, + "invalid Unicode '\\u%04X'", value); goto out; } - } - else if(0xDC00 <= value && value <= 0xDFFF) { - error_set(error, lex, "invalid Unicode '\\u%04X'", value); - goto out; - } - else if(value == 0) - { - error_set(error, lex, "\\u0000 is not allowed"); + } else if (0xDC00 <= value && value <= 0xDFFF) { + error_set(error, lex, json_error_invalid_syntax, + "invalid Unicode '\\u%04X'", value); goto out; } - if(utf8_encode(value, buffer, &length)) + if (utf8_encode(value, t, &length)) assert(0); - - memcpy(t, buffer, length); t += length; - } - else { - switch(*p) { - case '"': case '\\': case '/': - *t = *p; break; - case 'b': *t = '\b'; break; - case 'f': *t = '\f'; break; - case 'n': *t = '\n'; break; - case 'r': *t = '\r'; break; - case 't': *t = '\t'; break; - default: assert(0); + } else { + switch (*p) { + case '"': + case '\\': + case '/': + *t = *p; + break; + case 'b': + *t = '\b'; + break; + case 'f': + *t = '\f'; + break; + case 'n': + *t = '\n'; + break; + case 'r': + *t = '\r'; + break; + case 't': + *t = '\t'; + break; + default: + assert(0); } t++; p++; } - } - else + } else *(t++) = *(p++); } *t = '\0'; + lex->value.string.len = t - lex->value.string.val; lex->token = TOKEN_STRING; return; out: - free(lex->value.string); + lex_free_string(lex); } +#ifndef JANSSON_USING_CMAKE /* disabled if using cmake */ #if JSON_INTEGER_IS_LONG_LONG -#define json_strtoint strtoll +#ifdef _MSC_VER /* Microsoft Visual Studio */ +#define json_strtoint _strtoi64 #else -#define json_strtoint strtol +#define json_strtoint strtoll +#endif +#else +#define json_strtoint strtol +#endif #endif -static int lex_scan_number(lex_t *lex, char c, json_error_t *error) -{ +static int lex_scan_number(lex_t *lex, int c, json_error_t *error) { const char *saved_text; char *end; - double value; + double doubleval; lex->token = TOKEN_INVALID; - if(c == '-') + if (c == '-') c = lex_get_save(lex, error); - if(c == '0') { + if (c == '0') { c = lex_get_save(lex, error); - if(isdigit(c)) { + if (l_isdigit(c)) { lex_unget_unsave(lex, c); goto out; } - } - else if(isdigit(c)) { - c = lex_get_save(lex, error); - while(isdigit(c)) + } else if (l_isdigit(c)) { + do c = lex_get_save(lex, error); - } - else { - lex_unget_unsave(lex, c); - goto out; + while (l_isdigit(c)); + } else { + lex_unget_unsave(lex, c); + goto out; } - if(c != '.' && c != 'E' && c != 'e') { - json_int_t value; + if (!(lex->flags & JSON_DECODE_INT_AS_REAL) && c != '.' && c != 'E' && c != 'e') { + json_int_t intval; lex_unget_unsave(lex, c); saved_text = strbuffer_value(&lex->saved_text); errno = 0; - value = json_strtoint(saved_text, &end, 10); - if(errno == ERANGE) { - if(value < 0) - error_set(error, lex, "too big negative integer"); + intval = json_strtoint(saved_text, &end, 10); + if (errno == ERANGE) { + if (intval < 0) + error_set(error, lex, json_error_numeric_overflow, + "too big negative integer"); else - error_set(error, lex, "too big integer"); + error_set(error, lex, json_error_numeric_overflow, "too big integer"); goto out; } assert(end == saved_text + lex->saved_text.length); lex->token = TOKEN_INTEGER; - lex->value.integer = value; + lex->value.integer = intval; return 0; } - if(c == '.') { + if (c == '.') { c = lex_get(lex, error); - if(!isdigit(c)) + if (!l_isdigit(c)) { + lex_unget(lex, c); goto out; + } lex_save(lex, c); - c = lex_get_save(lex, error); - while(isdigit(c)) + do c = lex_get_save(lex, error); + while (l_isdigit(c)); } - if(c == 'E' || c == 'e') { + if (c == 'E' || c == 'e') { c = lex_get_save(lex, error); - if(c == '+' || c == '-') + if (c == '+' || c == '-') c = lex_get_save(lex, error); - if(!isdigit(c)) { + if (!l_isdigit(c)) { lex_unget_unsave(lex, c); goto out; } - c = lex_get_save(lex, error); - while(isdigit(c)) + do c = lex_get_save(lex, error); + while (l_isdigit(c)); } lex_unget_unsave(lex, c); - saved_text = strbuffer_value(&lex->saved_text); - value = strtod(saved_text, &end); - assert(end == saved_text + lex->saved_text.length); - - if(errno == ERANGE && value != 0) { - error_set(error, lex, "real number overflow"); + if (jsonp_strtod(&lex->saved_text, &doubleval)) { + error_set(error, lex, json_error_numeric_overflow, "real number overflow"); goto out; } lex->token = TOKEN_REAL; - lex->value.real = value; + lex->value.real = doubleval; return 0; out: return -1; } -static int lex_scan(lex_t *lex, json_error_t *error) -{ - char c; +static int lex_scan(lex_t *lex, json_error_t *error) { + int c; strbuffer_clear(&lex->saved_text); - if(lex->token == TOKEN_STRING) { - free(lex->value.string); - lex->value.string = NULL; - } - - c = lex_get(lex, error); - while(c == ' ' || c == '\t' || c == '\n' || c == '\r') - { - if(c == '\n') - lex->line++; + if (lex->token == TOKEN_STRING) + lex_free_string(lex); + do c = lex_get(lex, error); + while (c == ' ' || c == '\t' || c == '\n' || c == '\r'); + + if (c == STREAM_STATE_EOF) { + lex->token = TOKEN_EOF; + goto out; } - if(c == (char)EOF) { - if(lex_eof(lex)) - lex->token = TOKEN_EOF; - else - lex->token = TOKEN_INVALID; + if (c == STREAM_STATE_ERROR) { + lex->token = TOKEN_INVALID; goto out; } lex_save(lex, c); - if(c == '{' || c == '}' || c == '[' || c == ']' || c == ':' || c == ',') + if (c == '{' || c == '}' || c == '[' || c == ']' || c == ':' || c == ',') lex->token = c; - else if(c == '"') + else if (c == '"') lex_scan_string(lex, error); - else if(isdigit(c) || c == '-') { - if(lex_scan_number(lex, c, error)) + else if (l_isdigit(c) || c == '-') { + if (lex_scan_number(lex, c, error)) goto out; } - else if(isupper(c) || islower(c)) { + else if (l_isalpha(c)) { /* eat up the whole identifier for clearer error messages */ const char *saved_text; - c = lex_get_save(lex, error); - while(isupper(c) || islower(c)) + do c = lex_get_save(lex, error); + while (l_isalpha(c)); lex_unget_unsave(lex, c); saved_text = strbuffer_value(&lex->saved_text); - if(strcmp(saved_text, "true") == 0) + if (strcmp(saved_text, "true") == 0) lex->token = TOKEN_TRUE; - else if(strcmp(saved_text, "false") == 0) + else if (strcmp(saved_text, "false") == 0) lex->token = TOKEN_FALSE; - else if(strcmp(saved_text, "null") == 0) + else if (strcmp(saved_text, "null") == 0) lex->token = TOKEN_NULL; else lex->token = TOKEN_INVALID; @@ -579,96 +628,104 @@ static int lex_scan(lex_t *lex, json_error_t *error) return lex->token; } -static char *lex_steal_string(lex_t *lex) -{ +static char *lex_steal_string(lex_t *lex, size_t *out_len) { char *result = NULL; - if(lex->token == TOKEN_STRING) - { - result = lex->value.string; - lex->value.string = NULL; + if (lex->token == TOKEN_STRING) { + result = lex->value.string.val; + *out_len = lex->value.string.len; + lex->value.string.val = NULL; + lex->value.string.len = 0; } return result; } -static int lex_init(lex_t *lex, get_func get, eof_func eof, void *data) -{ - stream_init(&lex->stream, get, eof, data); - if(strbuffer_init(&lex->saved_text)) +static int lex_init(lex_t *lex, get_func get, size_t flags, void *data) { + stream_init(&lex->stream, get, data); + if (strbuffer_init(&lex->saved_text)) return -1; + lex->flags = flags; lex->token = TOKEN_INVALID; - lex->line = 1; - return 0; } -static void lex_close(lex_t *lex) -{ - if(lex->token == TOKEN_STRING) - free(lex->value.string); +static void lex_close(lex_t *lex) { + if (lex->token == TOKEN_STRING) + lex_free_string(lex); strbuffer_close(&lex->saved_text); } - /*** parser ***/ -static json_t *parse_value(lex_t *lex, json_error_t *error); +static json_t *parse_value(lex_t *lex, size_t flags, json_error_t *error); -static json_t *parse_object(lex_t *lex, json_error_t *error) -{ +static json_t *parse_object(lex_t *lex, size_t flags, json_error_t *error) { json_t *object = json_object(); - if(!object) + if (!object) return NULL; lex_scan(lex, error); - if(lex->token == '}') + if (lex->token == '}') return object; - while(1) { + while (1) { char *key; + size_t len; json_t *value; - if(lex->token != TOKEN_STRING) { - error_set(error, lex, "string or '}' expected"); + if (lex->token != TOKEN_STRING) { + error_set(error, lex, json_error_invalid_syntax, "string or '}' expected"); goto error; } - key = lex_steal_string(lex); - if(!key) + key = lex_steal_string(lex, &len); + if (!key) return NULL; + if (memchr(key, '\0', len)) { + jsonp_free(key); + error_set(error, lex, json_error_null_byte_in_key, + "NUL byte in object key not supported"); + goto error; + } + + if (flags & JSON_REJECT_DUPLICATES) { + if (json_object_getn(object, key, len)) { + jsonp_free(key); + error_set(error, lex, json_error_duplicate_key, "duplicate object key"); + goto error; + } + } lex_scan(lex, error); - if(lex->token != ':') { - free(key); - error_set(error, lex, "':' expected"); + if (lex->token != ':') { + jsonp_free(key); + error_set(error, lex, json_error_invalid_syntax, "':' expected"); goto error; } lex_scan(lex, error); - value = parse_value(lex, error); - if(!value) { - free(key); + value = parse_value(lex, flags, error); + if (!value) { + jsonp_free(key); goto error; } - if(json_object_set_nocheck(object, key, value)) { - free(key); - json_decref(value); + if (json_object_setn_new_nocheck(object, key, len, value)) { + jsonp_free(key); goto error; } - json_decref(value); - free(key); + jsonp_free(key); lex_scan(lex, error); - if(lex->token != ',') + if (lex->token != ',') break; lex_scan(lex, error); } - if(lex->token != '}') { - error_set(error, lex, "'}' expected"); + if (lex->token != '}') { + error_set(error, lex, json_error_invalid_syntax, "'}' expected"); goto error; } @@ -679,36 +736,33 @@ static json_t *parse_object(lex_t *lex, json_error_t *error) return NULL; } -static json_t *parse_array(lex_t *lex, json_error_t *error) -{ +static json_t *parse_array(lex_t *lex, size_t flags, json_error_t *error) { json_t *array = json_array(); - if(!array) + if (!array) return NULL; lex_scan(lex, error); - if(lex->token == ']') + if (lex->token == ']') return array; - while(lex->token) { - json_t *elem = parse_value(lex, error); - if(!elem) + while (lex->token) { + json_t *elem = parse_value(lex, flags, error); + if (!elem) goto error; - if(json_array_append(array, elem)) { - json_decref(elem); + if (json_array_append_new(array, elem)) { goto error; } - json_decref(elem); lex_scan(lex, error); - if(lex->token != ',') + if (lex->token != ',') break; lex_scan(lex, error); } - if(lex->token != ']') { - error_set(error, lex, "']' expected"); + if (lex->token != ']') { + error_set(error, lex, json_error_invalid_syntax, "']' expected"); goto error; } @@ -719,13 +773,31 @@ static json_t *parse_array(lex_t *lex, json_error_t *error) return NULL; } -static json_t *parse_value(lex_t *lex, json_error_t *error) -{ +static json_t *parse_value(lex_t *lex, size_t flags, json_error_t *error) { json_t *json; - switch(lex->token) { + lex->depth++; + if (lex->depth > JSON_PARSER_MAX_DEPTH) { + error_set(error, lex, json_error_stack_overflow, "maximum parsing depth reached"); + return NULL; + } + + switch (lex->token) { case TOKEN_STRING: { - json = json_string_nocheck(lex->value.string); + const char *value = lex->value.string.val; + size_t len = lex->value.string.len; + + if (!(flags & JSON_ALLOW_NUL)) { + if (memchr(value, '\0', len)) { + error_set(error, lex, json_error_null_character, + "\\u0000 is not allowed without JSON_ALLOW_NUL"); + return NULL; + } + } + + json = jsonp_stringn_nocheck_own(value, len); + lex->value.string.val = NULL; + lex->value.string.len = 0; break; } @@ -752,139 +824,225 @@ static json_t *parse_value(lex_t *lex, json_error_t *error) break; case '{': - json = parse_object(lex, error); + json = parse_object(lex, flags, error); break; case '[': - json = parse_array(lex, error); + json = parse_array(lex, flags, error); break; case TOKEN_INVALID: - error_set(error, lex, "invalid token"); + error_set(error, lex, json_error_invalid_syntax, "invalid token"); return NULL; default: - error_set(error, lex, "unexpected token"); + error_set(error, lex, json_error_invalid_syntax, "unexpected token"); return NULL; } - if(!json) + if (!json) return NULL; + lex->depth--; return json; } -static json_t *parse_json(lex_t *lex, json_error_t *error) -{ +static json_t *parse_json(lex_t *lex, size_t flags, json_error_t *error) { + json_t *result; + + lex->depth = 0; + lex_scan(lex, error); - if(lex->token != '[' && lex->token != '{') { - error_set(error, lex, "'[' or '{' expected"); + if (!(flags & JSON_DECODE_ANY)) { + if (lex->token != '[' && lex->token != '{') { + error_set(error, lex, json_error_invalid_syntax, "'[' or '{' expected"); + return NULL; + } + } + + result = parse_value(lex, flags, error); + if (!result) return NULL; + + if (!(flags & JSON_DISABLE_EOF_CHECK)) { + lex_scan(lex, error); + if (lex->token != TOKEN_EOF) { + error_set(error, lex, json_error_end_of_input_expected, + "end of file expected"); + json_decref(result); + return NULL; + } } - return parse_value(lex, error); + if (error) { + /* Save the position even though there was no error */ + error->position = (int)lex->stream.position; + } + + return result; } -typedef struct -{ +typedef struct { const char *data; - int pos; + size_t pos; } string_data_t; -static int string_get(void *data) -{ +static int string_get(void *data) { char c; string_data_t *stream = (string_data_t *)data; c = stream->data[stream->pos]; - if(c == '\0') + if (c == '\0') return EOF; - else - { + else { stream->pos++; - return c; + return (unsigned char)c; } } -static int string_eof(void *data) -{ - string_data_t *stream = (string_data_t *)data; - return (stream->data[stream->pos] == '\0'); +json_t *json_loads(const char *string, size_t flags, json_error_t *error) { + lex_t lex; + json_t *result; + string_data_t stream_data; + + jsonp_error_init(error, ""); + + if (string == NULL) { + error_set(error, NULL, json_error_invalid_argument, "wrong arguments"); + return NULL; + } + + stream_data.data = string; + stream_data.pos = 0; + + if (lex_init(&lex, string_get, flags, (void *)&stream_data)) + return NULL; + + result = parse_json(&lex, flags, error); + + lex_close(&lex); + return result; +} + +typedef struct { + const char *data; + size_t len; + size_t pos; +} buffer_data_t; + +static int buffer_get(void *data) { + char c; + buffer_data_t *stream = data; + if (stream->pos >= stream->len) + return EOF; + + c = stream->data[stream->pos]; + stream->pos++; + return (unsigned char)c; } -json_t *json_loads(const char *string, size_t flags, json_error_t *error) -{ +json_t *json_loadb(const char *buffer, size_t buflen, size_t flags, json_error_t *error) { lex_t lex; json_t *result; - (void)flags; /* unused */ + buffer_data_t stream_data; - string_data_t stream_data = {string, 0}; + jsonp_error_init(error, ""); - if(lex_init(&lex, string_get, string_eof, (void *)&stream_data)) + if (buffer == NULL) { + error_set(error, NULL, json_error_invalid_argument, "wrong arguments"); return NULL; + } - jsonp_error_init(error, ""); + stream_data.data = buffer; + stream_data.pos = 0; + stream_data.len = buflen; - result = parse_json(&lex, error); - if(!result) - goto out; + if (lex_init(&lex, buffer_get, flags, (void *)&stream_data)) + return NULL; - lex_scan(&lex, error); - if(lex.token != TOKEN_EOF) { - error_set(error, &lex, "end of file expected"); - json_decref(result); - result = NULL; - } + result = parse_json(&lex, flags, error); -out: lex_close(&lex); return result; } -json_t *json_loadf(FILE *input, size_t flags, json_error_t *error) -{ +json_t *json_loadf(FILE *input, size_t flags, json_error_t *error) { lex_t lex; const char *source; json_t *result; - (void)flags; /* unused */ - if(lex_init(&lex, (get_func)fgetc, (eof_func)feof, input)) + if (input == stdin) + source = ""; + else + source = ""; + + jsonp_error_init(error, source); + + if (input == NULL) { + error_set(error, NULL, json_error_invalid_argument, "wrong arguments"); return NULL; + } + + if (lex_init(&lex, (get_func)fgetc, flags, input)) + return NULL; + + result = parse_json(&lex, flags, error); + + lex_close(&lex); + return result; +} - if(input == stdin) +static int fd_get_func(int *fd) { +#ifdef HAVE_UNISTD_H + uint8_t c; + if (read(*fd, &c, 1) == 1) + return c; +#endif + return EOF; +} + +json_t *json_loadfd(int input, size_t flags, json_error_t *error) { + lex_t lex; + const char *source; + json_t *result; + +#ifdef HAVE_UNISTD_H + if (input == STDIN_FILENO) source = ""; else +#endif source = ""; jsonp_error_init(error, source); - result = parse_json(&lex, error); - if(!result) - goto out; - - lex_scan(&lex, error); - if(lex.token != TOKEN_EOF) { - error_set(error, &lex, "end of file expected"); - json_decref(result); - result = NULL; + if (input < 0) { + error_set(error, NULL, json_error_invalid_argument, "wrong arguments"); + return NULL; } -out: + if (lex_init(&lex, (get_func)fd_get_func, flags, &input)) + return NULL; + + result = parse_json(&lex, flags, error); + lex_close(&lex); return result; } -json_t *json_load_file(const char *path, size_t flags, json_error_t *error) -{ +json_t *json_load_file(const char *path, size_t flags, json_error_t *error) { json_t *result; FILE *fp; jsonp_error_init(error, path); - fp = fopen(path, "r"); - if(!fp) - { - error_set(error, NULL, "unable to open %s: %s", - path, strerror(errno)); + if (path == NULL) { + error_set(error, NULL, json_error_invalid_argument, "wrong arguments"); + return NULL; + } + + fp = fopen(path, "rb"); + if (!fp) { + error_set(error, NULL, json_error_cannot_open_file, "unable to open %s: %s", path, + strerror(errno)); return NULL; } @@ -893,3 +1051,56 @@ json_t *json_load_file(const char *path, size_t flags, json_error_t *error) fclose(fp); return result; } + +#define MAX_BUF_LEN 1024 + +typedef struct { + char data[MAX_BUF_LEN]; + size_t len; + size_t pos; + json_load_callback_t callback; + void *arg; +} callback_data_t; + +static int callback_get(void *data) { + char c; + callback_data_t *stream = data; + + if (stream->pos >= stream->len) { + stream->pos = 0; + stream->len = stream->callback(stream->data, MAX_BUF_LEN, stream->arg); + if (stream->len == 0 || stream->len == (size_t)-1) + return EOF; + } + + c = stream->data[stream->pos]; + stream->pos++; + return (unsigned char)c; +} + +json_t *json_load_callback(json_load_callback_t callback, void *arg, size_t flags, + json_error_t *error) { + lex_t lex; + json_t *result; + + callback_data_t stream_data; + + memset(&stream_data, 0, sizeof(stream_data)); + stream_data.callback = callback; + stream_data.arg = arg; + + jsonp_error_init(error, ""); + + if (callback == NULL) { + error_set(error, NULL, json_error_invalid_argument, "wrong arguments"); + return NULL; + } + + if (lex_init(&lex, (get_func)callback_get, flags, &stream_data)) + return NULL; + + result = parse_json(&lex, flags, error); + + lex_close(&lex); + return result; +} diff --git a/src/jansson/src/lookup3.h b/src/jansson/src/lookup3.h new file mode 100644 index 00000000..9b39aa1c --- /dev/null +++ b/src/jansson/src/lookup3.h @@ -0,0 +1,382 @@ +// clang-format off +/* +------------------------------------------------------------------------------- +lookup3.c, by Bob Jenkins, May 2006, Public Domain. + +These are functions for producing 32-bit hashes for hash table lookup. +hashword(), hashlittle(), hashlittle2(), hashbig(), mix(), and final() +are externally useful functions. Routines to test the hash are included +if SELF_TEST is defined. You can use this free for any purpose. It's in +the public domain. It has no warranty. + +You probably want to use hashlittle(). hashlittle() and hashbig() +hash byte arrays. hashlittle() is is faster than hashbig() on +little-endian machines. Intel and AMD are little-endian machines. +On second thought, you probably want hashlittle2(), which is identical to +hashlittle() except it returns two 32-bit hashes for the price of one. +You could implement hashbig2() if you wanted but I haven't bothered here. + +If you want to find a hash of, say, exactly 7 integers, do + a = i1; b = i2; c = i3; + mix(a,b,c); + a += i4; b += i5; c += i6; + mix(a,b,c); + a += i7; + final(a,b,c); +then use c as the hash value. If you have a variable length array of +4-byte integers to hash, use hashword(). If you have a byte array (like +a character string), use hashlittle(). If you have several byte arrays, or +a mix of things, see the comments above hashlittle(). + +Why is this so big? I read 12 bytes at a time into 3 4-byte integers, +then mix those integers. This is fast (you can do a lot more thorough +mixing with 12*3 instructions on 3 integers than you can with 3 instructions +on 1 byte), but shoehorning those bytes into integers efficiently is messy. +------------------------------------------------------------------------------- +*/ + +#include + +#ifdef HAVE_CONFIG_H +#include +#endif + +#ifdef HAVE_STDINT_H +#include /* defines uint32_t etc */ +#endif + +#ifdef HAVE_SYS_PARAM_H +#include /* attempt to define endianness */ +#endif + +#ifdef HAVE_ENDIAN_H +# include /* attempt to define endianness */ +#endif + +/* + * My best guess at if you are big-endian or little-endian. This may + * need adjustment. + */ +#if (defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && \ + __BYTE_ORDER == __LITTLE_ENDIAN) || \ + (defined(i386) || defined(__i386__) || defined(__i486__) || \ + defined(__i586__) || defined(__i686__) || defined(vax) || defined(MIPSEL)) +# define HASH_LITTLE_ENDIAN 1 +# define HASH_BIG_ENDIAN 0 +#elif (defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && \ + __BYTE_ORDER == __BIG_ENDIAN) || \ + (defined(sparc) || defined(POWERPC) || defined(mc68000) || defined(sel)) +# define HASH_LITTLE_ENDIAN 0 +# define HASH_BIG_ENDIAN 1 +#else +# define HASH_LITTLE_ENDIAN 0 +# define HASH_BIG_ENDIAN 0 +#endif + +#define hashsize(n) ((size_t)1<<(n)) +#define hashmask(n) (hashsize(n)-1) +#define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k)))) + +/* +------------------------------------------------------------------------------- +mix -- mix 3 32-bit values reversibly. + +This is reversible, so any information in (a,b,c) before mix() is +still in (a,b,c) after mix(). + +If four pairs of (a,b,c) inputs are run through mix(), or through +mix() in reverse, there are at least 32 bits of the output that +are sometimes the same for one pair and different for another pair. +This was tested for: +* pairs that differed by one bit, by two bits, in any combination + of top bits of (a,b,c), or in any combination of bottom bits of + (a,b,c). +* "differ" is defined as +, -, ^, or ~^. For + and -, I transformed + the output delta to a Gray code (a^(a>>1)) so a string of 1's (as + is commonly produced by subtraction) look like a single 1-bit + difference. +* the base values were pseudorandom, all zero but one bit set, or + all zero plus a counter that starts at zero. + +Some k values for my "a-=c; a^=rot(c,k); c+=b;" arrangement that +satisfy this are + 4 6 8 16 19 4 + 9 15 3 18 27 15 + 14 9 3 7 17 3 +Well, "9 15 3 18 27 15" didn't quite get 32 bits diffing +for "differ" defined as + with a one-bit base and a two-bit delta. I +used http://burtleburtle.net/bob/hash/avalanche.html to choose +the operations, constants, and arrangements of the variables. + +This does not achieve avalanche. There are input bits of (a,b,c) +that fail to affect some output bits of (a,b,c), especially of a. The +most thoroughly mixed value is c, but it doesn't really even achieve +avalanche in c. + +This allows some parallelism. Read-after-writes are good at doubling +the number of bits affected, so the goal of mixing pulls in the opposite +direction as the goal of parallelism. I did what I could. Rotates +seem to cost as much as shifts on every machine I could lay my hands +on, and rotates are much kinder to the top and bottom bits, so I used +rotates. +------------------------------------------------------------------------------- +*/ +#define mix(a,b,c) \ +{ \ + a -= c; a ^= rot(c, 4); c += b; \ + b -= a; b ^= rot(a, 6); a += c; \ + c -= b; c ^= rot(b, 8); b += a; \ + a -= c; a ^= rot(c,16); c += b; \ + b -= a; b ^= rot(a,19); a += c; \ + c -= b; c ^= rot(b, 4); b += a; \ +} + +/* +------------------------------------------------------------------------------- +final -- final mixing of 3 32-bit values (a,b,c) into c + +Pairs of (a,b,c) values differing in only a few bits will usually +produce values of c that look totally different. This was tested for +* pairs that differed by one bit, by two bits, in any combination + of top bits of (a,b,c), or in any combination of bottom bits of + (a,b,c). +* "differ" is defined as +, -, ^, or ~^. For + and -, I transformed + the output delta to a Gray code (a^(a>>1)) so a string of 1's (as + is commonly produced by subtraction) look like a single 1-bit + difference. +* the base values were pseudorandom, all zero but one bit set, or + all zero plus a counter that starts at zero. + +These constants passed: + 14 11 25 16 4 14 24 + 12 14 25 16 4 14 24 +and these came close: + 4 8 15 26 3 22 24 + 10 8 15 26 3 22 24 + 11 8 15 26 3 22 24 +------------------------------------------------------------------------------- +*/ +#define final(a,b,c) \ +{ \ + c ^= b; c -= rot(b,14); \ + a ^= c; a -= rot(c,11); \ + b ^= a; b -= rot(a,25); \ + c ^= b; c -= rot(b,16); \ + a ^= c; a -= rot(c,4); \ + b ^= a; b -= rot(a,14); \ + c ^= b; c -= rot(b,24); \ +} + +/* +------------------------------------------------------------------------------- +hashlittle() -- hash a variable-length key into a 32-bit value + k : the key (the unaligned variable-length array of bytes) + length : the length of the key, counting by bytes + initval : can be any 4-byte value +Returns a 32-bit value. Every bit of the key affects every bit of +the return value. Two keys differing by one or two bits will have +totally different hash values. + +The best hash table sizes are powers of 2. There is no need to do +mod a prime (mod is sooo slow!). If you need less than 32 bits, +use a bitmask. For example, if you need only 10 bits, do + h = (h & hashmask(10)); +In which case, the hash table should have hashsize(10) elements. + +If you are hashing n strings (uint8_t **)k, do it like this: + for (i=0, h=0; i 12) + { + a += k[0]; + b += k[1]; + c += k[2]; + mix(a,b,c); + length -= 12; + k += 3; + } + + /*----------------------------- handle the last (probably partial) block */ + /* + * "k[2]&0xffffff" actually reads beyond the end of the string, but + * then masks off the part it's not allowed to read. Because the + * string is aligned, the masked-off tail is in the same word as the + * rest of the string. Every machine with memory protection I've seen + * does it on word boundaries, so is OK with this. But VALGRIND will + * still catch it and complain. The masking trick does make the hash + * noticeably faster for short strings (like English words). + */ +#ifndef NO_MASKING_TRICK + + switch(length) + { + case 12: c+=k[2]; b+=k[1]; a+=k[0]; break; + case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0]; break; + case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0]; break; + case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0]; break; + case 8 : b+=k[1]; a+=k[0]; break; + case 7 : b+=k[1]&0xffffff; a+=k[0]; break; + case 6 : b+=k[1]&0xffff; a+=k[0]; break; + case 5 : b+=k[1]&0xff; a+=k[0]; break; + case 4 : a+=k[0]; break; + case 3 : a+=k[0]&0xffffff; break; + case 2 : a+=k[0]&0xffff; break; + case 1 : a+=k[0]&0xff; break; + case 0 : return c; /* zero length strings require no mixing */ + } + +#else /* make valgrind happy */ + + k8 = (const uint8_t *)k; + switch(length) + { + case 12: c+=k[2]; b+=k[1]; a+=k[0]; break; + case 11: c+=((uint32_t)k8[10])<<16; /* fall through */ + case 10: c+=((uint32_t)k8[9])<<8; /* fall through */ + case 9 : c+=k8[8]; /* fall through */ + case 8 : b+=k[1]; a+=k[0]; break; + case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */ + case 6 : b+=((uint32_t)k8[5])<<8; /* fall through */ + case 5 : b+=k8[4]; /* fall through */ + case 4 : a+=k[0]; break; + case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */ + case 2 : a+=((uint32_t)k8[1])<<8; /* fall through */ + case 1 : a+=k8[0]; break; + case 0 : return c; + } + +#endif /* !valgrind */ + + } else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) { + const uint16_t *k = (const uint16_t *)key; /* read 16-bit chunks */ + const uint8_t *k8; + + /*--------------- all but last block: aligned reads and different mixing */ + while (length > 12) + { + a += k[0] + (((uint32_t)k[1])<<16); + b += k[2] + (((uint32_t)k[3])<<16); + c += k[4] + (((uint32_t)k[5])<<16); + mix(a,b,c); + length -= 12; + k += 6; + } + + /*----------------------------- handle the last (probably partial) block */ + k8 = (const uint8_t *)k; + switch(length) + { + case 12: c+=k[4]+(((uint32_t)k[5])<<16); + b+=k[2]+(((uint32_t)k[3])<<16); + a+=k[0]+(((uint32_t)k[1])<<16); + break; + case 11: c+=((uint32_t)k8[10])<<16; /* fall through */ + case 10: c+=k[4]; + b+=k[2]+(((uint32_t)k[3])<<16); + a+=k[0]+(((uint32_t)k[1])<<16); + break; + case 9 : c+=k8[8]; /* fall through */ + case 8 : b+=k[2]+(((uint32_t)k[3])<<16); + a+=k[0]+(((uint32_t)k[1])<<16); + break; + case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */ + case 6 : b+=k[2]; + a+=k[0]+(((uint32_t)k[1])<<16); + break; + case 5 : b+=k8[4]; /* fall through */ + case 4 : a+=k[0]+(((uint32_t)k[1])<<16); + break; + case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */ + case 2 : a+=k[0]; + break; + case 1 : a+=k8[0]; + break; + case 0 : return c; /* zero length requires no mixing */ + } + + } else { /* need to read the key one byte at a time */ + const uint8_t *k = (const uint8_t *)key; + + /*--------------- all but the last block: affect some 32 bits of (a,b,c) */ + while (length > 12) + { + a += k[0]; + a += ((uint32_t)k[1])<<8; + a += ((uint32_t)k[2])<<16; + a += ((uint32_t)k[3])<<24; + b += k[4]; + b += ((uint32_t)k[5])<<8; + b += ((uint32_t)k[6])<<16; + b += ((uint32_t)k[7])<<24; + c += k[8]; + c += ((uint32_t)k[9])<<8; + c += ((uint32_t)k[10])<<16; + c += ((uint32_t)k[11])<<24; + mix(a,b,c); + length -= 12; + k += 12; + } + + /*-------------------------------- last block: affect all 32 bits of (c) */ + switch(length) /* all the case statements fall through */ + { + case 12: c+=((uint32_t)k[11])<<24; /* fall through */ + case 11: c+=((uint32_t)k[10])<<16; /* fall through */ + case 10: c+=((uint32_t)k[9])<<8; /* fall through */ + case 9 : c+=k[8]; /* fall through */ + case 8 : b+=((uint32_t)k[7])<<24; /* fall through */ + case 7 : b+=((uint32_t)k[6])<<16; /* fall through */ + case 6 : b+=((uint32_t)k[5])<<8; /* fall through */ + case 5 : b+=k[4]; /* fall through */ + case 4 : a+=((uint32_t)k[3])<<24; /* fall through */ + case 3 : a+=((uint32_t)k[2])<<16; /* fall through */ + case 2 : a+=((uint32_t)k[1])<<8; /* fall through */ + case 1 : a+=k[0]; + break; + case 0 : return c; + } + } + + final(a,b,c); + return c; +} diff --git a/src/jansson/src/memory.c b/src/jansson/src/memory.c new file mode 100644 index 00000000..2733035a --- /dev/null +++ b/src/jansson/src/memory.c @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2009-2016 Petri Lehtinen + * Copyright (c) 2011-2012 Basile Starynkevitch + * + * Jansson is free software; you can redistribute it and/or modify it + * under the terms of the MIT license. See LICENSE for details. + */ + +#include +#include + +#include "jansson.h" +#include "jansson_private.h" + +/* C89 allows these to be macros */ +#undef malloc +#undef free + +/* memory function pointers */ +static json_malloc_t do_malloc = malloc; +static json_free_t do_free = free; + +void *jsonp_malloc(size_t size) { + if (!size) + return NULL; + + return (*do_malloc)(size); +} + +void jsonp_free(void *ptr) { + if (!ptr) + return; + + (*do_free)(ptr); +} + +char *jsonp_strdup(const char *str) { return jsonp_strndup(str, strlen(str)); } + +char *jsonp_strndup(const char *str, size_t len) { + char *new_str; + + new_str = jsonp_malloc(len + 1); + if (!new_str) + return NULL; + + memcpy(new_str, str, len); + new_str[len] = '\0'; + return new_str; +} + +void json_set_alloc_funcs(json_malloc_t malloc_fn, json_free_t free_fn) { + do_malloc = malloc_fn; + do_free = free_fn; +} + +void json_get_alloc_funcs(json_malloc_t *malloc_fn, json_free_t *free_fn) { + if (malloc_fn) + *malloc_fn = do_malloc; + if (free_fn) + *free_fn = do_free; +} diff --git a/src/jansson/src/pack_unpack.c b/src/jansson/src/pack_unpack.c new file mode 100644 index 00000000..04c116ef --- /dev/null +++ b/src/jansson/src/pack_unpack.c @@ -0,0 +1,937 @@ +/* + * Copyright (c) 2009-2016 Petri Lehtinen + * Copyright (c) 2011-2012 Graeme Smecher + * + * Jansson is free software; you can redistribute it and/or modify + * it under the terms of the MIT license. See LICENSE for details. + */ + +#include "jansson.h" +#include "jansson_private.h" +#include "utf.h" +#include + +typedef struct { + int line; + int column; + size_t pos; + char token; +} token_t; + +typedef struct { + const char *start; + const char *fmt; + token_t prev_token; + token_t token; + token_t next_token; + json_error_t *error; + size_t flags; + int line; + int column; + size_t pos; + int has_error; +} scanner_t; + +#define token(scanner) ((scanner)->token.token) + +static const char *const type_names[] = {"object", "array", "string", "integer", + "real", "true", "false", "null"}; + +#define type_name(x) type_names[json_typeof(x)] + +static const char unpack_value_starters[] = "{[siIbfFOon"; + +static void scanner_init(scanner_t *s, json_error_t *error, size_t flags, + const char *fmt) { + s->error = error; + s->flags = flags; + s->fmt = s->start = fmt; + memset(&s->prev_token, 0, sizeof(token_t)); + memset(&s->token, 0, sizeof(token_t)); + memset(&s->next_token, 0, sizeof(token_t)); + s->line = 1; + s->column = 0; + s->pos = 0; + s->has_error = 0; +} + +static void next_token(scanner_t *s) { + const char *t; + s->prev_token = s->token; + + if (s->next_token.line) { + s->token = s->next_token; + s->next_token.line = 0; + return; + } + + if (!token(s) && !*s->fmt) + return; + + t = s->fmt; + s->column++; + s->pos++; + + /* skip space and ignored chars */ + while (*t == ' ' || *t == '\t' || *t == '\n' || *t == ',' || *t == ':') { + if (*t == '\n') { + s->line++; + s->column = 1; + } else + s->column++; + + s->pos++; + t++; + } + + s->token.token = *t; + s->token.line = s->line; + s->token.column = s->column; + s->token.pos = s->pos; + + if (*t) + t++; + s->fmt = t; +} + +static void prev_token(scanner_t *s) { + s->next_token = s->token; + s->token = s->prev_token; +} + +static void set_error(scanner_t *s, const char *source, enum json_error_code code, + const char *fmt, ...) { + va_list ap; + va_start(ap, fmt); + + jsonp_error_vset(s->error, s->token.line, s->token.column, s->token.pos, code, fmt, + ap); + + jsonp_error_set_source(s->error, source); + + va_end(ap); +} + +static json_t *pack(scanner_t *s, va_list *ap); + +/* ours will be set to 1 if jsonp_free() must be called for the result + afterwards */ +static char *read_string(scanner_t *s, va_list *ap, const char *purpose, size_t *out_len, + int *ours, int optional) { + char t; + strbuffer_t strbuff; + const char *str; + size_t length; + + next_token(s); + t = token(s); + prev_token(s); + + *ours = 0; + if (t != '#' && t != '%' && t != '+') { + /* Optimize the simple case */ + str = va_arg(*ap, const char *); + + if (!str) { + if (!optional) { + set_error(s, "", json_error_null_value, "NULL %s", purpose); + s->has_error = 1; + } + return NULL; + } + + length = strlen(str); + + if (!utf8_check_string(str, length)) { + set_error(s, "", json_error_invalid_utf8, "Invalid UTF-8 %s", purpose); + s->has_error = 1; + return NULL; + } + + *out_len = length; + return (char *)str; + } else if (optional) { + set_error(s, "", json_error_invalid_format, + "Cannot use '%c' on optional strings", t); + s->has_error = 1; + + return NULL; + } + + if (strbuffer_init(&strbuff)) { + set_error(s, "", json_error_out_of_memory, "Out of memory"); + s->has_error = 1; + } + + while (1) { + str = va_arg(*ap, const char *); + if (!str) { + set_error(s, "", json_error_null_value, "NULL %s", purpose); + s->has_error = 1; + } + + next_token(s); + + if (token(s) == '#') { + length = va_arg(*ap, int); + } else if (token(s) == '%') { + length = va_arg(*ap, size_t); + } else { + prev_token(s); + length = s->has_error ? 0 : strlen(str); + } + + if (!s->has_error && strbuffer_append_bytes(&strbuff, str, length) == -1) { + set_error(s, "", json_error_out_of_memory, "Out of memory"); + s->has_error = 1; + } + + next_token(s); + if (token(s) != '+') { + prev_token(s); + break; + } + } + + if (s->has_error) { + strbuffer_close(&strbuff); + return NULL; + } + + if (!utf8_check_string(strbuff.value, strbuff.length)) { + set_error(s, "", json_error_invalid_utf8, "Invalid UTF-8 %s", purpose); + strbuffer_close(&strbuff); + s->has_error = 1; + return NULL; + } + + *out_len = strbuff.length; + *ours = 1; + return strbuffer_steal_value(&strbuff); +} + +static json_t *pack_object(scanner_t *s, va_list *ap) { + json_t *object = json_object(); + next_token(s); + + while (token(s) != '}') { + char *key; + size_t len; + int ours; + json_t *value; + char valueOptional; + + if (!token(s)) { + set_error(s, "", json_error_invalid_format, + "Unexpected end of format string"); + goto error; + } + + if (token(s) != 's') { + set_error(s, "", json_error_invalid_format, + "Expected format 's', got '%c'", token(s)); + goto error; + } + + key = read_string(s, ap, "object key", &len, &ours, 0); + + next_token(s); + + next_token(s); + valueOptional = token(s); + prev_token(s); + + value = pack(s, ap); + if (!value) { + if (ours) + jsonp_free(key); + + if (valueOptional != '*') { + set_error(s, "", json_error_null_value, "NULL object value"); + s->has_error = 1; + } + + next_token(s); + continue; + } + + if (s->has_error) + json_decref(value); + + if (!s->has_error && json_object_set_new_nocheck(object, key, value)) { + set_error(s, "", json_error_out_of_memory, + "Unable to add key \"%s\"", key); + s->has_error = 1; + } + + if (ours) + jsonp_free(key); + + next_token(s); + } + + if (!s->has_error) + return object; + +error: + json_decref(object); + return NULL; +} + +static json_t *pack_array(scanner_t *s, va_list *ap) { + json_t *array = json_array(); + next_token(s); + + while (token(s) != ']') { + json_t *value; + char valueOptional; + + if (!token(s)) { + set_error(s, "", json_error_invalid_format, + "Unexpected end of format string"); + /* Format string errors are unrecoverable. */ + goto error; + } + + next_token(s); + valueOptional = token(s); + prev_token(s); + + value = pack(s, ap); + if (!value) { + if (valueOptional != '*') { + s->has_error = 1; + } + + next_token(s); + continue; + } + + if (s->has_error) + json_decref(value); + + if (!s->has_error && json_array_append_new(array, value)) { + set_error(s, "", json_error_out_of_memory, + "Unable to append to array"); + s->has_error = 1; + } + + next_token(s); + } + + if (!s->has_error) + return array; + +error: + json_decref(array); + return NULL; +} + +static json_t *pack_string(scanner_t *s, va_list *ap) { + char *str; + char t; + size_t len; + int ours; + int optional; + + next_token(s); + t = token(s); + optional = t == '?' || t == '*'; + if (!optional) + prev_token(s); + + str = read_string(s, ap, "string", &len, &ours, optional); + + if (!str) + return t == '?' && !s->has_error ? json_null() : NULL; + + if (s->has_error) { + /* It's impossible to reach this point if ours != 0, do not free str. */ + return NULL; + } + + if (ours) + return jsonp_stringn_nocheck_own(str, len); + + return json_stringn_nocheck(str, len); +} + +static json_t *pack_object_inter(scanner_t *s, va_list *ap, int need_incref) { + json_t *json; + char ntoken; + + next_token(s); + ntoken = token(s); + + if (ntoken != '?' && ntoken != '*') + prev_token(s); + + json = va_arg(*ap, json_t *); + + if (json) + return need_incref ? json_incref(json) : json; + + switch (ntoken) { + case '?': + return json_null(); + case '*': + return NULL; + default: + break; + } + + set_error(s, "", json_error_null_value, "NULL object"); + s->has_error = 1; + return NULL; +} + +static json_t *pack_integer(scanner_t *s, json_int_t value) { + json_t *json = json_integer(value); + + if (!json) { + set_error(s, "", json_error_out_of_memory, "Out of memory"); + s->has_error = 1; + } + + return json; +} + +static json_t *pack_real(scanner_t *s, double value) { + /* Allocate without setting value so we can identify OOM error. */ + json_t *json = json_real(0.0); + + if (!json) { + set_error(s, "", json_error_out_of_memory, "Out of memory"); + s->has_error = 1; + + return NULL; + } + + if (json_real_set(json, value)) { + json_decref(json); + + set_error(s, "", json_error_numeric_overflow, + "Invalid floating point value"); + s->has_error = 1; + + return NULL; + } + + return json; +} + +static json_t *pack(scanner_t *s, va_list *ap) { + switch (token(s)) { + case '{': + return pack_object(s, ap); + + case '[': + return pack_array(s, ap); + + case 's': /* string */ + return pack_string(s, ap); + + case 'n': /* null */ + return json_null(); + + case 'b': /* boolean */ + return va_arg(*ap, int) ? json_true() : json_false(); + + case 'i': /* integer from int */ + return pack_integer(s, va_arg(*ap, int)); + + case 'I': /* integer from json_int_t */ + return pack_integer(s, va_arg(*ap, json_int_t)); + + case 'f': /* real */ + return pack_real(s, va_arg(*ap, double)); + + case 'O': /* a json_t object; increments refcount */ + return pack_object_inter(s, ap, 1); + + case 'o': /* a json_t object; doesn't increment refcount */ + return pack_object_inter(s, ap, 0); + + default: + set_error(s, "", json_error_invalid_format, + "Unexpected format character '%c'", token(s)); + s->has_error = 1; + return NULL; + } +} + +static int unpack(scanner_t *s, json_t *root, va_list *ap); + +static int unpack_object(scanner_t *s, json_t *root, va_list *ap) { + int ret = -1; + int strict = 0; + int gotopt = 0; + + /* Use a set (emulated by a hashtable) to check that all object + keys are accessed. Checking that the correct number of keys + were accessed is not enough, as the same key can be unpacked + multiple times. + */ + hashtable_t key_set; + + if (hashtable_init(&key_set)) { + set_error(s, "", json_error_out_of_memory, "Out of memory"); + return -1; + } + + if (root && !json_is_object(root)) { + set_error(s, "", json_error_wrong_type, "Expected object, got %s", + type_name(root)); + goto out; + } + next_token(s); + + while (token(s) != '}') { + const char *key; + json_t *value; + int opt = 0; + + if (strict != 0) { + set_error(s, "", json_error_invalid_format, + "Expected '}' after '%c', got '%c'", (strict == 1 ? '!' : '*'), + token(s)); + goto out; + } + + if (!token(s)) { + set_error(s, "", json_error_invalid_format, + "Unexpected end of format string"); + goto out; + } + + if (token(s) == '!' || token(s) == '*') { + strict = (token(s) == '!' ? 1 : -1); + next_token(s); + continue; + } + + if (token(s) != 's') { + set_error(s, "", json_error_invalid_format, + "Expected format 's', got '%c'", token(s)); + goto out; + } + + key = va_arg(*ap, const char *); + if (!key) { + set_error(s, "", json_error_null_value, "NULL object key"); + goto out; + } + + next_token(s); + + if (token(s) == '?') { + opt = gotopt = 1; + next_token(s); + } + + if (!root) { + /* skipping */ + value = NULL; + } else { + value = json_object_get(root, key); + if (!value && !opt) { + set_error(s, "", json_error_item_not_found, + "Object item not found: %s", key); + goto out; + } + } + + if (unpack(s, value, ap)) + goto out; + + hashtable_set(&key_set, key, strlen(key), json_null()); + next_token(s); + } + + if (strict == 0 && (s->flags & JSON_STRICT)) + strict = 1; + + if (root && strict == 1) { + /* We need to check that all non optional items have been parsed */ + const char *key; + size_t key_len; + /* keys_res is 1 for uninitialized, 0 for success, -1 for error. */ + int keys_res = 1; + strbuffer_t unrecognized_keys; + json_t *value; + long unpacked = 0; + + if (gotopt || json_object_size(root) != key_set.size) { + json_object_foreach(root, key, value) { + key_len = strlen(key); + if (!hashtable_get(&key_set, key, key_len)) { + unpacked++; + + /* Save unrecognized keys for the error message */ + if (keys_res == 1) { + keys_res = strbuffer_init(&unrecognized_keys); + } else if (!keys_res) { + keys_res = strbuffer_append_bytes(&unrecognized_keys, ", ", 2); + } + + if (!keys_res) + keys_res = + strbuffer_append_bytes(&unrecognized_keys, key, key_len); + } + } + } + if (unpacked) { + set_error(s, "", json_error_end_of_input_expected, + "%li object item(s) left unpacked: %s", unpacked, + keys_res ? "" : strbuffer_value(&unrecognized_keys)); + strbuffer_close(&unrecognized_keys); + goto out; + } + } + + ret = 0; + +out: + hashtable_close(&key_set); + return ret; +} + +static int unpack_array(scanner_t *s, json_t *root, va_list *ap) { + size_t i = 0; + int strict = 0; + + if (root && !json_is_array(root)) { + set_error(s, "", json_error_wrong_type, "Expected array, got %s", + type_name(root)); + return -1; + } + next_token(s); + + while (token(s) != ']') { + json_t *value; + + if (strict != 0) { + set_error(s, "", json_error_invalid_format, + "Expected ']' after '%c', got '%c'", (strict == 1 ? '!' : '*'), + token(s)); + return -1; + } + + if (!token(s)) { + set_error(s, "", json_error_invalid_format, + "Unexpected end of format string"); + return -1; + } + + if (token(s) == '!' || token(s) == '*') { + strict = (token(s) == '!' ? 1 : -1); + next_token(s); + continue; + } + + if (!strchr(unpack_value_starters, token(s))) { + set_error(s, "", json_error_invalid_format, + "Unexpected format character '%c'", token(s)); + return -1; + } + + if (!root) { + /* skipping */ + value = NULL; + } else { + value = json_array_get(root, i); + if (!value) { + set_error(s, "", json_error_index_out_of_range, + "Array index %lu out of range", (unsigned long)i); + return -1; + } + } + + if (unpack(s, value, ap)) + return -1; + + next_token(s); + i++; + } + + if (strict == 0 && (s->flags & JSON_STRICT)) + strict = 1; + + if (root && strict == 1 && i != json_array_size(root)) { + long diff = (long)json_array_size(root) - (long)i; + set_error(s, "", json_error_end_of_input_expected, + "%li array item(s) left unpacked", diff); + return -1; + } + + return 0; +} + +static int unpack(scanner_t *s, json_t *root, va_list *ap) { + switch (token(s)) { + case '{': + return unpack_object(s, root, ap); + + case '[': + return unpack_array(s, root, ap); + + case 's': + if (root && !json_is_string(root)) { + set_error(s, "", json_error_wrong_type, + "Expected string, got %s", type_name(root)); + return -1; + } + + if (!(s->flags & JSON_VALIDATE_ONLY)) { + const char **str_target; + size_t *len_target = NULL; + + str_target = va_arg(*ap, const char **); + if (!str_target) { + set_error(s, "", json_error_null_value, "NULL string argument"); + return -1; + } + + next_token(s); + + if (token(s) == '%') { + len_target = va_arg(*ap, size_t *); + if (!len_target) { + set_error(s, "", json_error_null_value, + "NULL string length argument"); + return -1; + } + } else + prev_token(s); + + if (root) { + *str_target = json_string_value(root); + if (len_target) + *len_target = json_string_length(root); + } + } + return 0; + + case 'i': + if (root && !json_is_integer(root)) { + set_error(s, "", json_error_wrong_type, + "Expected integer, got %s", type_name(root)); + return -1; + } + + if (!(s->flags & JSON_VALIDATE_ONLY)) { + int *target = va_arg(*ap, int *); + if (root) + *target = (int)json_integer_value(root); + } + + return 0; + + case 'I': + if (root && !json_is_integer(root)) { + set_error(s, "", json_error_wrong_type, + "Expected integer, got %s", type_name(root)); + return -1; + } + + if (!(s->flags & JSON_VALIDATE_ONLY)) { + json_int_t *target = va_arg(*ap, json_int_t *); + if (root) + *target = json_integer_value(root); + } + + return 0; + + case 'b': + if (root && !json_is_boolean(root)) { + set_error(s, "", json_error_wrong_type, + "Expected true or false, got %s", type_name(root)); + return -1; + } + + if (!(s->flags & JSON_VALIDATE_ONLY)) { + int *target = va_arg(*ap, int *); + if (root) + *target = json_is_true(root); + } + + return 0; + + case 'f': + if (root && !json_is_real(root)) { + set_error(s, "", json_error_wrong_type, + "Expected real, got %s", type_name(root)); + return -1; + } + + if (!(s->flags & JSON_VALIDATE_ONLY)) { + double *target = va_arg(*ap, double *); + if (root) + *target = json_real_value(root); + } + + return 0; + + case 'F': + if (root && !json_is_number(root)) { + set_error(s, "", json_error_wrong_type, + "Expected real or integer, got %s", type_name(root)); + return -1; + } + + if (!(s->flags & JSON_VALIDATE_ONLY)) { + double *target = va_arg(*ap, double *); + if (root) + *target = json_number_value(root); + } + + return 0; + + case 'O': + if (root && !(s->flags & JSON_VALIDATE_ONLY)) + json_incref(root); + /* Fall through */ + + case 'o': + if (!(s->flags & JSON_VALIDATE_ONLY)) { + json_t **target = va_arg(*ap, json_t **); + if (root) + *target = root; + } + + return 0; + + case 'n': + /* Never assign, just validate */ + if (root && !json_is_null(root)) { + set_error(s, "", json_error_wrong_type, + "Expected null, got %s", type_name(root)); + return -1; + } + return 0; + + default: + set_error(s, "", json_error_invalid_format, + "Unexpected format character '%c'", token(s)); + return -1; + } +} + +json_t *json_vpack_ex(json_error_t *error, size_t flags, const char *fmt, va_list ap) { + scanner_t s; + va_list ap_copy; + json_t *value; + + if (!fmt || !*fmt) { + jsonp_error_init(error, ""); + jsonp_error_set(error, -1, -1, 0, json_error_invalid_argument, + "NULL or empty format string"); + return NULL; + } + jsonp_error_init(error, NULL); + + scanner_init(&s, error, flags, fmt); + next_token(&s); + + va_copy(ap_copy, ap); + value = pack(&s, &ap_copy); + va_end(ap_copy); + + /* This will cover all situations where s.has_error is true */ + if (!value) + return NULL; + + next_token(&s); + if (token(&s)) { + json_decref(value); + set_error(&s, "", json_error_invalid_format, + "Garbage after format string"); + return NULL; + } + + return value; +} + +json_t *json_pack_ex(json_error_t *error, size_t flags, const char *fmt, ...) { + json_t *value; + va_list ap; + + va_start(ap, fmt); + value = json_vpack_ex(error, flags, fmt, ap); + va_end(ap); + + return value; +} + +json_t *json_pack(const char *fmt, ...) { + json_t *value; + va_list ap; + + va_start(ap, fmt); + value = json_vpack_ex(NULL, 0, fmt, ap); + va_end(ap); + + return value; +} + +int json_vunpack_ex(json_t *root, json_error_t *error, size_t flags, const char *fmt, + va_list ap) { + scanner_t s; + va_list ap_copy; + + if (!root) { + jsonp_error_init(error, ""); + jsonp_error_set(error, -1, -1, 0, json_error_null_value, "NULL root value"); + return -1; + } + + if (!fmt || !*fmt) { + jsonp_error_init(error, ""); + jsonp_error_set(error, -1, -1, 0, json_error_invalid_argument, + "NULL or empty format string"); + return -1; + } + jsonp_error_init(error, NULL); + + scanner_init(&s, error, flags, fmt); + next_token(&s); + + va_copy(ap_copy, ap); + if (unpack(&s, root, &ap_copy)) { + va_end(ap_copy); + return -1; + } + va_end(ap_copy); + + next_token(&s); + if (token(&s)) { + set_error(&s, "", json_error_invalid_format, + "Garbage after format string"); + return -1; + } + + return 0; +} + +int json_unpack_ex(json_t *root, json_error_t *error, size_t flags, const char *fmt, + ...) { + int ret; + va_list ap; + + va_start(ap, fmt); + ret = json_vunpack_ex(root, error, flags, fmt, ap); + va_end(ap); + + return ret; +} + +int json_unpack(json_t *root, const char *fmt, ...) { + int ret; + va_list ap; + + va_start(ap, fmt); + ret = json_vunpack_ex(root, NULL, 0, fmt, ap); + va_end(ap); + + return ret; +} diff --git a/src/jansson/src/strbuffer.c b/src/jansson/src/strbuffer.c index 4e866bd6..d9bcd439 100644 --- a/src/jansson/src/strbuffer.c +++ b/src/jansson/src/strbuffer.c @@ -1,26 +1,29 @@ /* - * Copyright (c) 2009, 2010 Petri Lehtinen + * Copyright (c) 2009-2016 Petri Lehtinen * * Jansson is free software; you can redistribute it and/or modify * it under the terms of the MIT license. See LICENSE for details. */ +#ifndef _GNU_SOURCE #define _GNU_SOURCE +#endif + +#include "strbuffer.h" +#include "jansson_private.h" #include #include -#include "jansson_private.h" -#include "strbuffer.h" -#define STRBUFFER_MIN_SIZE 16 -#define STRBUFFER_FACTOR 2 +#define STRBUFFER_MIN_SIZE 16 +#define STRBUFFER_FACTOR 2 +#define STRBUFFER_SIZE_MAX ((size_t)-1) -int strbuffer_init(strbuffer_t *strbuff) -{ +int strbuffer_init(strbuffer_t *strbuff) { strbuff->size = STRBUFFER_MIN_SIZE; strbuff->length = 0; - strbuff->value = malloc(strbuff->size); - if(!strbuff->value) + strbuff->value = jsonp_malloc(strbuff->size); + if (!strbuff->value) return -1; /* initialize to empty */ @@ -28,52 +31,54 @@ int strbuffer_init(strbuffer_t *strbuff) return 0; } -void strbuffer_close(strbuffer_t *strbuff) -{ - free(strbuff->value); +void strbuffer_close(strbuffer_t *strbuff) { + if (strbuff->value) + jsonp_free(strbuff->value); + strbuff->size = 0; strbuff->length = 0; strbuff->value = NULL; } -void strbuffer_clear(strbuffer_t *strbuff) -{ +void strbuffer_clear(strbuffer_t *strbuff) { strbuff->length = 0; strbuff->value[0] = '\0'; } -const char *strbuffer_value(const strbuffer_t *strbuff) -{ - return strbuff->value; -} +const char *strbuffer_value(const strbuffer_t *strbuff) { return strbuff->value; } -char *strbuffer_steal_value(strbuffer_t *strbuff) -{ +char *strbuffer_steal_value(strbuffer_t *strbuff) { char *result = strbuff->value; - strbuffer_init(strbuff); + strbuff->value = NULL; return result; } -int strbuffer_append(strbuffer_t *strbuff, const char *string) -{ - return strbuffer_append_bytes(strbuff, string, strlen(string)); -} - -int strbuffer_append_byte(strbuffer_t *strbuff, char byte) -{ +int strbuffer_append_byte(strbuffer_t *strbuff, char byte) { return strbuffer_append_bytes(strbuff, &byte, 1); } -int strbuffer_append_bytes(strbuffer_t *strbuff, const char *data, int size) -{ - if(strbuff->length + size >= strbuff->size) - { - strbuff->size = max(strbuff->size * STRBUFFER_FACTOR, - strbuff->length + size + 1); +int strbuffer_append_bytes(strbuffer_t *strbuff, const char *data, size_t size) { + if (size >= strbuff->size - strbuff->length) { + size_t new_size; + char *new_value; + + /* avoid integer overflow */ + if (strbuff->size > STRBUFFER_SIZE_MAX / STRBUFFER_FACTOR || + size > STRBUFFER_SIZE_MAX - 1 || + strbuff->length > STRBUFFER_SIZE_MAX - 1 - size) + return -1; + + new_size = max(strbuff->size * STRBUFFER_FACTOR, strbuff->length + size + 1); - strbuff->value = realloc(strbuff->value, strbuff->size); - if(!strbuff->value) + new_value = jsonp_malloc(new_size); + if (!new_value) return -1; + + memcpy(new_value, strbuff->value, strbuff->length); + + jsonp_free(strbuff->value); + strbuff->value = new_value; + strbuff->size = new_size; } memcpy(strbuff->value + strbuff->length, data, size); @@ -83,13 +88,11 @@ int strbuffer_append_bytes(strbuffer_t *strbuff, const char *data, int size) return 0; } -char strbuffer_pop(strbuffer_t *strbuff) -{ - if(strbuff->length > 0) { +char strbuffer_pop(strbuffer_t *strbuff) { + if (strbuff->length > 0) { char c = strbuff->value[--strbuff->length]; strbuff->value[strbuff->length] = '\0'; return c; - } - else + } else return '\0'; } diff --git a/src/jansson/src/strbuffer.h b/src/jansson/src/strbuffer.h index f4c5f771..70f2646d 100644 --- a/src/jansson/src/strbuffer.h +++ b/src/jansson/src/strbuffer.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2009, 2010 Petri Lehtinen + * Copyright (c) 2009-2016 Petri Lehtinen * * Jansson is free software; you can redistribute it and/or modify * it under the terms of the MIT license. See LICENSE for details. @@ -8,23 +8,27 @@ #ifndef STRBUFFER_H #define STRBUFFER_H +#include "jansson.h" +#include + typedef struct { char *value; - int length; /* bytes used */ - int size; /* bytes allocated */ + size_t length; /* bytes used */ + size_t size; /* bytes allocated */ } strbuffer_t; -int strbuffer_init(strbuffer_t *strbuff); +int strbuffer_init(strbuffer_t *strbuff) JANSSON_ATTRS((warn_unused_result)); void strbuffer_close(strbuffer_t *strbuff); void strbuffer_clear(strbuffer_t *strbuff); const char *strbuffer_value(const strbuffer_t *strbuff); + +/* Steal the value and close the strbuffer */ char *strbuffer_steal_value(strbuffer_t *strbuff); -int strbuffer_append(strbuffer_t *strbuff, const char *string); int strbuffer_append_byte(strbuffer_t *strbuff, char byte); -int strbuffer_append_bytes(strbuffer_t *strbuff, const char *data, int size); +int strbuffer_append_bytes(strbuffer_t *strbuff, const char *data, size_t size); char strbuffer_pop(strbuffer_t *strbuff); diff --git a/src/jansson/src/strconv.c b/src/jansson/src/strconv.c new file mode 100644 index 00000000..c6f4fd16 --- /dev/null +++ b/src/jansson/src/strconv.c @@ -0,0 +1,132 @@ +#include "jansson_private.h" +#include "strbuffer.h" +#include +#include +#include +#include +#include + +/* need jansson_private_config.h to get the correct snprintf */ +#ifdef HAVE_CONFIG_H +#include +#endif + +#if JSON_HAVE_LOCALECONV +#include + +/* + - This code assumes that the decimal separator is exactly one + character. + + - If setlocale() is called by another thread between the call to + localeconv() and the call to sprintf() or strtod(), the result may + be wrong. setlocale() is not thread-safe and should not be used + this way. Multi-threaded programs should use uselocale() instead. +*/ + +static void to_locale(strbuffer_t *strbuffer) { + const char *point; + char *pos; + + point = localeconv()->decimal_point; + if (*point == '.') { + /* No conversion needed */ + return; + } + + pos = strchr(strbuffer->value, '.'); + if (pos) + *pos = *point; +} + +static void from_locale(char *buffer) { + const char *point; + char *pos; + + point = localeconv()->decimal_point; + if (*point == '.') { + /* No conversion needed */ + return; + } + + pos = strchr(buffer, *point); + if (pos) + *pos = '.'; +} +#endif + +int jsonp_strtod(strbuffer_t *strbuffer, double *out) { + double value; + char *end; + +#if JSON_HAVE_LOCALECONV + to_locale(strbuffer); +#endif + + errno = 0; + value = strtod(strbuffer->value, &end); + assert(end == strbuffer->value + strbuffer->length); + + if ((value == HUGE_VAL || value == -HUGE_VAL) && errno == ERANGE) { + /* Overflow */ + return -1; + } + + *out = value; + return 0; +} + +int jsonp_dtostr(char *buffer, size_t size, double value, int precision) { + int ret; + char *start, *end; + size_t length; + + if (precision == 0) + precision = 17; + + ret = snprintf(buffer, size, "%.*g", precision, value); + if (ret < 0) + return -1; + + length = (size_t)ret; + if (length >= size) + return -1; + +#if JSON_HAVE_LOCALECONV + from_locale(buffer); +#endif + + /* Make sure there's a dot or 'e' in the output. Otherwise + a real is converted to an integer when decoding */ + if (strchr(buffer, '.') == NULL && strchr(buffer, 'e') == NULL) { + if (length + 3 >= size) { + /* No space to append ".0" */ + return -1; + } + buffer[length] = '.'; + buffer[length + 1] = '0'; + buffer[length + 2] = '\0'; + length += 2; + } + + /* Remove leading '+' from positive exponent. Also remove leading + zeros from exponents (added by some printf() implementations) */ + start = strchr(buffer, 'e'); + if (start) { + start++; + end = start + 1; + + if (*start == '-') + start++; + + while (*end == '0') + end++; + + if (end != start) { + memmove(start, end, length - (size_t)(end - buffer)); + length -= (size_t)(end - start); + } + } + + return (int)length; +} diff --git a/src/jansson/src/utf.c b/src/jansson/src/utf.c index eb20f78a..28b2f7d3 100644 --- a/src/jansson/src/utf.c +++ b/src/jansson/src/utf.c @@ -1,111 +1,89 @@ /* - * Copyright (c) 2009, 2010 Petri Lehtinen + * Copyright (c) 2009-2016 Petri Lehtinen * * Jansson is free software; you can redistribute it and/or modify * it under the terms of the MIT license. See LICENSE for details. */ -#include #include "utf.h" +#include -int utf8_encode(int32_t codepoint, char *buffer, int *size) -{ - if(codepoint < 0) +int utf8_encode(int32_t codepoint, char *buffer, size_t *size) { + if (codepoint < 0) return -1; - else if(codepoint < 0x80) - { + else if (codepoint < 0x80) { buffer[0] = (char)codepoint; *size = 1; - } - else if(codepoint < 0x800) - { + } else if (codepoint < 0x800) { buffer[0] = 0xC0 + ((codepoint & 0x7C0) >> 6); buffer[1] = 0x80 + ((codepoint & 0x03F)); *size = 2; - } - else if(codepoint < 0x10000) - { + } else if (codepoint < 0x10000) { buffer[0] = 0xE0 + ((codepoint & 0xF000) >> 12); buffer[1] = 0x80 + ((codepoint & 0x0FC0) >> 6); buffer[2] = 0x80 + ((codepoint & 0x003F)); *size = 3; - } - else if(codepoint <= 0x10FFFF) - { + } else if (codepoint <= 0x10FFFF) { buffer[0] = 0xF0 + ((codepoint & 0x1C0000) >> 18); buffer[1] = 0x80 + ((codepoint & 0x03F000) >> 12); buffer[2] = 0x80 + ((codepoint & 0x000FC0) >> 6); buffer[3] = 0x80 + ((codepoint & 0x00003F)); *size = 4; - } - else + } else return -1; return 0; } -int utf8_check_first(char byte) -{ +size_t utf8_check_first(char byte) { unsigned char u = (unsigned char)byte; - if(u < 0x80) + if (u < 0x80) return 1; - if(/* 0x80 <= u && */u <= 0xBF) { + if (0x80 <= u && u <= 0xBF) { /* second, third or fourth byte of a multi-byte sequence, i.e. a "continuation byte" */ return 0; - } - else if(u == 0xC0 || u == 0xC1) { + } else if (u == 0xC0 || u == 0xC1) { /* overlong encoding of an ASCII byte */ return 0; - } - else if(/*0xC2 <= u && */u <= 0xDF) { + } else if (0xC2 <= u && u <= 0xDF) { /* 2-byte sequence */ return 2; } - else if(/*0xE0 <= u && */u <= 0xEF) { + else if (0xE0 <= u && u <= 0xEF) { /* 3-byte sequence */ return 3; - } - else if(/*0xF0 <= u && */u <= 0xF4) { + } else if (0xF0 <= u && u <= 0xF4) { /* 4-byte sequence */ return 4; - } - else { /* u >= 0xF5 */ + } else { /* u >= 0xF5 */ /* Restricted (start of 4-, 5- or 6-byte sequence) or invalid UTF-8 */ return 0; } } -int utf8_check_full(const char *buffer, int size, int32_t *codepoint) -{ - int i; +size_t utf8_check_full(const char *buffer, size_t size, int32_t *codepoint) { + size_t i; int32_t value = 0; unsigned char u = (unsigned char)buffer[0]; - if(size == 2) - { + if (size == 2) { value = u & 0x1F; - } - else if(size == 3) - { + } else if (size == 3) { value = u & 0xF; - } - else if(size == 4) - { + } else if (size == 4) { value = u & 0x7; - } - else + } else return 0; - for(i = 1; i < size; i++) - { + for (i = 1; i < size; i++) { u = (unsigned char)buffer[i]; - if(u < 0x80 || u > 0xBF) { + if (u < 0x80 || u > 0xBF) { /* not a continuation byte */ return 0; } @@ -113,73 +91,64 @@ int utf8_check_full(const char *buffer, int size, int32_t *codepoint) value = (value << 6) + (u & 0x3F); } - if(value > 0x10FFFF) { + if (value > 0x10FFFF) { /* not in Unicode range */ return 0; } - else if(0xD800 <= value && value <= 0xDFFF) { + else if (0xD800 <= value && value <= 0xDFFF) { /* invalid code point (UTF-16 surrogate halves) */ return 0; } - else if((size == 2 && value < 0x80) || - (size == 3 && value < 0x800) || - (size == 4 && value < 0x10000)) { + else if ((size == 2 && value < 0x80) || (size == 3 && value < 0x800) || + (size == 4 && value < 0x10000)) { /* overlong encoding */ return 0; } - if(codepoint) + if (codepoint) *codepoint = value; return 1; } -const char *utf8_iterate(const char *buffer, int32_t *codepoint) -{ - int count; +const char *utf8_iterate(const char *buffer, size_t bufsize, int32_t *codepoint) { + size_t count; int32_t value; - if(!*buffer) + if (!bufsize) return buffer; count = utf8_check_first(buffer[0]); - if(count <= 0) + if (count <= 0) return NULL; - if(count == 1) + if (count == 1) value = (unsigned char)buffer[0]; - else - { - if(!utf8_check_full(buffer, count, &value)) + else { + if (count > bufsize || !utf8_check_full(buffer, count, &value)) return NULL; } - if(codepoint) + if (codepoint) *codepoint = value; return buffer + count; } -int utf8_check_string(const char *string, int length) -{ - int i; - - if(length == -1) - length = strlen(string); +int utf8_check_string(const char *string, size_t length) { + size_t i; - for(i = 0; i < length; i++) - { - int count = utf8_check_first(string[i]); - if(count == 0) + for (i = 0; i < length; i++) { + size_t count = utf8_check_first(string[i]); + if (count == 0) return 0; - else if(count > 1) - { - if(i + count > length) + else if (count > 1) { + if (count > length - i) return 0; - if(!utf8_check_full(&string[i], count, NULL)) + if (!utf8_check_full(&string[i], count, NULL)) return 0; i += count - 1; diff --git a/src/jansson/src/utf.h b/src/jansson/src/utf.h index 862c5489..15212230 100644 --- a/src/jansson/src/utf.h +++ b/src/jansson/src/utf.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2009, 2010 Petri Lehtinen + * Copyright (c) 2009-2016 Petri Lehtinen * * Jansson is free software; you can redistribute it and/or modify * it under the terms of the MIT license. See LICENSE for details. @@ -9,31 +9,18 @@ #define UTF_H #ifdef HAVE_CONFIG_H -#include - -#ifdef HAVE_INTTYPES_H -/* inttypes.h includes stdint.h in a standard environment, so there's -no need to include stdint.h separately. If inttypes.h doesn't define -int32_t, it's defined in config.h. */ -#include -#endif /* HAVE_INTTYPES_H */ - -#else /* !HAVE_CONFIG_H */ -#ifdef _WIN32 -typedef int int32_t; -#else /* !_WIN32 */ -/* Assume a standard environment */ -#include -#endif /* _WIN32 */ +#include +#endif -#endif /* HAVE_CONFIG_H */ +#include +#include -int utf8_encode(int codepoint, char *buffer, int *size); +int utf8_encode(int32_t codepoint, char *buffer, size_t *size); -int utf8_check_first(char byte); -int utf8_check_full(const char *buffer, int size, int32_t *codepoint); -const char *utf8_iterate(const char *buffer, int32_t *codepoint); +size_t utf8_check_first(char byte); +size_t utf8_check_full(const char *buffer, size_t size, int32_t *codepoint); +const char *utf8_iterate(const char *buffer, size_t size, int32_t *codepoint); -int utf8_check_string(const char *string, int length); +int utf8_check_string(const char *string, size_t length); #endif diff --git a/src/jansson/src/value.c b/src/jansson/src/value.c index 89d7b71d..07af0877 100644 --- a/src/jansson/src/value.c +++ b/src/jansson/src/value.c @@ -1,172 +1,186 @@ /* - * Copyright (c) 2009, 2010 Petri Lehtinen + * Copyright (c) 2009-2016 Petri Lehtinen * * Jansson is free software; you can redistribute it and/or modify * it under the terms of the MIT license. See LICENSE for details. */ +#ifndef _GNU_SOURCE #define _GNU_SOURCE +#endif +#ifdef HAVE_CONFIG_H +#include +#endif + +#include #include #include #include -#include +#ifdef HAVE_STDINT_H +#include +#endif + #include "hashtable.h" +#include "jansson.h" #include "jansson_private.h" #include "utf.h" +/* Work around nonstandard isnan() and isinf() implementations */ +#ifndef isnan +#ifndef __sun +static JSON_INLINE int isnan(double x) { return x != x; } +#endif +#endif +#ifndef isinf +static JSON_INLINE int isinf(double x) { return !isnan(x) && isnan(x - x); } +#endif -static JSON_INLINE void json_init(json_t *json, json_type type) -{ +json_t *do_deep_copy(const json_t *json, hashtable_t *parents); + +static JSON_INLINE void json_init(json_t *json, json_type type) { json->type = type; json->refcount = 1; } +int jsonp_loop_check(hashtable_t *parents, const json_t *json, char *key, size_t key_size, + size_t *key_len_out) { + size_t key_len = snprintf(key, key_size, "%p", json); -/*** object ***/ - -/* This macro just returns a pointer that's a few bytes backwards from - string. This makes it possible to pass a pointer to object_key_t - when only the string inside it is used, without actually creating - an object_key_t instance. */ -#define string_to_key(string) container_of(string, object_key_t, key) - -static size_t hash_key(const void *ptr) -{ - const char *str = ((const object_key_t *)ptr)->key; - - size_t hash = 5381; - size_t c; + if (key_len_out) + *key_len_out = key_len; - while((c = (size_t)*str)) - { - hash = ((hash << 5) + hash) + c; - str++; - } + if (hashtable_get(parents, key, key_len)) + return -1; - return hash; + return hashtable_set(parents, key, key_len, json_null()); } -static int key_equal(const void *ptr1, const void *ptr2) -{ - return strcmp(((const object_key_t *)ptr1)->key, - ((const object_key_t *)ptr2)->key) == 0; -} +/*** object ***/ -static void value_decref(void *value) -{ - json_decref((json_t *)value); -} +extern volatile uint32_t hashtable_seed; -json_t *json_object(void) -{ - json_object_t *object = malloc(sizeof(json_object_t)); - if(!object) +json_t *json_object(void) { + json_object_t *object = jsonp_malloc(sizeof(json_object_t)); + if (!object) return NULL; + + if (!hashtable_seed) { + /* Autoseed */ + json_object_seed(0); + } + json_init(&object->json, JSON_OBJECT); - if(hashtable_init(&object->hashtable, hash_key, key_equal, - free, value_decref)) - { - free(object); + if (hashtable_init(&object->hashtable)) { + jsonp_free(object); return NULL; } - object->serial = 0; - object->visited = 0; - return &object->json; } -static void json_delete_object(json_object_t *object) -{ +static void json_delete_object(json_object_t *object) { hashtable_close(&object->hashtable); - free(object); + jsonp_free(object); } -size_t json_object_size(const json_t *json) -{ +size_t json_object_size(const json_t *json) { json_object_t *object; - if(!json_is_object(json)) - return -1; + if (!json_is_object(json)) + return 0; object = json_to_object(json); return object->hashtable.size; } -json_t *json_object_get(const json_t *json, const char *key) -{ +json_t *json_object_get(const json_t *json, const char *key) { + if (!key) + return NULL; + + return json_object_getn(json, key, strlen(key)); +} + +json_t *json_object_getn(const json_t *json, const char *key, size_t key_len) { json_object_t *object; - if(!json_is_object(json)) + if (!key || !json_is_object(json)) return NULL; object = json_to_object(json); - return hashtable_get(&object->hashtable, string_to_key(key)); + return hashtable_get(&object->hashtable, key, key_len); +} + +int json_object_set_new_nocheck(json_t *json, const char *key, json_t *value) { + if (!key) { + json_decref(value); + return -1; + } + return json_object_setn_new_nocheck(json, key, strlen(key), value); } -int json_object_set_new_nocheck(json_t *json, const char *key, json_t *value) -{ +int json_object_setn_new_nocheck(json_t *json, const char *key, size_t key_len, + json_t *value) { json_object_t *object; - object_key_t *k; - if(!key || !value) + if (!value) return -1; - if(!json_is_object(json) || json == value) - { + if (!key || !json_is_object(json) || json == value) { json_decref(value); return -1; } object = json_to_object(json); - /* offsetof(...) returns the size of object_key_t without the - last, flexible member. This way, the correct amount is - allocated. */ - k = malloc(offsetof(object_key_t, key) + - strlen(key) + 1); if(!k) return -1; + if (hashtable_set(&object->hashtable, key, key_len, value)) { + json_decref(value); + return -1; + } - k->serial = object->serial++; - strcpy(k->key, key); + return 0; +} - if(hashtable_set(&object->hashtable, k, value)) - { +int json_object_set_new(json_t *json, const char *key, json_t *value) { + if (!key) { json_decref(value); return -1; } - return 0; + return json_object_setn_new(json, key, strlen(key), value); } -int json_object_set_new(json_t *json, const char *key, json_t *value) -{ - if(!key || !utf8_check_string(key, -1)) - { +int json_object_setn_new(json_t *json, const char *key, size_t key_len, json_t *value) { + if (!key || !utf8_check_string(key, key_len)) { json_decref(value); return -1; } - return json_object_set_new_nocheck(json, key, value); + return json_object_setn_new_nocheck(json, key, key_len, value); +} + +int json_object_del(json_t *json, const char *key) { + if (!key) + return -1; + + return json_object_deln(json, key, strlen(key)); } -int json_object_del(json_t *json, const char *key) -{ +int json_object_deln(json_t *json, const char *key, size_t key_len) { json_object_t *object; - if(!json_is_object(json)) + if (!key || !json_is_object(json)) return -1; object = json_to_object(json); - return hashtable_del(&object->hashtable, string_to_key(key)); + return hashtable_del(&object->hashtable, key, key_len); } -int json_object_clear(json_t *json) -{ +int json_object_clear(json_t *json) { json_object_t *object; - if(!json_is_object(json)) + if (!json_is_object(json)) return -1; object = json_to_object(json); @@ -175,248 +189,297 @@ int json_object_clear(json_t *json) return 0; } -int json_object_update(json_t *object, json_t *other) -{ - void *iter; +int json_object_update(json_t *object, json_t *other) { + const char *key; + json_t *value; - if(!json_is_object(object) || !json_is_object(other)) + if (!json_is_object(object) || !json_is_object(other)) return -1; - iter = json_object_iter(other); - while(iter) { - const char *key; - json_t *value; + json_object_foreach(other, key, value) { + if (json_object_set_nocheck(object, key, value)) + return -1; + } - key = json_object_iter_key(iter); - value = json_object_iter_value(iter); + return 0; +} - if(json_object_set_nocheck(object, key, value)) - return -1; +int json_object_update_existing(json_t *object, json_t *other) { + const char *key; + size_t key_len; + json_t *value; + + if (!json_is_object(object) || !json_is_object(other)) + return -1; + + json_object_keylen_foreach(other, key, key_len, value) { + if (json_object_getn(object, key, key_len)) + json_object_setn_nocheck(object, key, key_len, value); + } - iter = json_object_iter_next(other, iter); + return 0; +} + +int json_object_update_missing(json_t *object, json_t *other) { + const char *key; + json_t *value; + + if (!json_is_object(object) || !json_is_object(other)) + return -1; + + json_object_foreach(other, key, value) { + if (!json_object_get(object, key)) + json_object_set_nocheck(object, key, value); } return 0; } -void *json_object_iter(json_t *json) -{ +int do_object_update_recursive(json_t *object, json_t *other, hashtable_t *parents) { + const char *key; + size_t key_len; + json_t *value; + char loop_key[LOOP_KEY_LEN]; + int res = 0; + size_t loop_key_len; + + if (!json_is_object(object) || !json_is_object(other)) + return -1; + + if (jsonp_loop_check(parents, other, loop_key, sizeof(loop_key), &loop_key_len)) + return -1; + + json_object_keylen_foreach(other, key, key_len, value) { + json_t *v = json_object_get(object, key); + + if (json_is_object(v) && json_is_object(value)) { + if (do_object_update_recursive(v, value, parents)) { + res = -1; + break; + } + } else { + if (json_object_setn_nocheck(object, key, key_len, value)) { + res = -1; + break; + } + } + } + + hashtable_del(parents, loop_key, loop_key_len); + + return res; +} + +int json_object_update_recursive(json_t *object, json_t *other) { + int res; + hashtable_t parents_set; + + if (hashtable_init(&parents_set)) + return -1; + res = do_object_update_recursive(object, other, &parents_set); + hashtable_close(&parents_set); + + return res; +} + +void *json_object_iter(json_t *json) { json_object_t *object; - if(!json_is_object(json)) + if (!json_is_object(json)) return NULL; object = json_to_object(json); return hashtable_iter(&object->hashtable); } -void *json_object_iter_at(json_t *json, const char *key) -{ +void *json_object_iter_at(json_t *json, const char *key) { json_object_t *object; - if(!key || !json_is_object(json)) + if (!key || !json_is_object(json)) return NULL; object = json_to_object(json); - return hashtable_iter_at(&object->hashtable, string_to_key(key)); + return hashtable_iter_at(&object->hashtable, key, strlen(key)); } -void *json_object_iter_next(json_t *json, void *iter) -{ +void *json_object_iter_next(json_t *json, void *iter) { json_object_t *object; - if(!json_is_object(json) || iter == NULL) + if (!json_is_object(json) || iter == NULL) return NULL; object = json_to_object(json); return hashtable_iter_next(&object->hashtable, iter); } -const object_key_t *jsonp_object_iter_fullkey(void *iter) -{ - if(!iter) +const char *json_object_iter_key(void *iter) { + if (!iter) return NULL; return hashtable_iter_key(iter); } -const char *json_object_iter_key(void *iter) -{ - if(!iter) - return NULL; +size_t json_object_iter_key_len(void *iter) { + if (!iter) + return 0; - return jsonp_object_iter_fullkey(iter)->key; + return hashtable_iter_key_len(iter); } -json_t *json_object_iter_value(void *iter) -{ - if(!iter) +json_t *json_object_iter_value(void *iter) { + if (!iter) return NULL; return (json_t *)hashtable_iter_value(iter); } -int json_object_iter_set_new(json_t *json, void *iter, json_t *value) -{ - json_object_t *object; - - if(!json_is_object(json) || !iter || !value) +int json_object_iter_set_new(json_t *json, void *iter, json_t *value) { + if (!json_is_object(json) || !iter || !value) { + json_decref(value); return -1; + } - object = json_to_object(json); - hashtable_iter_set(&object->hashtable, iter, value); - + hashtable_iter_set(iter, value); return 0; } -static int json_object_equal(json_t *object1, json_t *object2) -{ - void *iter; +void *json_object_key_to_iter(const char *key) { + if (!key) + return NULL; - if(json_object_size(object1) != json_object_size(object2)) - return 0; + return hashtable_key_to_iter(key); +} - iter = json_object_iter(object1); - while(iter) - { - const char *key; - json_t *value1, *value2; +static int json_object_equal(const json_t *object1, const json_t *object2) { + const char *key; + const json_t *value1, *value2; - key = json_object_iter_key(iter); - value1 = json_object_iter_value(iter); + if (json_object_size(object1) != json_object_size(object2)) + return 0; + + json_object_foreach((json_t *)object1, key, value1) { value2 = json_object_get(object2, key); - if(!json_equal(value1, value2)) + if (!json_equal(value1, value2)) return 0; - - iter = json_object_iter_next(object1, iter); } return 1; } -static json_t *json_object_copy(json_t *object) -{ +static json_t *json_object_copy(json_t *object) { json_t *result; - void *iter; + + const char *key; + json_t *value; result = json_object(); - if(!result) + if (!result) return NULL; - iter = json_object_iter(object); - while(iter) - { - const char *key; - json_t *value; - - key = json_object_iter_key(iter); - value = json_object_iter_value(iter); - json_object_set_nocheck(result, key, value); - - iter = json_object_iter_next(object, iter); - } + json_object_foreach(object, key, value) json_object_set_nocheck(result, key, value); return result; } -static json_t *json_object_deep_copy(json_t *object) -{ +static json_t *json_object_deep_copy(const json_t *object, hashtable_t *parents) { json_t *result; void *iter; + char loop_key[LOOP_KEY_LEN]; + size_t loop_key_len; - result = json_object(); - if(!result) + if (jsonp_loop_check(parents, object, loop_key, sizeof(loop_key), &loop_key_len)) return NULL; - iter = json_object_iter(object); - while(iter) - { - const char *key; - json_t *value; + result = json_object(); + if (!result) + goto out; + /* Cannot use json_object_foreach because object has to be cast + non-const */ + iter = json_object_iter((json_t *)object); + while (iter) { + const char *key; + const json_t *value; key = json_object_iter_key(iter); value = json_object_iter_value(iter); - json_object_set_new_nocheck(result, key, json_deep_copy(value)); - iter = json_object_iter_next(object, iter); + if (json_object_set_new_nocheck(result, key, do_deep_copy(value, parents))) { + json_decref(result); + result = NULL; + break; + } + iter = json_object_iter_next((json_t *)object, iter); } +out: + hashtable_del(parents, loop_key, loop_key_len); + return result; } - /*** array ***/ -json_t *json_array(void) -{ - json_array_t *array = malloc(sizeof(json_array_t)); - if(!array) +json_t *json_array(void) { + json_array_t *array = jsonp_malloc(sizeof(json_array_t)); + if (!array) return NULL; json_init(&array->json, JSON_ARRAY); array->entries = 0; array->size = 8; - array->table = malloc(array->size * sizeof(json_t *)); - if(!array->table) { - free(array); + array->table = jsonp_malloc(array->size * sizeof(json_t *)); + if (!array->table) { + jsonp_free(array); return NULL; } - array->visited = 0; - return &array->json; } -static void json_delete_array(json_array_t *array) -{ +static void json_delete_array(json_array_t *array) { size_t i; - for(i = 0; i < array->entries; i++) + for (i = 0; i < array->entries; i++) json_decref(array->table[i]); - free(array->table); - free(array); + jsonp_free(array->table); + jsonp_free(array); } -size_t json_array_size(const json_t *json) -{ - if(!json_is_array(json)) +size_t json_array_size(const json_t *json) { + if (!json_is_array(json)) return 0; return json_to_array(json)->entries; } -json_t *json_array_get(const json_t *json, size_t index) -{ +json_t *json_array_get(const json_t *json, size_t index) { json_array_t *array; - if(!json_is_array(json)) + if (!json_is_array(json)) return NULL; array = json_to_array(json); - if(index >= array->entries) + if (index >= array->entries) return NULL; return array->table[index]; } -int json_array_set_new(json_t *json, size_t index, json_t *value) -{ +int json_array_set_new(json_t *json, size_t index, json_t *value) { json_array_t *array; - if(!value) + if (!value) return -1; - if(!json_is_array(json) || json == value) - { + if (!json_is_array(json) || json == value) { json_decref(value); return -1; } array = json_to_array(json); - if(index >= array->entries) - { + if (index >= array->entries) { json_decref(value); return -1; } @@ -427,63 +490,54 @@ int json_array_set_new(json_t *json, size_t index, json_t *value) return 0; } -static void array_move(json_array_t *array, size_t dest, - size_t src, size_t count) -{ +static void array_move(json_array_t *array, size_t dest, size_t src, size_t count) { memmove(&array->table[dest], &array->table[src], count * sizeof(json_t *)); } -static void array_copy(json_t **dest, size_t dpos, - json_t **src, size_t spos, - size_t count) -{ +static void array_copy(json_t **dest, size_t dpos, json_t **src, size_t spos, + size_t count) { memcpy(&dest[dpos], &src[spos], count * sizeof(json_t *)); } -static json_t **json_array_grow(json_array_t *array, - size_t amount, - int copy) -{ +static json_t **json_array_grow(json_array_t *array, size_t amount, int copy) { size_t new_size; json_t **old_table, **new_table; - if(array->entries + amount <= array->size) + if (array->entries + amount <= array->size) return array->table; old_table = array->table; new_size = max(array->size + amount, array->size * 2); - new_table = malloc(new_size * sizeof(json_t *)); - if(!new_table) + new_table = jsonp_malloc(new_size * sizeof(json_t *)); + if (!new_table) return NULL; array->size = new_size; array->table = new_table; - if(copy) { + if (copy) { array_copy(array->table, 0, old_table, 0, array->entries); - free(old_table); + jsonp_free(old_table); return array->table; } return old_table; } -int json_array_append_new(json_t *json, json_t *value) -{ +int json_array_append_new(json_t *json, json_t *value) { json_array_t *array; - if(!value) + if (!value) return -1; - if(!json_is_array(json) || json == value) - { + if (!json_is_array(json) || json == value) { json_decref(value); return -1; } array = json_to_array(json); - if(!json_array_grow(array, 1, 1)) { + if (!json_array_grow(array, 1, 1)) { json_decref(value); return -1; } @@ -494,38 +548,35 @@ int json_array_append_new(json_t *json, json_t *value) return 0; } -int json_array_insert_new(json_t *json, size_t index, json_t *value) -{ +int json_array_insert_new(json_t *json, size_t index, json_t *value) { json_array_t *array; json_t **old_table; - if(!value) + if (!value) return -1; - if(!json_is_array(json) || json == value) { + if (!json_is_array(json) || json == value) { json_decref(value); return -1; } array = json_to_array(json); - if(index > array->entries) { + if (index > array->entries) { json_decref(value); return -1; } old_table = json_array_grow(array, 1, 0); - if(!old_table) { + if (!old_table) { json_decref(value); return -1; } - if(old_table != array->table) { + if (old_table != array->table) { array_copy(array->table, 0, old_table, 0, index); - array_copy(array->table, index + 1, old_table, index, - array->entries - index); - free(old_table); - } - else + array_copy(array->table, index + 1, old_table, index, array->entries - index); + jsonp_free(old_table); + } else array_move(array, index + 1, index, array->entries - index); array->table[index] = value; @@ -534,55 +585,55 @@ int json_array_insert_new(json_t *json, size_t index, json_t *value) return 0; } -int json_array_remove(json_t *json, size_t index) -{ +int json_array_remove(json_t *json, size_t index) { json_array_t *array; - if(!json_is_array(json)) + if (!json_is_array(json)) return -1; array = json_to_array(json); - if(index >= array->entries) + if (index >= array->entries) return -1; json_decref(array->table[index]); - array_move(array, index, index + 1, array->entries - index); + /* If we're removing the last element, nothing has to be moved */ + if (index < array->entries - 1) + array_move(array, index, index + 1, array->entries - index - 1); + array->entries--; return 0; } -int json_array_clear(json_t *json) -{ +int json_array_clear(json_t *json) { json_array_t *array; size_t i; - if(!json_is_array(json)) + if (!json_is_array(json)) return -1; array = json_to_array(json); - for(i = 0; i < array->entries; i++) + for (i = 0; i < array->entries; i++) json_decref(array->table[i]); array->entries = 0; return 0; } -int json_array_extend(json_t *json, json_t *other_json) -{ +int json_array_extend(json_t *json, json_t *other_json) { json_array_t *array, *other; size_t i; - if(!json_is_array(json) || !json_is_array(other_json)) + if (!json_is_array(json) || !json_is_array(other_json)) return -1; array = json_to_array(json); other = json_to_array(other_json); - if(!json_array_grow(array, other->entries, 1)) + if (!json_array_grow(array, other->entries, 1)) return -1; - for(i = 0; i < other->entries; i++) + for (i = 0; i < other->entries; i++) json_incref(other->table[i]); array_copy(array->table, array->entries, other->table, 0, other->entries); @@ -591,144 +642,249 @@ int json_array_extend(json_t *json, json_t *other_json) return 0; } -static int json_array_equal(json_t *array1, json_t *array2) -{ +static int json_array_equal(const json_t *array1, const json_t *array2) { size_t i, size; size = json_array_size(array1); - if(size != json_array_size(array2)) + if (size != json_array_size(array2)) return 0; - for(i = 0; i < size; i++) - { + for (i = 0; i < size; i++) { json_t *value1, *value2; value1 = json_array_get(array1, i); value2 = json_array_get(array2, i); - if(!json_equal(value1, value2)) + if (!json_equal(value1, value2)) return 0; } return 1; } -static json_t *json_array_copy(json_t *array) -{ +static json_t *json_array_copy(json_t *array) { json_t *result; size_t i; result = json_array(); - if(!result) + if (!result) return NULL; - for(i = 0; i < json_array_size(array); i++) + for (i = 0; i < json_array_size(array); i++) json_array_append(result, json_array_get(array, i)); return result; } -static json_t *json_array_deep_copy(json_t *array) -{ +static json_t *json_array_deep_copy(const json_t *array, hashtable_t *parents) { json_t *result; size_t i; + char loop_key[LOOP_KEY_LEN]; + size_t loop_key_len; - result = json_array(); - if(!result) + if (jsonp_loop_check(parents, array, loop_key, sizeof(loop_key), &loop_key_len)) return NULL; - for(i = 0; i < json_array_size(array); i++) - json_array_append_new(result, json_deep_copy(json_array_get(array, i))); + result = json_array(); + if (!result) + goto out; + + for (i = 0; i < json_array_size(array); i++) { + if (json_array_append_new(result, + do_deep_copy(json_array_get(array, i), parents))) { + json_decref(result); + result = NULL; + break; + } + } + +out: + hashtable_del(parents, loop_key, loop_key_len); return result; } /*** string ***/ -json_t *json_string_nocheck(const char *value) -{ +static json_t *string_create(const char *value, size_t len, int own) { + char *v; json_string_t *string; - if(!value) + if (!value) return NULL; - string = malloc(sizeof(json_string_t)); - if(!string) - return NULL; - json_init(&string->json, JSON_STRING); + if (own) + v = (char *)value; + else { + v = jsonp_strndup(value, len); + if (!v) + return NULL; + } - string->value = strdup(value); - if(!string->value) { - free(string); + string = jsonp_malloc(sizeof(json_string_t)); + if (!string) { + jsonp_free(v); return NULL; } + json_init(&string->json, JSON_STRING); + string->value = v; + string->length = len; return &string->json; } -json_t *json_string(const char *value) -{ - if(!value || !utf8_check_string(value, -1)) +json_t *json_string_nocheck(const char *value) { + if (!value) + return NULL; + + return string_create(value, strlen(value), 0); +} + +json_t *json_stringn_nocheck(const char *value, size_t len) { + return string_create(value, len, 0); +} + +/* this is private; "steal" is not a public API concept */ +json_t *jsonp_stringn_nocheck_own(const char *value, size_t len) { + return string_create(value, len, 1); +} + +json_t *json_string(const char *value) { + if (!value) + return NULL; + + return json_stringn(value, strlen(value)); +} + +json_t *json_stringn(const char *value, size_t len) { + if (!value || !utf8_check_string(value, len)) return NULL; - return json_string_nocheck(value); + return json_stringn_nocheck(value, len); } -const char *json_string_value(const json_t *json) -{ - if(!json_is_string(json)) +const char *json_string_value(const json_t *json) { + if (!json_is_string(json)) return NULL; return json_to_string(json)->value; } -int json_string_set_nocheck(json_t *json, const char *value) -{ +size_t json_string_length(const json_t *json) { + if (!json_is_string(json)) + return 0; + + return json_to_string(json)->length; +} + +int json_string_set_nocheck(json_t *json, const char *value) { + if (!value) + return -1; + + return json_string_setn_nocheck(json, value, strlen(value)); +} + +int json_string_setn_nocheck(json_t *json, const char *value, size_t len) { char *dup; json_string_t *string; - dup = strdup(value); - if(!dup) + if (!json_is_string(json) || !value) + return -1; + + dup = jsonp_strndup(value, len); + if (!dup) return -1; string = json_to_string(json); - free(string->value); + jsonp_free(string->value); string->value = dup; + string->length = len; return 0; } -int json_string_set(json_t *json, const char *value) -{ - if(!value || !utf8_check_string(value, -1)) +int json_string_set(json_t *json, const char *value) { + if (!value) return -1; - return json_string_set_nocheck(json, value); + return json_string_setn(json, value, strlen(value)); } -static void json_delete_string(json_string_t *string) -{ - free(string->value); - free(string); +int json_string_setn(json_t *json, const char *value, size_t len) { + if (!value || !utf8_check_string(value, len)) + return -1; + + return json_string_setn_nocheck(json, value, len); +} + +static void json_delete_string(json_string_t *string) { + jsonp_free(string->value); + jsonp_free(string); } -static int json_string_equal(json_t *string1, json_t *string2) -{ - return strcmp(json_string_value(string1), json_string_value(string2)) == 0; +static int json_string_equal(const json_t *string1, const json_t *string2) { + json_string_t *s1, *s2; + + s1 = json_to_string(string1); + s2 = json_to_string(string2); + return s1->length == s2->length && !memcmp(s1->value, s2->value, s1->length); } -static json_t *json_string_copy(json_t *string) -{ - return json_string_nocheck(json_string_value(string)); +static json_t *json_string_copy(const json_t *string) { + json_string_t *s; + + s = json_to_string(string); + return json_stringn_nocheck(s->value, s->length); } +json_t *json_vsprintf(const char *fmt, va_list ap) { + json_t *json = NULL; + int length; + char *buf; + va_list aq; + va_copy(aq, ap); + + length = vsnprintf(NULL, 0, fmt, ap); + if (length < 0) + goto out; + if (length == 0) { + json = json_string(""); + goto out; + } + + buf = jsonp_malloc((size_t)length + 1); + if (!buf) + goto out; + + vsnprintf(buf, (size_t)length + 1, fmt, aq); + if (!utf8_check_string(buf, length)) { + jsonp_free(buf); + goto out; + } + + json = jsonp_stringn_nocheck_own(buf, length); + +out: + va_end(aq); + return json; +} + +json_t *json_sprintf(const char *fmt, ...) { + json_t *result; + va_list ap; + + va_start(ap, fmt); + result = json_vsprintf(fmt, ap); + va_end(ap); + + return result; +} /*** integer ***/ -json_t *json_integer(json_int_t value) -{ - json_integer_t *integer = malloc(sizeof(json_integer_t)); - if(!integer) +json_t *json_integer(json_int_t value) { + json_integer_t *integer = jsonp_malloc(sizeof(json_integer_t)); + if (!integer) return NULL; json_init(&integer->json, JSON_INTEGER); @@ -736,17 +892,15 @@ json_t *json_integer(json_int_t value) return &integer->json; } -json_int_t json_integer_value(const json_t *json) -{ - if(!json_is_integer(json)) +json_int_t json_integer_value(const json_t *json) { + if (!json_is_integer(json)) return 0; return json_to_integer(json)->value; } -int json_integer_set(json_t *json, json_int_t value) -{ - if(!json_is_integer(json)) +int json_integer_set(json_t *json, json_int_t value) { + if (!json_is_integer(json)) return -1; json_to_integer(json)->value = value; @@ -754,28 +908,26 @@ int json_integer_set(json_t *json, json_int_t value) return 0; } -static void json_delete_integer(json_integer_t *integer) -{ - free(integer); -} +static void json_delete_integer(json_integer_t *integer) { jsonp_free(integer); } -static int json_integer_equal(json_t *integer1, json_t *integer2) -{ +static int json_integer_equal(const json_t *integer1, const json_t *integer2) { return json_integer_value(integer1) == json_integer_value(integer2); } -static json_t *json_integer_copy(json_t *integer) -{ +static json_t *json_integer_copy(const json_t *integer) { return json_integer(json_integer_value(integer)); } - /*** real ***/ -json_t *json_real(double value) -{ - json_real_t *real = malloc(sizeof(json_real_t)); - if(!real) +json_t *json_real(double value) { + json_real_t *real; + + if (isnan(value) || isinf(value)) + return NULL; + + real = jsonp_malloc(sizeof(json_real_t)); + if (!real) return NULL; json_init(&real->json, JSON_REAL); @@ -783,185 +935,178 @@ json_t *json_real(double value) return &real->json; } -double json_real_value(const json_t *json) -{ - if(!json_is_real(json)) +double json_real_value(const json_t *json) { + if (!json_is_real(json)) return 0; return json_to_real(json)->value; } -int json_real_set(json_t *json, double value) -{ - if(!json_is_real(json)) - return 0; +int json_real_set(json_t *json, double value) { + if (!json_is_real(json) || isnan(value) || isinf(value)) + return -1; json_to_real(json)->value = value; return 0; } -static void json_delete_real(json_real_t *real) -{ - free(real); -} +static void json_delete_real(json_real_t *real) { jsonp_free(real); } -static int json_real_equal(json_t *real1, json_t *real2) -{ +static int json_real_equal(const json_t *real1, const json_t *real2) { return json_real_value(real1) == json_real_value(real2); } -static json_t *json_real_copy(json_t *real) -{ +static json_t *json_real_copy(const json_t *real) { return json_real(json_real_value(real)); } - /*** number ***/ -double json_number_value(const json_t *json) -{ - if(json_is_integer(json)) - return json_integer_value(json); - else if(json_is_real(json)) +double json_number_value(const json_t *json) { + if (json_is_integer(json)) + return (double)json_integer_value(json); + else if (json_is_real(json)) return json_real_value(json); else return 0.0; } - /*** simple values ***/ -json_t *json_true(void) -{ +json_t *json_true(void) { static json_t the_true = {JSON_TRUE, (size_t)-1}; return &the_true; } - -json_t *json_false(void) -{ +json_t *json_false(void) { static json_t the_false = {JSON_FALSE, (size_t)-1}; return &the_false; } - -json_t *json_null(void) -{ +json_t *json_null(void) { static json_t the_null = {JSON_NULL, (size_t)-1}; return &the_null; } - /*** deletion ***/ -void json_delete(json_t *json) -{ - if(json_is_object(json)) - json_delete_object(json_to_object(json)); - - else if(json_is_array(json)) - json_delete_array(json_to_array(json)); - - else if(json_is_string(json)) - json_delete_string(json_to_string(json)); - - else if(json_is_integer(json)) - json_delete_integer(json_to_integer(json)); - - else if(json_is_real(json)) - json_delete_real(json_to_real(json)); +void json_delete(json_t *json) { + if (!json) + return; + + switch (json_typeof(json)) { + case JSON_OBJECT: + json_delete_object(json_to_object(json)); + break; + case JSON_ARRAY: + json_delete_array(json_to_array(json)); + break; + case JSON_STRING: + json_delete_string(json_to_string(json)); + break; + case JSON_INTEGER: + json_delete_integer(json_to_integer(json)); + break; + case JSON_REAL: + json_delete_real(json_to_real(json)); + break; + default: + return; + } /* json_delete is not called for true, false or null */ } - /*** equality ***/ -int json_equal(json_t *json1, json_t *json2) -{ - if(!json1 || !json2) +int json_equal(const json_t *json1, const json_t *json2) { + if (!json1 || !json2) return 0; - if(json_typeof(json1) != json_typeof(json2)) + if (json_typeof(json1) != json_typeof(json2)) return 0; /* this covers true, false and null as they are singletons */ - if(json1 == json2) + if (json1 == json2) return 1; - if(json_is_object(json1)) - return json_object_equal(json1, json2); - - if(json_is_array(json1)) - return json_array_equal(json1, json2); - - if(json_is_string(json1)) - return json_string_equal(json1, json2); - - if(json_is_integer(json1)) - return json_integer_equal(json1, json2); - - if(json_is_real(json1)) - return json_real_equal(json1, json2); - - return 0; + switch (json_typeof(json1)) { + case JSON_OBJECT: + return json_object_equal(json1, json2); + case JSON_ARRAY: + return json_array_equal(json1, json2); + case JSON_STRING: + return json_string_equal(json1, json2); + case JSON_INTEGER: + return json_integer_equal(json1, json2); + case JSON_REAL: + return json_real_equal(json1, json2); + default: + return 0; + } } - /*** copying ***/ -json_t *json_copy(json_t *json) -{ - if(!json) +json_t *json_copy(json_t *json) { + if (!json) return NULL; - if(json_is_object(json)) - return json_object_copy(json); - - if(json_is_array(json)) - return json_array_copy(json); - - if(json_is_string(json)) - return json_string_copy(json); - - if(json_is_integer(json)) - return json_integer_copy(json); + switch (json_typeof(json)) { + case JSON_OBJECT: + return json_object_copy(json); + case JSON_ARRAY: + return json_array_copy(json); + case JSON_STRING: + return json_string_copy(json); + case JSON_INTEGER: + return json_integer_copy(json); + case JSON_REAL: + return json_real_copy(json); + case JSON_TRUE: + case JSON_FALSE: + case JSON_NULL: + return json; + default: + return NULL; + } +} - if(json_is_real(json)) - return json_real_copy(json); +json_t *json_deep_copy(const json_t *json) { + json_t *res; + hashtable_t parents_set; - if(json_is_true(json) || json_is_false(json) || json_is_null(json)) - return json; + if (hashtable_init(&parents_set)) + return NULL; + res = do_deep_copy(json, &parents_set); + hashtable_close(&parents_set); - return NULL; + return res; } -json_t *json_deep_copy(json_t *json) -{ - if(!json) +json_t *do_deep_copy(const json_t *json, hashtable_t *parents) { + if (!json) return NULL; - if(json_is_object(json)) - return json_object_deep_copy(json); - - if(json_is_array(json)) - return json_array_deep_copy(json); - - /* for the rest of the types, deep copying doesn't differ from - shallow copying */ - - if(json_is_string(json)) - return json_string_copy(json); - - if(json_is_integer(json)) - return json_integer_copy(json); - - if(json_is_real(json)) - return json_real_copy(json); - - if(json_is_true(json) || json_is_false(json) || json_is_null(json)) - return json; - - return NULL; + switch (json_typeof(json)) { + case JSON_OBJECT: + return json_object_deep_copy(json, parents); + case JSON_ARRAY: + return json_array_deep_copy(json, parents); + /* for the rest of the types, deep copying doesn't differ from + shallow copying */ + case JSON_STRING: + return json_string_copy(json); + case JSON_INTEGER: + return json_integer_copy(json); + case JSON_REAL: + return json_real_copy(json); + case JSON_TRUE: + case JSON_FALSE: + case JSON_NULL: + return (json_t *)json; + default: + return NULL; + } } diff --git a/src/jansson/src/variadic.c b/src/jansson/src/variadic.c deleted file mode 100644 index 89ff1da1..00000000 --- a/src/jansson/src/variadic.c +++ /dev/null @@ -1,568 +0,0 @@ -/* - * Copyright (c) 2009, 2010 Petri Lehtinen - * Copyright (c) 2010 Graeme Smecher - * - * Jansson is free software; you can redistribute it and/or modify - * it under the terms of the MIT license. See LICENSE for details. - */ - -#include -#include -#include - -#include -#include "jansson_private.h" - -json_t *json_pack(json_error_t *error, const char *fmt, ...) { - int fmt_length = strlen(fmt); - va_list ap; - - /* Keep a stack of containers (lists and objects) */ - int depth = 0; - json_t **stack = NULL; - - /* Keep a list of objects we create in case of error */ - int free_count = 0; - json_t **free_list = NULL; - - json_t *cur = NULL; /* Current container */ - json_t *root = NULL; /* root object */ - json_t *obj = NULL; - - char *key = NULL; /* Current key in an object */ - char *s; - - int line = 1; - - /* Allocation provisioned for worst case */ - stack = calloc(fmt_length, sizeof(json_t *)); - free_list = calloc(fmt_length, sizeof(json_t *)); - - jsonp_error_init(error, ""); - - if(!stack || !free_list) - goto out; - - va_start(ap, fmt); - while(*fmt) { - switch(*fmt) { - case '\n': - line++; - break; - - case ' ': /* Whitespace */ - break; - - case ',': /* Element spacer */ - if(!root) - { - jsonp_error_set(error, line, -1, - "Unexpected COMMA precedes root element!"); - root = NULL; - goto out; - } - - if(!cur) - { - jsonp_error_set(error, line, -1, - "Unexpected COMMA outside a list or object!"); - root = NULL; - goto out; - } - - if(key) - { - jsonp_error_set(error, line, -1, - "Expected KEY, got COMMA!"); - root = NULL; - goto out; - } - break; - - case ':': /* Key/value separator */ - if(!key) - { - jsonp_error_set(error, line, -1, - "Got key/value separator without " - "a key preceding it!"); - root = NULL; - goto out; - } - - if(!json_is_object(cur)) - { - jsonp_error_set(error, line, -1, - "Got a key/value separator " - "(':') outside an object!"); - root = NULL; - goto out; - } - - break; - - case ']': /* Close array or object */ - case '}': - - if(key) - { - jsonp_error_set(error, line, -1, - "OBJECT or ARRAY ended with an " - "incomplete key/value pair!"); - root = NULL; - goto out; - } - - if(depth <= 0) - { - jsonp_error_set(error, line, -1, - "Too many close-brackets '%c'", *fmt); - root = NULL; - goto out; - } - - if(*fmt == ']' && !json_is_array(cur)) - { - jsonp_error_set(error, line, -1, - "Stray close-array ']' character"); - root = NULL; - goto out; - } - - if(*fmt == '}' && !json_is_object(cur)) - { - jsonp_error_set(error, line, -1, - "Stray close-object '}' character"); - root = NULL; - goto out; - } - - cur = stack[--depth]; - break; - - case '[': - obj = json_array(); - goto obj_common; - - case '{': - obj = json_object(); - goto obj_common; - - case 's': /* string */ - s = va_arg(ap, char*); - - if(!s) - { - jsonp_error_set(error, line, -1, - "Refusing to handle a NULL string"); - root = NULL; - goto out; - } - - if(json_is_object(cur) && !key) - { - /* It's a key */ - key = s; - break; - } - - obj = json_string(s); - goto obj_common; - - case 'n': /* null */ - obj = json_null(); - goto obj_common; - - case 'b': /* boolean */ - obj = va_arg(ap, int) ? - json_true() : json_false(); - goto obj_common; - - case 'i': /* integer */ - obj = json_integer(va_arg(ap, int)); - goto obj_common; - - case 'f': /* double-precision float */ - obj = json_real(va_arg(ap, double)); - goto obj_common; - - case 'O': /* a json_t object; increments refcount */ - obj = va_arg(ap, json_t *); - json_incref(obj); - goto obj_common; - - case 'o': /* a json_t object; doesn't increment refcount */ - obj = va_arg(ap, json_t *); - goto obj_common; - -obj_common: free_list[free_count++] = obj; - - /* Root this object to its parent */ - if(json_is_object(cur)) { - if(!key) - { - jsonp_error_set(error, line, -1, - "Expected key, got identifier '%c'!", *fmt); - root = NULL; - goto out; - } - - json_object_set_new(cur, key, obj); - key = NULL; - } - else if(json_is_array(cur)) - { - json_array_append_new(cur, obj); - } - else if(!root) - { - printf("Rooting\n"); - root = obj; - } - else - { - jsonp_error_set(error, line, -1, - "Can't figure out where to attach " - "'%c' object!", *fmt); - root = NULL; - goto out; - } - - /* If it was a container ('[' or '{'), descend on the stack */ - if(json_is_array(obj) || json_is_object(obj)) - { - stack[depth++] = cur; - cur = obj; - } - - break; - } - fmt++; - } - va_end(ap); - - if(depth != 0) { - jsonp_error_set(error, line, -1, - "Missing object or array close-brackets in format string"); - root = NULL; - goto out; - } - - /* Success: don't free everything we just built! */ - free_count = 0; - -out: - while(free_count) - json_decref(free_list[--free_count]); - - if(free_list) - free(free_list); - - if(stack) - free(stack); - - return(root); -} - -int json_unpack(json_t *root, json_error_t *error, const char *fmt, ...) { - va_list ap; - - int rv=0; /* Return value */ - int line = 1; /* Line number */ - - /* Keep a stack of containers (lists and objects) */ - int depth = 0; - json_t **stack; - - int array_index = 0; - char *key = NULL; /* Current key in an object */ - - json_t *cur = NULL; /* Current container */ - json_t *obj = NULL; - - int fmt_length = strlen(fmt); - - jsonp_error_init(error, ""); - - /* Allocation provisioned for worst case */ - stack = calloc(fmt_length, sizeof(json_t *)); - if(!stack) - { - jsonp_error_set(error, line, -1, "Out of memory!"); - rv = -1; - goto out; - } - - /* Even if we're successful, we need to know if the number of - * arguments provided matches the number of JSON objects. - * We can do this by counting the elements in every array or - * object we open up, and decrementing the count as we visit - * their children. */ - int unvisited = 0; - - va_start(ap, fmt); - while(*fmt) - { - switch(*fmt) - { - case ' ': /* Whitespace */ - break; - - case '\n': /* Line break */ - line++; - break; - - case ',': /* Element spacer */ - - if(!cur) - { - jsonp_error_set(error, line, -1, - "Unexpected COMMA outside a list or object!"); - rv = -1; - goto out; - } - - if(key) - { - jsonp_error_set(error, line, -1, - "Expected KEY, got COMMA!"); - rv = -1; - goto out; - } - break; - - case ':': /* Key/value separator */ - if(!json_is_object(cur) || !key) - { - jsonp_error_set(error, line, -1, "Unexpected ':'"); - rv = -1; - goto out; - } - break; - - case '[': - case '{': - /* Fetch object */ - if(!cur) - { - obj = root; - } - else if(json_is_object(cur)) - { - if(!key) - { - jsonp_error_set(error, line, -1, - "Objects can't be keys"); - rv = -1; - goto out; - } - obj = json_object_get(cur, key); - unvisited--; - key = NULL; - } - else if(json_is_array(cur)) - { - obj = json_array_get(cur, array_index); - unvisited--; - array_index++; - } - else - { - assert(0); - } - - /* Make sure we got what we expected */ - if(*fmt=='{' && !json_is_object(obj)) - { - rv = -2; - goto out; - } - - if(*fmt=='[' && !json_is_array(obj)) - { - rv = -2; - goto out; - } - - unvisited += json_is_object(obj) ? - json_object_size(obj) : - json_array_size(obj); - - /* Descend */ - stack[depth++] = cur; - cur = obj; - - key = NULL; - - break; - - - case ']': - case '}': - - if(json_is_array(cur) && *fmt!=']') - { - jsonp_error_set(error, line, -1, "Missing ']'"); - rv = -1; - goto out; - } - - if(json_is_object(cur) && *fmt!='}') - { - jsonp_error_set(error, line, -1, "Missing '}'"); - rv = -1; - goto out; - } - - if(key) - { - jsonp_error_set(error, line, -1, "Unexpected '%c'", *fmt); - rv = -1; - goto out; - } - - if(depth <= 0) - { - jsonp_error_set(error, line, -1, "Unexpected '%c'", *fmt); - rv = -1; - goto out; - } - - cur = stack[--depth]; - - break; - - case 's': - if(!key && json_is_object(cur)) - { - /* constant string for key */ - key = va_arg(ap, char*); - break; - } - /* fall through */ - - case 'i': /* integer */ - case 'f': /* double-precision float */ - case 'O': /* a json_t object; increments refcount */ - case 'o': /* a json_t object; borrowed reference */ - case 'b': /* boolean */ - case 'n': /* null */ - - /* Fetch object */ - if(!cur) - { - obj = root; - } - else if(json_is_object(cur)) - { - if(!key) - { - jsonp_error_set(error, line, -1, - "Only strings may be used as keys!"); - rv = -1; - goto out; - } - - obj = json_object_get(cur, key); - unvisited--; - key = NULL; - } - else if(json_is_array(cur)) - { - obj = json_array_get(cur, array_index); - unvisited--; - array_index++; - } - else - { - jsonp_error_set(error, line, -1, - "Unsure how to retrieve JSON object '%c'", - *fmt); - rv = -1; - goto out; - } - - switch(*fmt) - { - case 's': - if(!json_is_string(obj)) - { - jsonp_error_set(error, line, -1, - "Type mismatch! Object wasn't a string."); - rv = -2; - goto out; - } - *va_arg(ap, const char**) = json_string_value(obj); - break; - - case 'i': - if(!json_is_integer(obj)) - { - jsonp_error_set(error, line, -1, - "Type mismatch! Object wasn't an integer."); - rv = -2; - goto out; - } - *va_arg(ap, int*) = json_integer_value(obj); - break; - - case 'b': - if(!json_is_boolean(obj)) - { - jsonp_error_set(error, line, -1, - "Type mismatch! Object wasn't a boolean."); - rv = -2; - goto out; - } - *va_arg(ap, int*) = json_is_true(obj); - break; - - case 'f': - if(!json_is_number(obj)) - { - jsonp_error_set(error, line, -1, - "Type mismatch! Object wasn't a real."); - rv = -2; - goto out; - } - *va_arg(ap, double*) = json_number_value(obj); - break; - - case 'O': - json_incref(obj); - /* Fall through */ - - case 'o': - *va_arg(ap, json_t**) = obj; - break; - - case 'n': - /* Don't actually assign anything; we're just happy - * the null turned up as promised in the format - * string. */ - break; - - default: - jsonp_error_set(error, line, -1, - "Unknown format character '%c'", *fmt); - rv = -1; - goto out; - } - } - fmt++; - } - - /* Return 0 if everything was matched; otherwise the number of JSON - * objects we didn't get to. */ - rv = unvisited; - -out: - va_end(ap); - - if(stack) - free(stack); - - return(rv); -} - -/* vim: ts=4:expandtab:sw=4 - */ diff --git a/src/jansson/src/version.c b/src/jansson/src/version.c new file mode 100644 index 00000000..f1026af1 --- /dev/null +++ b/src/jansson/src/version.c @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2019 Sean Bright + * + * Jansson is free software; you can redistribute it and/or modify + * it under the terms of the MIT license. See LICENSE for details. + */ + +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif + +#include "jansson.h" + +const char *jansson_version_str(void) { return JANSSON_VERSION; } + +int jansson_version_cmp(int major, int minor, int micro) { + int diff; + + if ((diff = JANSSON_MAJOR_VERSION - major)) { + return diff; + } + + if ((diff = JANSSON_MINOR_VERSION - minor)) { + return diff; + } + + return JANSSON_MICRO_VERSION - micro; +}