bower / decompress-zip

Module that decompresses zip files
MIT License
102 stars 76 forks source link

Fix shift amounts in structures.js. #35

Closed xiemaisi closed 9 years ago

xiemaisi commented 10 years ago

As per the ECMA standard (Section 11.7.2), only the least significant five bits of the right operand of a shift operation are taken into account.

sindresorhus commented 10 years ago

I have no idea how this works, but I would happy to merge if you could get someone else to look this over ;)

sindresorhus commented 9 years ago

ping @xiemaisi

sindresorhus commented 9 years ago

@sheerun Any thoughts about this?

sheerun commented 9 years ago

According to unzip source external flags are fetched with:

xattr = (unsigned)((G.crec.external_file_attributes >> 16) & 0xFFFF);

and then processed with:

switch ((unsigned)(xattr & UNX_IFMT)) {
    case (unsigned)UNX_IFDIR:   attribs[0] = 'd';  break;
    case (unsigned)UNX_IFREG:   attribs[0] = '-';  break;
    case (unsigned)UNX_IFLNK:   attribs[0] = 'l';  break;
    case (unsigned)UNX_IFBLK:   attribs[0] = 'b';  break;
    case (unsigned)UNX_IFCHR:   attribs[0] = 'c';  break;
    case (unsigned)UNX_IFIFO:   attribs[0] = 'p';  break;
    case (unsigned)UNX_IFSOCK:  attribs[0] = 's';  break;
    default:          attribs[0] = '?';  break;
}

where UNX_IFMT is 0170000 and e.g. UNX_IFDIR is 0040000.

This gives shift of 28 and mask of 0x0F for external flags. The mode is xattr with 0xFFF mask.

Plus even then ECMA says that shift by 60 is equal to shift by 28 (you can shift max by 31).

It all means it's totally correct, yet not really changing anything commit.

sindresorhus commented 9 years ago

@sheerun Ok, leaving it up to you to decide on merging or closing this PR.