JJ
2019-09-16 01:23:16 UTC
I'm writing a slimmed down version of Base64 decoder based on the code of
below page (long URL warning).
<https://www.maxvergelli.com/base64-encoding-decoding-functions-in-vbscript-classic-asp/>
However, there's a problem when converting bynary byte array to binary
string. If I use `us-ascii` character set, the most significant bits are
stripped out from all of the bytes and the result becomes 7-bit data unless
I use `x-user-defined` character set. Did I miss something? Below is my
code.
'load base64 text from input file into string
set fs = createobject("scripting.filesystemobject")
set f = fs.opentextfile("input.b64")
s = f.readall
f.close
'convert base64 text string to binary byte array
set x = createobject("msxml2.domdocument.3.0")
set n = x.createelement("z")
n.datatype = "bin.base64"
n.text = s
d = n.nodetypedvalue
'convert bynary byte array to binary string
set ds = createobject("adodb.stream")
ds.type = 1 'binary
ds.open
ds.write d
ds.position = 0
ds.type = 2 'text
ds.charset = "us-ascii" '!!result is 7-bit!!
'ds.charset = "x-user-defined" 'can only use this to produce 8-bit
s = ds.readtext
set f = fs.createtextfile("output.bin")
f.write s
f.close
The `input.b64` test file is below. It's a 256 bytes ASCII from 0x00 to
0xFF.
AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyM
zQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZm
doaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZq
bnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3O
z9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/w==
below page (long URL warning).
<https://www.maxvergelli.com/base64-encoding-decoding-functions-in-vbscript-classic-asp/>
However, there's a problem when converting bynary byte array to binary
string. If I use `us-ascii` character set, the most significant bits are
stripped out from all of the bytes and the result becomes 7-bit data unless
I use `x-user-defined` character set. Did I miss something? Below is my
code.
'load base64 text from input file into string
set fs = createobject("scripting.filesystemobject")
set f = fs.opentextfile("input.b64")
s = f.readall
f.close
'convert base64 text string to binary byte array
set x = createobject("msxml2.domdocument.3.0")
set n = x.createelement("z")
n.datatype = "bin.base64"
n.text = s
d = n.nodetypedvalue
'convert bynary byte array to binary string
set ds = createobject("adodb.stream")
ds.type = 1 'binary
ds.open
ds.write d
ds.position = 0
ds.type = 2 'text
ds.charset = "us-ascii" '!!result is 7-bit!!
'ds.charset = "x-user-defined" 'can only use this to produce 8-bit
s = ds.readtext
set f = fs.createtextfile("output.bin")
f.write s
f.close
The `input.b64` test file is below. It's a 256 bytes ASCII from 0x00 to
0xFF.
AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyM
zQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZm
doaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZq
bnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3O
z9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/w==