summaryrefslogtreecommitdiff
path: root/data/extensions/jsr@javascriptrestrictor/wrappingS-WEBA.js
diff options
context:
space:
mode:
Diffstat (limited to 'data/extensions/jsr@javascriptrestrictor/wrappingS-WEBA.js')
-rw-r--r--data/extensions/jsr@javascriptrestrictor/wrappingS-WEBA.js140
1 files changed, 101 insertions, 39 deletions
diff --git a/data/extensions/jsr@javascriptrestrictor/wrappingS-WEBA.js b/data/extensions/jsr@javascriptrestrictor/wrappingS-WEBA.js
index 9f7282a..8bd7041 100644
--- a/data/extensions/jsr@javascriptrestrictor/wrappingS-WEBA.js
+++ b/data/extensions/jsr@javascriptrestrictor/wrappingS-WEBA.js
@@ -4,6 +4,7 @@
* \see https://webaudio.github.io/web-audio-api
*
* \author Copyright (C) 2021 Matus Svancar
+ * \author Copyright (C) 2023 Martin Zmitko
*
* \license SPDX-License-Identifier: GPL-3.0-or-later
* \license SPDX-License-Identifier: MPL-2.0
@@ -70,51 +71,106 @@
* * (1) - replace values by white noise based on domain key
*/
function audioFarble(array){
- // PRNG function needs to depend on the original audio, so that the same
- // audio is farbled the same way but different audio is farbled differently
- // See https://pagure.io/JShelter/webextension/issue/23
- const MAXUINT32 = 4294967295;
- let crc = new CRC16();
- for (value of array) {
- crc.single(value * MAXUINT32);
+ if (wasm.ready && wasm.grow(array.byteLength)) {
+ try {
+ farbleAudioWASM();
+ } catch (e) {
+ console.error("WebAssembly optimized farbling failed, falling back to JavaScript implementation", e);
+ farbleAudioJS();
+ }
+ } else {
+ farbleAudioJS();
+ }
+
+ function farbleAudioWASM() {
+ const ARRAY_BYTE_LEN = array.byteLength;
+ wasm.set(array, 0, true);
+ const crc = wasm.crc16Float(ARRAY_BYTE_LEN);
+ const mash = new Mash();
+ mash.addData(' ');
+ mash.addData(domainHash);
+ mash.addData("AudioFarbling");
+ mash.addData(crc);
+ wasm.farbleFloats(ARRAY_BYTE_LEN, mash.n | 0);
+ array.set(wasm.get(array.length, 0, true));
}
- var thisaudio_prng = alea(domainHash, "AudioFarbling", crc.crc);
- for (i in array) {
- // Possible improvements:
- // Copy a neighbor data (possibly with modifications
- // Make bigger canges than xoring with 1
- array[i] *= 0.99 + thisaudio_prng() / 100;
+ function farbleAudioJS() {
+ // PRNG function needs to depend on the original audio, so that the same
+ // audio is farbled the same way but different audio is farbled differently
+ // See https://pagure.io/JShelter/webextension/issue/23
+ const ARRAY_LEN = array.length;
+ const MAXUINT32 = 4294967295;
+ let crc = new CRC16();
+ for (let i = 0; i < ARRAY_LEN; i++) {
+ crc.single(array[i] * MAXUINT32);
+ }
+ var thisaudio_prng = alea(domainHash, "AudioFarbling", crc.crc);
+
+ for (let i = 0; i < ARRAY_LEN; i++) {
+ // Possible improvements:
+ // Copy neighbor data (possibly with modifications)
+ array[i] *= 0.99 + thisaudio_prng() / 100;
+ }
}
};
+
function audioFarbleInt(array) {
- // PRNG function needs to depend on the original audio, so that the same
- // audio is farbled the same way but different audio is farbled differently
- // See https://pagure.io/JShelter/webextension/issue/23
- let crc = new CRC16();
- for (value of array) {
- crc.single(value);
+ const ARRAY_LEN = array.byteLength;
+ if (wasm.ready && wasm.grow(ARRAY_LEN)) {
+ try {
+ farbleAudioIntWASM();
+ } catch (e) {
+ console.error("WebAssembly optimized farbling failed, falling back to JavaScript implementation", e);
+ farbleAudioIntJS();
+ }
+ } else {
+ farbleAudioIntJS();
}
- var thisaudio_prng = alea(domainHash, "AudioFarbling", crc.crc);
- for (i in array) {
- if (thisaudio_prng.get_bits(1)) { // Modify data with probability of 0.5
- // Possible improvements:
- // Copy a neighbor data (possibly with modifications
- // Make bigger canges than xoring with 1
- array[i] ^= 1;
+ function farbleAudioIntWASM() {
+ wasm.set(array);
+ const crc = wasm.crc16(ARRAY_LEN);
+ const mash = new Mash();
+ mash.addData(' ');
+ mash.addData(domainHash);
+ mash.addData("AudioFarbling");
+ mash.addData(crc);
+ wasm.farbleBytes(ARRAY_LEN, mash.n | 0, false);
+ array.set(wasm.get(ARRAY_LEN));
+ }
+
+ function farbleAudioIntJS() {
+ // PRNG function needs to depend on the original audio, so that the same
+ // audio is farbled the same way but different audio is farbled differently
+ // See https://pagure.io/JShelter/webextension/issue/23
+ let crc = new CRC16();
+ for (let i = 0; i < ARRAY_LEN; i++) {
+ crc.single(array[i]);
+ }
+ var thisaudio_prng = alea(domainHash, "AudioFarbling", crc.crc);
+
+ for (let i = 0; i < ARRAY_LEN; i++) {
+ if (thisaudio_prng.get_bits(1)) { // Modify data with probability of 0.5
+ // Possible improvements:
+ // Copy neighbor data (possibly with modifications)
+ // Make bigger changes than xoring with 1
+ array[i] ^= 1;
+ }
}
}
}
function whiteNoiseInt(array) {
noise_prng = alea(Date.now(), prng());
- for (i in array) {
+ const ARRAY_LEN = array.length;
+ for (let i = 0; i < ARRAY_LEN; i++) {
array[i] = (noise_prng() * 256) | 0;
}
}
function whiteNoiseFloat(array) {
+ const ARRAY_LEN = array.length;
noise_prng = alea(Date.now(), prng());
- for (i in array) {
+ for (let i = 0; i < ARRAY_LEN; i++) {
array[i] = (noise_prng() * 2) -1;
}
}
@@ -124,7 +180,7 @@
* (0) - replace by white noise (range <0,0.1>) based on domain key
* (1) - multiply array by fudge factor based on domain key
*/
- var audioFarbleBody = strToUint + audioFarble;
+ var audioFarbleBody = audioFarble;
var wrappers = [
{
parent_object: "AudioBuffer.prototype",
@@ -135,7 +191,7 @@
wrapped_name: "origGetChannelData",
}
],
- helping_code: "var behaviour = args[0]; var modified = new Set();" + audioFarbleBody + whiteNoiseFloat,
+ helping_code: "var behaviour = args[0]; WrapHelper.shared['WEBA_gcd_pool'] = new Set(); WrapHelper.shared['WEBA_origGetChannelData'] = origGetChannelData;" + audioFarbleBody + whiteNoiseFloat,
original_function: "parent.AudioBuffer.prototype.getChannelData",
wrapping_function_args: "channel",
/** \fn fake AudioBuffer.prototype.getChannelData
@@ -146,7 +202,7 @@
*/
wrapping_function_body: `
var floatArr = origGetChannelData.call(this, channel);
- if (modified.has(floatArr)) {
+ if (WrapHelper.shared['WEBA_gcd_pool'].has(floatArr)) {
return floatArr;
}
if (behaviour == 0) {
@@ -155,9 +211,9 @@
else if (behaviour == 1) {
whiteNoiseFloat(floatArr);
}
- modified.add(floatArr);
+ WrapHelper.shared['WEBA_gcd_pool'].add(floatArr);
setTimeout(function() {
- modified.delete(floatArr);
+ WrapHelper.shared['WEBA_gcd_pool'].delete(floatArr);
}, 300000); // Remove the information after 5 minutes, this might need tweaking
return floatArr;
`,
@@ -171,7 +227,7 @@
wrapped_name: "origCopyFromChannel",
}
],
- helping_code: "var behaviour = args[0];" + audioFarbleBody + whiteNoiseFloat,
+ helping_code: "var behaviour = args[0]; WrapHelper.shared['WEBA_gcd_pool'] = new Set();" + audioFarbleBody + whiteNoiseFloat,
original_function: "parent.AudioBuffer.prototype.copyFromChannel",
wrapping_function_args: "destination, channel, start",
/** \fn fake AudioBuffer.prototype.copyFromChannel
@@ -181,13 +237,19 @@
* audioFarble with destination array as argument - which changes array values according to chosen level.
*/
wrapping_function_body: `
- if (behaviour == 0) {
- origCopyFromChannel.call(this, destination, channel, start);
- audioFarble(destination);
- }
- else if (behaviour == 1) {
+ if (behaviour == 1) {
whiteNoiseFloat(destination);
}
+ else if (behaviour == 0) {
+ var floatArr = WrapHelper.shared['WEBA_origGetChannelData'].call(this, channel);
+ origCopyFromChannel.call(this, destination, channel, start);
+ if (WrapHelper.shared['WEBA_gcd_pool'].has(floatArr)) {
+ // Already farbled, no additional farbling
+ }
+ else {
+ audioFarble(destination);
+ }
+ }
`,
},
{