1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
//! # LibYML (a fork of unsafe-libyaml)
//!
//! [![GitHub][github-badge]][06]
//! [![Crates.io][crates-badge]][07]
//! [![lib.rs][libs-badge]][06]
//! [![Docs.rs][docs-badge]][08]
//! [![Codecov][codecov-badge]][09]
//! [![Build Status][build-badge]][10]
//!
//! This library is a fork of [unsafe-libyaml][01], a version of [libyaml][02]
//! translated from C to unsafe Rust with the assistance of [c2rust][03].
//!
//! This project, has been renamed to [LibYML][00] for simplicity and to avoid
//! confusion with the original [unsafe-libyaml][01] crate which is now
//! archived and no longer maintained.
//!
//! ## Credits and Acknowledgements
//!
//! This library is a fork of the excellent work done by [David Tolnay][04] and the
//! maintainers of the [unsafe-libyaml][01] library.
//!
//! LibYML has now evolved into a separate library with its own goals and direction
//! in mind and does not intend to replace the original unsafe-libyaml crate.
//!
//! If you are currently using unsafe-libyaml in your projects, we recommend
//! carefully evaluating your requirements and considering the stability and
//! maturity of the original library as well as looking at the features and
//! improvements offered by other libraries in the Rust ecosystem.
//!
//! I would like to express my sincere gratitude to [David Tolnay][04] and the
//! [unsafe-libyaml][01] and [libyaml][02] maintainers for their valuable
//! contributions to the Rust and C programming communities.
//!
//! ```toml
//! [dependencies]
//! libyml = "0.0.3"
//! ```
//!
//! Release notes are available under [GitHub releases][05].
//!
//! ### Rust Version Compatibility
//!
//! This library is compatible with Rust 1.60 and above.
//!
//! ## Features
//!
//! - **Serialization and Deserialization**: LibYML provides easy-to-use APIs for
//!   serializing Rust structs and enums into YAML format and deserializing YAML
//!   data into Rust types.
//! - **Custom Struct and Enum Support**: The library allows you to work with
//!   custom structs and enums, enabling seamless serialization and deserialization
//!   of your own data types.
//! - **Error Handling**: LibYML provides comprehensive error handling mechanisms,
//!   including detailed error messages and the ability to handle and recover from
//!   parsing and emission errors.
//! - **Streaming Support**: The library supports streaming of YAML data, allowing
//!   you to efficiently process large YAML documents incrementally.
//! - **Alias and Anchor Support**: LibYML handles YAML aliases and anchors,
//!   enabling you to work with complex YAML structures that involve references and
//!   duplicated data.
//! - **Tag Handling**: The library provides support for custom tags, allowing you
//!   to serialize and deserialize YAML data with specific type information.
//! - **Configurable Emitter**: LibYML allows you to customize the emitter
//!   settings, such as indentation, line width, and scalar style, to generate YAML
//!   output according to your preferences.
//! - **Extensive Documentation**: The library comes with detailed documentation
//!   and examples, making it easy to get started and understand how to use its
//!   various features effectively.
//! - **Safety and Efficiency**: LibYML is designed with safety and efficiency in
//!   mind. It minimizes the use of unsafe code and provides an interface that
//!   helps prevent common pitfalls and errors.
//!
//! ## Examples
//!
//! To get started with LibYML, you can use the examples provided in the
//! `examples` directory of the project.
//!
//! LibYML provides a set of comprehensive examples to demonstrate its usage and
//! capabilities. The examples cover various aspects of the library, including
//! initializing and deleting parsers, setting input strings, memory management,
//! string manipulation, and more.
//!
//! To run the examples, clone the repository and navigate to the examples
//! directory. Each example is contained in a separate file, focusing on specific
//! functionalities or use cases.
//!
//! For instance, to run all the example demonstrating the usage of the LibYML
//! APIs, you can execute the following command:
//!
//! ```shell
//! cargo run --example example
//! ```
//!
//! The command will execute all the examples code, demonstrating all the features
//! and use cases of the LibYML library.
//!
//! Here are a few notable examples you can also run individually:
//!
//! ### 1) The `apis` Example
//!
//! The `api.rs` file provides a set of low-level API functions for working with
//! YAML data in the LibYML library. The `apis` example showcases the usage of
//! these low-level APIs covering functions such as:
//!
//! - `yaml_parser_initialize` - Initialize a YAML parser,
//! - `yaml_parser_delete` - Delete a YAML parser,
//! - `yaml_parser_set_input_string` - Set the input string for the parser,
//! - `yaml_malloc` - Allocate memory,
//! - `yaml_realloc` - Reallocate memory,
//! - `yaml_free` - Free memory,
//! - `yaml_strdup` - Duplicate a string,
//! - `yaml_string_extend` - Extend a string,
//! - `yaml_string_join` - Join strings,
//!
//! You can run the `apis` example using the following command:
//!
//! ```shell
//! cargo run --example apis
//! ```
//!
//! ## License
//!
//! [MIT license](https://opensource.org/license/MIT), same as libyaml.
//!
//! [00]: https://libyml.com
//! [01]: https://github.com/dtolnay/unsafe-libyaml
//! [02]: https://github.com/yaml/libyaml/tree/2c891fc7a770e8ba2fec34fc6b545c672beb37e6
//! [03]: https://github.com/immunant/c2rust
//! [04]: https://github.com/dtolnay
//! [05]: https://github.com/sebastienrousseau/libyml/releases
//! [06]: https://github.com/sebastienrousseau/libyml
//! [07]: https://crates.io/crates/libyml
//! [08]: https://docs.rs/libyml
//! [09]: https://codecov.io/gh/sebastienrousseau/libyml
//! [10]: https://github.com/sebastienrousseau/libyml/actions?query=branch%3Amaster
//! [build-badge]: https://img.shields.io/github/actions/workflow/status/sebastienrousseau/libyml/release.yml?branch=master&style=for-the-badge&logo=github
//! [codecov-badge]: https://img.shields.io/codecov/c/github/sebastienrousseau/libyml?style=for-the-badge&token=Q9KJ6XXL67&logo=codecov
//! [crates-badge]: https://img.shields.io/crates/v/libyml.svg?style=for-the-badge&color=fc8d62&logo=rust
//! [libs-badge]: https://img.shields.io/badge/lib.rs-v0.0.3-orange.svg?style=for-the-badge
//! [docs-badge]: https://img.shields.io/badge/docs.rs-libyml-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs
//! [github-badge]: https://img.shields.io/badge/github-sebastienrousseau/libyml-8da0cb?style=for-the-badge&labelColor=555555&logo=github

//!

#![no_std]
#![doc(
    html_favicon_url = "https://kura.pro/libyml/images/favicon.ico",
    html_logo_url = "https://kura.pro/libyml/images/logos/libyml.svg",
    html_root_url = "https://docs.rs/libyml"
)]
#![crate_name = "libyml"]
#![crate_type = "lib"]

extern crate alloc;

use core::mem::size_of;

mod libc {
    pub(crate) use core::ffi::c_void;
    pub(crate) use core::primitive::{
        i32 as c_int, i64 as c_long, i8 as c_char, u32 as c_uint,
        u64 as c_ulong, u8 as c_uchar,
    };
}

#[macro_use]
mod externs {
    use crate::libc;
    use crate::ops::{die, ForceAdd as _, ForceInto as _};
    use alloc::alloc::{self as rust, Layout};
    use core::mem::{self, MaybeUninit};
    use core::ptr;
    use core::slice;

    const HEADER: usize = {
        let need_len = mem::size_of::<usize>();
        // Round up to multiple of MALLOC_ALIGN.
        (need_len + MALLOC_ALIGN - 1) & !(MALLOC_ALIGN - 1)
    };

    // `max_align_t` may be bigger than this, but libyaml does not use `long
    // double` or u128.
    const MALLOC_ALIGN: usize = {
        let int_align = mem::align_of::<libc::c_ulong>();
        let ptr_align = mem::align_of::<usize>();
        if int_align >= ptr_align {
            int_align
        } else {
            ptr_align
        }
    };

    pub(crate) unsafe fn malloc(
        size: libc::c_ulong,
    ) -> *mut libc::c_void {
        let size = HEADER.force_add(size.force_into());
        let layout = Layout::from_size_align(size, MALLOC_ALIGN)
            .ok()
            .unwrap_or_else(die);
        let memory = rust::alloc(layout);
        if memory.is_null() {
            rust::handle_alloc_error(layout);
        }
        memory.cast::<usize>().write(size);
        memory.add(HEADER).cast()
    }

    pub(crate) unsafe fn realloc(
        ptr: *mut libc::c_void,
        new_size: libc::c_ulong,
    ) -> *mut libc::c_void {
        let mut memory = ptr.cast::<u8>().sub(HEADER);
        let size = memory.cast::<usize>().read();
        let layout =
            Layout::from_size_align_unchecked(size, MALLOC_ALIGN);
        let new_size = HEADER.force_add(new_size.force_into());
        let new_layout =
            Layout::from_size_align(new_size, MALLOC_ALIGN)
                .ok()
                .unwrap_or_else(die);
        memory = rust::realloc(memory, layout, new_size);
        if memory.is_null() {
            rust::handle_alloc_error(new_layout);
        }
        memory.cast::<usize>().write(new_size);
        memory.add(HEADER).cast()
    }

    pub(crate) unsafe fn free(ptr: *mut libc::c_void) {
        let memory = ptr.cast::<u8>().sub(HEADER);
        let size = memory.cast::<usize>().read();
        let layout =
            Layout::from_size_align_unchecked(size, MALLOC_ALIGN);
        rust::dealloc(memory, layout);
    }

    pub(crate) unsafe fn memcmp(
        lhs: *const libc::c_void,
        rhs: *const libc::c_void,
        count: libc::c_ulong,
    ) -> libc::c_int {
        let lhs =
            slice::from_raw_parts(lhs.cast::<u8>(), count as usize);
        let rhs =
            slice::from_raw_parts(rhs.cast::<u8>(), count as usize);
        lhs.cmp(rhs) as libc::c_int
    }

    pub(crate) unsafe fn memcpy(
        dest: *mut libc::c_void,
        src: *const libc::c_void,
        count: libc::c_ulong,
    ) -> *mut libc::c_void {
        ptr::copy_nonoverlapping(
            src.cast::<MaybeUninit<u8>>(),
            dest.cast::<MaybeUninit<u8>>(),
            count as usize,
        );
        dest
    }

    pub(crate) unsafe fn memmove(
        dest: *mut libc::c_void,
        src: *const libc::c_void,
        count: libc::c_ulong,
    ) -> *mut libc::c_void {
        ptr::copy(
            src.cast::<MaybeUninit<u8>>(),
            dest.cast::<MaybeUninit<u8>>(),
            count as usize,
        );
        dest
    }

    pub(crate) unsafe fn memset(
        dest: *mut libc::c_void,
        ch: libc::c_int,
        count: libc::c_ulong,
    ) -> *mut libc::c_void {
        ptr::write_bytes(dest.cast::<u8>(), ch as u8, count as usize);
        dest
    }

    pub(crate) unsafe fn strcmp(
        lhs: *const libc::c_char,
        rhs: *const libc::c_char,
    ) -> libc::c_int {
        let lhs = slice::from_raw_parts(
            lhs.cast::<u8>(),
            strlen(lhs) as usize,
        );
        let rhs = slice::from_raw_parts(
            rhs.cast::<u8>(),
            strlen(rhs) as usize,
        );
        lhs.cmp(rhs) as libc::c_int
    }

    pub(crate) unsafe fn strdup(
        src: *const libc::c_char,
    ) -> *mut libc::c_char {
        let len = strlen(src);
        let dest = malloc(len + 1);
        memcpy(dest, src.cast(), len + 1);
        dest.cast()
    }

    pub(crate) unsafe fn strlen(
        str: *const libc::c_char,
    ) -> libc::c_ulong {
        let mut end = str;
        while *end != 0 {
            end = end.add(1);
        }
        end.offset_from(str) as libc::c_ulong
    }

    pub(crate) unsafe fn strncmp(
        lhs: *const libc::c_char,
        rhs: *const libc::c_char,
        mut count: libc::c_ulong,
    ) -> libc::c_int {
        let mut lhs = lhs.cast::<u8>();
        let mut rhs = rhs.cast::<u8>();
        while count > 0 && *lhs != 0 && *lhs == *rhs {
            lhs = lhs.add(1);
            rhs = rhs.add(1);
            count -= 1;
        }
        if count == 0 {
            0
        } else {
            (*lhs).cmp(&*rhs) as libc::c_int
        }
    }

    macro_rules! __assert {
        (false $(,)?) => {
            $crate::externs::__assert_fail(
                stringify!(false),
                file!(),
                line!(),
            )
        };
        ($assertion:expr $(,)?) => {
            if !$assertion {
                $crate::externs::__assert_fail(
                    stringify!($assertion),
                    file!(),
                    line!(),
                );
            }
        };
    }

    pub(crate) unsafe fn __assert_fail(
        __assertion: &'static str,
        __file: &'static str,
        __line: u32,
    ) -> ! {
        struct Abort;
        impl Drop for Abort {
            fn drop(&mut self) {
                panic!();
            }
        }
        let _abort_on_panic = Abort;
        panic!(
            "{}:{}: Assertion `{}` failed.",
            __file, __line, __assertion
        );
    }
}

mod fmt {
    use crate::yaml::yaml_char_t;
    use core::fmt::{self, Write};
    use core::ptr;

    pub(crate) struct WriteToPtr {
        ptr: *mut yaml_char_t,
    }

    impl WriteToPtr {
        pub(crate) unsafe fn new(ptr: *mut yaml_char_t) -> Self {
            WriteToPtr { ptr }
        }

        pub(crate) fn write_fmt(&mut self, args: fmt::Arguments<'_>) {
            let _ = Write::write_fmt(self, args);
        }
    }

    impl Write for WriteToPtr {
        fn write_str(&mut self, s: &str) -> fmt::Result {
            unsafe {
                ptr::copy_nonoverlapping(s.as_ptr(), self.ptr, s.len());
                self.ptr = self.ptr.add(s.len());
            }
            Ok(())
        }
    }
}

trait PointerExt: Sized {
    fn c_offset_from(self, origin: Self) -> isize;
}

impl<T> PointerExt for *const T {
    fn c_offset_from(self, origin: *const T) -> isize {
        (self as isize - origin as isize) / size_of::<T>() as isize
    }
}

impl<T> PointerExt for *mut T {
    fn c_offset_from(self, origin: *mut T) -> isize {
        (self as isize - origin as isize) / size_of::<T>() as isize
    }
}

#[macro_use]
mod macros;

/// API module for LibYML
pub mod api;
mod dumper;
mod emitter;
mod loader;
mod ops;
mod parser;
mod reader;
mod scanner;
mod success;
mod writer;
/// YAML API module for LibYML
pub mod yaml;

pub use crate::api::{
    yaml_alias_event_initialize, yaml_document_add_mapping,
    yaml_document_add_scalar, yaml_document_add_sequence,
    yaml_document_append_mapping_pair,
    yaml_document_append_sequence_item, yaml_document_delete,
    yaml_document_end_event_initialize, yaml_document_get_node,
    yaml_document_get_root_node, yaml_document_initialize,
    yaml_document_start_event_initialize, yaml_emitter_delete,
    yaml_emitter_initialize, yaml_emitter_set_break,
    yaml_emitter_set_canonical, yaml_emitter_set_encoding,
    yaml_emitter_set_indent, yaml_emitter_set_output,
    yaml_emitter_set_output_string, yaml_emitter_set_unicode,
    yaml_emitter_set_width, yaml_event_delete,
    yaml_mapping_end_event_initialize,
    yaml_mapping_start_event_initialize, yaml_parser_delete,
    yaml_parser_initialize, yaml_parser_set_encoding,
    yaml_parser_set_input, yaml_parser_set_input_string,
    yaml_scalar_event_initialize, yaml_sequence_end_event_initialize,
    yaml_sequence_start_event_initialize,
    yaml_stream_end_event_initialize,
    yaml_stream_start_event_initialize, yaml_token_delete,
};
pub use crate::dumper::{
    yaml_emitter_close, yaml_emitter_dump, yaml_emitter_open,
};
pub use crate::emitter::yaml_emitter_emit;
pub use crate::loader::yaml_parser_load;
pub use crate::parser::yaml_parser_parse;
pub use crate::scanner::yaml_parser_scan;
pub use crate::writer::yaml_emitter_flush;
pub use crate::yaml::{
    YamlAliasDataT, YamlBreakT, YamlDocumentT, YamlEmitterStateT,
    YamlEmitterT, YamlEncodingT, YamlErrorTypeT, YamlEventT,
    YamlEventTypeT, YamlMappingStyleT, YamlMarkT, YamlNodeItemT,
    YamlNodePairT, YamlNodeT, YamlNodeTypeT, YamlParserStateT,
    YamlParserT, YamlReadHandlerT, YamlScalarStyleT,
    YamlSequenceStyleT, YamlSimpleKeyT, YamlStackT, YamlTagDirectiveT,
    YamlTokenT, YamlTokenTypeT, YamlVersionDirectiveT,
    YamlWriteHandlerT,
};
#[doc(hidden)]
pub use crate::yaml::{
    YamlBreakT::*, YamlEmitterStateT::*, YamlEncodingT::*,
    YamlErrorTypeT::*, YamlEventTypeT::*, YamlMappingStyleT::*,
    YamlNodeTypeT::*, YamlParserStateT::*, YamlScalarStyleT::*,
    YamlSequenceStyleT::*, YamlTokenTypeT::*,
};