Wentao Shang | bd63e46 | 2012-12-03 16:19:33 -0800 | [diff] [blame] | 1 | /** |
Jeff Thompson | e6923bf | 2012-10-14 09:47:42 -0700 | [diff] [blame] | 2 | * This class is used to encode ccnb binary elements (blob, type/value pairs). |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 3 | * |
Jeff Thompson | 146d7de | 2012-11-17 16:15:28 -0800 | [diff] [blame] | 4 | * @author: Meki Cheraoui |
Jeff Thompson | 745026e | 2012-10-13 12:49:20 -0700 | [diff] [blame] | 5 | * See COPYING for copyright and distribution information. |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | var XML_EXT = 0x00; |
| 9 | |
| 10 | var XML_TAG = 0x01; |
| 11 | |
| 12 | var XML_DTAG = 0x02; |
| 13 | |
| 14 | var XML_ATTR = 0x03; |
| 15 | |
| 16 | var XML_DATTR = 0x04; |
| 17 | |
| 18 | var XML_BLOB = 0x05; |
| 19 | |
| 20 | var XML_UDATA = 0x06; |
| 21 | |
| 22 | var XML_CLOSE = 0x0; |
| 23 | |
| 24 | var XML_SUBTYPE_PROCESSING_INSTRUCTIONS = 16; |
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 25 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 26 | |
| 27 | var XML_TT_BITS = 3; |
| 28 | var XML_TT_MASK = ((1 << XML_TT_BITS) - 1); |
| 29 | var XML_TT_VAL_BITS = XML_TT_BITS + 1; |
| 30 | var XML_TT_VAL_MASK = ((1 << (XML_TT_VAL_BITS)) - 1); |
| 31 | var XML_REG_VAL_BITS = 7; |
| 32 | var XML_REG_VAL_MASK = ((1 << XML_REG_VAL_BITS) - 1); |
| 33 | var XML_TT_NO_MORE = (1 << XML_REG_VAL_BITS); // 0x80 |
| 34 | var BYTE_MASK = 0xFF; |
| 35 | var LONG_BYTES = 8; |
| 36 | var LONG_BITS = 64; |
| 37 | |
| 38 | var bits_11 = 0x0000007FF; |
| 39 | var bits_18 = 0x00003FFFF; |
| 40 | var bits_32 = 0x0FFFFFFFF; |
| 41 | |
Jeff Thompson | 2b14c7e | 2013-07-29 15:09:56 -0700 | [diff] [blame] | 42 | /** |
| 43 | * @constructor |
| 44 | */ |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 45 | var BinaryXMLEncoder = function BinaryXMLEncoder(){ |
Jeff Thompson | 96978b4 | 2012-12-29 21:59:54 -0800 | [diff] [blame] | 46 | this.ostream = new DynamicUint8Array(100); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 47 | this.offset =0; |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 48 | this.CODEC_NAME = "Binary"; |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 49 | }; |
| 50 | |
Jeff Thompson | 2b14c7e | 2013-07-29 15:09:56 -0700 | [diff] [blame] | 51 | /** |
Jeff Thompson | d24dad3 | 2012-11-18 18:19:28 -0800 | [diff] [blame] | 52 | * Encode utf8Content as utf8. |
| 53 | */ |
Jeff Thompson | c92706b | 2012-11-11 19:12:58 -0800 | [diff] [blame] | 54 | BinaryXMLEncoder.prototype.writeUString = function(/*String*/ utf8Content) { |
Jeff Thompson | d15aa7b | 2012-11-17 15:17:24 -0800 | [diff] [blame] | 55 | this.encodeUString(utf8Content, XML_UDATA); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 56 | }; |
| 57 | |
Jeff Thompson | c92706b | 2012-11-11 19:12:58 -0800 | [diff] [blame] | 58 | |
| 59 | BinaryXMLEncoder.prototype.writeBlob = function( |
| 60 | /*Uint8Array*/ binaryContent |
Jeff Thompson | c92706b | 2012-11-11 19:12:58 -0800 | [diff] [blame] | 61 | ) { |
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 62 | |
| 63 | if(LOG >3) console.log(binaryContent); |
| 64 | |
Jeff Thompson | d15aa7b | 2012-11-17 15:17:24 -0800 | [diff] [blame] | 65 | this.encodeBlob(binaryContent, binaryContent.length); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 66 | }; |
| 67 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 68 | |
Jeff Thompson | c92706b | 2012-11-11 19:12:58 -0800 | [diff] [blame] | 69 | BinaryXMLEncoder.prototype.writeStartElement = function( |
| 70 | /*String*/ tag, |
| 71 | /*TreeMap<String,String>*/ attributes |
| 72 | ) { |
| 73 | |
Jeff Thompson | d15aa7b | 2012-11-17 15:17:24 -0800 | [diff] [blame] | 74 | /*Long*/ var dictionaryVal = tag; //stringToTag(tag); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 75 | |
| 76 | if (null == dictionaryVal) { |
Jeff Thompson | d15aa7b | 2012-11-17 15:17:24 -0800 | [diff] [blame] | 77 | this.encodeUString(tag, XML_TAG); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 78 | } else { |
Jeff Thompson | d15aa7b | 2012-11-17 15:17:24 -0800 | [diff] [blame] | 79 | this.encodeTypeAndVal(XML_DTAG, dictionaryVal); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | if (null != attributes) { |
| 83 | this.writeAttributes(attributes); |
| 84 | } |
| 85 | }; |
| 86 | |
| 87 | |
Jeff Thompson | c92706b | 2012-11-11 19:12:58 -0800 | [diff] [blame] | 88 | BinaryXMLEncoder.prototype.writeEndElement = function() { |
Jeff Thompson | 96978b4 | 2012-12-29 21:59:54 -0800 | [diff] [blame] | 89 | this.ostream.ensureLength(this.offset + 1); |
| 90 | this.ostream.array[this.offset] = XML_CLOSE; |
Jeff Thompson | c92706b | 2012-11-11 19:12:58 -0800 | [diff] [blame] | 91 | this.offset += 1; |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Jeff Thompson | c92706b | 2012-11-11 19:12:58 -0800 | [diff] [blame] | 94 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 95 | BinaryXMLEncoder.prototype.writeAttributes = function(/*TreeMap<String,String>*/ attributes) { |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 96 | if (null == attributes) { |
| 97 | return; |
| 98 | } |
| 99 | |
| 100 | // the keySet of a TreeMap is sorted. |
| 101 | |
| 102 | for(var i=0; i<attributes.length;i++){ |
| 103 | var strAttr = attributes[i].k; |
| 104 | var strValue = attributes[i].v; |
| 105 | |
| 106 | var dictionaryAttr = stringToTag(strAttr); |
| 107 | if (null == dictionaryAttr) { |
| 108 | // not in dictionary, encode as attr |
| 109 | // compressed format wants length of tag represented as length-1 |
| 110 | // to save that extra bit, as tag cannot be 0 length. |
| 111 | // encodeUString knows to do that. |
Jeff Thompson | d15aa7b | 2012-11-17 15:17:24 -0800 | [diff] [blame] | 112 | this.encodeUString(strAttr, XML_ATTR); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 113 | } else { |
Jeff Thompson | d15aa7b | 2012-11-17 15:17:24 -0800 | [diff] [blame] | 114 | this.encodeTypeAndVal(XML_DATTR, dictionaryAttr); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 115 | } |
| 116 | // Write value |
Jeff Thompson | d15aa7b | 2012-11-17 15:17:24 -0800 | [diff] [blame] | 117 | this.encodeUString(strValue); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 118 | |
| 119 | } |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Jeff Thompson | c92706b | 2012-11-11 19:12:58 -0800 | [diff] [blame] | 122 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 123 | //returns a string |
| 124 | stringToTag = function(/*long*/ tagVal) { |
| 125 | if ((tagVal >= 0) && (tagVal < CCNProtocolDTagsStrings.length)) { |
| 126 | return CCNProtocolDTagsStrings[tagVal]; |
| 127 | } else if (tagVal == CCNProtocolDTags.CCNProtocolDataUnit) { |
| 128 | return CCNProtocolDTags.CCNPROTOCOL_DATA_UNIT; |
| 129 | } |
| 130 | return null; |
| 131 | }; |
| 132 | |
| 133 | //returns a Long |
| 134 | tagToString = function(/*String*/ tagName) { |
| 135 | // the slow way, but right now we don't care.... want a static lookup for the forward direction |
| 136 | for (var i=0; i < CCNProtocolDTagsStrings.length; ++i) { |
| 137 | if ((null != CCNProtocolDTagsStrings[i]) && (CCNProtocolDTagsStrings[i] == tagName)) { |
| 138 | return i; |
| 139 | } |
| 140 | } |
| 141 | if (CCNProtocolDTags.CCNPROTOCOL_DATA_UNIT == tagName) { |
| 142 | return CCNProtocolDTags.CCNProtocolDataUnit; |
| 143 | } |
| 144 | return null; |
| 145 | }; |
| 146 | |
Jeff Thompson | 2b14c7e | 2013-07-29 15:09:56 -0700 | [diff] [blame] | 147 | /** |
Jeff Thompson | d24dad3 | 2012-11-18 18:19:28 -0800 | [diff] [blame] | 148 | * If Content is a string, then encode as utf8 and write UDATA. |
| 149 | */ |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 150 | BinaryXMLEncoder.prototype.writeElement = function( |
| 151 | //long |
| 152 | tag, |
| 153 | //byte[] |
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 154 | Content, |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 155 | //TreeMap<String, String> |
Jeff Thompson | c92706b | 2012-11-11 19:12:58 -0800 | [diff] [blame] | 156 | attributes |
| 157 | ) { |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 158 | this.writeStartElement(tag, attributes); |
| 159 | // Will omit if 0-length |
| 160 | |
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 161 | if(typeof Content === 'number') { |
Jeff Thompson | c92706b | 2012-11-11 19:12:58 -0800 | [diff] [blame] | 162 | if(LOG>4) console.log('GOING TO WRITE THE NUMBER .charCodeAt(0) ' + Content.toString().charCodeAt(0) ); |
| 163 | if(LOG>4) console.log('GOING TO WRITE THE NUMBER ' + Content.toString() ); |
| 164 | if(LOG>4) console.log('type of number is ' + typeof Content.toString() ); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 165 | |
| 166 | this.writeUString(Content.toString()); |
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 167 | //whatever |
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 168 | } |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 169 | else if(typeof Content === 'string'){ |
Jeff Thompson | c92706b | 2012-11-11 19:12:58 -0800 | [diff] [blame] | 170 | if(LOG>4) console.log('GOING TO WRITE THE STRING ' + Content ); |
| 171 | if(LOG>4) console.log('type of STRING is ' + typeof Content ); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 172 | |
| 173 | this.writeUString(Content); |
| 174 | } |
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 175 | else{ |
Jeff Thompson | c92706b | 2012-11-11 19:12:58 -0800 | [diff] [blame] | 176 | if(LOG>4) console.log('GOING TO WRITE A BLOB ' + Content ); |
| 177 | |
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 178 | this.writeBlob(Content); |
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 181 | this.writeEndElement(); |
| 182 | } |
| 183 | |
Jeff Thompson | c92706b | 2012-11-11 19:12:58 -0800 | [diff] [blame] | 184 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 185 | |
| 186 | var TypeAndVal = function TypeAndVal(_type,_val) { |
| 187 | this.type = _type; |
| 188 | this.val = _val; |
| 189 | |
| 190 | }; |
| 191 | |
Jeff Thompson | c92706b | 2012-11-11 19:12:58 -0800 | [diff] [blame] | 192 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 193 | BinaryXMLEncoder.prototype.encodeTypeAndVal = function( |
| 194 | //int |
| 195 | type, |
| 196 | //long |
Jeff Thompson | d15aa7b | 2012-11-17 15:17:24 -0800 | [diff] [blame] | 197 | val |
Jeff Thompson | c92706b | 2012-11-11 19:12:58 -0800 | [diff] [blame] | 198 | ) { |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 199 | |
Jeff Thompson | c92706b | 2012-11-11 19:12:58 -0800 | [diff] [blame] | 200 | if(LOG>4) console.log('Encoding type '+ type+ ' and value '+ val); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 201 | |
Jeff Thompson | c92706b | 2012-11-11 19:12:58 -0800 | [diff] [blame] | 202 | if(LOG>4) console.log('OFFSET IS ' + this.offset); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 203 | |
| 204 | if ((type > XML_UDATA) || (type < 0) || (val < 0)) { |
Jeff Thompson | 34a2ec0 | 2012-09-29 21:47:05 -0700 | [diff] [blame] | 205 | throw new Error("Tag and value must be positive, and tag valid."); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | // Encode backwards. Calculate how many bytes we need: |
| 209 | var numEncodingBytes = this.numEncodingBytes(val); |
Jeff Thompson | 96978b4 | 2012-12-29 21:59:54 -0800 | [diff] [blame] | 210 | this.ostream.ensureLength(this.offset + numEncodingBytes); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 211 | |
| 212 | // Bottom 4 bits of val go in last byte with tag. |
Jeff Thompson | 96978b4 | 2012-12-29 21:59:54 -0800 | [diff] [blame] | 213 | this.ostream.array[this.offset + numEncodingBytes - 1] = |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 214 | //(byte) |
| 215 | (BYTE_MASK & |
| 216 | (((XML_TT_MASK & type) | |
| 217 | ((XML_TT_VAL_MASK & val) << XML_TT_BITS))) | |
| 218 | XML_TT_NO_MORE); // set top bit for last byte |
Jeff Thompson | 96978b4 | 2012-12-29 21:59:54 -0800 | [diff] [blame] | 219 | val = val >>> XML_TT_VAL_BITS; |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 220 | |
| 221 | // Rest of val goes into preceding bytes, 7 bits per byte, top bit |
| 222 | // is "more" flag. |
| 223 | var i = this.offset + numEncodingBytes - 2; |
| 224 | while ((0 != val) && (i >= this.offset)) { |
Jeff Thompson | 96978b4 | 2012-12-29 21:59:54 -0800 | [diff] [blame] | 225 | this.ostream.array[i] = //(byte) |
Jeff Thompson | c92706b | 2012-11-11 19:12:58 -0800 | [diff] [blame] | 226 | (BYTE_MASK & (val & XML_REG_VAL_MASK)); // leave top bit unset |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 227 | val = val >>> XML_REG_VAL_BITS; |
| 228 | --i; |
| 229 | } |
| 230 | if (val != 0) { |
Jeff Thompson | 34a2ec0 | 2012-09-29 21:47:05 -0700 | [diff] [blame] | 231 | throw new Error( "This should not happen: miscalculated encoding"); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 232 | //Log.warning(Log.FAC_ENCODING, "This should not happen: miscalculated encoding length, have " + val + " left."); |
| 233 | } |
| 234 | this.offset+= numEncodingBytes; |
| 235 | |
| 236 | return numEncodingBytes; |
| 237 | }; |
| 238 | |
Jeff Thompson | 2b14c7e | 2013-07-29 15:09:56 -0700 | [diff] [blame] | 239 | /** |
Jeff Thompson | d24dad3 | 2012-11-18 18:19:28 -0800 | [diff] [blame] | 240 | * Encode ustring as utf8. |
| 241 | */ |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 242 | BinaryXMLEncoder.prototype.encodeUString = function( |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 243 | //String |
| 244 | ustring, |
| 245 | //byte |
| 246 | type) { |
| 247 | |
Jeff Thompson | d24dad3 | 2012-11-18 18:19:28 -0800 | [diff] [blame] | 248 | if (null == ustring) |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 249 | return; |
Jeff Thompson | d24dad3 | 2012-11-18 18:19:28 -0800 | [diff] [blame] | 250 | if (type == XML_TAG || type == XML_ATTR && ustring.length == 0) |
| 251 | return; |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 252 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 253 | if(LOG>3) console.log("The string to write is "); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 254 | if(LOG>3) console.log(ustring); |
| 255 | |
Jeff Thompson | d24dad3 | 2012-11-18 18:19:28 -0800 | [diff] [blame] | 256 | var strBytes = DataUtils.stringToUtf8Array(ustring); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 257 | |
| 258 | this.encodeTypeAndVal(type, |
| 259 | (((type == XML_TAG) || (type == XML_ATTR)) ? |
| 260 | (strBytes.length-1) : |
Jeff Thompson | d15aa7b | 2012-11-17 15:17:24 -0800 | [diff] [blame] | 261 | strBytes.length)); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 262 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 263 | if(LOG>3) console.log("THE string to write is "); |
| 264 | |
| 265 | if(LOG>3) console.log(strBytes); |
| 266 | |
Jeff Thompson | 96978b4 | 2012-12-29 21:59:54 -0800 | [diff] [blame] | 267 | this.writeString(strBytes); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 268 | this.offset+= strBytes.length; |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 269 | }; |
| 270 | |
| 271 | |
| 272 | |
| 273 | BinaryXMLEncoder.prototype.encodeBlob = function( |
Jeff Thompson | c92706b | 2012-11-11 19:12:58 -0800 | [diff] [blame] | 274 | //Uint8Array |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 275 | blob, |
| 276 | //int |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 277 | length) { |
| 278 | |
| 279 | |
Jeff Thompson | 84ca950 | 2012-11-04 13:11:36 -0800 | [diff] [blame] | 280 | if (null == blob) |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 281 | return; |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 282 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 283 | if(LOG>4) console.log('LENGTH OF XML_BLOB IS '+length); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 284 | |
Jeff Thompson | c92706b | 2012-11-11 19:12:58 -0800 | [diff] [blame] | 285 | /*blobCopy = new Array(blob.Length); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 286 | |
Jeff Thompson | c92706b | 2012-11-11 19:12:58 -0800 | [diff] [blame] | 287 | for (i = 0; i < blob.length; i++) //in InStr.ToCharArray()) |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 288 | { |
| 289 | blobCopy[i] = blob[i]; |
Jeff Thompson | c92706b | 2012-11-11 19:12:58 -0800 | [diff] [blame] | 290 | }*/ |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 291 | |
Jeff Thompson | d15aa7b | 2012-11-17 15:17:24 -0800 | [diff] [blame] | 292 | this.encodeTypeAndVal(XML_BLOB, length); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 293 | |
Jeff Thompson | 96978b4 | 2012-12-29 21:59:54 -0800 | [diff] [blame] | 294 | this.writeBlobArray(blob); |
Jeff Thompson | c92706b | 2012-11-11 19:12:58 -0800 | [diff] [blame] | 295 | this.offset += length; |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 296 | }; |
| 297 | |
| 298 | var ENCODING_LIMIT_1_BYTE = ((1 << (XML_TT_VAL_BITS)) - 1); |
| 299 | var ENCODING_LIMIT_2_BYTES = ((1 << (XML_TT_VAL_BITS + XML_REG_VAL_BITS)) - 1); |
| 300 | var ENCODING_LIMIT_3_BYTES = ((1 << (XML_TT_VAL_BITS + 2 * XML_REG_VAL_BITS)) - 1); |
| 301 | |
| 302 | BinaryXMLEncoder.prototype.numEncodingBytes = function( |
| 303 | //long |
| 304 | x) { |
| 305 | if (x <= ENCODING_LIMIT_1_BYTE) return (1); |
| 306 | if (x <= ENCODING_LIMIT_2_BYTES) return (2); |
| 307 | if (x <= ENCODING_LIMIT_3_BYTES) return (3); |
| 308 | |
| 309 | var numbytes = 1; |
| 310 | |
| 311 | // Last byte gives you XML_TT_VAL_BITS |
| 312 | // Remainder each give you XML_REG_VAL_BITS |
| 313 | x = x >>> XML_TT_VAL_BITS; |
| 314 | while (x != 0) { |
| 315 | numbytes++; |
| 316 | x = x >>> XML_REG_VAL_BITS; |
| 317 | } |
| 318 | return (numbytes); |
| 319 | }; |
| 320 | |
| 321 | BinaryXMLEncoder.prototype.writeDateTime = function( |
| 322 | //String |
| 323 | tag, |
| 324 | //CCNTime |
| 325 | dateTime) { |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 326 | |
| 327 | if(LOG>4)console.log('ENCODING DATE with LONG VALUE'); |
| 328 | if(LOG>4)console.log(dateTime.msec); |
| 329 | |
| 330 | //var binarydate = DataUtils.unsignedLongToByteArray( Math.round((dateTime.msec/1000) * 4096) ); |
| 331 | |
| 332 | |
| 333 | //parse to hex |
| 334 | var binarydate = Math.round((dateTime.msec/1000) * 4096).toString(16) ; |
Jeff Thompson | 8023c15 | 2013-07-11 13:21:44 -0700 | [diff] [blame] | 335 | if (binarydate.length % 2 == 1) |
| 336 | binarydate = '0' + binarydate; |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 337 | |
Jeff Thompson | 8023c15 | 2013-07-11 13:21:44 -0700 | [diff] [blame] | 338 | // Hack toNumbers by appending a 0 which is ignored. |
| 339 | var binarydate = DataUtils.toNumbers( binarydate + '0') ; |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 340 | |
| 341 | |
| 342 | if(LOG>4)console.log('ENCODING DATE with BINARY VALUE'); |
| 343 | if(LOG>4)console.log(binarydate); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 344 | if(LOG>4)console.log('ENCODING DATE with BINARY VALUE(HEX)'); |
| 345 | if(LOG>4)console.log(DataUtils.toHex(binarydate)); |
| 346 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 347 | this.writeElement(tag, binarydate); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 348 | }; |
| 349 | |
Jeff Thompson | 96978b4 | 2012-12-29 21:59:54 -0800 | [diff] [blame] | 350 | // This does not update this.offset. |
| 351 | BinaryXMLEncoder.prototype.writeString = function(input) { |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 352 | |
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 353 | if(typeof input === 'string'){ |
| 354 | //console.log('went here'); |
| 355 | if(LOG>4) console.log('GOING TO WRITE A STRING'); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 356 | if(LOG>4) console.log(input); |
| 357 | |
Jeff Thompson | 96978b4 | 2012-12-29 21:59:54 -0800 | [diff] [blame] | 358 | this.ostream.ensureLength(this.offset + input.length); |
| 359 | for (var i = 0; i < input.length; i++) { |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 360 | if(LOG>4) console.log('input.charCodeAt(i)=' + input.charCodeAt(i)); |
Jeff Thompson | 96978b4 | 2012-12-29 21:59:54 -0800 | [diff] [blame] | 361 | this.ostream.array[this.offset + i] = (input.charCodeAt(i)); |
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 362 | } |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 363 | } |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 364 | else{ |
Jeff Thompson | c92706b | 2012-11-11 19:12:58 -0800 | [diff] [blame] | 365 | if(LOG>4) console.log('GOING TO WRITE A STRING IN BINARY FORM'); |
| 366 | if(LOG>4) console.log(input); |
| 367 | |
| 368 | this.writeBlobArray(input); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 369 | } |
| 370 | /* |
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 371 | else if(typeof input === 'object'){ |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 372 | |
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 373 | } |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 374 | */ |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 375 | }; |
| 376 | |
Jeff Thompson | c92706b | 2012-11-11 19:12:58 -0800 | [diff] [blame] | 377 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 378 | BinaryXMLEncoder.prototype.writeBlobArray = function( |
Jeff Thompson | c92706b | 2012-11-11 19:12:58 -0800 | [diff] [blame] | 379 | //Uint8Array |
Jeff Thompson | 96978b4 | 2012-12-29 21:59:54 -0800 | [diff] [blame] | 380 | blob) { |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 381 | |
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 382 | if(LOG>4) console.log('GOING TO WRITE A BLOB'); |
| 383 | |
Jeff Thompson | c92706b | 2012-11-11 19:12:58 -0800 | [diff] [blame] | 384 | this.ostream.set(blob, this.offset); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 385 | }; |
Meki Cherkaoui | abb973b | 2012-05-09 14:25:57 -0700 | [diff] [blame] | 386 | |
| 387 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 388 | BinaryXMLEncoder.prototype.getReducedOstream = function() { |
Jeff Thompson | c92706b | 2012-11-11 19:12:58 -0800 | [diff] [blame] | 389 | return this.ostream.subarray(0, this.offset); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 390 | }; |
| 391 | |