<commit_before>// Decryption module. Loaded in window.onload()
// Compatible with Google Chrome
// Compatible with Mozilla Firefox, Opera thanks to idb.filesystem.js by <NAME>
var Decryption = (function() {
	// Private
	var chunkSize = 1024 * 1024; // Size of one chunk in B
	var CEK = 'password'; // for tests
	var target = 'User';

	var smallQuota = 1024*1024;
	var largeQuota = 1024*1024*1024*100;

	var errorHandler = function(e) {
		console.log("Error");
		console.log(e);
	};

	var time, time_chunk;

	// API
	window.requestFileSystem  = window.requestFileSystem || window.webkitRequestFileSystem;

	// Constructor
	function Decryption(fname, f_id) {
		this.folder_id = f_id;
		this.filename = fname;
		this.nb_chk = 0;
		this.enc;
		this.getNbChunks();
	}

	// Public
	Decryption.prototype.fromBitArrayCodec = function(arr) {
		/** Convert from a bitArray to an array of bytes. */
		var out = [], bl = sjcl.bitArray.bitLength(arr), i, tmp;
		for (i=0; i<bl/8; i++) {
			if ((i&3) === 0)
				tmp = arr[i/4];
			out.push(tmp >>> 24);
			tmp <<= 8;
		}
		return out;
	};

	Decryption.prototype.getNbChunks = function() {
		var me = this;

		time = new Time();
		time.start();
		if(this.filename.length > 0) {
			var xhr = new XMLHttpRequest();
			xhr.open("POST", target+'/getNbChunks', true);
			xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

			xhr.onreadystatechange = function() {
				if(xhr.status == 200 && xhr.readyState == 4) {
					if(xhr.responseText > 0) {
						me.nb_chk = xhr.responseText;
						console.log("File splitted in "+me.nb_chk+" chunks");
						me.decryptChk(0);
					}
				}
			}
			xhr.send("filename="+this.filename+"&folder_id="+this.folder_id);
		}
	};

	Decryption.prototype.decryptChk = function(line) {
		var me = this;

		var chk;
		if(line === undefined)
			line = 0;

		console.log("Decrypting chunk "+(line+1));
		time_chunk = new Time();
		var xhr = new XMLHttpRequest();
		xhr.open("POST", target+'/getChunk', true);
		xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

		xhr.onreadystatechange = function() {
			if(xhr.status == 200 && xhr.readyState == 4) {
				if(xhr.responseText != '') {
					time_chunk.stop();
					console.log("Got chunk "+(line+1)+" contents in "+time_chunk.elapsed()+" ms");

					chk = decodeURIComponent(xhr.responseText);

					var split = chk.split(":");
					if (split.length !== 4) {
						throw new sjcl.exception.corrupt("Error :: Incomplete chunk!");
					}
					var c = sjcl.codec.base64.toBits(split[0]);
					var s = sjcl.codec.base64.toBits(split[1]);
					var a = sjcl.codec.base64.toBits(split[2]);
					var i = sjcl.codec.base64.toBits(split[3]);

					if(me.enc === undefined) {
						console.log("Generating key and derivating it");
						var key = sjcl.misc.pbkdf2(CEK, s, 2000, 256);
						me.enc = new sjcl.cipher.aes(key);
					}

					chk = sjcl.mode.gcm.decrypt(me.enc, c, i, a, 128);

					chk = me.fromBitArrayCodec(chk);
					chk = new Uint8Array(chk);

					// FileSystem API
					// We request large quota (100 GB) to avoid storage errors
					if(window.requestFileSystem === undefined) {
						console.log("Your web browser is currently not supported by Mui app");
					}
					else {
						window.requestFileSystem(
							window.PERSISTENT,
							largeQuota,
							function(fs) {<commit_msg>updated console.log<commit_after>// Decryption module. Loaded in window.onload()
// Compatible with Google Chrome
// Compatible with Mozilla Firefox, Opera thanks to idb.filesystem.js by <NAME>
var Decryption = (function() {
	// Private
	var chunkSize = 1024 * 1024; // Size of one chunk in B
	var CEK = 'password'; // for tests
	var target = 'User';

	var smallQuota = 1024*1024;
	var largeQuota = 1024*1024*1024*100;

	var errorHandler = function(e) {
		console.log("Error");
		console.log(e);
	};

	var time, time_chunk;

	// API
	window.requestFileSystem  = window.requestFileSystem || window.webkitRequestFileSystem;

	// Constructor
	function Decryption(fname, f_id) {
		this.folder_id = f_id;
		this.filename = fname;
		this.nb_chk = 0;
		this.enc;
		this.getNbChunks();
	}

	// Public
	Decryption.prototype.fromBitArrayCodec = function(arr) {
		/** Convert from a bitArray to an array of bytes. */
		var out = [], bl = sjcl.bitArray.bitLength(arr), i, tmp;
		for (i=0; i<bl/8; i++) {
			if ((i&3) === 0)
				tmp = arr[i/4];
			out.push(tmp >>> 24);
			tmp <<= 8;
		}
		return out;
	};

	Decryption.prototype.getNbChunks = function() {
		var me = this;

		time = new Time();
		time.start();
		if(this.filename.length > 0) {
			var xhr = new XMLHttpRequest();
			xhr.open("POST", target+'/getNbChunks', true);
			xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

			xhr.onreadystatechange = function() {
				if(xhr.status == 200 && xhr.readyState == 4) {
					if(xhr.responseText > 0) {
						me.nb_chk = xhr.responseText;
						console.log("File splitted in "+me.nb_chk+" chunks");
						me.decryptChk(0);
					}
				}
			}
			xhr.send("filename="+this.filename+"&folder_id="+this.folder_id);
		}
	};

	Decryption.prototype.decryptChk = function(line) {
		var me = this;

		var chk;
		if(line === undefined)
			line = 0;

		console.log("Decrypting chunk "+(line+1));
		time_chunk = new Time();
		var xhr = new XMLHttpRequest();
		xhr.open("POST", target+'/getChunk', true);
		xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

		xhr.onreadystatechange = function() {
			if(xhr.status == 200 && xhr.readyState == 4) {
				if(xhr.responseText != '') {
					time_chunk.stop();
					console.log("Got chunk "+(line+1)+" contents in "+time_chunk.elapsed()+" ms");

					chk = decodeURIComponent(xhr.responseText);

					var split = chk.split(":");
					if (split.length !== 4) {
						throw new sjcl.exception.corrupt("Error :: Incomplete chunk!");
					}
					var c = sjcl.codec.base64.toBits(split[0]);
					var s = sjcl.codec.base64.toBits(split[1]);
					var a = sjcl.codec.base64.toBits(split[2]);
					var i = sjcl.codec.base64.toBits(split[3]);

					if(me.enc === undefined) {
						console.log("Key derivation process...");
						var key = sjcl.misc.pbkdf2(CEK, s, 2000, 256);
						me.enc = new sjcl.cipher.aes(key);
					}

					chk = sjcl.mode.gcm.decrypt(me.enc, c, i, a, 128);

					chk = me.fromBitArrayCodec(chk);
					chk = new Uint8Array(chk);

					// FileSystem API
					// We request large quota (100 GB) to avoid storage errors
					if(window.requestFileSystem === undefined) {
						console.log("Your web browser is currently not supported by Mui app");
					}
					else {
						window.requestFileSystem(
							window.PERSISTENT,
							largeQuota,
							function(fs) {
{'fraction_non_alphanumeric': 0.1589720603615718, 'fraction_numerical': 0.02450321231137009, 'mean_word_length': 3.1067484662576685, 'pattern_counts': {'":': 2, '<': 11, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '811667', 'n_tokens_mistral': 2944, 'n_tokens_neox': 2368, 'n_words': 527}