summaryrefslogtreecommitdiffstats
path: root/src/include
diff options
context:
space:
mode:
authorPeng Wu <alexepico@gmail.com>2016-10-25 15:25:06 +0800
committerPeng Wu <alexepico@gmail.com>2016-10-25 15:25:06 +0800
commit17784a4a21e223d6d6a9ae77d6f1606584352ca8 (patch)
tree97862edabc64bd6c5d312fc84d5b75b00c73a2ff /src/include
parent226dd1d20e55e9b50f1d423a03651044ade13017 (diff)
downloadlibpinyin-17784a4a21e223d6d6a9ae77d6f1606584352ca8.tar.gz
libpinyin-17784a4a21e223d6d6a9ae77d6f1606584352ca8.tar.xz
libpinyin-17784a4a21e223d6d6a9ae77d6f1606584352ca8.zip
write get_check_sum method
Diffstat (limited to 'src/include')
-rw-r--r--src/include/memory_chunk.h30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/include/memory_chunk.h b/src/include/memory_chunk.h
index 49b54f6..e10c8cf 100644
--- a/src/include/memory_chunk.h
+++ b/src/include/memory_chunk.h
@@ -124,6 +124,36 @@ private:
m_allocated = m_data_begin + newsize;
return;
}
+
+ guint32 get_check_sum(const char * data, guint32 length){
+ guint32 checksum = 0x0;
+ guint32 aligns = length & ~0x3;
+
+ /* checksum for aligned parts. */
+ guint32 index = 0;
+ for (; index < aligns; index += sizeof(guint32)) {
+ const char * p = data + index;
+
+ /* use little endian here. */
+ guint32 item = *p | *(p + 1) << 8 |
+ *(p + 2) << 16 | *(p + 3) << 24;
+
+ checksum ^= item;
+ }
+
+ /* checksum for remained parts. */
+ guint32 shift = 0;
+ for (; index < length; index++) {
+ const char * p = data + index;
+
+ guint32 item = *p << shift;
+ shift += 8;
+
+ checksum ^= item;
+ }
+
+ return checksum;
+ }
public:
/**