* Load a WSA file. * @param filename Name of the file. * @param wsa Data buffer for the WSA. * @param wsaSize Current size of buffer. * @param reserveDisplayFrame True if we need to reserve the display frame. * @return Address of loaded WSA file, or NULL. */
| 190 | * @return Address of loaded WSA file, or NULL. |
| 191 | */ |
| 192 | void *WSA_LoadFile(const char *filename, void *wsa, uint32 wsaSize, bool reserveDisplayFrame) |
| 193 | { |
| 194 | WSAFlags flags; |
| 195 | WSAFileHeader fileheader; |
| 196 | WSAHeader *header; |
| 197 | uint32 bufferSizeMinimal; |
| 198 | uint32 bufferSizeOptimal; |
| 199 | uint16 lengthHeader; |
| 200 | uint8 fileno; |
| 201 | uint16 lengthPalette; |
| 202 | uint16 lengthFirstFrame; |
| 203 | uint32 lengthFileContent; |
| 204 | uint32 displaySize; |
| 205 | uint8 *buffer; |
| 206 | |
| 207 | memset(&flags, 0, sizeof(flags)); |
| 208 | |
| 209 | fileno = File_Open(filename, FILE_MODE_READ); |
| 210 | fileheader.frames = File_Read_LE16(fileno); |
| 211 | fileheader.width = File_Read_LE16(fileno); |
| 212 | fileheader.height = File_Read_LE16(fileno); |
| 213 | fileheader.requiredBufferSize = File_Read_LE16(fileno); |
| 214 | fileheader.hasPalette = File_Read_LE16(fileno); /* has palette */ |
| 215 | fileheader.firstFrameOffset = File_Read_LE32(fileno); /* Offset of 1st frame */ |
| 216 | fileheader.secondFrameOffset = File_Read_LE32(fileno); /* Offset of 2nd frame (end of 1st frame) */ |
| 217 | |
| 218 | lengthPalette = 0; |
| 219 | if (fileheader.hasPalette) { |
| 220 | flags.hasPalette = true; |
| 221 | |
| 222 | lengthPalette = 0x300; /* length of a 256 color RGB palette */ |
| 223 | } |
| 224 | |
| 225 | lengthFileContent = File_Seek(fileno, 0, 2); |
| 226 | |
| 227 | lengthFirstFrame = 0; |
| 228 | if (fileheader.firstFrameOffset != 0) { |
| 229 | lengthFirstFrame = fileheader.secondFrameOffset - fileheader.firstFrameOffset; |
| 230 | } else { |
| 231 | flags.hasNoFirstFrame = true; /* is the continuation of another WSA */ |
| 232 | } |
| 233 | |
| 234 | lengthFileContent -= lengthPalette + lengthFirstFrame + 10; |
| 235 | |
| 236 | displaySize = 0; |
| 237 | if (reserveDisplayFrame) { |
| 238 | flags.displayInBuffer = true; |
| 239 | displaySize = fileheader.width * fileheader.height; |
| 240 | } |
| 241 | |
| 242 | bufferSizeMinimal = displaySize + fileheader.requiredBufferSize - 33 + sizeof(WSAHeader); |
| 243 | bufferSizeOptimal = bufferSizeMinimal + lengthFileContent; |
| 244 | |
| 245 | if (wsaSize > 1 && wsaSize < bufferSizeMinimal) { |
| 246 | File_Close(fileno); |
| 247 | |
| 248 | return NULL; |
| 249 | } |
no test coverage detected