Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | "use strict"; |
michael@0 | 6 | |
michael@0 | 7 | const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components; |
michael@0 | 8 | |
michael@0 | 9 | let WSP = {}; |
michael@0 | 10 | Cu.import("resource://gre/modules/WspPduHelper.jsm", WSP); |
michael@0 | 11 | let WBXML = {}; |
michael@0 | 12 | Cu.import("resource://gre/modules/WbxmlPduHelper.jsm", WBXML); |
michael@0 | 13 | |
michael@0 | 14 | // set to true to see debug messages |
michael@0 | 15 | let DEBUG = WBXML.DEBUG_ALL | false; |
michael@0 | 16 | |
michael@0 | 17 | /** |
michael@0 | 18 | * Public identifier for SI |
michael@0 | 19 | * |
michael@0 | 20 | * @see http://technical.openmobilealliance.org/tech/omna/omna-wbxml-public-docid.aspx |
michael@0 | 21 | */ |
michael@0 | 22 | const PUBLIC_IDENTIFIER_SI = "-//WAPFORUM//DTD SI 1.0//EN"; |
michael@0 | 23 | |
michael@0 | 24 | /** |
michael@0 | 25 | * Parse inline date encoded in OPAQUE format. |
michael@0 | 26 | * |
michael@0 | 27 | * @param data |
michael@0 | 28 | * A wrapped object containing raw PDU data. |
michael@0 | 29 | * @param msg |
michael@0 | 30 | * Target object for decoding. |
michael@0 | 31 | * |
michael@0 | 32 | * @see WAP-167-SERVICEIND-20010731-A, clause 8.2.2 |
michael@0 | 33 | * |
michael@0 | 34 | */ |
michael@0 | 35 | this.OpaqueAsDate = { |
michael@0 | 36 | decode: function decode_opaque_as_date(data) { |
michael@0 | 37 | let size = WSP.UintVar.decode(data); |
michael@0 | 38 | let dateBuf = [0, 0, 0, 0, 0, 0, 0]; |
michael@0 | 39 | |
michael@0 | 40 | // Maximum length for date is 7 bytes. |
michael@0 | 41 | if (size > dateBuf.length) |
michael@0 | 42 | size = dateBuf.length |
michael@0 | 43 | |
michael@0 | 44 | // Read date date, non-specified parts remain 0. |
michael@0 | 45 | for (let i = 0; i < size; i++) { |
michael@0 | 46 | dateBuf[i] = WSP.Octet.decode(data); |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | // Decode and return result in "YYYY-MM-DDThh:mm:ssZ" form |
michael@0 | 50 | let year = ((dateBuf[0] >> 4) & 0x0F) * 1000 + (dateBuf[0] & 0x0F) * 100 + |
michael@0 | 51 | ((dateBuf[1] >> 4) & 0x0F) * 10 + (dateBuf[1] & 0x0F); |
michael@0 | 52 | let month = ((dateBuf[2] >> 4) & 0x0F) * 10 + (dateBuf[2] & 0x0F); |
michael@0 | 53 | let date = ((dateBuf[3] >> 4) & 0x0F) * 10 + (dateBuf[3] & 0x0F); |
michael@0 | 54 | let hour = ((dateBuf[4] >> 4) & 0x0F) * 10 + (dateBuf[4] & 0x0F); |
michael@0 | 55 | let minute = ((dateBuf[5] >> 4) & 0x0F) * 10 + (dateBuf[5] & 0x0F); |
michael@0 | 56 | let second = ((dateBuf[6] >> 4) & 0x0F) * 10 + (dateBuf[6] & 0x0F); |
michael@0 | 57 | let dateValue = new Date(Date.UTC(year, month - 1, date, hour, minute, second)); |
michael@0 | 58 | |
michael@0 | 59 | return dateValue.toISOString().replace(".000", ""); |
michael@0 | 60 | }, |
michael@0 | 61 | }; |
michael@0 | 62 | |
michael@0 | 63 | this.PduHelper = { |
michael@0 | 64 | |
michael@0 | 65 | /** |
michael@0 | 66 | * @param data |
michael@0 | 67 | * A wrapped object containing raw PDU data. |
michael@0 | 68 | * @param contentType |
michael@0 | 69 | * Content type of incoming SI message, should be "text/vnd.wap.si" or |
michael@0 | 70 | * "application/vnd.wap.sic". |
michael@0 | 71 | * |
michael@0 | 72 | * @return A message object containing attribute content and contentType. |
michael@0 | 73 | * |content| will contain string of decoded SI message if successfully |
michael@0 | 74 | * decoded, or raw data if failed. |
michael@0 | 75 | * |contentType| will be string representing corresponding type of |
michael@0 | 76 | * content. |
michael@0 | 77 | */ |
michael@0 | 78 | parse: function parse_si(data, contentType) { |
michael@0 | 79 | // We only need content and contentType |
michael@0 | 80 | let msg = { |
michael@0 | 81 | contentType: contentType |
michael@0 | 82 | }; |
michael@0 | 83 | |
michael@0 | 84 | /** |
michael@0 | 85 | * Message is compressed by WBXML, decode into string. |
michael@0 | 86 | * |
michael@0 | 87 | * @see WAP-192-WBXML-20010725-A |
michael@0 | 88 | */ |
michael@0 | 89 | if (contentType === "application/vnd.wap.sic") { |
michael@0 | 90 | let globalTokenOverride = {}; |
michael@0 | 91 | globalTokenOverride[0xC3] = { |
michael@0 | 92 | number: 0xC3, |
michael@0 | 93 | coder: OpaqueAsDate |
michael@0 | 94 | }; |
michael@0 | 95 | |
michael@0 | 96 | let appToken = { |
michael@0 | 97 | publicId: PUBLIC_IDENTIFIER_SI, |
michael@0 | 98 | tagTokenList: SI_TAG_FIELDS, |
michael@0 | 99 | attrTokenList: SI_ATTRIBUTE_FIELDS, |
michael@0 | 100 | valueTokenList: SI_VALUE_FIELDS, |
michael@0 | 101 | globalTokenOverride: globalTokenOverride |
michael@0 | 102 | } |
michael@0 | 103 | |
michael@0 | 104 | try { |
michael@0 | 105 | let parseResult = WBXML.PduHelper.parse(data, appToken); |
michael@0 | 106 | msg.content = parseResult.content; |
michael@0 | 107 | msg.contentType = "text/vnd.wap.si"; |
michael@0 | 108 | } catch (e) { |
michael@0 | 109 | // Provide raw data if we failed to parse. |
michael@0 | 110 | msg.content = data.array; |
michael@0 | 111 | } |
michael@0 | 112 | return msg; |
michael@0 | 113 | } |
michael@0 | 114 | |
michael@0 | 115 | /** |
michael@0 | 116 | * Message is plain text, transform raw to string. |
michael@0 | 117 | */ |
michael@0 | 118 | try { |
michael@0 | 119 | let stringData = WSP.Octet.decodeMultiple(data, data.array.length); |
michael@0 | 120 | msg.content = WSP.PduHelper.decodeStringContent(stringData, "UTF-8"); |
michael@0 | 121 | } catch (e) { |
michael@0 | 122 | // Provide raw data if we failed to parse. |
michael@0 | 123 | msg.content = data.array; |
michael@0 | 124 | } |
michael@0 | 125 | return msg; |
michael@0 | 126 | |
michael@0 | 127 | } |
michael@0 | 128 | }; |
michael@0 | 129 | |
michael@0 | 130 | /** |
michael@0 | 131 | * Tag tokens |
michael@0 | 132 | * |
michael@0 | 133 | * @see WAP-167-SERVICEIND-20010731-A, clause 8.3.1 |
michael@0 | 134 | */ |
michael@0 | 135 | const SI_TAG_FIELDS = (function () { |
michael@0 | 136 | let names = {}; |
michael@0 | 137 | function add(name, codepage, number) { |
michael@0 | 138 | let entry = { |
michael@0 | 139 | name: name, |
michael@0 | 140 | number: number, |
michael@0 | 141 | }; |
michael@0 | 142 | if (!names[codepage]) { |
michael@0 | 143 | names[codepage] = {}; |
michael@0 | 144 | } |
michael@0 | 145 | names[codepage][number] = entry; |
michael@0 | 146 | } |
michael@0 | 147 | |
michael@0 | 148 | add("si", 0, 0x05); |
michael@0 | 149 | add("indication", 0, 0x06); |
michael@0 | 150 | add("info", 0, 0x07); |
michael@0 | 151 | add("item", 0, 0x08); |
michael@0 | 152 | |
michael@0 | 153 | return names; |
michael@0 | 154 | })(); |
michael@0 | 155 | |
michael@0 | 156 | /** |
michael@0 | 157 | * Attribute Tokens |
michael@0 | 158 | * |
michael@0 | 159 | * @see WAP-167-SERVICEIND-20010731-A, clause 8.3.2 |
michael@0 | 160 | */ |
michael@0 | 161 | const SI_ATTRIBUTE_FIELDS = (function () { |
michael@0 | 162 | let names = {}; |
michael@0 | 163 | function add(name, value, codepage, number) { |
michael@0 | 164 | let entry = { |
michael@0 | 165 | name: name, |
michael@0 | 166 | value: value, |
michael@0 | 167 | number: number, |
michael@0 | 168 | }; |
michael@0 | 169 | if (!names[codepage]) { |
michael@0 | 170 | names[codepage] = {}; |
michael@0 | 171 | } |
michael@0 | 172 | names[codepage][number] = entry; |
michael@0 | 173 | } |
michael@0 | 174 | |
michael@0 | 175 | add("action", "signal-none", 0, 0x05); |
michael@0 | 176 | add("action", "signal-low", 0, 0x06); |
michael@0 | 177 | add("action", "signal-medium", 0, 0x07); |
michael@0 | 178 | add("action", "signal-high", 0, 0x08); |
michael@0 | 179 | add("action", "delete", 0, 0x09); |
michael@0 | 180 | add("created", "", 0, 0x0A); |
michael@0 | 181 | add("href", "", 0, 0x0B); |
michael@0 | 182 | add("href", "http://", 0, 0x0C); |
michael@0 | 183 | add("href", "http://www.", 0, 0x0D); |
michael@0 | 184 | add("href", "https://", 0, 0x0E); |
michael@0 | 185 | add("href", "https://www.", 0, 0x0F); |
michael@0 | 186 | add("si-expires", "", 0, 0x10); |
michael@0 | 187 | add("si-id", "", 0, 0x11); |
michael@0 | 188 | add("class", "", 0, 0x12); |
michael@0 | 189 | |
michael@0 | 190 | return names; |
michael@0 | 191 | })(); |
michael@0 | 192 | |
michael@0 | 193 | const SI_VALUE_FIELDS = (function () { |
michael@0 | 194 | let names = {}; |
michael@0 | 195 | function add(value, codepage, number) { |
michael@0 | 196 | let entry = { |
michael@0 | 197 | value: value, |
michael@0 | 198 | number: number, |
michael@0 | 199 | }; |
michael@0 | 200 | if (!names[codepage]) { |
michael@0 | 201 | names[codepage] = {}; |
michael@0 | 202 | } |
michael@0 | 203 | names[codepage][number] = entry; |
michael@0 | 204 | } |
michael@0 | 205 | |
michael@0 | 206 | add(".com/", 0, 0x85); |
michael@0 | 207 | add(".edu/", 0, 0x86); |
michael@0 | 208 | add(".net/", 0, 0x87); |
michael@0 | 209 | add(".org/", 0, 0x88); |
michael@0 | 210 | |
michael@0 | 211 | return names; |
michael@0 | 212 | })(); |
michael@0 | 213 | |
michael@0 | 214 | this.EXPORTED_SYMBOLS = [ |
michael@0 | 215 | // Parser |
michael@0 | 216 | "PduHelper", |
michael@0 | 217 | ]; |