int plf_get_payload_uncompressed(int fileIdx, int sectIdx, void** buffer, u32* buffer_size) { s_plf_section_entry* curEntry; void* tmpBuffer; PLF_VERIFY_IDX(fileIdx); curEntry = plf_int_get_section(fileIdx, sectIdx); if (!curEntry || buffer == 0 || buffer_size == 0) return PLF_E_PARAM; /* Allocate a temp buffer */ tmpBuffer = malloc(curEntry->hdr.dwSectionSize); if (tmpBuffer == 0) return PLF_E_MEM; *buffer_size = 0; *buffer = 0; /* Read raw data */ plf_get_payload_raw(fileIdx, sectIdx, tmpBuffer, 0, curEntry->hdr.dwSectionSize); if (curEntry->hdr.dwUncomprSize == 0) { *buffer = tmpBuffer; *buffer_size = curEntry->hdr.dwSectionSize; } else { void* uncomprBuffer; int gz_ret; /* Allocate the uncompressed buffer */ uncomprBuffer = malloc(curEntry->hdr.dwUncomprSize); if (uncomprBuffer == 0) { free(tmpBuffer); return PLF_E_MEM; } *buffer_size = curEntry->hdr.dwUncomprSize; /* Decompress */ gz_ret = gz_uncompress(uncomprBuffer, buffer_size, tmpBuffer, curEntry->hdr.dwSectionSize); if (gz_ret < 0) { *buffer_size = 0; free(uncomprBuffer); free(tmpBuffer); return PLF_E_STREAM; } free(tmpBuffer); *buffer = uncomprBuffer; } return 0; }
void FileInfo(const char* filename) { int verify_result; int installerIdx, i, num_entries; int fileIdx, fileIdxInstaller; s_plf_file *fileHdrInstaller; s_plf_section* installerSection = 0; void* buffer; fileIdx = plf_open_file(filename); DumpPLF(fileIdx); if (fileIdx < 0) { printf("!!! plf_open_file(%s) failed: %d\n", filename, fileIdx); return; } /* Verify the file */ verify_result = plf_verify(fileIdx); if (verify_result < 0) { printf("!!! plf_verify(%s) failed: %d\n", filename, verify_result); } /* Find the installer section */ num_entries = plf_get_num_sections(fileIdx); for(i = 0; i < num_entries; ++i) { installerSection = plf_get_section_header(fileIdx, i); if (installerSection != 0 && installerSection->dwSectionType == 0x0c) break; } if (i >= num_entries || installerSection == 0) { printf("!!! unable to find installer section in %s\n", filename); goto FileInfo_exit_1; } /* Found the installer */ installerIdx = i; /* Now open the installer from RAM */ buffer = malloc(installerSection->dwSectionSize); if (!buffer) { printf("!!! unable to alloc %d bytes\n", installerSection->dwSectionSize); goto FileInfo_exit_1; } plf_get_payload_raw(fileIdx, installerIdx, buffer, 0, installerSection->dwSectionSize); fileIdxInstaller = plf_open_ram(buffer, installerSection->dwSectionSize); if (fileIdxInstaller < 0) { printf("!!! plf_open_ram for installer failed: %d\n", fileIdxInstaller); goto FileInfo_exit_2; } fileHdrInstaller = plf_get_file_header(fileIdxInstaller); printf("*** Installer found! Version: %d.%d.%d ***\n", fileHdrInstaller->dwVersionMajor, fileHdrInstaller->dwVersionMinor, fileHdrInstaller->dwVersionBugfix); FileInfo_exit_2: free(buffer); FileInfo_exit_1: plf_close(fileIdx); }
int plf_check_crc(int fileIdx, int sectIdx) { u32 crc_accum = 0; u32 crc_num_size = 0; s_plf_section_entry* section; s_plf_file_entry* fileEntry; PLF_VERIFY_IDX(fileIdx); section = plf_int_get_section(fileIdx, sectIdx); fileEntry = &plf_files[fileIdx]; if (section == 0) return PLF_E_PARAM; if (fileEntry->fildes == -1) { // Buffer case crc32_calc_buffer(&crc_accum, &crc_num_size, (((u8*) fileEntry->buffer) + section->offset), section->hdr.dwSectionSize); crc32_calc_dw(&crc_accum, &crc_num_size); } else { // File case.. we need to read the contents of the file u8* tmpBuf = (u8*) malloc(0x1000); if (tmpBuf == 0) return PLF_E_MEM; u32 bytes_remaining = section->hdr.dwSectionSize; while (bytes_remaining > 0) { u32 read_len; if (bytes_remaining > 0x1000) { read_len = 0x1000; } else { read_len = bytes_remaining; } u32 offset = section->hdr.dwSectionSize - bytes_remaining; u32 bytes_read = plf_get_payload_raw(fileIdx, sectIdx, tmpBuf, offset, read_len); if (bytes_read < 0) { free(tmpBuf); return bytes_read; } crc32_calc_buffer(&crc_accum, &crc_num_size, tmpBuf, read_len); bytes_remaining -= read_len; } // finish crc free(tmpBuf); crc32_calc_dw(&crc_accum, &crc_num_size); } if (crc_accum == section->hdr.dwCRC32) return 0; else return PLF_E_CRC; }