diff --git a/code/vqa.cpp b/code/vqa.cpp index bb892ddd4..ad4006de3 100644 --- a/code/vqa.cpp +++ b/code/vqa.cpp @@ -299,16 +299,16 @@ long VQAClass::CacheHandler(long action, void * buffer, long nbytes) break; case VQACMD_SEEK: - switch ((int)(intptr_t)buffer) { - case 1: + switch (VQA_DecodeSeekOrigin(buffer)) { + case SEEK_CUR: Cache.file_buffer_pos += nbytes; rc = 0; break; - case 0: + case SEEK_SET: Cache.file_buffer_pos = nbytes; rc = 0; break; - case 2: + case SEEK_END: Cache.file_buffer_pos = Cache.file_size - nbytes - 1; rc = 0; break; @@ -846,26 +846,25 @@ long VQAClass::CCFileHandler(long action, void * buffer, long nbytes) break; /* - ** VQACMD_SEEK asks that you perform a seek relative to the current - ** position. NBytes is a signed number, indicating seek direction - ** (positive for forward, negative for backward). Buffer has no meaning - ** here. + ** VQACMD_SEEK asks that you perform a seek from the origin Buffer + ** names, which is a SEEK_SET, SEEK_CUR or SEEK_END value cast to a + ** pointer. NBytes is the signed offset from that origin. ** ** Any error code returned will be remapped by VQA library into ** VQAERR_SEEK. */ case VQACMD_SEEK: - error = (FileHandle.Seek(nbytes, (int)(intptr_t)buffer) == 0); + error = (FileHandle.Seek(nbytes, VQA_DecodeSeekOrigin(buffer)) == 0); break; case VQACMD_SEEKPEEK: if (nbytes > 0) { - error = FileHandle.Seek(nbytes - sizeof(tmp), (int)(intptr_t)buffer) == 0; + error = FileHandle.Seek(nbytes - sizeof(tmp), VQA_DecodeSeekOrigin(buffer)) == 0; if (error == 0) { error = FileHandle.Read(&tmp, sizeof(tmp)) != sizeof(tmp); } } else { - error = FileHandle.Seek(nbytes, (int)(intptr_t)buffer) == 0; + error = FileHandle.Seek(nbytes, VQA_DecodeSeekOrigin(buffer)) == 0; if (error == 0) { error = FileHandle.Read(&tmp, sizeof(tmp)) != sizeof(tmp); if (error == 0) { @@ -973,26 +972,25 @@ long VQAClass::MixFileHandler(long action, void * buffer, long nbytes) break; /* - ** VQACMD_SEEK asks that you perform a seek relative to the current - ** position. NBytes is a signed number, indicating seek direction - ** (positive for forward, negative for backward). Buffer has no meaning - ** here. + ** VQACMD_SEEK asks that you perform a seek from the origin Buffer + ** names, which is a SEEK_SET, SEEK_CUR or SEEK_END value cast to a + ** pointer. NBytes is the signed offset from that origin. ** ** Any error code returned will be remapped by VQA library into ** VQAERR_SEEK. */ case VQACMD_SEEK: - error = (FileHandle.Seek(nbytes, (int)(intptr_t)buffer) == 0); + error = (FileHandle.Seek(nbytes, VQA_DecodeSeekOrigin(buffer)) == 0); break; case VQACMD_SEEKPEEK: if (nbytes > 0) { - error = FileHandle.Seek(nbytes - sizeof(tmp), (int)(intptr_t)buffer) == 0; + error = FileHandle.Seek(nbytes - sizeof(tmp), VQA_DecodeSeekOrigin(buffer)) == 0; if (error == 0) { error = FileHandle.Read(&tmp, sizeof(tmp)) != sizeof(tmp); } } else { - error = FileHandle.Seek(nbytes, (int)(intptr_t)buffer) == 0; + error = FileHandle.Seek(nbytes, VQA_DecodeSeekOrigin(buffer)) == 0; if (error == 0) { error = FileHandle.Read(&tmp, sizeof(tmp)) != sizeof(tmp); if (error == 0) { diff --git a/code/vqalib/dstream.cpp b/code/vqalib/dstream.cpp index 456462e0f..e38527cf2 100644 --- a/code/vqalib/dstream.cpp +++ b/code/vqalib/dstream.cpp @@ -89,31 +89,30 @@ intptr_t __cdecl Disk_VQA_Stream_Handler(VQAHandle *vqa, long action, void *buff error = 1; break; - /* VQACMD_SEEK asks that you perform a seek relative to the current - * position. NBytes is a signed number, indicating seek direction - * (positive for forward, negative for backward). Buffer has no meaning - * here. + /* VQACMD_SEEK asks that you perform a seek from the origin Buffer + * names, which is a SEEK_SET, SEEK_CUR or SEEK_END value cast to a + * pointer. NBytes is the signed offset from that origin. * * Any error code returned will be remapped by VQA library into * VQAERR_SEEK. */ case VQACMD_SEEK: - error = (lseek(fh, nbytes, (int)(intptr_t)buffer) == -1); + error = (lseek(fh, nbytes, VQA_DecodeSeekOrigin(buffer)) == -1); break; case VQACMD_SEEKPEEK: if (nbytes > 0) { - error = lseek(fh, nbytes - 1, (int)(intptr_t)buffer) == -1; + error = lseek(fh, nbytes - 1, VQA_DecodeSeekOrigin(buffer)) == -1; if (error == 0) { error = read(fh, &temp, 1) != 1; } } else { - error = lseek(fh, nbytes, (int)(intptr_t)buffer) == -1; + error = lseek(fh, nbytes, VQA_DecodeSeekOrigin(buffer)) == -1; if (error == 0) { error = read(fh, &temp, 1) != 1; } if (error == 0) { - error = lseek(fh, -1, 1) == -1; + error = lseek(fh, -1, SEEK_CUR) == -1; } } break; @@ -186,24 +185,23 @@ intptr_t __cdecl Memory_VQA_Stream_Handler(VQAHandle *vqa, long action, void *bu error = 1; break; - /* VQACMD_SEEK asks that you perform a seek relative to the current - * position. NBytes is a signed number, indicating seek direction - * (positive for forward, negative for backward). Buffer has no meaning - * here. + /* VQACMD_SEEK asks that you perform a seek from the origin Buffer + * names, which is a SEEK_SET, SEEK_CUR or SEEK_END value cast to a + * pointer. NBytes is the signed offset from that origin. * * Any error code returned will be remapped by VQA library into * VQAERR_SEEK. */ case VQACMD_SEEK: case VQACMD_SEEKPEEK: - switch ((intptr_t)buffer) { + switch (VQA_DecodeSeekOrigin(buffer)) { - case 1: + case SEEK_CUR: cache->Offset += nbytes; error = 0; break; - case 0: + case SEEK_SET: p = cache->FileOffset; if (nbytes >= p) { cache->Offset = nbytes - p; diff --git a/code/vqalib/loader.cpp b/code/vqalib/loader.cpp index 63200ec69..fb05d1047 100644 --- a/code/vqalib/loader.cpp +++ b/code/vqalib/loader.cpp @@ -260,7 +260,7 @@ long VQA_LoadFrame(VQAHandleP *vqap, long flags) if (tocache > 0) { if (config->StreamHandler != Memory_VQA_Stream_Handler) { - config->StreamHandler((VQAHandle *)vqap, VQACMD_SEEK, NULL, foffset); + config->StreamHandler((VQAHandle *)vqap, VQACMD_SEEK, (void *)SEEK_SET, foffset); } if (config->StreamHandler((VQAHandle *)vqap, VQACMD_READ, &cache->Ptr[cache->Offset], tocache)) { return -4; @@ -1074,7 +1074,7 @@ long VQA_SeekLoop(VQAHandleP *vqap, long framenum, long flags) needs_seek = true; } cache->Offset = 0; - } else if (vqap->Config.StreamHandler((VQAHandle *)vqap, VQACMD_SEEK, 0, VQAFRAME_OFFSET(foff[framenum])) != 0) { + } else if (vqap->Config.StreamHandler((VQAHandle *)vqap, VQACMD_SEEK, (void *)SEEK_SET, VQAFRAME_OFFSET(foff[framenum])) != 0) { return(VQAERR_SEEK); } @@ -1083,7 +1083,7 @@ long VQA_SeekLoop(VQAHandleP *vqap, long framenum, long flags) } if (rc == VQAERR_NONE) { - if (needs_seek && vqap->Config.StreamHandler((VQAHandle *)vqap, VQACMD_SEEKPEEK, 0, cache->FileOffset + cache->Bytes) != 0) { + if (needs_seek && vqap->Config.StreamHandler((VQAHandle *)vqap, VQACMD_SEEKPEEK, (void *)SEEK_SET, cache->FileOffset + cache->Bytes) != 0) { return(VQAERR_SEEK); } } @@ -1826,7 +1826,7 @@ long Load_VQF(VQAHandleP *vqap, unsigned long frame_iffsize, char flags) /* Skip any unknown chunks. */ if (skip == true) { - if (vqap->Config.StreamHandler((VQAHandle *)vqap, VQACMD_SEEK, (void *)1, PADSIZE(iffsize))) { + if (vqap->Config.StreamHandler((VQAHandle *)vqap, VQACMD_SEEK, (void *)SEEK_CUR, PADSIZE(iffsize))) { return(VQAERR_SEEK); } } diff --git a/code/vqalib/vqaplay.h b/code/vqalib/vqaplay.h index 268e7fb24..3447bb396 100644 --- a/code/vqalib/vqaplay.h +++ b/code/vqalib/vqaplay.h @@ -366,6 +366,15 @@ typedef struct _VQAHandle { #define VQACMD_SEEKPEEK 8 #define VQACMD_SIZE 9 +/* VQACMD_SEEK and VQACMD_SEEKPEEK carry the seek origin in Buffer rather + * than a pointer. It comes back at pointer width before narrowing to the + * int origin that lseek and FileClass::Seek take. + */ +inline int VQA_DecodeSeekOrigin(void *buffer) +{ + return (int)(std::intptr_t)buffer; +} + #define VQAMEM_0 0 #define VQAMEM_1 1 #define VQAMEM_ALLOC 2