private static byte[] GetZipCentralDirectory(DataSource apk, ApkUtils.ZipSections apkSections) { long cdSizeBytes = apkSections.CentralDirectorySizeBytes; if (cdSizeBytes > int.MaxValue) { throw new ApkFormatException("ZIP Central Directory too large: " + cdSizeBytes); } long cdOffset = apkSections.CentralDirectoryOffset; return(apk.GetByteBuffer(cdOffset, cdSizeBytes)); }
private static List <CentralDirectoryRecord> ParseZipCentralDirectory(byte[] cd, ApkUtils.ZipSections apkSections) { long cdOffset = apkSections.CentralDirectoryOffset; int expectedCdRecordCount = apkSections.CentralDirectoryRecordCount; var cdRecords = new List <CentralDirectoryRecord>(expectedCdRecordCount); var entryNames = new HashSet <string>(); using (var ms = new DataSource(new MemoryStream(cd))) { for (var i = 0; i < expectedCdRecordCount; i++) { CentralDirectoryRecord cdRecord; try { cdRecord = CentralDirectoryRecord.GetRecord(ms); } catch (ZipFormatException e) { throw new ApkFormatException( "Malformed ZIP Central Directory record #" + (i + 1), e); } var entryName = cdRecord.Name; if (!entryNames.Add(entryName)) { throw new ApkFormatException( "Multiple ZIP entries with the same name: " + entryName); } cdRecords.Add(cdRecord); } if (ms.Remaining > 0) { throw new ApkFormatException( "Unused space at the end of ZIP Central Directory: " + ms.Remaining + " bytes starting at file offset " + (cdOffset + ms.Position)); } } return(cdRecords); }