Wentao Shang | bd63e46 | 2012-12-03 16:19:33 -0800 | [diff] [blame] | 1 | /** |
Jeff Thompson | 87021f7 | 2012-11-18 17:40:06 -0800 | [diff] [blame] | 2 | * @author: Meki Cheraoui, Jeff Thompson |
Jeff Thompson | 745026e | 2012-10-13 12:49:20 -0700 | [diff] [blame] | 3 | * See COPYING for copyright and distribution information. |
Jeff Thompson | 4e8b770 | 2012-10-21 23:42:27 -0700 | [diff] [blame] | 4 | * This class represents a Name as an array of components where each is a byte array. |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
Jeff Thompson | 87021f7 | 2012-11-18 17:40:06 -0800 | [diff] [blame] | 7 | /* |
| 8 | * Create a new Name from _components. |
Jeff Thompson | ff5a8dc | 2013-02-28 22:24:53 -0800 | [diff] [blame] | 9 | * If _components is a string, parse it as a URI. |
| 10 | * If _components is a Name, add a deep copy of its components. |
| 11 | * Otherwise it is an array of components where each is a string, byte array, ArrayBuffer, Uint8Array |
| 12 | * or Name. |
Jeff Thompson | 87021f7 | 2012-11-18 17:40:06 -0800 | [diff] [blame] | 13 | * Convert and store as an array of Uint8Array. |
| 14 | * If a component is a string, encode as utf8. |
| 15 | */ |
Jeff Thompson | f3bd359 | 2012-09-29 23:25:30 -0700 | [diff] [blame] | 16 | var Name = function Name(_components){ |
Jeff Thompson | 87021f7 | 2012-11-18 17:40:06 -0800 | [diff] [blame] | 17 | if( typeof _components == 'string') { |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 18 | if(LOG>3)console.log('Content Name String '+_components); |
Jeff Thompson | 87021f7 | 2012-11-18 17:40:06 -0800 | [diff] [blame] | 19 | this.components = Name.createNameArray(_components); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 20 | } |
Jeff Thompson | 87021f7 | 2012-11-18 17:40:06 -0800 | [diff] [blame] | 21 | else if(typeof _components === 'object'){ |
Jeff Thompson | 87021f7 | 2012-11-18 17:40:06 -0800 | [diff] [blame] | 22 | this.components = []; |
Jeff Thompson | ff5a8dc | 2013-02-28 22:24:53 -0800 | [diff] [blame] | 23 | if (_components instanceof Name) |
| 24 | this.add(_components); |
| 25 | else { |
| 26 | for (var i = 0; i < _components.length; ++i) |
| 27 | this.add(_components[i]); |
| 28 | } |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 29 | } |
Jeff Thompson | 87021f7 | 2012-11-18 17:40:06 -0800 | [diff] [blame] | 30 | else if(_components==null) |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 31 | this.components =[]; |
Jeff Thompson | 87021f7 | 2012-11-18 17:40:06 -0800 | [diff] [blame] | 32 | else |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 33 | if(LOG>1)console.log("NO CONTENT NAME GIVEN"); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 34 | }; |
| 35 | |
Jeff Thompson | 0d3f203 | 2012-11-04 12:55:27 -0800 | [diff] [blame] | 36 | Name.prototype.getName = function() { |
| 37 | return this.to_uri(); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 38 | }; |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 39 | |
Jeff Thompson | d855602 | 2013-07-02 18:15:46 -0700 | [diff] [blame] | 40 | /* Parse uri as a URI and return an array of Uint8Array components. |
Jeff Thompson | 87021f7 | 2012-11-18 17:40:06 -0800 | [diff] [blame] | 41 | * |
| 42 | */ |
Jeff Thompson | d855602 | 2013-07-02 18:15:46 -0700 | [diff] [blame] | 43 | Name.createNameArray = function(uri) { |
| 44 | uri = uri.trim(); |
| 45 | if (uri.length <= 0) |
Jeff Thompson | 0d3f203 | 2012-11-04 12:55:27 -0800 | [diff] [blame] | 46 | return []; |
| 47 | |
Jeff Thompson | d855602 | 2013-07-02 18:15:46 -0700 | [diff] [blame] | 48 | var iColon = uri.indexOf(':'); |
Jeff Thompson | 0d3f203 | 2012-11-04 12:55:27 -0800 | [diff] [blame] | 49 | if (iColon >= 0) { |
| 50 | // Make sure the colon came before a '/'. |
Jeff Thompson | d855602 | 2013-07-02 18:15:46 -0700 | [diff] [blame] | 51 | var iFirstSlash = uri.indexOf('/'); |
Jeff Thompson | 0d3f203 | 2012-11-04 12:55:27 -0800 | [diff] [blame] | 52 | if (iFirstSlash < 0 || iColon < iFirstSlash) |
Jeff Thompson | bd82926 | 2012-11-30 22:28:37 -0800 | [diff] [blame] | 53 | // Omit the leading protocol such as ndn: |
Jeff Thompson | d855602 | 2013-07-02 18:15:46 -0700 | [diff] [blame] | 54 | uri = uri.substr(iColon + 1, uri.length - iColon - 1).trim(); |
Jeff Thompson | 4e8b770 | 2012-10-21 23:42:27 -0700 | [diff] [blame] | 55 | } |
Jeff Thompson | 0d3f203 | 2012-11-04 12:55:27 -0800 | [diff] [blame] | 56 | |
Jeff Thompson | d855602 | 2013-07-02 18:15:46 -0700 | [diff] [blame] | 57 | if (uri[0] == '/') { |
| 58 | if (uri.length >= 2 && uri[1] == '/') { |
Jeff Thompson | 0d3f203 | 2012-11-04 12:55:27 -0800 | [diff] [blame] | 59 | // Strip the authority following "//". |
Jeff Thompson | d855602 | 2013-07-02 18:15:46 -0700 | [diff] [blame] | 60 | var iAfterAuthority = uri.indexOf('/', 2); |
Jeff Thompson | 0d3f203 | 2012-11-04 12:55:27 -0800 | [diff] [blame] | 61 | if (iAfterAuthority < 0) |
| 62 | // Unusual case: there was only an authority. |
| 63 | return []; |
| 64 | else |
Jeff Thompson | d855602 | 2013-07-02 18:15:46 -0700 | [diff] [blame] | 65 | uri = uri.substr(iAfterAuthority + 1, uri.length - iAfterAuthority - 1).trim(); |
Jeff Thompson | 0d3f203 | 2012-11-04 12:55:27 -0800 | [diff] [blame] | 66 | } |
| 67 | else |
Jeff Thompson | d855602 | 2013-07-02 18:15:46 -0700 | [diff] [blame] | 68 | uri = uri.substr(1, uri.length - 1).trim(); |
Jeff Thompson | 0d3f203 | 2012-11-04 12:55:27 -0800 | [diff] [blame] | 69 | } |
| 70 | |
Jeff Thompson | d855602 | 2013-07-02 18:15:46 -0700 | [diff] [blame] | 71 | var array = uri.split('/'); |
Jeff Thompson | 9b41100 | 2012-10-21 17:35:16 -0700 | [diff] [blame] | 72 | |
| 73 | // Unescape the components. |
Jeff Thompson | 4e8b770 | 2012-10-21 23:42:27 -0700 | [diff] [blame] | 74 | for (var i = 0; i < array.length; ++i) { |
Jeff Thompson | 78849a7 | 2013-02-03 22:35:54 -0800 | [diff] [blame] | 75 | var component = Name.fromEscapedString(array[i]); |
Jeff Thompson | 4e8b770 | 2012-10-21 23:42:27 -0700 | [diff] [blame] | 76 | |
Jeff Thompson | 78849a7 | 2013-02-03 22:35:54 -0800 | [diff] [blame] | 77 | if (component == null) { |
| 78 | // Ignore the illegal componenent. This also gets rid of a trailing '/'. |
| 79 | array.splice(i, 1); |
| 80 | --i; |
| 81 | continue; |
Jeff Thompson | 4e8b770 | 2012-10-21 23:42:27 -0700 | [diff] [blame] | 82 | } |
| 83 | else |
| 84 | array[i] = component; |
| 85 | } |
Jeff Thompson | 9b41100 | 2012-10-21 17:35:16 -0700 | [diff] [blame] | 86 | |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 87 | return array; |
| 88 | } |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 89 | |
| 90 | |
Jeff Thompson | f3bd359 | 2012-09-29 23:25:30 -0700 | [diff] [blame] | 91 | Name.prototype.from_ccnb = function(/*XMLDecoder*/ decoder) { |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 92 | decoder.readStartElement(this.getElementLabel()); |
| 93 | |
| 94 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 95 | this.components = new Array(); //new ArrayList<byte []>(); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 96 | |
| 97 | while (decoder.peekStartElement(CCNProtocolDTags.Component)) { |
| 98 | this.add(decoder.readBinaryElement(CCNProtocolDTags.Component)); |
| 99 | } |
| 100 | |
| 101 | decoder.readEndElement(); |
| 102 | }; |
| 103 | |
Jeff Thompson | f3bd359 | 2012-09-29 23:25:30 -0700 | [diff] [blame] | 104 | Name.prototype.to_ccnb = function(/*XMLEncoder*/ encoder) { |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 105 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 106 | if( this.components ==null ) |
Jeff Thompson | 34a2ec0 | 2012-09-29 21:47:05 -0700 | [diff] [blame] | 107 | throw new Error("CANNOT ENCODE EMPTY CONTENT NAME"); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 108 | |
| 109 | encoder.writeStartElement(this.getElementLabel()); |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 110 | var count = this.components.length; |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 111 | for (var i=0; i < count; i++) { |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 112 | encoder.writeElement(CCNProtocolDTags.Component, this.components[i]); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 113 | } |
| 114 | encoder.writeEndElement(); |
| 115 | }; |
| 116 | |
Jeff Thompson | f3bd359 | 2012-09-29 23:25:30 -0700 | [diff] [blame] | 117 | Name.prototype.getElementLabel = function(){ |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 118 | return CCNProtocolDTags.Name; |
| 119 | }; |
| 120 | |
Jeff Thompson | 87021f7 | 2012-11-18 17:40:06 -0800 | [diff] [blame] | 121 | /* |
Jeff Thompson | ff5a8dc | 2013-02-28 22:24:53 -0800 | [diff] [blame] | 122 | * component is a string, byte array, ArrayBuffer, Uint8Array or Name. |
Jeff Thompson | 87021f7 | 2012-11-18 17:40:06 -0800 | [diff] [blame] | 123 | * Convert to Uint8Array and add to this Name. |
| 124 | * If a component is a string, encode as utf8. |
Jeff Thompson | ff5a8dc | 2013-02-28 22:24:53 -0800 | [diff] [blame] | 125 | * Return this Name object to allow chaining calls to add. |
Jeff Thompson | 87021f7 | 2012-11-18 17:40:06 -0800 | [diff] [blame] | 126 | */ |
| 127 | Name.prototype.add = function(component){ |
Jeff Thompson | 9ab151a | 2012-11-24 21:22:30 -0800 | [diff] [blame] | 128 | var result; |
Jeff Thompson | 87021f7 | 2012-11-18 17:40:06 -0800 | [diff] [blame] | 129 | if(typeof component == 'string') |
| 130 | result = DataUtils.stringToUtf8Array(component); |
Jeff Thompson | 87021f7 | 2012-11-18 17:40:06 -0800 | [diff] [blame] | 131 | else if(typeof component == 'object' && component instanceof Uint8Array) |
| 132 | result = new Uint8Array(component); |
Jeff Thompson | 8a97ac5 | 2012-11-24 23:16:02 -0800 | [diff] [blame] | 133 | else if(typeof component == 'object' && component instanceof ArrayBuffer) { |
| 134 | // Make a copy. Don't use ArrayBuffer.slice since it isn't always supported. |
| 135 | result = new Uint8Array(new ArrayBuffer(component.byteLength)); |
| 136 | result.set(new Uint8Array(component)); |
| 137 | } |
Jeff Thompson | ff5a8dc | 2013-02-28 22:24:53 -0800 | [diff] [blame] | 138 | else if (typeof component == 'object' && component instanceof Name) { |
| 139 | var components; |
| 140 | if (component == this) |
| 141 | // special case, when we need to create a copy |
| 142 | components = this.components.slice(0, this.components.length); |
| 143 | else |
| 144 | components = component.components; |
| 145 | |
| 146 | for (var i = 0; i < components.length; ++i) |
| 147 | this.components.push(new Uint8Array(components[i])); |
| 148 | return this; |
Alexander Afanasyev | 78122d5 | 2013-02-27 18:49:54 -0800 | [diff] [blame] | 149 | } |
Jeff Thompson | 51ab765 | 2012-11-24 21:57:37 -0800 | [diff] [blame] | 150 | else if(typeof component == 'object') |
| 151 | // Assume component is a byte array. We can't check instanceof Array because |
| 152 | // this doesn't work in JavaScript if the array comes from a different module. |
| 153 | result = new Uint8Array(component); |
Jeff Thompson | 87021f7 | 2012-11-18 17:40:06 -0800 | [diff] [blame] | 154 | else |
Jeff Thompson | 9ab151a | 2012-11-24 21:22:30 -0800 | [diff] [blame] | 155 | throw new Error("Cannot add Name element at index " + this.components.length + |
| 156 | ": Invalid type"); |
Jeff Thompson | 87021f7 | 2012-11-18 17:40:06 -0800 | [diff] [blame] | 157 | |
Jeff Thompson | ff5a8dc | 2013-02-28 22:24:53 -0800 | [diff] [blame] | 158 | this.components.push(result); |
| 159 | return this; |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 160 | }; |
| 161 | |
Alexander Afanasyev | c083878 | 2013-02-27 19:13:06 -0800 | [diff] [blame] | 162 | /** |
| 163 | * @brief Add component that represents a segment number |
| 164 | * |
| 165 | * @param number Segment number (integer is expected) |
| 166 | * |
| 167 | * This component has a special format handling: |
| 168 | * - if number is zero, then %00 is added |
| 169 | * - if number is between 1 and 255, %00%01 .. %00%FF is added |
| 170 | * - ... |
| 171 | */ |
| 172 | Name.prototype.addSegment = function(number) { |
| 173 | // step 1: figure out how many bytes will be needed |
| 174 | var bytes = 1; // at least 1 byte |
| 175 | var test_number = number; |
| 176 | while (test_number > 0) { |
| 177 | bytes ++; |
| 178 | test_number >>= 8; |
| 179 | } |
| 180 | |
| 181 | var result = new Uint8Array (bytes); |
| 182 | var index = 0; |
| 183 | result[index] = 0; |
| 184 | index ++; |
| 185 | while (number > 0) { |
| 186 | result[index] = number & 0xFF; |
| 187 | number >>= 8; |
| 188 | index ++; |
| 189 | } |
| 190 | |
| 191 | this.components.push(result); |
| 192 | return this; |
| 193 | } |
| 194 | |
Jeff Thompson | bd82926 | 2012-11-30 22:28:37 -0800 | [diff] [blame] | 195 | // Return the escaped name string according to "CCNx URI Scheme". |
Jeff Thompson | 4e8b770 | 2012-10-21 23:42:27 -0700 | [diff] [blame] | 196 | Name.prototype.to_uri = function() { |
Jeff Thompson | 9530663 | 2013-01-23 19:20:37 -0800 | [diff] [blame] | 197 | if (this.components.length == 0) |
| 198 | return "/"; |
| 199 | |
Jeff Thompson | 4e8b770 | 2012-10-21 23:42:27 -0700 | [diff] [blame] | 200 | var result = ""; |
| 201 | |
| 202 | for(var i = 0; i < this.components.length; ++i) |
| 203 | result += "/"+ Name.toEscapedString(this.components[i]); |
| 204 | |
| 205 | return result; |
| 206 | }; |
Alexander Afanasyev | 78122d5 | 2013-02-27 18:49:54 -0800 | [diff] [blame] | 207 | |
Jeff Thompson | ff5a8dc | 2013-02-28 22:24:53 -0800 | [diff] [blame] | 208 | /** |
| 209 | * @brief Add component that represents a segment number |
| 210 | * |
| 211 | * @param number Segment number (integer is expected) |
| 212 | * |
| 213 | * This component has a special format handling: |
| 214 | * - if number is zero, then %00 is added |
| 215 | * - if number is between 1 and 255, %00%01 .. %00%FF is added |
| 216 | * - ... |
| 217 | */ |
| 218 | Name.prototype.addSegment = function(number) { |
| 219 | var segmentNumberBigEndian = DataUtils.nonNegativeIntToBigEndian(number); |
| 220 | // Put a 0 byte in front. |
| 221 | var segmentNumberComponent = new Uint8Array(segmentNumberBigEndian.length + 1); |
| 222 | segmentNumberComponent.set(segmentNumberBigEndian, 1); |
| 223 | |
| 224 | this.components.push(segmentNumberComponent); |
| 225 | return this; |
Jeff Thompson | 4e8b770 | 2012-10-21 23:42:27 -0700 | [diff] [blame] | 226 | }; |
| 227 | |
Jeff Thompson | 87021f7 | 2012-11-18 17:40:06 -0800 | [diff] [blame] | 228 | /* |
| 229 | * Return a new Name with the first nComponents components of this Name. |
| 230 | */ |
| 231 | Name.prototype.getPrefix = function(nComponents) { |
| 232 | return new Name(this.components.slice(0, nComponents)); |
| 233 | } |
| 234 | |
Alexander Afanasyev | 13bd648 | 2013-03-02 13:50:24 -0800 | [diff] [blame] | 235 | /** |
| 236 | * @brief Get prefix of the name, containing less minusComponents right components |
| 237 | * @param minusComponents number of components to cut from the back |
| 238 | */ |
| 239 | Name.prototype.cut = function (minusComponents) { |
| 240 | return new Name(this.components.slice(0, this.components.length-1)); |
| 241 | } |
| 242 | |
Jeff Thompson | 87021f7 | 2012-11-18 17:40:06 -0800 | [diff] [blame] | 243 | /* |
| 244 | * Return a new ArrayBuffer of the component at i. |
| 245 | */ |
| 246 | Name.prototype.getComponent = function(i) { |
| 247 | var result = new ArrayBuffer(this.components[i].length); |
| 248 | new Uint8Array(result).set(this.components[i]); |
| 249 | return result; |
| 250 | } |
| 251 | |
Jeff Thompson | 16a35f7 | 2012-11-25 08:07:33 -0800 | [diff] [blame] | 252 | /* |
| 253 | * The "file name" in a name is the last component that isn't blank and doesn't start with one of the |
| 254 | * special marker octets (for version, etc.). Return the index in this.components of |
| 255 | * the file name, or -1 if not found. |
| 256 | */ |
| 257 | Name.prototype.indexOfFileName = function() { |
| 258 | for (var i = this.components.length - 1; i >= 0; --i) { |
| 259 | var component = this.components[i]; |
| 260 | if (component.length <= 0) |
| 261 | continue; |
| 262 | |
| 263 | if (component[0] == 0 || component[0] == 0xC0 || component[0] == 0xC1 || |
| 264 | (component[0] >= 0xF5 && component[0] <= 0xFF)) |
| 265 | continue; |
| 266 | |
| 267 | return i; |
| 268 | } |
| 269 | |
| 270 | return -1; |
| 271 | } |
| 272 | |
Jeff Thompson | 990e300 | 2012-12-02 23:29:36 -0800 | [diff] [blame] | 273 | /* |
Jeff Thompson | 77fc9b7 | 2012-12-16 12:38:09 -0800 | [diff] [blame] | 274 | * Return true if this Name has the same components as name. |
| 275 | */ |
| 276 | Name.prototype.equalsName = function(name) { |
| 277 | if (this.components.length != name.components.length) |
| 278 | return false; |
| 279 | |
| 280 | // Start from the last component because they are more likely to differ. |
| 281 | for (var i = this.components.length - 1; i >= 0; --i) { |
| 282 | if (!DataUtils.arraysEqual(this.components[i], name.components[i])) |
| 283 | return false; |
| 284 | } |
| 285 | |
| 286 | return true; |
| 287 | } |
| 288 | |
| 289 | /* |
Jeff Thompson | 990e300 | 2012-12-02 23:29:36 -0800 | [diff] [blame] | 290 | * Find the last component in name that has a ContentDigest and return the digest value as Uint8Array, |
Jeff Thompson | ac3e62c | 2012-12-16 19:39:58 -0800 | [diff] [blame] | 291 | * or null if not found. See Name.getComponentContentDigestValue. |
Jeff Thompson | 990e300 | 2012-12-02 23:29:36 -0800 | [diff] [blame] | 292 | */ |
| 293 | Name.prototype.getContentDigestValue = function() { |
Jeff Thompson | 990e300 | 2012-12-02 23:29:36 -0800 | [diff] [blame] | 294 | for (var i = this.components.length - 1; i >= 0; --i) { |
Jeff Thompson | ac3e62c | 2012-12-16 19:39:58 -0800 | [diff] [blame] | 295 | var digestValue = Name.getComponentContentDigestValue(this.components[i]); |
| 296 | if (digestValue != null) |
| 297 | return digestValue; |
Jeff Thompson | 990e300 | 2012-12-02 23:29:36 -0800 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | return null; |
| 301 | } |
| 302 | |
Jeff Thompson | ac3e62c | 2012-12-16 19:39:58 -0800 | [diff] [blame] | 303 | /* |
| 304 | * If component is a ContentDigest, return the digest value as a Uint8Array subarray (don't modify!). |
| 305 | * If not a ContentDigest, return null. |
| 306 | * A ContentDigest component is Name.ContentDigestPrefix + 32 bytes + Name.ContentDigestSuffix. |
| 307 | */ |
| 308 | Name.getComponentContentDigestValue = function(component) { |
| 309 | var digestComponentLength = Name.ContentDigestPrefix.length + 32 + Name.ContentDigestSuffix.length; |
| 310 | // Check for the correct length and equal ContentDigestPrefix and ContentDigestSuffix. |
| 311 | if (component.length == digestComponentLength && |
| 312 | DataUtils.arraysEqual(component.subarray(0, Name.ContentDigestPrefix.length), |
| 313 | Name.ContentDigestPrefix) && |
| 314 | DataUtils.arraysEqual(component.subarray |
| 315 | (component.length - Name.ContentDigestSuffix.length, component.length), |
| 316 | Name.ContentDigestSuffix)) |
| 317 | return component.subarray(Name.ContentDigestPrefix.length, Name.ContentDigestPrefix.length + 32); |
| 318 | else |
| 319 | return null; |
| 320 | } |
| 321 | |
Jeff Thompson | 990e300 | 2012-12-02 23:29:36 -0800 | [diff] [blame] | 322 | // Meta GUID "%C1.M.G%C1" + ContentDigest with a 32 byte BLOB. |
| 323 | Name.ContentDigestPrefix = new Uint8Array([0xc1, 0x2e, 0x4d, 0x2e, 0x47, 0xc1, 0x01, 0xaa, 0x02, 0x85]); |
| 324 | Name.ContentDigestSuffix = new Uint8Array([0x00]); |
| 325 | |
Jeff Thompson | 78849a7 | 2013-02-03 22:35:54 -0800 | [diff] [blame] | 326 | /* |
Jeff Thompson | 4e8b770 | 2012-10-21 23:42:27 -0700 | [diff] [blame] | 327 | * Return component as an escaped string according to "CCNx URI Scheme". |
| 328 | * We can't use encodeURIComponent because that doesn't encode all the characters we want to. |
| 329 | */ |
| 330 | Name.toEscapedString = function(component) { |
| 331 | var result = ""; |
| 332 | var gotNonDot = false; |
| 333 | for (var i = 0; i < component.length; ++i) { |
| 334 | if (component[i] != 0x2e) { |
| 335 | gotNonDot = true; |
| 336 | break; |
| 337 | } |
| 338 | } |
| 339 | if (!gotNonDot) { |
| 340 | // Special case for component of zero or more periods. Add 3 periods. |
| 341 | result = "..."; |
| 342 | for (var i = 0; i < component.length; ++i) |
| 343 | result += "."; |
| 344 | } |
| 345 | else { |
| 346 | for (var i = 0; i < component.length; ++i) { |
Jeff Thompson | d855602 | 2013-07-02 18:15:46 -0700 | [diff] [blame] | 347 | var x = component[i]; |
Jeff Thompson | 4e8b770 | 2012-10-21 23:42:27 -0700 | [diff] [blame] | 348 | // Check for 0-9, A-Z, a-z, (+), (-), (.), (_) |
Jeff Thompson | d855602 | 2013-07-02 18:15:46 -0700 | [diff] [blame] | 349 | if (x >= 0x30 && x <= 0x39 || x >= 0x41 && x <= 0x5a || |
| 350 | x >= 0x61 && x <= 0x7a || x == 0x2b || x == 0x2d || |
| 351 | x == 0x2e || x == 0x5f) |
| 352 | result += String.fromCharCode(x); |
Jeff Thompson | 4e8b770 | 2012-10-21 23:42:27 -0700 | [diff] [blame] | 353 | else |
Jeff Thompson | d855602 | 2013-07-02 18:15:46 -0700 | [diff] [blame] | 354 | result += "%" + (x < 16 ? "0" : "") + x.toString(16).toUpperCase(); |
Jeff Thompson | 4e8b770 | 2012-10-21 23:42:27 -0700 | [diff] [blame] | 355 | } |
| 356 | } |
| 357 | return result; |
| 358 | }; |
Wentao Shang | b42483a | 2013-01-03 15:32:32 -0800 | [diff] [blame] | 359 | |
Jeff Thompson | 78849a7 | 2013-02-03 22:35:54 -0800 | [diff] [blame] | 360 | /* |
| 361 | * Return component as a Uint8Array by decoding the escapedString according to "CCNx URI Scheme". |
| 362 | * If escapedString is "", "." or ".." then return null, which means to skip the component in the name. |
| 363 | */ |
| 364 | Name.fromEscapedString = function(escapedString) { |
| 365 | var component = unescape(escapedString.trim()); |
| 366 | |
| 367 | if (component.match(/[^.]/) == null) { |
| 368 | // Special case for component of only periods. |
| 369 | if (component.length <= 2) |
| 370 | // Zero, one or two periods is illegal. Ignore this componenent to be |
| 371 | // consistent with the C implementation. |
| 372 | return null; |
| 373 | else |
| 374 | // Remove 3 periods. |
| 375 | return DataUtils.toNumbersFromString(component.substr(3, component.length - 3)); |
| 376 | } |
| 377 | else |
| 378 | return DataUtils.toNumbersFromString(component); |
| 379 | } |
| 380 | |
Wentao Shang | b42483a | 2013-01-03 15:32:32 -0800 | [diff] [blame] | 381 | Name.prototype.match = function(/*Name*/ name) { |
| 382 | var i_name = this.components; |
| 383 | var o_name = name.components; |
| 384 | |
| 385 | // The intrest name is longer than the name we are checking it against. |
| 386 | if (i_name.length > o_name.length) |
| 387 | return false; |
| 388 | |
| 389 | // Check if at least one of given components doesn't match. |
| 390 | for (var i = 0; i < i_name.length; ++i) { |
| 391 | if (!DataUtils.arraysEqual(i_name[i], o_name[i])) |
| 392 | return false; |
| 393 | } |
| 394 | |
| 395 | return true; |
| 396 | }; |