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; package brut.androlib.res.decoder;
import android.content.res.XmlResourceParser; 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 android.util.TypedValue;
import brut.androlib.AndrolibException; import brut.androlib.AndrolibException;
import brut.androlib.res.xml.ResXmlEncoders; import brut.androlib.res.xml.ResXmlEncoders;
import brut.util.ExtDataInput; import brut.util.ExtDataInput;
import com.mindprod.ledatastream.LEDataInputStream; 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.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.xmlpull.v1.XmlPullParserException;
/** /**
* @author Ryszard Wiśniewski <brut.alll@gmail.com> * @author Ryszard Wiśniewski <brut.alll@gmail.com>
@ -812,9 +809,7 @@ public class AXmlResourceParser implements XmlResourceParser {
} }
int event = m_event; int event = m_event;
if (event != START_DOCUMENT) {//keep m_lineNumber
resetEventInfo(); resetEventInfo();
}
while (true) { while (true) {
if (m_decreaseDepth) { if (m_decreaseDepth) {
@ -836,6 +831,7 @@ public class AXmlResourceParser implements XmlResourceParser {
chunkType = CHUNK_XML_START_TAG; chunkType = CHUNK_XML_START_TAG;
} else { } else {
chunkType = m_reader.readInt(); chunkType = m_reader.readInt();
}
if (chunkType == CHUNK_RESOURCEIDS) { if (chunkType == CHUNK_RESOURCEIDS) {
int chunkSize = m_reader.readInt(); int chunkSize = m_reader.readInt();
@ -850,22 +846,17 @@ public class AXmlResourceParser implements XmlResourceParser {
throw new IOException("Invalid chunk type (" + chunkType + ")."); 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. // Fake START_DOCUMENT event.
if (chunkType == CHUNK_XML_START_TAG && event == -1) { if (chunkType == CHUNK_XML_START_TAG && event == -1) {
m_event = START_DOCUMENT; m_event = START_DOCUMENT;
m_lineNumber = lineNumber;
break; break;
} }
// Common header.
/*chunkSize*/ m_reader.skipInt();
int lineNumber = m_reader.readInt();
/*0xFFFFFFFF*/ m_reader.skipInt();
if (chunkType == CHUNK_XML_START_NAMESPACE if (chunkType == CHUNK_XML_START_NAMESPACE
|| chunkType == CHUNK_XML_END_NAMESPACE) { || chunkType == CHUNK_XML_END_NAMESPACE) {
if (chunkType == CHUNK_XML_START_NAMESPACE) { if (chunkType == CHUNK_XML_START_NAMESPACE) {
@ -873,27 +864,19 @@ public class AXmlResourceParser implements XmlResourceParser {
int uri = m_reader.readInt(); int uri = m_reader.readInt();
m_namespaces.push(prefix, uri); m_namespaces.push(prefix, uri);
} else { } else {
/* /*prefix*/ m_reader.skipInt();
* prefix /*uri*/ m_reader.skipInt();
*/ m_reader.skipInt();
/*
* uri
*/ m_reader.skipInt();
m_namespaces.pop(); m_namespaces.pop();
} }
continue; continue;
} }
m_lineNumber = lineNumber; m_lineNumber = lineNumber;
}
if (chunkType == CHUNK_XML_START_TAG) { if (chunkType == CHUNK_XML_START_TAG) {
m_namespaceUri = m_reader.readInt(); m_namespaceUri = m_reader.readInt();
m_name = m_reader.readInt(); m_name = m_reader.readInt();
/* /*flags?*/ m_reader.skipInt();
* flags?
*/ m_reader.skipInt();
int attributeCount = m_reader.readInt(); int attributeCount = m_reader.readInt();
m_idAttribute = (attributeCount >>> 16) - 1; m_idAttribute = (attributeCount >>> 16) - 1;
attributeCount &= 0xFFFF; attributeCount &= 0xFFFF;
@ -907,13 +890,6 @@ public class AXmlResourceParser implements XmlResourceParser {
} }
m_namespaces.increaseDepth(); m_namespaces.increaseDepth();
m_event = START_TAG; 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; break;
} }
@ -927,12 +903,8 @@ public class AXmlResourceParser implements XmlResourceParser {
if (chunkType == CHUNK_XML_TEXT) { if (chunkType == CHUNK_XML_TEXT) {
m_name = m_reader.readInt(); m_name = m_reader.readInt();
/* /*?*/ m_reader.skipInt();
* ? /*?*/ m_reader.skipInt();
*/ m_reader.skipInt();
/*
* ?
*/ m_reader.skipInt();
m_event = TEXT; m_event = TEXT;
break; 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) { private void setFirstError(AndrolibException error) {
if (mFirstError == null) { if (mFirstError == null) {
mFirstError = error; mFirstError = error;
} }
} }
/////////////////////////////////// data /////////////////////////////////// data
/* /*
* All values are essentially indices, e.g. m_name is an index of name in * All values are essentially indices, e.g. m_name is
* m_strings. * an index of name in m_strings.
*/ */
private ExtDataInput m_reader; private ExtDataInput m_reader;
private ResAttrDecoder mAttrDecoder; private ResAttrDecoder mAttrDecoder;
private AndrolibException mFirstError; private AndrolibException mFirstError;
private boolean m_operational = false; private boolean m_operational = false;
private StringBlock m_strings; private StringBlock m_strings;
private int[] m_resourceIDs; private int[] m_resourceIDs;
@ -1056,16 +990,20 @@ public class AXmlResourceParser implements XmlResourceParser {
private int m_idAttribute; private int m_idAttribute;
private int m_classAttribute; private int m_classAttribute;
private int m_styleAttribute; private int m_styleAttribute;
private final static Logger LOGGER = 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 String E_NOT_SUPPORTED = "Method is not supported.";
private static final int ATTRIBUTE_IX_NAMESPACE_URI = 0, private static final int
ATTRIBUTE_IX_NAMESPACE_URI = 0,
ATTRIBUTE_IX_NAME = 1, ATTRIBUTE_IX_NAME = 1,
ATTRIBUTE_IX_VALUE_STRING = 2, ATTRIBUTE_IX_VALUE_STRING = 2,
ATTRIBUTE_IX_VALUE_TYPE = 3, ATTRIBUTE_IX_VALUE_TYPE = 3,
ATTRIBUTE_IX_VALUE_DATA = 4, ATTRIBUTE_IX_VALUE_DATA = 4,
ATTRIBUTE_LENGHT = 5; ATTRIBUTE_LENGHT = 5;
private static final int CHUNK_AXML_FILE = 0x00080003,
private static final int
CHUNK_AXML_FILE = 0x00080003,
CHUNK_RESOURCEIDS = 0x00080180, CHUNK_RESOURCEIDS = 0x00080180,
CHUNK_XML_FIRST = 0x00100100, CHUNK_XML_FIRST = 0x00100100,
CHUNK_XML_START_NAMESPACE = 0x00100100, CHUNK_XML_START_NAMESPACE = 0x00100100,
@ -1074,6 +1012,4 @@ public class AXmlResourceParser implements XmlResourceParser {
CHUNK_XML_END_TAG = 0x00100103, CHUNK_XML_END_TAG = 0x00100103,
CHUNK_XML_TEXT = 0x00100104, CHUNK_XML_TEXT = 0x00100104,
CHUNK_XML_LAST = 0x00100104; CHUNK_XML_LAST = 0x00100104;
private Writer dbgOut = null;
private final static boolean DBG = false;
} }

View File

@ -20,8 +20,6 @@ import brut.androlib.AndrolibException;
import brut.androlib.err.CantFind9PatchChunk; import brut.androlib.err.CantFind9PatchChunk;
import brut.util.ExtDataInput; import brut.util.ExtDataInput;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.awt.image.Raster;
import java.awt.image.WritableRaster;
import java.io.*; import java.io.*;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;

View File

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

View File

@ -17,10 +17,7 @@
package brut.androlib.res.util; package brut.androlib.res.util;
import java.io.*; import java.io.*;
import org.xmlpull.mxp1_serializer.MXSerializer; import org.xmlpull.mxp1_serializer.MXSerializer;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlSerializer;
/** /**
* @author Ryszard Wiśniewski <brut.alll@gmail.com> * @author Ryszard Wiśniewski <brut.alll@gmail.com>
@ -29,12 +26,10 @@ public class ExtMXSerializer extends MXSerializer implements ExtXmlSerializer {
@Override @Override
public void startDocument(String encoding, Boolean standalone) throws public void startDocument(String encoding, Boolean standalone) throws
IOException, IllegalArgumentException, IllegalStateException { IOException, IllegalArgumentException, IllegalStateException {
if (!enableLineOpt || mNewLine >= 1) {
super.startDocument(encoding != null ? encoding : mDefaultEncoding, super.startDocument(encoding != null ? encoding : mDefaultEncoding,
standalone); standalone);
this.newLine(); this.newLine();
} }
}
@Override @Override
protected void writeAttributeValue(String value, Writer out) protected void writeAttributeValue(String value, Writer out)
@ -71,129 +66,13 @@ public class ExtMXSerializer extends MXSerializer implements ExtXmlSerializer {
public ExtXmlSerializer newLine() throws IOException { public ExtXmlSerializer newLine() throws IOException {
super.out.write(lineSeparator); super.out.write(lineSeparator);
mCurLine ++;
dbg("Nline: " + mCurLine);
return this; 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) { public void setDisabledAttrEscape(boolean disabled) {
mIsDisabledAttrEscape = 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 String mDefaultEncoding;
private boolean mIsDisabledAttrEscape = false; 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 ExtXmlSerializer newLine() throws IOException;
public void setDisabledAttrEscape(boolean disabled); 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 = public static final String PROPERTY_SERIALIZER_INDENTATION =
"http://xmlpull.org/v1/doc/properties.html#serializer-indentation"; "http://xmlpull.org/v1/doc/properties.html#serializer-indentation";