AppMessage *AppMessageParser::parseHeader( uint8_t octet ) { AppMessage *msg = 0; // store the header octet m_header_buffer.putOctet( octet ); // is the header buffer full now? if ( m_header_buffer.isFull() ) { // yes, try parse the header jdksavdecc_appdu *p = &m_current_message.m_appdu.base; p->version = m_header_buffer.getOctet( JDKSAVDECC_APPDU_OFFSET_VERSION ); p->message_type = m_header_buffer.getOctet( JDKSAVDECC_APPDU_OFFSET_MESSAGE_TYPE ); p->payload_length = m_header_buffer.getDoublet( JDKSAVDECC_APPDU_OFFSET_PAYLOAD_LENGTH ); p->address = m_header_buffer.getEUI48( JDKSAVDECC_APPDU_OFFSET_ADDRESS ); p->reserved = m_header_buffer.getDoublet( JDKSAVDECC_APPDU_OFFSET_RESERVED ); // and validate the header msg = validateHeader(); } return msg; }
ElfProgram::ElfProgram(const unsigned int *elf, unsigned int size) : elf(elf), size(size) { //Trying to follow the "full recognition before processing" approach, //(http://www.cs.dartmouth.edu/~sergey/langsec/occupy/FullRecognition.jpg) //all of the elf fields that will later be used are checked in advance. //Unused fields are unchecked, so when using new fields, add new checks if(validateHeader()==false) throw runtime_error("Bad file"); }
bool MemoryBlock::deserialize(const StreamBuffer& buffer) { std::vector<StreamBuffer> buffers; StreamBuffer::separate(buffer, buffers); MemoryBlockHeader theader; memcpy((unsigned char*)&theader, buffers[0].buffer, sizeof(MemoryBlockHeader)); if(!validateHeader(theader)) return false; memcpy((unsigned char*)this, buffers[1].buffer, buffers[1].size); StreamBuffer::clear(buffers); return true; }
void testRead(SamFile &inSam) { // Read the SAM Header. SamFileHeader samHeader; assert(inSam.ReadHeader(samHeader)); validateHeader(samHeader); testCopyHeader(samHeader); testModHeader(samHeader); SamRecord samRecord; assert(inSam.ReadRecord(samHeader, samRecord) == true); validateRead1(samRecord); // Set a new quality and get the buffer. samRecord.setQuality("ABCDE"); validateRead1ModQuality(samRecord); // void* buffer = samRecord.getRecordBuffer(); assert(inSam.ReadRecord(samHeader, samRecord) == true); validateRead2(samRecord); assert(inSam.ReadRecord(samHeader, samRecord) == true); validateRead3(samRecord); assert(inSam.ReadRecord(samHeader, samRecord) == true); validateRead4(samRecord); assert(inSam.ReadRecord(samHeader, samRecord) == true); validateRead5(samRecord); assert(inSam.ReadRecord(samHeader, samRecord) == true); validateRead6(samRecord); assert(inSam.ReadRecord(samHeader, samRecord) == true); validateRead7(samRecord); assert(inSam.ReadRecord(samHeader, samRecord) == true); validateRead8(samRecord); assert(inSam.ReadRecord(samHeader, samRecord) == true); validateRead9(samRecord); assert(inSam.ReadRecord(samHeader, samRecord) == true); validateRead10(samRecord); }
bool unpackPackage(const wchar_t* packagePath, const wchar_t* targetDir) { writeLog(LOG_NORMAL, L"Unpacking package: %s", packagePath); writeLog(LOG_NORMAL, L"To Directory: %s", targetDir); if (!fsEnsureDirectoryExists(targetDir)) { writeLog(LOG_QUIET, L"ERROR: Target directory does not exist and cannot be created.", targetDir); return false; } NexasPackage* package = openPackage(packagePath); if (!package) return false; bool result = validateHeader(package) && readIndex(package) && extractFiles(package, targetDir); closePackage(package); writeLog(LOG_NORMAL, (result) ? L"Unpacking Successful." : L"ERROR: Unpacking Failed."); return result; }
void testCopyHeader(SamFileHeader& samHeader) { // Copy the header. SamFileHeader samHeader2; SamHeaderRecord* recPtr = samHeader.getNextHeaderRecord(); while(recPtr != NULL) { samHeader2.addRecordCopy(*recPtr); recPtr = samHeader.getNextHeaderRecord(); } // Add the comments. std::string nextComment = samHeader.getNextComment(); while(nextComment != SamFileHeader::EMPTY_RETURN) { samHeader2.addComment(nextComment.c_str()); nextComment = samHeader.getNextComment(); } // Validate the header. validateHeader(samHeader2); }
bool MemoryBlock::validateHeader(const StreamBuffer& thbuffer) { MemoryBlockHeader theader; memcpy((unsigned char*)&theader, thbuffer.buffer, sizeof(MemoryBlockHeader)); return validateHeader(theader); }
u_int8_t OggFileParser::parseInitialPage() { u_int8_t header_type_flag; u_int32_t bitstream_serial_number; parseStartOfPage(header_type_flag, bitstream_serial_number); // If this is a BOS page, examine the first 8 bytes of the first 'packet', to see whether // the track data type is one that we know how to stream: OggTrack* track; if ((header_type_flag&0x02) != 0) { // BOS char const* mimeType = NULL; // if unknown if (fPacketSizeTable != NULL && fPacketSizeTable->size[0] >= 8) { // sanity check char buf[8]; testBytes((u_int8_t*)buf, 8); if (strncmp(&buf[1], "vorbis", 6) == 0) { mimeType = "audio/VORBIS"; ++fNumUnfulfilledTracks; } else if (strncmp(buf, "OpusHead", 8) == 0) { mimeType = "audio/OPUS"; ++fNumUnfulfilledTracks; } else if (strncmp(&buf[1], "theora", 6) == 0) { mimeType = "video/THEORA"; ++fNumUnfulfilledTracks; } } // Add a new track descriptor for this track: track = new OggTrack; track->trackNumber = bitstream_serial_number; track->mimeType = mimeType; fOurFile.addTrack(track); } else { // not a BOS page // Because this is not a BOS page, the specified track should already have been seen: track = fOurFile.lookup(bitstream_serial_number); } if (track != NULL) { // sanity check #ifdef DEBUG fprintf(stderr, "This track's MIME type: %s\n", track->mimeType == NULL ? "(unknown)" : track->mimeType); #endif if (track->mimeType != NULL && (strcmp(track->mimeType, "audio/VORBIS") == 0 || strcmp(track->mimeType, "video/THEORA") == 0 || strcmp(track->mimeType, "audio/OPUS") == 0)) { // Special-case handling of Vorbis, Theora, or Opus tracks: // Make a copy of each packet, until we get the three special headers that we need: Boolean isVorbis = strcmp(track->mimeType, "audio/VORBIS") == 0; Boolean isTheora = strcmp(track->mimeType, "video/THEORA") == 0; for (unsigned j = 0; j < fPacketSizeTable->numCompletedPackets && track->weNeedHeaders(); ++j) { unsigned const packetSize = fPacketSizeTable->size[j]; if (packetSize == 0) continue; // sanity check delete[] fSavedPacket/*if any*/; fSavedPacket = new u_int8_t[packetSize]; getBytes(fSavedPacket, packetSize); fPacketSizeTable->totSizes -= packetSize; // The start of the packet tells us whether its a header that we know about: Boolean headerIsKnown = False; unsigned index = 0; if (isVorbis) { u_int8_t const firstByte = fSavedPacket[0]; headerIsKnown = firstByte == 1 || firstByte == 3 || firstByte == 5; index = (firstByte-1)/2; // 1, 3, or 5 => 0, 1, or 2 } else if (isTheora) { u_int8_t const firstByte = fSavedPacket[0]; headerIsKnown = firstByte == 0x80 || firstByte == 0x81 || firstByte == 0x82; index = firstByte &~0x80; // 0x80, 0x81, or 0x82 => 0, 1, or 2 } else { // Opus if (strncmp((char const*)fSavedPacket, "OpusHead", 8) == 0) { headerIsKnown = True; index = 0; // "identification" header } else if (strncmp((char const*)fSavedPacket, "OpusTags", 8) == 0) { headerIsKnown = True; index = 1; // "comment" header } } if (headerIsKnown) { #ifdef DEBUG char const* headerName[3] = { "identification", "comment", "setup" }; fprintf(stderr, "Saved %d-byte %s \"%s\" header\n", packetSize, track->mimeType, headerName[index]); #endif // This is a header, but first check it for validity: if (!validateHeader(track, fSavedPacket, packetSize)) continue; // Save this header (deleting any old header of the same type that we'd saved before) delete[] track->vtoHdrs.header[index]; track->vtoHdrs.header[index] = fSavedPacket; fSavedPacket = NULL; track->vtoHdrs.headerSize[index] = packetSize; if (!track->weNeedHeaders()) { // We now have all of the needed Vorbis, Theora, or Opus headers for this track: --fNumUnfulfilledTracks; } // Note: The above code won't work if a required header is fragmented over // more than one 'page'. We assume that that won't ever happen... } } } } // Skip over any remaining packet data bytes: if (fPacketSizeTable->totSizes > 0) { #ifdef DEBUG fprintf(stderr, "Skipping %d remaining packet data bytes\n", fPacketSizeTable->totSizes); #endif skipBytes(fPacketSizeTable->totSizes); } return header_type_flag; }
void testFlagRead(const char* fileName) { SamFile inSam; SamFileHeader samHeader; SamRecord samRecord; //////////////////////////////////////////////////////////// // Required flag 0x48 (only flag 73 matches) // Exclude nothing assert(inSam.OpenForRead(fileName)); assert(inSam.ReadHeader(samHeader)); validateHeader(samHeader); inSam.SetReadFlags(0x48, 0x0); assert(inSam.ReadRecord(samHeader, samRecord) == true); validateRead1(samRecord); assert(inSam.ReadRecord(samHeader, samRecord) == false); inSam.Close(); //////////////////////////////////////////////////////////// // No required flags. // Exclude 0x48. This leaves just the one read with flag 133. assert(inSam.OpenForRead(fileName)); assert(inSam.ReadHeader(samHeader)); validateHeader(samHeader); inSam.SetReadFlags(0x0, 0x48); assert(inSam.ReadRecord(samHeader, samRecord) == true); validateRead2(samRecord); assert(inSam.ReadRecord(samHeader, samRecord) == false); inSam.Close(); //////////////////////////////////////////////////////////// // Required flag 0x40 // Exclude 0x48. // This will not find any records since the exclude and required conflict. assert(inSam.OpenForRead(fileName)); assert(inSam.ReadHeader(samHeader)); validateHeader(samHeader); inSam.SetReadFlags(0x40, 0x48); assert(inSam.ReadRecord(samHeader, samRecord) == false); inSam.Close(); //////////////////////////////////////////////////////////// // Required flag 0x4 // Exclude 0x8. // Only finds flag 133. assert(inSam.OpenForRead(fileName)); assert(inSam.ReadHeader(samHeader)); validateHeader(samHeader); inSam.SetReadFlags(0x4, 0x8); assert(inSam.ReadRecord(samHeader, samRecord) == true); validateRead2(samRecord); assert(inSam.ReadRecord(samHeader, samRecord) == false); inSam.Close(); //////////////////////////////////////////////////////////// // Required flag 0x4 // Exclude nothing // Finds flags 133 & 141. assert(inSam.OpenForRead(fileName)); assert(inSam.ReadHeader(samHeader)); validateHeader(samHeader); inSam.SetReadFlags(0x4, 0x0); assert(inSam.ReadRecord(samHeader, samRecord) == true); validateRead2(samRecord); assert(inSam.ReadRecord(samHeader, samRecord) == true); validateRead8(samRecord); assert(inSam.ReadRecord(samHeader, samRecord) == true); validateRead10(samRecord); assert(inSam.ReadRecord(samHeader, samRecord) == false); inSam.Close(); }