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