UTF8.js
4.9 KB
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
// UTF8 : Manage UTF-8 strings in ArrayBuffers
if(module.require) {
require('string.fromcodepoint');
require('string.prototype.codepointat');
}
var UTF8={
// non UTF8 encoding detection (cf README file for details)
'isNotUTF8': function(bytes, byteOffset, byteLength) {
try {
UTF8.getStringFromBytes(bytes, byteOffset, byteLength, true);
} catch(e) {
return true;
}
return false;
},
// UTF8 decoding functions
'getCharLength': function(theByte) {
// 4 bytes encoded char (mask 11110000)
if(0xF0 == (theByte&0xF0)) {
return 4;
// 3 bytes encoded char (mask 11100000)
} else if(0xE0 == (theByte&0xE0)) {
return 3;
// 2 bytes encoded char (mask 11000000)
} else if(0xC0 == (theByte&0xC0)) {
return 2;
// 1 bytes encoded char
} else if(theByte == (theByte&0x7F)) {
return 1;
}
return 0;
},
'getCharCode': function(bytes, byteOffset, charLength) {
var charCode = 0, mask = '';
byteOffset = byteOffset || 0;
// Retrieve charLength if not given
charLength = charLength || UTF8.getCharLength(bytes[byteOffset]);
if(charLength == 0) {
throw new Error(bytes[byteOffset].toString(2)+' is not a significative' +
' byte (offset:'+byteOffset+').');
}
// Return byte value if charlength is 1
if(1 === charLength) {
return bytes[byteOffset];
}
// Test UTF8 integrity
mask = '00000000'.slice(0, charLength) + 1 + '00000000'.slice(charLength + 1);
if(bytes[byteOffset]&(parseInt(mask, 2))) {
throw Error('Index ' + byteOffset + ': A ' + charLength + ' bytes' +
' encoded char' +' cannot encode the '+(charLength+1)+'th rank bit to 1.');
}
// Reading the first byte
mask='0000'.slice(0,charLength+1)+'11111111'.slice(charLength+1);
charCode+=(bytes[byteOffset]&parseInt(mask,2))<<((--charLength)*6);
// Reading the next bytes
while(charLength) {
if(0x80!==(bytes[byteOffset+1]&0x80)
||0x40===(bytes[byteOffset+1]&0x40)) {
throw Error('Index '+(byteOffset+1)+': Next bytes of encoded char'
+' must begin with a "10" bit sequence.');
}
charCode += ((bytes[++byteOffset]&0x3F) << ((--charLength) * 6));
}
return charCode;
},
'getStringFromBytes': function(bytes, byteOffset, byteLength, strict) {
var charLength, chars = [];
byteOffset = byteOffset|0;
byteLength=('number' === typeof byteLength ?
byteLength :
bytes.byteLength || bytes.length
);
for(; byteOffset < byteLength; byteOffset++) {
charLength = UTF8.getCharLength(bytes[byteOffset]);
if(byteOffset + charLength > byteLength) {
if(strict) {
throw Error('Index ' + byteOffset + ': Found a ' + charLength +
' bytes encoded char declaration but only ' +
(byteLength - byteOffset) +' bytes are available.');
}
} else {
chars.push(String.fromCodePoint(
UTF8.getCharCode(bytes, byteOffset, charLength, strict)
));
}
byteOffset += charLength - 1;
}
return chars.join('');
},
// UTF8 encoding functions
'getBytesForCharCode': function(charCode) {
if(charCode < 128) {
return 1;
} else if(charCode < 2048) {
return 2;
} else if(charCode < 65536) {
return 3;
} else if(charCode < 2097152) {
return 4;
}
throw new Error('CharCode '+charCode+' cannot be encoded with UTF8.');
},
'setBytesFromCharCode': function(charCode, bytes, byteOffset, neededBytes) {
charCode = charCode|0;
bytes = bytes || [];
byteOffset = byteOffset|0;
neededBytes = neededBytes || UTF8.getBytesForCharCode(charCode);
// Setting the charCode as it to bytes if the byte length is 1
if(1 == neededBytes) {
bytes[byteOffset] = charCode;
} else {
// Computing the first byte
bytes[byteOffset++] =
(parseInt('1111'.slice(0, neededBytes), 2) << 8 - neededBytes) +
(charCode >>> ((--neededBytes) * 6));
// Computing next bytes
for(;neededBytes>0;) {
bytes[byteOffset++] = ((charCode>>>((--neededBytes) * 6))&0x3F)|0x80;
}
}
return bytes;
},
'setBytesFromString': function(string, bytes, byteOffset, byteLength, strict) {
string = string || '';
bytes = bytes || [];
byteOffset = byteOffset|0;
byteLength = ('number' === typeof byteLength ?
byteLength :
bytes.byteLength||Infinity
);
for(var i = 0, j = string.length; i < j; i++) {
var neededBytes = UTF8.getBytesForCharCode(string[i].codePointAt(0));
if(strict && byteOffset + neededBytes > byteLength) {
throw new Error('Not enought bytes to encode the char "' + string[i] +
'" at the offset "' + byteOffset + '".');
}
UTF8.setBytesFromCharCode(string[i].codePointAt(0),
bytes, byteOffset, neededBytes, strict);
byteOffset += neededBytes;
}
return bytes;
}
};
if('undefined' !== typeof module) {
module.exports = UTF8;
}