int LZ4Reader::parseInt() { if (currentOffset >= uncompressedBufferLen) { uncompressedBufferLen = uncompressBuffer(); currentOffset = 0; } int n = 0; if (currentOffset + 3 < uncompressedBufferLen) { //Parse it normally n = Utils::decode_int(uncompressedBuffer, currentOffset); currentOffset += 4; } else { //Need to parse the next buffer int numBytes = 3; for (; currentOffset < uncompressedBufferLen; numBytes--) { n += (uncompressedBuffer[currentOffset++] & 0xFF) << (numBytes * 8); } //Get the new buffer uncompressedBufferLen = uncompressBuffer(); currentOffset = 0; //Read the remaining bytes for (; numBytes >= 0; numBytes--) { n += (uncompressedBuffer[currentOffset++] & 0xFF) << (numBytes * 8); } } return n; }
char LZ4Reader::parseByte() { if (currentOffset >= uncompressedBufferLen) { uncompressedBufferLen = uncompressBuffer(); currentOffset = 0; } return uncompressedBuffer[currentOffset++]; }
//========================================================================== bool test_uncompress(EventMsgView const* eview, std::vector<unsigned char> &dest) { unsigned long origsize = eview->origDataSize(); bool success = false; if(origsize != 0 && origsize != 78) { // compressed success = uncompressBuffer((unsigned char*)eview->eventData(), eview->eventLength(), dest, origsize); } else { // uncompressed anyway success = true; } return success; }
int archiveCheckFilesForUnsafeFself() { if (!uf) return -1; FileListEntry *archive_entry = archive_list.head; int i; for (i = 0; i < archive_list.length; i++) { // Set pos unzGoToFilePos64(uf, (unz64_file_pos *)&archive_entry->reserved); // Open if (unzOpenCurrentFile(uf) >= 0) { uint32_t magic = 0; archiveFileRead(ARCHIVE_FD, &magic, sizeof(uint32_t)); // SCE magic if (magic == 0x00454353) { char sce_header[0x84]; archiveFileRead(ARCHIVE_FD, sce_header, sizeof(sce_header)); uint64_t elf1_offset = *(uint64_t *)(sce_header + 0x3C); uint64_t phdr_offset = *(uint64_t *)(sce_header + 0x44); uint64_t section_info_offset = *(uint64_t *)(sce_header + 0x54); int i; // jump to elf1 // Until here we have read 0x88 bytes for (i = 0; i < elf1_offset - 0x88; i += sizeof(uint32_t)) { uint32_t dummy = 0; archiveFileRead(ARCHIVE_FD, &dummy, sizeof(uint32_t)); } // Check imports char *buffer = malloc(archive_entry->size); if (buffer) { int size = archiveFileRead(ARCHIVE_FD, buffer, archive_entry->size); Elf32_Ehdr *elf1 = (Elf32_Ehdr*)buffer; Elf32_Phdr *phdr = (Elf32_Phdr*)(buffer + phdr_offset - elf1_offset); segment_info *info = (segment_info*)(buffer + section_info_offset - elf1_offset); // segment is elf2 section char *segment = buffer + info->offset - elf1_offset; // zlib compress magic char *uncompressed_buffer = NULL; if (segment[0] == 0x78) { // uncompressedBuffer will return elf2 section uncompressed_buffer = uncompressBuffer(elf1, phdr, info, segment); if (uncompressed_buffer) { segment = uncompressed_buffer; } } int unsafe = checkForUnsafeImports(segment); if (uncompressed_buffer) { free(uncompressed_buffer); } free(buffer); if (unsafe) { archiveFileClose(ARCHIVE_FD); return unsafe; } } // Check authid flag uint64_t authid = *(uint64_t *)(sce_header + 0x7C); if (authid != 0x2F00000000000002) { archiveFileClose(ARCHIVE_FD); return 1; // Unsafe } } archiveFileClose(ARCHIVE_FD); } // Next archive_entry = archive_entry->next; } return 0; // Safe }
bool GOrgueConfigFileReader::Read(GOrgueFile* file) { m_Entries.clear(); if (!file->Open()) { wxLogError(_("Failed to open file '%s'"), file->GetName().c_str()); return false; } GOrgueBuffer<uint8_t> data; try { data.resize(file->GetSize()); } catch (GOrgueOutOfMemory e) { wxLogError(_("Failed to load file '%s' into the memory"), file->GetName().c_str()); file->Close(); return false; } if (!file->Read(data)) { file->Close(); wxLogError(_("Failed to read file '%s'"), file->GetName().c_str()); return false; } file->Close(); GOrgueHash hash; hash.Update(data.get(), data.GetSize()); m_Hash = hash.getStringHash(); if (isBufferCompressed(data)) { if (!uncompressBuffer(data)) { wxLogError(_("Failed to decompress file '%s'"), file->GetName().c_str()); return false; } } wxMBConv* conv; wxCSConv isoConv(wxT("ISO-8859-1")); uint8_t* dataPtr = data.get(); size_t length = data.GetCount(); if (length >= 3 && data[0] == 0xEF && data[1] == 0xBB && data[2] == 0xBF) { conv = &wxConvUTF8; dataPtr = data.get() + 3; length -= 3; } else conv = &isoConv; wxString input((const char*)dataPtr, *conv, length); data.free(); if (length && input.Len() == 0) { wxLogError(_("Failed to decode file '%s'"), file->GetName().c_str()); return false; } unsigned pos = 0; wxString group; std::map<wxString, wxString>* grp = NULL; unsigned lineno = 0; while(pos < input.Len()) { wxString line = GetNextLine(input, pos); lineno++; if (line == wxEmptyString) continue; if (line.Len() >= 1 && line[0] == wxT(';')) continue; if (line.Len() > 1 && line[0] == wxT('[')) { if (line[line.Len() - 1] != wxT(']')) { line = line.Trim(); if (line[line.Len() - 1] != wxT(']')) { wxLogError(_("Invalid Config entry at line %d: %s"), lineno, line.c_str()); continue; } wxLogError(_("Invalid section start at line %d: %s"), lineno, line.c_str()); } group = line.Mid(1, line.Len() - 2); if (m_Entries.find(group) != m_Entries.end()) { wxLogWarning(_("Duplicate group at line %d: %s"), lineno, group.c_str()); } grp = &m_Entries[group]; } else { if (!grp) { wxLogError(_("Config entry without any group at line %d"), lineno); continue; } int datapos = line.find(wxT("="), 0); if (datapos <= 0) { wxLogError(_("Invalid Config entry at line %d: %s"), lineno, line.c_str()); continue; } wxString name = line.Mid(0, datapos); if (grp->find(name) != grp->end()) { wxLogWarning(_("Duplicate entry in section %s at line %d: %s"), group.c_str(), lineno, name.c_str()); } (*grp)[name] = line.Mid(datapos + 1); } } return true; }