pbf-9fe59c76.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. /**
  2. * Cesium - https://github.com/CesiumGS/cesium
  3. *
  4. * Copyright 2011-2020 Cesium Contributors
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * Columbus View (Pat. Pend.)
  19. *
  20. * Portions licensed separately.
  21. * See https://github.com/CesiumGS/cesium/blob/master/LICENSE.md for full licensing details.
  22. */
  23. define(['exports'], function (exports) { 'use strict';
  24. var read = function (buffer, offset, isLE, mLen, nBytes) {
  25. var e, m;
  26. var eLen = (nBytes * 8) - mLen - 1;
  27. var eMax = (1 << eLen) - 1;
  28. var eBias = eMax >> 1;
  29. var nBits = -7;
  30. var i = isLE ? (nBytes - 1) : 0;
  31. var d = isLE ? -1 : 1;
  32. var s = buffer[offset + i];
  33. i += d;
  34. e = s & ((1 << (-nBits)) - 1);
  35. s >>= (-nBits);
  36. nBits += eLen;
  37. for (; nBits > 0; e = (e * 256) + buffer[offset + i], i += d, nBits -= 8) {}
  38. m = e & ((1 << (-nBits)) - 1);
  39. e >>= (-nBits);
  40. nBits += mLen;
  41. for (; nBits > 0; m = (m * 256) + buffer[offset + i], i += d, nBits -= 8) {}
  42. if (e === 0) {
  43. e = 1 - eBias;
  44. } else if (e === eMax) {
  45. return m ? NaN : ((s ? -1 : 1) * Infinity)
  46. } else {
  47. m = m + Math.pow(2, mLen);
  48. e = e - eBias;
  49. }
  50. return (s ? -1 : 1) * m * Math.pow(2, e - mLen)
  51. };
  52. var write = function (buffer, value, offset, isLE, mLen, nBytes) {
  53. var e, m, c;
  54. var eLen = (nBytes * 8) - mLen - 1;
  55. var eMax = (1 << eLen) - 1;
  56. var eBias = eMax >> 1;
  57. var rt = (mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0);
  58. var i = isLE ? 0 : (nBytes - 1);
  59. var d = isLE ? 1 : -1;
  60. var s = value < 0 || (value === 0 && 1 / value < 0) ? 1 : 0;
  61. value = Math.abs(value);
  62. if (isNaN(value) || value === Infinity) {
  63. m = isNaN(value) ? 1 : 0;
  64. e = eMax;
  65. } else {
  66. e = Math.floor(Math.log(value) / Math.LN2);
  67. if (value * (c = Math.pow(2, -e)) < 1) {
  68. e--;
  69. c *= 2;
  70. }
  71. if (e + eBias >= 1) {
  72. value += rt / c;
  73. } else {
  74. value += rt * Math.pow(2, 1 - eBias);
  75. }
  76. if (value * c >= 2) {
  77. e++;
  78. c /= 2;
  79. }
  80. if (e + eBias >= eMax) {
  81. m = 0;
  82. e = eMax;
  83. } else if (e + eBias >= 1) {
  84. m = ((value * c) - 1) * Math.pow(2, mLen);
  85. e = e + eBias;
  86. } else {
  87. m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen);
  88. e = 0;
  89. }
  90. }
  91. for (; mLen >= 8; buffer[offset + i] = m & 0xff, i += d, m /= 256, mLen -= 8) {}
  92. e = (e << mLen) | m;
  93. eLen += mLen;
  94. for (; eLen > 0; buffer[offset + i] = e & 0xff, i += d, e /= 256, eLen -= 8) {}
  95. buffer[offset + i - d] |= s * 128;
  96. };
  97. var ieee754 = {
  98. read: read,
  99. write: write
  100. };
  101. function Pbf(buf) {
  102. this.buf = ArrayBuffer.isView && ArrayBuffer.isView(buf) ? buf : new Uint8Array(buf || 0);
  103. this.pos = 0;
  104. this.type = 0;
  105. this.length = this.buf.length;
  106. }
  107. Pbf.Varint = 0;
  108. Pbf.Fixed64 = 1;
  109. Pbf.Bytes = 2;
  110. Pbf.Fixed32 = 5;
  111. var SHIFT_LEFT_32 = (1 << 16) * (1 << 16);
  112. var SHIFT_RIGHT_32 = 1 / SHIFT_LEFT_32;
  113. Pbf.prototype = {
  114. destroy: function() {
  115. this.buf = null;
  116. },
  117. readFields: function(readField, result, end) {
  118. end = end || this.length;
  119. while (this.pos < end) {
  120. var val = this.readVarint(),
  121. tag = val >> 3,
  122. startPos = this.pos;
  123. this.type = val & 0x7;
  124. readField(tag, result, this);
  125. if (this.pos === startPos) this.skip(val);
  126. }
  127. return result;
  128. },
  129. readMessage: function(readField, result) {
  130. return this.readFields(readField, result, this.readVarint() + this.pos);
  131. },
  132. readFixed32: function() {
  133. var val = readUInt32(this.buf, this.pos);
  134. this.pos += 4;
  135. return val;
  136. },
  137. readSFixed32: function() {
  138. var val = readInt32(this.buf, this.pos);
  139. this.pos += 4;
  140. return val;
  141. },
  142. readFixed64: function() {
  143. var val = readUInt32(this.buf, this.pos) + readUInt32(this.buf, this.pos + 4) * SHIFT_LEFT_32;
  144. this.pos += 8;
  145. return val;
  146. },
  147. readSFixed64: function() {
  148. var val = readUInt32(this.buf, this.pos) + readInt32(this.buf, this.pos + 4) * SHIFT_LEFT_32;
  149. this.pos += 8;
  150. return val;
  151. },
  152. readFloat: function() {
  153. var val = ieee754.read(this.buf, this.pos, true, 23, 4);
  154. this.pos += 4;
  155. return val;
  156. },
  157. readDouble: function() {
  158. var val = ieee754.read(this.buf, this.pos, true, 52, 8);
  159. this.pos += 8;
  160. return val;
  161. },
  162. readVarint: function(isSigned) {
  163. var buf = this.buf,
  164. val, b;
  165. b = buf[this.pos++]; val = b & 0x7f; if (b < 0x80) return val;
  166. b = buf[this.pos++]; val |= (b & 0x7f) << 7; if (b < 0x80) return val;
  167. b = buf[this.pos++]; val |= (b & 0x7f) << 14; if (b < 0x80) return val;
  168. b = buf[this.pos++]; val |= (b & 0x7f) << 21; if (b < 0x80) return val;
  169. b = buf[this.pos]; val |= (b & 0x0f) << 28;
  170. return readVarintRemainder(val, isSigned, this);
  171. },
  172. readVarint64: function() {
  173. return this.readVarint(true);
  174. },
  175. readSVarint: function() {
  176. var num = this.readVarint();
  177. return num % 2 === 1 ? (num + 1) / -2 : num / 2;
  178. },
  179. readBoolean: function() {
  180. return Boolean(this.readVarint());
  181. },
  182. readString: function() {
  183. var end = this.readVarint() + this.pos,
  184. str = readUtf8(this.buf, this.pos, end);
  185. this.pos = end;
  186. return str;
  187. },
  188. readBytes: function() {
  189. var end = this.readVarint() + this.pos,
  190. buffer = this.buf.subarray(this.pos, end);
  191. this.pos = end;
  192. return buffer;
  193. },
  194. readPackedVarint: function(arr, isSigned) {
  195. var end = readPackedEnd(this);
  196. arr = arr || [];
  197. while (this.pos < end) arr.push(this.readVarint(isSigned));
  198. return arr;
  199. },
  200. readPackedSVarint: function(arr) {
  201. var end = readPackedEnd(this);
  202. arr = arr || [];
  203. while (this.pos < end) arr.push(this.readSVarint());
  204. return arr;
  205. },
  206. readPackedBoolean: function(arr) {
  207. var end = readPackedEnd(this);
  208. arr = arr || [];
  209. while (this.pos < end) arr.push(this.readBoolean());
  210. return arr;
  211. },
  212. readPackedFloat: function(arr) {
  213. var end = readPackedEnd(this);
  214. arr = arr || [];
  215. while (this.pos < end) arr.push(this.readFloat());
  216. return arr;
  217. },
  218. readPackedDouble: function(arr) {
  219. var end = readPackedEnd(this);
  220. arr = arr || [];
  221. while (this.pos < end) arr.push(this.readDouble());
  222. return arr;
  223. },
  224. readPackedFixed32: function(arr) {
  225. var end = readPackedEnd(this);
  226. arr = arr || [];
  227. while (this.pos < end) arr.push(this.readFixed32());
  228. return arr;
  229. },
  230. readPackedSFixed32: function(arr) {
  231. var end = readPackedEnd(this);
  232. arr = arr || [];
  233. while (this.pos < end) arr.push(this.readSFixed32());
  234. return arr;
  235. },
  236. readPackedFixed64: function(arr) {
  237. var end = readPackedEnd(this);
  238. arr = arr || [];
  239. while (this.pos < end) arr.push(this.readFixed64());
  240. return arr;
  241. },
  242. readPackedSFixed64: function(arr) {
  243. var end = readPackedEnd(this);
  244. arr = arr || [];
  245. while (this.pos < end) arr.push(this.readSFixed64());
  246. return arr;
  247. },
  248. skip: function(val) {
  249. var type = val & 0x7;
  250. if (type === Pbf.Varint) while (this.buf[this.pos++] > 0x7f) {}
  251. else if (type === Pbf.Bytes) this.pos = this.readVarint() + this.pos;
  252. else if (type === Pbf.Fixed32) this.pos += 4;
  253. else if (type === Pbf.Fixed64) this.pos += 8;
  254. else throw new Error('Unimplemented type: ' + type);
  255. },
  256. writeTag: function(tag, type) {
  257. this.writeVarint((tag << 3) | type);
  258. },
  259. realloc: function(min) {
  260. var length = this.length || 16;
  261. while (length < this.pos + min) length *= 2;
  262. if (length !== this.length) {
  263. var buf = new Uint8Array(length);
  264. buf.set(this.buf);
  265. this.buf = buf;
  266. this.length = length;
  267. }
  268. },
  269. finish: function() {
  270. this.length = this.pos;
  271. this.pos = 0;
  272. return this.buf.subarray(0, this.length);
  273. },
  274. writeFixed32: function(val) {
  275. this.realloc(4);
  276. writeInt32(this.buf, val, this.pos);
  277. this.pos += 4;
  278. },
  279. writeSFixed32: function(val) {
  280. this.realloc(4);
  281. writeInt32(this.buf, val, this.pos);
  282. this.pos += 4;
  283. },
  284. writeFixed64: function(val) {
  285. this.realloc(8);
  286. writeInt32(this.buf, val & -1, this.pos);
  287. writeInt32(this.buf, Math.floor(val * SHIFT_RIGHT_32), this.pos + 4);
  288. this.pos += 8;
  289. },
  290. writeSFixed64: function(val) {
  291. this.realloc(8);
  292. writeInt32(this.buf, val & -1, this.pos);
  293. writeInt32(this.buf, Math.floor(val * SHIFT_RIGHT_32), this.pos + 4);
  294. this.pos += 8;
  295. },
  296. writeVarint: function(val) {
  297. val = +val || 0;
  298. if (val > 0xfffffff || val < 0) {
  299. writeBigVarint(val, this);
  300. return;
  301. }
  302. this.realloc(4);
  303. this.buf[this.pos++] = val & 0x7f | (val > 0x7f ? 0x80 : 0); if (val <= 0x7f) return;
  304. this.buf[this.pos++] = ((val >>>= 7) & 0x7f) | (val > 0x7f ? 0x80 : 0); if (val <= 0x7f) return;
  305. this.buf[this.pos++] = ((val >>>= 7) & 0x7f) | (val > 0x7f ? 0x80 : 0); if (val <= 0x7f) return;
  306. this.buf[this.pos++] = (val >>> 7) & 0x7f;
  307. },
  308. writeSVarint: function(val) {
  309. this.writeVarint(val < 0 ? -val * 2 - 1 : val * 2);
  310. },
  311. writeBoolean: function(val) {
  312. this.writeVarint(Boolean(val));
  313. },
  314. writeString: function(str) {
  315. str = String(str);
  316. this.realloc(str.length * 4);
  317. this.pos++;
  318. var startPos = this.pos;
  319. this.pos = writeUtf8(this.buf, str, this.pos);
  320. var len = this.pos - startPos;
  321. if (len >= 0x80) makeRoomForExtraLength(startPos, len, this);
  322. this.pos = startPos - 1;
  323. this.writeVarint(len);
  324. this.pos += len;
  325. },
  326. writeFloat: function(val) {
  327. this.realloc(4);
  328. ieee754.write(this.buf, val, this.pos, true, 23, 4);
  329. this.pos += 4;
  330. },
  331. writeDouble: function(val) {
  332. this.realloc(8);
  333. ieee754.write(this.buf, val, this.pos, true, 52, 8);
  334. this.pos += 8;
  335. },
  336. writeBytes: function(buffer) {
  337. var len = buffer.length;
  338. this.writeVarint(len);
  339. this.realloc(len);
  340. for (var i = 0; i < len; i++) this.buf[this.pos++] = buffer[i];
  341. },
  342. writeRawMessage: function(fn, obj) {
  343. this.pos++;
  344. var startPos = this.pos;
  345. fn(obj, this);
  346. var len = this.pos - startPos;
  347. if (len >= 0x80) makeRoomForExtraLength(startPos, len, this);
  348. this.pos = startPos - 1;
  349. this.writeVarint(len);
  350. this.pos += len;
  351. },
  352. writeMessage: function(tag, fn, obj) {
  353. this.writeTag(tag, Pbf.Bytes);
  354. this.writeRawMessage(fn, obj);
  355. },
  356. writePackedVarint: function(tag, arr) { this.writeMessage(tag, writePackedVarint, arr); },
  357. writePackedSVarint: function(tag, arr) { this.writeMessage(tag, writePackedSVarint, arr); },
  358. writePackedBoolean: function(tag, arr) { this.writeMessage(tag, writePackedBoolean, arr); },
  359. writePackedFloat: function(tag, arr) { this.writeMessage(tag, writePackedFloat, arr); },
  360. writePackedDouble: function(tag, arr) { this.writeMessage(tag, writePackedDouble, arr); },
  361. writePackedFixed32: function(tag, arr) { this.writeMessage(tag, writePackedFixed32, arr); },
  362. writePackedSFixed32: function(tag, arr) { this.writeMessage(tag, writePackedSFixed32, arr); },
  363. writePackedFixed64: function(tag, arr) { this.writeMessage(tag, writePackedFixed64, arr); },
  364. writePackedSFixed64: function(tag, arr) { this.writeMessage(tag, writePackedSFixed64, arr); },
  365. writeBytesField: function(tag, buffer) {
  366. this.writeTag(tag, Pbf.Bytes);
  367. this.writeBytes(buffer);
  368. },
  369. writeFixed32Field: function(tag, val) {
  370. this.writeTag(tag, Pbf.Fixed32);
  371. this.writeFixed32(val);
  372. },
  373. writeSFixed32Field: function(tag, val) {
  374. this.writeTag(tag, Pbf.Fixed32);
  375. this.writeSFixed32(val);
  376. },
  377. writeFixed64Field: function(tag, val) {
  378. this.writeTag(tag, Pbf.Fixed64);
  379. this.writeFixed64(val);
  380. },
  381. writeSFixed64Field: function(tag, val) {
  382. this.writeTag(tag, Pbf.Fixed64);
  383. this.writeSFixed64(val);
  384. },
  385. writeVarintField: function(tag, val) {
  386. this.writeTag(tag, Pbf.Varint);
  387. this.writeVarint(val);
  388. },
  389. writeSVarintField: function(tag, val) {
  390. this.writeTag(tag, Pbf.Varint);
  391. this.writeSVarint(val);
  392. },
  393. writeStringField: function(tag, str) {
  394. this.writeTag(tag, Pbf.Bytes);
  395. this.writeString(str);
  396. },
  397. writeFloatField: function(tag, val) {
  398. this.writeTag(tag, Pbf.Fixed32);
  399. this.writeFloat(val);
  400. },
  401. writeDoubleField: function(tag, val) {
  402. this.writeTag(tag, Pbf.Fixed64);
  403. this.writeDouble(val);
  404. },
  405. writeBooleanField: function(tag, val) {
  406. this.writeVarintField(tag, Boolean(val));
  407. }
  408. };
  409. function readVarintRemainder(l, s, p) {
  410. var buf = p.buf,
  411. h, b;
  412. b = buf[p.pos++]; h = (b & 0x70) >> 4; if (b < 0x80) return toNum(l, h, s);
  413. b = buf[p.pos++]; h |= (b & 0x7f) << 3; if (b < 0x80) return toNum(l, h, s);
  414. b = buf[p.pos++]; h |= (b & 0x7f) << 10; if (b < 0x80) return toNum(l, h, s);
  415. b = buf[p.pos++]; h |= (b & 0x7f) << 17; if (b < 0x80) return toNum(l, h, s);
  416. b = buf[p.pos++]; h |= (b & 0x7f) << 24; if (b < 0x80) return toNum(l, h, s);
  417. b = buf[p.pos++]; h |= (b & 0x01) << 31; if (b < 0x80) return toNum(l, h, s);
  418. throw new Error('Expected varint not more than 10 bytes');
  419. }
  420. function readPackedEnd(pbf) {
  421. return pbf.type === Pbf.Bytes ?
  422. pbf.readVarint() + pbf.pos : pbf.pos + 1;
  423. }
  424. function toNum(low, high, isSigned) {
  425. if (isSigned) {
  426. return high * 0x100000000 + (low >>> 0);
  427. }
  428. return ((high >>> 0) * 0x100000000) + (low >>> 0);
  429. }
  430. function writeBigVarint(val, pbf) {
  431. var low, high;
  432. if (val >= 0) {
  433. low = (val % 0x100000000) | 0;
  434. high = (val / 0x100000000) | 0;
  435. } else {
  436. low = ~(-val % 0x100000000);
  437. high = ~(-val / 0x100000000);
  438. if (low ^ 0xffffffff) {
  439. low = (low + 1) | 0;
  440. } else {
  441. low = 0;
  442. high = (high + 1) | 0;
  443. }
  444. }
  445. if (val >= 0x10000000000000000 || val < -0x10000000000000000) {
  446. throw new Error('Given varint doesn\'t fit into 10 bytes');
  447. }
  448. pbf.realloc(10);
  449. writeBigVarintLow(low, high, pbf);
  450. writeBigVarintHigh(high, pbf);
  451. }
  452. function writeBigVarintLow(low, high, pbf) {
  453. pbf.buf[pbf.pos++] = low & 0x7f | 0x80; low >>>= 7;
  454. pbf.buf[pbf.pos++] = low & 0x7f | 0x80; low >>>= 7;
  455. pbf.buf[pbf.pos++] = low & 0x7f | 0x80; low >>>= 7;
  456. pbf.buf[pbf.pos++] = low & 0x7f | 0x80; low >>>= 7;
  457. pbf.buf[pbf.pos] = low & 0x7f;
  458. }
  459. function writeBigVarintHigh(high, pbf) {
  460. var lsb = (high & 0x07) << 4;
  461. pbf.buf[pbf.pos++] |= lsb | ((high >>>= 3) ? 0x80 : 0); if (!high) return;
  462. pbf.buf[pbf.pos++] = high & 0x7f | ((high >>>= 7) ? 0x80 : 0); if (!high) return;
  463. pbf.buf[pbf.pos++] = high & 0x7f | ((high >>>= 7) ? 0x80 : 0); if (!high) return;
  464. pbf.buf[pbf.pos++] = high & 0x7f | ((high >>>= 7) ? 0x80 : 0); if (!high) return;
  465. pbf.buf[pbf.pos++] = high & 0x7f | ((high >>>= 7) ? 0x80 : 0); if (!high) return;
  466. pbf.buf[pbf.pos++] = high & 0x7f;
  467. }
  468. function makeRoomForExtraLength(startPos, len, pbf) {
  469. var extraLen =
  470. len <= 0x3fff ? 1 :
  471. len <= 0x1fffff ? 2 :
  472. len <= 0xfffffff ? 3 : Math.ceil(Math.log(len) / (Math.LN2 * 7));
  473. pbf.realloc(extraLen);
  474. for (var i = pbf.pos - 1; i >= startPos; i--) pbf.buf[i + extraLen] = pbf.buf[i];
  475. }
  476. function writePackedVarint(arr, pbf) { for (var i = 0; i < arr.length; i++) pbf.writeVarint(arr[i]); }
  477. function writePackedSVarint(arr, pbf) { for (var i = 0; i < arr.length; i++) pbf.writeSVarint(arr[i]); }
  478. function writePackedFloat(arr, pbf) { for (var i = 0; i < arr.length; i++) pbf.writeFloat(arr[i]); }
  479. function writePackedDouble(arr, pbf) { for (var i = 0; i < arr.length; i++) pbf.writeDouble(arr[i]); }
  480. function writePackedBoolean(arr, pbf) { for (var i = 0; i < arr.length; i++) pbf.writeBoolean(arr[i]); }
  481. function writePackedFixed32(arr, pbf) { for (var i = 0; i < arr.length; i++) pbf.writeFixed32(arr[i]); }
  482. function writePackedSFixed32(arr, pbf) { for (var i = 0; i < arr.length; i++) pbf.writeSFixed32(arr[i]); }
  483. function writePackedFixed64(arr, pbf) { for (var i = 0; i < arr.length; i++) pbf.writeFixed64(arr[i]); }
  484. function writePackedSFixed64(arr, pbf) { for (var i = 0; i < arr.length; i++) pbf.writeSFixed64(arr[i]); }
  485. function readUInt32(buf, pos) {
  486. return ((buf[pos]) |
  487. (buf[pos + 1] << 8) |
  488. (buf[pos + 2] << 16)) +
  489. (buf[pos + 3] * 0x1000000);
  490. }
  491. function writeInt32(buf, val, pos) {
  492. buf[pos] = val;
  493. buf[pos + 1] = (val >>> 8);
  494. buf[pos + 2] = (val >>> 16);
  495. buf[pos + 3] = (val >>> 24);
  496. }
  497. function readInt32(buf, pos) {
  498. return ((buf[pos]) |
  499. (buf[pos + 1] << 8) |
  500. (buf[pos + 2] << 16)) +
  501. (buf[pos + 3] << 24);
  502. }
  503. function readUtf8(buf, pos, end) {
  504. var str = '';
  505. var i = pos;
  506. while (i < end) {
  507. var b0 = buf[i];
  508. var c = null;
  509. var bytesPerSequence =
  510. b0 > 0xEF ? 4 :
  511. b0 > 0xDF ? 3 :
  512. b0 > 0xBF ? 2 : 1;
  513. if (i + bytesPerSequence > end) break;
  514. var b1, b2, b3;
  515. if (bytesPerSequence === 1) {
  516. if (b0 < 0x80) {
  517. c = b0;
  518. }
  519. } else if (bytesPerSequence === 2) {
  520. b1 = buf[i + 1];
  521. if ((b1 & 0xC0) === 0x80) {
  522. c = (b0 & 0x1F) << 0x6 | (b1 & 0x3F);
  523. if (c <= 0x7F) {
  524. c = null;
  525. }
  526. }
  527. } else if (bytesPerSequence === 3) {
  528. b1 = buf[i + 1];
  529. b2 = buf[i + 2];
  530. if ((b1 & 0xC0) === 0x80 && (b2 & 0xC0) === 0x80) {
  531. c = (b0 & 0xF) << 0xC | (b1 & 0x3F) << 0x6 | (b2 & 0x3F);
  532. if (c <= 0x7FF || (c >= 0xD800 && c <= 0xDFFF)) {
  533. c = null;
  534. }
  535. }
  536. } else if (bytesPerSequence === 4) {
  537. b1 = buf[i + 1];
  538. b2 = buf[i + 2];
  539. b3 = buf[i + 3];
  540. if ((b1 & 0xC0) === 0x80 && (b2 & 0xC0) === 0x80 && (b3 & 0xC0) === 0x80) {
  541. c = (b0 & 0xF) << 0x12 | (b1 & 0x3F) << 0xC | (b2 & 0x3F) << 0x6 | (b3 & 0x3F);
  542. if (c <= 0xFFFF || c >= 0x110000) {
  543. c = null;
  544. }
  545. }
  546. }
  547. if (c === null) {
  548. c = 0xFFFD;
  549. bytesPerSequence = 1;
  550. } else if (c > 0xFFFF) {
  551. c -= 0x10000;
  552. str += String.fromCharCode(c >>> 10 & 0x3FF | 0xD800);
  553. c = 0xDC00 | c & 0x3FF;
  554. }
  555. str += String.fromCharCode(c);
  556. i += bytesPerSequence;
  557. }
  558. return str;
  559. }
  560. function writeUtf8(buf, str, pos) {
  561. for (var i = 0, c, lead; i < str.length; i++) {
  562. c = str.charCodeAt(i);
  563. if (c > 0xD7FF && c < 0xE000) {
  564. if (lead) {
  565. if (c < 0xDC00) {
  566. buf[pos++] = 0xEF;
  567. buf[pos++] = 0xBF;
  568. buf[pos++] = 0xBD;
  569. lead = c;
  570. continue;
  571. } else {
  572. c = lead - 0xD800 << 10 | c - 0xDC00 | 0x10000;
  573. lead = null;
  574. }
  575. } else {
  576. if (c > 0xDBFF || (i + 1 === str.length)) {
  577. buf[pos++] = 0xEF;
  578. buf[pos++] = 0xBF;
  579. buf[pos++] = 0xBD;
  580. } else {
  581. lead = c;
  582. }
  583. continue;
  584. }
  585. } else if (lead) {
  586. buf[pos++] = 0xEF;
  587. buf[pos++] = 0xBF;
  588. buf[pos++] = 0xBD;
  589. lead = null;
  590. }
  591. if (c < 0x80) {
  592. buf[pos++] = c;
  593. } else {
  594. if (c < 0x800) {
  595. buf[pos++] = c >> 0x6 | 0xC0;
  596. } else {
  597. if (c < 0x10000) {
  598. buf[pos++] = c >> 0xC | 0xE0;
  599. } else {
  600. buf[pos++] = c >> 0x12 | 0xF0;
  601. buf[pos++] = c >> 0xC & 0x3F | 0x80;
  602. }
  603. buf[pos++] = c >> 0x6 & 0x3F | 0x80;
  604. }
  605. buf[pos++] = c & 0x3F | 0x80;
  606. }
  607. }
  608. return pos;
  609. }
  610. exports.Protobuf = Pbf;
  611. });