re-merge libkeychain
This commit is contained in:
@@ -13,7 +13,6 @@ import android.text.method.LinkMovementMethod;
|
||||
import android.widget.TextView;
|
||||
|
||||
import org.sufficientlysecure.keychain.BuildConfig;
|
||||
import org.sufficientlysecure.keychain.Constants;
|
||||
import org.sufficientlysecure.keychain.KeychainApplication;
|
||||
import org.sufficientlysecure.keychain.R;
|
||||
import org.sufficientlysecure.keychain.ui.SettingsActivity;
|
||||
|
||||
@@ -36,9 +36,7 @@ import android.widget.Button;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import org.openintents.openpgp.util.OpenPgpUtils.UserId;
|
||||
import org.sufficientlysecure.keychain.R;
|
||||
import org.sufficientlysecure.keychain.model.SubKey.UnifiedKeyInfo;
|
||||
import org.sufficientlysecure.keychain.remote.ui.RequestKeyPermissionPresenter.RequestKeyPermissionMvpView;
|
||||
import org.sufficientlysecure.keychain.ui.dialog.CustomAlertDialogBuilder;
|
||||
import org.sufficientlysecure.keychain.ui.util.KeyInfoFormatter;
|
||||
|
||||
@@ -19,13 +19,10 @@ package org.sufficientlysecure.keychain.securitytoken;
|
||||
|
||||
import android.nfc.Tag;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.util.Log;
|
||||
|
||||
import org.bouncycastle.util.encoders.Hex;
|
||||
import org.sufficientlysecure.keychain.Constants;
|
||||
import org.sufficientlysecure.keychain.securitytoken.SecurityTokenInfo.TokenType;
|
||||
import org.sufficientlysecure.keychain.securitytoken.SecurityTokenInfo.TransportType;
|
||||
import org.sufficientlysecure.keychain.ui.base.BaseSecurityTokenActivity;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
|
||||
@@ -18,7 +18,6 @@
|
||||
package org.sufficientlysecure.keychain.service;
|
||||
|
||||
|
||||
import android.Manifest;
|
||||
import android.accounts.Account;
|
||||
import android.app.PendingIntent;
|
||||
import android.app.Service;
|
||||
|
||||
@@ -35,8 +35,6 @@ import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.CheckBox;
|
||||
import android.widget.CompoundButton;
|
||||
import android.widget.CompoundButton.OnCheckedChangeListener;
|
||||
import android.widget.TextView;
|
||||
|
||||
import org.sufficientlysecure.keychain.Constants;
|
||||
|
||||
@@ -18,10 +18,7 @@
|
||||
package org.sufficientlysecure.keychain.ui;
|
||||
|
||||
import android.arch.lifecycle.ViewModelProviders;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
|
||||
import org.sufficientlysecure.keychain.R;
|
||||
import org.sufficientlysecure.keychain.ui.base.BaseActivity;
|
||||
|
||||
@@ -32,7 +32,6 @@ import android.widget.TextView;
|
||||
|
||||
import org.sufficientlysecure.keychain.R;
|
||||
import org.sufficientlysecure.keychain.securitytoken.KeyFormat;
|
||||
import org.sufficientlysecure.keychain.securitytoken.SecurityTokenConnection;
|
||||
import org.sufficientlysecure.keychain.ui.CreateKeyActivity.FragAction;
|
||||
import org.sufficientlysecure.keychain.util.Choice;
|
||||
|
||||
|
||||
@@ -30,7 +30,6 @@ import android.widget.TextView;
|
||||
|
||||
import org.sufficientlysecure.keychain.Constants;
|
||||
import org.sufficientlysecure.keychain.R;
|
||||
import org.sufficientlysecure.keychain.securitytoken.SecurityTokenConnection;
|
||||
import org.sufficientlysecure.keychain.ui.CreateKeyActivity.FragAction;
|
||||
import org.sufficientlysecure.keychain.util.Passphrase;
|
||||
|
||||
|
||||
@@ -42,12 +42,8 @@ import org.sufficientlysecure.keychain.livedata.GenericLiveData;
|
||||
import org.sufficientlysecure.keychain.model.SubKey.UnifiedKeyInfo;
|
||||
import org.sufficientlysecure.keychain.ui.chips.EncryptRecipientChipsInput;
|
||||
import org.sufficientlysecure.keychain.ui.chips.EncryptRecipientChipsInput.EncryptRecipientChip;
|
||||
import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils;
|
||||
import org.sufficientlysecure.keychain.ui.util.Notify;
|
||||
import org.sufficientlysecure.keychain.ui.util.Notify.Style;
|
||||
import org.sufficientlysecure.keychain.ui.widget.KeySpinner;
|
||||
import org.sufficientlysecure.keychain.util.Passphrase;
|
||||
import timber.log.Timber;
|
||||
|
||||
|
||||
public class EncryptModeAsymmetricFragment extends EncryptModeFragment {
|
||||
|
||||
@@ -22,10 +22,8 @@ import java.io.IOException;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.FragmentTransaction;
|
||||
import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
|
||||
import org.apache.james.mime4j.util.MimeUtil;
|
||||
@@ -34,7 +32,6 @@ import org.sufficientlysecure.keychain.R;
|
||||
import org.sufficientlysecure.keychain.ui.util.Notify;
|
||||
import org.sufficientlysecure.keychain.ui.util.Notify.Style;
|
||||
import org.sufficientlysecure.keychain.util.FileHelper;
|
||||
import timber.log.Timber;
|
||||
|
||||
|
||||
public class EncryptTextActivity extends EncryptActivity {
|
||||
|
||||
@@ -27,13 +27,9 @@ import org.sufficientlysecure.keychain.ui.base.BaseActivity;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.security.KeyStore;
|
||||
import java.security.KeyStoreException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.cert.CertificateException;
|
||||
|
||||
|
||||
public class SettingsSmartPGPAuthoritiesActivity extends BaseActivity {
|
||||
|
||||
|
||||
@@ -7,7 +7,6 @@ import android.view.View;
|
||||
import android.widget.TextView;
|
||||
|
||||
import eu.davidea.flexibleadapter.FlexibleAdapter;
|
||||
import eu.davidea.flexibleadapter.items.AbstractHeaderItem;
|
||||
import eu.davidea.flexibleadapter.items.IFlexible;
|
||||
import eu.davidea.flexibleadapter.items.IHeader;
|
||||
import eu.davidea.viewholders.FlexibleViewHolder;
|
||||
|
||||
@@ -18,27 +18,20 @@
|
||||
package org.sufficientlysecure.keychain.ui.adapter;
|
||||
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Typeface;
|
||||
import android.text.format.DateFormat;
|
||||
import android.view.View;
|
||||
|
||||
import eu.davidea.flexibleadapter.FlexibleAdapter;
|
||||
import eu.davidea.flexibleadapter.items.AbstractFlexibleItem;
|
||||
import eu.davidea.flexibleadapter.items.IFlexible;
|
||||
import org.bouncycastle.bcpg.sig.KeyFlags;
|
||||
import org.sufficientlysecure.keychain.R;
|
||||
import org.sufficientlysecure.keychain.pgp.CanonicalizedSecretKey.SecretKeyType;
|
||||
import org.sufficientlysecure.keychain.service.SaveKeyringParcel.SubkeyAdd;
|
||||
import org.sufficientlysecure.keychain.ui.SubKeyItem.SubkeyViewHolder;
|
||||
import org.sufficientlysecure.keychain.ui.ViewKeyAdvSubkeysFragment;
|
||||
import org.sufficientlysecure.keychain.ui.ViewKeyAdvSubkeysFragment.SubkeyEditViewModel;
|
||||
import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils;
|
||||
|
||||
|
||||
public class SubkeyAddedItem extends AbstractFlexibleItem<SubkeyViewHolder> {
|
||||
|
||||
@@ -26,7 +26,6 @@ import android.support.v7.widget.RecyclerView.AdapterDataObserver;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
|
||||
import org.sufficientlysecure.keychain.R;
|
||||
import org.sufficientlysecure.keychain.ui.adapter.IdentityAdapter;
|
||||
|
||||
@@ -28,7 +28,6 @@ import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.graphics.Bitmap;
|
||||
import android.net.NetworkInfo;
|
||||
import android.net.Uri;
|
||||
import android.net.wifi.WifiManager;
|
||||
import android.os.Build.VERSION_CODES;
|
||||
import android.os.Bundle;
|
||||
|
||||
@@ -19,9 +19,6 @@ package org.sufficientlysecure.keychain.ui.util;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.Resources.Theme;
|
||||
import android.text.SpannableStringBuilder;
|
||||
import android.text.Spanned;
|
||||
import android.text.style.StrikethroughSpan;
|
||||
import android.util.TypedValue;
|
||||
|
||||
public class FormattingUtils {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user