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
use std::{
    convert::TryFrom,
    fmt, ptr, result, slice,
    str::{self, FromStr, Utf8Error},
};

use ffi;
use libc::c_int;

use crate::{
    error::return_err,
    mpi::{integer::Format as IntegerFormat, Integer},
    Error, NonNull, Result,
};

#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub enum Format {
    Default = ffi::GCRYSEXP_FMT_DEFAULT as isize,
    Canonical = ffi::GCRYSEXP_FMT_CANON as isize,
    Base64 = ffi::GCRYSEXP_FMT_BASE64 as isize,
    Advanced = ffi::GCRYSEXP_FMT_ADVANCED as isize,
}

pub struct SExpression(NonNull<ffi::gcry_sexp_t>);

impl Drop for SExpression {
    #[inline]
    fn drop(&mut self) {
        unsafe {
            ffi::gcry_sexp_release(self.as_raw());
        }
    }
}

impl SExpression {
    impl_wrapper!(SExpression: ffi::gcry_sexp_t);

    #[inline]
    pub fn from_bytes(bytes: impl AsRef<[u8]>) -> Result<SExpression> {
        let _ = crate::init_default();
        let bytes = bytes.as_ref();
        unsafe {
            let mut result: ffi::gcry_sexp_t = ptr::null_mut();
            return_err!(ffi::gcry_sexp_sscan(
                &mut result,
                ptr::null_mut(),
                bytes.as_ptr().cast(),
                bytes.len()
            ));
            Ok(SExpression::from_raw(result))
        }
    }

    #[inline]
    pub fn to_bytes(&self, format: Format) -> Vec<u8> {
        let mut buffer = vec![0; self.len_encoded(format)];
        self.encode(format, &mut buffer);
        buffer.pop();
        buffer
    }

    #[inline]
    pub fn len_encoded(&self, format: Format) -> usize {
        unsafe { ffi::gcry_sexp_sprint(self.as_raw(), format as c_int, ptr::null_mut(), 0) }
    }

    #[inline]
    pub fn encode(&self, format: Format, buf: &mut [u8]) -> Option<usize> {
        unsafe {
            match ffi::gcry_sexp_sprint(
                self.as_raw(),
                format as c_int,
                buf.as_mut_ptr().cast(),
                buf.len(),
            ) {
                0 => None,
                x => Some(x),
            }
        }
    }

    #[inline]
    pub fn elements(&self) -> Elements<'_> {
        unsafe {
            Elements {
                sexp: self,
                first: 0,
                last: ffi::gcry_sexp_length(self.as_raw()),
            }
        }
    }

    #[inline]
    pub fn head(&self) -> Option<SExpression> {
        unsafe {
            ffi::gcry_sexp_car(self.as_raw())
                .as_mut()
                .map(|x| SExpression::from_raw(x))
        }
    }

    #[inline]
    pub fn tail(&self) -> Option<SExpression> {
        unsafe {
            ffi::gcry_sexp_cdr(self.as_raw())
                .as_mut()
                .map(|x| SExpression::from_raw(x))
        }
    }

    #[inline]
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    #[inline]
    pub fn len(&self) -> usize {
        unsafe { ffi::gcry_sexp_length(self.as_raw()) as usize }
    }

    #[inline]
    pub fn find_token(&self, token: impl AsRef<[u8]>) -> Option<SExpression> {
        let token = token.as_ref();
        unsafe {
            ffi::gcry_sexp_find_token(self.as_raw(), token.as_ptr().cast(), token.len())
                .as_mut()
                .map(|x| SExpression::from_raw(x))
        }
    }

    #[inline]
    pub fn get(&self, idx: u32) -> Option<SExpression> {
        unsafe {
            ffi::gcry_sexp_nth(self.as_raw(), idx as c_int)
                .as_mut()
                .map(|x| SExpression::from_raw(x))
        }
    }

    #[inline]
    pub fn get_bytes(&self, idx: u32) -> Option<&[u8]> {
        unsafe {
            let mut len = 0;
            ffi::gcry_sexp_nth_data(self.as_raw(), idx as c_int, &mut len)
                .as_ref()
                .map(|x| slice::from_raw_parts(x as *const _ as *const _, len))
        }
    }

    #[inline]
    pub fn get_str(&self, idx: u32) -> result::Result<&str, Option<Utf8Error>> {
        self.get_bytes(idx)
            .map_or(Err(None), |s| str::from_utf8(s).map_err(Some))
    }

    #[inline]
    pub fn get_integer(&self, idx: u32, fmt: IntegerFormat) -> Option<Integer> {
        unsafe {
            ffi::gcry_sexp_nth_mpi(self.as_raw(), idx as c_int, fmt as c_int)
                .as_mut()
                .map(|x| Integer::from_raw(x))
        }
    }
}

impl TryFrom<&[u8]> for SExpression {
    type Error = Error;

    #[inline]
    fn try_from(b: &[u8]) -> Result<Self> {
        SExpression::from_bytes(b)
    }
}

impl FromStr for SExpression {
    type Err = Error;

    #[inline]
    fn from_str(s: &str) -> Result<Self> {
        SExpression::from_bytes(s)
    }
}

impl fmt::Debug for SExpression {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        use std::{ascii, fmt::Write};

        f.write_str("SExpression(\"")?;
        for b in self
            .to_bytes(Format::Advanced)
            .into_iter()
            .flat_map(|b| ascii::escape_default(b))
        {
            f.write_char(b as char)?;
        }
        f.write_str("\")")
    }
}

impl<'a> IntoIterator for &'a SExpression {
    type Item = SExpression;
    type IntoIter = Elements<'a>;

    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        self.elements()
    }
}

#[derive(Debug)]
pub struct Elements<'a> {
    sexp: &'a SExpression,
    first: c_int,
    last: c_int,
}

impl Iterator for Elements<'_> {
    type Item = SExpression;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        if self.first < self.last {
            let elem = unsafe {
                SExpression::from_raw(ffi::gcry_sexp_nth(self.sexp.as_raw(), self.first))
            };
            self.first += 1;
            Some(elem)
        } else {
            None
        }
    }

    #[inline]
    fn nth(&mut self, n: usize) -> Option<Self::Item> {
        self.first = if (n as u64) < ((self.last - self.first) as u64) {
            self.first + (n as c_int)
        } else {
            self.last
        };
        self.next()
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        let size = (self.last - self.first) as usize;
        (size, Some(size))
    }
}

impl DoubleEndedIterator for Elements<'_> {
    #[inline]
    fn next_back(&mut self) -> Option<Self::Item> {
        if self.first < self.last {
            self.last -= 1;
            unsafe {
                Some(SExpression::from_raw(ffi::gcry_sexp_nth(
                    self.sexp.as_raw(),
                    self.last,
                )))
            }
        } else {
            None
        }
    }
}

impl ExactSizeIterator for Elements<'_> {}
impl ::std::iter::FusedIterator for Elements<'_> {}