Пример #1
0
// Do not cache `Buffer.isEncoding` when checking encoding names as some
// modules monkey-patch it to support additional encodings
function normalizeEncoding(enc) {
  const nenc = internalUtil.normalizeEncoding(enc);
  if (typeof nenc !== 'string' &&
      (Buffer.isEncoding === isEncoding || !Buffer.isEncoding(enc)))
    throw new Error(`Unknown encoding: ${enc}`);
  return nenc || enc;
}
Пример #2
0
// Do not cache `Buffer.isEncoding` when checking encoding names as some
// modules monkey-patch it to support additional encodings
function normalizeEncoding(enc) {
  const nenc = internalUtil.normalizeEncoding(enc);
  if (typeof nenc !== 'string' &&
      (Buffer.isEncoding === isEncoding || !Buffer.isEncoding(enc)))
    throw new errors.TypeError('ERR_UNKNOWN_ENCODING', enc);
  return nenc || enc;
}
Пример #3
0
  transcode = function transcode(source, fromEncoding, toEncoding) {
    if (!isUint8Array(source))
      throw new TypeError('"source" argument must be a Buffer or Uint8Array');
    if (source.length === 0) return Buffer.alloc(0);

    fromEncoding = normalizeEncoding(fromEncoding) || fromEncoding;
    toEncoding = normalizeEncoding(toEncoding) || toEncoding;
    const result = _transcode(source, fromEncoding, toEncoding);
    if (typeof result !== 'number')
      return result;

    const code = icuErrName(result);
    const err = new Error(`Unable to transcode Buffer [${code}]`);
    err.code = code;
    err.errno = result;
    throw err;
  };
Пример #4
0
function getDecoder(decoder, encoding) {
  encoding = normalizeEncoding(encoding);
  if (StringDecoder === undefined)
    StringDecoder = require('string_decoder').StringDecoder;
  decoder = decoder || new StringDecoder(encoding);
  assert(decoder.encoding === encoding, 'Cannot change encoding');
  return decoder;
}
Пример #5
0
function main({ encoding, n }) {
  const { normalizeEncoding } = require('internal/util');

  bench.start();
  for (var i = 0; i < n; i++) {
    normalizeEncoding(encoding);
  }
  bench.end(n);
}
Пример #6
0
  transcode = function transcode(source, fromEncoding, toEncoding) {
    if (!isUint8Array(source))
      throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'source',
                                 ['Buffer', 'Uint8Array'], source);
    if (source.length === 0) return Buffer.alloc(0);

    fromEncoding = normalizeEncoding(fromEncoding) || fromEncoding;
    toEncoding = normalizeEncoding(toEncoding) || toEncoding;
    const result = _transcode(source, fromEncoding, toEncoding);
    if (typeof result !== 'number')
      return result;

    const code = icuErrName(result);
    const err = new Error(`Unable to transcode Buffer [${code}]`);
    err.code = code;
    err.errno = result;
    throw err;
  };
Пример #7
0
function fill_(buf, val, start, end, encoding) {
  // Handle string cases:
  if (typeof val === 'string') {
    if (typeof start === 'string') {
      encoding = start;
      start = 0;
      end = buf.length;
    } else if (typeof end === 'string') {
      encoding = end;
      end = buf.length;
    }

    if (encoding !== undefined && typeof encoding !== 'string') {
      throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'encoding',
                                 'string', encoding);
    }
    var normalizedEncoding = normalizeEncoding(encoding);
    if (normalizedEncoding === undefined) {
      throw new errors.TypeError('ERR_UNKNOWN_ENCODING', encoding);
    }

    if (val.length === 0) {
      // Previously, if val === '', the Buffer would not fill,
      // which is rather surprising.
      val = 0;
    } else if (val.length === 1) {
      var code = val.charCodeAt(0);
      if ((normalizedEncoding === 'utf8' && code < 128) ||
          normalizedEncoding === 'latin1') {
        // Fast path: If `val` fits into a single byte, use that numeric value.
        val = code;
      }
    }
  } else if (typeof val === 'number') {
    val = val & 255;
  }

  // Invalid ranges are not set to a default, so can range check early.
  if (start < 0 || end > buf.length)
    throw new errors.RangeError('ERR_INDEX_OUT_OF_RANGE');

  if (end <= start)
    return 0;

  start = start >>> 0;
  end = end === undefined ? buf.length : end >>> 0;
  const fillLength = bindingFill(buf, val, start, end, encoding);

  if (fillLength === -1)
    throw new errors.TypeError('ERR_INVALID_ARG_VALUE', 'value', val);

  return fillLength;
}
Пример #8
0
Hash.prototype.digest = function digest(outputEncoding) {
  const state = this[kState];
  if (state[kFinalized])
    throw new ERR_CRYPTO_HASH_FINALIZED();
  outputEncoding = outputEncoding || getDefaultEncoding();
  if (normalizeEncoding(outputEncoding) === 'utf16le')
    throw new ERR_CRYPTO_HASH_DIGEST_NO_UTF16();

  // Explicit conversion for backward compatibility.
  const ret = this[kHandle].digest(`${outputEncoding}`);
  state[kFinalized] = true;
  return ret;
};
Пример #9
0
Buffer.prototype.fill = function fill(val, start, end, encoding) {
  // Handle string cases:
  if (typeof val === 'string') {
    if (typeof start === 'string') {
      encoding = start;
      start = 0;
      end = this.length;
    } else if (typeof end === 'string') {
      encoding = end;
      end = this.length;
    }

    if (encoding !== undefined && typeof encoding !== 'string') {
      throw new TypeError('encoding must be a string');
    }
    var normalizedEncoding = internalUtil.normalizeEncoding(encoding);
    if (normalizedEncoding === undefined) {
      throw new TypeError('Unknown encoding: ' + encoding);
    }

    if (val.length === 0) {
      // Previously, if val === '', the Buffer would not fill,
      // which is rather surprising.
      val = 0;
    } else if (val.length === 1) {
      var code = val.charCodeAt(0);
      if ((normalizedEncoding === 'utf8' && code < 128) ||
          normalizedEncoding === 'latin1') {
        // Fast path: If `val` fits into a single byte, use that numeric value.
        val = code;
      }
    }
  } else if (typeof val === 'number') {
    val = val & 255;
  }

  // Invalid ranges are not set to a default, so can range check early.
  if (start < 0 || end > this.length)
    throw new RangeError('Out of range index');

  if (end <= start)
    return this;

  start = start >>> 0;
  end = end === undefined ? this.length : end >>> 0;

  binding.fill(this, val, start, end, encoding);

  return this;
};
Пример #10
0
function main({ input, n }) {
  const { normalizeEncoding } = require('internal/util');
  const inputs = getInput(input);
  var noDead = '';

  bench.start();
  for (var i = 0; i < n; i += 1) {
    for (var j = 0; j < inputs.length; ++j) {
      noDead = normalizeEncoding(inputs[j]);
    }
  }
  bench.end(n);
  assert.ok(noDead === undefined || noDead.length > 0);
}
Пример #11
0
Hmac.prototype.digest = function digest(outputEncoding) {
  const state = this[kState];
  outputEncoding = outputEncoding || getDefaultEncoding();
  if (normalizeEncoding(outputEncoding) === 'utf16le')
    throw new ERR_CRYPTO_HASH_DIGEST_NO_UTF16();

  if (state[kFinalized]) {
    const buf = Buffer.from('');
    return outputEncoding === 'buffer' ? buf : buf.toString(outputEncoding);
  }

  // Explicit conversion for backward compatibility.
  const ret = this[kHandle].digest(`${outputEncoding}`);
  state[kFinalized] = true;
  return ret;
};
Пример #12
0
Buffer.isEncoding = function isEncoding(encoding) {
  return typeof encoding === 'string' &&
         typeof normalizeEncoding(encoding) === 'string';
};
Пример #13
0
function getDecoder(decoder, encoding) {
  encoding = internalUtil.normalizeEncoding(encoding);
  decoder = decoder || new StringDecoder(encoding);
  assert(decoder.encoding === encoding, 'Cannot change encoding');
  return decoder;
}
Пример #14
0
Buffer.isEncoding = function(encoding) {
  return typeof encoding === 'string' &&
         typeof internalUtil.normalizeEncoding(encoding) === 'string';
};
tests.forEach((i) => {
  assert.strictEqual(util.normalizeEncoding(i[0]), i[1]);
});