diff --git a/scripts/DeflOpt.exe b/scripts/DeflOpt.exe new file mode 100644 index 0000000..7f0cd4c Binary files /dev/null and b/scripts/DeflOpt.exe differ diff --git a/scripts/conv_zip.py b/scripts/conv_zip.py new file mode 100644 index 0000000..62bef05 --- /dev/null +++ b/scripts/conv_zip.py @@ -0,0 +1,109 @@ +import binascii +import os +import sys +import time +import shutil + +LEN = 2**16 +huffman = False +TARGET=os.getcwd() +SOURCE=sys.argv[1] +DEFLATE=os.path.basename(sys.argv[1]) + ".deflate" + + +if os.path.isfile("rom.zip"): + os.unlink("rom.zip") + +os.system("wine kzip.exe rom /s1 %s" % SOURCE) +os.system("wine DeflOpt.exe /a rom.zip") +os.system("ruby zip2raw.rb rom.zip") + +if os.path.isfile("rom.zip"): + os.unlink("rom.zip") + +data = open(DEFLATE).read() +os.unlink(DEFLATE) +zip_size = len(data) +cfile = open("/tmp/loader.c","w") +hfile = open("/tmp/loader.h","w") + +parts = [] +cnt = len(data) / ((2**15) -1 ) +r = len(data) - (cnt * ((2**15) -1)) +for i in range(0, cnt): + parts.append(((2**15) -1 )) +parts.append(r) + +hfile.write('''/* +File: %s +Time: %s +*/ +#ifndef __FIFO_H__ +#define __FIFO_H__ + +#define ROM_ZIP_SIZE %i +#define ROM_BUFFER_CNT %i + +''' % (os.path.basename(SOURCE),time.strftime("%a, %d %b %Y %H:%M:%S", + time.localtime()),zip_size,len(parts))) + + +for idx,val in enumerate(parts): + hfile.write('#define ROM_BUFFER_SIZE%02i %i\n' % (idx+1,val)) + + +hfile.write('\n#endif\n') +hfile.close() +cfile.write('''/* +File: %s +Time: %s +*/ +#include +#include +''') + + +addr = 0 +for idx,val in enumerate(parts): + cfile.write(''' +const char _rom%02i[ROM_BUFFER_SIZE%02i] PROGMEM = { +''' % (idx+1,idx+1)) + l = addr + h = addr + parts[idx] + addr+= parts[idx] + for idx,c in enumerate(data[l:h]): + c = ord(c) + if idx 255 + warn "Unsupported compression method" + exit 1 + end + last_mod_time = ARGF.read(2) # unsupported until i know how to convert dos to unix time + last_mod_date = ARGF.read(2) # unsupported until i know how to convert dos to unix time + + crc_32 = ARGF.read(4).unpack("V").first + + size = ARGF.read(4).unpack("V").first + if use_data_descriptor + size = :auto + end + + uncompressed_size = ARGF.read(4).unpack("V").first + + file_name_length = ARGF.read(2).unpack("v").first + extra_field_length = ARGF.read(2).unpack("v").first + + file_name = ARGF.read(file_name_length) + ARGF.read(extra_field_length) # extra field.. no use (yet?) + + file_name2 = file_name + if use_file_names + outfile = nil + while !outfile + + if !(file_name2 =~ /\//) + used_filename = file_name2 + outfile = File.open("#{file_name2}.deflate","w") + end + file_name2 = "zip2gz_file%05i" % rand(100000) + + end + else + outfile = STDOUT + end + + + #generate gz header + #outfile.print "\x1f\x8b" + #outfile.putc c_method + + flags = 0 + + if include_file_names && !file_name.empty? + flags |= 0b1000 + end + + #outfile.print flags.chr + #outfile.print [0].pack("V") # no time conversion (yet) + #outfile.print 2.chr # maybe copy first 3 bits of the zip extra flags? default to maximum compression + #outfile.print 255.chr # this is stored in the central directory... (same for comments) we don't parse this... + + #if include_file_names && !file_name.empty? + # outfile.print file_name.tr("\x00"," ")+"\x00" + #end + + if size != :auto + blocks = size / 0x100 + rest = size & 0xFF + + blocks.times do + outfile.print ARGF.read(0x100) + end + + outfile.print ARGF.read(rest) + else + + size = -14 + + buffer = "" + loop do + buffer << ARGF.getc + size += 1 + if buffer.size > 14 + outfile.putc buffer[0] + buffer[0,1] = "" + end + + if buffer.size == 14 + crc, c_size, u_size, magic_number = buffer.unpack("VVVa2") + if c_size == size && c_size <= u_size && magic_number == "PK" + + break + + ARGF.ungetc "K" + ARGF.ungetc "P" + uncompressed_size = u_size + crc_32 = crc + end + end + + + end + end + + #outfile.print [crc_32, uncompressed_size ].pack("VV") + if file_name.empty? + warn "Successfully wrote file crc32:%08X length:%i to %s" % [crc_32, size, (outfile == STDOUT ? "stdout" : "#{used_filename}.deflate")] + else + warn "Successfully wrote file \"%s\" crc32:%08X length:%i to %s" % [file_name, crc_32, size, (outfile == STDOUT ? "stdout" : "#{used_filename}.deflate")] + end + + if use_file_names + outfile.close + end + +end + +