re-merge libkeychain

This commit is contained in:
Vincent Breitmoser
2019-11-15 11:55:00 +01:00
parent d3e48db520
commit 864fbc95ea
37 changed files with 3 additions and 153 deletions

View File

@@ -0,0 +1,171 @@
/*
* Copyright (C) 2017 Dominik Schürmann <dominik@dominikschuermann.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.sufficientlysecure.keychain.util;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CoderResult;
import java.nio.charset.CodingErrorAction;
import android.content.ClipDescription;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
/** This class can be used to guess whether a stream of data is encoded in a given
* charset or not.
*
* An object of this class must be initialized with a byte[] buffer, which should
* be filled with data, then processed with {@link #readBytesFromBuffer}. This can
* be done any number of times. Once all data has been read, a final status can be
* read using the getter methods.
*/
public class CharsetVerifier {
private final ByteBuffer bufWrap;
private final CharBuffer dummyOutput;
private final CharsetDecoder charsetDecoder;
private boolean isFinished;
private boolean isFaulty;
private boolean isGuessed;
private boolean isPossibleTextMimeType;
private boolean isTextMimeType;
private String charset;
private String mimeType;
public CharsetVerifier(@NonNull byte[] buf, @NonNull String mimeType, @Nullable String charset) {
this.mimeType = mimeType;
isTextMimeType = ClipDescription.compareMimeTypes(mimeType, "text/*");
isPossibleTextMimeType = isTextMimeType
|| ClipDescription.compareMimeTypes(mimeType, "application/octet-stream")
|| ClipDescription.compareMimeTypes(mimeType, "application/x-download");
if (!isPossibleTextMimeType) {
charsetDecoder = null;
bufWrap = null;
dummyOutput = null;
return;
}
bufWrap = ByteBuffer.wrap(buf);
dummyOutput = CharBuffer.allocate(buf.length);
// the charset defaults to us-ascii, but we want to default to utf-8
if (charset == null || "us-ascii".equals(charset)) {
charset = "utf-8";
isGuessed = true;
} else {
isGuessed = false;
}
this.charset = charset;
charsetDecoder = Charset.forName(charset).newDecoder();
charsetDecoder.onMalformedInput(CodingErrorAction.REPORT);
charsetDecoder.onUnmappableCharacter(CodingErrorAction.REPORT);
charsetDecoder.reset();
}
public void readBytesFromBuffer(int pos, int len) {
if (isFinished) {
throw new IllegalStateException("cannot write again after reading charset status!");
}
if (isFaulty || bufWrap == null) {
return;
}
bufWrap.rewind();
bufWrap.position(pos);
bufWrap.limit(len);
dummyOutput.rewind();
CoderResult result = charsetDecoder.decode(bufWrap, dummyOutput, false);
if (result.isError()) {
isFaulty = true;
}
}
private void finishIfNecessary() {
if (isFinished || isFaulty || bufWrap == null) {
return;
}
isFinished = true;
bufWrap.rewind();
bufWrap.limit(0);
dummyOutput.rewind();
CoderResult result = charsetDecoder.decode(bufWrap, dummyOutput, true);
if (result.isError()) {
isFaulty = true;
}
}
public String getGuessedMimeType() {
if (isTextMimeType) {
return mimeType;
}
if (isProbablyText()) {
return "text/plain";
}
return mimeType;
}
public boolean isCharsetFaulty() {
finishIfNecessary();
return isFaulty;
}
public boolean isCharsetGuessed() {
finishIfNecessary();
return isGuessed;
}
public String getCharset() {
finishIfNecessary();
if (!isPossibleTextMimeType || (isGuessed && isFaulty)) {
return null;
}
return charset;
}
public String getMaybeFaultyCharset() {
return charset;
}
/** Returns true if the data which was read is definitely binary.
*
* This can happen when either the supplied mimeType indicated a non-ambiguous
* binary data type, or if we guessed a charset but got errors while decoding.
*/
public boolean isDefinitelyBinary() {
finishIfNecessary();
return !isTextMimeType && (!isPossibleTextMimeType || (isGuessed && isFaulty));
}
/** Returns true iff the data which was read is probably (or
* definitely) text.
*
* The corner case where isDefinitelyBinary returns false but isProbablyText
* returns true is where the charset was provided by the data (so is not
* guessed) but is still faulty.
*/
public boolean isProbablyText() {
finishIfNecessary();
return isTextMimeType || isPossibleTextMimeType && (!isGuessed || !isFaulty);
}
}

View File

@@ -0,0 +1,65 @@
/*
* Copyright (C) 2007 The Guava Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package org.sufficientlysecure.keychain.util;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import android.support.annotation.NonNull;
public final class CountingOutputStream extends FilterOutputStream {
private long count;
/**
* Wraps another output stream, counting the number of bytes written.
*
* @param out
* the output stream to be wrapped
*/
public CountingOutputStream(@NonNull OutputStream out) {
super(out);
}
/**
* Returns the number of bytes written.
*/
public long getCount() {
return count;
}
@Override
public void write(@NonNull byte[] b, int off, int len) throws IOException {
out.write(b, off, len);
count += len;
}
@Override
public void write(int b) throws IOException {
out.write(b);
count++;
}
// Overriding close() because FilterOutputStream's close() method pre-JDK8 has bad behavior:
// it silently ignores any exception thrown by flush(). Instead, just close the delegate stream.
// It should flush itself if necessary.
@Override
public void close() throws IOException {
out.close();
}
}

View File

@@ -52,7 +52,6 @@ import android.provider.OpenableColumns;
import android.support.v4.app.Fragment;
import android.widget.Toast;
import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.R;
import timber.log.Timber;

View File

@@ -0,0 +1,59 @@
/*
* Copyright (C) 2010-2014 Thialfihar <thi@thialfihar.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.sufficientlysecure.keychain.util;
import java.io.InputStream;
/**
* Wrapper to include size besides an InputStream
*/
public class InputData {
public static final int UNKNOWN_FILESIZE = -1;
private PositionAwareInputStream mInputStream;
private long mSize;
String mOriginalFilename;
public InputData(InputStream inputStream, long size, String originalFilename) {
mInputStream = new PositionAwareInputStream(inputStream);
mSize = size;
mOriginalFilename = originalFilename;
}
public InputData(InputStream inputStream, long size) {
mInputStream = new PositionAwareInputStream(inputStream);
mSize = size;
mOriginalFilename = "";
}
public InputStream getInputStream() {
return mInputStream;
}
public String getOriginalFilename () {
return mOriginalFilename;
}
public long getSize() {
return mSize;
}
public long getStreamPosition() {
return mInputStream.position();
}
}

View File

@@ -0,0 +1,40 @@
/*
* Copyright (C) 2010-2014 Thialfihar <thi@thialfihar.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.sufficientlysecure.keychain.util;
import java.util.ArrayList;
import java.util.Iterator;
public class IterableIterator<T> implements Iterable<T> {
private Iterator<T> mIter;
public IterableIterator(Iterator<T> iter, boolean failsafe) {
mIter = iter;
if (failsafe && mIter == null) {
// is there a better way?
mIter = new ArrayList<T>().iterator();
}
}
public IterableIterator(Iterator<T> iter) {
this(iter, false);
}
public Iterator<T> iterator() {
return mIter;
}
}

View File

@@ -0,0 +1,34 @@
/*
* Copyright (C) 2017 Dominik Schürmann <dominik@dominikschuermann.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.sufficientlysecure.keychain.util;
import java.util.Iterator;
/**
* An extended iterator interface, which knows the total number of its entries beforehand.
*/
public interface IteratorWithSize<E> extends Iterator<E> {
/**
* Returns the total number of entries in this iterator.
*
* @return the number of entries in this iterator.
*/
int getSize();
}

View File

@@ -12,7 +12,6 @@ package org.sufficientlysecure.keychain.util;
import android.os.Build;
import android.os.Process;
import android.util.Log;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;

View File

@@ -0,0 +1,84 @@
/*
* Copyright (C) 2010-2014 Thialfihar <thi@thialfihar.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.sufficientlysecure.keychain.util;
import java.io.IOException;
import java.io.InputStream;
public class PositionAwareInputStream extends InputStream {
private InputStream mStream;
private long mPosition;
public PositionAwareInputStream(InputStream in) {
mStream = in;
mPosition = 0;
}
@Override
public int read() throws IOException {
int ch = mStream.read();
++mPosition;
return ch;
}
@Override
public int available() throws IOException {
return mStream.available();
}
@Override
public void close() throws IOException {
mStream.close();
}
@Override
public boolean markSupported() {
return false;
}
@Override
public int read(byte[] b) throws IOException {
int result = mStream.read(b);
mPosition += result;
return result;
}
@Override
public int read(byte[] b, int offset, int length) throws IOException {
int result = mStream.read(b, offset, length);
mPosition += result;
return result;
}
@Override
public synchronized void reset() throws IOException {
mStream.reset();
mPosition = 0;
}
@Override
public long skip(long n) throws IOException {
long result = mStream.skip(n);
mPosition += result;
return result;
}
public long position() {
return mPosition;
}
}

View File

@@ -0,0 +1,56 @@
/*
* Copyright (C) 2014 Dominik Schürmann <dominik@dominikschuermann.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.sufficientlysecure.keychain.util;
import org.sufficientlysecure.keychain.Constants;
import java.nio.ByteBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CodingErrorAction;
import android.util.Log;
public class Utf8Util {
public static boolean isValidUTF8(byte[] input) {
CharsetDecoder cs = Charset.forName("UTF-8").newDecoder();
try {
cs.decode(ByteBuffer.wrap(input));
return true;
} catch (CharacterCodingException e) {
return false;
}
}
public static String fromUTF8ByteArrayReplaceBadEncoding(byte[] input) {
final CharsetDecoder charsetDecoder = Charset.forName("UTF-8").newDecoder();
charsetDecoder.onMalformedInput(CodingErrorAction.REPLACE);
charsetDecoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
try {
return charsetDecoder.decode(ByteBuffer.wrap(input)).toString();
} catch (CharacterCodingException e) {
Log.e(Constants.TAG, "Decoding failed!", e);
return charsetDecoder.replacement();
}
}
}