Fix VS compilation warnings; cleanup API.

This commit is contained in:
Eugene Kliuchnikov 2016-06-14 13:42:47 +02:00
parent 2b4bdc4bcd
commit be1a53a61b
9 changed files with 44 additions and 66 deletions

View File

@ -122,7 +122,7 @@ static void StoreVarLenUint8(size_t n, size_t* storage_ix, uint8_t* storage) {
size_t nbits = Log2FloorNonZero(n);
BrotliWriteBits(1, 1, storage_ix, storage);
BrotliWriteBits(3, nbits, storage_ix, storage);
BrotliWriteBits(nbits, n - (1u << nbits), storage_ix, storage);
BrotliWriteBits(nbits, n - ((size_t)1 << nbits), storage_ix, storage);
}
}

View File

@ -194,7 +194,7 @@ static BROTLI_INLINE void EmitInsertLen(size_t insertlen,
const uint32_t nbits = Log2FloorNonZero(tail);
const size_t code = nbits + 50;
BrotliWriteBits(depth[code], bits[code], storage_ix, storage);
BrotliWriteBits(nbits, tail - (1u << nbits), storage_ix, storage);
BrotliWriteBits(nbits, tail - ((size_t)1 << nbits), storage_ix, storage);
++histo[code];
} else {
BrotliWriteBits(depth[61], bits[61], storage_ix, storage);
@ -243,7 +243,7 @@ static BROTLI_INLINE void EmitCopyLen(size_t copylen,
const uint32_t nbits = Log2FloorNonZero(tail);
const size_t code = nbits + 28;
BrotliWriteBits(depth[code], bits[code], storage_ix, storage);
BrotliWriteBits(nbits, tail - (1u << nbits), storage_ix, storage);
BrotliWriteBits(nbits, tail - ((size_t)1 << nbits), storage_ix, storage);
++histo[code];
} else {
BrotliWriteBits(depth[39], bits[39], storage_ix, storage);
@ -282,7 +282,7 @@ static BROTLI_INLINE void EmitCopyLenLastDistance(size_t copylen,
const uint32_t nbits = Log2FloorNonZero(tail);
const size_t code = nbits + 28;
BrotliWriteBits(depth[code], bits[code], storage_ix, storage);
BrotliWriteBits(nbits, tail - (1u << nbits), storage_ix, storage);
BrotliWriteBits(nbits, tail - ((size_t)1 << nbits), storage_ix, storage);
BrotliWriteBits(depth[64], bits[64], storage_ix, storage);
++histo[code];
++histo[64];

View File

@ -153,7 +153,7 @@ static BROTLI_INLINE void EmitCopyLen(size_t copylen, uint32_t** commands) {
const size_t tail = copylen - 70;
const size_t nbits = Log2FloorNonZero(tail);
const size_t code = nbits + 52;
const size_t extra = tail - (1u << nbits);
const size_t extra = tail - ((size_t)1 << nbits);
**commands = (uint32_t)(code | (extra << 8));
} else {
const size_t extra = copylen - 2118;
@ -187,7 +187,7 @@ static BROTLI_INLINE void EmitCopyLenLastDistance(
const size_t tail = copylen - 72;
const size_t nbits = Log2FloorNonZero(tail);
const size_t code = nbits + 52;
const size_t extra = tail - (1u << nbits);
const size_t extra = tail - ((size_t)1 << nbits);
**commands = (uint32_t)(code | (extra << 8));
++(*commands);
**commands = 64;

View File

@ -12,26 +12,28 @@
namespace brotli {
static void ConvertParams(const BrotliParams* from, BrotliEncoderParams* to) {
BrotliEncoderParamsSetDefault(to);
static void SetParams(const BrotliParams* from, BrotliEncoderState* to) {
BrotliEncoderMode mode = BROTLI_MODE_GENERIC;
if (from->mode == BrotliParams::MODE_TEXT) {
to->mode = BROTLI_MODE_TEXT;
mode = BROTLI_MODE_TEXT;
} else if (from->mode == BrotliParams::MODE_FONT) {
to->mode = BROTLI_MODE_FONT;
mode = BROTLI_MODE_FONT;
}
to->quality = from->quality;
to->lgwin = from->lgwin;
to->lgblock = from->lgblock;
BrotliEncoderSetParameter(to, BROTLI_PARAM_MODE, (uint32_t)mode);
BrotliEncoderSetParameter(to, BROTLI_PARAM_QUALITY, (uint32_t)from->quality);
BrotliEncoderSetParameter(to, BROTLI_PARAM_LGWIN, (uint32_t)from->lgwin);
BrotliEncoderSetParameter(to, BROTLI_PARAM_LGBLOCK, (uint32_t)from->lgblock);
}
BrotliCompressor::BrotliCompressor(BrotliParams params) {
BrotliEncoderParams encoder_params;
ConvertParams(&params, &encoder_params);
state_ = BrotliEncoderCreateState(&encoder_params, 0, 0, 0);
state_ = BrotliEncoderCreateInstance(0, 0, 0);
if (state_ == 0) std::exit(EXIT_FAILURE); /* OOM */
SetParams(&params, state_);
}
BrotliCompressor::~BrotliCompressor(void) { BrotliEncoderDestroyState(state_); }
BrotliCompressor::~BrotliCompressor(void) {
BrotliEncoderDestroyInstance(state_);
}
bool BrotliCompressor::WriteMetaBlock(const size_t input_size,
const uint8_t* input_buffer,
@ -95,12 +97,11 @@ int BrotliCompressWithCustomDictionary(size_t dictsize, const uint8_t* dict,
const uint8_t* next_in = NULL;
size_t total_out = 0;
bool end_of_input = false;
BrotliEncoderParams encoder_params;
BrotliEncoderState* s;
ConvertParams(&params, &encoder_params);
s = BrotliEncoderCreateState(&encoder_params, 0, 0, 0);
s = BrotliEncoderCreateInstance(0, 0, 0);
if (!s) return 0;
SetParams(&params, s);
BrotliEncoderSetCustomDictionary(s, dictsize, dict);
output_buffer = new uint8_t[kOutputBufferSize];
@ -130,7 +131,7 @@ int BrotliCompressWithCustomDictionary(size_t dictsize, const uint8_t* dict,
}
delete[] output_buffer;
BrotliEncoderDestroyState(s);
BrotliEncoderDestroyInstance(s);
return result ? 1 : 0;
}

View File

@ -42,7 +42,8 @@ static const size_t kMaxNumDelayedSymbols = 0x2fff;
#define COPY_ARRAY(dst, src) memcpy(dst, src, sizeof(src));
void BrotliEncoderParamsSetDefault(BrotliEncoderParams* self) {
static void BrotliEncoderParamsSetDefault(
BrotliEncoderParams* self) {
self->mode = BROTLI_DEFAULT_MODE;
self->quality = BROTLI_DEFAULT_QUALITY;
self->lgwin = BROTLI_DEFAULT_WINDOW;
@ -118,7 +119,7 @@ static int EnsureInitialized(BrotliEncoderState* s);
size_t BrotliEncoderInputBlockSize(BrotliEncoderState* s) {
if (!EnsureInitialized(s)) return 0;
return 1u << s->params_.lgblock;
return (size_t)1 << s->params_.lgblock;
}
static uint64_t UnprocessedInputSize(BrotliEncoderState* s) {
@ -761,7 +762,7 @@ void BrotliEncoderCopyInputToRingBuffer(BrotliEncoderState* s,
}
}
void BrotliEncoderSetCustomDictionary(BrotliEncoderState* s, const size_t size,
void BrotliEncoderSetCustomDictionary(BrotliEncoderState* s, size_t size,
const uint8_t* dict) {
size_t max_dict_size = MaxBackwardLimit(s->params_.lgwin);
size_t dict_size = size;
@ -894,7 +895,8 @@ int BrotliEncoderWriteData(BrotliEncoderState* s, const int is_last,
&s->num_literals_);
if (BROTLI_IS_OOM(m)) return 0;
max_length = BROTLI_MIN(size_t, mask + 1, 1u << kBrotliMaxInputBlockBits);
max_length =
BROTLI_MIN(size_t, mask + 1, (size_t)1 << kBrotliMaxInputBlockBits);
{
const size_t max_literals = max_length / 8;
const size_t max_commands = max_length / 8;
@ -1291,7 +1293,6 @@ int BrotliEncoderCompress(int quality, int lgwin, BrotliEncoderMode mode,
size_t* encoded_size,
uint8_t* encoded_buffer) {
BrotliEncoderState* s;
BrotliEncoderParams params;
size_t out_size = *encoded_size;
const uint8_t* input_start = input_buffer;
uint8_t* output_start = encoded_buffer;
@ -1317,11 +1318,7 @@ int BrotliEncoderCompress(int quality, int lgwin, BrotliEncoderMode mode,
return 1;
}
BrotliEncoderParamsSetDefault(&params);
params.quality = quality;
params.lgwin = lgwin;
params.mode = mode;
s = BrotliEncoderCreateState(&params, 0, 0, 0);
s = BrotliEncoderCreateInstance(0, 0, 0);
if (!s) {
return 0;
} else {
@ -1330,11 +1327,15 @@ int BrotliEncoderCompress(int quality, int lgwin, BrotliEncoderMode mode,
size_t available_out = *encoded_size;
uint8_t* next_out = encoded_buffer;
size_t total_out = 0;
int result = BrotliEncoderCompressStream(s, BROTLI_OPERATION_FINISH,
int result = 0;
BrotliEncoderSetParameter(s, BROTLI_PARAM_QUALITY, (uint32_t)quality);
BrotliEncoderSetParameter(s, BROTLI_PARAM_LGWIN, (uint32_t)lgwin);
BrotliEncoderSetParameter(s, BROTLI_PARAM_MODE, (uint32_t)mode);
result = BrotliEncoderCompressStream(s, BROTLI_OPERATION_FINISH,
&available_in, &next_in, &available_out, &next_out, &total_out);
if (!BrotliEncoderIsFinished(s)) result = 0;
*encoded_size = total_out;
BrotliEncoderDestroyState(s);
BrotliEncoderDestroyInstance(s);
if (!result || (max_out_size && *encoded_size > max_out_size)) {
goto fallback;
}
@ -1369,7 +1370,7 @@ static int BrotliEncoderCompressStreamFast(
BrotliEncoderState* s, BrotliEncoderOperation op, size_t* available_in,
const uint8_t** next_in, size_t* available_out, uint8_t** next_out,
size_t* total_out) {
const size_t block_size_limit = 1u << s->params_.lgwin;
const size_t block_size_limit = (size_t)1 << s->params_.lgwin;
const size_t buf_size = BROTLI_MIN(size_t, kCompressFragmentTwoPassBlockSize,
BROTLI_MIN(size_t, *available_in, block_size_limit));
uint32_t* tmp_command_buf = NULL;

View File

@ -69,9 +69,6 @@ typedef enum BrotliEncoderParameter {
BROTLI_PARAM_LGBLOCK = 3
} BrotliEncoderParameter;
/* DEPRECATED */
void BrotliEncoderParamsSetDefault(BrotliEncoderParams* params);
/* A state can not be reused for multiple brotli streams. */
typedef struct BrotliEncoderStateStruct BrotliEncoderState;
@ -85,30 +82,9 @@ int BrotliEncoderSetParameter(
BrotliEncoderState* BrotliEncoderCreateInstance(brotli_alloc_func alloc_func,
brotli_free_func free_func,
void* opaque);
/* DEPRECATED */
static inline BrotliEncoderState* BrotliEncoderCreateState(
const BrotliEncoderParams* params, brotli_alloc_func alloc_func,
brotli_free_func free_func, void* opaque) {
BrotliEncoderState* result = BrotliEncoderCreateInstance(
alloc_func, free_func, opaque);
if (!result) return result;
BrotliEncoderSetParameter(
result, BROTLI_PARAM_MODE, (uint32_t)params->mode);
BrotliEncoderSetParameter(
result, BROTLI_PARAM_QUALITY, (uint32_t)params->quality);
BrotliEncoderSetParameter(
result, BROTLI_PARAM_LGWIN, (uint32_t)params->lgwin);
BrotliEncoderSetParameter(
result, BROTLI_PARAM_LGBLOCK, (uint32_t)params->lgblock);
return result;
}
/* Deinitializes and frees BrotliEncoderState instance. */
void BrotliEncoderDestroyInstance(BrotliEncoderState* state);
/* DEPRECATED */
static inline void BrotliEncoderDestroyState(BrotliEncoderState* state) {
BrotliEncoderDestroyInstance(state);
}
/* The maximum input size that can be processed at once. */
size_t BrotliEncoderInputBlockSize(BrotliEncoderState* state);

View File

@ -42,8 +42,8 @@ int BrotliSetDepth(int p0, HuffmanTree* pool, uint8_t* depth, int max_depth) {
}
/* Sort the root nodes, least popular first. */
static inline int SortHuffmanTree(const HuffmanTree* v0,
const HuffmanTree* v1) {
static BROTLI_INLINE int SortHuffmanTree(const HuffmanTree* v0,
const HuffmanTree* v1) {
if (v0->total_count_ != v1->total_count_) {
return (v0->total_count_ < v1->total_count_) ? 1 : 0;
}
@ -463,7 +463,7 @@ static uint16_t BrotliReverseBits(size_t num_bits, uint16_t bits) {
bits = (uint16_t)(bits >> 4);
retval |= kLut[bits & 0xf];
}
retval >>= (-num_bits & 0x3);
retval >>= ((0 - num_bits) & 0x3);
return (uint16_t)retval;
}

View File

@ -11,7 +11,7 @@
#include <assert.h>
#include <stdlib.h> /* exit, free, malloc */
#include <strings.h> /* memcpy */
#include <string.h> /* memcpy */
#include "../common/types.h"
#include "./port.h"

View File

@ -90,7 +90,7 @@ int BrotliFindAllStaticDictionaryMatches(const uint8_t* data,
for (i = 0; i < num; ++i) {
const DictWord w = kStaticDictionaryWords[offset + i];
const size_t l = w.len;
const size_t n = 1u << kBrotliDictionarySizeBitsByLength[l];
const size_t n = (size_t)1 << kBrotliDictionarySizeBitsByLength[l];
const size_t id = w.idx;
if (w.transform == 0) {
const size_t matchlen = DictMatchLength(data, id, l, max_length);
@ -329,7 +329,7 @@ int BrotliFindAllStaticDictionaryMatches(const uint8_t* data,
for (i = 0; i < num; ++i) {
const DictWord w = kStaticDictionaryWords[offset + i];
const size_t l = w.len;
const size_t n = 1u << kBrotliDictionarySizeBitsByLength[l];
const size_t n = (size_t)1 << kBrotliDictionarySizeBitsByLength[l];
const size_t id = w.idx;
if (w.transform == 0) {
const uint8_t* s;
@ -421,7 +421,7 @@ int BrotliFindAllStaticDictionaryMatches(const uint8_t* data,
for (i = 0; i < num; ++i) {
const DictWord w = kStaticDictionaryWords[offset + i];
const size_t l = w.len;
const size_t n = 1u << kBrotliDictionarySizeBitsByLength[l];
const size_t n = (size_t)1 << kBrotliDictionarySizeBitsByLength[l];
const size_t id = w.idx;
if (w.transform == 0 && IsMatch(w, &data[2], max_length - 2)) {
if (data[0] == 0xc2) {
@ -450,7 +450,7 @@ int BrotliFindAllStaticDictionaryMatches(const uint8_t* data,
for (i = 0; i < num; ++i) {
const DictWord w = kStaticDictionaryWords[offset + i];
const size_t l = w.len;
const size_t n = 1u << kBrotliDictionarySizeBitsByLength[l];
const size_t n = (size_t)1 << kBrotliDictionarySizeBitsByLength[l];
const size_t id = w.idx;
if (w.transform == 0 && IsMatch(w, &data[5], max_length - 5)) {
AddMatch(id + (data[0] == ' ' ? 41 : 72) * n, l + 5, l, matches);