/
encoding.cr
257 lines (214 loc) · 6.84 KB
/
encoding.cr
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
{% unless flag?(:without_iconv) %}
require "crystal/iconv"
{% end %}
class IO
# Has the `name` and the `invalid` option.
struct EncodingOptions
getter name : String
getter invalid : Symbol?
def initialize(@name : String, @invalid : Symbol?)
EncodingOptions.check_invalid(invalid)
end
def self.check_invalid(invalid) : Nil
if invalid && invalid != :skip
raise ArgumentError.new "Valid values for `invalid` option are `nil` and `:skip`, not #{invalid.inspect}"
end
end
end
end
{% if flag?(:without_iconv) %}
require "./encoding_stubs"
{% skip_file %}
{% end %}
class IO
private class Encoder
def initialize(@encoding_options : EncodingOptions)
@iconv = Crystal::Iconv.new("UTF-8", encoding_options.name, encoding_options.invalid)
@closed = false
end
def write(io, slice : Bytes) : Nil
inbuf_ptr = slice.to_unsafe
inbytesleft = LibC::SizeT.new(slice.size)
outbuf = uninitialized UInt8[1024]
while inbytesleft > 0
outbuf_ptr = outbuf.to_unsafe
outbytesleft = LibC::SizeT.new(outbuf.size)
err = @iconv.convert(pointerof(inbuf_ptr), pointerof(inbytesleft), pointerof(outbuf_ptr), pointerof(outbytesleft))
if err == Crystal::Iconv::ERROR
@iconv.handle_invalid(pointerof(inbuf_ptr), pointerof(inbytesleft))
end
io.write(outbuf.to_slice[0, outbuf.size - outbytesleft])
end
end
def close : Nil
return if @closed
@closed = true
@iconv.close
end
def finalize
close
end
end
private class Decoder
BUFFER_SIZE = 4 * 1024
OUT_BUFFER_SIZE = 4 * 1024
property out_slice : Bytes
@in_buffer : Pointer(UInt8)
def initialize(@encoding_options : EncodingOptions)
@iconv = Crystal::Iconv.new(encoding_options.name, "UTF-8", encoding_options.invalid)
@buffer = Bytes.new((GC.malloc_atomic(BUFFER_SIZE).as(UInt8*)), BUFFER_SIZE)
@in_buffer = @buffer.to_unsafe
@in_buffer_left = LibC::SizeT.new(0)
@out_buffer = Bytes.new((GC.malloc_atomic(OUT_BUFFER_SIZE).as(UInt8*)), OUT_BUFFER_SIZE)
@out_slice = Bytes.empty
@closed = false
end
def read(io) : Nil
loop do
return unless @out_slice.empty?
if @in_buffer_left == 0
@in_buffer = @buffer.to_unsafe
@in_buffer_left = LibC::SizeT.new(io.read(@buffer))
end
# If, after refilling the buffer, we couldn't read new bytes
# it means we reached the end
break if @in_buffer_left == 0
# Convert bytes using iconv
out_buffer = @out_buffer.to_unsafe
out_buffer_left = LibC::SizeT.new(OUT_BUFFER_SIZE)
result = @iconv.convert(pointerof(@in_buffer), pointerof(@in_buffer_left), pointerof(out_buffer), pointerof(out_buffer_left))
@out_slice = @out_buffer[0, OUT_BUFFER_SIZE - out_buffer_left]
# Check for errors
if result == Crystal::Iconv::ERROR
case Errno.value
when Errno::EILSEQ
# For an illegal sequence we just skip one byte and we'll continue next
@iconv.handle_invalid(pointerof(@in_buffer), pointerof(@in_buffer_left))
when Errno::EINVAL
# EINVAL means "An incomplete multibyte sequence has been encountered in the input."
old_in_buffer_left = @in_buffer_left
# On invalid multibyte sequence we try to read more bytes
# to see if they complete the sequence
refill_in_buffer(io)
# If we couldn't read anything new, we raise or skip
if old_in_buffer_left == @in_buffer_left
@iconv.handle_invalid(pointerof(@in_buffer), pointerof(@in_buffer_left))
end
else
# Not an error we can handle
end
# Continue decoding after an error
next
end
break
end
end
private def refill_in_buffer(io)
buffer_remaining = BUFFER_SIZE - @in_buffer_left - (@in_buffer - @buffer.to_unsafe)
if buffer_remaining < 64
@buffer.copy_from(@in_buffer, @in_buffer_left)
@in_buffer = @buffer.to_unsafe
buffer_remaining = BUFFER_SIZE - @in_buffer_left
end
@in_buffer_left += LibC::SizeT.new(io.read(Slice.new(@in_buffer + @in_buffer_left, buffer_remaining)))
end
def read_byte(io) : UInt8?
read(io)
if out_slice.empty?
nil
else
byte = out_slice.to_unsafe.value
advance 1
byte
end
end
def read_utf8(io, slice) : Int32
count = 0
until slice.empty?
read(io)
break if out_slice.empty?
available = Math.min(out_slice.size, slice.size)
out_slice[0, available].copy_to(slice.to_unsafe, available)
advance(available)
count += available
slice += available
end
count
end
def gets(io, delimiter : UInt8, limit : Int, chomp) : String?
read(io)
return nil if @out_slice.empty?
index = @out_slice.index(delimiter)
if index
# If we find it past the limit, limit the result
if index >= limit
index = limit
else
index += 1
end
return gets_index(index, delimiter, chomp)
end
# Check if there's limit bytes in the out slice
if @out_slice.size >= limit
return gets_index(limit, delimiter, chomp)
end
# We need to read from the out_slice into a String until we find that byte,
# or until we consumed limit bytes
String.build do |str|
loop do
limit -= @out_slice.size
write str
read(io)
break if @out_slice.empty?
index = @out_slice.index(delimiter)
if index
if index >= limit
index = limit
else
index += 1
end
write str, index
break
else
if limit < @out_slice.size
write(str, limit)
break
end
end
end
str.chomp!(delimiter) if chomp
end
end
private def gets_index(index, delimiter, chomp)
advance_increment = index
if chomp && index > 0 && @out_slice[index - 1] === delimiter
index -= 1
if delimiter === '\n' && index > 0 && @out_slice[index - 1] === '\r'
index -= 1
end
end
string = String.new(@out_slice[0, index])
advance(advance_increment)
string
end
def write(io) : Nil
io.write @out_slice
@out_slice = Bytes.empty
end
def write(io, numbytes) : Nil
io.write @out_slice[0, numbytes]
@out_slice += numbytes
end
def advance(numbytes) : Nil
@out_slice += numbytes
end
def close : Nil
return if @closed
@closed = true
@iconv.close
end
def finalize
close
end
end
end