Compare commits

..

2 Commits

Author SHA1 Message Date
Shwoomple 9bda607649 lib/file: Implement cut function. 2022-11-12 10:59:08 +05:30
Shwoomple 76dd30c45a lib/file:Fix issue #71 (Out of Bounds access) 2022-11-12 10:58:27 +05:30
2 changed files with 45 additions and 6 deletions

View File

@ -187,7 +187,7 @@ ErrorOrVoid File::insertByte(uint8_t byte){
this->fileStream.seekg(0); this->fileStream.seekg(0);
this->write(readData); this->write(readData);
this->cursorPosition++; this->cursorPosition++;
delete buffer; delete[] buffer;
}catch(std::exception& e){ }catch(std::exception& e){
failure = true; failure = true;
} }
@ -209,7 +209,7 @@ ErrorOrVoid File::insert(std::vector<uint8_t> data){
this->fileStream.seekg(0); this->fileStream.seekg(0);
this->write(readData); this->write(readData);
this->cursorPosition += data.size(); this->cursorPosition += data.size();
delete buffer; delete[] buffer;
}catch(std::exception& e){ }catch(std::exception& e){
failure = true; failure = true;
} }
@ -230,10 +230,9 @@ ErrorOrVoid File::insertString(tiny_utf8::string string){
this->fileStream.seekg(0); this->fileStream.seekg(0);
//TODO: fix hack. tinyutf8 appends "_utf-8" when readData is assigned: tiny_utf8::string((char *) buffer); this->writeString(readData);
this->writeString(readData.substr(0, readData.find("_utf-8")));
this->cursorPosition += string.size(); this->cursorPosition += string.size();
delete buffer; delete[] buffer;
}catch(std::exception& e){ }catch(std::exception& e){
failure = true; failure = true;
} }
@ -257,13 +256,39 @@ ErrorOr<uint8_t> File::cutByte(){
this->fileStream.seekg(0); this->fileStream.seekg(0);
this->write(readData); this->write(readData);
this->cursorPosition++; this->cursorPosition++;
delete buffer; delete[] buffer;
}catch(std::exception& e){ }catch(std::exception& e){
failure = true; failure = true;
} }
return failure ? ErrorOr<uint8_t>(true, ErrorCodes::UNKNOWN) : ErrorOr<uint8_t>(byte); return failure ? ErrorOr<uint8_t>(true, ErrorCodes::UNKNOWN) : ErrorOr<uint8_t>(byte);
} }
ErrorOr<std::vector<uint8_t>> File::cut(uint64_t length){
bool failure = false;
std::vector<uint8_t> bytes;
try{
uint8_t* buffer = new uint8_t[this->size.value];
std::vector<uint8_t> readData;
this->fileStream.read(reinterpret_cast<char*>(buffer), this->size.value);
readData = std::vector<uint8_t>(buffer, buffer+this->size.value);
bytes = std::vector<uint8_t>(readData.begin() + this->cursorPosition, readData.begin() + (this->cursorPosition + length));
readData.erase(readData.begin() + this->cursorPosition, readData.begin() + (this->cursorPosition + length));
std::filesystem::resize_file(this->path, readData.size());
this->fileStream.seekg(0);
this->write(readData);
this->cursorPosition += length;
delete[] buffer;
}catch(std::exception& e){
failure = true;
}
return failure ? ErrorOr<std::vector<uint8_t>>(true, ErrorCodes::UNKNOWN) :ErrorOr<std::vector<uint8_t>>(bytes);
}
ErrorOr<File*> File::open(std::string path, char mode, uint64_t startPosition){ ErrorOr<File*> File::open(std::string path, char mode, uint64_t startPosition){
if (!std::filesystem::exists(path) && (mode == 'r' || mode == 'm')) { if (!std::filesystem::exists(path) && (mode == 'r' || mode == 'm')) {
return ErrorOr<File*>(true, ErrorCodes::FILE_NOT_FOUND, nullptr); return ErrorOr<File*>(true, ErrorCodes::FILE_NOT_FOUND, nullptr);

View File

@ -180,4 +180,18 @@ int main(){
ASSERT(cutByte.value == '.'); ASSERT(cutByte.value == '.');
ASSERT(cutByteString == "Hallo, Hi THE CAKE IS A LIE, Ich bin Shwoomple"); ASSERT(cutByteString == "Hallo, Hi THE CAKE IS A LIE, Ich bin Shwoomple");
std::cout << "Passed cut byte test." << std::endl; std::cout << "Passed cut byte test." << std::endl;
modifyFile->open();
modifyFile->cursorPosition = 9;
ErrorOr<std::vector<uint8_t>> cutBytes = modifyFile->cut(18);
modifyFile->close();
readFile->open();
readFile->cursorPosition = 0;
tiny_utf8::string cutBytesString = readFile->readString(readFile->size.value).value;
readFile->close();
ASSERT(cutBytes.value == std::vector<uint8_t>({' ', 'T', 'H', 'E', ' ', 'C', 'A', 'K', 'E', ' ', 'I', 'S', ' ','A', ' ', 'L', 'I', 'E'}))
ASSERT(cutBytesString == "Hallo, Hi, Ich bin Shwoomple");
std::cout << "Passed cut test." << std::endl;
} }