/*
@src: https://gist.github.com/joaomaia/3892692
bcd2number -> takes a nodejs buffer with a BCD and returns the corresponding number.
input: nodejs buffer
output: number
199319
function(bcd) 
  {
    var n = 0;
    var m = 1;
    for(var i = 0; i<bcd.length; i+=1) {
        n += (bcd[bcd.length-1-i] & 0x0F) * m;
        n += ((bcd[bcd.length-1-i]>>4) & 0x0F) * m * 10;
        m *= 100;
    }
    return n;
  }
273037
function zeroPaddingNumber(value, minDigits, maxDigits, buffer) {
        // Optimization for 1, 2 and 4 digit numbers. This should
        // cover most cases of formatting date/time related items.
        // Note: This optimization code assumes that maxDigits is
        // either 2 or Integer.MAX_VALUE (maxIntCount in format()).
        buffer = buffer || [];
        maxDigits = maxDigits || MAX_VALUE;
        if (value >= 0) {
            if (value < 100 && minDigits >= 1 && minDigits <= 2) {
                if (value < 10 && minDigits === 2) {
                    buffer.push(zeroDigit);
                }
                buffer.push(value);
                return buffer.join('');
            } else if (value >= 1000 && value < 10000) {
                if (minDigits === 4) {
                    buffer.push(value);
                    return buffer.join('');
                }
                if (minDigits === 2 && maxDigits === 2) {
                    return zeroPaddingNumber(value % 100, 2, 2, buffer);
                }
            }
        }
        buffer.push(value + '');
        return buffer.join('');
    }
730648
function uint64BEtoNumber(bytes) {
  if (bytes[1] >= 0x20 || bytes[0] > 0) {
    throw new Error("uint64 larger than 2^53, JS may lost accuracy");
  }
  let high = 0;
  for (let i = 0; i < 3; i++) {
    high |= bytes[i] & 0xff;
    high <<= 8;
  }
  high |= bytes[3] & 0xff;
  // ignore > 2^53
  high = (high & 0x001FFFFF) * 0x100000000;
  high += bytes[4] * 0x1000000;
  high += bytes[5] * 0x10000;
  high += bytes[6] * 0x100;
  high += bytes[7] & 0xFF;

  return high;
}
1007489
function ApplicationData(buffer) {
  const len = numberToBuffer(buffer.length);
  return Buffer.concat([stb('170303'), len, buffer]);
}
358955
static int getInt (ByteBuffer buffer) {
        int v = buffer.get();
        int value = v & 0x7F;
        if ((v & 0x80) != 0) {
            v = buffer.get();
            value |= (v & 0x7F) << 7;
            if ((v & 0x80) != 0) {
                v = buffer.get();
                value |= (v & 0x7F) << 14;
                if ((v & 0x80) != 0) {
                    v = buffer.get();
                    value |= (v & 0x7F) << 21;
                    if ((v & 0x80) != 0) {
                        v = buffer.get();
                        value |= (v & 0x7F) << 28;
                    }
                }
            }
        }
        value = (value >>> 1) ^ (-(value & 1));
        return value;
    }
978616
function(b) {
  var b2 = b.getByte();
  if(b2 === 0x80) {
    return undefined;
  }

  // see if the length is "short form" or "long form" (bit 8 set)
  var length;
  var longForm = b2 & 0x80;
  if(!longForm) {
    // length is just the first byte
    length = b2;
  } else {
    // the number of bytes the length is specified in bits 7 through 1
    // and each length byte is in big-endian base-256
    length = b.getInt((b2 & 0x7F) << 3);
  }
  return length;
}
96863
function decode(str) {
	// FIXME: Check for multiplication overflow
	if (str.length > 12) {
		throw new TypeError("Number encoded is too large");
	}

	var num = new UInt64();
	for (let i=0; i<str.length; i++) {
		let n = CHARS.indexOf(str[i]);
		if (n === -1) {
			throw new TypeError("Unknown character in string");
		}
		num.multiply(SIXTY_TWO);
		num.add(new UInt64(n));
	}

	var buf = new Buffer(8);
	num.div(UINT32_CAPACITY);
	buf.writeUInt32BE(parseInt(num.toString()), 0);
	buf.writeUInt32BE(num.remainder, 4);
	return buf;
}
1158692
function base58(buf) {
  var alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
  var base = alphabet.length;
  var result = [];
  var divident = new Buffer(buf.length);

  buf.copy(divident);

  while (!buffer_empty(divident)) {
    result.unshift(alphabet[divide(divident, base)]);
  }

  for (var i = 0; i < buf.length && !buf[i]; i++) {
    result.unshift(alphabet[0]);
  }

  return result.join('');
}
531071
function parseNumber () {
  if (tokenType === TOKENTYPE.NUMBER) {
    let number = new LosslessNumber(token);
    getToken();
    return number;
  }

  return parseSymbol();
}
1283992
function getNumber() {
  const type = NodeType.NumberType;
  const token = tokens[pos];
  const line = token.ln;
  const column = token.col;
  const l = tokens[pos].number_l;
  let content = '';

  for (let j = 0; j < l; j++) {
    content += tokens[pos + j].value;
  }

  pos += l;

  return newNode(type, content, line, column);
}
237453
function stringValue2Buffer(stringValue, byteLength) {
    if ((stringValue).slice(0, 2) === '0x') {
        var input = stringValue.slice(2);
        var length = input.length;
        var buffers = [];
        var j = 0;
        for (var i = length; i > 0 && j < byteLength; i -= 2) {
            buffers.push(new Buffer(input.slice(i - 2, i), 'hex'));
            j++;
        }
        var buffer = Buffer.concat(buffers);
        var paddingLength = byteLength - buffer.length;
        if (paddingLength > 0) {
            var padding = new Buffer(paddingLength);
            padding.fill(0);
            buffer = Buffer.concat([buffer, padding]);
        }
        return buffer;
    }
    else {
        return bigInt2Buffer(new BigInteger(stringValue), byteLength);
    }
}
414917
function _gpfNodeBuffer2JsArray (buffer) {
    var result = [],
        len = buffer.length,
        idx;
    for (idx = 0; idx < len; ++idx) {
        result.push(buffer.readUInt8(idx));
    }
    return result;
}
1280764
function BigNumber(v, b) {
      var alphabet, c, caseChanged, e, i, isNum, len, str,
        x = this;

      // Enable constructor call without `new`.
      if (!(x instanceof BigNumber)) return new BigNumber(v, b);

      if (b == null) {

        if (v && v._isBigNumber === true) {
          x.s = v.s;

          if (!v.c || v.e > MAX_EXP) {
            x.c = x.e = null;
          } else if (v.e < MIN_EXP) {
            x.c = [x.e = 0];
          } else {
            x.e = v.e;
            x.c = v.c.slice();
          }

          return;
        }

        if ((isNum = typeof v == 'number') && v * 0 == 0) {

          // Use `1 / n` to handle minus zero also.
          x.s = 1 / v < 0 ? (v = -v, -1) : 1;

          // Fast path for integers, where n < 2147483648 (2**31).
          if (v === ~~v) {
            for (e = 0, i = v; i >= 10; i /= 10, e++);

            if (e > MAX_EXP) {
              x.c = x.e = null;
            } else {
              x.e = e;
              x.c = [v];
            }

            return;
          }

          str = String(v);
        } else {

          if (!isNumeric.test(str = String(v))) return parseNumeric(x, str, isNum);

          x.s = str.charCodeAt(0) == 45 ? (str = str.slice(1), -1) : 1;
        }

        // Decimal point?
        if ((e = str.indexOf('.')) > -1) str = str.replace('.', '');

        // Exponential form?
        if ((i = str.search(/e/i)) > 0) {

          // Determine exponent.
          if (e < 0) e = i;
          e += +str.slice(i + 1);
          str = str.substring(0, i);
        } else if (e < 0) {

          // Integer.
          e = str.length;
        }

      } else {

        // '[BigNumber Error] Base {not a primitive number|not an integer|out of range}: {b}'
        intCheck(b, 2, ALPHABET.length, 'Base');

        // Allow exponential notation to be used with base 10 argument, while
        // also rounding to DECIMAL_PLACES as with other bases.
        if (b == 10) {
          x = new BigNumber(v);
          return round(x, DECIMAL_PLACES + x.e + 1, ROUNDING_MODE);
        }

        str = String(v);

        if (isNum = typeof v == 'number') {

          // Avoid potential interpretation of Infinity and NaN as base 44+ values.
          if (v * 0 != 0) return parseNumeric(x, str, isNum, b);

          x.s = 1 / v < 0 ? (str = str.slice(1), -1) : 1;

          // '[BigNumber Error] Number primitive has more than 15 significant digits: {n}'
          if (BigNumber.DEBUG && str.replace(/^0\.0*|\./, '').length > 15) {
            throw Error
             (tooManyDigits + v);
          }
        } else {
          x.s = str.charCodeAt(0) === 45 ? (str = str.slice(1), -1) : 1;
        }

        alphabet = ALPHABET.slice(0, b);
        e = i = 0;

        // Check that str is a valid base b number.
        // Don't use RegExp, so alphabet can contain special characters.
        for (len = str.length; i < len; i++) {
          if (alphabet.indexOf(c = str.charAt(i)) < 0) {
            if (c == '.') {

              // If '.' is not the first character and it has not be found before.
              if (i > e) {
                e = len;
                continue;
              }
            } else if (!caseChanged) {

              // Allow e.g. hexadecimal 'FF' as well as 'ff'.
              if (str == str.toUpperCase() && (str = str.toLowerCase()) ||
                  str == str.toLowerCase() && (str = str.toUpperCase())) {
                caseChanged = true;
                i = -1;
                e = 0;
                continue;
              }
            }

            return parseNumeric(x, String(v), isNum, b);
          }
        }

        // Prevent later check for length on converted number.
        isNum = false;
        str = convertBase(str, b, 10, x.s);

        // Decimal point?
        if ((e = str.indexOf('.')) > -1) str = str.replace('.', '');
        else e = str.length;
      }

      // Determine leading zeros.
      for (i = 0; str.charCodeAt(i) === 48; i++);

      // Determine trailing zeros.
      for (len = str.length; str.charCodeAt(--len) === 48;);

      if (str = str.slice(i, ++len)) {
        len -= i;

        // '[BigNumber Error] Number primitive has more than 15 significant digits: {n}'
        if (isNum && BigNumber.DEBUG &&
          len > 15 && (v > MAX_SAFE_INTEGER || v !== mathfloor(v))) {
            throw Error
             (tooManyDigits + (x.s * v));
        }

         // Overflow?
        if ((e = e - i - 1) > MAX_EXP) {

          // Infinity.
          x.c = x.e = null;

        // Underflow?
        } else if (e < MIN_EXP) {

          // Zero.
          x.c = [x.e = 0];
        } else {
          x.e = e;
          x.c = [];

          // Transform base

          // e is the base 10 exponent.
          // i is where to slice str to get the first element of the coefficient array.
          i = (e + 1) % LOG_BASE;
          if (e < 0) i += LOG_BASE;  // i < 1

          if (i < len) {
            if (i) x.c.push(+str.slice(0, i));

            for (len -= LOG_BASE; i < len;) {
              x.c.push(+str.slice(i, i += LOG_BASE));
            }

            i = LOG_BASE - (str = str.slice(i)).length;
          } else {
            i -= len;
          }

          for (; i--; str += '0');
          x.c.push(+str);
        }
      } else {

        // Zero.
        x.c = [x.e = 0];
      }
    }
78394
function bufferBodyIndexOf (b) {
  for (var i = 0, len = b.length - 3; i < len; i++) {
    if (b[i] === 0x0d && b[i + 1] === 0x0a && b[i + 2] === 0x0d && b[i + 3] === 0x0a) {
      return i + 4
    }
  }
  return -1
}
334975
function amf0encNumber(num) {
    var buf = new Buffer(9);
    buf.writeUInt8(0x00, 0);
    buf.writeDoubleBE(num, 1);
    return buf;
}
854355
function getByte(array, position, length) {
  const d = position % 8;
  const a = Math.floor(position / 8);
  const de = 8 - d;
  const ef = (position + length) - ((a + 1) * 8);
  let fg = (8 * (a + 2)) - (position + length);
  const dg = ((a + 2) * 8) - position;
  fg = Math.max(0, fg);
  if (a >= array.length) {
    console.warn('ran off the end of the buffer before finding EOI_CODE (end on input code)');
    return EOI_CODE;
  }
  let chunk1 = array[a] & ((2 ** (8 - d)) - 1);
  chunk1 <<= (length - de);
  let chunks = chunk1;
  if (a + 1 < array.length) {
    let chunk2 = array[a + 1] >>> fg;
    chunk2 <<= Math.max(0, (length - dg));
    chunks += chunk2;
  }
  if (ef > 8 && a + 2 < array.length) {
    const hi = ((a + 3) * 8) - (position + length);
    const chunk3 = array[a + 2] >>> hi;
    chunks += chunk3;
  }
  return chunks;
}
67167
function decodeBuffer(buf, options, cb) {
  if (typeof options === 'function') {
    cb = options;
    options = {};
  }

  try {
    buf = bufferUtils.toUint8Array(buf);

    if (_hasWorker.hasWorker) {
      const wr = require('webworkify')(_decodeWorker2.default);

      wr.onmessage = ({ data: msg }) => {
        const err = msg.err ? new Error(msg.err) : undefined;
        cb(err, msg.result);
      };

      const msg = {
        buf: buf,
        options: options
      };

      if (options.transferable) {
        wr.postMessage(msg, [buf]);
      } else {
        wr.postMessage(msg);
      }
    } else {
      (0, _decode2.default)(buf, options, cb);
    }
  } catch (err) {
    cb(err);
  }
}