Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
StevenArzt committed Nov 3, 2017
0 parents commit 2774b7c
Show file tree
Hide file tree
Showing 31 changed files with 2,823 additions and 0 deletions.
20 changes: 20 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="build/classes" path="src">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="build/classes"/>
</classpath>
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target/
23 changes: 23 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>axml</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
3 changes: 3 additions & 0 deletions .settings/org.eclipse.core.resources.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
eclipse.preferences.version=1
encoding/<project>=UTF-8
encoding/src=UTF-8
5 changes: 5 additions & 0 deletions .settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
org.eclipse.jdt.core.compiler.compliance=1.7
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=1.7
4 changes: 4 additions & 0 deletions .settings/org.eclipse.m2e.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
1 change: 1 addition & 0 deletions build/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/classes/
60 changes: 60 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>pxb.android</groupId>
<artifactId>axml</artifactId>
<name>axml</name>
<version>2.0.0-SNAPSHOT</version>
<description>The axml components for reading binary Android XML files</description>
<packaging>jar</packaging>

<properties>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<finalName>axml</finalName>
<sourceDirectory>src</sourceDirectory>
<outputDirectory>build/classes</outputDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.1</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.10.4</version>
<configuration>
<failOnError>false</failOnError>
</configuration>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
33 changes: 33 additions & 0 deletions src/pxb/android/ResConst.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (c) 2009-2013 Panxiaobo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package pxb.android;

public interface ResConst {
int RES_STRING_POOL_TYPE = 0x0001;
int RES_TABLE_TYPE = 0x0002;
int RES_TABLE_PACKAGE_TYPE = 0x0200;
int RES_TABLE_TYPE_SPEC_TYPE = 0x0202;
int RES_TABLE_TYPE_TYPE = 0x0201;

int RES_XML_TYPE = 0x0003;
int RES_XML_RESOURCE_MAP_TYPE = 0x0180;
int RES_XML_END_NAMESPACE_TYPE = 0x0101;
int RES_XML_END_ELEMENT_TYPE = 0x0103;
int RES_XML_START_NAMESPACE_TYPE = 0x0100;
int RES_XML_START_ELEMENT_TYPE = 0x0102;
int RES_XML_CDATA_TYPE = 0x0104;

}
61 changes: 61 additions & 0 deletions src/pxb/android/StringItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright (c) 2009-2013 Panxiaobo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package pxb.android;

public class StringItem {
public String data;
public int dataOffset;
public int index;

public StringItem() {
super();
}

public StringItem(String data) {
super();
this.data = data;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
StringItem other = (StringItem) obj;
if (data == null) {
if (other.data != null)
return false;
} else if (!data.equals(other.data))
return false;
return true;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((data == null) ? 0 : data.hashCode());
return result;
}

public String toString() {
return String.format("S%04d %s", index, data);
}

}
163 changes: 163 additions & 0 deletions src/pxb/android/StringItems.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/*
* Copyright (c) 2009-2013 Panxiaobo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package pxb.android;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@SuppressWarnings("serial")
public class StringItems extends ArrayList<StringItem> {
private static final int UTF8_FLAG = 0x00000100;


public static String[] read(ByteBuffer in) throws IOException {
int trunkOffset = in.position() - 8;
int stringCount = in.getInt();
int styleOffsetCount = in.getInt();
int flags = in.getInt();
int stringDataOffset = in.getInt();
int stylesOffset = in.getInt();
int offsets[] = new int[stringCount];
String strings[] = new String[stringCount];
for (int i = 0; i < stringCount; i++) {
offsets[i] = in.getInt();
}

int base = trunkOffset + stringDataOffset;
for (int i = 0; i < offsets.length; i++) {
in.position(base + offsets[i]);
String s;

if (0 != (flags & UTF8_FLAG)) {
u8length(in); // ignored
int u8len = u8length(in);
int start = in.position();
int blength = u8len;
while (in.get(start + blength) != 0) {
blength++;
}
s = new String(in.array(), start, blength, "UTF-8");
} else {
int length = u16length(in);
s = new String(in.array(), in.position(), length * 2, "UTF-16LE");
}
strings[i] = s;
}
return strings;
}

static int u16length(ByteBuffer in) {
int length = in.getShort() & 0xFFFF;
if (length > 0x7FFF) {
length = ((length & 0x7FFF) << 8) | (in.getShort() & 0xFFFF);
}
return length;
}

static int u8length(ByteBuffer in) {
int len = in.get() & 0xFF;
if ((len & 0x80) != 0) {
len = ((len & 0x7F) << 8) | (in.get() & 0xFF);
}
return len;
}

byte[] stringData;

public int getSize() {
return 5 * 4 + this.size() * 4 + stringData.length + 0;// TODO
}

public void prepare() throws IOException {
for (StringItem s : this) {
if (s.data.length() > 0x7FFF) {
useUTF8 = false;
}
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int i = 0;
int offset = 0;
baos.reset();
Map<String, Integer> map = new HashMap<String, Integer>();
for (StringItem item : this) {
item.index = i++;
String stringData = item.data;
Integer of = map.get(stringData);
if (of != null) {
item.dataOffset = of;
} else {
item.dataOffset = offset;
map.put(stringData, offset);
if (useUTF8) {
int length = stringData.length();
byte[] data = stringData.getBytes("UTF-8");
int u8lenght = data.length;

if (length > 0x7F) {
offset++;
baos.write((length >> 8) | 0x80);
}
baos.write(length);

if (u8lenght > 0x7F) {
offset++;
baos.write((u8lenght >> 8) | 0x80);
}
baos.write(u8lenght);
baos.write(data);
baos.write(0);
offset += 3 + u8lenght;
} else {
int length = stringData.length();
byte[] data = stringData.getBytes("UTF-16LE");
if (length > 0x7FFF) {
int x = (length >> 16) | 0x8000;
baos.write(x);
baos.write(x >> 8);
offset += 2;
}
baos.write(length);
baos.write(length >> 8);
baos.write(data);
baos.write(0);
baos.write(0);
offset += 4 + data.length;
}
}
}
// TODO
stringData = baos.toByteArray();
}

private boolean useUTF8 = true;

public void write(ByteBuffer out) throws IOException {
out.putInt(this.size());
out.putInt(0);// TODO style count
out.putInt(useUTF8 ? UTF8_FLAG : 0);
out.putInt(7 * 4 + this.size() * 4);
out.putInt(0);
for (StringItem item : this) {
out.putInt(item.dataOffset);
}
out.put(stringData);
// TODO
}
}
Loading

0 comments on commit 2774b7c

Please sign in to comment.