From 46a1253d7805a443b731a7251dd528d05258a6d2 Mon Sep 17 00:00:00 2001 From: upstream svn Date: Mon, 8 Feb 2010 20:51:54 +0000 Subject: [PATCH] Upstream tarball 10011 --- .svn-revision | 2 +- src/ExternalConn.cpp | 26 ++++++++++++++++++-------- src/RLE.cpp | 51 ++++++++++++++++++++++++--------------------------- src/RLE.h | 14 ++++++-------- 4 files changed, 49 insertions(+), 44 deletions(-) diff --git a/.svn-revision b/.svn-revision index db6b8511..a46a2b58 100644 --- a/.svn-revision +++ b/.svn-revision @@ -1 +1 @@ -10010 +10011 diff --git a/src/ExternalConn.cpp b/src/ExternalConn.cpp index d514e75a..498664d0 100644 --- a/src/ExternalConn.cpp +++ b/src/ExternalConn.cpp @@ -921,8 +921,12 @@ void CPartFile_Encoder::Encode(CECTag *parent) // if (!m_file->m_SrcpartFrequency.empty()) { int part_enc_size; - const uint8 *part_enc_data = m_enc_data.m_part_status.Encode(m_file->m_SrcpartFrequency, part_enc_size); - parent->AddTag(CECTag(EC_TAG_PARTFILE_PART_STATUS, part_enc_size, part_enc_data)); + bool changed; + const uint8 *part_enc_data = m_enc_data.m_part_status.Encode(m_file->m_SrcpartFrequency, part_enc_size, changed); + if (changed) { + parent->AddTag(CECTag(EC_TAG_PARTFILE_PART_STATUS, part_enc_size, part_enc_data)); + } + delete[] part_enc_data; } // @@ -940,10 +944,12 @@ void CPartFile_Encoder::Encode(CECTag *parent) } int gap_enc_size = 0; - const uint8 *gap_enc_data = m_enc_data.m_gap_status.Encode(gaps, gap_enc_size); - - CECTag etag(EC_TAG_PARTFILE_GAP_STATUS, gap_enc_size, (void *)gap_enc_data); - parent->AddTag(etag); + bool changed; + const uint8 *gap_enc_data = m_enc_data.m_gap_status.Encode(gaps, gap_enc_size, changed); + if (changed) { + parent->AddTag(CECTag(EC_TAG_PARTFILE_GAP_STATUS, gap_enc_size, (void *)gap_enc_data)); + } + delete[] gap_enc_data; // // Requested blocks @@ -968,8 +974,12 @@ void CKnownFile_Encoder::Encode(CECTag *parent) // Don't add tag if available parts aren't populated yet. if (!m_file->m_AvailPartFrequency.empty()) { int part_enc_size; - const uint8 *part_enc_data = m_enc_data.Encode(m_file->m_AvailPartFrequency, part_enc_size); - parent->AddTag(CECTag(EC_TAG_PARTFILE_PART_STATUS, part_enc_size, part_enc_data)); + bool changed; + const uint8 *part_enc_data = m_enc_data.Encode(m_file->m_AvailPartFrequency, part_enc_size, changed); + if (changed) { + parent->AddTag(CECTag(EC_TAG_PARTFILE_PART_STATUS, part_enc_size, part_enc_data)); + } + delete[] part_enc_data; } } diff --git a/src/RLE.cpp b/src/RLE.cpp index 0fa2d7f3..5478fb62 100644 --- a/src/RLE.cpp +++ b/src/RLE.cpp @@ -49,10 +49,8 @@ void RLE_Data::setup(int len, bool use_diff, uint8 * content) memset(m_buff, 0, m_len); } // - // in worst case 2-byte sequence encoded as 3. So, data can grow by 50% - m_enc_buff = new uint8[m_len*3/2 + 1]; } else { - m_buff = m_enc_buff = 0; + m_buff = 0; } } @@ -62,7 +60,6 @@ RLE_Data &RLE_Data::operator=(const RLE_Data &obj) return *this; delete [] m_buff; - delete [] m_enc_buff; setup(obj.m_len, obj.m_use_diff, obj.m_buff); return *this; @@ -71,21 +68,18 @@ RLE_Data &RLE_Data::operator=(const RLE_Data &obj) RLE_Data::~RLE_Data() { delete [] m_buff; - delete [] m_enc_buff; } -void RLE_Data::Realloc(int size) +bool RLE_Data::Realloc(int size) { if ( size == m_len ) { - return; + return false; } if (size == 0) { delete [] m_buff; - delete [] m_enc_buff; m_buff = 0; - m_enc_buff = 0; m_len = 0; - return; + return true; } uint8 *buff = new uint8[size]; if (m_len == 0) { @@ -99,11 +93,8 @@ void RLE_Data::Realloc(int size) delete [] m_buff; m_buff = buff; - // m_enc_buff doesn't need to be copied over - delete [] m_enc_buff; - m_enc_buff = new uint8[size*3/2 + 1]; - m_len = size; + return true; } const uint8 *RLE_Data::Decode(const uint8 *buff, int len) @@ -156,9 +147,9 @@ const uint8 *RLE_Data::Decode(const uint8 *buff, int len) return m_buff; } -const uint8 * RLE_Data::Encode(const uint8 *data, int inlen, int &outlen) +const uint8 * RLE_Data::Encode(const uint8 *data, int inlen, int &outlen, bool &changed) { - Realloc(inlen); // adjust size if necessary + changed = Realloc(inlen); // adjust size if necessary if (m_len == 0) { outlen = 0; @@ -170,14 +161,20 @@ const uint8 * RLE_Data::Encode(const uint8 *data, int inlen, int &outlen) if ( m_use_diff ) { for (int i = 0; i < m_len; i++) { m_buff[i] ^= data[i]; + if (m_buff[i]) { + changed = true; + } } } else { memcpy(m_buff, data, m_len); + changed = true; } // // now RLE // + // In worst case 2-byte sequence is encoded as 3. So, data can grow by 50%. + uint8 * enc_buff = new uint8[m_len * 3/2 + 1]; int i = 0, j = 0; while ( i != m_len ) { uint8 curr_val = m_buff[i]; @@ -187,12 +184,12 @@ const uint8 * RLE_Data::Encode(const uint8 *data, int inlen, int &outlen) } if (i - seq_start > 1) { // if there's 2 or more equal vals - put it twice in stream - m_enc_buff[j++] = curr_val; - m_enc_buff[j++] = curr_val; - m_enc_buff[j++] = i - seq_start; + enc_buff[j++] = curr_val; + enc_buff[j++] = curr_val; + enc_buff[j++] = i - seq_start; } else { // single value - put it as is - m_enc_buff[j++] = curr_val; + enc_buff[j++] = curr_val; } } @@ -205,17 +202,17 @@ const uint8 * RLE_Data::Encode(const uint8 *data, int inlen, int &outlen) memcpy(m_buff, data, m_len); } - return m_enc_buff; + return enc_buff; } -const uint8 * RLE_Data::Encode(const ArrayOfUInts16 &data, int &outlen) +const uint8 * RLE_Data::Encode(const ArrayOfUInts16 &data, int &outlen, bool &changed) { // To encode, first copy the UInts16 to a uint8 array // and limit them to 0xff. // The encoded size is the size of data. int size = (int) data.size(); if (size == 0) { - return Encode(0, 0, outlen); + return Encode(0, 0, outlen, changed); } CScopedPtr buf(new uint8[size]); uint8 * bufPtr = buf.get(); @@ -224,10 +221,10 @@ const uint8 * RLE_Data::Encode(const ArrayOfUInts16 &data, int &outlen) uint16 ui = data[i]; bufPtr[i] = (ui > 0xff) ? 0xff : (uint8) ui; } - return Encode(bufPtr, size, outlen); + return Encode(bufPtr, size, outlen, changed); } -const uint8 * RLE_Data::Encode(const ArrayOfUInts64 &data, int &outlen) +const uint8 * RLE_Data::Encode(const ArrayOfUInts64 &data, int &outlen, bool &changed) { // uint64 is copied to a uint8 buffer // first all low bytes, then all second low bytes and so on @@ -236,7 +233,7 @@ const uint8 * RLE_Data::Encode(const ArrayOfUInts64 &data, int &outlen) // 8A8B7A7B6A6B5A5B0404030300000000 int size = (int) data.size(); if (size == 0) { - return Encode(0, 0, outlen); + return Encode(0, 0, outlen, changed); } CScopedPtr buf(new uint8[size * 8]); uint8 * bufPtr = buf.get(); @@ -247,7 +244,7 @@ const uint8 * RLE_Data::Encode(const ArrayOfUInts64 &data, int &outlen) u >>= 8; } } - return Encode(bufPtr, size * 8, outlen); + return Encode(bufPtr, size * 8, outlen, changed); } void RLE_Data::Decode(const uint8 *data, int len, ArrayOfUInts64 &outdata) diff --git a/src/RLE.h b/src/RLE.h index 372d3781..27278318 100644 --- a/src/RLE.h +++ b/src/RLE.h @@ -43,10 +43,10 @@ public: ~RLE_Data(); - const uint8 *Encode(const uint8 *data, int inlen, int &outlen); + const uint8 *Encode(const uint8 *data, int inlen, int &outlen, bool &changed); - const uint8 *Encode(const ArrayOfUInts16 &data, int &outlen); - const uint8 *Encode(const ArrayOfUInts64 &data, int &outlen); + const uint8 *Encode(const ArrayOfUInts16 &data, int &outlen, bool &changed); + const uint8 *Encode(const ArrayOfUInts64 &data, int &outlen, bool &changed); const uint8 *Decode(const uint8 *data, int len); void Decode(const uint8 *data, int len, ArrayOfUInts64 &outdata); @@ -66,14 +66,12 @@ private: void setup(int len, bool use_diff, uint8 * content = 0); // change size of internal buffers - void Realloc(int size); + // returns true if size was changed + bool Realloc(int size); // Encode: source data (original or diff in diff mode) - // Decode: store last data (differential only) + // Decode: store decoded data uint8 *m_buff; - // Encode: stores packed data - // Decode: input gets unpacked here - uint8 *m_enc_buff; // Unpacked size int m_len; // Use differential encoding -- 2.11.4.GIT