blob: c4bb39f3cb84c8565910c1a41fbcbb878f63d1ea [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 Thompson87021f72012-11-18 17:40:06 -08007/*
8 * Create a new Name from _components.
Jeff Thompsonff5a8dc2013-02-28 22:24:53 -08009 * 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 Thompson87021f72012-11-18 17:40:06 -080013 * Convert and store as an array of Uint8Array.
14 * If a component is a string, encode as utf8.
15 */
Jeff Thompsonf3bd3592012-09-29 23:25:30 -070016var Name = function Name(_components){
Jeff Thompson87021f72012-11-18 17:40:06 -080017 if( typeof _components == 'string') {
Jeff Thompson86aea882012-09-29 17:32:48 -070018 if(LOG>3)console.log('Content Name String '+_components);
Jeff Thompson87021f72012-11-18 17:40:06 -080019 this.components = Name.createNameArray(_components);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070020 }
Jeff Thompson87021f72012-11-18 17:40:06 -080021 else if(typeof _components === 'object'){
Jeff Thompson87021f72012-11-18 17:40:06 -080022 this.components = [];
Jeff Thompsonff5a8dc2013-02-28 22:24:53 -080023 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 Cherkaouif441d3a2012-04-22 15:17:52 -070029 }
Jeff Thompson87021f72012-11-18 17:40:06 -080030 else if(_components==null)
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070031 this.components =[];
Jeff Thompson87021f72012-11-18 17:40:06 -080032 else
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070033 if(LOG>1)console.log("NO CONTENT NAME GIVEN");
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070034};
35
Jeff Thompson0d3f2032012-11-04 12:55:27 -080036Name.prototype.getName = function() {
37 return this.to_uri();
Meki Cherkaoui8f173612012-06-06 01:05:40 -070038};
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070039
Jeff Thompson87021f72012-11-18 17:40:06 -080040/* Parse name as a URI and return an array of Uint8Array components.
41 *
42 */
Jeff Thompson0d3f2032012-11-04 12:55:27 -080043Name.createNameArray = function(name) {
44 name = name.trim();
45 if (name.length <= 0)
46 return [];
47
48 var iColon = name.indexOf(':');
49 if (iColon >= 0) {
50 // Make sure the colon came before a '/'.
51 var iFirstSlash = name.indexOf('/');
52 if (iFirstSlash < 0 || iColon < iFirstSlash)
Jeff Thompsonbd829262012-11-30 22:28:37 -080053 // Omit the leading protocol such as ndn:
Jeff Thompson0d3f2032012-11-04 12:55:27 -080054 name = name.substr(iColon + 1, name.length - iColon - 1).trim();
Jeff Thompson4e8b7702012-10-21 23:42:27 -070055 }
Jeff Thompson0d3f2032012-11-04 12:55:27 -080056
57 if (name[0] == '/') {
58 if (name.length >= 2 && name[1] == '/') {
59 // Strip the authority following "//".
60 var iAfterAuthority = name.indexOf('/', 2);
61 if (iAfterAuthority < 0)
62 // Unusual case: there was only an authority.
63 return [];
64 else
65 name = name.substr(iAfterAuthority + 1, name.length - iAfterAuthority - 1).trim();
66 }
67 else
68 name = name.substr(1, name.length - 1).trim();
69 }
70
71 var array = name.split('/');
Jeff Thompson9b411002012-10-21 17:35:16 -070072
73 // Unescape the components.
Jeff Thompson4e8b7702012-10-21 23:42:27 -070074 for (var i = 0; i < array.length; ++i) {
Jeff Thompson78849a72013-02-03 22:35:54 -080075 var component = Name.fromEscapedString(array[i]);
Jeff Thompson4e8b7702012-10-21 23:42:27 -070076
Jeff Thompson78849a72013-02-03 22:35:54 -080077 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 Thompson4e8b7702012-10-21 23:42:27 -070082 }
83 else
84 array[i] = component;
85 }
Jeff Thompson9b411002012-10-21 17:35:16 -070086
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070087 return array;
88}
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070089
90
Jeff Thompsonf3bd3592012-09-29 23:25:30 -070091Name.prototype.from_ccnb = function(/*XMLDecoder*/ decoder) {
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070092 decoder.readStartElement(this.getElementLabel());
93
94
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070095 this.components = new Array(); //new ArrayList<byte []>();
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070096
97 while (decoder.peekStartElement(CCNProtocolDTags.Component)) {
98 this.add(decoder.readBinaryElement(CCNProtocolDTags.Component));
99 }
100
101 decoder.readEndElement();
102};
103
Jeff Thompsonf3bd3592012-09-29 23:25:30 -0700104Name.prototype.to_ccnb = function(/*XMLEncoder*/ encoder) {
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700105
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700106 if( this.components ==null )
Jeff Thompson34a2ec02012-09-29 21:47:05 -0700107 throw new Error("CANNOT ENCODE EMPTY CONTENT NAME");
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700108
109 encoder.writeStartElement(this.getElementLabel());
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700110 var count = this.components.length;
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700111 for (var i=0; i < count; i++) {
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700112 encoder.writeElement(CCNProtocolDTags.Component, this.components[i]);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700113 }
114 encoder.writeEndElement();
115};
116
Jeff Thompsonf3bd3592012-09-29 23:25:30 -0700117Name.prototype.getElementLabel = function(){
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700118 return CCNProtocolDTags.Name;
119};
120
Jeff Thompson87021f72012-11-18 17:40:06 -0800121/*
Jeff Thompsonff5a8dc2013-02-28 22:24:53 -0800122 * component is a string, byte array, ArrayBuffer, Uint8Array or Name.
Jeff Thompson87021f72012-11-18 17:40:06 -0800123 * Convert to Uint8Array and add to this Name.
124 * If a component is a string, encode as utf8.
Jeff Thompsonff5a8dc2013-02-28 22:24:53 -0800125 * Return this Name object to allow chaining calls to add.
Jeff Thompson87021f72012-11-18 17:40:06 -0800126 */
127Name.prototype.add = function(component){
Jeff Thompson9ab151a2012-11-24 21:22:30 -0800128 var result;
Jeff Thompson87021f72012-11-18 17:40:06 -0800129 if(typeof component == 'string')
130 result = DataUtils.stringToUtf8Array(component);
Jeff Thompson87021f72012-11-18 17:40:06 -0800131 else if(typeof component == 'object' && component instanceof Uint8Array)
132 result = new Uint8Array(component);
Jeff Thompson8a97ac52012-11-24 23:16:02 -0800133 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 Thompsonff5a8dc2013-02-28 22:24:53 -0800138 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 Afanasyev78122d52013-02-27 18:49:54 -0800149 }
Jeff Thompson51ab7652012-11-24 21:57:37 -0800150 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 Thompson87021f72012-11-18 17:40:06 -0800154 else
Jeff Thompson9ab151a2012-11-24 21:22:30 -0800155 throw new Error("Cannot add Name element at index " + this.components.length +
156 ": Invalid type");
Jeff Thompson87021f72012-11-18 17:40:06 -0800157
Jeff Thompsonff5a8dc2013-02-28 22:24:53 -0800158 this.components.push(result);
159 return this;
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700160};
161
Alexander Afanasyevc0838782013-02-27 19:13:06 -0800162/**
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 */
172Name.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 Thompsonbd829262012-11-30 22:28:37 -0800195// Return the escaped name string according to "CCNx URI Scheme".
Jeff Thompson4e8b7702012-10-21 23:42:27 -0700196Name.prototype.to_uri = function() {
Jeff Thompson95306632013-01-23 19:20:37 -0800197 if (this.components.length == 0)
198 return "/";
199
Jeff Thompson4e8b7702012-10-21 23:42:27 -0700200 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 Afanasyev78122d52013-02-27 18:49:54 -0800207
Jeff Thompsonff5a8dc2013-02-28 22:24:53 -0800208/**
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*/
218Name.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 Thompson4e8b7702012-10-21 23:42:27 -0700226};
227
Jeff Thompson87021f72012-11-18 17:40:06 -0800228/*
229 * Return a new Name with the first nComponents components of this Name.
230 */
231Name.prototype.getPrefix = function(nComponents) {
232 return new Name(this.components.slice(0, nComponents));
233}
234
Alexander Afanasyev13bd6482013-03-02 13:50:24 -0800235/**
236 * @brief Get prefix of the name, containing less minusComponents right components
237 * @param minusComponents number of components to cut from the back
238 */
239Name.prototype.cut = function (minusComponents) {
240 return new Name(this.components.slice(0, this.components.length-1));
241}
242
Jeff Thompson87021f72012-11-18 17:40:06 -0800243/*
244 * Return a new ArrayBuffer of the component at i.
245 */
246Name.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 Thompson16a35f72012-11-25 08:07:33 -0800252/*
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 */
257Name.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 Thompson990e3002012-12-02 23:29:36 -0800273/*
Jeff Thompson77fc9b72012-12-16 12:38:09 -0800274 * Return true if this Name has the same components as name.
275 */
276Name.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 Thompson990e3002012-12-02 23:29:36 -0800290 * Find the last component in name that has a ContentDigest and return the digest value as Uint8Array,
Jeff Thompsonac3e62c2012-12-16 19:39:58 -0800291 * or null if not found. See Name.getComponentContentDigestValue.
Jeff Thompson990e3002012-12-02 23:29:36 -0800292 */
293Name.prototype.getContentDigestValue = function() {
Jeff Thompson990e3002012-12-02 23:29:36 -0800294 for (var i = this.components.length - 1; i >= 0; --i) {
Jeff Thompsonac3e62c2012-12-16 19:39:58 -0800295 var digestValue = Name.getComponentContentDigestValue(this.components[i]);
296 if (digestValue != null)
297 return digestValue;
Jeff Thompson990e3002012-12-02 23:29:36 -0800298 }
299
300 return null;
301}
302
Jeff Thompsonac3e62c2012-12-16 19:39:58 -0800303/*
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 */
308Name.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 Thompson990e3002012-12-02 23:29:36 -0800322// Meta GUID "%C1.M.G%C1" + ContentDigest with a 32 byte BLOB.
323Name.ContentDigestPrefix = new Uint8Array([0xc1, 0x2e, 0x4d, 0x2e, 0x47, 0xc1, 0x01, 0xaa, 0x02, 0x85]);
324Name.ContentDigestSuffix = new Uint8Array([0x00]);
325
Jeff Thompson78849a72013-02-03 22:35:54 -0800326/*
Jeff Thompson4e8b7702012-10-21 23:42:27 -0700327 * 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 */
330Name.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) {
347 var value = component[i];
348 // Check for 0-9, A-Z, a-z, (+), (-), (.), (_)
349 if (value >= 0x30 && value <= 0x39 || value >= 0x41 && value <= 0x5a ||
350 value >= 0x61 && value <= 0x7a || value == 0x2b || value == 0x2d ||
351 value == 0x2e || value == 0x5f)
352 result += String.fromCharCode(value);
353 else
354 result += "%" + (value < 16 ? "0" : "") + value.toString(16).toUpperCase();
355 }
356 }
357 return result;
358};
Wentao Shangb42483a2013-01-03 15:32:32 -0800359
Jeff Thompson78849a72013-02-03 22:35:54 -0800360/*
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 */
364Name.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 Shangb42483a2013-01-03 15:32:32 -0800381Name.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};