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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
extern crate byteorder;
#[macro_use]
extern crate failure;
#[macro_use]
extern crate quote;
#[macro_use]
extern crate syn;
#[macro_use]
extern crate proc_macro_hack;
extern crate proc_macro;

use std::io::{Error as IOError};
use std::str::FromStr;
use byteorder::{ByteOrder, WriteBytesExt, BE, LE};
use quote::ToTokens;
use proc_macro::TokenStream;
use syn::{Expr, IntSuffix, FloatSuffix, Lit, LitInt, LitFloat, UnOp};
use syn::buffer::TokenBuffer;
use syn::punctuated::Punctuated;
use syn::synom::ParseError;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Endianness {
    LE,
    BE,
}

#[cfg(not(feature = "default-big-endian"))]
const DEFAULT_ENDIANNESS: Endianness = Endianness::LE;

#[cfg(feature = "default-big-endian")]
const DEFAULT_ENDIANNESS: Endianness = Endianness::BE;

#[derive(Debug, Fail)]
enum Error {
    #[fail(display = "unsupported prefixed expression in the macro: {} [+] {}", _0, _1)]
    UnsupportedPrefixedExpression(String, String),
    #[fail(display = "unsupported expression in the macro: {}", _0)]
    UnsupportedExpression(String),
    #[fail(display = "unsupported literal in the macro: {}", _0)]
    UnsupportedLit(String),
    #[fail(display = "unsupported numeric suffix in the macro: {}", _0)]
    UnsupportedNumberSuffix(String),
    #[fail(display = "failed to parse the input as a comma-separated list: {}", _0)]
    InvalidInput(#[cause] ParseError),
    #[fail(display = "failed to parse endianness: {}", _0)]
    InvalidEndianness(String),
    #[fail(display = "failed to write a suffixed value: {}, negative: {}, given suffix: {}, requested suffix: {}", _0, _1, _2, _3)]
    IncompatibleNumberSuffix(String, bool, String, String),
    #[fail(display = "failed to write a value: {}", _0)]
    IO(#[cause] IOError),
}

impl From<ParseError> for Error {

    fn from(from: ParseError) -> Self {
        Error::InvalidInput(from)
    }
}

impl From<IOError> for Error {

    fn from(from: IOError) -> Self {
        Error::IO(from)
    }
}

impl Error {

    pub fn unsupported_expression(expr: Expr) -> Self {
        Error::UnsupportedExpression(expr.into_tokens().to_string())
    }

    pub fn unsupported_lit(lit: Lit) -> Self {
        Error::UnsupportedLit(lit.into_tokens().to_string())
    }

    pub fn unsupported_prefixed_expression(op: UnOp, expr: Expr) -> Self {
        Error::UnsupportedPrefixedExpression(op.into_tokens().to_string(), expr.into_tokens().to_string())
    }
}

fn int_to_suffix(negative: bool, int: &LitInt) -> Result<IntSuffix, Error> {
    let num_bits = int.value();
    let s = if negative {
        match () {
            () if num_bits > 0x80000000 => IntSuffix::I64,
            () if num_bits > 0x8000     => IntSuffix::I32,
            () if num_bits > 0x80       => IntSuffix::I16,
            () => IntSuffix::I8,
        }
    } else {
        match () {
            () if num_bits > 0xFFFFFFFF => IntSuffix::U64,
            () if num_bits > 0xFFFF     => IntSuffix::U32,
            () if num_bits > 0xFF       => IntSuffix::U16,
            () => IntSuffix::U8,
        }
    };
    let s = match (s, int.suffix()) {
        // If none is specified use the least size suffix possible.
        (s, IntSuffix::None) => s,
        // Allowed casts Uint -> Uint.
        (IntSuffix::U8 , IntSuffix::U8 ) => IntSuffix::U8 ,
        (IntSuffix::U8 , IntSuffix::U16) => IntSuffix::U16,
        (IntSuffix::U8 , IntSuffix::U32) => IntSuffix::U32,
        (IntSuffix::U8 , IntSuffix::U64) => IntSuffix::U64,
        (IntSuffix::U16, IntSuffix::U16) => IntSuffix::U16,
        (IntSuffix::U16, IntSuffix::U32) => IntSuffix::U32,
        (IntSuffix::U16, IntSuffix::U64) => IntSuffix::U64,
        (IntSuffix::U32, IntSuffix::U32) => IntSuffix::U32,
        (IntSuffix::U32, IntSuffix::U64) => IntSuffix::U64,
        (IntSuffix::U64, IntSuffix::U64) => IntSuffix::U64,
        // Allowed casts Sint -> Sint.
        (IntSuffix::I8 , IntSuffix::I8 ) => IntSuffix::I8 ,
        (IntSuffix::I8 , IntSuffix::I16) => IntSuffix::I16,
        (IntSuffix::I8 , IntSuffix::I32) => IntSuffix::I32,
        (IntSuffix::I8 , IntSuffix::I64) => IntSuffix::I64,
        (IntSuffix::I16, IntSuffix::I16) => IntSuffix::I16,
        (IntSuffix::I16, IntSuffix::I32) => IntSuffix::I32,
        (IntSuffix::I16, IntSuffix::I64) => IntSuffix::I64,
        (IntSuffix::I32, IntSuffix::I32) => IntSuffix::I32,
        (IntSuffix::I32, IntSuffix::I64) => IntSuffix::I64,
        (IntSuffix::I64, IntSuffix::I64) => IntSuffix::I64,
        // Allowed casts Uint -> Sint.
        (IntSuffix::U8 , IntSuffix::I8 ) if num_bits < 0x80               => IntSuffix::I8 ,
        (IntSuffix::U16, IntSuffix::I16) if num_bits < 0x8000             => IntSuffix::I16,
        (IntSuffix::U32, IntSuffix::I32) if num_bits < 0x80000000         => IntSuffix::I32,
        (IntSuffix::U64, IntSuffix::I64) if num_bits < 0x8000000000000000 => IntSuffix::I64,
        (IntSuffix::U8 , IntSuffix::I16) => IntSuffix::I16,
        (IntSuffix::U8 , IntSuffix::I32) => IntSuffix::I32,
        (IntSuffix::U8 , IntSuffix::I64) => IntSuffix::I64,
        (IntSuffix::U16, IntSuffix::I32) => IntSuffix::I32,
        (IntSuffix::U16, IntSuffix::I64) => IntSuffix::I64,
        (IntSuffix::U32, IntSuffix::I64) => IntSuffix::I64,
        // Everything else is either invalid or ambiguous.
        (given, requested) => {
            return Err(Error::IncompatibleNumberSuffix(
                int.into_tokens().to_string(),
                negative,
                format!("{:?}", given),
                format!("{:?}", requested),
            ));
        },
    };
    Ok(s)
}

fn bytify_implementation_int<O: ByteOrder>(negative: bool, int: LitInt, output: &mut Vec<u8>) -> Result<(), Error> {
    let num_bits = int.value();
    let num_bits_suffix = int_to_suffix(negative, &int)?;
    match num_bits_suffix {
        IntSuffix::U8 => {
            output.write_u8(num_bits as u8)?;
        },
        IntSuffix::I8 => {
            if negative {
                output.write_u8((!(num_bits as u8)).wrapping_add(1))?;
            } else {
                output.write_u8(   num_bits as u8)?;
            }
        },
        IntSuffix::U16 => {
            output.write_u16::<O>(num_bits as u16)?;
        },
        IntSuffix::I16 => {
            if negative {
                output.write_u16::<O>((!(num_bits as u16)).wrapping_add(1))?;
            } else {
                output.write_u16::<O>(   num_bits as u16)?;
            }
        },
        IntSuffix::U32 => {
            output.write_u32::<O>(num_bits as u32)?;
        },
        IntSuffix::I32 => {
            if negative {
                output.write_u32::<O>((!(num_bits as u32)).wrapping_add(1))?;
            } else {
                output.write_u32::<O>(   num_bits as u32)?;
            }
        },
        IntSuffix::U64 => {
            output.write_u64::<O>(num_bits as u64)?;
        },
        IntSuffix::I64 => {
            if negative {
                output.write_u64::<O>((!(num_bits as u64)).wrapping_add(1))?;
            } else {
                output.write_u64::<O>(   num_bits as u64)?;
            }
        },
        // Everything else is either invalid or ambiguous.
        s => {
            return Err(Error::UnsupportedNumberSuffix(format!("{:?}", s)));
        },
    }
    Ok(())
}

fn float_to_suffix(negative: bool, float: &LitFloat) -> Result<FloatSuffix, Error> {
    let num_bits = float.value();
    let s = if num_bits > 3.40282347e+38 {
        FloatSuffix::F64
    } else {
        FloatSuffix::F32
    };
    let s = match (s, float.suffix()) {
        // If none is specified use the least size suffix possible.
        (s, FloatSuffix::None) => s,
        // The only possible float cast.
        (FloatSuffix::F32, FloatSuffix::F64) => FloatSuffix::F64,
        // Everything else is either invalid or ambiguous.
        (given, requested) => {
            return Err(Error::IncompatibleNumberSuffix(
                float.into_tokens().to_string(),
                negative,
                format!("{:?}", given),
                format!("{:?}", requested),
            ));
        },
    };
    Ok(s)
}

fn bytify_implementation_float<O: ByteOrder>(negative: bool, float: LitFloat, output: &mut Vec<u8>) -> Result<(), Error> {
    let num_bits = float.value();
    let num_bits_suffix = float_to_suffix(negative, &float)?;
    match num_bits_suffix {
        FloatSuffix::F32 => {
            if negative {
                output.write_f32::<O>(-(num_bits as f32))?;
            } else {
                output.write_f32::<O>(  num_bits as f32 )?;
            }
        },
        FloatSuffix::F64 => {
            if negative {
                output.write_f64::<O>(-num_bits)?;
            } else {
                output.write_f64::<O>( num_bits)?;
            }
        },
        // Everything else is either invalid or ambiguous.
        s => {
            return Err(Error::UnsupportedNumberSuffix(format!("{:?}", s)));
        },
    }
    Ok(())
}

fn bytify_implementation_element<O: ByteOrder>(lit: Lit, output: &mut Vec<u8>) -> Result<(), Error> {
    match lit {
        Lit::Char(c) => {
            let offset = output.len();
            output.resize(c.value().len_utf8() + offset, 0u8);
            c.value().encode_utf8(&mut output[offset ..]);
        },
        Lit::Str(string) => {
            output.extend_from_slice(string.value().as_bytes());
        },
        Lit::Int(int) => {
            bytify_implementation_int::<O>(false, int, output)?;
        },
        Lit::Float(float) => {
            bytify_implementation_float::<O>(false, float, output)?;
        },
        lit => {
            return Err(Error::unsupported_lit(lit));
        },
    }
    Ok(())
}

fn bytify_implementation(input: &str) -> Result<String, Error> {
    let input_buffer = TokenBuffer::new(TokenStream::from_str(input).unwrap());
    let mut c = input_buffer.begin();
    let mut output: Vec<u8> = Vec::new();
    loop {
        if c.eof() {
            break;
        }
        let (
            input,
            c_rem,
        ): (
            Punctuated<Expr, Token![,]>,
            _,
        ) = Punctuated::parse_terminated_nonempty(c)?;
        for expr in input {
            let (
                endianness,
                expr,
            ) = match expr {
                /* it is not, actually! */ Expr::Type(tpe_expr) => {
                    let expr = *tpe_expr.expr;
                    let endianness = match tpe_expr.ty.into_tokens().to_string().as_str() {
                        "BE" | "be" => Endianness::BE,
                        "LE" | "le" => Endianness::LE,
                        invalid => {
                            return Err(Error::InvalidEndianness(invalid.to_string()));
                        },
                    };
                    (endianness, expr)
                },
                expr => {
                    (DEFAULT_ENDIANNESS, expr)
                },
            };
            match expr {
                Expr::Lit(lit_expr) => {
                    if endianness == Endianness::BE {
                        bytify_implementation_element::<BE>(lit_expr.lit, &mut output)?;
                    } else {
                        bytify_implementation_element::<LE>(lit_expr.lit, &mut output)?;
                    }
                },
                /* why this is not a part of the literal? */ Expr::Unary(unary_expr) => {
                    match unary_expr.op {
                        UnOp::Neg(op) => {
                            match *unary_expr.expr {
                                Expr::Lit(lit_expr) => {
                                    match lit_expr.lit {
                                        Lit::Int(int) => {
                                            if endianness == Endianness::BE {
                                                bytify_implementation_int::<BE>(true, int, &mut output)?;
                                            } else {
                                                bytify_implementation_int::<LE>(true, int, &mut output)?;
                                            }
                                        },
                                        Lit::Float(float) => {
                                            if endianness == Endianness::BE {
                                                bytify_implementation_float::<BE>(true, float, &mut output)?;
                                            } else {
                                                bytify_implementation_float::<LE>(true, float, &mut output)?;
                                            }
                                        },
                                        lit => {
                                            return Err(Error::unsupported_lit(lit));
                                        },
                                    }
                                },
                                expr => {
                                    return Err(Error::unsupported_prefixed_expression(UnOp::Neg(op), expr));
                                },
                            }
                        },
                        op => {
                            return Err(Error::unsupported_prefixed_expression(op, *unary_expr.expr));
                        },
                    }
                },
                expr => {
                    return Err(Error::unsupported_expression(expr));
                },
            }
        }
        c = c_rem;
    }
    Ok(quote! {
        [
            #(#output),*
        ]
    }.into_tokens().to_string())
}

proc_macro_expr_impl! {

    pub fn bytify_inner(input: &str) -> String {
        bytify_implementation(input).unwrap_or_else(|err| panic!("{}", err))
    }
}