Chris Wren | 2b6c21d | 2013-10-02 14:16:04 -0400 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package com.android.launcher3; |
| 17 | |
Chris Wren | f6a22ad | 2013-12-12 11:20:09 -0500 | [diff] [blame^] | 18 | import com.android.launcher3.backup.BackupProtos; |
Chris Wren | 2b6c21d | 2013-10-02 14:16:04 -0400 | [diff] [blame] | 19 | import com.android.launcher3.backup.BackupProtos.CheckedMessage; |
| 20 | import com.android.launcher3.backup.BackupProtos.Favorite; |
| 21 | import com.android.launcher3.backup.BackupProtos.Key; |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 22 | import com.android.launcher3.backup.BackupProtos.Journal; |
Chris Wren | 2b6c21d | 2013-10-02 14:16:04 -0400 | [diff] [blame] | 23 | import com.android.launcher3.backup.BackupProtos.Resource; |
| 24 | import com.android.launcher3.backup.BackupProtos.Screen; |
| 25 | import com.android.launcher3.backup.BackupProtos.Widget; |
| 26 | |
| 27 | import com.google.protobuf.nano.InvalidProtocolBufferNanoException; |
| 28 | import com.google.protobuf.nano.MessageNano; |
| 29 | |
| 30 | import java.io.BufferedInputStream; |
| 31 | import java.io.ByteArrayOutputStream; |
| 32 | import java.io.File; |
| 33 | import java.io.FileInputStream; |
| 34 | import java.io.FileNotFoundException; |
| 35 | import java.io.FileOutputStream; |
| 36 | import java.io.IOException; |
| 37 | import java.lang.System; |
| 38 | import java.util.zip.CRC32; |
| 39 | |
Chris Wren | f6a22ad | 2013-12-12 11:20:09 -0500 | [diff] [blame^] | 40 | import javax.xml.bind.DatatypeConverter; |
| 41 | |
| 42 | |
Chris Wren | 2b6c21d | 2013-10-02 14:16:04 -0400 | [diff] [blame] | 43 | /** |
Chris Wren | f6a22ad | 2013-12-12 11:20:09 -0500 | [diff] [blame^] | 44 | * Commandline utility for decoding Launcher3 backup protocol buffers. |
Chris Wren | 2b6c21d | 2013-10-02 14:16:04 -0400 | [diff] [blame] | 45 | * |
Chris Wren | f6a22ad | 2013-12-12 11:20:09 -0500 | [diff] [blame^] | 46 | * <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 Wren | 2b6c21d | 2013-10-02 14:16:04 -0400 | [diff] [blame] | 51 | * |
Chris Wren | f6a22ad | 2013-12-12 11:20:09 -0500 | [diff] [blame^] | 52 | * <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 Wren | 2b6c21d | 2013-10-02 14:16:04 -0400 | [diff] [blame] | 64 | */ |
| 65 | class DecoderRing { |
Chris Wren | f6a22ad | 2013-12-12 11:20:09 -0500 | [diff] [blame^] | 66 | 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 Wren | 2b6c21d | 2013-10-02 14:16:04 -0400 | [diff] [blame] | 75 | public static void main(String[ ] args) |
| 76 | throws Exception { |
| 77 | File source = null; |
Chris Wren | ba2923a | 2013-12-10 15:48:47 -0500 | [diff] [blame] | 78 | Class type = null; |
Chris Wren | f6a22ad | 2013-12-12 11:20:09 -0500 | [diff] [blame^] | 79 | boolean extractImages = false; |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 80 | int skip = 0; |
| 81 | |
Chris Wren | 2b6c21d | 2013-10-02 14:16:04 -0400 | [diff] [blame] | 82 | 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 Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 87 | } else if ("-j".equals(args[i])) { |
| 88 | type = Journal.class; |
Chris Wren | 2b6c21d | 2013-10-02 14:16:04 -0400 | [diff] [blame] | 89 | } 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 Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 95 | } 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 Wren | f6a22ad | 2013-12-12 11:20:09 -0500 | [diff] [blame^] | 101 | } else if ("-x".equals(args[i])) { |
| 102 | extractImages = true; |
Chris Wren | 2b6c21d | 2013-10-02 14:16:04 -0400 | [diff] [blame] | 103 | } 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 Wren | ba2923a | 2013-12-10 15:48:47 -0500 | [diff] [blame] | 111 | if (type == null) { |
Chris Wren | f6a22ad | 2013-12-12 11:20:09 -0500 | [diff] [blame^] | 112 | 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 Wren | ba2923a | 2013-12-10 15:48:47 -0500 | [diff] [blame] | 141 | } |
Chris Wren | 2b6c21d | 2013-10-02 14:16:04 -0400 | [diff] [blame] | 142 | |
| 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 Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 160 | int offset = 0; |
| 161 | if (skip > 0) { |
| 162 | offset = Math.min(skip, n); |
| 163 | n -= offset; |
| 164 | skip -= offset; |
| 165 | } |
Chris Wren | 2b6c21d | 2013-10-02 14:16:04 -0400 | [diff] [blame] | 166 | if (n > 0) { |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 167 | byteStream.write(buffer, offset, n); |
Chris Wren | 2b6c21d | 2013-10-02 14:16:04 -0400 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | } catch (IOException e) { |
| 171 | System.err.println("failed to read input: " + e); |
| 172 | System.exit(1); |
| 173 | } |
Chris Wren | 2b6c21d | 2013-10-02 14:16:04 -0400 | [diff] [blame] | 174 | |
| 175 | MessageNano proto = null; |
| 176 | if (type == Key.class) { |
| 177 | Key key = new Key(); |
| 178 | try { |
Chris Wren | 978194c | 2013-10-03 17:47:22 -0400 | [diff] [blame] | 179 | key = Key.parseFrom(byteStream.toByteArray()); |
Chris Wren | 2b6c21d | 2013-10-02 14:16:04 -0400 | [diff] [blame] | 180 | } 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 Wren | f6a22ad | 2013-12-12 11:20:09 -0500 | [diff] [blame^] | 186 | System.err.println("key checksum failed"); |
Chris Wren | 2b6c21d | 2013-10-02 14:16:04 -0400 | [diff] [blame] | 187 | 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 Wren | f6a22ad | 2013-12-12 11:20:09 -0500 | [diff] [blame^] | 202 | System.err.println("wrapper checksum failed"); |
Chris Wren | 2b6c21d | 2013-10-02 14:16:04 -0400 | [diff] [blame] | 203 | 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 Wren | f6a22ad | 2013-12-12 11:20:09 -0500 | [diff] [blame^] | 218 | if (extractImages) { |
| 219 | String prefix = "stdin"; |
| 220 | if (source != null) { |
| 221 | prefix = source.getName(); |
Chris Wren | 2b6c21d | 2013-10-02 14:16:04 -0400 | [diff] [blame] | 222 | } |
Chris Wren | f6a22ad | 2013-12-12 11:20:09 -0500 | [diff] [blame^] | 223 | // 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 Wren | 2b6c21d | 2013-10-02 14:16:04 -0400 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | |
| 249 | // success |
| 250 | System.exit(0); |
| 251 | } |
| 252 | |
Chris Wren | f6a22ad | 2013-12-12 11:20:09 -0500 | [diff] [blame^] | 253 | 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 Wren | 2b6c21d | 2013-10-02 14:16:04 -0400 | [diff] [blame] | 272 | 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 Wren | f6a22ad | 2013-12-12 11:20:09 -0500 | [diff] [blame^] | 284 | System.err.println("launcher_protoutil [-x] [-S b] [-k|-f|-i|-s|-w] [filename]"); |
Chris Wren | 2b6c21d | 2013-10-02 14:16:04 -0400 | [diff] [blame] | 285 | 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 Wren | f6a22ad | 2013-12-12 11:20:09 -0500 | [diff] [blame^] | 290 | System.err.println("\t-S b\tskip b bytes"); |
| 291 | System.err.println("\t-x\textract image data to files"); |
Chris Wren | 2b6c21d | 2013-10-02 14:16:04 -0400 | [diff] [blame] | 292 | System.err.println("\tfilename\tread from filename, not stdin"); |
| 293 | System.exit(1); |
| 294 | } |
| 295 | } |