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