| 1204 | } |
| 1205 | |
| 1206 | bool WINAPI CascReadFile(HANDLE hFile, void * pvBuffer, DWORD dwBytesToRead, PDWORD PtrBytesRead) |
| 1207 | { |
| 1208 | ULONGLONG SaveFilePointer; |
| 1209 | ULONGLONG StartOffset; |
| 1210 | ULONGLONG EndOffset; |
| 1211 | TCascFile * hf; |
| 1212 | LPBYTE pbBuffer = (LPBYTE)pvBuffer; |
| 1213 | DWORD dwBytesRead1 = 0; // From cache |
| 1214 | DWORD dwBytesRead2 = 0; // From file |
| 1215 | DWORD dwErrCode; |
| 1216 | |
| 1217 | // The buffer must be valid |
| 1218 | if(pvBuffer == NULL) |
| 1219 | { |
| 1220 | SetCascError(ERROR_INVALID_PARAMETER); |
| 1221 | return false; |
| 1222 | } |
| 1223 | |
| 1224 | // Validate the file handle |
| 1225 | if((hf = TCascFile::IsValid(hFile)) == NULL) |
| 1226 | { |
| 1227 | SetCascError(ERROR_INVALID_HANDLE); |
| 1228 | return false; |
| 1229 | } |
| 1230 | |
| 1231 | // Check files with zero size |
| 1232 | if(hf->ContentSize == 0) |
| 1233 | { |
| 1234 | PtrBytesRead[0] = 0; |
| 1235 | return true; |
| 1236 | } |
| 1237 | |
| 1238 | // If we don't have file frames loaded, we need to do it now. |
| 1239 | // Need to do it before file range check, as the file size may be unknown at this point |
| 1240 | dwErrCode = EnsureFileSpanFramesLoaded(hf); |
| 1241 | if(dwErrCode != ERROR_SUCCESS) |
| 1242 | { |
| 1243 | SetCascError(dwErrCode); |
| 1244 | return false; |
| 1245 | } |
| 1246 | |
| 1247 | // If the file position is at or beyond end of file, do nothing |
| 1248 | SaveFilePointer = StartOffset = hf->FilePointer; |
| 1249 | if(StartOffset >= hf->ContentSize) |
| 1250 | { |
| 1251 | PtrBytesRead[0] = 0; |
| 1252 | return true; |
| 1253 | } |
| 1254 | |
| 1255 | // If the read area goes beyond end of the file, cut the number of bytes to read |
| 1256 | EndOffset = StartOffset + dwBytesToRead; |
| 1257 | if(EndOffset > hf->ContentSize) |
| 1258 | { |
| 1259 | EndOffset = hf->ContentSize; |
| 1260 | } |
| 1261 | |
| 1262 | // Can we handle the request (at least partially) from the cache? |
| 1263 | if((dwBytesRead1 = ReadFile_Cache(hf, pbBuffer, StartOffset, EndOffset)) != 0) |