处理offset为负数情况

This commit is contained in:
mtdxc 2026-05-26 15:19:37 +08:00 committed by cqm
parent 86a7204bab
commit 2a17869463
2 changed files with 21 additions and 15 deletions

View File

@ -128,11 +128,11 @@ int MP4FileDisk::onWrite(const void *data, size_t bytes) {
return bytes == fwrite(data, 1, bytes, _file.get()) ? 0 : ferror(_file.get());
}
int MP4FileDisk::onSeek(uint64_t offset) {
return fseek64(_file.get(), offset, SEEK_SET);
int MP4FileDisk::onSeek(int64_t offset) {
return fseek64(_file.get(), offset, offset >= 0 ? SEEK_SET : SEEK_END);
}
uint64_t MP4FileDisk::onTell() {
int64_t MP4FileDisk::onTell() {
return ftell64(_file.get());
}
@ -149,15 +149,21 @@ size_t MP4FileMemory::fileSize() const{
return _memory.size();
}
uint64_t MP4FileMemory::onTell(){
int64_t MP4FileMemory::onTell(){
return _offset;
}
int MP4FileMemory::onSeek(uint64_t offset){
if (offset > _memory.size()) {
return -1;
int MP4FileMemory::onSeek(int64_t offset){
if (offset < 0) {
if (offset + _offset < 0)
return -1;
_offset += offset;
} else {
if (offset > _memory.size()) {
return -1;
}
_offset = offset;
}
_offset = offset;
return 0;
}

View File

@ -66,7 +66,7 @@ public:
* [AUTO-TRANSLATED:f8a5b290]
*/
virtual uint64_t onTell() = 0;
virtual int64_t onTell() = 0;
/**
* seek至文件某处
@ -78,7 +78,7 @@ public:
* [AUTO-TRANSLATED:936089eb]
*/
virtual int onSeek(uint64_t offset) = 0;
virtual int onSeek(int64_t offset) = 0;
/**
*
@ -136,8 +136,8 @@ public:
void closeFile();
protected:
uint64_t onTell() override;
int onSeek(uint64_t offset) override;
int64_t onTell() override;
int onSeek(int64_t offset) override;
int onRead(void *data, size_t bytes) override;
int onWrite(const void *data, size_t bytes) override;
@ -167,13 +167,13 @@ public:
std::string getAndClearMemory();
protected:
uint64_t onTell() override;
int onSeek(uint64_t offset) override;
int64_t onTell() override;
int onSeek(int64_t offset) override;
int onRead(void *data, size_t bytes) override;
int onWrite(const void *data, size_t bytes) override;
private:
uint64_t _offset = 0;
int64_t _offset = 0;
std::string _memory;
};