Skip to content

Commit

Permalink
In-content search support for encrypted files, closes #1375 (#1388 by @…
Browse files Browse the repository at this point in the history
opensource21 authored Jul 27, 2021
1 parent e2dbb64 commit 83a53f7
Showing 2 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -72,7 +72,9 @@ private static AlertDialog.Builder buildDialog(final Activity activity, final At
opt.isOnlyFirstContentMatch = onlyFirstContentMatchCheckBox.isChecked();
opt.ignoredDirectories = appSettings.getFileSearchIgnorelist();
opt.maxSearchDepth = appSettings.getSearchMaxDepth();

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
opt.password = appSettings.getDefaultPassword();
}
appSettings.setSearchQueryRegexUsing(opt.isRegexQuery);
appSettings.setSearchQueryCaseSensitivity(opt.isCaseSensitiveQuery);
appSettings.setSearchInContent(opt.isSearchInContent);
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Build;
import android.support.design.widget.Snackbar;
import android.support.v4.util.Pair;
import android.view.View;
@@ -11,12 +12,17 @@
import net.gsantner.markor.R;
import net.gsantner.markor.format.TextFormat;
import net.gsantner.opoc.util.Callback;
import net.gsantner.opoc.util.FileUtils;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.ref.WeakReference;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
@@ -27,6 +33,8 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import other.de.stanetz.jpencconverter.JavaPasswordbasedCryption;

@SuppressWarnings("WeakerAccess")

public class SearchEngine {
@@ -60,6 +68,7 @@ public static class SearchOptions {
public List<String> ignoredDirectories;
public boolean isShowMatchPreview = true;
public boolean isShowResultOnCancel = true;
public char[] password = new char[0];
}

public static class FitFile {
@@ -393,7 +402,7 @@ private List<Pair<String, Integer>> getContentMatches(final File file, final boo
return ret;
}

try (final BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)))) {
try (final BufferedReader br = new BufferedReader(new InputStreamReader(getInputStream(file)))) {
int lineNumber = 0;
for (String line; (line = br.readLine()) != null; ) {
if (isCancelled() || _isCanceled) {
@@ -429,5 +438,19 @@ private boolean isIgnored(File directory) {
}
return false;
}

private InputStream getInputStream(File file) throws FileNotFoundException {
if (isEncryptedFile(file)) {
final byte[] encryptedContext = FileUtils.readCloseStreamWithSize(new FileInputStream(file), (int) file.length());
return new ByteArrayInputStream(JavaPasswordbasedCryption.getDecryptedText(encryptedContext, _config.password.clone()).getBytes(StandardCharsets.UTF_8));
} else {
return new FileInputStream(file);
}
}
}


private static boolean isEncryptedFile(File file) {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && file.getName().endsWith(JavaPasswordbasedCryption.DEFAULT_ENCRYPTION_EXTENSION);
}
}

0 comments on commit 83a53f7

Please sign in to comment.