mirror of https://github.com/libAthena/athena.git
Added YAML serialization DNA subclass
This commit is contained in:
parent
43c317529c
commit
84591d4260
|
@ -17,7 +17,7 @@ set(ATHENA_VERSION
|
|||
|
||||
add_subdirectory(extern)
|
||||
|
||||
include_directories(include ${LZO_INCLUDE_DIR} ${ZLIB_INCLUDE_DIR})
|
||||
include_directories(include ${LZO_INCLUDE_DIR} ${ZLIB_INCLUDE_DIR} ${YAML_INCLUDE_DIR})
|
||||
if (NOT MSVC)
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
|
||||
endif()
|
||||
|
@ -45,6 +45,7 @@ add_library(AthenaCore
|
|||
src/LZ77/LZBase.cpp
|
||||
src/Athena/FileInfo.cpp
|
||||
src/Athena/Dir.cpp
|
||||
src/Athena/DNAYaml.cpp
|
||||
${CORE_EXTRA}
|
||||
|
||||
include/Athena/IStream.hpp
|
||||
|
@ -67,6 +68,7 @@ add_library(AthenaCore
|
|||
include/Athena/Dir.hpp
|
||||
include/gekko_support.h
|
||||
include/Athena/DNA.hpp
|
||||
include/Athena/DNAYaml.hpp
|
||||
)
|
||||
|
||||
add_library(AthenaSakura
|
||||
|
|
4
PKGBUILD
4
PKGBUILD
|
@ -1,11 +1,11 @@
|
|||
# PKGBUILD for libAthena
|
||||
_pkgname=libathena
|
||||
pkgname=$_pkgname-git
|
||||
pkgver=2.2.0.3.g4a688a1
|
||||
pkgver=2.2.0.8.g2490fef
|
||||
pkgrel=1
|
||||
pkgdesc="Basic cross platform IO library"
|
||||
arch=('i686' 'x86_64')
|
||||
source=("${pkgname%-*}::git+https://github.com/libAthena/Athena.git")
|
||||
source=("${pkgname%-*}::git+https://github.com/libAthena/Athena.git#branch=yaml")
|
||||
options=(staticlibs)
|
||||
license="MIT"
|
||||
makedepends=('git cmake sed')
|
||||
|
|
|
@ -1,2 +1,3 @@
|
|||
add_subdirectory(lzo)
|
||||
add_subdirectory(zlib)
|
||||
add_subdirectory(yaml)
|
||||
|
|
|
@ -0,0 +1,21 @@
|
|||
# Minimal CMake project for building a static library under Windows.
|
||||
|
||||
cmake_minimum_required(VERSION 2.8)
|
||||
project(yaml C)
|
||||
|
||||
set(YAML_VERSION_MAJOR 0)
|
||||
set(YAML_VERSION_MINOR 1)
|
||||
set(YAML_VERSION_PATCH 6)
|
||||
set(YAML_VERSION_STRING "${YAML_VERSION_MAJOR}.${YAML_VERSION_MINOR}.${YAML_VERSION_PATCH}")
|
||||
|
||||
file(GLOB SRC src/*.c)
|
||||
|
||||
include_directories(include win32)
|
||||
add_definitions(-DYAML_DECLARE_STATIC)
|
||||
add_library(yaml STATIC ${SRC} include/yaml.h)
|
||||
|
||||
set(YAML_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include CACHE PATH "YAML include path" FORCE)
|
||||
if(WIN32 AND NOT UNIX)
|
||||
install(DIRECTORY include/ DESTINATION include COMPONENT yaml)
|
||||
install(TARGETS yaml DESTINATION lib COMPONENT yaml)
|
||||
endif()
|
|
@ -0,0 +1,19 @@
|
|||
Copyright (c) 2006 Kirill Simonov
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do
|
||||
so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
|
@ -0,0 +1,27 @@
|
|||
LibYAML - A C library for parsing and emitting YAML.
|
||||
|
||||
To build and install the library, run:
|
||||
$ ./configure
|
||||
$ make
|
||||
# make install
|
||||
|
||||
If you checked the source code from the Subversion repository, run
|
||||
$ ./bootstrap
|
||||
$ ./configure
|
||||
$ make
|
||||
# make install
|
||||
|
||||
For more information, check the LibYAML homepage:
|
||||
'http://pyyaml.org/wiki/LibYAML'.
|
||||
|
||||
Post your questions and opinions to the YAML-Core mailing list:
|
||||
'http://lists.sourceforge.net/lists/listinfo/yaml-core'.
|
||||
|
||||
Submit bug reports and feature requests to the LibYAML bug tracker:
|
||||
'http://pyyaml.org/newticket?component=libyaml'.
|
||||
|
||||
LibYAML is written by Kirill Simonov <xi@resolvent.net>. It is released
|
||||
under the MIT license. See the file LICENSE for more details.
|
||||
|
||||
This project is developed for Python Software Foundation as a part of
|
||||
Google Summer of Code under the mentorship of Clark Evans.
|
|
@ -0,0 +1,28 @@
|
|||
From: Kirill Simonov <xi@resolvent.net>
|
||||
To: yaml-core@lists.sourceforge.net
|
||||
Subject: LibYAML-0.1.4: A minor bugfix release
|
||||
|
||||
This is a minor bugfix release of LibYAML, a YAML parser and emitter
|
||||
written in C:
|
||||
|
||||
* Fixed a bug that prevented an empty mapping being used as a simple key
|
||||
(thank to spitzak(at)rhythm(dot)com).
|
||||
* Fixed pointer overflow when calculating the position of a potential
|
||||
simple key (thank to ppelletier(at)oblong(dot)com).
|
||||
* Fixed yaml.dll not exporting any symbols
|
||||
(thank to pxn11432(at)nifty(dot)com).
|
||||
* Added pkg-config support (thank to rainwoodman(at)gmail(dot)com).
|
||||
|
||||
LibYAML homepage: http://pyyaml.org/wiki/LibYAML
|
||||
TAR.GZ package: http://pyyaml.org/download/libyaml/yaml-0.1.4.tar.gz
|
||||
SVN repository: http://svn.pyyaml.org/libyaml/branches/stable
|
||||
Bug tracker: http://pyyaml.org/newticket?component=libyaml
|
||||
|
||||
The library is functionally complete, but the documentation is scarce
|
||||
and the API may change. For more information, you may check the project
|
||||
homepage, the doxygen-generated documentation in the `doc` directory of
|
||||
the source distribution, and examples in the `tests` directory.
|
||||
|
||||
LibYAML is written by Kirill Simonov <xi@resolvent.net> and released
|
||||
under the MIT license; see the file LICENSE for more details.
|
||||
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,4 @@
|
|||
#define YAML_VERSION_MAJOR 0
|
||||
#define YAML_VERSION_MINOR 1
|
||||
#define YAML_VERSION_PATCH 6
|
||||
#define YAML_VERSION_STRING "0.1.6"
|
|
@ -0,0 +1,394 @@
|
|||
|
||||
#include "yaml_private.h"
|
||||
|
||||
/*
|
||||
* API functions.
|
||||
*/
|
||||
|
||||
YAML_DECLARE(int)
|
||||
yaml_emitter_open(yaml_emitter_t *emitter);
|
||||
|
||||
YAML_DECLARE(int)
|
||||
yaml_emitter_close(yaml_emitter_t *emitter);
|
||||
|
||||
YAML_DECLARE(int)
|
||||
yaml_emitter_dump(yaml_emitter_t *emitter, yaml_document_t *document);
|
||||
|
||||
/*
|
||||
* Clean up functions.
|
||||
*/
|
||||
|
||||
static void
|
||||
yaml_emitter_delete_document_and_anchors(yaml_emitter_t *emitter);
|
||||
|
||||
/*
|
||||
* Anchor functions.
|
||||
*/
|
||||
|
||||
static void
|
||||
yaml_emitter_anchor_node(yaml_emitter_t *emitter, int index);
|
||||
|
||||
static yaml_char_t *
|
||||
yaml_emitter_generate_anchor(yaml_emitter_t *emitter, int anchor_id);
|
||||
|
||||
|
||||
/*
|
||||
* Serialize functions.
|
||||
*/
|
||||
|
||||
static int
|
||||
yaml_emitter_dump_node(yaml_emitter_t *emitter, int index);
|
||||
|
||||
static int
|
||||
yaml_emitter_dump_alias(yaml_emitter_t *emitter, yaml_char_t *anchor);
|
||||
|
||||
static int
|
||||
yaml_emitter_dump_scalar(yaml_emitter_t *emitter, yaml_node_t *node,
|
||||
yaml_char_t *anchor);
|
||||
|
||||
static int
|
||||
yaml_emitter_dump_sequence(yaml_emitter_t *emitter, yaml_node_t *node,
|
||||
yaml_char_t *anchor);
|
||||
|
||||
static int
|
||||
yaml_emitter_dump_mapping(yaml_emitter_t *emitter, yaml_node_t *node,
|
||||
yaml_char_t *anchor);
|
||||
|
||||
/*
|
||||
* Issue a STREAM-START event.
|
||||
*/
|
||||
|
||||
YAML_DECLARE(int)
|
||||
yaml_emitter_open(yaml_emitter_t *emitter)
|
||||
{
|
||||
yaml_event_t event;
|
||||
yaml_mark_t mark = { 0, 0, 0 };
|
||||
|
||||
assert(emitter); /* Non-NULL emitter object is required. */
|
||||
assert(!emitter->opened); /* Emitter should not be opened yet. */
|
||||
|
||||
STREAM_START_EVENT_INIT(event, YAML_ANY_ENCODING, mark, mark);
|
||||
|
||||
if (!yaml_emitter_emit(emitter, &event)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
emitter->opened = 1;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Issue a STREAM-END event.
|
||||
*/
|
||||
|
||||
YAML_DECLARE(int)
|
||||
yaml_emitter_close(yaml_emitter_t *emitter)
|
||||
{
|
||||
yaml_event_t event;
|
||||
yaml_mark_t mark = { 0, 0, 0 };
|
||||
|
||||
assert(emitter); /* Non-NULL emitter object is required. */
|
||||
assert(emitter->opened); /* Emitter should be opened. */
|
||||
|
||||
if (emitter->closed) return 1;
|
||||
|
||||
STREAM_END_EVENT_INIT(event, mark, mark);
|
||||
|
||||
if (!yaml_emitter_emit(emitter, &event)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
emitter->closed = 1;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Dump a YAML document.
|
||||
*/
|
||||
|
||||
YAML_DECLARE(int)
|
||||
yaml_emitter_dump(yaml_emitter_t *emitter, yaml_document_t *document)
|
||||
{
|
||||
yaml_event_t event;
|
||||
yaml_mark_t mark = { 0, 0, 0 };
|
||||
|
||||
assert(emitter); /* Non-NULL emitter object is required. */
|
||||
assert(document); /* Non-NULL emitter object is expected. */
|
||||
|
||||
emitter->document = document;
|
||||
|
||||
if (!emitter->opened) {
|
||||
if (!yaml_emitter_open(emitter)) goto error;
|
||||
}
|
||||
|
||||
if (STACK_EMPTY(emitter, document->nodes)) {
|
||||
if (!yaml_emitter_close(emitter)) goto error;
|
||||
yaml_emitter_delete_document_and_anchors(emitter);
|
||||
return 1;
|
||||
}
|
||||
|
||||
assert(emitter->opened); /* Emitter should be opened. */
|
||||
|
||||
emitter->anchors = yaml_malloc(sizeof(*(emitter->anchors))
|
||||
* (document->nodes.top - document->nodes.start));
|
||||
if (!emitter->anchors) goto error;
|
||||
memset(emitter->anchors, 0, sizeof(*(emitter->anchors))
|
||||
* (document->nodes.top - document->nodes.start));
|
||||
|
||||
DOCUMENT_START_EVENT_INIT(event, document->version_directive,
|
||||
document->tag_directives.start, document->tag_directives.end,
|
||||
document->start_implicit, mark, mark);
|
||||
if (!yaml_emitter_emit(emitter, &event)) goto error;
|
||||
|
||||
yaml_emitter_anchor_node(emitter, 1);
|
||||
if (!yaml_emitter_dump_node(emitter, 1)) goto error;
|
||||
|
||||
DOCUMENT_END_EVENT_INIT(event, document->end_implicit, mark, mark);
|
||||
if (!yaml_emitter_emit(emitter, &event)) goto error;
|
||||
|
||||
yaml_emitter_delete_document_and_anchors(emitter);
|
||||
|
||||
return 1;
|
||||
|
||||
error:
|
||||
|
||||
yaml_emitter_delete_document_and_anchors(emitter);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Clean up the emitter object after a document is dumped.
|
||||
*/
|
||||
|
||||
static void
|
||||
yaml_emitter_delete_document_and_anchors(yaml_emitter_t *emitter)
|
||||
{
|
||||
int index;
|
||||
|
||||
if (!emitter->anchors) {
|
||||
yaml_document_delete(emitter->document);
|
||||
emitter->document = NULL;
|
||||
return;
|
||||
}
|
||||
|
||||
for (index = 0; emitter->document->nodes.start + index
|
||||
< emitter->document->nodes.top; index ++) {
|
||||
yaml_node_t node = emitter->document->nodes.start[index];
|
||||
if (!emitter->anchors[index].serialized) {
|
||||
yaml_free(node.tag);
|
||||
if (node.type == YAML_SCALAR_NODE) {
|
||||
yaml_free(node.data.scalar.value);
|
||||
}
|
||||
}
|
||||
if (node.type == YAML_SEQUENCE_NODE) {
|
||||
STACK_DEL(emitter, node.data.sequence.items);
|
||||
}
|
||||
if (node.type == YAML_MAPPING_NODE) {
|
||||
STACK_DEL(emitter, node.data.mapping.pairs);
|
||||
}
|
||||
}
|
||||
|
||||
STACK_DEL(emitter, emitter->document->nodes);
|
||||
yaml_free(emitter->anchors);
|
||||
|
||||
emitter->anchors = NULL;
|
||||
emitter->last_anchor_id = 0;
|
||||
emitter->document = NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* Check the references of a node and assign the anchor id if needed.
|
||||
*/
|
||||
|
||||
static void
|
||||
yaml_emitter_anchor_node(yaml_emitter_t *emitter, int index)
|
||||
{
|
||||
yaml_node_t *node = emitter->document->nodes.start + index - 1;
|
||||
yaml_node_item_t *item;
|
||||
yaml_node_pair_t *pair;
|
||||
|
||||
emitter->anchors[index-1].references ++;
|
||||
|
||||
if (emitter->anchors[index-1].references == 1) {
|
||||
switch (node->type) {
|
||||
case YAML_SEQUENCE_NODE:
|
||||
for (item = node->data.sequence.items.start;
|
||||
item < node->data.sequence.items.top; item ++) {
|
||||
yaml_emitter_anchor_node(emitter, *item);
|
||||
}
|
||||
break;
|
||||
case YAML_MAPPING_NODE:
|
||||
for (pair = node->data.mapping.pairs.start;
|
||||
pair < node->data.mapping.pairs.top; pair ++) {
|
||||
yaml_emitter_anchor_node(emitter, pair->key);
|
||||
yaml_emitter_anchor_node(emitter, pair->value);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
else if (emitter->anchors[index-1].references == 2) {
|
||||
emitter->anchors[index-1].anchor = (++ emitter->last_anchor_id);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Generate a textual representation for an anchor.
|
||||
*/
|
||||
|
||||
#define ANCHOR_TEMPLATE "id%03d"
|
||||
#define ANCHOR_TEMPLATE_LENGTH 16
|
||||
|
||||
static yaml_char_t *
|
||||
yaml_emitter_generate_anchor(yaml_emitter_t *emitter, int anchor_id)
|
||||
{
|
||||
yaml_char_t *anchor = yaml_malloc(ANCHOR_TEMPLATE_LENGTH);
|
||||
|
||||
if (!anchor) return NULL;
|
||||
|
||||
sprintf((char *)anchor, ANCHOR_TEMPLATE, anchor_id);
|
||||
|
||||
return anchor;
|
||||
}
|
||||
|
||||
/*
|
||||
* Serialize a node.
|
||||
*/
|
||||
|
||||
static int
|
||||
yaml_emitter_dump_node(yaml_emitter_t *emitter, int index)
|
||||
{
|
||||
yaml_node_t *node = emitter->document->nodes.start + index - 1;
|
||||
int anchor_id = emitter->anchors[index-1].anchor;
|
||||
yaml_char_t *anchor = NULL;
|
||||
|
||||
if (anchor_id) {
|
||||
anchor = yaml_emitter_generate_anchor(emitter, anchor_id);
|
||||
if (!anchor) return 0;
|
||||
}
|
||||
|
||||
if (emitter->anchors[index-1].serialized) {
|
||||
return yaml_emitter_dump_alias(emitter, anchor);
|
||||
}
|
||||
|
||||
emitter->anchors[index-1].serialized = 1;
|
||||
|
||||
switch (node->type) {
|
||||
case YAML_SCALAR_NODE:
|
||||
return yaml_emitter_dump_scalar(emitter, node, anchor);
|
||||
case YAML_SEQUENCE_NODE:
|
||||
return yaml_emitter_dump_sequence(emitter, node, anchor);
|
||||
case YAML_MAPPING_NODE:
|
||||
return yaml_emitter_dump_mapping(emitter, node, anchor);
|
||||
default:
|
||||
assert(0); /* Could not happen. */
|
||||
break;
|
||||
}
|
||||
|
||||
return 0; /* Could not happen. */
|
||||
}
|
||||
|
||||
/*
|
||||
* Serialize an alias.
|
||||
*/
|
||||
|
||||
static int
|
||||
yaml_emitter_dump_alias(yaml_emitter_t *emitter, yaml_char_t *anchor)
|
||||
{
|
||||
yaml_event_t event;
|
||||
yaml_mark_t mark = { 0, 0, 0 };
|
||||
|
||||
ALIAS_EVENT_INIT(event, anchor, mark, mark);
|
||||
|
||||
return yaml_emitter_emit(emitter, &event);
|
||||
}
|
||||
|
||||
/*
|
||||
* Serialize a scalar.
|
||||
*/
|
||||
|
||||
static int
|
||||
yaml_emitter_dump_scalar(yaml_emitter_t *emitter, yaml_node_t *node,
|
||||
yaml_char_t *anchor)
|
||||
{
|
||||
yaml_event_t event;
|
||||
yaml_mark_t mark = { 0, 0, 0 };
|
||||
|
||||
int plain_implicit = (strcmp((char *)node->tag,
|
||||
YAML_DEFAULT_SCALAR_TAG) == 0);
|
||||
int quoted_implicit = (strcmp((char *)node->tag,
|
||||
YAML_DEFAULT_SCALAR_TAG) == 0);
|
||||
|
||||
SCALAR_EVENT_INIT(event, anchor, node->tag, node->data.scalar.value,
|
||||
node->data.scalar.length, plain_implicit, quoted_implicit,
|
||||
node->data.scalar.style, mark, mark);
|
||||
|
||||
return yaml_emitter_emit(emitter, &event);
|
||||
}
|
||||
|
||||
/*
|
||||
* Serialize a sequence.
|
||||
*/
|
||||
|
||||
static int
|
||||
yaml_emitter_dump_sequence(yaml_emitter_t *emitter, yaml_node_t *node,
|
||||
yaml_char_t *anchor)
|
||||
{
|
||||
yaml_event_t event;
|
||||
yaml_mark_t mark = { 0, 0, 0 };
|
||||
|
||||
int implicit = (strcmp((char *)node->tag, YAML_DEFAULT_SEQUENCE_TAG) == 0);
|
||||
|
||||
yaml_node_item_t *item;
|
||||
|
||||
SEQUENCE_START_EVENT_INIT(event, anchor, node->tag, implicit,
|
||||
node->data.sequence.style, mark, mark);
|
||||
if (!yaml_emitter_emit(emitter, &event)) return 0;
|
||||
|
||||
for (item = node->data.sequence.items.start;
|
||||
item < node->data.sequence.items.top; item ++) {
|
||||
if (!yaml_emitter_dump_node(emitter, *item)) return 0;
|
||||
}
|
||||
|
||||
SEQUENCE_END_EVENT_INIT(event, mark, mark);
|
||||
if (!yaml_emitter_emit(emitter, &event)) return 0;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Serialize a mapping.
|
||||
*/
|
||||
|
||||
static int
|
||||
yaml_emitter_dump_mapping(yaml_emitter_t *emitter, yaml_node_t *node,
|
||||
yaml_char_t *anchor)
|
||||
{
|
||||
yaml_event_t event;
|
||||
yaml_mark_t mark = { 0, 0, 0 };
|
||||
|
||||
int implicit = (strcmp((char *)node->tag, YAML_DEFAULT_MAPPING_TAG) == 0);
|
||||
|
||||
yaml_node_pair_t *pair;
|
||||
|
||||
MAPPING_START_EVENT_INIT(event, anchor, node->tag, implicit,
|
||||
node->data.mapping.style, mark, mark);
|
||||
if (!yaml_emitter_emit(emitter, &event)) return 0;
|
||||
|
||||
for (pair = node->data.mapping.pairs.start;
|
||||
pair < node->data.mapping.pairs.top; pair ++) {
|
||||
if (!yaml_emitter_dump_node(emitter, pair->key)) return 0;
|
||||
if (!yaml_emitter_dump_node(emitter, pair->value)) return 0;
|
||||
}
|
||||
|
||||
MAPPING_END_EVENT_INIT(event, mark, mark);
|
||||
if (!yaml_emitter_emit(emitter, &event)) return 0;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,444 @@
|
|||
|
||||
#include "yaml_private.h"
|
||||
|
||||
/*
|
||||
* API functions.
|
||||
*/
|
||||
|
||||
YAML_DECLARE(int)
|
||||
yaml_parser_load(yaml_parser_t *parser, yaml_document_t *document);
|
||||
|
||||
/*
|
||||
* Error handling.
|
||||
*/
|
||||
|
||||
static int
|
||||
yaml_parser_set_composer_error(yaml_parser_t *parser,
|
||||
const char *problem, yaml_mark_t problem_mark);
|
||||
|
||||
static int
|
||||
yaml_parser_set_composer_error_context(yaml_parser_t *parser,
|
||||
const char *context, yaml_mark_t context_mark,
|
||||
const char *problem, yaml_mark_t problem_mark);
|
||||
|
||||
|
||||
/*
|
||||
* Alias handling.
|
||||
*/
|
||||
|
||||
static int
|
||||
yaml_parser_register_anchor(yaml_parser_t *parser,
|
||||
int index, yaml_char_t *anchor);
|
||||
|
||||
/*
|
||||
* Clean up functions.
|
||||
*/
|
||||
|
||||
static void
|
||||
yaml_parser_delete_aliases(yaml_parser_t *parser);
|
||||
|
||||
/*
|
||||
* Composer functions.
|
||||
*/
|
||||
|
||||
static int
|
||||
yaml_parser_load_document(yaml_parser_t *parser, yaml_event_t *first_event);
|
||||
|
||||
static int
|
||||
yaml_parser_load_node(yaml_parser_t *parser, yaml_event_t *first_event);
|
||||
|
||||
static int
|
||||
yaml_parser_load_alias(yaml_parser_t *parser, yaml_event_t *first_event);
|
||||
|
||||
static int
|
||||
yaml_parser_load_scalar(yaml_parser_t *parser, yaml_event_t *first_event);
|
||||
|
||||
static int
|
||||
yaml_parser_load_sequence(yaml_parser_t *parser, yaml_event_t *first_event);
|
||||
|
||||
static int
|
||||
yaml_parser_load_mapping(yaml_parser_t *parser, yaml_event_t *first_event);
|
||||
|
||||
/*
|
||||
* Load the next document of the stream.
|
||||
*/
|
||||
|
||||
YAML_DECLARE(int)
|
||||
yaml_parser_load(yaml_parser_t *parser, yaml_document_t *document)
|
||||
{
|
||||
yaml_event_t event;
|
||||
|
||||
assert(parser); /* Non-NULL parser object is expected. */
|
||||
assert(document); /* Non-NULL document object is expected. */
|
||||
|
||||
memset(document, 0, sizeof(yaml_document_t));
|
||||
if (!STACK_INIT(parser, document->nodes, INITIAL_STACK_SIZE))
|
||||
goto error;
|
||||
|
||||
if (!parser->stream_start_produced) {
|
||||
if (!yaml_parser_parse(parser, &event)) goto error;
|
||||
assert(event.type == YAML_STREAM_START_EVENT);
|
||||
/* STREAM-START is expected. */
|
||||
}
|
||||
|
||||
if (parser->stream_end_produced) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!yaml_parser_parse(parser, &event)) goto error;
|
||||
if (event.type == YAML_STREAM_END_EVENT) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!STACK_INIT(parser, parser->aliases, INITIAL_STACK_SIZE))
|
||||
goto error;
|
||||
|
||||
parser->document = document;
|
||||
|
||||
if (!yaml_parser_load_document(parser, &event)) goto error;
|
||||
|
||||
yaml_parser_delete_aliases(parser);
|
||||
parser->document = NULL;
|
||||
|
||||
return 1;
|
||||
|
||||
error:
|
||||
|
||||
yaml_parser_delete_aliases(parser);
|
||||
yaml_document_delete(document);
|
||||
parser->document = NULL;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Set composer error.
|
||||
*/
|
||||
|
||||
static int
|
||||
yaml_parser_set_composer_error(yaml_parser_t *parser,
|
||||
const char *problem, yaml_mark_t problem_mark)
|
||||
{
|
||||
parser->error = YAML_COMPOSER_ERROR;
|
||||
parser->problem = problem;
|
||||
parser->problem_mark = problem_mark;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Set composer error with context.
|
||||
*/
|
||||
|
||||
static int
|
||||
yaml_parser_set_composer_error_context(yaml_parser_t *parser,
|
||||
const char *context, yaml_mark_t context_mark,
|
||||
const char *problem, yaml_mark_t problem_mark)
|
||||
{
|
||||
parser->error = YAML_COMPOSER_ERROR;
|
||||
parser->context = context;
|
||||
parser->context_mark = context_mark;
|
||||
parser->problem = problem;
|
||||
parser->problem_mark = problem_mark;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Delete the stack of aliases.
|
||||
*/
|
||||
|
||||
static void
|
||||
yaml_parser_delete_aliases(yaml_parser_t *parser)
|
||||
{
|
||||
while (!STACK_EMPTY(parser, parser->aliases)) {
|
||||
yaml_free(POP(parser, parser->aliases).anchor);
|
||||
}
|
||||
STACK_DEL(parser, parser->aliases);
|
||||
}
|
||||
|
||||
/*
|
||||
* Compose a document object.
|
||||
*/
|
||||
|
||||
static int
|
||||
yaml_parser_load_document(yaml_parser_t *parser, yaml_event_t *first_event)
|
||||
{
|
||||
yaml_event_t event;
|
||||
|
||||
assert(first_event->type == YAML_DOCUMENT_START_EVENT);
|
||||
/* DOCUMENT-START is expected. */
|
||||
|
||||
parser->document->version_directive
|
||||
= first_event->data.document_start.version_directive;
|
||||
parser->document->tag_directives.start
|
||||
= first_event->data.document_start.tag_directives.start;
|
||||
parser->document->tag_directives.end
|
||||
= first_event->data.document_start.tag_directives.end;
|
||||
parser->document->start_implicit
|
||||
= first_event->data.document_start.implicit;
|
||||
parser->document->start_mark = first_event->start_mark;
|
||||
|
||||
if (!yaml_parser_parse(parser, &event)) return 0;
|
||||
|
||||
if (!yaml_parser_load_node(parser, &event)) return 0;
|
||||
|
||||
if (!yaml_parser_parse(parser, &event)) return 0;
|
||||
assert(event.type == YAML_DOCUMENT_END_EVENT);
|
||||
/* DOCUMENT-END is expected. */
|
||||
|
||||
parser->document->end_implicit = event.data.document_end.implicit;
|
||||
parser->document->end_mark = event.end_mark;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Compose a node.
|
||||
*/
|
||||
|
||||
static int
|
||||
yaml_parser_load_node(yaml_parser_t *parser, yaml_event_t *first_event)
|
||||
{
|
||||
switch (first_event->type) {
|
||||
case YAML_ALIAS_EVENT:
|
||||
return yaml_parser_load_alias(parser, first_event);
|
||||
case YAML_SCALAR_EVENT:
|
||||
return yaml_parser_load_scalar(parser, first_event);
|
||||
case YAML_SEQUENCE_START_EVENT:
|
||||
return yaml_parser_load_sequence(parser, first_event);
|
||||
case YAML_MAPPING_START_EVENT:
|
||||
return yaml_parser_load_mapping(parser, first_event);
|
||||
default:
|
||||
assert(0); /* Could not happen. */
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Add an anchor.
|
||||
*/
|
||||
|
||||
static int
|
||||
yaml_parser_register_anchor(yaml_parser_t *parser,
|
||||
int index, yaml_char_t *anchor)
|
||||
{
|
||||
yaml_alias_data_t data;
|
||||
yaml_alias_data_t *alias_data;
|
||||
|
||||
if (!anchor) return 1;
|
||||
|
||||
data.anchor = anchor;
|
||||
data.index = index;
|
||||
data.mark = parser->document->nodes.start[index-1].start_mark;
|
||||
|
||||
for (alias_data = parser->aliases.start;
|
||||
alias_data != parser->aliases.top; alias_data ++) {
|
||||
if (strcmp((char *)alias_data->anchor, (char *)anchor) == 0) {
|
||||
yaml_free(anchor);
|
||||
return yaml_parser_set_composer_error_context(parser,
|
||||
"found duplicate anchor; first occurence",
|
||||
alias_data->mark, "second occurence", data.mark);
|
||||
}
|
||||
}
|
||||
|
||||
if (!PUSH(parser, parser->aliases, data)) {
|
||||
yaml_free(anchor);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Compose a node corresponding to an alias.
|
||||
*/
|
||||
|
||||
static int
|
||||
yaml_parser_load_alias(yaml_parser_t *parser, yaml_event_t *first_event)
|
||||
{
|
||||
yaml_char_t *anchor = first_event->data.alias.anchor;
|
||||
yaml_alias_data_t *alias_data;
|
||||
|
||||
for (alias_data = parser->aliases.start;
|
||||
alias_data != parser->aliases.top; alias_data ++) {
|
||||
if (strcmp((char *)alias_data->anchor, (char *)anchor) == 0) {
|
||||
yaml_free(anchor);
|
||||
return alias_data->index;
|
||||
}
|
||||
}
|
||||
|
||||
yaml_free(anchor);
|
||||
return yaml_parser_set_composer_error(parser, "found undefined alias",
|
||||
first_event->start_mark);
|
||||
}
|
||||
|
||||
/*
|
||||
* Compose a scalar node.
|
||||
*/
|
||||
|
||||
static int
|
||||
yaml_parser_load_scalar(yaml_parser_t *parser, yaml_event_t *first_event)
|
||||
{
|
||||
yaml_node_t node;
|
||||
int index;
|
||||
yaml_char_t *tag = first_event->data.scalar.tag;
|
||||
|
||||
if (!STACK_LIMIT(parser, parser->document->nodes, INT_MAX-1)) goto error;
|
||||
|
||||
if (!tag || strcmp((char *)tag, "!") == 0) {
|
||||
yaml_free(tag);
|
||||
tag = yaml_strdup((yaml_char_t *)YAML_DEFAULT_SCALAR_TAG);
|
||||
if (!tag) goto error;
|
||||
}
|
||||
|
||||
SCALAR_NODE_INIT(node, tag, first_event->data.scalar.value,
|
||||
first_event->data.scalar.length, first_event->data.scalar.style,
|
||||
first_event->start_mark, first_event->end_mark);
|
||||
|
||||
if (!PUSH(parser, parser->document->nodes, node)) goto error;
|
||||
|
||||
index = parser->document->nodes.top - parser->document->nodes.start;
|
||||
|
||||
if (!yaml_parser_register_anchor(parser, index,
|
||||
first_event->data.scalar.anchor)) return 0;
|
||||
|
||||
return index;
|
||||
|
||||
error:
|
||||
yaml_free(tag);
|
||||
yaml_free(first_event->data.scalar.anchor);
|
||||
yaml_free(first_event->data.scalar.value);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Compose a sequence node.
|
||||
*/
|
||||
|
||||
static int
|
||||
yaml_parser_load_sequence(yaml_parser_t *parser, yaml_event_t *first_event)
|
||||
{
|
||||
yaml_event_t event;
|
||||
yaml_node_t node;
|
||||
struct {
|
||||
yaml_node_item_t *start;
|
||||
yaml_node_item_t *end;
|
||||
yaml_node_item_t *top;
|
||||
} items = { NULL, NULL, NULL };
|
||||
int index, item_index;
|
||||
yaml_char_t *tag = first_event->data.sequence_start.tag;
|
||||
|
||||
if (!STACK_LIMIT(parser, parser->document->nodes, INT_MAX-1)) goto error;
|
||||
|
||||
if (!tag || strcmp((char *)tag, "!") == 0) {
|
||||
yaml_free(tag);
|
||||
tag = yaml_strdup((yaml_char_t *)YAML_DEFAULT_SEQUENCE_TAG);
|
||||
if (!tag) goto error;
|
||||
}
|
||||
|
||||
if (!STACK_INIT(parser, items, INITIAL_STACK_SIZE)) goto error;
|
||||
|
||||
SEQUENCE_NODE_INIT(node, tag, items.start, items.end,
|
||||
first_event->data.sequence_start.style,
|
||||
first_event->start_mark, first_event->end_mark);
|
||||
|
||||
if (!PUSH(parser, parser->document->nodes, node)) goto error;
|
||||
|
||||
index = parser->document->nodes.top - parser->document->nodes.start;
|
||||
|
||||
if (!yaml_parser_register_anchor(parser, index,
|
||||
first_event->data.sequence_start.anchor)) return 0;
|
||||
|
||||
if (!yaml_parser_parse(parser, &event)) return 0;
|
||||
|
||||
while (event.type != YAML_SEQUENCE_END_EVENT) {
|
||||
if (!STACK_LIMIT(parser,
|
||||
parser->document->nodes.start[index-1].data.sequence.items,
|
||||
INT_MAX-1)) return 0;
|
||||
item_index = yaml_parser_load_node(parser, &event);
|
||||
if (!item_index) return 0;
|
||||
if (!PUSH(parser,
|
||||
parser->document->nodes.start[index-1].data.sequence.items,
|
||||
item_index)) return 0;
|
||||
if (!yaml_parser_parse(parser, &event)) return 0;
|
||||
}
|
||||
|
||||
parser->document->nodes.start[index-1].end_mark = event.end_mark;
|
||||
|
||||
return index;
|
||||
|
||||
error:
|
||||
yaml_free(tag);
|
||||
yaml_free(first_event->data.sequence_start.anchor);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Compose a mapping node.
|
||||
*/
|
||||
|
||||
static int
|
||||
yaml_parser_load_mapping(yaml_parser_t *parser, yaml_event_t *first_event)
|
||||
{
|
||||
yaml_event_t event;
|
||||
yaml_node_t node;
|
||||
struct {
|
||||
yaml_node_pair_t *start;
|
||||
yaml_node_pair_t *end;
|
||||
yaml_node_pair_t *top;
|
||||
} pairs = { NULL, NULL, NULL };
|
||||
int index;
|
||||
yaml_node_pair_t pair;
|
||||
yaml_char_t *tag = first_event->data.mapping_start.tag;
|
||||
|
||||
if (!STACK_LIMIT(parser, parser->document->nodes, INT_MAX-1)) goto error;
|
||||
|
||||
if (!tag || strcmp((char *)tag, "!") == 0) {
|
||||
yaml_free(tag);
|
||||
tag = yaml_strdup((yaml_char_t *)YAML_DEFAULT_MAPPING_TAG);
|
||||
if (!tag) goto error;
|
||||
}
|
||||
|
||||
if (!STACK_INIT(parser, pairs, INITIAL_STACK_SIZE)) goto error;
|
||||
|
||||
MAPPING_NODE_INIT(node, tag, pairs.start, pairs.end,
|
||||
first_event->data.mapping_start.style,
|
||||
first_event->start_mark, first_event->end_mark);
|
||||
|
||||
if (!PUSH(parser, parser->document->nodes, node)) goto error;
|
||||
|
||||
index = parser->document->nodes.top - parser->document->nodes.start;
|
||||
|
||||
if (!yaml_parser_register_anchor(parser, index,
|
||||
first_event->data.mapping_start.anchor)) return 0;
|
||||
|
||||
if (!yaml_parser_parse(parser, &event)) return 0;
|
||||
|
||||
while (event.type != YAML_MAPPING_END_EVENT) {
|
||||
if (!STACK_LIMIT(parser,
|
||||
parser->document->nodes.start[index-1].data.mapping.pairs,
|
||||
INT_MAX-1)) return 0;
|
||||
pair.key = yaml_parser_load_node(parser, &event);
|
||||
if (!pair.key) return 0;
|
||||
if (!yaml_parser_parse(parser, &event)) return 0;
|
||||
pair.value = yaml_parser_load_node(parser, &event);
|
||||
if (!pair.value) return 0;
|
||||
if (!PUSH(parser,
|
||||
parser->document->nodes.start[index-1].data.mapping.pairs,
|
||||
pair)) return 0;
|
||||
if (!yaml_parser_parse(parser, &event)) return 0;
|
||||
}
|
||||
|
||||
parser->document->nodes.start[index-1].end_mark = event.end_mark;
|
||||
|
||||
return index;
|
||||
|
||||
error:
|
||||
yaml_free(tag);
|
||||
yaml_free(first_event->data.mapping_start.anchor);
|
||||
return 0;
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,469 @@
|
|||
|
||||
#include "yaml_private.h"
|
||||
|
||||
/*
|
||||
* Declarations.
|
||||
*/
|
||||
|
||||
static int
|
||||
yaml_parser_set_reader_error(yaml_parser_t *parser, const char *problem,
|
||||
size_t offset, int value);
|
||||
|
||||
static int
|
||||
yaml_parser_update_raw_buffer(yaml_parser_t *parser);
|
||||
|
||||
static int
|
||||
yaml_parser_determine_encoding(yaml_parser_t *parser);
|
||||
|
||||
YAML_DECLARE(int)
|
||||
yaml_parser_update_buffer(yaml_parser_t *parser, size_t length);
|
||||
|
||||
/*
|
||||
* Set the reader error and return 0.
|
||||
*/
|
||||
|
||||
static int
|
||||
yaml_parser_set_reader_error(yaml_parser_t *parser, const char *problem,
|
||||
size_t offset, int value)
|
||||
{
|
||||
parser->error = YAML_READER_ERROR;
|
||||
parser->problem = problem;
|
||||
parser->problem_offset = offset;
|
||||
parser->problem_value = value;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Byte order marks.
|
||||
*/
|
||||
|
||||
#define BOM_UTF8 "\xef\xbb\xbf"
|
||||
#define BOM_UTF16LE "\xff\xfe"
|
||||
#define BOM_UTF16BE "\xfe\xff"
|
||||
|
||||
/*
|
||||
* Determine the input stream encoding by checking the BOM symbol. If no BOM is
|
||||
* found, the UTF-8 encoding is assumed. Return 1 on success, 0 on failure.
|
||||
*/
|
||||
|
||||
static int
|
||||
yaml_parser_determine_encoding(yaml_parser_t *parser)
|
||||
{
|
||||
/* Ensure that we had enough bytes in the raw buffer. */
|
||||
|
||||
while (!parser->eof
|
||||
&& parser->raw_buffer.last - parser->raw_buffer.pointer < 3) {
|
||||
if (!yaml_parser_update_raw_buffer(parser)) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* Determine the encoding. */
|
||||
|
||||
if (parser->raw_buffer.last - parser->raw_buffer.pointer >= 2
|
||||
&& !memcmp(parser->raw_buffer.pointer, BOM_UTF16LE, 2)) {
|
||||
parser->encoding = YAML_UTF16LE_ENCODING;
|
||||
parser->raw_buffer.pointer += 2;
|
||||
parser->offset += 2;
|
||||
}
|
||||
else if (parser->raw_buffer.last - parser->raw_buffer.pointer >= 2
|
||||
&& !memcmp(parser->raw_buffer.pointer, BOM_UTF16BE, 2)) {
|
||||
parser->encoding = YAML_UTF16BE_ENCODING;
|
||||
parser->raw_buffer.pointer += 2;
|
||||
parser->offset += 2;
|
||||
}
|
||||
else if (parser->raw_buffer.last - parser->raw_buffer.pointer >= 3
|
||||
&& !memcmp(parser->raw_buffer.pointer, BOM_UTF8, 3)) {
|
||||
parser->encoding = YAML_UTF8_ENCODING;
|
||||
parser->raw_buffer.pointer += 3;
|
||||
parser->offset += 3;
|
||||
}
|
||||
else {
|
||||
parser->encoding = YAML_UTF8_ENCODING;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Update the raw buffer.
|
||||
*/
|
||||
|
||||
static int
|
||||
yaml_parser_update_raw_buffer(yaml_parser_t *parser)
|
||||
{
|
||||
size_t size_read = 0;
|
||||
|
||||
/* Return if the raw buffer is full. */
|
||||
|
||||
if (parser->raw_buffer.start == parser->raw_buffer.pointer
|
||||
&& parser->raw_buffer.last == parser->raw_buffer.end)
|
||||
return 1;
|
||||
|
||||
/* Return on EOF. */
|
||||
|
||||
if (parser->eof) return 1;
|
||||
|
||||
/* Move the remaining bytes in the raw buffer to the beginning. */
|
||||
|
||||
if (parser->raw_buffer.start < parser->raw_buffer.pointer
|
||||
&& parser->raw_buffer.pointer < parser->raw_buffer.last) {
|
||||
memmove(parser->raw_buffer.start, parser->raw_buffer.pointer,
|
||||
parser->raw_buffer.last - parser->raw_buffer.pointer);
|
||||
}
|
||||
parser->raw_buffer.last -=
|
||||
parser->raw_buffer.pointer - parser->raw_buffer.start;
|
||||
parser->raw_buffer.pointer = parser->raw_buffer.start;
|
||||
|
||||
/* Call the read handler to fill the buffer. */
|
||||
|
||||
if (!parser->read_handler(parser->read_handler_data, parser->raw_buffer.last,
|
||||
parser->raw_buffer.end - parser->raw_buffer.last, &size_read)) {
|
||||
return yaml_parser_set_reader_error(parser, "input error",
|
||||
parser->offset, -1);
|
||||
}
|
||||
parser->raw_buffer.last += size_read;
|
||||
if (!size_read) {
|
||||
parser->eof = 1;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Ensure that the buffer contains at least `length` characters.
|
||||
* Return 1 on success, 0 on failure.
|
||||
*
|
||||
* The length is supposed to be significantly less that the buffer size.
|
||||
*/
|
||||
|
||||
YAML_DECLARE(int)
|
||||
yaml_parser_update_buffer(yaml_parser_t *parser, size_t length)
|
||||
{
|
||||
int first = 1;
|
||||
|
||||
assert(parser->read_handler); /* Read handler must be set. */
|
||||
|
||||
/* If the EOF flag is set and the raw buffer is empty, do nothing. */
|
||||
|
||||
if (parser->eof && parser->raw_buffer.pointer == parser->raw_buffer.last)
|
||||
return 1;
|
||||
|
||||
/* Return if the buffer contains enough characters. */
|
||||
|
||||
if (parser->unread >= length)
|
||||
return 1;
|
||||
|
||||
/* Determine the input encoding if it is not known yet. */
|
||||
|
||||
if (!parser->encoding) {
|
||||
if (!yaml_parser_determine_encoding(parser))
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Move the unread characters to the beginning of the buffer. */
|
||||
|
||||
if (parser->buffer.start < parser->buffer.pointer
|
||||
&& parser->buffer.pointer < parser->buffer.last) {
|
||||
size_t size = parser->buffer.last - parser->buffer.pointer;
|
||||
memmove(parser->buffer.start, parser->buffer.pointer, size);
|
||||
parser->buffer.pointer = parser->buffer.start;
|
||||
parser->buffer.last = parser->buffer.start + size;
|
||||
}
|
||||
else if (parser->buffer.pointer == parser->buffer.last) {
|
||||
parser->buffer.pointer = parser->buffer.start;
|
||||
parser->buffer.last = parser->buffer.start;
|
||||
}
|
||||
|
||||
/* Fill the buffer until it has enough characters. */
|
||||
|
||||
while (parser->unread < length)
|
||||
{
|
||||
/* Fill the raw buffer if necessary. */
|
||||
|
||||
if (!first || parser->raw_buffer.pointer == parser->raw_buffer.last) {
|
||||
if (!yaml_parser_update_raw_buffer(parser)) return 0;
|
||||
}
|
||||
first = 0;
|
||||
|
||||
/* Decode the raw buffer. */
|
||||
|
||||
while (parser->raw_buffer.pointer != parser->raw_buffer.last)
|
||||
{
|
||||
unsigned int value = 0, value2 = 0;
|
||||
int incomplete = 0;
|
||||
unsigned char octet;
|
||||
unsigned int width = 0;
|
||||
int low, high;
|
||||
size_t k;
|
||||
size_t raw_unread = parser->raw_buffer.last - parser->raw_buffer.pointer;
|
||||
|
||||
/* Decode the next character. */
|
||||
|
||||
switch (parser->encoding)
|
||||
{
|
||||
case YAML_UTF8_ENCODING:
|
||||
|
||||
/*
|
||||
* Decode a UTF-8 character. Check RFC 3629
|
||||
* (http://www.ietf.org/rfc/rfc3629.txt) for more details.
|
||||
*
|
||||
* The following table (taken from the RFC) is used for
|
||||
* decoding.
|
||||
*
|
||||
* Char. number range | UTF-8 octet sequence
|
||||
* (hexadecimal) | (binary)
|
||||
* --------------------+------------------------------------
|
||||
* 0000 0000-0000 007F | 0xxxxxxx
|
||||
* 0000 0080-0000 07FF | 110xxxxx 10xxxxxx
|
||||
* 0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx
|
||||
* 0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
|
||||
*
|
||||
* Additionally, the characters in the range 0xD800-0xDFFF
|
||||
* are prohibited as they are reserved for use with UTF-16
|
||||
* surrogate pairs.
|
||||
*/
|
||||
|
||||
/* Determine the length of the UTF-8 sequence. */
|
||||
|
||||
octet = parser->raw_buffer.pointer[0];
|
||||
width = (octet & 0x80) == 0x00 ? 1 :
|
||||
(octet & 0xE0) == 0xC0 ? 2 :
|
||||
(octet & 0xF0) == 0xE0 ? 3 :
|
||||
(octet & 0xF8) == 0xF0 ? 4 : 0;
|
||||
|
||||
/* Check if the leading octet is valid. */
|
||||
|
||||
if (!width)
|
||||
return yaml_parser_set_reader_error(parser,
|
||||
"invalid leading UTF-8 octet",
|
||||
parser->offset, octet);
|
||||
|
||||
/* Check if the raw buffer contains an incomplete character. */
|
||||
|
||||
if (width > raw_unread) {
|
||||
if (parser->eof) {
|
||||
return yaml_parser_set_reader_error(parser,
|
||||
"incomplete UTF-8 octet sequence",
|
||||
parser->offset, -1);
|
||||
}
|
||||
incomplete = 1;
|
||||
break;
|
||||
}
|
||||
|
||||
/* Decode the leading octet. */
|
||||
|
||||
value = (octet & 0x80) == 0x00 ? octet & 0x7F :
|
||||
(octet & 0xE0) == 0xC0 ? octet & 0x1F :
|
||||
(octet & 0xF0) == 0xE0 ? octet & 0x0F :
|
||||
(octet & 0xF8) == 0xF0 ? octet & 0x07 : 0;
|
||||
|
||||
/* Check and decode the trailing octets. */
|
||||
|
||||
for (k = 1; k < width; k ++)
|
||||
{
|
||||
octet = parser->raw_buffer.pointer[k];
|
||||
|
||||
/* Check if the octet is valid. */
|
||||
|
||||
if ((octet & 0xC0) != 0x80)
|
||||
return yaml_parser_set_reader_error(parser,
|
||||
"invalid trailing UTF-8 octet",
|
||||
parser->offset+k, octet);
|
||||
|
||||
/* Decode the octet. */
|
||||
|
||||
value = (value << 6) + (octet & 0x3F);
|
||||
}
|
||||
|
||||
/* Check the length of the sequence against the value. */
|
||||
|
||||
if (!((width == 1) ||
|
||||
(width == 2 && value >= 0x80) ||
|
||||
(width == 3 && value >= 0x800) ||
|
||||
(width == 4 && value >= 0x10000)))
|
||||
return yaml_parser_set_reader_error(parser,
|
||||
"invalid length of a UTF-8 sequence",
|
||||
parser->offset, -1);
|
||||
|
||||
/* Check the range of the value. */
|
||||
|
||||
if ((value >= 0xD800 && value <= 0xDFFF) || value > 0x10FFFF)
|
||||
return yaml_parser_set_reader_error(parser,
|
||||
"invalid Unicode character",
|
||||
parser->offset, value);
|
||||
|
||||
break;
|
||||
|
||||
case YAML_UTF16LE_ENCODING:
|
||||
case YAML_UTF16BE_ENCODING:
|
||||
|
||||
low = (parser->encoding == YAML_UTF16LE_ENCODING ? 0 : 1);
|
||||
high = (parser->encoding == YAML_UTF16LE_ENCODING ? 1 : 0);
|
||||
|
||||
/*
|
||||
* The UTF-16 encoding is not as simple as one might
|
||||
* naively think. Check RFC 2781
|
||||
* (http://www.ietf.org/rfc/rfc2781.txt).
|
||||
*
|
||||
* Normally, two subsequent bytes describe a Unicode
|
||||
* character. However a special technique (called a
|
||||
* surrogate pair) is used for specifying character
|
||||
* values larger than 0xFFFF.
|
||||
*
|
||||
* A surrogate pair consists of two pseudo-characters:
|
||||
* high surrogate area (0xD800-0xDBFF)
|
||||
* low surrogate area (0xDC00-0xDFFF)
|
||||
*
|
||||
* The following formulas are used for decoding
|
||||
* and encoding characters using surrogate pairs:
|
||||
*
|
||||
* U = U' + 0x10000 (0x01 00 00 <= U <= 0x10 FF FF)
|
||||
* U' = yyyyyyyyyyxxxxxxxxxx (0 <= U' <= 0x0F FF FF)
|
||||
* W1 = 110110yyyyyyyyyy
|
||||
* W2 = 110111xxxxxxxxxx
|
||||
*
|
||||
* where U is the character value, W1 is the high surrogate
|
||||
* area, W2 is the low surrogate area.
|
||||
*/
|
||||
|
||||
/* Check for incomplete UTF-16 character. */
|
||||
|
||||
if (raw_unread < 2) {
|
||||
if (parser->eof) {
|
||||
return yaml_parser_set_reader_error(parser,
|
||||
"incomplete UTF-16 character",
|
||||
parser->offset, -1);
|
||||
}
|
||||
incomplete = 1;
|
||||
break;
|
||||
}
|
||||
|
||||
/* Get the character. */
|
||||
|
||||
value = parser->raw_buffer.pointer[low]
|
||||
+ (parser->raw_buffer.pointer[high] << 8);
|
||||
|
||||
/* Check for unexpected low surrogate area. */
|
||||
|
||||
if ((value & 0xFC00) == 0xDC00)
|
||||
return yaml_parser_set_reader_error(parser,
|
||||
"unexpected low surrogate area",
|
||||
parser->offset, value);
|
||||
|
||||
/* Check for a high surrogate area. */
|
||||
|
||||
if ((value & 0xFC00) == 0xD800) {
|
||||
|
||||
width = 4;
|
||||
|
||||
/* Check for incomplete surrogate pair. */
|
||||
|
||||
if (raw_unread < 4) {
|
||||
if (parser->eof) {
|
||||
return yaml_parser_set_reader_error(parser,
|
||||
"incomplete UTF-16 surrogate pair",
|
||||
parser->offset, -1);
|
||||
}
|
||||
incomplete = 1;
|
||||
break;
|
||||
}
|
||||
|
||||
/* Get the next character. */
|
||||
|
||||
value2 = parser->raw_buffer.pointer[low+2]
|
||||
+ (parser->raw_buffer.pointer[high+2] << 8);
|
||||
|
||||
/* Check for a low surrogate area. */
|
||||
|
||||
if ((value2 & 0xFC00) != 0xDC00)
|
||||
return yaml_parser_set_reader_error(parser,
|
||||
"expected low surrogate area",
|
||||
parser->offset+2, value2);
|
||||
|
||||
/* Generate the value of the surrogate pair. */
|
||||
|
||||
value = 0x10000 + ((value & 0x3FF) << 10) + (value2 & 0x3FF);
|
||||
}
|
||||
|
||||
else {
|
||||
width = 2;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
assert(1); /* Impossible. */
|
||||
}
|
||||
|
||||
/* Check if the raw buffer contains enough bytes to form a character. */
|
||||
|
||||
if (incomplete) break;
|
||||
|
||||
/*
|
||||
* Check if the character is in the allowed range:
|
||||
* #x9 | #xA | #xD | [#x20-#x7E] (8 bit)
|
||||
* | #x85 | [#xA0-#xD7FF] | [#xE000-#xFFFD] (16 bit)
|
||||
* | [#x10000-#x10FFFF] (32 bit)
|
||||
*/
|
||||
|
||||
if (! (value == 0x09 || value == 0x0A || value == 0x0D
|
||||
|| (value >= 0x20 && value <= 0x7E)
|
||||
|| (value == 0x85) || (value >= 0xA0 && value <= 0xD7FF)
|
||||
|| (value >= 0xE000 && value <= 0xFFFD)
|
||||
|| (value >= 0x10000 && value <= 0x10FFFF)))
|
||||
return yaml_parser_set_reader_error(parser,
|
||||
"control characters are not allowed",
|
||||
parser->offset, value);
|
||||
|
||||
/* Move the raw pointers. */
|
||||
|
||||
parser->raw_buffer.pointer += width;
|
||||
parser->offset += width;
|
||||
|
||||
/* Finally put the character into the buffer. */
|
||||
|
||||
/* 0000 0000-0000 007F -> 0xxxxxxx */
|
||||
if (value <= 0x7F) {
|
||||
*(parser->buffer.last++) = value;
|
||||
}
|
||||
/* 0000 0080-0000 07FF -> 110xxxxx 10xxxxxx */
|
||||
else if (value <= 0x7FF) {
|
||||
*(parser->buffer.last++) = 0xC0 + (value >> 6);
|
||||
*(parser->buffer.last++) = 0x80 + (value & 0x3F);
|
||||
}
|
||||
/* 0000 0800-0000 FFFF -> 1110xxxx 10xxxxxx 10xxxxxx */
|
||||
else if (value <= 0xFFFF) {
|
||||
*(parser->buffer.last++) = 0xE0 + (value >> 12);
|
||||
*(parser->buffer.last++) = 0x80 + ((value >> 6) & 0x3F);
|
||||
*(parser->buffer.last++) = 0x80 + (value & 0x3F);
|
||||
}
|
||||
/* 0001 0000-0010 FFFF -> 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
|
||||
else {
|
||||
*(parser->buffer.last++) = 0xF0 + (value >> 18);
|
||||
*(parser->buffer.last++) = 0x80 + ((value >> 12) & 0x3F);
|
||||
*(parser->buffer.last++) = 0x80 + ((value >> 6) & 0x3F);
|
||||
*(parser->buffer.last++) = 0x80 + (value & 0x3F);
|
||||
}
|
||||
|
||||
parser->unread ++;
|
||||
}
|
||||
|
||||
/* On EOF, put NUL into the buffer and return. */
|
||||
|
||||
if (parser->eof) {
|
||||
*(parser->buffer.last++) = '\0';
|
||||
parser->unread ++;
|
||||
return 1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (parser->offset >= PTRDIFF_MAX)
|
||||
return yaml_parser_set_reader_error(parser, "input is too long",
|
||||
PTRDIFF_MAX, -1);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,141 @@
|
|||
|
||||
#include "yaml_private.h"
|
||||
|
||||
/*
|
||||
* Declarations.
|
||||
*/
|
||||
|
||||
static int
|
||||
yaml_emitter_set_writer_error(yaml_emitter_t *emitter |