Subversion-Projekte lars-tiefland.ci

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
875 lars 1
/*
2
 * Copyright (c) 2012 chick307 <chick307@gmail.com>
3
 *
4
 * Licensed under the MIT License.
5
 * http://opensource.org/licenses/mit-license
6
 */
7
 
8
void function(global, callback) {
9
	if (typeof module === 'object') {
10
		module.exports = callback();
11
	} else if (typeof define === 'function') {
12
		define(callback);
13
	} else {
14
		global.adler32cs = callback();
15
	}
16
}(this, function() {
17
	var _hasArrayBuffer = typeof ArrayBuffer === 'function' &&
18
		typeof Uint8Array === 'function';
19
 
20
	var _Buffer = null, _isBuffer = (function() {
21
		if (!_hasArrayBuffer)
22
			return function _isBuffer() { return false };
23
 
24
		try {
25
			var buffer = require('buffer');
26
			if (typeof buffer.Buffer === 'function')
27
				_Buffer = buffer.Buffer;
28
		} catch (error) {}
29
 
30
		return function _isBuffer(value) {
31
			return value instanceof ArrayBuffer ||
32
				_Buffer !== null && value instanceof _Buffer;
33
		};
34
	}());
35
 
36
	var _utf8ToBinary = (function() {
37
		if (_Buffer !== null) {
38
			return function _utf8ToBinary(utf8String) {
39
				return new _Buffer(utf8String, 'utf8').toString('binary');
40
			};
41
		} else {
42
			return function _utf8ToBinary(utf8String) {
43
				return unescape(encodeURIComponent(utf8String));
44
			};
45
		}
46
	}());
47
 
48
	var MOD = 65521;
49
 
50
	var _update = function _update(checksum, binaryString) {
51
		var a = checksum & 0xFFFF, b = checksum >>> 16;
52
		for (var i = 0, length = binaryString.length; i < length; i++) {
53
			a = (a + (binaryString.charCodeAt(i) & 0xFF)) % MOD;
54
			b = (b + a) % MOD;
55
		}
56
		return (b << 16 | a) >>> 0;
57
	};
58
 
59
	var _updateUint8Array = function _updateUint8Array(checksum, uint8Array) {
60
		var a = checksum & 0xFFFF, b = checksum >>> 16;
61
		for (var i = 0, length = uint8Array.length, x; i < length; i++) {
62
			a = (a + uint8Array[i]) % MOD;
63
			b = (b + a) % MOD;
64
		}
65
		return (b << 16 | a) >>> 0
66
	};
67
 
68
	var exports = {};
69
 
70
	var Adler32 = exports.Adler32 = (function() {
71
		var ctor = function Adler32(checksum) {
72
			if (!(this instanceof ctor)) {
73
				throw new TypeError(
74
					'Constructor cannot called be as a function.');
75
			}
76
			if (!isFinite(checksum = checksum == null ? 1 : +checksum)) {
77
				throw new Error(
78
					'First arguments needs to be a finite number.');
79
			}
80
			this.checksum = checksum >>> 0;
81
		};
82
 
83
		var proto = ctor.prototype = {};
84
		proto.constructor = ctor;
85
 
86
		ctor.from = function(from) {
87
			from.prototype = proto;
88
			return from;
89
		}(function from(binaryString) {
90
			if (!(this instanceof ctor)) {
91
				throw new TypeError(
92
					'Constructor cannot called be as a function.');
93
			}
94
			if (binaryString == null)
95
				throw new Error('First argument needs to be a string.');
96
			this.checksum = _update(1, binaryString.toString());
97
		});
98
 
99
		ctor.fromUtf8 = function(fromUtf8) {
100
			fromUtf8.prototype = proto;
101
			return fromUtf8;
102
		}(function fromUtf8(utf8String) {
103
			if (!(this instanceof ctor)) {
104
				throw new TypeError(
105
					'Constructor cannot called be as a function.');
106
			}
107
			if (utf8String == null)
108
				throw new Error('First argument needs to be a string.');
109
			var binaryString = _utf8ToBinary(utf8String.toString());
110
			this.checksum = _update(1, binaryString);
111
		});
112
 
113
		if (_hasArrayBuffer) {
114
			ctor.fromBuffer = function(fromBuffer) {
115
				fromBuffer.prototype = proto;
116
				return fromBuffer;
117
			}(function fromBuffer(buffer) {
118
				if (!(this instanceof ctor)) {
119
					throw new TypeError(
120
						'Constructor cannot called be as a function.');
121
				}
122
				if (!_isBuffer(buffer))
123
					throw new Error('First argument needs to be ArrayBuffer.');
124
				var array = new Uint8Array(buffer);
125
				return this.checksum = _updateUint8Array(1, array);
126
			});
127
		}
128
 
129
		proto.update = function update(binaryString) {
130
			if (binaryString == null)
131
				throw new Error('First argument needs to be a string.');
132
			binaryString = binaryString.toString();
133
			return this.checksum = _update(this.checksum, binaryString);
134
		};
135
 
136
		proto.updateUtf8 = function updateUtf8(utf8String) {
137
			if (utf8String == null)
138
				throw new Error('First argument needs to be a string.');
139
			var binaryString = _utf8ToBinary(utf8String.toString());
140
			return this.checksum = _update(this.checksum, binaryString);
141
		};
142
 
143
		if (_hasArrayBuffer) {
144
			proto.updateBuffer = function updateBuffer(buffer) {
145
				if (!_isBuffer(buffer))
146
					throw new Error('First argument needs to be ArrayBuffer.');
147
				var array = new Uint8Array(buffer);
148
				return this.checksum = _updateUint8Array(this.checksum, array);
149
			};
150
		}
151
 
152
		proto.clone = function clone() {
153
			return new Adler32(this.checksum);
154
		};
155
 
156
		return ctor;
157
	}());
158
 
159
	exports.from = function from(binaryString) {
160
		if (binaryString == null)
161
			throw new Error('First argument needs to be a string.');
162
		return _update(1, binaryString.toString());
163
	};
164
 
165
	exports.fromUtf8 = function fromUtf8(utf8String) {
166
		if (utf8String == null)
167
			throw new Error('First argument needs to be a string.');
168
		var binaryString = _utf8ToBinary(utf8String.toString());
169
		return _update(1, binaryString);
170
	};
171
 
172
	if (_hasArrayBuffer) {
173
		exports.fromBuffer = function fromBuffer(buffer) {
174
			if (!_isBuffer(buffer))
175
				throw new Error('First argument need to be ArrayBuffer.');
176
			var array = new Uint8Array(buffer);
177
			return _updateUint8Array(1, array);
178
		};
179
	}
180
 
181
	return exports;
182
});