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