forked from kam66be/jSEND
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsend-2.0.0.source.js
executable file
·246 lines (242 loc) · 8.64 KB
/
jsend-2.0.0.source.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/* -----------------------------------------------------------------------------
* jSEND v2.0.0
* -----------------------------------------------------------------------------
* Date: Fri Sep 24 19:35:11 2010 +0100
*
* Summary: This plugin provides compression & binary-to-text encoding
* for use in XMLHTTPRequest/AJAX post requests
*
* Author: Michael Kortstiege, Copyright 2010
* Website: http://jsend.org/
*
* License: Dual licensed under the MIT or GPL Version 2 licenses.
* (http://jsend.org/license/)
*
* Credits: See http://jsend.org/about/
*
* -----------------------------------------------------------------------------
* USAGE
* -----------------------------------------------------------------------------
* X/HTML <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"><script>
* <script type="text/javascript" src="jsend.min.js"></script>
*
* JS var str = "String to Squeeze, Encode & Deliver";
* var data = $.jSEND(str);
* // Send data to server
* -----------------------------------------------------------------------------
*/
(function($)
{
$.jSEND = function(sData)
{
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
MAIN SQUEEZE
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
sData = String.fromCharCode(74)+sData;
/* --------
Init
-------- */
var iCount = 256;
var sFill = '';
var iFillCount = 0;
var iEmptyCount = 0;
for (var i = 0; i < iCount; i++)
sFill += String.fromCharCode(224);
var iDictSize = iCount;
var oDictionary = {};
for (var i = 0; i < iCount; i++)
oDictionary[String.fromCharCode(i)] = i;
var aCodes = [];
var sData2 = ''; //String.fromCharCode(224);
var sPattern = '';
/* --------
Go
-------- */
for (var i = 0, iLn = sData.length; i < iLn; i++)
{
var sChar = sData.charAt(i);
var iCode = sChar.charCodeAt(0);
/* --------------------
Handle UCS Chars
-------------------- */
if (iCode > 255) {
/* ------------------------------------------------------------
Replace some UCS Chars with their ANSI (128-159) pendants
------------------------------------------------------------ */
var iChk = iCode;
switch (iCode) {
case 8364: iCode = 128; break; case 8218: iCode = 130; break; case 402: iCode = 131; break;
case 8222: iCode = 132; break; case 8230: iCode = 133; break; case 8224: iCode = 134; break;
case 8225: iCode = 135; break; case 710: iCode = 136; break; case 8240: iCode = 137; break;
case 352: iCode = 138; break; case 8249: iCode = 139; break; case 338: iCode = 140; break;
case 381: iCode = 142; break; case 8216: iCode = 145; break; case 8217: iCode = 146; break;
case 8220: iCode = 147; break; case 8221: iCode = 148; break; case 8226: iCode = 149; break;
case 8211: iCode = 150; break; case 8212: iCode = 151; break; case 732: iCode = 152; break;
case 8482: iCode = 153; break; case 353: iCode = 154; break; case 8250: iCode = 155; break;
case 339: iCode = 156; break; case 382: iCode = 158; break; case 376: iCode = 159; break;
}
if (iChk != iCode) {
sChar = String.fromCharCode(iCode);
iEmptyCount++;
iFillCount++;
if (iFillCount >= iCount) {
sData2 += sFill;
iFillCount = 0;
}
}
else {
if (iFillCount > 0) {
sData2 += sFill.substr(0,iFillCount);
iFillCount = 0;
}
sData2 += String.fromCharCode(parseInt(iCode / 256));
}
}
/* ---------------------------
Handle ASCII/ANSI Chars
--------------------------- */
else {
iEmptyCount++;
iFillCount++;
if (iFillCount >= iCount) {
sData2 += sFill;
iFillCount = 0;
}
}
/* -------------------------
Start LZW Compression
------------------------- */
var sCombined = sPattern + sChar;
if (oDictionary[sCombined])
sPattern = sCombined;
else
{
if (iCode > 255)
sChar = String.fromCharCode(iCode % 256);
aCodes.push(oDictionary[sPattern]);
oDictionary[sCombined] = iDictSize++;
sPattern = '' + sChar;
}
/* -----------------------
End LZW Compression
----------------------- */
}
/* ----------
Flush
---------- */
if (sPattern != '')
aCodes.push(oDictionary[sPattern]);
if (iFillCount > 0)
sData2 += sFill.substr(0,iFillCount);
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SUB-SQUEEZE & DOUBLE ENCODING
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
var sChars = ecode847(encodeBinary(aCodes));
if (sData.length != iEmptyCount)
{
var sChars2 = ecode847(encodeBinary(compressLZW(sData2)));
return sChars + '==' + sChars2;
}
else
return sChars;
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
FUNCTIONS
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
/* ------------------
LZW Compressor
------------------ */
function compressLZW(sData)
{
var aCodes = [];
var iDictSize = 256;
var oDictionary = {};
for (var i = 0; i < 256; i++)
oDictionary[String.fromCharCode(i)] = i;
var sPattern = '';
for (var i = 0, iLn = sData.length; i < iLn; i++)
{
var sChar = sData.charAt(i);
var sCombined = sPattern + sChar;
if (oDictionary[sCombined])
sPattern = sCombined;
else
{
aCodes.push(oDictionary[sPattern]);
oDictionary[sCombined] = iDictSize++;
sPattern = '' + sChar;
}
}
if (sPattern != '')
aCodes.push(oDictionary[sPattern]);
return aCodes;
}
/* ------------------
Binary Encoder
------------------ */
function encodeBinary(aCodes)
{
var iDictCount = 256;
var aCharCodes = [];
var iBits = 8;
var iRest = 0;
var iRestLength = 0;
for(var i=0, iLn = aCodes.length; i < iLn; i++)
{
iRest = (iRest << iBits) + aCodes[i];
iRestLength += iBits;
iDictCount++;
if (iDictCount >> iBits)
iBits++;
while (iRestLength > 7)
{
iRestLength -= 8;
aCharCodes.push(iRest >> iRestLength);
iRest &= (1 << iRestLength) - 1;
}
}
aCharCodes.push(iRestLength ? iRest << (8 - iRestLength) : '');
return aCharCodes;
}
/* ------------------
847enc Encoder
------------------ */
function ecode847(aCharCodes)
{
var aTmp = [];
var iCount = 0;
var iChar = 0;
var sChars = '';
for(var i=0, iLn = aCharCodes.length; i < iLn; i++)
{
var iValue = aCharCodes[i];
if (iValue > 127) {
iValue -= 128;
iChar += Math.pow(2,iCount);
}
if (iValue == 0 || iValue == 34 || iValue == 37 || iValue == 38 ||
iValue == 39 || iValue == 43 || iValue == 61 || iValue == 92)
aTmp.push('='+String.fromCharCode((iValue+16)));
else
aTmp.push(String.fromCharCode(iValue));
iCount++;
if (iCount > 6)
{
if (iChar == 0 || iChar == 34 || iChar == 37 || iChar == 38 ||
iChar == 39 || iChar == 43 || iChar == 61 || iChar == 92)
sChars += ('=' + String.fromCharCode((iChar+16)) + aTmp.join(''));
else
sChars += (String.fromCharCode(iChar) + aTmp.join(''));
aTmp = [];
iChar = 0;
iCount = 0;
}
}
if (iChar == 0 || iChar == 34 || iChar == 37 || iChar == 38 ||
iChar == 39 || iChar == 43 || iChar == 61 || iChar == 92)
sChars += ('=' + String.fromCharCode((iChar+16)) + aTmp.join(''));
else
sChars += (String.fromCharCode(iChar) + aTmp.join(''));
return sChars;
}
}
})(jQuery);