Skip to content

Commit 6e27f66

Browse files
committed
Improved BASE64 to update has_error when decoding.
Added manual tests.
1 parent a4c1263 commit 6e27f66

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

library/text/encoder/src/base64.e

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,17 +125,17 @@ feature -- Decoder
125125
byte_count := 0
126126

127127
pos := next_encoded_character_position (v, pos)
128-
if pos <= n then
128+
if pos < n then
129129
byte1 := base64chars.index_of (v[pos], 1) - 1
130130
byte_count := byte_count + 1
131131

132132
pos := next_encoded_character_position (v, pos)
133-
if pos <= n then
133+
if pos < n then
134134
byte2 := base64chars.index_of (v[pos], 1) - 1
135135
byte_count := byte_count + 1
136136

137137
pos := next_encoded_character_position (v, pos)
138-
if pos <= n then
138+
if pos < n then
139139
c := v[pos]
140140
if c /= '=' then
141141
byte3 := base64chars.index_of (c, 1) - 1
@@ -150,8 +150,14 @@ feature -- Decoder
150150
byte_count := byte_count + 1
151151
end
152152
end
153+
else
154+
has_error := True
153155
end
156+
else
157+
has_error := True
154158
end
159+
else
160+
has_error := True
155161
end
156162
-- pos := pos + byte_count
157163

@@ -293,7 +299,7 @@ feature {NONE} -- Constants
293299
character_map: STRING = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
294300

295301
note
296-
copyright: "Copyright (c) 1984-2011, Eiffel Software and others"
302+
copyright: "Copyright (c) 2011-2014, Eiffel Software and others"
297303
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
298304
source: "[
299305
Eiffel Software

library/text/encoder/tests/test_base64.e

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,38 @@ feature -- Test routines
3434
assert ("decoded encoded string is same", u ~ s)
3535
end
3636

37+
feature -- Tests
38+
39+
test_valid_64_encoding
40+
do
41+
assert ("Expected encoded True:", is_valid_base64_encoding ((create {BASE64}).encoded_string ("content")))
42+
end
43+
44+
test_not_valid64_encoding
45+
do
46+
assert ("Expected encoded False:", not is_valid_base64_encoding ("content"))
47+
assert ("Expected encoded False:", not is_valid_base64_encoding ("!@#$%%^"))
48+
end
49+
50+
feature {NONE} -- Implementation
51+
52+
is_valid_base64_encoding (a_string: STRING): BOOLEAN
53+
-- is `a_string' base64 encoded?
54+
local
55+
l_encoder: BASE64
56+
l_string: STRING
57+
l_retry: BOOLEAN
58+
do
59+
if not l_retry then
60+
create l_encoder
61+
l_string := l_encoder.decoded_string (a_string)
62+
Result := not l_encoder.has_error
63+
end
64+
rescue
65+
l_retry := True
66+
retry
67+
end
68+
3769
note
3870
copyright: "Copyright (c) 1984-2011, Eiffel Software and others"
3971
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"

0 commit comments

Comments
 (0)