How to use the bodec.toHex function in bodec

To help you get started, we’ve selected a few bodec examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github es-git / es-git / ts / mixins / fs-db.ts View on Github external
throw new Error("Only v2 pack indexes supported");
  }

  // Get the number of hashes in index
  // This is the value of the last fan-out entry
  let hashOffset = 8 + 255 * 4;
  const length = readUint32(buffer, hashOffset);
  hashOffset += 4;
  const crcOffset = hashOffset + 20 * length;
  const lengthOffset = crcOffset + 4 * length;
  const largeOffset = lengthOffset + 4 * length;
  let checkOffset = largeOffset;
  const indexes = new Array(length);
  for (let i = 0; i < length; i++) {
    const start = hashOffset + i * 20;
    const hash = bodec.toHex(bodec.slice(buffer, start, start + 20));
    const crc = readUint32(buffer, crcOffset + i * 4);
    let offset = readUint32(buffer, lengthOffset + i * 4);
    if (offset & 0x80000000) {
      offset = largeOffset + (offset &0x7fffffff) * 8;
      checkOffset = Math.max(checkOffset, offset + 8);
      offset = readUint64(buffer, offset);
    }
    indexes[i] = {
      hash: hash,
      offset: offset,
      crc: crc
    };
  }
  const packChecksum = bodec.toHex(bodec.slice(buffer, checkOffset, checkOffset + 20));
  const checksum = bodec.toHex(bodec.slice(buffer, checkOffset + 20, checkOffset + 40));
  if (sha1(bodec.slice(buffer, 0, checkOffset + 20)) !== checksum) {
github es-git / es-git / test / test-pack-codec.js View on Github external
function verifyEncodePack() {
    try {
      unpackStream(newPack);
      if (bodec.toHex(pack) !== bodec.toHex(newPack)) {
        throw new Error("Final pack doesn't match original.");
      }
    }
    catch (err) {
      console.log(bodec.toHex(pack));
      console.log(bodec.toHex(newPack));
      throw err;
    }
  }
]);
github es-git / es-git / test / test-pack-codec.js View on Github external
function verifyEncodePack() {
    try {
      unpackStream(newPack);
      if (bodec.toHex(pack) !== bodec.toHex(newPack)) {
        throw new Error("Final pack doesn't match original.");
      }
    }
    catch (err) {
      console.log(bodec.toHex(pack));
      console.log(bodec.toHex(newPack));
      throw err;
    }
  }
]);
github es-git / es-git / test / test-pack-codec.js View on Github external
function verifyEncodePack() {
    try {
      unpackStream(newPack);
      if (bodec.toHex(pack) !== bodec.toHex(newPack)) {
        throw new Error("Final pack doesn't match original.");
      }
    }
    catch (err) {
      console.log(bodec.toHex(pack));
      console.log(bodec.toHex(newPack));
      throw err;
    }
  }
]);
github es-git / es-git / ts / mixins / fs-db.ts View on Github external
const hash = bodec.toHex(bodec.slice(buffer, start, start + 20));
    const crc = readUint32(buffer, crcOffset + i * 4);
    let offset = readUint32(buffer, lengthOffset + i * 4);
    if (offset & 0x80000000) {
      offset = largeOffset + (offset &0x7fffffff) * 8;
      checkOffset = Math.max(checkOffset, offset + 8);
      offset = readUint64(buffer, offset);
    }
    indexes[i] = {
      hash: hash,
      offset: offset,
      crc: crc
    };
  }
  const packChecksum = bodec.toHex(bodec.slice(buffer, checkOffset, checkOffset + 20));
  const checksum = bodec.toHex(bodec.slice(buffer, checkOffset + 20, checkOffset + 40));
  if (sha1(bodec.slice(buffer, 0, checkOffset + 20)) !== checksum) {
    throw new Error("Checksum mistmatch");
  }

  const byHash = {};
  indexes.sort((a, b) => a.offset - b.offset);
  indexes.forEach(data => {
    byHash[data.hash] = {
      offset: data.offset,
      crc: data.crc,
    };
  });
  const offsets = indexes.map(entry => entry.offset).sort((a, b) => a - b);

  return {
    offsets: offsets,
github es-git / es-git / ts / lib / pack-codec.ts View on Github external
export function parseEntry(chunk : Uint8Array) : Entry {
  let offset = 0;
  let byte = chunk[offset++];
  const type = numToType[(byte >> 4) & 0x7];
  let size = byte & 0xf;
  let left = 4;
  while (byte & 0x80) {
    byte = chunk[offset++];
    size |= (byte & 0x7f) << left;
    left += 7;
  }
  size = size >>> 0;
  let ref;
  if (type === "ref-delta") {
    ref = bodec.toHex(bodec.slice(chunk, offset, offset += 20));
  }
  else if (type === "ofs-delta") {
    byte = chunk[offset++];
    ref = byte & 0x7f;
    while (byte & 0x80) {
      byte = chunk[offset++];
      ref = ((ref + 1) << 7) | (byte & 0x7f);
    }
  }

  const body = inflate(bodec.slice(chunk, offset));
  if (body.length !== size) {
    throw new Error("Size mismatch");
  }
  const result : any = {
    type: type,
github es-git / es-git / ts / lib / object-codec.ts View on Github external
let i = 0;
  const length = body.length;
  let start;
  let mode;
  let name;
  let hash;
  const tree : TreeBody = {};
  while (i < length) {
    start = i;
    i = indexOf(body, 0x20, start);
    if (i < 0) throw new SyntaxError("Missing space");
    mode = parseOct(body, start, i++);
    start = i;
    i = indexOf(body, 0x00, start);
    name = bodec.toUnicode(body, start, i++);
    hash = bodec.toHex(body, i, i += 20);
    tree[name] = {
      mode: mode,
      hash: hash
    };
  }
  return tree;
}
github digidem / react-mapfilter / app / js / lib / backbone-github.js View on Github external
var i = 0;
  var length = body.length;
  var start;
  var mode;
  var name;
  var hash;
  var tree = {};
  while (i < length) {
    start = i;
    i = indexOf(body, 0x20, start);
    if (i < 0) throw new SyntaxError("Missing space");
    mode = parseOct(body, start, i++);
    start = i;
    i = indexOf(body, 0x00, start);
    name = bodec.toUnicode(body, start, i++);
    hash = bodec.toHex(body, i, i += 20);
    tree[name] = {
      mode: mode,
      hash: hash
    };
  }
  return tree;
}