From e59793e29ecb47c63b72de4f9d1e6d044a0ff6f2 Mon Sep 17 00:00:00 2001 From: наб Date: Mon, 11 Nov 2024 15:23:46 +0100 Subject: Turn char[APT_BUFFER_SIZE] buffers into std::array --- apt-pkg/contrib/fileutl.cc | 35 +++++++++++++++++------------------ apt-pkg/contrib/hashes.cc | 17 +++++++++-------- ftparchive/apt-ftparchive.cc | 10 ++++------ ftparchive/multicompress.cc | 13 +++++++------ ftparchive/sources.cc | 8 ++++---- methods/store.cc | 9 +++++---- 6 files changed, 46 insertions(+), 46 deletions(-) diff --git a/apt-pkg/contrib/fileutl.cc b/apt-pkg/contrib/fileutl.cc index 4ad7b4cd5..db97738e3 100644 --- a/apt-pkg/contrib/fileutl.cc +++ b/apt-pkg/contrib/fileutl.cc @@ -59,6 +59,7 @@ #include #include +#include #include #include @@ -1246,12 +1247,11 @@ public: } virtual bool InternalSkip(unsigned long long Over) { - unsigned long long constexpr buffersize = APT_BUFFER_SIZE; - char buffer[buffersize]; + std::array buffer; while (Over != 0) { - unsigned long long toread = std::min(buffersize, Over); - if (filefd->Read(buffer, toread) == false) + auto toread = std::min(buffer.size(), Over); + if (filefd->Read(buffer.data(), toread) == false) return filefd->FileFdError("Unable to seek ahead %llu",Over); Over -= toread; } @@ -1273,10 +1273,10 @@ public: { unsigned long long size = 0; unsigned long long const oldSeek = filefd->Tell(); - char ignore[APT_BUFFER_SIZE]; + std::array ignore; unsigned long long read = 0; do { - if (filefd->Read(ignore, sizeof(ignore), &read) == false) + if (filefd->Read(ignore.data(), ignore.size(), &read) == false) { filefd->Seek(oldSeek); return 0; @@ -1994,7 +1994,7 @@ class APT_HIDDEN LzmaFileFdPrivate: public FileFdPrivate { /*{{{*/ struct LZMAFILE { FILE* file; FileFd * const filefd; - uint8_t buffer[APT_BUFFER_SIZE]; + std::array buffer; lzma_stream stream; lzma_ret err; bool eof; @@ -2005,19 +2005,18 @@ class APT_HIDDEN LzmaFileFdPrivate: public FileFdPrivate { /*{{{*/ { if (compressing == true && filefd->Failed() == false) { - size_t constexpr buffersize = sizeof(buffer)/sizeof(buffer[0]); while(true) { - stream.avail_out = buffersize; - stream.next_out = buffer; + stream.avail_out = buffer.size(); + stream.next_out = buffer.data(); err = lzma_code(&stream, LZMA_FINISH); if (err != LZMA_OK && err != LZMA_STREAM_END) { _error->Error("~LZMAFILE: Compress finalisation failed"); break; } - size_t const n = buffersize - stream.avail_out; - if (n && fwrite(buffer, 1, n, file) != n) + size_t const n = buffer.size() - stream.avail_out; + if (n && fwrite(buffer.data(), 1, n, file) != n) { _error->Errno("~LZMAFILE",_("Write error")); break; @@ -2112,8 +2111,8 @@ public: lzma->stream.avail_out = Size; if (lzma->stream.avail_in == 0) { - lzma->stream.next_in = lzma->buffer; - lzma->stream.avail_in = fread(lzma->buffer, 1, sizeof(lzma->buffer)/sizeof(lzma->buffer[0]), lzma->file); + lzma->stream.next_in = lzma->buffer.data(); + lzma->stream.avail_in = fread(lzma->buffer.data(), 1, lzma->buffer.size(), lzma->file); } lzma->err = lzma_code(&lzma->stream, LZMA_RUN); if (lzma->err == LZMA_STREAM_END) @@ -2147,13 +2146,13 @@ public: ssize_t Res; lzma->stream.next_in = (uint8_t *)From; lzma->stream.avail_in = Size; - lzma->stream.next_out = lzma->buffer; - lzma->stream.avail_out = sizeof(lzma->buffer)/sizeof(lzma->buffer[0]); + lzma->stream.next_out = lzma->buffer.data(); + lzma->stream.avail_out = lzma->buffer.size(); lzma->err = lzma_code(&lzma->stream, LZMA_RUN); if (lzma->err != LZMA_OK) return -1; - size_t const n = sizeof(lzma->buffer)/sizeof(lzma->buffer[0]) - lzma->stream.avail_out; - size_t const m = (n == 0) ? 0 : fwrite(lzma->buffer, 1, n, lzma->file); + size_t const n = lzma->buffer.size() - lzma->stream.avail_out; + size_t const m = (n == 0) ? 0 : fwrite(lzma->buffer.data(), 1, n, lzma->file); if (m != n) { Res = -1; diff --git a/apt-pkg/contrib/hashes.cc b/apt-pkg/contrib/hashes.cc index 7ff5f9e9f..0ad51fff0 100644 --- a/apt-pkg/contrib/hashes.cc +++ b/apt-pkg/contrib/hashes.cc @@ -21,6 +21,7 @@ #include #include +#include #include #include #include @@ -372,33 +373,33 @@ bool Hashes::Add(const unsigned char * const Data, unsigned long long const Size } bool Hashes::AddFD(int const Fd,unsigned long long Size) { - unsigned char Buf[APT_BUFFER_SIZE]; + std::array Buf; bool const ToEOF = (Size == UntilEOF); while (Size != 0 || ToEOF) { - decltype(Size) n = sizeof(Buf); + decltype(Size) n = Buf.size(); if (!ToEOF) n = std::min(Size, n); - ssize_t const Res = read(Fd,Buf,n); + ssize_t const Res = read(Fd,Buf.data(),n); if (Res < 0 || (!ToEOF && Res != (ssize_t) n)) // error, or short read return false; if (ToEOF && Res == 0) // EOF break; Size -= Res; - if (Add(Buf, Res) == false) + if (Add(Buf.data(), Res) == false) return false; } return true; } bool Hashes::AddFD(FileFd &Fd,unsigned long long Size) { - unsigned char Buf[APT_BUFFER_SIZE]; + std::array Buf; bool const ToEOF = (Size == 0); while (Size != 0 || ToEOF) { - decltype(Size) n = sizeof(Buf); + decltype(Size) n = Buf.size(); if (!ToEOF) n = std::min(Size, n); decltype(Size) a = 0; - if (Fd.Read(Buf, n, &a) == false) // error + if (Fd.Read(Buf.data(), n, &a) == false) // error return false; if (ToEOF == false) { @@ -408,7 +409,7 @@ bool Hashes::AddFD(FileFd &Fd,unsigned long long Size) else if (a == 0) // EOF break; Size -= a; - if (Add(Buf, a) == false) + if (Add(Buf.data(), a) == false) return false; } return true; diff --git a/ftparchive/apt-ftparchive.cc b/ftparchive/apt-ftparchive.cc index d1ecaa547..9c86d18dd 100644 --- a/ftparchive/apt-ftparchive.cc +++ b/ftparchive/apt-ftparchive.cc @@ -387,17 +387,15 @@ bool PackageMap::GenContents(Configuration &Setup, return false; unsigned long long Size = Head.Size(); - unsigned char Buf[APT_BUFFER_SIZE]; + std::array Buf; while (Size != 0) { - unsigned long long ToRead = Size; - if (Size > sizeof(Buf)) - ToRead = sizeof(Buf); + auto ToRead = std::min(Size, Buf.size()); - if (Head.Read(Buf,ToRead) == false) + if (Head.Read(Buf.data(),ToRead) == false) return false; - if (Comp.Input.Write(Buf, ToRead) == false) + if (Comp.Input.Write(Buf.data(), ToRead) == false) return _error->Errno("fwrite",_("Error writing header to contents file")); Size -= ToRead; diff --git a/ftparchive/multicompress.cc b/ftparchive/multicompress.cc index ac85670d5..cbbbe3518 100644 --- a/ftparchive/multicompress.cc +++ b/ftparchive/multicompress.cc @@ -21,6 +21,7 @@ #include #include +#include #include #include #include @@ -264,23 +265,23 @@ bool MultiCompress::Child(int const &FD) /* Okay, now we just feed data from FD to all the other FDs. Also stash a hash of the data to use later. */ SetNonBlock(FD,false); - unsigned char Buffer[32*1024]; + std::array Buffer; unsigned long long FileSize = 0; Hashes MD5(Hashes::MD5SUM); while (1) { WaitFd(FD,false); - int Res = read(FD,Buffer,sizeof(Buffer)); + int Res = read(FD,Buffer.data(),Buffer.size()); if (Res == 0) break; if (Res < 0) continue; - MD5.Add(Buffer,Res); + MD5.Add(Buffer.data(),Res); FileSize += Res; for (Files *I = Outputs; I != 0; I = I->Next) { - if (I->TmpFile.Write(Buffer, Res) == false) + if (I->TmpFile.Write(Buffer.data(), Res) == false) { _error->Errno("write",_("IO to subprocess/file failed")); break; @@ -319,12 +320,12 @@ bool MultiCompress::Child(int const &FD) while (1) { unsigned long long Res = 0; - if (CompFd.Read(Buffer,sizeof(Buffer), &Res) == false) + if (CompFd.Read(Buffer.data(),Buffer.size(), &Res) == false) return _error->Errno("read",_("Failed to read while computing MD5")); if (Res == 0) break; NewFileSize += Res; - OldMD5.Add(Buffer,Res); + OldMD5.Add(Buffer.data(),Res); } CompFd.Close(); diff --git a/ftparchive/sources.cc b/ftparchive/sources.cc index a59224395..4c7a51dff 100644 --- a/ftparchive/sources.cc +++ b/ftparchive/sources.cc @@ -3,7 +3,7 @@ #include #include -// for memcpy +#include #include #include @@ -42,16 +42,16 @@ bool DscExtract::Read(std::string FileName) IsClearSigned = (FileName != F.Name()); std::ostringstream data; - char buffer[1024]; + std::array buffer; do { unsigned long long actual = 0; - if (F.Read(buffer, sizeof(buffer)-1, &actual) == false) + if (F.Read(buffer.data(), buffer.size()-1, &actual) == false) return _error->Errno("read", "Failed to read dsc file %s", FileName.c_str()); if (actual == 0) break; Length += actual; buffer[actual] = '\0'; - data << buffer; + data << buffer.data(); } while(true); // adding two newlines 'off record' for pkgTagSection.Scan() calls diff --git a/methods/store.cc b/methods/store.cc index 8b30efaf3..23d6b43d0 100644 --- a/methods/store.cc +++ b/methods/store.cc @@ -21,6 +21,7 @@ #include #include +#include #include #include #include @@ -103,10 +104,10 @@ bool StoreMethod::Fetch(FetchItem *Itm) /*{{{*/ Res.Size = 0; while (1) { - unsigned char Buffer[APT_BUFFER_SIZE]; + std::array Buffer; unsigned long long Count = 0; - if (!From.Read(Buffer,sizeof(Buffer),&Count)) + if (!From.Read(Buffer.data(),Buffer.size(),&Count)) { if (To.IsOpen()) To.OpFail(); @@ -116,8 +117,8 @@ bool StoreMethod::Fetch(FetchItem *Itm) /*{{{*/ break; Res.Size += Count; - Hash.Add(Buffer,Count); - if (To.IsOpen() && To.Write(Buffer,Count) == false) + Hash.Add(Buffer.data(),Count); + if (To.IsOpen() && To.Write(Buffer.data(),Count) == false) { Failed = true; break; -- cgit v1.2.3-70-g09d2