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