working on reverting bad text on manifest

This commit is contained in:
Connor Tumbleson 2012-08-08 09:06:14 -05:00
parent 361fdea59e
commit ca1e7a811e
5 changed files with 78 additions and 266 deletions

View File

@ -16,22 +16,19 @@
package brut.androlib.res.decoder;
import android.content.res.XmlResourceParser;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.Writer;
import org.xmlpull.v1.XmlPullParserException;
import android.util.TypedValue;
import brut.androlib.AndrolibException;
import brut.androlib.res.xml.ResXmlEncoders;
import brut.util.ExtDataInput;
import com.mindprod.ledatastream.LEDataInputStream;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.xmlpull.v1.XmlPullParserException;
/**
* @author Ryszard Wiśniewski <brut.alll@gmail.com>
@ -812,9 +809,7 @@ public class AXmlResourceParser implements XmlResourceParser {
}
int event = m_event;
if (event != START_DOCUMENT) {//keep m_lineNumber
resetEventInfo();
}
resetEventInfo();
while (true) {
if (m_decreaseDepth) {
@ -836,64 +831,52 @@ public class AXmlResourceParser implements XmlResourceParser {
chunkType = CHUNK_XML_START_TAG;
} else {
chunkType = m_reader.readInt();
if (chunkType == CHUNK_RESOURCEIDS) {
int chunkSize = m_reader.readInt();
if (chunkSize < 8 || (chunkSize % 4) != 0) {
throw new IOException("Invalid resource ids size (" + chunkSize + ").");
}
m_resourceIDs = m_reader.readIntArray(chunkSize / 4 - 2);
continue;
}
if (chunkType < CHUNK_XML_FIRST || chunkType > CHUNK_XML_LAST) {
throw new IOException("Invalid chunk type (" + chunkType + ").");
}
// Common header.
/*
* chunkSize
*/ m_reader.skipInt();
int lineNumber = m_reader.readInt();
/*
* 0xFFFFFFFF
*/ m_reader.skipInt();
// Fake START_DOCUMENT event.
if (chunkType == CHUNK_XML_START_TAG && event == -1) {
m_event = START_DOCUMENT;
m_lineNumber = lineNumber;
break;
}
if (chunkType == CHUNK_XML_START_NAMESPACE
|| chunkType == CHUNK_XML_END_NAMESPACE) {
if (chunkType == CHUNK_XML_START_NAMESPACE) {
int prefix = m_reader.readInt();
int uri = m_reader.readInt();
m_namespaces.push(prefix, uri);
} else {
/*
* prefix
*/ m_reader.skipInt();
/*
* uri
*/ m_reader.skipInt();
m_namespaces.pop();
}
continue;
}
m_lineNumber = lineNumber;
}
if (chunkType == CHUNK_RESOURCEIDS) {
int chunkSize = m_reader.readInt();
if (chunkSize < 8 || (chunkSize % 4) != 0) {
throw new IOException("Invalid resource ids size (" + chunkSize + ").");
}
m_resourceIDs = m_reader.readIntArray(chunkSize / 4 - 2);
continue;
}
if (chunkType < CHUNK_XML_FIRST || chunkType > CHUNK_XML_LAST) {
throw new IOException("Invalid chunk type (" + chunkType + ").");
}
// Fake START_DOCUMENT event.
if (chunkType == CHUNK_XML_START_TAG && event == -1) {
m_event = START_DOCUMENT;
break;
}
// Common header.
/*chunkSize*/ m_reader.skipInt();
int lineNumber = m_reader.readInt();
/*0xFFFFFFFF*/ m_reader.skipInt();
if (chunkType == CHUNK_XML_START_NAMESPACE
|| chunkType == CHUNK_XML_END_NAMESPACE) {
if (chunkType == CHUNK_XML_START_NAMESPACE) {
int prefix = m_reader.readInt();
int uri = m_reader.readInt();
m_namespaces.push(prefix, uri);
} else {
/*prefix*/ m_reader.skipInt();
/*uri*/ m_reader.skipInt();
m_namespaces.pop();
}
continue;
}
m_lineNumber = lineNumber;
if (chunkType == CHUNK_XML_START_TAG) {
m_namespaceUri = m_reader.readInt();
m_name = m_reader.readInt();
/*
* flags?
*/ m_reader.skipInt();
/*flags?*/ m_reader.skipInt();
int attributeCount = m_reader.readInt();
m_idAttribute = (attributeCount >>> 16) - 1;
attributeCount &= 0xFFFF;
@ -907,13 +890,6 @@ public class AXmlResourceParser implements XmlResourceParser {
}
m_namespaces.increaseDepth();
m_event = START_TAG;
m_strings.touch(m_name, m_name);
for (int i = 0; i < attributeCount; i++) {
m_strings.touch(m_attributes[ATTRIBUTE_IX_NAME], m_name);
m_strings.touch(m_attributes[ATTRIBUTE_IX_VALUE_STRING], m_name);
}
m_strings.touch(m_name, m_name);
sortAttrs();
break;
}
@ -927,12 +903,8 @@ public class AXmlResourceParser implements XmlResourceParser {
if (chunkType == CHUNK_XML_TEXT) {
m_name = m_reader.readInt();
/*
* ?
*/ m_reader.skipInt();
/*
* ?
*/ m_reader.skipInt();
/*?*/ m_reader.skipInt();
/*?*/ m_reader.skipInt();
m_event = TEXT;
break;
}
@ -989,60 +961,22 @@ public class AXmlResourceParser implements XmlResourceParser {
}
}
private void sortAttrs() {
int attributeCount = m_attributes.length / ATTRIBUTE_LENGHT;
int tmp1[][] = new int[attributeCount][];
int tmp2[] = null;
for (int i = 0; i < attributeCount; i++) {
tmp1[i] = new int[ATTRIBUTE_LENGHT + 1];
for (int j = 0; j < ATTRIBUTE_LENGHT; j++) {
tmp1[i][j] = m_attributes[i * ATTRIBUTE_LENGHT + j];
}
tmp1[i][ATTRIBUTE_LENGHT] = i;
if (DBG) {
try {
if (dbgOut == null) {
dbgOut = new BufferedWriter(new FileWriter("C:\\res.log", false));
}
dbgOut.write("Namespace: " + getAttributeNamespace(i)
+ ", Name: " + getAttributeName(i)
+ ", Value: " + getAttributeValue(i) + ", Array: "
+ formatArray(tmp1[i], 0, ATTRIBUTE_LENGHT) + "\n");
dbgOut.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
for (int j = 1; j < attributeCount; j++) {
for (int i = 1; i < attributeCount; i++) {
if (compareAttr(tmp1[i], tmp1[i - 1])) {
tmp2 = tmp1[i - 1];
tmp1[i - 1] = tmp1[i];
tmp1[i] = tmp2;
}
}
}
for (int i = 0; i < attributeCount; i++) {
for (int j = 0; j < ATTRIBUTE_LENGHT; j++) {
m_attributes[i * ATTRIBUTE_LENGHT + j] = tmp1[i][j];
}
}
}
private void setFirstError(AndrolibException error) {
if (mFirstError == null) {
mFirstError = error;
}
}
/////////////////////////////////// data
/*
* All values are essentially indices, e.g. m_name is an index of name in
* m_strings.
* All values are essentially indices, e.g. m_name is
* an index of name in m_strings.
*/
private ExtDataInput m_reader;
private ResAttrDecoder mAttrDecoder;
private AndrolibException mFirstError;
private boolean m_operational = false;
private StringBlock m_strings;
private int[] m_resourceIDs;
@ -1056,24 +990,26 @@ public class AXmlResourceParser implements XmlResourceParser {
private int m_idAttribute;
private int m_classAttribute;
private int m_styleAttribute;
private final static Logger LOGGER =
Logger.getLogger(AXmlResourceParser.class.getName());
Logger.getLogger(AXmlResourceParser.class.getName());
private static final String E_NOT_SUPPORTED = "Method is not supported.";
private static final int ATTRIBUTE_IX_NAMESPACE_URI = 0,
ATTRIBUTE_IX_NAME = 1,
ATTRIBUTE_IX_VALUE_STRING = 2,
ATTRIBUTE_IX_VALUE_TYPE = 3,
ATTRIBUTE_IX_VALUE_DATA = 4,
ATTRIBUTE_LENGHT = 5;
private static final int CHUNK_AXML_FILE = 0x00080003,
CHUNK_RESOURCEIDS = 0x00080180,
CHUNK_XML_FIRST = 0x00100100,
private static final int
ATTRIBUTE_IX_NAMESPACE_URI = 0,
ATTRIBUTE_IX_NAME = 1,
ATTRIBUTE_IX_VALUE_STRING = 2,
ATTRIBUTE_IX_VALUE_TYPE = 3,
ATTRIBUTE_IX_VALUE_DATA = 4,
ATTRIBUTE_LENGHT = 5;
private static final int
CHUNK_AXML_FILE = 0x00080003,
CHUNK_RESOURCEIDS = 0x00080180,
CHUNK_XML_FIRST = 0x00100100,
CHUNK_XML_START_NAMESPACE = 0x00100100,
CHUNK_XML_END_NAMESPACE = 0x00100101,
CHUNK_XML_START_TAG = 0x00100102,
CHUNK_XML_END_TAG = 0x00100103,
CHUNK_XML_TEXT = 0x00100104,
CHUNK_XML_LAST = 0x00100104;
private Writer dbgOut = null;
private final static boolean DBG = false;
CHUNK_XML_END_NAMESPACE = 0x00100101,
CHUNK_XML_START_TAG = 0x00100102,
CHUNK_XML_END_TAG = 0x00100103,
CHUNK_XML_TEXT = 0x00100104,
CHUNK_XML_LAST = 0x00100104;
}

View File

@ -20,8 +20,6 @@ import brut.androlib.AndrolibException;
import brut.androlib.err.CantFind9PatchChunk;
import brut.util.ExtDataInput;
import java.awt.image.BufferedImage;
import java.awt.image.Raster;
import java.awt.image.WritableRaster;
import java.io.*;
import javax.imageio.ImageIO;
import org.apache.commons.io.IOUtils;
@ -30,7 +28,7 @@ import org.apache.commons.io.IOUtils;
* @author Ryszard Wiśniewski <brut.alll@gmail.com>
*/
public class Res9patchStreamDecoder implements ResStreamDecoder {
public void decode(InputStream in, OutputStream out)
public void decode(InputStream in, OutputStream out)
throws AndrolibException {
try {
byte[] data = IOUtils.toByteArray(in);

View File

@ -109,7 +109,8 @@ public class XmlPullStreamDecoder implements ResStreamDecoder {
}
return true;
}
};//factory.newSerializerWrapper(mSerial);
};
//factory.newSerializerWrapper(mSerial);
par.setInput(in, null);
ser.setOutput(out, null);

View File

@ -17,10 +17,7 @@
package brut.androlib.res.util;
import java.io.*;
import org.xmlpull.mxp1_serializer.MXSerializer;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlSerializer;
/**
* @author Ryszard Wiśniewski <brut.alll@gmail.com>
@ -29,11 +26,9 @@ public class ExtMXSerializer extends MXSerializer implements ExtXmlSerializer {
@Override
public void startDocument(String encoding, Boolean standalone) throws
IOException, IllegalArgumentException, IllegalStateException {
if (!enableLineOpt || mNewLine >= 1) {
super.startDocument(encoding != null ? encoding : mDefaultEncoding,
standalone);
this.newLine();
}
super.startDocument(encoding != null ? encoding : mDefaultEncoding,
standalone);
this.newLine();
}
@Override
@ -71,129 +66,13 @@ public class ExtMXSerializer extends MXSerializer implements ExtXmlSerializer {
public ExtXmlSerializer newLine() throws IOException {
super.out.write(lineSeparator);
mCurLine ++;
dbg("Nline: " + mCurLine);
return this;
}
//XmlPullParser.START_TAG("</")
// if(doIndent) writeIndent();
@Override
protected void writeIndent() throws IOException {
if(!enableLineOpt || mCurLine < mNewLine) {
super.writeIndent();
mCurLine ++;
dbg("Iline: "+mCurLine);
}
}
@Override
public ExtXmlSerializer setLineNumber(int newLine, int event) throws IOException {
dbg(/*"curline: " + mCurLine + */", event: " + XmlPullParser.TYPES[event] +
", newline: " + newLine);
if (newLine == -1) {//Can`t fount line number info
enableLineOpt = false;
} else {
enableLineOpt = true;
if (event == XmlPullParser.START_DOCUMENT) {
if (newLine > 1) {
mNewLine = 1;
} else {
mNewLine = 0;
}
} else {
mNewLine = newLine;
if (!startTagIncomplete) {//XmlPullParser.START_TAG("<")
dbg(", old event:"+XmlPullParser.TYPES[mLastEvent]);
if(mLastEvent != XmlPullParser.END_TAG) {
moveToLine (newLine);
} else {
moveToLine (newLine - 1);
}
}
}
mLastEvent = event;
}
return this;
}
//XmlPullParser.END_TAG(" />") and XmlPullParser.START_TAG("><")
@Override
protected void writeNamespaceDeclarations() throws IOException {
super.writeNamespaceDeclarations();
if (enableLineOpt) {
if (mLastEvent == XmlPullParser.END_TAG) {
moveToLine (mNewLine);
} else {
moveToLine (mNewLine - 1);
}
}
}
private ExtXmlSerializer moveToLine(int newLine) throws IOException {
int addLines = newLine - mCurLine;
dbg(", addLines: " + addLines);
for (; addLines > 0; addLines --) {
newLine();
}
return this;
}
@Override
protected void reset() {
super.reset();
mCurLine = 1;
mLastEvent = XmlPullParser.START_DOCUMENT;
enableLineOpt = false;
}
public void setDisabledAttrEscape(boolean disabled) {
mIsDisabledAttrEscape = disabled;
}
@Override
public XmlSerializer text(String text) throws IOException {
if (enableLineOpt) {
mCurLine += (getTextLineNum(text) - 1);
}
return super.text(text);
}
private int getTextLineNum(String text) {
String str = "." + text + ".";
int linenum = str.split("\\n").length + str.split("\\r").length
- str.split("\\n\\r").length;//(Unix(LF)-1) + (Mac(CR)-1) - (Win(CRLF)-1) + 1
return linenum;
}
@Override
public XmlSerializer text(char[] buf, int start, int len)
throws IOException {
if (enableLineOpt) {
mCurLine += (getTextLineNum(new String(buf, start, len)) - 1);
}
return super.text(buf, start, len);
}
@Override
public void ignorableWhitespace(String text) throws IOException {
if (enableLineOpt) {
mCurLine += (getTextLineNum(text) - 1);
}
super.ignorableWhitespace(text);
}
private String mDefaultEncoding;
private boolean mIsDisabledAttrEscape = false;
private int mCurLine;
private int mNewLine;
private int mLastEvent;
private boolean enableLineOpt = false;
private final static boolean DBG = false;
public ExtXmlSerializer dbg(String str) throws IOException {
if(DBG)
super.out.write("<!--"+str+"-->");
return this;
}
}

View File

@ -26,8 +26,6 @@ public interface ExtXmlSerializer extends XmlSerializer {
public ExtXmlSerializer newLine() throws IOException;
public void setDisabledAttrEscape(boolean disabled);
public ExtXmlSerializer setLineNumber(int newLine, int event) throws IOException;
public ExtXmlSerializer dbg(String str) throws IOException;
public static final String PROPERTY_SERIALIZER_INDENTATION =
"http://xmlpull.org/v1/doc/properties.html#serializer-indentation";