* Read bytes from a chunk file into a buffer. * * @param index The index given by ChunkFile_Open() of the file. * @param chunk The chunk to read from. * @param buffer The buffer to read into. * @param length The amount of bytes to read. * @return The amount of bytes truly read, or 0 if there was a failure. */
| 1062 | * @return The amount of bytes truly read, or 0 if there was a failure. |
| 1063 | */ |
| 1064 | uint32 ChunkFile_Read(uint8 index, uint32 chunk, void *buffer, uint32 buflen) |
| 1065 | { |
| 1066 | uint32 value = 0; |
| 1067 | uint32 length = 0; |
| 1068 | bool first = true; |
| 1069 | |
| 1070 | while (true) { |
| 1071 | if (File_Read(index, &value, 4) != 4 && !first) return 0; |
| 1072 | |
| 1073 | if (value == 0 && File_Read(index, &value, 4) != 4 && !first) return 0; |
| 1074 | |
| 1075 | if (File_Read(index, &length, 4) != 4 && !first) return 0; |
| 1076 | |
| 1077 | length = HTOBE32(length); |
| 1078 | |
| 1079 | if (value == chunk) { |
| 1080 | buflen = min(buflen, length); |
| 1081 | |
| 1082 | File_Read(index, buffer, buflen); |
| 1083 | |
| 1084 | length += 1; |
| 1085 | length &= 0xFFFFFFFE; |
| 1086 | |
| 1087 | if (buflen < length) File_Seek(index, length - buflen, 1); |
| 1088 | |
| 1089 | return buflen; |
| 1090 | } |
| 1091 | |
| 1092 | if (first) { |
| 1093 | File_Seek(index, 12, 0); |
| 1094 | first = false; |
| 1095 | continue; |
| 1096 | } |
| 1097 | |
| 1098 | length += 1; |
| 1099 | length &= 0xFFFFFFFE; |
| 1100 | File_Seek(index, length, 1); |
| 1101 | } |
| 1102 | } |
no test coverage detected