Coverage Report

Created: 2025-02-25 06:39

/rust/registry/src/index.crates.io-6f17d22bba15001f/unsafe-libyaml-0.2.11/src/reader.rs
Line
Count
Source (jump to first uncovered line)
1
use crate::externs::{memcmp, memmove};
2
use crate::ops::ForceAdd as _;
3
use crate::success::{Success, FAIL, OK};
4
use crate::yaml::{size_t, yaml_char_t};
5
use crate::{
6
    libc, yaml_parser_t, PointerExt, YAML_ANY_ENCODING, YAML_READER_ERROR, YAML_UTF16BE_ENCODING,
7
    YAML_UTF16LE_ENCODING, YAML_UTF8_ENCODING,
8
};
9
use core::ptr::addr_of_mut;
10
11
0
unsafe fn yaml_parser_set_reader_error(
12
0
    parser: *mut yaml_parser_t,
13
0
    problem: *const libc::c_char,
14
0
    offset: size_t,
15
0
    value: libc::c_int,
16
0
) -> Success {
17
0
    (*parser).error = YAML_READER_ERROR;
18
0
    let fresh0 = addr_of_mut!((*parser).problem);
19
0
    *fresh0 = problem;
20
0
    (*parser).problem_offset = offset;
21
0
    (*parser).problem_value = value;
22
0
    FAIL
23
0
}
24
25
const BOM_UTF8: *const libc::c_char = b"\xEF\xBB\xBF\0" as *const u8 as *const libc::c_char;
26
const BOM_UTF16LE: *const libc::c_char = b"\xFF\xFE\0" as *const u8 as *const libc::c_char;
27
const BOM_UTF16BE: *const libc::c_char = b"\xFE\xFF\0" as *const u8 as *const libc::c_char;
28
29
0
unsafe fn yaml_parser_determine_encoding(parser: *mut yaml_parser_t) -> Success {
30
0
    while !(*parser).eof
31
0
        && ((*parser)
32
0
            .raw_buffer
33
0
            .last
34
0
            .c_offset_from((*parser).raw_buffer.pointer) as libc::c_long)
35
0
            < 3_i64
36
    {
37
0
        if yaml_parser_update_raw_buffer(parser).fail {
38
0
            return FAIL;
39
0
        }
40
    }
41
0
    if (*parser)
42
0
        .raw_buffer
43
0
        .last
44
0
        .c_offset_from((*parser).raw_buffer.pointer) as libc::c_long
45
0
        >= 2_i64
46
0
        && memcmp(
47
0
            (*parser).raw_buffer.pointer as *const libc::c_void,
48
0
            BOM_UTF16LE as *const libc::c_void,
49
0
            2_u64,
50
0
        ) == 0
51
0
    {
52
0
        (*parser).encoding = YAML_UTF16LE_ENCODING;
53
0
        let fresh1 = addr_of_mut!((*parser).raw_buffer.pointer);
54
0
        *fresh1 = (*fresh1).wrapping_offset(2_isize);
55
0
        let fresh2 = addr_of_mut!((*parser).offset);
56
0
        *fresh2 = (*fresh2 as libc::c_ulong).force_add(2_u64) as size_t;
57
0
    } else if (*parser)
58
0
        .raw_buffer
59
0
        .last
60
0
        .c_offset_from((*parser).raw_buffer.pointer) as libc::c_long
61
0
        >= 2_i64
62
0
        && memcmp(
63
0
            (*parser).raw_buffer.pointer as *const libc::c_void,
64
0
            BOM_UTF16BE as *const libc::c_void,
65
0
            2_u64,
66
0
        ) == 0
67
0
    {
68
0
        (*parser).encoding = YAML_UTF16BE_ENCODING;
69
0
        let fresh3 = addr_of_mut!((*parser).raw_buffer.pointer);
70
0
        *fresh3 = (*fresh3).wrapping_offset(2_isize);
71
0
        let fresh4 = addr_of_mut!((*parser).offset);
72
0
        *fresh4 = (*fresh4 as libc::c_ulong).force_add(2_u64) as size_t;
73
0
    } else if (*parser)
74
0
        .raw_buffer
75
0
        .last
76
0
        .c_offset_from((*parser).raw_buffer.pointer) as libc::c_long
77
0
        >= 3_i64
78
0
        && memcmp(
79
0
            (*parser).raw_buffer.pointer as *const libc::c_void,
80
0
            BOM_UTF8 as *const libc::c_void,
81
0
            3_u64,
82
0
        ) == 0
83
0
    {
84
0
        (*parser).encoding = YAML_UTF8_ENCODING;
85
0
        let fresh5 = addr_of_mut!((*parser).raw_buffer.pointer);
86
0
        *fresh5 = (*fresh5).wrapping_offset(3_isize);
87
0
        let fresh6 = addr_of_mut!((*parser).offset);
88
0
        *fresh6 = (*fresh6 as libc::c_ulong).force_add(3_u64) as size_t;
89
0
    } else {
90
0
        (*parser).encoding = YAML_UTF8_ENCODING;
91
0
    }
92
0
    OK
93
0
}
94
95
0
unsafe fn yaml_parser_update_raw_buffer(parser: *mut yaml_parser_t) -> Success {
96
0
    let mut size_read: size_t = 0_u64;
97
0
    if (*parser).raw_buffer.start == (*parser).raw_buffer.pointer
98
0
        && (*parser).raw_buffer.last == (*parser).raw_buffer.end
99
    {
100
0
        return OK;
101
0
    }
102
0
    if (*parser).eof {
103
0
        return OK;
104
0
    }
105
0
    if (*parser).raw_buffer.start < (*parser).raw_buffer.pointer
106
0
        && (*parser).raw_buffer.pointer < (*parser).raw_buffer.last
107
0
    {
108
0
        memmove(
109
0
            (*parser).raw_buffer.start as *mut libc::c_void,
110
0
            (*parser).raw_buffer.pointer as *const libc::c_void,
111
0
            (*parser)
112
0
                .raw_buffer
113
0
                .last
114
0
                .c_offset_from((*parser).raw_buffer.pointer) as libc::c_long
115
0
                as libc::c_ulong,
116
0
        );
117
0
    }
118
0
    let fresh7 = addr_of_mut!((*parser).raw_buffer.last);
119
0
    *fresh7 = (*fresh7).wrapping_offset(
120
0
        -((*parser)
121
0
            .raw_buffer
122
0
            .pointer
123
0
            .c_offset_from((*parser).raw_buffer.start) as libc::c_long as isize),
124
0
    );
125
0
    let fresh8 = addr_of_mut!((*parser).raw_buffer.pointer);
126
0
    *fresh8 = (*parser).raw_buffer.start;
127
0
    if (*parser).read_handler.expect("non-null function pointer")(
128
0
        (*parser).read_handler_data,
129
0
        (*parser).raw_buffer.last,
130
0
        (*parser)
131
0
            .raw_buffer
132
0
            .end
133
0
            .c_offset_from((*parser).raw_buffer.last) as size_t,
134
0
        addr_of_mut!(size_read),
135
0
    ) == 0
136
    {
137
0
        return yaml_parser_set_reader_error(
138
0
            parser,
139
0
            b"input error\0" as *const u8 as *const libc::c_char,
140
0
            (*parser).offset,
141
0
            -1,
142
0
        );
143
0
    }
144
0
    let fresh9 = addr_of_mut!((*parser).raw_buffer.last);
145
0
    *fresh9 = (*fresh9).wrapping_offset(size_read as isize);
146
0
    if size_read == 0 {
147
0
        (*parser).eof = true;
148
0
    }
149
0
    OK
150
0
}
151
152
0
pub(crate) unsafe fn yaml_parser_update_buffer(
153
0
    parser: *mut yaml_parser_t,
154
0
    length: size_t,
155
0
) -> Success {
156
0
    let mut first = true;
157
0
    __assert!(((*parser).read_handler).is_some());
158
0
    if (*parser).eof && (*parser).raw_buffer.pointer == (*parser).raw_buffer.last {
159
0
        return OK;
160
0
    }
161
0
    if (*parser).unread >= length {
162
0
        return OK;
163
0
    }
164
0
    if (*parser).encoding == YAML_ANY_ENCODING {
165
0
        if yaml_parser_determine_encoding(parser).fail {
166
0
            return FAIL;
167
0
        }
168
0
    }
169
0
    if (*parser).buffer.start < (*parser).buffer.pointer
170
0
        && (*parser).buffer.pointer < (*parser).buffer.last
171
0
    {
172
0
        let size: size_t = (*parser)
173
0
            .buffer
174
0
            .last
175
0
            .c_offset_from((*parser).buffer.pointer) as size_t;
176
0
        memmove(
177
0
            (*parser).buffer.start as *mut libc::c_void,
178
0
            (*parser).buffer.pointer as *const libc::c_void,
179
0
            size,
180
0
        );
181
0
        let fresh10 = addr_of_mut!((*parser).buffer.pointer);
182
0
        *fresh10 = (*parser).buffer.start;
183
0
        let fresh11 = addr_of_mut!((*parser).buffer.last);
184
0
        *fresh11 = (*parser).buffer.start.wrapping_offset(size as isize);
185
0
    } else if (*parser).buffer.pointer == (*parser).buffer.last {
186
0
        let fresh12 = addr_of_mut!((*parser).buffer.pointer);
187
0
        *fresh12 = (*parser).buffer.start;
188
0
        let fresh13 = addr_of_mut!((*parser).buffer.last);
189
0
        *fresh13 = (*parser).buffer.start;
190
0
    }
191
0
    while (*parser).unread < length {
192
0
        if !first || (*parser).raw_buffer.pointer == (*parser).raw_buffer.last {
193
0
            if yaml_parser_update_raw_buffer(parser).fail {
194
0
                return FAIL;
195
0
            }
196
0
        }
197
0
        first = false;
198
0
        while (*parser).raw_buffer.pointer != (*parser).raw_buffer.last {
199
0
            let mut value: libc::c_uint = 0;
200
0
            let value2: libc::c_uint;
201
0
            let mut incomplete = false;
202
0
            let mut octet: libc::c_uchar;
203
0
            let mut width: libc::c_uint = 0;
204
0
            let low: libc::c_int;
205
0
            let high: libc::c_int;
206
0
            let mut k: size_t;
207
0
            let raw_unread: size_t = (*parser)
208
0
                .raw_buffer
209
0
                .last
210
0
                .c_offset_from((*parser).raw_buffer.pointer)
211
0
                as size_t;
212
0
            match (*parser).encoding {
213
                YAML_UTF8_ENCODING => {
214
0
                    octet = *(*parser).raw_buffer.pointer;
215
0
                    width = if octet & 0x80 == 0 {
216
0
                        1
217
0
                    } else if octet & 0xE0 == 0xC0 {
218
0
                        2
219
0
                    } else if octet & 0xF0 == 0xE0 {
220
0
                        3
221
0
                    } else if octet & 0xF8 == 0xF0 {
222
0
                        4
223
                    } else {
224
0
                        0
225
                    } as libc::c_uint;
226
0
                    if width == 0 {
227
0
                        return yaml_parser_set_reader_error(
228
0
                            parser,
229
0
                            b"invalid leading UTF-8 octet\0" as *const u8 as *const libc::c_char,
230
0
                            (*parser).offset,
231
0
                            octet as libc::c_int,
232
0
                        );
233
0
                    }
234
0
                    if width as libc::c_ulong > raw_unread {
235
0
                        if (*parser).eof {
236
0
                            return yaml_parser_set_reader_error(
237
0
                                parser,
238
0
                                b"incomplete UTF-8 octet sequence\0" as *const u8
239
0
                                    as *const libc::c_char,
240
0
                                (*parser).offset,
241
0
                                -1,
242
0
                            );
243
0
                        }
244
0
                        incomplete = true;
245
                    } else {
246
0
                        value = if octet & 0x80 == 0 {
247
0
                            octet & 0x7F
248
0
                        } else if octet & 0xE0 == 0xC0 {
249
0
                            octet & 0x1F
250
0
                        } else if octet & 0xF0 == 0xE0 {
251
0
                            octet & 0xF
252
0
                        } else if octet & 0xF8 == 0xF0 {
253
0
                            octet & 0x7
254
                        } else {
255
0
                            0
256
                        } as libc::c_uint;
257
0
                        k = 1_u64;
258
0
                        while k < width as libc::c_ulong {
259
0
                            octet = *(*parser).raw_buffer.pointer.wrapping_offset(k as isize);
260
0
                            if octet & 0xC0 != 0x80 {
261
0
                                return yaml_parser_set_reader_error(
262
0
                                    parser,
263
0
                                    b"invalid trailing UTF-8 octet\0" as *const u8
264
0
                                        as *const libc::c_char,
265
0
                                    (*parser).offset.force_add(k),
266
0
                                    octet as libc::c_int,
267
0
                                );
268
0
                            }
269
0
                            value = (value << 6).force_add((octet & 0x3F) as libc::c_uint);
270
0
                            k = k.force_add(1);
271
                        }
272
0
                        if !(width == 1
273
0
                            || width == 2 && value >= 0x80
274
0
                            || width == 3 && value >= 0x800
275
0
                            || width == 4 && value >= 0x10000)
276
                        {
277
0
                            return yaml_parser_set_reader_error(
278
0
                                parser,
279
0
                                b"invalid length of a UTF-8 sequence\0" as *const u8
280
0
                                    as *const libc::c_char,
281
0
                                (*parser).offset,
282
0
                                -1,
283
0
                            );
284
0
                        }
285
0
                        if value >= 0xD800 && value <= 0xDFFF || value > 0x10FFFF {
286
0
                            return yaml_parser_set_reader_error(
287
0
                                parser,
288
0
                                b"invalid Unicode character\0" as *const u8 as *const libc::c_char,
289
0
                                (*parser).offset,
290
0
                                value as libc::c_int,
291
0
                            );
292
0
                        }
293
                    }
294
                }
295
                YAML_UTF16LE_ENCODING | YAML_UTF16BE_ENCODING => {
296
0
                    low = if (*parser).encoding == YAML_UTF16LE_ENCODING {
297
0
                        0
298
                    } else {
299
0
                        1
300
                    };
301
0
                    high = if (*parser).encoding == YAML_UTF16LE_ENCODING {
302
0
                        1
303
                    } else {
304
0
                        0
305
                    };
306
0
                    if raw_unread < 2_u64 {
307
0
                        if (*parser).eof {
308
0
                            return yaml_parser_set_reader_error(
309
0
                                parser,
310
0
                                b"incomplete UTF-16 character\0" as *const u8
311
0
                                    as *const libc::c_char,
312
0
                                (*parser).offset,
313
0
                                -1,
314
0
                            );
315
0
                        }
316
0
                        incomplete = true;
317
                    } else {
318
0
                        value = (*(*parser).raw_buffer.pointer.wrapping_offset(low as isize)
319
0
                            as libc::c_int
320
0
                            + ((*(*parser).raw_buffer.pointer.wrapping_offset(high as isize)
321
0
                                as libc::c_int)
322
0
                                << 8)) as libc::c_uint;
323
0
                        if value & 0xFC00 == 0xDC00 {
324
0
                            return yaml_parser_set_reader_error(
325
0
                                parser,
326
0
                                b"unexpected low surrogate area\0" as *const u8
327
0
                                    as *const libc::c_char,
328
0
                                (*parser).offset,
329
0
                                value as libc::c_int,
330
0
                            );
331
0
                        }
332
0
                        if value & 0xFC00 == 0xD800 {
333
0
                            width = 4;
334
0
                            if raw_unread < 4_u64 {
335
0
                                if (*parser).eof {
336
0
                                    return yaml_parser_set_reader_error(
337
0
                                        parser,
338
0
                                        b"incomplete UTF-16 surrogate pair\0" as *const u8
339
0
                                            as *const libc::c_char,
340
0
                                        (*parser).offset,
341
0
                                        -1,
342
0
                                    );
343
0
                                }
344
0
                                incomplete = true;
345
                            } else {
346
0
                                value2 = (*(*parser)
347
0
                                    .raw_buffer
348
0
                                    .pointer
349
0
                                    .wrapping_offset((low + 2) as isize)
350
0
                                    as libc::c_int
351
0
                                    + ((*(*parser)
352
0
                                        .raw_buffer
353
0
                                        .pointer
354
0
                                        .wrapping_offset((high + 2) as isize)
355
0
                                        as libc::c_int)
356
0
                                        << 8))
357
0
                                    as libc::c_uint;
358
0
                                if value2 & 0xFC00 != 0xDC00 {
359
0
                                    return yaml_parser_set_reader_error(
360
0
                                        parser,
361
0
                                        b"expected low surrogate area\0" as *const u8
362
0
                                            as *const libc::c_char,
363
0
                                        (*parser).offset.force_add(2_u64),
364
0
                                        value2 as libc::c_int,
365
0
                                    );
366
0
                                }
367
0
                                value = 0x10000_u32
368
0
                                    .force_add((value & 0x3FF) << 10)
369
0
                                    .force_add(value2 & 0x3FF);
370
                            }
371
0
                        } else {
372
0
                            width = 2;
373
0
                        }
374
                    }
375
                }
376
0
                _ => {}
377
            }
378
0
            if incomplete {
379
0
                break;
380
0
            }
381
0
            if !(value == 0x9
382
0
                || value == 0xA
383
0
                || value == 0xD
384
0
                || value >= 0x20 && value <= 0x7E
385
0
                || value == 0x85
386
0
                || value >= 0xA0 && value <= 0xD7FF
387
0
                || value >= 0xE000 && value <= 0xFFFD
388
0
                || value >= 0x10000 && value <= 0x10FFFF)
389
            {
390
0
                return yaml_parser_set_reader_error(
391
0
                    parser,
392
0
                    b"control characters are not allowed\0" as *const u8 as *const libc::c_char,
393
0
                    (*parser).offset,
394
0
                    value as libc::c_int,
395
0
                );
396
0
            }
397
0
            let fresh14 = addr_of_mut!((*parser).raw_buffer.pointer);
398
0
            *fresh14 = (*fresh14).wrapping_offset(width as isize);
399
0
            let fresh15 = addr_of_mut!((*parser).offset);
400
0
            *fresh15 = (*fresh15 as libc::c_ulong).force_add(width as libc::c_ulong) as size_t;
401
0
            if value <= 0x7F {
402
0
                let fresh16 = addr_of_mut!((*parser).buffer.last);
403
0
                let fresh17 = *fresh16;
404
0
                *fresh16 = (*fresh16).wrapping_offset(1);
405
0
                *fresh17 = value as yaml_char_t;
406
0
            } else if value <= 0x7FF {
407
0
                let fresh18 = addr_of_mut!((*parser).buffer.last);
408
0
                let fresh19 = *fresh18;
409
0
                *fresh18 = (*fresh18).wrapping_offset(1);
410
0
                *fresh19 = 0xC0_u32.force_add(value >> 6) as yaml_char_t;
411
0
                let fresh20 = addr_of_mut!((*parser).buffer.last);
412
0
                let fresh21 = *fresh20;
413
0
                *fresh20 = (*fresh20).wrapping_offset(1);
414
0
                *fresh21 = 0x80_u32.force_add(value & 0x3F) as yaml_char_t;
415
0
            } else if value <= 0xFFFF {
416
0
                let fresh22 = addr_of_mut!((*parser).buffer.last);
417
0
                let fresh23 = *fresh22;
418
0
                *fresh22 = (*fresh22).wrapping_offset(1);
419
0
                *fresh23 = 0xE0_u32.force_add(value >> 12) as yaml_char_t;
420
0
                let fresh24 = addr_of_mut!((*parser).buffer.last);
421
0
                let fresh25 = *fresh24;
422
0
                *fresh24 = (*fresh24).wrapping_offset(1);
423
0
                *fresh25 = 0x80_u32.force_add(value >> 6 & 0x3F) as yaml_char_t;
424
0
                let fresh26 = addr_of_mut!((*parser).buffer.last);
425
0
                let fresh27 = *fresh26;
426
0
                *fresh26 = (*fresh26).wrapping_offset(1);
427
0
                *fresh27 = 0x80_u32.force_add(value & 0x3F) as yaml_char_t;
428
0
            } else {
429
0
                let fresh28 = addr_of_mut!((*parser).buffer.last);
430
0
                let fresh29 = *fresh28;
431
0
                *fresh28 = (*fresh28).wrapping_offset(1);
432
0
                *fresh29 = 0xF0_u32.force_add(value >> 18) as yaml_char_t;
433
0
                let fresh30 = addr_of_mut!((*parser).buffer.last);
434
0
                let fresh31 = *fresh30;
435
0
                *fresh30 = (*fresh30).wrapping_offset(1);
436
0
                *fresh31 = 0x80_u32.force_add(value >> 12 & 0x3F) as yaml_char_t;
437
0
                let fresh32 = addr_of_mut!((*parser).buffer.last);
438
0
                let fresh33 = *fresh32;
439
0
                *fresh32 = (*fresh32).wrapping_offset(1);
440
0
                *fresh33 = 0x80_u32.force_add(value >> 6 & 0x3F) as yaml_char_t;
441
0
                let fresh34 = addr_of_mut!((*parser).buffer.last);
442
0
                let fresh35 = *fresh34;
443
0
                *fresh34 = (*fresh34).wrapping_offset(1);
444
0
                *fresh35 = 0x80_u32.force_add(value & 0x3F) as yaml_char_t;
445
0
            }
446
0
            let fresh36 = addr_of_mut!((*parser).unread);
447
0
            *fresh36 = (*fresh36).force_add(1);
448
        }
449
0
        if (*parser).eof {
450
0
            let fresh37 = addr_of_mut!((*parser).buffer.last);
451
0
            let fresh38 = *fresh37;
452
0
            *fresh37 = (*fresh37).wrapping_offset(1);
453
0
            *fresh38 = b'\0';
454
0
            let fresh39 = addr_of_mut!((*parser).unread);
455
0
            *fresh39 = (*fresh39).force_add(1);
456
0
            return OK;
457
0
        }
458
    }
459
0
    if (*parser).offset >= (!0_u64).wrapping_div(2_u64) {
460
0
        return yaml_parser_set_reader_error(
461
0
            parser,
462
0
            b"input is too long\0" as *const u8 as *const libc::c_char,
463
0
            (*parser).offset,
464
0
            -1,
465
0
        );
466
0
    }
467
0
    OK
468
0
}