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