ciphr 0.0.2 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,44 +1,46 @@
1
- # strictly used by parser classes for literals/wiring
2
- # TODO: disable registration
3
-
4
- module Ciphr::Functions::Reader
5
- class StringReader < Ciphr::Functions::Function
6
- def apply
7
- StringProc.new(options[:string])
8
- end
9
-
10
- class StringProc #extend Proc?
11
- def initialize(str)
12
- @str = str
13
- end
14
-
15
- def call
16
- begin
17
- @str
18
- ensure
19
- @str = nil
20
- end
21
- end
22
- end
23
- end
24
-
25
- class FileReader < Ciphr::Functions::Function
26
- def apply
27
- f = File.open(options[:file], "r")
28
- Proc.new do
29
- chunk = f.read(256)
30
- f.close if ! chunk
31
- chunk
32
- end
33
- end
34
- end
35
-
36
- class IoReader < Ciphr::Functions::Function
37
- def apply
38
- input = args[0]
39
- Proc.new do
40
- input.read(256)
41
- end
42
- end
43
- end
44
- end
1
+ # strictly used by parser classes for literals/wiring
2
+ # TODO: disable registration
3
+
4
+ module Ciphr::Functions::Reader
5
+ class StringReader < Ciphr::Functions::Function
6
+ def apply
7
+ StringProc.new(options[:string])
8
+ end
9
+
10
+ class StringProc #extend Proc?
11
+ def initialize(str)
12
+ @str = str
13
+ end
14
+
15
+ def call
16
+ begin
17
+ @str
18
+ ensure
19
+ @str = nil
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
+ class FileReader < Ciphr::Functions::Function
26
+ def apply
27
+ f = File.open(options[:file], "r")
28
+ f.binmode
29
+ Proc.new do
30
+ chunk = f.read(256)
31
+ f.close if ! chunk
32
+ chunk
33
+ end
34
+ end
35
+ end
36
+
37
+ class IoReader < Ciphr::Functions::Function
38
+ def apply
39
+ input = args[0]
40
+ input.binmode
41
+ Proc.new do
42
+ input.read(256)
43
+ end
44
+ end
45
+ end
46
+ end
@@ -1,121 +1,121 @@
1
- module Ciphr::Functions::Simple
2
- class Cat < Ciphr::Functions::Function
3
- def self.variants
4
- [[['cat','catenate'], {}]]
5
- end
6
-
7
- def self.params
8
- [:input]
9
- end
10
-
11
- def apply
12
- inputs = @args
13
- i = 0
14
- chunk = nil
15
- Proc.new do
16
- chunk = inputs[i].read(256)
17
- if ! chunk
18
- i += 1
19
- chunk = inputs[i] && inputs[i].read(256)
20
- end
21
- #while !(chunk = inputs[i].read(256)) && i < inputs.size
22
- # i++
23
- #end
24
- chunk
25
- end
26
- end
27
- end
28
-
29
- class Repack < Ciphr::Functions::Function
30
- def apply
31
- input, ch1in, ch2in = @args
32
- content, ch1, ch2 = [input.read, ch1in.read, ch2in.read]
33
- Proc.new do
34
- if content
35
- begin
36
- content.unpack(ch1).pack(ch2)
37
- ensure
38
- content = nil
39
- end
40
- else
41
- nil
42
- end
43
- end
44
- end
45
-
46
- def self.variants
47
- [ [['repack'], {}] ]
48
- end
49
-
50
- def self.params
51
- [:input,:ch1,:ch2]
52
- end
53
- end
54
-
55
- class Translate < Ciphr::Functions::Function
56
- def apply
57
- input, ch1in, ch2in = @args
58
- ch1, ch2 = [ch1in.read, ch2in.read]
59
- Proc.new do
60
- inchunk = input.read(1)
61
- if inchunk
62
- inchunk.tr(ch1, ch2)
63
- else
64
- nil
65
- end
66
- end
67
- end
68
-
69
- def self.variants
70
- [ [['tr','translate'], {}] ]
71
- end
72
-
73
- def self.params
74
- [:input,:ch1,:ch2]
75
- end
76
- end
77
-
78
- class Replace < Ciphr::Functions::Function
79
- def apply
80
- input, searchin, replacein = @args
81
- search, replace = [searchin.read, replacein.read]
82
- buf = ""
83
- Proc.new do
84
- if buf.size == search.size && search.size > 0
85
- buf = ""
86
- replace
87
- else
88
- inchunk = input.read(1)
89
- if inchunk
90
- if inchunk == search[buf.size]
91
- buf += inchunk
92
- ""
93
- else
94
- buf += inchunk
95
- input.prepend(buf[1,buf.size])
96
- ret = buf[0]
97
- buf = ""
98
- ret
99
- end
100
- else
101
- if buf.size > 0
102
- ret = buf
103
- buf = ""
104
- ret
105
- else
106
- nil
107
- end
108
- end
109
- end
110
- end
111
- end
112
-
113
- def self.variants
114
- [ [['repl','replace'], {}] ]
115
- end
116
-
117
- def self.params
118
- [:input,:search,:replace]
119
- end
120
- end
121
- end
1
+ module Ciphr::Functions::Simple
2
+ class Cat < Ciphr::Functions::Function
3
+ def self.variants
4
+ [[['cat','catenate'], {}]]
5
+ end
6
+
7
+ def self.params
8
+ [:input]
9
+ end
10
+
11
+ def apply
12
+ inputs = @args
13
+ i = 0
14
+ chunk = nil
15
+ Proc.new do
16
+ chunk = inputs[i].read(256)
17
+ if ! chunk
18
+ i += 1
19
+ chunk = inputs[i] && inputs[i].read(256)
20
+ end
21
+ #while !(chunk = inputs[i].read(256)) && i < inputs.size
22
+ # i++
23
+ #end
24
+ chunk
25
+ end
26
+ end
27
+ end
28
+
29
+ class Repack < Ciphr::Functions::Function
30
+ def apply
31
+ input, ch1in, ch2in = @args
32
+ content, ch1, ch2 = [input.read, ch1in.read, ch2in.read]
33
+ Proc.new do
34
+ if content
35
+ begin
36
+ content.unpack(ch1).pack(ch2)
37
+ ensure
38
+ content = nil
39
+ end
40
+ else
41
+ nil
42
+ end
43
+ end
44
+ end
45
+
46
+ def self.variants
47
+ [ [['repack'], {}] ]
48
+ end
49
+
50
+ def self.params
51
+ [:input,:ch1,:ch2]
52
+ end
53
+ end
54
+
55
+ class Translate < Ciphr::Functions::Function
56
+ def apply
57
+ input, ch1in, ch2in = @args
58
+ ch1, ch2 = [ch1in.read, ch2in.read]
59
+ Proc.new do
60
+ inchunk = input.read(1)
61
+ if inchunk
62
+ inchunk.tr(ch1, ch2)
63
+ else
64
+ nil
65
+ end
66
+ end
67
+ end
68
+
69
+ def self.variants
70
+ [ [['tr','translate'], {}] ]
71
+ end
72
+
73
+ def self.params
74
+ [:input,:ch1,:ch2]
75
+ end
76
+ end
77
+
78
+ class Replace < Ciphr::Functions::Function
79
+ def apply
80
+ input, searchin, replacein = @args
81
+ search, replace = [searchin.read, replacein.read]
82
+ buf = ""
83
+ Proc.new do
84
+ if buf.size == search.size && search.size > 0
85
+ buf = ""
86
+ replace
87
+ else
88
+ inchunk = input.read(1)
89
+ if inchunk
90
+ if inchunk == search[buf.size]
91
+ buf += inchunk
92
+ ""
93
+ else
94
+ buf += inchunk
95
+ input.prepend(buf[1,buf.size])
96
+ ret = buf[0]
97
+ buf = ""
98
+ ret
99
+ end
100
+ else
101
+ if buf.size > 0
102
+ ret = buf
103
+ buf = ""
104
+ ret
105
+ else
106
+ nil
107
+ end
108
+ end
109
+ end
110
+ end
111
+ end
112
+
113
+ def self.variants
114
+ [ [['repl','replace'], {}] ]
115
+ end
116
+
117
+ def self.params
118
+ [:input,:search,:replace]
119
+ end
120
+ end
121
+ end
@@ -1,38 +1,38 @@
1
- require 'cgi'
2
-
3
- module Ciphr::Functions::URL
4
- #TODO: differentiate between URL and CGI encoding (with '+' char)
5
- class UrlEncoding < Ciphr::Functions::InvertibleFunction
6
- def apply
7
- input = @args[0]
8
- if !invert
9
- Proc.new do
10
- chunk = input.read(1)
11
- chunk && CGI.escape(chunk)
12
- end
13
- else
14
- Proc.new do
15
- chunk = input.read(1)
16
- if (chunk == "%")
17
- chunk += input.read(2)
18
- chunk && CGI.unescape(chunk)
19
- elsif chunk == '+'
20
- ' '
21
- else
22
- chunk
23
- end
24
- end
25
- end
26
- end
27
-
28
- def self.variants
29
- [
30
- [['url','uri','cgi'],{}]
31
- ]
32
- end
33
-
34
- def self.params
35
- [:input]
36
- end
37
- end
38
- end
1
+ require 'cgi'
2
+
3
+ module Ciphr::Functions::URL
4
+ #TODO: differentiate between URL and CGI encoding (with '+' char)
5
+ class UrlEncoding < Ciphr::Functions::InvertibleFunction
6
+ def apply
7
+ input = @args[0]
8
+ if !invert
9
+ Proc.new do
10
+ chunk = input.read(1)
11
+ chunk && CGI.escape(chunk)
12
+ end
13
+ else
14
+ Proc.new do
15
+ chunk = input.read(1)
16
+ if (chunk == "%")
17
+ chunk += input.read(2)
18
+ chunk && CGI.unescape(chunk)
19
+ elsif chunk == '+'
20
+ ' '
21
+ else
22
+ chunk
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ def self.variants
29
+ [
30
+ [['url','uri','cgi'],{}]
31
+ ]
32
+ end
33
+
34
+ def self.params
35
+ [:input]
36
+ end
37
+ end
38
+ end
@@ -1,94 +1,95 @@
1
- require 'zlib'
2
-
3
- module Ciphr::Functions::ZLib
4
- class Deflate < Ciphr::Functions::InvertibleFunction
5
- def apply
6
- input = @args[0]
7
- zstream = invert ? Zlib::Inflate.new : Zlib::Deflate.new
8
- Proc.new do
9
- chunk = input.read(256)
10
- if chunk
11
- if invert
12
- zstream.inflate(chunk)
13
- else
14
- zstream.deflate(chunk,Zlib::SYNC_FLUSH)
15
- end
16
- else
17
- begin
18
- #zstream.finish if invert
19
- ensure
20
- zstream.close
21
- end
22
- end
23
- end
24
- end
25
-
26
- def self.variants
27
- [
28
- [['deflate'], {}]
29
- ]
30
- end
31
-
32
- def self.params
33
- [:input]
34
- end
35
- end
36
-
37
-
38
- class Gzip < Ciphr::Functions::InvertibleFunction
39
- class UncloseableIOProxy # hack to prevent GzipWriter from closing StringIO
40
- def initialize(delegate)
41
- @delegate = delegate
42
- end
43
-
44
- def method_missing(meth, *args, &block)
45
- if meth.to_s != "close"
46
- @delegate.send(meth, *args, &block)
47
- else
48
- nil
49
- end
50
- end
51
- end
52
-
53
- def apply
54
- input = @args[0]
55
- sio = StringIO.new
56
- gz = !invert ? Zlib::GzipWriter.new(UncloseableIOProxy.new(sio)) : Zlib::GzipReader.new(input)
57
- Proc.new do
58
- if invert # unzip
59
- gz.read(256)
60
- else # zip
61
- chunk = input.read(256)
62
- if chunk
63
- gz.write chunk
64
- sio.rewind
65
- ret = sio.read
66
- sio.rewind
67
- sio.truncate(0)
68
- ret
69
- elsif gz
70
- gz.close
71
- gz = nil
72
- sio.rewind
73
- ret = sio.read
74
- sio.rewind
75
- sio.truncate(0)
76
- ret
77
- else
78
- nil
79
- end
80
- end
81
- end
82
- end
83
-
84
- def self.variants
85
- [
86
- [['gzip','gz'], {}]
87
- ]
88
- end
89
-
90
- def self.params
91
- [:input]
92
- end
93
- end
94
- end
1
+ require 'zlib'
2
+
3
+ module Ciphr::Functions::ZLib
4
+ class Deflate < Ciphr::Functions::InvertibleFunction
5
+ def apply
6
+ input = @args[0]
7
+ zstream = invert ? Zlib::Inflate.new : Zlib::Deflate.new
8
+ Proc.new do
9
+ chunk = input.read(256)
10
+ if chunk
11
+ if invert
12
+ zstream.inflate(chunk)
13
+ else
14
+ zstream.deflate(chunk,Zlib::SYNC_FLUSH)
15
+ end
16
+ else
17
+ begin
18
+ #zstream.finish if invert
19
+ ensure
20
+ zstream.close
21
+ end
22
+ end
23
+ end
24
+ end
25
+
26
+ def self.variants
27
+ [
28
+ [['deflate'], {}]
29
+ ]
30
+ end
31
+
32
+ def self.params
33
+ [:input]
34
+ end
35
+ end
36
+
37
+
38
+ class Gzip < Ciphr::Functions::InvertibleFunction
39
+ class UncloseableIOProxy # hack to prevent GzipWriter from closing StringIO
40
+ def initialize(delegate)
41
+ @delegate = delegate
42
+ end
43
+
44
+ def method_missing(meth, *args, &block)
45
+ if meth.to_s != "close"
46
+ @delegate.send(meth, *args, &block)
47
+ else
48
+ nil
49
+ end
50
+ end
51
+ end
52
+
53
+ def apply
54
+ input = @args[0]
55
+ sio = StringIO.new
56
+ sio.binmode
57
+ gz = !invert ? Zlib::GzipWriter.new(UncloseableIOProxy.new(sio)) : Zlib::GzipReader.new(input)
58
+ Proc.new do
59
+ if invert # unzip
60
+ gz.read(256)
61
+ else # zip
62
+ chunk = input.read(256)
63
+ if chunk
64
+ gz.write chunk
65
+ sio.rewind
66
+ ret = sio.read
67
+ sio.rewind
68
+ sio.truncate(0)
69
+ ret
70
+ elsif gz
71
+ gz.close
72
+ gz = nil
73
+ sio.rewind
74
+ ret = sio.read
75
+ sio.rewind
76
+ sio.truncate(0)
77
+ ret
78
+ else
79
+ nil
80
+ end
81
+ end
82
+ end
83
+ end
84
+
85
+ def self.variants
86
+ [
87
+ [['gzip','gz'], {}]
88
+ ]
89
+ end
90
+
91
+ def self.params
92
+ [:input]
93
+ end
94
+ end
95
+ end