flatulent 0.0.3 → 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.
Files changed (51) hide show
  1. data/flatulent-0.0.4.gem +0 -0
  2. data/install.rb +210 -0
  3. data/lib/flatulent.rb +76 -61
  4. data/rails/app/controllers/flatulent_controller.rb +6 -4
  5. data/rails/log/development.log +7242 -0
  6. data/rails/tmp/sessions/ruby_sess.82080df62364f531 +0 -0
  7. data/rails/tmp/sessions/ruby_sess.8900d08ad306f253 +0 -0
  8. data/rails/tmp/sessions/ruby_sess.f8fa47c3571d2497 +0 -0
  9. metadata +7 -49
  10. data/flatulent-0.0.2.gem +0 -0
  11. data/rails/lib/flatulent.rb +0 -450
  12. data/rails/lib/flatulent/attributes.rb +0 -79
  13. data/rails/lib/flatulent/crypt/blowfish-tables.rb +0 -190
  14. data/rails/lib/flatulent/crypt/blowfish.rb +0 -109
  15. data/rails/lib/flatulent/crypt/cbc.rb +0 -123
  16. data/rails/lib/flatulent/crypt/gost.rb +0 -140
  17. data/rails/lib/flatulent/crypt/idea.rb +0 -193
  18. data/rails/lib/flatulent/crypt/noise.rb +0 -94
  19. data/rails/lib/flatulent/crypt/purerubystringio.rb +0 -378
  20. data/rails/lib/flatulent/crypt/rijndael-tables.rb +0 -117
  21. data/rails/lib/flatulent/crypt/rijndael.rb +0 -269
  22. data/rails/lib/flatulent/fontfiles/banner.flf +0 -2494
  23. data/rails/lib/flatulent/fontfiles/big.flf +0 -2204
  24. data/rails/lib/flatulent/fontfiles/block.flf +0 -1691
  25. data/rails/lib/flatulent/fontfiles/bubble.flf +0 -1630
  26. data/rails/lib/flatulent/fontfiles/digital.flf +0 -1286
  27. data/rails/lib/flatulent/fontfiles/ivrit.flf +0 -900
  28. data/rails/lib/flatulent/fontfiles/lean.flf +0 -1691
  29. data/rails/lib/flatulent/fontfiles/mini.flf +0 -899
  30. data/rails/lib/flatulent/fontfiles/mnemonic.flf +0 -3702
  31. data/rails/lib/flatulent/fontfiles/script.flf +0 -1493
  32. data/rails/lib/flatulent/fontfiles/shadow.flf +0 -1097
  33. data/rails/lib/flatulent/fontfiles/slant.flf +0 -1295
  34. data/rails/lib/flatulent/fontfiles/small.flf +0 -1097
  35. data/rails/lib/flatulent/fontfiles/smscript.flf +0 -1097
  36. data/rails/lib/flatulent/fontfiles/smshadow.flf +0 -899
  37. data/rails/lib/flatulent/fontfiles/smslant.flf +0 -1097
  38. data/rails/lib/flatulent/fontfiles/standard.flf +0 -2227
  39. data/rails/lib/flatulent/fontfiles/term.flf +0 -600
  40. data/rails/lib/flatulent/pervasives.rb +0 -32
  41. data/rails/lib/flatulent/stringxor.rb +0 -27
  42. data/rails/lib/flatulent/text.rb +0 -6
  43. data/rails/lib/flatulent/text/double_metaphone.rb +0 -356
  44. data/rails/lib/flatulent/text/figlet.rb +0 -17
  45. data/rails/lib/flatulent/text/figlet/font.rb +0 -117
  46. data/rails/lib/flatulent/text/figlet/smusher.rb +0 -64
  47. data/rails/lib/flatulent/text/figlet/typesetter.rb +0 -68
  48. data/rails/lib/flatulent/text/levenshtein.rb +0 -65
  49. data/rails/lib/flatulent/text/metaphone.rb +0 -97
  50. data/rails/lib/flatulent/text/porter_stemming.rb +0 -171
  51. data/rails/lib/flatulent/text/soundex.rb +0 -61
@@ -1,140 +0,0 @@
1
- # gost.rb
2
- # Adapted by Richard Kernahan <kernighan_rich@rubyforge.org>
3
- # from C++ code written by Wei Dai
4
- # of the Crypto++ project http://www.eskimo.com/~weidai/cryptlib.html
5
-
6
- module Crypt
7
- class Gost
8
-
9
- require 'crypt/cbc'
10
- include CBC
11
-
12
- ULONG = 0x100000000
13
-
14
- def block_size
15
- return(8)
16
- end
17
-
18
-
19
- def initialize(userKey)
20
-
21
- # These are the S-boxes given in Applied Cryptography 2nd Ed., p. 333
22
- @sBox = [
23
- [4, 10, 9, 2, 13, 8, 0, 14, 6, 11, 1, 12, 7, 15, 5, 3],
24
- [14, 11, 4, 12, 6, 13, 15, 10, 2, 3, 8, 1, 0, 7, 5, 9],
25
- [5, 8, 1, 13, 10, 3, 4, 2, 14, 15, 12, 7, 6, 0, 9, 11],
26
- [7, 13, 10, 1, 0, 8, 9, 15, 14, 4, 6, 12, 11, 2, 5, 3],
27
- [6, 12, 7, 1, 5, 15, 13, 8, 4, 10, 9, 14, 0, 3, 11, 2],
28
- [4, 11, 10, 0, 7, 2, 1, 13, 3, 6, 8, 5, 9, 12, 15, 14],
29
- [13, 11, 4, 1, 3, 15, 5, 9, 0, 10, 14, 7, 6, 8, 2, 12],
30
- [1, 15, 13, 0, 5, 7, 10, 4, 9, 2, 3, 14, 6, 11, 8, 12]
31
- ]
32
-
33
- # These are the S-boxes given in the GOST source code listing in Applied
34
- # Cryptography 2nd Ed., p. 644. They appear to be from the DES S-boxes
35
- # [13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7 ],
36
- # [ 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1 ],
37
- # [12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11 ],
38
- # [ 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9 ],
39
- # [ 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15 ],
40
- # [10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8 ],
41
- # [15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10 ],
42
- # [14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7 ]
43
-
44
- # precalculate the S table
45
- @sTable = precalculate_S_table()
46
-
47
- # derive the 32-byte key from the user-supplied key
48
- userKeyLength = userKey.length
49
- @key = userKey[0..31].unpack('C'*32)
50
- if (userKeyLength < 32)
51
- userKeyLength.upto(31) { @key << 0 }
52
- end
53
- end
54
-
55
-
56
- def precalculate_S_table()
57
- sTable = [[], [], [], []]
58
- 0.upto(3) { |i|
59
- 0.upto(255) { |j|
60
- t = @sBox[2*i][j % 16] | (@sBox[2*i+1][j/16] << 4)
61
- u = (8*i + 11) % 32
62
- v = (t << u) | (t >> (32-u))
63
- sTable[i][j] = (v % ULONG)
64
- }
65
- }
66
- return(sTable)
67
- end
68
-
69
-
70
- def f(longWord)
71
- longWord = longWord % ULONG
72
- a, b, c, d = [longWord].pack('L').unpack('CCCC')
73
- return(@sTable[3][d] ^ @sTable[2][c] ^ @sTable[1][b] ^ @sTable[0][a])
74
- end
75
-
76
-
77
- def encrypt_pair(xl, xr)
78
- 3.times {
79
- xr ^= f(xl+@key[0])
80
- xl ^= f(xr+@key[1])
81
- xr ^= f(xl+@key[2])
82
- xl ^= f(xr+@key[3])
83
- xr ^= f(xl+@key[4])
84
- xl ^= f(xr+@key[5])
85
- xr ^= f(xl+@key[6])
86
- xl ^= f(xr+@key[7])
87
- }
88
- xr ^= f(xl+@key[7])
89
- xl ^= f(xr+@key[6])
90
- xr ^= f(xl+@key[5])
91
- xl ^= f(xr+@key[4])
92
- xr ^= f(xl+@key[3])
93
- xl ^= f(xr+@key[2])
94
- xr ^= f(xl+@key[1])
95
- xl ^= f(xr+@key[0])
96
- return([xr, xl])
97
- end
98
-
99
-
100
- def decrypt_pair(xl, xr)
101
- xr ^= f(xl+@key[0])
102
- xl ^= f(xr+@key[1])
103
- xr ^= f(xl+@key[2])
104
- xl ^= f(xr+@key[3])
105
- xr ^= f(xl+@key[4])
106
- xl ^= f(xr+@key[5])
107
- xr ^= f(xl+@key[6])
108
- xl ^= f(xr+@key[7])
109
- 3.times {
110
- xr ^= f(xl+@key[7])
111
- xl ^= f(xr+@key[6])
112
- xr ^= f(xl+@key[5])
113
- xl ^= f(xr+@key[4])
114
- xr ^= f(xl+@key[3])
115
- xl ^= f(xr+@key[2])
116
- xr ^= f(xl+@key[1])
117
- xl ^= f(xr+@key[0])
118
- }
119
- return([xr, xl])
120
- end
121
-
122
-
123
- def encrypt_block(block)
124
- xl, xr = block.unpack('NN')
125
- xl, xr = encrypt_pair(xl, xr)
126
- encrypted = [xl, xr].pack('NN')
127
- return(encrypted)
128
- end
129
-
130
-
131
- def decrypt_block(block)
132
- xl, xr = block.unpack('NN')
133
- xl, xr = decrypt_pair(xl, xr)
134
- decrypted = [xl, xr].pack('NN')
135
- return(decrypted)
136
- end
137
-
138
-
139
- end
140
- end
@@ -1,193 +0,0 @@
1
- # idea.rb Richard Kernahan <kernighan_rich@rubyforge.org>
2
-
3
- # IDEA (International Data Encryption Algorithm) by
4
- # Xuejia Lai and James Massey (1992). Refer to license info at end.
5
- # Ported by Richard Kernahan 2005
6
-
7
- module Crypt
8
- class IDEA
9
-
10
- require 'crypt/cbc'
11
- include Crypt::CBC
12
-
13
- require 'digest/md5'
14
-
15
- ULONG = 0x100000000
16
- USHORT = 0x10000
17
-
18
- ENCRYPT = 0
19
- DECRYPT = 1
20
-
21
-
22
- def block_size
23
- return(8)
24
- end
25
-
26
-
27
- def initialize(key128, mode)
28
- # IDEA is subject to attack unless the key is sufficiently random, so we
29
- # take an MD5 digest of a variable-length passphrase to ensure a solid key
30
- if (key128.class == String)
31
- digest = Digest::MD5.new(key128).digest
32
- key128 = digest.unpack('n'*8)
33
- end
34
- raise "Key must be 128 bits (8 words)" unless (key128.class == Array) && (key128.length == 8)
35
- raise "Mode must be IDEA::ENCRYPT or IDEA::DECRYPT" unless ((mode == ENCRYPT) | (mode == DECRYPT))
36
- if (mode == ENCRYPT)
37
- @subkeys = generate_encryption_subkeys(key128)
38
- else (mode == DECRYPT)
39
- @subkeys = generate_decryption_subkeys(key128)
40
- end
41
- end
42
-
43
-
44
- def mul(a, b)
45
- modulus = 0x10001
46
- return((1 - b) % USHORT) if (a == 0)
47
- return((1 - a) % USHORT) if (b == 0)
48
- return((a * b) % modulus)
49
- end
50
-
51
-
52
- def mulInv(x)
53
- modulus = 0x10001
54
- x = x.to_i % USHORT
55
- return(x) if (x <= 1)
56
- t1 = USHORT / x
57
- y = modulus % x
58
- if (y == 1)
59
- inv = (1 - t1) & 0xFFFF
60
- return(inv)
61
- end
62
- t0 = 1
63
- while (y != 1)
64
- q = x / y
65
- x = x % y
66
- t0 = t0 + (q * t1)
67
- return(t0) if (x == 1)
68
- q = y / x
69
- y = y % x
70
- t1 = t1 + (q * t0)
71
- end
72
- inv = (1 - t1) & 0xFFFF
73
- return(inv)
74
- end
75
-
76
-
77
- def generate_encryption_subkeys(key)
78
- encrypt_keys = []
79
- encrypt_keys[0..7] = key.dup
80
- 8.upto(51) { |i|
81
- a = ((i + 1) % 8 > 0) ? (i-7) : (i-15)
82
- b = ((i + 2) % 8 < 2) ? (i-14) : (i-6)
83
- encrypt_keys[i] = ((encrypt_keys[a] << 9) | (encrypt_keys[b] >> 7)) % USHORT
84
- }
85
- return(encrypt_keys)
86
- end
87
-
88
-
89
- def generate_decryption_subkeys(key)
90
- encrypt_keys = generate_encryption_subkeys(key)
91
- decrypt_keys = []
92
- decrypt_keys[48] = mulInv(encrypt_keys.shift)
93
- decrypt_keys[49] = (-encrypt_keys.shift) % USHORT
94
- decrypt_keys[50] = (-encrypt_keys.shift) % USHORT
95
- decrypt_keys[51] = mulInv(encrypt_keys.shift)
96
- 42.step(0, -6) { |i|
97
- decrypt_keys[i+4] = encrypt_keys.shift % USHORT
98
- decrypt_keys[i+5] = encrypt_keys.shift % USHORT
99
- decrypt_keys[i] = mulInv(encrypt_keys.shift)
100
- if (i ==0)
101
- decrypt_keys[1] = (-encrypt_keys.shift) % USHORT
102
- decrypt_keys[2] = (-encrypt_keys.shift) % USHORT
103
- else
104
- decrypt_keys[i+2] = (-encrypt_keys.shift) % USHORT
105
- decrypt_keys[i+1] = (-encrypt_keys.shift) % USHORT
106
- end
107
- decrypt_keys[i+3] = mulInv(encrypt_keys.shift)
108
- }
109
- return(decrypt_keys)
110
- end
111
-
112
-
113
- def crypt_pair(l, r)
114
- word = [l, r].pack('NN').unpack('nnnn')
115
- k = @subkeys[0..51]
116
- 8.downto(1) { |i|
117
- word[0] = mul(word[0], k.shift)
118
- word[1] = (word[1] + k.shift) % USHORT
119
- word[2] = (word[2] + k.shift) % USHORT
120
- word[3] = mul(word[3], k.shift)
121
- t2 = word[0] ^ word[2]
122
- t2 = mul(t2, k.shift)
123
- t1 = (t2 + (word[1] ^ word[3])) % USHORT
124
- t1 = mul(t1, k.shift)
125
- t2 = (t1 + t2) % USHORT
126
- word[0] ^= t1
127
- word[3] ^= t2
128
- t2 ^= word[1]
129
- word[1] = word[2] ^ t1
130
- word[2] = t2
131
- }
132
- result = []
133
- result << mul(word[0], k.shift)
134
- result << (word[2] + k.shift) % USHORT
135
- result << (word[1] + k.shift) % USHORT
136
- result << mul(word[3], k.shift)
137
- twoLongs = result.pack('nnnn').unpack('NN')
138
- return(twoLongs)
139
- end
140
-
141
- def encrypt_block(block)
142
- xl, xr = block.unpack('NN')
143
- xl, xr = crypt_pair(xl, xr)
144
- encrypted = [xl, xr].pack('NN')
145
- return(encrypted)
146
- end
147
-
148
-
149
- def decrypt_block(block)
150
- xl, xr = block.unpack('NN')
151
- xl, xr = crypt_pair(xl, xr)
152
- decrypted = [xl, xr].pack('NN')
153
- return(decrypted)
154
- end
155
-
156
-
157
- end
158
- end
159
-
160
- # LICENSE INFORMATION
161
- #
162
- # This software product contains the IDEA algorithm as described and claimed in
163
- # US patent 5,214,703, EPO patent 0482154 (covering Austria, France, Germany,
164
- # Italy, the Netherlands, Spain, Sweden, Switzerland, and the UK), and Japanese
165
- # patent application 508119/1991, "Device for the conversion of a digital block
166
- # and use of same" (hereinafter referred to as "the algorithm"). Any use of
167
- # the algorithm for commercial purposes is thus subject to a license from Ascom
168
- # Systec Ltd. of CH-5506 Maegenwil (Switzerland), being the patentee and sole
169
- # owner of all rights, including the trademark IDEA.
170
- #
171
- # Commercial purposes shall mean any revenue generating purpose including but
172
- # not limited to:
173
- #
174
- # i) Using the algorithm for company internal purposes (subject to a site
175
- # license).
176
- #
177
- # ii) Incorporating the algorithm into any software and distributing such
178
- # software and/or providing services relating thereto to others (subject to
179
- # a product license).
180
- #
181
- # iii) Using a product containing the algorithm not covered by an IDEA license
182
- # (subject to an end user license).
183
- #
184
- # All such end user license agreements are available exclusively from Ascom
185
- # Systec Ltd and may be requested via the WWW at http://www.ascom.ch/systec or
186
- # by email to idea@ascom.ch.
187
- #
188
- # Use other than for commercial purposes is strictly limited to non-revenue
189
- # generating data transfer between private individuals. The use by government
190
- # agencies, non-profit organizations, etc is considered as use for commercial
191
- # purposes but may be subject to special conditions. Any misuse will be
192
- # prosecuted.
193
-
@@ -1,94 +0,0 @@
1
- # crypt/rattle.rb Richard Kernahan <kernighan_rich@rubyforge.org>
2
-
3
- # add_noise - take a message and intersperse noise to make a new noisy message of given byte-length
4
- # remove_noise - take a noisy message and extract the message
5
-
6
- module Crypt
7
- module Noise
8
-
9
- def add_noise(newLength)
10
- message = self
11
- usableNoisyMessageLength = newLength / 9 * 8
12
- bitmapSize = newLength / 9
13
- remainingBytes = newLength - usableNoisyMessageLength - bitmapSize
14
- if (message.length > usableNoisyMessageLength)
15
- minimumNewLength = (message.length / 8.0).ceil * 9
16
- puts "For a clear text of #{message.length} bytes, the minimum obscured length"
17
- puts "is #{minimumNewLength} bytes which allows for no noise in the message."
18
- puts "You should choose an obscured length of at least double the clear text"
19
- puts "length, such as #{message.length / 8 * 32} bytes"
20
- raise "Insufficient length for noisy message"
21
- end
22
- bitmap = []
23
- usableNoisyMessageLength.times { bitmap << false }
24
- srand(Time.now.to_i)
25
- positionsSelected = 0
26
- while (positionsSelected < message.length)
27
- positionTaken = rand(usableNoisyMessageLength)
28
- if bitmap[positionTaken]
29
- next
30
- else
31
- bitmap[positionTaken] = true
32
- positionsSelected = positionsSelected.next
33
- end
34
- end
35
-
36
- noisyMessage = ""
37
- 0.upto(bitmapSize-1) { |byte|
38
- c = 0
39
- 0.upto(7) { |bit|
40
- c = c + (1<<bit) if bitmap[byte * 8 + bit]
41
- }
42
- noisyMessage << c.chr
43
- }
44
- posInMessage = 0
45
- 0.upto(usableNoisyMessageLength-1) { |pos|
46
- if bitmap[pos]
47
- meaningfulByte = message[posInMessage]
48
- noisyMessage << meaningfulByte
49
- posInMessage = posInMessage.next
50
- else
51
- noiseByte = rand(256).chr
52
- noisyMessage << noiseByte
53
- end
54
- }
55
- remainingBytes.times {
56
- noiseByte = rand(256).chr
57
- noisyMessage << noiseByte
58
- }
59
- return(noisyMessage)
60
- end
61
-
62
-
63
- def remove_noise
64
- noisyMessage = self
65
- bitmapSize = noisyMessage.length / 9
66
- actualMessageLength = bitmapSize * 8
67
-
68
- actualMessageStart = bitmapSize
69
- actualMessageFinish = bitmapSize + actualMessageLength - 1
70
- actualMessage = noisyMessage[actualMessageStart..actualMessageFinish]
71
-
72
- bitmap = []
73
- 0.upto(bitmapSize - 1) { |byte|
74
- c = noisyMessage[byte]
75
- 0.upto(7) { |bit|
76
- bitmap[byte * 8 + bit] = (c[bit] == 1)
77
- }
78
- }
79
- clearMessage = ""
80
- 0.upto(actualMessageLength) { |pos|
81
- meaningful = bitmap[pos]
82
- if meaningful
83
- clearMessage << actualMessage[pos]
84
- end
85
- }
86
- return(clearMessage)
87
- end
88
-
89
- end
90
- end
91
-
92
- class String
93
- include Crypt::Noise
94
- end
@@ -1,378 +0,0 @@
1
- # Thanks to Binky DaClown who wrote this pure-ruby implementation
2
- # http://rubyforge.org/projects/prstringio/
3
- # Apparently CBC does not work well with the C-based stringio
4
-
5
- module Crypt
6
- class PureRubyStringIO
7
-
8
- include Enumerable
9
-
10
- SEEK_CUR = IO::SEEK_CUR
11
- SEEK_END = IO::SEEK_END
12
- SEEK_SET = IO::SEEK_SET
13
-
14
- @@relayMethods = [:<<, :all?, :any?, :binmode, :close, :close_read, :close_write, :closed?, :closed_read?,
15
- :closed_write?, :collect, :detect, :each, :each_byte, :each_line, :each_with_index,
16
- :entries, :eof, :eof?, :fcntl, :fileno, :find, :find_all, :flush, :fsync, :getc, :gets,
17
- :grep, :include?, :inject, :isatty, :length, :lineno, :lineno=, :map, :max, :member?,
18
- :min, :partition, :path, :pid, :pos, :pos=, :print, :printf, :putc, :puts, :read,
19
- :readchar, :readline, :readlines, :reject, :rewind, :seek, :select, :size, :sort,
20
- :sort_by, :string, :string=, :sync, :sync=, :sysread, :syswrite, :tell, :truncate, :tty?,
21
- :ungetc, :write, :zip]
22
-
23
- def self.open(string="", mode="r+")
24
- if block_given? then
25
- sio = new(string, mode)
26
- rc = yield(sio)
27
- sio.close
28
- rc
29
- else
30
- new(string, mode)
31
- end
32
- end
33
-
34
- def <<(obj)
35
- requireWritable
36
- write obj
37
- self
38
- end
39
-
40
- def binmode
41
- self
42
- end
43
-
44
- def close
45
- requireOpen
46
- @sio_closed_read = true
47
- @sio_closed_write = true
48
- self
49
- end
50
-
51
- def close_read
52
- raise IOError, "closing non-duplex IO for reading", caller if closed_read?
53
- @sio_closed_read = true
54
- self
55
- end
56
-
57
- def close_write
58
- raise IOError, "closing non-duplex IO for writing", caller if closed_write?
59
- @sio_closed_read = true
60
- self
61
- end
62
-
63
- def closed?
64
- closed_read? && closed_write?
65
- end
66
-
67
- def closed_read?
68
- @sio_closed_read
69
- end
70
-
71
- def closed_write?
72
- @sio_closed_write
73
- end
74
-
75
- def each(sep_string=$/, &block)
76
- requireReadable
77
- @sio_string.each(sep_string, &block)
78
- @sio_pos = @sio_string.length
79
- end
80
-
81
- def each_byte(&block)
82
- requireReadable
83
- @sio_string.each_byte(&block)
84
- @sio_pos = @sio_string.length
85
- end
86
-
87
- def eof
88
- requireReadable { @sio_pos >= @sio_string.length }
89
- end
90
-
91
- def fcntl(integer_cmd, arg)
92
- raise NotImplementedError, "The fcntl() function is unimplemented on this machine", caller
93
- end
94
-
95
- def fileno
96
- nil
97
- end
98
-
99
- def flush
100
- self
101
- end
102
-
103
- def fsync
104
- 0
105
- end
106
-
107
- def getc
108
- requireReadable
109
- char = @sio_string[@sio_pos]
110
- @sio_pos += 1 unless char.nil?
111
- char
112
- end
113
-
114
- def gets(sep_string=$/)
115
- requireReadable
116
- @sio_lineno += 1
117
- pstart = @sio_pos
118
- @sio_pos = @sio_string.index(sep_string, @sio_pos) || [@sio_string.length, @sio_pos].max
119
- @sio_string[pstart..@sio_pos]
120
- end
121
-
122
- def initialize(string="", mode="r+")
123
- @sio_string = string.to_s
124
- @sio_lineno = 0
125
- @mode = mode
126
- @relay = nil
127
- case mode.delete("b")
128
- when "r"
129
- @sio_closed_read = false
130
- @sio_closed_write = true
131
- @sio_pos = 0
132
- when "r+"
133
- @sio_closed_read = false
134
- @sio_closed_write = false
135
- @sio_pos = 0
136
- when "w"
137
- @sio_closed_read = true
138
- @sio_closed_write = false
139
- @sio_pos = 0
140
- @sio_string.replace("")
141
- when "w+"
142
- @sio_closed_read = false
143
- @sio_closed_write = false
144
- @sio_pos = 0
145
- @sio_string.replace("")
146
- when "a"
147
- @sio_closed_read = true
148
- @sio_closed_write = false
149
- @sio_pos = @sio_string.length
150
- when "a+"
151
- @sio_closed_read = false
152
- @sio_closed_write = false
153
- @sio_pos = @sio_string.length
154
- else
155
- raise ArgumentError, "illegal access mode #{mode}", caller
156
- end
157
- end
158
-
159
- def isatty
160
- flase
161
- end
162
-
163
- def length
164
- @sio_string.length
165
- end
166
-
167
- def lineno
168
- @sio_lineno
169
- end
170
-
171
- def lineno=(integer)
172
- @sio_lineno = integer
173
- end
174
-
175
- def path
176
- nil
177
- end
178
-
179
- def pid
180
- nil
181
- end
182
-
183
- def pos
184
- @sio_pos
185
- end
186
-
187
- def pos=(integer)
188
- raise Errno::EINVAL, "Invalid argument", caller if integer < 0
189
- @sio_pos = integer
190
- end
191
-
192
- def print(*args)
193
- requireWritable
194
- args.unshift($_) if args.empty
195
- args.each { |obj| write(obj) }
196
- write($\) unless $\.nil?
197
- nil
198
- end
199
-
200
- def printf(format_string, *args)
201
- requireWritable
202
- write format(format_string, *args)
203
- nil
204
- end
205
-
206
- def putc(obj)
207
- requireWritable
208
- write(obj.is_a?(Numeric) ? sprintf("%c", obj) : obj.to_s[0..0])
209
- obj
210
- end
211
-
212
- def puts(*args)
213
- requireWritable
214
- args.unshift("") if args.empty?
215
- args.each { |obj|
216
- write obj
217
- write $/
218
- }
219
- nil
220
- end
221
-
222
- def read(length=nil, buffer=nil)
223
- requireReadable
224
- len = length || [@sio_string.length - @sio_pos, 0].max
225
- raise ArgumentError, "negative length #{len} given", caller if len < 0
226
- buffer ||= ""
227
- pstart = @sio_pos
228
- @sio_pos += len
229
- buffer.replace(@sio_string[pstart..@sio_pos])
230
- buffer.empty? && !length.nil? ? nil : buffer
231
- end
232
-
233
- def readchar
234
- requireReadable
235
- raise EOFError, "End of file reached", caller if eof?
236
- getc
237
- end
238
-
239
- def readline
240
- requireReadable
241
- raise EOFError, "End of file reached", caller if eof?
242
- gets
243
- end
244
-
245
- def readlines(sep_string=$/)
246
- requireReadable
247
- raise EOFError, "End of file reached", caller if eof?
248
- rc = []
249
- until eof
250
- rc << gets(sep_string)
251
- end
252
- rc
253
- end
254
-
255
- def reopen(string, mode=nil)
256
- if string.is_a?(self.class) then
257
- raise ArgumentError, "wrong number of arguments (2 for 1)", caller if !mode.nil?
258
- @relay = string
259
- instance_eval(%Q{
260
- class << self
261
- @@relayMethods.each { |name|
262
- define_method(name, ObjectSpace._id2ref(#{@relay.object_id}).method(("original_" + name.to_s).to_sym).to_proc)
263
- }
264
- end
265
- })
266
- else
267
- raise ArgumentError, "wrong number of arguments (1 for 2)", caller if mode.nil?
268
- class << self
269
- @@relayMethods.each { |name|
270
- alias_method(name, "original_#{name}".to_sym)
271
- public name
272
- }
273
- @relay = nil
274
- end unless @relay.nil?
275
- @sio_string = string.to_s
276
- @mode = mode
277
- end
278
- end
279
-
280
- def rewind
281
- @sio_pos = 0
282
- @sio_lineno = 0
283
- end
284
-
285
- def seek(amount, whence=SEEK_SET)
286
- if whence == SEEK_CUR then
287
- offset += @sio_pos
288
- elsif whence == SEEK_END then
289
- offset += size
290
- end
291
- @sio_pos = offset
292
- end
293
-
294
- def string
295
- @sio_string
296
- end
297
-
298
- def string=(newstring)
299
- @sio_string = newstring
300
- end
301
-
302
- def sync
303
- true
304
- end
305
-
306
- def sync=(boolean)
307
- boolean
308
- end
309
-
310
- def sysread(length=nil, buffer=nil)
311
- requireReadable
312
- raise EOFError, "End of file reached", caller if eof?
313
- read(length, buffer)
314
- end
315
-
316
- def syswrite(string)
317
- requireWritable
318
- addition = "\000" * (@sio_string.length - @sio_pos) + string.to_s
319
- @sio_string[@sio_pos..(addition.length - 1)] = addition
320
- @sio_pos += addition.size
321
- addition.size
322
- end
323
-
324
- #In ruby 1.8.4 truncate differs from the docs in two ways.
325
- #First, if an integer greater that the length is given then the string is expanded to the new integer
326
- #length. As this expansion seems to contain junk characters instead of nulls I suspect this may be a
327
- #flaw in the C code which could cause a core dump if abused/used.
328
- #Second, the documentation states that truncate returns 0. It returns the integer instead.
329
- #This implementation follows the documentation in the first instance as I suspect this will be fixed
330
- #in the C code. In the second instance, it follows the actions of the C code instead of the docs.
331
- #This was decided as it causes no immedeate harm and this ruby implentation is to be as compatable
332
- #as possible with the C version. Should the C version change to match the docs the ruby version
333
- #will be simple to update as well.
334
- def truncate(integer)
335
- requireWritable
336
- raise Errno::EINVAL, "Invalid argument - negative length", caller if integer < 0
337
- @sio_string[[integer, @sio_string.length].max..-1] = ""
338
- integer
339
- end
340
-
341
- def ungetc(integer)
342
- requireWritable
343
- if @sio_pos > 0 then
344
- @sio_pos -= 1
345
- putc(integer)
346
- @sio_pos -= 1
347
- end
348
- end
349
-
350
- alias :each_line :each
351
- alias :eof? :eof
352
- alias :size :length
353
- alias :tty? :isatty
354
- alias :tell :pos
355
- alias :write :syswrite
356
-
357
- protected
358
- @@relayMethods.each { |name|
359
- alias_method("original_#{name}".to_sym, name)
360
- protected "original_#{name}".to_sym
361
- }
362
-
363
- private
364
-
365
- def requireReadable
366
- raise IOError, "not opened for reading", caller[1..-1] if @sio_closed_read
367
- end
368
-
369
- def requireWritable
370
- raise IOError, "not opened for writing", caller[1..-1] if @sio_closed_write
371
- end
372
-
373
- def requireOpen
374
- raise IOError, "closed stream", caller[1..-1] if @sio_closed_read && @sio_closed_write
375
- end
376
-
377
- end
378
- end