blob: 7098e189dda62e5b89170fe519a94eb443367d63 [file] [log] [blame]
Chris Wren2b6c21d2013-10-02 14:16:04 -04001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.android.launcher3;
17
18import com.android.launcher3.backup.BackupProtos.CheckedMessage;
19import com.android.launcher3.backup.BackupProtos.Favorite;
20import com.android.launcher3.backup.BackupProtos.Key;
Chris Wren92aa4232013-10-04 11:29:36 -040021import com.android.launcher3.backup.BackupProtos.Journal;
Chris Wren2b6c21d2013-10-02 14:16:04 -040022import com.android.launcher3.backup.BackupProtos.Resource;
23import com.android.launcher3.backup.BackupProtos.Screen;
24import com.android.launcher3.backup.BackupProtos.Widget;
25
26import com.google.protobuf.nano.InvalidProtocolBufferNanoException;
27import com.google.protobuf.nano.MessageNano;
28
29import java.io.BufferedInputStream;
30import java.io.ByteArrayOutputStream;
31import java.io.File;
32import java.io.FileInputStream;
33import java.io.FileNotFoundException;
34import java.io.FileOutputStream;
35import java.io.IOException;
36import java.lang.System;
Chris Wren2fd31822013-12-18 14:11:40 -050037import java.util.LinkedList;
38import java.util.List;
Chris Wren2b6c21d2013-10-02 14:16:04 -040039import java.util.zip.CRC32;
40
Chris Wrenf6a22ad2013-12-12 11:20:09 -050041import javax.xml.bind.DatatypeConverter;
42
43
Chris Wren2b6c21d2013-10-02 14:16:04 -040044/**
Chris Wrenf6a22ad2013-12-12 11:20:09 -050045 * Commandline utility for decoding Launcher3 backup protocol buffers.
Chris Wren2b6c21d2013-10-02 14:16:04 -040046 *
Chris Wrenf6a22ad2013-12-12 11:20:09 -050047 * <P>When using com.android.internal.backup.LocalTransport, the file names are base64-encoded Key
48 * protocol buffers with a prefix, that have been base64-encoded again by the transport:
49 * <pre>
Chris Wren2fd31822013-12-18 14:11:40 -050050 * echo "TDpDQUlnL0pxVTVnOD0=" | launcher_protoutil -k
Chris Wrenf6a22ad2013-12-12 11:20:09 -050051 * </pre>
Chris Wren2b6c21d2013-10-02 14:16:04 -040052 *
Chris Wrenf6a22ad2013-12-12 11:20:09 -050053 * <P>This tool understands these file names and will use the embedded Key to detect the type and
54 * extract the payload automatically:
55 * <pre>
56 * launcher_protoutil /tmp/TDpDQUlnL0pxVTVnOD0=
57 * </pre>
58 *
59 * <P>With payload debugging enabled, base64-encoded protocol buffers will be written to the logs.
Chris Wren2fd31822013-12-18 14:11:40 -050060 * Copy the encoded snippet from the log, and specify the type explicitly, with the Logs flags:
Chris Wrenf6a22ad2013-12-12 11:20:09 -050061 * <pre>
Chris Wren2fd31822013-12-18 14:11:40 -050062 * echo "CAEYLiCJ9JKsDw==" | launcher_protoutil -L -k
63 * </pre>
64 * For backup payloads it is more convenient to copy the log snippet to a file:
65 * <pre>
66 * launcher_protoutil -L -f favorite.log
Chris Wrenf6a22ad2013-12-12 11:20:09 -050067 * </pre>
Chris Wren2b6c21d2013-10-02 14:16:04 -040068 */
69class DecoderRing {
Chris Wren2fd31822013-12-18 14:11:40 -050070
71 public static final String STANDARD_IN = "**stdin**";
72
Chris Wrenf6a22ad2013-12-12 11:20:09 -050073 private static Class[] TYPES = {
74 Key.class,
75 Favorite.class,
76 Screen.class,
77 Resource.class,
78 Widget.class
79 };
80 static final int ICON_TYPE_BITMAP = 1;
81
Chris Wren2b6c21d2013-10-02 14:16:04 -040082 public static void main(String[ ] args)
83 throws Exception {
Chris Wren2fd31822013-12-18 14:11:40 -050084 Class defaultType = null;
Chris Wrenf6a22ad2013-12-12 11:20:09 -050085 boolean extractImages = false;
Chris Wren2fd31822013-12-18 14:11:40 -050086 boolean fromLogs = false;
Chris Wren92aa4232013-10-04 11:29:36 -040087 int skip = 0;
Chris Wren2fd31822013-12-18 14:11:40 -050088 List<File> files = new LinkedList<File>();
Chris Wren92aa4232013-10-04 11:29:36 -040089
Chris Wren2b6c21d2013-10-02 14:16:04 -040090 for (int i = 0; i < args.length; i++) {
91 if ("-k".equals(args[i])) {
Chris Wren2fd31822013-12-18 14:11:40 -050092 defaultType = Key.class;
Chris Wren2b6c21d2013-10-02 14:16:04 -040093 } else if ("-f".equals(args[i])) {
Chris Wren2fd31822013-12-18 14:11:40 -050094 defaultType = Favorite.class;
Chris Wren92aa4232013-10-04 11:29:36 -040095 } else if ("-j".equals(args[i])) {
Chris Wren2fd31822013-12-18 14:11:40 -050096 defaultType = Journal.class;
Chris Wren2b6c21d2013-10-02 14:16:04 -040097 } else if ("-i".equals(args[i])) {
Chris Wren2fd31822013-12-18 14:11:40 -050098 defaultType = Resource.class;
Chris Wren2b6c21d2013-10-02 14:16:04 -040099 } else if ("-s".equals(args[i])) {
Chris Wren2fd31822013-12-18 14:11:40 -0500100 defaultType = Screen.class;
Chris Wren2b6c21d2013-10-02 14:16:04 -0400101 } else if ("-w".equals(args[i])) {
Chris Wren2fd31822013-12-18 14:11:40 -0500102 defaultType = Widget.class;
Chris Wren92aa4232013-10-04 11:29:36 -0400103 } else if ("-S".equals(args[i])) {
104 if ((i + 1) < args.length) {
105 skip = Integer.valueOf(args[++i]);
106 } else {
107 usage(args);
108 }
Chris Wrenf6a22ad2013-12-12 11:20:09 -0500109 } else if ("-x".equals(args[i])) {
110 extractImages = true;
Chris Wren2fd31822013-12-18 14:11:40 -0500111 } else if ("-L".equals(args[i])) {
112 fromLogs = true;
Chris Wren2b6c21d2013-10-02 14:16:04 -0400113 } else if (args[i] != null && !args[i].startsWith("-")) {
Chris Wren2fd31822013-12-18 14:11:40 -0500114 files.add(new File(args[i]));
Chris Wren2b6c21d2013-10-02 14:16:04 -0400115 } else {
116 System.err.println("Unsupported flag: " + args[i]);
117 usage(args);
118 }
119 }
120
Chris Wren2fd31822013-12-18 14:11:40 -0500121 if (defaultType == null && files.isEmpty()) {
122 // can't infer file type without the key
123 usage(args);
Chris Wrenba2923a2013-12-10 15:48:47 -0500124 }
Chris Wren2b6c21d2013-10-02 14:16:04 -0400125
Chris Wren2fd31822013-12-18 14:11:40 -0500126 if (files.size() > 1) {
127 System.err.println("Explicit type ignored for multiple files.");
128 defaultType = null;
129 }
130
131 if (files.isEmpty()) {
132 files.add(new File(STANDARD_IN));
133 }
134
135 for (File source : files) {
136 Class type = null;
137 if (defaultType == null) {
138 Key key = decodeKey(source.getName().getBytes(), fromLogs);
139 type = TYPES[key.type];
140 System.err.println("This is a " + type.getSimpleName() + " backup");
141 } else {
142 type = defaultType;
143 }
144
145 // read in the bytes
146 ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
147 BufferedInputStream input = null;
148 if (source.getName() == STANDARD_IN) {
149 input = new BufferedInputStream(System.in);
150 } else {
151 try {
152 input = new BufferedInputStream(new FileInputStream(source));
153 } catch (FileNotFoundException e) {
154 System.err.println("failed to open file: " + source + ", " + e);
155 System.exit(1);
156 }
157 }
158 byte[] buffer = new byte[1024];
Chris Wren2b6c21d2013-10-02 14:16:04 -0400159 try {
Chris Wren2fd31822013-12-18 14:11:40 -0500160 while (input.available() > 0) {
161 int n = input.read(buffer);
162 int offset = 0;
163 if (skip > 0) {
164 offset = Math.min(skip, n);
165 n -= offset;
166 skip -= offset;
167 }
168 if (n > 0) {
169 byteStream.write(buffer, offset, n);
170 }
171 }
172 } catch (IOException e) {
173 System.err.println("failed to read input: " + e);
Chris Wren2b6c21d2013-10-02 14:16:04 -0400174 System.exit(1);
175 }
Chris Wren2fd31822013-12-18 14:11:40 -0500176
177 MessageNano proto = null;
178 byte[] payload = byteStream.toByteArray();
179 if (type == Key.class) {
180 proto = decodeKey(payload, fromLogs);
181 } else {
182 proto = decodeBackupData(payload, type, fromLogs);
183 }
184
185 // Generic string output
186 System.out.println(proto.toString());
187
188 if (extractImages) {
189 String prefix = "stdin";
190 if (source != null) {
191 prefix = source.getName();
Chris Wren92aa4232013-10-04 11:29:36 -0400192 }
Chris Wren2fd31822013-12-18 14:11:40 -0500193 // save off the icon bits in a file for inspection
194 if (proto instanceof Resource) {
195 Resource icon = (Resource) proto;
196 writeImageData(icon.data, prefix + ".png");
197 }
198
199 // save off the icon bits in a file for inspection
200 if (proto instanceof Favorite) {
201 Favorite favorite = (Favorite) proto;
202 if (favorite.iconType == ICON_TYPE_BITMAP) {
203 writeImageData(favorite.icon, prefix + ".png");
204 }
205 }
206
207 // save off the widget icon and preview bits in files for inspection
208 if (proto instanceof Widget) {
209 Widget widget = (Widget) proto;
210 if (widget.icon != null) {
211 writeImageData(widget.icon.data, prefix + "_icon.png");
212 }
213 if (widget.preview != null) {
214 writeImageData(widget.preview.data, prefix + "_preview.png");
215 }
Chris Wren2b6c21d2013-10-02 14:16:04 -0400216 }
217 }
Chris Wren2fd31822013-12-18 14:11:40 -0500218 }
219 System.exit(0);
220 }
221
222 // In logcat, backup data is base64 encoded, but in localtransport files it is raw
223 private static MessageNano decodeBackupData(byte[] payload, Class type, boolean fromLogs)
224 throws InstantiationException, IllegalAccessException {
225 MessageNano proto;// other types are wrapped in a checksum message
226 CheckedMessage wrapper = new CheckedMessage();
227 try {
228 if (fromLogs) {
229 payload = DatatypeConverter.parseBase64Binary(new String(payload));
230 }
231 MessageNano.mergeFrom(wrapper, payload);
232 } catch (InvalidProtocolBufferNanoException e) {
233 System.err.println("failed to parse wrapper: " + e);
Chris Wren2b6c21d2013-10-02 14:16:04 -0400234 System.exit(1);
235 }
Chris Wren2b6c21d2013-10-02 14:16:04 -0400236
Chris Wren2fd31822013-12-18 14:11:40 -0500237 CRC32 checksum = new CRC32();
238 checksum.update(wrapper.payload);
239 if (wrapper.checksum != checksum.getValue()) {
240 System.err.println("wrapper checksum failed");
241 System.exit(1);
Chris Wren2b6c21d2013-10-02 14:16:04 -0400242 }
243
Chris Wren2fd31822013-12-18 14:11:40 -0500244 // decode the actual message
245 proto = (MessageNano) type.newInstance();
246 try {
247 MessageNano.mergeFrom(proto, wrapper.payload);
248 } catch (InvalidProtocolBufferNanoException e) {
249 System.err.println("failed to parse proto: " + e);
250 System.exit(1);
251 }
252 return proto;
253 }
Chris Wren2b6c21d2013-10-02 14:16:04 -0400254
Chris Wren2fd31822013-12-18 14:11:40 -0500255 // In logcat, keys are base64 encoded with no prefix.
256 // The localtransport adds a prefix and the base64 encodes the whole thing again.
257 private static Key decodeKey(byte[] payload, boolean fromLogs) {
258 Key key = new Key();
259 try {
260 String encodedKey = new String(payload);
261 if (!fromLogs) {
262 byte[] rawKey = DatatypeConverter.parseBase64Binary(encodedKey);
263 if (rawKey[0] != 'L' || rawKey[1] != ':') {
264 System.err.println(encodedKey + " is not a launcher backup key.");
265 System.exit(1);
Chris Wrenf6a22ad2013-12-12 11:20:09 -0500266 }
Chris Wren2fd31822013-12-18 14:11:40 -0500267 encodedKey = new String(rawKey, 2, rawKey.length - 2);
Chris Wrenf6a22ad2013-12-12 11:20:09 -0500268 }
Chris Wren2fd31822013-12-18 14:11:40 -0500269 byte[] keyProtoData = DatatypeConverter.parseBase64Binary(encodedKey);
270 key = Key.parseFrom(keyProtoData);
271 } catch (InvalidProtocolBufferNanoException protoException) {
272 System.err.println("failed to extract key from filename: " + protoException);
273 System.exit(1);
274 } catch (IllegalArgumentException base64Exception) {
275 System.err.println("failed to extract key from filename: " + base64Exception);
276 System.exit(1);
Chris Wren2b6c21d2013-10-02 14:16:04 -0400277 }
278
Chris Wren2fd31822013-12-18 14:11:40 -0500279 // keys are self-checked
280 if (key.checksum != checkKey(key)) {
281 System.err.println("key ckecksum failed");
282 System.exit(1);
283 }
284 return key;
Chris Wren2b6c21d2013-10-02 14:16:04 -0400285 }
286
Chris Wrenf6a22ad2013-12-12 11:20:09 -0500287 private static void writeImageData(byte[] data, String path) {
288 FileOutputStream iconFile = null;
289 try {
290 iconFile = new FileOutputStream(path);
291 iconFile.write(data);
292 System.err.println("wrote " + path);
293 } catch (IOException e) {
294 System.err.println("failed to write image file: " + e);
295 } finally {
296 if (iconFile != null) {
297 try {
298 iconFile.close();
299 } catch (IOException e) {
300 System.err.println("failed to close the image file: " + e);
301 }
302 }
303 }
304 }
305
Chris Wren2b6c21d2013-10-02 14:16:04 -0400306 private static long checkKey(Key key) {
307 CRC32 checksum = new CRC32();
308 checksum.update(key.type);
309 checksum.update((int) (key.id & 0xffff));
310 checksum.update((int) ((key.id >> 32) & 0xffff));
311 if (key.name != null && key.name.length() > 0) {
312 checksum.update(key.name.getBytes());
313 }
314 return checksum.getValue();
315 }
316
317 private static void usage(String[] args) {
Chris Wrenf6a22ad2013-12-12 11:20:09 -0500318 System.err.println("launcher_protoutil [-x] [-S b] [-k|-f|-i|-s|-w] [filename]");
Chris Wren2b6c21d2013-10-02 14:16:04 -0400319 System.err.println("\t-k\tdecode a key");
320 System.err.println("\t-f\tdecode a favorite");
321 System.err.println("\t-i\tdecode a icon");
322 System.err.println("\t-s\tdecode a screen");
323 System.err.println("\t-w\tdecode a widget");
Chris Wrenf6a22ad2013-12-12 11:20:09 -0500324 System.err.println("\t-S b\tskip b bytes");
325 System.err.println("\t-x\textract image data to files");
Chris Wren2fd31822013-12-18 14:11:40 -0500326 System.err.println("\t-l\texpect data from logcat, instead of the local transport");
Chris Wren2b6c21d2013-10-02 14:16:04 -0400327 System.err.println("\tfilename\tread from filename, not stdin");
328 System.exit(1);
329 }
330}