Merge "support multiple files and fix logcat smippets for launcher_protoutil" into jb-ub-now-kermit
This commit is contained in:
commit
5f85bb4977
1 file changed
+171
-136
@@ -15,7 +15,6 @@
|
|||||||
*/
|
*/
|
||||||
package com.android.launcher3;
|
package com.android.launcher3;
|
||||||
|
|
||||||
import com.android.launcher3.backup.BackupProtos;
|
|
||||||
import com.android.launcher3.backup.BackupProtos.CheckedMessage;
|
import com.android.launcher3.backup.BackupProtos.CheckedMessage;
|
||||||
import com.android.launcher3.backup.BackupProtos.Favorite;
|
import com.android.launcher3.backup.BackupProtos.Favorite;
|
||||||
import com.android.launcher3.backup.BackupProtos.Key;
|
import com.android.launcher3.backup.BackupProtos.Key;
|
||||||
@@ -35,6 +34,8 @@ import java.io.FileNotFoundException;
|
|||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.lang.System;
|
import java.lang.System;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
import java.util.zip.CRC32;
|
import java.util.zip.CRC32;
|
||||||
|
|
||||||
import javax.xml.bind.DatatypeConverter;
|
import javax.xml.bind.DatatypeConverter;
|
||||||
@@ -46,7 +47,7 @@ import javax.xml.bind.DatatypeConverter;
|
|||||||
* <P>When using com.android.internal.backup.LocalTransport, the file names are base64-encoded Key
|
* <P>When using com.android.internal.backup.LocalTransport, the file names are base64-encoded Key
|
||||||
* protocol buffers with a prefix, that have been base64-encoded again by the transport:
|
* protocol buffers with a prefix, that have been base64-encoded again by the transport:
|
||||||
* <pre>
|
* <pre>
|
||||||
* echo TDpDQUlnL0pxVTVnOD0= | base64 -D | dd bs=1 skip=2 | base64 -D | launcher_protoutil -k
|
* echo "TDpDQUlnL0pxVTVnOD0=" | launcher_protoutil -k
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
*
|
||||||
* <P>This tool understands these file names and will use the embedded Key to detect the type and
|
* <P>This tool understands these file names and will use the embedded Key to detect the type and
|
||||||
@@ -56,13 +57,19 @@ import javax.xml.bind.DatatypeConverter;
|
|||||||
* </pre>
|
* </pre>
|
||||||
*
|
*
|
||||||
* <P>With payload debugging enabled, base64-encoded protocol buffers will be written to the logs.
|
* <P>With payload debugging enabled, base64-encoded protocol buffers will be written to the logs.
|
||||||
* Copy the encoded log snippet into a file, and specify the type explicitly:
|
* Copy the encoded snippet from the log, and specify the type explicitly, with the Logs flags:
|
||||||
* <pre>
|
* <pre>
|
||||||
* base64 -D icon.log > icon.bin
|
* echo "CAEYLiCJ9JKsDw==" | launcher_protoutil -L -k
|
||||||
* launcher_protoutil -i icon.bin
|
* </pre>
|
||||||
|
* For backup payloads it is more convenient to copy the log snippet to a file:
|
||||||
|
* <pre>
|
||||||
|
* launcher_protoutil -L -f favorite.log
|
||||||
* </pre>
|
* </pre>
|
||||||
*/
|
*/
|
||||||
class DecoderRing {
|
class DecoderRing {
|
||||||
|
|
||||||
|
public static final String STANDARD_IN = "**stdin**";
|
||||||
|
|
||||||
private static Class[] TYPES = {
|
private static Class[] TYPES = {
|
||||||
Key.class,
|
Key.class,
|
||||||
Favorite.class,
|
Favorite.class,
|
||||||
@@ -74,24 +81,25 @@ class DecoderRing {
|
|||||||
|
|
||||||
public static void main(String[ ] args)
|
public static void main(String[ ] args)
|
||||||
throws Exception {
|
throws Exception {
|
||||||
File source = null;
|
Class defaultType = null;
|
||||||
Class type = null;
|
|
||||||
boolean extractImages = false;
|
boolean extractImages = false;
|
||||||
|
boolean fromLogs = false;
|
||||||
int skip = 0;
|
int skip = 0;
|
||||||
|
List<File> files = new LinkedList<File>();
|
||||||
|
|
||||||
for (int i = 0; i < args.length; i++) {
|
for (int i = 0; i < args.length; i++) {
|
||||||
if ("-k".equals(args[i])) {
|
if ("-k".equals(args[i])) {
|
||||||
type = Key.class;
|
defaultType = Key.class;
|
||||||
} else if ("-f".equals(args[i])) {
|
} else if ("-f".equals(args[i])) {
|
||||||
type = Favorite.class;
|
defaultType = Favorite.class;
|
||||||
} else if ("-j".equals(args[i])) {
|
} else if ("-j".equals(args[i])) {
|
||||||
type = Journal.class;
|
defaultType = Journal.class;
|
||||||
} else if ("-i".equals(args[i])) {
|
} else if ("-i".equals(args[i])) {
|
||||||
type = Resource.class;
|
defaultType = Resource.class;
|
||||||
} else if ("-s".equals(args[i])) {
|
} else if ("-s".equals(args[i])) {
|
||||||
type = Screen.class;
|
defaultType = Screen.class;
|
||||||
} else if ("-w".equals(args[i])) {
|
} else if ("-w".equals(args[i])) {
|
||||||
type = Widget.class;
|
defaultType = Widget.class;
|
||||||
} else if ("-S".equals(args[i])) {
|
} else if ("-S".equals(args[i])) {
|
||||||
if ((i + 1) < args.length) {
|
if ((i + 1) < args.length) {
|
||||||
skip = Integer.valueOf(args[++i]);
|
skip = Integer.valueOf(args[++i]);
|
||||||
@@ -100,154 +108,180 @@ class DecoderRing {
|
|||||||
}
|
}
|
||||||
} else if ("-x".equals(args[i])) {
|
} else if ("-x".equals(args[i])) {
|
||||||
extractImages = true;
|
extractImages = true;
|
||||||
|
} else if ("-L".equals(args[i])) {
|
||||||
|
fromLogs = true;
|
||||||
} else if (args[i] != null && !args[i].startsWith("-")) {
|
} else if (args[i] != null && !args[i].startsWith("-")) {
|
||||||
source = new File(args[i]);
|
files.add(new File(args[i]));
|
||||||
} else {
|
} else {
|
||||||
System.err.println("Unsupported flag: " + args[i]);
|
System.err.println("Unsupported flag: " + args[i]);
|
||||||
usage(args);
|
usage(args);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type == null) {
|
if (defaultType == null && files.isEmpty()) {
|
||||||
if (source == null) {
|
// can't infer file type without the key
|
||||||
usage(args);
|
usage(args);
|
||||||
} else {
|
|
||||||
Key key = new Key();
|
|
||||||
try {
|
|
||||||
byte[] rawKey = DatatypeConverter.parseBase64Binary(source.getName());
|
|
||||||
if (rawKey[0] != 'L' || rawKey[1] != ':') {
|
|
||||||
System.err.println("you must specify the payload type. " +
|
|
||||||
source.getName() + " is not a launcher backup key.");
|
|
||||||
System.exit(1);
|
|
||||||
}
|
|
||||||
String encodedPayload = new String(rawKey, 2, rawKey.length - 2);
|
|
||||||
byte[] keyProtoData = DatatypeConverter.parseBase64Binary(encodedPayload);
|
|
||||||
key = Key.parseFrom(keyProtoData);
|
|
||||||
} catch (InvalidProtocolBufferNanoException protoException) {
|
|
||||||
System.err.println("failed to extract key from filename: " + protoException);
|
|
||||||
System.exit(1);
|
|
||||||
} catch (IllegalArgumentException base64Exception) {
|
|
||||||
System.err.println("failed to extract key from filename: " + base64Exception);
|
|
||||||
System.exit(1);
|
|
||||||
}
|
|
||||||
// keys are self-checked
|
|
||||||
if (key.checksum != checkKey(key)) {
|
|
||||||
System.err.println("key ckecksum failed");
|
|
||||||
System.exit(1);
|
|
||||||
}
|
|
||||||
type = TYPES[key.type];
|
|
||||||
System.err.println("This is a " + type.getSimpleName() + " backup");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// read in the bytes
|
if (files.size() > 1) {
|
||||||
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
|
System.err.println("Explicit type ignored for multiple files.");
|
||||||
BufferedInputStream input = null;
|
defaultType = null;
|
||||||
if (source == null) {
|
}
|
||||||
input = new BufferedInputStream(System.in);
|
|
||||||
} else {
|
if (files.isEmpty()) {
|
||||||
|
files.add(new File(STANDARD_IN));
|
||||||
|
}
|
||||||
|
|
||||||
|
for (File source : files) {
|
||||||
|
Class type = null;
|
||||||
|
if (defaultType == null) {
|
||||||
|
Key key = decodeKey(source.getName().getBytes(), fromLogs);
|
||||||
|
type = TYPES[key.type];
|
||||||
|
System.err.println("This is a " + type.getSimpleName() + " backup");
|
||||||
|
} else {
|
||||||
|
type = defaultType;
|
||||||
|
}
|
||||||
|
|
||||||
|
// read in the bytes
|
||||||
|
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
|
||||||
|
BufferedInputStream input = null;
|
||||||
|
if (source.getName() == STANDARD_IN) {
|
||||||
|
input = new BufferedInputStream(System.in);
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
input = new BufferedInputStream(new FileInputStream(source));
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
System.err.println("failed to open file: " + source + ", " + e);
|
||||||
|
System.exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
byte[] buffer = new byte[1024];
|
||||||
try {
|
try {
|
||||||
input = new BufferedInputStream(new FileInputStream(source));
|
while (input.available() > 0) {
|
||||||
} catch (FileNotFoundException e) {
|
int n = input.read(buffer);
|
||||||
System.err.println("failed to open file: " + source + ", " + e);
|
int offset = 0;
|
||||||
|
if (skip > 0) {
|
||||||
|
offset = Math.min(skip, n);
|
||||||
|
n -= offset;
|
||||||
|
skip -= offset;
|
||||||
|
}
|
||||||
|
if (n > 0) {
|
||||||
|
byteStream.write(buffer, offset, n);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println("failed to read input: " + e);
|
||||||
System.exit(1);
|
System.exit(1);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
byte[] buffer = new byte[1024];
|
MessageNano proto = null;
|
||||||
try {
|
byte[] payload = byteStream.toByteArray();
|
||||||
while (input.available() > 0) {
|
if (type == Key.class) {
|
||||||
int n = input.read(buffer);
|
proto = decodeKey(payload, fromLogs);
|
||||||
int offset = 0;
|
} else {
|
||||||
if (skip > 0) {
|
proto = decodeBackupData(payload, type, fromLogs);
|
||||||
offset = Math.min(skip, n);
|
}
|
||||||
n -= offset;
|
|
||||||
skip -= offset;
|
// Generic string output
|
||||||
|
System.out.println(proto.toString());
|
||||||
|
|
||||||
|
if (extractImages) {
|
||||||
|
String prefix = "stdin";
|
||||||
|
if (source != null) {
|
||||||
|
prefix = source.getName();
|
||||||
}
|
}
|
||||||
if (n > 0) {
|
// save off the icon bits in a file for inspection
|
||||||
byteStream.write(buffer, offset, n);
|
if (proto instanceof Resource) {
|
||||||
|
Resource icon = (Resource) proto;
|
||||||
|
writeImageData(icon.data, prefix + ".png");
|
||||||
|
}
|
||||||
|
|
||||||
|
// save off the icon bits in a file for inspection
|
||||||
|
if (proto instanceof Favorite) {
|
||||||
|
Favorite favorite = (Favorite) proto;
|
||||||
|
if (favorite.iconType == ICON_TYPE_BITMAP) {
|
||||||
|
writeImageData(favorite.icon, prefix + ".png");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// save off the widget icon and preview bits in files for inspection
|
||||||
|
if (proto instanceof Widget) {
|
||||||
|
Widget widget = (Widget) proto;
|
||||||
|
if (widget.icon != null) {
|
||||||
|
writeImageData(widget.icon.data, prefix + "_icon.png");
|
||||||
|
}
|
||||||
|
if (widget.preview != null) {
|
||||||
|
writeImageData(widget.preview.data, prefix + "_preview.png");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
}
|
||||||
System.err.println("failed to read input: " + e);
|
System.exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// In logcat, backup data is base64 encoded, but in localtransport files it is raw
|
||||||
|
private static MessageNano decodeBackupData(byte[] payload, Class type, boolean fromLogs)
|
||||||
|
throws InstantiationException, IllegalAccessException {
|
||||||
|
MessageNano proto;// other types are wrapped in a checksum message
|
||||||
|
CheckedMessage wrapper = new CheckedMessage();
|
||||||
|
try {
|
||||||
|
if (fromLogs) {
|
||||||
|
payload = DatatypeConverter.parseBase64Binary(new String(payload));
|
||||||
|
}
|
||||||
|
MessageNano.mergeFrom(wrapper, payload);
|
||||||
|
} catch (InvalidProtocolBufferNanoException e) {
|
||||||
|
System.err.println("failed to parse wrapper: " + e);
|
||||||
System.exit(1);
|
System.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
MessageNano proto = null;
|
CRC32 checksum = new CRC32();
|
||||||
if (type == Key.class) {
|
checksum.update(wrapper.payload);
|
||||||
Key key = new Key();
|
if (wrapper.checksum != checksum.getValue()) {
|
||||||
try {
|
System.err.println("wrapper checksum failed");
|
||||||
key = Key.parseFrom(byteStream.toByteArray());
|
System.exit(1);
|
||||||
} catch (InvalidProtocolBufferNanoException e) {
|
|
||||||
System.err.println("failed to parse proto: " + e);
|
|
||||||
System.exit(1);
|
|
||||||
}
|
|
||||||
// keys are self-checked
|
|
||||||
if (key.checksum != checkKey(key)) {
|
|
||||||
System.err.println("key checksum failed");
|
|
||||||
System.exit(1);
|
|
||||||
}
|
|
||||||
proto = key;
|
|
||||||
} else {
|
|
||||||
// other types are wrapped in a checksum message
|
|
||||||
CheckedMessage wrapper = new CheckedMessage();
|
|
||||||
try {
|
|
||||||
MessageNano.mergeFrom(wrapper, byteStream.toByteArray());
|
|
||||||
} catch (InvalidProtocolBufferNanoException e) {
|
|
||||||
System.err.println("failed to parse wrapper: " + e);
|
|
||||||
System.exit(1);
|
|
||||||
}
|
|
||||||
CRC32 checksum = new CRC32();
|
|
||||||
checksum.update(wrapper.payload);
|
|
||||||
if (wrapper.checksum != checksum.getValue()) {
|
|
||||||
System.err.println("wrapper checksum failed");
|
|
||||||
System.exit(1);
|
|
||||||
}
|
|
||||||
// decode the actual message
|
|
||||||
proto = (MessageNano) type.newInstance();
|
|
||||||
try {
|
|
||||||
MessageNano.mergeFrom(proto, wrapper.payload);
|
|
||||||
} catch (InvalidProtocolBufferNanoException e) {
|
|
||||||
System.err.println("failed to parse proto: " + e);
|
|
||||||
System.exit(1);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generic string output
|
// decode the actual message
|
||||||
System.out.println(proto.toString());
|
proto = (MessageNano) type.newInstance();
|
||||||
|
try {
|
||||||
|
MessageNano.mergeFrom(proto, wrapper.payload);
|
||||||
|
} catch (InvalidProtocolBufferNanoException e) {
|
||||||
|
System.err.println("failed to parse proto: " + e);
|
||||||
|
System.exit(1);
|
||||||
|
}
|
||||||
|
return proto;
|
||||||
|
}
|
||||||
|
|
||||||
if (extractImages) {
|
// In logcat, keys are base64 encoded with no prefix.
|
||||||
String prefix = "stdin";
|
// The localtransport adds a prefix and the base64 encodes the whole thing again.
|
||||||
if (source != null) {
|
private static Key decodeKey(byte[] payload, boolean fromLogs) {
|
||||||
prefix = source.getName();
|
Key key = new Key();
|
||||||
}
|
try {
|
||||||
// save off the icon bits in a file for inspection
|
String encodedKey = new String(payload);
|
||||||
if (proto instanceof Resource) {
|
if (!fromLogs) {
|
||||||
Resource icon = (Resource) proto;
|
byte[] rawKey = DatatypeConverter.parseBase64Binary(encodedKey);
|
||||||
writeImageData(icon.data, prefix + ".png");
|
if (rawKey[0] != 'L' || rawKey[1] != ':') {
|
||||||
}
|
System.err.println(encodedKey + " is not a launcher backup key.");
|
||||||
|
System.exit(1);
|
||||||
// save off the icon bits in a file for inspection
|
|
||||||
if (proto instanceof Favorite) {
|
|
||||||
Favorite favorite = (Favorite) proto;
|
|
||||||
if (favorite.iconType == ICON_TYPE_BITMAP) {
|
|
||||||
writeImageData(favorite.icon, prefix + ".png");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// save off the widget icon and preview bits in files for inspection
|
|
||||||
if (proto instanceof Widget) {
|
|
||||||
Widget widget = (Widget) proto;
|
|
||||||
if (widget.icon != null) {
|
|
||||||
writeImageData(widget.icon.data, prefix + "_icon.png");
|
|
||||||
}
|
|
||||||
if (widget.preview != null) {
|
|
||||||
writeImageData(widget.preview.data, prefix + "_preview.png");
|
|
||||||
}
|
}
|
||||||
|
encodedKey = new String(rawKey, 2, rawKey.length - 2);
|
||||||
}
|
}
|
||||||
|
byte[] keyProtoData = DatatypeConverter.parseBase64Binary(encodedKey);
|
||||||
|
key = Key.parseFrom(keyProtoData);
|
||||||
|
} catch (InvalidProtocolBufferNanoException protoException) {
|
||||||
|
System.err.println("failed to extract key from filename: " + protoException);
|
||||||
|
System.exit(1);
|
||||||
|
} catch (IllegalArgumentException base64Exception) {
|
||||||
|
System.err.println("failed to extract key from filename: " + base64Exception);
|
||||||
|
System.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// success
|
// keys are self-checked
|
||||||
System.exit(0);
|
if (key.checksum != checkKey(key)) {
|
||||||
|
System.err.println("key ckecksum failed");
|
||||||
|
System.exit(1);
|
||||||
|
}
|
||||||
|
return key;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void writeImageData(byte[] data, String path) {
|
private static void writeImageData(byte[] data, String path) {
|
||||||
@@ -289,6 +323,7 @@ class DecoderRing {
|
|||||||
System.err.println("\t-w\tdecode a widget");
|
System.err.println("\t-w\tdecode a widget");
|
||||||
System.err.println("\t-S b\tskip b bytes");
|
System.err.println("\t-S b\tskip b bytes");
|
||||||
System.err.println("\t-x\textract image data to files");
|
System.err.println("\t-x\textract image data to files");
|
||||||
|
System.err.println("\t-l\texpect data from logcat, instead of the local transport");
|
||||||
System.err.println("\tfilename\tread from filename, not stdin");
|
System.err.println("\tfilename\tread from filename, not stdin");
|
||||||
System.exit(1);
|
System.exit(1);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user