summaryrefslogtreecommitdiff
path: root/deps/npm/node_modules/tar/old/parser.js
blob: 1582ee7577ea2f69071b0af0ddc1e090aae82869 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
module.exports = Parser
Parser.create = create
Parser.File = File

var tar = require("./tar")
  , Stream = require("stream").Stream
  , fs = require("fs")

function create (cb) {
  return new Parser(cb)
}

var s = 0
  , HEADER = s ++
  , BODY = s ++
  , PAD = s ++

function Parser (cb) {
  this.fields = tar.fields
  this.fieldSize = tar.fieldSize
  this.state = HEADER
  this.position = 0
  this.currentFile = null
  this._header = []
  this._headerPosition = 0
  this._bodyPosition = 0
  this.writable = true
  Stream.apply(this)
  if (cb) this.on("file", cb)
}

Parser.prototype = Object.create(Stream.prototype)

Parser.prototype.write = function (chunk) {
  switch (this.state) {
    case HEADER:
      // buffer up to 512 bytes in memory, and then
      // parse it, emit a "file" event, and stream the rest
      this._header.push(chunk)
      this._headerPosition += chunk.length
      if (this._headerPosition >= tar.headerSize) {
        return this._parseHeader()
      }
      return true

    case BODY:
      // stream it through until the end of the file is reached,
      // and then step over any \0 byte padding.
      var cl = chunk.length
        , bp = this._bodyPosition
        , np = cl + bp
        , s = this.currentFile.size
      if (np < s) {
        this._bodyPosition = np
        return this.currentFile.write(chunk)
      }
      var c = chunk.slice(0, (s - bp))
      this.currentFile.write(c)
      this._closeFile()
      return this.write(chunk.slice(s - bp))

    case PAD:
      for (var i = 0, l = chunk.length; i < l; i ++) {
        if (chunk[i] !== 0) {
          this.state = HEADER
          return this.write(chunk.slice(i))
        }
      }
  }
  return true
}

Parser.prototype.end = function (chunk) {
  if (chunk) this.write(chunk)
  if (this.currentFile) this._closeFile()
  this.emit("end")
  this.emit("close")
}

// at this point, we have at least 512 bytes of header chunks
Parser.prototype._parseHeader = function () {
  var hp = this._headerPosition
    , last = this._header.pop()
    , rem

  if (hp < 512) return this.emit("error", new Error(
    "Trying to parse header before finished"))

  if (hp > 512) {
    var ll = last.length
      , llIntend = 512 - hp + ll
    rem = last.slice(llIntend)
    last = last.slice(0, llIntend)
  }
  this._header.push(last)

  var fields = tar.fields
    , pos = 0
    , field = 0
    , fieldEnds = tar.fieldEnds
    , fieldSize = tar.fieldSize
    , set = {}
    , fpos = 0

  Object.keys(fieldSize).forEach(function (f) {
    set[ fields[f] ] = new Buffer(fieldSize[f])
  })

  this._header.forEach(function (chunk) {
    for (var i = 0, l = chunk.length; i < l; i ++, pos ++, fpos ++) {
      if (pos >= fieldEnds[field]) {
        field ++
        fpos = 0
      }
      // header is null-padded, so when the fields run out,
      // just finish.
      if (null === fields[field]) return
      set[fields[field]][fpos] = chunk[i]
    }
  })

  this._header.length = 0

  // type definitions here:
  // http://cdrecord.berlios.de/private/man/star/star.4.html
  var type = set.TYPE.toString()
    , file = this.currentFile = new File(set)
  if (type === "\0" ||
      type >= "0" && type <= "7") {
    this._addExtended(file)
    this.emit("file", file)
  } else if (type === "g") {
    this._global = this._global || {}
    readPax(this, file, this._global)
  } else if (type === "h" || type === "x" || type === "X") {
    this._extended = this._extended || {}
    readPax(this, file, this._extended)
  } else if (type === "K") {
    this._readLongField(file, "linkname")
  } else if (type === "L") {
    this._readLongField(file, "name")
  }

  this.state = BODY
  if (rem) return this.write(rem)
  return true
}

function readPax (self, file, obj) {
  var buf = ""
  file.on("data", function (c) {
    buf += c
    var lines = buf.split(/\r?\n/)
    buf = lines.pop()
    lines.forEach(function (line) {
      line = line.match(/^[0-9]+ ([^=]+)=(.*)/)
      if (!line) return
      obj[line[1]] = line[2]
    })
  })
}

Parser.prototype._readLongField = function (f, field) {
  var self = this
  this._longFields[field] = ""
  f.on("data", function (c) {
    self._longFields[field] += c
  })
}

Parser.prototype._addExtended = function (file) {
  var g = this._global || {}
    , e = this._extended || {}
  file.extended = {}
  ;[g, e].forEach(function (h) {
    Object.keys(h).forEach(function (k) {
      file.extended[k] = h[k]
      // handle known fields
      switch (k) {
        case "path": file.name = h[k]; break
        case "ctime": file.ctime = new Date(1000 * h[k]); break
        case "mtime": file.mtime = new Date(1000 * h[k]); break
        case "gid": file.gid = parseInt(h[k], 10); break
        case "uid": file.uid = parseInt(h[k], 10); break
        case "charset": file.charset = h[k]; break
        case "gname": file.group = h[k]; break
        case "uname": file.user = h[k]; break
        case "linkpath": file.linkname = h[k]; break
        case "size": file.size = parseInt(h[k], 10); break
        case "SCHILY.devmajor": file.dev.major = parseInt(h[k], 10); break
        case "SCHILY.devminor": file.dev.minor = parseInt(h[k], 10); break
      }
    })
  })
  var lf = this._longFields || {}
  Object.keys(lf).forEach(function (f) {
    file[f] = lf[f]
  })
  this._extended = {}
  this._longFields = {}
}

Parser.prototype._closeFile = function () {
  if (!this.currentFile) return this.emit("error", new Error(
    "Trying to close without current file"))

  this._headerPosition = this._bodyPosition = 0
  this.currentFile.end()
  this.currentFile = null
  this.state = PAD
}


// file stuff

function strF (f) {
  return f.toString("ascii").split("\0").shift() || ""
}

function parse256 (buf) {
  // first byte MUST be either 80 or FF
  // 80 for positive, FF for 2's comp
  var positive
  if (buf[0] === 0x80) positive = true
  else if (buf[0] === 0xFF) positive = false
  else return 0

  if (!positive) {
    // this is rare enough that the string slowness
    // is not a big deal.  You need *very* old files
    // to ever hit this path.
    var s = ""
    for (var i = 1, l = buf.length; i < l; i ++) {
      var byte = buf[i].toString(2)
      if (byte.length < 8) {
        byte = new Array(byte.length - 8 + 1).join("1") + byte
      }
      s += byte
    }
    var ht = s.match(/^([01]*)(10*)$/)
      , head = ht[1]
      , tail = ht[2]
    head = head.split("1").join("2")
               .split("0").join("1")
               .split("2").join("0")
    return -1 * parseInt(head + tail, 2)
  }

  var sum = 0
  for (var i = 1, l = buf.length, p = l - 1; i < l; i ++, p--) {
    sum += buf[i] * Math.pow(256, p)
  }
  return sum
}

function nF (f) {
  if (f[0] & 128 === 128) {
    return parse256(f)
  }
  return parseInt(f.toString("ascii").replace(/\0+/g, "").trim(), 8) || 0
}

function bufferMatch (a, b) {
  if (a.length != b.length) return false
  for (var i = 0, l = a.length; i < l; i ++) {
    if (a[i] !== b[i]) return false
  }
  return true
}

function File (fields) {
  this._raw = fields
  this.name = strF(fields.NAME)
  this.mode = nF(fields.MODE)
  this.uid = nF(fields.UID)
  this.gid = nF(fields.GID)
  this.size = nF(fields.SIZE)
  this.mtime = new Date(nF(fields.MTIME) * 1000)
  this.cksum = nF(fields.CKSUM)
  this.type = strF(fields.TYPE)
  this.linkname = strF(fields.LINKNAME)

  this.ustar = bufferMatch(fields.USTAR, tar.ustar)

  if (this.ustar) {
    this.ustarVersion = nF(fields.USTARVER)
    this.user = strF(fields.UNAME)
    this.group = strF(fields.GNAME)
    this.dev = { major: nF(fields.DEVMAJ)
               , minor: nF(fields.DEVMIN) }
    this.prefix = strF(fields.PREFIX)
    if (this.prefix) {
      this.name = this.prefix + "/" + this.name
    }
  }

  this.writable = true
  this.readable = true
  Stream.apply(this)
}

File.prototype = Object.create(Stream.prototype)

File.types = { File:            "0"
             , HardLink:        "1"
             , SymbolicLink:    "2"
             , CharacterDevice: "3"
             , BlockDevice:     "4"
             , Directory:       "5"
             , FIFO:            "6"
             , ContiguousFile:  "7" }

Object.keys(File.types).forEach(function (t) {
  File.prototype["is"+t] = function () {
    return File.types[t] === this.type
  }
  File.types[ File.types[t] ] = File.types[t]
})

// contiguous files are treated as regular files for most purposes.
File.prototype.isFile = function () {
  return this.type === "0" && this.name.slice(-1) !== "/"
      || this.type === "7"
}

File.prototype.isDirectory = function () {
  return this.type === "5"
      || this.type === "0" && this.name.slice(-1) === "/"
}

File.prototype.write = function (c) {
  this.emit("data", c)
  return true
}

File.prototype.end = function (c) {
  if (c) this.write(c)
  this.emit("end")
  this.emit("close")
}

File.prototype.pause = function () { this.emit("pause") }

File.prototype.resume = function () { this.emit("resume") }