/** * get the region with a int ip address with memory binary search algorithm * * @param ip * @ */ public DataBlock memorySearch(long ip) { int blen = IndexBlock.getIndexBlockLength(); if (dbBinStr == null) { dbBinStr = new byte[(int)raf.length()]; raf.seek(0L); raf.readFully(dbBinStr, 0, dbBinStr.Length); //initialize the global vars firstIndexPtr = Util.getIntLong(dbBinStr, 0); lastIndexPtr = Util.getIntLong(dbBinStr, 4); totalIndexBlocks = (int)((lastIndexPtr - firstIndexPtr) / blen) + 1; } //search the index blocks to define the data int l = 0, h = totalIndexBlocks; long sip, eip, dataptr = 0; while (l <= h) { int m = (l + h) >> 1; int p = (int)(firstIndexPtr + m * blen); sip = Util.getIntLong(dbBinStr, p); if (ip < sip) { h = m - 1; } else { eip = Util.getIntLong(dbBinStr, p + 4); if (ip > eip) { l = m + 1; } else { dataptr = Util.getIntLong(dbBinStr, p + 8); break; } } } //not matched if (dataptr == 0) { return(null); } //get the data int dataLen = (int)((dataptr >> 24) & 0xFF); int dataPtr = (int)((dataptr & 0x00FFFFFF)); int city_id = (int)Util.getIntLong(dbBinStr, dataPtr); //String region = new String(dbBinStr, dataPtr + 4, dataLen - 4, "UTF-8"); var region = Encoding.UTF8.GetString(dbBinStr, dataPtr + 4, dataLen - 4); return(new DataBlock(city_id, region, dataPtr)); }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: public CSOFileSectorDevice(java.io.RandomAccessFile fileAccess, byte[] header) throws java.io.IOException public CSOFileSectorDevice(RandomAccessFile fileAccess, sbyte[] header) : base(fileAccess) { ByteBuffer byteBuffer = ByteBuffer.wrap(header).order(ByteOrder.LITTLE_ENDIAN); /* * u32 'CISO' * u64 image size in bytes (first u32 is highest 32-bit, second u32 is lowest 32-bit) * u32 sector size? (00000800 = 2048 = sector size) * u32 ? (1) * u32[] sector offsets (as many as image size / sector size, I guess) */ long lengthInBytes = (((long)byteBuffer.getInt(4)) << 32) | (byteBuffer.getInt(8) & 0xFFFFFFFFL); int sectorSize = byteBuffer.getInt(16); offsetShift = byteBuffer.get(21) & 0xFF; numSectors = getNumSectors(lengthInBytes, sectorSize); sectorOffsets = new long[numSectors + 1]; sbyte[] offsetData = new sbyte[(numSectors + 1) * 4]; fileAccess.seek(24); fileAccess.readFully(offsetData); ByteBuffer offsetBuffer = ByteBuffer.wrap(offsetData).order(ByteOrder.LITTLE_ENDIAN); for (int i = 0; i <= numSectors; i++) { sectorOffsets[i] = offsetBuffer.getInt(i * 4) & 0xFFFFFFFFL; if (i > 0) { if ((sectorOffsets[i] & sectorOffsetMask) < (sectorOffsets[i - 1] & sectorOffsetMask)) { Console.WriteLine(string.Format("Corrupted CISO - Invalid offset [{0:D}]: 0x{1:X8} < 0x{2:X8}", i, sectorOffsets[i], sectorOffsets[i - 1])); } } } }
public static void Main(string[] args) { try { RandomAccessFile inToc = new RandomAccessFile("tmp/umdbuffer.toc", "r"); RandomAccessFile inIso = new RandomAccessFile("tmp/umdbuffer.iso", "r"); RandomAccessFile outIso = new RandomAccessFile("tmp/umd.iso", "rw"); int numSectors = inToc.readInt(); Console.WriteLine(string.Format("numSectors={0:D}", numSectors)); sbyte[] buffer = new sbyte[sectorLength]; for (int i = 4; i < inToc.Length(); i += 8) { int sectorNumber = inToc.readInt(); int bufferedSectorNumber = inToc.readInt(); inIso.seek(bufferedSectorNumber * (long)sectorLength); inIso.readFully(buffer); outIso.seek(sectorNumber * (long)sectorLength); outIso.write(buffer); } inToc.close(); inIso.close(); outIso.close(); } catch (FileNotFoundException e) { Console.WriteLine(e.ToString()); Console.Write(e.StackTrace); } catch (IOException e) { Console.WriteLine(e.ToString()); Console.Write(e.StackTrace); } }
public static void Main(string[] args) { File fileToc1 = new File("tmp/umdbuffer1.toc"); File fileIso1 = new File("tmp/umdbuffer1.iso"); File fileToc2 = new File("tmp/umdbuffer2.toc"); File fileIso2 = new File("tmp/umdbuffer2.iso"); File fileToc = new File("tmp/umdbuffer.toc"); File fileIso = new File("tmp/umdbuffer.iso"); try { System.IO.FileStream fosToc = new System.IO.FileStream(fileToc, System.IO.FileMode.Create, System.IO.FileAccess.Write); System.IO.FileStream fosIso = new System.IO.FileStream(fileIso, System.IO.FileMode.Create, System.IO.FileAccess.Write); System.IO.FileStream fisToc1 = new System.IO.FileStream(fileToc1, System.IO.FileMode.Open, System.IO.FileAccess.Read); System.IO.FileStream fisIso1 = new System.IO.FileStream(fileIso1, System.IO.FileMode.Open, System.IO.FileAccess.Read); System.IO.FileStream fisToc2 = new System.IO.FileStream(fileToc2, System.IO.FileMode.Open, System.IO.FileAccess.Read); DataOutput toc = new DataOutputStream(fosToc); DataOutput iso = new DataOutputStream(fosIso); DataInput toc1 = new DataInputStream(fisToc1); DataInput iso1 = new DataInputStream(fisIso1); DataInput toc2 = new DataInputStream(fisToc2); RandomAccessFile iso2 = new RandomAccessFile(fileIso2, "r"); int numSectors = toc1.readInt(); int numSectorsMerge = toc2.readInt(); toc.writeInt(System.Math.Max(numSectors, numSectorsMerge)); Dictionary <int, int> tocHashMap = new Dictionary <int, int>(); for (int i = 4; i < fileToc1.Length(); i += 8) { int sectorNumber = toc1.readInt(); int bufferedSectorNumber = toc1.readInt(); tocHashMap[sectorNumber] = bufferedSectorNumber; toc.writeInt(sectorNumber); toc.writeInt(bufferedSectorNumber); } sbyte[] buffer = new sbyte[sectorLength]; for (int i = 0; i < fileIso1.Length(); i += buffer.Length) { iso1.readFully(buffer); iso.write(buffer); } int nextFreeBufferedSectorNumber = (int)(fileIso1.Length() / sectorLength); for (int i = 4; i < fileToc2.Length(); i += 8) { int sectorNumber = toc2.readInt(); int bufferedSectorNumber = toc2.readInt(); if (!tocHashMap.ContainsKey(sectorNumber)) { iso2.seek(bufferedSectorNumber * (long)sectorLength); iso2.readFully(buffer); iso.write(buffer); toc.writeInt(sectorNumber); toc.writeInt(nextFreeBufferedSectorNumber); tocHashMap[sectorNumber] = nextFreeBufferedSectorNumber; nextFreeBufferedSectorNumber++; } } fosToc.Close(); fosIso.Close(); fisToc1.Close(); fisIso1.Close(); fisToc2.Close(); iso2.close(); } catch (FileNotFoundException e) { Console.WriteLine(e.ToString()); Console.Write(e.StackTrace); } catch (IOException e) { Console.WriteLine(e.ToString()); Console.Write(e.StackTrace); } }