Hi Gerd

Patch attached to fix offsetSize in check16 and have readVarLength()
return the size.

Ticker
Index: src/test/display/MdrCheck.java
===================================================================
--- src/test/display/MdrCheck.java	(revision 593)
+++ src/test/display/MdrCheck.java	(working copy)
@@ -19,6 +19,7 @@
 import java.text.Collator;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.concurrent.atomic.AtomicInteger;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
@@ -631,10 +632,11 @@
 		long end = section.getEnd();
 
 		reader.position(start);
-		decoder.huffmanDecodingFailed = true; // being pessimistic here 
-		int remSize = readVarLength(reader);
-		if (section.getLen() - remSize != reader.position() - start) {
-			error ("bad remaining size %d", remSize);
+		decoder.huffmanDecodingFailed = true; // being pessimistic here
+		AtomicInteger varLength = new AtomicInteger();
+		int remSize = readVarLength(reader, varLength);
+		if (section.getLen() - remSize != varLength.get()) {
+			error("bad remaining size %d", remSize);
 			return;
 		}
 
@@ -643,13 +645,13 @@
 		int huffmanTab2Size = (1 << decoder.lookupBits) * 2;
 		int maxDepth = reader.get1u(); // max code length
 		if (maxDepth >= 32) {
-			error ("maximum depth too high: %d", maxDepth);
+			error("maximum depth too high: %d", maxDepth);
 			return; // don't know how to handle this
 			
 		}
 		int rowsTab1 = reader.get1u(); // rows in search table 
 		if (rowsTab1 >= 32) {
-			error ("number of rows for search table too high: %d", rowsTab1);
+			error("number of rows for search table too high: %d", rowsTab1);
 			return; // don't know how to handle this
 			
 		}
@@ -657,15 +659,17 @@
 		if (symWidth != 8) {
 			return; // don't know yet how to handle this
 		}
-		int symBytes = readVarLength(reader);
+		int symBytes = readVarLength(reader, varLength);
+		int offsetSize = varLength.get(); 
 		int headerSize = (int) (reader.position() - start);
 		int minCodeBytes = (int) Math.ceil(maxDepth / 8.0); 
-		int offsetSize = remSize < 128 ? 1 : 2; 
 		int tab1Width = 1 + offsetSize + minCodeBytes; 
 
 		int tab1Bytes = section.getLen() - huffmanTab2Size - symBytes - headerSize;
 		if (rowsTab1 * tab1Width != tab1Bytes) {
-			error ("search table: rows x width doesn't match table expected size");
+			error("search table: rows x width doesn't match table expected size");
+			//error("rows=%d, width=%d, secLen=%d, hdrSize=%d, tab2Size=%d symBytes=%d",
+			//	  rowsTab1, tab1Width, section.getLen(), headerSize, huffmanTab2Size, symBytes);
 			return; // don't know how to handle this
 		}
 		for (int i = 0; i< rowsTab1; i++) {
@@ -693,29 +697,28 @@
 
 	/**
 	 * Read varying length integer where first byte also gives number of following bytes.
+	 * See also imgfmt/app/mdr/MdrUtils.writeVarLength() and ./MdrDisplay.printSect17()
+	 *
 	 * @param reader the reader to use
 	 * @return the length
 	 */
-	private static int readVarLength(ImgFileReader reader) {
+	private int readVarLength(ImgFileReader reader, AtomicInteger varLength) {
 		int peek = reader.get1u();
-		int partlength = 0;
-		if ((peek & 1) == 1) {
-			partlength = peek;
-			partlength >>= 1;
+		if ((peek & 0x1) == 0x1) {
+			varLength.set(1);
+			return peek >> 1;
+		} else if ((peek & 0x3) == 0x2) {
+			varLength.set(2);
+			return peek >> 2 | reader.get1u() << 6;
+		} else if ((peek & 0x7) == 0x4) {
+			varLength.set(3);
+			return peek >> 3 | reader.get2u() << 5;
+		}  else { // bottom 3 bits clear so assume:
+			varLength.set(4);
+			test("readVarLength length 4. Not sure of correct shift for %x", peek);
+			//return peek >> 3 | reader.get3u() << 5; // it could be this
+			return peek >> 4 | reader.get3u() << 4; // mkgmap writeVarLength does this
 		}
-		if ((peek & 3) == 2) {
-			partlength = reader.get1u();
-			partlength <<= 8;
-			partlength += peek;
-			partlength >>= 2;
-		}
-		if ((peek & 7) == 4) {
-			partlength = reader.get2u();
-			partlength <<= 8;
-			partlength += peek;
-			partlength >>= 3;
-		}
-		return partlength;
 	}
 
 	private void check18() {
_______________________________________________
mkgmap-dev mailing list
mkgmap-dev@lists.mkgmap.org.uk
https://www.mkgmap.org.uk/mailman/listinfo/mkgmap-dev

Reply via email to