blob: 6ad6bc5a55351be35e595b5e8878485762f348eb [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
Chris Wrenf6a22ad2013-12-12 11:20:09 -050018import com.android.launcher3.backup.BackupProtos;
Chris Wren2b6c21d2013-10-02 14:16:04 -040019import com.android.launcher3.backup.BackupProtos.CheckedMessage;
20import com.android.launcher3.backup.BackupProtos.Favorite;
21import com.android.launcher3.backup.BackupProtos.Key;
Chris Wren92aa4232013-10-04 11:29:36 -040022import com.android.launcher3.backup.BackupProtos.Journal;
Chris Wren2b6c21d2013-10-02 14:16:04 -040023import com.android.launcher3.backup.BackupProtos.Resource;
24import com.android.launcher3.backup.BackupProtos.Screen;
25import com.android.launcher3.backup.BackupProtos.Widget;
26
27import com.google.protobuf.nano.InvalidProtocolBufferNanoException;
28import com.google.protobuf.nano.MessageNano;
29
30import java.io.BufferedInputStream;
31import java.io.ByteArrayOutputStream;
32import java.io.File;
33import java.io.FileInputStream;
34import java.io.FileNotFoundException;
35import java.io.FileOutputStream;
36import java.io.IOException;
37import java.lang.System;
38import java.util.zip.CRC32;
39
Chris Wrenf6a22ad2013-12-12 11:20:09 -050040import javax.xml.bind.DatatypeConverter;
41
42
Chris Wren2b6c21d2013-10-02 14:16:04 -040043/**
Chris Wrenf6a22ad2013-12-12 11:20:09 -050044 * Commandline utility for decoding Launcher3 backup protocol buffers.
Chris Wren2b6c21d2013-10-02 14:16:04 -040045 *
Chris Wrenf6a22ad2013-12-12 11:20:09 -050046 * <P>When using com.android.internal.backup.LocalTransport, the file names are base64-encoded Key
47 * protocol buffers with a prefix, that have been base64-encoded again by the transport:
48 * <pre>
49 * echo TDpDQUlnL0pxVTVnOD0= | base64 -D | dd bs=1 skip=2 | base64 -D | launcher_protoutil -k
50 * </pre>
Chris Wren2b6c21d2013-10-02 14:16:04 -040051 *
Chris Wrenf6a22ad2013-12-12 11:20:09 -050052 * <P>This tool understands these file names and will use the embedded Key to detect the type and
53 * extract the payload automatically:
54 * <pre>
55 * launcher_protoutil /tmp/TDpDQUlnL0pxVTVnOD0=
56 * </pre>
57 *
58 * <P>With payload debugging enabled, base64-encoded protocol buffers will be written to the logs.
59 * Copy the encoded log snippet into a file, and specify the type explicitly:
60 * <pre>
61 * base64 -D icon.log > icon.bin
62 * launcher_protoutil -i icon.bin
63 * </pre>
Chris Wren2b6c21d2013-10-02 14:16:04 -040064 */
65class DecoderRing {
Chris Wrenf6a22ad2013-12-12 11:20:09 -050066 private static Class[] TYPES = {
67 Key.class,
68 Favorite.class,
69 Screen.class,
70 Resource.class,
71 Widget.class
72 };
73 static final int ICON_TYPE_BITMAP = 1;
74
Chris Wren2b6c21d2013-10-02 14:16:04 -040075 public static void main(String[ ] args)
76 throws Exception {
77 File source = null;
Chris Wrenba2923a2013-12-10 15:48:47 -050078 Class type = null;
Chris Wrenf6a22ad2013-12-12 11:20:09 -050079 boolean extractImages = false;
Chris Wren92aa4232013-10-04 11:29:36 -040080 int skip = 0;
81
Chris Wren2b6c21d2013-10-02 14:16:04 -040082 for (int i = 0; i < args.length; i++) {
83 if ("-k".equals(args[i])) {
84 type = Key.class;
85 } else if ("-f".equals(args[i])) {
86 type = Favorite.class;
Chris Wren92aa4232013-10-04 11:29:36 -040087 } else if ("-j".equals(args[i])) {
88 type = Journal.class;
Chris Wren2b6c21d2013-10-02 14:16:04 -040089 } else if ("-i".equals(args[i])) {
90 type = Resource.class;
91 } else if ("-s".equals(args[i])) {
92 type = Screen.class;
93 } else if ("-w".equals(args[i])) {
94 type = Widget.class;
Chris Wren92aa4232013-10-04 11:29:36 -040095 } else if ("-S".equals(args[i])) {
96 if ((i + 1) < args.length) {
97 skip = Integer.valueOf(args[++i]);
98 } else {
99 usage(args);
100 }
Chris Wrenf6a22ad2013-12-12 11:20:09 -0500101 } else if ("-x".equals(args[i])) {
102 extractImages = true;
Chris Wren2b6c21d2013-10-02 14:16:04 -0400103 } else if (args[i] != null && !args[i].startsWith("-")) {
104 source = new File(args[i]);
105 } else {
106 System.err.println("Unsupported flag: " + args[i]);
107 usage(args);
108 }
109 }
110
Chris Wrenba2923a2013-12-10 15:48:47 -0500111 if (type == null) {
Chris Wrenf6a22ad2013-12-12 11:20:09 -0500112 if (source == null) {
113 usage(args);
114 } else {
115 Key key = new Key();
116 try {
117 byte[] rawKey = DatatypeConverter.parseBase64Binary(source.getName());
118 if (rawKey[0] != 'L' || rawKey[1] != ':') {
119 System.err.println("you must specify the payload type. " +
120 source.getName() + " is not a launcher backup key.");
121 System.exit(1);
122 }
123 String encodedPayload = new String(rawKey, 2, rawKey.length - 2);
124 byte[] keyProtoData = DatatypeConverter.parseBase64Binary(encodedPayload);
125 key = Key.parseFrom(keyProtoData);
126 } catch (InvalidProtocolBufferNanoException protoException) {
127 System.err.println("failed to extract key from filename: " + protoException);
128 System.exit(1);
129 } catch (IllegalArgumentException base64Exception) {
130 System.err.println("failed to extract key from filename: " + base64Exception);
131 System.exit(1);
132 }
133 // keys are self-checked
134 if (key.checksum != checkKey(key)) {
135 System.err.println("key ckecksum failed");
136 System.exit(1);
137 }
138 type = TYPES[key.type];
139 System.err.println("This is a " + type.getSimpleName() + " backup");
140 }
Chris Wrenba2923a2013-12-10 15:48:47 -0500141 }
Chris Wren2b6c21d2013-10-02 14:16:04 -0400142
143 // read in the bytes
144 ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
145 BufferedInputStream input = null;
146 if (source == null) {
147 input = new BufferedInputStream(System.in);
148 } else {
149 try {
150 input = new BufferedInputStream(new FileInputStream(source));
151 } catch (FileNotFoundException e) {
152 System.err.println("failed to open file: " + source + ", " + e);
153 System.exit(1);
154 }
155 }
156 byte[] buffer = new byte[1024];
157 try {
158 while (input.available() > 0) {
159 int n = input.read(buffer);
Chris Wren92aa4232013-10-04 11:29:36 -0400160 int offset = 0;
161 if (skip > 0) {
162 offset = Math.min(skip, n);
163 n -= offset;
164 skip -= offset;
165 }
Chris Wren2b6c21d2013-10-02 14:16:04 -0400166 if (n > 0) {
Chris Wren92aa4232013-10-04 11:29:36 -0400167 byteStream.write(buffer, offset, n);
Chris Wren2b6c21d2013-10-02 14:16:04 -0400168 }
169 }
170 } catch (IOException e) {
171 System.err.println("failed to read input: " + e);
172 System.exit(1);
173 }
Chris Wren2b6c21d2013-10-02 14:16:04 -0400174
175 MessageNano proto = null;
176 if (type == Key.class) {
177 Key key = new Key();
178 try {
Chris Wren978194c2013-10-03 17:47:22 -0400179 key = Key.parseFrom(byteStream.toByteArray());
Chris Wren2b6c21d2013-10-02 14:16:04 -0400180 } catch (InvalidProtocolBufferNanoException e) {
181 System.err.println("failed to parse proto: " + e);
182 System.exit(1);
183 }
184 // keys are self-checked
185 if (key.checksum != checkKey(key)) {
Chris Wrenf6a22ad2013-12-12 11:20:09 -0500186 System.err.println("key checksum failed");
Chris Wren2b6c21d2013-10-02 14:16:04 -0400187 System.exit(1);
188 }
189 proto = key;
190 } else {
191 // other types are wrapped in a checksum message
192 CheckedMessage wrapper = new CheckedMessage();
193 try {
194 MessageNano.mergeFrom(wrapper, byteStream.toByteArray());
195 } catch (InvalidProtocolBufferNanoException e) {
196 System.err.println("failed to parse wrapper: " + e);
197 System.exit(1);
198 }
199 CRC32 checksum = new CRC32();
200 checksum.update(wrapper.payload);
201 if (wrapper.checksum != checksum.getValue()) {
Chris Wrenf6a22ad2013-12-12 11:20:09 -0500202 System.err.println("wrapper checksum failed");
Chris Wren2b6c21d2013-10-02 14:16:04 -0400203 System.exit(1);
204 }
205 // decode the actual message
206 proto = (MessageNano) type.newInstance();
207 try {
208 MessageNano.mergeFrom(proto, wrapper.payload);
209 } catch (InvalidProtocolBufferNanoException e) {
210 System.err.println("failed to parse proto: " + e);
211 System.exit(1);
212 }
213 }
214
215 // Generic string output
216 System.out.println(proto.toString());
217
Chris Wrenf6a22ad2013-12-12 11:20:09 -0500218 if (extractImages) {
219 String prefix = "stdin";
220 if (source != null) {
221 prefix = source.getName();
Chris Wren2b6c21d2013-10-02 14:16:04 -0400222 }
Chris Wrenf6a22ad2013-12-12 11:20:09 -0500223 // save off the icon bits in a file for inspection
224 if (proto instanceof Resource) {
225 Resource icon = (Resource) proto;
226 writeImageData(icon.data, prefix + ".png");
227 }
228
229 // save off the icon bits in a file for inspection
230 if (proto instanceof Favorite) {
231 Favorite favorite = (Favorite) proto;
232 if (favorite.iconType == ICON_TYPE_BITMAP) {
233 writeImageData(favorite.icon, prefix + ".png");
234 }
235 }
236
237 // save off the widget icon and preview bits in files for inspection
238 if (proto instanceof Widget) {
239 Widget widget = (Widget) proto;
240 if (widget.icon != null) {
241 writeImageData(widget.icon.data, prefix + "_icon.png");
242 }
243 if (widget.preview != null) {
244 writeImageData(widget.preview.data, prefix + "_preview.png");
245 }
Chris Wren2b6c21d2013-10-02 14:16:04 -0400246 }
247 }
248
249 // success
250 System.exit(0);
251 }
252
Chris Wrenf6a22ad2013-12-12 11:20:09 -0500253 private static void writeImageData(byte[] data, String path) {
254 FileOutputStream iconFile = null;
255 try {
256 iconFile = new FileOutputStream(path);
257 iconFile.write(data);
258 System.err.println("wrote " + path);
259 } catch (IOException e) {
260 System.err.println("failed to write image file: " + e);
261 } finally {
262 if (iconFile != null) {
263 try {
264 iconFile.close();
265 } catch (IOException e) {
266 System.err.println("failed to close the image file: " + e);
267 }
268 }
269 }
270 }
271
Chris Wren2b6c21d2013-10-02 14:16:04 -0400272 private static long checkKey(Key key) {
273 CRC32 checksum = new CRC32();
274 checksum.update(key.type);
275 checksum.update((int) (key.id & 0xffff));
276 checksum.update((int) ((key.id >> 32) & 0xffff));
277 if (key.name != null && key.name.length() > 0) {
278 checksum.update(key.name.getBytes());
279 }
280 return checksum.getValue();
281 }
282
283 private static void usage(String[] args) {
Chris Wrenf6a22ad2013-12-12 11:20:09 -0500284 System.err.println("launcher_protoutil [-x] [-S b] [-k|-f|-i|-s|-w] [filename]");
Chris Wren2b6c21d2013-10-02 14:16:04 -0400285 System.err.println("\t-k\tdecode a key");
286 System.err.println("\t-f\tdecode a favorite");
287 System.err.println("\t-i\tdecode a icon");
288 System.err.println("\t-s\tdecode a screen");
289 System.err.println("\t-w\tdecode a widget");
Chris Wrenf6a22ad2013-12-12 11:20:09 -0500290 System.err.println("\t-S b\tskip b bytes");
291 System.err.println("\t-x\textract image data to files");
Chris Wren2b6c21d2013-10-02 14:16:04 -0400292 System.err.println("\tfilename\tread from filename, not stdin");
293 System.exit(1);
294 }
295}