Fixed NBTSizeTracker missing a lot of data being read. Also made new NBT object allocation claim 32-bits in the size tracker.

(cherry picked from commit de066a86da)

Conflicts:
	patches/minecraft/net/minecraft/nbt/CompressedStreamTools.java.patch
	patches/minecraft/net/minecraft/nbt/NBTTagList.java.patch
This commit is contained in:
Lex Manos 2015-04-17 02:07:01 -07:00 committed by cpw
parent 61984561cd
commit 1effc27790
7 changed files with 131 additions and 4 deletions

View file

@ -16,7 +16,25 @@
public static void func_74793_a(NBTTagCompound p_74793_0_, File p_74793_1_) throws IOException public static void func_74793_a(NBTTagCompound p_74793_0_, File p_74793_1_) throws IOException
{ {
File file2 = new File(p_74793_1_.getAbsolutePath() + "_tmp"); File file2 = new File(p_74793_1_.getAbsolutePath() + "_tmp");
@@ -182,7 +179,6 @@ @@ -156,6 +153,7 @@
private static NBTBase func_152455_a(DataInput p_152455_0_, int p_152455_1_, NBTSizeTracker p_152455_2_) throws IOException
{
byte b0 = p_152455_0_.readByte();
+ p_152455_2_.func_152450_a(8); // Forge: Count everything!
if (b0 == 0)
{
@@ -163,7 +161,8 @@
}
else
{
- p_152455_0_.readUTF();
+ NBTSizeTracker.readUTF(p_152455_2_, p_152455_0_.readUTF()); //Forge: Count this string.
+ p_152455_2_.func_152450_a(32); //Forge: 4 extra bytes for the object allocation.
NBTBase nbtbase = NBTBase.func_150284_a(b0);
try
@@ -182,7 +181,6 @@
} }
} }
@ -24,7 +42,7 @@
public static void func_74795_b(NBTTagCompound p_74795_0_, File p_74795_1_) throws IOException public static void func_74795_b(NBTTagCompound p_74795_0_, File p_74795_1_) throws IOException
{ {
DataOutputStream dataoutputstream = new DataOutputStream(new FileOutputStream(p_74795_1_)); DataOutputStream dataoutputstream = new DataOutputStream(new FileOutputStream(p_74795_1_));
@@ -197,13 +193,11 @@ @@ -197,13 +195,11 @@
} }
} }

View file

@ -0,0 +1,37 @@
--- ../src-base/minecraft/net/minecraft/nbt/NBTSizeTracker.java
+++ ../src-work/minecraft/net/minecraft/nbt/NBTSizeTracker.java
@@ -25,4 +25,34 @@
throw new RuntimeException("Tried to read NBT tag that was too big; tried to allocate: " + this.field_152453_c + "bytes where max allowed: " + this.field_152452_b);
}
}
+
+ /*
+ * UTF8 is not a simple encoding system, each character can be either
+ * 1, 2, or 3 bytes. Depending on where it's numerical value falls.
+ * We have to count up each character individually to see the true
+ * length of the data.
+ *
+ * Basic concept is that it uses the MSB of each byte as a 'read more' signal.
+ * So it has to shift each 7-bit segment.
+ *
+ * This will accurately count the correct byte length to encode this string, plus the 2 bytes for it's length prefix.
+ */
+ public static void readUTF(NBTSizeTracker tracker, String data)
+ {
+ tracker.func_152450_a(16); //Header length
+ if (data == null)
+ return;
+
+ int len = data.length();
+ int utflen = 0;
+
+ for (int i = 0; i < len; i++)
+ {
+ int c = data.charAt(i);
+ if ((c >= 0x0001) && (c <= 0x007F)) utflen += 1;
+ else if (c > 0x07FF) utflen += 3;
+ else utflen += 2;
+ }
+ tracker.func_152450_a(8 * utflen);
+ }
}

View file

@ -0,0 +1,10 @@
--- ../src-base/minecraft/net/minecraft/nbt/NBTTagByteArray.java
+++ ../src-work/minecraft/net/minecraft/nbt/NBTTagByteArray.java
@@ -25,6 +25,7 @@
void func_152446_a(DataInput p_152446_1_, int p_152446_2_, NBTSizeTracker p_152446_3_) throws IOException
{
+ p_152446_3_.func_152450_a(32); //Forge: Count the length as well
int j = p_152446_1_.readInt();
p_152446_3_.func_152450_a((long)(8 * j));
this.field_74754_a = new byte[j];

View file

@ -0,0 +1,27 @@
--- ../src-base/minecraft/net/minecraft/nbt/NBTTagCompound.java
+++ ../src-work/minecraft/net/minecraft/nbt/NBTTagCompound.java
@@ -48,7 +48,7 @@
while ((b0 = func_152447_a(p_152446_1_, p_152446_3_)) != 0)
{
String s = func_152448_b(p_152446_1_, p_152446_3_);
- p_152446_3_.func_152450_a((long)(16 * s.length()));
+ NBTSizeTracker.readUTF(p_152446_3_, s); // Forge: Correctly read String length including header.
NBTBase nbtbase = func_152449_a(b0, s, p_152446_1_, p_152446_2_ + 1, p_152446_3_);
this.field_74784_a.put(s, nbtbase);
}
@@ -379,6 +379,7 @@
private static byte func_152447_a(DataInput p_152447_0_, NBTSizeTracker p_152447_1_) throws IOException
{
+ p_152447_1_.func_152450_a(8);
return p_152447_0_.readByte();
}
@@ -389,6 +390,7 @@
static NBTBase func_152449_a(byte p_152449_0_, String p_152449_1_, DataInput p_152449_2_, int p_152449_3_, NBTSizeTracker p_152449_4_)
{
+ p_152449_4_.func_152450_a(32); //Forge: 4 extra bytes for the object allocation.
NBTBase nbtbase = NBTBase.func_150284_a(p_152449_0_);
try

View file

@ -0,0 +1,10 @@
--- ../src-base/minecraft/net/minecraft/nbt/NBTTagIntArray.java
+++ ../src-work/minecraft/net/minecraft/nbt/NBTTagIntArray.java
@@ -29,6 +29,7 @@
void func_152446_a(DataInput p_152446_1_, int p_152446_2_, NBTSizeTracker p_152446_3_) throws IOException
{
+ p_152446_3_.func_152450_a(32); //Forge: Count the length as well
int j = p_152446_1_.readInt();
p_152446_3_.func_152450_a((long)(32 * j));
this.field_74749_a = new int[j];

View file

@ -8,7 +8,21 @@
import java.io.DataInput; import java.io.DataInput;
import java.io.DataOutput; import java.io.DataOutput;
import java.io.IOException; import java.io.IOException;
@@ -91,7 +89,6 @@ @@ -45,11 +43,13 @@
{
p_152446_3_.func_152450_a(8L);
this.field_74746_b = p_152446_1_.readByte();
+ p_152446_3_.func_152450_a(32); //Forge: Count the length as well
int j = p_152446_1_.readInt();
this.field_74747_a = new ArrayList();
for (int k = 0; k < j; ++k)
{
+ p_152446_3_.func_152450_a(32); //Forge: 4 extra bytes for the object allocation.
NBTBase nbtbase = NBTBase.func_150284_a(this.field_74746_b);
nbtbase.func_152446_a(p_152446_1_, p_152446_2_ + 1, p_152446_3_);
this.field_74747_a.add(nbtbase);
@@ -91,7 +91,6 @@
this.field_74747_a.add(p_74742_1_); this.field_74747_a.add(p_74742_1_);
} }
@ -16,7 +30,7 @@
public void func_150304_a(int p_150304_1_, NBTBase p_150304_2_) public void func_150304_a(int p_150304_1_, NBTBase p_150304_2_)
{ {
if (p_150304_1_ >= 0 && p_150304_1_ < this.field_74747_a.size()) if (p_150304_1_ >= 0 && p_150304_1_ < this.field_74747_a.size())
@@ -114,7 +111,6 @@ @@ -114,7 +113,6 @@
} }
} }

View file

@ -0,0 +1,11 @@
--- ../src-base/minecraft/net/minecraft/nbt/NBTTagString.java
+++ ../src-work/minecraft/net/minecraft/nbt/NBTTagString.java
@@ -32,7 +32,7 @@
void func_152446_a(DataInput p_152446_1_, int p_152446_2_, NBTSizeTracker p_152446_3_) throws IOException
{
this.field_74751_a = p_152446_1_.readUTF();
- p_152446_3_.func_152450_a((long)(16 * this.field_74751_a.length()));
+ NBTSizeTracker.readUTF(p_152446_3_, field_74751_a); // Forge: Correctly read String length including header.
}
public byte func_74732_a()