fa-toolkit
Version:
a devtool for fast app
1 lines • 1.94 kB
JavaScript
;var CONSTANTS=require("./constants"),ZIP_EOCD_REC_MIN_SIZE=22,ZIP_FH_REC_SIG=67324752,ZIP_DC_REC_SIG=33639248,ZIP_EOCD_REC_SIG=101010256,EOCD_CENTRAL_DIR_OFFSET_FIELD_OFFSET=16;function parserZip(e,t){var r,n,a={tag:!1,length:e.length,sections:{header:null,central:null,footer:null}};return a.sections.footer=readEOCD(e),a.sections.footer.tag&&(a.sections.central=readCD(e,a.sections.footer.previous,a.sections.footer.startIndex-a.sections.footer.previous),a.sections.central.tag&&(r=a.sections.central.previous,n=a.sections.central.startIndex-a.sections.central.previous,t&&(a.sections.sign=readSignBlock(e,r),n=a.sections.sign.previous),a.sections.header=readFH(e,r,n),a.sections.header.tag&&(a.tag=!0))),a}function readEOCD(e){var t={tag:!1};if(!e||e.length<ZIP_EOCD_REC_MIN_SIZE)return t;for(var r=e.length-ZIP_EOCD_REC_MIN_SIZE;0<=r;r--)if(ZIP_EOCD_REC_SIG===e.readInt32LE(r)){t.tag=!0,t.startIndex=r,t.len=e.length-r,t.previous=e.readInt32LE(r+EOCD_CENTRAL_DIR_OFFSET_FIELD_OFFSET);break}return t}function readCD(e,t,r){var n={tag:!1};return!e||e.length<t||ZIP_DC_REC_SIG===e.readInt32LE(t)&&(n.tag=!0,n.startIndex=t,n.len=r,n.previous=e.readInt32LE(t+42)),n}function readFH(e,t,r){var n={tag:!1};return!e||e.length<t||ZIP_FH_REC_SIG===e.readInt32LE(t)&&(n.tag=!0,n.startIndex=t,n.len=r,n.previous=-1),n}function readSignBlock(e,t){var r={tag:!1};if(!e||e.length<t)return r;var n=CONSTANTS.isApkSign()?CONSTANTS.SIGN_MAGIC_APK:CONSTANTS.SIGN_MAGIC,t=e.indexOf(n);return 0<t&&(n=e.readInt32LE(t-8),e.readInt32LE(e=t+16-n-8)===n&&(r.tag=!0,r.len=n+8,r.startIndex=t+16,r.previous=e)),r}function checkZipBuffer(e){return!e||e.length<=4?(console.log("### Sign Failed, cannot open file!"),!1):ZIP_FH_REC_SIG===e.readInt32LE(0)||(console.log("### Sign Failed, zip file error!"),!1)}module.exports={parserZip:parserZip,checkZipBuffer:checkZipBuffer,EOCD_CENTRAL_DIR_OFFSET_FIELD_OFFSET:EOCD_CENTRAL_DIR_OFFSET_FIELD_OFFSET};