7 os.write(fd, bytearray([(val & 0xFF0000) >> 16, (val & 0xFF00) >> 8, val & 0xFF]))
12 return "usage: infile outfile"
13 fin = os.open(argv[0], os.O_RDONLY)
14 size = os.fstat(fin).st_size
17 return "Input file size is not multiple of 512 bytes!"
20 return "Too large input file, max supported is 2Gbyte in length!"
22 print(
"Size is " + str(size) +
" blocks.")
23 fout = os.open(argv[1], os.O_TRUNC|os.O_WRONLY|os.O_CREAT)
24 os.write(fout, b
"XemuBlockCompressedImage000\0")
27 tabpos = os.lseek(fout, 0, os.SEEK_CUR)
28 print(
"Pos-table offset = " + str(tabpos))
29 print(
"Data offset = " + str(size * 3 + tabpos))
30 os.lseek(fout, size * 3 + tabpos, os.SEEK_SET)
34 print(
"Reading all the file ...")
36 for blocknum
in range(size):
37 block = os.read(fin, 512)
39 raise IOError(
"We must have 512 bytes to be able to read at all steps!")
41 for i
in range(1, 512):
42 if block[i] != fillval:
46 bmap[blocknum] = datpos
47 if os.write(fout, block) != 512:
48 raise IOError(
"We must have 512 bytes to be able to written at once!")
51 if sbmap[fillval] > -1:
52 bmap[blocknum] = sbmap[fillval]
54 sbmap[fillval] = datpos | 0x800000
55 bmap[blocknum] = sbmap[fillval]
56 if os.write(fout, block) != 512:
57 raise IOError(
"We must have 512 bytes to be able to written at once!")
63 os.lseek(fout, tabpos, os.SEEK_SET)
64 for blocknum
in range(size):
65 if bmap[blocknum] < 0:
66 raise RuntimeError(
"block with -1 ...")
70 print(
"Compression ratio will be: " + str(100 * (size - ncompressed) / ncompressed) +
"%")
75 if __name__ ==
"__main__":
78 sys.stderr.write(
"ERROR: " + r +
"\n")