var toBuffer=require("to-buffer"),alloc=require("buffer-alloc"),ZEROS="0000000000000000000",SEVENS="7777777777777777777",ZERO_OFFSET="0".charCodeAt(0),USTAR="ustar\x0000",MASK=parseInt("7777",8),clamp=function(e,r,t){return"number"!=typeof e?t:(e=~~e)>=r?r:e>=0?e:(e+=r)>=0?e:0},toType=function(e){switch(e){case 0:return"file";case 1:return"link";case 2:return"symlink";case 3:return"character-device";case 4:return"block-device";case 5:return"directory";case 6:return"fifo";case 7:return"contiguous-file";case 72:return"pax-header";case 55:return"pax-global-header";case 27:return"gnu-long-link-path";case 28:case 30:return"gnu-long-path"}return null},toTypeflag=function(e){switch(e){case"file":return 0;case"link":return 1;case"symlink":return 2;case"character-device":return 3;case"block-device":return 4;case"directory":return 5;case"fifo":return 6;case"contiguous-file":return 7;case"pax-header":return 72}return 0},indexOf=function(e,r,t,n){for(;t<n;t++)if(e[t]===r)return t;return n},cksum=function(e){for(var r=256,t=0;t<148;t++)r+=e[t];for(var n=156;n<512;n++)r+=e[n];return r},encodeOct=function(e,r){return(e=e.toString(8)).length>r?SEVENS.slice(0,r)+" ":ZEROS.slice(0,r-e.length)+e+" "};function parse256(e){var r;if(128===e[0])r=!0;else{if(255!==e[0])return null;r=!1}for(var t=!1,n=[],c=e.length-1;c>0;c--){var a=e[c];r?n.push(a):t&&0===a?n.push(0):t?(t=!1,n.push(256-a)):n.push(255-a)}var i=0,o=n.length;for(c=0;c<o;c++)i+=n[c]*Math.pow(256,c);return r?i:-1*i}var decodeOct=function(e,r,t){if(128&(e=e.slice(r,r+t))[r=0])return parse256(e);for(;r<e.length&&32===e[r];)r++;for(var n=clamp(indexOf(e,32,r,e.length),e.length,e.length);r<n&&0===e[r];)r++;return n===r?0:parseInt(e.slice(r,n).toString(),8)},decodeStr=function(e,r,t,n){return e.slice(r,indexOf(e,0,r,r+t)).toString(n)},addLength=function(e){var r=Buffer.byteLength(e),t=Math.floor(Math.log(r)/Math.log(10))+1;return r+t>=Math.pow(10,t)&&t++,r+t+e};exports.decodeLongPath=function(e,r){return decodeStr(e,0,e.length,r)},exports.encodePax=function(e){var r="";e.name&&(r+=addLength(" path="+e.name+"\n")),e.linkname&&(r+=addLength(" linkpath="+e.linkname+"\n"));var t=e.pax;if(t)for(var n in t)r+=addLength(" "+n+"="+t[n]+"\n");return toBuffer(r)},exports.decodePax=function(e){for(var r={};e.length;){for(var t=0;t<e.length&&32!==e[t];)t++;var n=parseInt(e.slice(0,t).toString(),10);if(!n)return r;var c=e.slice(t+1,n-1).toString(),a=c.indexOf("=");if(-1===a)return r;r[c.slice(0,a)]=c.slice(a+1),e=e.slice(n)}return r},exports.encode=function(e){var r=alloc(512),t=e.name,n="";if(5===e.typeflag&&"/"!==t[t.length-1]&&(t+="/"),Buffer.byteLength(t)!==t.length)return null;for(;Buffer.byteLength(t)>100;){var c=t.indexOf("/");if(-1===c)return null;n+=n?"/"+t.slice(0,c):t.slice(0,c),t=t.slice(c+1)}return Buffer.byteLength(t)>100||Buffer.byteLength(n)>155?null:e.linkname&&Buffer.byteLength(e.linkname)>100?null:(r.write(t),r.write(encodeOct(e.mode&MASK,6),100),r.write(encodeOct(e.uid,6),108),r.write(encodeOct(e.gid,6),116),r.write(encodeOct(e.size,11),124),r.write(encodeOct(e.mtime.getTime()/1e3|0,11),136),r[156]=ZERO_OFFSET+toTypeflag(e.type),e.linkname&&r.write(e.linkname,157),r.write(USTAR,257),e.uname&&r.write(e.uname,265),e.gname&&r.write(e.gname,297),r.write(encodeOct(e.devmajor||0,6),329),r.write(encodeOct(e.devminor||0,6),337),n&&r.write(n,345),r.write(encodeOct(cksum(r),6),148),r)},exports.decode=function(e,r){var t=0===e[156]?0:e[156]-ZERO_OFFSET,n=decodeStr(e,0,100,r),c=decodeOct(e,100,8),a=decodeOct(e,108,8),i=decodeOct(e,116,8),o=decodeOct(e,124,12),u=decodeOct(e,136,12),d=toType(t),l=0===e[157]?null:decodeStr(e,157,100,r),f=decodeStr(e,265,32),s=decodeStr(e,297,32),g=decodeOct(e,329,8),h=decodeOct(e,337,8);e[345]&&(n=decodeStr(e,345,155,r)+"/"+n),0===t&&n&&"/"===n[n.length-1]&&(t=5);var p=cksum(e);if(256===p)return null;if(p!==decodeOct(e,148,8))throw new Error("Invalid tar header. Maybe the tar is corrupted or it needs to be gunzipped?");return{name:n,mode:c,uid:a,gid:i,size:o,mtime:new Date(1e3*u),type:d,linkname:l,uname:f,gname:s,devmajor:g,devminor:h}};