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