You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

headers.js 7.2KB

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