├── 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 |
14 | RECORD15 | 16 | 17 | 18 | 19 | 20 | 25 | 26 | 27 | 30 | 31 | 32 |37 | 38 | 39 | 73 | 74 | | 75 | 76 | |
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 | *
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
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 |
--------------------------------------------------------------------------------