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

fix: utils decodemessage

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