"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/decompress-zip/lib/structures.js" (8 Mar 2017, 6885 Bytes) of archive /windows/misc/atom-windows.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Javascript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 'use strict';
    2 var binary = require('binary');
    3 
    4 var convertDateTime = function (dosDate, dosTime) {
    5     var year = ((dosDate >> 9) & 0x7F) + 1980;
    6     var month = (dosDate >> 5) & 0x0F;
    7     var day = dosDate & 0x1F;
    8 
    9     var hour = (dosTime >> 11);
   10     var minute = (dosTime >> 5) & 0x3F;
   11     var second = (dosTime & 0x1F) * 2;
   12 
   13     var result = new Date(year, month - 1, day, hour, minute, second, 0);
   14 
   15     return result;
   16 };
   17 
   18 var convertGeneralPurposeFlags = function (value) {
   19     var bits = [];
   20 
   21     for (var i = 0; i < 16; i++) {
   22         bits[i] = (value >> i) & 1;
   23     }
   24 
   25     return {
   26         encrypted: !!bits[0],
   27         compressionFlag1: !!bits[1],
   28         compressionFlag2: !!bits[2],
   29         useDataDescriptor: !!bits[3],
   30         enhancedDeflating: !!bits[4],
   31         compressedPatched: !!bits[5],
   32         strongEncryption: !!bits[6],
   33         utf8: !!bits[11],
   34         encryptedCD: !!bits[13]
   35     };
   36 };
   37 
   38 var parseExternalFileAttributes = function (externalAttributes, platform) {
   39     var types = {
   40         // In theory, any of these could be set. Realistically, though, it will
   41         // be regular, directory or symlink
   42         1: 'NamedPipe',
   43         2: 'Character',
   44         4: 'Directory',
   45         6: 'Block',
   46         8: 'File',
   47         10: 'SymbolicLink',
   48         12: 'Socket'
   49     };
   50 
   51     switch (platform) {
   52 
   53     case 3: // Unix
   54         return {
   55             platform: 'Unix',
   56             type: types[(externalAttributes >> 28) & 0x0F],
   57             mode: (externalAttributes >> 16) & 0xFFF
   58         };
   59 
   60     // case 0: // MSDOS
   61     default:
   62         if (platform !== 0) {
   63             console.warn('Possibly unsupported ZIP platform type, ' + platform);
   64         }
   65 
   66         var attribs = {
   67             A: (externalAttributes >> 5) & 0x01,
   68             D: (externalAttributes >> 4) & 0x01,
   69             V: (externalAttributes >> 3) & 0x01,
   70             S: (externalAttributes >> 2) & 0x01,
   71             H: (externalAttributes >> 1) & 0x01,
   72             R: externalAttributes & 0x01
   73         };
   74 
   75         // With no better guidance we'll make the default permissions ugo+r
   76         var mode = parseInt('0444', 8);
   77 
   78         if (attribs.D) {
   79             mode |= parseInt('0111', 8); // Set the execute bit
   80         }
   81 
   82         if (!attribs.R) {
   83             mode |= parseInt('0222', 8); // Set the write bit
   84         }
   85 
   86         mode &= ~process.umask();
   87 
   88         return {
   89             platform: 'DOS',
   90             type: attribs.D ? 'Directory' : 'File',
   91             mode: mode
   92         };
   93     }
   94 };
   95 
   96 var readEndRecord = function (buffer) {
   97     var data = binary.parse(buffer)
   98     .word32lu('signature')
   99     .word16lu('diskNumber')
  100     .word16lu('directoryStartDisk')
  101     .word16lu('directoryEntryCountDisk')
  102     .word16lu('directoryEntryCount')
  103     .word32lu('directorySize')
  104     .word32lu('directoryOffset')
  105     .word16lu('commentLength')
  106     .buffer('comment', 'commentLength')
  107     .vars;
  108 
  109     data.comment = data.comment.toString();
  110 
  111     return data;
  112 };
  113 
  114 var directorySort = function (a, b) {
  115     return a.relativeOffsetOfLocalHeader - b.relativeOffsetOfLocalHeader;
  116 };
  117 
  118 var readDirectory = function (buffer) {
  119     var directory = [];
  120     var current;
  121     var index = 0;
  122 
  123     while (index < buffer.length) {
  124         current = binary.parse(buffer.slice(index, index + 46))
  125         .word32lu('signature')
  126         .word8lu('creatorSpecVersion')
  127         .word8lu('creatorPlatform')
  128         .word8lu('requiredSpecVersion')
  129         .word8lu('requiredPlatform')
  130         .word16lu('generalPurposeBitFlag')
  131         .word16lu('compressionMethod')
  132         .word16lu('lastModFileTime')
  133         .word16lu('lastModFileDate')
  134         .word32lu('crc32')
  135         .word32lu('compressedSize')
  136         .word32lu('uncompressedSize')
  137         .word16lu('fileNameLength')
  138         .word16lu('extraFieldLength')
  139         .word16lu('fileCommentLength')
  140         .word16lu('diskNumberStart')
  141         .word16lu('internalFileAttributes')
  142         .word32lu('externalFileAttributes')
  143         .word32lu('relativeOffsetOfLocalHeader')
  144         .vars;
  145 
  146         index += 46;
  147 
  148         current.generalPurposeFlags = convertGeneralPurposeFlags(current.generalPurposeBitFlag);
  149         current.fileAttributes = parseExternalFileAttributes(current.externalFileAttributes, current.creatorPlatform);
  150 
  151         current.modifiedTime = convertDateTime(current.lastModFileDate, current.lastModFileTime);
  152         current.fileName = current.extraField = current.fileComment = '';
  153         current.headerLength = 46 + current.fileNameLength + current.extraFieldLength + current.fileCommentLength;
  154 
  155         if (current.fileNameLength > 0) {
  156             current.fileName = buffer.slice(index, index + current.fileNameLength).toString();
  157             index += current.fileNameLength;
  158         }
  159 
  160         if (current.extraFieldLength > 0) {
  161             current.extraField = buffer.slice(index, index + current.extraFieldLength).toString();
  162             index += current.extraFieldLength;
  163         }
  164 
  165         if (current.fileCommentLength > 0) {
  166             current.fileComment = buffer.slice(index, index + current.fileCommentLength).toString();
  167             index += current.fileCommentLength;
  168         }
  169 
  170         if (current.fileAttributes.type !== 'Directory' && current.fileName.substr(-1) === '/') {
  171             // TODO: check that this is a reasonable check
  172             current.fileAttributes.type = 'Directory';
  173         }
  174 
  175         directory.push(current);
  176     }
  177 
  178     directory.sort(directorySort);
  179 
  180     return directory;
  181 };
  182 
  183 var readFileEntry = function (buffer) {
  184     var index = 0;
  185 
  186     var fileEntry = binary.parse(buffer.slice(index, 30))
  187     .word32lu('signature')
  188     .word16lu('versionNeededToExtract')
  189     .word16lu('generalPurposeBitFlag')
  190     .word16lu('compressionMethod')
  191     .word16lu('lastModFileTime')
  192     .word16lu('lastModFileDate')
  193     .word32lu('crc32')
  194     .word32lu('compressedSize')
  195     .word32lu('uncompressedSize')
  196     .word16lu('fileNameLength')
  197     .word16lu('extraFieldLength')
  198     .vars;
  199 
  200     index += 30;
  201 
  202     fileEntry.fileName = fileEntry.extraField = '';
  203 
  204     fileEntry.entryLength = 30 + fileEntry.fileNameLength + fileEntry.extraFieldLength;
  205 
  206     if (fileEntry.entryLength > structures.maxFileEntrySize) {
  207         throw new Error('File entry unexpectedly large: ' + fileEntry.entryLength + ' (max: ' + structures.maxFileEntrySize + ')');
  208     }
  209 
  210     if (fileEntry.fileNameLength > 0) {
  211         fileEntry.fileName = buffer.slice(index, index + fileEntry.fileNameLength).toString();
  212         index += fileEntry.fileNameLength;
  213     }
  214 
  215     if (fileEntry.extraFieldLength > 0) {
  216         fileEntry.extraField = buffer.slice(index, index + fileEntry.extraFieldLength).toString();
  217         index += fileEntry.extraFieldLength;
  218     }
  219 
  220     return fileEntry;
  221 };
  222 
  223 var structures = module.exports = {
  224     readEndRecord: readEndRecord,
  225     readDirectory: readDirectory,
  226     readFileEntry: readFileEntry,
  227     maxFileEntrySize: 4096
  228 };