blob: cda44bbc2bfe9a8b068e33f0431172bf047a90ec [file] [log] [blame]
Wentao Shangbd63e462012-12-03 16:19:33 -08001/**
Jeff Thompson87021f72012-11-18 17:40:06 -08002 * @author: Meki Cheraoui, Jeff Thompson
Jeff Thompson745026e2012-10-13 12:49:20 -07003 * See COPYING for copyright and distribution information.
Jeff Thompson4e8b7702012-10-21 23:42:27 -07004 * This class represents a Name as an array of components where each is a byte array.
Meki Cherkaouif441d3a2012-04-22 15:17:52 -07005 */
6
Jeff Thompson2b14c7e2013-07-29 15:09:56 -07007/**
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 Thompson87021f72012-11-18 17:40:06 -080014 */
Jeff Thompson2b14c7e2013-07-29 15:09:56 -070015var 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 Cherkaouif441d3a2012-04-22 15:17:52 -070019 }
Jeff Thompson2b14c7e2013-07-29 15:09:56 -070020 else if(typeof components === 'object'){
Jeff Thompson87021f72012-11-18 17:40:06 -080021 this.components = [];
Jeff Thompson2b14c7e2013-07-29 15:09:56 -070022 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 Cherkaouif441d3a2012-04-22 15:17:52 -070028 }
Jeff Thompson2b14c7e2013-07-29 15:09:56 -070029 else if(components==null)
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070030 this.components =[];
Jeff Thompson87021f72012-11-18 17:40:06 -080031 else
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070032 if(LOG>1)console.log("NO CONTENT NAME GIVEN");
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070033};
34
Jeff Thompson0d3f2032012-11-04 12:55:27 -080035Name.prototype.getName = function() {
36 return this.to_uri();
Meki Cherkaoui8f173612012-06-06 01:05:40 -070037};
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070038
Jeff Thompson2b14c7e2013-07-29 15:09:56 -070039/** Parse uri as a URI and return an array of Uint8Array components.
Jeff Thompson87021f72012-11-18 17:40:06 -080040 */
Jeff Thompsond8556022013-07-02 18:15:46 -070041Name.createNameArray = function(uri) {
42 uri = uri.trim();
43 if (uri.length <= 0)
Jeff Thompson0d3f2032012-11-04 12:55:27 -080044 return [];
45
Jeff Thompsond8556022013-07-02 18:15:46 -070046 var iColon = uri.indexOf(':');
Jeff Thompson0d3f2032012-11-04 12:55:27 -080047 if (iColon >= 0) {
48 // Make sure the colon came before a '/'.
Jeff Thompsond8556022013-07-02 18:15:46 -070049 var iFirstSlash = uri.indexOf('/');
Jeff Thompson0d3f2032012-11-04 12:55:27 -080050 if (iFirstSlash < 0 || iColon < iFirstSlash)
Jeff Thompsonbd829262012-11-30 22:28:37 -080051 // Omit the leading protocol such as ndn:
Jeff Thompsond8556022013-07-02 18:15:46 -070052 uri = uri.substr(iColon + 1, uri.length - iColon - 1).trim();
Jeff Thompson4e8b7702012-10-21 23:42:27 -070053 }
Jeff Thompson0d3f2032012-11-04 12:55:27 -080054
Jeff Thompsond8556022013-07-02 18:15:46 -070055 if (uri[0] == '/') {
56 if (uri.length >= 2 && uri[1] == '/') {
Jeff Thompson0d3f2032012-11-04 12:55:27 -080057 // Strip the authority following "//".
Jeff Thompsond8556022013-07-02 18:15:46 -070058 var iAfterAuthority = uri.indexOf('/', 2);
Jeff Thompson0d3f2032012-11-04 12:55:27 -080059 if (iAfterAuthority < 0)
60 // Unusual case: there was only an authority.
61 return [];
62 else
Jeff Thompsond8556022013-07-02 18:15:46 -070063 uri = uri.substr(iAfterAuthority + 1, uri.length - iAfterAuthority - 1).trim();
Jeff Thompson0d3f2032012-11-04 12:55:27 -080064 }
65 else
Jeff Thompsond8556022013-07-02 18:15:46 -070066 uri = uri.substr(1, uri.length - 1).trim();
Jeff Thompson0d3f2032012-11-04 12:55:27 -080067 }
68
Jeff Thompsond8556022013-07-02 18:15:46 -070069 var array = uri.split('/');
Jeff Thompson9b411002012-10-21 17:35:16 -070070
71 // Unescape the components.
Jeff Thompson4e8b7702012-10-21 23:42:27 -070072 for (var i = 0; i < array.length; ++i) {
Jeff Thompson78849a72013-02-03 22:35:54 -080073 var component = Name.fromEscapedString(array[i]);
Jeff Thompson4e8b7702012-10-21 23:42:27 -070074
Jeff Thompson78849a72013-02-03 22:35:54 -080075 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 Thompson4e8b7702012-10-21 23:42:27 -070080 }
81 else
82 array[i] = component;
83 }
Jeff Thompson9b411002012-10-21 17:35:16 -070084
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070085 return array;
86}
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070087
88
Jeff Thompsonf3bd3592012-09-29 23:25:30 -070089Name.prototype.from_ccnb = function(/*XMLDecoder*/ decoder) {
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070090 decoder.readStartElement(this.getElementLabel());
91
92
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070093 this.components = new Array(); //new ArrayList<byte []>();
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070094
95 while (decoder.peekStartElement(CCNProtocolDTags.Component)) {
96 this.add(decoder.readBinaryElement(CCNProtocolDTags.Component));
97 }
98
99 decoder.readEndElement();
100};
101
Jeff Thompsonf3bd3592012-09-29 23:25:30 -0700102Name.prototype.to_ccnb = function(/*XMLEncoder*/ encoder) {
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700103
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700104 if( this.components ==null )
Jeff Thompson34a2ec02012-09-29 21:47:05 -0700105 throw new Error("CANNOT ENCODE EMPTY CONTENT NAME");
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700106
107 encoder.writeStartElement(this.getElementLabel());
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700108 var count = this.components.length;
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700109 for (var i=0; i < count; i++) {
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700110 encoder.writeElement(CCNProtocolDTags.Component, this.components[i]);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700111 }
112 encoder.writeEndElement();
113};
114
Jeff Thompsonf3bd3592012-09-29 23:25:30 -0700115Name.prototype.getElementLabel = function(){
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700116 return CCNProtocolDTags.Name;
117};
118
Jeff Thompson2b14c7e2013-07-29 15:09:56 -0700119/**
120 * Convert the component to a Uint8Array and add to this Name.
Jeff Thompsonff5a8dc2013-02-28 22:24:53 -0800121 * Return this Name object to allow chaining calls to add.
Jeff Thompson2b14c7e2013-07-29 15:09:56 -0700122 * @param {String|Array<number>|ArrayBuffer|Uint8Array|Name} component If a component is a string, encode as utf8.
123 * @returns {Name}
Jeff Thompson87021f72012-11-18 17:40:06 -0800124 */
125Name.prototype.add = function(component){
Jeff Thompson9ab151a2012-11-24 21:22:30 -0800126 var result;
Jeff Thompson87021f72012-11-18 17:40:06 -0800127 if(typeof component == 'string')
128 result = DataUtils.stringToUtf8Array(component);
Jeff Thompson87021f72012-11-18 17:40:06 -0800129 else if(typeof component == 'object' && component instanceof Uint8Array)
130 result = new Uint8Array(component);
Jeff Thompson8a97ac52012-11-24 23:16:02 -0800131 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 Thompsonff5a8dc2013-02-28 22:24:53 -0800136 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 Afanasyev78122d52013-02-27 18:49:54 -0800147 }
Jeff Thompson51ab7652012-11-24 21:57:37 -0800148 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 Thompson87021f72012-11-18 17:40:06 -0800152 else
Jeff Thompson9ab151a2012-11-24 21:22:30 -0800153 throw new Error("Cannot add Name element at index " + this.components.length +
154 ": Invalid type");
Jeff Thompson87021f72012-11-18 17:40:06 -0800155
Jeff Thompsonff5a8dc2013-02-28 22:24:53 -0800156 this.components.push(result);
157 return this;
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700158};
159
Alexander Afanasyevc0838782013-02-27 19:13:06 -0800160/**
Jeff Thompson2b14c7e2013-07-29 15:09:56 -0700161 * Return the escaped name string according to "NDNx URI Scheme".
162 * @returns {String}
Alexander Afanasyevc0838782013-02-27 19:13:06 -0800163 */
Jeff Thompson4e8b7702012-10-21 23:42:27 -0700164Name.prototype.to_uri = function() {
Jeff Thompson95306632013-01-23 19:20:37 -0800165 if (this.components.length == 0)
166 return "/";
167
Jeff Thompson4e8b7702012-10-21 23:42:27 -0700168 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 Afanasyev78122d52013-02-27 18:49:54 -0800175
Jeff Thompsonff5a8dc2013-02-28 22:24:53 -0800176/**
Jeff Thompson2b14c7e2013-07-29 15:09:56 -0700177 * 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 Thompsonff5a8dc2013-02-28 22:24:53 -0800186Name.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 Thompson4e8b7702012-10-21 23:42:27 -0700194};
195
Jeff Thompson2b14c7e2013-07-29 15:09:56 -0700196/**
Jeff Thompson87021f72012-11-18 17:40:06 -0800197 * Return a new Name with the first nComponents components of this Name.
198 */
199Name.prototype.getPrefix = function(nComponents) {
200 return new Name(this.components.slice(0, nComponents));
201}
202
Alexander Afanasyev13bd6482013-03-02 13:50:24 -0800203/**
204 * @brief Get prefix of the name, containing less minusComponents right components
205 * @param minusComponents number of components to cut from the back
206 */
207Name.prototype.cut = function (minusComponents) {
208 return new Name(this.components.slice(0, this.components.length-1));
209}
210
Jeff Thompson2b14c7e2013-07-29 15:09:56 -0700211/**
Jeff Thompson87021f72012-11-18 17:40:06 -0800212 * Return a new ArrayBuffer of the component at i.
213 */
214Name.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 Thompson2b14c7e2013-07-29 15:09:56 -0700220/**
Jeff Thompson16a35f72012-11-25 08:07:33 -0800221 * 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 */
225Name.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 Thompson2b14c7e2013-07-29 15:09:56 -0700241/**
Jeff Thompson77fc9b72012-12-16 12:38:09 -0800242 * Return true if this Name has the same components as name.
243 */
244Name.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 Thompson2b14c7e2013-07-29 15:09:56 -0700257/**
Jeff Thompson990e3002012-12-02 23:29:36 -0800258 * Find the last component in name that has a ContentDigest and return the digest value as Uint8Array,
Jeff Thompsonac3e62c2012-12-16 19:39:58 -0800259 * or null if not found. See Name.getComponentContentDigestValue.
Jeff Thompson990e3002012-12-02 23:29:36 -0800260 */
261Name.prototype.getContentDigestValue = function() {
Jeff Thompson990e3002012-12-02 23:29:36 -0800262 for (var i = this.components.length - 1; i >= 0; --i) {
Jeff Thompsonac3e62c2012-12-16 19:39:58 -0800263 var digestValue = Name.getComponentContentDigestValue(this.components[i]);
264 if (digestValue != null)
265 return digestValue;
Jeff Thompson990e3002012-12-02 23:29:36 -0800266 }
267
268 return null;
269}
270
Jeff Thompson2b14c7e2013-07-29 15:09:56 -0700271/**
Jeff Thompsonac3e62c2012-12-16 19:39:58 -0800272 * 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 */
276Name.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 Thompson990e3002012-12-02 23:29:36 -0800290// Meta GUID "%C1.M.G%C1" + ContentDigest with a 32 byte BLOB.
291Name.ContentDigestPrefix = new Uint8Array([0xc1, 0x2e, 0x4d, 0x2e, 0x47, 0xc1, 0x01, 0xaa, 0x02, 0x85]);
292Name.ContentDigestSuffix = new Uint8Array([0x00]);
293
Jeff Thompson2b14c7e2013-07-29 15:09:56 -0700294/**
Jeff Thompson4e8b7702012-10-21 23:42:27 -0700295 * 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 */
298Name.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 Thompsond8556022013-07-02 18:15:46 -0700315 var x = component[i];
Jeff Thompson4e8b7702012-10-21 23:42:27 -0700316 // Check for 0-9, A-Z, a-z, (+), (-), (.), (_)
Jeff Thompsond8556022013-07-02 18:15:46 -0700317 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 Thompson4e8b7702012-10-21 23:42:27 -0700321 else
Jeff Thompsond8556022013-07-02 18:15:46 -0700322 result += "%" + (x < 16 ? "0" : "") + x.toString(16).toUpperCase();
Jeff Thompson4e8b7702012-10-21 23:42:27 -0700323 }
324 }
325 return result;
326};
Wentao Shangb42483a2013-01-03 15:32:32 -0800327
Jeff Thompson2b14c7e2013-07-29 15:09:56 -0700328/**
Jeff Thompson78849a72013-02-03 22:35:54 -0800329 * 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 */
332Name.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 Shangb42483a2013-01-03 15:32:32 -0800349Name.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};