Skip to content

Commit

Permalink
Add support for append statements
Browse files Browse the repository at this point in the history
  • Loading branch information
ExcaliburZero committed Nov 5, 2023
1 parent 4fa83d8 commit 38a8cba
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 2 deletions.
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.
* [ ] `PutVarChr VAR OFFSET VAR [TYPE]`
* [ ] `Debug [MODE]`
* [ ] `Padding VAR [FILENUM] [BASE_OFF]`
* [ ] `Append [DIRECTION]`
* [x] `Append [DIRECTION]`
* [x] `Encryption ALGO KEY [IVEC] [MODE] [KEYLEN]`
* [x] `Print MESSAGE`
* [x] `GetArray VAR ARRAY VAR_IDX`
Expand Down
54 changes: 54 additions & 0 deletions src/server/keyword_docs/append.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
Append [DIRECTION]

Command to enable the append mode in the *Log commands, so if
the output filename already exists it will not be overwritten,
the new content is concatenated (appended) to the existent one.
Note that with real files (not memory files) the user may be
prompted before writing the output file if it already existed
before the running of the script.
Note that the reimport mode may not work correctly when you use
a combo of MEMORY_FILE and Append, so the direct and more
simple Log to file + Append is suggested.
Note that from QuickBMS 0.11 the Append command also affects
the Put* commands (Put/PutDString/PutCT).

Arguments:
DIRECTION This is a new optional argument that allows to
specify where placing the new content:
-1 prefix:
a negative value means that the new content
will be placed before the current file, so the
old content will be appended to the new one
0 append:
the new content will be appended to the current
one (default, just like without DIRECTION)
1 overwrite:
the new content will overwrite the current one
without changing the file size if the new one
is smaller, use goto to set the offset where
placing the new content.
2 insert:
the new content will be inserted in the current
position (size = position + data + remaining)

Examples:
append
Log "dump.dat" 0 0x10
Log "dump.dat" 0x10 0x100

The following is a particular example for allocating a
MEMORY_FILE and using it instead of TEMPORARY_FILE saving space
on the disk and performances:

math TMP = CHUNKS
math TMP *= 0x8000
log MEMORY_FILE 0 0
putvarchr MEMORY_FILE TMP 0 # pre-allocation for speed
log MEMORY_FILE 0 0 # reset the position and size
append
for i = 0 < CHUNKS
...
clog MEMORY_FILE OFFSET ZSIZE 0x8000
next i
append
get SIZE asize MEMORY_FILE
4 changes: 4 additions & 0 deletions src/server/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,10 @@ pub fn get_keyword_docs() -> HashMap<String, String> {
"filexor".to_string(),
include_str!("keyword_docs/filexor.txt").to_string(),
),
(
"append".to_string(),
include_str!("keyword_docs/append.txt").to_string(),
),
]
.iter()
.cloned()
Expand Down
6 changes: 6 additions & 0 deletions tree-sitter-quickbms/grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ module.exports = grammar({
$.reverselong_statement,
$.reverselonglong_statement,
$.filexor_statement,
$.append_statement,
),
set_statement: $ => seq(
$.set,
Expand Down Expand Up @@ -220,6 +221,10 @@ module.exports = grammar({
field("offset", optional($._expression)),
// field("file_number", optional($._expression)),
),
append_statement: $ => seq(
$.append,
field("direction", optional(choice("-1", "0", "1", "2"))),
),
comparison: $ => choice(
"<",
">",
Expand Down Expand Up @@ -329,6 +334,7 @@ module.exports = grammar({
reverselong: $ => /[Rr][Ee][Vv][Ee][Rr][Ss][Ee][Ll][Oo][Nn][Gg]/,
reverselonglong: $ => /[Rr][Ee][Vv][Ee][Rr][Ss][Ee][Ll][Oo][Nn][Gg][Ll][Oo][Nn][Gg]/,
filexor: $ => /[Ff][Ii][Ll][Ee][Xx][Oo][Rr]/,
append: $ => /[Aa][Pp][Pp][Ee][Nn][Dd]/,
question_mark: $ => /\?/,

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

append
Log "dump.dat" 0 0x10
Log "dump.dat" 0x10 0x100
append -1
append 0
append 1
append 2

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

(source_file
(append_statement
(append))
(log_statement
(log)
(string_literal)
(integer_literal)
(integer_literal))
(log_statement
(log)
(string_literal)
(integer_literal)
(integer_literal))
(append_statement
(append))
(append_statement
(append))
(append_statement
(append))
(append_statement
(append)))
8 changes: 7 additions & 1 deletion tree-sitter-quickbms/test/corpus/log.bms
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Log "dump.dat" 0 SIZE
Log "" 0 SIZE
Log "folder/name.*" 0 SIZE
Log "redecrypted_file.dat" 0 SIZE MEMORY_FILE
Log "dump.dat" 0 0x10

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

Expand Down Expand Up @@ -42,4 +43,9 @@ Log "redecrypted_file.dat" 0 SIZE MEMORY_FILE
(string_literal)
(integer_literal)
(identifier)
(identifier)))
(identifier))
(log_statement
(log)
(string_literal)
(integer_literal)
(integer_literal)))

0 comments on commit 38a8cba

Please sign in to comment.