CoolFace
Datasetpublic

basant307/AI_Governance_Project

sourceHugging Faceapache-2.0updated 2mo agoView on Hugging Face
0likes45downloads
headers.js296 linesDownload Raw Back to tar-stream
1var alloc = Buffer.alloc2 3var ZEROS = '0000000000000000000'4var SEVENS = '7777777777777777777'5var ZERO_OFFSET = '0'.charCodeAt(0)6var USTAR_MAGIC = Buffer.from('ustar\x00', 'binary')7var USTAR_VER = Buffer.from('00', 'binary')8var GNU_MAGIC = Buffer.from('ustar\x20', 'binary')9var GNU_VER = Buffer.from('\x20\x00', 'binary')10var MASK = parseInt('7777', 8)11var MAGIC_OFFSET = 25712var VERSION_OFFSET = 26313 14var clamp = function (index, len, defaultValue) {15  if (typeof index !== 'number') return defaultValue16  index = ~~index // Coerce to integer.17  if (index >= len) return len18  if (index >= 0) return index19  index += len20  if (index >= 0) return index21  return 022}23 24var toType = function (flag) {25  switch (flag) {26    case 0:27      return 'file'28    case 1:29      return 'link'30    case 2:31      return 'symlink'32    case 3:33      return 'character-device'34    case 4:35      return 'block-device'36    case 5:37      return 'directory'38    case 6:39      return 'fifo'40    case 7:41      return 'contiguous-file'42    case 72:43      return 'pax-header'44    case 55:45      return 'pax-global-header'46    case 27:47      return 'gnu-long-link-path'48    case 28:49    case 30:50      return 'gnu-long-path'51  }52 53  return null54}55 56var toTypeflag = function (flag) {57  switch (flag) {58    case 'file':59      return 060    case 'link':61      return 162    case 'symlink':63      return 264    case 'character-device':65      return 366    case 'block-device':67      return 468    case 'directory':69      return 570    case 'fifo':71      return 672    case 'contiguous-file':73      return 774    case 'pax-header':75      return 7276  }77 78  return 079}80 81var indexOf = function (block, num, offset, end) {82  for (; offset < end; offset++) {83    if (block[offset] === num) return offset84  }85  return end86}87 88var cksum = function (block) {89  var sum = 8 * 3290  for (var i = 0; i < 148; i++) sum += block[i]91  for (var j = 156; j < 512; j++) sum += block[j]92  return sum93}94 95var encodeOct = function (val, n) {96  val = val.toString(8)97  if (val.length > n) return SEVENS.slice(0, n) + ' '98  else return ZEROS.slice(0, n - val.length) + val + ' '99}100 101/* Copied from the node-tar repo and modified to meet102 * tar-stream coding standard.103 *104 * Source: https://github.com/npm/node-tar/blob/51b6627a1f357d2eb433e7378e5f05e83b7aa6cd/lib/header.js#L349105 */106function parse256 (buf) {107  // first byte MUST be either 80 or FF108  // 80 for positive, FF for 2's comp109  var positive110  if (buf[0] === 0x80) positive = true111  else if (buf[0] === 0xFF) positive = false112  else return null113 114  // build up a base-256 tuple from the least sig to the highest115  var tuple = []116  for (var i = buf.length - 1; i > 0; i--) {117    var byte = buf[i]118    if (positive) tuple.push(byte)119    else tuple.push(0xFF - byte)120  }121 122  var sum = 0123  var l = tuple.length124  for (i = 0; i < l; i++) {125    sum += tuple[i] * Math.pow(256, i)126  }127 128  return positive ? sum : -1 * sum129}130 131var decodeOct = function (val, offset, length) {132  val = val.slice(offset, offset + length)133  offset = 0134 135  // If prefixed with 0x80 then parse as a base-256 integer136  if (val[offset] & 0x80) {137    return parse256(val)138  } else {139    // Older versions of tar can prefix with spaces140    while (offset < val.length && val[offset] === 32) offset++141    var end = clamp(indexOf(val, 32, offset, val.length), val.length, val.length)142    while (offset < end && val[offset] === 0) offset++143    if (end === offset) return 0144    return parseInt(val.slice(offset, end).toString(), 8)145  }146}147 148var decodeStr = function (val, offset, length, encoding) {149  return val.slice(offset, indexOf(val, 0, offset, offset + length)).toString(encoding)150}151 152var addLength = function (str) {153  var len = Buffer.byteLength(str)154  var digits = Math.floor(Math.log(len) / Math.log(10)) + 1155  if (len + digits >= Math.pow(10, digits)) digits++156 157  return (len + digits) + str158}159 160exports.decodeLongPath = function (buf, encoding) {161  return decodeStr(buf, 0, buf.length, encoding)162}163 164exports.encodePax = function (opts) { // TODO: encode more stuff in pax165  var result = ''166  if (opts.name) result += addLength(' path=' + opts.name + '\n')167  if (opts.linkname) result += addLength(' linkpath=' + opts.linkname + '\n')168  var pax = opts.pax169  if (pax) {170    for (var key in pax) {171      result += addLength(' ' + key + '=' + pax[key] + '\n')172    }173  }174  return Buffer.from(result)175}176 177exports.decodePax = function (buf) {178  var result = {}179 180  while (buf.length) {181    var i = 0182    while (i < buf.length && buf[i] !== 32) i++183    var len = parseInt(buf.slice(0, i).toString(), 10)184    if (!len) return result185 186    var b = buf.slice(i + 1, len - 1).toString()187    var keyIndex = b.indexOf('=')188    if (keyIndex === -1) return result189    result[b.slice(0, keyIndex)] = b.slice(keyIndex + 1)190 191    buf = buf.slice(len)192  }193 194  return result195}196 197exports.encode = function (opts) {198  var buf = alloc(512)199  var name = opts.name200  var prefix = ''201 202  if (opts.typeflag === 5 && name[name.length - 1] !== '/') name += '/'203  if (Buffer.byteLength(name) !== name.length) return null // utf-8204 205  while (Buffer.byteLength(name) > 100) {206    var i = name.indexOf('/')207    if (i === -1) return null208    prefix += prefix ? '/' + name.slice(0, i) : name.slice(0, i)209    name = name.slice(i + 1)210  }211 212  if (Buffer.byteLength(name) > 100 || Buffer.byteLength(prefix) > 155) return null213  if (opts.linkname && Buffer.byteLength(opts.linkname) > 100) return null214 215  buf.write(name)216  buf.write(encodeOct(opts.mode & MASK, 6), 100)217  buf.write(encodeOct(opts.uid, 6), 108)218  buf.write(encodeOct(opts.gid, 6), 116)219  buf.write(encodeOct(opts.size, 11), 124)220  buf.write(encodeOct((opts.mtime.getTime() / 1000) | 0, 11), 136)221 222  buf[156] = ZERO_OFFSET + toTypeflag(opts.type)223 224  if (opts.linkname) buf.write(opts.linkname, 157)225 226  USTAR_MAGIC.copy(buf, MAGIC_OFFSET)227  USTAR_VER.copy(buf, VERSION_OFFSET)228  if (opts.uname) buf.write(opts.uname, 265)229  if (opts.gname) buf.write(opts.gname, 297)230  buf.write(encodeOct(opts.devmajor || 0, 6), 329)231  buf.write(encodeOct(opts.devminor || 0, 6), 337)232 233  if (prefix) buf.write(prefix, 345)234 235  buf.write(encodeOct(cksum(buf), 6), 148)236 237  return buf238}239 240exports.decode = function (buf, filenameEncoding, allowUnknownFormat) {241  var typeflag = buf[156] === 0 ? 0 : buf[156] - ZERO_OFFSET242 243  var name = decodeStr(buf, 0, 100, filenameEncoding)244  var mode = decodeOct(buf, 100, 8)245  var uid = decodeOct(buf, 108, 8)246  var gid = decodeOct(buf, 116, 8)247  var size = decodeOct(buf, 124, 12)248  var mtime = decodeOct(buf, 136, 12)249  var type = toType(typeflag)250  var linkname = buf[157] === 0 ? null : decodeStr(buf, 157, 100, filenameEncoding)251  var uname = decodeStr(buf, 265, 32)252  var gname = decodeStr(buf, 297, 32)253  var devmajor = decodeOct(buf, 329, 8)254  var devminor = decodeOct(buf, 337, 8)255 256  var c = cksum(buf)257 258  // checksum is still initial value if header was null.259  if (c === 8 * 32) return null260 261  // valid checksum262  if (c !== decodeOct(buf, 148, 8)) throw new Error('Invalid tar header. Maybe the tar is corrupted or it needs to be gunzipped?')263 264  if (USTAR_MAGIC.compare(buf, MAGIC_OFFSET, MAGIC_OFFSET + 6) === 0) {265    // ustar (posix) format.266    // prepend prefix, if present.267    if (buf[345]) name = decodeStr(buf, 345, 155, filenameEncoding) + '/' + name268  } else if (GNU_MAGIC.compare(buf, MAGIC_OFFSET, MAGIC_OFFSET + 6) === 0 &&269             GNU_VER.compare(buf, VERSION_OFFSET, VERSION_OFFSET + 2) === 0) {270    // 'gnu'/'oldgnu' format. Similar to ustar, but has support for incremental and271    // multi-volume tarballs.272  } else {273    if (!allowUnknownFormat) {274      throw new Error('Invalid tar header: unknown format.')275    }276  }277 278  // to support old tar versions that use trailing / to indicate dirs279  if (typeflag === 0 && name && name[name.length - 1] === '/') typeflag = 5280 281  return {282    name,283    mode,284    uid,285    gid,286    size,287    mtime: new Date(1000 * mtime),288    type,289    linkname,290    uname,291    gname,292    devmajor,293    devminor294  }295}296 
basant307/AI_Governance_Project · CoolFace