Core Function BinaryReplace

From Sputnik Wiki
(Difference between revisions)
Jump to: navigation, search
m (1 revision)
 
(5 intermediate revisions by one user not shown)
Line 1: Line 1:
 
<pre>
 
<pre>
BinaryReplace( <binary-array>, <needle>, <replacement>, <maxReplacements> )
+
BinaryReplace( <binary-array>, <needle>, <replacement>, <offset>, <maxReplacements> )
 
</pre>
 
</pre>
  
Line 24: Line 24:
  
 
(It will be cast as binary if it isn't already)
 
(It will be cast as binary if it isn't already)
 +
 +
==== offset ====
 +
 +
Optional; Position to begin the search
 +
 +
Default: 0 (beginning)
  
 
==== maxReplacements ====
 
==== maxReplacements ====
  
 
Optional; The max allowed replacements to do
 
Optional; The max allowed replacements to do
 +
 +
Default: -1  (replace all)
  
 
=== Return Value ===
 
=== Return Value ===
Line 37: Line 45:
 
=== Remarks ===
 
=== Remarks ===
  
None.
+
If you want to replace single bytes you SHOULD cast the find and replace as byte (see example) this is because if you don't do that then it will use a normal search and replace which involves splitting and remaking the binary.
 +
 
 +
Doing that on a single byte? Could potentially take a long time and would be rather silly when you can just simply cast to byte and replace on the spot with no need to break up and remake the binary.
  
 
=== Example ===
 
=== Example ===
 +
 +
How to do a single byte replace for another byte
 +
 +
<syntaxhighlight lang="sputnik">
 +
// To replace single bytes you must cast
 +
// your find AND replace as a byte
 +
 +
// Assign a value to do the replace on
 +
$subject = Pack("A*", "Hello world!!!");
 +
// Print unchanged
 +
say $subject; // "Hello world!!!"
 +
// Do the replacement
 +
BinaryReplace($subject, (byte)108, (byte)74);
 +
// Print changed
 +
say $subject; // HeJJo worJd!!!
 +
</syntaxhighlight>
 +
 +
Single byte replace using chars
 +
 +
<syntaxhighlight lang="sputnik">
 +
// To replace single bytes you must cast
 +
// your find AND replace as a byte
 +
// to do this with chars is easy example
 +
 +
// Assign a value to do the replace on
 +
$subject = Pack("A*", "Hello world!!!");
 +
// Print unchanged
 +
say $subject; // "Hello world!!!"
 +
// Do the replacement
 +
BinaryReplace($subject, (byte)@'!', (byte)@'?');
 +
// Print changed
 +
say $subject; // Hello world???
 +
</syntaxhighlight>
  
 
Simple replacement
 
Simple replacement
Line 115: Line 158:
  
 
// Do the replacement
 
// Do the replacement
BinaryReplace($subject, $find, $replace, 1);
+
BinaryReplace($subject, $find, $replace, 0, 1);
  
 
// Print changed
 
// Print changed
Line 135: Line 178:
  
 
// Do the replacement
 
// Do the replacement
BinaryReplace($subject, $find, $replace, 1);
+
BinaryReplace($subject, $find, $replace, 0, 1);
  
 
// Print changed
 
// Print changed
 
say $subject; // Hello Spuntik ok Spuntik hehe world!!!
 
say $subject; // Hello Spuntik ok Spuntik hehe world!!!
 +
</syntaxhighlight>
 +
 +
Using the offset to not replace the first match
 +
 +
<syntaxhighlight lang="sputnik">
 +
// Assign a value to do the replace on
 +
$subject = Pack("A*", "Hello world ok world hehe world!!!");
 +
// Assign a value to find
 +
$find = Pack("A*", "world");
 +
// Assign a value to replace the found with
 +
$replace = Pack("A*", "Spuntik");
 +
 +
// Print unchanged
 +
say $subject; // Hello world ok world hehe world!!!
 +
 +
// Do the replacement
 +
BinaryReplace($subject, $find, $replace, 10, 1);
 +
 +
// Print changed
 +
say $subject; // Hello world ok Spuntik hehe world!!!
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
[[Category:Core Function]]
 
[[Category:Core Function]]

Latest revision as of 12:37, 14 June 2015

BinaryReplace( <binary-array>, <needle>, <replacement>, <offset>, <maxReplacements> )

Contents

Description

Search for a byte or string of bytes in a binary variable and replace it.

Parameters

binary-array

The binary variable to do the replacement on.

needle

The needle to find.

(It will be cast as binary if it isn't already)

replacement

The replacement to use.

(It will be cast as binary if it isn't already)

offset

Optional; Position to begin the search

Default: 0 (beginning)

maxReplacements

Optional; The max allowed replacements to do

Default: -1 (replace all)

Return Value

Success: Returns true.

Failure: Returns false.

Remarks

If you want to replace single bytes you SHOULD cast the find and replace as byte (see example) this is because if you don't do that then it will use a normal search and replace which involves splitting and remaking the binary.

Doing that on a single byte? Could potentially take a long time and would be rather silly when you can just simply cast to byte and replace on the spot with no need to break up and remake the binary.

Example

How to do a single byte replace for another byte

// To replace single bytes you must cast
// your find AND replace as a byte
 
// Assign a value to do the replace on
$subject = Pack("A*", "Hello world!!!");
// Print unchanged
say $subject; // "Hello world!!!"
// Do the replacement
BinaryReplace($subject, (byte)108, (byte)74);
// Print changed
say $subject; // HeJJo worJd!!!

Single byte replace using chars

// To replace single bytes you must cast
// your find AND replace as a byte
// to do this with chars is easy example
 
// Assign a value to do the replace on
$subject = Pack("A*", "Hello world!!!");
// Print unchanged
say $subject; // "Hello world!!!"
// Do the replacement
BinaryReplace($subject, (byte)@'!', (byte)@'?');
// Print changed
say $subject; // Hello world???

Simple replacement

// Assign a value to do the replace on
$subject = Pack("A*", "Hello world!!!");
// Assign a value to find
$find = Pack("A*", "world");
// Assign a value to replace the found with
$replace = Pack("A*", "cat");
 
// Print unchanged
say $subject; // Hello world!!!
 
// Do the replacement
BinaryReplace($subject, $find, $replace);
 
// Print changed
say $subject; // Hello cat!!!

This time the replacement is bigger than what is being replaced

// Assign a value to do the replace on
$subject = Pack("A*", "Hello world!!!");
// Assign a value to find
$find = Pack("A*", "world");
// Assign a value to replace the found with
$replace = Pack("A*", "Spuntik");
 
// Print unchanged
say $subject; // Hello world!!!
 
// Do the replacement
BinaryReplace($subject, $find, $replace);
 
// Print changed
say $subject; // Hello Spuntik!!!

Of course it will replace all it finds

// Assign a value to do the replace on
$subject = Pack("A*", "Hello world ok world hehe world!!!");
// Assign a value to find
$find = Pack("A*", "world");
// Assign a value to replace the found with
$replace = Pack("A*", "Spuntik");
 
// Print unchanged
say $subject; // Hello world ok world hehe world!!!
 
// Do the replacement
BinaryReplace($subject, $find, $replace);
 
// Print changed
say $subject; // Hello Spuntik ok Spuntik hehe Spuntik!!!

Only allow one replacement

// Assign a value to do the replace on
$subject = Pack("A*", "Hello world ok world hehe world!!!");
// Assign a value to find
$find = Pack("A*", "world");
// Assign a value to replace the found with
$replace = Pack("A*", "Spuntik");
 
// Print unchanged
say $subject; // Hello world ok world hehe world!!!
 
// Do the replacement
BinaryReplace($subject, $find, $replace, 0, 1);
 
// Print changed
say $subject; // Hello Spuntik ok world hehe world!!!

Only allow two replacements

// Assign a value to do the replace on
$subject = Pack("A*", "Hello world ok world hehe world!!!");
// Assign a value to find
$find = Pack("A*", "world");
// Assign a value to replace the found with
$replace = Pack("A*", "Spuntik");
 
// Print unchanged
say $subject; // Hello world ok world hehe world!!!
 
// Do the replacement
BinaryReplace($subject, $find, $replace, 0, 1);
 
// Print changed
say $subject; // Hello Spuntik ok Spuntik hehe world!!!

Using the offset to not replace the first match

// Assign a value to do the replace on
$subject = Pack("A*", "Hello world ok world hehe world!!!");
// Assign a value to find
$find = Pack("A*", "world");
// Assign a value to replace the found with
$replace = Pack("A*", "Spuntik");
 
// Print unchanged
say $subject; // Hello world ok world hehe world!!!
 
// Do the replacement
BinaryReplace($subject, $find, $replace, 10, 1);
 
// Print changed
say $subject; // Hello world ok Spuntik hehe world!!!
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox