├── README ├── .DS_Store ├── src ├── Webcam.fla ├── README.txt ├── com │ └── adobe │ │ ├── utils │ │ ├── IntUtil.as │ │ ├── NumberFormatter.as │ │ ├── DictionaryUtil.as │ │ ├── XMLUtil.as │ │ ├── ArrayUtil.as │ │ ├── StringUtil.as │ │ └── DateUtil.as │ │ ├── images │ │ ├── BitString.as │ │ └── PNGEncoder.as │ │ ├── webapis │ │ ├── ServiceBase.as │ │ ├── events │ │ │ └── ServiceEvent.as │ │ └── URLLoaderBase.as │ │ ├── net │ │ ├── DynamicURLLoader.as │ │ ├── IURIResolver.as │ │ ├── URIEncodingBitmap.as │ │ └── proxies │ │ │ └── RFC2817Socket.as │ │ ├── errors │ │ └── IllegalStateError.as │ │ ├── serialization │ │ └── json │ │ │ ├── JSONTokenType.as │ │ │ ├── JSON.as │ │ │ ├── JSONParseError.as │ │ │ ├── JSONToken.as │ │ │ ├── JSONDecoder.as │ │ │ ├── JSONEncoder.as │ │ │ └── JSONTokenizer.as │ │ └── crypto │ │ ├── WSSEUsernameToken.as │ │ ├── SHA1.as │ │ ├── SHA224.as │ │ ├── SHA256.as │ │ └── MD5.as └── Webcam.as ├── htdocs ├── shutter.mp3 ├── webcam.swf ├── cameras │ └── 1.jpg ├── record.php ├── view.php ├── camera.php └── webcam.js └── README.txt /README: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lavaeagle/Simple-Webcam-Stream-PHP/HEAD/.DS_Store -------------------------------------------------------------------------------- /src/Webcam.fla: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lavaeagle/Simple-Webcam-Stream-PHP/HEAD/src/Webcam.fla -------------------------------------------------------------------------------- /htdocs/shutter.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lavaeagle/Simple-Webcam-Stream-PHP/HEAD/htdocs/shutter.mp3 -------------------------------------------------------------------------------- /htdocs/webcam.swf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lavaeagle/Simple-Webcam-Stream-PHP/HEAD/htdocs/webcam.swf -------------------------------------------------------------------------------- /htdocs/cameras/1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lavaeagle/Simple-Webcam-Stream-PHP/HEAD/htdocs/cameras/1.jpg -------------------------------------------------------------------------------- /src/README.txt: -------------------------------------------------------------------------------- 1 | BUILDING INSTRUCTIONS 2 | 3 | This library requires the AS3 Core Library (as3corelib) available from Google Code: 4 | http://code.google.com/p/as3corelib/ 5 | 6 | After downloading and extracting the package, place the "com" directory right here, 7 | alongside the "Webcam.fla" and "Webcam.as" files. 8 | 9 | You should then be able to compile the FLA into a SWF. 10 | This requires Adobe Flash CS3 (this is a Flash 9 movie). 11 | -------------------------------------------------------------------------------- /htdocs/record.php: -------------------------------------------------------------------------------- 1 | 18 | -------------------------------------------------------------------------------- /htdocs/view.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 8 | 9 | 21 | 22 | 23 | 24 |

VIEW

25 |
26 | 27 |
28 | 29 | 30 | -------------------------------------------------------------------------------- /src/com/adobe/utils/IntUtil.as: -------------------------------------------------------------------------------- 1 | 2 | package com.adobe.utils { 3 | 4 | import flash.utils.Endian; 5 | 6 | /** 7 | * Contains reusable methods for operations pertaining 8 | * to int values. 9 | */ 10 | public class IntUtil { 11 | 12 | /** 13 | * Rotates x left n bits 14 | * 15 | * @langversion ActionScript 3.0 16 | * @playerversion Flash 9.0 17 | * @tiptext 18 | */ 19 | public static function rol ( x:int, n:int ):int { 20 | return ( x << n ) | ( x >>> ( 32 - n ) ); 21 | } 22 | 23 | /** 24 | * Rotates x right n bits 25 | * 26 | * @langversion ActionScript 3.0 27 | * @playerversion Flash 9.0 28 | * @tiptext 29 | */ 30 | public static function ror ( x:int, n:int ):uint { 31 | var nn:int = 32 - n; 32 | return ( x << nn ) | ( x >>> ( 32 - nn ) ); 33 | } 34 | 35 | /** String for quick lookup of a hex character based on index */ 36 | private static var hexChars:String = "0123456789abcdef"; 37 | 38 | /** 39 | * Outputs the hex value of a int, allowing the developer to specify 40 | * the endinaness in the process. Hex output is lowercase. 41 | * 42 | * @param n The int value to output as hex 43 | * @param bigEndian Flag to output the int as big or little endian 44 | * @return A string of length 8 corresponding to the 45 | * hex representation of n ( minus the leading "0x" ) 46 | * @langversion ActionScript 3.0 47 | * @playerversion Flash 9.0 48 | * @tiptext 49 | */ 50 | public static function toHex( n:int, bigEndian:Boolean = false ):String { 51 | var s:String = ""; 52 | 53 | if ( bigEndian ) { 54 | for ( var i:int = 0; i < 4; i++ ) { 55 | s += hexChars.charAt( ( n >> ( ( 3 - i ) * 8 + 4 ) ) & 0xF ) 56 | + hexChars.charAt( ( n >> ( ( 3 - i ) * 8 ) ) & 0xF ); 57 | } 58 | } else { 59 | for ( var x:int = 0; x < 4; x++ ) { 60 | s += hexChars.charAt( ( n >> ( x * 8 + 4 ) ) & 0xF ) 61 | + hexChars.charAt( ( n >> ( x * 8 ) ) & 0xF ); 62 | } 63 | } 64 | 65 | return s; 66 | } 67 | } 68 | 69 | } -------------------------------------------------------------------------------- /src/com/adobe/images/BitString.as: -------------------------------------------------------------------------------- 1 | /* 2 | Adobe Systems Incorporated(r) Source Code License Agreement 3 | Copyright(c) 2005 Adobe Systems Incorporated. All rights reserved. 4 | 5 | Please read this Source Code License Agreement carefully before using 6 | the source code. 7 | 8 | Adobe Systems Incorporated grants to you a perpetual, worldwide, non-exclusive, 9 | no-charge, royalty-free, irrevocable copyright license, to reproduce, 10 | prepare derivative works of, publicly display, publicly perform, and 11 | distribute this source code and such derivative works in source or 12 | object code form without any attribution requirements. 13 | 14 | The name "Adobe Systems Incorporated" must not be used to endorse or promote products 15 | derived from the source code without prior written permission. 16 | 17 | You agree to indemnify, hold harmless and defend Adobe Systems Incorporated from and 18 | against any loss, damage, claims or lawsuits, including attorney's 19 | fees that arise or result from your use or distribution of the source 20 | code. 21 | 22 | THIS SOURCE CODE IS PROVIDED "AS IS" AND "WITH ALL FAULTS", WITHOUT 23 | ANY TECHNICAL SUPPORT OR ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, 24 | BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 25 | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. ALSO, THERE IS NO WARRANTY OF 26 | NON-INFRINGEMENT, TITLE OR QUIET ENJOYMENT. IN NO EVENT SHALL MACROMEDIA 27 | OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 28 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 29 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 30 | OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 31 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 32 | OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOURCE CODE, EVEN IF 33 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | package com.adobe.images 36 | { 37 | public class BitString 38 | { 39 | public var len:int = 0; 40 | public var val:int = 0; 41 | } 42 | } -------------------------------------------------------------------------------- /src/com/adobe/webapis/ServiceBase.as: -------------------------------------------------------------------------------- 1 | /* 2 | Adobe Systems Incorporated(r) Source Code License Agreement 3 | Copyright(c) 2005 Adobe Systems Incorporated. All rights reserved. 4 | 5 | Please read this Source Code License Agreement carefully before using 6 | the source code. 7 | 8 | Adobe Systems Incorporated grants to you a perpetual, worldwide, non-exclusive, 9 | no-charge, royalty-free, irrevocable copyright license, to reproduce, 10 | prepare derivative works of, publicly display, publicly perform, and 11 | distribute this source code and such derivative works in source or 12 | object code form without any attribution requirements. 13 | 14 | The name "Adobe Systems Incorporated" must not be used to endorse or promote products 15 | derived from the source code without prior written permission. 16 | 17 | You agree to indemnify, hold harmless and defend Adobe Systems Incorporated from and 18 | against any loss, damage, claims or lawsuits, including attorney's 19 | fees that arise or result from your use or distribution of the source 20 | code. 21 | 22 | THIS SOURCE CODE IS PROVIDED "AS IS" AND "WITH ALL FAULTS", WITHOUT 23 | ANY TECHNICAL SUPPORT OR ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, 24 | BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 25 | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. ALSO, THERE IS NO WARRANTY OF 26 | NON-INFRINGEMENT, TITLE OR QUIET ENJOYMENT. IN NO EVENT SHALL MACROMEDIA 27 | OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 28 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 29 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 30 | OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 31 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 32 | OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOURCE CODE, EVEN IF 33 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | 36 | 37 | package com.adobe.webapis 38 | { 39 | import flash.events.EventDispatcher; 40 | 41 | /** 42 | * Base class for remote service classes. 43 | */ 44 | public class ServiceBase extends EventDispatcher 45 | { 46 | public function ServiceBase() 47 | { 48 | } 49 | 50 | } 51 | } -------------------------------------------------------------------------------- /htdocs/camera.php: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | 7 | JPEGCam Test Page 8 | 9 | 10 | 11 | 12 | 13 |
14 |

RECORD

15 | 16 | 17 | 18 | 19 | 20 | 25 | 26 | 27 | 30 | 31 | 32 |
33 | 34 |    35 | 36 |
37 | 38 | 39 | 73 | 74 |
  75 |
76 |
77 | 78 | 79 | -------------------------------------------------------------------------------- /src/com/adobe/net/DynamicURLLoader.as: -------------------------------------------------------------------------------- 1 | /* 2 | Adobe Systems Incorporated(r) Source Code License Agreement 3 | Copyright(c) 2005 Adobe Systems Incorporated. All rights reserved. 4 | 5 | Please read this Source Code License Agreement carefully before using 6 | the source code. 7 | 8 | Adobe Systems Incorporated grants to you a perpetual, worldwide, non-exclusive, 9 | no-charge, royalty-free, irrevocable copyright license, to reproduce, 10 | prepare derivative works of, publicly display, publicly perform, and 11 | distribute this source code and such derivative works in source or 12 | object code form without any attribution requirements. 13 | 14 | The name "Adobe Systems Incorporated" must not be used to endorse or promote products 15 | derived from the source code without prior written permission. 16 | 17 | You agree to indemnify, hold harmless and defend Adobe Systems Incorporated from and 18 | against any loss, damage, claims or lawsuits, including attorney's 19 | fees that arise or result from your use or distribution of the source 20 | code. 21 | 22 | THIS SOURCE CODE IS PROVIDED "AS IS" AND "WITH ALL FAULTS", WITHOUT 23 | ANY TECHNICAL SUPPORT OR ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, 24 | BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 25 | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. ALSO, THERE IS NO WARRANTY OF 26 | NON-INFRINGEMENT, TITLE OR QUIET ENJOYMENT. IN NO EVENT SHALL MACROMEDIA 27 | OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 28 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 29 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 30 | OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 31 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 32 | OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOURCE CODE, EVEN IF 33 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | 36 | package com.adobe.net 37 | { 38 | import flash.net.URLLoader; 39 | 40 | /** 41 | * Class that provides a dynamic implimentation of the URLLoader class. 42 | * 43 | * This class provides no API implimentations. However, since the class is 44 | * declared as dynamic, it can be used in place of URLLoader, and allow 45 | * you to dynamically attach properties to it (which URLLoader does not allow). 46 | * 47 | * @langversion ActionScript 3.0 48 | * @playerversion Flash 9.0 49 | * @tiptext 50 | */ 51 | public dynamic class DynamicURLLoader extends URLLoader 52 | { 53 | public function DynamicURLLoader() 54 | { 55 | super(); 56 | } 57 | } 58 | } -------------------------------------------------------------------------------- /src/com/adobe/errors/IllegalStateError.as: -------------------------------------------------------------------------------- 1 | /* 2 | Adobe Systems Incorporated(r) Source Code License Agreement 3 | Copyright(c) 2005 Adobe Systems Incorporated. All rights reserved. 4 | 5 | Please read this Source Code License Agreement carefully before using 6 | the source code. 7 | 8 | Adobe Systems Incorporated grants to you a perpetual, worldwide, non-exclusive, 9 | no-charge, royalty-free, irrevocable copyright license, to reproduce, 10 | prepare derivative works of, publicly display, publicly perform, and 11 | distribute this source code and such derivative works in source or 12 | object code form without any attribution requirements. 13 | 14 | The name "Adobe Systems Incorporated" must not be used to endorse or promote products 15 | derived from the source code without prior written permission. 16 | 17 | You agree to indemnify, hold harmless and defend Adobe Systems Incorporated from and 18 | against any loss, damage, claims or lawsuits, including attorney's 19 | fees that arise or result from your use or distribution of the source 20 | code. 21 | 22 | THIS SOURCE CODE IS PROVIDED "AS IS" AND "WITH ALL FAULTS", WITHOUT 23 | ANY TECHNICAL SUPPORT OR ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, 24 | BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 25 | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. ALSO, THERE IS NO WARRANTY OF 26 | NON-INFRINGEMENT, TITLE OR QUIET ENJOYMENT. IN NO EVENT SHALL MACROMEDIA 27 | OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 28 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 29 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 30 | OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 31 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 32 | OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOURCE CODE, EVEN IF 33 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | 36 | package com.adobe.errors 37 | { 38 | /** 39 | * This class represents an Error that is thrown when a method is called when 40 | * the receiving instance is in an invalid state. 41 | * 42 | * For example, this may occur if a method has been called, and other properties 43 | * in the instance have not been initialized properly. 44 | * 45 | * @langversion ActionScript 3.0 46 | * @playerversion Flash 9.0 47 | * @tiptext 48 | * 49 | */ 50 | public class IllegalStateError extends Error 51 | { 52 | /** 53 | * Constructor 54 | * 55 | * @param message A message describing the error in detail. 56 | * 57 | * @langversion ActionScript 3.0 58 | * @playerversion Flash 9.0 59 | * @tiptext 60 | */ 61 | public function IllegalStateError(message:String) 62 | { 63 | super(message); 64 | } 65 | } 66 | } -------------------------------------------------------------------------------- /src/com/adobe/serialization/json/JSONTokenType.as: -------------------------------------------------------------------------------- 1 | /* 2 | Adobe Systems Incorporated(r) Source Code License Agreement 3 | Copyright(c) 2005 Adobe Systems Incorporated. All rights reserved. 4 | 5 | Please read this Source Code License Agreement carefully before using 6 | the source code. 7 | 8 | Adobe Systems Incorporated grants to you a perpetual, worldwide, non-exclusive, 9 | no-charge, royalty-free, irrevocable copyright license, to reproduce, 10 | prepare derivative works of, publicly display, publicly perform, and 11 | distribute this source code and such derivative works in source or 12 | object code form without any attribution requirements. 13 | 14 | The name "Adobe Systems Incorporated" must not be used to endorse or promote products 15 | derived from the source code without prior written permission. 16 | 17 | You agree to indemnify, hold harmless and defend Adobe Systems Incorporated from and 18 | against any loss, damage, claims or lawsuits, including attorney's 19 | fees that arise or result from your use or distribution of the source 20 | code. 21 | 22 | THIS SOURCE CODE IS PROVIDED "AS IS" AND "WITH ALL FAULTS", WITHOUT 23 | ANY TECHNICAL SUPPORT OR ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, 24 | BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 25 | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. ALSO, THERE IS NO WARRANTY OF 26 | NON-INFRINGEMENT, TITLE OR QUIET ENJOYMENT. IN NO EVENT SHALL MACROMEDIA 27 | OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 28 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 29 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 30 | OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 31 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 32 | OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOURCE CODE, EVEN IF 33 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | 36 | package com.adobe.serialization.json { 37 | 38 | /** 39 | * Class containing constant values for the different types 40 | * of tokens in a JSON encoded string. 41 | */ 42 | public class JSONTokenType { 43 | 44 | public static const UNKNOWN:int = -1; 45 | 46 | public static const COMMA:int = 0; 47 | 48 | public static const LEFT_BRACE:int = 1; 49 | 50 | public static const RIGHT_BRACE:int = 2; 51 | 52 | public static const LEFT_BRACKET:int = 3; 53 | 54 | public static const RIGHT_BRACKET:int = 4; 55 | 56 | public static const COLON:int = 6; 57 | 58 | public static const TRUE:int = 7; 59 | 60 | public static const FALSE:int = 8; 61 | 62 | public static const NULL:int = 9; 63 | 64 | public static const STRING:int = 10; 65 | 66 | public static const NUMBER:int = 11; 67 | 68 | } 69 | 70 | } -------------------------------------------------------------------------------- /src/com/adobe/utils/NumberFormatter.as: -------------------------------------------------------------------------------- 1 | /* 2 | Adobe Systems Incorporated(r) Source Code License Agreement 3 | Copyright(c) 2005 Adobe Systems Incorporated. All rights reserved. 4 | 5 | Please read this Source Code License Agreement carefully before using 6 | the source code. 7 | 8 | Adobe Systems Incorporated grants to you a perpetual, worldwide, non-exclusive, 9 | no-charge, royalty-free, irrevocable copyright license, to reproduce, 10 | prepare derivative works of, publicly display, publicly perform, and 11 | distribute this source code and such derivative works in source or 12 | object code form without any attribution requirements. 13 | 14 | The name "Adobe Systems Incorporated" must not be used to endorse or promote products 15 | derived from the source code without prior written permission. 16 | 17 | You agree to indemnify, hold harmless and defend Adobe Systems Incorporated from and 18 | against any loss, damage, claims or lawsuits, including attorney's 19 | fees that arise or result from your use or distribution of the source 20 | code. 21 | 22 | THIS SOURCE CODE IS PROVIDED "AS IS" AND "WITH ALL FAULTS", WITHOUT 23 | ANY TECHNICAL SUPPORT OR ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, 24 | BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 25 | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. ALSO, THERE IS NO WARRANTY OF 26 | NON-INFRINGEMENT, TITLE OR QUIET ENJOYMENT. IN NO EVENT SHALL MACROMEDIA 27 | OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 28 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 29 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 30 | OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 31 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 32 | OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOURCE CODE, EVEN IF 33 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | 36 | package com.adobe.utils 37 | { 38 | 39 | /** 40 | * Class that contains static utility methods for formatting Numbers 41 | * 42 | * @langversion ActionScript 3.0 43 | * @playerversion Flash 9.0 44 | * @tiptext 45 | * 46 | * @see #mx.formatters.NumberFormatter 47 | */ 48 | public class NumberFormatter 49 | { 50 | 51 | /** 52 | * Formats a number to include a leading zero if it is a single digit 53 | * between -1 and 10. 54 | * 55 | * @param n The number that will be formatted 56 | * 57 | * @return A string with single digits between -1 and 10 padded with a 58 | * leading zero. 59 | * 60 | * @langversion ActionScript 3.0 61 | * @playerversion Flash 9.0 62 | * @tiptext 63 | */ 64 | public static function addLeadingZero(n:Number):String 65 | { 66 | var out:String = String(n); 67 | 68 | if(n < 10 && n > -1) 69 | { 70 | out = "0" + out; 71 | } 72 | 73 | return out; 74 | } 75 | 76 | } 77 | } -------------------------------------------------------------------------------- /src/com/adobe/webapis/events/ServiceEvent.as: -------------------------------------------------------------------------------- 1 | /* 2 | Adobe Systems Incorporated(r) Source Code License Agreement 3 | Copyright(c) 2005 Adobe Systems Incorporated. All rights reserved. 4 | 5 | Please read this Source Code License Agreement carefully before using 6 | the source code. 7 | 8 | Adobe Systems Incorporated grants to you a perpetual, worldwide, non-exclusive, 9 | no-charge, royalty-free, irrevocable copyright license, to reproduce, 10 | prepare derivative works of, publicly display, publicly perform, and 11 | distribute this source code and such derivative works in source or 12 | object code form without any attribution requirements. 13 | 14 | The name "Adobe Systems Incorporated" must not be used to endorse or promote products 15 | derived from the source code without prior written permission. 16 | 17 | You agree to indemnify, hold harmless and defend Adobe Systems Incorporated from and 18 | against any loss, damage, claims or lawsuits, including attorney's 19 | fees that arise or result from your use or distribution of the source 20 | code. 21 | 22 | THIS SOURCE CODE IS PROVIDED "AS IS" AND "WITH ALL FAULTS", WITHOUT 23 | ANY TECHNICAL SUPPORT OR ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, 24 | BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 25 | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. ALSO, THERE IS NO WARRANTY OF 26 | NON-INFRINGEMENT, TITLE OR QUIET ENJOYMENT. IN NO EVENT SHALL MACROMEDIA 27 | OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 28 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 29 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 30 | OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 31 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 32 | OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOURCE CODE, EVEN IF 33 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | 36 | 37 | package com.adobe.webapis.events 38 | { 39 | 40 | import flash.events.Event; 41 | 42 | /** 43 | * Event class that contains data loaded from remote services. 44 | * 45 | * @author Mike Chambers 46 | */ 47 | public class ServiceEvent extends Event 48 | { 49 | private var _data:Object = new Object();; 50 | 51 | /** 52 | * Constructor for ServiceEvent class. 53 | * 54 | * @param type The type of event that the instance represents. 55 | */ 56 | public function ServiceEvent(type:String, bubbles:Boolean = false, 57 | cancelable:Boolean=false) 58 | { 59 | super(type, bubbles, cancelable); 60 | } 61 | 62 | /** 63 | * This object contains data loaded in response 64 | * to remote service calls, and properties associated with that call. 65 | */ 66 | public function get data():Object 67 | { 68 | return _data; 69 | } 70 | 71 | public function set data(d:Object):void 72 | { 73 | _data = d; 74 | } 75 | 76 | 77 | } 78 | } -------------------------------------------------------------------------------- /src/com/adobe/utils/DictionaryUtil.as: -------------------------------------------------------------------------------- 1 | /* 2 | Adobe Systems Incorporated(r) Source Code License Agreement 3 | Copyright(c) 2005 Adobe Systems Incorporated. All rights reserved. 4 | 5 | Please read this Source Code License Agreement carefully before using 6 | the source code. 7 | 8 | Adobe Systems Incorporated grants to you a perpetual, worldwide, non-exclusive, 9 | no-charge, royalty-free, irrevocable copyright license, to reproduce, 10 | prepare derivative works of, publicly display, publicly perform, and 11 | distribute this source code and such derivative works in source or 12 | object code form without any attribution requirements. 13 | 14 | The name "Adobe Systems Incorporated" must not be used to endorse or promote products 15 | derived from the source code without prior written permission. 16 | 17 | You agree to indemnify, hold harmless and defend Adobe Systems Incorporated from and 18 | against any loss, damage, claims or lawsuits, including attorney's 19 | fees that arise or result from your use or distribution of the source 20 | code. 21 | 22 | THIS SOURCE CODE IS PROVIDED "AS IS" AND "WITH ALL FAULTS", WITHOUT 23 | ANY TECHNICAL SUPPORT OR ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, 24 | BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 25 | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. ALSO, THERE IS NO WARRANTY OF 26 | NON-INFRINGEMENT, TITLE OR QUIET ENJOYMENT. IN NO EVENT SHALL MACROMEDIA 27 | OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 28 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 29 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 30 | OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 31 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 32 | OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOURCE CODE, EVEN IF 33 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | 36 | package com.adobe.utils 37 | { 38 | import flash.utils.Dictionary; 39 | 40 | public class DictionaryUtil 41 | { 42 | 43 | /** 44 | * Returns an Array of all keys within the specified dictionary. 45 | * 46 | * @param d The Dictionary instance whose keys will be returned. 47 | * 48 | * @return Array of keys contained within the Dictionary 49 | * 50 | * @langversion ActionScript 3.0 51 | * @playerversion Flash 9.0 52 | * @tiptext 53 | */ 54 | public static function getKeys(d:Dictionary):Array 55 | { 56 | var a:Array = new Array(); 57 | 58 | for (var key:Object in d) 59 | { 60 | a.push(key); 61 | } 62 | 63 | return a; 64 | } 65 | 66 | /** 67 | * Returns an Array of all values within the specified dictionary. 68 | * 69 | * @param d The Dictionary instance whose values will be returned. 70 | * 71 | * @return Array of values contained within the Dictionary 72 | * 73 | * @langversion ActionScript 3.0 74 | * @playerversion Flash 9.0 75 | * @tiptext 76 | */ 77 | public static function getValues(d:Dictionary):Array 78 | { 79 | var a:Array = new Array(); 80 | 81 | for each (var value:Object in d) 82 | { 83 | a.push(value); 84 | } 85 | 86 | return a; 87 | } 88 | 89 | } 90 | } -------------------------------------------------------------------------------- /src/com/adobe/serialization/json/JSON.as: -------------------------------------------------------------------------------- 1 | /* 2 | Adobe Systems Incorporated(r) Source Code License Agreement 3 | Copyright(c) 2005 Adobe Systems Incorporated. All rights reserved. 4 | 5 | Please read this Source Code License Agreement carefully before using 6 | the source code. 7 | 8 | Adobe Systems Incorporated grants to you a perpetual, worldwide, non-exclusive, 9 | no-charge, royalty-free, irrevocable copyright license, to reproduce, 10 | prepare derivative works of, publicly display, publicly perform, and 11 | distribute this source code and such derivative works in source or 12 | object code form without any attribution requirements. 13 | 14 | The name "Adobe Systems Incorporated" must not be used to endorse or promote products 15 | derived from the source code without prior written permission. 16 | 17 | You agree to indemnify, hold harmless and defend Adobe Systems Incorporated from and 18 | against any loss, damage, claims or lawsuits, including attorney's 19 | fees that arise or result from your use or distribution of the source 20 | code. 21 | 22 | THIS SOURCE CODE IS PROVIDED "AS IS" AND "WITH ALL FAULTS", WITHOUT 23 | ANY TECHNICAL SUPPORT OR ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, 24 | BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 25 | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. ALSO, THERE IS NO WARRANTY OF 26 | NON-INFRINGEMENT, TITLE OR QUIET ENJOYMENT. IN NO EVENT SHALL MACROMEDIA 27 | OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 28 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 29 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 30 | OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 31 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 32 | OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOURCE CODE, EVEN IF 33 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | 36 | package com.adobe.serialization.json { 37 | 38 | /** 39 | * This class provides encoding and decoding of the JSON format. 40 | * 41 | * Example usage: 42 | * 43 | * // create a JSON string from an internal object 44 | * JSON.encode( myObject ); 45 | * 46 | * // read a JSON string into an internal object 47 | * var myObject:Object = JSON.decode( jsonString ); 48 | * 49 | */ 50 | public class JSON { 51 | 52 | 53 | /** 54 | * Encodes a object into a JSON string. 55 | * 56 | * @param o The object to create a JSON string for 57 | * @return the JSON string representing o 58 | * @langversion ActionScript 3.0 59 | * @playerversion Flash 9.0 60 | * @tiptext 61 | */ 62 | public static function encode( o:Object ):String { 63 | 64 | var encoder:JSONEncoder = new JSONEncoder( o ); 65 | return encoder.getString(); 66 | 67 | } 68 | 69 | /** 70 | * Decodes a JSON string into a native object. 71 | * 72 | * @param s The JSON string representing the object 73 | * @return A native object as specified by s 74 | * @throw JSONParseError 75 | * @langversion ActionScript 3.0 76 | * @playerversion Flash 9.0 77 | * @tiptext 78 | */ 79 | public static function decode( s:String ):* { 80 | 81 | var decoder:JSONDecoder = new JSONDecoder( s ) 82 | return decoder.getValue(); 83 | 84 | } 85 | 86 | } 87 | 88 | } -------------------------------------------------------------------------------- /src/com/adobe/serialization/json/JSONParseError.as: -------------------------------------------------------------------------------- 1 | /* 2 | Adobe Systems Incorporated(r) Source Code License Agreement 3 | Copyright(c) 2005 Adobe Systems Incorporated. All rights reserved. 4 | 5 | Please read this Source Code License Agreement carefully before using 6 | the source code. 7 | 8 | Adobe Systems Incorporated grants to you a perpetual, worldwide, non-exclusive, 9 | no-charge, royalty-free, irrevocable copyright license, to reproduce, 10 | prepare derivative works of, publicly display, publicly perform, and 11 | distribute this source code and such derivative works in source or 12 | object code form without any attribution requirements. 13 | 14 | The name "Adobe Systems Incorporated" must not be used to endorse or promote products 15 | derived from the source code without prior written permission. 16 | 17 | You agree to indemnify, hold harmless and defend Adobe Systems Incorporated from and 18 | against any loss, damage, claims or lawsuits, including attorney's 19 | fees that arise or result from your use or distribution of the source 20 | code. 21 | 22 | THIS SOURCE CODE IS PROVIDED "AS IS" AND "WITH ALL FAULTS", WITHOUT 23 | ANY TECHNICAL SUPPORT OR ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, 24 | BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 25 | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. ALSO, THERE IS NO WARRANTY OF 26 | NON-INFRINGEMENT, TITLE OR QUIET ENJOYMENT. IN NO EVENT SHALL MACROMEDIA 27 | OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 28 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 29 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 30 | OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 31 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 32 | OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOURCE CODE, EVEN IF 33 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | 36 | package com.adobe.serialization.json { 37 | 38 | /** 39 | * 40 | * 41 | */ 42 | public class JSONParseError extends Error { 43 | 44 | /** The location in the string where the error occurred */ 45 | private var _location:int; 46 | 47 | /** The string in which the parse error occurred */ 48 | private var _text:String; 49 | 50 | /** 51 | * Constructs a new JSONParseError. 52 | * 53 | * @param message The error message that occured during parsing 54 | * @langversion ActionScript 3.0 55 | * @playerversion Flash 9.0 56 | * @tiptext 57 | */ 58 | public function JSONParseError( message:String = "", location:int = 0, text:String = "") { 59 | super( message ); 60 | //name = "JSONParseError"; 61 | _location = location; 62 | _text = text; 63 | } 64 | 65 | /** 66 | * Provides read-only access to the location variable. 67 | * 68 | * @return The location in the string where the error occurred 69 | * @langversion ActionScript 3.0 70 | * @playerversion Flash 9.0 71 | * @tiptext 72 | */ 73 | public function get location():int { 74 | return _location; 75 | } 76 | 77 | /** 78 | * Provides read-only access to the text variable. 79 | * 80 | * @return The string in which the error occurred 81 | * @langversion ActionScript 3.0 82 | * @playerversion Flash 9.0 83 | * @tiptext 84 | */ 85 | public function get text():String { 86 | return _text; 87 | } 88 | } 89 | 90 | } -------------------------------------------------------------------------------- /src/com/adobe/net/IURIResolver.as: -------------------------------------------------------------------------------- 1 | /* 2 | Adobe Systems Incorporated(r) Source Code License Agreement 3 | Copyright(c) 2005 Adobe Systems Incorporated. All rights reserved. 4 | 5 | Please read this Source Code License Agreement carefully before using 6 | the source code. 7 | 8 | Adobe Systems Incorporated grants to you a perpetual, worldwide, non-exclusive, 9 | no-charge, royalty-free, irrevocable copyright license, to reproduce, 10 | prepare derivative works of, publicly display, publicly perform, and 11 | distribute this source code and such derivative works in source or 12 | object code form without any attribution requirements. 13 | 14 | The name "Adobe Systems Incorporated" must not be used to endorse or promote products 15 | derived from the source code without prior written permission. 16 | 17 | You agree to indemnify, hold harmless and defend Adobe Systems Incorporated from and 18 | against any loss, damage, claims or lawsuits, including attorney's 19 | fees that arise or result from your use or distribution of the source 20 | code. 21 | 22 | THIS SOURCE CODE IS PROVIDED "AS IS" AND "WITH ALL FAULTS", WITHOUT 23 | ANY TECHNICAL SUPPORT OR ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, 24 | BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 25 | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. ALSO, THERE IS NO WARRANTY OF 26 | NON-INFRINGEMENT, TITLE OR QUIET ENJOYMENT. IN NO EVENT SHALL MACROMEDIA 27 | OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 28 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 29 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 30 | OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 31 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 32 | OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOURCE CODE, EVEN IF 33 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | 36 | package com.adobe.net 37 | { 38 | /** 39 | * The URI class cannot know about DNS aliases, virtual hosts, or 40 | * symbolic links that may be involved. The application can provide 41 | * an implementation of this interface to resolve the URI before the 42 | * URI class makes any comparisons. For example, a web host has 43 | * two aliases: 44 | * 45 | *

46 | * http://www.site.com/ 47 | * http://www.site.net/ 48 | *

49 | * 50 | *

The application can provide an implementation that automatically 51 | * resolves site.net to site.com before URI compares two URI objects. 52 | * Only the application can know and understand the context in which 53 | * the URI's are being used.

54 | * 55 | *

Use the URI.resolver accessor to assign a custom resolver to 56 | * the URI class. Any resolver specified is global to all instances 57 | * of URI.

58 | * 59 | *

URI will call this before performing URI comparisons in the 60 | * URI.getRelation() and URI.getCommonParent() functions. 61 | * 62 | * @see URI.getRelation 63 | * @see URI.getCommonParent 64 | * 65 | * @langversion ActionScript 3.0 66 | * @playerversion Flash 9.0 67 | */ 68 | public interface IURIResolver 69 | { 70 | /** 71 | * Implement this method to provide custom URI resolution for 72 | * your application. 73 | * 74 | * @langversion ActionScript 3.0 75 | * @playerversion Flash 9.0 76 | */ 77 | function resolve(uri:URI) : URI; 78 | } 79 | } -------------------------------------------------------------------------------- /src/com/adobe/serialization/json/JSONToken.as: -------------------------------------------------------------------------------- 1 | /* 2 | Adobe Systems Incorporated(r) Source Code License Agreement 3 | Copyright(c) 2005 Adobe Systems Incorporated. All rights reserved. 4 | 5 | Please read this Source Code License Agreement carefully before using 6 | the source code. 7 | 8 | Adobe Systems Incorporated grants to you a perpetual, worldwide, non-exclusive, 9 | no-charge, royalty-free, irrevocable copyright license, to reproduce, 10 | prepare derivative works of, publicly display, publicly perform, and 11 | distribute this source code and such derivative works in source or 12 | object code form without any attribution requirements. 13 | 14 | The name "Adobe Systems Incorporated" must not be used to endorse or promote products 15 | derived from the source code without prior written permission. 16 | 17 | You agree to indemnify, hold harmless and defend Adobe Systems Incorporated from and 18 | against any loss, damage, claims or lawsuits, including attorney's 19 | fees that arise or result from your use or distribution of the source 20 | code. 21 | 22 | THIS SOURCE CODE IS PROVIDED "AS IS" AND "WITH ALL FAULTS", WITHOUT 23 | ANY TECHNICAL SUPPORT OR ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, 24 | BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 25 | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. ALSO, THERE IS NO WARRANTY OF 26 | NON-INFRINGEMENT, TITLE OR QUIET ENJOYMENT. IN NO EVENT SHALL MACROMEDIA 27 | OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 28 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 29 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 30 | OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 31 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 32 | OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOURCE CODE, EVEN IF 33 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | 36 | package com.adobe.serialization.json { 37 | 38 | public class JSONToken { 39 | 40 | private var _type:int; 41 | private var _value:Object; 42 | 43 | /** 44 | * Creates a new JSONToken with a specific token type and value. 45 | * 46 | * @param type The JSONTokenType of the token 47 | * @param value The value of the token 48 | * @langversion ActionScript 3.0 49 | * @playerversion Flash 9.0 50 | * @tiptext 51 | */ 52 | public function JSONToken( type:int = -1 /* JSONTokenType.UNKNOWN */, value:Object = null ) { 53 | _type = type; 54 | _value = value; 55 | } 56 | 57 | /** 58 | * Returns the type of the token. 59 | * 60 | * @see com.adobe.serialization.json.JSONTokenType 61 | * @langversion ActionScript 3.0 62 | * @playerversion Flash 9.0 63 | * @tiptext 64 | */ 65 | public function get type():int { 66 | return _type; 67 | } 68 | 69 | /** 70 | * Sets the type of the token. 71 | * 72 | * @see com.adobe.serialization.json.JSONTokenType 73 | * @langversion ActionScript 3.0 74 | * @playerversion Flash 9.0 75 | * @tiptext 76 | */ 77 | public function set type( value:int ):void { 78 | _type = value; 79 | } 80 | 81 | /** 82 | * Gets the value of the token 83 | * 84 | * @see com.adobe.serialization.json.JSONTokenType 85 | * @langversion ActionScript 3.0 86 | * @playerversion Flash 9.0 87 | * @tiptext 88 | */ 89 | public function get value():Object { 90 | return _value; 91 | } 92 | 93 | /** 94 | * Sets the value of the token 95 | * 96 | * @see com.adobe.serialization.json.JSONTokenType 97 | * @langversion ActionScript 3.0 98 | * @playerversion Flash 9.0 99 | * @tiptext 100 | */ 101 | public function set value ( v:Object ):void { 102 | _value = v; 103 | } 104 | 105 | } 106 | 107 | } -------------------------------------------------------------------------------- /src/com/adobe/webapis/URLLoaderBase.as: -------------------------------------------------------------------------------- 1 | /* 2 | Adobe Systems Incorporated(r) Source Code License Agreement 3 | Copyright(c) 2005 Adobe Systems Incorporated. All rights reserved. 4 | 5 | Please read this Source Code License Agreement carefully before using 6 | the source code. 7 | 8 | Adobe Systems Incorporated grants to you a perpetual, worldwide, non-exclusive, 9 | no-charge, royalty-free, irrevocable copyright license, to reproduce, 10 | prepare derivative works of, publicly display, publicly perform, and 11 | distribute this source code and such derivative works in source or 12 | object code form without any attribution requirements. 13 | 14 | The name "Adobe Systems Incorporated" must not be used to endorse or promote products 15 | derived from the source code without prior written permission. 16 | 17 | You agree to indemnify, hold harmless and defend Adobe Systems Incorporated from and 18 | against any loss, damage, claims or lawsuits, including attorney's 19 | fees that arise or result from your use or distribution of the source 20 | code. 21 | 22 | THIS SOURCE CODE IS PROVIDED "AS IS" AND "WITH ALL FAULTS", WITHOUT 23 | ANY TECHNICAL SUPPORT OR ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, 24 | BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 25 | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. ALSO, THERE IS NO WARRANTY OF 26 | NON-INFRINGEMENT, TITLE OR QUIET ENJOYMENT. IN NO EVENT SHALL MACROMEDIA 27 | OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 28 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 29 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 30 | OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 31 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 32 | OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOURCE CODE, EVEN IF 33 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | 36 | package com.adobe.webapis 37 | { 38 | import flash.events.IOErrorEvent; 39 | import flash.events.SecurityErrorEvent; 40 | import flash.events.ProgressEvent; 41 | 42 | import com.adobe.net.DynamicURLLoader; 43 | 44 | /** 45 | * Dispatched when data is 46 | * received as the download operation progresses. 47 | * 48 | * @eventType flash.events.ProgressEvent.PROGRESS 49 | * 50 | * @langversion ActionScript 3.0 51 | * @playerversion Flash 9.0 52 | */ 53 | [Event(name="progress", type="flash.events.ProgressEvent")] 54 | 55 | /** 56 | * Dispatched if a call to the server results in a fatal 57 | * error that terminates the download. 58 | * 59 | * @eventType flash.events.IOErrorEvent.IO_ERROR 60 | * 61 | * @langversion ActionScript 3.0 62 | * @playerversion Flash 9.0 63 | */ 64 | [Event(name="ioError", type="flash.events.IOErrorEvent")] 65 | 66 | /** 67 | * A securityError event occurs if a call attempts to 68 | * load data from a server outside the security sandbox. 69 | * 70 | * @eventType flash.events.SecurityErrorEvent.SECURITY_ERROR 71 | * 72 | * @langversion ActionScript 3.0 73 | * @playerversion Flash 9.0 74 | */ 75 | [Event(name="securityError", type="flash.events.SecurityErrorEvent")] 76 | 77 | /** 78 | * Base class for services that utilize URLLoader 79 | * to communicate with remote APIs / Services. 80 | * 81 | * @langversion ActionScript 3.0 82 | * @playerversion Flash 9.0 83 | */ 84 | public class URLLoaderBase extends ServiceBase 85 | { 86 | protected function getURLLoader():DynamicURLLoader 87 | { 88 | var loader:DynamicURLLoader = new DynamicURLLoader(); 89 | loader.addEventListener("progress", onProgress); 90 | loader.addEventListener("ioError", onIOError); 91 | loader.addEventListener("securityError", onSecurityError); 92 | 93 | return loader; 94 | } 95 | 96 | private function onIOError(event:IOErrorEvent):void 97 | { 98 | dispatchEvent(event); 99 | } 100 | 101 | private function onSecurityError(event:SecurityErrorEvent):void 102 | { 103 | dispatchEvent(event); 104 | } 105 | 106 | private function onProgress(event:ProgressEvent):void 107 | { 108 | dispatchEvent(event); 109 | } 110 | } 111 | } -------------------------------------------------------------------------------- /src/com/adobe/crypto/WSSEUsernameToken.as: -------------------------------------------------------------------------------- 1 | /* 2 | Adobe Systems Incorporated(r) Source Code License Agreement 3 | Copyright(c) 2005 Adobe Systems Incorporated. All rights reserved. 4 | 5 | Please read this Source Code License Agreement carefully before using 6 | the source code. 7 | 8 | Adobe Systems Incorporated grants to you a perpetual, worldwide, non-exclusive, 9 | no-charge, royalty-free, irrevocable copyright license, to reproduce, 10 | prepare derivative works of, publicly display, publicly perform, and 11 | distribute this source code and such derivative works in source or 12 | object code form without any attribution requirements. 13 | 14 | The name "Adobe Systems Incorporated" must not be used to endorse or promote products 15 | derived from the source code without prior written permission. 16 | 17 | You agree to indemnify, hold harmless and defend Adobe Systems Incorporated from and 18 | against any loss, damage, claims or lawsuits, including attorney's 19 | fees that arise or result from your use or distribution of the source 20 | code. 21 | 22 | THIS SOURCE CODE IS PROVIDED "AS IS" AND "WITH ALL FAULTS", WITHOUT 23 | ANY TECHNICAL SUPPORT OR ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, 24 | BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 25 | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. ALSO, THERE IS NO WARRANTY OF 26 | NON-INFRINGEMENT, TITLE OR QUIET ENJOYMENT. IN NO EVENT SHALL MACROMEDIA 27 | OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 28 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 29 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 30 | OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 31 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 32 | OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOURCE CODE, EVEN IF 33 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | 36 | package com.adobe.crypto 37 | { 38 | import mx.formatters.DateFormatter; 39 | import mx.utils.Base64Encoder; 40 | 41 | /** 42 | * Web Services Security Username Token 43 | * 44 | * Implementation based on algorithm description at 45 | * http://www.oasis-open.org/committees/wss/documents/WSS-Username-02-0223-merged.pdf 46 | */ 47 | public class WSSEUsernameToken 48 | { 49 | /** 50 | * Generates a WSSE Username Token. 51 | * 52 | * @param username The username 53 | * @param password The password 54 | * @param nonce A cryptographically random nonce (if null, the nonce 55 | * will be generated) 56 | * @param timestamp The time at which the token is generated (if null, 57 | * the time will be set to the moment of execution) 58 | * @return The generated token 59 | * @langversion ActionScript 3.0 60 | * @playerversion Flash 9.0 61 | * @tiptext 62 | */ 63 | public static function getUsernameToken(username:String, password:String, nonce:String=null, timestamp:Date=null):String 64 | { 65 | if (nonce == null) 66 | { 67 | nonce = generateNonce(); 68 | } 69 | nonce = base64Encode(nonce); 70 | 71 | var created:String = generateTimestamp(timestamp); 72 | 73 | var password64:String = getBase64Digest(nonce, 74 | created, 75 | password); 76 | 77 | var token:String = new String("UsernameToken Username=\""); 78 | token += username + "\", " + 79 | "PasswordDigest=\"" + password64 + "\", " + 80 | "Nonce=\"" + nonce + "\", " + 81 | "Created=\"" + created + "\""; 82 | return token; 83 | } 84 | 85 | private static function generateNonce():String 86 | { 87 | // Math.random returns a Number between 0 and 1. We don't want our 88 | // nonce to contain invalid characters (e.g. the period) so we 89 | // strip them out before returning the result. 90 | var s:String = Math.random().toString(); 91 | return s.replace(".", ""); 92 | } 93 | 94 | internal static function base64Encode(s:String):String 95 | { 96 | var encoder:Base64Encoder = new Base64Encoder(); 97 | encoder.encode(s); 98 | return encoder.flush(); 99 | } 100 | 101 | internal static function generateTimestamp(timestamp:Date):String 102 | { 103 | if (timestamp == null) 104 | { 105 | timestamp = new Date(); 106 | } 107 | var dateFormatter:DateFormatter = new DateFormatter(); 108 | dateFormatter.formatString = "YYYY-MM-DDTJJ:NN:SS" 109 | return dateFormatter.format(timestamp) + "Z"; 110 | } 111 | 112 | internal static function getBase64Digest(nonce:String, created:String, password:String):String 113 | { 114 | return SHA1.hashToBase64(nonce + created + password); 115 | } 116 | } 117 | } -------------------------------------------------------------------------------- /src/com/adobe/net/URIEncodingBitmap.as: -------------------------------------------------------------------------------- 1 | /* 2 | Adobe Systems Incorporated(r) Source Code License Agreement 3 | Copyright(c) 2005 Adobe Systems Incorporated. All rights reserved. 4 | 5 | Please read this Source Code License Agreement carefully before using 6 | the source code. 7 | 8 | Adobe Systems Incorporated grants to you a perpetual, worldwide, non-exclusive, 9 | no-charge, royalty-free, irrevocable copyright license, to reproduce, 10 | prepare derivative works of, publicly display, publicly perform, and 11 | distribute this source code and such derivative works in source or 12 | object code form without any attribution requirements. 13 | 14 | The name "Adobe Systems Incorporated" must not be used to endorse or promote products 15 | derived from the source code without prior written permission. 16 | 17 | You agree to indemnify, hold harmless and defend Adobe Systems Incorporated from and 18 | against any loss, damage, claims or lawsuits, including attorney's 19 | fees that arise or result from your use or distribution of the source 20 | code. 21 | 22 | THIS SOURCE CODE IS PROVIDED "AS IS" AND "WITH ALL FAULTS", WITHOUT 23 | ANY TECHNICAL SUPPORT OR ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, 24 | BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 25 | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. ALSO, THERE IS NO WARRANTY OF 26 | NON-INFRINGEMENT, TITLE OR QUIET ENJOYMENT. IN NO EVENT SHALL MACROMEDIA 27 | OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 28 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 29 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 30 | OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 31 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 32 | OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOURCE CODE, EVEN IF 33 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | 36 | package com.adobe.net 37 | { 38 | import flash.utils.ByteArray; 39 | 40 | /** 41 | * This class implements an efficient lookup table for URI 42 | * character escaping. This class is only needed if you 43 | * create a derived class of URI to handle custom URI 44 | * syntax. This class is used internally by URI. 45 | * 46 | * @langversion ActionScript 3.0 47 | * @playerversion Flash 9.0* 48 | */ 49 | public class URIEncodingBitmap extends ByteArray 50 | { 51 | /** 52 | * Constructor. Creates an encoding bitmap using the given 53 | * string of characters as the set of characters that need 54 | * to be URI escaped. 55 | * 56 | * @langversion ActionScript 3.0 57 | * @playerversion Flash 9.0 58 | */ 59 | public function URIEncodingBitmap(charsToEscape:String) : void 60 | { 61 | var i:int; 62 | var data:ByteArray = new ByteArray(); 63 | 64 | // Initialize our 128 bits (16 bytes) to zero 65 | for (i = 0; i < 16; i++) 66 | this.writeByte(0); 67 | 68 | data.writeUTFBytes(charsToEscape); 69 | data.position = 0; 70 | 71 | while (data.bytesAvailable) 72 | { 73 | var c:int = data.readByte(); 74 | 75 | if (c > 0x7f) 76 | continue; // only escape low bytes 77 | 78 | var enc:int; 79 | this.position = (c >> 3); 80 | enc = this.readByte(); 81 | enc |= 1 << (c & 0x7); 82 | this.position = (c >> 3); 83 | this.writeByte(enc); 84 | } 85 | } 86 | 87 | /** 88 | * Based on the data table contained in this object, check 89 | * if the given character should be escaped. 90 | * 91 | * @param char the character to be escaped. Only the first 92 | * character in the string is used. Any other characters 93 | * are ignored. 94 | * 95 | * @return the integer value of the raw UTF8 character. For 96 | * example, if '%' is given, the return value is 37 (0x25). 97 | * If the character given does not need to be escaped, the 98 | * return value is zero. 99 | * 100 | * @langversion ActionScript 3.0 101 | * @playerversion Flash 9.0 102 | */ 103 | public function ShouldEscape(char:String) : int 104 | { 105 | var data:ByteArray = new ByteArray(); 106 | var c:int, mask:int; 107 | 108 | // write the character into a ByteArray so 109 | // we can pull it out as a raw byte value. 110 | data.writeUTFBytes(char); 111 | data.position = 0; 112 | c = data.readByte(); 113 | 114 | if (c & 0x80) 115 | { 116 | // don't escape high byte characters. It can make international 117 | // URI's unreadable. We just want to escape characters that would 118 | // make URI syntax ambiguous. 119 | return 0; 120 | } 121 | else if ((c < 0x1f) || (c == 0x7f)) 122 | { 123 | // control characters must be escaped. 124 | return c; 125 | } 126 | 127 | this.position = (c >> 3); 128 | mask = this.readByte(); 129 | 130 | if (mask & (1 << (c & 0x7))) 131 | { 132 | // we need to escape this, return the numeric value 133 | // of the character 134 | return c; 135 | } 136 | else 137 | { 138 | return 0; 139 | } 140 | } 141 | } 142 | } -------------------------------------------------------------------------------- /src/com/adobe/utils/XMLUtil.as: -------------------------------------------------------------------------------- 1 | /* 2 | Adobe Systems Incorporated(r) Source Code License Agreement 3 | Copyright(c) 2005 Adobe Systems Incorporated. All rights reserved. 4 | 5 | Please read this Source Code License Agreement carefully before using 6 | the source code. 7 | 8 | Adobe Systems Incorporated grants to you a perpetual, worldwide, non-exclusive, 9 | no-charge, royalty-free, irrevocable copyright license, to reproduce, 10 | prepare derivative works of, publicly display, publicly perform, and 11 | distribute this source code and such derivative works in source or 12 | object code form without any attribution requirements. 13 | 14 | The name "Adobe Systems Incorporated" must not be used to endorse or promote products 15 | derived from the source code without prior written permission. 16 | 17 | You agree to indemnify, hold harmless and defend Adobe Systems Incorporated from and 18 | against any loss, damage, claims or lawsuits, including attorney's 19 | fees that arise or result from your use or distribution of the source 20 | code. 21 | 22 | THIS SOURCE CODE IS PROVIDED "AS IS" AND "WITH ALL FAULTS", WITHOUT 23 | ANY TECHNICAL SUPPORT OR ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, 24 | BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 25 | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. ALSO, THERE IS NO WARRANTY OF 26 | NON-INFRINGEMENT, TITLE OR QUIET ENJOYMENT. IN NO EVENT SHALL MACROMEDIA 27 | OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 28 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 29 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 30 | OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 31 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 32 | OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOURCE CODE, EVEN IF 33 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | 36 | package com.adobe.utils 37 | { 38 | 39 | public class XMLUtil 40 | { 41 | /** 42 | * Constant representing a text node type returned from XML.nodeKind. 43 | * 44 | * @see XML.nodeKind() 45 | * 46 | * @langversion ActionScript 3.0 47 | * @playerversion Flash 9.0 48 | */ 49 | public static const TEXT:String = "text"; 50 | 51 | /** 52 | * Constant representing a comment node type returned from XML.nodeKind. 53 | * 54 | * @see XML.nodeKind() 55 | * 56 | * @langversion ActionScript 3.0 57 | * @playerversion Flash 9.0 58 | */ 59 | public static const COMMENT:String = "comment"; 60 | 61 | /** 62 | * Constant representing a processing instruction type returned from XML.nodeKind. 63 | * 64 | * @see XML.nodeKind() 65 | * 66 | * @langversion ActionScript 3.0 67 | * @playerversion Flash 9.0 68 | */ 69 | public static const PROCESSING_INSTRUCTION:String = "processing-instruction"; 70 | 71 | /** 72 | * Constant representing an attribute type returned from XML.nodeKind. 73 | * 74 | * @see XML.nodeKind() 75 | * 76 | * @langversion ActionScript 3.0 77 | * @playerversion Flash 9.0 78 | */ 79 | public static const ATTRIBUTE:String = "attribute"; 80 | 81 | /** 82 | * Constant representing a element type returned from XML.nodeKind. 83 | * 84 | * @see XML.nodeKind() 85 | * 86 | * @langversion ActionScript 3.0 87 | * @playerversion Flash 9.0 88 | */ 89 | public static const ELEMENT:String = "element"; 90 | 91 | /** 92 | * Checks whether the specified string is valid and well formed XML. 93 | * 94 | * @param data The string that is being checked to see if it is valid XML. 95 | * 96 | * @return A Boolean value indicating whether the specified string is 97 | * valid XML. 98 | * 99 | * @langversion ActionScript 3.0 100 | * @playerversion Flash 9.0 101 | */ 102 | public static function isValidXML(data:String):Boolean 103 | { 104 | var xml:XML; 105 | 106 | try 107 | { 108 | xml = new XML(data); 109 | } 110 | catch(e:Error) 111 | { 112 | return false; 113 | } 114 | 115 | if(xml.nodeKind() != XMLUtil.ELEMENT) 116 | { 117 | return false; 118 | } 119 | 120 | return true; 121 | } 122 | 123 | /** 124 | * Returns the next sibling of the specified node relative to the node's parent. 125 | * 126 | * @param x The node whose next sibling will be returned. 127 | * 128 | * @return The next sibling of the node. null if the node does not have 129 | * a sibling after it, or if the node has no parent. 130 | * 131 | * @langversion ActionScript 3.0 132 | * @playerversion Flash 9.0 133 | */ 134 | public static function getNextSibling(x:XML):XML 135 | { 136 | return XMLUtil.getSiblingByIndex(x, 1); 137 | } 138 | 139 | /** 140 | * Returns the sibling before the specified node relative to the node's parent. 141 | * 142 | * @param x The node whose sibling before it will be returned. 143 | * 144 | * @return The sibling before the node. null if the node does not have 145 | * a sibling before it, or if the node has no parent. 146 | * 147 | * @langversion ActionScript 3.0 148 | * @playerversion Flash 9.0 149 | */ 150 | public static function getPreviousSibling(x:XML):XML 151 | { 152 | return XMLUtil.getSiblingByIndex(x, -1); 153 | } 154 | 155 | protected static function getSiblingByIndex(x:XML, count:int):XML 156 | { 157 | var out:XML; 158 | 159 | try 160 | { 161 | out = x.parent().children()[x.childIndex() + count]; 162 | } 163 | catch(e:Error) 164 | { 165 | return null; 166 | } 167 | 168 | return out; 169 | } 170 | } 171 | } -------------------------------------------------------------------------------- /src/com/adobe/images/PNGEncoder.as: -------------------------------------------------------------------------------- 1 | /* 2 | Adobe Systems Incorporated(r) Source Code License Agreement 3 | Copyright(c) 2005 Adobe Systems Incorporated. All rights reserved. 4 | 5 | Please read this Source Code License Agreement carefully before using 6 | the source code. 7 | 8 | Adobe Systems Incorporated grants to you a perpetual, worldwide, non-exclusive, 9 | no-charge, royalty-free, irrevocable copyright license, to reproduce, 10 | prepare derivative works of, publicly display, publicly perform, and 11 | distribute this source code and such derivative works in source or 12 | object code form without any attribution requirements. 13 | 14 | The name "Adobe Systems Incorporated" must not be used to endorse or promote products 15 | derived from the source code without prior written permission. 16 | 17 | You agree to indemnify, hold harmless and defend Adobe Systems Incorporated from and 18 | against any loss, damage, claims or lawsuits, including attorney's 19 | fees that arise or result from your use or distribution of the source 20 | code. 21 | 22 | THIS SOURCE CODE IS PROVIDED "AS IS" AND "WITH ALL FAULTS", WITHOUT 23 | ANY TECHNICAL SUPPORT OR ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, 24 | BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 25 | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. ALSO, THERE IS NO WARRANTY OF 26 | NON-INFRINGEMENT, TITLE OR QUIET ENJOYMENT. IN NO EVENT SHALL MACROMEDIA 27 | OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 28 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 29 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 30 | OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 31 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 32 | OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOURCE CODE, EVEN IF 33 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | package com.adobe.images 36 | { 37 | import flash.geom.*; 38 | import flash.display.Bitmap; 39 | import flash.display.BitmapData; 40 | import flash.utils.ByteArray; 41 | 42 | /** 43 | * Class that converts BitmapData into a valid PNG 44 | */ 45 | public class PNGEncoder 46 | { 47 | /** 48 | * Created a PNG image from the specified BitmapData 49 | * 50 | * @param image The BitmapData that will be converted into the PNG format. 51 | * @return a ByteArray representing the PNG encoded image data. 52 | * @langversion ActionScript 3.0 53 | * @playerversion Flash 9.0 54 | * @tiptext 55 | */ 56 | public static function encode(img:BitmapData):ByteArray { 57 | // Create output byte array 58 | var png:ByteArray = new ByteArray(); 59 | // Write PNG signature 60 | png.writeUnsignedInt(0x89504e47); 61 | png.writeUnsignedInt(0x0D0A1A0A); 62 | // Build IHDR chunk 63 | var IHDR:ByteArray = new ByteArray(); 64 | IHDR.writeInt(img.width); 65 | IHDR.writeInt(img.height); 66 | IHDR.writeUnsignedInt(0x08060000); // 32bit RGBA 67 | IHDR.writeByte(0); 68 | writeChunk(png,0x49484452,IHDR); 69 | // Build IDAT chunk 70 | var IDAT:ByteArray= new ByteArray(); 71 | for(var i:int=0;i < img.height;i++) { 72 | // no filter 73 | IDAT.writeByte(0); 74 | var p:uint; 75 | var j:int; 76 | if ( !img.transparent ) { 77 | for(j=0;j < img.width;j++) { 78 | p = img.getPixel(j,i); 79 | IDAT.writeUnsignedInt( 80 | uint(((p&0xFFFFFF) << 8)|0xFF)); 81 | } 82 | } else { 83 | for(j=0;j < img.width;j++) { 84 | p = img.getPixel32(j,i); 85 | IDAT.writeUnsignedInt( 86 | uint(((p&0xFFFFFF) << 8)| 87 | (p>>>24))); 88 | } 89 | } 90 | } 91 | IDAT.compress(); 92 | writeChunk(png,0x49444154,IDAT); 93 | // Build IEND chunk 94 | writeChunk(png,0x49454E44,null); 95 | // return PNG 96 | return png; 97 | } 98 | 99 | private static var crcTable:Array; 100 | private static var crcTableComputed:Boolean = false; 101 | 102 | private static function writeChunk(png:ByteArray, 103 | type:uint, data:ByteArray):void { 104 | if (!crcTableComputed) { 105 | crcTableComputed = true; 106 | crcTable = []; 107 | var c:uint; 108 | for (var n:uint = 0; n < 256; n++) { 109 | c = n; 110 | for (var k:uint = 0; k < 8; k++) { 111 | if (c & 1) { 112 | c = uint(uint(0xedb88320) ^ 113 | uint(c >>> 1)); 114 | } else { 115 | c = uint(c >>> 1); 116 | } 117 | } 118 | crcTable[n] = c; 119 | } 120 | } 121 | var len:uint = 0; 122 | if (data != null) { 123 | len = data.length; 124 | } 125 | png.writeUnsignedInt(len); 126 | var p:uint = png.position; 127 | png.writeUnsignedInt(type); 128 | if ( data != null ) { 129 | png.writeBytes(data); 130 | } 131 | var e:uint = png.position; 132 | png.position = p; 133 | c = 0xffffffff; 134 | for (var i:int = 0; i < (e-p); i++) { 135 | c = uint(crcTable[ 136 | (c ^ png.readUnsignedByte()) & 137 | uint(0xff)] ^ uint(c >>> 8)); 138 | } 139 | c = uint(c^uint(0xffffffff)); 140 | png.position = e; 141 | png.writeUnsignedInt(c); 142 | } 143 | } 144 | } -------------------------------------------------------------------------------- /src/com/adobe/utils/ArrayUtil.as: -------------------------------------------------------------------------------- 1 | /* 2 | Adobe Systems Incorporated(r) Source Code License Agreement 3 | Copyright(c) 2005 Adobe Systems Incorporated. All rights reserved. 4 | 5 | Please read this Source Code License Agreement carefully before using 6 | the source code. 7 | 8 | Adobe Systems Incorporated grants to you a perpetual, worldwide, non-exclusive, 9 | no-charge, royalty-free, irrevocable copyright license, to reproduce, 10 | prepare derivative works of, publicly display, publicly perform, and 11 | distribute this source code and such derivative works in source or 12 | object code form without any attribution requirements. 13 | 14 | The name "Adobe Systems Incorporated" must not be used to endorse or promote products 15 | derived from the source code without prior written permission. 16 | 17 | You agree to indemnify, hold harmless and defend Adobe Systems Incorporated from and 18 | against any loss, damage, claims or lawsuits, including attorney's 19 | fees that arise or result from your use or distribution of the source 20 | code. 21 | 22 | THIS SOURCE CODE IS PROVIDED "AS IS" AND "WITH ALL FAULTS", WITHOUT 23 | ANY TECHNICAL SUPPORT OR ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, 24 | BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 25 | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. ALSO, THERE IS NO WARRANTY OF 26 | NON-INFRINGEMENT, TITLE OR QUIET ENJOYMENT. IN NO EVENT SHALL MACROMEDIA 27 | OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 28 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 29 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 30 | OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 31 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 32 | OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOURCE CODE, EVEN IF 33 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | 36 | package com.adobe.utils 37 | { 38 | 39 | /** 40 | * Class that contains static utility methods for manipulating and working 41 | * with Arrays. 42 | * 43 | * Note that all APIs assume that they are working with well formed arrays. 44 | * i.e. they will only manipulate indexed values. 45 | * 46 | * @langversion ActionScript 3.0 47 | * @playerversion Flash 9.0 48 | * @tiptext 49 | */ 50 | public class ArrayUtil 51 | { 52 | 53 | /** 54 | * Determines whether the specified array contains the specified value. 55 | * 56 | * @param arr The array that will be checked for the specified value. 57 | * 58 | * @param value The object which will be searched for within the array 59 | * 60 | * @return True if the array contains the value, False if it does not. 61 | * 62 | * @langversion ActionScript 3.0 63 | * @playerversion Flash 9.0 64 | * @tiptext 65 | */ 66 | public static function arrayContainsValue(arr:Array, value:Object):Boolean 67 | { 68 | return (arr.indexOf(value) != -1); 69 | } 70 | 71 | /** 72 | * Remove all instances of the specified value from the array, 73 | * 74 | * @param arr The array from which the value will be removed 75 | * 76 | * @param value The object that will be removed from the array. 77 | * 78 | * @langversion ActionScript 3.0 79 | * @playerversion Flash 9.0 80 | * @tiptext 81 | */ 82 | public static function removeValueFromArray(arr:Array, value:Object):void 83 | { 84 | var len:uint = arr.length; 85 | 86 | for(var i:Number = len; i > -1; i--) 87 | { 88 | if(arr[i] === value) 89 | { 90 | arr.splice(i, 1); 91 | } 92 | } 93 | } 94 | 95 | /** 96 | * Create a new array that only contains unique instances of objects 97 | * in the specified array. 98 | * 99 | * Basically, this can be used to remove duplication object instances 100 | * from an array 101 | * 102 | * @param arr The array which contains the values that will be used to 103 | * create the new array that contains no duplicate values. 104 | * 105 | * @return A new array which only contains unique items from the specified 106 | * array. 107 | * 108 | * @langversion ActionScript 3.0 109 | * @playerversion Flash 9.0 110 | * @tiptext 111 | */ 112 | public static function createUniqueCopy(a:Array):Array 113 | { 114 | var newArray:Array = new Array(); 115 | 116 | var len:Number = a.length; 117 | var item:Object; 118 | 119 | for (var i:uint = 0; i < len; ++i) 120 | { 121 | item = a[i]; 122 | 123 | if(ArrayUtil.arrayContainsValue(newArray, item)) 124 | { 125 | continue; 126 | } 127 | 128 | newArray.push(item); 129 | } 130 | 131 | return newArray; 132 | } 133 | 134 | /** 135 | * Creates a copy of the specified array. 136 | * 137 | * Note that the array returned is a new array but the items within the 138 | * array are not copies of the items in the original array (but rather 139 | * references to the same items) 140 | * 141 | * @param arr The array that will be copies 142 | * 143 | * @return A new array which contains the same items as the array passed 144 | * in. 145 | * 146 | * @langversion ActionScript 3.0 147 | * @playerversion Flash 9.0 148 | * @tiptext 149 | */ 150 | public static function copyArray(arr:Array):Array 151 | { 152 | return arr.slice(); 153 | } 154 | 155 | /** 156 | * Compares two arrays and returns a boolean indicating whether the arrays 157 | * contain the same values at the same indexes. 158 | * 159 | * @param arr1 The first array that will be compared to the second. 160 | * 161 | * @param arr2 The second array that will be compared to the first. 162 | * 163 | * @return True if the arrays contains the same values at the same indexes. 164 | False if they do not. 165 | * 166 | * @langversion ActionScript 3.0 167 | * @playerversion Flash 9.0 168 | * @tiptext 169 | */ 170 | public static function arraysAreEqual(arr1:Array, arr2:Array):Boolean 171 | { 172 | if(arr1.length != arr2.length) 173 | { 174 | return false; 175 | } 176 | 177 | var len:Number = arr1.length; 178 | 179 | for(var i:Number = 0; i < len; i++) 180 | { 181 | if(arr1[i] !== arr2[i]) 182 | { 183 | return false; 184 | } 185 | } 186 | 187 | return true; 188 | } 189 | } 190 | } 191 | -------------------------------------------------------------------------------- /src/Webcam.as: -------------------------------------------------------------------------------- 1 | package { /* JPEGCam v1.0.9 */ /* Webcam library for capturing JPEG images and submitting to a server */ /* Copyright (c) 2008 - 2009 Joseph Huckaby */ /* Licensed under the GNU Lesser Public License */ /* http://www.gnu.org/licenses/lgpl.html */ import flash.display.LoaderInfo; import flash.display.Sprite; import flash.display.StageAlign; import flash.display.StageScaleMode; import flash.display.Bitmap; import flash.display.BitmapData; import flash.events.*; import flash.utils.*; import flash.media.Camera; import flash.media.Video; import flash.external.ExternalInterface; import flash.net.*; import flash.system.Security; import flash.system.SecurityPanel; import flash.media.Sound; import flash.media.SoundChannel; import flash.geom.Matrix; import com.adobe.images.JPGEncoder; public class Webcam extends Sprite { private var video:Video; private var encoder:JPGEncoder; private var snd:Sound; private var channel:SoundChannel = new SoundChannel(); private var jpeg_quality:int; private var video_width:int; private var video_height:int; private var server_width:int; private var server_height:int; private var camera:Camera; private var bmp:Bitmap; private var bmpdata:BitmapData; private var url:String; private var stealth:int; public function Webcam() { // class constructor flash.system.Security.allowDomain("*"); var flashvars:Object = LoaderInfo(this.root.loaderInfo).parameters; video_width = Math.floor( flashvars.width ); video_height = Math.floor( flashvars.height ); server_width = Math.floor( flashvars.server_width ); server_height = Math.floor( flashvars.server_height ); stage.scaleMode = StageScaleMode.NO_SCALE; // stage.scaleMode = StageScaleMode.EXACT_FIT; stage.align = StageAlign.TOP_LEFT; stage.stageWidth = Math.max(video_width, server_width); stage.stageHeight = Math.max(video_height, server_height); // Hack to auto-select iSight camera on Mac (JPEGCam Issue #5, submitted by manuel.gonzalez.noriega) // From: http://www.squidder.com/2009/03/09/trick-auto-select-mac-isight-in-flash/ var cameraIdx:int = -1; for (var idx = 0, len = Camera.names.length; idx < len; idx++) { if (Camera.names[idx] == "USB Video Class Video") { cameraIdx = idx; idx = len; } } if (cameraIdx > -1) camera = Camera.getCamera( String(cameraIdx) ); else camera = Camera.getCamera(); if (camera != null) { camera.addEventListener(ActivityEvent.ACTIVITY, activityHandler); video = new Video( Math.max(video_width, server_width), Math.max(video_height, server_height) ); video.attachCamera(camera); addChild(video); if ((video_width < server_width) && (video_height < server_height)) { video.scaleX = video_width / server_width; video.scaleY = video_height / server_height; } camera.setQuality(0, 100); camera.setKeyFrameInterval(10); camera.setMode( Math.max(video_width, server_width), Math.max(video_height, server_height), 30); // do not detect motion (may help reduce CPU usage) camera.setMotionLevel( 100 ); ExternalInterface.addCallback('_snap', snap); ExternalInterface.addCallback('_configure', configure); ExternalInterface.addCallback('_upload', upload); ExternalInterface.addCallback('_reset', reset); if (flashvars.shutter_enabled == 1) { snd = new Sound(); snd.load( new URLRequest( flashvars.shutter_url ) ); } jpeg_quality = 90; ExternalInterface.call('webcam.flash_notify', 'flashLoadComplete', true); } else { trace("You need a camera."); ExternalInterface.call('webcam.flash_notify', "error", "No camera was detected."); } } public function set_quality(new_quality:int) { // set JPEG image quality if (new_quality < 0) new_quality = 0; if (new_quality > 100) new_quality = 100; jpeg_quality = new_quality; } public function configure(panel:String = SecurityPanel.CAMERA) { // show configure dialog inside flash movie Security.showSettings(panel); } private function activityHandler(event:ActivityEvent):void { trace("activityHandler: " + event); } public function snap(url, new_quality, shutter, new_stealth = 0) { // take snapshot from camera, and upload if URL was provided if (new_quality) set_quality(new_quality); stealth = new_stealth; trace("in snap(), drawing to bitmap"); if (shutter) { channel = snd.play(); setTimeout( snap2, 10, url ); } else snap2(url); } public function snap2(url) { // take snapshot, convert to jpeg, submit to server bmpdata = new BitmapData( Math.max(video_width, server_width), Math.max(video_height, server_height) ); bmpdata.draw( video ); if (!stealth) { // draw snapshot on stage bmp = new Bitmap( bmpdata ); addChild( bmp ); // stop capturing video video.attachCamera( null ); removeChild( video ); } // if URL was provided, upload now if (url) upload( url ); } public function upload(url) { if (bmpdata) { if ((video_width > server_width) && (video_height > server_height)) { // resize image downward before submitting var tmpdata = new BitmapData(server_width, server_height); var matrix = new Matrix(); matrix.scale( server_width / video_width, server_height / video_height ); tmpdata.draw( bmpdata, matrix, null, null, null, true ); // smoothing bmpdata = tmpdata; } // need resize trace("converting to jpeg"); var ba:ByteArray; encoder = new JPGEncoder( jpeg_quality ); ba = encoder.encode( bmpdata ); trace("jpeg length: " + ba.length); var head:URLRequestHeader = new URLRequestHeader("Accept","text/*"); var req:URLRequest = new URLRequest( url ); req.requestHeaders.push(head); req.data = ba; req.method = URLRequestMethod.POST; req.contentType = "image/jpeg"; var loader:URLLoader = new URLLoader(); loader.addEventListener(Event.COMPLETE, onLoaded); trace("sending post to: " + url); try { loader.load(req); } catch (error:Error) { trace("Unable to load requested document."); ExternalInterface.call('webcam.flash_notify', "error", "Unable to post data: " + error); } } else { ExternalInterface.call('webcam.flash_notify', "error", "Nothing to upload, must capture an image first."); } } public function onLoaded(evt:Event):void { // image upload complete var msg = "unknown"; if (evt && evt.target && evt.target.data) msg = evt.target.data; ExternalInterface.call('webcam.flash_notify', "success", msg); } public function reset() { // reset video after taking snapshot if (bmp) { removeChild( bmp ); bmp = null; bmpdata = null; video.attachCamera(camera); addChild(video); } } } } -------------------------------------------------------------------------------- /htdocs/webcam.js: -------------------------------------------------------------------------------- 1 | /* JPEGCam v1.0.9 */ 2 | /* Webcam library for capturing JPEG images and submitting to a server */ 3 | /* Copyright (c) 2008 - 2009 Joseph Huckaby */ 4 | /* Licensed under the GNU Lesser Public License */ 5 | /* http://www.gnu.org/licenses/lgpl.html */ 6 | 7 | /* Usage: 8 | 16 | Take Snapshot 17 | */ 18 | 19 | // Everything is under a 'webcam' Namespace 20 | window.webcam = { 21 | version: '1.0.9', 22 | 23 | // globals 24 | ie: !!navigator.userAgent.match(/MSIE/), 25 | protocol: location.protocol.match(/https/i) ? 'https' : 'http', 26 | callback: null, // user callback for completed uploads 27 | swf_url: 'webcam.swf', // URI to webcam.swf movie (defaults to cwd) 28 | shutter_url: 'shutter.mp3', // URI to shutter.mp3 sound 29 | api_url: '', // URL to upload script 30 | loaded: false, // true when webcam movie finishes loading 31 | quality: 90, // JPEG quality (1 - 100) 32 | shutter_sound: true, // shutter sound effect on/off 33 | stealth: false, // stealth mode (do not freeze image upon capture) 34 | hooks: { 35 | onLoad: null, 36 | onComplete: null, 37 | onError: null 38 | }, // callback hook functions 39 | 40 | set_hook: function(name, callback) { 41 | // set callback hook 42 | // supported hooks: onLoad, onComplete, onError 43 | if (typeof(this.hooks[name]) == 'undefined') 44 | return alert("Hook type not supported: " + name); 45 | 46 | this.hooks[name] = callback; 47 | }, 48 | 49 | fire_hook: function(name, value) { 50 | // fire hook callback, passing optional value to it 51 | if (this.hooks[name]) { 52 | if (typeof(this.hooks[name]) == 'function') { 53 | // callback is function reference, call directly 54 | this.hooks[name](value); 55 | } 56 | else if (typeof(this.hooks[name]) == 'array') { 57 | // callback is PHP-style object instance method 58 | this.hooks[name][0][this.hooks[name][1]](value); 59 | } 60 | else if (window[this.hooks[name]]) { 61 | // callback is global function name 62 | window[ this.hooks[name] ](value); 63 | } 64 | return true; 65 | } 66 | return false; // no hook defined 67 | }, 68 | 69 | set_api_url: function(url) { 70 | // set location of upload API script 71 | this.api_url = url; 72 | }, 73 | 74 | set_swf_url: function(url) { 75 | // set location of SWF movie (defaults to webcam.swf in cwd) 76 | this.swf_url = url; 77 | }, 78 | 79 | get_html: function(width, height, server_width, server_height) { 80 | // Return HTML for embedding webcam capture movie 81 | // Specify pixel width and height (640x480, 320x240, etc.) 82 | // Server width and height are optional, and default to movie width/height 83 | if (!server_width) server_width = width; 84 | if (!server_height) server_height = height; 85 | 86 | var html = ''; 87 | var flashvars = 'shutter_enabled=' + (this.shutter_sound ? 1 : 0) + 88 | '&shutter_url=' + escape(this.shutter_url) + 89 | '&width=' + width + 90 | '&height=' + height + 91 | '&server_width=' + server_width + 92 | '&server_height=' + server_height; 93 | 94 | if (this.ie) { 95 | html += ''; 96 | } 97 | else { 98 | html += ''; 99 | } 100 | 101 | this.loaded = false; 102 | return html; 103 | }, 104 | 105 | get_movie: function() { 106 | // get reference to movie object/embed in DOM 107 | if (!this.loaded) return alert("ERROR: Movie is not loaded yet"); 108 | var movie = document.getElementById('webcam_movie'); 109 | if (!movie) alert("ERROR: Cannot locate movie 'webcam_movie' in DOM"); 110 | return movie; 111 | }, 112 | 113 | set_stealth: function(stealth) { 114 | // set or disable stealth mode 115 | this.stealth = stealth; 116 | }, 117 | 118 | snap: function(url, callback, stealth) { 119 | // take snapshot and send to server 120 | // specify fully-qualified URL to server API script 121 | // and callback function (string or function object) 122 | if (callback) this.set_hook('onComplete', callback); 123 | if (url) this.set_api_url(url); 124 | if (typeof(stealth) != 'undefined') this.set_stealth( stealth ); 125 | 126 | this.get_movie()._snap( this.api_url, this.quality, this.shutter_sound ? 1 : 0, this.stealth ? 1 : 0 ); 127 | }, 128 | 129 | freeze: function() { 130 | // freeze webcam image (capture but do not upload) 131 | this.get_movie()._snap('', this.quality, this.shutter_sound ? 1 : 0, 0 ); 132 | }, 133 | 134 | upload: function(url, callback) { 135 | // upload image to server after taking snapshot 136 | // specify fully-qualified URL to server API script 137 | // and callback function (string or function object) 138 | if (callback) this.set_hook('onComplete', callback); 139 | if (url) this.set_api_url(url); 140 | 141 | this.get_movie()._upload( this.api_url ); 142 | }, 143 | 144 | reset: function() { 145 | // reset movie after taking snapshot 146 | this.get_movie()._reset(); 147 | }, 148 | 149 | configure: function(panel) { 150 | // open flash configuration panel -- specify tab name: 151 | // "camera", "privacy", "default", "localStorage", "microphone", "settingsManager" 152 | if (!panel) panel = "camera"; 153 | this.get_movie()._configure(panel); 154 | }, 155 | 156 | set_quality: function(new_quality) { 157 | // set the JPEG quality (1 - 100) 158 | // default is 90 159 | this.quality = new_quality; 160 | }, 161 | 162 | set_shutter_sound: function(enabled, url) { 163 | // enable or disable the shutter sound effect 164 | // defaults to enabled 165 | this.shutter_sound = enabled; 166 | this.shutter_url = url ? url : 'shutter.mp3'; 167 | }, 168 | 169 | flash_notify: function(type, msg) { 170 | // receive notification from flash about event 171 | switch (type) { 172 | case 'flashLoadComplete': 173 | // movie loaded successfully 174 | this.loaded = true; 175 | this.fire_hook('onLoad'); 176 | break; 177 | 178 | case 'error': 179 | // HTTP POST error most likely 180 | if (!this.fire_hook('onError', msg)) { 181 | alert("JPEGCam Flash Error: " + msg); 182 | } 183 | break; 184 | 185 | case 'success': 186 | // upload complete, execute user callback function 187 | // and pass raw API script results to function 188 | this.fire_hook('onComplete', msg.toString()); 189 | break; 190 | 191 | default: 192 | // catch-all, just in case 193 | alert("jpegcam flash_notify: " + type + ": " + msg); 194 | break; 195 | } 196 | } 197 | }; 198 | -------------------------------------------------------------------------------- /src/com/adobe/serialization/json/JSONDecoder.as: -------------------------------------------------------------------------------- 1 | /* 2 | Adobe Systems Incorporated(r) Source Code License Agreement 3 | Copyright(c) 2005 Adobe Systems Incorporated. All rights reserved. 4 | 5 | Please read this Source Code License Agreement carefully before using 6 | the source code. 7 | 8 | Adobe Systems Incorporated grants to you a perpetual, worldwide, non-exclusive, 9 | no-charge, royalty-free, irrevocable copyright license, to reproduce, 10 | prepare derivative works of, publicly display, publicly perform, and 11 | distribute this source code and such derivative works in source or 12 | object code form without any attribution requirements. 13 | 14 | The name "Adobe Systems Incorporated" must not be used to endorse or promote products 15 | derived from the source code without prior written permission. 16 | 17 | You agree to indemnify, hold harmless and defend Adobe Systems Incorporated from and 18 | against any loss, damage, claims or lawsuits, including attorney's 19 | fees that arise or result from your use or distribution of the source 20 | code. 21 | 22 | THIS SOURCE CODE IS PROVIDED "AS IS" AND "WITH ALL FAULTS", WITHOUT 23 | ANY TECHNICAL SUPPORT OR ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, 24 | BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 25 | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. ALSO, THERE IS NO WARRANTY OF 26 | NON-INFRINGEMENT, TITLE OR QUIET ENJOYMENT. IN NO EVENT SHALL MACROMEDIA 27 | OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 28 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 29 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 30 | OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 31 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 32 | OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOURCE CODE, EVEN IF 33 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | 36 | package com.adobe.serialization.json { 37 | 38 | public class JSONDecoder { 39 | 40 | /** The value that will get parsed from the JSON string */ 41 | private var value:*; 42 | 43 | /** The tokenizer designated to read the JSON string */ 44 | private var tokenizer:JSONTokenizer; 45 | 46 | /** The current token from the tokenizer */ 47 | private var token:JSONToken; 48 | 49 | /** 50 | * Constructs a new JSONDecoder to parse a JSON string 51 | * into a native object. 52 | * 53 | * @param s The JSON string to be converted 54 | * into a native object 55 | * @langversion ActionScript 3.0 56 | * @playerversion Flash 9.0 57 | * @tiptext 58 | */ 59 | public function JSONDecoder( s:String ) { 60 | 61 | tokenizer = new JSONTokenizer( s ); 62 | 63 | nextToken(); 64 | value = parseValue(); 65 | } 66 | 67 | /** 68 | * Gets the internal object that was created by parsing 69 | * the JSON string passed to the constructor. 70 | * 71 | * @return The internal object representation of the JSON 72 | * string that was passed to the constructor 73 | * @langversion ActionScript 3.0 74 | * @playerversion Flash 9.0 75 | * @tiptext 76 | */ 77 | public function getValue():* { 78 | return value; 79 | } 80 | 81 | /** 82 | * Returns the next token from the tokenzier reading 83 | * the JSON string 84 | */ 85 | private function nextToken():JSONToken { 86 | return token = tokenizer.getNextToken(); 87 | } 88 | 89 | /** 90 | * Attempt to parse an array 91 | */ 92 | private function parseArray():Array { 93 | // create an array internally that we're going to attempt 94 | // to parse from the tokenizer 95 | var a:Array = new Array(); 96 | 97 | // grab the next token from the tokenizer to move 98 | // past the opening [ 99 | nextToken(); 100 | 101 | // check to see if we have an empty array 102 | if ( token.type == JSONTokenType.RIGHT_BRACKET ) { 103 | // we're done reading the array, so return it 104 | return a; 105 | } 106 | 107 | // deal with elements of the array, and use an "infinite" 108 | // loop because we could have any amount of elements 109 | while ( true ) { 110 | // read in the value and add it to the array 111 | a.push ( parseValue() ); 112 | 113 | // after the value there should be a ] or a , 114 | nextToken(); 115 | 116 | if ( token.type == JSONTokenType.RIGHT_BRACKET ) { 117 | // we're done reading the array, so return it 118 | return a; 119 | } else if ( token.type == JSONTokenType.COMMA ) { 120 | // move past the comma and read another value 121 | nextToken(); 122 | } else { 123 | tokenizer.parseError( "Expecting ] or , but found " + token.value ); 124 | } 125 | } 126 | return null; 127 | } 128 | 129 | /** 130 | * Attempt to parse an object 131 | */ 132 | private function parseObject():Object { 133 | // create the object internally that we're going to 134 | // attempt to parse from the tokenizer 135 | var o:Object = new Object(); 136 | 137 | // store the string part of an object member so 138 | // that we can assign it a value in the object 139 | var key:String 140 | 141 | // grab the next token from the tokenizer 142 | nextToken(); 143 | 144 | // check to see if we have an empty object 145 | if ( token.type == JSONTokenType.RIGHT_BRACE ) { 146 | // we're done reading the object, so return it 147 | return o; 148 | } 149 | 150 | // deal with members of the object, and use an "infinite" 151 | // loop because we could have any amount of members 152 | while ( true ) { 153 | 154 | if ( token.type == JSONTokenType.STRING ) { 155 | // the string value we read is the key for the object 156 | key = String( token.value ); 157 | 158 | // move past the string to see what's next 159 | nextToken(); 160 | 161 | // after the string there should be a : 162 | if ( token.type == JSONTokenType.COLON ) { 163 | 164 | // move past the : and read/assign a value for the key 165 | nextToken(); 166 | o[key] = parseValue(); 167 | 168 | // move past the value to see what's next 169 | nextToken(); 170 | 171 | // after the value there's either a } or a , 172 | if ( token.type == JSONTokenType.RIGHT_BRACE ) { 173 | // // we're done reading the object, so return it 174 | return o; 175 | 176 | } else if ( token.type == JSONTokenType.COMMA ) { 177 | // skip past the comma and read another member 178 | nextToken(); 179 | } else { 180 | tokenizer.parseError( "Expecting } or , but found " + token.value ); 181 | } 182 | } else { 183 | tokenizer.parseError( "Expecting : but found " + token.value ); 184 | } 185 | } else { 186 | tokenizer.parseError( "Expecting string but found " + token.value ); 187 | } 188 | } 189 | return null; 190 | } 191 | 192 | /** 193 | * Attempt to parse a value 194 | */ 195 | private function parseValue():Object { 196 | 197 | switch ( token.type ) { 198 | case JSONTokenType.LEFT_BRACE: 199 | return parseObject(); 200 | 201 | case JSONTokenType.LEFT_BRACKET: 202 | return parseArray(); 203 | 204 | case JSONTokenType.STRING: 205 | case JSONTokenType.NUMBER: 206 | case JSONTokenType.TRUE: 207 | case JSONTokenType.FALSE: 208 | case JSONTokenType.NULL: 209 | return token.value; 210 | 211 | default: 212 | tokenizer.parseError( "Unexpected " + token.value ); 213 | 214 | } 215 | return null; 216 | } 217 | } 218 | } 219 | -------------------------------------------------------------------------------- /src/com/adobe/utils/StringUtil.as: -------------------------------------------------------------------------------- 1 | /* 2 | Adobe Systems Incorporated(r) Source Code License Agreement 3 | Copyright(c) 2005 Adobe Systems Incorporated. All rights reserved. 4 | 5 | Please read this Source Code License Agreement carefully before using 6 | the source code. 7 | 8 | Adobe Systems Incorporated grants to you a perpetual, worldwide, non-exclusive, 9 | no-charge, royalty-free, irrevocable copyright license, to reproduce, 10 | prepare derivative works of, publicly display, publicly perform, and 11 | distribute this source code and such derivative works in source or 12 | object code form without any attribution requirements. 13 | 14 | The name "Adobe Systems Incorporated" must not be used to endorse or promote products 15 | derived from the source code without prior written permission. 16 | 17 | You agree to indemnify, hold harmless and defend Adobe Systems Incorporated from and 18 | against any loss, damage, claims or lawsuits, including attorney's 19 | fees that arise or result from your use or distribution of the source 20 | code. 21 | 22 | THIS SOURCE CODE IS PROVIDED "AS IS" AND "WITH ALL FAULTS", WITHOUT 23 | ANY TECHNICAL SUPPORT OR ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, 24 | BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 25 | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. ALSO, THERE IS NO WARRANTY OF 26 | NON-INFRINGEMENT, TITLE OR QUIET ENJOYMENT. IN NO EVENT SHALL MACROMEDIA 27 | OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 28 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 29 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 30 | OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 31 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 32 | OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOURCE CODE, EVEN IF 33 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | 36 | package com.adobe.utils 37 | { 38 | 39 | /** 40 | * Class that contains static utility methods for manipulating Strings. 41 | * 42 | * @langversion ActionScript 3.0 43 | * @playerversion Flash 9.0 44 | * @tiptext 45 | */ 46 | public class StringUtil 47 | { 48 | 49 | 50 | /** 51 | * Does a case insensitive compare or two strings and returns true if 52 | * they are equal. 53 | * 54 | * @param s1 The first string to compare. 55 | * 56 | * @param s2 The second string to compare. 57 | * 58 | * @returns A boolean value indicating whether the strings' values are 59 | * equal in a case sensitive compare. 60 | * 61 | * @langversion ActionScript 3.0 62 | * @playerversion Flash 9.0 63 | * @tiptext 64 | */ 65 | public static function stringsAreEqual(s1:String, s2:String, 66 | caseSensitive:Boolean):Boolean 67 | { 68 | if(caseSensitive) 69 | { 70 | return (s1 == s2); 71 | } 72 | else 73 | { 74 | return (s1.toUpperCase() == s2.toUpperCase()); 75 | } 76 | } 77 | 78 | /** 79 | * Removes whitespace from the front and the end of the specified 80 | * string. 81 | * 82 | * @param input The String whose beginning and ending whitespace will 83 | * will be removed. 84 | * 85 | * @returns A String with whitespace removed from the begining and end 86 | * 87 | * @langversion ActionScript 3.0 88 | * @playerversion Flash 9.0 89 | * @tiptext 90 | */ 91 | public static function trim(input:String):String 92 | { 93 | return StringUtil.ltrim(StringUtil.rtrim(input)); 94 | } 95 | 96 | /** 97 | * Removes whitespace from the front of the specified string. 98 | * 99 | * @param input The String whose beginning whitespace will will be removed. 100 | * 101 | * @returns A String with whitespace removed from the begining 102 | * 103 | * @langversion ActionScript 3.0 104 | * @playerversion Flash 9.0 105 | * @tiptext 106 | */ 107 | public static function ltrim(input:String):String 108 | { 109 | var size:Number = input.length; 110 | for(var i:Number = 0; i < size; i++) 111 | { 112 | if(input.charCodeAt(i) > 32) 113 | { 114 | return input.substring(i); 115 | } 116 | } 117 | return ""; 118 | } 119 | 120 | /** 121 | * Removes whitespace from the end of the specified string. 122 | * 123 | * @param input The String whose ending whitespace will will be removed. 124 | * 125 | * @returns A String with whitespace removed from the end 126 | * 127 | * @langversion ActionScript 3.0 128 | * @playerversion Flash 9.0 129 | * @tiptext 130 | */ 131 | public static function rtrim(input:String):String 132 | { 133 | var size:Number = input.length; 134 | for(var i:Number = size; i > 0; i--) 135 | { 136 | if(input.charCodeAt(i - 1) > 32) 137 | { 138 | return input.substring(0, i); 139 | } 140 | } 141 | 142 | return ""; 143 | } 144 | 145 | /** 146 | * Determines whether the specified string begins with the spcified prefix. 147 | * 148 | * @param input The string that the prefix will be checked against. 149 | * 150 | * @param prefix The prefix that will be tested against the string. 151 | * 152 | * @returns True if the string starts with the prefix, false if it does not. 153 | * 154 | * @langversion ActionScript 3.0 155 | * @playerversion Flash 9.0 156 | * @tiptext 157 | */ 158 | public static function beginsWith(input:String, prefix:String):Boolean 159 | { 160 | return (prefix == input.substring(0, prefix.length)); 161 | } 162 | 163 | /** 164 | * Determines whether the specified string ends with the spcified suffix. 165 | * 166 | * @param input The string that the suffic will be checked against. 167 | * 168 | * @param prefix The suffic that will be tested against the string. 169 | * 170 | * @returns True if the string ends with the suffix, false if it does not. 171 | * 172 | * @langversion ActionScript 3.0 173 | * @playerversion Flash 9.0 174 | * @tiptext 175 | */ 176 | public static function endsWith(input:String, suffix:String):Boolean 177 | { 178 | return (suffix == input.substring(input.length - suffix.length)); 179 | } 180 | 181 | /** 182 | * Removes all instances of the remove string in the input string. 183 | * 184 | * @param input The string that will be checked for instances of remove 185 | * string 186 | * 187 | * @param remove The string that will be removed from the input string. 188 | * 189 | * @returns A String with the remove string removed. 190 | * 191 | * @langversion ActionScript 3.0 192 | * @playerversion Flash 9.0 193 | * @tiptext 194 | */ 195 | public static function remove(input:String, remove:String):String 196 | { 197 | return StringUtil.replace(input, remove, ""); 198 | } 199 | 200 | /** 201 | * Replaces all instances of the replace string in the input string 202 | * with the replaceWith string. 203 | * 204 | * @param input The string that instances of replace string will be 205 | * replaces with removeWith string. 206 | * 207 | * @param replace The string that will be replaced by instances of 208 | * the replaceWith string. 209 | * 210 | * @param replaceWith The string that will replace instances of replace 211 | * string. 212 | * 213 | * @returns A new String with the replace string replaced with the 214 | * replaceWith string. 215 | * 216 | * @langversion ActionScript 3.0 217 | * @playerversion Flash 9.0 218 | * @tiptext 219 | */ 220 | public static function replace(input:String, replace:String, replaceWith:String):String 221 | { 222 | //change to StringBuilder 223 | var sb:String = new String(); 224 | var found:Boolean = false; 225 | 226 | var sLen:Number = input.length; 227 | var rLen:Number = replace.length; 228 | 229 | for (var i:Number = 0; i < sLen; i++) 230 | { 231 | if(input.charAt(i) == replace.charAt(0)) 232 | { 233 | found = true; 234 | for(var j:Number = 0; j < rLen; j++) 235 | { 236 | if(!(input.charAt(i + j) == replace.charAt(j))) 237 | { 238 | found = false; 239 | break; 240 | } 241 | } 242 | 243 | if(found) 244 | { 245 | sb += replaceWith; 246 | i = i + (rLen - 1); 247 | continue; 248 | } 249 | } 250 | sb += input.charAt(i); 251 | } 252 | //TODO : if the string is not found, should we return the original 253 | //string? 254 | return sb; 255 | } 256 | } 257 | } -------------------------------------------------------------------------------- /src/com/adobe/net/proxies/RFC2817Socket.as: -------------------------------------------------------------------------------- 1 | /* 2 | Adobe Systems Incorporated(r) Source Code License Agreement 3 | Copyright(c) 2005 Adobe Systems Incorporated. All rights reserved. 4 | 5 | Please read this Source Code License Agreement carefully before using 6 | the source code. 7 | 8 | Adobe Systems Incorporated grants to you a perpetual, worldwide, non-exclusive, 9 | no-charge, royalty-free, irrevocable copyright license, to reproduce, 10 | prepare derivative works of, publicly display, publicly perform, and 11 | distribute this source code and such derivative works in source or 12 | object code form without any attribution requirements. 13 | 14 | The name "Adobe Systems Incorporated" must not be used to endorse or promote products 15 | derived from the source code without prior written permission. 16 | 17 | You agree to indemnify, hold harmless and defend Adobe Systems Incorporated from and 18 | against any loss, damage, claims or lawsuits, including attorney's 19 | fees that arise or result from your use or distribution of the source 20 | code. 21 | 22 | THIS SOURCE CODE IS PROVIDED "AS IS" AND "WITH ALL FAULTS", WITHOUT 23 | ANY TECHNICAL SUPPORT OR ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, 24 | BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 25 | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. ALSO, THERE IS NO WARRANTY OF 26 | NON-INFRINGEMENT, TITLE OR QUIET ENJOYMENT. IN NO EVENT SHALL MACROMEDIA 27 | OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 28 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 29 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 30 | OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 31 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 32 | OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOURCE CODE, EVEN IF 33 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | package com.adobe.net.proxies 36 | { 37 | import flash.events.Event; 38 | import flash.events.IOErrorEvent; 39 | import flash.events.ProgressEvent; 40 | import flash.net.Socket; 41 | 42 | /** 43 | * This class allows TCP socket connections through HTTP proxies in accordance with 44 | * RFC 2817: 45 | * 46 | * ftp://ftp.rfc-editor.org/in-notes/rfc2817.txt 47 | * 48 | * It can also be used to make direct connections to a destination, as well. If you 49 | * pass the host and port into the constructor, no proxy will be used. You can also 50 | * call connect, passing in the host and the port, and if you didn't set the proxy 51 | * info, a direct connection will be made. A proxy is only used after you have called 52 | * the setProxyInfo function. 53 | * 54 | * The connection to and negotiation with the proxy is completely hidden. All the 55 | * same events are thrown whether you are using a proxy or not, and the data you 56 | * receive from the target server will look exact as it would if you were connected 57 | * to it directly rather than through a proxy. 58 | * 59 | * @author Christian Cantrell 60 | * 61 | **/ 62 | public class RFC2817Socket 63 | extends Socket 64 | { 65 | private var proxyHost:String = null; 66 | private var host:String = null; 67 | private var proxyPort:int = 0; 68 | private var port:int = 0; 69 | private var deferredEventHandlers:Object = new Object(); 70 | private var buffer:String = new String(); 71 | 72 | /** 73 | * Construct a new RFC2817Socket object. If you pass in the host and the port, 74 | * no proxy will be used. If you want to use a proxy, instantiate with no 75 | * arguments, call setProxyInfo, then call connect. 76 | **/ 77 | public function RFC2817Socket(host:String = null, port:int = 0) 78 | { 79 | if (host != null && port != 0) 80 | { 81 | super(host, port); 82 | } 83 | } 84 | 85 | /** 86 | * Set the proxy host and port number. Your connection will only proxied if 87 | * this function has been called. 88 | **/ 89 | public function setProxyInfo(host:String, port:int):void 90 | { 91 | this.proxyHost = host; 92 | this.proxyPort = port; 93 | 94 | var deferredSocketDataHandler:Object = this.deferredEventHandlers[ProgressEvent.SOCKET_DATA]; 95 | var deferredConnectHandler:Object = this.deferredEventHandlers[Event.CONNECT]; 96 | 97 | if (deferredSocketDataHandler != null) 98 | { 99 | super.removeEventListener(ProgressEvent.SOCKET_DATA, deferredSocketDataHandler.listener, deferredSocketDataHandler.useCapture); 100 | } 101 | 102 | if (deferredConnectHandler != null) 103 | { 104 | super.removeEventListener(Event.CONNECT, deferredConnectHandler.listener, deferredConnectHandler.useCapture); 105 | } 106 | } 107 | 108 | /** 109 | * Connect to the specified host over the specified port. If you want your 110 | * connection proxied, call the setProxyInfo function first. 111 | **/ 112 | public override function connect(host:String, port:int):void 113 | { 114 | if (this.proxyHost == null) 115 | { 116 | this.redirectConnectEvent(); 117 | this.redirectSocketDataEvent(); 118 | super.connect(host, port); 119 | } 120 | else 121 | { 122 | this.host = host; 123 | this.port = port; 124 | super.addEventListener(Event.CONNECT, this.onConnect); 125 | super.addEventListener(ProgressEvent.SOCKET_DATA, this.onSocketData); 126 | super.connect(this.proxyHost, this.proxyPort); 127 | } 128 | } 129 | 130 | private function onConnect(event:Event):void 131 | { 132 | this.writeUTFBytes("CONNECT "+this.host+":"+this.port+" HTTP/1.1\n\n"); 133 | this.flush(); 134 | this.redirectConnectEvent(); 135 | } 136 | 137 | private function onSocketData(event:ProgressEvent):void 138 | { 139 | while (this.bytesAvailable != 0) 140 | { 141 | this.buffer += this.readUTFBytes(1); 142 | if (this.buffer.search(/\r?\n\r?\n$/) != -1) 143 | { 144 | this.checkResponse(event); 145 | break; 146 | } 147 | } 148 | } 149 | 150 | private function checkResponse(event:ProgressEvent):void 151 | { 152 | var responseCode:String = this.buffer.substr(this.buffer.indexOf(" ")+1, 3); 153 | 154 | if (responseCode.search(/^2/) == -1) 155 | { 156 | var ioError:IOErrorEvent = new IOErrorEvent(IOErrorEvent.IO_ERROR); 157 | ioError.text = "Error connecting to the proxy ["+this.proxyHost+"] on port ["+this.proxyPort+"]: " + this.buffer; 158 | this.dispatchEvent(ioError); 159 | } 160 | else 161 | { 162 | this.redirectSocketDataEvent(); 163 | this.dispatchEvent(new Event(Event.CONNECT)); 164 | if (this.bytesAvailable > 0) 165 | { 166 | this.dispatchEvent(event); 167 | } 168 | } 169 | this.buffer = null; 170 | } 171 | 172 | private function redirectConnectEvent():void 173 | { 174 | super.removeEventListener(Event.CONNECT, onConnect); 175 | var deferredEventHandler:Object = this.deferredEventHandlers[Event.CONNECT]; 176 | if (deferredEventHandler != null) 177 | { 178 | super.addEventListener(Event.CONNECT, deferredEventHandler.listener, deferredEventHandler.useCapture, deferredEventHandler.priority, deferredEventHandler.useWeakReference); 179 | } 180 | } 181 | 182 | private function redirectSocketDataEvent():void 183 | { 184 | super.removeEventListener(ProgressEvent.SOCKET_DATA, onSocketData); 185 | var deferredEventHandler:Object = this.deferredEventHandlers[ProgressEvent.SOCKET_DATA]; 186 | if (deferredEventHandler != null) 187 | { 188 | super.addEventListener(ProgressEvent.SOCKET_DATA, deferredEventHandler.listener, deferredEventHandler.useCapture, deferredEventHandler.priority, deferredEventHandler.useWeakReference); 189 | } 190 | } 191 | 192 | public override function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int=0.0, useWeakReference:Boolean=false):void 193 | { 194 | if (type == Event.CONNECT || type == ProgressEvent.SOCKET_DATA) 195 | { 196 | this.deferredEventHandlers[type] = {listener:listener,useCapture:useCapture, priority:priority, useWeakReference:useWeakReference}; 197 | } 198 | else 199 | { 200 | super.addEventListener(type, listener, useCapture, priority, useWeakReference); 201 | } 202 | } 203 | } 204 | } -------------------------------------------------------------------------------- /src/com/adobe/crypto/SHA1.as: -------------------------------------------------------------------------------- 1 | /* 2 | Adobe Systems Incorporated(r) Source Code License Agreement 3 | Copyright(c) 2005 Adobe Systems Incorporated. All rights reserved. 4 | 5 | Please read this Source Code License Agreement carefully before using 6 | the source code. 7 | 8 | Adobe Systems Incorporated grants to you a perpetual, worldwide, non-exclusive, 9 | no-charge, royalty-free, irrevocable copyright license, to reproduce, 10 | prepare derivative works of, publicly display, publicly perform, and 11 | distribute this source code and such derivative works in source or 12 | object code form without any attribution requirements. 13 | 14 | The name "Adobe Systems Incorporated" must not be used to endorse or promote products 15 | derived from the source code without prior written permission. 16 | 17 | You agree to indemnify, hold harmless and defend Adobe Systems Incorporated from and 18 | against any loss, damage, claims or lawsuits, including attorney's 19 | fees that arise or result from your use or distribution of the source 20 | code. 21 | 22 | THIS SOURCE CODE IS PROVIDED "AS IS" AND "WITH ALL FAULTS", WITHOUT 23 | ANY TECHNICAL SUPPORT OR ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, 24 | BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 25 | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. ALSO, THERE IS NO WARRANTY OF 26 | NON-INFRINGEMENT, TITLE OR QUIET ENJOYMENT. IN NO EVENT SHALL MACROMEDIA 27 | OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 28 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 29 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 30 | OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 31 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 32 | OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOURCE CODE, EVEN IF 33 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | 36 | package com.adobe.crypto 37 | { 38 | import com.adobe.utils.IntUtil; 39 | import flash.utils.ByteArray; 40 | import mx.utils.Base64Encoder; 41 | 42 | /** 43 | * US Secure Hash Algorithm 1 (SHA1) 44 | * 45 | * Implementation based on algorithm description at 46 | * http://www.faqs.org/rfcs/rfc3174.html 47 | */ 48 | public class SHA1 49 | { 50 | /** 51 | * Performs the SHA1 hash algorithm on a string. 52 | * 53 | * @param s The string to hash 54 | * @return A string containing the hash value of s 55 | * @langversion ActionScript 3.0 56 | * @playerversion 9.0 57 | * @tiptext 58 | */ 59 | public static function hash( s:String ):String 60 | { 61 | var blocks:Array = createBlocksFromString( s ); 62 | var byteArray:ByteArray = hashBlocks( blocks ); 63 | 64 | return IntUtil.toHex( byteArray.readInt(), true ) 65 | + IntUtil.toHex( byteArray.readInt(), true ) 66 | + IntUtil.toHex( byteArray.readInt(), true ) 67 | + IntUtil.toHex( byteArray.readInt(), true ) 68 | + IntUtil.toHex( byteArray.readInt(), true ); 69 | } 70 | 71 | /** 72 | * Performs the SHA1 hash algorithm on a ByteArray. 73 | * 74 | * @param data The ByteArray data to hash 75 | * @return A string containing the hash value of data 76 | * @langversion ActionScript 3.0 77 | * @playerversion 9.0 78 | */ 79 | public static function hashBytes( data:ByteArray ):String 80 | { 81 | var blocks:Array = SHA1.createBlocksFromByteArray( data ); 82 | var byteArray:ByteArray = hashBlocks(blocks); 83 | 84 | return IntUtil.toHex( byteArray.readInt(), true ) 85 | + IntUtil.toHex( byteArray.readInt(), true ) 86 | + IntUtil.toHex( byteArray.readInt(), true ) 87 | + IntUtil.toHex( byteArray.readInt(), true ) 88 | + IntUtil.toHex( byteArray.readInt(), true ); 89 | } 90 | 91 | /** 92 | * Performs the SHA1 hash algorithm on a string, then does 93 | * Base64 encoding on the result. 94 | * 95 | * @param s The string to hash 96 | * @return The base64 encoded hash value of s 97 | * @langversion ActionScript 3.0 98 | * @playerversion 9.0 99 | * @tiptext 100 | */ 101 | public static function hashToBase64( s:String ):String 102 | { 103 | var blocks:Array = SHA1.createBlocksFromString( s ); 104 | var byteArray:ByteArray = hashBlocks(blocks); 105 | 106 | // ByteArray.toString() returns the contents as a UTF-8 string, 107 | // which we can't use because certain byte sequences might trigger 108 | // a UTF-8 conversion. Instead, we convert the bytes to characters 109 | // one by one. 110 | var charsInByteArray:String = ""; 111 | byteArray.position = 0; 112 | for (var j:int = 0; j < byteArray.length; j++) 113 | { 114 | var byte:uint = byteArray.readUnsignedByte(); 115 | charsInByteArray += String.fromCharCode(byte); 116 | } 117 | 118 | var encoder:Base64Encoder = new Base64Encoder(); 119 | encoder.encode(charsInByteArray); 120 | return encoder.flush(); 121 | } 122 | 123 | private static function hashBlocks( blocks:Array ):ByteArray 124 | { 125 | // initialize the h's 126 | var h0:int = 0x67452301; 127 | var h1:int = 0xefcdab89; 128 | var h2:int = 0x98badcfe; 129 | var h3:int = 0x10325476; 130 | var h4:int = 0xc3d2e1f0; 131 | 132 | var len:int = blocks.length; 133 | var w:Array = new Array( 80 ); 134 | 135 | // loop over all of the blocks 136 | for ( var i:int = 0; i < len; i += 16 ) { 137 | 138 | // 6.1.c 139 | var a:int = h0; 140 | var b:int = h1; 141 | var c:int = h2; 142 | var d:int = h3; 143 | var e:int = h4; 144 | 145 | // 80 steps to process each block 146 | // TODO: unroll for faster execution, or 4 loops of 147 | // 20 each to avoid the k and f function calls 148 | for ( var t:int = 0; t < 80; t++ ) { 149 | 150 | if ( t < 16 ) { 151 | // 6.1.a 152 | w[ t ] = blocks[ i + t ]; 153 | } else { 154 | // 6.1.b 155 | w[ t ] = IntUtil.rol( w[ t - 3 ] ^ w[ t - 8 ] ^ w[ t - 14 ] ^ w[ t - 16 ], 1 ); 156 | } 157 | 158 | // 6.1.d 159 | var temp:int = IntUtil.rol( a, 5 ) + f( t, b, c, d ) + e + int( w[ t ] ) + k( t ); 160 | 161 | e = d; 162 | d = c; 163 | c = IntUtil.rol( b, 30 ); 164 | b = a; 165 | a = temp; 166 | } 167 | 168 | // 6.1.e 169 | h0 += a; 170 | h1 += b; 171 | h2 += c; 172 | h3 += d; 173 | h4 += e; 174 | } 175 | 176 | var byteArray:ByteArray = new ByteArray(); 177 | byteArray.writeInt(h0); 178 | byteArray.writeInt(h1); 179 | byteArray.writeInt(h2); 180 | byteArray.writeInt(h3); 181 | byteArray.writeInt(h4); 182 | byteArray.position = 0; 183 | return byteArray; 184 | } 185 | 186 | /** 187 | * Performs the logical function based on t 188 | */ 189 | private static function f( t:int, b:int, c:int, d:int ):int { 190 | if ( t < 20 ) { 191 | return ( b & c ) | ( ~b & d ); 192 | } else if ( t < 40 ) { 193 | return b ^ c ^ d; 194 | } else if ( t < 60 ) { 195 | return ( b & c ) | ( b & d ) | ( c & d ); 196 | } 197 | return b ^ c ^ d; 198 | } 199 | 200 | /** 201 | * Determines the constant value based on t 202 | */ 203 | private static function k( t:int ):int { 204 | if ( t < 20 ) { 205 | return 0x5a827999; 206 | } else if ( t < 40 ) { 207 | return 0x6ed9eba1; 208 | } else if ( t < 60 ) { 209 | return 0x8f1bbcdc; 210 | } 211 | return 0xca62c1d6; 212 | } 213 | 214 | /** 215 | * Converts a ByteArray to a sequence of 16-word blocks 216 | * that we'll do the processing on. Appends padding 217 | * and length in the process. 218 | * 219 | * @param data The data to split into blocks 220 | * @return An array containing the blocks into which data was split 221 | */ 222 | private static function createBlocksFromByteArray( data:ByteArray ):Array 223 | { 224 | var oldPosition:int = data.position; 225 | data.position = 0; 226 | 227 | var blocks:Array = new Array(); 228 | var len:int = data.length * 8; 229 | var mask:int = 0xFF; // ignore hi byte of characters > 0xFF 230 | for( var i:int = 0; i < len; i += 8 ) 231 | { 232 | blocks[ i >> 5 ] |= ( data.readByte() & mask ) << ( 24 - i % 32 ); 233 | } 234 | 235 | // append padding and length 236 | blocks[ len >> 5 ] |= 0x80 << ( 24 - len % 32 ); 237 | blocks[ ( ( ( len + 64 ) >> 9 ) << 4 ) + 15 ] = len; 238 | 239 | data.position = oldPosition; 240 | 241 | return blocks; 242 | } 243 | 244 | /** 245 | * Converts a string to a sequence of 16-word blocks 246 | * that we'll do the processing on. Appends padding 247 | * and length in the process. 248 | * 249 | * @param s The string to split into blocks 250 | * @return An array containing the blocks that s was split into. 251 | */ 252 | private static function createBlocksFromString( s:String ):Array 253 | { 254 | var blocks:Array = new Array(); 255 | var len:int = s.length * 8; 256 | var mask:int = 0xFF; // ignore hi byte of characters > 0xFF 257 | for( var i:int = 0; i < len; i += 8 ) { 258 | blocks[ i >> 5 ] |= ( s.charCodeAt( i / 8 ) & mask ) << ( 24 - i % 32 ); 259 | } 260 | 261 | // append padding and length 262 | blocks[ len >> 5 ] |= 0x80 << ( 24 - len % 32 ); 263 | blocks[ ( ( ( len + 64 ) >> 9 ) << 4 ) + 15 ] = len; 264 | return blocks; 265 | } 266 | 267 | } 268 | } -------------------------------------------------------------------------------- /src/com/adobe/crypto/SHA224.as: -------------------------------------------------------------------------------- 1 | /* 2 | Adobe Systems Incorporated(r) Source Code License Agreement 3 | Copyright(c) 2005 Adobe Systems Incorporated. All rights reserved. 4 | 5 | Please read this Source Code License Agreement carefully before using 6 | the source code. 7 | 8 | Adobe Systems Incorporated grants to you a perpetual, worldwide, non-exclusive, 9 | no-charge, royalty-free, irrevocable copyright license, to reproduce, 10 | prepare derivative works of, publicly display, publicly perform, and 11 | distribute this source code and such derivative works in source or 12 | object code form without any attribution requirements. 13 | 14 | The name "Adobe Systems Incorporated" must not be used to endorse or promote products 15 | derived from the source code without prior written permission. 16 | 17 | You agree to indemnify, hold harmless and defend Adobe Systems Incorporated from and 18 | against any loss, damage, claims or lawsuits, including attorney's 19 | fees that arise or result from your use or distribution of the source 20 | code. 21 | 22 | THIS SOURCE CODE IS PROVIDED "AS IS" AND "WITH ALL FAULTS", WITHOUT 23 | ANY TECHNICAL SUPPORT OR ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, 24 | BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 25 | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. ALSO, THERE IS NO WARRANTY OF 26 | NON-INFRINGEMENT, TITLE OR QUIET ENJOYMENT. IN NO EVENT SHALL MACROMEDIA 27 | OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 28 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 29 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 30 | OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 31 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 32 | OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOURCE CODE, EVEN IF 33 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | 36 | package com.adobe.crypto 37 | { 38 | import com.adobe.utils.IntUtil; 39 | import flash.utils.ByteArray; 40 | import mx.utils.Base64Encoder; 41 | 42 | /** 43 | * The SHA-224 algorithm 44 | * 45 | * @see http://csrc.nist.gov/publications/fips/fips180-2/fips180-2withchangenotice.pdf 46 | */ 47 | public class SHA224 48 | { 49 | 50 | /** 51 | * Performs the SHA224 hash algorithm on a string. 52 | * 53 | * @param s The string to hash 54 | * @return A string containing the hash value of s 55 | * @langversion ActionScript 3.0 56 | * @playerversion 9.0 57 | * @tiptext 58 | */ 59 | public static function hash( s:String ):String { 60 | var blocks:Array = createBlocksFromString( s ); 61 | var byteArray:ByteArray = hashBlocks( blocks ); 62 | return IntUtil.toHex( byteArray.readInt(), true ) 63 | + IntUtil.toHex( byteArray.readInt(), true ) 64 | + IntUtil.toHex( byteArray.readInt(), true ) 65 | + IntUtil.toHex( byteArray.readInt(), true ) 66 | + IntUtil.toHex( byteArray.readInt(), true ) 67 | + IntUtil.toHex( byteArray.readInt(), true ) 68 | + IntUtil.toHex( byteArray.readInt(), true ); 69 | } 70 | 71 | /** 72 | * Performs the SHA224 hash algorithm on a ByteArray. 73 | * 74 | * @param data The ByteArray data to hash 75 | * @return A string containing the hash value of data 76 | * @langversion ActionScript 3.0 77 | * @playerversion 9.0 78 | */ 79 | public static function hashBytes( data:ByteArray ):String 80 | { 81 | var blocks:Array = createBlocksFromByteArray( data ); 82 | var byteArray:ByteArray = hashBlocks(blocks); 83 | return IntUtil.toHex( byteArray.readInt(), true ) 84 | + IntUtil.toHex( byteArray.readInt(), true ) 85 | + IntUtil.toHex( byteArray.readInt(), true ) 86 | + IntUtil.toHex( byteArray.readInt(), true ) 87 | + IntUtil.toHex( byteArray.readInt(), true ) 88 | + IntUtil.toHex( byteArray.readInt(), true ) 89 | + IntUtil.toHex( byteArray.readInt(), true ); 90 | } 91 | 92 | /** 93 | * Performs the SHA224 hash algorithm on a string, then does 94 | * Base64 encoding on the result. 95 | * 96 | * @param s The string to hash 97 | * @return The base64 encoded hash value of s 98 | * @langversion ActionScript 3.0 99 | * @playerversion 9.0 100 | * @tiptext 101 | */ 102 | public static function hashToBase64( s:String ):String 103 | { 104 | var blocks:Array = createBlocksFromString( s ); 105 | var byteArray:ByteArray = hashBlocks(blocks); 106 | 107 | // ByteArray.toString() returns the contents as a UTF-8 string, 108 | // which we can't use because certain byte sequences might trigger 109 | // a UTF-8 conversion. Instead, we convert the bytes to characters 110 | // one by one. 111 | var charsInByteArray:String = ""; 112 | byteArray.position = 0; 113 | for (var j:int = 0; j < byteArray.length; j++) 114 | { 115 | var byte:uint = byteArray.readUnsignedByte(); 116 | charsInByteArray += String.fromCharCode(byte); 117 | } 118 | 119 | var encoder:Base64Encoder = new Base64Encoder(); 120 | encoder.encode(charsInByteArray); 121 | return encoder.flush(); 122 | } 123 | 124 | private static function hashBlocks( blocks:Array ):ByteArray { 125 | var h0:int = 0xc1059ed8; 126 | var h1:int = 0x367cd507; 127 | var h2:int = 0x3070dd17; 128 | var h3:int = 0xf70e5939; 129 | var h4:int = 0xffc00b31; 130 | var h5:int = 0x68581511; 131 | var h6:int = 0x64f98fa7; 132 | var h7:int = 0xbefa4fa4; 133 | 134 | var k:Array = new Array(0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2); 135 | 136 | var len:int = blocks.length; 137 | var w:Array = new Array(); 138 | 139 | // loop over all of the blocks 140 | for ( var i:int = 0; i < len; i += 16 ) { 141 | 142 | var a:int = h0; 143 | var b:int = h1; 144 | var c:int = h2; 145 | var d:int = h3; 146 | var e:int = h4; 147 | var f:int = h5; 148 | var g:int = h6; 149 | var h:int = h7; 150 | 151 | for(var t:int = 0; t < 64; t++) { 152 | 153 | if ( t < 16 ) { 154 | w[t] = blocks[ i + t ]; 155 | if(isNaN(w[t])) { w[t] = 0; } 156 | } else { 157 | var ws0:int = IntUtil.ror(w[t-15], 7) ^ IntUtil.ror(w[t-15], 18) ^ (w[t-15] >>> 3); 158 | var ws1:int = IntUtil.ror(w[t-2], 17) ^ IntUtil.ror(w[t-2], 19) ^ (w[t-2] >>> 10); 159 | w[t] = w[t-16] + ws0 + w[t-7] + ws1; 160 | } 161 | 162 | var s0:int = IntUtil.ror(a, 2) ^ IntUtil.ror(a, 13) ^ IntUtil.ror(a, 22); 163 | var maj:int = (a & b) ^ (a & c) ^ (b & c); 164 | var t2:int = s0 + maj; 165 | var s1:int = IntUtil.ror(e, 6) ^ IntUtil.ror(e, 11) ^ IntUtil.ror(e, 25); 166 | var ch:int = (e & f) ^ ((~e) & g); 167 | var t1:int = h + s1 + ch + k[t] + w[t]; 168 | 169 | h = g; 170 | g = f; 171 | f = e; 172 | e = d + t1; 173 | d = c; 174 | c = b; 175 | b = a; 176 | a = t1 + t2; 177 | } 178 | 179 | //Add this chunk's hash to result so far: 180 | h0 += a; 181 | h1 += b; 182 | h2 += c; 183 | h3 += d; 184 | h4 += e; 185 | h5 += f; 186 | h6 += g; 187 | h7 += h; 188 | } 189 | 190 | var byteArray:ByteArray = new ByteArray(); 191 | byteArray.writeInt(h0); 192 | byteArray.writeInt(h1); 193 | byteArray.writeInt(h2); 194 | byteArray.writeInt(h3); 195 | byteArray.writeInt(h4); 196 | byteArray.writeInt(h5); 197 | byteArray.writeInt(h6); 198 | byteArray.position = 0; 199 | return byteArray; 200 | } 201 | 202 | /** 203 | * Converts a ByteArray to a sequence of 16-word blocks 204 | * that we'll do the processing on. Appends padding 205 | * and length in the process. 206 | * 207 | * @param data The data to split into blocks 208 | * @return An array containing the blocks into which data was split 209 | */ 210 | private static function createBlocksFromByteArray( data:ByteArray ):Array 211 | { 212 | var oldPosition:int = data.position; 213 | data.position = 0; 214 | 215 | var blocks:Array = new Array(); 216 | var len:int = data.length * 8; 217 | var mask:int = 0xFF; // ignore hi byte of characters > 0xFF 218 | for( var i:int = 0; i < len; i += 8 ) 219 | { 220 | blocks[ i >> 5 ] |= ( data.readByte() & mask ) << ( 24 - i % 32 ); 221 | } 222 | 223 | // append padding and length 224 | blocks[ len >> 5 ] |= 0x80 << ( 24 - len % 32 ); 225 | blocks[ ( ( ( len + 64 ) >> 9 ) << 4 ) + 15 ] = len; 226 | 227 | data.position = oldPosition; 228 | 229 | return blocks; 230 | } 231 | 232 | /** 233 | * Converts a string to a sequence of 16-word blocks 234 | * that we'll do the processing on. Appends padding 235 | * and length in the process. 236 | * 237 | * @param s The string to split into blocks 238 | * @return An array containing the blocks that s was split into. 239 | */ 240 | private static function createBlocksFromString( s:String ):Array 241 | { 242 | var blocks:Array = new Array(); 243 | var len:int = s.length * 8; 244 | var mask:int = 0xFF; // ignore hi byte of characters > 0xFF 245 | for( var i:int = 0; i < len; i += 8 ) { 246 | blocks[ i >> 5 ] |= ( s.charCodeAt( i / 8 ) & mask ) << ( 24 - i % 32 ); 247 | } 248 | 249 | // append padding and length 250 | blocks[ len >> 5 ] |= 0x80 << ( 24 - len % 32 ); 251 | blocks[ ( ( ( len + 64 ) >> 9 ) << 4 ) + 15 ] = len; 252 | return blocks; 253 | } 254 | } 255 | } -------------------------------------------------------------------------------- /src/com/adobe/crypto/SHA256.as: -------------------------------------------------------------------------------- 1 | /* 2 | Adobe Systems Incorporated(r) Source Code License Agreement 3 | Copyright(c) 2005 Adobe Systems Incorporated. All rights reserved. 4 | 5 | Please read this Source Code License Agreement carefully before using 6 | the source code. 7 | 8 | Adobe Systems Incorporated grants to you a perpetual, worldwide, non-exclusive, 9 | no-charge, royalty-free, irrevocable copyright license, to reproduce, 10 | prepare derivative works of, publicly display, publicly perform, and 11 | distribute this source code and such derivative works in source or 12 | object code form without any attribution requirements. 13 | 14 | The name "Adobe Systems Incorporated" must not be used to endorse or promote products 15 | derived from the source code without prior written permission. 16 | 17 | You agree to indemnify, hold harmless and defend Adobe Systems Incorporated from and 18 | against any loss, damage, claims or lawsuits, including attorney's 19 | fees that arise or result from your use or distribution of the source 20 | code. 21 | 22 | THIS SOURCE CODE IS PROVIDED "AS IS" AND "WITH ALL FAULTS", WITHOUT 23 | ANY TECHNICAL SUPPORT OR ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, 24 | BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 25 | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. ALSO, THERE IS NO WARRANTY OF 26 | NON-INFRINGEMENT, TITLE OR QUIET ENJOYMENT. IN NO EVENT SHALL MACROMEDIA 27 | OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 28 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 29 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 30 | OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 31 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 32 | OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOURCE CODE, EVEN IF 33 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | 36 | package com.adobe.crypto 37 | { 38 | import com.adobe.utils.IntUtil; 39 | import flash.utils.ByteArray; 40 | import mx.utils.Base64Encoder; 41 | 42 | /** 43 | * The SHA-256 algorithm 44 | * 45 | * @see http://csrc.nist.gov/publications/fips/fips180-2/fips180-2withchangenotice.pdf 46 | */ 47 | public class SHA256 48 | { 49 | 50 | /** 51 | * Performs the SHA256 hash algorithm on a string. 52 | * 53 | * @param s The string to hash 54 | * @return A string containing the hash value of s 55 | * @langversion ActionScript 3.0 56 | * @playerversion 9.0 57 | * @tiptext 58 | */ 59 | public static function hash( s:String ):String { 60 | var blocks:Array = createBlocksFromString( s ); 61 | var byteArray:ByteArray = hashBlocks( blocks ); 62 | 63 | return IntUtil.toHex( byteArray.readInt(), true ) 64 | + IntUtil.toHex( byteArray.readInt(), true ) 65 | + IntUtil.toHex( byteArray.readInt(), true ) 66 | + IntUtil.toHex( byteArray.readInt(), true ) 67 | + IntUtil.toHex( byteArray.readInt(), true ) 68 | + IntUtil.toHex( byteArray.readInt(), true ) 69 | + IntUtil.toHex( byteArray.readInt(), true ) 70 | + IntUtil.toHex( byteArray.readInt(), true ); 71 | } 72 | 73 | /** 74 | * Performs the SHA256 hash algorithm on a ByteArray. 75 | * 76 | * @param data The ByteArray data to hash 77 | * @return A string containing the hash value of data 78 | * @langversion ActionScript 3.0 79 | * @playerversion 9.0 80 | */ 81 | public static function hashBytes( data:ByteArray ):String 82 | { 83 | var blocks:Array = createBlocksFromByteArray( data ); 84 | var byteArray:ByteArray = hashBlocks(blocks); 85 | 86 | return IntUtil.toHex( byteArray.readInt(), true ) 87 | + IntUtil.toHex( byteArray.readInt(), true ) 88 | + IntUtil.toHex( byteArray.readInt(), true ) 89 | + IntUtil.toHex( byteArray.readInt(), true ) 90 | + IntUtil.toHex( byteArray.readInt(), true ) 91 | + IntUtil.toHex( byteArray.readInt(), true ) 92 | + IntUtil.toHex( byteArray.readInt(), true ) 93 | + IntUtil.toHex( byteArray.readInt(), true ); 94 | } 95 | 96 | /** 97 | * Performs the SHA256 hash algorithm on a string, then does 98 | * Base64 encoding on the result. 99 | * 100 | * @param s The string to hash 101 | * @return The base64 encoded hash value of s 102 | * @langversion ActionScript 3.0 103 | * @playerversion 9.0 104 | * @tiptext 105 | */ 106 | public static function hashToBase64( s:String ):String 107 | { 108 | var blocks:Array = createBlocksFromString( s ); 109 | var byteArray:ByteArray = hashBlocks(blocks); 110 | 111 | // ByteArray.toString() returns the contents as a UTF-8 string, 112 | // which we can't use because certain byte sequences might trigger 113 | // a UTF-8 conversion. Instead, we convert the bytes to characters 114 | // one by one. 115 | var charsInByteArray:String = ""; 116 | byteArray.position = 0; 117 | for (var j:int = 0; j < byteArray.length; j++) 118 | { 119 | var byte:uint = byteArray.readUnsignedByte(); 120 | charsInByteArray += String.fromCharCode(byte); 121 | } 122 | 123 | var encoder:Base64Encoder = new Base64Encoder(); 124 | encoder.encode(charsInByteArray); 125 | return encoder.flush(); 126 | } 127 | 128 | private static function hashBlocks( blocks:Array ):ByteArray { 129 | var h0:int = 0x6a09e667; 130 | var h1:int = 0xbb67ae85; 131 | var h2:int = 0x3c6ef372; 132 | var h3:int = 0xa54ff53a; 133 | var h4:int = 0x510e527f; 134 | var h5:int = 0x9b05688c; 135 | var h6:int = 0x1f83d9ab; 136 | var h7:int = 0x5be0cd19; 137 | 138 | var k:Array = new Array(0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2); 139 | 140 | var len:int = blocks.length; 141 | var w:Array = new Array( 64 ); 142 | 143 | // loop over all of the blocks 144 | for ( var i:int = 0; i < len; i += 16 ) { 145 | 146 | var a:int = h0; 147 | var b:int = h1; 148 | var c:int = h2; 149 | var d:int = h3; 150 | var e:int = h4; 151 | var f:int = h5; 152 | var g:int = h6; 153 | var h:int = h7; 154 | 155 | for(var t:int = 0; t < 64; t++) { 156 | 157 | if ( t < 16 ) { 158 | w[t] = blocks[ i + t ]; 159 | if(isNaN(w[t])) { w[t] = 0; } 160 | } else { 161 | var ws0:int = IntUtil.ror(w[t-15], 7) ^ IntUtil.ror(w[t-15], 18) ^ (w[t-15] >>> 3); 162 | var ws1:int = IntUtil.ror(w[t-2], 17) ^ IntUtil.ror(w[t-2], 19) ^ (w[t-2] >>> 10); 163 | w[t] = w[t-16] + ws0 + w[t-7] + ws1; 164 | } 165 | 166 | var s0:int = IntUtil.ror(a, 2) ^ IntUtil.ror(a, 13) ^ IntUtil.ror(a, 22); 167 | var maj:int = (a & b) ^ (a & c) ^ (b & c); 168 | var t2:int = s0 + maj; 169 | var s1:int = IntUtil.ror(e, 6) ^ IntUtil.ror(e, 11) ^ IntUtil.ror(e, 25); 170 | var ch:int = (e & f) ^ ((~e) & g); 171 | var t1:int = h + s1 + ch + k[t] + w[t]; 172 | 173 | h = g; 174 | g = f; 175 | f = e; 176 | e = d + t1; 177 | d = c; 178 | c = b; 179 | b = a; 180 | a = t1 + t2; 181 | } 182 | 183 | //Add this chunk's hash to result so far: 184 | h0 += a; 185 | h1 += b; 186 | h2 += c; 187 | h3 += d; 188 | h4 += e; 189 | h5 += f; 190 | h6 += g; 191 | h7 += h; 192 | } 193 | 194 | var byteArray:ByteArray = new ByteArray(); 195 | byteArray.writeInt(h0); 196 | byteArray.writeInt(h1); 197 | byteArray.writeInt(h2); 198 | byteArray.writeInt(h3); 199 | byteArray.writeInt(h4); 200 | byteArray.writeInt(h5); 201 | byteArray.writeInt(h6); 202 | byteArray.writeInt(h7); 203 | byteArray.position = 0; 204 | return byteArray; 205 | } 206 | 207 | /** 208 | * Converts a ByteArray to a sequence of 16-word blocks 209 | * that we'll do the processing on. Appends padding 210 | * and length in the process. 211 | * 212 | * @param data The data to split into blocks 213 | * @return An array containing the blocks into which data was split 214 | */ 215 | private static function createBlocksFromByteArray( data:ByteArray ):Array 216 | { 217 | var oldPosition:int = data.position; 218 | data.position = 0; 219 | 220 | var blocks:Array = new Array(); 221 | var len:int = data.length * 8; 222 | var mask:int = 0xFF; // ignore hi byte of characters > 0xFF 223 | for( var i:int = 0; i < len; i += 8 ) 224 | { 225 | blocks[ i >> 5 ] |= ( data.readByte() & mask ) << ( 24 - i % 32 ); 226 | } 227 | 228 | // append padding and length 229 | blocks[ len >> 5 ] |= 0x80 << ( 24 - len % 32 ); 230 | blocks[ ( ( ( len + 64 ) >> 9 ) << 4 ) + 15 ] = len; 231 | 232 | data.position = oldPosition; 233 | 234 | return blocks; 235 | } 236 | 237 | /** 238 | * Converts a string to a sequence of 16-word blocks 239 | * that we'll do the processing on. Appends padding 240 | * and length in the process. 241 | * 242 | * @param s The string to split into blocks 243 | * @return An array containing the blocks that s was split into. 244 | */ 245 | private static function createBlocksFromString( s:String ):Array 246 | { 247 | var blocks:Array = new Array(); 248 | var len:int = s.length * 8; 249 | var mask:int = 0xFF; // ignore hi byte of characters > 0xFF 250 | for( var i:int = 0; i < len; i += 8 ) { 251 | blocks[ i >> 5 ] |= ( s.charCodeAt( i / 8 ) & mask ) << ( 24 - i % 32 ); 252 | } 253 | 254 | // append padding and length 255 | blocks[ len >> 5 ] |= 0x80 << ( 24 - len % 32 ); 256 | blocks[ ( ( ( len + 64 ) >> 9 ) << 4 ) + 15 ] = len; 257 | return blocks; 258 | } 259 | } 260 | } -------------------------------------------------------------------------------- /README.txt: -------------------------------------------------------------------------------- 1 | PHP WebCam Stream 2 | 3 | JPEGCam v1.0.4 4 | 5 | Webcam library for capturing JPEG images and submitting to a server 6 | Copyright (c) 2008 - 2009 Joseph Huckaby 7 | Licensed under the GNU Lesser Public License 8 | http://www.gnu.org/licenses/lgpl.html 9 | 10 | OVERVIEW 11 | 12 | JPEGCam is a simple, JavaScript and Flash library that allows you to enable 13 | your users to submit Webcam snapshots to your server in JPEG format. The 14 | Flash movie is variable-sized, and has no visible user interface controls. 15 | All commands sent to the movie are done so from JavaScript, so you can 16 | implement your own look & feel on your site, create your own buttons, and 17 | tell the Flash movie what to do from your own code. 18 | 19 | REQUIREMENTS 20 | 21 | JavaScript-enabled browser 22 | Flash Player 9 23 | 24 | EMBEDDING IN YOUR PAGE 25 | 26 | (For a working example, see "test.html" in the htdocs folder.) 27 | 28 | First, copy the following files to your web server: 29 | 30 | webcam.js 31 | webcam.swf 32 | shutter.mp3 33 | 34 | Next, edit your HTML and load the JavaScript library: 35 | 36 | 37 | 38 | Configure a few settings (see API CALLS for complete list): 39 | 40 | 45 | 46 | Load the movie into the page. If you want to load the movie immediately, 47 | simply use document.write() as shown below. If you are designing a DHTML 48 | application, you can call webcam.get_html(...) at any time to dynamically 49 | populate a DIV or other element after the page is finished loading. 50 | 51 | 54 | 55 | Add some controls for sending commands to the movie (see API CALLS): 56 | 57 |

58 | 59 |     60 | 61 |
62 | 63 | Finally, add some code for handling the server response: 64 | 65 | 71 | 72 | That's it! See the following sections for a complete list of all the 73 | available API calls, and how to write the server-side code. 74 | 75 | API CALLS 76 | 77 | Here are all the available API calls for the JPEGCam JavaScript library. 78 | Everything is under a top-level global 'webcam' namespace. 79 | 80 | webcam.set_hook( HOOK_NAME, USER_FUNCTION ); 81 | 82 | This allows you to set a user callback function that will be fired for 83 | various events in the JPEGCam system. Here are all the events you 84 | can hook: 85 | 86 | onLoad 87 | Fires when the Flash movie is loaded on the page. This is useful 88 | for knowing when the movie is ready to receive scripting calls. 89 | 90 | onComplete 91 | Fires when the JPEG upload is complete. 92 | Your function will be passed the raw output from the API script 93 | that received the file upload, as the first argument. 94 | 95 | onError 96 | Fires when an error occurs. If this hook is not defined, the library 97 | will display a simple JavaScript alert dialog. Your function will be 98 | passed the error text as the first argument. 99 | 100 | webcam.set_api_url( URL ); 101 | 102 | This allows you to set the URL to your server-side script that will 103 | receive the JPEG uploads from the Flash movie. Beware of cross-domain 104 | restrictions in Flash. 105 | 106 | webcam.set_swf_url( URL ); 107 | 108 | This allows you to set the URL to the location of the "webcam.swf" Flash 109 | movie on your server. It is recommended to keep this file in the same 110 | directory as your HTML page, but if that is not possible, set the path 111 | using this function. Beware of cross-domain restrictions in Flash. 112 | The default is the current directory that your HTML page lives in. 113 | 114 | webcam.set_quality( QUALITY ); 115 | 116 | This allows you to adjust the JPEG compression quality of the images 117 | taken from the camera. The range is 1 - 100, with 1 being the lowest 118 | quality (but smallest size files), to 100 being the highest quality 119 | (but largest files). This does NOT control the resolution of the images, 120 | only the JPEG compression. The default is 90. 121 | 122 | webcam.set_shutter_sound( ENABLED, [ URL ] ); 123 | 124 | This allows you to enable or disable the "shutter" sound effect that 125 | the Flash movie makes when a snapshot is taken. Pass in a boolean 126 | true or false to the function. It defaults to true. If set to false 127 | the sound effect will not even be loaded. 128 | 129 | You can optionally pass a second argument to this function, which 130 | should be a URL (relative to page or fully qualified) to an MP3 131 | sound effect for the shutter sound. This defaults to 'shutter.mp3' 132 | in the current directory relative to the HTML page. 133 | 134 | These values cannot be changed after get_html() is called (see below). 135 | 136 | webcam.get_html( WIDTH, HEIGHT, [SERVER_WIDTH, SERVER_HEIGHT] ); 137 | 138 | This returns the necessary HTML code to embed the Flash movie into your 139 | page. Pass in the desired pixel width & height, which not only controls 140 | the visual size of the movie, but also the JPEG image width & height. 141 | Standard sizes are 320x240 and 640x480. 142 | 143 | You can optionally pass a desired server image width and height. If 144 | these differ from the video width and height, the captured images will 145 | be resized to match just prior to upload. 146 | 147 | webcam.snap(); 148 | 149 | This instructs the Flash movie to take a snapshot and upload the JPEG 150 | to the server. Make sure you set the URL to your API script using 151 | webcam.set_api_url(), and have a callback function ready to receive 152 | the results from the server, using webcam.set_hook(). 153 | 154 | webcam.configure( PANEL ); 155 | 156 | This launches one of Flash's configuration panels, used to setup camera 157 | devices, privacy settings, and more. Pass in one of the following strings 158 | which sets the default panel "tab" in the settings dialog: 159 | "camera", "privacy", "default", "localStorage", "microphone", or 160 | "settingsManager". Example: 161 | 162 | webcam.configure( 'camera' ); 163 | 164 | webcam.freeze(); 165 | 166 | Optional, new in v1.0.4. This is not required if you use webcam.snap(), 167 | described above. 168 | 169 | This captures an image from the webcam but does NOT upload it. 170 | Instead, the image is displayed "frozen" in the Flash movie, and the user 171 | may take further action. For example, you may provide separate "Upload" 172 | and "Reset" buttons to upload the frozen image and/or reset the camera. 173 | 174 | webcam.upload(); 175 | 176 | Optional, new in v1.0.4. This is not required if you use webcam.snap(), 177 | described above. 178 | 179 | This uploads the captured image to the server, previously frozen with 180 | webcam.freeze(). This is provided as its own function so you can 181 | have separate "Capture" and "Upload" buttons for the user. 182 | 183 | webcam.reset(); 184 | 185 | Optional, new in v1.0.4. This resets the frozen image, previously captured 186 | with webcam.freeze(), and restores the live webcam feed for further 187 | capturing. 188 | 189 | SERVER-SIDE CODE 190 | 191 | The Flash movie makes a HTTP POST to your server-side script, using the 192 | Content-Type 'image/jpeg'. This is a NON-STANDARD method which is unlike 193 | submitting a form from a web page. If you are using PHP, the JPEG data 194 | will NOT be in the normal $_POST variable. Instead, you should read it 195 | from the special PHP file wrapper 'php://input'. For example: 196 | 197 | $jpeg_data = file_get_contents('php://input'); 198 | 199 | You can write this raw, binary JPEG data to a file handle using the PHP 200 | function file_put_contents(): 201 | 202 | $filename = "my_file.jpg"; 203 | $result = file_put_contents( $filename, $jpeg_data ); 204 | 205 | Any output from your script is passed back through the Flash movie to the 206 | JavaScript code, which in turn passes it to your onComplete callback function. 207 | 208 | For example, if you want your script to pass back a URL to the JPEG image, 209 | save the file where you want it, and construct a URL to the file. Then simply 210 | print the URL to the output like this: 211 | 212 | (This assumes you are saving the files to the current working directory) 213 | 214 | $url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['REQUEST_URI']) 215 | . '/' . $filename; 216 | print "$url\n"; 217 | 218 | (See "test.php" for a working example.) 219 | 220 | FAQ 221 | 222 | Q. I cannot see the image from my camera! What am I doing wrong? 223 | 224 | A. You probably have to setup the camera device in the Flash Camera settings 225 | dialog first. Often Flash doesn't auto-detect the right device. 226 | 227 | webcam.configure( 'camera' ); 228 | 229 | It is always a good idea to provide a "Configure..." button on your 230 | page which calls this function, so users can easily get to it. 231 | 232 | 233 | Q. What is this ugly permission dialog? Can't I just make it remember me? 234 | 235 | A. Yes, you certainly can! In the Flash setup dialogs, click on the 2nd icon 236 | from the left (i.e. Privacy Settings), and you can click "Allow", then 237 | check the "Remember" checkbox. 238 | 239 | You can send your users directly to the Privacy config panel by calling: 240 | webcam.configure( 'privacy' ); 241 | 242 | A cool trick is to detect "new" users (via a cookie) and register an onLoad 243 | handler to send them directly to the Privacy settings. 244 | 245 | webcam.set_hook( 'onLoad', 'my_load_handler' ); 246 | function my_load_handler() { 247 | if (is_new_user()) 248 | webcam.configure( 'privacy' ); 249 | } 250 | 251 | Of course, you have to write the is_new_user() function yourself. 252 | I no wanna be settin' no cookies on your domain. 253 | -------------------------------------------------------------------------------- /src/com/adobe/crypto/MD5.as: -------------------------------------------------------------------------------- 1 | /* 2 | Adobe Systems Incorporated(r) Source Code License Agreement 3 | Copyright(c) 2005 Adobe Systems Incorporated. All rights reserved. 4 | 5 | Please read this Source Code License Agreement carefully before using 6 | the source code. 7 | 8 | Adobe Systems Incorporated grants to you a perpetual, worldwide, non-exclusive, 9 | no-charge, royalty-free, irrevocable copyright license, to reproduce, 10 | prepare derivative works of, publicly display, publicly perform, and 11 | distribute this source code and such derivative works in source or 12 | object code form without any attribution requirements. 13 | 14 | The name "Adobe Systems Incorporated" must not be used to endorse or promote products 15 | derived from the source code without prior written permission. 16 | 17 | You agree to indemnify, hold harmless and defend Adobe Systems Incorporated from and 18 | against any loss, damage, claims or lawsuits, including attorney's 19 | fees that arise or result from your use or distribution of the source 20 | code. 21 | 22 | THIS SOURCE CODE IS PROVIDED "AS IS" AND "WITH ALL FAULTS", WITHOUT 23 | ANY TECHNICAL SUPPORT OR ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, 24 | BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 25 | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. ALSO, THERE IS NO WARRANTY OF 26 | NON-INFRINGEMENT, TITLE OR QUIET ENJOYMENT. IN NO EVENT SHALL MACROMEDIA 27 | OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 28 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 29 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 30 | OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 31 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 32 | OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOURCE CODE, EVEN IF 33 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | 36 | package com.adobe.crypto { 37 | 38 | import com.adobe.utils.IntUtil; 39 | 40 | /** 41 | * The MD5 Message-Digest Algorithm 42 | * 43 | * Implementation based on algorithm description at 44 | * http://www.faqs.org/rfcs/rfc1321.html 45 | */ 46 | public class MD5 { 47 | 48 | /** 49 | * Performs the MD5 hash algorithm on a string. 50 | * 51 | * @param s The string to hash 52 | * @return A string containing the hash value of s 53 | * @langversion ActionScript 3.0 54 | * @playerversion Flash 9.0 55 | * @tiptext 56 | */ 57 | public static function hash( s:String ):String { 58 | // initialize the md buffers 59 | var a:int = 1732584193; 60 | var b:int = -271733879; 61 | var c:int = -1732584194; 62 | var d:int = 271733878; 63 | 64 | // variables to store previous values 65 | var aa:int; 66 | var bb:int; 67 | var cc:int; 68 | var dd:int; 69 | 70 | // create the blocks from the string and 71 | // save the length as a local var to reduce 72 | // lookup in the loop below 73 | var x:Array = createBlocks( s ); 74 | var len:int = x.length; 75 | 76 | // loop over all of the blocks 77 | for ( var i:int = 0; i < len; i += 16) { 78 | // save previous values 79 | aa = a; 80 | bb = b; 81 | cc = c; 82 | dd = d; 83 | 84 | // Round 1 85 | a = ff( a, b, c, d, x[i+ 0], 7, -680876936 ); // 1 86 | d = ff( d, a, b, c, x[i+ 1], 12, -389564586 ); // 2 87 | c = ff( c, d, a, b, x[i+ 2], 17, 606105819 ); // 3 88 | b = ff( b, c, d, a, x[i+ 3], 22, -1044525330 ); // 4 89 | a = ff( a, b, c, d, x[i+ 4], 7, -176418897 ); // 5 90 | d = ff( d, a, b, c, x[i+ 5], 12, 1200080426 ); // 6 91 | c = ff( c, d, a, b, x[i+ 6], 17, -1473231341 ); // 7 92 | b = ff( b, c, d, a, x[i+ 7], 22, -45705983 ); // 8 93 | a = ff( a, b, c, d, x[i+ 8], 7, 1770035416 ); // 9 94 | d = ff( d, a, b, c, x[i+ 9], 12, -1958414417 ); // 10 95 | c = ff( c, d, a, b, x[i+10], 17, -42063 ); // 11 96 | b = ff( b, c, d, a, x[i+11], 22, -1990404162 ); // 12 97 | a = ff( a, b, c, d, x[i+12], 7, 1804603682 ); // 13 98 | d = ff( d, a, b, c, x[i+13], 12, -40341101 ); // 14 99 | c = ff( c, d, a, b, x[i+14], 17, -1502002290 ); // 15 100 | b = ff( b, c, d, a, x[i+15], 22, 1236535329 ); // 16 101 | 102 | // Round 2 103 | a = gg( a, b, c, d, x[i+ 1], 5, -165796510 ); // 17 104 | d = gg( d, a, b, c, x[i+ 6], 9, -1069501632 ); // 18 105 | c = gg( c, d, a, b, x[i+11], 14, 643717713 ); // 19 106 | b = gg( b, c, d, a, x[i+ 0], 20, -373897302 ); // 20 107 | a = gg( a, b, c, d, x[i+ 5], 5, -701558691 ); // 21 108 | d = gg( d, a, b, c, x[i+10], 9, 38016083 ); // 22 109 | c = gg( c, d, a, b, x[i+15], 14, -660478335 ); // 23 110 | b = gg( b, c, d, a, x[i+ 4], 20, -405537848 ); // 24 111 | a = gg( a, b, c, d, x[i+ 9], 5, 568446438 ); // 25 112 | d = gg( d, a, b, c, x[i+14], 9, -1019803690 ); // 26 113 | c = gg( c, d, a, b, x[i+ 3], 14, -187363961 ); // 27 114 | b = gg( b, c, d, a, x[i+ 8], 20, 1163531501 ); // 28 115 | a = gg( a, b, c, d, x[i+13], 5, -1444681467 ); // 29 116 | d = gg( d, a, b, c, x[i+ 2], 9, -51403784 ); // 30 117 | c = gg( c, d, a, b, x[i+ 7], 14, 1735328473 ); // 31 118 | b = gg( b, c, d, a, x[i+12], 20, -1926607734 ); // 32 119 | 120 | // Round 3 121 | a = hh( a, b, c, d, x[i+ 5], 4, -378558 ); // 33 122 | d = hh( d, a, b, c, x[i+ 8], 11, -2022574463 ); // 34 123 | c = hh( c, d, a, b, x[i+11], 16, 1839030562 ); // 35 124 | b = hh( b, c, d, a, x[i+14], 23, -35309556 ); // 36 125 | a = hh( a, b, c, d, x[i+ 1], 4, -1530992060 ); // 37 126 | d = hh( d, a, b, c, x[i+ 4], 11, 1272893353 ); // 38 127 | c = hh( c, d, a, b, x[i+ 7], 16, -155497632 ); // 39 128 | b = hh( b, c, d, a, x[i+10], 23, -1094730640 ); // 40 129 | a = hh( a, b, c, d, x[i+13], 4, 681279174 ); // 41 130 | d = hh( d, a, b, c, x[i+ 0], 11, -358537222 ); // 42 131 | c = hh( c, d, a, b, x[i+ 3], 16, -722521979 ); // 43 132 | b = hh( b, c, d, a, x[i+ 6], 23, 76029189 ); // 44 133 | a = hh( a, b, c, d, x[i+ 9], 4, -640364487 ); // 45 134 | d = hh( d, a, b, c, x[i+12], 11, -421815835 ); // 46 135 | c = hh( c, d, a, b, x[i+15], 16, 530742520 ); // 47 136 | b = hh( b, c, d, a, x[i+ 2], 23, -995338651 ); // 48 137 | 138 | // Round 4 139 | a = ii( a, b, c, d, x[i+ 0], 6, -198630844 ); // 49 140 | d = ii( d, a, b, c, x[i+ 7], 10, 1126891415 ); // 50 141 | c = ii( c, d, a, b, x[i+14], 15, -1416354905 ); // 51 142 | b = ii( b, c, d, a, x[i+ 5], 21, -57434055 ); // 52 143 | a = ii( a, b, c, d, x[i+12], 6, 1700485571 ); // 53 144 | d = ii( d, a, b, c, x[i+ 3], 10, -1894986606 ); // 54 145 | c = ii( c, d, a, b, x[i+10], 15, -1051523 ); // 55 146 | b = ii( b, c, d, a, x[i+ 1], 21, -2054922799 ); // 56 147 | a = ii( a, b, c, d, x[i+ 8], 6, 1873313359 ); // 57 148 | d = ii( d, a, b, c, x[i+15], 10, -30611744 ); // 58 149 | c = ii( c, d, a, b, x[i+ 6], 15, -1560198380 ); // 59 150 | b = ii( b, c, d, a, x[i+13], 21, 1309151649 ); // 60 151 | a = ii( a, b, c, d, x[i+ 4], 6, -145523070 ); // 61 152 | d = ii( d, a, b, c, x[i+11], 10, -1120210379 ); // 62 153 | c = ii( c, d, a, b, x[i+ 2], 15, 718787259 ); // 63 154 | b = ii( b, c, d, a, x[i+ 9], 21, -343485551 ); // 64 155 | 156 | a += aa; 157 | b += bb; 158 | c += cc; 159 | d += dd; 160 | } 161 | 162 | // Finish up by concatening the buffers with their hex output 163 | return IntUtil.toHex( a ) + IntUtil.toHex( b ) + IntUtil.toHex( c ) + IntUtil.toHex( d ); 164 | } 165 | 166 | /** 167 | * Auxiliary function f as defined in RFC 168 | */ 169 | private static function f( x:int, y:int, z:int ):int { 170 | return ( x & y ) | ( (~x) & z ); 171 | } 172 | 173 | /** 174 | * Auxiliary function g as defined in RFC 175 | */ 176 | private static function g( x:int, y:int, z:int ):int { 177 | return ( x & z ) | ( y & (~z) ); 178 | } 179 | 180 | /** 181 | * Auxiliary function h as defined in RFC 182 | */ 183 | private static function h( x:int, y:int, z:int ):int { 184 | return x ^ y ^ z; 185 | } 186 | 187 | /** 188 | * Auxiliary function i as defined in RFC 189 | */ 190 | private static function i( x:int, y:int, z:int ):int { 191 | return y ^ ( x | (~z) ); 192 | } 193 | 194 | /** 195 | * A generic transformation function. The logic of ff, gg, hh, and 196 | * ii are all the same, minus the function used, so pull that logic 197 | * out and simplify the method bodies for the transoformation functions. 198 | */ 199 | private static function transform( func:Function, a:int, b:int, c:int, d:int, x:int, s:int, t:int):int { 200 | var tmp:int = a + int( func( b, c, d ) ) + x + t; 201 | return IntUtil.rol( tmp, s ) + b; 202 | } 203 | 204 | /** 205 | * ff transformation function 206 | */ 207 | private static function ff ( a:int, b:int, c:int, d:int, x:int, s:int, t:int ):int { 208 | return transform( f, a, b, c, d, x, s, t ); 209 | } 210 | 211 | /** 212 | * gg transformation function 213 | */ 214 | private static function gg ( a:int, b:int, c:int, d:int, x:int, s:int, t:int ):int { 215 | return transform( g, a, b, c, d, x, s, t ); 216 | } 217 | 218 | /** 219 | * hh transformation function 220 | */ 221 | private static function hh ( a:int, b:int, c:int, d:int, x:int, s:int, t:int ):int { 222 | return transform( h, a, b, c, d, x, s, t ); 223 | } 224 | 225 | /** 226 | * ii transformation function 227 | */ 228 | private static function ii ( a:int, b:int, c:int, d:int, x:int, s:int, t:int ):int { 229 | return transform( i, a, b, c, d, x, s, t ); 230 | } 231 | 232 | /** 233 | * Converts a string to a sequence of 16-word blocks 234 | * that we'll do the processing on. Appends padding 235 | * and length in the process. 236 | * 237 | * @param s The string to split into blocks 238 | * @return An array containing the blocks that s was 239 | * split into. 240 | */ 241 | private static function createBlocks( s:String ):Array { 242 | var blocks:Array = new Array(); 243 | var len:int = s.length * 8; 244 | var mask:int = 0xFF; // ignore hi byte of characters > 0xFF 245 | for( var i:int = 0; i < len; i += 8 ) { 246 | blocks[ i >> 5 ] |= ( s.charCodeAt( i / 8 ) & mask ) << ( i % 32 ); 247 | } 248 | 249 | // append padding and length 250 | blocks[ len >> 5 ] |= 0x80 << ( len % 32 ); 251 | blocks[ ( ( ( len + 64 ) >>> 9 ) << 4 ) + 14 ] = len; 252 | return blocks; 253 | } 254 | 255 | } 256 | } -------------------------------------------------------------------------------- /src/com/adobe/serialization/json/JSONEncoder.as: -------------------------------------------------------------------------------- 1 | /* 2 | Adobe Systems Incorporated(r) Source Code License Agreement 3 | Copyright(c) 2005 Adobe Systems Incorporated. All rights reserved. 4 | 5 | Please read this Source Code License Agreement carefully before using 6 | the source code. 7 | 8 | Adobe Systems Incorporated grants to you a perpetual, worldwide, non-exclusive, 9 | no-charge, royalty-free, irrevocable copyright license, to reproduce, 10 | prepare derivative works of, publicly display, publicly perform, and 11 | distribute this source code and such derivative works in source or 12 | object code form without any attribution requirements. 13 | 14 | The name "Adobe Systems Incorporated" must not be used to endorse or promote products 15 | derived from the source code without prior written permission. 16 | 17 | You agree to indemnify, hold harmless and defend Adobe Systems Incorporated from and 18 | against any loss, damage, claims or lawsuits, including attorney's 19 | fees that arise or result from your use or distribution of the source 20 | code. 21 | 22 | THIS SOURCE CODE IS PROVIDED "AS IS" AND "WITH ALL FAULTS", WITHOUT 23 | ANY TECHNICAL SUPPORT OR ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, 24 | BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 25 | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. ALSO, THERE IS NO WARRANTY OF 26 | NON-INFRINGEMENT, TITLE OR QUIET ENJOYMENT. IN NO EVENT SHALL MACROMEDIA 27 | OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 28 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 29 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 30 | OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 31 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 32 | OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOURCE CODE, EVEN IF 33 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | 36 | package com.adobe.serialization.json 37 | { 38 | 39 | import flash.utils.describeType; 40 | 41 | public class JSONEncoder { 42 | 43 | /** The string that is going to represent the object we're encoding */ 44 | private var jsonString:String; 45 | 46 | /** 47 | * Creates a new JSONEncoder. 48 | * 49 | * @param o The object to encode as a JSON string 50 | * @langversion ActionScript 3.0 51 | * @playerversion Flash 9.0 52 | * @tiptext 53 | */ 54 | public function JSONEncoder( value:* ) { 55 | jsonString = convertToString( value ); 56 | 57 | } 58 | 59 | /** 60 | * Gets the JSON string from the encoder. 61 | * 62 | * @return The JSON string representation of the object 63 | * that was passed to the constructor 64 | * @langversion ActionScript 3.0 65 | * @playerversion Flash 9.0 66 | * @tiptext 67 | */ 68 | public function getString():String { 69 | return jsonString; 70 | } 71 | 72 | /** 73 | * Converts a value to it's JSON string equivalent. 74 | * 75 | * @param value The value to convert. Could be any 76 | * type (object, number, array, etc) 77 | */ 78 | private function convertToString( value:* ):String { 79 | 80 | // determine what value is and convert it based on it's type 81 | if ( value is String ) { 82 | 83 | // escape the string so it's formatted correctly 84 | return escapeString( value as String ); 85 | 86 | } else if ( value is Number ) { 87 | 88 | // only encode numbers that finate 89 | return isFinite( value as Number) ? value.toString() : "null"; 90 | 91 | } else if ( value is Boolean ) { 92 | 93 | // convert boolean to string easily 94 | return value ? "true" : "false"; 95 | 96 | } else if ( value is Array ) { 97 | 98 | // call the helper method to convert an array 99 | return arrayToString( value as Array ); 100 | 101 | } else if ( value is Object && value != null ) { 102 | 103 | // call the helper method to convert an object 104 | return objectToString( value ); 105 | } 106 | return "null"; 107 | } 108 | 109 | /** 110 | * Escapes a string accoding to the JSON specification. 111 | * 112 | * @param str The string to be escaped 113 | * @return The string with escaped special characters 114 | * according to the JSON specification 115 | */ 116 | private function escapeString( str:String ):String { 117 | // create a string to store the string's jsonstring value 118 | var s:String = ""; 119 | // current character in the string we're processing 120 | var ch:String; 121 | // store the length in a local variable to reduce lookups 122 | var len:Number = str.length; 123 | 124 | // loop over all of the characters in the string 125 | for ( var i:int = 0; i < len; i++ ) { 126 | 127 | // examine the character to determine if we have to escape it 128 | ch = str.charAt( i ); 129 | switch ( ch ) { 130 | 131 | case '"': // quotation mark 132 | s += "\\\""; 133 | break; 134 | 135 | //case '/': // solidus 136 | // s += "\\/"; 137 | // break; 138 | 139 | case '\\': // reverse solidus 140 | s += "\\\\"; 141 | break; 142 | 143 | case '\b': // bell 144 | s += "\\b"; 145 | break; 146 | 147 | case '\f': // form feed 148 | s += "\\f"; 149 | break; 150 | 151 | case '\n': // newline 152 | s += "\\n"; 153 | break; 154 | 155 | case '\r': // carriage return 156 | s += "\\r"; 157 | break; 158 | 159 | case '\t': // horizontal tab 160 | s += "\\t"; 161 | break; 162 | 163 | default: // everything else 164 | 165 | // check for a control character and escape as unicode 166 | if ( ch < ' ' ) { 167 | // get the hex digit(s) of the character (either 1 or 2 digits) 168 | var hexCode:String = ch.charCodeAt( 0 ).toString( 16 ); 169 | 170 | // ensure that there are 4 digits by adjusting 171 | // the # of zeros accordingly. 172 | var zeroPad:String = hexCode.length == 2 ? "00" : "000"; 173 | 174 | // create the unicode escape sequence with 4 hex digits 175 | s += "\\u" + zeroPad + hexCode; 176 | } else { 177 | 178 | // no need to do any special encoding, just pass-through 179 | s += ch; 180 | 181 | } 182 | } // end switch 183 | 184 | } // end for loop 185 | 186 | return "\"" + s + "\""; 187 | } 188 | 189 | /** 190 | * Converts an array to it's JSON string equivalent 191 | * 192 | * @param a The array to convert 193 | * @return The JSON string representation of a 194 | */ 195 | private function arrayToString( a:Array ):String { 196 | // create a string to store the array's jsonstring value 197 | var s:String = ""; 198 | 199 | // loop over the elements in the array and add their converted 200 | // values to the string 201 | for ( var i:int = 0; i < a.length; i++ ) { 202 | // when the length is 0 we're adding the first element so 203 | // no comma is necessary 204 | if ( s.length > 0 ) { 205 | // we've already added an element, so add the comma separator 206 | s += "," 207 | } 208 | 209 | // convert the value to a string 210 | s += convertToString( a[i] ); 211 | } 212 | 213 | // KNOWN ISSUE: In ActionScript, Arrays can also be associative 214 | // objects and you can put anything in them, ie: 215 | // myArray["foo"] = "bar"; 216 | // 217 | // These properties aren't picked up in the for loop above because 218 | // the properties don't correspond to indexes. However, we're 219 | // sort of out luck because the JSON specification doesn't allow 220 | // these types of array properties. 221 | // 222 | // So, if the array was also used as an associative object, there 223 | // may be some values in the array that don't get properly encoded. 224 | // 225 | // A possible solution is to instead encode the Array as an Object 226 | // but then it won't get decoded correctly (and won't be an 227 | // Array instance) 228 | 229 | // close the array and return it's string value 230 | return "[" + s + "]"; 231 | } 232 | 233 | /** 234 | * Converts an object to it's JSON string equivalent 235 | * 236 | * @param o The object to convert 237 | * @return The JSON string representation of o 238 | */ 239 | private function objectToString( o:Object ):String 240 | { 241 | // create a string to store the object's jsonstring value 242 | var s:String = ""; 243 | 244 | // determine if o is a class instance or a plain object 245 | var classInfo:XML = describeType( o ); 246 | if ( classInfo.@name.toString() == "Object" ) 247 | { 248 | // the value of o[key] in the loop below - store this 249 | // as a variable so we don't have to keep looking up o[key] 250 | // when testing for valid values to convert 251 | var value:Object; 252 | 253 | // loop over the keys in the object and add their converted 254 | // values to the string 255 | for ( var key:String in o ) 256 | { 257 | // assign value to a variable for quick lookup 258 | value = o[key]; 259 | 260 | // don't add function's to the JSON string 261 | if ( value is Function ) 262 | { 263 | // skip this key and try another 264 | continue; 265 | } 266 | 267 | // when the length is 0 we're adding the first item so 268 | // no comma is necessary 269 | if ( s.length > 0 ) { 270 | // we've already added an item, so add the comma separator 271 | s += "," 272 | } 273 | 274 | s += escapeString( key ) + ":" + convertToString( value ); 275 | } 276 | } 277 | else // o is a class instance 278 | { 279 | // Loop over all of the variables and accessors in the class and 280 | // serialize them along with their values. 281 | for each ( var v:XML in classInfo..*.( name() == "variable" || name() == "accessor" ) ) 282 | { 283 | // When the length is 0 we're adding the first item so 284 | // no comma is necessary 285 | if ( s.length > 0 ) { 286 | // We've already added an item, so add the comma separator 287 | s += "," 288 | } 289 | 290 | s += escapeString( v.@name.toString() ) + ":" 291 | + convertToString( o[ v.@name ] ); 292 | } 293 | 294 | } 295 | 296 | return "{" + s + "}"; 297 | } 298 | 299 | 300 | } 301 | 302 | } 303 | -------------------------------------------------------------------------------- /src/com/adobe/serialization/json/JSONTokenizer.as: -------------------------------------------------------------------------------- 1 | /* 2 | Adobe Systems Incorporated(r) Source Code License Agreement 3 | Copyright(c) 2005 Adobe Systems Incorporated. All rights reserved. 4 | 5 | Please read this Source Code License Agreement carefully before using 6 | the source code. 7 | 8 | Adobe Systems Incorporated grants to you a perpetual, worldwide, non-exclusive, 9 | no-charge, royalty-free, irrevocable copyright license, to reproduce, 10 | prepare derivative works of, publicly display, publicly perform, and 11 | distribute this source code and such derivative works in source or 12 | object code form without any attribution requirements. 13 | 14 | The name "Adobe Systems Incorporated" must not be used to endorse or promote products 15 | derived from the source code without prior written permission. 16 | 17 | You agree to indemnify, hold harmless and defend Adobe Systems Incorporated from and 18 | against any loss, damage, claims or lawsuits, including attorney's 19 | fees that arise or result from your use or distribution of the source 20 | code. 21 | 22 | THIS SOURCE CODE IS PROVIDED "AS IS" AND "WITH ALL FAULTS", WITHOUT 23 | ANY TECHNICAL SUPPORT OR ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, 24 | BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 25 | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. ALSO, THERE IS NO WARRANTY OF 26 | NON-INFRINGEMENT, TITLE OR QUIET ENJOYMENT. IN NO EVENT SHALL MACROMEDIA 27 | OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 28 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 29 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 30 | OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 31 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 32 | OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOURCE CODE, EVEN IF 33 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | 36 | package com.adobe.serialization.json { 37 | 38 | public class JSONTokenizer { 39 | 40 | /** The object that will get parsed from the JSON string */ 41 | private var obj:Object; 42 | 43 | /** The JSON string to be parsed */ 44 | private var jsonString:String; 45 | 46 | /** The current parsing location in the JSON string */ 47 | private var loc:int; 48 | 49 | /** The current character in the JSON string during parsing */ 50 | private var ch:String; 51 | 52 | /** 53 | * Constructs a new JSONDecoder to parse a JSON string 54 | * into a native object. 55 | * 56 | * @param s The JSON string to be converted 57 | * into a native object 58 | */ 59 | public function JSONTokenizer( s:String ) { 60 | jsonString = s; 61 | loc = 0; 62 | 63 | // prime the pump by getting the first character 64 | nextChar(); 65 | } 66 | 67 | /** 68 | * Gets the next token in the input sting and advances 69 | * the character to the next character after the token 70 | */ 71 | public function getNextToken():JSONToken { 72 | var token:JSONToken = new JSONToken(); 73 | 74 | // skip any whitespace / comments since the last 75 | // token was read 76 | skipIgnored(); 77 | 78 | // examine the new character and see what we have... 79 | switch ( ch ) { 80 | 81 | case '{': 82 | token.type = JSONTokenType.LEFT_BRACE; 83 | token.value = '{'; 84 | nextChar(); 85 | break 86 | 87 | case '}': 88 | token.type = JSONTokenType.RIGHT_BRACE; 89 | token.value = '}'; 90 | nextChar(); 91 | break 92 | 93 | case '[': 94 | token.type = JSONTokenType.LEFT_BRACKET; 95 | token.value = '['; 96 | nextChar(); 97 | break 98 | 99 | case ']': 100 | token.type = JSONTokenType.RIGHT_BRACKET; 101 | token.value = ']'; 102 | nextChar(); 103 | break 104 | 105 | case ',': 106 | token.type = JSONTokenType.COMMA; 107 | token.value = ','; 108 | nextChar(); 109 | break 110 | 111 | case ':': 112 | token.type = JSONTokenType.COLON; 113 | token.value = ':'; 114 | nextChar(); 115 | break; 116 | 117 | case 't': // attempt to read true 118 | var possibleTrue:String = "t" + nextChar() + nextChar() + nextChar(); 119 | 120 | if ( possibleTrue == "true" ) { 121 | token.type = JSONTokenType.TRUE; 122 | token.value = true; 123 | nextChar(); 124 | } else { 125 | parseError( "Expecting 'true' but found " + possibleTrue ); 126 | } 127 | 128 | break; 129 | 130 | case 'f': // attempt to read false 131 | var possibleFalse:String = "f" + nextChar() + nextChar() + nextChar() + nextChar(); 132 | 133 | if ( possibleFalse == "false" ) { 134 | token.type = JSONTokenType.FALSE; 135 | token.value = false; 136 | nextChar(); 137 | } else { 138 | parseError( "Expecting 'false' but found " + possibleFalse ); 139 | } 140 | 141 | break; 142 | 143 | case 'n': // attempt to read null 144 | 145 | var possibleNull:String = "n" + nextChar() + nextChar() + nextChar(); 146 | 147 | if ( possibleNull == "null" ) { 148 | token.type = JSONTokenType.NULL; 149 | token.value = null; 150 | nextChar(); 151 | } else { 152 | parseError( "Expecting 'null' but found " + possibleNull ); 153 | } 154 | 155 | break; 156 | 157 | case '"': // the start of a string 158 | token = readString(); 159 | break; 160 | 161 | default: 162 | // see if we can read a number 163 | if ( isDigit( ch ) || ch == '-' ) { 164 | token = readNumber(); 165 | } else if ( ch == '' ) { 166 | // check for reading past the end of the string 167 | return null; 168 | } else { 169 | // not sure what was in the input string - it's not 170 | // anything we expected 171 | parseError( "Unexpected " + ch + " encountered" ); 172 | } 173 | } 174 | 175 | return token; 176 | } 177 | 178 | /** 179 | * Attempts to read a string from the input string. Places 180 | * the character location at the first character after the 181 | * string. It is assumed that ch is " before this method is called. 182 | * 183 | * @return the JSONToken with the string value if a string could 184 | * be read. Throws an error otherwise. 185 | */ 186 | private function readString():JSONToken { 187 | // the token for the string we'll try to read 188 | var token:JSONToken = new JSONToken(); 189 | token.type = JSONTokenType.STRING; 190 | 191 | // the string to store the string we'll try to read 192 | var string:String = ""; 193 | 194 | // advance past the first " 195 | nextChar(); 196 | 197 | while ( ch != '"' && ch != '' ) { 198 | 199 | // unescape the escape sequences in the string 200 | if ( ch == '\\' ) { 201 | 202 | // get the next character so we know what 203 | // to unescape 204 | nextChar(); 205 | 206 | switch ( ch ) { 207 | 208 | case '"': // quotation mark 209 | string += '"'; 210 | break; 211 | 212 | case '/': // solidus 213 | string += "/"; 214 | break; 215 | 216 | case '\\': // reverse solidus 217 | string += '\\'; 218 | break; 219 | 220 | case 'b': // bell 221 | string += '\b'; 222 | break; 223 | 224 | case 'f': // form feed 225 | string += '\f'; 226 | break; 227 | 228 | case 'n': // newline 229 | string += '\n'; 230 | break; 231 | 232 | case 'r': // carriage return 233 | string += '\r'; 234 | break; 235 | 236 | case 't': // horizontal tab 237 | string += '\t' 238 | break; 239 | 240 | case 'u': 241 | // convert a unicode escape sequence 242 | // to it's character value - expecting 243 | // 4 hex digits 244 | 245 | // save the characters as a string we'll convert to an int 246 | var hexValue:String = ""; 247 | 248 | // try to find 4 hex characters 249 | for ( var i:int = 0; i < 4; i++ ) { 250 | // get the next character and determine 251 | // if it's a valid hex digit or not 252 | if ( !isHexDigit( nextChar() ) ) { 253 | parseError( " Excepted a hex digit, but found: " + ch ); 254 | } 255 | // valid, add it to the value 256 | hexValue += ch; 257 | } 258 | 259 | // convert hexValue to an integer, and use that 260 | // integrer value to create a character to add 261 | // to our string. 262 | string += String.fromCharCode( parseInt( hexValue, 16 ) ); 263 | 264 | break; 265 | 266 | default: 267 | // couldn't unescape the sequence, so just 268 | // pass it through 269 | string += '\\' + ch; 270 | 271 | } 272 | 273 | } else { 274 | // didn't have to unescape, so add the character to the string 275 | string += ch; 276 | 277 | } 278 | 279 | // move to the next character 280 | nextChar(); 281 | 282 | } 283 | 284 | // we read past the end of the string without closing it, which 285 | // is a parse error 286 | if ( ch == '' ) { 287 | parseError( "Unterminated string literal" ); 288 | } 289 | 290 | // move past the closing " in the input string 291 | nextChar(); 292 | 293 | // attach to the string to the token so we can return it 294 | token.value = string; 295 | 296 | return token; 297 | } 298 | 299 | /** 300 | * Attempts to read a number from the input string. Places 301 | * the character location at the first character after the 302 | * number. 303 | * 304 | * @return The JSONToken with the number value if a number could 305 | * be read. Throws an error otherwise. 306 | */ 307 | private function readNumber():JSONToken { 308 | // the token for the number we'll try to read 309 | var token:JSONToken = new JSONToken(); 310 | token.type = JSONTokenType.NUMBER; 311 | 312 | // the string to accumulate the number characters 313 | // into that we'll convert to a number at the end 314 | var input:String = ""; 315 | 316 | // check for a negative number 317 | if ( ch == '-' ) { 318 | input += '-'; 319 | nextChar(); 320 | } 321 | 322 | // the number must start with a digit 323 | if ( !isDigit( ch ) ) 324 | { 325 | parseError( "Expecting a digit" ); 326 | } 327 | 328 | // 0 can only be the first digit if it 329 | // is followed by a decimal point 330 | if ( ch == '0' ) 331 | { 332 | input += ch; 333 | nextChar(); 334 | 335 | // make sure no other digits come after 0 336 | if ( isDigit( ch ) ) 337 | { 338 | parseError( "A digit cannot immediately follow 0" ); 339 | } 340 | } 341 | else 342 | { 343 | // read numbers while we can 344 | while ( isDigit( ch ) ) { 345 | input += ch; 346 | nextChar(); 347 | } 348 | } 349 | 350 | // check for a decimal value 351 | if ( ch == '.' ) { 352 | input += '.'; 353 | nextChar(); 354 | 355 | // after the decimal there has to be a digit 356 | if ( !isDigit( ch ) ) 357 | { 358 | parseError( "Expecting a digit" ); 359 | } 360 | 361 | // read more numbers to get the decimal value 362 | while ( isDigit( ch ) ) { 363 | input += ch; 364 | nextChar(); 365 | } 366 | } 367 | 368 | // check for scientific notation 369 | if ( ch == 'e' || ch == 'E' ) 370 | { 371 | input += "e" 372 | nextChar(); 373 | // check for sign 374 | if ( ch == '+' || ch == '-' ) 375 | { 376 | input += ch; 377 | nextChar(); 378 | } 379 | 380 | // require at least one number for the exponent 381 | // in this case 382 | if ( !isDigit( ch ) ) 383 | { 384 | parseError( "Scientific notation number needs exponent value" ); 385 | } 386 | 387 | // read in the exponent 388 | while ( isDigit( ch ) ) 389 | { 390 | input += ch; 391 | nextChar(); 392 | } 393 | } 394 | 395 | // convert the string to a number value 396 | var num:Number = Number( input ); 397 | 398 | if ( isFinite( num ) && !isNaN( num ) ) { 399 | token.value = num; 400 | return token; 401 | } else { 402 | parseError( "Number " + num + " is not valid!" ); 403 | } 404 | return null; 405 | } 406 | 407 | /** 408 | * Reads the next character in the input 409 | * string and advances the character location. 410 | * 411 | * @return The next character in the input string, or 412 | * null if we've read past the end. 413 | */ 414 | private function nextChar():String { 415 | return ch = jsonString.charAt( loc++ ); 416 | } 417 | 418 | /** 419 | * Advances the character location past any 420 | * sort of white space and comments 421 | */ 422 | private function skipIgnored():void { 423 | skipWhite(); 424 | skipComments(); 425 | skipWhite(); 426 | } 427 | 428 | /** 429 | * Skips comments in the input string, either 430 | * single-line or multi-line. Advances the character 431 | * to the first position after the end of the comment. 432 | */ 433 | private function skipComments():void { 434 | if ( ch == '/' ) { 435 | // Advance past the first / to find out what type of comment 436 | nextChar(); 437 | switch ( ch ) { 438 | case '/': // single-line comment, read through end of line 439 | 440 | // Loop over the characters until we find 441 | // a newline or until there's no more characters left 442 | do { 443 | nextChar(); 444 | } while ( ch != '\n' && ch != '' ) 445 | 446 | // move past the \n 447 | nextChar(); 448 | 449 | break; 450 | 451 | case '*': // multi-line comment, read until closing */ 452 | 453 | // move past the opening * 454 | nextChar(); 455 | 456 | // try to find a trailing */ 457 | while ( true ) { 458 | if ( ch == '*' ) { 459 | // check to see if we have a closing / 460 | nextChar(); 461 | if ( ch == '/') { 462 | // move past the end of the closing */ 463 | nextChar(); 464 | break; 465 | } 466 | } else { 467 | // move along, looking if the next character is a * 468 | nextChar(); 469 | } 470 | 471 | // when we're here we've read past the end of 472 | // the string without finding a closing */, so error 473 | if ( ch == '' ) { 474 | parseError( "Multi-line comment not closed" ); 475 | } 476 | } 477 | 478 | break; 479 | 480 | // Can't match a comment after a /, so it's a parsing error 481 | default: 482 | parseError( "Unexpected " + ch + " encountered (expecting '/' or '*' )" ); 483 | } 484 | } 485 | 486 | } 487 | 488 | 489 | /** 490 | * Skip any whitespace in the input string and advances 491 | * the character to the first character after any possible 492 | * whitespace. 493 | */ 494 | private function skipWhite():void { 495 | 496 | // As long as there are spaces in the input 497 | // stream, advance the current location pointer 498 | // past them 499 | while ( isWhiteSpace( ch ) ) { 500 | nextChar(); 501 | } 502 | 503 | } 504 | 505 | /** 506 | * Determines if a character is whitespace or not. 507 | * 508 | * @return True if the character passed in is a whitespace 509 | * character 510 | */ 511 | private function isWhiteSpace( ch:String ):Boolean { 512 | return ( ch == ' ' || ch == '\t' || ch == '\n' ); 513 | } 514 | 515 | /** 516 | * Determines if a character is a digit [0-9]. 517 | * 518 | * @return True if the character passed in is a digit 519 | */ 520 | private function isDigit( ch:String ):Boolean { 521 | return ( ch >= '0' && ch <= '9' ); 522 | } 523 | 524 | /** 525 | * Determines if a character is a digit [0-9]. 526 | * 527 | * @return True if the character passed in is a digit 528 | */ 529 | private function isHexDigit( ch:String ):Boolean { 530 | // get the uppercase value of ch so we only have 531 | // to compare the value between 'A' and 'F' 532 | var uc:String = ch.toUpperCase(); 533 | 534 | // a hex digit is a digit of A-F, inclusive ( using 535 | // our uppercase constraint ) 536 | return ( isDigit( ch ) || ( uc >= 'A' && uc <= 'F' ) ); 537 | } 538 | 539 | /** 540 | * Raises a parsing error with a specified message, tacking 541 | * on the error location and the original string. 542 | * 543 | * @param message The message indicating why the error occurred 544 | */ 545 | public function parseError( message:String ):void { 546 | throw new JSONParseError( message, loc, jsonString ); 547 | } 548 | } 549 | 550 | } 551 | -------------------------------------------------------------------------------- /src/com/adobe/utils/DateUtil.as: -------------------------------------------------------------------------------- 1 | /* 2 | Adobe Systems Incorporated(r) Source Code License Agreement 3 | Copyright(c) 2005 Adobe Systems Incorporated. All rights reserved. 4 | 5 | Please read this Source Code License Agreement carefully before using 6 | the source code. 7 | 8 | Adobe Systems Incorporated grants to you a perpetual, worldwide, non-exclusive, 9 | no-charge, royalty-free, irrevocable copyright license, to reproduce, 10 | prepare derivative works of, publicly display, publicly perform, and 11 | distribute this source code and such derivative works in source or 12 | object code form without any attribution requirements. 13 | 14 | The name "Adobe Systems Incorporated" must not be used to endorse or promote products 15 | derived from the source code without prior written permission. 16 | 17 | You agree to indemnify, hold harmless and defend Adobe Systems Incorporated from and 18 | against any loss, damage, claims or lawsuits, including attorney's 19 | fees that arise or result from your use or distribution of the source 20 | code. 21 | 22 | THIS SOURCE CODE IS PROVIDED "AS IS" AND "WITH ALL FAULTS", WITHOUT 23 | ANY TECHNICAL SUPPORT OR ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, 24 | BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 25 | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. ALSO, THERE IS NO WARRANTY OF 26 | NON-INFRINGEMENT, TITLE OR QUIET ENJOYMENT. IN NO EVENT SHALL MACROMEDIA 27 | OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 28 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 29 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 30 | OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 31 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 32 | OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOURCE CODE, EVEN IF 33 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | 36 | package com.adobe.utils 37 | { 38 | import com.adobe.utils.ArrayUtil; 39 | import mx.formatters.DateBase; 40 | 41 | /** 42 | * Class that contains static utility methods for manipulating and working 43 | * with Dates. 44 | * 45 | * @langversion ActionScript 3.0 46 | * @playerversion Flash 9.0 47 | * @tiptext 48 | */ 49 | public class DateUtil 50 | { 51 | 52 | /** 53 | * Returns the English Short Month name (3 letters) for the Month that 54 | * the Date represents. 55 | * 56 | * @param d The Date instance whose month will be used to retrieve the 57 | * short month name. 58 | * 59 | * @return An English 3 Letter Month abbreviation. 60 | * 61 | * @langversion ActionScript 3.0 62 | * @playerversion Flash 9.0 63 | * @tiptext 64 | * 65 | * @see SHORT_MONTH 66 | */ 67 | public static function getShortMonthName(d:Date):String 68 | { 69 | return DateBase.monthNamesShort[d.getMonth()]; 70 | } 71 | 72 | /** 73 | * Returns the index of the month that the short month name string 74 | * represents. 75 | * 76 | * @param m The 3 letter abbreviation representing a short month name. 77 | * 78 | * @param Optional parameter indicating whether the search should be case 79 | * sensitive 80 | * 81 | * @return A int that represents that month represented by the specifed 82 | * short name. 83 | * 84 | * @langversion ActionScript 3.0 85 | * @playerversion Flash 9.0 86 | * @tiptext 87 | * 88 | * @see SHORT_MONTH 89 | */ 90 | public static function getShortMonthIndex(m:String):int 91 | { 92 | return DateBase.monthNamesShort.indexOf(m); 93 | } 94 | 95 | /** 96 | * Returns the English full Month name for the Month that 97 | * the Date represents. 98 | * 99 | * @param d The Date instance whose month will be used to retrieve the 100 | * full month name. 101 | * 102 | * @return An English full month name. 103 | * 104 | * @langversion ActionScript 3.0 105 | * @playerversion Flash 9.0 106 | * @tiptext 107 | * 108 | * @see FULL_MONTH 109 | */ 110 | public static function getFullMonthName(d:Date):String 111 | { 112 | return DateBase.monthNamesLong[d.getMonth()]; 113 | } 114 | 115 | /** 116 | * Returns the index of the month that the full month name string 117 | * represents. 118 | * 119 | * @param m A full month name. 120 | * 121 | * @return A int that represents that month represented by the specifed 122 | * full month name. 123 | * 124 | * @langversion ActionScript 3.0 125 | * @playerversion Flash 9.0 126 | * @tiptext 127 | * 128 | * @see FULL_MONTH 129 | */ 130 | public static function getFullMonthIndex(m:String):int 131 | { 132 | return DateBase.monthNamesLong.indexOf(m); 133 | } 134 | 135 | /** 136 | * Returns the English Short Day name (3 letters) for the day that 137 | * the Date represents. 138 | * 139 | * @param d The Date instance whose day will be used to retrieve the 140 | * short day name. 141 | * 142 | * @return An English 3 Letter day abbreviation. 143 | * 144 | * @langversion ActionScript 3.0 145 | * @playerversion Flash 9.0 146 | * @tiptext 147 | * 148 | * @see SHORT_DAY 149 | */ 150 | public static function getShortDayName(d:Date):String 151 | { 152 | return DateBase.dayNamesShort[d.getDay()]; 153 | } 154 | 155 | /** 156 | * Returns the index of the day that the short day name string 157 | * represents. 158 | * 159 | * @param m A short day name. 160 | * 161 | * @return A int that represents that short day represented by the specifed 162 | * full month name. 163 | * 164 | * @langversion ActionScript 3.0 165 | * @playerversion Flash 9.0 166 | * @tiptext 167 | * 168 | * @see SHORT_DAY 169 | */ 170 | public static function getShortDayIndex(d:String):int 171 | { 172 | return DateBase.dayNamesShort.indexOf(d); 173 | } 174 | 175 | /** 176 | * Returns the English full day name for the day that 177 | * the Date represents. 178 | * 179 | * @param d The Date instance whose day will be used to retrieve the 180 | * full day name. 181 | * 182 | * @return An English full day name. 183 | * 184 | * @langversion ActionScript 3.0 185 | * @playerversion Flash 9.0 186 | * @tiptext 187 | * 188 | * @see FULL_DAY 189 | */ 190 | public static function getFullDayName(d:Date):String 191 | { 192 | return DateBase.dayNamesLong[d.getDay()]; 193 | } 194 | 195 | /** 196 | * Returns the index of the day that the full day name string 197 | * represents. 198 | * 199 | * @param m A full day name. 200 | * 201 | * @return A int that represents that full day represented by the specifed 202 | * full month name. 203 | * 204 | * @langversion ActionScript 3.0 205 | * @playerversion Flash 9.0 206 | * @tiptext 207 | * 208 | * @see FULL_DAY 209 | */ 210 | public static function getFullDayIndex(d:String):int 211 | { 212 | return DateBase.dayNamesLong.indexOf(d); 213 | } 214 | 215 | /** 216 | * Returns a two digit representation of the year represented by the 217 | * specified date. 218 | * 219 | * @param d The Date instance whose year will be used to generate a two 220 | * digit string representation of the year. 221 | * 222 | * @return A string that contains a 2 digit representation of the year. 223 | * Single digits will be padded with 0. 224 | * 225 | * @langversion ActionScript 3.0 226 | * @playerversion Flash 9.0 227 | * @tiptext 228 | */ 229 | public static function getShortYear(d:Date):String 230 | { 231 | var dStr:String = String(d.getFullYear()); 232 | 233 | if(dStr.length < 3) 234 | { 235 | return dStr; 236 | } 237 | 238 | return (dStr.substr(dStr.length - 2)); 239 | } 240 | 241 | /** 242 | * Compares two dates and returns an integer depending on their relationship. 243 | * 244 | * Returns -1 if d1 is greater than d2. 245 | * Returns 1 if d2 is greater than d1. 246 | * Returns 0 if both dates are equal. 247 | * 248 | * @param d1 The date that will be compared to the second date. 249 | * @param d2 The date that will be compared to the first date. 250 | * 251 | * @return An int indicating how the two dates compare. 252 | * 253 | * @langversion ActionScript 3.0 254 | * @playerversion Flash 9.0 255 | * @tiptext 256 | */ 257 | public static function compareDates(d1:Date, d2:Date):int 258 | { 259 | var d1ms:Number = d1.getTime(); 260 | var d2ms:Number = d2.getTime(); 261 | 262 | if(d1ms > d2ms) 263 | { 264 | return -1; 265 | } 266 | else if(d1ms < d2ms) 267 | { 268 | return 1; 269 | } 270 | else 271 | { 272 | return 0; 273 | } 274 | } 275 | 276 | /** 277 | * Returns a short hour (0 - 12) represented by the specified date. 278 | * 279 | * If the hour is less than 12 (0 - 11 AM) then the hour will be returned. 280 | * 281 | * If the hour is greater than 12 (12 - 23 PM) then the hour minus 12 282 | * will be returned. 283 | * 284 | * @param d1 The Date from which to generate the short hour 285 | * 286 | * @return An int between 0 and 13 ( 1 - 12 ) representing the short hour. 287 | * 288 | * @langversion ActionScript 3.0 289 | * @playerversion Flash 9.0 290 | * @tiptext 291 | */ 292 | public static function getShortHour(d:Date):int 293 | { 294 | var h:int = d.hours; 295 | 296 | if(h == 0 || h == 12) 297 | { 298 | return 12; 299 | } 300 | else if(h > 12) 301 | { 302 | return h - 12; 303 | } 304 | else 305 | { 306 | return h; 307 | } 308 | } 309 | 310 | /** 311 | * Returns a string indicating whether the date represents a time in the 312 | * ante meridiem (AM) or post meridiem (PM). 313 | * 314 | * If the hour is less than 12 then "AM" will be returned. 315 | * 316 | * If the hour is greater than 12 then "PM" will be returned. 317 | * 318 | * @param d1 The Date from which to generate the 12 hour clock indicator. 319 | * 320 | * @return A String ("AM" or "PM") indicating which half of the day the 321 | * hour represents. 322 | * 323 | * @langversion ActionScript 3.0 324 | * @playerversion Flash 9.0 325 | * @tiptext 326 | */ 327 | public static function getAMPM(d:Date):String 328 | { 329 | return (d.hours > 11)? "PM" : "AM"; 330 | } 331 | 332 | /** 333 | * Parses dates that conform to RFC822 into Date objects. This method also 334 | * supports four-digit years (not supported in RFC822), but two-digit years 335 | * (referring to the 20th century) are fine, too. 336 | * 337 | * This function is useful for parsing RSS .91, .92, and 2.0 dates. 338 | * 339 | * @param str 340 | * 341 | * @returns 342 | * 343 | * @langversion ActionScript 3.0 344 | * @playerversion Flash 9.0 345 | * @tiptext 346 | * 347 | * @see http://asg.web.cmu.edu/rfc/rfc822.html 348 | */ 349 | public static function parseRFC822(str:String):Date 350 | { 351 | var finalDate:Date; 352 | try 353 | { 354 | var dateParts:Array = str.split(" "); 355 | var day:String = null; 356 | 357 | if (dateParts[0].search(/\d/) == -1) 358 | { 359 | day = dateParts.shift().replace(/\W/, ""); 360 | } 361 | 362 | var date:Number = Number(dateParts.shift()); 363 | var month:Number = Number(DateUtil.getShortMonthIndex(dateParts.shift())); 364 | var year:Number = Number(dateParts.shift()); 365 | var timeParts:Array = dateParts.shift().split(":"); 366 | var hour:Number = int(timeParts.shift()); 367 | var minute:Number = int(timeParts.shift()); 368 | var second:Number = (timeParts.length > 0) ? int(timeParts.shift()): 0; 369 | 370 | var milliseconds:Number = Date.UTC(year, month, date, hour, minute, second, 0); 371 | 372 | var timezone:String = dateParts.shift(); 373 | var offset:Number = 0; 374 | 375 | if (timezone.search(/\d/) == -1) 376 | { 377 | switch(timezone) 378 | { 379 | case "UT": 380 | offset = 0; 381 | break; 382 | case "UTC": 383 | offset = 0; 384 | break; 385 | case "GMT": 386 | offset = 0; 387 | break; 388 | case "EST": 389 | offset = (-5 * 3600000); 390 | break; 391 | case "EDT": 392 | offset = (-4 * 3600000); 393 | break; 394 | case "CST": 395 | offset = (-6 * 3600000); 396 | break; 397 | case "CDT": 398 | offset = (-5 * 3600000); 399 | break; 400 | case "MST": 401 | offset = (-7 * 3600000); 402 | break; 403 | case "MDT": 404 | offset = (-6 * 3600000); 405 | break; 406 | case "PST": 407 | offset = (-8 * 3600000); 408 | break; 409 | case "PDT": 410 | offset = (-7 * 3600000); 411 | break; 412 | case "Z": 413 | offset = 0; 414 | break; 415 | case "A": 416 | offset = (-1 * 3600000); 417 | break; 418 | case "M": 419 | offset = (-12 * 3600000); 420 | break; 421 | case "N": 422 | offset = (1 * 3600000); 423 | break; 424 | case "Y": 425 | offset = (12 * 3600000); 426 | break; 427 | default: 428 | offset = 0; 429 | } 430 | } 431 | else 432 | { 433 | var multiplier:Number = 1; 434 | var oHours:Number = 0; 435 | var oMinutes:Number = 0; 436 | if (timezone.length != 4) 437 | { 438 | if (timezone.charAt(0) == "-") 439 | { 440 | multiplier = -1; 441 | } 442 | timezone = timezone.substr(1, 4); 443 | } 444 | oHours = Number(timezone.substr(0, 2)); 445 | oMinutes = Number(timezone.substr(2, 2)); 446 | offset = (((oHours * 3600000) + (oMinutes * 60000)) * multiplier); 447 | } 448 | 449 | finalDate = new Date(milliseconds - offset); 450 | 451 | if (finalDate.toString() == "Invalid Date") 452 | { 453 | throw new Error("This date does not conform to RFC822."); 454 | } 455 | } 456 | catch (e:Error) 457 | { 458 | var eStr:String = "Unable to parse the string [" +str+ "] into a date. "; 459 | eStr += "The internal error was: " + e.toString(); 460 | throw new Error(eStr); 461 | } 462 | return finalDate; 463 | } 464 | 465 | /** 466 | * Returns a date string formatted according to RFC822. 467 | * 468 | * @param d 469 | * 470 | * @returns 471 | * 472 | * @langversion ActionScript 3.0 473 | * @playerversion Flash 9.0 474 | * @tiptext 475 | * 476 | * @see http://asg.web.cmu.edu/rfc/rfc822.html 477 | */ 478 | public static function toRFC822(d:Date):String 479 | { 480 | var date:Number = d.getUTCDate(); 481 | var hours:Number = d.getUTCHours(); 482 | var minutes:Number = d.getUTCMinutes(); 483 | var seconds:Number = d.getUTCSeconds(); 484 | var sb:String = new String(); 485 | sb += DateBase.dayNamesShort[d.getUTCDay()]; 486 | sb += ", "; 487 | 488 | if (date < 10) 489 | { 490 | sb += "0"; 491 | } 492 | sb += date; 493 | sb += " "; 494 | //sb += DateUtil.SHORT_MONTH[d.getUTCMonth()]; 495 | sb += DateBase.monthNamesShort[d.getUTCMonth()]; 496 | sb += " "; 497 | sb += d.getUTCFullYear(); 498 | sb += " "; 499 | if (hours < 10) 500 | { 501 | sb += "0"; 502 | } 503 | sb += hours; 504 | sb += ":"; 505 | if (minutes < 10) 506 | { 507 | sb += "0"; 508 | } 509 | sb += minutes; 510 | sb += ":"; 511 | if (seconds < 10) 512 | { 513 | sb += "0"; 514 | } 515 | sb += seconds; 516 | sb += " GMT"; 517 | return sb; 518 | } 519 | 520 | /** 521 | * Parses dates that conform to the W3C Date-time Format into Date objects. 522 | * 523 | * This function is useful for parsing RSS 1.0 and Atom 1.0 dates. 524 | * 525 | * @param str 526 | * 527 | * @returns 528 | * 529 | * @langversion ActionScript 3.0 530 | * @playerversion Flash 9.0 531 | * @tiptext 532 | * 533 | * @see http://www.w3.org/TR/NOTE-datetime 534 | */ 535 | public static function parseW3CDTF(str:String):Date 536 | { 537 | var finalDate:Date; 538 | try 539 | { 540 | var dateStr:String = str.substring(0, str.indexOf("T")); 541 | var timeStr:String = str.substring(str.indexOf("T")+1, str.length); 542 | var dateArr:Array = dateStr.split("-"); 543 | var year:Number = Number(dateArr.shift()); 544 | var month:Number = Number(dateArr.shift()); 545 | var date:Number = Number(dateArr.shift()); 546 | 547 | var multiplier:Number; 548 | var offsetHours:Number; 549 | var offsetMinutes:Number; 550 | var offsetStr:String; 551 | 552 | if (timeStr.indexOf("Z") != -1) 553 | { 554 | multiplier = 1; 555 | offsetHours = 0; 556 | offsetMinutes = 0; 557 | timeStr = timeStr.replace("Z", ""); 558 | } 559 | else if (timeStr.indexOf("+") != -1) 560 | { 561 | multiplier = 1; 562 | offsetStr = timeStr.substring(timeStr.indexOf("+")+1, timeStr.length); 563 | offsetHours = Number(offsetStr.substring(0, offsetStr.indexOf(":"))); 564 | offsetMinutes = Number(offsetStr.substring(offsetStr.indexOf(":")+1, offsetStr.length)); 565 | timeStr = timeStr.substring(0, timeStr.indexOf("+")); 566 | } 567 | else // offset is - 568 | { 569 | multiplier = -1; 570 | offsetStr = timeStr.substring(timeStr.indexOf("-")+1, timeStr.length); 571 | offsetHours = Number(offsetStr.substring(0, offsetStr.indexOf(":"))); 572 | offsetMinutes = Number(offsetStr.substring(offsetStr.indexOf(":")+1, offsetStr.length)); 573 | timeStr = timeStr.substring(0, timeStr.indexOf("-")); 574 | } 575 | var timeArr:Array = timeStr.split(":"); 576 | var hour:Number = Number(timeArr.shift()); 577 | var minutes:Number = Number(timeArr.shift()); 578 | var secondsArr:Array = (timeArr.length > 0) ? String(timeArr.shift()).split(".") : null; 579 | var seconds:Number = (secondsArr != null && secondsArr.length > 0) ? Number(secondsArr.shift()) : 0; 580 | var milliseconds:Number = (secondsArr != null && secondsArr.length > 0) ? Number(secondsArr.shift()) : 0; 581 | var utc:Number = Date.UTC(year, month-1, date, hour, minutes, seconds, milliseconds); 582 | var offset:Number = (((offsetHours * 3600000) + (offsetMinutes * 60000)) * multiplier); 583 | finalDate = new Date(utc - offset); 584 | 585 | if (finalDate.toString() == "Invalid Date") 586 | { 587 | throw new Error("This date does not conform to W3CDTF."); 588 | } 589 | } 590 | catch (e:Error) 591 | { 592 | var eStr:String = "Unable to parse the string [" +str+ "] into a date. "; 593 | eStr += "The internal error was: " + e.toString(); 594 | throw new Error(eStr); 595 | } 596 | return finalDate; 597 | } 598 | 599 | /** 600 | * Returns a date string formatted according to W3CDTF. 601 | * 602 | * @param d 603 | * @param includeMilliseconds Determines whether to include the 604 | * milliseconds value (if any) in the formatted string. 605 | * 606 | * @returns 607 | * 608 | * @langversion ActionScript 3.0 609 | * @playerversion Flash 9.0 610 | * @tiptext 611 | * 612 | * @see http://www.w3.org/TR/NOTE-datetime 613 | */ 614 | public static function toW3CDTF(d:Date,includeMilliseconds:Boolean=false):String 615 | { 616 | var date:Number = d.getUTCDate(); 617 | var month:Number = d.getUTCMonth(); 618 | var hours:Number = d.getUTCHours(); 619 | var minutes:Number = d.getUTCMinutes(); 620 | var seconds:Number = d.getUTCSeconds(); 621 | var milliseconds:Number = d.getUTCMilliseconds(); 622 | var sb:String = new String(); 623 | 624 | sb += d.getUTCFullYear(); 625 | sb += "-"; 626 | 627 | //thanks to "dom" who sent in a fix for the line below 628 | if (month + 1 < 10) 629 | { 630 | sb += "0"; 631 | } 632 | sb += month + 1; 633 | sb += "-"; 634 | if (date < 10) 635 | { 636 | sb += "0"; 637 | } 638 | sb += date; 639 | sb += "T"; 640 | if (hours < 10) 641 | { 642 | sb += "0"; 643 | } 644 | sb += hours; 645 | sb += ":"; 646 | if (minutes < 10) 647 | { 648 | sb += "0"; 649 | } 650 | sb += minutes; 651 | sb += ":"; 652 | if (seconds < 10) 653 | { 654 | sb += "0"; 655 | } 656 | sb += seconds; 657 | if (includeMilliseconds && milliseconds > 0) 658 | { 659 | sb += "."; 660 | sb += milliseconds; 661 | } 662 | sb += "-00:00"; 663 | return sb; 664 | } 665 | } 666 | } 667 | --------------------------------------------------------------------------------