blob: 1c404fdf42d7e97ac4d618efbbdac4552479c6bb [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 */
Alexander Afanasyev78122d52013-02-27 18:49:54 -08006
Jeff Thompson87021f72012-11-18 17:40:06 -08007/*
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 Afanasyev78122d52013-02-27 18:49:54 -080010 * where each is a string, byte array, ArrayBuffer or Uint8Array.
Jeff Thompson87021f72012-11-18 17:40:06 -080011 * Convert and store as an array of Uint8Array.
12 * If a component is a string, encode as utf8.
13 */
Jeff Thompsonf3bd3592012-09-29 23:25:30 -070014var Name = function Name(_components){
Alexander Afanasyev78122d52013-02-27 18:49:54 -080015 if( typeof _components == 'string') {
Jeff Thompson86aea882012-09-29 17:32:48 -070016 if(LOG>3)console.log('Content Name String '+_components);
Jeff Thompson87021f72012-11-18 17:40:06 -080017 this.components = Name.createNameArray(_components);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070018 }
Alexander Afanasyev78122d52013-02-27 18:49:54 -080019 else if(typeof _components === 'object'){
Jeff Thompson86aea882012-09-29 17:32:48 -070020 if(LOG>4)console.log('Content Name Array '+_components);
Jeff Thompson87021f72012-11-18 17:40:06 -080021 this.components = [];
22 for (var i = 0; i < _components.length; ++i)
23 this.add(_components[i]);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070024 }
Jeff Thompson87021f72012-11-18 17:40:06 -080025 else if(_components==null)
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070026 this.components =[];
Jeff Thompson87021f72012-11-18 17:40:06 -080027 else
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070028 if(LOG>1)console.log("NO CONTENT NAME GIVEN");
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070029};
30
Jeff Thompson0d3f2032012-11-04 12:55:27 -080031Name.prototype.getName = function() {
32 return this.to_uri();
Meki Cherkaoui8f173612012-06-06 01:05:40 -070033};
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070034
Jeff Thompson87021f72012-11-18 17:40:06 -080035/* Parse name as a URI and return an array of Uint8Array components.
36 *
37 */
Jeff Thompson0d3f2032012-11-04 12:55:27 -080038Name.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 Thompsonbd829262012-11-30 22:28:37 -080048 // Omit the leading protocol such as ndn:
Jeff Thompson0d3f2032012-11-04 12:55:27 -080049 name = name.substr(iColon + 1, name.length - iColon - 1).trim();
Jeff Thompson4e8b7702012-10-21 23:42:27 -070050 }
Alexander Afanasyev78122d52013-02-27 18:49:54 -080051
Jeff Thompson0d3f2032012-11-04 12:55:27 -080052 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 Afanasyev78122d52013-02-27 18:49:54 -080067
Jeff Thompson9b411002012-10-21 17:35:16 -070068 // Unescape the components.
Jeff Thompson4e8b7702012-10-21 23:42:27 -070069 for (var i = 0; i < array.length; ++i) {
Jeff Thompson78849a72013-02-03 22:35:54 -080070 var component = Name.fromEscapedString(array[i]);
Alexander Afanasyev78122d52013-02-27 18:49:54 -080071
Jeff Thompson78849a72013-02-03 22:35:54 -080072 if (component == null) {
73 // Ignore the illegal componenent. This also gets rid of a trailing '/'.
74 array.splice(i, 1);
Alexander Afanasyev78122d52013-02-27 18:49:54 -080075 --i;
Jeff Thompson78849a72013-02-03 22:35:54 -080076 continue;
Jeff Thompson4e8b7702012-10-21 23:42:27 -070077 }
78 else
79 array[i] = component;
80 }
Jeff Thompson9b411002012-10-21 17:35:16 -070081
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070082 return array;
83}
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070084
85
Jeff Thompsonf3bd3592012-09-29 23:25:30 -070086Name.prototype.from_ccnb = function(/*XMLDecoder*/ decoder) {
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070087 decoder.readStartElement(this.getElementLabel());
88
Alexander Afanasyev78122d52013-02-27 18:49:54 -080089
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070090 this.components = new Array(); //new ArrayList<byte []>();
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070091
92 while (decoder.peekStartElement(CCNProtocolDTags.Component)) {
93 this.add(decoder.readBinaryElement(CCNProtocolDTags.Component));
94 }
Alexander Afanasyev78122d52013-02-27 18:49:54 -080095
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070096 decoder.readEndElement();
97};
98
Jeff Thompsonf3bd3592012-09-29 23:25:30 -070099Name.prototype.to_ccnb = function(/*XMLEncoder*/ encoder) {
Alexander Afanasyev78122d52013-02-27 18:49:54 -0800100
101 if( this.components ==null )
Jeff Thompson34a2ec02012-09-29 21:47:05 -0700102 throw new Error("CANNOT ENCODE EMPTY CONTENT NAME");
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700103
104 encoder.writeStartElement(this.getElementLabel());
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700105 var count = this.components.length;
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700106 for (var i=0; i < count; i++) {
Jeff Thompsone85ff1d2012-09-29 21:21:57 -0700107 encoder.writeElement(CCNProtocolDTags.Component, this.components[i]);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700108 }
109 encoder.writeEndElement();
110};
111
Jeff Thompsonf3bd3592012-09-29 23:25:30 -0700112Name.prototype.getElementLabel = function(){
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700113 return CCNProtocolDTags.Name;
114};
115
Jeff Thompson87021f72012-11-18 17:40:06 -0800116/*
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 */
122Name.prototype.add = function(component){
Jeff Thompson9ab151a2012-11-24 21:22:30 -0800123 var result;
Alexander Afanasyev78122d52013-02-27 18:49:54 -0800124 if(typeof component == 'string') {
Jeff Thompson87021f72012-11-18 17:40:06 -0800125 result = DataUtils.stringToUtf8Array(component);
Alexander Afanasyev78122d52013-02-27 18:49:54 -0800126 this.components.push (result);
127 }
128 else if(typeof component == 'object' && component instanceof Uint8Array) {
Jeff Thompson87021f72012-11-18 17:40:06 -0800129 result = new Uint8Array(component);
Alexander Afanasyev78122d52013-02-27 18:49:54 -0800130 this.components.push (result);
131 }
132 else if(typeof component == 'object' && component instanceof ArrayBuffer) {
Jeff Thompson8a97ac52012-11-24 23:16:02 -0800133 // 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 Afanasyev78122d52013-02-27 18:49:54 -0800136 this.components.push(result);
Jeff Thompson8a97ac52012-11-24 23:16:02 -0800137 }
Alexander Afanasyev78122d52013-02-27 18:49:54 -0800138 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 Thompson51ab7652012-11-24 21:57:37 -0800149 // 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 Afanasyev78122d52013-02-27 18:49:54 -0800152 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 Cherkaouif441d3a2012-04-22 15:17:52 -0700159};
160
Alexander Afanasyevc0838782013-02-27 19:13:06 -0800161/**
162 * @brief Add component that represents a segment number
163 *
164 * @param number Segment number (integer is expected)
165 *
166 * This component has a special format handling:
167 * - if number is zero, then %00 is added
168 * - if number is between 1 and 255, %00%01 .. %00%FF is added
169 * - ...
170 */
171Name.prototype.addSegment = function(number) {
172 // step 1: figure out how many bytes will be needed
173 var bytes = 1; // at least 1 byte
174 var test_number = number;
175 while (test_number > 0) {
176 bytes ++;
177 test_number >>= 8;
178 }
179
180 var result = new Uint8Array (bytes);
181 var index = 0;
182 result[index] = 0;
183 index ++;
184 while (number > 0) {
185 result[index] = number & 0xFF;
186 number >>= 8;
187 index ++;
188 }
189
190 this.components.push(result);
191 return this;
192}
193
Jeff Thompsonbd829262012-11-30 22:28:37 -0800194// Return the escaped name string according to "CCNx URI Scheme".
Alexander Afanasyev78122d52013-02-27 18:49:54 -0800195Name.prototype.to_uri = function() {
Jeff Thompson95306632013-01-23 19:20:37 -0800196 if (this.components.length == 0)
197 return "/";
Alexander Afanasyev78122d52013-02-27 18:49:54 -0800198
Jeff Thompson4e8b7702012-10-21 23:42:27 -0700199 var result = "";
Alexander Afanasyev78122d52013-02-27 18:49:54 -0800200
Jeff Thompson4e8b7702012-10-21 23:42:27 -0700201 for(var i = 0; i < this.components.length; ++i)
202 result += "/"+ Name.toEscapedString(this.components[i]);
Alexander Afanasyev78122d52013-02-27 18:49:54 -0800203
204 return result;
Jeff Thompson4e8b7702012-10-21 23:42:27 -0700205};
206
Jeff Thompson87021f72012-11-18 17:40:06 -0800207/*
208 * Return a new Name with the first nComponents components of this Name.
209 */
210Name.prototype.getPrefix = function(nComponents) {
211 return new Name(this.components.slice(0, nComponents));
212}
213
214/*
215 * Return a new ArrayBuffer of the component at i.
216 */
217Name.prototype.getComponent = function(i) {
218 var result = new ArrayBuffer(this.components[i].length);
219 new Uint8Array(result).set(this.components[i]);
220 return result;
221}
222
Jeff Thompson16a35f72012-11-25 08:07:33 -0800223/*
224 * The "file name" in a name is the last component that isn't blank and doesn't start with one of the
225 * special marker octets (for version, etc.). Return the index in this.components of
226 * the file name, or -1 if not found.
227 */
228Name.prototype.indexOfFileName = function() {
229 for (var i = this.components.length - 1; i >= 0; --i) {
230 var component = this.components[i];
231 if (component.length <= 0)
232 continue;
Alexander Afanasyev78122d52013-02-27 18:49:54 -0800233
234 if (component[0] == 0 || component[0] == 0xC0 || component[0] == 0xC1 ||
Jeff Thompson16a35f72012-11-25 08:07:33 -0800235 (component[0] >= 0xF5 && component[0] <= 0xFF))
236 continue;
Alexander Afanasyev78122d52013-02-27 18:49:54 -0800237
Jeff Thompson16a35f72012-11-25 08:07:33 -0800238 return i;
239 }
Alexander Afanasyev78122d52013-02-27 18:49:54 -0800240
Jeff Thompson16a35f72012-11-25 08:07:33 -0800241 return -1;
242}
243
Jeff Thompson990e3002012-12-02 23:29:36 -0800244/*
Jeff Thompson77fc9b72012-12-16 12:38:09 -0800245 * Return true if this Name has the same components as name.
246 */
247Name.prototype.equalsName = function(name) {
248 if (this.components.length != name.components.length)
249 return false;
Alexander Afanasyev78122d52013-02-27 18:49:54 -0800250
Jeff Thompson77fc9b72012-12-16 12:38:09 -0800251 // Start from the last component because they are more likely to differ.
252 for (var i = this.components.length - 1; i >= 0; --i) {
253 if (!DataUtils.arraysEqual(this.components[i], name.components[i]))
254 return false;
255 }
Alexander Afanasyev78122d52013-02-27 18:49:54 -0800256
Jeff Thompson77fc9b72012-12-16 12:38:09 -0800257 return true;
258}
259
260/*
Alexander Afanasyev78122d52013-02-27 18:49:54 -0800261 * Find the last component in name that has a ContentDigest and return the digest value as Uint8Array,
Jeff Thompsonac3e62c2012-12-16 19:39:58 -0800262 * or null if not found. See Name.getComponentContentDigestValue.
Jeff Thompson990e3002012-12-02 23:29:36 -0800263 */
264Name.prototype.getContentDigestValue = function() {
Jeff Thompson990e3002012-12-02 23:29:36 -0800265 for (var i = this.components.length - 1; i >= 0; --i) {
Jeff Thompsonac3e62c2012-12-16 19:39:58 -0800266 var digestValue = Name.getComponentContentDigestValue(this.components[i]);
267 if (digestValue != null)
268 return digestValue;
Jeff Thompson990e3002012-12-02 23:29:36 -0800269 }
Alexander Afanasyev78122d52013-02-27 18:49:54 -0800270
Jeff Thompson990e3002012-12-02 23:29:36 -0800271 return null;
272}
273
Jeff Thompsonac3e62c2012-12-16 19:39:58 -0800274/*
275 * If component is a ContentDigest, return the digest value as a Uint8Array subarray (don't modify!).
276 * If not a ContentDigest, return null.
277 * A ContentDigest component is Name.ContentDigestPrefix + 32 bytes + Name.ContentDigestSuffix.
278 */
279Name.getComponentContentDigestValue = function(component) {
Alexander Afanasyev78122d52013-02-27 18:49:54 -0800280 var digestComponentLength = Name.ContentDigestPrefix.length + 32 + Name.ContentDigestSuffix.length;
Jeff Thompsonac3e62c2012-12-16 19:39:58 -0800281 // Check for the correct length and equal ContentDigestPrefix and ContentDigestSuffix.
282 if (component.length == digestComponentLength &&
Alexander Afanasyev78122d52013-02-27 18:49:54 -0800283 DataUtils.arraysEqual(component.subarray(0, Name.ContentDigestPrefix.length),
Jeff Thompsonac3e62c2012-12-16 19:39:58 -0800284 Name.ContentDigestPrefix) &&
285 DataUtils.arraysEqual(component.subarray
286 (component.length - Name.ContentDigestSuffix.length, component.length),
287 Name.ContentDigestSuffix))
288 return component.subarray(Name.ContentDigestPrefix.length, Name.ContentDigestPrefix.length + 32);
289 else
290 return null;
291}
292
Alexander Afanasyev78122d52013-02-27 18:49:54 -0800293// Meta GUID "%C1.M.G%C1" + ContentDigest with a 32 byte BLOB.
Jeff Thompson990e3002012-12-02 23:29:36 -0800294Name.ContentDigestPrefix = new Uint8Array([0xc1, 0x2e, 0x4d, 0x2e, 0x47, 0xc1, 0x01, 0xaa, 0x02, 0x85]);
295Name.ContentDigestSuffix = new Uint8Array([0x00]);
296
Jeff Thompson78849a72013-02-03 22:35:54 -0800297/*
Jeff Thompson4e8b7702012-10-21 23:42:27 -0700298 * Return component as an escaped string according to "CCNx URI Scheme".
299 * We can't use encodeURIComponent because that doesn't encode all the characters we want to.
300 */
301Name.toEscapedString = function(component) {
302 var result = "";
303 var gotNonDot = false;
304 for (var i = 0; i < component.length; ++i) {
305 if (component[i] != 0x2e) {
306 gotNonDot = true;
307 break;
308 }
309 }
310 if (!gotNonDot) {
311 // Special case for component of zero or more periods. Add 3 periods.
312 result = "...";
313 for (var i = 0; i < component.length; ++i)
314 result += ".";
315 }
316 else {
317 for (var i = 0; i < component.length; ++i) {
318 var value = component[i];
319 // Check for 0-9, A-Z, a-z, (+), (-), (.), (_)
320 if (value >= 0x30 && value <= 0x39 || value >= 0x41 && value <= 0x5a ||
Alexander Afanasyev78122d52013-02-27 18:49:54 -0800321 value >= 0x61 && value <= 0x7a || value == 0x2b || value == 0x2d ||
Jeff Thompson4e8b7702012-10-21 23:42:27 -0700322 value == 0x2e || value == 0x5f)
323 result += String.fromCharCode(value);
324 else
325 result += "%" + (value < 16 ? "0" : "") + value.toString(16).toUpperCase();
326 }
327 }
328 return result;
329};
Wentao Shangb42483a2013-01-03 15:32:32 -0800330
Jeff Thompson78849a72013-02-03 22:35:54 -0800331/*
332 * Return component as a Uint8Array by decoding the escapedString according to "CCNx URI Scheme".
333 * If escapedString is "", "." or ".." then return null, which means to skip the component in the name.
334 */
335Name.fromEscapedString = function(escapedString) {
336 var component = unescape(escapedString.trim());
Alexander Afanasyev78122d52013-02-27 18:49:54 -0800337
Jeff Thompson78849a72013-02-03 22:35:54 -0800338 if (component.match(/[^.]/) == null) {
Alexander Afanasyev78122d52013-02-27 18:49:54 -0800339 // Special case for component of only periods.
Jeff Thompson78849a72013-02-03 22:35:54 -0800340 if (component.length <= 2)
341 // Zero, one or two periods is illegal. Ignore this componenent to be
342 // consistent with the C implementation.
343 return null;
344 else
345 // Remove 3 periods.
346 return DataUtils.toNumbersFromString(component.substr(3, component.length - 3));
347 }
348 else
349 return DataUtils.toNumbersFromString(component);
350}
351
Wentao Shangb42483a2013-01-03 15:32:32 -0800352Name.prototype.match = function(/*Name*/ name) {
353 var i_name = this.components;
354 var o_name = name.components;
355
356 // The intrest name is longer than the name we are checking it against.
357 if (i_name.length > o_name.length)
358 return false;
359
360 // Check if at least one of given components doesn't match.
361 for (var i = 0; i < i_name.length; ++i) {
362 if (!DataUtils.arraysEqual(i_name[i], o_name[i]))
363 return false;
364 }
365
366 return true;
367};