<commit_before>'use strict';

var alphabets = require('./alphabets');
var v6 = require('ipv6').v6;
var bignum = require('bignum');

var NUM_MAXVALUE = Math.pow(2, 32) - 1;
var QUAD85 = 85 * 85 * 85 * 85;
var TRIO85 = 85 * 85 * 85;
var DUO85  = 85 * 85;
var SING85 = 85;

var DEFAULT_ENCODING = 'z85';

/* Characters to allow (and ignore) in an encoded buffer */
var IGNORE_CHARS = [
  0x09, /* horizontal tab */
  0x0a, /* line feed, new line */
  0x0b, /* vertical tab */
  0x0c, /* form feed, new page */
  0x0d, /* carriage return */
  0x20  /* space */
];

var ASCII85_ENC_START = '<~';
var ASCII85_ENC_END   = '~>';

/* Function borrowed from noseglid/canumb (github) */
function pad(width, number)
{
  return new Array(1 + width - number.length).join('0') + number;
}

function encodeBignumIPv6(num)
{
  var enctable = alphabets.ipv6.enc;

  var enc = [];
  for (var i = 1; i < 20; ++i) {
    enc.push(enctable[num.mod(85).toNumber()]); /* Ranges between 0 - 84 */
    num = num.div(85);
  }
  enc.push(enctable[num.toNumber()]); /* What's left is also in range 0 - 84 */
  return enc.reverse().join('');
}

function encodeBufferIPv6(buffer)
{
  if (16 !== buffer.length) {
    /* An IPv6 address must be exactly 16 bytes, 128 bits long */
    return false;
  }

  return encodeBignumIPv6(bignum.fromBuffer(buffer));
}

function encodeStringIPv6(string)
{
  var addr = new v6.Address(string);
  if (!addr.isValid()) {
    return false;
  }

  var num = bignum(addr.parsedAddress.map(function(el) {
    return pad(4, el);
  }).join(''), 16);

  return encodeBignumIPv6(num);
}

function decodeStringIPv6(string)
{
  if (20 !== string.length) {
    /* An encoded IPv6 is always (5/4) * 16 = 20 bytes */
    return false;
  }

  var dectable = alphabets.ipv6.dec;
  var i = 0;
  var binary = string.split('').reduceRight(function(memo, el) {
    var num = bignum(dectable[el.charCodeAt(0)]);
    var fact = bignum(85).pow(i++);
    var contrib = num.mul(fact);
    return memo.add(contrib);
  }, bignum(0));

  return v6.Address.fromBigInteger(binary).correctForm();
}

function decodeBufferIPv6(buffer)
{
  return decodeStringIPv6(buffer.toString());
}

function encodeBuffer(buffer, encoding)
{
  if ('z85' === encoding && buffer.length % 4 !== 0) {
    return false;
  }
  var enctable = alphabets[encoding].enc;
  var padding = (buffer.length % 4 === 0) ? 0 : 4 - buffer.length % 4;

  var result = '';
  for (var i = 0; i < buffer.length; i += 4) {

    /* 32 bit number of the current 4 bytes (padded with 0 as necessary) */
    var num = ((buffer[i] << 24) >>> 0) + // Shift right to force unsigned number
        (((i + 1 > buffer.length ? 0 : buffer[i + 1]) << 16) >>> 0) +
        (((i + 2 > buffer.length ? 0 : buffer[i + 2]) <<  8) >>> 0) +
        (((i + 3 > buffer.length ? 0 : buffer[i + 3]) <<  0) >>> 0);

    /* Create 5 characters from '!' to 'u' alphabet */
    var block = [];
    for (var j = 0; j < 5; ++j) {
      block.unshift(enctable[num % 85]);
      num = Math.floor(num / 85);
    }
    
    block = block.join('');
    if (block === '!!!!!' && 'ascii85' === encoding) {
      block = 'z';
    }
    /* And append them to the result */
    result += block;
  }

  return (('ascii85' === encoding) ? ASCII85_ENC_START : '') +
         result.substring(0, result.length - padding) +
         (('ascii85' === encoding) ? ASCII85_ENC_END : '');
}

function encodeString(string, encoding)<commit_msg>Fix code style<commit_after>'use strict';

var alphabets = require('./alphabets');
var v6 = require('ipv6').v6;
var bignum = require('bignum');

var NUM_MAXVALUE = Math.pow(2, 32) - 1;
var QUAD85 = 85 * 85 * 85 * 85;
var TRIO85 = 85 * 85 * 85;
var DUO85  = 85 * 85;
var SING85 = 85;

var DEFAULT_ENCODING = 'z85';

/* Characters to allow (and ignore) in an encoded buffer */
var IGNORE_CHARS = [
  0x09, /* horizontal tab */
  0x0a, /* line feed, new line */
  0x0b, /* vertical tab */
  0x0c, /* form feed, new page */
  0x0d, /* carriage return */
  0x20  /* space */
];

var ASCII85_ENC_START = '<~';
var ASCII85_ENC_END   = '~>';

/* Function borrowed from noseglid/canumb (github) */
function pad(width, number)
{
  return new Array(1 + width - number.length).join('0') + number;
}

function encodeBignumIPv6(num)
{
  var enctable = alphabets.ipv6.enc;

  var enc = [];
  for (var i = 1; i < 20; ++i) {
    enc.push(enctable[num.mod(85).toNumber()]); /* Ranges between 0 - 84 */
    num = num.div(85);
  }
  enc.push(enctable[num.toNumber()]); /* What's left is also in range 0 - 84 */
  return enc.reverse().join('');
}

function encodeBufferIPv6(buffer)
{
  if (16 !== buffer.length) {
    /* An IPv6 address must be exactly 16 bytes, 128 bits long */
    return false;
  }

  return encodeBignumIPv6(bignum.fromBuffer(buffer));
}

function encodeStringIPv6(string)
{
  var addr = new v6.Address(string);
  if (!addr.isValid()) {
    return false;
  }

  var num = bignum(addr.parsedAddress.map(function(el) {
    return pad(4, el);
  }).join(''), 16);

  return encodeBignumIPv6(num);
}

function decodeStringIPv6(string)
{
  if (20 !== string.length) {
    /* An encoded IPv6 is always (5/4) * 16 = 20 bytes */
    return false;
  }

  var dectable = alphabets.ipv6.dec;
  var i = 0;
  var binary = string.split('').reduceRight(function(memo, el) {
    var num = bignum(dectable[el.charCodeAt(0)]);
    var fact = bignum(85).pow(i++);
    var contrib = num.mul(fact);
    return memo.add(contrib);
  }, bignum(0));

  return v6.Address.fromBigInteger(binary).correctForm();
}

function decodeBufferIPv6(buffer)
{
  return decodeStringIPv6(buffer.toString());
}

function encodeBuffer(buffer, encoding)
{
  if ('z85' === encoding && buffer.length % 4 !== 0) {
    return false;
  }
  var enctable = alphabets[encoding].enc;
  var padding = (buffer.length % 4 === 0) ? 0 : 4 - buffer.length % 4;

  var result = '';
  for (var i = 0; i < buffer.length; i += 4) {

    /* 32 bit number of the current 4 bytes (padded with 0 as necessary) */
    var num = ((buffer[i] << 24) >>> 0) + // Shift right to force unsigned number
        (((i + 1 > buffer.length ? 0 : buffer[i + 1]) << 16) >>> 0) +
        (((i + 2 > buffer.length ? 0 : buffer[i + 2]) <<  8) >>> 0) +
        (((i + 3 > buffer.length ? 0 : buffer[i + 3]) <<  0) >>> 0);

    /* Create 5 characters from '!' to 'u' alphabet */
    var block = [];
    for (var j = 0; j < 5; ++j) {
      block.unshift(enctable[num % 85]);
      num = Math.floor(num / 85);
    }

    block = block.join('');
    if (block === '!!!!!' && 'ascii85' === encoding) {
      block = 'z';
    }
    /* And append them to the result */
    result += block;
  }

  return (('ascii85' === encoding) ? ASCII85_ENC_START : '') +
         result.substring(0, result.length - padding) +
         (('ascii85' === encoding) ? ASCII85_ENC_END : '');
}

function encodeString(string, encoding)
{'fraction_non_alphanumeric': 0.17656500802568217, 'fraction_numerical': 0.04640303516708011, 'mean_word_length': 2.945883707541739, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2916270', 'n_tokens_mistral': 2742, 'n_tokens_neox': 2471, 'n_words': 669}