1 // An Entry consisting of:
3 // "%d %s=%s\n", <length>, <keyword>, <value>
5 // The length is a decimal number, and includes itself and the \n
6 // \0 does not terminate anything. Only the length terminates the string.
7 // Numeric values are decimal strings.
9 module.exports = ExtendedHeader
11 var Entry = require("./entry.js")
12 , inherits = require("inherits")
13 , tar = require("../tar.js")
14 , numeric = tar.numeric
15 , keyTrans = { "SCHILY.dev": "dev"
17 , "SCHILY.nlink": "nlink" }
19 function ExtendedHeader () {
20 Entry.apply(this, arguments)
21 this.on("data", this._parse)
33 inherits(ExtendedHeader, Entry)
34 ExtendedHeader.prototype._parse = parse
37 , states = ExtendedHeader.states = {}
38 , SIZE = states.SIZE = s++
39 , KEY = states.KEY = s++
40 , VAL = states.VAL = s++
41 , ERR = states.ERR = s++
43 Object.keys(states).forEach(function (s) {
44 states[states[s]] = states[s]
49 // char code values for comparison
50 var _0 = "0".charCodeAt(0)
51 , _9 = "9".charCodeAt(0)
52 , point = ".".charCodeAt(0)
53 , a = "a".charCodeAt(0)
54 , Z = "Z".charCodeAt(0)
55 , a = "a".charCodeAt(0)
56 , z = "z".charCodeAt(0)
57 , space = " ".charCodeAt(0)
58 , eq = "=".charCodeAt(0)
59 , cr = "\n".charCodeAt(0)
62 if (this._state === ERR) return
64 for ( var i = 0, l = c.length
66 ; this._position++, this._fieldPos++, i++) {
67 // console.error("top of loop, size="+this._size)
71 if (this._size >= 0 && this._fieldPos > this._size) {
72 error(this, "field exceeds length="+this._size)
76 switch (this._state) {
80 // console.error("parsing size, b=%d, rest=%j", b, c.slice(i).toString())
83 // this._fieldPos = this._sizeBuf.length
84 this._size = parseInt(new Buffer(this._sizeBuf).toString(), 10)
85 this._sizeBuf.length = 0
88 if (b < _0 || b > _9) {
89 error(this, "expected [" + _0 + ".." + _9 + "], got " + b)
96 // can be any char except =, not > size.
99 this._key = new Buffer(this._keyBuf).toString()
100 if (keyTrans[this._key]) this._key = keyTrans[this._key]
101 this._keyBuf.length = 0
108 // field must end with cr
109 if (this._fieldPos === this._size - 1) {
110 // console.error("finished with "+this._key)
112 error(this, "expected \\n at end of field")
115 var val = new Buffer(this._valBuf).toString()
116 if (numeric[this._key]) {
117 val = parseFloat(val)
119 this.fields[this._key] = val
121 this._valBuf.length = 0
133 function error (me, msg) {
134 msg = "invalid header: " + msg
135 + "\nposition=" + me._position
136 + "\nfield position=" + me._fieldPos