Skip to content

Commit

Permalink
Add support for Padding statements
Browse files Browse the repository at this point in the history
  • Loading branch information
ExcaliburZero committed Nov 5, 2023
1 parent cd0ef12 commit 5757052
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ The following QuickBMS commands are currently supported by the langauge server.
* [x] `GetVarChr VAR VAR OFFSET [TYPE]`
* [x] `PutVarChr VAR OFFSET VAR [TYPE]`
* [ ] `Debug [MODE]`
* [ ] `Padding VAR [FILENUM] [BASE_OFF]`
* [x] `Padding VAR [FILENUM] [BASE_OFF]`
* [x] `Append [DIRECTION]`
* [x] `Encryption ALGO KEY [IVEC] [MODE] [KEYLEN]`
* [x] `Print MESSAGE`
Expand Down
19 changes: 19 additions & 0 deletions src/server/keyword_docs/padding.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Padding VAR [FILENUM] [BASE_OFF]

When called it performs an automatic GoTo to the next position
of the file skipping the aligned data.
Imagine to have a file where it's used an alignment of 4 bytes
and your current file offset is 0x39, if you use Padding 4 the
offset will be automatically changed to 0x3c.
By default the padding is referred to the beginning of the file
(offset 0).

Arguments:
VAR Size of the alignment, like 4 or 16 and so on
FILENUM Number of the file associated to the archive (0)
BASE_OFF base offset from where calculating the padding (0)

Examples:
Get NAME string
Padding 4
get OFFSET long
4 changes: 4 additions & 0 deletions src/server/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,10 @@ pub fn get_keyword_docs() -> HashMap<String, String> {
"clog".to_string(),
include_str!("keyword_docs/clog.txt").to_string(),
),
(
"padding".to_string(),
include_str!("keyword_docs/padding.txt").to_string(),
),
]
.iter()
.cloned()
Expand Down
13 changes: 13 additions & 0 deletions tree-sitter-quickbms/grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ module.exports = grammar({
$.putvarchr_statement,
$.comtype_statement,
$.clog_statement,
$.padding_statement,
),
set_statement: $ => seq(
$.set,
Expand Down Expand Up @@ -263,6 +264,17 @@ module.exports = grammar({
),
)),
),
padding_statement: $ => seq(
$.padding,
field("variable", $._expression),
optional(choice(
field("file_number", $._expression),
seq(
field("file_number", $._expression),
field("base_offset", $._expression),
),
)),
),
comparison: $ => choice(
"<",
">",
Expand Down Expand Up @@ -388,6 +400,7 @@ module.exports = grammar({
putvarchr: $ => /[Pp][Uu][Tt][Vv][Aa][Rr][Cc][Hh][Rr]/,
comtype: $ => /[Cc][Oo][Mm][Tt][Yy][Pp][Ee]/,
clog: $ => /[Cc][Ll][Oo][Gg]/,
padding: $ => /[Pp][Aa][Dd][Dd][Ii][Nn][Gg]/,
question_mark: $ => /\?/,

identifier: $ => /[a-zA-Z_\\\.]+[a-zA-Z0-9_\-\\\.]*/,
Expand Down
24 changes: 24 additions & 0 deletions tree-sitter-quickbms/test/corpus/padding.bms
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
================================================================================
Padding statements
================================================================================

Get NAME string
Padding 4
get OFFSET long

--------------------------------------------------------------------------------

(source_file
(get_statement
(get)
(identifier)
(type
(string)))
(padding_statement
(padding)
(integer_literal))
(get_statement
(get)
(identifier)
(type
(long))))

0 comments on commit 5757052

Please sign in to comment.