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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
#![feature(ptr_sub_ptr, lazy_cell, box_patterns)]

use bincode::{Decode, Encode};
use std::{borrow::Cow, rc::Rc};

#[allow(unused_imports)]
use nom::{
    branch::alt,
    bytes::complete::{escaped, tag, take_till, take_until, take_while, take_while_m_n},
    character::{
        complete::{alphanumeric1, char as cchar, multispace0, multispace1, none_of, one_of},
        is_alphabetic, is_newline, is_space,
        streaming::space1,
    },
    combinator::{fail, map, map_res, opt, value},
    number::complete::be_u8,
    sequence::{delimited, tuple},
};
use nom::{
    bytes::complete::take,
    combinator::success,
    error::{context, ErrorKind, ParseError},
    multi::{length_count, length_data},
    number::{complete, Endianness},
    Parser,
};
use nom_supreme::{error::*, ParserExt};
use serde::{Deserialize, Serialize};
use serde_bytes::ByteBuf;

pub mod lua51;
pub mod lua52;
pub mod lua53;
pub mod lua54;
pub mod luajit;
pub mod luau;
pub mod utils;

pub type IResult<I, O, E = ErrorTree<I>> = Result<(I, O), nom::Err<E>>;

#[derive(Clone, Default, Debug, PartialEq, Eq, Serialize, Deserialize, Encode, Decode)]
pub struct LuaHeader {
    pub lua_version: u8,
    pub format_version: u8,
    pub big_endian: bool,
    pub int_size: u8,
    pub size_t_size: u8,
    pub instruction_size: u8,
    pub number_size: u8,
    pub number_integral: bool,
    // for luajit
    pub stripped: bool,
    pub has_ffi: bool,
}

impl LuaHeader {
    pub fn endian(&self) -> Endianness {
        if self.big_endian {
            Endianness::Big
        } else {
            Endianness::Little
        }
    }
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize, Encode, Decode)]
#[serde(untagged)]
pub enum LuaNumber {
    Integer(i64),
    Float(f64),
}

impl std::fmt::Display for LuaNumber {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Float(n) => write!(f, "{n}"),
            Self::Integer(i) => write!(f, "{i}"),
        }
    }
}

#[derive(Clone, Debug, Default, Serialize, Deserialize, Encode, Decode)]
pub struct ConstTable {
    pub array: Vec<LuaConstant>,
    pub hash: Vec<(LuaConstant, LuaConstant)>,
}

#[derive(Clone, Default, Serialize, Deserialize, Encode, Decode)]
#[serde(untagged)]
pub enum LuaConstant {
    #[default]
    Null,
    Bool(bool),
    Number(LuaNumber),
    String(#[bincode(with_serde)] Rc<ByteBuf>),
    // for luajit
    Proto(usize),
    Table(Box<ConstTable>),
    // // for luau
    // Imp(u32),
}

impl<T: Into<Vec<u8>>> From<T> for LuaConstant {
    fn from(value: T) -> Self {
        Self::String(Rc::new(ByteBuf::from(value)))
    }
}

impl std::fmt::Debug for LuaConstant {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Null => write!(f, "Null"),
            Self::Bool(arg0) => f.debug_tuple("Bool").field(arg0).finish(),
            Self::Number(arg0) => match arg0 {
                LuaNumber::Float(n) => f.debug_tuple("Number").field(n).finish(),
                LuaNumber::Integer(n) => f.debug_tuple("Integer").field(n).finish(),
            },
            Self::String(arg0) => f
                .debug_tuple("String")
                .field(&String::from_utf8_lossy(arg0))
                .finish(),
            Self::Proto(i) => f.debug_tuple("Proto").field(i).finish(),
            Self::Table(box ConstTable { array, hash }) => f
                .debug_struct("Table")
                .field("array", array)
                .field("hash", hash)
                .finish(),
            // Self::Imp(imp) => f.debug_tuple("Imp").field(imp).finish(),
        }
    }
}

#[derive(Debug, Default, Serialize, Deserialize, Encode, Decode)]
pub struct LuaLocal {
    pub name: String,
    pub start_pc: u64,
    pub end_pc: u64,
    pub reg: u8, // for luau
}

#[derive(Debug, Serialize, Deserialize, Encode, Decode)]
pub struct LuaVarArgInfo {
    pub has_arg: bool,
    pub needs_arg: bool,
}

impl LuaVarArgInfo {
    pub fn new() -> Self {
        Self {
            has_arg: true,
            needs_arg: true,
        }
    }
}

#[derive(Debug, Default, Serialize, Deserialize, Encode, Decode)]
pub struct UpVal {
    pub on_stack: bool,
    pub id: u8,
    pub kind: u8,
}

#[derive(Default, Serialize, Deserialize, Encode, Decode)]
pub struct LuaChunk {
    pub name: Vec<u8>,
    pub line_defined: u64,
    pub last_line_defined: u64,
    pub num_upvalues: u8,
    pub num_params: u8,
    /// Equivalent to framesize for luajit
    pub max_stack: u8,
    /// for luajit
    pub flags: u8,
    pub is_vararg: Option<LuaVarArgInfo>,
    pub instructions: Vec<u32>,
    pub constants: Vec<LuaConstant>,
    /// for luajit
    pub num_constants: Vec<LuaNumber>,
    pub prototypes: Vec<Self>,
    pub source_lines: Vec<(u32, u32)>,
    pub locals: Vec<LuaLocal>,
    /// for lua53
    pub upvalue_infos: Vec<UpVal>,
    pub upvalue_names: Vec<Vec<u8>>,
}

impl std::fmt::Debug for LuaChunk {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("LuaChunk")
            .field("name", &String::from_utf8_lossy(&self.name))
            .field("line_defined", &self.line_defined)
            .field("last_line_defined", &self.last_line_defined)
            .field("is_vararg", &self.is_vararg.is_some())
            .field("num_params", &self.num_params)
            .field("num_upvalues", &self.num_upvalues)
            .field("locals", &self.locals)
            .field("constants", &self.constants)
            .field("prototypes", &self.prototypes)
            .finish()
    }
}

impl LuaChunk {
    pub fn name(&self) -> Cow<str> {
        String::from_utf8_lossy(&self.name)
    }

    pub fn flags(&self) -> luajit::ProtoFlags {
        luajit::ProtoFlags::from_bits(self.flags).unwrap()
    }

    pub fn is_empty(&self) -> bool {
        self.instructions.is_empty()
    }
}

#[derive(Debug, Serialize, Deserialize, Encode, Decode)]
pub struct LuaBytecode {
    pub header: LuaHeader,
    pub main_chunk: LuaChunk,
}

fn lua_header(input: &[u8]) -> IResult<&[u8], LuaHeader, ErrorTree<&[u8]>> {
    let (rest, (_, result)) = tuple((
        tag(b"\x1BLua"),
        alt((
            map(
                tuple((
                    tag(b"\x51"),
                    be_u8,
                    be_u8,
                    be_u8,
                    be_u8,
                    be_u8,
                    be_u8,
                    be_u8,
                )),
                |(
                    _,
                    format_version,
                    big_endian,
                    int_size,
                    size_t_size,
                    instruction_size,
                    number_size,
                    number_integral,
                )| LuaHeader {
                    lua_version: LUA51,
                    format_version,
                    big_endian: big_endian != 1,
                    int_size,
                    size_t_size,
                    instruction_size,
                    number_size,
                    number_integral: number_integral != 0,
                    ..Default::default()
                },
            ),
            map(
                tuple((
                    tag(b"\x52"),
                    be_u8,
                    be_u8,
                    be_u8,
                    be_u8,
                    be_u8,
                    be_u8,
                    be_u8,
                    take(6usize), // LUAC_DATA
                )),
                |(
                    _,
                    format_version,
                    big_endian,
                    int_size,
                    size_t_size,
                    instruction_size,
                    number_size,
                    number_integral,
                    _,
                )| LuaHeader {
                    lua_version: LUA52,
                    format_version,
                    big_endian: big_endian != 1,
                    int_size,
                    size_t_size,
                    instruction_size,
                    number_size,
                    number_integral: number_integral != 0,
                    ..Default::default()
                },
            ),
            map(
                tuple((
                    tag(b"\x53"),
                    be_u8,
                    take(6usize), // LUAC_DATA
                    be_u8,
                    be_u8,
                    be_u8,
                    be_u8,
                    be_u8,
                    complete::le_i64,
                    complete::le_f64,
                    be_u8,
                )),
                |(
                    _,
                    format_version,
                    _luac_data,
                    int_size,
                    size_t_size,
                    instruction_size,
                    _integer_size, // lua_Integer
                    number_size,
                    _,
                    _,
                    _,
                )| LuaHeader {
                    lua_version: LUA53,
                    format_version,
                    big_endian: cfg!(target_endian = "big"),
                    int_size,
                    size_t_size,
                    instruction_size,
                    number_size,
                    number_integral: false,
                    ..Default::default()
                },
            ),
            map(
                tuple((
                    tag(b"\x54"),
                    be_u8,
                    take(6usize), // LUAC_DATA
                    be_u8,
                    be_u8,
                    be_u8,
                    complete::le_i64,
                    complete::le_f64,
                    be_u8,
                )),
                |(
                    _,
                    format_version,
                    _luac_data,
                    instruction_size,
                    _integer_size, // lua_Integer
                    number_size,
                    _,
                    _,
                    _,
                )| LuaHeader {
                    lua_version: LUA54,
                    format_version,
                    big_endian: cfg!(target_endian = "big"),
                    int_size: 4,
                    size_t_size: 8,
                    instruction_size,
                    number_size,
                    number_integral: false,
                    ..Default::default()
                },
            ),
        )),
    ))(input)?;
    Ok((rest, result))
}

fn must<I, O, E: ParseError<I>, P: Parser<I, O, E>>(
    cond: bool,
    mut parser: P,
) -> impl FnMut(I) -> IResult<I, O, E> {
    move |input| {
        if cond {
            parser.parse(input)
        } else {
            Err(nom::Err::Error(E::from_error_kind(
                input,
                ErrorKind::Switch,
            )))
        }
    }
}

fn lua_int<'a>(header: &LuaHeader) -> impl Parser<&'a [u8], u64, ErrorTree<&'a [u8]>> {
    let intsize = header.int_size;
    alt((
        must(
            intsize == 8,
            map(complete::u64(header.endian()), |v| v as u64),
        ),
        must(
            intsize == 4,
            map(complete::u32(header.endian()), |v| v as u64),
        ),
        must(
            intsize == 2,
            map(complete::u16(header.endian()), |v| v as u64),
        ),
        must(intsize == 1, map(be_u8, |v| v as u64)),
    ))
    .context("integer")
}

fn lua_size_t<'a>(header: &LuaHeader) -> impl Parser<&'a [u8], u64, ErrorTree<&'a [u8]>> {
    let sizesize = header.size_t_size;
    alt((
        must(
            sizesize == 8,
            map(complete::u64(header.endian()), |v| v as u64),
        ),
        must(
            sizesize == 4,
            map(complete::u32(header.endian()), |v| v as u64),
        ),
        must(
            sizesize == 2,
            map(complete::u16(header.endian()), |v| v as u64),
        ),
        must(sizesize == 1, map(be_u8, |v| v as u64)),
    ))
    .context("size_t")
}

fn lua_number<'a>(header: &LuaHeader) -> impl Parser<&'a [u8], LuaNumber, ErrorTree<&'a [u8]>> {
    let int = header.number_integral;
    let size = header.number_size;
    alt((
        must(
            int == true,
            map(
                alt((
                    must(size == 8, map(complete::be_i8, |v| v as i64)),
                    must(size == 4, map(complete::i16(header.endian()), |v| v as i64)),
                    must(size == 2, map(complete::i32(header.endian()), |v| v as i64)),
                    must(size == 1, map(complete::i64(header.endian()), |v| v as i64)),
                )),
                |v| LuaNumber::Integer(v),
            ),
        ),
        must(
            int == false,
            map(
                alt((
                    must(size == 8, map(complete::f64(header.endian()), |v| v as f64)),
                    must(size == 4, map(complete::f32(header.endian()), |v| v as f64)),
                )),
                |v| LuaNumber::Float(v),
            ),
        ),
    ))
    .context("number")
}

pub fn lua_bytecode(input: &[u8]) -> IResult<&[u8], LuaBytecode, ErrorTree<&[u8]>> {
    let (input, header) = alt((lua_header, luajit::lj_header))(input)?;
    log::trace!("header: {header:?}");
    let (input, main_chunk) = match header.lua_version {
        LUA51 => lua51::lua_chunk(&header).parse(input)?,
        LUA52 => lua52::lua_chunk(&header).parse(input)?,
        LUA53 => lua53::lua_chunk(&header).parse(input)?,
        LUA54 => lua54::lua_chunk(&header).parse(input)?,
        LUAJ1 | LUAJ2 => luajit::lj_chunk(&header).parse(input)?,
        _ => context("unsupported lua version", fail)(input)?,
    };
    Ok((input, LuaBytecode { header, main_chunk }))
}

pub fn parse(input: &[u8]) -> Result<LuaBytecode, String> {
    lua_bytecode(input).map(|x| x.1).map_err(|e| {
        format!(
            "{:#?}",
            e.map(|e| e.map_locations(|p| unsafe { p.as_ptr().sub_ptr(input.as_ptr()) }))
        )
    })
}

#[cfg(feature = "rmp-serde")]
impl LuaBytecode {
    pub fn from_msgpack(mp: &[u8]) -> Result<Self, rmp_serde::decode::Error> {
        rmp_serde::from_slice(mp)
    }

    pub fn to_msgpack(&self) -> Result<Vec<u8>, rmp_serde::encode::Error> {
        rmp_serde::to_vec(self)
    }
}

pub const LUA51: u8 = 0x51;
pub const LUA52: u8 = 0x52;
pub const LUA53: u8 = 0x53;
pub const LUA54: u8 = 0x54;
pub const LUAJ1: u8 = 0x11;
pub const LUAJ2: u8 = 0x12;