Can someone please look this over and make sure that my changes make sense?
Please note, that with this commit, there are two Base64 classes, one uses direct string-to-string conversions, and the other uses BinaryData conversions which should theoretically be faster especially for a lot of data. > On Oct 21, 2019, at 9:43 PM, [email protected] wrote: > > This is an automated email from the ASF dual-hosted git repository. > > harbs pushed a commit to branch feature/issue-500 > in repository https://gitbox.apache.org/repos/asf/royale-asjs.git > > commit cba39fbce2674e8d2b44756bff21bc215f933db6 > Author: Harbs <[email protected]> > AuthorDate: Mon Oct 21 21:41:55 2019 +0300 > > Fixed #500 > --- > LICENSE | 26 +++ > .../main/royale/org/apache/royale/ace/ACEEditor.as | 2 +- > .../projects/Core/src/main/royale/CoreClasses.as | 3 +- > .../main/royale/org/apache/royale/utils/Base64.as | 200 +++++++++++++++++++++ > .../MXRoyale/src/main/royale/MXRoyaleClasses.as | 7 - > .../src/main/royale/mx/utils/Base64Decoder.as | 9 +- > .../src/main/royale/mx/utils/Base64Encoder.as | 8 +- > .../src/main/royale/mx/utils/Base64JSWrapper.as | 53 ------ > .../main/royale/mx/utils/TextEncoderLiteWrapper.as | 52 ------ > 9 files changed, 239 insertions(+), 121 deletions(-) > > diff --git a/LICENSE b/LICENSE > index ee914c9..6ef80c3 100644 > --- a/LICENSE > +++ b/LICENSE > @@ -368,3 +368,29 @@ SOFTWARE. > Most of the .as files in frameworks/projects/Crux/src/main/royale > are derived from the files in the Swiz framework which are available > under Apache License 2.0. > + > +The file > frameworks/projects/Core/src/main/royale/org/apache/royale/utils/Base64.as > +is from https://sociodox.com/base64.html which is available under an > +MIT License. > + > +The MIT License (MIT) > + > +Copyright (C) 2012 Jean-Philippe Auclair > + > +Permission is hereby granted, free of charge, to any person obtaining a copy > +of this software and associated documentation files (the "Software"), to deal > +in the Software without restriction, including without limitation the rights > +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell > +copies of the Software, and to permit persons to whom the Software is > +furnished to do so, subject to the following conditions: > + > +The above copyright notice and this permission notice shall be included in > all > +copies or substantial portions of the Software. > + > +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR > +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, > +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE > +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER > +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, > +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE > +SOFTWARE. > diff --git > a/frameworks/projects/Ace/src/main/royale/org/apache/royale/ace/ACEEditor.as > b/frameworks/projects/Ace/src/main/royale/org/apache/royale/ace/ACEEditor.as > index a194f78..3f42ad2 100644 > --- > a/frameworks/projects/Ace/src/main/royale/org/apache/royale/ace/ACEEditor.as > +++ > b/frameworks/projects/Ace/src/main/royale/org/apache/royale/ace/ACEEditor.as > @@ -35,7 +35,7 @@ package org.apache.royale.ace > { > /** > * <inject_html> > - * <script > src="https://cdn.rawgit.com/ajaxorg/ace-builds/master/src-min-noconflict/ace.js"></script> > + * <script > src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.4.6/ace.js"></script> > * </inject_html> > */ > public function ACEEditor() > diff --git a/frameworks/projects/Core/src/main/royale/CoreClasses.as > b/frameworks/projects/Core/src/main/royale/CoreClasses.as > index 6d03a3b..650d0c6 100644 > --- a/frameworks/projects/Core/src/main/royale/CoreClasses.as > +++ b/frameworks/projects/Core/src/main/royale/CoreClasses.as > @@ -296,7 +296,8 @@ internal class CoreClasses > > import org.apache.royale.utils.array.rangeCheck; rangeCheck; > > - import org.apache.royale.utils.string.Base64; Base64; > + import org.apache.royale.utils.string.Base64; > org.apache.royale.utils.string.Base64; > + import org.apache.royale.utils.Base64; org.apache.royale.utils.Base64; > import org.apache.royale.utils.string.contains; contains; > import org.apache.royale.utils.string.isWhitespace; isWhitespace; > import org.apache.royale.utils.string.trim; trim; > diff --git > a/frameworks/projects/Core/src/main/royale/org/apache/royale/utils/Base64.as > b/frameworks/projects/Core/src/main/royale/org/apache/royale/utils/Base64.as > new file mode 100644 > index 0000000..2cdc2d8 > --- /dev/null > +++ > b/frameworks/projects/Core/src/main/royale/org/apache/royale/utils/Base64.as > @@ -0,0 +1,200 @@ > +//////////////////////////////////////////////////////////////////////////////// > +// > +// Licensed to the Apache Software Foundation (ASF) under one or more > +// contributor license agreements. See the NOTICE file distributed with > +// this work for additional information regarding copyright ownership. > +// The ASF licenses this file to You under the Apache License, Version 2.0 > +// (the "License"); you may not use this file except in compliance with > +// the License. You may obtain a copy of the License at > +// > +// http://www.apache.org/licenses/LICENSE-2.0 > +// > +// Unless required by applicable law or agreed to in writing, software > +// distributed under the License is distributed on an "AS IS" BASIS, > +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. > +// See the License for the specific language governing permissions and > +// limitations under the License. > +// > +//////////////////////////////////////////////////////////////////////////////// > + > +/** > + * Implementation derived from: > + * https://sociodox.com/base64.html > + * available under MIT License. > + * > + * By: Jean-Philippe Auclair : http://jpauclair.net > + * Based on article: > http://jpauclair.net/2010/01/09/base64-optimized-as3-lib/ > + * Benchmark: > + * This version: encode: 260ms decode: 255ms > + * Blog version: encode: 322ms decode: 694ms > + * as3Crypto encode: 6728ms decode: 4098ms > + * > + * Encode: com.sociodox.utils.Base64 is 25.8x faster than as3Crypto Base64 > + * Decode: com.sociodox.utils.Base64 is 16x faster than as3Crypto Base64 > + * > + * Optimize & Profile any Flash content with TheMiner ( > http://www.sociodox.com/theminer ) > + */ > + > +package org.apache.royale.utils > +{ > + public class Base64 > + { > + > + public static function encode(data:BinaryData):String > + { > + var out:BinaryData = new BinaryData(); > + //Presetting the length keep the memory smaller and > optimize speed since there is no "grow" needed > + out.length = (2 + data.length - ((data.length + 2) % > 3)) * 4 / 3; //Preset length //1.6 to 1.5 ms > + var i:int = 0; > + var r:int = data.length % 3; > + var len:int = data.length - r; > + var c:uint; //read (3) character AND write (4) > characters > + var outPos:int = 0; > + while (i < len) > + { > + //Read 3 Characters (8bit * 3 = 24 bits) > + c = data.readByteAt(int(i++)) << 16 | > data.readByteAt(int(i++)) << 8 | data.readByteAt(int(i++)); > + > + out.writeByteAt(int(outPos++),encodeChars[int(c > >>> 18)]); > + out.writeByteAt(int(outPos++), > encodeChars[int(c >>> 12 & 0x3f)]); > + out.writeByteAt(int(outPos++), > encodeChars[int(c >>> 6 & 0x3f)]); > + out.writeByteAt(int(outPos++), > encodeChars[int(c & 0x3f)]); > + } > + > + if (r == 1) //Need two "=" padding > + { > + //Read one char, write two chars, write padding > + c = data[int(i)]; > + > + out.writeByteAt(int(outPos++), > encodeChars[int(c >>> 2)]); > + out.writeByteAt(int(outPos++), > encodeChars[int((c & 0x03) << 4)]); > + out.writeByteAt(int(outPos++), 61); > + out.writeByteAt(int(outPos++), 61); > + } > + else if (r == 2) //Need one "=" padding > + { > + c = data.readByteAt(int(i++)) << 8 | > data.readByteAt(int(i)); > + > + out.writeByteAt(int(outPos++), > encodeChars[int(c >>> 10)]); > + out.writeByteAt(int(outPos++), > encodeChars[int(c >>> 4 & 0x3f)]); > + out.writeByteAt(int(outPos++), > encodeChars[int((c & 0x0f) << 2)]); > + out.writeByteAt(int(outPos++), 61); > + } > + > + return out.readUTFBytes(out.length); > + } > + > + public static function decode(str:String):BinaryData > + { > + var c1:int; > + var c2:int; > + var c3:int; > + var c4:int; > + var i:int = 0; > + var len:int = str.length; > + > + var byteString:BinaryData = new BinaryData(); > + byteString.writeUTFBytes(str); > + var outPos:int = 0; > + while (i < len) > + { > + //c1 > + c1 = > decodeChars[int(byteString.readByteAt(i++))]; > + if (c1 == -1) > + break; > + > + //c2 > + c2 = > decodeChars[int(byteString.readByteAt(i++))]; > + if (c2 == -1) > + break; > + > + byteString.writeByteAt(int(outPos++), (c1 << 2) > | ((c2 & 0x30) >> 4)); > + > + //c3 > + c3 = byteString.readByteAt(int(i++)); > + if (c3 == 61) > + { > + byteString.length = outPos > + return byteString; > + } > + > + c3 = decodeChars[int(c3)]; > + if (c3 == -1) > + break; > + > + byteString.writeByteAt(int(outPos++), ((c2 & > 0x0f) << 4) | ((c3 & 0x3c) >> 2)); > + > + //c4 > + c4 = byteString.readByteAt(int(i++)); > + if (c4 == 61) > + { > + byteString.length = outPos > + return byteString; > + } > + > + c4 = decodeChars[int(c4)]; > + if (c4 == -1) > + break; > + > + byteString.writeByteAt(int(outPos++), ((c3 & > 0x03) << 6) | c4); > + } > + byteString.length = outPos; > + byteString.position = 0; > + return byteString; > + } > + > + public static function InitEncoreChar():Vector.<int> > + { > + var encodeChars:Vector.<int> = new Vector.<int>(64, > true); > + > + // We could push the number directly > + // but I think it's nice to see the characters (with no > overhead on encode/decode) > + var chars:String = > "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; > + for (var i:int = 0; i < 64; i++) > + { > + encodeChars[i] = chars.charCodeAt(i); > + } > + > + return encodeChars; > + } > + > + public static function InitDecodeChar():Vector.<int> > + { > + > + var decodeChars:Vector.<int> = new <int>[ > + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, > -1, -1, -1, -1, > + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, > -1, -1, -1, -1, > + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, > -1, -1, -1, 63, > + 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, > -1, -1, -1, -1, > + -1, 0, 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, -1, > -1, -1, -1, -1, > + -1, 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, -1, > -1, -1, -1, -1, > + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, > -1, -1, -1, -1, > + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, > -1, -1, -1, -1, > + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, > -1, -1, -1, -1, > + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, > -1, -1, -1, -1, > + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, > -1, -1, -1, -1, > + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, > -1, -1, -1, -1, > + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, > -1, -1, -1, -1, > + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, > -1, -1, -1, -1]; > + > + return decodeChars; > + } > + private static var _encodeChars:Vector.<int>; > + private static function get encodeChars():Vector.<int>{ > + if(!_encodeChars){ > + _encodeChars = InitEncoreChar(); > + } > + return _encodeChars; > + } > + private static var _decodeChars:Vector.<int>; > + private static function get decodeChars():Vector.<int>{ > + if(!_decodeChars){ > + _decodeChars = InitDecodeChar(); > + } > + return _decodeChars; > + } > + > + } > +} > \ No newline at end of file > diff --git a/frameworks/projects/MXRoyale/src/main/royale/MXRoyaleClasses.as > b/frameworks/projects/MXRoyale/src/main/royale/MXRoyaleClasses.as > index ed82485..19991fd 100644 > --- a/frameworks/projects/MXRoyale/src/main/royale/MXRoyaleClasses.as > +++ b/frameworks/projects/MXRoyale/src/main/royale/MXRoyaleClasses.as > @@ -213,13 +213,6 @@ internal class MXRoyaleClasses > import mx.controls.PopUpButton; PopUpButton; > import mx.controls.PopUpMenuButton; PopUpMenuButton; > > - > - COMPILE::JS > - { > - import mx.utils.TextEncoderLiteWrapper; TextEncoderLiteWrapper; > - import mx.utils.Base64JSWrapper; Base64JSWrapper; > - } > - > COMPILE::SWF > { > import mx.controls.beads.CSSImageAndTextButtonView; > CSSImageAndTextButtonView; > diff --git > a/frameworks/projects/MXRoyale/src/main/royale/mx/utils/Base64Decoder.as > b/frameworks/projects/MXRoyale/src/main/royale/mx/utils/Base64Decoder.as > index 2a718d8..b9a8c25 100644 > --- a/frameworks/projects/MXRoyale/src/main/royale/mx/utils/Base64Decoder.as > +++ b/frameworks/projects/MXRoyale/src/main/royale/mx/utils/Base64Decoder.as > @@ -21,6 +21,8 @@ package mx.utils > { > > import mx.utils.ByteArray; > +import org.apache.royale.utils.Base64; > +import org.apache.royale.utils.BinaryData; > > /* > import mx.resources.IResourceManager; > @@ -87,10 +89,9 @@ public class Base64Decoder > COMPILE::JS > public function decode(data:String):String > { > - > - var bytes:Object = new Base64JSWrapper().toByteArray(data); > - decodedString = new > TextEncoderLiteWrapper('utf-8').decode(bytes); > - > + var bytes:BinaryData = Base64.decode(data); > + bytes.readUTFBytes(bytes.length); > + decodedString = bytes.readUTFBytes(bytes.length); > return decodedString; > } > > diff --git > a/frameworks/projects/MXRoyale/src/main/royale/mx/utils/Base64Encoder.as > b/frameworks/projects/MXRoyale/src/main/royale/mx/utils/Base64Encoder.as > index 8ea8349..4b04d17 100644 > --- a/frameworks/projects/MXRoyale/src/main/royale/mx/utils/Base64Encoder.as > +++ b/frameworks/projects/MXRoyale/src/main/royale/mx/utils/Base64Encoder.as > @@ -21,6 +21,8 @@ package mx.utils > { > > import mx.utils.ByteArray; > +import org.apache.royale.utils.BinaryData; > +import org.apache.royale.utils.Base64; > > > /** > @@ -138,9 +140,9 @@ public class Base64Encoder > COMPILE::JS > public function encode(data:String, encoding:String = "utf-8"):void > { > - var bytes:Object = new TextEncoderLiteWrapper(encoding).encode(data); > - encodedString = new Base64JSWrapper().fromByteArray(bytes); > - > + var bytes:BinaryData = new BinaryData(); > + bytes.writeUTFBytes(data); > + encodedString = Base64.encode(bytes); > } > > /** > diff --git > a/frameworks/projects/MXRoyale/src/main/royale/mx/utils/Base64JSWrapper.as > b/frameworks/projects/MXRoyale/src/main/royale/mx/utils/Base64JSWrapper.as > deleted file mode 100644 > index f3f94a0..0000000 > --- a/frameworks/projects/MXRoyale/src/main/royale/mx/utils/Base64JSWrapper.as > +++ /dev/null > @@ -1,53 +0,0 @@ > -//////////////////////////////////////////////////////////////////////////////// > -// > -// Licensed to the Apache Software Foundation (ASF) under one or more > -// contributor license agreements. See the NOTICE file distributed with > -// this work for additional information regarding copyright ownership. > -// The ASF licenses this file to You under the Apache License, Version 2.0 > -// (the "License"); you may not use this file except in compliance with > -// the License. You may obtain a copy of the License at > -// > -// http://www.apache.org/licenses/LICENSE-2.0 > -// > -// Unless required by applicable law or agreed to in writing, software > -// distributed under the License is distributed on an "AS IS" BASIS, > -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. > -// See the License for the specific language governing permissions and > -// limitations under the License. > -// > -//////////////////////////////////////////////////////////////////////////////// > -package mx.utils > -{ > - COMPILE::JS > - public class Base64JSWrapper > - { > - /** > - * The Royale Compiler will inject html into the index.html > file. Surround with > - * "inject_html" tag as follows: > - * > - * <inject_html> > - * <script type="text/javascript" > src="http://rawgit.com/beatgammit/base64-js/master/base64js.min.js"></script> > - * </inject_html> > - */ > - public function Base64JSWrapper() > - { > - } > - > - public function fromByteArray(bytes:Object):String > - { > - var base64js:Object = window["base64js"]; > - > - return base64js["fromByteArray"](bytes); > - > - } > - > - public function toByteArray(b64Str:String):Object > - { > - var base64js:Object = window["base64js"]; > - > - return base64js["toByteArray"](b64Str); > - > - } > - > - } > -} > \ No newline at end of file > diff --git > a/frameworks/projects/MXRoyale/src/main/royale/mx/utils/TextEncoderLiteWrapper.as > > b/frameworks/projects/MXRoyale/src/main/royale/mx/utils/TextEncoderLiteWrapper.as > deleted file mode 100644 > index f0c209c..0000000 > --- > a/frameworks/projects/MXRoyale/src/main/royale/mx/utils/TextEncoderLiteWrapper.as > +++ /dev/null > @@ -1,52 +0,0 @@ > -//////////////////////////////////////////////////////////////////////////////// > -// > -// Licensed to the Apache Software Foundation (ASF) under one or more > -// contributor license agreements. See the NOTICE file distributed with > -// this work for additional information regarding copyright ownership. > -// The ASF licenses this file to You under the Apache License, Version 2.0 > -// (the "License"); you may not use this file except in compliance with > -// the License. You may obtain a copy of the License at > -// > -// http://www.apache.org/licenses/LICENSE-2.0 > -// > -// Unless required by applicable law or agreed to in writing, software > -// distributed under the License is distributed on an "AS IS" BASIS, > -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. > -// See the License for the specific language governing permissions and > -// limitations under the License. > -// > -//////////////////////////////////////////////////////////////////////////////// > -package mx.utils > -{ > - COMPILE::JS > - public class TextEncoderLiteWrapper > - { > - private var _encoding:String; > - > - /** > - * The Royale Compiler will inject html into the index.html > file. Surround with > - * "inject_html" tag as follows: > - * > - * <inject_html> > - * <script type="text/javascript" > src="http://rawgit.com/coolaj86/TextEncoderLite/master/text-encoder-lite.min.js"></script> > - * </inject_html> > - */ > - public function TextEncoderLiteWrapper(encoding:String = > "utf-8") > - { > - _encoding = encoding ; > - } > - > - public function encode(data:String):Object > - { > - return new (TextEncoder)(_encoding).encode(data); > - > - } > - > - public function decode(data:Object):String > - { > - return new (TextDecoder)(_encoding).decode(data); > - > - } > - > - } > -} > \ No newline at end of file >
