#!/usr/bin/ruby #------------------------------------------------- # Rero2's Restrict Data Archiver (RRDA) # # Archive file 生成スクリプト - Ver1.00 # Jan.08.2000 # Programed by Rero2(K.Kunikane) # #------------------------------------------------- #--------------------------------------- # 設定なぞ #--------------------------------------- Header = "RRDA - Ver1.00" ZipDir = "zipworks" ArcFile = "rrda.dat" #--------------------------------------- # クラス宣言あれこれ #--------------------------------------- # (構造体の代わりにしか使っていないのが恥ずかしい) #----------------------------------- # ファイル名保持クラス class Dat # クラス(データ)の初期化 def initialize(dataname, filename) @data = dataname @file = filename @zipfile = nil @zipped = false @zipsize = 0 end attr_accessor:data attr_accessor:file attr_accessor:zipfile # データの圧縮処理 def zip # フォルダがなければ掘る if FileTest::exist?(ZipDir) if (FileTest::directory?(ZipDir) == false) print "\nError!!\n" print "Can't make directory "+ZipDir+".\n" exit end else Dir::mkdir(ZipDir) end # ファイルネーム準備 filename = File::basename(@file) @zipfile = ZipDir+"/"+filename+".gz" # ファイルコピー、圧縮 command = "cp "+@file+" "+ZipDir+"/"+filename system(command) command = "gzip -9 "+ZipDir+"/"+filename system(command) end # 圧縮データのファイルサイズを渡す def filesize? if @zipsize == 0 zip if (@zipfile != nil) && (FileTest::exist?(@zipfile)) @zipsize = FileTest::size?(@zipfile) end @zipsize else @zipsize end end end #--------------------------------------- # Main Routine - 本体 #--------------------------------------- #----------------- # arg のファイル名をファイルリストとみなし一覧をがーっと # 読み込むあたり # mode = false output = ArcFile linenum = 0 list1 = Array.new list2 = Array.new key = [nil, nil, nil, nil] while line = gets() linenum += 1 # コメントアウト next if line =~ /^#/ # ブランクスキップ next if line =~ /^$/ # 出力データネームの指定 if line =~ /^\\name\s(.*)/ output = $1 next end # レジストキーの指定 if line =~ /^\\key\s(.*)/ key[0] = $1[0] key[1] = $1[1] key[2] = $1[2] key[3] = $1[3] next end # 制限モードへの移行 if line =~ /^\\restriction/ mode = true next end # 制限モードの解除(隠し(笑) if line =~ /^\\open/ mode = false next end # 他はファイル指定とみなす line.delete!("\s") line.chop! word = line.split(",") if word.size != 2 print "Error!!\n" printf("File Name Error in %d\n", linenum); exit end if mode == false list1.push(Dat.new(word[0], word[1])) else list2.push(Dat.new(word[0], word[1])) end end #----------------- # ファイルを gzip 圧縮するあたり # list1.each do |i| print File::basename(i.file)+"->" size = i.filesize? print i.zipfile printf(" (%dbyte)\n", size) end list2.each do |i| print File::basename(i.file)+"->" size = i.filesize? print i.zipfile printf(" (%dbyte)\n", size) end #----------------- # 前半ヘッダー仮生成 # out = File.open(ZipDir+"/header_f.lst", "w") out.puts(Header) out.printf("%08x\n", 0); out.printf("%08x,%08x\n", 0, 0); d1_size = 0 list1.each do |i| size = i.filesize? out.printf("%08x,%08x,%s\n", d1_size, size, i.data); d1_size += size end out.puts("END") out.close h1_size = FileTest::size?(ZipDir+"/header_f.lst") d2_size = h1_size + d1_size #----------------- # 後半ヘッダー生成 # out = File.open(ZipDir+"/header_r.lst", "w") out.puts(Header) out.printf("%08x\n", 0); out.printf("%08x,%08x\n", 0, 0); list2.each do |i| size = i.filesize? out.printf("%08x,%08x,%s\n", d2_size, size, i.data); d2_size += size end out.puts("END") out.close command = "gzip "+ZipDir+"/header_r.lst" system(command) h2_size = FileTest::size?(ZipDir+"/header_r.lst.gz") #----------------- # 前半ヘッダー再生成 # out = File.open(ZipDir+"/header_f.lst", "w") out.puts(Header) out.printf("%08x\n", h1_size); out.printf("%08x,%08x\n", d2_size, h2_size); d1_size = h1_size list1.each do |i| size = i.filesize? out.printf("%08x,%08x,%s\n", d1_size, size, i.data); d1_size += size end out.puts("END") out.close #----------------- # 前半データアーカイブ # out = File.open(ZipDir+"/part_a.tmp", "w") out.binmode parts = File.open(ZipDir+"/header_f.lst", "r") parts.binmode tmp = parts.read out.write(tmp) parts.close list1.each do |i| parts = File.open(i.zipfile, "r") parts.binmode tmp = parts.read out.write(tmp) parts.close end out.close #----------------- # 後半データアーカイブ # out = File.open(ZipDir+"/part_b.tmp", "w") out.binmode list2.each do |i| parts = File.open(i.zipfile, "r") parts.binmode tmp = parts.read out.write(tmp) parts.close end parts = File.open(ZipDir+"/header_r.lst.gz", "r") parts.binmode tmp = parts.read out.write(tmp) parts.close out.close #----------------- # レジストキー練り込み # print "dope key code...\n" parts = File.open(ZipDir+"/part_b.tmp", "r") parts.binmode tmp = parts.read parts.close key_ptr = d1_size i = 0 while i < tmp.size tmp[i] = (tmp[i] ^ 0xaa) ^ key[key_ptr % 4] i += 1 key_ptr += 1 end out = File.open(ZipDir+"/part_b.tmp", "w") out.binmode out.write(tmp) out.close #----------------- # 最終組み立て # print "building.\n" parts = File.open(ZipDir+"/part_a.tmp", "r") parts.binmode tmp1 = parts.read parts.close parts = File.open(ZipDir+"/part_b.tmp", "r") parts.binmode tmp2 = parts.read parts.close out = File.open(output, "w") out.binmode out.write(tmp1) out.write(tmp2) out.close #----------------- # 後始末 # command = "rm -rf "+ZipDir system(command) print "done.\n"