szsk (owner)

Revisions

gist: 4012 Download_button fork
public
Description:
D&D Base64 encode/decode
Public Clone URL: git://gist.github.com/4012.git
Embed All Files: show embed
ddbase64.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# D&D Base64 encode/decode
 
require "vr/vruby"
require "vr/clipboard"
require "digest/md5"
require "yaml"
 
# exerb用
# カレントディレクトリを実行ファイルと同じディレクトリにする
if $Exerb
Dir.chdir( ExerbRuntime.filepath.sub( /[\\\/][^\\\/]*?$/, "" ) )
end
 
 
 
CTypes = File.open( "mime.yaml" ) { |f| YAML.load( f ) }
 
def CTypes.ext( mime )
return self[ mime ] ? self[ mime ] : nil
end
 
def CTypes.mime( ext )
mime = self.index( ext )
return mime ? mime : "unknown-type"
end
 
 
 
class Base64
def initialize
@text = ""
@bin = ""
@mime = ""
@ext = ""
end
 
attr_accessor :text, :bin, :mime, :ext
end
 
def decode64( base64_text )
base64 = Base64.new
base64.text = base64_text
base64.text.gsub!( /^\s+|\s+$/, "" )
base64.text.gsub!( /base64,/, "" )
 
base64.text.gsub!( /data:(.*?);/ ) do
base64.mime = $1
""
end
 
ext = CTypes.ext( base64.mime )
base64.ext = "." + ext if ext
 
base64.bin = base64.text.unpack( "m" )[0]
 
return base64
end
 
def encode64( bin, file = "" )
base64 = Base64.new
base64.bin = bin
base64.text = [ base64.bin ].pack( "m" ).gsub( /\s/, "" )
 
mime = CTypes.mime( file.sub( /^[^.]+\./, "" ) )
base64.mime = mime if mime
 
return base64
end
 
class Base64Window < VRForm
def construct
if ARGV.length == 1 and File.exist?( ARGV[0] )
# ファイルを渡した場合、ファイルをバイナリから
# base64に変換してクリップボードに格納
bin = File.open( ARGV[0], "rb" ).read( )
base64 = encode64( bin, ARGV[0] )
b64text = "data:" + base64.mime + ";base64," + base64.text
 
# 正しく変換されているか元ファイルと比較
cmp64 = decode64( b64text )
 
if base64.bin == cmp64.bin
Clipboard.open( self.hWnd ) do |cb|
cb.setText( "data:" + base64.mime + ";base64," + base64.text )
end
else
raise "decode failed."
end
else
# 拡張子無しの場合、クリップボードの中身を
# base64としてバイナリに変換
Clipboard.open( self.hWnd ) do |cb|
cliptext = cb.getText
base64 = decode64( cliptext )
 
# 正しく変換されているか元ファイルと比較
cmp64 = encode64( base64.bin )
 
if cliptext == cmp64.text
md5 = Digest::MD5.hexdigest( base64.bin )
File.open( md5 + base64.ext, "wb" ) { |r| r.puts base64.bin }
else
raise "encode failed."
end
end
end
 
exit
end
end
 
VRLocalScreen.showForm( Base64Window, 0, 0, 0, 0 )
 
mime.yaml #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
---
application/octet-stream: exe
vide/mpeg: mpg
video/x-ms-asf: asf
application/exe: exe
audio/x-pn-realaudio: rm
text/plain: txt
application/pdf: pdf
text/html: html
video/x-mpeg: mpg
image/png: png
application/x-gzip: gz
image/x-png: png
application/x-stuffit: sit
vide/x-msvideo: avi
application/x-zip-compressed: zip
image/jpeg: jpg
application/zip: zip
application/x-tar: tar
image/x-ms-bmp: bmp
application/gzip: gz
audio/x-wav: wav
audio/wav: wav
audio/x-mpeg: mp3
audio/mpeg: mp3
image/tiff: tif
image/gif: gif
application/x-7zip-compressed: 7z
application/x-shockwave-flash: swf
image/x-bmp: bmp
application/postscript: ai
application/java-archiver: jar
text/xml: xml