UNPKG

3.91 kBJavaScriptView Raw
1/*
2Copyright 2011 Timothy J Fontaine <tjfontaine@gmail.com>
3
4Permission is hereby granted, free of charge, to any person obtaining a copy
5of this software and associated documentation files (the "Software"), to deal
6in the Software without restriction, including without limitation the rights
7to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8copies of the Software, and to permit persons to whom the Software is
9furnished to do so, subject to the following conditions:
10
11The above copyright notice and this permission notice shall be included in
12all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
21*/
22
23"use strict";
24
25var POINTER_MASK = 0xC0;
26
27var isPointer = function (len) {
28 return (len & POINTER_MASK) > 0;
29};
30
31var unpack = function (buff, pos) {
32 var hit_label,
33 read_size,
34 end,
35 increaseRead,
36 increasePosition,
37 readLen,
38 parts,
39 len;
40
41 parts = [];
42 read_size = 0;
43 hit_label = false;
44
45 increaseRead = function (length) {
46 if (length === undefined) {
47 length = 1;
48 }
49
50 if (!hit_label) {
51 read_size += length;
52 }
53 };
54
55 increasePosition = function (length) {
56 if (length === undefined) {
57 length = 1;
58 }
59 increaseRead(length);
60 pos += length;
61 };
62
63 readLen = function () {
64 len = buff.readUInt8(pos);
65 increasePosition();
66 };
67
68 readLen();
69
70 while (len !== 0) {
71 if (isPointer(len)) {
72 len -= POINTER_MASK;
73 len = len << 8;
74 pos = len + buff.readUInt8(pos);
75 increaseRead();
76 hit_label = true;
77 } else {
78 end = pos + len;
79 parts.push(buff.toString('ascii', pos, end));
80 increasePosition(len);
81 }
82
83 readLen();
84 }
85
86 return {
87 read: read_size,
88 value: parts.join('.'),
89 };
90};
91exports.unpack = unpack;
92
93function splitMax(str, sep, max) {
94 var tmp = str.split(sep, max)
95 var txt = tmp.join(sep)
96 var rest = str.replace(txt, '').replace(sep, '')
97 tmp.push(rest)
98 return tmp
99}
100
101var pack = function (str, buff, buf_pos, index) {
102 var written = 0,
103 found_ptr = false,
104 written_parts = [],
105 parts, name, left;
106
107 var compress = function (s) {
108 if (!s) { return false; }
109
110 var offset = index[s];
111 if (offset) {
112 offset = (POINTER_MASK << 8) + offset;
113 buff.writeUInt16BE(offset, buf_pos + written);
114 written += 2;
115 return true;
116 }
117 return false;
118 };
119
120 var splitStr = function (s) {
121 if (s && s.length) {
122 parts = splitMax(s, '.', 1);
123 name = parts[0];
124 if (parts.length > 1) {
125 left = parts[1];
126 } else {
127 left = undefined;
128 }
129 } else {
130 parts = [];
131 name = undefined;
132 left = undefined;
133 }
134 };
135
136 found_ptr = compress(str);
137 splitStr(str);
138
139 while (!found_ptr && parts.length > 0) {
140 written_parts.push({
141 value: name,
142 offset: written + buf_pos,
143 });
144
145 buff.writeUInt8(name.length, written + buf_pos);
146 written += 1;
147 buff.write(name, written + buf_pos, name.length);
148 written += name.length;
149
150 found_ptr = compress(left);
151 splitStr(left);
152 }
153
154 if (!found_ptr) {
155 buff.writeUInt8(0, written + buf_pos);
156 written += 1;
157 }
158
159 if (written_parts.length) {
160 var i = written_parts.length;
161 while (i > 0) {
162 var key = written_parts.slice(i-1);
163 var names = [];
164 key.forEach(function(k) { names.push(k.value); });
165 index[names.join('.')] = key[0].offset;
166 i -= 1;
167 }
168 }
169
170 return written;
171};
172exports.pack = pack;