From 260946009a039dc1c628578e6be5a69f6b33ed03 Mon Sep 17 00:00:00 2001 From: Thialfihar Date: Thu, 20 May 2010 22:03:41 +0000 Subject: [PATCH] properly import keys from streams that may contain multiple blocks of ASCII armour or binary data, also small adjustment in strings --- res/values-de/strings.xml | 6 +- res/values-ko/strings.xml | 6 +- res/values-ru/strings.xml | 6 +- res/values/strings.xml | 6 +- src/org/thialfihar/android/apg/Apg.java | 74 ++++++++++++------------- 5 files changed, 49 insertions(+), 49 deletions(-) diff --git a/res/values-de/strings.xml b/res/values-de/strings.xml index e43d5f3f3..aa6975b2e 100644 --- a/res/values-de/strings.xml +++ b/res/values-de/strings.xml @@ -166,9 +166,9 @@ Please specify which file to export to.\nWARNING! You are about to export SECRET keys.\nWARNING! File will be overwritten if it exists. Do you really want to delete the key '%s'?\nYou can't undo this! Do you really want to delete the SECRET key '%s'?\nYou can't undo this! - Succssfully added %s keys and updated %s keys." - Succssfully added %s keys. - Succssfully updated %s keys. + Succssfully added %s key(s) and updated %s key(s)." + Succssfully added %s key(s). + Succssfully updated %s key(s). No keys added or updated. Succssfully exported 1 key. Succssfully exported %s keys. diff --git a/res/values-ko/strings.xml b/res/values-ko/strings.xml index e43d5f3f3..aa6975b2e 100644 --- a/res/values-ko/strings.xml +++ b/res/values-ko/strings.xml @@ -166,9 +166,9 @@ Please specify which file to export to.\nWARNING! You are about to export SECRET keys.\nWARNING! File will be overwritten if it exists. Do you really want to delete the key '%s'?\nYou can't undo this! Do you really want to delete the SECRET key '%s'?\nYou can't undo this! - Succssfully added %s keys and updated %s keys." - Succssfully added %s keys. - Succssfully updated %s keys. + Succssfully added %s key(s) and updated %s key(s)." + Succssfully added %s key(s). + Succssfully updated %s key(s). No keys added or updated. Succssfully exported 1 key. Succssfully exported %s keys. diff --git a/res/values-ru/strings.xml b/res/values-ru/strings.xml index e43d5f3f3..aa6975b2e 100644 --- a/res/values-ru/strings.xml +++ b/res/values-ru/strings.xml @@ -166,9 +166,9 @@ Please specify which file to export to.\nWARNING! You are about to export SECRET keys.\nWARNING! File will be overwritten if it exists. Do you really want to delete the key '%s'?\nYou can't undo this! Do you really want to delete the SECRET key '%s'?\nYou can't undo this! - Succssfully added %s keys and updated %s keys." - Succssfully added %s keys. - Succssfully updated %s keys. + Succssfully added %s key(s) and updated %s key(s)." + Succssfully added %s key(s). + Succssfully updated %s key(s). No keys added or updated. Succssfully exported 1 key. Succssfully exported %s keys. diff --git a/res/values/strings.xml b/res/values/strings.xml index e43d5f3f3..aa6975b2e 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -166,9 +166,9 @@ Please specify which file to export to.\nWARNING! You are about to export SECRET keys.\nWARNING! File will be overwritten if it exists. Do you really want to delete the key '%s'?\nYou can't undo this! Do you really want to delete the SECRET key '%s'?\nYou can't undo this! - Succssfully added %s keys and updated %s keys." - Succssfully added %s keys. - Succssfully updated %s keys. + Succssfully added %s key(s) and updated %s key(s)." + Succssfully added %s key(s). + Succssfully updated %s key(s). No keys added or updated. Succssfully exported 1 key. Succssfully exported %s keys. diff --git a/src/org/thialfihar/android/apg/Apg.java b/src/org/thialfihar/android/apg/Apg.java index dee9fe94e..c9a163f94 100644 --- a/src/org/thialfihar/android/apg/Apg.java +++ b/src/org/thialfihar/android/apg/Apg.java @@ -691,7 +691,6 @@ public class Apg { ProgressDialogUpdater progress) throws GeneralException, FileNotFoundException, PGPException, IOException { Bundle returnData = new Bundle(); - PGPObjectFactory objectFactory = null; if (type == Id.type.secret_key) { progress.setProgress(R.string.progress_importingSecretKeys, 0, 100); @@ -704,47 +703,48 @@ public class Apg { } FileInputStream fileIn = new FileInputStream(filename); - InputStream in = PGPUtil.getDecoderStream(fileIn); - objectFactory = new PGPObjectFactory(in); - - Vector objects = new Vector(); - Object obj = objectFactory.nextObject(); - while (obj != null) { - objects.add(obj); - obj = objectFactory.nextObject(); - } - + long fileSize = new File(filename).length(); + PositionAwareInputStream progressIn = new PositionAwareInputStream(fileIn); + // need to have access to the bufferedInput, so we can reuse it for the possible + // PGPObject chunks after the first one, e.g. files with several consecutive ASCII + // armour blocks + BufferedInputStream bufferedInput = new BufferedInputStream(progressIn); int newKeys = 0; int oldKeys = 0; - for (int i = 0; i < objects.size(); ++i) { - progress.setProgress(i * 100 / objects.size(), 100); - obj = objects.get(i); - PGPPublicKeyRing publicKeyRing; - PGPSecretKeyRing secretKeyRing; - int retValue; - - if (type == Id.type.secret_key) { - if (!(obj instanceof PGPSecretKeyRing)) { - continue; - } - secretKeyRing = (PGPSecretKeyRing) obj; - retValue = saveKeyRing(context, secretKeyRing); - } else { - if (!(obj instanceof PGPPublicKeyRing)) { - continue; - } - publicKeyRing = (PGPPublicKeyRing) obj; - retValue = saveKeyRing(context, publicKeyRing); + while (true) { + InputStream in = PGPUtil.getDecoderStream(bufferedInput); + PGPObjectFactory objectFactory = new PGPObjectFactory(in); + Object obj = objectFactory.nextObject(); + // if the first is already a null object, then we can stop trying + if (obj == null) { + break; } + while (obj != null) { + PGPPublicKeyRing publicKeyRing; + PGPSecretKeyRing secretKeyRing; + // a return value that doesn't match any Id.return_value.* values, in case + // saveKeyRing is never called + int retValue = 2107; - if (retValue == Id.return_value.error) { - throw new GeneralException(context.getString(R.string.error_savingKeys)); - } + if (type == Id.type.secret_key && obj instanceof PGPSecretKeyRing) { + secretKeyRing = (PGPSecretKeyRing) obj; + retValue = saveKeyRing(context, secretKeyRing); + } else if (type == Id.type.public_key && obj instanceof PGPPublicKeyRing) { + publicKeyRing = (PGPPublicKeyRing) obj; + retValue = saveKeyRing(context, publicKeyRing); + } - if (retValue == Id.return_value.updated) { - ++oldKeys; - } else if (retValue == Id.return_value.ok) { - ++newKeys; + if (retValue == Id.return_value.error) { + throw new GeneralException(context.getString(R.string.error_savingKeys)); + } + + if (retValue == Id.return_value.updated) { + ++oldKeys; + } else if (retValue == Id.return_value.ok) { + ++newKeys; + } + progress.setProgress((int)(100 * progressIn.position() / fileSize), 100); + obj = objectFactory.nextObject(); } }