dom/voicemail/test/marionette/pdu_builder.js

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /* Any copyright is dedicated to the Public Domain.
michael@0 2 * http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 3 "use strict";
michael@0 4
michael@0 5 let RIL = {};
michael@0 6 SpecialPowers.Cu.import("resource://gre/modules/ril_consts.js", RIL);
michael@0 7
michael@0 8 // Only bring in what we need from ril_worker/RadioInterfaceLayer here. Reusing
michael@0 9 // that code turns out to be a nightmare, so there is some code duplication.
michael@0 10 let PDUBuilder = {
michael@0 11 toHexString: function(n, length) {
michael@0 12 let str = n.toString(16);
michael@0 13 if (str.length < length) {
michael@0 14 for (let i = 0; i < length - str.length; i++) {
michael@0 15 str = "0" + str;
michael@0 16 }
michael@0 17 }
michael@0 18 return str.toUpperCase();
michael@0 19 },
michael@0 20
michael@0 21 writeUint16: function(value) {
michael@0 22 this.buf += (value & 0xff).toString(16).toUpperCase();
michael@0 23 this.buf += ((value >> 8) & 0xff).toString(16).toUpperCase();
michael@0 24 },
michael@0 25
michael@0 26 writeHexOctet: function(octet) {
michael@0 27 this.buf += this.toHexString(octet, 2);
michael@0 28 },
michael@0 29
michael@0 30 writeSwappedNibbleBCD: function(data) {
michael@0 31 data = data.toString();
michael@0 32 let zeroCharCode = '0'.charCodeAt(0);
michael@0 33
michael@0 34 for (let i = 0; i < data.length; i += 2) {
michael@0 35 let low = data.charCodeAt(i) - zeroCharCode;
michael@0 36 let high;
michael@0 37 if (i + 1 < data.length) {
michael@0 38 high = data.charCodeAt(i + 1) - zeroCharCode;
michael@0 39 } else {
michael@0 40 high = 0xF;
michael@0 41 }
michael@0 42
michael@0 43 this.writeHexOctet((high << 4) | low);
michael@0 44 }
michael@0 45 },
michael@0 46
michael@0 47 writeStringAsSeptets: function(message, paddingBits, langIndex,
michael@0 48 langShiftIndex) {
michael@0 49 const langTable = RIL.PDU_NL_LOCKING_SHIFT_TABLES[langIndex];
michael@0 50 const langShiftTable = RIL.PDU_NL_SINGLE_SHIFT_TABLES[langShiftIndex];
michael@0 51
michael@0 52 let dataBits = paddingBits;
michael@0 53 let data = 0;
michael@0 54 for (let i = 0; i < message.length; i++) {
michael@0 55 let septet = langTable.indexOf(message[i]);
michael@0 56 if (septet == RIL.PDU_NL_EXTENDED_ESCAPE) {
michael@0 57 continue;
michael@0 58 }
michael@0 59
michael@0 60 if (septet >= 0) {
michael@0 61 data |= septet << dataBits;
michael@0 62 dataBits += 7;
michael@0 63 } else {
michael@0 64 septet = langShiftTable.indexOf(message[i]);
michael@0 65 if (septet == -1) {
michael@0 66 throw new Error(message[i] + " not in 7 bit alphabet "
michael@0 67 + langIndex + ":" + langShiftIndex + "!");
michael@0 68 }
michael@0 69
michael@0 70 if (septet == RIL.PDU_NL_RESERVED_CONTROL) {
michael@0 71 continue;
michael@0 72 }
michael@0 73
michael@0 74 data |= RIL.PDU_NL_EXTENDED_ESCAPE << dataBits;
michael@0 75 dataBits += 7;
michael@0 76 data |= septet << dataBits;
michael@0 77 dataBits += 7;
michael@0 78 }
michael@0 79
michael@0 80 for (; dataBits >= 8; dataBits -= 8) {
michael@0 81 this.writeHexOctet(data & 0xFF);
michael@0 82 data >>>= 8;
michael@0 83 }
michael@0 84 }
michael@0 85
michael@0 86 if (dataBits != 0) {
michael@0 87 this.writeHexOctet(data & 0xFF);
michael@0 88 }
michael@0 89 },
michael@0 90
michael@0 91 buildAddress: function(address) {
michael@0 92 let addressFormat = RIL.PDU_TOA_ISDN; // 81
michael@0 93 if (address[0] == '+') {
michael@0 94 addressFormat = RIL.PDU_TOA_INTERNATIONAL | RIL.PDU_TOA_ISDN; // 91
michael@0 95 address = address.substring(1);
michael@0 96 }
michael@0 97
michael@0 98 this.buf = "";
michael@0 99 this.writeHexOctet(address.length);
michael@0 100 this.writeHexOctet(addressFormat);
michael@0 101 this.writeSwappedNibbleBCD(address);
michael@0 102
michael@0 103 return this.buf;
michael@0 104 },
michael@0 105
michael@0 106 // assumes 7 bit encoding
michael@0 107 buildUserData: function(options) {
michael@0 108 let headerLength = 0;
michael@0 109 this.buf = "";
michael@0 110 if (options.headers) {
michael@0 111 for each (let header in options.headers) {
michael@0 112 headerLength += 2; // id + length octets
michael@0 113 if (header.octets) {
michael@0 114 headerLength += header.octets.length;
michael@0 115 }
michael@0 116 };
michael@0 117 }
michael@0 118
michael@0 119 let encodedBodyLength = options.body.length;
michael@0 120 let headerOctets = (headerLength ? headerLength + 1 : 0);
michael@0 121
michael@0 122 let paddingBits;
michael@0 123 let userDataLengthInSeptets;
michael@0 124 let headerSeptets = Math.ceil(headerOctets * 8 / 7);
michael@0 125 userDataLengthInSeptets = headerSeptets + encodedBodyLength;
michael@0 126 paddingBits = headerSeptets * 7 - headerOctets * 8;
michael@0 127
michael@0 128 this.writeHexOctet(userDataLengthInSeptets);
michael@0 129 if (options.headers) {
michael@0 130 this.writeHexOctet(headerLength);
michael@0 131
michael@0 132 for each (let header in options.headers) {
michael@0 133 this.writeHexOctet(header.id);
michael@0 134 this.writeHexOctet(header.length);
michael@0 135
michael@0 136 if (header.octets) {
michael@0 137 for each (let octet in header.octets) {
michael@0 138 this.writeHexOctet(octet);
michael@0 139 }
michael@0 140 }
michael@0 141 }
michael@0 142 }
michael@0 143
michael@0 144 this.writeStringAsSeptets(options.body, paddingBits,
michael@0 145 RIL.PDU_NL_IDENTIFIER_DEFAULT,
michael@0 146 RIL.PDU_NL_IDENTIFIER_DEFAULT);
michael@0 147 return this.buf;
michael@0 148 }
michael@0 149 };

mercurial