| 
									
										
										
										
											2017-01-28 15:15:27 -07:00
										 |  |  | (function (exports) { | 
					
						
							|  |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // unpack labels with 0x20 compression pointer support
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // ui8 is the ArrayBuffer of the entire packet
 | 
					
						
							|  |  |  | // ptr is the current index of the packet
 | 
					
						
							|  |  |  | // q = { byteLength: 0, cpcount: 0, labels: [], name: '' }
 | 
					
						
							| 
									
										
										
										
											2017-02-02 19:43:56 -07:00
										 |  |  | //
 | 
					
						
							|  |  |  | // NOTE:
 | 
					
						
							|  |  |  | // "NAME"s are terminated with 0x00
 | 
					
						
							|  |  |  | // "RDATA" is terminated by its length
 | 
					
						
							| 
									
										
										
										
											2017-01-28 15:15:27 -07:00
										 |  |  | exports.DNS_UNPACK_LABELS = function (ui8, ptr, q) { | 
					
						
							|  |  |  |   if (q.cpcount > 25) { | 
					
						
							|  |  |  |     throw new Error("compression pointer loop detected (over 25 pointers seems like a loop)"); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   var total = ptr; | 
					
						
							|  |  |  |   var i; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   var len; | 
					
						
							|  |  |  |   var label = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   do { | 
					
						
							|  |  |  |     label.length = 0; | 
					
						
							|  |  |  |     len = ui8[total]; | 
					
						
							| 
									
										
										
										
											2017-02-02 18:39:32 -07:00
										 |  |  |     if (len === undefined){ | 
					
						
							| 
									
										
										
										
											2017-02-02 19:43:56 -07:00
										 |  |  |       // RDATA is terminated by undefined, not len === 0
 | 
					
						
							|  |  |  |       break; | 
					
						
							| 
									
										
										
										
											2017-02-02 18:39:32 -07:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-02-02 19:43:56 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-28 15:15:27 -07:00
										 |  |  |     if (0xc0 === len) { | 
					
						
							|  |  |  |       var cpptr = ui8[total + 1]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       // we're not coming back!
 | 
					
						
							|  |  |  |       ptr = cpptr; | 
					
						
							|  |  |  |       q.cpcount += 1; | 
					
						
							|  |  |  |       q.byteLength += 2; // cp and len
 | 
					
						
							| 
									
										
										
										
											2017-02-02 18:39:32 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-02 19:43:56 -07:00
										 |  |  |       // recursion
 | 
					
						
							| 
									
										
										
										
											2017-01-28 15:15:27 -07:00
										 |  |  |       return exports.DNS_UNPACK_LABELS(ui8, ptr, q); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-02-02 19:43:56 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-28 15:15:27 -07:00
										 |  |  |     //str.length = 0; // fast empty array
 | 
					
						
							|  |  |  |     if (ui8.byteLength - total < len) { | 
					
						
							|  |  |  |       throw new Error( | 
					
						
							|  |  |  |         "Expected a string of length " + len | 
					
						
							|  |  |  |           + " but packet itself has " + (ui8.byteLength - total) + " bytes remaining" | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-02-02 19:43:56 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-28 15:15:27 -07:00
										 |  |  |     for (i = 0; i < len; i += 1) { | 
					
						
							|  |  |  |       total += 1; | 
					
						
							|  |  |  |       // TODO check url-allowable characters
 | 
					
						
							|  |  |  |       label.push(String.fromCharCode(ui8[total])); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-02-02 19:43:56 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-28 15:15:27 -07:00
										 |  |  |     if (label.length) { | 
					
						
							|  |  |  |       q.labels.push(label.join('')); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-02-02 19:43:56 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-28 15:15:27 -07:00
										 |  |  |     total += 1; | 
					
						
							| 
									
										
										
										
											2017-02-02 19:43:56 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   } while (0 !== len && undefined !== len); | 
					
						
							| 
									
										
										
										
											2017-01-28 15:15:27 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   //str.pop(); // remove trailing '.'
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   q.name = q.labels.join('.'); | 
					
						
							|  |  |  |   if (0 === q.cpcount) { | 
					
						
							|  |  |  |     q.byteLength = total - ptr; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2017-02-02 19:43:56 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-28 15:15:27 -07:00
										 |  |  |   return q; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | }('undefined' !== typeof window ? window : exports)); |