您的位置:首页 > 文旅 > 美景 > 安卓xml乱码/加密转换:abx2xml和xml2abx使用及源码介绍

安卓xml乱码/加密转换:abx2xml和xml2abx使用及源码介绍

2024/10/5 19:19:58 来源:https://blog.csdn.net/learnframework/article/details/141003310  浏览:    关键词:安卓xml乱码/加密转换:abx2xml和xml2abx使用及源码介绍

背景:

上一篇文章
android系统中data下的xml乱码无法查看问题剖析及解决方法
发布后,想要寻找一个可以直接把二进制xml和普通xml进行相互转换的,当时还写了相关的方案,但是当时没有找到现成的开源工具,后来经过相关粉丝提醒找到了和方法2一模一样思路的开源工具那就是abx2xml和xml2abx。

转化命令使用介绍abx2xml和xml2abx

转换前
在这里插入图片描述属于二进制乱码
使用命令转换,命令:

abx2xml ./system/users/0/appwidgets.xml ./system/users/0/appwidgets-read.xml

在这里插入图片描述转化后的./system/users/0/appwidgets-read.xml变成我们常见的普通xml
同样普通xml也可以转成二进制xml,转化命令

xml2abx  ./system/users/0/appwidgets-read.xml ./system/users/0/appwidgets-binary.xml

然后看看./system/users/0/appwidgets-binary.xml是不是变得二进制不可读了
在这里插入图片描述

abx2xml和xml2abx命令解析

abx2xml和xml2abx其实代码都是一样的,本质就是个sh脚本而已,运行的

#!/system/bin/sh
export CLASSPATH=/system/framework/abx.jar
exec app_process /system/bin com.android.commands.abx.Abx "$0" "$@"

可以看出本质上都是调用到了Abx这个java类,参数就是一个$0,这个代表命令本身,比如使用使用是:
abx2xml input.xml output.xml
那么这里的 0 就是 a b x 2 x m l ,后面 i n p u t . x m l o u t p u t . x m l 就是 0就是abx2xml,后面input.xml output.xml就是 0就是abx2xml,后面input.xmloutput.xml就是@

源码剖析Abx类

public class Abx {private static final String USAGE = "" +"usage: abx2xml [-i] input [output]\n" +"usage: xml2abx [-i] input [output]\n\n" +"Converts between human-readable XML and Android Binary XML.\n\n" +"When invoked with the '-i' argument, the output of a successful conversion\n" +"will overwrite the original input file. Input can be '-' to use stdin, and\n" +"output can be '-' to use stdout.\n";private static InputStream openInput(String arg) throws IOException {if ("-".equals(arg)) {return System.in;} else {return new FileInputStream(arg);}}private static OutputStream openOutput(String arg) throws IOException {if ("-".equals(arg)) {return System.out;} else {return new FileOutputStream(arg);}}private static void mainInternal(String[] args) {if (args.length < 2) {throw new IllegalArgumentException("Missing arguments");}final XmlPullParser in;final XmlSerializer out;if (args[0].endsWith("abx2xml")) {//这里根据传递近来参数看看是否要二进制xml转普通还是逆过来转in = Xml.newBinaryPullParser(); //二进制转普通,那么输入就是BinaryPullParser,输出就是普通的out = Xml.newSerializer();} else if (args[0].endsWith("xml2abx")) {in = Xml.newPullParser();//普通转二进制,那么输入就是普通的,输出就是newBinarySerializerout = Xml.newBinarySerializer();} else {throw new IllegalArgumentException("Unsupported conversion");}final boolean inPlace = "-i".equals(args[1]);final String inputArg = inPlace ? args[2] : args[1];final String outputArg = inPlace ? args[2] + ".tmp" : args[2];try (InputStream is = openInput(inputArg);OutputStream os = openOutput(outputArg)) {in.setInput(is, StandardCharsets.UTF_8.name());//输入设置对应的流InputStreamout.setOutput(os, StandardCharsets.UTF_8.name());//输出设置对应的流OutputStreamout.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);Xml.copy(in, out);//再调用 Xml.copy,重点就是在这out.flush();} catch (Exception e) {// Clean up failed output before throwingif (inPlace) {new File(outputArg).delete();}throw new IllegalStateException(e);}// Successful in-place conversion of a file requires a renameif (inPlace) {if (!new File(outputArg).renameTo(new File(inputArg))) {throw new IllegalStateException("Failed rename");}}}public static void main(String[] args) {try {mainInternal(args);System.exit(0);} catch (Exception e) {System.err.println(e.toString());System.err.println();System.err.println(USAGE);System.exit(1);}}
}

上面代码核心部分有注释,其实整体算比较简单,核心方法就剩下一个Xml.copy方法
XML的相关copy方法如下:

 /*** Copy the first XML document into the second document.* <p>* Implemented by reading all events from the given {@link XmlPullParser}* and writing them directly to the given {@link XmlSerializer}. This can be* useful for transparently converting between underlying wire protocols.** @hide*/public static void copy(@NonNull XmlPullParser in, @NonNull XmlSerializer out)throws XmlPullParserException, IOException {// Some parsers may have already consumed the event that starts the// document, so we manually emit that event here for consistencyif (in.getEventType() == XmlPullParser.START_DOCUMENT) {out.startDocument(in.getInputEncoding(), true);}while (true) {final int token = in.nextToken();//不断循环xml的内容节点等,简单说就是in读出什么就往out中写什么switch (token) {case XmlPullParser.START_DOCUMENT:out.startDocument(in.getInputEncoding(), true);break;case XmlPullParser.END_DOCUMENT:out.endDocument();return;case XmlPullParser.START_TAG:out.startTag(normalizeNamespace(in.getNamespace()), in.getName());for (int i = 0; i < in.getAttributeCount(); i++) {out.attribute(normalizeNamespace(in.getAttributeNamespace(i)),in.getAttributeName(i), in.getAttributeValue(i));}break;case XmlPullParser.END_TAG:out.endTag(normalizeNamespace(in.getNamespace()), in.getName());break;case XmlPullParser.TEXT:out.text(in.getText());break;case XmlPullParser.CDSECT:out.cdsect(in.getText());break;case XmlPullParser.ENTITY_REF:out.entityRef(in.getName());break;case XmlPullParser.IGNORABLE_WHITESPACE:out.ignorableWhitespace(in.getText());break;case XmlPullParser.PROCESSING_INSTRUCTION:out.processingInstruction(in.getText());break;case XmlPullParser.COMMENT:out.comment(in.getText());break;case XmlPullParser.DOCDECL:out.docdecl(in.getText());break;default:throw new IllegalStateException("Unknown token " + token);}}}

更多framework详细代码和资料参考如下链接

hal+perfetto+surfaceflinger
https://mp.weixin.qq.com/s/LbVLnu1udqExHVKxd74ILg
在这里插入图片描述

其他课程七件套专题:在这里插入图片描述
点击这里
https://mp.weixin.qq.com/s/Qv8zjgQ0CkalKmvi8tMGaw

视频试看:
https://www.bilibili.com/video/BV1wc41117L4/

更多framework假威风耗:androidframework007

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com