unsafe_libyaml/
macros.rs

1macro_rules! BUFFER_INIT {
2    ($buffer:expr, $size:expr) => {{
3        let start = addr_of_mut!($buffer.start);
4        *start = yaml_malloc($size as size_t) as *mut yaml_char_t;
5        let pointer = addr_of_mut!($buffer.pointer);
6        *pointer = $buffer.start;
7        let last = addr_of_mut!($buffer.last);
8        *last = *pointer;
9        let end = addr_of_mut!($buffer.end);
10        *end = $buffer.start.wrapping_add($size as usize);
11    }};
12}
13
14macro_rules! BUFFER_DEL {
15    ($buffer:expr) => {{
16        yaml_free($buffer.start as *mut libc::c_void);
17        let end = addr_of_mut!($buffer.end);
18        *end = ptr::null_mut::<yaml_char_t>();
19        let pointer = addr_of_mut!($buffer.pointer);
20        *pointer = *end;
21        let start = addr_of_mut!($buffer.start);
22        *start = *pointer;
23    }};
24}
25
26macro_rules! STRING_ASSIGN {
27    ($string:expr, $length:expr) => {
28        yaml_string_t {
29            start: $string,
30            end: $string.wrapping_offset($length as isize),
31            pointer: $string,
32        }
33    };
34}
35
36macro_rules! STRING_INIT {
37    ($string:expr) => {{
38        $string.start = yaml_malloc(16) as *mut yaml_char_t;
39        $string.pointer = $string.start;
40        $string.end = $string.start.wrapping_add(16);
41        memset($string.start as *mut libc::c_void, 0, 16);
42    }};
43}
44
45macro_rules! STRING_DEL {
46    ($string:expr) => {{
47        yaml_free($string.start as *mut libc::c_void);
48        $string.end = ptr::null_mut::<yaml_char_t>();
49        $string.pointer = $string.end;
50        $string.start = $string.pointer;
51    }};
52}
53
54macro_rules! STRING_EXTEND {
55    ($string:expr) => {
56        if $string.pointer.wrapping_add(5) >= $string.end {
57            yaml_string_extend(
58                addr_of_mut!($string.start),
59                addr_of_mut!($string.pointer),
60                addr_of_mut!($string.end),
61            );
62        }
63    };
64}
65
66macro_rules! CLEAR {
67    ($string:expr) => {{
68        $string.pointer = $string.start;
69        memset(
70            $string.start as *mut libc::c_void,
71            0,
72            $string.end.c_offset_from($string.start) as libc::c_ulong,
73        );
74    }};
75}
76
77macro_rules! JOIN {
78    ($string_a:expr, $string_b:expr) => {{
79        yaml_string_join(
80            addr_of_mut!($string_a.start),
81            addr_of_mut!($string_a.pointer),
82            addr_of_mut!($string_a.end),
83            addr_of_mut!($string_b.start),
84            addr_of_mut!($string_b.pointer),
85            addr_of_mut!($string_b.end),
86        );
87        $string_b.pointer = $string_b.start;
88    }};
89}
90
91macro_rules! CHECK_AT {
92    ($string:expr, $octet:expr, $offset:expr) => {
93        *$string.pointer.offset($offset as isize) == $octet
94    };
95}
96
97macro_rules! CHECK {
98    ($string:expr, $octet:expr) => {
99        *$string.pointer == $octet
100    };
101}
102
103macro_rules! IS_ALPHA {
104    ($string:expr) => {
105        *$string.pointer >= b'0' && *$string.pointer <= b'9'
106            || *$string.pointer >= b'A' && *$string.pointer <= b'Z'
107            || *$string.pointer >= b'a' && *$string.pointer <= b'z'
108            || *$string.pointer == b'_'
109            || *$string.pointer == b'-'
110    };
111}
112
113macro_rules! IS_DIGIT {
114    ($string:expr) => {
115        *$string.pointer >= b'0' && *$string.pointer <= b'9'
116    };
117}
118
119macro_rules! AS_DIGIT {
120    ($string:expr) => {
121        (*$string.pointer - b'0') as libc::c_int
122    };
123}
124
125macro_rules! IS_HEX_AT {
126    ($string:expr, $offset:expr) => {
127        *$string.pointer.wrapping_offset($offset) >= b'0'
128            && *$string.pointer.wrapping_offset($offset) <= b'9'
129            || *$string.pointer.wrapping_offset($offset) >= b'A'
130                && *$string.pointer.wrapping_offset($offset) <= b'F'
131            || *$string.pointer.wrapping_offset($offset) >= b'a'
132                && *$string.pointer.wrapping_offset($offset) <= b'f'
133    };
134}
135
136macro_rules! AS_HEX_AT {
137    ($string:expr, $offset:expr) => {
138        if *$string.pointer.wrapping_offset($offset) >= b'A'
139            && *$string.pointer.wrapping_offset($offset) <= b'F'
140        {
141            *$string.pointer.wrapping_offset($offset) - b'A' + 10
142        } else if *$string.pointer.wrapping_offset($offset) >= b'a'
143            && *$string.pointer.wrapping_offset($offset) <= b'f'
144        {
145            *$string.pointer.wrapping_offset($offset) - b'a' + 10
146        } else {
147            *$string.pointer.wrapping_offset($offset) - b'0'
148        } as libc::c_int
149    };
150}
151
152macro_rules! IS_ASCII {
153    ($string:expr) => {
154        *$string.pointer <= b'\x7F'
155    };
156}
157
158macro_rules! IS_PRINTABLE {
159    ($string:expr) => {
160        match *$string.pointer {
161            // ASCII
162            0x0A | 0x20..=0x7E => true,
163            // U+A0 ... U+BF
164            0xC2 => match *$string.pointer.wrapping_offset(1) {
165                0xA0..=0xBF => true,
166                _ => false,
167            },
168            // U+C0 ... U+CFFF
169            0xC3..=0xEC => true,
170            // U+D000 ... U+D7FF
171            0xED => match *$string.pointer.wrapping_offset(1) {
172                0x00..=0x9F => true,
173                _ => false,
174            },
175            // U+E000 ... U+EFFF
176            0xEE => true,
177            // U+F000 ... U+FFFD
178            0xEF => match *$string.pointer.wrapping_offset(1) {
179                0xBB => match *$string.pointer.wrapping_offset(2) {
180                    // except U+FEFF
181                    0xBF => false,
182                    _ => true,
183                },
184                0xBF => match *$string.pointer.wrapping_offset(2) {
185                    0xBE | 0xBF => false,
186                    _ => true,
187                },
188                _ => true,
189            },
190            // U+10000 ... U+10FFFF
191            0xF0..=0xF4 => true,
192            _ => false,
193        }
194    };
195}
196
197macro_rules! IS_Z_AT {
198    ($string:expr, $offset:expr) => {
199        CHECK_AT!($string, b'\0', $offset)
200    };
201}
202
203macro_rules! IS_Z {
204    ($string:expr) => {
205        IS_Z_AT!($string, 0)
206    };
207}
208
209macro_rules! IS_BOM {
210    ($string:expr) => {
211        CHECK_AT!($string, b'\xEF', 0)
212            && CHECK_AT!($string, b'\xBB', 1)
213            && CHECK_AT!($string, b'\xBF', 2)
214    };
215}
216
217macro_rules! IS_SPACE_AT {
218    ($string:expr, $offset:expr) => {
219        CHECK_AT!($string, b' ', $offset)
220    };
221}
222
223macro_rules! IS_SPACE {
224    ($string:expr) => {
225        IS_SPACE_AT!($string, 0)
226    };
227}
228
229macro_rules! IS_TAB_AT {
230    ($string:expr, $offset:expr) => {
231        CHECK_AT!($string, b'\t', $offset)
232    };
233}
234
235macro_rules! IS_TAB {
236    ($string:expr) => {
237        IS_TAB_AT!($string, 0)
238    };
239}
240
241macro_rules! IS_BLANK_AT {
242    ($string:expr, $offset:expr) => {
243        IS_SPACE_AT!($string, $offset) || IS_TAB_AT!($string, $offset)
244    };
245}
246
247macro_rules! IS_BLANK {
248    ($string:expr) => {
249        IS_BLANK_AT!($string, 0)
250    };
251}
252
253macro_rules! IS_BREAK_AT {
254    ($string:expr, $offset:expr) => {
255        CHECK_AT!($string, b'\r', $offset)
256            || CHECK_AT!($string, b'\n', $offset)
257            || CHECK_AT!($string, b'\xC2', $offset) && CHECK_AT!($string, b'\x85', $offset + 1)
258            || CHECK_AT!($string, b'\xE2', $offset)
259                && CHECK_AT!($string, b'\x80', $offset + 1)
260                && CHECK_AT!($string, b'\xA8', $offset + 2)
261            || CHECK_AT!($string, b'\xE2', $offset)
262                && CHECK_AT!($string, b'\x80', $offset + 1)
263                && CHECK_AT!($string, b'\xA9', $offset + 2)
264    };
265}
266
267macro_rules! IS_BREAK {
268    ($string:expr) => {
269        IS_BREAK_AT!($string, 0)
270    };
271}
272
273macro_rules! IS_CRLF {
274    ($string:expr) => {
275        CHECK_AT!($string, b'\r', 0) && CHECK_AT!($string, b'\n', 1)
276    };
277}
278
279macro_rules! IS_BREAKZ_AT {
280    ($string:expr, $offset:expr) => {
281        IS_BREAK_AT!($string, $offset) || IS_Z_AT!($string, $offset)
282    };
283}
284
285macro_rules! IS_BREAKZ {
286    ($string:expr) => {
287        IS_BREAKZ_AT!($string, 0)
288    };
289}
290
291macro_rules! IS_BLANKZ_AT {
292    ($string:expr, $offset:expr) => {
293        IS_BLANK_AT!($string, $offset) || IS_BREAKZ_AT!($string, $offset)
294    };
295}
296
297macro_rules! IS_BLANKZ {
298    ($string:expr) => {
299        IS_BLANKZ_AT!($string, 0)
300    };
301}
302
303macro_rules! WIDTH_AT {
304    ($string:expr, $offset:expr) => {
305        if *$string.pointer.wrapping_offset($offset as isize) & 0x80 == 0x00 {
306            1
307        } else if *$string.pointer.wrapping_offset($offset as isize) & 0xE0 == 0xC0 {
308            2
309        } else if *$string.pointer.wrapping_offset($offset as isize) & 0xF0 == 0xE0 {
310            3
311        } else if *$string.pointer.wrapping_offset($offset as isize) & 0xF8 == 0xF0 {
312            4
313        } else {
314            0
315        }
316    };
317}
318
319macro_rules! WIDTH {
320    ($string:expr) => {
321        WIDTH_AT!($string, 0)
322    };
323}
324
325macro_rules! MOVE {
326    ($string:expr) => {
327        $string.pointer = $string.pointer.wrapping_offset(WIDTH!($string) as isize)
328    };
329}
330
331macro_rules! COPY {
332    ($string_a:expr, $string_b:expr) => {
333        if *$string_b.pointer & 0x80 == 0x00 {
334            *$string_a.pointer = *$string_b.pointer;
335            $string_a.pointer = $string_a.pointer.wrapping_offset(1);
336            $string_b.pointer = $string_b.pointer.wrapping_offset(1);
337        } else if *$string_b.pointer & 0xE0 == 0xC0 {
338            *$string_a.pointer = *$string_b.pointer;
339            $string_a.pointer = $string_a.pointer.wrapping_offset(1);
340            $string_b.pointer = $string_b.pointer.wrapping_offset(1);
341            *$string_a.pointer = *$string_b.pointer;
342            $string_a.pointer = $string_a.pointer.wrapping_offset(1);
343            $string_b.pointer = $string_b.pointer.wrapping_offset(1);
344        } else if *$string_b.pointer & 0xF0 == 0xE0 {
345            *$string_a.pointer = *$string_b.pointer;
346            $string_a.pointer = $string_a.pointer.wrapping_offset(1);
347            $string_b.pointer = $string_b.pointer.wrapping_offset(1);
348            *$string_a.pointer = *$string_b.pointer;
349            $string_a.pointer = $string_a.pointer.wrapping_offset(1);
350            $string_b.pointer = $string_b.pointer.wrapping_offset(1);
351            *$string_a.pointer = *$string_b.pointer;
352            $string_a.pointer = $string_a.pointer.wrapping_offset(1);
353            $string_b.pointer = $string_b.pointer.wrapping_offset(1);
354        } else if *$string_b.pointer & 0xF8 == 0xF0 {
355            *$string_a.pointer = *$string_b.pointer;
356            $string_a.pointer = $string_a.pointer.wrapping_offset(1);
357            $string_b.pointer = $string_b.pointer.wrapping_offset(1);
358            *$string_a.pointer = *$string_b.pointer;
359            $string_a.pointer = $string_a.pointer.wrapping_offset(1);
360            $string_b.pointer = $string_b.pointer.wrapping_offset(1);
361            *$string_a.pointer = *$string_b.pointer;
362            $string_a.pointer = $string_a.pointer.wrapping_offset(1);
363            $string_b.pointer = $string_b.pointer.wrapping_offset(1);
364            *$string_a.pointer = *$string_b.pointer;
365            $string_a.pointer = $string_a.pointer.wrapping_offset(1);
366            $string_b.pointer = $string_b.pointer.wrapping_offset(1);
367        }
368    };
369}
370
371macro_rules! STACK_INIT {
372    ($stack:expr, $type:ty) => {{
373        $stack.start = yaml_malloc(16 * size_of::<$type>() as libc::c_ulong) as *mut $type;
374        $stack.top = $stack.start;
375        $stack.end = $stack.start.offset(16_isize);
376    }};
377}
378
379macro_rules! STACK_DEL {
380    ($stack:expr) => {
381        yaml_free($stack.start as *mut libc::c_void);
382        $stack.end = ptr::null_mut();
383        $stack.top = ptr::null_mut();
384        $stack.start = ptr::null_mut();
385    };
386}
387
388macro_rules! STACK_EMPTY {
389    ($stack:expr) => {
390        $stack.start == $stack.top
391    };
392}
393
394macro_rules! STACK_LIMIT {
395    ($context:expr, $stack:expr) => {
396        if $stack.top.c_offset_from($stack.start) < libc::c_int::MAX as isize - 1 {
397            OK
398        } else {
399            (*$context).error = YAML_MEMORY_ERROR;
400            FAIL
401        }
402    };
403}
404
405macro_rules! PUSH {
406    (do $stack:expr, $push:expr) => {{
407        if $stack.top == $stack.end {
408            yaml_stack_extend(
409                addr_of_mut!($stack.start) as *mut *mut libc::c_void,
410                addr_of_mut!($stack.top) as *mut *mut libc::c_void,
411                addr_of_mut!($stack.end) as *mut *mut libc::c_void,
412            );
413        }
414        $push;
415        $stack.top = $stack.top.wrapping_offset(1);
416    }};
417    ($stack:expr, *$value:expr) => {
418        PUSH!(do $stack, ptr::copy_nonoverlapping($value, $stack.top, 1))
419    };
420    ($stack:expr, $value:expr) => {
421        PUSH!(do $stack, ptr::write($stack.top, $value))
422    };
423}
424
425macro_rules! POP {
426    ($stack:expr) => {
427        *{
428            $stack.top = $stack.top.offset(-1);
429            $stack.top
430        }
431    };
432}
433
434macro_rules! QUEUE_INIT {
435    ($queue:expr, $type:ty) => {{
436        $queue.start = yaml_malloc(16 * size_of::<$type>() as libc::c_ulong) as *mut $type;
437        $queue.tail = $queue.start;
438        $queue.head = $queue.tail;
439        $queue.end = $queue.start.offset(16_isize);
440    }};
441}
442
443macro_rules! QUEUE_DEL {
444    ($queue:expr) => {
445        yaml_free($queue.start as *mut libc::c_void);
446        $queue.end = ptr::null_mut();
447        $queue.tail = ptr::null_mut();
448        $queue.head = ptr::null_mut();
449        $queue.start = ptr::null_mut();
450    };
451}
452
453macro_rules! QUEUE_EMPTY {
454    ($queue:expr) => {
455        $queue.head == $queue.tail
456    };
457}
458
459macro_rules! ENQUEUE {
460    (do $queue:expr, $enqueue:expr) => {{
461        if $queue.tail == $queue.end {
462            yaml_queue_extend(
463                addr_of_mut!($queue.start) as *mut *mut libc::c_void,
464                addr_of_mut!($queue.head) as *mut *mut libc::c_void,
465                addr_of_mut!($queue.tail) as *mut *mut libc::c_void,
466                addr_of_mut!($queue.end) as *mut *mut libc::c_void,
467            );
468        }
469        $enqueue;
470        $queue.tail = $queue.tail.wrapping_offset(1);
471    }};
472    ($queue:expr, *$value:expr) => {
473        ENQUEUE!(do $queue, ptr::copy_nonoverlapping($value, $queue.tail, 1))
474    };
475    ($queue:expr, $value:expr) => {
476        ENQUEUE!(do $queue, ptr::write($queue.tail, $value))
477    };
478}
479
480macro_rules! DEQUEUE {
481    ($queue:expr) => {
482        *{
483            let head = $queue.head;
484            $queue.head = $queue.head.wrapping_offset(1);
485            head
486        }
487    };
488}
489
490macro_rules! QUEUE_INSERT {
491    ($queue:expr, $index:expr, $value:expr) => {{
492        if $queue.tail == $queue.end {
493            yaml_queue_extend(
494                addr_of_mut!($queue.start) as *mut *mut libc::c_void,
495                addr_of_mut!($queue.head) as *mut *mut libc::c_void,
496                addr_of_mut!($queue.tail) as *mut *mut libc::c_void,
497                addr_of_mut!($queue.end) as *mut *mut libc::c_void,
498            );
499        }
500        memmove(
501            $queue
502                .head
503                .wrapping_offset($index as isize)
504                .wrapping_offset(1_isize) as *mut libc::c_void,
505            $queue.head.wrapping_offset($index as isize) as *const libc::c_void,
506            ($queue.tail.c_offset_from($queue.head) as libc::c_ulong)
507                .wrapping_sub($index)
508                .wrapping_mul(size_of::<yaml_token_t>() as libc::c_ulong),
509        );
510        *$queue.head.wrapping_offset($index as isize) = $value;
511        let fresh14 = addr_of_mut!($queue.tail);
512        *fresh14 = (*fresh14).wrapping_offset(1);
513    }};
514}