Commit c24e19a1 authored by 邓晓峰's avatar 邓晓峰

fix: utils decodemessage

parent 130e1683
......@@ -209,4 +209,4 @@ WireMessage.prototype.encode = function () {
return buffer;
};
export default WritableStream;
\ No newline at end of file
export default WireMessage;
\ No newline at end of file
......@@ -217,5 +217,5 @@ WireMessage.prototype.encode = function () {
return buffer;
};
var _default = WritableStream;
var _default = WireMessage;
exports.default = _default;
\ No newline at end of file
......@@ -3,6 +3,7 @@ import {
MESSAGE_TYPE,
} from './consts';
import Message from './Message';
import WireMessage from './WireMessage';
/* eslint-disable */
/**
......@@ -15,55 +16,51 @@ import Message from './Message';
* @throws {Error} Invalid option parameter found.
* @private
*/
export const validate = function(obj, keys) {
export const validate = function (obj, keys) {
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
if (keys.hasOwnProperty(key)) {
if (typeof obj[key] !== keys[key])
throw new Error(format(ERROR.INVALID_TYPE, [typeof obj[key], key]));
if (typeof obj[key] !== keys[key]) throw new Error(format(ERROR.INVALID_TYPE, [typeof obj[key], key]));
} else {
var errorStr = "Unknown property, " + key + ". Valid properties are:";
for (var validKey in keys)
if (keys.hasOwnProperty(validKey))
errorStr = errorStr+" "+validKey;
var errorStr = 'Unknown property, ' + key + '. Valid properties are:';
for (var validKey in keys) if (keys.hasOwnProperty(validKey)) errorStr = errorStr + ' ' + validKey;
throw new Error(errorStr);
}
}
}
};
};
/**
/**
* Format an error message text.
* @private
* @param {error} ERROR value above.
* @param {substitutions} [array] substituted into the text.
* @return the text with the substitutions made.
*/
export const format = function(error, substitutions) {
export const format = function (error, substitutions) {
var text = error.text;
if (substitutions) {
var field,start;
for (var i=0; i<substitutions.length; i++) {
field = "{"+i+"}";
var field, start;
for (var i = 0; i < substitutions.length; i++) {
field = '{' + i + '}';
start = text.indexOf(field);
if(start > 0) {
var part1 = text.substring(0,start);
var part2 = text.substring(start+field.length);
text = part1+substitutions[i]+part2;
if (start > 0) {
var part1 = text.substring(0, start);
var part2 = text.substring(start + field.length);
text = part1 + substitutions[i] + part2;
}
}
}
return text;
};
};
export function decodeMessage(input,pos) {
export function decodeMessage(input, pos) {
var startingPos = pos;
var first = input[pos];
var type = first >> 4;
var messageInfo = first &= 0x0f;
var messageInfo = (first &= 0x0f);
pos += 1;
// Decode the remaining length (MBI format)
var digit;
......@@ -71,24 +68,23 @@ import Message from './Message';
var multiplier = 1;
do {
if (pos == input.length) {
return [null,startingPos];
return [null, startingPos];
}
digit = input[pos++];
remLength += ((digit & 0x7F) * multiplier);
remLength += (digit & 0x7f) * multiplier;
multiplier *= 128;
} while ((digit & 0x80) !== 0);
var endPos = pos+remLength;
var endPos = pos + remLength;
if (endPos > input.length) {
return [null,startingPos];
return [null, startingPos];
}
var wireMessage = new WireMessage(type);
switch(type) {
switch (type) {
case MESSAGE_TYPE.CONNACK:
var connectAcknowledgeFlags = input[pos++];
if (connectAcknowledgeFlags & 0x01)
wireMessage.sessionPresent = true;
if (connectAcknowledgeFlags & 0x01) wireMessage.sessionPresent = true;
wireMessage.returnCode = input[pos++];
break;
......@@ -106,10 +102,8 @@ import Message from './Message';
}
var message = new Message(input.subarray(pos, endPos));
if ((messageInfo & 0x01) == 0x01)
message.retained = true;
if ((messageInfo & 0x08) == 0x08)
message.duplicate = true;
if ((messageInfo & 0x01) == 0x01) message.retained = true;
if ((messageInfo & 0x08) == 0x08) message.duplicate = true;
message.qos = qos;
message.destinationName = topicName;
wireMessage.payloadMessage = message;
......@@ -133,30 +127,30 @@ import Message from './Message';
break;
}
return [wireMessage,endPos];
}
return [wireMessage, endPos];
}
export function writeUint16(input, buffer, offset) {
export function writeUint16(input, buffer, offset) {
buffer[offset++] = input >> 8; //MSB
buffer[offset++] = input % 256; //LSB
return offset;
}
}
export function writeString(input, utf8Length, buffer, offset) {
export function writeString(input, utf8Length, buffer, offset) {
offset = writeUint16(utf8Length, buffer, offset);
stringToUTF8(input, buffer, offset);
return offset + utf8Length;
}
}
export function readUint16(buffer, offset) {
return 256*buffer[offset] + buffer[offset+1];
}
export function readUint16(buffer, offset) {
return 256 * buffer[offset] + buffer[offset + 1];
}
/**
/**
* Encodes an MQTT Multi-Byte Integer
* @private
*/
export function encodeMBI(number) {
export function encodeMBI(number) {
var output = new Array(1);
var numBytes = 0;
......@@ -167,126 +161,125 @@ import Message from './Message';
digit |= 0x80;
}
output[numBytes++] = digit;
} while ( (number > 0) && (numBytes<4) );
} while (number > 0 && numBytes < 4);
return output;
}
}
/**
/**
* Takes a String and calculates its length in bytes when encoded in UTF8.
* @private
*/
export function UTF8Length(input) {
export function UTF8Length(input) {
var output = 0;
for (var i = 0; i<input.length; i++)
{
for (var i = 0; i < input.length; i++) {
var charCode = input.charCodeAt(i);
if (charCode > 0x7FF)
{
if (charCode > 0x7ff) {
// Surrogate pair means its a 4 byte character
if (0xD800 <= charCode && charCode <= 0xDBFF)
{
if (0xd800 <= charCode && charCode <= 0xdbff) {
i++;
output++;
}
output +=3;
}
else if (charCode > 0x7F)
output +=2;
else
output++;
output += 3;
} else if (charCode > 0x7f) output += 2;
else output++;
}
return output;
}
}
/**
/**
* Takes a String and writes it into an array as UTF8 encoded bytes.
* @private
*/
export function stringToUTF8(input, output, start) {
export function stringToUTF8(input, output, start) {
var pos = start;
for (var i = 0; i<input.length; i++) {
for (var i = 0; i < input.length; i++) {
var charCode = input.charCodeAt(i);
// Check for a surrogate pair.
if (0xD800 <= charCode && charCode <= 0xDBFF) {
if (0xd800 <= charCode && charCode <= 0xdbff) {
var lowCharCode = input.charCodeAt(++i);
if (isNaN(lowCharCode)) {
throw new Error(format(ERROR.MALFORMED_UNICODE, [charCode, lowCharCode]));
}
charCode = ((charCode - 0xD800)<<10) + (lowCharCode - 0xDC00) + 0x10000;
charCode = ((charCode - 0xd800) << 10) + (lowCharCode - 0xdc00) + 0x10000;
}
if (charCode <= 0x7F) {
if (charCode <= 0x7f) {
output[pos++] = charCode;
} else if (charCode <= 0x7FF) {
output[pos++] = charCode>>6 & 0x1F | 0xC0;
output[pos++] = charCode & 0x3F | 0x80;
} else if (charCode <= 0xFFFF) {
output[pos++] = charCode>>12 & 0x0F | 0xE0;
output[pos++] = charCode>>6 & 0x3F | 0x80;
output[pos++] = charCode & 0x3F | 0x80;
} else if (charCode <= 0x7ff) {
output[pos++] = ((charCode >> 6) & 0x1f) | 0xc0;
output[pos++] = (charCode & 0x3f) | 0x80;
} else if (charCode <= 0xffff) {
output[pos++] = ((charCode >> 12) & 0x0f) | 0xe0;
output[pos++] = ((charCode >> 6) & 0x3f) | 0x80;
output[pos++] = (charCode & 0x3f) | 0x80;
} else {
output[pos++] = charCode>>18 & 0x07 | 0xF0;
output[pos++] = charCode>>12 & 0x3F | 0x80;
output[pos++] = charCode>>6 & 0x3F | 0x80;
output[pos++] = charCode & 0x3F | 0x80;
output[pos++] = ((charCode >> 18) & 0x07) | 0xf0;
output[pos++] = ((charCode >> 12) & 0x3f) | 0x80;
output[pos++] = ((charCode >> 6) & 0x3f) | 0x80;
output[pos++] = (charCode & 0x3f) | 0x80;
}
}
return output;
}
}
export function parseUTF8(input, offset, length) {
var output = "";
export function parseUTF8(input, offset, length) {
var output = '';
var utf16;
var pos = offset;
while (pos < offset+length)
{
while (pos < offset + length) {
var byte1 = input[pos++];
if (byte1 < 128)
utf16 = byte1;
else
{
var byte2 = input[pos++]-128;
if (byte2 < 0)
throw new Error(format(ERROR.MALFORMED_UTF, [byte1.toString(16), byte2.toString(16),""]));
if (byte1 < 0xE0) // 2 byte character
utf16 = 64*(byte1-0xC0) + byte2;
else
{
var byte3 = input[pos++]-128;
if (byte1 < 128) utf16 = byte1;
else {
var byte2 = input[pos++] - 128;
if (byte2 < 0) throw new Error(format(ERROR.MALFORMED_UTF, [byte1.toString(16), byte2.toString(16), '']));
if (byte1 < 0xe0)
// 2 byte character
utf16 = 64 * (byte1 - 0xc0) + byte2;
else {
var byte3 = input[pos++] - 128;
if (byte3 < 0)
throw new Error(format(ERROR.MALFORMED_UTF, [byte1.toString(16), byte2.toString(16), byte3.toString(16)]));
if (byte1 < 0xF0) // 3 byte character
utf16 = 4096*(byte1-0xE0) + 64*byte2 + byte3;
else
{
var byte4 = input[pos++]-128;
if (byte1 < 0xf0)
// 3 byte character
utf16 = 4096 * (byte1 - 0xe0) + 64 * byte2 + byte3;
else {
var byte4 = input[pos++] - 128;
if (byte4 < 0)
throw new Error(format(ERROR.MALFORMED_UTF, [byte1.toString(16), byte2.toString(16), byte3.toString(16), byte4.toString(16)]));
if (byte1 < 0xF8) // 4 byte character
utf16 = 262144*(byte1-0xF0) + 4096*byte2 + 64*byte3 + byte4;
else // longer encodings are not supported
throw new Error(format(ERROR.MALFORMED_UTF, [byte1.toString(16), byte2.toString(16), byte3.toString(16), byte4.toString(16)]));
throw new Error(
format(ERROR.MALFORMED_UTF, [
byte1.toString(16),
byte2.toString(16),
byte3.toString(16),
byte4.toString(16),
]),
);
if (byte1 < 0xf8)
// 4 byte character
utf16 = 262144 * (byte1 - 0xf0) + 4096 * byte2 + 64 * byte3 + byte4;
// longer encodings are not supported
else
throw new Error(
format(ERROR.MALFORMED_UTF, [
byte1.toString(16),
byte2.toString(16),
byte3.toString(16),
byte4.toString(16),
]),
);
}
}
}
if (utf16 > 0xFFFF) // 4 byte character - express as a surrogate pair
{
if (utf16 > 0xffff) {
// 4 byte character - express as a surrogate pair
utf16 -= 0x10000;
output += String.fromCharCode(0xD800 + (utf16 >> 10)); // lead character
utf16 = 0xDC00 + (utf16 & 0x3FF); // trail character
output += String.fromCharCode(0xd800 + (utf16 >> 10)); // lead character
utf16 = 0xdc00 + (utf16 & 0x3ff); // trail character
}
output += String.fromCharCode(utf16);
}
return output;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment