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 | |
| 18 | import com.android.launcher3.backup.BackupProtos.CheckedMessage; |
| 19 | import com.android.launcher3.backup.BackupProtos.Favorite; |
| 20 | import com.android.launcher3.backup.BackupProtos.Key; |
| 21 | import com.android.launcher3.backup.BackupProtos.Resource; |
| 22 | import com.android.launcher3.backup.BackupProtos.Screen; |
| 23 | import com.android.launcher3.backup.BackupProtos.Widget; |
| 24 | |
| 25 | import com.google.protobuf.nano.InvalidProtocolBufferNanoException; |
| 26 | import com.google.protobuf.nano.MessageNano; |
| 27 | |
| 28 | import java.io.BufferedInputStream; |
| 29 | import java.io.ByteArrayOutputStream; |
| 30 | import java.io.File; |
| 31 | import java.io.FileInputStream; |
| 32 | import java.io.FileNotFoundException; |
| 33 | import java.io.FileOutputStream; |
| 34 | import java.io.IOException; |
| 35 | import java.lang.System; |
| 36 | import java.util.zip.CRC32; |
| 37 | |
| 38 | /** |
| 39 | * Commandline utility for decoding protos written to the android logs during debugging. |
| 40 | * |
| 41 | * base64 -D icon.log > icon.bin |
| 42 | * java -classpath $ANDROID_HOST_OUT/framework/protoutil.jar:$ANDROID_HOST_OUT/../common/obj/JAVA_LIBRARIES/host-libprotobuf-java-2.3.0-nano_intermediates/javalib.jar \ |
| 43 | * com.android.launcher3.DecoderRing -i icon.bin |
| 44 | * |
| 45 | * TODO: write a wrapper to setup the classpath |
| 46 | */ |
| 47 | class DecoderRing { |
| 48 | public static void main(String[ ] args) |
| 49 | throws Exception { |
| 50 | File source = null; |
| 51 | Class type = Key.class; |
| 52 | for (int i = 0; i < args.length; i++) { |
| 53 | if ("-k".equals(args[i])) { |
| 54 | type = Key.class; |
| 55 | } else if ("-f".equals(args[i])) { |
| 56 | type = Favorite.class; |
| 57 | } else if ("-i".equals(args[i])) { |
| 58 | type = Resource.class; |
| 59 | } else if ("-s".equals(args[i])) { |
| 60 | type = Screen.class; |
| 61 | } else if ("-w".equals(args[i])) { |
| 62 | type = Widget.class; |
| 63 | } else if (args[i] != null && !args[i].startsWith("-")) { |
| 64 | source = new File(args[i]); |
| 65 | } else { |
| 66 | System.err.println("Unsupported flag: " + args[i]); |
| 67 | usage(args); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | |
| 72 | // read in the bytes |
| 73 | ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); |
| 74 | BufferedInputStream input = null; |
| 75 | if (source == null) { |
| 76 | input = new BufferedInputStream(System.in); |
| 77 | } else { |
| 78 | try { |
| 79 | input = new BufferedInputStream(new FileInputStream(source)); |
| 80 | } catch (FileNotFoundException e) { |
| 81 | System.err.println("failed to open file: " + source + ", " + e); |
| 82 | System.exit(1); |
| 83 | } |
| 84 | } |
| 85 | byte[] buffer = new byte[1024]; |
| 86 | try { |
| 87 | while (input.available() > 0) { |
| 88 | int n = input.read(buffer); |
| 89 | if (n > 0) { |
| 90 | byteStream.write(buffer, 0, n); |
| 91 | } |
| 92 | } |
| 93 | } catch (IOException e) { |
| 94 | System.err.println("failed to read input: " + e); |
| 95 | System.exit(1); |
| 96 | } |
| 97 | System.err.println("read this many bytes: " + byteStream.size()); |
| 98 | |
| 99 | MessageNano proto = null; |
| 100 | if (type == Key.class) { |
| 101 | Key key = new Key(); |
| 102 | try { |
| 103 | MessageNano.mergeFrom(key, byteStream.toByteArray()); |
| 104 | } catch (InvalidProtocolBufferNanoException e) { |
| 105 | System.err.println("failed to parse proto: " + e); |
| 106 | System.exit(1); |
| 107 | } |
| 108 | // keys are self-checked |
| 109 | if (key.checksum != checkKey(key)) { |
| 110 | System.err.println("key ckecksum failed"); |
| 111 | System.exit(1); |
| 112 | } |
| 113 | proto = key; |
| 114 | } else { |
| 115 | // other types are wrapped in a checksum message |
| 116 | CheckedMessage wrapper = new CheckedMessage(); |
| 117 | try { |
| 118 | MessageNano.mergeFrom(wrapper, byteStream.toByteArray()); |
| 119 | } catch (InvalidProtocolBufferNanoException e) { |
| 120 | System.err.println("failed to parse wrapper: " + e); |
| 121 | System.exit(1); |
| 122 | } |
| 123 | CRC32 checksum = new CRC32(); |
| 124 | checksum.update(wrapper.payload); |
| 125 | if (wrapper.checksum != checksum.getValue()) { |
| 126 | System.err.println("wrapper ckecksum failed"); |
| 127 | System.exit(1); |
| 128 | } |
| 129 | // decode the actual message |
| 130 | proto = (MessageNano) type.newInstance(); |
| 131 | try { |
| 132 | MessageNano.mergeFrom(proto, wrapper.payload); |
| 133 | } catch (InvalidProtocolBufferNanoException e) { |
| 134 | System.err.println("failed to parse proto: " + e); |
| 135 | System.exit(1); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | // Generic string output |
| 140 | System.out.println(proto.toString()); |
| 141 | |
| 142 | // save off the icon bits in a file for inspection |
| 143 | if (proto instanceof Resource) { |
| 144 | Resource icon = (Resource) proto; |
| 145 | final String path = "icon.webp"; |
| 146 | FileOutputStream iconFile = new FileOutputStream(path); |
| 147 | iconFile.write(icon.data); |
| 148 | iconFile.close(); |
| 149 | System.err.println("wrote " + path); |
| 150 | } |
| 151 | |
| 152 | // save off the widget icon and preview bits in files for inspection |
| 153 | if (proto instanceof Widget) { |
| 154 | Widget widget = (Widget) proto; |
| 155 | if (widget.icon != null) { |
| 156 | final String path = "widget_icon.webp"; |
| 157 | FileOutputStream iconFile = new FileOutputStream(path); |
| 158 | iconFile.write(widget.icon.data); |
| 159 | iconFile.close(); |
| 160 | System.err.println("wrote " + path); |
| 161 | } |
| 162 | if (widget.preview != null) { |
| 163 | final String path = "widget_preview.webp"; |
| 164 | FileOutputStream iconFile = new FileOutputStream(path); |
| 165 | iconFile.write(widget.preview.data); |
| 166 | iconFile.close(); |
| 167 | System.err.println("wrote " + path); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | // success |
| 172 | System.exit(0); |
| 173 | } |
| 174 | |
| 175 | private static long checkKey(Key key) { |
| 176 | CRC32 checksum = new CRC32(); |
| 177 | checksum.update(key.type); |
| 178 | checksum.update((int) (key.id & 0xffff)); |
| 179 | checksum.update((int) ((key.id >> 32) & 0xffff)); |
| 180 | if (key.name != null && key.name.length() > 0) { |
| 181 | checksum.update(key.name.getBytes()); |
| 182 | } |
| 183 | return checksum.getValue(); |
| 184 | } |
| 185 | |
| 186 | private static void usage(String[] args) { |
| 187 | System.err.println("DecoderRing type [input]"); |
| 188 | System.err.println("\t-k\tdecode a key"); |
| 189 | System.err.println("\t-f\tdecode a favorite"); |
| 190 | System.err.println("\t-i\tdecode a icon"); |
| 191 | System.err.println("\t-s\tdecode a screen"); |
| 192 | System.err.println("\t-w\tdecode a widget"); |
| 193 | System.err.println("\tfilename\tread from filename, not stdin"); |
| 194 | System.exit(1); |
| 195 | } |
| 196 | } |