DecoderRing.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. import com.android.launcher3.backup.BackupProtos.CheckedMessage;
  18. import com.android.launcher3.backup.BackupProtos.Favorite;
  19. import com.android.launcher3.backup.BackupProtos.Key;
  20. import com.android.launcher3.backup.BackupProtos.Journal;
  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. import com.google.protobuf.nano.InvalidProtocolBufferNanoException;
  25. import com.google.protobuf.nano.MessageNano;
  26. import java.io.BufferedInputStream;
  27. import java.io.ByteArrayOutputStream;
  28. import java.io.File;
  29. import java.io.FileInputStream;
  30. import java.io.FileNotFoundException;
  31. import java.io.FileOutputStream;
  32. import java.io.IOException;
  33. import java.lang.System;
  34. import java.util.LinkedList;
  35. import java.util.List;
  36. import java.util.zip.CRC32;
  37. import javax.xml.bind.DatatypeConverter;
  38. /**
  39. * Commandline utility for decoding Launcher3 backup protocol buffers.
  40. *
  41. * <P>When using com.android.internal.backup.LocalTransport, the file names are base64-encoded Key
  42. * protocol buffers with a prefix, that have been base64-encoded again by the transport:
  43. * <pre>
  44. * echo "TDpDQUlnL0pxVTVnOD0=" | launcher_protoutil -k
  45. * </pre>
  46. *
  47. * <P>This tool understands these file names and will use the embedded Key to detect the type and
  48. * extract the payload automatically:
  49. * <pre>
  50. * launcher_protoutil /tmp/TDpDQUlnL0pxVTVnOD0=
  51. * </pre>
  52. *
  53. * <P>With payload debugging enabled, base64-encoded protocol buffers will be written to the logs.
  54. * Copy the encoded snippet from the log, and specify the type explicitly, with the Logs flags:
  55. * <pre>
  56. * echo "CAEYLiCJ9JKsDw==" | launcher_protoutil -L -k
  57. * </pre>
  58. * For backup payloads it is more convenient to copy the log snippet to a file:
  59. * <pre>
  60. * launcher_protoutil -L -f favorite.log
  61. * </pre>
  62. */
  63. class DecoderRing {
  64. public static final String STANDARD_IN = "**stdin**";
  65. private static Class[] TYPES = {
  66. Key.class,
  67. Favorite.class,
  68. Screen.class,
  69. Resource.class,
  70. Widget.class
  71. };
  72. static final int ICON_TYPE_BITMAP = 1;
  73. public static void main(String[ ] args)
  74. throws Exception {
  75. Class defaultType = null;
  76. boolean extractImages = false;
  77. boolean fromLogs = false;
  78. int skip = 0;
  79. List<File> files = new LinkedList<File>();
  80. boolean verbose = false;
  81. for (int i = 0; i < args.length; i++) {
  82. if ("-k".equals(args[i])) {
  83. defaultType = Key.class;
  84. } else if ("-f".equals(args[i])) {
  85. defaultType = Favorite.class;
  86. } else if ("-j".equals(args[i])) {
  87. defaultType = Journal.class;
  88. } else if ("-i".equals(args[i])) {
  89. defaultType = Resource.class;
  90. } else if ("-s".equals(args[i])) {
  91. defaultType = Screen.class;
  92. } else if ("-w".equals(args[i])) {
  93. defaultType = Widget.class;
  94. } else if ("-S".equals(args[i])) {
  95. if ((i + 1) < args.length) {
  96. skip = Integer.valueOf(args[++i]);
  97. } else {
  98. usage(args);
  99. }
  100. } else if ("-x".equals(args[i])) {
  101. extractImages = true;
  102. } else if ("-v".equals(args[i])) {
  103. verbose = true;
  104. } else if ("-L".equals(args[i])) {
  105. fromLogs = true;
  106. } else if (args[i] != null && !args[i].startsWith("-")) {
  107. files.add(new File(args[i]));
  108. } else {
  109. System.err.println("Unsupported flag: " + args[i]);
  110. usage(args);
  111. }
  112. }
  113. if (defaultType == null && files.isEmpty()) {
  114. // can't infer file type without the key
  115. usage(args);
  116. }
  117. if (files.size() > 1 && defaultType != null) {
  118. System.err.println("Explicit type ignored for multiple files.");
  119. defaultType = null;
  120. }
  121. if (files.isEmpty()) {
  122. files.add(new File(STANDARD_IN));
  123. }
  124. for (File source : files) {
  125. Class type = null;
  126. if (defaultType == null) {
  127. Key key = decodeKey(source.getName().getBytes(), fromLogs);
  128. if (key != null) {
  129. type = TYPES[key.type];
  130. if (verbose) {
  131. System.err.println(source.getName() + " is a " + type.getSimpleName());
  132. System.out.println(key.toString());
  133. }
  134. }
  135. } else {
  136. type = defaultType;
  137. }
  138. // read in the bytes
  139. ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
  140. BufferedInputStream input = null;
  141. if (source.getName() == STANDARD_IN) {
  142. input = new BufferedInputStream(System.in);
  143. } else {
  144. try {
  145. input = new BufferedInputStream(new FileInputStream(source));
  146. } catch (FileNotFoundException e) {
  147. System.err.println("failed to open file: " + source + ", " + e);
  148. System.exit(1);
  149. }
  150. }
  151. byte[] buffer = new byte[1024];
  152. try {
  153. while (input.available() > 0) {
  154. int n = input.read(buffer);
  155. int offset = 0;
  156. if (skip > 0) {
  157. offset = Math.min(skip, n);
  158. n -= offset;
  159. skip -= offset;
  160. }
  161. if (n > 0) {
  162. byteStream.write(buffer, offset, n);
  163. }
  164. }
  165. } catch (IOException e) {
  166. System.err.println("failed to read input: " + e);
  167. System.exit(1);
  168. }
  169. MessageNano proto = null;
  170. byte[] payload = byteStream.toByteArray();
  171. if (type == Key.class) {
  172. proto = decodeKey(payload, fromLogs);
  173. } else if (type != null) {
  174. proto = decodeBackupData(payload, type, fromLogs);
  175. }
  176. // Generic string output
  177. if (proto != null) {
  178. System.out.println(proto.toString());
  179. }
  180. if (extractImages) {
  181. String prefix = "stdin";
  182. if (source != null) {
  183. prefix = source.getName();
  184. }
  185. // save off the icon bits in a file for inspection
  186. if (proto instanceof Resource) {
  187. Resource icon = (Resource) proto;
  188. writeImageData(icon.data, prefix + ".png");
  189. }
  190. // save off the icon bits in a file for inspection
  191. if (proto instanceof Favorite) {
  192. Favorite favorite = (Favorite) proto;
  193. if (favorite.iconType == ICON_TYPE_BITMAP) {
  194. writeImageData(favorite.icon, prefix + ".png");
  195. }
  196. }
  197. // save off the widget icon and preview bits in files for inspection
  198. if (proto instanceof Widget) {
  199. Widget widget = (Widget) proto;
  200. if (widget.icon != null) {
  201. writeImageData(widget.icon.data, prefix + "_icon.png");
  202. }
  203. }
  204. }
  205. }
  206. System.exit(0);
  207. }
  208. // In logcat, backup data is base64 encoded, but in localtransport files it is raw
  209. private static MessageNano decodeBackupData(byte[] payload, Class type, boolean fromLogs)
  210. throws InstantiationException, IllegalAccessException {
  211. MessageNano proto;// other types are wrapped in a checksum message
  212. CheckedMessage wrapper = new CheckedMessage();
  213. try {
  214. if (fromLogs) {
  215. payload = DatatypeConverter.parseBase64Binary(new String(payload));
  216. }
  217. MessageNano.mergeFrom(wrapper, payload);
  218. } catch (InvalidProtocolBufferNanoException e) {
  219. System.err.println("failed to parse wrapper: " + e);
  220. System.exit(1);
  221. }
  222. CRC32 checksum = new CRC32();
  223. checksum.update(wrapper.payload);
  224. if (wrapper.checksum != checksum.getValue()) {
  225. System.err.println("wrapper checksum failed");
  226. System.exit(1);
  227. }
  228. // decode the actual message
  229. proto = (MessageNano) type.newInstance();
  230. try {
  231. MessageNano.mergeFrom(proto, wrapper.payload);
  232. } catch (InvalidProtocolBufferNanoException e) {
  233. System.err.println("failed to parse proto: " + e);
  234. System.exit(1);
  235. }
  236. return proto;
  237. }
  238. // In logcat, keys are base64 encoded with no prefix.
  239. // The localtransport adds a prefix and the base64 encodes the whole thing again.
  240. private static Key decodeKey(byte[] payload, boolean fromLogs) {
  241. Key key = new Key();
  242. try {
  243. String encodedKey = new String(payload);
  244. if (!fromLogs) {
  245. byte[] rawKey = DatatypeConverter.parseBase64Binary(encodedKey);
  246. if (rawKey[0] != 'L' || rawKey[1] != ':') {
  247. System.err.println(encodedKey + " is not a launcher backup key.");
  248. return null;
  249. }
  250. encodedKey = new String(rawKey, 2, rawKey.length - 2);
  251. }
  252. byte[] keyProtoData = DatatypeConverter.parseBase64Binary(encodedKey);
  253. key = Key.parseFrom(keyProtoData);
  254. } catch (InvalidProtocolBufferNanoException protoException) {
  255. System.err.println("failed to extract key from filename: " + protoException);
  256. return null;
  257. } catch (IllegalArgumentException base64Exception) {
  258. System.err.println("failed to extract key from filename: " + base64Exception);
  259. return null;
  260. }
  261. // keys are self-checked
  262. if (key.checksum != checkKey(key)) {
  263. System.err.println("key ckecksum failed");
  264. return null;
  265. }
  266. return key;
  267. }
  268. private static void writeImageData(byte[] data, String path) {
  269. FileOutputStream iconFile = null;
  270. try {
  271. iconFile = new FileOutputStream(path);
  272. iconFile.write(data);
  273. System.err.println("wrote " + path);
  274. } catch (IOException e) {
  275. System.err.println("failed to write image file: " + e);
  276. } finally {
  277. if (iconFile != null) {
  278. try {
  279. iconFile.close();
  280. } catch (IOException e) {
  281. System.err.println("failed to close the image file: " + e);
  282. }
  283. }
  284. }
  285. }
  286. private static long checkKey(Key key) {
  287. CRC32 checksum = new CRC32();
  288. checksum.update(key.type);
  289. checksum.update((int) (key.id & 0xffff));
  290. checksum.update((int) ((key.id >> 32) & 0xffff));
  291. if (key.name != null && key.name.length() > 0) {
  292. checksum.update(key.name.getBytes());
  293. }
  294. return checksum.getValue();
  295. }
  296. private static void usage(String[] args) {
  297. System.err.println("launcher_protoutil [-x] [-S b] [-k|-f|-i|-s|-w] [filename]");
  298. System.err.println("\t-k\tdecode a key");
  299. System.err.println("\t-f\tdecode a favorite");
  300. System.err.println("\t-i\tdecode a icon");
  301. System.err.println("\t-s\tdecode a screen");
  302. System.err.println("\t-w\tdecode a widget");
  303. System.err.println("\t-S b\tskip b bytes");
  304. System.err.println("\t-x\textract image data to files");
  305. System.err.println("\t-v\tprint key type data, as well as payload");
  306. System.err.println("\t-l\texpect data from logcat, instead of the local transport");
  307. System.err.println("\tfilename\tread from filename, not stdin");
  308. System.exit(1);
  309. }
  310. }