blob: 833f3838446c8dd4e66ef2061999a97396223171 [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;
149 }
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
Jeff Thompsonbd829262012-11-30 22:28:37 -0800162// Return the escaped name string according to "CCNx URI Scheme".
Jeff Thompson4e8b7702012-10-21 23:42:27 -0700163Name.prototype.to_uri = function() {
Jeff Thompson95306632013-01-23 19:20:37 -0800164 if (this.components.length == 0)
165 return "/";
166
Jeff Thompson4e8b7702012-10-21 23:42:27 -0700167 var result = "";
168
169 for(var i = 0; i < this.components.length; ++i)
170 result += "/"+ Name.toEscapedString(this.components[i]);
171
172 return result;
173};
174
Jeff Thompsonff5a8dc2013-02-28 22:24:53 -0800175/**
176* @brief Add component that represents a segment number
177*
178* @param number Segment number (integer is expected)
179*
180* This component has a special format handling:
181* - if number is zero, then %00 is added
182* - if number is between 1 and 255, %00%01 .. %00%FF is added
183* - ...
184*/
185Name.prototype.addSegment = function(number) {
186 var segmentNumberBigEndian = DataUtils.nonNegativeIntToBigEndian(number);
187 // Put a 0 byte in front.
188 var segmentNumberComponent = new Uint8Array(segmentNumberBigEndian.length + 1);
189 segmentNumberComponent.set(segmentNumberBigEndian, 1);
190
191 this.components.push(segmentNumberComponent);
192 return this;
193};
194
Jeff Thompson87021f72012-11-18 17:40:06 -0800195/*
196 * Return a new Name with the first nComponents components of this Name.
197 */
198Name.prototype.getPrefix = function(nComponents) {
199 return new Name(this.components.slice(0, nComponents));
200}
201
202/*
203 * Return a new ArrayBuffer of the component at i.
204 */
205Name.prototype.getComponent = function(i) {
206 var result = new ArrayBuffer(this.components[i].length);
207 new Uint8Array(result).set(this.components[i]);
208 return result;
209}
210
Jeff Thompson16a35f72012-11-25 08:07:33 -0800211/*
212 * The "file name" in a name is the last component that isn't blank and doesn't start with one of the
213 * special marker octets (for version, etc.). Return the index in this.components of
214 * the file name, or -1 if not found.
215 */
216Name.prototype.indexOfFileName = function() {
217 for (var i = this.components.length - 1; i >= 0; --i) {
218 var component = this.components[i];
219 if (component.length <= 0)
220 continue;
221
222 if (component[0] == 0 || component[0] == 0xC0 || component[0] == 0xC1 ||
223 (component[0] >= 0xF5 && component[0] <= 0xFF))
224 continue;
225
226 return i;
227 }
228
229 return -1;
230}
231
Jeff Thompson990e3002012-12-02 23:29:36 -0800232/*
Jeff Thompson77fc9b72012-12-16 12:38:09 -0800233 * Return true if this Name has the same components as name.
234 */
235Name.prototype.equalsName = function(name) {
236 if (this.components.length != name.components.length)
237 return false;
238
239 // Start from the last component because they are more likely to differ.
240 for (var i = this.components.length - 1; i >= 0; --i) {
241 if (!DataUtils.arraysEqual(this.components[i], name.components[i]))
242 return false;
243 }
244
245 return true;
246}
247
248/*
Jeff Thompson990e3002012-12-02 23:29:36 -0800249 * Find the last component in name that has a ContentDigest and return the digest value as Uint8Array,
Jeff Thompsonac3e62c2012-12-16 19:39:58 -0800250 * or null if not found. See Name.getComponentContentDigestValue.
Jeff Thompson990e3002012-12-02 23:29:36 -0800251 */
252Name.prototype.getContentDigestValue = function() {
Jeff Thompson990e3002012-12-02 23:29:36 -0800253 for (var i = this.components.length - 1; i >= 0; --i) {
Jeff Thompsonac3e62c2012-12-16 19:39:58 -0800254 var digestValue = Name.getComponentContentDigestValue(this.components[i]);
255 if (digestValue != null)
256 return digestValue;
Jeff Thompson990e3002012-12-02 23:29:36 -0800257 }
258
259 return null;
260}
261
Jeff Thompsonac3e62c2012-12-16 19:39:58 -0800262/*
263 * If component is a ContentDigest, return the digest value as a Uint8Array subarray (don't modify!).
264 * If not a ContentDigest, return null.
265 * A ContentDigest component is Name.ContentDigestPrefix + 32 bytes + Name.ContentDigestSuffix.
266 */
267Name.getComponentContentDigestValue = function(component) {
268 var digestComponentLength = Name.ContentDigestPrefix.length + 32 + Name.ContentDigestSuffix.length;
269 // Check for the correct length and equal ContentDigestPrefix and ContentDigestSuffix.
270 if (component.length == digestComponentLength &&
271 DataUtils.arraysEqual(component.subarray(0, Name.ContentDigestPrefix.length),
272 Name.ContentDigestPrefix) &&
273 DataUtils.arraysEqual(component.subarray
274 (component.length - Name.ContentDigestSuffix.length, component.length),
275 Name.ContentDigestSuffix))
276 return component.subarray(Name.ContentDigestPrefix.length, Name.ContentDigestPrefix.length + 32);
277 else
278 return null;
279}
280
Jeff Thompson990e3002012-12-02 23:29:36 -0800281// Meta GUID "%C1.M.G%C1" + ContentDigest with a 32 byte BLOB.
282Name.ContentDigestPrefix = new Uint8Array([0xc1, 0x2e, 0x4d, 0x2e, 0x47, 0xc1, 0x01, 0xaa, 0x02, 0x85]);
283Name.ContentDigestSuffix = new Uint8Array([0x00]);
284
Jeff Thompson78849a72013-02-03 22:35:54 -0800285/*
Jeff Thompson4e8b7702012-10-21 23:42:27 -0700286 * Return component as an escaped string according to "CCNx URI Scheme".
287 * We can't use encodeURIComponent because that doesn't encode all the characters we want to.
288 */
289Name.toEscapedString = function(component) {
290 var result = "";
291 var gotNonDot = false;
292 for (var i = 0; i < component.length; ++i) {
293 if (component[i] != 0x2e) {
294 gotNonDot = true;
295 break;
296 }
297 }
298 if (!gotNonDot) {
299 // Special case for component of zero or more periods. Add 3 periods.
300 result = "...";
301 for (var i = 0; i < component.length; ++i)
302 result += ".";
303 }
304 else {
305 for (var i = 0; i < component.length; ++i) {
306 var value = component[i];
307 // Check for 0-9, A-Z, a-z, (+), (-), (.), (_)
308 if (value >= 0x30 && value <= 0x39 || value >= 0x41 && value <= 0x5a ||
309 value >= 0x61 && value <= 0x7a || value == 0x2b || value == 0x2d ||
310 value == 0x2e || value == 0x5f)
311 result += String.fromCharCode(value);
312 else
313 result += "%" + (value < 16 ? "0" : "") + value.toString(16).toUpperCase();
314 }
315 }
316 return result;
317};
Wentao Shangb42483a2013-01-03 15:32:32 -0800318
Jeff Thompson78849a72013-02-03 22:35:54 -0800319/*
320 * Return component as a Uint8Array by decoding the escapedString according to "CCNx URI Scheme".
321 * If escapedString is "", "." or ".." then return null, which means to skip the component in the name.
322 */
323Name.fromEscapedString = function(escapedString) {
324 var component = unescape(escapedString.trim());
325
326 if (component.match(/[^.]/) == null) {
327 // Special case for component of only periods.
328 if (component.length <= 2)
329 // Zero, one or two periods is illegal. Ignore this componenent to be
330 // consistent with the C implementation.
331 return null;
332 else
333 // Remove 3 periods.
334 return DataUtils.toNumbersFromString(component.substr(3, component.length - 3));
335 }
336 else
337 return DataUtils.toNumbersFromString(component);
338}
339
Wentao Shangb42483a2013-01-03 15:32:32 -0800340Name.prototype.match = function(/*Name*/ name) {
341 var i_name = this.components;
342 var o_name = name.components;
343
344 // The intrest name is longer than the name we are checking it against.
345 if (i_name.length > o_name.length)
346 return false;
347
348 // Check if at least one of given components doesn't match.
349 for (var i = 0; i < i_name.length; ++i) {
350 if (!DataUtils.arraysEqual(i_name[i], o_name[i]))
351 return false;
352 }
353
354 return true;
355};