Subversion-Projekte lars-tiefland.cienc

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
9 lars 1
/*
2
Base64 - 1.1.0
3
 
4
Copyright (c) 2006 Steve Webster
5
 
6
Permission is hereby granted, free of charge, to any person obtaining a copy of
7
this software and associated documentation files (the "Software"), to deal in
8
the Software without restriction, including without limitation the rights to
9
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10
the Software, and to permit persons to whom the Software is furnished to do so,
11
subject to the following conditions:
12
 
13
The above copyright notice and this permission notice shall be included in all
14
copies or substantial portions of the Software.
15
 
16
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
*/
23
 
24
package com.dynamicflash.util {
25
 
26
	import flash.utils.ByteArray;
27
 
28
	public class Base64 {
29
 
30
		private static const BASE64_CHARS:String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
31
 
32
		public static const version:String = "1.1.0";
33
 
34
		public static function encode(data:String):String {
35
			// Convert string to ByteArray
36
			var bytes:ByteArray = new ByteArray();
37
			bytes.writeUTFBytes(data);
38
 
39
			// Return encoded ByteArray
40
			return encodeByteArray(bytes);
41
		}
42
 
43
		public static function encodeByteArray(data:ByteArray):String {
44
			// Initialise output
45
			var output:String = "";
46
 
47
			// Create data and output buffers
48
			var dataBuffer:Array;
49
			var outputBuffer:Array = new Array(4);
50
 
51
			// Rewind ByteArray
52
			data.position = 0;
53
 
54
			// while there are still bytes to be processed
55
			while (data.bytesAvailable > 0) {
56
				// Create new data buffer and populate next 3 bytes from data
57
				dataBuffer = new Array();
58
				for (var i:uint = 0; i < 3 && data.bytesAvailable > 0; i++) {
59
					dataBuffer[i] = data.readUnsignedByte();
60
				}
61
 
62
				// Convert to data buffer Base64 character positions and
63
				// store in output buffer
64
				outputBuffer[0] = (dataBuffer[0] & 0xfc) >> 2;
65
				outputBuffer[1] = ((dataBuffer[0] & 0x03) << 4) | ((dataBuffer[1]) >> 4);
66
				outputBuffer[2] = ((dataBuffer[1] & 0x0f) << 2) | ((dataBuffer[2]) >> 6);
67
				outputBuffer[3] = dataBuffer[2] & 0x3f;
68
 
69
				// If data buffer was short (i.e not 3 characters) then set
70
				// end character indexes in data buffer to index of '=' symbol.
71
				// This is necessary because Base64 data is always a multiple of
72
				// 4 bytes and is basses with '=' symbols.
73
				for (var j:uint = dataBuffer.length; j < 3; j++) {
74
					outputBuffer[j + 1] = 64;
75
				}
76
 
77
				// Loop through output buffer and add Base64 characters to
78
				// encoded data string for each character.
79
				for (var k:uint = 0; k < outputBuffer.length; k++) {
80
					output += BASE64_CHARS.charAt(outputBuffer[k]);
81
				}
82
			}
83
 
84
			// Return encoded data
85
			return output;
86
		}
87
 
88
		public static function decode(data:String):String {
89
			// Decode data to ByteArray
90
			var bytes:ByteArray = decodeToByteArray(data);
91
 
92
			// Convert to string and return
93
			return bytes.readUTFBytes(bytes.length);
94
		}
95
 
96
		public static function decodeToByteArray(data:String):ByteArray {
97
			// Initialise output ByteArray for decoded data
98
			var output:ByteArray = new ByteArray();
99
 
100
			// Create data and output buffers
101
			var dataBuffer:Array = new Array(4);
102
			var outputBuffer:Array = new Array(3);
103
 
104
			// While there are data bytes left to be processed
105
			for (var i:uint = 0; i < data.length; i += 4) {
106
				// Populate data buffer with position of Base64 characters for
107
				// next 4 bytes from encoded data
108
				for (var j:uint = 0; j < 4 && i + j < data.length; j++) {
109
					dataBuffer[j] = BASE64_CHARS.indexOf(data.charAt(i + j));
110
				}
111
 
112
      			// Decode data buffer back into bytes
113
				outputBuffer[0] = (dataBuffer[0] << 2) + ((dataBuffer[1] & 0x30) >> 4);
114
				outputBuffer[1] = ((dataBuffer[1] & 0x0f) << 4) + ((dataBuffer[2] & 0x3c) >> 2);
115
				outputBuffer[2] = ((dataBuffer[2] & 0x03) << 6) + dataBuffer[3];
116
 
117
				// Add all non-padded bytes in output buffer to decoded data
118
				for (var k:uint = 0; k < outputBuffer.length; k++) {
119
					if (dataBuffer[k+1] == 64) break;
120
					output.writeByte(outputBuffer[k]);
121
				}
122
			}
123
 
124
			// Rewind decoded data ByteArray
125
			output.position = 0;
126
 
127
			// Return decoded data
128
			return output;
129
		}
130
 
131
		public function Base64() {
132
			throw new Error("Base64 class is static container only");
133
		}
134
	}
135
}