0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
BlockCompressionCodecNone.cc
Go to the documentation of this file.
1 /* -*- c++ -*-
2  * Copyright (C) 2007-2015 Hypertable, Inc.
3  *
4  * This file is part of Hypertable.
5  *
6  * Hypertable is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; version 3 of the
9  * License, or any later version.
10  *
11  * Hypertable is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA.
20  */
21 
26 
27 #include <Common/Compat.h>
28 
30 
31 #include <Common/Checksum.h>
32 #include <Common/DynamicBuffer.h>
33 #include <Common/Error.h>
34 #include <Common/Logger.h>
35 
36 using namespace Hypertable;
37 
38 void
40  DynamicBuffer &output, BlockHeader &header, size_t reserve) {
41  output.clear();
42  output.reserve(header.encoded_length() + input.fill() + reserve);
43 
44  header.set_compression_type(NONE);
45  memcpy(output.base+header.encoded_length(), input.base, input.fill());
46  header.set_data_length(input.fill());
47  header.set_data_zlength(input.fill());
48  header.set_data_checksum(fletcher32(output.base + header.encoded_length(),
49  header.get_data_zlength()));
50 
51  output.ptr = output.base;
52  header.encode(&output.ptr);
53  output.ptr += header.get_data_zlength();
54 }
55 
56 
57 void
59  DynamicBuffer &output, BlockHeader &header) {
60 
61  const uint8_t *msg_ptr = input.base;
62  size_t remaining = input.fill();
63 
64  header.decode(&msg_ptr, &remaining);
65 
66  if (header.get_data_zlength() > remaining)
67  HT_THROWF(Error::BLOCK_COMPRESSOR_BAD_HEADER, "Block decompression error, "
68  "header zlength = %lu, actual = %lu",
69  (Lu)header.get_data_zlength(), (Lu)remaining);
70 
71  uint32_t checksum = fletcher32(msg_ptr, header.get_data_zlength());
72  if (checksum != header.get_data_checksum())
74  "checksum mismatch header=%lx, computed=%lx",
75  (Lu)header.get_data_checksum(), (Lu)checksum);
76 
77  output.reserve(header.get_data_length());
78 
79  memcpy(output.base, msg_ptr, header.get_data_length());
80  output.ptr = output.base + header.get_data_length();
81 }
void set_data_length(uint32_t length)
Sets the uncompressed data length field.
Definition: BlockHeader.h:91
void set_data_checksum(uint32_t checksum)
Sets the checksum field.
Definition: BlockHeader.h:113
void set_data_zlength(uint32_t zlength)
Sets the compressed data length field.
Definition: BlockHeader.h:101
uint8_t * ptr
Pointer to the end of the used part of the buffer.
A dynamic, resizable and reference counted memory buffer.
Definition: DynamicBuffer.h:42
uint32_t get_data_checksum()
Gets the checksum field.
Definition: BlockHeader.h:118
uint32_t fletcher32(const void *data8, size_t len8)
Compute fletcher32 checksum for arbitary data.
Definition: Checksum.cc:42
uint32_t get_data_length()
Gets the uncompressed data length field.
Definition: BlockHeader.h:96
A dynamic, resizable memory buffer.
Logging routines and macros.
Compatibility Macros for C/C++.
virtual void deflate(const DynamicBuffer &input, DynamicBuffer &output, BlockHeader &header, size_t reserve=0)
Transforms a buffer into a serialized block using no compression.
virtual void decode(const uint8_t **bufp, size_t *remainp)
Decodes serialized block header.
Definition: BlockHeader.cc:104
Hypertable definitions
Implementation of checksum routines.
void clear()
Clears the buffer.
Declarations for BlockCompressionCodecNone.
void set_compression_type(uint16_t type)
Sets the compression type field.
Definition: BlockHeader.h:123
virtual void inflate(const DynamicBuffer &input, DynamicBuffer &output, BlockHeader &header)
Deserializes a block produced by deflate().
#define HT_THROWF(_code_, _fmt_,...)
Definition: Error.h:490
uint8_t * base
Pointer to the allocated memory buffer.
size_t fill() const
Returns the size of the used portion.
Definition: DynamicBuffer.h:70
virtual size_t encoded_length()
Returns length of serizlized block header.
Definition: BlockHeader.cc:76
long unsigned int Lu
Shortcut for printf formats.
Definition: String.h:47
uint32_t get_data_zlength()
Gets the compressed data length field.
Definition: BlockHeader.h:106
Base class for block headers.
Definition: BlockHeader.h:48
Error codes, Exception handling, error logging.
void reserve(size_t len, bool nocopy=false)
Reserve space for additional data Will grow the space to exactly what's needed.
Definition: DynamicBuffer.h:95
virtual void encode(uint8_t **bufp)
Encodes serialized representation of block header.
Definition: BlockHeader.cc:82