Browse Source

add kzip raw compress stuff

optixx 14 years ago
parent
commit
2ad1d63ca6
5 changed files with 260 additions and 0 deletions
  1. BIN
      scripts/DeflOpt.exe
  2. 109 0
      scripts/conv_zip.py
  3. 5 0
      scripts/deflate.sh
  4. BIN
      scripts/kzip.exe
  5. 146 0
      scripts/zip2raw.rb

BIN
scripts/DeflOpt.exe


+ 109 - 0
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 <avr/pgmspace.h>       
+#include <loader.h>       
+''')
+
+
+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<len(data)-1:
+            cfile.write("0x%02x," % c)
+        else:
+            cfile.write("0x%02x" % c)
+        if idx and idx%16==0:
+            cfile.write("\n")
+    cfile.write('''
+    };
+''')
+
+cfile.write('PGM_VOID_P _rom[ROM_BUFFER_CNT]= {')
+for idx,val in enumerate(parts):
+    if idx<len(parts)-1:
+        cfile.write('''&_rom%02i,''' % (idx+1))
+    else:
+        cfile.write('''&_rom%02i''' % (idx+1))
+cfile.write('''};
+''')
+
+cfile.write('const int _rom_size[ROM_BUFFER_CNT] = {')
+for idx,val in enumerate(parts):
+    if idx<len(parts)-1:
+        cfile.write('''%i,''' % (val))
+    else:
+        cfile.write('''%i''' % (val))
+cfile.write('''};
+''')
+        
+cfile.close()
+
+shutil.copy("/tmp/loader.c", os.path.join(TARGET,"loader.c"))
+shutil.copy("/tmp/loader.h", os.path.join(TARGET,"loader.h"))
+print "wrote loader.c"
+print "wrote loader.h"

+ 5 - 0
scripts/deflate.sh

@@ -0,0 +1,5 @@
+rm -rf rom.zip
+wine kzip.exe rom /s1 ../roms/qd16boot02.smc
+wine DeflOpt.exe /a rom.zip
+ruby zip2raw.rb rom.zip
+rm -rf rom.zip

BIN
scripts/kzip.exe


+ 146 - 0
scripts/zip2raw.rb

@@ -0,0 +1,146 @@
+
+use_file_names = true
+include_file_names = false
+
+loop do
+
+
+  if ARGF.read(2) != "PK" 
+      if ARGF.eof?
+        exit
+      end
+      warn "Input is not a zip file"
+      exit 1
+  end
+
+  if !(ARGF.read(2) == "\003\004") # file header
+    exit
+  end
+  
+  version = ARGF.read(2).unpack("v").first
+
+  # i should use this to sort out some incompatible file formats....
+
+  
+
+  bitflag = ( ARGF.read(2).unpack("v").first)
+
+  use_data_descriptor =  bitflag[3] == 1
+
+  # should check for things that are unsupported for sure....
+  c_method = ARGF.read(2).unpack("v").first
+  if c_method != 8
+      warn "Unknown compression method"
+  end
+  
+  if c_method > 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
+
+