diff options
Diffstat (limited to 'apt-pkg/contrib')
-rw-r--r-- | apt-pkg/contrib/hashes.cc | 11 | ||||
-rw-r--r-- | apt-pkg/contrib/mmap.cc | 108 | ||||
-rw-r--r-- | apt-pkg/contrib/mmap.h | 2 | ||||
-rw-r--r-- | apt-pkg/contrib/sha256.cc | 32 |
4 files changed, 106 insertions, 47 deletions
diff --git a/apt-pkg/contrib/hashes.cc b/apt-pkg/contrib/hashes.cc index fcc2f887c..52b9bfbe6 100644 --- a/apt-pkg/contrib/hashes.cc +++ b/apt-pkg/contrib/hashes.cc @@ -34,7 +34,7 @@ HashString::HashString(string Type, string Hash) : Type(Type), Hash(Hash) { } -HashString::HashString(string StringedHash) +HashString::HashString(string StringedHash) /*{{{*/ { // legacy: md5sum without "MD5Sum:" prefix if (StringedHash.find(":") == string::npos && StringedHash.size() == 32) @@ -50,9 +50,8 @@ HashString::HashString(string StringedHash) if(_config->FindB("Debug::Hashes",false) == true) std::clog << "HashString(string): " << Type << " : " << Hash << std::endl; } - - -bool HashString::VerifyFile(string filename) const + /*}}}*/ +bool HashString::VerifyFile(string filename) const /*{{{*/ { FileFd fd; MD5Summation MD5; @@ -83,7 +82,7 @@ bool HashString::VerifyFile(string filename) const return (fileHash == Hash); } - + /*}}}*/ const char** HashString::SupportedHashes() { return _SupportedHashes; @@ -94,13 +93,11 @@ bool HashString::empty() const return (Type.empty() || Hash.empty()); } - string HashString::toStr() const { return Type+string(":")+Hash; } - // Hashes::AddFD - Add the contents of the FD /*{{{*/ // --------------------------------------------------------------------- /* */ diff --git a/apt-pkg/contrib/mmap.cc b/apt-pkg/contrib/mmap.cc index 04a45811b..5f56178f4 100644 --- a/apt-pkg/contrib/mmap.cc +++ b/apt-pkg/contrib/mmap.cc @@ -13,17 +13,13 @@ libc6 generates warnings -- which should be errors, g++ isn't properly strict. - The configure test notes that some OS's have broken private mmap's - so on those OS's we can't use mmap. This means we have to use - configure to test mmap and can't rely on the POSIX - _POSIX_MAPPED_FILES test. - ##################################################################### */ /*}}}*/ // Include Files /*{{{*/ #define _BSD_SOURCE #include <apt-pkg/mmap.h> #include <apt-pkg/error.h> +#include <apt-pkg/configuration.h> #include <apti18n.h> @@ -31,6 +27,8 @@ #include <sys/stat.h> #include <unistd.h> #include <fcntl.h> +#include <stdlib.h> +#include <signal.h> #include <cstring> /*}}}*/ @@ -141,6 +139,20 @@ bool MMap::Sync(unsigned long Start,unsigned long Stop) } /*}}}*/ +// DynamicMMapSegfaultHandler /*{{{*/ +// --------------------------------------------------------------------- +/* In theory, the mmap should never segfault because we check the available + size of our mmap before we use it, but there are a few reports out there + which state that the mmap segfaults without further notice. So this handler + will take care of all these segfaults which should never happen... */ +void DynamicMMapSegfaultHandler(int) +{ + _error->Error(_("Dynamic MMap segfaults, most likely because it ran out of room. " + "Please increase the size of APT::Cache-Limit. (man 5 apt.conf)")); + _error->DumpErrors(); + exit(EXIT_FAILURE); +} + /*}}}*/ // DynamicMMap::DynamicMMap - Constructor /*{{{*/ // --------------------------------------------------------------------- /* */ @@ -166,13 +178,30 @@ DynamicMMap::DynamicMMap(FileFd &F,unsigned long Flags,unsigned long WorkSpace) /*}}}*/ // DynamicMMap::DynamicMMap - Constructor for a non-file backed map /*{{{*/ // --------------------------------------------------------------------- -/* This is just a fancy malloc really.. */ +/* We try here to use mmap to reserve some space - this is much more + cooler than the fallback solution to simply allocate a char array + and could come in handy later than we are able to grow such an mmap */ DynamicMMap::DynamicMMap(unsigned long Flags,unsigned long WorkSpace) : MMap(Flags | NoImmMap | UnMapped), Fd(0), WorkSpace(WorkSpace) { if (_error->PendingError() == true) return; - + + if (_config->FindB("MMap::SegfaultHandler",true) == true) + { + struct sigaction sa; + sa.sa_handler = DynamicMMapSegfaultHandler; + sigaction(SIGSEGV, &sa, NULL); + } + +#ifdef _POSIX_MAPPED_FILES + // use anonymous mmap() to get the memory + Base = (unsigned char*) mmap(0, WorkSpace, PROT_READ|PROT_WRITE, + MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); + if(Base != MAP_FAILED) + return; +#endif + // fallback to a static allocated space Base = new unsigned char[WorkSpace]; memset(Base,0,WorkSpace); iSize = 0; @@ -185,7 +214,11 @@ DynamicMMap::~DynamicMMap() { if (Fd == 0) { +#ifdef _POSIX_MAPPED_FILES + munmap(Base, WorkSpace); +#else delete [] (unsigned char *)Base; +#endif return; } @@ -207,14 +240,16 @@ unsigned long DynamicMMap::RawAllocate(unsigned long Size,unsigned long Aln) iSize = Result + Size; - // Just in case error check - if (Result + Size > WorkSpace) + // try to grow the buffer + while(Result + Size > WorkSpace) { - _error->Error(_("Dynamic MMap ran out of room. Please increase the size " - "of APT::Cache-Limit. Current value: %lu. (man 5 apt.conf)"), WorkSpace); - return 0; + if(!Grow()) + { + _error->Error(_("Dynamic MMap ran out of room. Please increase the size " + "of APT::Cache-Limit. Current value: %lu. (man 5 apt.conf)"), WorkSpace); + return 0; + } } - return Result; } /*}}}*/ @@ -234,7 +269,6 @@ unsigned long DynamicMMap::Allocate(unsigned long ItemSize) if (I->ItemSize == ItemSize) break; } - // No pool is allocated, use an unallocated one if (I == Pools + PoolCount) { @@ -270,14 +304,17 @@ unsigned long DynamicMMap::WriteString(const char *String, unsigned long Len) { unsigned long Result = iSize; - // Just in case error check - if (Result + Len > WorkSpace) + // try to grow the buffer + while(Result + Len > WorkSpace) { - _error->Error(_("Dynamic MMap ran out of room. Please increase the size " - "of APT::Cache-Limit. Current value: %lu. (man 5 apt.conf)"), WorkSpace); - return 0; - } - + if(!Grow()) + { + _error->Error(_("Dynamic MMap ran out of room. Please increase the size " + "of APT::Cache-Limit. Current value: %lu. (man 5 apt.conf)"), WorkSpace); + return 0; + } + } + if (Len == (unsigned long)-1) Len = strlen(String); iSize += Len + 1; @@ -286,3 +323,32 @@ unsigned long DynamicMMap::WriteString(const char *String, return Result; } /*}}}*/ +// DynamicMMap::Grow - Grow the mmap /*{{{*/ +// --------------------------------------------------------------------- +/* This method will try to grow the mmap we currently use. This doesn't + work most of the time because we can't move the mmap around in the + memory for now as this would require to adjust quite a lot of pointers + but why we should not at least try to grow it before we give up? */ +bool DynamicMMap::Grow() +{ +#ifdef _POSIX_MAPPED_FILES + unsigned long newSize = WorkSpace + 1024*1024; + + if(Fd != 0) + { + Fd->Seek(newSize - 1); + char C = 0; + Fd->Write(&C,sizeof(C)); + } + + Base = mremap(Base, WorkSpace, newSize, 0); + if(Base == MAP_FAILED) + return false; + + WorkSpace = newSize; + return true; +#else + return false; +#endif +} + /*}}}*/ diff --git a/apt-pkg/contrib/mmap.h b/apt-pkg/contrib/mmap.h index 19cf7582d..bde62217d 100644 --- a/apt-pkg/contrib/mmap.h +++ b/apt-pkg/contrib/mmap.h @@ -84,6 +84,8 @@ class DynamicMMap : public MMap unsigned long WorkSpace; Pool *Pools; unsigned int PoolCount; + + bool Grow(); public: diff --git a/apt-pkg/contrib/sha256.cc b/apt-pkg/contrib/sha256.cc index ecda3d8e8..e380c13ae 100644 --- a/apt-pkg/contrib/sha256.cc +++ b/apt-pkg/contrib/sha256.cc @@ -1,5 +1,5 @@ /* - * Cryptographic API. + * Cryptographic API. {{{ * * SHA-256, as specified in * http://csrc.nist.gov/cryptval/shs/sha256-384-512.pdf @@ -17,7 +17,7 @@ * Software Foundation; either version 2 of the License, or (at your option) * any later version. * - */ + */ /*}}}*/ #ifdef __GNUG__ #pragma implementation "apt-pkg/sha256.h" @@ -65,20 +65,20 @@ static inline u32 Maj(u32 x, u32 y, u32 z) #define H6 0x1f83d9ab #define H7 0x5be0cd19 -static inline void LOAD_OP(int I, u32 *W, const u8 *input) +static inline void LOAD_OP(int I, u32 *W, const u8 *input) /*{{{*/ { W[I] = ( ((u32) input[I * 4 + 0] << 24) | ((u32) input[I * 4 + 1] << 16) | ((u32) input[I * 4 + 2] << 8) | ((u32) input[I * 4 + 3])); } - + /*}}}*/ static inline void BLEND_OP(int I, u32 *W) { W[I] = s1(W[I-2]) + W[I-7] + s0(W[I-15]) + W[I-16]; } -static void sha256_transform(u32 *state, const u8 *input) +static void sha256_transform(u32 *state, const u8 *input) /*{{{*/ { u32 a, b, c, d, e, f, g, h, t1, t2; u32 W[64]; @@ -240,8 +240,8 @@ static void sha256_transform(u32 *state, const u8 *input) a = b = c = d = e = f = g = h = t1 = t2 = 0; memset(W, 0, 64 * sizeof(u32)); } - -SHA256Summation::SHA256Summation() + /*}}}*/ +SHA256Summation::SHA256Summation() /*{{{*/ { Sum.state[0] = H0; Sum.state[1] = H1; @@ -255,8 +255,8 @@ SHA256Summation::SHA256Summation() memset(Sum.buf, 0, sizeof(Sum.buf)); Done = false; } - -bool SHA256Summation::Add(const u8 *data, unsigned long len) + /*}}}*/ +bool SHA256Summation::Add(const u8 *data, unsigned long len) /*{{{*/ { struct sha256_ctx *sctx = ∑ unsigned int i, index, part_len; @@ -291,8 +291,8 @@ bool SHA256Summation::Add(const u8 *data, unsigned long len) return true; } - -SHA256SumValue SHA256Summation::Result() + /*}}}*/ +SHA256SumValue SHA256Summation::Result() /*{{{*/ { struct sha256_ctx *sctx = ∑ if (!Done) { @@ -340,7 +340,7 @@ SHA256SumValue SHA256Summation::Result() return res; } - + /*}}}*/ // SHA256SumValue::SHA256SumValue - Constructs the sum from a string /*{{{*/ // --------------------------------------------------------------------- /* The string form of a SHA256 is a 64 character hex number */ @@ -349,7 +349,6 @@ SHA256SumValue::SHA256SumValue(string Str) memset(Sum,0,sizeof(Sum)); Set(Str); } - /*}}}*/ // SHA256SumValue::SHA256SumValue - Default constructor /*{{{*/ // --------------------------------------------------------------------- @@ -358,7 +357,6 @@ SHA256SumValue::SHA256SumValue() { memset(Sum,0,sizeof(Sum)); } - /*}}}*/ // SHA256SumValue::Set - Set the sum from a string /*{{{*/ // --------------------------------------------------------------------- @@ -391,9 +389,7 @@ string SHA256SumValue::Value() const return string(Result); } - - - + /*}}}*/ // SHA256SumValue::operator == - Comparator /*{{{*/ // --------------------------------------------------------------------- /* Call memcmp on the buffer */ @@ -402,8 +398,6 @@ bool SHA256SumValue::operator == (const SHA256SumValue & rhs) const return memcmp(Sum,rhs.Sum,sizeof(Sum)) == 0; } /*}}}*/ - - // SHA256Summation::AddFD - Add content of file into the checksum /*{{{*/ // --------------------------------------------------------------------- /* */ |