Core Function BinaryMidReplace

From Sputnik Wiki
Jump to: navigation, search
BinaryMidReplace( <binary-array>, <replacement>, <start>, <length> )

Contents

Description

Replace a section (from start to length) of the binary with the replacement binary.

Parameters

binary-array

The binary variable to use.

replacement

A variable to replace the section with

(If it is not binary it will cast as binary for the replacement)

start

Position to start from.

If the start is a negative value the position will work backwards from the length of the binary.

length

Optional; How many bytes to remove.

Default is all remaining bytes;

If length is given and is negative, then that many bytes will be omitted from the end of binary (after the start position has been calculated when a start is negative).

If start denotes the position of this truncation or beyond, empty binary variable will be returned.

Return Value

Success: Returns true.

Failure: Returns false (On error or if there was no bytes were added).

Remarks

None.

Example

Remove everything after position 2 then insert the replacement

// Create the binary
$a = Pack("A*", "Hello World!");
// Create the replacement binary
$b = Pack("A*", "CatDog");
// Print it
say "Before:";
say BinaryExpand($a);
// Remove bytes and insert in its place
BinaryMidReplace($a, $b, 2);
// Print result
say "After:";
say BinaryExpand($a);
// Prints:
// Before:
// 00 | 48 65 6C 6C 6F 20 57 6F 72 6C 64 21 -- -- -- -- Hello World!
// After:
// 00 | 48 65 43 61 74 44 6F 67 -- -- -- -- -- -- -- -- HeCatDog

Remove 3 bytes after position 2 then insert the replacement

// Create the binary
$a = Pack("A*", "Hello World!");
// Create the replacement binary
$b = Pack("A*", "CatDog");
// Print it
say "Before:";
say BinaryExpand($a);
// Remove bytes and insert in its place
BinaryMidReplace($a, $b, 2, 3);
// Print result
say "After:";
say BinaryExpand($a);
// Prints:
// Before:
// 00 | 48 65 6C 6C 6F 20 57 6F 72 6C 64 21 -- -- -- -- Hello World!
// After:
// 00 | 48 65 43 61 74 44 6F 67 20 57 6F 72 6C 64 21 -- HeCatDog World!

Remove the last 3 bytes then insert the replacement

// Create the binary
$a = Pack("A*", "Hello World!");
// Create the replacement binary
$b = Pack("A*", "CatDog");
// Print it
say "Before:";
say BinaryExpand($a);
// Remove bytes and insert in its place
BinaryMidReplace($a, $b, -3);
// Print result
say "After:";
say BinaryExpand($a);
// Prints:
// Before:
// 00 | 48 65 6C 6C 6F 20 57 6F 72 6C 64 21 -- -- -- -- Hello World!
// After:
// 00 | 48 65 6C 6C 6F 20 57 6F 72 43 61 74 44 6F 67 -- Hello WorCatDog

At the end

// Create the binary
$a = Pack("A*", "Test");
// Create the replacement binary
$b = Pack("A*", "CatDog");
// Print it
say "Before:";
say BinaryExpand($a);
// Remove bytes and insert in its place
BinaryMidReplace($a, $b, 4);
// Print result
say "After:";
say BinaryExpand($a);
// Prints:
// Before:
// 00 | 48 65 6C 6C 6F 20 57 6F 72 6C 64 21 -- -- -- -- Hello World!
// After:
// 00 | 54 65 73 74 43 61 74 44 6F 67 -- -- -- -- -- -- TestCatDog

At the beginning

// Create the binary
$a = Pack("A*", "Test");
// Create the replacement binary
$b = Pack("A*", "CatDog");
// Print it
say "Before:";
say BinaryExpand($a);
// Remove bytes and insert in its place
BinaryMidReplace($a, $b, 0);
// Print result
say "After:";
say BinaryExpand($a);
// Prints:
// Before:
// 00 | 48 65 6C 6C 6F 20 57 6F 72 6C 64 21 -- -- -- -- Hello World!
// After:
// 00 | 3 61 74 44 6F 67 -- -- -- -- -- -- -- -- -- -- CatDog
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox