Skip to content

Commit

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

The "write-mode" alternative of the previous command which
allows to perform various complex operations with custom
algorithms (like in my Deer Hunter 2004/2005 scripts).
It can be compared to C as: var1[offset] = var2;
Note that PutVarChr can be also used as an allocator of memory
that is often useful in the implementation of custom
decompression algorithms or, moreover, for pre-allocating a
MEMORY_FILE for storing chunks. This is useful to avoid wasting
time and memory with the incremental allocation, remember only
to use the command "Log MEMORY_FILE 0 0" after it for resetting
the position of the MEMORY_FILE.

arguments
VAR Variable or memory file in which you want to put
the element
OFFSET Position of the output where placing the element,
it can also be negative in which case it will work
from the end of the variable (may not work in some
conditions)
VAR Source variable which will contain the element to
write, it's also possible to store the address of
the variable which may be useful with external DLLs
TYPE Type of the element to read and assign to the first
variable, if not specified it's a BYTE (8bit).
You can specify most of the available datatypes
like short, long, longlong and so on.

Examples:
For i = 0 < SIZE
GetVarChr TMP MEMORY_FILE i
Math TMP ^= 0xff
PutVarChr MEMORY_FILE i TMP
Next i
4 changes: 4 additions & 0 deletions src/server/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,10 @@ pub fn get_keyword_docs() -> HashMap<String, String> {
"getvarchr".to_string(),
include_str!("keyword_docs/getvarchr.txt").to_string(),
),
(
"putvarchr".to_string(),
include_str!("keyword_docs/putvarchr.txt").to_string(),
),
(
"byte".to_string(),
include_str!("keyword_docs/byte.txt").to_string(),
Expand Down
11 changes: 11 additions & 0 deletions tree-sitter-quickbms/grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ module.exports = grammar({
$.filexor_statement,
$.append_statement,
$.getvarchr_statement,
$.putvarchr_statement,
),
set_statement: $ => seq(
$.set,
Expand Down Expand Up @@ -234,6 +235,14 @@ module.exports = grammar({
field("type", optional($.type)),
"\n", // Note: needed to avoid a conflict with a string statement following a getvarchr statement that does not have a type
),
putvarchr_statement: $ => seq(
$.putvarchr,
field("left_variable", $._expression),
field("offset", $._expression),
field("right_variable", $._expression),
field("type", optional($.type)),
"\n", // Note: needed to avoid a conflict with a string statement following a getvarchr statement that does not have a type
),
comparison: $ => choice(
"<",
">",
Expand All @@ -250,6 +259,7 @@ module.exports = grammar({
"=",
"+=",
"/=",
"^=",
"*",
"<<",
"u<<",
Expand Down Expand Up @@ -350,6 +360,7 @@ module.exports = grammar({
filexor: $ => /[Ff][Ii][Ll][Ee][Xx][Oo][Rr]/,
append: $ => /[Aa][Pp][Pp][Ee][Nn][Dd]/,
getvarchr: $ => /[Gg][Ee][Tt][Vv][Aa][Rr][Cc][Hh][Rr]/,
putvarchr: $ => /[Pp][Uu][Tt][Vv][Aa][Rr][Cc][Hh][Rr]/,
question_mark: $ => /\?/,

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

For i = 0 < SIZE
GetVarChr TMP MEMORY_FILE i
Math TMP ^= 0xff
PutVarChr MEMORY_FILE i TMP
Next i

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

(source_file
(for_statement
(for)
(identifier)
(operation)
(integer_literal)
(comparison)
(identifier)
(getvarchr_statement
(getvarchr)
(identifier)
(identifier)
(identifier))
(math_statement
(math)
(identifier)
(operation)
(integer_literal))
(putvarchr_statement
(putvarchr)
(identifier)
(identifier)
(identifier))
(next_statement
(next)
(identifier))))

0 comments on commit 7e1f6f9

Please sign in to comment.