<commit_before>var CordovaExif = (function () {

	var Exif, FileHandle, BinaryImage;

	FileHandle = {
		url: null,
		callback: null,
		getExif: false,

		setup: function(imageURI, callback, getExif) {
			FileHandle.url = imageURI;
			FileHandle.callback = callback;

			if(getExif){
				FileHandle.getExif = getExif;
			}

			window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, FileHandle.gotFS, FileHandle.fail);
		},

		readData: function(imageURI, callback){
			FileHandle.setup(imageURI, callback, true);
		},

		readBase64: function(imageURI, callback){
			FileHandle.setup(imageURI, callback, false);
		},

		gotFS: function(fileSystem) {
			window.resolveLocalFileSystemURL(FileHandle.url, FileHandle.gotFileEntry, FileHandle.fail);
		},

		gotFileEntry: function(fileEntry) {
			fileEntry.file(FileHandle.readFile, FileHandle.fail);
		},

		fail: function(error) {
			// error.code
		},

		readFile: function(file){
			var fileReader = new FileReader();
			fileReader.onload = FileHandle.readFileSuccess;
			fileReader.readAsBinaryString(file);
		},

		readFileSuccess: function(e){
			var binaryImage,
				binTarget = e.target.result;

			if(!FileHandle.getExif){
				FileHandle.callback(window.btoa(binTarget));
				return false;
			}

			binaryImage = new BinaryImage(binTarget);
			FileHandle.handleBinaryImage(binaryImage);
		},

		handleBinaryImage: function(binaryImage){
			var exifObject = Exif.find(binaryImage);
			FileHandle.callback(exifObject);
		}
	};

	Exif = {
		find: function(image){
			var marker,
				offset = 2,
				exifOffset;

			// Check if is a valid JPEG
			if (image.getByteAt(0) !== 0xFF || image.getByteAt(1) !== 0xD8) {
				return false;
			}

			while (offset < image.length) {
				if (image.getByteAt(offset) !== 0xFF) {
					return false;
				}

				marker = image.getByteAt(offset+1);

				// Check if is a EXIF marker
				if(marker === 225) {
					exifOffset = offset + 4;
					return Exif.read(image, exifOffset);
				}
			}
		},

		read: function(image, start){

			var tag,
				tags,
				gpsData,
				exifData,
				bigEndian,
				exifOffset = start + 6;


			// Check if is a valid EXIF data
			if (image.getStringAt(start, 4) !== 'Exif') {
				return false;
			}


			// May be 0x4949 or 0x4D4D to be valid
			if (image.getShortAt(exifOffset) === 0x4949) {
				bigEndian = false;
			}
			else if (image.getShortAt(exifOffset) === 0x4D4D) {
				bigEndian = true;
			}
			else {
				return false;
			}<commit_msg>Fixed logic of finding EXIF marker<commit_after>var CordovaExif = (function () {

	var Exif, FileHandle, BinaryImage;

	FileHandle = {
		url: null,
		callback: null,
		getExif: false,

		setup: function(imageURI, callback, getExif) {
			FileHandle.url = imageURI;
			FileHandle.callback = callback;

			if(getExif){
				FileHandle.getExif = getExif;
			}

			window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, FileHandle.gotFS, FileHandle.fail);
		},

		readData: function(imageURI, callback){
			FileHandle.setup(imageURI, callback, true);
		},

		readBase64: function(imageURI, callback){
			FileHandle.setup(imageURI, callback, false);
		},

		gotFS: function(fileSystem) {
			window.resolveLocalFileSystemURL(FileHandle.url, FileHandle.gotFileEntry, FileHandle.fail);
		},

		gotFileEntry: function(fileEntry) {
			fileEntry.file(FileHandle.readFile, FileHandle.fail);
		},

		fail: function(error) {
			// error.code
		},

		readFile: function(file){
			var fileReader = new FileReader();
			fileReader.onload = FileHandle.readFileSuccess;
			fileReader.readAsBinaryString(file);
		},

		readFileSuccess: function(e){
			var binaryImage,
				binTarget = e.target.result;

			if(!FileHandle.getExif){
				FileHandle.callback(window.btoa(binTarget));
				return false;
			}

			binaryImage = new BinaryImage(binTarget);
			FileHandle.handleBinaryImage(binaryImage);
		},

		handleBinaryImage: function(binaryImage){
			var exifObject = Exif.find(binaryImage);
			FileHandle.callback(exifObject);
		}
	};

	Exif = {
		find: function(image){
			// Check if is a valid JPEG
			if (image.getByteAt(0) !== 0xFF || image.getByteAt(1) !== 0xD8) return false;

			var offset = 2;
			while (offset < image.length) {
				if (image.getByteAt(offset) === 0xFF) {
					// Check if is a EXIF marker
					if (image.getByteAt(offset + 1) === 0xE1)
						return Exif.read(image, offset + 4);
					offset += 2;
				} else ++offset;
			}
			return false;
		},

		read: function(image, start){

			var tag,
				tags,
				gpsData,
				exifData,
				bigEndian,
				exifOffset = start + 6;


			// Check if is a valid EXIF data
			if (image.getStringAt(start, 4) !== 'Exif') {
				return false;
			}


			// May be 0x4949 or 0x4D4D to be valid
			if (image.getShortAt(exifOffset) === 0x4949) {
				bigEndian = false;
			}
			else if (image.getShortAt(exifOffset) === 0x4D4D) {
				bigEndian = true;
			}
			else {
				return false;
			}
{'fraction_non_alphanumeric': 0.12653562653562653, 'fraction_numerical': 0.013513513513513514, 'mean_word_length': 3.281332164767748, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3047100', 'n_tokens_mistral': 2101, 'n_tokens_neox': 1674, 'n_words': 353}