#
unsafe_libyaml/
writer.rs

    1use crate::ops::ForceAdd as _;
    2use crate::success::{Success, FAIL, OK};
    3use crate::yaml::size_t;
    4use crate::{
    5    libc, yaml_emitter_t, PointerExt, YAML_ANY_ENCODING, YAML_UTF16LE_ENCODING, YAML_UTF8_ENCODING,
    6    YAML_WRITER_ERROR,
    7};
    8use core::ptr::addr_of_mut;
    9
    10unsafe fn yaml_emitter_set_writer_error(
    11    emitter: *mut yaml_emitter_t,
    12    problem: *const libc::c_char,
    13) -> Success {
    14    (*emitter).error = YAML_WRITER_ERROR;
    15    let fresh0 = addr_of_mut!((*emitter).problem);
    16    *fresh0 = problem;
    17    FAIL
    18}
    19
    20/// Flush the accumulated characters to the output.
    21pub unsafe fn yaml_emitter_flush(emitter: *mut yaml_emitter_t) -> Success {
    22    __assert!(!emitter.is_null());
    23    __assert!(((*emitter).write_handler).is_some());
    24    __assert!((*emitter).encoding != YAML_ANY_ENCODING);
    25    let fresh1 = addr_of_mut!((*emitter).buffer.last);
    26    *fresh1 = (*emitter).buffer.pointer;
    27    let fresh2 = addr_of_mut!((*emitter).buffer.pointer);
    28    *fresh2 = (*emitter).buffer.start;
    29    if (*emitter).buffer.start == (*emitter).buffer.last {
    30        return OK;
    31    }
    32    if (*emitter).encoding == YAML_UTF8_ENCODING {
    33        if (*emitter).write_handler.expect("non-null function pointer")(
    34            (*emitter).write_handler_data,
    35            (*emitter).buffer.start,
    36            (*emitter)
    37                .buffer
    38                .last
    39                .c_offset_from((*emitter).buffer.start) as size_t,
    40        ) != 0
    41        {
    42            let fresh3 = addr_of_mut!((*emitter).buffer.last);
    43            *fresh3 = (*emitter).buffer.start;
    44            let fresh4 = addr_of_mut!((*emitter).buffer.pointer);
    45            *fresh4 = (*emitter).buffer.start;
    46            return OK;
    47        } else {
    48            return yaml_emitter_set_writer_error(
    49                emitter,
    50                b"write error\0" as *const u8 as *const libc::c_char,
    51            );
    52        }
    53    }
    54    let low: libc::c_int = if (*emitter).encoding == YAML_UTF16LE_ENCODING {
    55        0
    56    } else {
    57        1
    58    };
    59    let high: libc::c_int = if (*emitter).encoding == YAML_UTF16LE_ENCODING {
    60        1
    61    } else {
    62        0
    63    };
    64    while (*emitter).buffer.pointer != (*emitter).buffer.last {
    65        let mut octet: libc::c_uchar;
    66        let mut value: libc::c_uint;
    67        let mut k: size_t;
    68        octet = *(*emitter).buffer.pointer;
    69        let width: libc::c_uint = if octet & 0x80 == 0 {
    70            1
    71        } else if octet & 0xE0 == 0xC0 {
    72            2
    73        } else if octet & 0xF0 == 0xE0 {
    74            3
    75        } else if octet & 0xF8 == 0xF0 {
    76            4
    77        } else {
    78            0
    79        } as libc::c_uint;
    80        value = if octet & 0x80 == 0 {
    81            octet & 0x7F
    82        } else if octet & 0xE0 == 0xC0 {
    83            octet & 0x1F
    84        } else if octet & 0xF0 == 0xE0 {
    85            octet & 0xF
    86        } else if octet & 0xF8 == 0xF0 {
    87            octet & 0x7
    88        } else {
    89            0
    90        } as libc::c_uint;
    91        k = 1_u64;
    92        while k < width as libc::c_ulong {
    93            octet = *(*emitter).buffer.pointer.wrapping_offset(k as isize);
    94            value = (value << 6).force_add((octet & 0x3F) as libc::c_uint);
    95            k = k.force_add(1);
    96        }
    97        let fresh5 = addr_of_mut!((*emitter).buffer.pointer);
    98        *fresh5 = (*fresh5).wrapping_offset(width as isize);
    99        if value < 0x10000 {
    100            *(*emitter).raw_buffer.last.wrapping_offset(high as isize) =
    101                (value >> 8) as libc::c_uchar;
    102            *(*emitter).raw_buffer.last.wrapping_offset(low as isize) =
    103                (value & 0xFF) as libc::c_uchar;
    104            let fresh6 = addr_of_mut!((*emitter).raw_buffer.last);
    105            *fresh6 = (*fresh6).wrapping_offset(2_isize);
    106        } else {
    107            value = value.wrapping_sub(0x10000);
    108            *(*emitter).raw_buffer.last.wrapping_offset(high as isize) =
    109                0xD8_u32.force_add(value >> 18) as libc::c_uchar;
    110            *(*emitter).raw_buffer.last.wrapping_offset(low as isize) =
    111                (value >> 10 & 0xFF) as libc::c_uchar;
    112            *(*emitter)
    113                .raw_buffer
    114                .last
    115                .wrapping_offset((high + 2) as isize) =
    116                0xDC_u32.force_add(value >> 8 & 0xFF) as libc::c_uchar;
    117            *(*emitter)
    118                .raw_buffer
    119                .last
    120                .wrapping_offset((low + 2) as isize) = (value & 0xFF) as libc::c_uchar;
    121            let fresh7 = addr_of_mut!((*emitter).raw_buffer.last);
    122            *fresh7 = (*fresh7).wrapping_offset(4_isize);
    123        }
    124    }
    125    if (*emitter).write_handler.expect("non-null function pointer")(
    126        (*emitter).write_handler_data,
    127        (*emitter).raw_buffer.start,
    128        (*emitter)
    129            .raw_buffer
    130            .last
    131            .c_offset_from((*emitter).raw_buffer.start) as size_t,
    132    ) != 0
    133    {
    134        let fresh8 = addr_of_mut!((*emitter).buffer.last);
    135        *fresh8 = (*emitter).buffer.start;
    136        let fresh9 = addr_of_mut!((*emitter).buffer.pointer);
    137        *fresh9 = (*emitter).buffer.start;
    138        let fresh10 = addr_of_mut!((*emitter).raw_buffer.last);
    139        *fresh10 = (*emitter).raw_buffer.start;
    140        let fresh11 = addr_of_mut!((*emitter).raw_buffer.pointer);
    141        *fresh11 = (*emitter).raw_buffer.start;
    142        OK
    143    } else {
    144        yaml_emitter_set_writer_error(
    145            emitter,
    146            b"write error\0" as *const u8 as *const libc::c_char,
    147        )
    148    }
    149}