-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
26 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
function replace (buf, position, replacement) { | ||
let end = position + replacement.length | ||
if (position < 0) { | ||
return replace(buf, buf.length + position, replacement) | ||
} | ||
if (end > buf.length) { | ||
throw new Error('The replacement will cause the result to overflow.') | ||
} | ||
return Buffer.from(buf).fill(replacement, position, end) | ||
} | ||
|
||
function xor (x, y) { | ||
if (x.length !== y.length) { | ||
throw new Error('The length of two buffers is different') | ||
} | ||
let z = Buffer.alloc(x.length) | ||
for (let i = 0; i < z.length; i ++) { | ||
z[i] = x[i] ^ y[i] | ||
} | ||
return z | ||
} | ||
|
||
exports.replace = replace | ||
exports.xor = xor |