Skip to content

Commit

Permalink
Add support for GetDString statements
Browse files Browse the repository at this point in the history
  • Loading branch information
ExcaliburZero committed Nov 5, 2023
1 parent 701ddc2 commit 6b218b4
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ The following QuickBMS commands are currently supported by the langauge server.
* [x] `Next [VAR] [OP] [VALUE]`
* [x] `Get VAR TYPE [FILENUM] [OFFSET]`
* [ ] Look into `OFFSET` argument. It is in list of commands, but not in the `Get` documentation.
* [ ] `GetDString VAR LENGTH [FILENUM]`
* [x] `GetDString VAR LENGTH [FILENUM]`
* [x] `GoTo OFFSET [FILENUM] [TYPE]`
* [x] `IDString [FILENUM] STRING`
* [x] `Log NAME OFFSET SIZE [FILENUM] [XSIZE]`
Expand Down
20 changes: 20 additions & 0 deletions src/server/keyword_docs/getdstring.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
GetDString VAR LENGTH [FILENUM]

It reads a defined amount of data from the file and stores it
in the given variable.
It's useful with filenames and other strings that have a length
specified in a previous 8, 16 or 32 bit field.

Arguments:
VAR Variable which will receive the read data
LENGTH Amount of bytes to read.
There is also an experimental method in which you
can specify the elements and their size like
LENGTH*NUM, for example:
getdstring ARRAY NUMBERS*4
FILENUM Number of the file associated to the archive (0)

Examples:
GetDString NAME NAME_LENGTH
GetDString NAME 0x100
getdstring ARRAY ELEMENTS*4
4 changes: 4 additions & 0 deletions src/server/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,10 @@ pub fn get_keyword_docs() -> HashMap<String, String> {
"extension".to_string(),
include_str!("keyword_docs/extension.txt").to_string(),
),
(
"getdstring".to_string(),
include_str!("keyword_docs/getdstring.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 @@ -49,6 +49,7 @@ module.exports = grammar({
$.clog_statement,
$.padding_statement,
$.savepos_statement,
$.getdstring_statement,
),
set_statement: $ => seq(
$.set,
Expand Down Expand Up @@ -282,6 +283,12 @@ module.exports = grammar({
field("variable", $._expression),
field("file_number", optional($._expression)),
),
getdstring_statement: $ => seq(
$.getdstring,
field("variable", $._expression),
field("length", choice($._expression, $.array_size_expression)),
field("file_number", optional($._expression)),
),
comparison: $ => choice(
"<",
">",
Expand Down Expand Up @@ -350,6 +357,11 @@ module.exports = grammar({
$.integer_literal,
$.identifier,
),
array_size_expression: $ => seq(
field("length", $._expression),
"*",
field("number", $._expression),
),

print: $ => /[Pp][Rr][Ii][Nn][Tt]/,
set: $ => /[Ss][Ee][Tt]/,
Expand Down Expand Up @@ -411,6 +423,7 @@ module.exports = grammar({
clog: $ => /[Cc][Ll][Oo][Gg]/,
padding: $ => /[Pp][Aa][Dd][Dd][Ii][Nn][Gg]/,
savepos: $ => /[Ss][Aa][Vv][Ee][Pp][Oo][Ss]/,
getdstring: $ => /[Gg][Ee][Tt][Dd][Ss][Tt][Rr][Ii][Nn][Gg]/,
question_mark: $ => /\?/,

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

GetDString NAME NAME_LENGTH
GetDString NAME 0x100
getdstring ARRAY ELEMENTS*4

getdstring name 0x10

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

(source_file
(getdstring_statement
(getdstring)
(identifier)
(identifier))
(getdstring_statement
(getdstring)
(identifier)
(integer_literal))
(getdstring_statement
(getdstring)
(identifier)
(array_size_expression
(identifier)
(integer_literal)))
(getdstring_statement
(getdstring)
(identifier)
(integer_literal)))

0 comments on commit 6b218b4

Please sign in to comment.