internal static bool ReadAsDirectory(BigEndianBinaryReader br) { bool dir; br.BaseStream.Seek(8, SeekOrigin.Current); byte tocData = br.ReadByte(); dir = tocData == 128; br.BaseStream.Seek(-9, SeekOrigin.Current); return dir; }
private int readPPByte(BigEndianBinaryReader reader) { long returnpos = reader.BaseStream.Position + 4; reader.BaseStream.Position = (reader.ReadInt32() & 0xFFFFFF); reader.BaseStream.Position = (reader.ReadInt32() & 0xFFFFFF); int val = reader.ReadByte(); reader.BaseStream.Position = returnpos; return val; }
private string readString(BigEndianBinaryReader reader, int length) { byte[] stringarray = new byte[length]; for (int i = 0; i < length; i++) { stringarray[i] = reader.ReadByte(); } return System.Text.Encoding.UTF8.GetString(stringarray).Substring(0, length-1); }
private int disassembleCommand(BigEndianBinaryReader reader, int dwAddress, StringBuilder log) { if (dwAddress > m_dwCodeSize) // check we haven't gone over our code size return 0x7FFFFFFF; if ((dwAddress >> 14) > page) //Switch pages if needed { page++; reader.BaseStream.Position = m_ppCodePages[page]; } byte op = reader.ReadByte(); if (op > 155) { log.Append(string.Format("ERROR: invalid opcode {0:d}", op)); return 0x7FFFFFFF; } if (op == 45) // enter log.Append(string.Format("{0:X6}: === sub_{0:X6} ==========\n", dwAddress)); //log.Append(string.Format("{0:X6}:\t{1:d3}\t{2:s}\t", dwAddress, op, ops[op].Name)); log.Append(string.Format("{0:s}\t",ops[op].Name)); int l = ops[op].Length; // prepare opcode parameter(s) scrArg arg; // set defaults to keep c# compiler quiet arg.bValue3 = 0; arg.bValue2 = 0; arg.bValue1 = 0; arg.bValue0 = 0; arg.dwValue = 0; arg.fValue = 0; arg.nValue = 0; arg.sValue = 0; arg.wValue = 0; switch (l) { case 5: // usually DWORD or float arg.bValue3 = reader.ReadByte(); arg.bValue2 = reader.ReadByte(); arg.bValue1 = reader.ReadByte(); arg.bValue0 = reader.ReadByte(); break; case 4: // 3-byte int or 3 1-byte ints arg.bValue3 = 0; arg.bValue2 = reader.ReadByte(); arg.bValue1 = reader.ReadByte(); arg.bValue0 = reader.ReadByte(); break; case 3: // short or two 1-byte ints arg.bValue3 = 0; arg.bValue2 = 0; arg.bValue1 = reader.ReadByte(); arg.bValue0 = reader.ReadByte(); break; case 2: // BYTE arg.dwValue = 0; arg.bValue0 = reader.ReadByte(); break; } // print switch (op) { case 37: // ipush1 log.Append(string.Format("ipush\t{0:d}", arg.bValue0)); break; case 38: log.Append(string.Format("ipush\t{0:d}, {1:d}", arg.bValue1, arg.bValue0)); break; case 39: log.Append(string.Format("ipush\t{0:d}, {1:d}, {2:d}", arg.bValue2, arg.bValue1, arg.bValue0)); break; case 40: // ipush log.Append(string.Format("ipush\t{0:d}", arg.nValue)); break; case 41: // fpush log.Append(string.Format("fpush\t{0:f}", arg.fValue)); break; case 44: { int index = ((arg.bValue1 << 2) & 0x300) | arg.bValue0; log.Append(string.Format("native\t{0:X4} => {1:d}:${2:X8}, ({3:d}):{4:d}", arg.wValue, index, index < m_dwNativeSize ? m_ppNatives[index] : 0, (arg.bValue1 & 0x3E) >> 1, arg.bValue1 & 1)); // TODO: get hash from .native seg } break; case 45: arg.bValue3 = reader.ReadByte(); arg.bValue2 = reader.ReadByte(); arg.bValue1 = reader.ReadByte(); arg.bValue0 = reader.ReadByte(); log.Append(string.Format("enter\t{0:d} 0x{1:X} {2:d} ", arg.bValue3, (arg.bValue2 << 8) | arg.bValue1, arg.bValue0)); if (arg.bValue0 > 0) { // printf (" WARNING: 'enter' last parameter != 0 " ); for (int i = 1; i < arg.bValue0; i++) log.Append(string.Format("{0:d}", reader.ReadByte())); //putc(dwPage[dwAddressLow + 5 + i], stdout); } l = 5 + arg.bValue0; break; case 46: log.Append(string.Format("ret \t{0:d} {1:d}", arg.bValue1, arg.bValue0)); break; case 52: // parray case 53: // arrayget1 case 54: // arrayset1 case 55: // pframe1 case 56: // frameget case 57: // frameset case 58: // stackgetp case 59: // stackget case 60: // stackset case 61: // iaddimm1 case 62: // pgetimm1 case 63: // psetimm1 case 64: // imulimm1 log.Append(string.Format("{0:d}", arg.bValue0)); break; case 65: // ipush2 case 66: // iaddimm2 log.Append(string.Format("{0:d}", arg.wValue)); break; case 67: // pgetimm2 case 68: // psetimm2 log.Append(string.Format("0x{0:X}", arg.wValue)); break; case 69: // imilimm2 log.Append(string.Format("{0:d}", arg.wValue)); break; case 70: // arraygetp2 case 71: // arrayget2 case 72: // arrayset2 case 73: // pframe2 case 74: // frameget2 case 75: // frameset2 case 76: // pstatic2 case 77: // staticget2 case 78: // staticset2 case 79: // pglobal2 case 80: // globalget2 case 81: // globalset2 log.Append(string.Format("0x{0:X}", arg.wValue)); break; case 82: // call2h0 case 83: case 84: case 85: case 86: case 87: case 88: case 89: case 90: case 91: case 92: case 93: case 94: case 95: case 96: case 97: // call2hf log.Append(string.Format("call \t#{0:X}", ((op - 82) << 16) | arg.wValue)); break; case 98: // jr2 case 99: // jfr2 case 100: // jner2 case 101: // jeqr2 case 102: // jler2 case 103: // jltr2 case 104: // jger2: case 105: // jgtr2 log.Append(string.Format("{0:d} => #{0:X}", arg.sValue, dwAddress + 3 + arg.sValue)); // TODO: calculate target break; case 106: // pglobal3 log.Append(string.Format("pglobal\t{0:X}", arg.dwValue)); break; case 107: // globalget3 log.Append(string.Format("getg\t{0:X}", arg.dwValue)); break; case 108: // globalget3 log.Append(string.Format("setg\t{0:X}", arg.dwValue)); break; case 109: // ipush3 log.Append(string.Format("ipush\t{0:d}", arg.dwValue)); break; // { "switchr2", 0 }, // 110 length = 2 + byte[1]*6 case 110: // TODO: calculate targets { arg.bValue0 = reader.ReadByte(); l = 2 + arg.bValue0 * 6; log.Append(string.Format("[{0:d}]: ", arg.bValue0)); for (int i = 0; i < arg.bValue0; i++) { //int off = (int)reader.BaseStream.Position + (i * 6); //reader.BaseStream.Position = off; int value = (reader.ReadByte() << 24) + (reader.ReadByte() << 16) + (reader.ReadByte() << 8) + reader.ReadByte(); int target = (reader.ReadByte() << 8) + reader.ReadByte(); log.Append(string.Format("<{0:d}, {1:d} => #{2:X}>", value, target, target + reader.BaseStream.Position)); } } break; case 111: arg.bValue0 = reader.ReadByte(); l = arg.bValue0 + 2; log.Append(string.Format("spush\t[0x{0:X}] '{1}'", arg.bValue0, readString(reader, arg.bValue0))); break; case 112: arg.bValue3 = reader.ReadByte(); arg.bValue2 = reader.ReadByte(); arg.bValue1 = reader.ReadByte(); arg.bValue0 = reader.ReadByte(); l = (int)(arg.nValue + 5); log.Append(string.Format("spush\t[0x{0:X}] '{1}'", arg.dwValue, readString(reader, arg.nValue))); break; // { "spush0", 1 }, // 113 push "" case 114: // scpy case 115: // itos case 116: // sadd case 117: // saddi log.Append(string.Format("[{0:d}]", arg.bValue0)); break; case 122: case 123: case 124: case 125: case 126: case 127: case 128: case 129: // ret0r0 .. ret1r3 case 130: case 131: case 132: case 133: case 134: case 135: case 136: case 137: // ret2r0 .. ret3r3 log.Append(string.Format("ret \t{0:d} {1:d}", (op - 122) >> 2, (op - 122) & 3)); break; case 138: case 139: case 140: case 141: case 142: case 143: case 144: case 145: case 146: // iimmn1 .. iimm7 log.Append(string.Format("ipush\t{0:d}", op - 139)); break; case 147: case 148: case 149: case 150: case 151: case 152: case 153: case 154: case 155: // fimmn1 .. fimm7 log.Append(string.Format("fpush\t{0:f}", op - 148.0f)); break; } log.Append("\n"); return dwAddress + l; }