redrust/
resp_parser.rs

1extern crate c2rust_bitfields;
2extern crate libc;
3extern crate core;
4extern "C" {
5    fn memcpy(
6        _: *mut libc::c_void,
7        _: *const libc::c_void,
8        _: libc::c_ulong,
9    ) -> *mut libc::c_void;
10    fn strtod(_: *const libc::c_char, _: *mut *mut libc::c_char) -> libc::c_double;
11    fn string2ll(
12        s: *const libc::c_char,
13        slen: size_t,
14        value: *mut libc::c_longlong,
15    ) -> libc::c_int;
16    fn strchr(_: *const libc::c_char, _: libc::c_int) -> *mut libc::c_char;
17}
18pub type size_t = libc::c_ulong;
19#[derive(Copy, Clone)]
20#[repr(C)]
21pub struct ReplyParser {
22    pub curr_location: *const libc::c_char,
23    pub callbacks: ReplyParserCallbacks,
24}
25#[derive(Copy, Clone)]
26#[repr(C)]
27pub struct ReplyParserCallbacks {
28    pub null_array_callback: Option::<
29        unsafe extern "C" fn(*mut libc::c_void, *const libc::c_char, size_t) -> (),
30    >,
31    pub null_bulk_string_callback: Option::<
32        unsafe extern "C" fn(*mut libc::c_void, *const libc::c_char, size_t) -> (),
33    >,
34    pub bulk_string_callback: Option::<
35        unsafe extern "C" fn(
36            *mut libc::c_void,
37            *const libc::c_char,
38            size_t,
39            *const libc::c_char,
40            size_t,
41        ) -> (),
42    >,
43    pub error_callback: Option::<
44        unsafe extern "C" fn(
45            *mut libc::c_void,
46            *const libc::c_char,
47            size_t,
48            *const libc::c_char,
49            size_t,
50        ) -> (),
51    >,
52    pub simple_str_callback: Option::<
53        unsafe extern "C" fn(
54            *mut libc::c_void,
55            *const libc::c_char,
56            size_t,
57            *const libc::c_char,
58            size_t,
59        ) -> (),
60    >,
61    pub long_callback: Option::<
62        unsafe extern "C" fn(
63            *mut libc::c_void,
64            libc::c_longlong,
65            *const libc::c_char,
66            size_t,
67        ) -> (),
68    >,
69    pub array_callback: Option::<
70        unsafe extern "C" fn(
71            *mut ReplyParser,
72            *mut libc::c_void,
73            size_t,
74            *const libc::c_char,
75        ) -> (),
76    >,
77    pub set_callback: Option::<
78        unsafe extern "C" fn(
79            *mut ReplyParser,
80            *mut libc::c_void,
81            size_t,
82            *const libc::c_char,
83        ) -> (),
84    >,
85    pub map_callback: Option::<
86        unsafe extern "C" fn(
87            *mut ReplyParser,
88            *mut libc::c_void,
89            size_t,
90            *const libc::c_char,
91        ) -> (),
92    >,
93    pub bool_callback: Option::<
94        unsafe extern "C" fn(
95            *mut libc::c_void,
96            libc::c_int,
97            *const libc::c_char,
98            size_t,
99        ) -> (),
100    >,
101    pub double_callback: Option::<
102        unsafe extern "C" fn(
103            *mut libc::c_void,
104            libc::c_double,
105            *const libc::c_char,
106            size_t,
107        ) -> (),
108    >,
109    pub big_number_callback: Option::<
110        unsafe extern "C" fn(
111            *mut libc::c_void,
112            *const libc::c_char,
113            size_t,
114            *const libc::c_char,
115            size_t,
116        ) -> (),
117    >,
118    pub verbatim_string_callback: Option::<
119        unsafe extern "C" fn(
120            *mut libc::c_void,
121            *const libc::c_char,
122            *const libc::c_char,
123            size_t,
124            *const libc::c_char,
125            size_t,
126        ) -> (),
127    >,
128    pub attribute_callback: Option::<
129        unsafe extern "C" fn(
130            *mut ReplyParser,
131            *mut libc::c_void,
132            size_t,
133            *const libc::c_char,
134        ) -> (),
135    >,
136    pub null_callback: Option::<
137        unsafe extern "C" fn(*mut libc::c_void, *const libc::c_char, size_t) -> (),
138    >,
139    pub error: Option::<unsafe extern "C" fn(*mut libc::c_void) -> ()>,
140}
141unsafe extern "C" fn parseBulk(
142    mut parser: *mut ReplyParser,
143    mut p_ctx: *mut libc::c_void,
144) -> libc::c_int {
145    let mut proto: *const libc::c_char = (*parser).curr_location;
146    let mut p: *mut libc::c_char = strchr(
147        proto.offset(1 as libc::c_int as isize),
148        '\r' as i32,
149    );
150    let mut bulklen: libc::c_longlong = 0;
151    (*parser).curr_location = p.offset(2 as libc::c_int as isize);
152    string2ll(
153        proto.offset(1 as libc::c_int as isize),
154        (p.offset_from(proto) as libc::c_long - 1 as libc::c_int as libc::c_long)
155            as size_t,
156        &mut bulklen,
157    );
158    if bulklen == -(1 as libc::c_int) as libc::c_longlong {
159        ((*parser).callbacks.null_bulk_string_callback)
160            .expect(
161                "non-null function pointer",
162            )(
163            p_ctx,
164            proto,
165            ((*parser).curr_location).offset_from(proto) as libc::c_long as size_t,
166        );
167    } else {
168        let mut str: *const libc::c_char = (*parser).curr_location;
169        (*parser).curr_location = ((*parser).curr_location).offset(bulklen as isize);
170        (*parser)
171            .curr_location = ((*parser).curr_location).offset(2 as libc::c_int as isize);
172        ((*parser).callbacks.bulk_string_callback)
173            .expect(
174                "non-null function pointer",
175            )(
176            p_ctx,
177            str,
178            bulklen as size_t,
179            proto,
180            ((*parser).curr_location).offset_from(proto) as libc::c_long as size_t,
181        );
182    }
183    return 0 as libc::c_int;
184}
185unsafe extern "C" fn parseSimpleString(
186    mut parser: *mut ReplyParser,
187    mut p_ctx: *mut libc::c_void,
188) -> libc::c_int {
189    let mut proto: *const libc::c_char = (*parser).curr_location;
190    let mut p: *mut libc::c_char = strchr(
191        proto.offset(1 as libc::c_int as isize),
192        '\r' as i32,
193    );
194    (*parser).curr_location = p.offset(2 as libc::c_int as isize);
195    ((*parser).callbacks.simple_str_callback)
196        .expect(
197            "non-null function pointer",
198        )(
199        p_ctx,
200        proto.offset(1 as libc::c_int as isize),
201        (p.offset_from(proto) as libc::c_long - 1 as libc::c_int as libc::c_long)
202            as size_t,
203        proto,
204        ((*parser).curr_location).offset_from(proto) as libc::c_long as size_t,
205    );
206    return 0 as libc::c_int;
207}
208unsafe extern "C" fn parseError(
209    mut parser: *mut ReplyParser,
210    mut p_ctx: *mut libc::c_void,
211) -> libc::c_int {
212    let mut proto: *const libc::c_char = (*parser).curr_location;
213    let mut p: *mut libc::c_char = strchr(
214        proto.offset(1 as libc::c_int as isize),
215        '\r' as i32,
216    );
217    (*parser).curr_location = p.offset(2 as libc::c_int as isize);
218    ((*parser).callbacks.error_callback)
219        .expect(
220            "non-null function pointer",
221        )(
222        p_ctx,
223        proto.offset(1 as libc::c_int as isize),
224        (p.offset_from(proto) as libc::c_long - 1 as libc::c_int as libc::c_long)
225            as size_t,
226        proto,
227        ((*parser).curr_location).offset_from(proto) as libc::c_long as size_t,
228    );
229    return 0 as libc::c_int;
230}
231unsafe extern "C" fn parseLong(
232    mut parser: *mut ReplyParser,
233    mut p_ctx: *mut libc::c_void,
234) -> libc::c_int {
235    let mut proto: *const libc::c_char = (*parser).curr_location;
236    let mut p: *mut libc::c_char = strchr(
237        proto.offset(1 as libc::c_int as isize),
238        '\r' as i32,
239    );
240    (*parser).curr_location = p.offset(2 as libc::c_int as isize);
241    let mut val: libc::c_longlong = 0;
242    string2ll(
243        proto.offset(1 as libc::c_int as isize),
244        (p.offset_from(proto) as libc::c_long - 1 as libc::c_int as libc::c_long)
245            as size_t,
246        &mut val,
247    );
248    ((*parser).callbacks.long_callback)
249        .expect(
250            "non-null function pointer",
251        )(
252        p_ctx,
253        val,
254        proto,
255        ((*parser).curr_location).offset_from(proto) as libc::c_long as size_t,
256    );
257    return 0 as libc::c_int;
258}
259unsafe extern "C" fn parseAttributes(
260    mut parser: *mut ReplyParser,
261    mut p_ctx: *mut libc::c_void,
262) -> libc::c_int {
263    let mut proto: *const libc::c_char = (*parser).curr_location;
264    let mut p: *mut libc::c_char = strchr(
265        proto.offset(1 as libc::c_int as isize),
266        '\r' as i32,
267    );
268    let mut len: libc::c_longlong = 0;
269    string2ll(
270        proto.offset(1 as libc::c_int as isize),
271        (p.offset_from(proto) as libc::c_long - 1 as libc::c_int as libc::c_long)
272            as size_t,
273        &mut len,
274    );
275    p = p.offset(2 as libc::c_int as isize);
276    (*parser).curr_location = p;
277    ((*parser).callbacks.attribute_callback)
278        .expect("non-null function pointer")(parser, p_ctx, len as size_t, proto);
279    return 0 as libc::c_int;
280}
281unsafe extern "C" fn parseVerbatimString(
282    mut parser: *mut ReplyParser,
283    mut p_ctx: *mut libc::c_void,
284) -> libc::c_int {
285    let mut proto: *const libc::c_char = (*parser).curr_location;
286    let mut p: *mut libc::c_char = strchr(
287        proto.offset(1 as libc::c_int as isize),
288        '\r' as i32,
289    );
290    let mut bulklen: libc::c_longlong = 0;
291    (*parser).curr_location = p.offset(2 as libc::c_int as isize);
292    string2ll(
293        proto.offset(1 as libc::c_int as isize),
294        (p.offset_from(proto) as libc::c_long - 1 as libc::c_int as libc::c_long)
295            as size_t,
296        &mut bulklen,
297    );
298    let mut format: *const libc::c_char = (*parser).curr_location;
299    (*parser).curr_location = ((*parser).curr_location).offset(bulklen as isize);
300    (*parser)
301        .curr_location = ((*parser).curr_location).offset(2 as libc::c_int as isize);
302    ((*parser).callbacks.verbatim_string_callback)
303        .expect(
304            "non-null function pointer",
305        )(
306        p_ctx,
307        format,
308        format.offset(4 as libc::c_int as isize),
309        (bulklen - 4 as libc::c_int as libc::c_longlong) as size_t,
310        proto,
311        ((*parser).curr_location).offset_from(proto) as libc::c_long as size_t,
312    );
313    return 0 as libc::c_int;
314}
315unsafe extern "C" fn parseBigNumber(
316    mut parser: *mut ReplyParser,
317    mut p_ctx: *mut libc::c_void,
318) -> libc::c_int {
319    let mut proto: *const libc::c_char = (*parser).curr_location;
320    let mut p: *mut libc::c_char = strchr(
321        proto.offset(1 as libc::c_int as isize),
322        '\r' as i32,
323    );
324    (*parser).curr_location = p.offset(2 as libc::c_int as isize);
325    ((*parser).callbacks.big_number_callback)
326        .expect(
327            "non-null function pointer",
328        )(
329        p_ctx,
330        proto.offset(1 as libc::c_int as isize),
331        (p.offset_from(proto) as libc::c_long - 1 as libc::c_int as libc::c_long)
332            as size_t,
333        proto,
334        ((*parser).curr_location).offset_from(proto) as libc::c_long as size_t,
335    );
336    return 0 as libc::c_int;
337}
338unsafe extern "C" fn parseNull(
339    mut parser: *mut ReplyParser,
340    mut p_ctx: *mut libc::c_void,
341) -> libc::c_int {
342    let mut proto: *const libc::c_char = (*parser).curr_location;
343    let mut p: *mut libc::c_char = strchr(
344        proto.offset(1 as libc::c_int as isize),
345        '\r' as i32,
346    );
347    (*parser).curr_location = p.offset(2 as libc::c_int as isize);
348    ((*parser).callbacks.null_callback)
349        .expect(
350            "non-null function pointer",
351        )(
352        p_ctx,
353        proto,
354        ((*parser).curr_location).offset_from(proto) as libc::c_long as size_t,
355    );
356    return 0 as libc::c_int;
357}
358unsafe extern "C" fn parseDouble(
359    mut parser: *mut ReplyParser,
360    mut p_ctx: *mut libc::c_void,
361) -> libc::c_int {
362    let mut proto: *const libc::c_char = (*parser).curr_location;
363    let mut p: *mut libc::c_char = strchr(
364        proto.offset(1 as libc::c_int as isize),
365        '\r' as i32,
366    );
367    (*parser).curr_location = p.offset(2 as libc::c_int as isize);
368    let mut buf: [libc::c_char; 5121] = [0; 5121];
369    let mut len: size_t = (p.offset_from(proto) as libc::c_long
370        - 1 as libc::c_int as libc::c_long) as size_t;
371    let mut d: libc::c_double = 0.;
372    if len <= (5 as libc::c_int * 1024 as libc::c_int) as libc::c_ulong {
373        memcpy(
374            buf.as_mut_ptr() as *mut libc::c_void,
375            proto.offset(1 as libc::c_int as isize) as *const libc::c_void,
376            len,
377        );
378        buf[len as usize] = '\0' as i32 as libc::c_char;
379        d = strtod(buf.as_mut_ptr(), 0 as *mut *mut libc::c_char);
380    } else {
381        d = 0 as libc::c_int as libc::c_double;
382    }
383    ((*parser).callbacks.double_callback)
384        .expect(
385            "non-null function pointer",
386        )(
387        p_ctx,
388        d,
389        proto,
390        ((*parser).curr_location).offset_from(proto) as libc::c_long as size_t,
391    );
392    return 0 as libc::c_int;
393}
394unsafe extern "C" fn parseBool(
395    mut parser: *mut ReplyParser,
396    mut p_ctx: *mut libc::c_void,
397) -> libc::c_int {
398    let mut proto: *const libc::c_char = (*parser).curr_location;
399    let mut p: *mut libc::c_char = strchr(
400        proto.offset(1 as libc::c_int as isize),
401        '\r' as i32,
402    );
403    (*parser).curr_location = p.offset(2 as libc::c_int as isize);
404    ((*parser).callbacks.bool_callback)
405        .expect(
406            "non-null function pointer",
407        )(
408        p_ctx,
409        (*proto.offset(1 as libc::c_int as isize) as libc::c_int == 't' as i32)
410            as libc::c_int,
411        proto,
412        ((*parser).curr_location).offset_from(proto) as libc::c_long as size_t,
413    );
414    return 0 as libc::c_int;
415}
416unsafe extern "C" fn parseArray(
417    mut parser: *mut ReplyParser,
418    mut p_ctx: *mut libc::c_void,
419) -> libc::c_int {
420    let mut proto: *const libc::c_char = (*parser).curr_location;
421    let mut p: *mut libc::c_char = strchr(
422        proto.offset(1 as libc::c_int as isize),
423        '\r' as i32,
424    );
425    let mut len: libc::c_longlong = 0;
426    string2ll(
427        proto.offset(1 as libc::c_int as isize),
428        (p.offset_from(proto) as libc::c_long - 1 as libc::c_int as libc::c_long)
429            as size_t,
430        &mut len,
431    );
432    p = p.offset(2 as libc::c_int as isize);
433    (*parser).curr_location = p;
434    if len == -(1 as libc::c_int) as libc::c_longlong {
435        ((*parser).callbacks.null_array_callback)
436            .expect(
437                "non-null function pointer",
438            )(
439            p_ctx,
440            proto,
441            ((*parser).curr_location).offset_from(proto) as libc::c_long as size_t,
442        );
443    } else {
444        ((*parser).callbacks.array_callback)
445            .expect("non-null function pointer")(parser, p_ctx, len as size_t, proto);
446    }
447    return 0 as libc::c_int;
448}
449unsafe extern "C" fn parseSet(
450    mut parser: *mut ReplyParser,
451    mut p_ctx: *mut libc::c_void,
452) -> libc::c_int {
453    let mut proto: *const libc::c_char = (*parser).curr_location;
454    let mut p: *mut libc::c_char = strchr(
455        proto.offset(1 as libc::c_int as isize),
456        '\r' as i32,
457    );
458    let mut len: libc::c_longlong = 0;
459    string2ll(
460        proto.offset(1 as libc::c_int as isize),
461        (p.offset_from(proto) as libc::c_long - 1 as libc::c_int as libc::c_long)
462            as size_t,
463        &mut len,
464    );
465    p = p.offset(2 as libc::c_int as isize);
466    (*parser).curr_location = p;
467    ((*parser).callbacks.set_callback)
468        .expect("non-null function pointer")(parser, p_ctx, len as size_t, proto);
469    return 0 as libc::c_int;
470}
471unsafe extern "C" fn parseMap(
472    mut parser: *mut ReplyParser,
473    mut p_ctx: *mut libc::c_void,
474) -> libc::c_int {
475    let mut proto: *const libc::c_char = (*parser).curr_location;
476    let mut p: *mut libc::c_char = strchr(
477        proto.offset(1 as libc::c_int as isize),
478        '\r' as i32,
479    );
480    let mut len: libc::c_longlong = 0;
481    string2ll(
482        proto.offset(1 as libc::c_int as isize),
483        (p.offset_from(proto) as libc::c_long - 1 as libc::c_int as libc::c_long)
484            as size_t,
485        &mut len,
486    );
487    p = p.offset(2 as libc::c_int as isize);
488    (*parser).curr_location = p;
489    ((*parser).callbacks.map_callback)
490        .expect("non-null function pointer")(parser, p_ctx, len as size_t, proto);
491    return 0 as libc::c_int;
492}
493#[no_mangle]
494pub unsafe extern "C" fn parseReply(
495    mut parser: *mut ReplyParser,
496    mut p_ctx: *mut libc::c_void,
497) -> libc::c_int {
498    match *((*parser).curr_location).offset(0 as libc::c_int as isize) as libc::c_int {
499        36 => return parseBulk(parser, p_ctx),
500        43 => return parseSimpleString(parser, p_ctx),
501        45 => return parseError(parser, p_ctx),
502        58 => return parseLong(parser, p_ctx),
503        42 => return parseArray(parser, p_ctx),
504        126 => return parseSet(parser, p_ctx),
505        37 => return parseMap(parser, p_ctx),
506        35 => return parseBool(parser, p_ctx),
507        44 => return parseDouble(parser, p_ctx),
508        95 => return parseNull(parser, p_ctx),
509        40 => return parseBigNumber(parser, p_ctx),
510        61 => return parseVerbatimString(parser, p_ctx),
511        124 => return parseAttributes(parser, p_ctx),
512        _ => {
513            if ((*parser).callbacks.error).is_some() {
514                ((*parser).callbacks.error).expect("non-null function pointer")(p_ctx);
515            }
516        }
517    }
518    return -(1 as libc::c_int);
519}