headers.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. var ZEROS = '0000000000000000000'
  2. var SEVENS = '7777777777777777777'
  3. var ZERO_OFFSET = '0'.charCodeAt(0)
  4. var USTAR = 'ustar\x0000'
  5. var MASK = parseInt('7777', 8)
  6. var clamp = function (index, len, defaultValue) {
  7. if (typeof index !== 'number') return defaultValue
  8. index = ~~index // Coerce to integer.
  9. if (index >= len) return len
  10. if (index >= 0) return index
  11. index += len
  12. if (index >= 0) return index
  13. return 0
  14. }
  15. var toType = function (flag) {
  16. switch (flag) {
  17. case 0:
  18. return 'file'
  19. case 1:
  20. return 'link'
  21. case 2:
  22. return 'symlink'
  23. case 3:
  24. return 'character-device'
  25. case 4:
  26. return 'block-device'
  27. case 5:
  28. return 'directory'
  29. case 6:
  30. return 'fifo'
  31. case 7:
  32. return 'contiguous-file'
  33. case 72:
  34. return 'pax-header'
  35. case 55:
  36. return 'pax-global-header'
  37. case 27:
  38. return 'gnu-long-link-path'
  39. case 28:
  40. case 30:
  41. return 'gnu-long-path'
  42. }
  43. return null
  44. }
  45. var toTypeflag = function (flag) {
  46. switch (flag) {
  47. case 'file':
  48. return 0
  49. case 'link':
  50. return 1
  51. case 'symlink':
  52. return 2
  53. case 'character-device':
  54. return 3
  55. case 'block-device':
  56. return 4
  57. case 'directory':
  58. return 5
  59. case 'fifo':
  60. return 6
  61. case 'contiguous-file':
  62. return 7
  63. case 'pax-header':
  64. return 72
  65. }
  66. return 0
  67. }
  68. var alloc = function (size) {
  69. var buf = new Buffer(size)
  70. buf.fill(0)
  71. return buf
  72. }
  73. var indexOf = function (block, num, offset, end) {
  74. for (; offset < end; offset++) {
  75. if (block[offset] === num) return offset
  76. }
  77. return end
  78. }
  79. var cksum = function (block) {
  80. var sum = 8 * 32
  81. for (var i = 0; i < 148; i++) sum += block[i]
  82. for (var j = 156; j < 512; j++) sum += block[j]
  83. return sum
  84. }
  85. var encodeOct = function (val, n) {
  86. val = val.toString(8)
  87. if (val.length > n) return SEVENS.slice(0, n) + ' '
  88. else return ZEROS.slice(0, n - val.length) + val + ' '
  89. }
  90. /* Copied from the node-tar repo and modified to meet
  91. * tar-stream coding standard.
  92. *
  93. * Source: https://github.com/npm/node-tar/blob/51b6627a1f357d2eb433e7378e5f05e83b7aa6cd/lib/header.js#L349
  94. */
  95. function parse256 (buf) {
  96. // first byte MUST be either 80 or FF
  97. // 80 for positive, FF for 2's comp
  98. var positive
  99. if (buf[0] === 0x80) positive = true
  100. else if (buf[0] === 0xFF) positive = false
  101. else return null
  102. // build up a base-256 tuple from the least sig to the highest
  103. var zero = false
  104. var tuple = []
  105. for (var i = buf.length - 1; i > 0; i--) {
  106. var byte = buf[i]
  107. if (positive) tuple.push(byte)
  108. else if (zero && byte === 0) tuple.push(0)
  109. else if (zero) {
  110. zero = false
  111. tuple.push(0x100 - byte)
  112. } else tuple.push(0xFF - byte)
  113. }
  114. var sum = 0
  115. var l = tuple.length
  116. for (i = 0; i < l; i++) {
  117. sum += tuple[i] * Math.pow(256, i)
  118. }
  119. return positive ? sum : -1 * sum
  120. }
  121. var decodeOct = function (val, offset, length) {
  122. val = val.slice(offset, offset + length)
  123. offset = 0
  124. // If prefixed with 0x80 then parse as a base-256 integer
  125. if (val[offset] & 0x80) {
  126. return parse256(val)
  127. } else {
  128. // Older versions of tar can prefix with spaces
  129. while (offset < val.length && val[offset] === 32) offset++
  130. var end = clamp(indexOf(val, 32, offset, val.length), val.length, val.length)
  131. while (offset < end && val[offset] === 0) offset++
  132. if (end === offset) return 0
  133. return parseInt(val.slice(offset, end).toString(), 8)
  134. }
  135. }
  136. var decodeStr = function (val, offset, length) {
  137. return val.slice(offset, indexOf(val, 0, offset, offset + length)).toString()
  138. }
  139. var addLength = function (str) {
  140. var len = Buffer.byteLength(str)
  141. var digits = Math.floor(Math.log(len) / Math.log(10)) + 1
  142. if (len + digits >= Math.pow(10, digits)) digits++
  143. return (len + digits) + str
  144. }
  145. exports.decodeLongPath = function (buf) {
  146. return decodeStr(buf, 0, buf.length)
  147. }
  148. exports.encodePax = function (opts) { // TODO: encode more stuff in pax
  149. var result = ''
  150. if (opts.name) result += addLength(' path=' + opts.name + '\n')
  151. if (opts.linkname) result += addLength(' linkpath=' + opts.linkname + '\n')
  152. var pax = opts.pax
  153. if (pax) {
  154. for (var key in pax) {
  155. result += addLength(' ' + key + '=' + pax[key] + '\n')
  156. }
  157. }
  158. return new Buffer(result)
  159. }
  160. exports.decodePax = function (buf) {
  161. var result = {}
  162. while (buf.length) {
  163. var i = 0
  164. while (i < buf.length && buf[i] !== 32) i++
  165. var len = parseInt(buf.slice(0, i).toString(), 10)
  166. if (!len) return result
  167. var b = buf.slice(i + 1, len - 1).toString()
  168. var keyIndex = b.indexOf('=')
  169. if (keyIndex === -1) return result
  170. result[b.slice(0, keyIndex)] = b.slice(keyIndex + 1)
  171. buf = buf.slice(len)
  172. }
  173. return result
  174. }
  175. exports.encode = function (opts) {
  176. var buf = alloc(512)
  177. var name = opts.name
  178. var prefix = ''
  179. if (opts.typeflag === 5 && name[name.length - 1] !== '/') name += '/'
  180. if (Buffer.byteLength(name) !== name.length) return null // utf-8
  181. while (Buffer.byteLength(name) > 100) {
  182. var i = name.indexOf('/')
  183. if (i === -1) return null
  184. prefix += prefix ? '/' + name.slice(0, i) : name.slice(0, i)
  185. name = name.slice(i + 1)
  186. }
  187. if (Buffer.byteLength(name) > 100 || Buffer.byteLength(prefix) > 155) return null
  188. if (opts.linkname && Buffer.byteLength(opts.linkname) > 100) return null
  189. buf.write(name)
  190. buf.write(encodeOct(opts.mode & MASK, 6), 100)
  191. buf.write(encodeOct(opts.uid, 6), 108)
  192. buf.write(encodeOct(opts.gid, 6), 116)
  193. buf.write(encodeOct(opts.size, 11), 124)
  194. buf.write(encodeOct((opts.mtime.getTime() / 1000) | 0, 11), 136)
  195. buf[156] = ZERO_OFFSET + toTypeflag(opts.type)
  196. if (opts.linkname) buf.write(opts.linkname, 157)
  197. buf.write(USTAR, 257)
  198. if (opts.uname) buf.write(opts.uname, 265)
  199. if (opts.gname) buf.write(opts.gname, 297)
  200. buf.write(encodeOct(opts.devmajor || 0, 6), 329)
  201. buf.write(encodeOct(opts.devminor || 0, 6), 337)
  202. if (prefix) buf.write(prefix, 345)
  203. buf.write(encodeOct(cksum(buf), 6), 148)
  204. return buf
  205. }
  206. exports.decode = function (buf) {
  207. var typeflag = buf[156] === 0 ? 0 : buf[156] - ZERO_OFFSET
  208. var name = decodeStr(buf, 0, 100)
  209. var mode = decodeOct(buf, 100, 8)
  210. var uid = decodeOct(buf, 108, 8)
  211. var gid = decodeOct(buf, 116, 8)
  212. var size = decodeOct(buf, 124, 12)
  213. var mtime = decodeOct(buf, 136, 12)
  214. var type = toType(typeflag)
  215. var linkname = buf[157] === 0 ? null : decodeStr(buf, 157, 100)
  216. var uname = decodeStr(buf, 265, 32)
  217. var gname = decodeStr(buf, 297, 32)
  218. var devmajor = decodeOct(buf, 329, 8)
  219. var devminor = decodeOct(buf, 337, 8)
  220. if (buf[345]) name = decodeStr(buf, 345, 155) + '/' + name
  221. // to support old tar versions that use trailing / to indicate dirs
  222. if (typeflag === 0 && name && name[name.length - 1] === '/') typeflag = 5
  223. var c = cksum(buf)
  224. // checksum is still initial value if header was null.
  225. if (c === 8 * 32) return null
  226. // valid checksum
  227. if (c !== decodeOct(buf, 148, 8)) throw new Error('Invalid tar header. Maybe the tar is corrupted or it needs to be gunzipped?')
  228. return {
  229. name: name,
  230. mode: mode,
  231. uid: uid,
  232. gid: gid,
  233. size: size,
  234. mtime: new Date(1000 * mtime),
  235. type: type,
  236. linkname: linkname,
  237. uname: uname,
  238. gname: gname,
  239. devmajor: devmajor,
  240. devminor: devminor
  241. }
  242. }