Bug 753034: add readSwappedNibbleBcdString for complex GSM address. r=philikon

This commit is contained in:
Vicamo Yang 2012-05-24 15:38:33 -07:00
Родитель bf52b7f8c6
Коммит 9fa8cd8aa9
1 изменённых файлов: 53 добавлений и 11 удалений

Просмотреть файл

@ -3326,17 +3326,29 @@ let GsmPDUHelper = {
((octet & 0x0f) <= 0x09) * (octet & 0x0f) * 10;
},
/**
* Convert a semi-octet (number) to a GSM BCD char.
*/
bcdChars: "0123456789*#,;",
semiOctetToBcdChar: function semiOctetToBcdChar(semiOctet) {
if (semiOctet >= 14) {
throw new RangeError();
}
return this.bcdChars.charAt(semiOctet);
},
/**
* Read a *swapped nibble* binary coded decimal (BCD)
*
* @param length
* @param pairs
* Number of nibble *pairs* to read.
*
* @return the decimal as a number.
*/
readSwappedNibbleBCD: function readSwappedNibbleBCD(length) {
readSwappedNibbleBcdNum: function readSwappedNibbleBcdNum(pairs) {
let number = 0;
for (let i = 0; i < length; i++) {
for (let i = 0; i < pairs; i++) {
let octet = this.readHexOctet();
// Ignore 'ff' octets as they're often used as filler.
if (octet == 0xff) {
@ -3355,6 +3367,32 @@ let GsmPDUHelper = {
return number;
},
/**
* Read a *swapped nibble* binary coded string (BCD)
*
* @param pairs
* Number of nibble *pairs* to read.
*
* @return The BCD string.
*/
readSwappedNibbleBcdString: function readSwappedNibbleBcdString(pairs) {
let str = "";
for (let i = 0; i < pairs; i++) {
let nibbleH = this.readHexNibble();
let nibbleL = this.readHexNibble();
if (nibbleL == 0x0F) {
break;
}
str += this.semiOctetToBcdChar(nibbleL);
if (nibbleH != 0x0F) {
str += this.semiOctetToBcdChar(nibbleH);
}
}
return str;
},
/**
* Write numerical data as swapped nibble BCD.
*
@ -3720,6 +3758,10 @@ let GsmPDUHelper = {
/**
* Read SM-TL Address.
*
* @param len
* Length of useful semi-octets within the Address-Value field. For
* example, the lenth of "12345" should be 5, and 4 for "1234".
*
* @see 3GPP TS 23.040 9.1.2.5
*/
readAddress: function readAddress(len) {
@ -3737,7 +3779,7 @@ let GsmPDUHelper = {
let toa = this.readHexOctet();
// Address-Value
let addr = this.readSwappedNibbleBCD(len / 2).toString();
let addr = this.readSwappedNibbleBcdString(len / 2);
if (addr.length <= 0) {
if (DEBUG) debug("PDU error: no number provided");
return null;
@ -3909,12 +3951,12 @@ let GsmPDUHelper = {
* @see 3GPP TS 23.040 9.2.3.11
*/
readTimestamp: function readTimestamp() {
let year = this.readSwappedNibbleBCD(1) + PDU_TIMESTAMP_YEAR_OFFSET;
let month = this.readSwappedNibbleBCD(1) - 1;
let day = this.readSwappedNibbleBCD(1);
let hour = this.readSwappedNibbleBCD(1);
let minute = this.readSwappedNibbleBCD(1);
let second = this.readSwappedNibbleBCD(1);
let year = this.readSwappedNibbleBcdNum(1) + PDU_TIMESTAMP_YEAR_OFFSET;
let month = this.readSwappedNibbleBcdNum(1) - 1;
let day = this.readSwappedNibbleBcdNum(1);
let hour = this.readSwappedNibbleBcdNum(1);
let minute = this.readSwappedNibbleBcdNum(1);
let second = this.readSwappedNibbleBcdNum(1);
let timestamp = Date.UTC(year, month, day, hour, minute, second);
// If the most significant bit of the least significant nibble is 1,
@ -4064,7 +4106,7 @@ let GsmPDUHelper = {
if (smscLength > 0) {
let smscTypeOfAddress = this.readHexOctet();
// Subtract the type-of-address octet we just read from the length.
msg.SMSC = this.readSwappedNibbleBCD(smscLength - 1).toString();
msg.SMSC = this.readSwappedNibbleBcdString(smscLength - 1);
if ((smscTypeOfAddress >> 4) == (PDU_TOA_INTERNATIONAL >> 4)) {
msg.SMSC = '+' + msg.SMSC;
}