Files
openlayers/vectortile/ol.ext/pbf.js
T
2015-09-10 15:23:57 +02:00

693 lines
22 KiB
JavaScript

goog.provide('ol.ext.pbf');
/** @typedef {function(*)} */
ol.ext.pbf;
(function() {
var exports = {};
var module = {exports: exports};
var define;
/**
* @fileoverview
* @suppress {accessControls, ambiguousFunctionDecl, checkDebuggerStatement, checkRegExp, checkTypes, checkVars, const, constantProperty, deprecated, duplicate, es5Strict, fileoverviewTags, missingProperties, nonStandardJsDocs, strictModuleDepCheck, suspiciousCode, undefinedNames, undefinedVars, unknownDefines, uselessCode, visibility}
*/
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.pbf = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
'use strict';
// lightweight Buffer shim for pbf browser build
// based on code from github.com/feross/buffer (MIT-licensed)
module.exports = Buffer;
var ieee754 = require('ieee754');
var BufferMethods;
function Buffer(length) {
var arr;
if (length && length.length) {
arr = length;
length = arr.length;
}
var buf = new Uint8Array(length || 0);
if (arr) buf.set(arr);
buf.readUInt32LE = BufferMethods.readUInt32LE;
buf.writeUInt32LE = BufferMethods.writeUInt32LE;
buf.readInt32LE = BufferMethods.readInt32LE;
buf.writeInt32LE = BufferMethods.writeInt32LE;
buf.readFloatLE = BufferMethods.readFloatLE;
buf.writeFloatLE = BufferMethods.writeFloatLE;
buf.readDoubleLE = BufferMethods.readDoubleLE;
buf.writeDoubleLE = BufferMethods.writeDoubleLE;
buf.toString = BufferMethods.toString;
buf.write = BufferMethods.write;
buf.slice = BufferMethods.slice;
buf.copy = BufferMethods.copy;
buf._isBuffer = true;
return buf;
}
var lastStr, lastStrEncoded;
BufferMethods = {
readUInt32LE: function(pos) {
return ((this[pos]) |
(this[pos + 1] << 8) |
(this[pos + 2] << 16)) +
(this[pos + 3] * 0x1000000);
},
writeUInt32LE: function(val, pos) {
this[pos] = val;
this[pos + 1] = (val >>> 8);
this[pos + 2] = (val >>> 16);
this[pos + 3] = (val >>> 24);
},
readInt32LE: function(pos) {
return ((this[pos]) |
(this[pos + 1] << 8) |
(this[pos + 2] << 16)) +
(this[pos + 3] << 24);
},
readFloatLE: function(pos) { return ieee754.read(this, pos, true, 23, 4); },
readDoubleLE: function(pos) { return ieee754.read(this, pos, true, 52, 8); },
writeFloatLE: function(val, pos) { return ieee754.write(this, val, pos, true, 23, 4); },
writeDoubleLE: function(val, pos) { return ieee754.write(this, val, pos, true, 52, 8); },
toString: function(encoding, start, end) {
var str = '',
tmp = '';
start = start || 0;
end = Math.min(this.length, end || this.length);
for (var i = start; i < end; i++) {
var ch = this[i];
if (ch <= 0x7F) {
str += decodeURIComponent(tmp) + String.fromCharCode(ch);
tmp = '';
} else {
tmp += '%' + ch.toString(16);
}
}
str += decodeURIComponent(tmp);
return str;
},
write: function(str, pos) {
var bytes = str === lastStr ? lastStrEncoded : encodeString(str);
for (var i = 0; i < bytes.length; i++) {
this[pos + i] = bytes[i];
}
},
slice: function(start, end) {
return this.subarray(start, end);
},
copy: function(buf, pos) {
pos = pos || 0;
for (var i = 0; i < this.length; i++) {
buf[pos + i] = this[i];
}
}
};
BufferMethods.writeInt32LE = BufferMethods.writeUInt32LE;
Buffer.byteLength = function(str) {
lastStr = str;
lastStrEncoded = encodeString(str);
return lastStrEncoded.length;
};
Buffer.isBuffer = function(buf) {
return !!(buf && buf._isBuffer);
};
function encodeString(str) {
var length = str.length,
bytes = [];
for (var i = 0, c, lead; i < length; i++) {
c = str.charCodeAt(i); // code point
if (c > 0xD7FF && c < 0xE000) {
if (lead) {
if (c < 0xDC00) {
bytes.push(0xEF, 0xBF, 0xBD);
lead = c;
continue;
} else {
c = lead - 0xD800 << 10 | c - 0xDC00 | 0x10000;
lead = null;
}
} else {
if (c > 0xDBFF || (i + 1 === length)) bytes.push(0xEF, 0xBF, 0xBD);
else lead = c;
continue;
}
} else if (lead) {
bytes.push(0xEF, 0xBF, 0xBD);
lead = null;
}
if (c < 0x80) bytes.push(c);
else if (c < 0x800) bytes.push(c >> 0x6 | 0xC0, c & 0x3F | 0x80);
else if (c < 0x10000) bytes.push(c >> 0xC | 0xE0, c >> 0x6 & 0x3F | 0x80, c & 0x3F | 0x80);
else bytes.push(c >> 0x12 | 0xF0, c >> 0xC & 0x3F | 0x80, c >> 0x6 & 0x3F | 0x80, c & 0x3F | 0x80);
}
return bytes;
}
},{"ieee754":3}],2:[function(require,module,exports){
(function (global){
'use strict';
module.exports = Pbf;
var Buffer = global.Buffer || require('./buffer');
function Pbf(buf) {
this.buf = !Buffer.isBuffer(buf) ? new Buffer(buf || 0) : buf;
this.pos = 0;
this.length = this.buf.length;
}
Pbf.Varint = 0; // varint: int32, int64, uint32, uint64, sint32, sint64, bool, enum
Pbf.Fixed64 = 1; // 64-bit: double, fixed64, sfixed64
Pbf.Bytes = 2; // length-delimited: string, bytes, embedded messages, packed repeated fields
Pbf.Fixed32 = 5; // 32-bit: float, fixed32, sfixed32
var SHIFT_LEFT_32 = (1 << 16) * (1 << 16),
SHIFT_RIGHT_32 = 1 / SHIFT_LEFT_32,
POW_2_63 = Math.pow(2, 63);
Pbf.prototype = {
destroy: function() {
this.buf = null;
},
// === READING =================================================================
readFields: function(readField, result, end) {
end = end || this.length;
while (this.pos < end) {
var val = this.readVarint(),
tag = val >> 3,
startPos = this.pos;
readField(tag, result, this);
if (this.pos === startPos) this.skip(val);
}
return result;
},
readMessage: function(readField, result) {
return this.readFields(readField, result, this.readVarint() + this.pos);
},
readFixed32: function() {
var val = this.buf.readUInt32LE(this.pos);
this.pos += 4;
return val;
},
readSFixed32: function() {
var val = this.buf.readInt32LE(this.pos);
this.pos += 4;
return val;
},
// 64-bit int handling is based on github.com/dpw/node-buffer-more-ints (MIT-licensed)
readFixed64: function() {
var val = this.buf.readUInt32LE(this.pos) + this.buf.readUInt32LE(this.pos + 4) * SHIFT_LEFT_32;
this.pos += 8;
return val;
},
readSFixed64: function() {
var val = this.buf.readUInt32LE(this.pos) + this.buf.readInt32LE(this.pos + 4) * SHIFT_LEFT_32;
this.pos += 8;
return val;
},
readFloat: function() {
var val = this.buf.readFloatLE(this.pos);
this.pos += 4;
return val;
},
readDouble: function() {
var val = this.buf.readDoubleLE(this.pos);
this.pos += 8;
return val;
},
readVarint: function() {
var buf = this.buf,
val, b, b0, b1, b2, b3;
b0 = buf[this.pos++]; if (b0 < 0x80) return b0; b0 = b0 & 0x7f;
b1 = buf[this.pos++]; if (b1 < 0x80) return b0 | b1 << 7; b1 = (b1 & 0x7f) << 7;
b2 = buf[this.pos++]; if (b2 < 0x80) return b0 | b1 | b2 << 14; b2 = (b2 & 0x7f) << 14;
b3 = buf[this.pos++]; if (b3 < 0x80) return b0 | b1 | b2 | b3 << 21;
val = b0 | b1 | b2 | (b3 & 0x7f) << 21;
b = buf[this.pos++]; val += (b & 0x7f) * 0x10000000; if (b < 0x80) return val;
b = buf[this.pos++]; val += (b & 0x7f) * 0x800000000; if (b < 0x80) return val;
b = buf[this.pos++]; val += (b & 0x7f) * 0x40000000000; if (b < 0x80) return val;
b = buf[this.pos++]; val += (b & 0x7f) * 0x2000000000000; if (b < 0x80) return val;
b = buf[this.pos++]; val += (b & 0x7f) * 0x100000000000000; if (b < 0x80) return val;
b = buf[this.pos++]; val += (b & 0x7f) * 0x8000000000000000; if (b < 0x80) return val;
throw new Error('Expected varint not more than 10 bytes');
},
readVarint64: function() {
var startPos = this.pos,
val = this.readVarint();
if (val < POW_2_63) return val;
var pos = this.pos - 2;
while (this.buf[pos] === 0xff) pos--;
if (pos < startPos) pos = startPos;
val = 0;
for (var i = 0; i < pos - startPos + 1; i++) {
var b = ~this.buf[startPos + i] & 0x7f;
val += i < 4 ? b << i * 7 : b * Math.pow(2, i * 7);
}
return -val - 1;
},
readSVarint: function() {
var num = this.readVarint();
return num % 2 === 1 ? (num + 1) / -2 : num / 2; // zigzag encoding
},
readBoolean: function() {
return Boolean(this.readVarint());
},
readString: function() {
var end = this.readVarint() + this.pos,
str = this.buf.toString('utf8', this.pos, end);
this.pos = end;
return str;
},
readBytes: function() {
var end = this.readVarint() + this.pos,
buffer = this.buf.slice(this.pos, end);
this.pos = end;
return buffer;
},
// verbose for performance reasons; doesn't affect gzipped size
readPackedVarint: function() {
var end = this.readVarint() + this.pos, arr = [];
while (this.pos < end) arr.push(this.readVarint());
return arr;
},
readPackedSVarint: function() {
var end = this.readVarint() + this.pos, arr = [];
while (this.pos < end) arr.push(this.readSVarint());
return arr;
},
readPackedBoolean: function() {
var end = this.readVarint() + this.pos, arr = [];
while (this.pos < end) arr.push(this.readBoolean());
return arr;
},
readPackedFloat: function() {
var end = this.readVarint() + this.pos, arr = [];
while (this.pos < end) arr.push(this.readFloat());
return arr;
},
readPackedDouble: function() {
var end = this.readVarint() + this.pos, arr = [];
while (this.pos < end) arr.push(this.readDouble());
return arr;
},
readPackedFixed32: function() {
var end = this.readVarint() + this.pos, arr = [];
while (this.pos < end) arr.push(this.readFixed32());
return arr;
},
readPackedSFixed32: function() {
var end = this.readVarint() + this.pos, arr = [];
while (this.pos < end) arr.push(this.readSFixed32());
return arr;
},
readPackedFixed64: function() {
var end = this.readVarint() + this.pos, arr = [];
while (this.pos < end) arr.push(this.readFixed64());
return arr;
},
readPackedSFixed64: function() {
var end = this.readVarint() + this.pos, arr = [];
while (this.pos < end) arr.push(this.readSFixed64());
return arr;
},
skip: function(val) {
var type = val & 0x7;
if (type === Pbf.Varint) while (this.buf[this.pos++] > 0x7f) {}
else if (type === Pbf.Bytes) this.pos = this.readVarint() + this.pos;
else if (type === Pbf.Fixed32) this.pos += 4;
else if (type === Pbf.Fixed64) this.pos += 8;
else throw new Error('Unimplemented type: ' + type);
},
// === WRITING =================================================================
writeTag: function(tag, type) {
this.writeVarint((tag << 3) | type);
},
realloc: function(min) {
var length = this.length || 16;
while (length < this.pos + min) length *= 2;
if (length !== this.length) {
var buf = new Buffer(length);
this.buf.copy(buf);
this.buf = buf;
this.length = length;
}
},
finish: function() {
this.length = this.pos;
this.pos = 0;
return this.buf.slice(0, this.length);
},
writeFixed32: function(val) {
this.realloc(4);
this.buf.writeUInt32LE(val, this.pos);
this.pos += 4;
},
writeSFixed32: function(val) {
this.realloc(4);
this.buf.writeInt32LE(val, this.pos);
this.pos += 4;
},
writeFixed64: function(val) {
this.realloc(8);
this.buf.writeInt32LE(val & -1, this.pos);
this.buf.writeUInt32LE(Math.floor(val * SHIFT_RIGHT_32), this.pos + 4);
this.pos += 8;
},
writeSFixed64: function(val) {
this.realloc(8);
this.buf.writeInt32LE(val & -1, this.pos);
this.buf.writeInt32LE(Math.floor(val * SHIFT_RIGHT_32), this.pos + 4);
this.pos += 8;
},
writeVarint: function(val) {
val = +val;
if (val <= 0x7f) {
this.realloc(1);
this.buf[this.pos++] = val;
} else if (val <= 0x3fff) {
this.realloc(2);
this.buf[this.pos++] = ((val >>> 0) & 0x7f) | 0x80;
this.buf[this.pos++] = ((val >>> 7) & 0x7f);
} else if (val <= 0x1fffff) {
this.realloc(3);
this.buf[this.pos++] = ((val >>> 0) & 0x7f) | 0x80;
this.buf[this.pos++] = ((val >>> 7) & 0x7f) | 0x80;
this.buf[this.pos++] = ((val >>> 14) & 0x7f);
} else if (val <= 0xfffffff) {
this.realloc(4);
this.buf[this.pos++] = ((val >>> 0) & 0x7f) | 0x80;
this.buf[this.pos++] = ((val >>> 7) & 0x7f) | 0x80;
this.buf[this.pos++] = ((val >>> 14) & 0x7f) | 0x80;
this.buf[this.pos++] = ((val >>> 21) & 0x7f);
} else {
var pos = this.pos;
while (val >= 0x80) {
this.realloc(1);
this.buf[this.pos++] = (val & 0xff) | 0x80;
val /= 0x80;
}
this.realloc(1);
this.buf[this.pos++] = val | 0;
if (this.pos - pos > 10) throw new Error('Given varint doesn\'t fit into 10 bytes');
}
},
writeSVarint: function(val) {
this.writeVarint(val < 0 ? -val * 2 - 1 : val * 2);
},
writeBoolean: function(val) {
this.writeVarint(Boolean(val));
},
writeString: function(str) {
str = String(str);
var bytes = Buffer.byteLength(str);
this.writeVarint(bytes);
this.realloc(bytes);
this.buf.write(str, this.pos);
this.pos += bytes;
},
writeFloat: function(val) {
this.realloc(4);
this.buf.writeFloatLE(val, this.pos);
this.pos += 4;
},
writeDouble: function(val) {
this.realloc(8);
this.buf.writeDoubleLE(val, this.pos);
this.pos += 8;
},
writeBytes: function(buffer) {
var len = buffer.length;
this.writeVarint(len);
this.realloc(len);
for (var i = 0; i < len; i++) this.buf[this.pos++] = buffer[i];
},
writeRawMessage: function(fn, obj) {
this.pos++; // reserve 1 byte for short message length
// write the message directly to the buffer and see how much was written
var startPos = this.pos;
fn(obj, this);
var len = this.pos - startPos;
var varintLen =
len <= 0x7f ? 1 :
len <= 0x3fff ? 2 :
len <= 0x1fffff ? 3 :
len <= 0xfffffff ? 4 : Math.ceil(Math.log(len) / (Math.LN2 * 7));
// if 1 byte isn't enough for encoding message length, shift the data to the right
if (varintLen > 1) {
this.realloc(varintLen - 1);
for (var i = this.pos - 1; i >= startPos; i--) this.buf[i + varintLen - 1] = this.buf[i];
}
// finally, write the message length in the reserved place and restore the position
this.pos = startPos - 1;
this.writeVarint(len);
this.pos += len;
},
writeMessage: function(tag, fn, obj) {
this.writeTag(tag, Pbf.Bytes);
this.writeRawMessage(fn, obj);
},
writePackedVarint: function(tag, arr) { this.writeMessage(tag, writePackedVarint, arr); },
writePackedSVarint: function(tag, arr) { this.writeMessage(tag, writePackedSVarint, arr); },
writePackedBoolean: function(tag, arr) { this.writeMessage(tag, writePackedBoolean, arr); },
writePackedFloat: function(tag, arr) { this.writeMessage(tag, writePackedFloat, arr); },
writePackedDouble: function(tag, arr) { this.writeMessage(tag, writePackedDouble, arr); },
writePackedFixed32: function(tag, arr) { this.writeMessage(tag, writePackedFixed32, arr); },
writePackedSFixed32: function(tag, arr) { this.writeMessage(tag, writePackedSFixed32, arr); },
writePackedFixed64: function(tag, arr) { this.writeMessage(tag, writePackedFixed64, arr); },
writePackedSFixed64: function(tag, arr) { this.writeMessage(tag, writePackedSFixed64, arr); },
writeBytesField: function(tag, buffer) {
this.writeTag(tag, Pbf.Bytes);
this.writeBytes(buffer);
},
writeFixed32Field: function(tag, val) {
this.writeTag(tag, Pbf.Fixed32);
this.writeFixed32(val);
},
writeSFixed32Field: function(tag, val) {
this.writeTag(tag, Pbf.Fixed32);
this.writeSFixed32(val);
},
writeFixed64Field: function(tag, val) {
this.writeTag(tag, Pbf.Fixed64);
this.writeFixed64(val);
},
writeSFixed64Field: function(tag, val) {
this.writeTag(tag, Pbf.Fixed64);
this.writeSFixed64(val);
},
writeVarintField: function(tag, val) {
this.writeTag(tag, Pbf.Varint);
this.writeVarint(val);
},
writeSVarintField: function(tag, val) {
this.writeTag(tag, Pbf.Varint);
this.writeSVarint(val);
},
writeStringField: function(tag, str) {
this.writeTag(tag, Pbf.Bytes);
this.writeString(str);
},
writeFloatField: function(tag, val) {
this.writeTag(tag, Pbf.Fixed32);
this.writeFloat(val);
},
writeDoubleField: function(tag, val) {
this.writeTag(tag, Pbf.Fixed64);
this.writeDouble(val);
},
writeBooleanField: function(tag, val) {
this.writeVarintField(tag, Boolean(val));
}
};
function writePackedVarint(arr, pbf) { for (var i = 0; i < arr.length; i++) pbf.writeVarint(arr[i]); }
function writePackedSVarint(arr, pbf) { for (var i = 0; i < arr.length; i++) pbf.writeSVarint(arr[i]); }
function writePackedFloat(arr, pbf) { for (var i = 0; i < arr.length; i++) pbf.writeFloat(arr[i]); }
function writePackedDouble(arr, pbf) { for (var i = 0; i < arr.length; i++) pbf.writeDouble(arr[i]); }
function writePackedBoolean(arr, pbf) { for (var i = 0; i < arr.length; i++) pbf.writeBoolean(arr[i]); }
function writePackedFixed32(arr, pbf) { for (var i = 0; i < arr.length; i++) pbf.writeFixed32(arr[i]); }
function writePackedSFixed32(arr, pbf) { for (var i = 0; i < arr.length; i++) pbf.writeSFixed32(arr[i]); }
function writePackedFixed64(arr, pbf) { for (var i = 0; i < arr.length; i++) pbf.writeFixed64(arr[i]); }
function writePackedSFixed64(arr, pbf) { for (var i = 0; i < arr.length; i++) pbf.writeSFixed64(arr[i]); }
}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
},{"./buffer":1}],3:[function(require,module,exports){
exports.read = function (buffer, offset, isLE, mLen, nBytes) {
var e, m
var eLen = nBytes * 8 - mLen - 1
var eMax = (1 << eLen) - 1
var eBias = eMax >> 1
var nBits = -7
var i = isLE ? (nBytes - 1) : 0
var d = isLE ? -1 : 1
var s = buffer[offset + i]
i += d
e = s & ((1 << (-nBits)) - 1)
s >>= (-nBits)
nBits += eLen
for (; nBits > 0; e = e * 256 + buffer[offset + i], i += d, nBits -= 8) {}
m = e & ((1 << (-nBits)) - 1)
e >>= (-nBits)
nBits += mLen
for (; nBits > 0; m = m * 256 + buffer[offset + i], i += d, nBits -= 8) {}
if (e === 0) {
e = 1 - eBias
} else if (e === eMax) {
return m ? NaN : ((s ? -1 : 1) * Infinity)
} else {
m = m + Math.pow(2, mLen)
e = e - eBias
}
return (s ? -1 : 1) * m * Math.pow(2, e - mLen)
}
exports.write = function (buffer, value, offset, isLE, mLen, nBytes) {
var e, m, c
var eLen = nBytes * 8 - mLen - 1
var eMax = (1 << eLen) - 1
var eBias = eMax >> 1
var rt = (mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0)
var i = isLE ? 0 : (nBytes - 1)
var d = isLE ? 1 : -1
var s = value < 0 || (value === 0 && 1 / value < 0) ? 1 : 0
value = Math.abs(value)
if (isNaN(value) || value === Infinity) {
m = isNaN(value) ? 1 : 0
e = eMax
} else {
e = Math.floor(Math.log(value) / Math.LN2)
if (value * (c = Math.pow(2, -e)) < 1) {
e--
c *= 2
}
if (e + eBias >= 1) {
value += rt / c
} else {
value += rt * Math.pow(2, 1 - eBias)
}
if (value * c >= 2) {
e++
c /= 2
}
if (e + eBias >= eMax) {
m = 0
e = eMax
} else if (e + eBias >= 1) {
m = (value * c - 1) * Math.pow(2, mLen)
e = e + eBias
} else {
m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen)
e = 0
}
}
for (; mLen >= 8; buffer[offset + i] = m & 0xff, i += d, m /= 256, mLen -= 8) {}
e = (e << mLen) | m
eLen += mLen
for (; eLen > 0; buffer[offset + i] = e & 0xff, i += d, e /= 256, eLen -= 8) {}
buffer[offset + i - d] |= s * 128
}
},{}]},{},[2])(2)
});
ol.ext.pbf = module.exports;
})();