//--------------------------------------------------------------------------- int32_t PacketFile::readPackedPacket(int32_t packet, puint8_t buffer) { int32_t result = 0; if((packet == -1) || (packet == currentPacket) || (seekPacket(packet) == NO_ERROR)) { if((getStorageType() == STORAGE_TYPE_RAW) || (getStorageType() == STORAGE_TYPE_FWF)) { seek(packetBase); result = read(buffer, packetSize); } else { switch(getStorageType()) { case STORAGE_TYPE_LZD: { seek(packetBase + sizeof(int32_t)); read(buffer, packetSize); } break; case STORAGE_TYPE_ZLIB: { seek(packetBase + sizeof(int32_t)); read(buffer, packetSize); } break; } } } return result; }
//--------------------------------------------------------------------------- int32_t PacketFile::writePacket(int32_t packet, puint8_t buffer) { //-------------------------------------------------------- // This function replaces the packet with the contents // of buffer. There are two restrictions. The first is // that the packet must be the same length as the existing // packet. If not, buffer over/under run will occur. // The second is that the packet cannot be compressed since // there is no gaurantee that the new data will compress // to exactly the same length. Returns NO_ERROR if packet // written successfully. Otherwise returns error. int32_t result = 0; if((packet < 0) || (packet >= numPackets)) { return 0; } seekPacket(packet); if(packetType == STORAGE_TYPE_LZD || packetType == STORAGE_TYPE_HF || packetType == STORAGE_TYPE_ZLIB) { return (PACKET_WRONG_SIZE); } else { result = write(buffer, packetSize); } if(result == packetUnpackedSize) { return(NO_ERROR); } return BAD_WRITE_ERR; }
//--------------------------------------------------------------------------- long PacketFile::readPackedPacket (long packet, unsigned char *buffer) { long result = 0; if ((packet==-1) || (packet == currentPacket) || (seekPacket(packet) == NO_ERR)) { if ((getStorageType() == STORAGE_TYPE_RAW) || (getStorageType() == STORAGE_TYPE_FWF)) { seek(packetBase); result = read(buffer, packetSize); } else { switch (getStorageType()) { case STORAGE_TYPE_LZD: { seek(packetBase+sizeof(long)); read(buffer,packetSize); } break; case STORAGE_TYPE_ZLIB: { seek(packetBase+sizeof(long)); read(buffer,packetSize); } break; } } } return result; }
//--------------------------------------------------------------------------- void PacketFile::operator -- (void) { if(currentPacket-- <= 0) { currentPacket = 0; } seekPacket(currentPacket); }
//--------------------------------------------------------------------------- void PacketFile::operator ++ (void) { if(++currentPacket >= numPackets) { currentPacket = numPackets - 1; } seekPacket(currentPacket); }
//--------------------------------------------------------------------------- int32_t PacketFile::insertPacket(int32_t packet, puint8_t buffer, int32_t nbytes, uint8_t pType) { //-------------------------------------------------------- // This function writes the packet to the current end // of file and stores the packet address in the seek // table. Originally, replace was a NONO. No, we check // for the size and, if it is different, insert the new // packet into a new file and basically spend many timeparts doing it. // Necessary for the teditor. // I Love it. int32_t result = 0; if(packet < 0) { return result; } //--------------------------------------------------------------- // Only used here, so OK if regular WINDOWS(tm) malloc! puint8_t workBuffer = (puint8_t)malloc(DEFAULT_MAX_PACKET); //------------------------------------------------------------- // All new code here. Basically, open a new packet file, // write each of the old packets and this new one. Close all // and copy the new one over the old one. Open the new one and // set pointers accordingly. PacketFile tmpFile; result = tmpFile.create("AF3456AF.788"); if(packet >= numPackets) { numPackets++; } tmpFile.reserve(numPackets); for(size_t i = 0; i < numPackets; i++) { if(i == packet) { if(nbytes >= DEFAULT_MAX_PACKET) { //---------------------------------------------------- // Not sure what to do here. We'll try reallocating ::free(workBuffer); workBuffer = (puint8_t)malloc(packetSize); } tmpFile.writePacket(i, buffer, nbytes, pType); } else { seekPacket(i); int32_t storageType = getStorageType(); int32_t packetSize = getPacketSize(); if(packetSize >= DEFAULT_MAX_PACKET) { //---------------------------------------------------- // Not sure what to do here. We'll try reallocating ::free(workBuffer); workBuffer = (puint8_t)malloc(packetSize); } readPacket(i, workBuffer); tmpFile.writePacket(i, workBuffer, packetSize, storageType); } } //------------------------------------ // Now close and reassign everything. char ourFileName[250]; int32_t ourFileMode = 0; strcpy(ourFileName, fileName); ourFileMode = fileMode; tmpFile.close(); close(); remove(ourFileName); rename("AF3456AF.788", ourFileName); remove("AF3456AF.788"); open(ourFileName, (FileMode)ourFileMode); seekPacket(packet); return result; }
//--------------------------------------------------------------------------- int32_t PacketFile::readPacket(int32_t packet, puint8_t buffer) { int32_t result = 0; if((packet == -1) || (packet == currentPacket) || (seekPacket(packet) == NO_ERROR)) { if((getStorageType() == STORAGE_TYPE_RAW) || (getStorageType() == STORAGE_TYPE_FWF)) { seek(packetBase); result = read(buffer, packetSize); } else { switch(getStorageType()) { case STORAGE_TYPE_LZD: { seek(packetBase + sizeof(int32_t)); if(!LZPacketBuffer) { LZPacketBuffer = (puint8_t)malloc(LZPacketBufferSize); gosASSERT(LZPacketBuffer); } if((int32_t)LZPacketBufferSize < packetSize) { LZPacketBufferSize = packetSize; free(LZPacketBuffer); LZPacketBuffer = (puint8_t)malloc(LZPacketBufferSize); gosASSERT(LZPacketBuffer); } if(LZPacketBuffer) { read(LZPacketBuffer, (packetSize - sizeof(int32_t))); int32_t decompLength = LZDecomp(buffer, LZPacketBuffer, packetSize - sizeof(int32_t)); if(decompLength != packetUnpackedSize) result = 0; else result = decompLength; } } break; case STORAGE_TYPE_ZLIB: { seek(packetBase + sizeof(int32_t)); if(!LZPacketBuffer) { LZPacketBuffer = (puint8_t)malloc(LZPacketBufferSize); gosASSERT(LZPacketBuffer); } if((int32_t)LZPacketBufferSize < packetSize) { LZPacketBufferSize = packetSize; free(LZPacketBuffer); LZPacketBuffer = (puint8_t)malloc(LZPacketBufferSize); gosASSERT(LZPacketBuffer); } if(LZPacketBuffer) { read(LZPacketBuffer, (packetSize - sizeof(int32_t))); uint32_t decompLength = LZPacketBufferSize; int32_t decompResult = uncompress(buffer, &decompLength, LZPacketBuffer, packetSize - sizeof(int32_t)); if((decompResult != Z_OK) || ((int32_t)decompLength != packetUnpackedSize)) result = 0; else result = decompLength; } } break; case STORAGE_TYPE_HF: STOP(("Tried to read a Huffman Compressed Packet. No Longer Supported!!")); break; } } } return result; }