提交 af034d37 编写于 作者: H hjchen2

Merge branch 'dev-latest' of https://github.com/hjchen2/paddle-mobile into dev-latest

...@@ -13,8 +13,8 @@ See the License for the specific language governing permissions and ...@@ -13,8 +13,8 @@ See the License for the specific language governing permissions and
limitations under the License. */ limitations under the License. */
#include "io/executor.h" #include "io/executor.h"
#include <operators/math/gemm.h>
#include <algorithm> #include <algorithm>
#include <utility>
#include <vector> #include <vector>
#include "common/enforce.h" #include "common/enforce.h"
#include "common/log.h" #include "common/log.h"
...@@ -26,7 +26,7 @@ limitations under the License. */ ...@@ -26,7 +26,7 @@ limitations under the License. */
#include "framework/program/var_desc.h" #include "framework/program/var_desc.h"
#include "framework/scope.h" #include "framework/scope.h"
#include "framework/tensor.h" #include "framework/tensor.h"
#include "operators/math/gemm.h"
namespace paddle_mobile { namespace paddle_mobile {
...@@ -34,8 +34,7 @@ using framework::Variable; ...@@ -34,8 +34,7 @@ using framework::Variable;
template <typename Dtype, Precision P> template <typename Dtype, Precision P>
Executor<Dtype, P>::Executor(const framework::Program<Dtype> p, Executor<Dtype, P>::Executor(const framework::Program<Dtype> p,
const bool use_optimize, const bool use_optimize, const bool loddable)
const bool loddable)
: program_(p), use_optimize_(use_optimize), loddable_(loddable) { : program_(p), use_optimize_(use_optimize), loddable_(loddable) {
Variable *variable_ptr = program_.scope->Var("batch_size"); Variable *variable_ptr = program_.scope->Var("batch_size");
variable_ptr->SetValue<int>(1); variable_ptr->SetValue<int>(1);
...@@ -77,20 +76,20 @@ Executor<Dtype, P>::Executor(const framework::Program<Dtype> p, ...@@ -77,20 +76,20 @@ Executor<Dtype, P>::Executor(const framework::Program<Dtype> p,
} }
} }
template<typename Dtype> template <typename Dtype>
void LoadMemInternal(void **data, framework::LoDTensor *tensor) { void LoadMemInternal(void **data, framework::LoDTensor *tensor) {
char **data_buf = reinterpret_cast<char **>(data); char **data_buf = reinterpret_cast<char **>(data);
int64_t size = tensor->numel(); int64_t size = tensor->numel();
Dtype* tensor_data = tensor->mutable_data<Dtype>(); Dtype *tensor_data = tensor->mutable_data<Dtype>();
if (0) { if (0) {
// TODO should be moved into operator init function // TODO(hjchen2) should be moved into operator init function
float min_value; float min_value;
float max_value; float max_value;
memcpy(&min_value, data_buf, sizeof(float)); memcpy(&min_value, data_buf, sizeof(float));
memcpy(&max_value, data_buf + sizeof(float), sizeof(float)); memcpy(&max_value, data_buf + sizeof(float), sizeof(float));
data_buf += 2 * sizeof(float); data_buf += 2 * sizeof(float);
const float factor = (max_value - min_value) / 255.0; const float factor = (max_value - min_value) / 255.0;
const uint8_t *uint8_data = reinterpret_cast<uint8_t*>(data_buf); const uint8_t *uint8_data = reinterpret_cast<uint8_t *>(data_buf);
for (int k = 0; k < size; ++k) { for (int k = 0; k < size; ++k) {
tensor_data[k] = uint8_data[k] * factor + min_value; tensor_data[k] = uint8_data[k] * factor + min_value;
} }
...@@ -103,21 +102,20 @@ void LoadMemInternal(void **data, framework::LoDTensor *tensor) { ...@@ -103,21 +102,20 @@ void LoadMemInternal(void **data, framework::LoDTensor *tensor) {
template <typename Dtype, Precision P> template <typename Dtype, Precision P>
void Executor<Dtype, P>::LoadMemory( void Executor<Dtype, P>::LoadMemory(
void **data, void **data, const std::shared_ptr<framework::VarDesc> var_desc,
const std::shared_ptr<framework::VarDesc> var_desc,
framework::LoDTensor *tensor) { framework::LoDTensor *tensor) {
char **data_buf = reinterpret_cast<char**>(data); char **data_buf = reinterpret_cast<char **>(data);
// version // version
uint32_t version = *(reinterpret_cast<uint32_t*>(*data_buf)); uint32_t version = *(reinterpret_cast<uint32_t *>(*data_buf));
*data_buf += sizeof(uint32_t); *data_buf += sizeof(uint32_t);
// lod information // lod information
uint64_t lod_level = *(reinterpret_cast<uint64_t*>(*data_buf)); uint64_t lod_level = *(reinterpret_cast<uint64_t *>(*data_buf));
*data_buf += sizeof(uint64_t); *data_buf += sizeof(uint64_t);
auto *lod = tensor->mutable_lod(); auto *lod = tensor->mutable_lod();
lod->resize(lod_level); lod->resize(lod_level);
for (uint64_t i = 0; i < lod_level; ++i) { for (uint64_t i = 0; i < lod_level; ++i) {
uint64_t size = *(reinterpret_cast<uint64_t*>(*data_buf)); uint64_t size = *(reinterpret_cast<uint64_t *>(*data_buf));
*data_buf += sizeof(uint64_t); *data_buf += sizeof(uint64_t);
std::vector<size_t> tmp_dim(size / sizeof(size_t)); std::vector<size_t> tmp_dim(size / sizeof(size_t));
memcpy(tmp_dim.data(), *data_buf, size); memcpy(tmp_dim.data(), *data_buf, size);
...@@ -125,10 +123,10 @@ void Executor<Dtype, P>::LoadMemory( ...@@ -125,10 +123,10 @@ void Executor<Dtype, P>::LoadMemory(
*data_buf += size; *data_buf += size;
} }
// tensor version // tensor version
uint32_t tensor_version = *(reinterpret_cast<uint32_t*>(*data_buf)); uint32_t tensor_version = *(reinterpret_cast<uint32_t *>(*data_buf));
*data_buf += sizeof(uint32_t); *data_buf += sizeof(uint32_t);
// tensor desc size // tensor desc size
int32_t tensor_desc_size = *(reinterpret_cast<int32_t*>(*data_buf)); int32_t tensor_desc_size = *(reinterpret_cast<int32_t *>(*data_buf));
*data_buf += sizeof(int32_t); *data_buf += sizeof(int32_t);
// skip tensor desc // skip tensor desc
*data_buf += tensor_desc_size; *data_buf += tensor_desc_size;
...@@ -138,13 +136,13 @@ void Executor<Dtype, P>::LoadMemory( ...@@ -138,13 +136,13 @@ void Executor<Dtype, P>::LoadMemory(
// parse tensor from stream // parse tensor from stream
switch (tensor_desc.DataType()) { switch (tensor_desc.DataType()) {
case framework::VARTYPE_TYPE_FP32: case framework::VARTYPE_TYPE_FP32:
LoadMemInternal<float>((void**)data_buf, tensor); LoadMemInternal<float>(reinterpret_cast<void **>(data_buf), tensor);
break; break;
case framework::VARTYPE_TYPE_INT8: case framework::VARTYPE_TYPE_INT8:
LoadMemInternal<int8_t>((void**)data_buf, tensor); LoadMemInternal<int8_t>(reinterpret_cast<void **>(data_buf), tensor);
break; break;
case framework::VARTYPE_TYPE_INT32: case framework::VARTYPE_TYPE_INT32:
LoadMemInternal<int>((void**)data_buf, tensor); LoadMemInternal<int>(reinterpret_cast<void **>(data_buf), tensor);
break; break;
default: default:
LOG(kLOG_ERROR) << "data type is not supported"; LOG(kLOG_ERROR) << "data type is not supported";
...@@ -164,8 +162,8 @@ void Executor<Dtype, P>::InitMemory() { ...@@ -164,8 +162,8 @@ void Executor<Dtype, P>::InitMemory() {
char *origin_data = char *origin_data =
ReadFileToBuff(program_.model_path + "/" + var_desc->Name()); ReadFileToBuff(program_.model_path + "/" + var_desc->Name());
char *data = origin_data; char *data = origin_data;
LoadMemory((void**)&data, var_desc, tensor); LoadMemory(reinterpret_cast<void **>(&data), var_desc, tensor);
delete [] origin_data; delete[] origin_data;
} else { } else {
if (var_desc->Type() == framework::VARTYPE_TYPE_LOD_TENSOR) { if (var_desc->Type() == framework::VARTYPE_TYPE_LOD_TENSOR) {
varInputMemory(var_desc, var, tensor); varInputMemory(var_desc, var, tensor);
...@@ -180,7 +178,8 @@ void Executor<Dtype, P>::InitCombineMemory() { ...@@ -180,7 +178,8 @@ void Executor<Dtype, P>::InitCombineMemory() {
char *origin_data = nullptr; char *origin_data = nullptr;
bool self_alloc = false; bool self_alloc = false;
if (program_.combined_params_buf && program_.combined_params_len) { if (program_.combined_params_buf && program_.combined_params_len) {
origin_data = (char *)program_.combined_params_buf; origin_data = reinterpret_cast<char *>(
const_cast<uint8_t *>(program_.combined_params_buf));
} else { } else {
self_alloc = true; self_alloc = true;
origin_data = ReadFileToBuff(program_.para_path); origin_data = ReadFileToBuff(program_.para_path);
...@@ -195,7 +194,7 @@ void Executor<Dtype, P>::InitCombineMemory() { ...@@ -195,7 +194,7 @@ void Executor<Dtype, P>::InitCombineMemory() {
if (var_desc->Name() == "feed" || var_desc->Name() == "fetch") { if (var_desc->Name() == "feed" || var_desc->Name() == "fetch") {
continue; continue;
} }
LoadMemory((void**)&data, var_desc, tensor); LoadMemory(reinterpret_cast<void **>(&data), var_desc, tensor);
} else { } else {
if (var_desc->Type() == framework::VARTYPE_TYPE_LOD_TENSOR) { if (var_desc->Type() == framework::VARTYPE_TYPE_LOD_TENSOR) {
varInputMemory(var_desc, var, tensor); varInputMemory(var_desc, var, tensor);
...@@ -204,7 +203,7 @@ void Executor<Dtype, P>::InitCombineMemory() { ...@@ -204,7 +203,7 @@ void Executor<Dtype, P>::InitCombineMemory() {
} }
} }
if (self_alloc) { if (self_alloc) {
delete [] origin_data; delete[] origin_data;
} }
LOG(kLOG_INFO) << "init combine memory finish"; LOG(kLOG_INFO) << "init combine memory finish";
} }
...@@ -402,12 +401,12 @@ void Executor<Dtype, P>::InjectVariable(const framework::Tensor &t, ...@@ -402,12 +401,12 @@ void Executor<Dtype, P>::InjectVariable(const framework::Tensor &t,
g_feed_value->GetMutable<framework::LoDTensor>(); g_feed_value->GetMutable<framework::LoDTensor>();
feed_tensor->Resize(t.dims()); feed_tensor->Resize(t.dims());
feed_tensor->ShareDataWith(t); feed_tensor->ShareDataWith(t);
}; }
template <typename Dtype, Precision P> template <typename Dtype, Precision P>
void Executor<Dtype, P>::FeedData(const framework::Tensor &t) { void Executor<Dtype, P>::FeedData(const framework::Tensor &t) {
InjectVariable(t, "feed"); InjectVariable(t, "feed");
}; }
template <typename Dtype, Precision P> template <typename Dtype, Precision P>
std::shared_ptr<framework::Tensor> Executor<Dtype, P>::FetchResult(int id) { std::shared_ptr<framework::Tensor> Executor<Dtype, P>::FetchResult(int id) {
...@@ -423,14 +422,14 @@ std::shared_ptr<framework::Tensor> Executor<Dtype, P>::FetchResult(int id) { ...@@ -423,14 +422,14 @@ std::shared_ptr<framework::Tensor> Executor<Dtype, P>::FetchResult(int id) {
auto *output_tensor = framework::GetVarValue<framework::LoDTensor>( auto *output_tensor = framework::GetVarValue<framework::LoDTensor>(
out_keys[0], output_map, *(program_.scope)); out_keys[0], output_map, *(program_.scope));
return std::make_shared<framework::Tensor>(framework::Tensor(*output_tensor)); return std::make_shared<framework::Tensor>(framework::Tensor(*output_tensor));
}; }
template <typename Dtype, Precision P> template <typename Dtype, Precision P>
void Executor<Dtype, P>::Predict_From_To(int start, int end) { void Executor<Dtype, P>::Predict_From_To(int start, int end) {
std::shared_ptr<framework::BlockDesc> to_predict_block = std::shared_ptr<framework::BlockDesc> to_predict_block =
to_predict_program_->Block(0); to_predict_program_->Block(0);
auto &ops = ops_of_block_[*to_predict_block.get()]; auto &ops = ops_of_block_[*to_predict_block.get()];
end = end < 0 ? (int)ops.size() : end; end = end < 0 ? static_cast<int>(ops.size()) : end;
PADDLE_MOBILE_ENFORCE(start >= 0 && start < end && end <= ops.size(), PADDLE_MOBILE_ENFORCE(start >= 0 && start < end && end <= ops.size(),
"start or end parameter is wrong"); "start or end parameter is wrong");
...@@ -451,17 +450,17 @@ void Executor<Dtype, P>::Predict_From_To(int start, int end) { ...@@ -451,17 +450,17 @@ void Executor<Dtype, P>::Predict_From_To(int start, int end) {
profile[i].runEnd = (uint64_t)ts.tv_sec * 1e9 + ts.tv_nsec; profile[i].runEnd = (uint64_t)ts.tv_sec * 1e9 + ts.tv_nsec;
#endif #endif
} }
}; }
template <typename Dtype, Precision P> template <typename Dtype, Precision P>
void Executor<Dtype, P>::Predict_From(int start) { void Executor<Dtype, P>::Predict_From(int start) {
Predict_From_To(start); Predict_From_To(start);
}; }
template <typename Dtype, Precision P> template <typename Dtype, Precision P>
void Executor<Dtype, P>::Predict_To(int end) { void Executor<Dtype, P>::Predict_To(int end) {
Predict_From_To(0, end); Predict_From_To(0, end);
}; }
#endif #endif
template class Executor<CPU, Precision::FP32>; template class Executor<CPU, Precision::FP32>;
......
...@@ -14,16 +14,16 @@ limitations under the License. */ ...@@ -14,16 +14,16 @@ limitations under the License. */
#pragma once #pragma once
#include <map>
#include <memory>
#include <string>
#include <vector>
#include "common/types.h" #include "common/types.h"
#include "common/util.h" #include "common/util.h"
#include "framework/lod_tensor.h" #include "framework/lod_tensor.h"
#include "framework/operator.h" #include "framework/operator.h"
#include "framework/program/program.h" #include "framework/program/program.h"
#include "framework/tensor.h" #include "framework/tensor.h"
#include <memory>
#include <string>
#include <vector>
#include <map>
namespace paddle_mobile { namespace paddle_mobile {
...@@ -36,8 +36,7 @@ class Executor { ...@@ -36,8 +36,7 @@ class Executor {
// @param use_optimize bool whether use operator fusion to speed up or not // @param use_optimize bool whether use operator fusion to speed up or not
// @param loddable bool // @param loddable bool
Executor(const framework::Program<Dtype> program, Executor(const framework::Program<Dtype> program,
const bool use_optimize = true, const bool use_optimize = true, const bool loddable = false);
const bool loddable = false);
// predict with tensor input // predict with tensor input
// @param t input tensor to do prediction // @param t input tensor to do prediction
...@@ -68,7 +67,7 @@ class Executor { ...@@ -68,7 +67,7 @@ class Executor {
framework::LoDTensor *tensor) const; framework::LoDTensor *tensor) const;
void InitMemory(); void InitMemory();
void InitCombineMemory(); void InitCombineMemory();
void LoadMemory(void** data, void LoadMemory(void **data,
const std::shared_ptr<framework::VarDesc> var_desc, const std::shared_ptr<framework::VarDesc> var_desc,
framework::LoDTensor *tensor); framework::LoDTensor *tensor);
......
...@@ -30,4 +30,3 @@ namespace ops = paddle_mobile::operators; ...@@ -30,4 +30,3 @@ namespace ops = paddle_mobile::operators;
#ifdef PADDLE_MOBILE_CPU #ifdef PADDLE_MOBILE_CPU
REGISTER_OPERATOR_CPU(dequantize, ops::DequantizeOp); REGISTER_OPERATOR_CPU(dequantize, ops::DequantizeOp);
#endif #endif
...@@ -12,8 +12,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ...@@ -12,8 +12,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. */ limitations under the License. */
#include "feed_op.h" #include "operators/feed_op.h"
namespace ops = paddle_mobile::operators; namespace ops = paddle_mobile::operators;
...@@ -26,4 +25,3 @@ REGISTER_OPERATOR_MALI_GPU(feed, ops::FeedOp); ...@@ -26,4 +25,3 @@ REGISTER_OPERATOR_MALI_GPU(feed, ops::FeedOp);
#ifdef PADDLE_MOBILE_FPGA #ifdef PADDLE_MOBILE_FPGA
REGISTER_OPERATOR_FPGA(feed, ops::FeedOp); REGISTER_OPERATOR_FPGA(feed, ops::FeedOp);
#endif #endif
...@@ -44,7 +44,7 @@ class FeedOp : public framework::OperatorBase<DeviceType> { ...@@ -44,7 +44,7 @@ class FeedOp : public framework::OperatorBase<DeviceType> {
} }
void RunImpl() const { void RunImpl() const {
auto input = (Tensor *)const_cast<LoDTensor *>(param_.InputX()); auto input = reinterpret_cast<Tensor *>(param_.InputX());
fpga::format_image(input); fpga::format_image(input);
auto input_ptr = input->data<float>(); auto input_ptr = input->data<float>();
Tensor *output = param_.Out(); Tensor *output = param_.Out();
...@@ -53,7 +53,7 @@ class FeedOp : public framework::OperatorBase<DeviceType> { ...@@ -53,7 +53,7 @@ class FeedOp : public framework::OperatorBase<DeviceType> {
fpga::BypassArgs args; fpga::BypassArgs args;
args.convert_type = fpga::DATA_FP32_TO_FP16; args.convert_type = fpga::DATA_FP32_TO_FP16;
args.layout_type = fpga::LAYOUT_NO_CONVERT; args.layout_type = fpga::LAYOUT_NO_CONVERT;
args.image.address = (void *)input_ptr; args.image.address = input_ptr;
args.image.channels = input->dims()[1]; args.image.channels = input->dims()[1];
args.image.height = input->dims()[2]; args.image.height = input->dims()[2];
args.image.width = input->dims()[3]; args.image.width = input->dims()[3];
...@@ -78,4 +78,3 @@ class FeedOp : public framework::OperatorBase<DeviceType> { ...@@ -78,4 +78,3 @@ class FeedOp : public framework::OperatorBase<DeviceType> {
} // namespace operators } // namespace operators
} // namespace paddle_mobile } // namespace paddle_mobile
...@@ -12,10 +12,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ...@@ -12,10 +12,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. */ limitations under the License. */
#include "fetch_op.h" #include "operators/fetch_op.h"
namespace paddle_mobile {
namespace operators {}
} // namespace paddle_mobile
namespace ops = paddle_mobile::operators; namespace ops = paddle_mobile::operators;
#ifdef PADDLE_MOBILE_CPU #ifdef PADDLE_MOBILE_CPU
...@@ -27,4 +24,3 @@ REGISTER_OPERATOR_MALI_GPU(fetch, ops::FetchOp); ...@@ -27,4 +24,3 @@ REGISTER_OPERATOR_MALI_GPU(fetch, ops::FetchOp);
#ifdef PADDLE_MOBILE_FPGA #ifdef PADDLE_MOBILE_FPGA
REGISTER_OPERATOR_FPGA(fetch, ops::FetchOp); REGISTER_OPERATOR_FPGA(fetch, ops::FetchOp);
#endif #endif
...@@ -46,4 +46,3 @@ class FetchOp : public framework::OperatorBase<DeviceType> { ...@@ -46,4 +46,3 @@ class FetchOp : public framework::OperatorBase<DeviceType> {
} // namespace operators } // namespace operators
} // namespace paddle_mobile } // namespace paddle_mobile
...@@ -23,12 +23,12 @@ limitations under the License. */ ...@@ -23,12 +23,12 @@ limitations under the License. */
namespace paddle_mobile { namespace paddle_mobile {
namespace operators { namespace operators {
template<> template <>
bool DequantizeKernel<CPU, float>::Init(DequantizeParam<CPU> *param) { bool DequantizeKernel<CPU, float>::Init(DequantizeParam<CPU> *param) {
return true; return true;
} }
template<> template <>
void DequantizeKernel<CPU, float>::Compute( void DequantizeKernel<CPU, float>::Compute(
const DequantizeParam<CPU> &param) const { const DequantizeParam<CPU> &param) const {
const Tensor *input = param.input_; const Tensor *input = param.input_;
...@@ -70,7 +70,7 @@ void DequantizeKernel<CPU, float>::Compute( ...@@ -70,7 +70,7 @@ void DequantizeKernel<CPU, float>::Compute(
} }
} }
} // namespace paddle_mobile
} // namespace operators } // namespace operators
} // namespace paddle_mobile
#endif #endif
...@@ -28,9 +28,7 @@ float32_t vmaxvq_f32(float32x4_t r) { ...@@ -28,9 +28,7 @@ float32_t vmaxvq_f32(float32x4_t r) {
} }
#endif #endif
int32x4_t vrnd_towards_zero(float32x4_t r) { int32x4_t vrnd_towards_zero(float32x4_t r) { return vcvtq_s32_f32(r); }
return vcvtq_s32_f32(r);
}
int32x4_t vrnd_away_zero(float32x4_t r) { int32x4_t vrnd_away_zero(float32x4_t r) {
float32x4_t plus = vdupq_n_f32(0.5); float32x4_t plus = vdupq_n_f32(0.5);
...@@ -85,7 +83,7 @@ int32x4_t vrnd_to_even(float32x4_t r) { ...@@ -85,7 +83,7 @@ int32x4_t vrnd_to_even(float32x4_t r) {
smask = vsubq_s32(smask, one); smask = vsubq_s32(smask, one);
rnd = vaddq_s32(rnd, smask); rnd = vaddq_s32(rnd, smask);
return rnd; return rnd;
#endif #endif
} }
#endif #endif
...@@ -93,7 +91,7 @@ namespace paddle_mobile { ...@@ -93,7 +91,7 @@ namespace paddle_mobile {
namespace operators { namespace operators {
static float find_abs_max(const Tensor *input) { static float find_abs_max(const Tensor *input) {
float max_abs = float(0); float max_abs = 0.f;
const float *x = input->data<const float>(); const float *x = input->data<const float>();
size_t size = input->numel(); size_t size = input->numel();
#if defined(__ARM_NEON__) || defined(__ARM_NEON) #if defined(__ARM_NEON__) || defined(__ARM_NEON)
...@@ -130,8 +128,7 @@ static float find_abs_max(const Tensor *input) { ...@@ -130,8 +128,7 @@ static float find_abs_max(const Tensor *input) {
return max_abs; return max_abs;
} }
static void quantize_round_to_even(const Tensor *input, static void quantize_round_to_even(const Tensor *input, const float scale,
const float scale,
Tensor *output) { Tensor *output) {
const float *x = input->data<const float>(); const float *x = input->data<const float>();
int8_t *y = output->data<int8_t>(); int8_t *y = output->data<int8_t>();
...@@ -183,8 +180,7 @@ static void quantize_round_to_even(const Tensor *input, ...@@ -183,8 +180,7 @@ static void quantize_round_to_even(const Tensor *input,
} }
} }
static void quantize_round_to_zero(const Tensor *input, static void quantize_round_to_zero(const Tensor *input, const float scale,
const float scale,
Tensor *output) { Tensor *output) {
const float *x = input->data<const float>(); const float *x = input->data<const float>();
int8_t *y = output->data<int8_t>(); int8_t *y = output->data<int8_t>();
...@@ -225,8 +221,7 @@ static void quantize_round_to_zero(const Tensor *input, ...@@ -225,8 +221,7 @@ static void quantize_round_to_zero(const Tensor *input,
} }
} }
static void quantize_round_to_nearest(const Tensor *input, static void quantize_round_to_nearest(const Tensor *input, const float scale,
const float scale,
Tensor *output) { Tensor *output) {
const float *x = input->data<const float>(); const float *x = input->data<const float>();
int8_t *y = output->data<int8_t>(); int8_t *y = output->data<int8_t>();
...@@ -267,15 +262,14 @@ static void quantize_round_to_nearest(const Tensor *input, ...@@ -267,15 +262,14 @@ static void quantize_round_to_nearest(const Tensor *input,
} }
} }
template<> template <>
bool QuantizeKernel<CPU, float>::Init(QuantizeParam<CPU> *param) { bool QuantizeKernel<CPU, float>::Init(QuantizeParam<CPU> *param) {
return true; return true;
} }
template<> template <>
void QuantizeKernel<CPU, float>::Compute( void QuantizeKernel<CPU, float>::Compute(
const QuantizeParam<CPU> &param) const { const QuantizeParam<CPU> &param) const {
// TODO
float max_abs = 0.f; float max_abs = 0.f;
const Tensor *input = param.input_; const Tensor *input = param.input_;
Tensor *output = param.out_; Tensor *output = param.out_;
...@@ -306,7 +300,7 @@ void QuantizeKernel<CPU, float>::Compute( ...@@ -306,7 +300,7 @@ void QuantizeKernel<CPU, float>::Compute(
} }
} }
} // namespace paddle_mobile
} // namespace operators } // namespace operators
} // namespace paddle_mobile
#endif #endif
...@@ -13,6 +13,7 @@ See the License for the specific language governing permissions and ...@@ -13,6 +13,7 @@ See the License for the specific language governing permissions and
limitations under the License. */ limitations under the License. */
#include "operators/quantize_op.h" #include "operators/quantize_op.h"
#include <vector>
namespace paddle_mobile { namespace paddle_mobile {
namespace operators { namespace operators {
...@@ -32,4 +33,3 @@ namespace ops = paddle_mobile::operators; ...@@ -32,4 +33,3 @@ namespace ops = paddle_mobile::operators;
#ifdef PADDLE_MOBILE_CPU #ifdef PADDLE_MOBILE_CPU
REGISTER_OPERATOR_CPU(quantize, ops::QuantizeOp); REGISTER_OPERATOR_CPU(quantize, ops::QuantizeOp);
#endif #endif
...@@ -57,7 +57,7 @@ ...@@ -57,7 +57,7 @@
/* Workaround for Microsoft compilers. */ /* Workaround for Microsoft compilers. */
#ifdef _MSC_VER #ifdef _MSC_VER
# define inline __inline #define inline __inline
#endif #endif
/** /**
...@@ -81,7 +81,7 @@ ...@@ -81,7 +81,7 @@
#define MAX_UINT64_ENCODED_SIZE 10 #define MAX_UINT64_ENCODED_SIZE 10
#ifndef PROTOBUF_C_UNPACK_ERROR #ifndef PROTOBUF_C_UNPACK_ERROR
# define PROTOBUF_C_UNPACK_ERROR(...) #define PROTOBUF_C_UNPACK_ERROR(...)
#endif #endif
const char protobuf_c_empty_string[] = ""; const char protobuf_c_empty_string[] = "";
...@@ -93,7 +93,7 @@ const char protobuf_c_empty_string[] = ""; ...@@ -93,7 +93,7 @@ const char protobuf_c_empty_string[] = "";
* STRUCT_MEMBER_PTR(). * STRUCT_MEMBER_PTR().
*/ */
#define STRUCT_MEMBER_P(struct_p, struct_offset) \ #define STRUCT_MEMBER_P(struct_p, struct_offset) \
((void *) ((uint8_t *) (struct_p) + (struct_offset))) ((void *)((uint8_t *)(struct_p) + (struct_offset)))
/** /**
* Return field in a `ProtobufCMessage` based on offset. * Return field in a `ProtobufCMessage` based on offset.
...@@ -102,7 +102,7 @@ const char protobuf_c_empty_string[] = ""; ...@@ -102,7 +102,7 @@ const char protobuf_c_empty_string[] = "";
* Cast it to the passed type. * Cast it to the passed type.
*/ */
#define STRUCT_MEMBER(member_type, struct_p, struct_offset) \ #define STRUCT_MEMBER(member_type, struct_p, struct_offset) \
(*(member_type *) STRUCT_MEMBER_P((struct_p), (struct_offset))) (*(member_type *)STRUCT_MEMBER_P((struct_p), (struct_offset)))
/** /**
* Return field in a `ProtobufCMessage` based on offset. * Return field in a `ProtobufCMessage` based on offset.
...@@ -111,7 +111,7 @@ const char protobuf_c_empty_string[] = ""; ...@@ -111,7 +111,7 @@ const char protobuf_c_empty_string[] = "";
* it to a pointer to the passed type. * it to a pointer to the passed type.
*/ */
#define STRUCT_MEMBER_PTR(member_type, struct_p, struct_offset) \ #define STRUCT_MEMBER_PTR(member_type, struct_p, struct_offset) \
((member_type *) STRUCT_MEMBER_P((struct_p), (struct_offset))) ((member_type *)STRUCT_MEMBER_P((struct_p), (struct_offset)))
/* Assertions for magic numbers. */ /* Assertions for magic numbers. */
...@@ -131,43 +131,24 @@ const char protobuf_c_empty_string[] = ""; ...@@ -131,43 +131,24 @@ const char protobuf_c_empty_string[] = "";
/* --- version --- */ /* --- version --- */
const char * const char *protobuf_c_version(void) { return PROTOBUF_C_VERSION; }
protobuf_c_version(void)
{
return PROTOBUF_C_VERSION;
}
uint32_t uint32_t protobuf_c_version_number(void) { return PROTOBUF_C_VERSION_NUMBER; }
protobuf_c_version_number(void)
{
return PROTOBUF_C_VERSION_NUMBER;
}
/* --- allocator --- */ /* --- allocator --- */
static void * static void *system_alloc(void *allocator_data, size_t size) {
system_alloc(void *allocator_data, size_t size)
{
return malloc(size); return malloc(size);
} }
static void static void system_free(void *allocator_data, void *data) { free(data); }
system_free(void *allocator_data, void *data)
{
free(data);
}
static inline void * static inline void *do_alloc(ProtobufCAllocator *allocator, size_t size) {
do_alloc(ProtobufCAllocator *allocator, size_t size)
{
return allocator->alloc(allocator->allocator_data, size); return allocator->alloc(allocator->allocator_data, size);
} }
static inline void static inline void do_free(ProtobufCAllocator *allocator, void *data) {
do_free(ProtobufCAllocator *allocator, void *data) if (data != NULL) allocator->free(allocator->allocator_data, data);
{
if (data != NULL)
allocator->free(allocator->allocator_data, data);
} }
/* /*
...@@ -183,11 +164,9 @@ static ProtobufCAllocator protobuf_c__allocator = { ...@@ -183,11 +164,9 @@ static ProtobufCAllocator protobuf_c__allocator = {
/* === buffer-simple === */ /* === buffer-simple === */
void void protobuf_c_buffer_simple_append(ProtobufCBuffer *buffer, size_t len,
protobuf_c_buffer_simple_append(ProtobufCBuffer *buffer, const uint8_t *data) {
size_t len, const uint8_t *data) ProtobufCBufferSimple *simp = (ProtobufCBufferSimple *)buffer;
{
ProtobufCBufferSimple *simp = (ProtobufCBufferSimple *) buffer;
size_t new_len = simp->len + len; size_t new_len = simp->len + len;
if (new_len > simp->alloced) { if (new_len > simp->alloced) {
...@@ -195,13 +174,10 @@ protobuf_c_buffer_simple_append(ProtobufCBuffer *buffer, ...@@ -195,13 +174,10 @@ protobuf_c_buffer_simple_append(ProtobufCBuffer *buffer,
size_t new_alloced = simp->alloced * 2; size_t new_alloced = simp->alloced * 2;
uint8_t *new_data; uint8_t *new_data;
if (allocator == NULL) if (allocator == NULL) allocator = &protobuf_c__allocator;
allocator = &protobuf_c__allocator; while (new_alloced < new_len) new_alloced += new_alloced;
while (new_alloced < new_len)
new_alloced += new_alloced;
new_data = do_alloc(allocator, new_alloced); new_data = do_alloc(allocator, new_alloced);
if (!new_data) if (!new_data) return;
return;
memcpy(new_data, simp->data, simp->len); memcpy(new_data, simp->data, simp->len);
if (simp->must_free_data) if (simp->must_free_data)
do_free(allocator, simp->data); do_free(allocator, simp->data);
...@@ -232,9 +208,7 @@ protobuf_c_buffer_simple_append(ProtobufCBuffer *buffer, ...@@ -232,9 +208,7 @@ protobuf_c_buffer_simple_append(ProtobufCBuffer *buffer,
* \return * \return
* Number of bytes required. * Number of bytes required.
*/ */
static inline size_t static inline size_t get_tag_size(uint32_t number) {
get_tag_size(uint32_t number)
{
if (number < (1UL << 4)) { if (number < (1UL << 4)) {
return 1; return 1;
} else if (number < (1UL << 11)) { } else if (number < (1UL << 11)) {
...@@ -257,9 +231,7 @@ get_tag_size(uint32_t number) ...@@ -257,9 +231,7 @@ get_tag_size(uint32_t number)
* \return * \return
* Number of bytes required. * Number of bytes required.
*/ */
static inline size_t static inline size_t uint32_size(uint32_t v) {
uint32_size(uint32_t v)
{
if (v < (1UL << 7)) { if (v < (1UL << 7)) {
return 1; return 1;
} else if (v < (1UL << 14)) { } else if (v < (1UL << 14)) {
...@@ -282,9 +254,7 @@ uint32_size(uint32_t v) ...@@ -282,9 +254,7 @@ uint32_size(uint32_t v)
* \return * \return
* Number of bytes required. * Number of bytes required.
*/ */
static inline size_t static inline size_t int32_size(int32_t v) {
int32_size(int32_t v)
{
if (v < 0) { if (v < 0) {
return 10; return 10;
} else if (v < (1L << 7)) { } else if (v < (1L << 7)) {
...@@ -309,13 +279,11 @@ int32_size(int32_t v) ...@@ -309,13 +279,11 @@ int32_size(int32_t v)
* \return * \return
* ZigZag encoded integer. * ZigZag encoded integer.
*/ */
static inline uint32_t static inline uint32_t zigzag32(int32_t v) {
zigzag32(int32_t v)
{
if (v < 0) if (v < 0)
return (-(uint32_t)v) * 2 - 1; return (-(uint32_t)v) * 2 - 1;
else else
return (uint32_t)(v) * 2; return (uint32_t)(v)*2;
} }
/** /**
...@@ -328,11 +296,7 @@ zigzag32(int32_t v) ...@@ -328,11 +296,7 @@ zigzag32(int32_t v)
* \return * \return
* Number of bytes required. * Number of bytes required.
*/ */
static inline size_t static inline size_t sint32_size(int32_t v) { return uint32_size(zigzag32(v)); }
sint32_size(int32_t v)
{
return uint32_size(zigzag32(v));
}
/** /**
* Return the number of bytes required to store a 64-bit unsigned integer in * Return the number of bytes required to store a 64-bit unsigned integer in
...@@ -343,13 +307,11 @@ sint32_size(int32_t v) ...@@ -343,13 +307,11 @@ sint32_size(int32_t v)
* \return * \return
* Number of bytes required. * Number of bytes required.
*/ */
static inline size_t static inline size_t uint64_size(uint64_t v) {
uint64_size(uint64_t v) uint32_t upper_v = (uint32_t)(v >> 32);
{
uint32_t upper_v = (uint32_t) (v >> 32);
if (upper_v == 0) { if (upper_v == 0) {
return uint32_size((uint32_t) v); return uint32_size((uint32_t)v);
} else if (upper_v < (1UL << 3)) { } else if (upper_v < (1UL << 3)) {
return 5; return 5;
} else if (upper_v < (1UL << 10)) { } else if (upper_v < (1UL << 10)) {
...@@ -374,13 +336,11 @@ uint64_size(uint64_t v) ...@@ -374,13 +336,11 @@ uint64_size(uint64_t v)
* \return * \return
* ZigZag encoded integer. * ZigZag encoded integer.
*/ */
static inline uint64_t static inline uint64_t zigzag64(int64_t v) {
zigzag64(int64_t v)
{
if (v < 0) if (v < 0)
return (-(uint64_t)v) * 2 - 1; return (-(uint64_t)v) * 2 - 1;
else else
return (uint64_t)(v) * 2; return (uint64_t)(v)*2;
} }
/** /**
...@@ -393,11 +353,7 @@ zigzag64(int64_t v) ...@@ -393,11 +353,7 @@ zigzag64(int64_t v)
* \return * \return
* Number of bytes required. * Number of bytes required.
*/ */
static inline size_t static inline size_t sint64_size(int64_t v) { return uint64_size(zigzag64(v)); }
sint64_size(int64_t v)
{
return uint64_size(zigzag64(v));
}
/** /**
* Calculate the serialized size of a single required message field, including * Calculate the serialized size of a single required message field, including
...@@ -410,25 +366,23 @@ sint64_size(int64_t v) ...@@ -410,25 +366,23 @@ sint64_size(int64_t v)
* \return * \return
* Number of bytes required. * Number of bytes required.
*/ */
static size_t static size_t required_field_get_packed_size(
required_field_get_packed_size(const ProtobufCFieldDescriptor *field, const ProtobufCFieldDescriptor *field, const void *member) {
const void *member)
{
size_t rv = get_tag_size(field->id); size_t rv = get_tag_size(field->id);
switch (field->type) { switch (field->type) {
case PROTOBUF_C_TYPE_SINT32: case PROTOBUF_C_TYPE_SINT32:
return rv + sint32_size(*(const int32_t *) member); return rv + sint32_size(*(const int32_t *)member);
case PROTOBUF_C_TYPE_ENUM: case PROTOBUF_C_TYPE_ENUM:
case PROTOBUF_C_TYPE_INT32: case PROTOBUF_C_TYPE_INT32:
return rv + int32_size(*(const int32_t *) member); return rv + int32_size(*(const int32_t *)member);
case PROTOBUF_C_TYPE_UINT32: case PROTOBUF_C_TYPE_UINT32:
return rv + uint32_size(*(const uint32_t *) member); return rv + uint32_size(*(const uint32_t *)member);
case PROTOBUF_C_TYPE_SINT64: case PROTOBUF_C_TYPE_SINT64:
return rv + sint64_size(*(const int64_t *) member); return rv + sint64_size(*(const int64_t *)member);
case PROTOBUF_C_TYPE_INT64: case PROTOBUF_C_TYPE_INT64:
case PROTOBUF_C_TYPE_UINT64: case PROTOBUF_C_TYPE_UINT64:
return rv + uint64_size(*(const uint64_t *) member); return rv + uint64_size(*(const uint64_t *)member);
case PROTOBUF_C_TYPE_SFIXED32: case PROTOBUF_C_TYPE_SFIXED32:
case PROTOBUF_C_TYPE_FIXED32: case PROTOBUF_C_TYPE_FIXED32:
return rv + 4; return rv + 4;
...@@ -442,16 +396,16 @@ required_field_get_packed_size(const ProtobufCFieldDescriptor *field, ...@@ -442,16 +396,16 @@ required_field_get_packed_size(const ProtobufCFieldDescriptor *field,
case PROTOBUF_C_TYPE_DOUBLE: case PROTOBUF_C_TYPE_DOUBLE:
return rv + 8; return rv + 8;
case PROTOBUF_C_TYPE_STRING: { case PROTOBUF_C_TYPE_STRING: {
const char *str = *(char * const *) member; const char *str = *(char *const *)member;
size_t len = str ? strlen(str) : 0; size_t len = str ? strlen(str) : 0;
return rv + uint32_size(len) + len; return rv + uint32_size(len) + len;
} }
case PROTOBUF_C_TYPE_BYTES: { case PROTOBUF_C_TYPE_BYTES: {
size_t len = ((const ProtobufCBinaryData *) member)->len; size_t len = ((const ProtobufCBinaryData *)member)->len;
return rv + uint32_size(len) + len; return rv + uint32_size(len) + len;
} }
case PROTOBUF_C_TYPE_MESSAGE: { case PROTOBUF_C_TYPE_MESSAGE: {
const ProtobufCMessage *msg = *(ProtobufCMessage * const *) member; const ProtobufCMessage *msg = *(ProtobufCMessage *const *)member;
size_t subrv = msg ? protobuf_c_message_get_packed_size(msg) : 0; size_t subrv = msg ? protobuf_c_message_get_packed_size(msg) : 0;
return rv + uint32_size(subrv) + subrv; return rv + uint32_size(subrv) + subrv;
} }
...@@ -474,20 +428,16 @@ required_field_get_packed_size(const ProtobufCFieldDescriptor *field, ...@@ -474,20 +428,16 @@ required_field_get_packed_size(const ProtobufCFieldDescriptor *field,
* \return * \return
* Number of bytes required. * Number of bytes required.
*/ */
static size_t static size_t oneof_field_get_packed_size(const ProtobufCFieldDescriptor *field,
oneof_field_get_packed_size(const ProtobufCFieldDescriptor *field,
uint32_t oneof_case, uint32_t oneof_case,
const void *member) const void *member) {
{
if (oneof_case != field->id) { if (oneof_case != field->id) {
return 0; return 0;
} }
if (field->type == PROTOBUF_C_TYPE_MESSAGE || if (field->type == PROTOBUF_C_TYPE_MESSAGE ||
field->type == PROTOBUF_C_TYPE_STRING) field->type == PROTOBUF_C_TYPE_STRING) {
{ const void *ptr = *(const void *const *)member;
const void *ptr = *(const void * const *) member; if (ptr == NULL || ptr == field->default_value) return 0;
if (ptr == NULL || ptr == field->default_value)
return 0;
} }
return required_field_get_packed_size(field, member); return required_field_get_packed_size(field, member);
} }
...@@ -506,33 +456,26 @@ oneof_field_get_packed_size(const ProtobufCFieldDescriptor *field, ...@@ -506,33 +456,26 @@ oneof_field_get_packed_size(const ProtobufCFieldDescriptor *field,
* \return * \return
* Number of bytes required. * Number of bytes required.
*/ */
static size_t static size_t optional_field_get_packed_size(
optional_field_get_packed_size(const ProtobufCFieldDescriptor *field, const ProtobufCFieldDescriptor *field, const protobuf_c_boolean has,
const protobuf_c_boolean has, const void *member) {
const void *member)
{
if (field->type == PROTOBUF_C_TYPE_MESSAGE || if (field->type == PROTOBUF_C_TYPE_MESSAGE ||
field->type == PROTOBUF_C_TYPE_STRING) field->type == PROTOBUF_C_TYPE_STRING) {
{ const void *ptr = *(const void *const *)member;
const void *ptr = *(const void * const *) member; if (ptr == NULL || ptr == field->default_value) return 0;
if (ptr == NULL || ptr == field->default_value)
return 0;
} else { } else {
if (!has) if (!has) return 0;
return 0;
} }
return required_field_get_packed_size(field, member); return required_field_get_packed_size(field, member);
} }
static protobuf_c_boolean static protobuf_c_boolean field_is_zeroish(
field_is_zeroish(const ProtobufCFieldDescriptor *field, const ProtobufCFieldDescriptor *field, const void *member) {
const void *member)
{
protobuf_c_boolean ret = FALSE; protobuf_c_boolean ret = FALSE;
switch (field->type) { switch (field->type) {
case PROTOBUF_C_TYPE_BOOL: case PROTOBUF_C_TYPE_BOOL:
ret = (0 == *(const protobuf_c_boolean *) member); ret = (0 == *(const protobuf_c_boolean *)member);
break; break;
case PROTOBUF_C_TYPE_ENUM: case PROTOBUF_C_TYPE_ENUM:
case PROTOBUF_C_TYPE_SINT32: case PROTOBUF_C_TYPE_SINT32:
...@@ -540,28 +483,28 @@ field_is_zeroish(const ProtobufCFieldDescriptor *field, ...@@ -540,28 +483,28 @@ field_is_zeroish(const ProtobufCFieldDescriptor *field,
case PROTOBUF_C_TYPE_UINT32: case PROTOBUF_C_TYPE_UINT32:
case PROTOBUF_C_TYPE_SFIXED32: case PROTOBUF_C_TYPE_SFIXED32:
case PROTOBUF_C_TYPE_FIXED32: case PROTOBUF_C_TYPE_FIXED32:
ret = (0 == *(const uint32_t *) member); ret = (0 == *(const uint32_t *)member);
break; break;
case PROTOBUF_C_TYPE_SINT64: case PROTOBUF_C_TYPE_SINT64:
case PROTOBUF_C_TYPE_INT64: case PROTOBUF_C_TYPE_INT64:
case PROTOBUF_C_TYPE_UINT64: case PROTOBUF_C_TYPE_UINT64:
case PROTOBUF_C_TYPE_SFIXED64: case PROTOBUF_C_TYPE_SFIXED64:
case PROTOBUF_C_TYPE_FIXED64: case PROTOBUF_C_TYPE_FIXED64:
ret = (0 == *(const uint64_t *) member); ret = (0 == *(const uint64_t *)member);
break; break;
case PROTOBUF_C_TYPE_FLOAT: case PROTOBUF_C_TYPE_FLOAT:
ret = (0 == *(const float *) member); ret = (0 == *(const float *)member);
break; break;
case PROTOBUF_C_TYPE_DOUBLE: case PROTOBUF_C_TYPE_DOUBLE:
ret = (0 == *(const double *) member); ret = (0 == *(const double *)member);
break; break;
case PROTOBUF_C_TYPE_STRING: case PROTOBUF_C_TYPE_STRING:
ret = (NULL == *(const char * const *) member) || ret = (NULL == *(const char *const *)member) ||
('\0' == **(const char * const *) member); ('\0' == **(const char *const *)member);
break; break;
case PROTOBUF_C_TYPE_BYTES: case PROTOBUF_C_TYPE_BYTES:
case PROTOBUF_C_TYPE_MESSAGE: case PROTOBUF_C_TYPE_MESSAGE:
ret = (NULL == *(const void * const *) member); ret = (NULL == *(const void *const *)member);
break; break;
default: default:
ret = TRUE; ret = TRUE;
...@@ -584,12 +527,9 @@ field_is_zeroish(const ProtobufCFieldDescriptor *field, ...@@ -584,12 +527,9 @@ field_is_zeroish(const ProtobufCFieldDescriptor *field,
* \return * \return
* Number of bytes required. * Number of bytes required.
*/ */
static size_t static size_t unlabeled_field_get_packed_size(
unlabeled_field_get_packed_size(const ProtobufCFieldDescriptor *field, const ProtobufCFieldDescriptor *field, const void *member) {
const void *member) if (field_is_zeroish(field, member)) return 0;
{
if (field_is_zeroish(field, member))
return 0;
return required_field_get_packed_size(field, member); return required_field_get_packed_size(field, member);
} }
...@@ -607,43 +547,34 @@ unlabeled_field_get_packed_size(const ProtobufCFieldDescriptor *field, ...@@ -607,43 +547,34 @@ unlabeled_field_get_packed_size(const ProtobufCFieldDescriptor *field,
* \return * \return
* Number of bytes required. * Number of bytes required.
*/ */
static size_t static size_t repeated_field_get_packed_size(
repeated_field_get_packed_size(const ProtobufCFieldDescriptor *field, const ProtobufCFieldDescriptor *field, size_t count, const void *member) {
size_t count, const void *member)
{
size_t header_size; size_t header_size;
size_t rv = 0; size_t rv = 0;
unsigned i; unsigned i;
void *array = *(void * const *) member; void *array = *(void *const *)member;
if (count == 0) if (count == 0) return 0;
return 0;
header_size = get_tag_size(field->id); header_size = get_tag_size(field->id);
if (0 == (field->flags & PROTOBUF_C_FIELD_FLAG_PACKED)) if (0 == (field->flags & PROTOBUF_C_FIELD_FLAG_PACKED)) header_size *= count;
header_size *= count;
switch (field->type) { switch (field->type) {
case PROTOBUF_C_TYPE_SINT32: case PROTOBUF_C_TYPE_SINT32:
for (i = 0; i < count; i++) for (i = 0; i < count; i++) rv += sint32_size(((int32_t *)array)[i]);
rv += sint32_size(((int32_t *) array)[i]);
break; break;
case PROTOBUF_C_TYPE_ENUM: case PROTOBUF_C_TYPE_ENUM:
case PROTOBUF_C_TYPE_INT32: case PROTOBUF_C_TYPE_INT32:
for (i = 0; i < count; i++) for (i = 0; i < count; i++) rv += int32_size(((int32_t *)array)[i]);
rv += int32_size(((int32_t *) array)[i]);
break; break;
case PROTOBUF_C_TYPE_UINT32: case PROTOBUF_C_TYPE_UINT32:
for (i = 0; i < count; i++) for (i = 0; i < count; i++) rv += uint32_size(((uint32_t *)array)[i]);
rv += uint32_size(((uint32_t *) array)[i]);
break; break;
case PROTOBUF_C_TYPE_SINT64: case PROTOBUF_C_TYPE_SINT64:
for (i = 0; i < count; i++) for (i = 0; i < count; i++) rv += sint64_size(((int64_t *)array)[i]);
rv += sint64_size(((int64_t *) array)[i]);
break; break;
case PROTOBUF_C_TYPE_INT64: case PROTOBUF_C_TYPE_INT64:
case PROTOBUF_C_TYPE_UINT64: case PROTOBUF_C_TYPE_UINT64:
for (i = 0; i < count; i++) for (i = 0; i < count; i++) rv += uint64_size(((uint64_t *)array)[i]);
rv += uint64_size(((uint64_t *) array)[i]);
break; break;
case PROTOBUF_C_TYPE_SFIXED32: case PROTOBUF_C_TYPE_SFIXED32:
case PROTOBUF_C_TYPE_FIXED32: case PROTOBUF_C_TYPE_FIXED32:
...@@ -660,20 +591,20 @@ repeated_field_get_packed_size(const ProtobufCFieldDescriptor *field, ...@@ -660,20 +591,20 @@ repeated_field_get_packed_size(const ProtobufCFieldDescriptor *field,
break; break;
case PROTOBUF_C_TYPE_STRING: case PROTOBUF_C_TYPE_STRING:
for (i = 0; i < count; i++) { for (i = 0; i < count; i++) {
size_t len = strlen(((char **) array)[i]); size_t len = strlen(((char **)array)[i]);
rv += uint32_size(len) + len; rv += uint32_size(len) + len;
} }
break; break;
case PROTOBUF_C_TYPE_BYTES: case PROTOBUF_C_TYPE_BYTES:
for (i = 0; i < count; i++) { for (i = 0; i < count; i++) {
size_t len = ((ProtobufCBinaryData *) array)[i].len; size_t len = ((ProtobufCBinaryData *)array)[i].len;
rv += uint32_size(len) + len; rv += uint32_size(len) + len;
} }
break; break;
case PROTOBUF_C_TYPE_MESSAGE: case PROTOBUF_C_TYPE_MESSAGE:
for (i = 0; i < count; i++) { for (i = 0; i < count; i++) {
size_t len = protobuf_c_message_get_packed_size( size_t len =
((ProtobufCMessage **) array)[i]); protobuf_c_message_get_packed_size(((ProtobufCMessage **)array)[i]);
rv += uint32_size(len) + len; rv += uint32_size(len) + len;
} }
break; break;
...@@ -694,9 +625,8 @@ repeated_field_get_packed_size(const ProtobufCFieldDescriptor *field, ...@@ -694,9 +625,8 @@ repeated_field_get_packed_size(const ProtobufCFieldDescriptor *field,
* \return * \return
* Number of bytes required. * Number of bytes required.
*/ */
static inline size_t static inline size_t unknown_field_get_packed_size(
unknown_field_get_packed_size(const ProtobufCMessageUnknownField *field) const ProtobufCMessageUnknownField *field) {
{
return get_tag_size(field->tag) + field->len; return get_tag_size(field->tag) + field->len;
} }
...@@ -705,47 +635,31 @@ unknown_field_get_packed_size(const ProtobufCMessageUnknownField *field) ...@@ -705,47 +635,31 @@ unknown_field_get_packed_size(const ProtobufCMessageUnknownField *field)
/* /*
* Calculate the serialized size of the message. * Calculate the serialized size of the message.
*/ */
size_t protobuf_c_message_get_packed_size(const ProtobufCMessage *message) size_t protobuf_c_message_get_packed_size(const ProtobufCMessage *message) {
{
unsigned i; unsigned i;
size_t rv = 0; size_t rv = 0;
ASSERT_IS_MESSAGE(message); ASSERT_IS_MESSAGE(message);
for (i = 0; i < message->descriptor->n_fields; i++) { for (i = 0; i < message->descriptor->n_fields; i++) {
const ProtobufCFieldDescriptor *field = const ProtobufCFieldDescriptor *field = message->descriptor->fields + i;
message->descriptor->fields + i; const void *member = ((const char *)message) + field->offset;
const void *member = const void *qmember = ((const char *)message) + field->quantifier_offset;
((const char *) message) + field->offset;
const void *qmember =
((const char *) message) + field->quantifier_offset;
if (field->label == PROTOBUF_C_LABEL_REQUIRED) { if (field->label == PROTOBUF_C_LABEL_REQUIRED) {
rv += required_field_get_packed_size(field, member); rv += required_field_get_packed_size(field, member);
} else if ((field->label == PROTOBUF_C_LABEL_OPTIONAL || } else if ((field->label == PROTOBUF_C_LABEL_OPTIONAL ||
field->label == PROTOBUF_C_LABEL_NONE) && field->label == PROTOBUF_C_LABEL_NONE) &&
(0 != (field->flags & PROTOBUF_C_FIELD_FLAG_ONEOF))) { (0 != (field->flags & PROTOBUF_C_FIELD_FLAG_ONEOF))) {
rv += oneof_field_get_packed_size( rv += oneof_field_get_packed_size(field, *(const uint32_t *)qmember,
field, member);
*(const uint32_t *) qmember,
member
);
} else if (field->label == PROTOBUF_C_LABEL_OPTIONAL) { } else if (field->label == PROTOBUF_C_LABEL_OPTIONAL) {
rv += optional_field_get_packed_size( rv += optional_field_get_packed_size(
field, field, *(protobuf_c_boolean *)qmember, member);
*(protobuf_c_boolean *) qmember,
member
);
} else if (field->label == PROTOBUF_C_LABEL_NONE) { } else if (field->label == PROTOBUF_C_LABEL_NONE) {
rv += unlabeled_field_get_packed_size( rv += unlabeled_field_get_packed_size(field, member);
field,
member
);
} else { } else {
rv += repeated_field_get_packed_size( rv += repeated_field_get_packed_size(field, *(const size_t *)qmember,
field, member);
*(const size_t *) qmember,
member
);
} }
} }
for (i = 0; i < message->n_unknown_fields; i++) for (i = 0; i < message->n_unknown_fields; i++)
...@@ -773,9 +687,7 @@ size_t protobuf_c_message_get_packed_size(const ProtobufCMessage *message) ...@@ -773,9 +687,7 @@ size_t protobuf_c_message_get_packed_size(const ProtobufCMessage *message)
* \return * \return
* Number of bytes written to `out`. * Number of bytes written to `out`.
*/ */
static inline size_t static inline size_t uint32_pack(uint32_t value, uint8_t *out) {
uint32_pack(uint32_t value, uint8_t *out)
{
unsigned rv = 0; unsigned rv = 0;
if (value >= 0x80) { if (value >= 0x80) {
...@@ -810,9 +722,7 @@ uint32_pack(uint32_t value, uint8_t *out) ...@@ -810,9 +722,7 @@ uint32_pack(uint32_t value, uint8_t *out)
* \return * \return
* Number of bytes written to `out`. * Number of bytes written to `out`.
*/ */
static inline size_t static inline size_t int32_pack(int32_t value, uint8_t *out) {
int32_pack(int32_t value, uint8_t *out)
{
if (value < 0) { if (value < 0) {
out[0] = value | 0x80; out[0] = value | 0x80;
out[1] = (value >> 7) | 0x80; out[1] = (value >> 7) | 0x80;
...@@ -838,9 +748,7 @@ int32_pack(int32_t value, uint8_t *out) ...@@ -838,9 +748,7 @@ int32_pack(int32_t value, uint8_t *out)
* \return * \return
* Number of bytes written to `out`. * Number of bytes written to `out`.
*/ */
static inline size_t static inline size_t sint32_pack(int32_t value, uint8_t *out) {
sint32_pack(int32_t value, uint8_t *out)
{
return uint32_pack(zigzag32(value), out); return uint32_pack(zigzag32(value), out);
} }
...@@ -855,15 +763,12 @@ sint32_pack(int32_t value, uint8_t *out) ...@@ -855,15 +763,12 @@ sint32_pack(int32_t value, uint8_t *out)
* \return * \return
* Number of bytes written to `out`. * Number of bytes written to `out`.
*/ */
static size_t static size_t uint64_pack(uint64_t value, uint8_t *out) {
uint64_pack(uint64_t value, uint8_t *out) uint32_t hi = (uint32_t)(value >> 32);
{ uint32_t lo = (uint32_t)value;
uint32_t hi = (uint32_t) (value >> 32);
uint32_t lo = (uint32_t) value;
unsigned rv; unsigned rv;
if (hi == 0) if (hi == 0) return uint32_pack((uint32_t)lo, out);
return uint32_pack((uint32_t) lo, out);
out[0] = (lo) | 0x80; out[0] = (lo) | 0x80;
out[1] = (lo >> 7) | 0x80; out[1] = (lo >> 7) | 0x80;
out[2] = (lo >> 14) | 0x80; out[2] = (lo >> 14) | 0x80;
...@@ -895,9 +800,7 @@ uint64_pack(uint64_t value, uint8_t *out) ...@@ -895,9 +800,7 @@ uint64_pack(uint64_t value, uint8_t *out)
* \return * \return
* Number of bytes written to `out`. * Number of bytes written to `out`.
*/ */
static inline size_t static inline size_t sint64_pack(int64_t value, uint8_t *out) {
sint64_pack(int64_t value, uint8_t *out)
{
return uint64_pack(zigzag64(value), out); return uint64_pack(zigzag64(value), out);
} }
...@@ -912,9 +815,7 @@ sint64_pack(int64_t value, uint8_t *out) ...@@ -912,9 +815,7 @@ sint64_pack(int64_t value, uint8_t *out)
* \return * \return
* Number of bytes written to `out`. * Number of bytes written to `out`.
*/ */
static inline size_t static inline size_t fixed32_pack(uint32_t value, void *out) {
fixed32_pack(uint32_t value, void *out)
{
#if !defined(WORDS_BIGENDIAN) #if !defined(WORDS_BIGENDIAN)
memcpy(out, &value, 4); memcpy(out, &value, 4);
#else #else
...@@ -943,14 +844,12 @@ fixed32_pack(uint32_t value, void *out) ...@@ -943,14 +844,12 @@ fixed32_pack(uint32_t value, void *out)
* \return * \return
* Number of bytes written to `out`. * Number of bytes written to `out`.
*/ */
static inline size_t static inline size_t fixed64_pack(uint64_t value, void *out) {
fixed64_pack(uint64_t value, void *out)
{
#if !defined(WORDS_BIGENDIAN) #if !defined(WORDS_BIGENDIAN)
memcpy(out, &value, 8); memcpy(out, &value, 8);
#else #else
fixed32_pack(value, out); fixed32_pack(value, out);
fixed32_pack(value >> 32, ((char *) out) + 4); fixed32_pack(value >> 32, ((char *)out) + 4);
#endif #endif
return 8; return 8;
} }
...@@ -968,9 +867,7 @@ fixed64_pack(uint64_t value, void *out) ...@@ -968,9 +867,7 @@ fixed64_pack(uint64_t value, void *out)
* \return * \return
* Number of bytes written to `out`. * Number of bytes written to `out`.
*/ */
static inline size_t static inline size_t boolean_pack(protobuf_c_boolean value, uint8_t *out) {
boolean_pack(protobuf_c_boolean value, uint8_t *out)
{
*out = value ? TRUE : FALSE; *out = value ? TRUE : FALSE;
return 1; return 1;
} }
...@@ -990,9 +887,7 @@ boolean_pack(protobuf_c_boolean value, uint8_t *out) ...@@ -990,9 +887,7 @@ boolean_pack(protobuf_c_boolean value, uint8_t *out)
* \return * \return
* Number of bytes written to `out`. * Number of bytes written to `out`.
*/ */
static inline size_t static inline size_t string_pack(const char *str, uint8_t *out) {
string_pack(const char *str, uint8_t *out)
{
if (str == NULL) { if (str == NULL) {
out[0] = 0; out[0] = 0;
return 1; return 1;
...@@ -1015,9 +910,8 @@ string_pack(const char *str, uint8_t *out) ...@@ -1015,9 +910,8 @@ string_pack(const char *str, uint8_t *out)
* \return * \return
* Number of bytes written to `out`. * Number of bytes written to `out`.
*/ */
static inline size_t static inline size_t binary_data_pack(const ProtobufCBinaryData *bd,
binary_data_pack(const ProtobufCBinaryData *bd, uint8_t *out) uint8_t *out) {
{
size_t len = bd->len; size_t len = bd->len;
size_t rv = uint32_pack(len, out); size_t rv = uint32_pack(len, out);
memcpy(out + rv, bd->data, len); memcpy(out + rv, bd->data, len);
...@@ -1035,17 +929,15 @@ binary_data_pack(const ProtobufCBinaryData *bd, uint8_t *out) ...@@ -1035,17 +929,15 @@ binary_data_pack(const ProtobufCBinaryData *bd, uint8_t *out)
* \return * \return
* Number of bytes written to `out`. * Number of bytes written to `out`.
*/ */
static inline size_t static inline size_t prefixed_message_pack(const ProtobufCMessage *message,
prefixed_message_pack(const ProtobufCMessage *message, uint8_t *out) uint8_t *out) {
{
if (message == NULL) { if (message == NULL) {
out[0] = 0; out[0] = 0;
return 1; return 1;
} else { } else {
size_t rv = protobuf_c_message_pack(message, out + 1); size_t rv = protobuf_c_message_pack(message, out + 1);
uint32_t rv_packed_size = uint32_size(rv); uint32_t rv_packed_size = uint32_size(rv);
if (rv_packed_size != 1) if (rv_packed_size != 1) memmove(out + rv_packed_size, out + 1, rv);
memmove(out + rv_packed_size, out + 1, rv);
return uint32_pack(rv, out) + rv; return uint32_pack(rv, out) + rv;
} }
} }
...@@ -1064,13 +956,11 @@ prefixed_message_pack(const ProtobufCMessage *message, uint8_t *out) ...@@ -1064,13 +956,11 @@ prefixed_message_pack(const ProtobufCMessage *message, uint8_t *out)
* \return * \return
* Number of bytes written to `out`. * Number of bytes written to `out`.
*/ */
static size_t static size_t tag_pack(uint32_t id, uint8_t *out) {
tag_pack(uint32_t id, uint8_t *out)
{
if (id < (1UL << (32 - 3))) if (id < (1UL << (32 - 3)))
return uint32_pack(id << 3, out); return uint32_pack(id << 3, out);
else else
return uint64_pack(((uint64_t) id) << 3, out); return uint64_pack(((uint64_t)id) << 3, out);
} }
/** /**
...@@ -1085,52 +975,52 @@ tag_pack(uint32_t id, uint8_t *out) ...@@ -1085,52 +975,52 @@ tag_pack(uint32_t id, uint8_t *out)
* \return * \return
* Number of bytes written to `out`. * Number of bytes written to `out`.
*/ */
static size_t static size_t required_field_pack(const ProtobufCFieldDescriptor *field,
required_field_pack(const ProtobufCFieldDescriptor *field, const void *member, uint8_t *out) {
const void *member, uint8_t *out)
{
size_t rv = tag_pack(field->id, out); size_t rv = tag_pack(field->id, out);
switch (field->type) { switch (field->type) {
case PROTOBUF_C_TYPE_SINT32: case PROTOBUF_C_TYPE_SINT32:
out[0] |= PROTOBUF_C_WIRE_TYPE_VARINT; out[0] |= PROTOBUF_C_WIRE_TYPE_VARINT;
return rv + sint32_pack(*(const int32_t *) member, out + rv); return rv + sint32_pack(*(const int32_t *)member, out + rv);
case PROTOBUF_C_TYPE_ENUM: case PROTOBUF_C_TYPE_ENUM:
case PROTOBUF_C_TYPE_INT32: case PROTOBUF_C_TYPE_INT32:
out[0] |= PROTOBUF_C_WIRE_TYPE_VARINT; out[0] |= PROTOBUF_C_WIRE_TYPE_VARINT;
return rv + int32_pack(*(const int32_t *) member, out + rv); return rv + int32_pack(*(const int32_t *)member, out + rv);
case PROTOBUF_C_TYPE_UINT32: case PROTOBUF_C_TYPE_UINT32:
out[0] |= PROTOBUF_C_WIRE_TYPE_VARINT; out[0] |= PROTOBUF_C_WIRE_TYPE_VARINT;
return rv + uint32_pack(*(const uint32_t *) member, out + rv); return rv + uint32_pack(*(const uint32_t *)member, out + rv);
case PROTOBUF_C_TYPE_SINT64: case PROTOBUF_C_TYPE_SINT64:
out[0] |= PROTOBUF_C_WIRE_TYPE_VARINT; out[0] |= PROTOBUF_C_WIRE_TYPE_VARINT;
return rv + sint64_pack(*(const int64_t *) member, out + rv); return rv + sint64_pack(*(const int64_t *)member, out + rv);
case PROTOBUF_C_TYPE_INT64: case PROTOBUF_C_TYPE_INT64:
case PROTOBUF_C_TYPE_UINT64: case PROTOBUF_C_TYPE_UINT64:
out[0] |= PROTOBUF_C_WIRE_TYPE_VARINT; out[0] |= PROTOBUF_C_WIRE_TYPE_VARINT;
return rv + uint64_pack(*(const uint64_t *) member, out + rv); return rv + uint64_pack(*(const uint64_t *)member, out + rv);
case PROTOBUF_C_TYPE_SFIXED32: case PROTOBUF_C_TYPE_SFIXED32:
case PROTOBUF_C_TYPE_FIXED32: case PROTOBUF_C_TYPE_FIXED32:
case PROTOBUF_C_TYPE_FLOAT: case PROTOBUF_C_TYPE_FLOAT:
out[0] |= PROTOBUF_C_WIRE_TYPE_32BIT; out[0] |= PROTOBUF_C_WIRE_TYPE_32BIT;
return rv + fixed32_pack(*(const uint32_t *) member, out + rv); return rv + fixed32_pack(*(const uint32_t *)member, out + rv);
case PROTOBUF_C_TYPE_SFIXED64: case PROTOBUF_C_TYPE_SFIXED64:
case PROTOBUF_C_TYPE_FIXED64: case PROTOBUF_C_TYPE_FIXED64:
case PROTOBUF_C_TYPE_DOUBLE: case PROTOBUF_C_TYPE_DOUBLE:
out[0] |= PROTOBUF_C_WIRE_TYPE_64BIT; out[0] |= PROTOBUF_C_WIRE_TYPE_64BIT;
return rv + fixed64_pack(*(const uint64_t *) member, out + rv); return rv + fixed64_pack(*(const uint64_t *)member, out + rv);
case PROTOBUF_C_TYPE_BOOL: case PROTOBUF_C_TYPE_BOOL:
out[0] |= PROTOBUF_C_WIRE_TYPE_VARINT; out[0] |= PROTOBUF_C_WIRE_TYPE_VARINT;
return rv + boolean_pack(*(const protobuf_c_boolean *) member, out + rv); return rv + boolean_pack(*(const protobuf_c_boolean *)member, out + rv);
case PROTOBUF_C_TYPE_STRING: case PROTOBUF_C_TYPE_STRING:
out[0] |= PROTOBUF_C_WIRE_TYPE_LENGTH_PREFIXED; out[0] |= PROTOBUF_C_WIRE_TYPE_LENGTH_PREFIXED;
return rv + string_pack(*(char *const *) member, out + rv); return rv + string_pack(*(char *const *)member, out + rv);
case PROTOBUF_C_TYPE_BYTES: case PROTOBUF_C_TYPE_BYTES:
out[0] |= PROTOBUF_C_WIRE_TYPE_LENGTH_PREFIXED; out[0] |= PROTOBUF_C_WIRE_TYPE_LENGTH_PREFIXED;
return rv + binary_data_pack((const ProtobufCBinaryData *) member, out + rv); return rv +
binary_data_pack((const ProtobufCBinaryData *)member, out + rv);
case PROTOBUF_C_TYPE_MESSAGE: case PROTOBUF_C_TYPE_MESSAGE:
out[0] |= PROTOBUF_C_WIRE_TYPE_LENGTH_PREFIXED; out[0] |= PROTOBUF_C_WIRE_TYPE_LENGTH_PREFIXED;
return rv + prefixed_message_pack(*(ProtobufCMessage * const *) member, out + rv); return rv + prefixed_message_pack(*(ProtobufCMessage *const *)member,
out + rv);
} }
PROTOBUF_C__ASSERT_NOT_REACHED(); PROTOBUF_C__ASSERT_NOT_REACHED();
return 0; return 0;
...@@ -1151,20 +1041,16 @@ required_field_pack(const ProtobufCFieldDescriptor *field, ...@@ -1151,20 +1041,16 @@ required_field_pack(const ProtobufCFieldDescriptor *field,
* \return * \return
* Number of bytes written to `out`. * Number of bytes written to `out`.
*/ */
static size_t static size_t oneof_field_pack(const ProtobufCFieldDescriptor *field,
oneof_field_pack(const ProtobufCFieldDescriptor *field, uint32_t oneof_case, const void *member,
uint32_t oneof_case, uint8_t *out) {
const void *member, uint8_t *out)
{
if (oneof_case != field->id) { if (oneof_case != field->id) {
return 0; return 0;
} }
if (field->type == PROTOBUF_C_TYPE_MESSAGE || if (field->type == PROTOBUF_C_TYPE_MESSAGE ||
field->type == PROTOBUF_C_TYPE_STRING) field->type == PROTOBUF_C_TYPE_STRING) {
{ const void *ptr = *(const void *const *)member;
const void *ptr = *(const void * const *) member; if (ptr == NULL || ptr == field->default_value) return 0;
if (ptr == NULL || ptr == field->default_value)
return 0;
} }
return required_field_pack(field, member, out); return required_field_pack(field, member, out);
} }
...@@ -1183,20 +1069,15 @@ oneof_field_pack(const ProtobufCFieldDescriptor *field, ...@@ -1183,20 +1069,15 @@ oneof_field_pack(const ProtobufCFieldDescriptor *field,
* \return * \return
* Number of bytes written to `out`. * Number of bytes written to `out`.
*/ */
static size_t static size_t optional_field_pack(const ProtobufCFieldDescriptor *field,
optional_field_pack(const ProtobufCFieldDescriptor *field,
const protobuf_c_boolean has, const protobuf_c_boolean has,
const void *member, uint8_t *out) const void *member, uint8_t *out) {
{
if (field->type == PROTOBUF_C_TYPE_MESSAGE || if (field->type == PROTOBUF_C_TYPE_MESSAGE ||
field->type == PROTOBUF_C_TYPE_STRING) field->type == PROTOBUF_C_TYPE_STRING) {
{ const void *ptr = *(const void *const *)member;
const void *ptr = *(const void * const *) member; if (ptr == NULL || ptr == field->default_value) return 0;
if (ptr == NULL || ptr == field->default_value)
return 0;
} else { } else {
if (!has) if (!has) return 0;
return 0;
} }
return required_field_pack(field, member, out); return required_field_pack(field, member, out);
} }
...@@ -1213,12 +1094,9 @@ optional_field_pack(const ProtobufCFieldDescriptor *field, ...@@ -1213,12 +1094,9 @@ optional_field_pack(const ProtobufCFieldDescriptor *field,
* \return * \return
* Number of bytes written to `out`. * Number of bytes written to `out`.
*/ */
static size_t static size_t unlabeled_field_pack(const ProtobufCFieldDescriptor *field,
unlabeled_field_pack(const ProtobufCFieldDescriptor *field, const void *member, uint8_t *out) {
const void *member, uint8_t *out) if (field_is_zeroish(field, member)) return 0;
{
if (field_is_zeroish(field, member))
return 0;
return required_field_pack(field, member, out); return required_field_pack(field, member, out);
} }
...@@ -1232,9 +1110,7 @@ unlabeled_field_pack(const ProtobufCFieldDescriptor *field, ...@@ -1232,9 +1110,7 @@ unlabeled_field_pack(const ProtobufCFieldDescriptor *field,
* \return * \return
* Size of the field. * Size of the field.
*/ */
static inline size_t static inline size_t sizeof_elt_in_repeated_array(ProtobufCType type) {
sizeof_elt_in_repeated_array(ProtobufCType type)
{
switch (type) { switch (type) {
case PROTOBUF_C_TYPE_SINT32: case PROTOBUF_C_TYPE_SINT32:
case PROTOBUF_C_TYPE_INT32: case PROTOBUF_C_TYPE_INT32:
...@@ -1273,16 +1149,14 @@ sizeof_elt_in_repeated_array(ProtobufCType type) ...@@ -1273,16 +1149,14 @@ sizeof_elt_in_repeated_array(ProtobufCType type)
* \param[in] n * \param[in] n
* Number of elements in the source array. * Number of elements in the source array.
*/ */
static void static void copy_to_little_endian_32(void *out, const void *in,
copy_to_little_endian_32(void *out, const void *in, const unsigned n) const unsigned n) {
{
#if !defined(WORDS_BIGENDIAN) #if !defined(WORDS_BIGENDIAN)
memcpy(out, in, n * 4); memcpy(out, in, n * 4);
#else #else
unsigned i; unsigned i;
const uint32_t *ini = in; const uint32_t *ini = in;
for (i = 0; i < n; i++) for (i = 0; i < n; i++) fixed32_pack(ini[i], (uint32_t *)out + i);
fixed32_pack(ini[i], (uint32_t *) out + i);
#endif #endif
} }
...@@ -1296,16 +1170,14 @@ copy_to_little_endian_32(void *out, const void *in, const unsigned n) ...@@ -1296,16 +1170,14 @@ copy_to_little_endian_32(void *out, const void *in, const unsigned n)
* \param[in] n * \param[in] n
* Number of elements in the source array. * Number of elements in the source array.
*/ */
static void static void copy_to_little_endian_64(void *out, const void *in,
copy_to_little_endian_64(void *out, const void *in, const unsigned n) const unsigned n) {
{
#if !defined(WORDS_BIGENDIAN) #if !defined(WORDS_BIGENDIAN)
memcpy(out, in, n * 8); memcpy(out, in, n * 8);
#else #else
unsigned i; unsigned i;
const uint64_t *ini = in; const uint64_t *ini = in;
for (i = 0; i < n; i++) for (i = 0; i < n; i++) fixed64_pack(ini[i], (uint64_t *)out + i);
fixed64_pack(ini[i], (uint64_t *) out + i);
#endif #endif
} }
...@@ -1318,19 +1190,13 @@ copy_to_little_endian_64(void *out, const void *in, const unsigned n) ...@@ -1318,19 +1190,13 @@ copy_to_little_endian_64(void *out, const void *in, const unsigned n)
* \return * \return
* Number of bytes. * Number of bytes.
*/ */
static unsigned static unsigned get_type_min_size(ProtobufCType type) {
get_type_min_size(ProtobufCType type) if (type == PROTOBUF_C_TYPE_SFIXED32 || type == PROTOBUF_C_TYPE_FIXED32 ||
{ type == PROTOBUF_C_TYPE_FLOAT) {
if (type == PROTOBUF_C_TYPE_SFIXED32 ||
type == PROTOBUF_C_TYPE_FIXED32 ||
type == PROTOBUF_C_TYPE_FLOAT)
{
return 4; return 4;
} }
if (type == PROTOBUF_C_TYPE_SFIXED64 || if (type == PROTOBUF_C_TYPE_SFIXED64 || type == PROTOBUF_C_TYPE_FIXED64 ||
type == PROTOBUF_C_TYPE_FIXED64 || type == PROTOBUF_C_TYPE_DOUBLE) {
type == PROTOBUF_C_TYPE_DOUBLE)
{
return 8; return 8;
} }
return 1; return 1;
...@@ -1351,11 +1217,10 @@ get_type_min_size(ProtobufCType type) ...@@ -1351,11 +1217,10 @@ get_type_min_size(ProtobufCType type)
* \return * \return
* Number of bytes serialised to `out`. * Number of bytes serialised to `out`.
*/ */
static size_t static size_t repeated_field_pack(const ProtobufCFieldDescriptor *field,
repeated_field_pack(const ProtobufCFieldDescriptor *field, size_t count, const void *member,
size_t count, const void *member, uint8_t *out) uint8_t *out) {
{ void *array = *(void *const *)member;
void *array = *(void * const *) member;
unsigned i; unsigned i;
if (0 != (field->flags & PROTOBUF_C_FIELD_FLAG_PACKED)) { if (0 != (field->flags & PROTOBUF_C_FIELD_FLAG_PACKED)) {
...@@ -1367,8 +1232,7 @@ repeated_field_pack(const ProtobufCFieldDescriptor *field, ...@@ -1367,8 +1232,7 @@ repeated_field_pack(const ProtobufCFieldDescriptor *field,
unsigned actual_length_size; unsigned actual_length_size;
uint8_t *payload_at; uint8_t *payload_at;
if (count == 0) if (count == 0) return 0;
return 0;
header_len = tag_pack(field->id, out); header_len = tag_pack(field->id, out);
out[0] |= PROTOBUF_C_WIRE_TYPE_LENGTH_PREFIXED; out[0] |= PROTOBUF_C_WIRE_TYPE_LENGTH_PREFIXED;
len_start = header_len; len_start = header_len;
...@@ -1392,38 +1256,38 @@ repeated_field_pack(const ProtobufCFieldDescriptor *field, ...@@ -1392,38 +1256,38 @@ repeated_field_pack(const ProtobufCFieldDescriptor *field,
break; break;
case PROTOBUF_C_TYPE_ENUM: case PROTOBUF_C_TYPE_ENUM:
case PROTOBUF_C_TYPE_INT32: { case PROTOBUF_C_TYPE_INT32: {
const int32_t *arr = (const int32_t *) array; const int32_t *arr = (const int32_t *)array;
for (i = 0; i < count; i++) for (i = 0; i < count; i++)
payload_at += int32_pack(arr[i], payload_at); payload_at += int32_pack(arr[i], payload_at);
break; break;
} }
case PROTOBUF_C_TYPE_SINT32: { case PROTOBUF_C_TYPE_SINT32: {
const int32_t *arr = (const int32_t *) array; const int32_t *arr = (const int32_t *)array;
for (i = 0; i < count; i++) for (i = 0; i < count; i++)
payload_at += sint32_pack(arr[i], payload_at); payload_at += sint32_pack(arr[i], payload_at);
break; break;
} }
case PROTOBUF_C_TYPE_SINT64: { case PROTOBUF_C_TYPE_SINT64: {
const int64_t *arr = (const int64_t *) array; const int64_t *arr = (const int64_t *)array;
for (i = 0; i < count; i++) for (i = 0; i < count; i++)
payload_at += sint64_pack(arr[i], payload_at); payload_at += sint64_pack(arr[i], payload_at);
break; break;
} }
case PROTOBUF_C_TYPE_UINT32: { case PROTOBUF_C_TYPE_UINT32: {
const uint32_t *arr = (const uint32_t *) array; const uint32_t *arr = (const uint32_t *)array;
for (i = 0; i < count; i++) for (i = 0; i < count; i++)
payload_at += uint32_pack(arr[i], payload_at); payload_at += uint32_pack(arr[i], payload_at);
break; break;
} }
case PROTOBUF_C_TYPE_INT64: case PROTOBUF_C_TYPE_INT64:
case PROTOBUF_C_TYPE_UINT64: { case PROTOBUF_C_TYPE_UINT64: {
const uint64_t *arr = (const uint64_t *) array; const uint64_t *arr = (const uint64_t *)array;
for (i = 0; i < count; i++) for (i = 0; i < count; i++)
payload_at += uint64_pack(arr[i], payload_at); payload_at += uint64_pack(arr[i], payload_at);
break; break;
} }
case PROTOBUF_C_TYPE_BOOL: { case PROTOBUF_C_TYPE_BOOL: {
const protobuf_c_boolean *arr = (const protobuf_c_boolean *) array; const protobuf_c_boolean *arr = (const protobuf_c_boolean *)array;
for (i = 0; i < count; i++) for (i = 0; i < count; i++)
payload_at += boolean_pack(arr[i], payload_at); payload_at += boolean_pack(arr[i], payload_at);
break; break;
...@@ -1436,15 +1300,15 @@ repeated_field_pack(const ProtobufCFieldDescriptor *field, ...@@ -1436,15 +1300,15 @@ repeated_field_pack(const ProtobufCFieldDescriptor *field,
actual_length_size = uint32_size(payload_len); actual_length_size = uint32_size(payload_len);
if (length_size_min != actual_length_size) { if (length_size_min != actual_length_size) {
assert(actual_length_size == length_size_min + 1); assert(actual_length_size == length_size_min + 1);
memmove(out + header_len + 1, out + header_len, memmove(out + header_len + 1, out + header_len, payload_len);
payload_len);
header_len++; header_len++;
} }
uint32_pack(payload_len, out + len_start); uint32_pack(payload_len, out + len_start);
return header_len + payload_len; return header_len + payload_len;
} else { } else {
/* not "packed" cased */ /* not "packed" cased */
/* CONSIDER: optimize this case a bit (by putting the loop inside the switch) */ /* CONSIDER: optimize this case a bit (by putting the loop inside the
* switch) */
size_t rv = 0; size_t rv = 0;
unsigned siz = sizeof_elt_in_repeated_array(field->type); unsigned siz = sizeof_elt_in_repeated_array(field->type);
...@@ -1456,9 +1320,8 @@ repeated_field_pack(const ProtobufCFieldDescriptor *field, ...@@ -1456,9 +1320,8 @@ repeated_field_pack(const ProtobufCFieldDescriptor *field,
} }
} }
static size_t static size_t unknown_field_pack(const ProtobufCMessageUnknownField *field,
unknown_field_pack(const ProtobufCMessageUnknownField *field, uint8_t *out) uint8_t *out) {
{
size_t rv = tag_pack(field->tag, out); size_t rv = tag_pack(field->tag, out);
out[0] |= field->wire_type; out[0] |= field->wire_type;
memcpy(out + rv, field->data, field->len); memcpy(out + rv, field->data, field->len);
...@@ -1467,17 +1330,14 @@ unknown_field_pack(const ProtobufCMessageUnknownField *field, uint8_t *out) ...@@ -1467,17 +1330,14 @@ unknown_field_pack(const ProtobufCMessageUnknownField *field, uint8_t *out)
/**@}*/ /**@}*/
size_t size_t protobuf_c_message_pack(const ProtobufCMessage *message, uint8_t *out) {
protobuf_c_message_pack(const ProtobufCMessage *message, uint8_t *out)
{
unsigned i; unsigned i;
size_t rv = 0; size_t rv = 0;
ASSERT_IS_MESSAGE(message); ASSERT_IS_MESSAGE(message);
for (i = 0; i < message->descriptor->n_fields; i++) { for (i = 0; i < message->descriptor->n_fields; i++) {
const ProtobufCFieldDescriptor *field = const ProtobufCFieldDescriptor *field = message->descriptor->fields + i;
message->descriptor->fields + i; const void *member = ((const char *)message) + field->offset;
const void *member = ((const char *) message) + field->offset;
/* /*
* It doesn't hurt to compute qmember (a pointer to the * It doesn't hurt to compute qmember (a pointer to the
...@@ -1488,32 +1348,23 @@ protobuf_c_message_pack(const ProtobufCMessage *message, uint8_t *out) ...@@ -1488,32 +1348,23 @@ protobuf_c_message_pack(const ProtobufCMessage *message, uint8_t *out)
* - an optional field that isn't a pointer type * - an optional field that isn't a pointer type
* (Meaning: not a message or a string). * (Meaning: not a message or a string).
*/ */
const void *qmember = const void *qmember = ((const char *)message) + field->quantifier_offset;
((const char *) message) + field->quantifier_offset;
if (field->label == PROTOBUF_C_LABEL_REQUIRED) { if (field->label == PROTOBUF_C_LABEL_REQUIRED) {
rv += required_field_pack(field, member, out + rv); rv += required_field_pack(field, member, out + rv);
} else if ((field->label == PROTOBUF_C_LABEL_OPTIONAL || } else if ((field->label == PROTOBUF_C_LABEL_OPTIONAL ||
field->label == PROTOBUF_C_LABEL_NONE) && field->label == PROTOBUF_C_LABEL_NONE) &&
(0 != (field->flags & PROTOBUF_C_FIELD_FLAG_ONEOF))) { (0 != (field->flags & PROTOBUF_C_FIELD_FLAG_ONEOF))) {
rv += oneof_field_pack( rv +=
field, oneof_field_pack(field, *(const uint32_t *)qmember, member, out + rv);
*(const uint32_t *) qmember,
member,
out + rv
);
} else if (field->label == PROTOBUF_C_LABEL_OPTIONAL) { } else if (field->label == PROTOBUF_C_LABEL_OPTIONAL) {
rv += optional_field_pack( rv += optional_field_pack(field, *(const protobuf_c_boolean *)qmember,
field, member, out + rv);
*(const protobuf_c_boolean *) qmember,
member,
out + rv
);
} else if (field->label == PROTOBUF_C_LABEL_NONE) { } else if (field->label == PROTOBUF_C_LABEL_NONE) {
rv += unlabeled_field_pack(field, member, out + rv); rv += unlabeled_field_pack(field, member, out + rv);
} else { } else {
rv += repeated_field_pack(field, *(const size_t *) qmember, rv += repeated_field_pack(field, *(const size_t *)qmember, member,
member, out + rv); out + rv);
} }
} }
for (i = 0; i < message->n_unknown_fields; i++) for (i = 0; i < message->n_unknown_fields; i++)
...@@ -1542,10 +1393,9 @@ protobuf_c_message_pack(const ProtobufCMessage *message, uint8_t *out) ...@@ -1542,10 +1393,9 @@ protobuf_c_message_pack(const ProtobufCMessage *message, uint8_t *out)
* \return * \return
* Number of bytes packed. * Number of bytes packed.
*/ */
static size_t static size_t required_field_pack_to_buffer(
required_field_pack_to_buffer(const ProtobufCFieldDescriptor *field, const ProtobufCFieldDescriptor *field, const void *member,
const void *member, ProtobufCBuffer *buffer) ProtobufCBuffer *buffer) {
{
size_t rv; size_t rv;
uint8_t scratch[MAX_UINT64_ENCODED_SIZE * 2]; uint8_t scratch[MAX_UINT64_ENCODED_SIZE * 2];
...@@ -1553,63 +1403,63 @@ required_field_pack_to_buffer(const ProtobufCFieldDescriptor *field, ...@@ -1553,63 +1403,63 @@ required_field_pack_to_buffer(const ProtobufCFieldDescriptor *field,
switch (field->type) { switch (field->type) {
case PROTOBUF_C_TYPE_SINT32: case PROTOBUF_C_TYPE_SINT32:
scratch[0] |= PROTOBUF_C_WIRE_TYPE_VARINT; scratch[0] |= PROTOBUF_C_WIRE_TYPE_VARINT;
rv += sint32_pack(*(const int32_t *) member, scratch + rv); rv += sint32_pack(*(const int32_t *)member, scratch + rv);
buffer->append(buffer, rv, scratch); buffer->append(buffer, rv, scratch);
break; break;
case PROTOBUF_C_TYPE_ENUM: case PROTOBUF_C_TYPE_ENUM:
case PROTOBUF_C_TYPE_INT32: case PROTOBUF_C_TYPE_INT32:
scratch[0] |= PROTOBUF_C_WIRE_TYPE_VARINT; scratch[0] |= PROTOBUF_C_WIRE_TYPE_VARINT;
rv += int32_pack(*(const int32_t *) member, scratch + rv); rv += int32_pack(*(const int32_t *)member, scratch + rv);
buffer->append(buffer, rv, scratch); buffer->append(buffer, rv, scratch);
break; break;
case PROTOBUF_C_TYPE_UINT32: case PROTOBUF_C_TYPE_UINT32:
scratch[0] |= PROTOBUF_C_WIRE_TYPE_VARINT; scratch[0] |= PROTOBUF_C_WIRE_TYPE_VARINT;
rv += uint32_pack(*(const uint32_t *) member, scratch + rv); rv += uint32_pack(*(const uint32_t *)member, scratch + rv);
buffer->append(buffer, rv, scratch); buffer->append(buffer, rv, scratch);
break; break;
case PROTOBUF_C_TYPE_SINT64: case PROTOBUF_C_TYPE_SINT64:
scratch[0] |= PROTOBUF_C_WIRE_TYPE_VARINT; scratch[0] |= PROTOBUF_C_WIRE_TYPE_VARINT;
rv += sint64_pack(*(const int64_t *) member, scratch + rv); rv += sint64_pack(*(const int64_t *)member, scratch + rv);
buffer->append(buffer, rv, scratch); buffer->append(buffer, rv, scratch);
break; break;
case PROTOBUF_C_TYPE_INT64: case PROTOBUF_C_TYPE_INT64:
case PROTOBUF_C_TYPE_UINT64: case PROTOBUF_C_TYPE_UINT64:
scratch[0] |= PROTOBUF_C_WIRE_TYPE_VARINT; scratch[0] |= PROTOBUF_C_WIRE_TYPE_VARINT;
rv += uint64_pack(*(const uint64_t *) member, scratch + rv); rv += uint64_pack(*(const uint64_t *)member, scratch + rv);
buffer->append(buffer, rv, scratch); buffer->append(buffer, rv, scratch);
break; break;
case PROTOBUF_C_TYPE_SFIXED32: case PROTOBUF_C_TYPE_SFIXED32:
case PROTOBUF_C_TYPE_FIXED32: case PROTOBUF_C_TYPE_FIXED32:
case PROTOBUF_C_TYPE_FLOAT: case PROTOBUF_C_TYPE_FLOAT:
scratch[0] |= PROTOBUF_C_WIRE_TYPE_32BIT; scratch[0] |= PROTOBUF_C_WIRE_TYPE_32BIT;
rv += fixed32_pack(*(const uint32_t *) member, scratch + rv); rv += fixed32_pack(*(const uint32_t *)member, scratch + rv);
buffer->append(buffer, rv, scratch); buffer->append(buffer, rv, scratch);
break; break;
case PROTOBUF_C_TYPE_SFIXED64: case PROTOBUF_C_TYPE_SFIXED64:
case PROTOBUF_C_TYPE_FIXED64: case PROTOBUF_C_TYPE_FIXED64:
case PROTOBUF_C_TYPE_DOUBLE: case PROTOBUF_C_TYPE_DOUBLE:
scratch[0] |= PROTOBUF_C_WIRE_TYPE_64BIT; scratch[0] |= PROTOBUF_C_WIRE_TYPE_64BIT;
rv += fixed64_pack(*(const uint64_t *) member, scratch + rv); rv += fixed64_pack(*(const uint64_t *)member, scratch + rv);
buffer->append(buffer, rv, scratch); buffer->append(buffer, rv, scratch);
break; break;
case PROTOBUF_C_TYPE_BOOL: case PROTOBUF_C_TYPE_BOOL:
scratch[0] |= PROTOBUF_C_WIRE_TYPE_VARINT; scratch[0] |= PROTOBUF_C_WIRE_TYPE_VARINT;
rv += boolean_pack(*(const protobuf_c_boolean *) member, scratch + rv); rv += boolean_pack(*(const protobuf_c_boolean *)member, scratch + rv);
buffer->append(buffer, rv, scratch); buffer->append(buffer, rv, scratch);
break; break;
case PROTOBUF_C_TYPE_STRING: { case PROTOBUF_C_TYPE_STRING: {
const char *str = *(char *const *) member; const char *str = *(char *const *)member;
size_t sublen = str ? strlen(str) : 0; size_t sublen = str ? strlen(str) : 0;
scratch[0] |= PROTOBUF_C_WIRE_TYPE_LENGTH_PREFIXED; scratch[0] |= PROTOBUF_C_WIRE_TYPE_LENGTH_PREFIXED;
rv += uint32_pack(sublen, scratch + rv); rv += uint32_pack(sublen, scratch + rv);
buffer->append(buffer, rv, scratch); buffer->append(buffer, rv, scratch);
buffer->append(buffer, sublen, (const uint8_t *) str); buffer->append(buffer, sublen, (const uint8_t *)str);
rv += sublen; rv += sublen;
break; break;
} }
case PROTOBUF_C_TYPE_BYTES: { case PROTOBUF_C_TYPE_BYTES: {
const ProtobufCBinaryData *bd = ((const ProtobufCBinaryData *) member); const ProtobufCBinaryData *bd = ((const ProtobufCBinaryData *)member);
size_t sublen = bd->len; size_t sublen = bd->len;
scratch[0] |= PROTOBUF_C_WIRE_TYPE_LENGTH_PREFIXED; scratch[0] |= PROTOBUF_C_WIRE_TYPE_LENGTH_PREFIXED;
...@@ -1622,7 +1472,7 @@ required_field_pack_to_buffer(const ProtobufCFieldDescriptor *field, ...@@ -1622,7 +1472,7 @@ required_field_pack_to_buffer(const ProtobufCFieldDescriptor *field,
case PROTOBUF_C_TYPE_MESSAGE: { case PROTOBUF_C_TYPE_MESSAGE: {
uint8_t simple_buffer_scratch[256]; uint8_t simple_buffer_scratch[256];
size_t sublen; size_t sublen;
const ProtobufCMessage *msg = *(ProtobufCMessage * const *) member; const ProtobufCMessage *msg = *(ProtobufCMessage *const *)member;
ProtobufCBufferSimple simple_buffer = ProtobufCBufferSimple simple_buffer =
PROTOBUF_C_BUFFER_SIMPLE_INIT(simple_buffer_scratch); PROTOBUF_C_BUFFER_SIMPLE_INIT(simple_buffer_scratch);
...@@ -1645,7 +1495,8 @@ required_field_pack_to_buffer(const ProtobufCFieldDescriptor *field, ...@@ -1645,7 +1495,8 @@ required_field_pack_to_buffer(const ProtobufCFieldDescriptor *field,
} }
/** /**
* Pack a oneof field to a buffer. Only packs the field that is selected by the case enum. * Pack a oneof field to a buffer. Only packs the field that is selected by the
* case enum.
* *
* \param field * \param field
* Field descriptor. * Field descriptor.
...@@ -1658,20 +1509,17 @@ required_field_pack_to_buffer(const ProtobufCFieldDescriptor *field, ...@@ -1658,20 +1509,17 @@ required_field_pack_to_buffer(const ProtobufCFieldDescriptor *field,
* \return * \return
* Number of bytes serialised to `buffer`. * Number of bytes serialised to `buffer`.
*/ */
static size_t static size_t oneof_field_pack_to_buffer(const ProtobufCFieldDescriptor *field,
oneof_field_pack_to_buffer(const ProtobufCFieldDescriptor *field,
uint32_t oneof_case, uint32_t oneof_case,
const void *member, ProtobufCBuffer *buffer) const void *member,
{ ProtobufCBuffer *buffer) {
if (oneof_case != field->id) { if (oneof_case != field->id) {
return 0; return 0;
} }
if (field->type == PROTOBUF_C_TYPE_MESSAGE || if (field->type == PROTOBUF_C_TYPE_MESSAGE ||
field->type == PROTOBUF_C_TYPE_STRING) field->type == PROTOBUF_C_TYPE_STRING) {
{ const void *ptr = *(const void *const *)member;
const void *ptr = *(const void *const *) member; if (ptr == NULL || ptr == field->default_value) return 0;
if (ptr == NULL || ptr == field->default_value)
return 0;
} }
return required_field_pack_to_buffer(field, member, buffer); return required_field_pack_to_buffer(field, member, buffer);
} }
...@@ -1690,20 +1538,15 @@ oneof_field_pack_to_buffer(const ProtobufCFieldDescriptor *field, ...@@ -1690,20 +1538,15 @@ oneof_field_pack_to_buffer(const ProtobufCFieldDescriptor *field,
* \return * \return
* Number of bytes serialised to `buffer`. * Number of bytes serialised to `buffer`.
*/ */
static size_t static size_t optional_field_pack_to_buffer(
optional_field_pack_to_buffer(const ProtobufCFieldDescriptor *field, const ProtobufCFieldDescriptor *field, const protobuf_c_boolean has,
const protobuf_c_boolean has, const void *member, ProtobufCBuffer *buffer) {
const void *member, ProtobufCBuffer *buffer)
{
if (field->type == PROTOBUF_C_TYPE_MESSAGE || if (field->type == PROTOBUF_C_TYPE_MESSAGE ||
field->type == PROTOBUF_C_TYPE_STRING) field->type == PROTOBUF_C_TYPE_STRING) {
{ const void *ptr = *(const void *const *)member;
const void *ptr = *(const void *const *) member; if (ptr == NULL || ptr == field->default_value) return 0;
if (ptr == NULL || ptr == field->default_value)
return 0;
} else { } else {
if (!has) if (!has) return 0;
return 0;
} }
return required_field_pack_to_buffer(field, member, buffer); return required_field_pack_to_buffer(field, member, buffer);
} }
...@@ -1720,12 +1563,10 @@ optional_field_pack_to_buffer(const ProtobufCFieldDescriptor *field, ...@@ -1720,12 +1563,10 @@ optional_field_pack_to_buffer(const ProtobufCFieldDescriptor *field,
* \return * \return
* Number of bytes serialised to `buffer`. * Number of bytes serialised to `buffer`.
*/ */
static size_t static size_t unlabeled_field_pack_to_buffer(
unlabeled_field_pack_to_buffer(const ProtobufCFieldDescriptor *field, const ProtobufCFieldDescriptor *field, const void *member,
const void *member, ProtobufCBuffer *buffer) ProtobufCBuffer *buffer) {
{ if (field_is_zeroish(field, member)) return 0;
if (field_is_zeroish(field, member))
return 0;
return required_field_pack_to_buffer(field, member, buffer); return required_field_pack_to_buffer(field, member, buffer);
} }
...@@ -1741,10 +1582,8 @@ unlabeled_field_pack_to_buffer(const ProtobufCFieldDescriptor *field, ...@@ -1741,10 +1582,8 @@ unlabeled_field_pack_to_buffer(const ProtobufCFieldDescriptor *field,
* \return * \return
* Number of bytes required. * Number of bytes required.
*/ */
static size_t static size_t get_packed_payload_length(const ProtobufCFieldDescriptor *field,
get_packed_payload_length(const ProtobufCFieldDescriptor *field, unsigned count, const void *array) {
unsigned count, const void *array)
{
unsigned rv = 0; unsigned rv = 0;
unsigned i; unsigned i;
...@@ -1759,34 +1598,29 @@ get_packed_payload_length(const ProtobufCFieldDescriptor *field, ...@@ -1759,34 +1598,29 @@ get_packed_payload_length(const ProtobufCFieldDescriptor *field,
return count * 8; return count * 8;
case PROTOBUF_C_TYPE_ENUM: case PROTOBUF_C_TYPE_ENUM:
case PROTOBUF_C_TYPE_INT32: { case PROTOBUF_C_TYPE_INT32: {
const int32_t *arr = (const int32_t *) array; const int32_t *arr = (const int32_t *)array;
for (i = 0; i < count; i++) for (i = 0; i < count; i++) rv += int32_size(arr[i]);
rv += int32_size(arr[i]);
break; break;
} }
case PROTOBUF_C_TYPE_SINT32: { case PROTOBUF_C_TYPE_SINT32: {
const int32_t *arr = (const int32_t *) array; const int32_t *arr = (const int32_t *)array;
for (i = 0; i < count; i++) for (i = 0; i < count; i++) rv += sint32_size(arr[i]);
rv += sint32_size(arr[i]);
break; break;
} }
case PROTOBUF_C_TYPE_UINT32: { case PROTOBUF_C_TYPE_UINT32: {
const uint32_t *arr = (const uint32_t *) array; const uint32_t *arr = (const uint32_t *)array;
for (i = 0; i < count; i++) for (i = 0; i < count; i++) rv += uint32_size(arr[i]);
rv += uint32_size(arr[i]);
break; break;
} }
case PROTOBUF_C_TYPE_SINT64: { case PROTOBUF_C_TYPE_SINT64: {
const int64_t *arr = (const int64_t *) array; const int64_t *arr = (const int64_t *)array;
for (i = 0; i < count; i++) for (i = 0; i < count; i++) rv += sint64_size(arr[i]);
rv += sint64_size(arr[i]);
break; break;
} }
case PROTOBUF_C_TYPE_INT64: case PROTOBUF_C_TYPE_INT64:
case PROTOBUF_C_TYPE_UINT64: { case PROTOBUF_C_TYPE_UINT64: {
const uint64_t *arr = (const uint64_t *) array; const uint64_t *arr = (const uint64_t *)array;
for (i = 0; i < count; i++) for (i = 0; i < count; i++) rv += uint64_size(arr[i]);
rv += uint64_size(arr[i]);
break; break;
} }
case PROTOBUF_C_TYPE_BOOL: case PROTOBUF_C_TYPE_BOOL:
...@@ -1811,11 +1645,9 @@ get_packed_payload_length(const ProtobufCFieldDescriptor *field, ...@@ -1811,11 +1645,9 @@ get_packed_payload_length(const ProtobufCFieldDescriptor *field,
* \return * \return
* Number of bytes packed. * Number of bytes packed.
*/ */
static size_t static size_t pack_buffer_packed_payload(const ProtobufCFieldDescriptor *field,
pack_buffer_packed_payload(const ProtobufCFieldDescriptor *field,
unsigned count, const void *array, unsigned count, const void *array,
ProtobufCBuffer *buffer) ProtobufCBuffer *buffer) {
{
uint8_t scratch[16]; uint8_t scratch[16];
size_t rv = 0; size_t rv = 0;
unsigned i; unsigned i;
...@@ -1829,7 +1661,7 @@ pack_buffer_packed_payload(const ProtobufCFieldDescriptor *field, ...@@ -1829,7 +1661,7 @@ pack_buffer_packed_payload(const ProtobufCFieldDescriptor *field,
goto no_packing_needed; goto no_packing_needed;
#else #else
for (i = 0; i < count; i++) { for (i = 0; i < count; i++) {
unsigned len = fixed32_pack(((uint32_t *) array)[i], scratch); unsigned len = fixed32_pack(((uint32_t *)array)[i], scratch);
buffer->append(buffer, len, scratch); buffer->append(buffer, len, scratch);
rv += len; rv += len;
} }
...@@ -1843,7 +1675,7 @@ pack_buffer_packed_payload(const ProtobufCFieldDescriptor *field, ...@@ -1843,7 +1675,7 @@ pack_buffer_packed_payload(const ProtobufCFieldDescriptor *field,
goto no_packing_needed; goto no_packing_needed;
#else #else
for (i = 0; i < count; i++) { for (i = 0; i < count; i++) {
unsigned len = fixed64_pack(((uint64_t *) array)[i], scratch); unsigned len = fixed64_pack(((uint64_t *)array)[i], scratch);
buffer->append(buffer, len, scratch); buffer->append(buffer, len, scratch);
rv += len; rv += len;
} }
...@@ -1852,28 +1684,28 @@ pack_buffer_packed_payload(const ProtobufCFieldDescriptor *field, ...@@ -1852,28 +1684,28 @@ pack_buffer_packed_payload(const ProtobufCFieldDescriptor *field,
case PROTOBUF_C_TYPE_ENUM: case PROTOBUF_C_TYPE_ENUM:
case PROTOBUF_C_TYPE_INT32: case PROTOBUF_C_TYPE_INT32:
for (i = 0; i < count; i++) { for (i = 0; i < count; i++) {
unsigned len = int32_pack(((int32_t *) array)[i], scratch); unsigned len = int32_pack(((int32_t *)array)[i], scratch);
buffer->append(buffer, len, scratch); buffer->append(buffer, len, scratch);
rv += len; rv += len;
} }
break; break;
case PROTOBUF_C_TYPE_SINT32: case PROTOBUF_C_TYPE_SINT32:
for (i = 0; i < count; i++) { for (i = 0; i < count; i++) {
unsigned len = sint32_pack(((int32_t *) array)[i], scratch); unsigned len = sint32_pack(((int32_t *)array)[i], scratch);
buffer->append(buffer, len, scratch); buffer->append(buffer, len, scratch);
rv += len; rv += len;
} }
break; break;
case PROTOBUF_C_TYPE_UINT32: case PROTOBUF_C_TYPE_UINT32:
for (i = 0; i < count; i++) { for (i = 0; i < count; i++) {
unsigned len = uint32_pack(((uint32_t *) array)[i], scratch); unsigned len = uint32_pack(((uint32_t *)array)[i], scratch);
buffer->append(buffer, len, scratch); buffer->append(buffer, len, scratch);
rv += len; rv += len;
} }
break; break;
case PROTOBUF_C_TYPE_SINT64: case PROTOBUF_C_TYPE_SINT64:
for (i = 0; i < count; i++) { for (i = 0; i < count; i++) {
unsigned len = sint64_pack(((int64_t *) array)[i], scratch); unsigned len = sint64_pack(((int64_t *)array)[i], scratch);
buffer->append(buffer, len, scratch); buffer->append(buffer, len, scratch);
rv += len; rv += len;
} }
...@@ -1881,14 +1713,14 @@ pack_buffer_packed_payload(const ProtobufCFieldDescriptor *field, ...@@ -1881,14 +1713,14 @@ pack_buffer_packed_payload(const ProtobufCFieldDescriptor *field,
case PROTOBUF_C_TYPE_INT64: case PROTOBUF_C_TYPE_INT64:
case PROTOBUF_C_TYPE_UINT64: case PROTOBUF_C_TYPE_UINT64:
for (i = 0; i < count; i++) { for (i = 0; i < count; i++) {
unsigned len = uint64_pack(((uint64_t *) array)[i], scratch); unsigned len = uint64_pack(((uint64_t *)array)[i], scratch);
buffer->append(buffer, len, scratch); buffer->append(buffer, len, scratch);
rv += len; rv += len;
} }
break; break;
case PROTOBUF_C_TYPE_BOOL: case PROTOBUF_C_TYPE_BOOL:
for (i = 0; i < count; i++) { for (i = 0; i < count; i++) {
unsigned len = boolean_pack(((protobuf_c_boolean *) array)[i], scratch); unsigned len = boolean_pack(((protobuf_c_boolean *)array)[i], scratch);
buffer->append(buffer, len, scratch); buffer->append(buffer, len, scratch);
rv += len; rv += len;
} }
...@@ -1905,15 +1737,12 @@ no_packing_needed: ...@@ -1905,15 +1737,12 @@ no_packing_needed:
#endif #endif
} }
static size_t static size_t repeated_field_pack_to_buffer(
repeated_field_pack_to_buffer(const ProtobufCFieldDescriptor *field, const ProtobufCFieldDescriptor *field, unsigned count, const void *member,
unsigned count, const void *member, ProtobufCBuffer *buffer) {
ProtobufCBuffer *buffer) char *array = *(char *const *)member;
{
char *array = *(char * const *) member;
if (count == 0) if (count == 0) return 0;
return 0;
if (0 != (field->flags & PROTOBUF_C_FIELD_FLAG_PACKED)) { if (0 != (field->flags & PROTOBUF_C_FIELD_FLAG_PACKED)) {
uint8_t scratch[MAX_UINT64_ENCODED_SIZE * 2]; uint8_t scratch[MAX_UINT64_ENCODED_SIZE * 2];
size_t rv = tag_pack(field->id, scratch); size_t rv = tag_pack(field->id, scratch);
...@@ -1929,7 +1758,8 @@ repeated_field_pack_to_buffer(const ProtobufCFieldDescriptor *field, ...@@ -1929,7 +1758,8 @@ repeated_field_pack_to_buffer(const ProtobufCFieldDescriptor *field,
} else { } else {
size_t siz; size_t siz;
unsigned i; unsigned i;
/* CONSIDER: optimize this case a bit (by putting the loop inside the switch) */ /* CONSIDER: optimize this case a bit (by putting the loop inside the
* switch) */
unsigned rv = 0; unsigned rv = 0;
siz = sizeof_elt_in_repeated_array(field->type); siz = sizeof_elt_in_repeated_array(field->type);
...@@ -1941,10 +1771,8 @@ repeated_field_pack_to_buffer(const ProtobufCFieldDescriptor *field, ...@@ -1941,10 +1771,8 @@ repeated_field_pack_to_buffer(const ProtobufCFieldDescriptor *field,
} }
} }
static size_t static size_t unknown_field_pack_to_buffer(
unknown_field_pack_to_buffer(const ProtobufCMessageUnknownField *field, const ProtobufCMessageUnknownField *field, ProtobufCBuffer *buffer) {
ProtobufCBuffer *buffer)
{
uint8_t header[MAX_UINT64_ENCODED_SIZE]; uint8_t header[MAX_UINT64_ENCODED_SIZE];
size_t rv = tag_pack(field->tag, header); size_t rv = tag_pack(field->tag, header);
...@@ -1956,53 +1784,32 @@ unknown_field_pack_to_buffer(const ProtobufCMessageUnknownField *field, ...@@ -1956,53 +1784,32 @@ unknown_field_pack_to_buffer(const ProtobufCMessageUnknownField *field,
/**@}*/ /**@}*/
size_t size_t protobuf_c_message_pack_to_buffer(const ProtobufCMessage *message,
protobuf_c_message_pack_to_buffer(const ProtobufCMessage *message, ProtobufCBuffer *buffer) {
ProtobufCBuffer *buffer)
{
unsigned i; unsigned i;
size_t rv = 0; size_t rv = 0;
ASSERT_IS_MESSAGE(message); ASSERT_IS_MESSAGE(message);
for (i = 0; i < message->descriptor->n_fields; i++) { for (i = 0; i < message->descriptor->n_fields; i++) {
const ProtobufCFieldDescriptor *field = const ProtobufCFieldDescriptor *field = message->descriptor->fields + i;
message->descriptor->fields + i; const void *member = ((const char *)message) + field->offset;
const void *member = const void *qmember = ((const char *)message) + field->quantifier_offset;
((const char *) message) + field->offset;
const void *qmember =
((const char *) message) + field->quantifier_offset;
if (field->label == PROTOBUF_C_LABEL_REQUIRED) { if (field->label == PROTOBUF_C_LABEL_REQUIRED) {
rv += required_field_pack_to_buffer(field, member, buffer); rv += required_field_pack_to_buffer(field, member, buffer);
} else if ((field->label == PROTOBUF_C_LABEL_OPTIONAL || } else if ((field->label == PROTOBUF_C_LABEL_OPTIONAL ||
field->label == PROTOBUF_C_LABEL_NONE) && field->label == PROTOBUF_C_LABEL_NONE) &&
(0 != (field->flags & PROTOBUF_C_FIELD_FLAG_ONEOF))) { (0 != (field->flags & PROTOBUF_C_FIELD_FLAG_ONEOF))) {
rv += oneof_field_pack_to_buffer( rv += oneof_field_pack_to_buffer(field, *(const uint32_t *)qmember,
field, member, buffer);
*(const uint32_t *) qmember,
member,
buffer
);
} else if (field->label == PROTOBUF_C_LABEL_OPTIONAL) { } else if (field->label == PROTOBUF_C_LABEL_OPTIONAL) {
rv += optional_field_pack_to_buffer( rv += optional_field_pack_to_buffer(
field, field, *(const protobuf_c_boolean *)qmember, member, buffer);
*(const protobuf_c_boolean *) qmember,
member,
buffer
);
} else if (field->label == PROTOBUF_C_LABEL_NONE) { } else if (field->label == PROTOBUF_C_LABEL_NONE) {
rv += unlabeled_field_pack_to_buffer( rv += unlabeled_field_pack_to_buffer(field, member, buffer);
field,
member,
buffer
);
} else { } else {
rv += repeated_field_pack_to_buffer( rv += repeated_field_pack_to_buffer(field, *(const size_t *)qmember,
field, member, buffer);
*(const size_t *) qmember,
member,
buffer
);
} }
} }
for (i = 0; i < message->n_unknown_fields; i++) for (i = 0; i < message->n_unknown_fields; i++)
...@@ -2020,14 +1827,12 @@ protobuf_c_message_pack_to_buffer(const ProtobufCMessage *message, ...@@ -2020,14 +1827,12 @@ protobuf_c_message_pack_to_buffer(const ProtobufCMessage *message,
* @{ * @{
*/ */
static inline int static inline int int_range_lookup(unsigned n_ranges,
int_range_lookup(unsigned n_ranges, const ProtobufCIntRange *ranges, int value) const ProtobufCIntRange *ranges, int value) {
{
unsigned n; unsigned n;
unsigned start; unsigned start;
if (n_ranges == 0) if (n_ranges == 0) return -1;
return -1;
start = 0; start = 0;
n = n_ranges; n = n_ranges;
while (n > 1) { while (n > 1) {
...@@ -2035,38 +1840,30 @@ int_range_lookup(unsigned n_ranges, const ProtobufCIntRange *ranges, int value) ...@@ -2035,38 +1840,30 @@ int_range_lookup(unsigned n_ranges, const ProtobufCIntRange *ranges, int value)
if (value < ranges[mid].start_value) { if (value < ranges[mid].start_value) {
n = mid - start; n = mid - start;
} else if (value >= ranges[mid].start_value + } else if (value >=
(int) (ranges[mid + 1].orig_index - ranges[mid].start_value +
ranges[mid].orig_index)) (int)(ranges[mid + 1].orig_index - ranges[mid].orig_index)) {
{
unsigned new_start = mid + 1; unsigned new_start = mid + 1;
n = start + n - new_start; n = start + n - new_start;
start = new_start; start = new_start;
} else } else
return (value - ranges[mid].start_value) + return (value - ranges[mid].start_value) + ranges[mid].orig_index;
ranges[mid].orig_index;
} }
if (n > 0) { if (n > 0) {
unsigned start_orig_index = ranges[start].orig_index; unsigned start_orig_index = ranges[start].orig_index;
unsigned range_size = unsigned range_size = ranges[start + 1].orig_index - start_orig_index;
ranges[start + 1].orig_index - start_orig_index;
if (ranges[start].start_value <= value && if (ranges[start].start_value <= value &&
value < (int) (ranges[start].start_value + range_size)) value < (int)(ranges[start].start_value + range_size)) {
{ return (value - ranges[start].start_value) + start_orig_index;
return (value - ranges[start].start_value) +
start_orig_index;
} }
} }
return -1; return -1;
} }
static size_t static size_t parse_tag_and_wiretype(size_t len, const uint8_t *data,
parse_tag_and_wiretype(size_t len,
const uint8_t *data,
uint32_t *tag_out, uint32_t *tag_out,
ProtobufCWireType *wiretype_out) ProtobufCWireType *wiretype_out) {
{
unsigned max_rv = len > 5 ? 5 : len; unsigned max_rv = len > 5 ? 5 : len;
uint32_t tag = (data[0] & 0x7f) >> 3; uint32_t tag = (data[0] & 0x7f) >> 3;
unsigned shift = 4; unsigned shift = 4;
...@@ -2103,10 +1900,9 @@ struct _ScannedMember { ...@@ -2103,10 +1900,9 @@ struct _ScannedMember {
const uint8_t *data; /**< Pointer to field data. */ const uint8_t *data; /**< Pointer to field data. */
}; };
static inline uint32_t static inline uint32_t scan_length_prefixed_data(size_t len,
scan_length_prefixed_data(size_t len, const uint8_t *data, const uint8_t *data,
size_t *prefix_len_out) size_t *prefix_len_out) {
{
unsigned hdr_max = len < 5 ? len : 5; unsigned hdr_max = len < 5 ? len : 5;
unsigned hdr_len; unsigned hdr_len;
uint32_t val = 0; uint32_t val = 0;
...@@ -2116,8 +1912,7 @@ scan_length_prefixed_data(size_t len, const uint8_t *data, ...@@ -2116,8 +1912,7 @@ scan_length_prefixed_data(size_t len, const uint8_t *data,
for (i = 0; i < hdr_max; i++) { for (i = 0; i < hdr_max; i++) {
val |= (data[i] & 0x7f) << shift; val |= (data[i] & 0x7f) << shift;
shift += 7; shift += 7;
if ((data[i] & 0x80) == 0) if ((data[i] & 0x80) == 0) break;
break;
} }
if (i == hdr_max) { if (i == hdr_max) {
PROTOBUF_C_UNPACK_ERROR("error parsing length for length-prefixed data"); PROTOBUF_C_UNPACK_ERROR("error parsing length for length-prefixed data");
...@@ -2132,13 +1927,10 @@ scan_length_prefixed_data(size_t len, const uint8_t *data, ...@@ -2132,13 +1927,10 @@ scan_length_prefixed_data(size_t len, const uint8_t *data,
return hdr_len + val; return hdr_len + val;
} }
static size_t static size_t max_b128_numbers(size_t len, const uint8_t *data) {
max_b128_numbers(size_t len, const uint8_t *data)
{
size_t rv = 0; size_t rv = 0;
while (len--) while (len--)
if ((*data++ & 0x80) == 0) if ((*data++ & 0x80) == 0) ++rv;
++rv;
return rv; return rv;
} }
...@@ -2158,46 +1950,33 @@ max_b128_numbers(size_t len, const uint8_t *data) ...@@ -2158,46 +1950,33 @@ max_b128_numbers(size_t len, const uint8_t *data)
* some of its fields may have been reused and changed to their default * some of its fields may have been reused and changed to their default
* values during the merge. * values during the merge.
*/ */
static protobuf_c_boolean static protobuf_c_boolean merge_messages(ProtobufCMessage *earlier_msg,
merge_messages(ProtobufCMessage *earlier_msg,
ProtobufCMessage *latter_msg, ProtobufCMessage *latter_msg,
ProtobufCAllocator *allocator) ProtobufCAllocator *allocator) {
{
unsigned i; unsigned i;
const ProtobufCFieldDescriptor *fields = const ProtobufCFieldDescriptor *fields = latter_msg->descriptor->fields;
latter_msg->descriptor->fields;
for (i = 0; i < latter_msg->descriptor->n_fields; i++) { for (i = 0; i < latter_msg->descriptor->n_fields; i++) {
if (fields[i].label == PROTOBUF_C_LABEL_REPEATED) { if (fields[i].label == PROTOBUF_C_LABEL_REPEATED) {
size_t *n_earlier = size_t *n_earlier =
STRUCT_MEMBER_PTR(size_t, earlier_msg, STRUCT_MEMBER_PTR(size_t, earlier_msg, fields[i].quantifier_offset);
fields[i].quantifier_offset);
uint8_t **p_earlier = uint8_t **p_earlier =
STRUCT_MEMBER_PTR(uint8_t *, earlier_msg, STRUCT_MEMBER_PTR(uint8_t *, earlier_msg, fields[i].offset);
fields[i].offset);
size_t *n_latter = size_t *n_latter =
STRUCT_MEMBER_PTR(size_t, latter_msg, STRUCT_MEMBER_PTR(size_t, latter_msg, fields[i].quantifier_offset);
fields[i].quantifier_offset);
uint8_t **p_latter = uint8_t **p_latter =
STRUCT_MEMBER_PTR(uint8_t *, latter_msg, STRUCT_MEMBER_PTR(uint8_t *, latter_msg, fields[i].offset);
fields[i].offset);
if (*n_earlier > 0) { if (*n_earlier > 0) {
if (*n_latter > 0) { if (*n_latter > 0) {
/* Concatenate the repeated field */ /* Concatenate the repeated field */
size_t el_size = size_t el_size = sizeof_elt_in_repeated_array(fields[i].type);
sizeof_elt_in_repeated_array(fields[i].type);
uint8_t *new_field; uint8_t *new_field;
new_field = do_alloc(allocator, new_field = do_alloc(allocator, (*n_earlier + *n_latter) * el_size);
(*n_earlier + *n_latter) * el_size); if (!new_field) return FALSE;
if (!new_field)
return FALSE;
memcpy(new_field, *p_earlier, memcpy(new_field, *p_earlier, *n_earlier * el_size);
*n_earlier * el_size); memcpy(new_field + *n_earlier * el_size, *p_latter,
memcpy(new_field +
*n_earlier * el_size,
*p_latter,
*n_latter * el_size); *n_latter * el_size);
do_free(allocator, *p_latter); do_free(allocator, *p_latter);
...@@ -2216,14 +1995,10 @@ merge_messages(ProtobufCMessage *earlier_msg, ...@@ -2216,14 +1995,10 @@ merge_messages(ProtobufCMessage *earlier_msg,
} else if (fields[i].label == PROTOBUF_C_LABEL_OPTIONAL || } else if (fields[i].label == PROTOBUF_C_LABEL_OPTIONAL ||
fields[i].label == PROTOBUF_C_LABEL_NONE) { fields[i].label == PROTOBUF_C_LABEL_NONE) {
const ProtobufCFieldDescriptor *field; const ProtobufCFieldDescriptor *field;
uint32_t *earlier_case_p = STRUCT_MEMBER_PTR(uint32_t, uint32_t *earlier_case_p =
earlier_msg, STRUCT_MEMBER_PTR(uint32_t, earlier_msg, fields[i].quantifier_offset);
fields[i]. uint32_t *latter_case_p =
quantifier_offset); STRUCT_MEMBER_PTR(uint32_t, latter_msg, fields[i].quantifier_offset);
uint32_t *latter_case_p = STRUCT_MEMBER_PTR(uint32_t,
latter_msg,
fields[i].
quantifier_offset);
protobuf_c_boolean need_to_merge = FALSE; protobuf_c_boolean need_to_merge = FALSE;
void *earlier_elem; void *earlier_elem;
void *latter_elem; void *latter_elem;
...@@ -2232,17 +2007,11 @@ merge_messages(ProtobufCMessage *earlier_msg, ...@@ -2232,17 +2007,11 @@ merge_messages(ProtobufCMessage *earlier_msg,
if (fields[i].flags & PROTOBUF_C_FIELD_FLAG_ONEOF) { if (fields[i].flags & PROTOBUF_C_FIELD_FLAG_ONEOF) {
if (*latter_case_p == 0) { if (*latter_case_p == 0) {
/* lookup correct oneof field */ /* lookup correct oneof field */
int field_index = int field_index = int_range_lookup(
int_range_lookup( latter_msg->descriptor->n_field_ranges,
latter_msg->descriptor latter_msg->descriptor->field_ranges, *earlier_case_p);
->n_field_ranges, if (field_index < 0) return FALSE;
latter_msg->descriptor field = latter_msg->descriptor->fields + field_index;
->field_ranges,
*earlier_case_p);
if (field_index < 0)
return FALSE;
field = latter_msg->descriptor->fields +
field_index;
} else { } else {
/* Oneof is present in the latter message, move on */ /* Oneof is present in the latter message, move on */
continue; continue;
...@@ -2257,12 +2026,11 @@ merge_messages(ProtobufCMessage *earlier_msg, ...@@ -2257,12 +2026,11 @@ merge_messages(ProtobufCMessage *earlier_msg,
switch (field->type) { switch (field->type) {
case PROTOBUF_C_TYPE_MESSAGE: { case PROTOBUF_C_TYPE_MESSAGE: {
ProtobufCMessage *em = *(ProtobufCMessage **) earlier_elem; ProtobufCMessage *em = *(ProtobufCMessage **)earlier_elem;
ProtobufCMessage *lm = *(ProtobufCMessage **) latter_elem; ProtobufCMessage *lm = *(ProtobufCMessage **)latter_elem;
if (em != NULL) { if (em != NULL) {
if (lm != NULL) { if (lm != NULL) {
if (!merge_messages(em, lm, allocator)) if (!merge_messages(em, lm, allocator)) return FALSE;
return FALSE;
/* Already merged */ /* Already merged */
need_to_merge = FALSE; need_to_merge = FALSE;
} else { } else {
...@@ -2273,25 +2041,18 @@ merge_messages(ProtobufCMessage *earlier_msg, ...@@ -2273,25 +2041,18 @@ merge_messages(ProtobufCMessage *earlier_msg,
break; break;
} }
case PROTOBUF_C_TYPE_BYTES: { case PROTOBUF_C_TYPE_BYTES: {
uint8_t *e_data = uint8_t *e_data = ((ProtobufCBinaryData *)earlier_elem)->data;
((ProtobufCBinaryData *) earlier_elem)->data; uint8_t *l_data = ((ProtobufCBinaryData *)latter_elem)->data;
uint8_t *l_data = const ProtobufCBinaryData *d_bd = (ProtobufCBinaryData *)def_val;
((ProtobufCBinaryData *) latter_elem)->data;
const ProtobufCBinaryData *d_bd =
(ProtobufCBinaryData *) def_val;
need_to_merge = need_to_merge =
(e_data != NULL && (e_data != NULL && (d_bd == NULL || e_data != d_bd->data)) &&
(d_bd == NULL || (l_data == NULL || (d_bd != NULL && l_data == d_bd->data));
e_data != d_bd->data)) &&
(l_data == NULL ||
(d_bd != NULL &&
l_data == d_bd->data));
break; break;
} }
case PROTOBUF_C_TYPE_STRING: { case PROTOBUF_C_TYPE_STRING: {
char *e_str = *(char **) earlier_elem; char *e_str = *(char **)earlier_elem;
char *l_str = *(char **) latter_elem; char *l_str = *(char **)latter_elem;
const char *d_str = def_val; const char *d_str = def_val;
need_to_merge = e_str != d_str && l_str == d_str; need_to_merge = e_str != d_str && l_str == d_str;
...@@ -2301,15 +2062,13 @@ merge_messages(ProtobufCMessage *earlier_msg, ...@@ -2301,15 +2062,13 @@ merge_messages(ProtobufCMessage *earlier_msg,
/* Could be has field or case enum, the logic is /* Could be has field or case enum, the logic is
* equivalent, since 0 (FALSE) means not set for * equivalent, since 0 (FALSE) means not set for
* oneof */ * oneof */
need_to_merge = (*earlier_case_p != 0) && need_to_merge = (*earlier_case_p != 0) && (*latter_case_p == 0);
(*latter_case_p == 0);
break; break;
} }
} }
if (need_to_merge) { if (need_to_merge) {
size_t el_size = size_t el_size = sizeof_elt_in_repeated_array(field->type);
sizeof_elt_in_repeated_array(field->type);
memcpy(latter_elem, earlier_elem, el_size); memcpy(latter_elem, earlier_elem, el_size);
/* /*
* Reset the element from the old message to 0 * Reset the element from the old message to 0
...@@ -2340,16 +2099,16 @@ merge_messages(ProtobufCMessage *earlier_msg, ...@@ -2340,16 +2099,16 @@ merge_messages(ProtobufCMessage *earlier_msg,
* others; the remaining error checking is done by * others; the remaining error checking is done by
* parse_packed_repeated_member(). * parse_packed_repeated_member().
*/ */
static protobuf_c_boolean static protobuf_c_boolean count_packed_elements(ProtobufCType type, size_t len,
count_packed_elements(ProtobufCType type, const uint8_t *data,
size_t len, const uint8_t *data, size_t *count_out) size_t *count_out) {
{
switch (type) { switch (type) {
case PROTOBUF_C_TYPE_SFIXED32: case PROTOBUF_C_TYPE_SFIXED32:
case PROTOBUF_C_TYPE_FIXED32: case PROTOBUF_C_TYPE_FIXED32:
case PROTOBUF_C_TYPE_FLOAT: case PROTOBUF_C_TYPE_FLOAT:
if (len % 4 != 0) { if (len % 4 != 0) {
PROTOBUF_C_UNPACK_ERROR("length must be a multiple of 4 for fixed-length 32-bit types"); PROTOBUF_C_UNPACK_ERROR(
"length must be a multiple of 4 for fixed-length 32-bit types");
return FALSE; return FALSE;
} }
*count_out = len / 4; *count_out = len / 4;
...@@ -2358,7 +2117,8 @@ count_packed_elements(ProtobufCType type, ...@@ -2358,7 +2117,8 @@ count_packed_elements(ProtobufCType type,
case PROTOBUF_C_TYPE_FIXED64: case PROTOBUF_C_TYPE_FIXED64:
case PROTOBUF_C_TYPE_DOUBLE: case PROTOBUF_C_TYPE_DOUBLE:
if (len % 8 != 0) { if (len % 8 != 0) {
PROTOBUF_C_UNPACK_ERROR("length must be a multiple of 8 for fixed-length 64-bit types"); PROTOBUF_C_UNPACK_ERROR(
"length must be a multiple of 8 for fixed-length 64-bit types");
return FALSE; return FALSE;
} }
*count_out = len / 8; *count_out = len / 8;
...@@ -2379,117 +2139,92 @@ count_packed_elements(ProtobufCType type, ...@@ -2379,117 +2139,92 @@ count_packed_elements(ProtobufCType type,
case PROTOBUF_C_TYPE_BYTES: case PROTOBUF_C_TYPE_BYTES:
case PROTOBUF_C_TYPE_MESSAGE: case PROTOBUF_C_TYPE_MESSAGE:
default: default:
PROTOBUF_C_UNPACK_ERROR("bad protobuf-c type %u for packed-repeated", type); PROTOBUF_C_UNPACK_ERROR("bad protobuf-c type %u for packed-repeated",
type);
return FALSE; return FALSE;
} }
} }
static inline uint32_t static inline uint32_t parse_uint32(unsigned len, const uint8_t *data) {
parse_uint32(unsigned len, const uint8_t *data)
{
uint32_t rv = data[0] & 0x7f; uint32_t rv = data[0] & 0x7f;
if (len > 1) { if (len > 1) {
rv |= ((uint32_t) (data[1] & 0x7f) << 7); rv |= ((uint32_t)(data[1] & 0x7f) << 7);
if (len > 2) { if (len > 2) {
rv |= ((uint32_t) (data[2] & 0x7f) << 14); rv |= ((uint32_t)(data[2] & 0x7f) << 14);
if (len > 3) { if (len > 3) {
rv |= ((uint32_t) (data[3] & 0x7f) << 21); rv |= ((uint32_t)(data[3] & 0x7f) << 21);
if (len > 4) if (len > 4) rv |= ((uint32_t)(data[4]) << 28);
rv |= ((uint32_t) (data[4]) << 28);
} }
} }
} }
return rv; return rv;
} }
static inline uint32_t static inline uint32_t parse_int32(unsigned len, const uint8_t *data) {
parse_int32(unsigned len, const uint8_t *data)
{
return parse_uint32(len, data); return parse_uint32(len, data);
} }
static inline int32_t static inline int32_t unzigzag32(uint32_t v) {
unzigzag32(uint32_t v)
{
if (v & 1) if (v & 1)
return -(v >> 1) - 1; return -(v >> 1) - 1;
else else
return v >> 1; return v >> 1;
} }
static inline uint32_t static inline uint32_t parse_fixed_uint32(const uint8_t *data) {
parse_fixed_uint32(const uint8_t *data)
{
#if !defined(WORDS_BIGENDIAN) #if !defined(WORDS_BIGENDIAN)
uint32_t t; uint32_t t;
memcpy(&t, data, 4); memcpy(&t, data, 4);
return t; return t;
#else #else
return data[0] | return data[0] | ((uint32_t)(data[1]) << 8) | ((uint32_t)(data[2]) << 16) |
((uint32_t) (data[1]) << 8) | ((uint32_t)(data[3]) << 24);
((uint32_t) (data[2]) << 16) |
((uint32_t) (data[3]) << 24);
#endif #endif
} }
static uint64_t static uint64_t parse_uint64(unsigned len, const uint8_t *data) {
parse_uint64(unsigned len, const uint8_t *data)
{
unsigned shift, i; unsigned shift, i;
uint64_t rv; uint64_t rv;
if (len < 5) if (len < 5) return parse_uint32(len, data);
return parse_uint32(len, data); rv = ((uint64_t)(data[0] & 0x7f)) | ((uint64_t)(data[1] & 0x7f) << 7) |
rv = ((uint64_t) (data[0] & 0x7f)) | ((uint64_t)(data[2] & 0x7f) << 14) | ((uint64_t)(data[3] & 0x7f) << 21);
((uint64_t) (data[1] & 0x7f) << 7) |
((uint64_t) (data[2] & 0x7f) << 14) |
((uint64_t) (data[3] & 0x7f) << 21);
shift = 28; shift = 28;
for (i = 4; i < len; i++) { for (i = 4; i < len; i++) {
rv |= (((uint64_t) (data[i] & 0x7f)) << shift); rv |= (((uint64_t)(data[i] & 0x7f)) << shift);
shift += 7; shift += 7;
} }
return rv; return rv;
} }
static inline int64_t static inline int64_t unzigzag64(uint64_t v) {
unzigzag64(uint64_t v)
{
if (v & 1) if (v & 1)
return -(v >> 1) - 1; return -(v >> 1) - 1;
else else
return v >> 1; return v >> 1;
} }
static inline uint64_t static inline uint64_t parse_fixed_uint64(const uint8_t *data) {
parse_fixed_uint64(const uint8_t *data)
{
#if !defined(WORDS_BIGENDIAN) #if !defined(WORDS_BIGENDIAN)
uint64_t t; uint64_t t;
memcpy(&t, data, 8); memcpy(&t, data, 8);
return t; return t;
#else #else
return (uint64_t) parse_fixed_uint32(data) | return (uint64_t)parse_fixed_uint32(data) |
(((uint64_t) parse_fixed_uint32(data + 4)) << 32); (((uint64_t)parse_fixed_uint32(data + 4)) << 32);
#endif #endif
} }
static protobuf_c_boolean static protobuf_c_boolean parse_boolean(unsigned len, const uint8_t *data) {
parse_boolean(unsigned len, const uint8_t *data)
{
unsigned i; unsigned i;
for (i = 0; i < len; i++) for (i = 0; i < len; i++)
if (data[i] & 0x7f) if (data[i] & 0x7f) return TRUE;
return TRUE;
return FALSE; return FALSE;
} }
static protobuf_c_boolean static protobuf_c_boolean parse_required_member(
parse_required_member(ScannedMember *scanned_member, ScannedMember *scanned_member, void *member, ProtobufCAllocator *allocator,
void *member, protobuf_c_boolean maybe_clear) {
ProtobufCAllocator *allocator,
protobuf_c_boolean maybe_clear)
{
unsigned len = scanned_member->len; unsigned len = scanned_member->len;
const uint8_t *data = scanned_member->data; const uint8_t *data = scanned_member->data;
ProtobufCWireType wire_type = scanned_member->wire_type; ProtobufCWireType wire_type = scanned_member->wire_type;
...@@ -2497,63 +2232,53 @@ parse_required_member(ScannedMember *scanned_member, ...@@ -2497,63 +2232,53 @@ parse_required_member(ScannedMember *scanned_member,
switch (scanned_member->field->type) { switch (scanned_member->field->type) {
case PROTOBUF_C_TYPE_ENUM: case PROTOBUF_C_TYPE_ENUM:
case PROTOBUF_C_TYPE_INT32: case PROTOBUF_C_TYPE_INT32:
if (wire_type != PROTOBUF_C_WIRE_TYPE_VARINT) if (wire_type != PROTOBUF_C_WIRE_TYPE_VARINT) return FALSE;
return FALSE; *(int32_t *)member = parse_int32(len, data);
*(int32_t *) member = parse_int32(len, data);
return TRUE; return TRUE;
case PROTOBUF_C_TYPE_UINT32: case PROTOBUF_C_TYPE_UINT32:
if (wire_type != PROTOBUF_C_WIRE_TYPE_VARINT) if (wire_type != PROTOBUF_C_WIRE_TYPE_VARINT) return FALSE;
return FALSE; *(uint32_t *)member = parse_uint32(len, data);
*(uint32_t *) member = parse_uint32(len, data);
return TRUE; return TRUE;
case PROTOBUF_C_TYPE_SINT32: case PROTOBUF_C_TYPE_SINT32:
if (wire_type != PROTOBUF_C_WIRE_TYPE_VARINT) if (wire_type != PROTOBUF_C_WIRE_TYPE_VARINT) return FALSE;
return FALSE; *(int32_t *)member = unzigzag32(parse_uint32(len, data));
*(int32_t *) member = unzigzag32(parse_uint32(len, data));
return TRUE; return TRUE;
case PROTOBUF_C_TYPE_SFIXED32: case PROTOBUF_C_TYPE_SFIXED32:
case PROTOBUF_C_TYPE_FIXED32: case PROTOBUF_C_TYPE_FIXED32:
case PROTOBUF_C_TYPE_FLOAT: case PROTOBUF_C_TYPE_FLOAT:
if (wire_type != PROTOBUF_C_WIRE_TYPE_32BIT) if (wire_type != PROTOBUF_C_WIRE_TYPE_32BIT) return FALSE;
return FALSE; *(uint32_t *)member = parse_fixed_uint32(data);
*(uint32_t *) member = parse_fixed_uint32(data);
return TRUE; return TRUE;
case PROTOBUF_C_TYPE_INT64: case PROTOBUF_C_TYPE_INT64:
case PROTOBUF_C_TYPE_UINT64: case PROTOBUF_C_TYPE_UINT64:
if (wire_type != PROTOBUF_C_WIRE_TYPE_VARINT) if (wire_type != PROTOBUF_C_WIRE_TYPE_VARINT) return FALSE;
return FALSE; *(uint64_t *)member = parse_uint64(len, data);
*(uint64_t *) member = parse_uint64(len, data);
return TRUE; return TRUE;
case PROTOBUF_C_TYPE_SINT64: case PROTOBUF_C_TYPE_SINT64:
if (wire_type != PROTOBUF_C_WIRE_TYPE_VARINT) if (wire_type != PROTOBUF_C_WIRE_TYPE_VARINT) return FALSE;
return FALSE; *(int64_t *)member = unzigzag64(parse_uint64(len, data));
*(int64_t *) member = unzigzag64(parse_uint64(len, data));
return TRUE; return TRUE;
case PROTOBUF_C_TYPE_SFIXED64: case PROTOBUF_C_TYPE_SFIXED64:
case PROTOBUF_C_TYPE_FIXED64: case PROTOBUF_C_TYPE_FIXED64:
case PROTOBUF_C_TYPE_DOUBLE: case PROTOBUF_C_TYPE_DOUBLE:
if (wire_type != PROTOBUF_C_WIRE_TYPE_64BIT) if (wire_type != PROTOBUF_C_WIRE_TYPE_64BIT) return FALSE;
return FALSE; *(uint64_t *)member = parse_fixed_uint64(data);
*(uint64_t *) member = parse_fixed_uint64(data);
return TRUE; return TRUE;
case PROTOBUF_C_TYPE_BOOL: case PROTOBUF_C_TYPE_BOOL:
*(protobuf_c_boolean *) member = parse_boolean(len, data); *(protobuf_c_boolean *)member = parse_boolean(len, data);
return TRUE; return TRUE;
case PROTOBUF_C_TYPE_STRING: { case PROTOBUF_C_TYPE_STRING: {
char **pstr = member; char **pstr = member;
unsigned pref_len = scanned_member->length_prefix_len; unsigned pref_len = scanned_member->length_prefix_len;
if (wire_type != PROTOBUF_C_WIRE_TYPE_LENGTH_PREFIXED) if (wire_type != PROTOBUF_C_WIRE_TYPE_LENGTH_PREFIXED) return FALSE;
return FALSE;
if (maybe_clear && *pstr != NULL) { if (maybe_clear && *pstr != NULL) {
const char *def = scanned_member->field->default_value; const char *def = scanned_member->field->default_value;
if (*pstr != NULL && *pstr != def) if (*pstr != NULL && *pstr != def) do_free(allocator, *pstr);
do_free(allocator, *pstr);
} }
*pstr = do_alloc(allocator, len - pref_len + 1); *pstr = do_alloc(allocator, len - pref_len + 1);
if (*pstr == NULL) if (*pstr == NULL) return FALSE;
return FALSE;
memcpy(*pstr, data + pref_len, len - pref_len); memcpy(*pstr, data + pref_len, len - pref_len);
(*pstr)[len - pref_len] = 0; (*pstr)[len - pref_len] = 0;
return TRUE; return TRUE;
...@@ -2563,20 +2288,16 @@ parse_required_member(ScannedMember *scanned_member, ...@@ -2563,20 +2288,16 @@ parse_required_member(ScannedMember *scanned_member,
const ProtobufCBinaryData *def_bd; const ProtobufCBinaryData *def_bd;
unsigned pref_len = scanned_member->length_prefix_len; unsigned pref_len = scanned_member->length_prefix_len;
if (wire_type != PROTOBUF_C_WIRE_TYPE_LENGTH_PREFIXED) if (wire_type != PROTOBUF_C_WIRE_TYPE_LENGTH_PREFIXED) return FALSE;
return FALSE;
def_bd = scanned_member->field->default_value; def_bd = scanned_member->field->default_value;
if (maybe_clear && if (maybe_clear && bd->data != NULL &&
bd->data != NULL && (def_bd == NULL || bd->data != def_bd->data)) {
(def_bd == NULL || bd->data != def_bd->data))
{
do_free(allocator, bd->data); do_free(allocator, bd->data);
} }
if (len - pref_len > 0) { if (len - pref_len > 0) {
bd->data = do_alloc(allocator, len - pref_len); bd->data = do_alloc(allocator, len - pref_len);
if (bd->data == NULL) if (bd->data == NULL) return FALSE;
return FALSE;
memcpy(bd->data, data + pref_len, len - pref_len); memcpy(bd->data, data + pref_len, len - pref_len);
} else { } else {
bd->data = NULL; bd->data = NULL;
...@@ -2591,51 +2312,41 @@ parse_required_member(ScannedMember *scanned_member, ...@@ -2591,51 +2312,41 @@ parse_required_member(ScannedMember *scanned_member,
protobuf_c_boolean merge_successful = TRUE; protobuf_c_boolean merge_successful = TRUE;
unsigned pref_len = scanned_member->length_prefix_len; unsigned pref_len = scanned_member->length_prefix_len;
if (wire_type != PROTOBUF_C_WIRE_TYPE_LENGTH_PREFIXED) if (wire_type != PROTOBUF_C_WIRE_TYPE_LENGTH_PREFIXED) return FALSE;
return FALSE;
def_mess = scanned_member->field->default_value; def_mess = scanned_member->field->default_value;
subm = protobuf_c_message_unpack(scanned_member->field->descriptor, subm =
allocator, protobuf_c_message_unpack(scanned_member->field->descriptor,
len - pref_len, allocator, len - pref_len, data + pref_len);
data + pref_len);
if (maybe_clear && *pmessage != NULL && *pmessage != def_mess) {
if (maybe_clear &&
*pmessage != NULL &&
*pmessage != def_mess)
{
if (subm != NULL) if (subm != NULL)
merge_successful = merge_messages(*pmessage, subm, allocator); merge_successful = merge_messages(*pmessage, subm, allocator);
/* Delete the previous message */ /* Delete the previous message */
protobuf_c_message_free_unpacked(*pmessage, allocator); protobuf_c_message_free_unpacked(*pmessage, allocator);
} }
*pmessage = subm; *pmessage = subm;
if (subm == NULL || !merge_successful) if (subm == NULL || !merge_successful) return FALSE;
return FALSE;
return TRUE; return TRUE;
} }
} }
return FALSE; return FALSE;
} }
static protobuf_c_boolean static protobuf_c_boolean parse_oneof_member(ScannedMember *scanned_member,
parse_oneof_member (ScannedMember *scanned_member,
void *member, void *member,
ProtobufCMessage *message, ProtobufCMessage *message,
ProtobufCAllocator *allocator) ProtobufCAllocator *allocator) {
{ uint32_t *oneof_case = STRUCT_MEMBER_PTR(
uint32_t *oneof_case = STRUCT_MEMBER_PTR(uint32_t, message, uint32_t, message, scanned_member->field->quantifier_offset);
scanned_member->field->quantifier_offset);
/* If we have already parsed a member of this oneof, free it. */ /* If we have already parsed a member of this oneof, free it. */
if (*oneof_case != 0) { if (*oneof_case != 0) {
/* lookup field */ /* lookup field */
int field_index = int field_index =
int_range_lookup(message->descriptor->n_field_ranges, int_range_lookup(message->descriptor->n_field_ranges,
message->descriptor->field_ranges, message->descriptor->field_ranges, *oneof_case);
*oneof_case); if (field_index < 0) return FALSE;
if (field_index < 0)
return FALSE;
const ProtobufCFieldDescriptor *old_field = const ProtobufCFieldDescriptor *old_field =
message->descriptor->fields + field_index; message->descriptor->fields + field_index;
size_t el_size = sizeof_elt_in_repeated_array(old_field->type); size_t el_size = sizeof_elt_in_repeated_array(old_field->type);
...@@ -2644,16 +2355,13 @@ parse_oneof_member (ScannedMember *scanned_member, ...@@ -2644,16 +2355,13 @@ parse_oneof_member (ScannedMember *scanned_member,
case PROTOBUF_C_TYPE_STRING: { case PROTOBUF_C_TYPE_STRING: {
char **pstr = member; char **pstr = member;
const char *def = old_field->default_value; const char *def = old_field->default_value;
if (*pstr != NULL && *pstr != def) if (*pstr != NULL && *pstr != def) do_free(allocator, *pstr);
do_free(allocator, *pstr);
break; break;
} }
case PROTOBUF_C_TYPE_BYTES: { case PROTOBUF_C_TYPE_BYTES: {
ProtobufCBinaryData *bd = member; ProtobufCBinaryData *bd = member;
const ProtobufCBinaryData *def_bd = old_field->default_value; const ProtobufCBinaryData *def_bd = old_field->default_value;
if (bd->data != NULL && if (bd->data != NULL && (def_bd == NULL || bd->data != def_bd->data)) {
(def_bd == NULL || bd->data != def_bd->data))
{
do_free(allocator, bd->data); do_free(allocator, bd->data);
} }
break; break;
...@@ -2669,74 +2377,59 @@ parse_oneof_member (ScannedMember *scanned_member, ...@@ -2669,74 +2377,59 @@ parse_oneof_member (ScannedMember *scanned_member,
break; break;
} }
memset (member, 0, el_size); memset(member, 0, el_size);
} }
if (!parse_required_member (scanned_member, member, allocator, TRUE)) if (!parse_required_member(scanned_member, member, allocator, TRUE))
return FALSE; return FALSE;
*oneof_case = scanned_member->tag; *oneof_case = scanned_member->tag;
return TRUE; return TRUE;
} }
static protobuf_c_boolean parse_optional_member(ScannedMember *scanned_member,
static protobuf_c_boolean
parse_optional_member(ScannedMember *scanned_member,
void *member, void *member,
ProtobufCMessage *message, ProtobufCMessage *message,
ProtobufCAllocator *allocator) ProtobufCAllocator *allocator) {
{
if (!parse_required_member(scanned_member, member, allocator, TRUE)) if (!parse_required_member(scanned_member, member, allocator, TRUE))
return FALSE; return FALSE;
if (scanned_member->field->quantifier_offset != 0) if (scanned_member->field->quantifier_offset != 0)
STRUCT_MEMBER(protobuf_c_boolean, STRUCT_MEMBER(protobuf_c_boolean, message,
message,
scanned_member->field->quantifier_offset) = TRUE; scanned_member->field->quantifier_offset) = TRUE;
return TRUE; return TRUE;
} }
static protobuf_c_boolean static protobuf_c_boolean parse_repeated_member(ScannedMember *scanned_member,
parse_repeated_member(ScannedMember *scanned_member,
void *member, void *member,
ProtobufCMessage *message, ProtobufCMessage *message,
ProtobufCAllocator *allocator) ProtobufCAllocator *allocator) {
{
const ProtobufCFieldDescriptor *field = scanned_member->field; const ProtobufCFieldDescriptor *field = scanned_member->field;
size_t *p_n = STRUCT_MEMBER_PTR(size_t, message, field->quantifier_offset); size_t *p_n = STRUCT_MEMBER_PTR(size_t, message, field->quantifier_offset);
size_t siz = sizeof_elt_in_repeated_array(field->type); size_t siz = sizeof_elt_in_repeated_array(field->type);
char *array = *(char **) member; char *array = *(char **)member;
if (!parse_required_member(scanned_member, array + siz * (*p_n), if (!parse_required_member(scanned_member, array + siz * (*p_n), allocator,
allocator, FALSE)) FALSE)) {
{
return FALSE; return FALSE;
} }
*p_n += 1; *p_n += 1;
return TRUE; return TRUE;
} }
static unsigned static unsigned scan_varint(unsigned len, const uint8_t *data) {
scan_varint(unsigned len, const uint8_t *data)
{
unsigned i; unsigned i;
if (len > 10) if (len > 10) len = 10;
len = 10;
for (i = 0; i < len; i++) for (i = 0; i < len; i++)
if ((data[i] & 0x80) == 0) if ((data[i] & 0x80) == 0) break;
break; if (i == len) return 0;
if (i == len)
return 0;
return i + 1; return i + 1;
} }
static protobuf_c_boolean static protobuf_c_boolean parse_packed_repeated_member(
parse_packed_repeated_member(ScannedMember *scanned_member, ScannedMember *scanned_member, void *member, ProtobufCMessage *message) {
void *member,
ProtobufCMessage *message)
{
const ProtobufCFieldDescriptor *field = scanned_member->field; const ProtobufCFieldDescriptor *field = scanned_member->field;
size_t *p_n = STRUCT_MEMBER_PTR(size_t, message, field->quantifier_offset); size_t *p_n = STRUCT_MEMBER_PTR(size_t, message, field->quantifier_offset);
size_t siz = sizeof_elt_in_repeated_array(field->type); size_t siz = sizeof_elt_in_repeated_array(field->type);
void *array = *(char **) member + siz * (*p_n); void *array = *(char **)member + siz * (*p_n);
const uint8_t *at = scanned_member->data + scanned_member->length_prefix_len; const uint8_t *at = scanned_member->data + scanned_member->length_prefix_len;
size_t rem = scanned_member->len - scanned_member->length_prefix_len; size_t rem = scanned_member->len - scanned_member->length_prefix_len;
size_t count = 0; size_t count = 0;
...@@ -2751,7 +2444,7 @@ parse_packed_repeated_member(ScannedMember *scanned_member, ...@@ -2751,7 +2444,7 @@ parse_packed_repeated_member(ScannedMember *scanned_member,
goto no_unpacking_needed; goto no_unpacking_needed;
#else #else
for (i = 0; i < count; i++) { for (i = 0; i < count; i++) {
((uint32_t *) array)[i] = parse_fixed_uint32(at); ((uint32_t *)array)[i] = parse_fixed_uint32(at);
at += 4; at += 4;
} }
break; break;
...@@ -2764,7 +2457,7 @@ parse_packed_repeated_member(ScannedMember *scanned_member, ...@@ -2764,7 +2457,7 @@ parse_packed_repeated_member(ScannedMember *scanned_member,
goto no_unpacking_needed; goto no_unpacking_needed;
#else #else
for (i = 0; i < count; i++) { for (i = 0; i < count; i++) {
((uint64_t *) array)[i] = parse_fixed_uint64(at); ((uint64_t *)array)[i] = parse_fixed_uint64(at);
at += 8; at += 8;
} }
break; break;
...@@ -2777,7 +2470,7 @@ parse_packed_repeated_member(ScannedMember *scanned_member, ...@@ -2777,7 +2470,7 @@ parse_packed_repeated_member(ScannedMember *scanned_member,
PROTOBUF_C_UNPACK_ERROR("bad packed-repeated int32 value"); PROTOBUF_C_UNPACK_ERROR("bad packed-repeated int32 value");
return FALSE; return FALSE;
} }
((int32_t *) array)[count++] = parse_int32(s, at); ((int32_t *)array)[count++] = parse_int32(s, at);
at += s; at += s;
rem -= s; rem -= s;
} }
...@@ -2789,7 +2482,7 @@ parse_packed_repeated_member(ScannedMember *scanned_member, ...@@ -2789,7 +2482,7 @@ parse_packed_repeated_member(ScannedMember *scanned_member,
PROTOBUF_C_UNPACK_ERROR("bad packed-repeated sint32 value"); PROTOBUF_C_UNPACK_ERROR("bad packed-repeated sint32 value");
return FALSE; return FALSE;
} }
((int32_t *) array)[count++] = unzigzag32(parse_uint32(s, at)); ((int32_t *)array)[count++] = unzigzag32(parse_uint32(s, at));
at += s; at += s;
rem -= s; rem -= s;
} }
...@@ -2801,7 +2494,7 @@ parse_packed_repeated_member(ScannedMember *scanned_member, ...@@ -2801,7 +2494,7 @@ parse_packed_repeated_member(ScannedMember *scanned_member,
PROTOBUF_C_UNPACK_ERROR("bad packed-repeated enum or uint32 value"); PROTOBUF_C_UNPACK_ERROR("bad packed-repeated enum or uint32 value");
return FALSE; return FALSE;
} }
((uint32_t *) array)[count++] = parse_uint32(s, at); ((uint32_t *)array)[count++] = parse_uint32(s, at);
at += s; at += s;
rem -= s; rem -= s;
} }
...@@ -2814,7 +2507,7 @@ parse_packed_repeated_member(ScannedMember *scanned_member, ...@@ -2814,7 +2507,7 @@ parse_packed_repeated_member(ScannedMember *scanned_member,
PROTOBUF_C_UNPACK_ERROR("bad packed-repeated sint64 value"); PROTOBUF_C_UNPACK_ERROR("bad packed-repeated sint64 value");
return FALSE; return FALSE;
} }
((int64_t *) array)[count++] = unzigzag64(parse_uint64(s, at)); ((int64_t *)array)[count++] = unzigzag64(parse_uint64(s, at));
at += s; at += s;
rem -= s; rem -= s;
} }
...@@ -2827,7 +2520,7 @@ parse_packed_repeated_member(ScannedMember *scanned_member, ...@@ -2827,7 +2520,7 @@ parse_packed_repeated_member(ScannedMember *scanned_member,
PROTOBUF_C_UNPACK_ERROR("bad packed-repeated int64/uint64 value"); PROTOBUF_C_UNPACK_ERROR("bad packed-repeated int64/uint64 value");
return FALSE; return FALSE;
} }
((int64_t *) array)[count++] = parse_uint64(s, at); ((int64_t *)array)[count++] = parse_uint64(s, at);
at += s; at += s;
rem -= s; rem -= s;
} }
...@@ -2839,7 +2532,7 @@ parse_packed_repeated_member(ScannedMember *scanned_member, ...@@ -2839,7 +2532,7 @@ parse_packed_repeated_member(ScannedMember *scanned_member,
PROTOBUF_C_UNPACK_ERROR("bad packed-repeated boolean value"); PROTOBUF_C_UNPACK_ERROR("bad packed-repeated boolean value");
return FALSE; return FALSE;
} }
((protobuf_c_boolean *) array)[i] = at[i]; ((protobuf_c_boolean *)array)[i] = at[i];
} }
break; break;
default: default:
...@@ -2856,61 +2549,47 @@ no_unpacking_needed: ...@@ -2856,61 +2549,47 @@ no_unpacking_needed:
#endif #endif
} }
static protobuf_c_boolean static protobuf_c_boolean is_packable_type(ProtobufCType type) {
is_packable_type(ProtobufCType type) return type != PROTOBUF_C_TYPE_STRING && type != PROTOBUF_C_TYPE_BYTES &&
{
return
type != PROTOBUF_C_TYPE_STRING &&
type != PROTOBUF_C_TYPE_BYTES &&
type != PROTOBUF_C_TYPE_MESSAGE; type != PROTOBUF_C_TYPE_MESSAGE;
} }
static protobuf_c_boolean static protobuf_c_boolean parse_member(ScannedMember *scanned_member,
parse_member(ScannedMember *scanned_member,
ProtobufCMessage *message, ProtobufCMessage *message,
ProtobufCAllocator *allocator) ProtobufCAllocator *allocator) {
{
const ProtobufCFieldDescriptor *field = scanned_member->field; const ProtobufCFieldDescriptor *field = scanned_member->field;
void *member; void *member;
if (field == NULL) { if (field == NULL) {
ProtobufCMessageUnknownField *ufield = ProtobufCMessageUnknownField *ufield =
message->unknown_fields + message->unknown_fields + (message->n_unknown_fields++);
(message->n_unknown_fields++);
ufield->tag = scanned_member->tag; ufield->tag = scanned_member->tag;
ufield->wire_type = scanned_member->wire_type; ufield->wire_type = scanned_member->wire_type;
ufield->len = scanned_member->len; ufield->len = scanned_member->len;
ufield->data = do_alloc(allocator, scanned_member->len); ufield->data = do_alloc(allocator, scanned_member->len);
if (ufield->data == NULL) if (ufield->data == NULL) return FALSE;
return FALSE;
memcpy(ufield->data, scanned_member->data, ufield->len); memcpy(ufield->data, scanned_member->data, ufield->len);
return TRUE; return TRUE;
} }
member = (char *) message + field->offset; member = (char *)message + field->offset;
switch (field->label) { switch (field->label) {
case PROTOBUF_C_LABEL_REQUIRED: case PROTOBUF_C_LABEL_REQUIRED:
return parse_required_member(scanned_member, member, return parse_required_member(scanned_member, member, allocator, TRUE);
allocator, TRUE);
case PROTOBUF_C_LABEL_OPTIONAL: case PROTOBUF_C_LABEL_OPTIONAL:
case PROTOBUF_C_LABEL_NONE: case PROTOBUF_C_LABEL_NONE:
if (0 != (field->flags & PROTOBUF_C_FIELD_FLAG_ONEOF)) { if (0 != (field->flags & PROTOBUF_C_FIELD_FLAG_ONEOF)) {
return parse_oneof_member(scanned_member, member, return parse_oneof_member(scanned_member, member, message, allocator);
message, allocator);
} else { } else {
return parse_optional_member(scanned_member, member, return parse_optional_member(scanned_member, member, message,
message, allocator); allocator);
} }
case PROTOBUF_C_LABEL_REPEATED: case PROTOBUF_C_LABEL_REPEATED:
if (scanned_member->wire_type == if (scanned_member->wire_type == PROTOBUF_C_WIRE_TYPE_LENGTH_PREFIXED &&
PROTOBUF_C_WIRE_TYPE_LENGTH_PREFIXED &&
(0 != (field->flags & PROTOBUF_C_FIELD_FLAG_PACKED) || (0 != (field->flags & PROTOBUF_C_FIELD_FLAG_PACKED) ||
is_packable_type(field->type))) is_packable_type(field->type))) {
{ return parse_packed_repeated_member(scanned_member, member, message);
return parse_packed_repeated_member(scanned_member,
member, message);
} else { } else {
return parse_repeated_member(scanned_member, return parse_repeated_member(scanned_member, member, message,
member, message,
allocator); allocator);
} }
} }
...@@ -2925,20 +2604,16 @@ parse_member(ScannedMember *scanned_member, ...@@ -2925,20 +2604,16 @@ parse_member(ScannedMember *scanned_member,
* for old code, and which would be useful to support allocating * for old code, and which would be useful to support allocating
* descriptors dynamically). * descriptors dynamically).
*/ */
static void static void message_init_generic(const ProtobufCMessageDescriptor *desc,
message_init_generic(const ProtobufCMessageDescriptor *desc, ProtobufCMessage *message) {
ProtobufCMessage *message)
{
unsigned i; unsigned i;
memset(message, 0, desc->sizeof_message); memset(message, 0, desc->sizeof_message);
message->descriptor = desc; message->descriptor = desc;
for (i = 0; i < desc->n_fields; i++) { for (i = 0; i < desc->n_fields; i++) {
if (desc->fields[i].default_value != NULL && if (desc->fields[i].default_value != NULL &&
desc->fields[i].label != PROTOBUF_C_LABEL_REPEATED) desc->fields[i].label != PROTOBUF_C_LABEL_REPEATED) {
{ void *field = STRUCT_MEMBER_P(message, desc->fields[i].offset);
void *field =
STRUCT_MEMBER_P(message, desc->fields[i].offset);
const void *dv = desc->fields[i].default_value; const void *dv = desc->fields[i].default_value;
switch (desc->fields[i].type) { switch (desc->fields[i].type) {
...@@ -2972,7 +2647,7 @@ message_init_generic(const ProtobufCMessageDescriptor *desc, ...@@ -2972,7 +2647,7 @@ message_init_generic(const ProtobufCMessageDescriptor *desc,
* The next line essentially implements a cast * The next line essentially implements a cast
* from const, which is totally unavoidable. * from const, which is totally unavoidable.
*/ */
*(const void **) field = dv; *(const void **)field = dv;
break; break;
} }
} }
...@@ -2998,27 +2673,23 @@ message_init_generic(const ProtobufCMessageDescriptor *desc, ...@@ -2998,27 +2673,23 @@ message_init_generic(const ProtobufCMessageDescriptor *desc,
* that we would overflow if we needed a slab larger than provided. * that we would overflow if we needed a slab larger than provided.
*/ */
#define MAX_SCANNED_MEMBER_SLAB \ #define MAX_SCANNED_MEMBER_SLAB \
(sizeof(unsigned int)*8 - 1 \ (sizeof(unsigned int) * 8 - 1 - BOUND_SIZEOF_SCANNED_MEMBER_LOG2 - \
- BOUND_SIZEOF_SCANNED_MEMBER_LOG2 \ FIRST_SCANNED_MEMBER_SLAB_SIZE_LOG2)
- FIRST_SCANNED_MEMBER_SLAB_SIZE_LOG2)
#define REQUIRED_FIELD_BITMAP_SET(index) \ #define REQUIRED_FIELD_BITMAP_SET(index) \
(required_fields_bitmap[(index)/8] |= (1UL<<((index)%8))) (required_fields_bitmap[(index) / 8] |= (1UL << ((index) % 8)))
#define REQUIRED_FIELD_BITMAP_IS_SET(index) \ #define REQUIRED_FIELD_BITMAP_IS_SET(index) \
(required_fields_bitmap[(index)/8] & (1UL<<((index)%8))) (required_fields_bitmap[(index) / 8] & (1UL << ((index) % 8)))
ProtobufCMessage * ProtobufCMessage *protobuf_c_message_unpack(
protobuf_c_message_unpack(const ProtobufCMessageDescriptor *desc, const ProtobufCMessageDescriptor *desc, ProtobufCAllocator *allocator,
ProtobufCAllocator *allocator, size_t len, const uint8_t *data) {
size_t len, const uint8_t *data)
{
ProtobufCMessage *rv; ProtobufCMessage *rv;
size_t rem = len; size_t rem = len;
const uint8_t *at = data; const uint8_t *at = data;
const ProtobufCFieldDescriptor *last_field = desc->fields + 0; const ProtobufCFieldDescriptor *last_field = desc->fields + 0;
ScannedMember first_member_slab[1UL << ScannedMember first_member_slab[1UL << FIRST_SCANNED_MEMBER_SLAB_SIZE_LOG2];
FIRST_SCANNED_MEMBER_SLAB_SIZE_LOG2];
/* /*
* scanned_member_slabs[i] is an array of arrays of ScannedMember. * scanned_member_slabs[i] is an array of arrays of ScannedMember.
...@@ -3041,12 +2712,10 @@ protobuf_c_message_unpack(const ProtobufCMessageDescriptor *desc, ...@@ -3041,12 +2712,10 @@ protobuf_c_message_unpack(const ProtobufCMessageDescriptor *desc,
ASSERT_IS_MESSAGE_DESCRIPTOR(desc); ASSERT_IS_MESSAGE_DESCRIPTOR(desc);
if (allocator == NULL) if (allocator == NULL) allocator = &protobuf_c__allocator;
allocator = &protobuf_c__allocator;
rv = do_alloc(allocator, desc->sizeof_message); rv = do_alloc(allocator, desc->sizeof_message);
if (!rv) if (!rv) return (NULL);
return (NULL);
scanned_member_slabs[0] = first_member_slab; scanned_member_slabs[0] = first_member_slab;
required_fields_bitmap_len = (desc->n_fields + 7) / 8; required_fields_bitmap_len = (desc->n_fields + 7) / 8;
...@@ -3080,7 +2749,7 @@ protobuf_c_message_unpack(const ProtobufCMessageDescriptor *desc, ...@@ -3080,7 +2749,7 @@ protobuf_c_message_unpack(const ProtobufCMessageDescriptor *desc,
if (used == 0) { if (used == 0) {
PROTOBUF_C_UNPACK_ERROR("error parsing tag/wiretype at offset %u", PROTOBUF_C_UNPACK_ERROR("error parsing tag/wiretype at offset %u",
(unsigned) (at - data)); (unsigned)(at - data));
goto error_cleanup_during_scan; goto error_cleanup_during_scan;
} }
/* /*
...@@ -3090,9 +2759,7 @@ protobuf_c_message_unpack(const ProtobufCMessageDescriptor *desc, ...@@ -3090,9 +2759,7 @@ protobuf_c_message_unpack(const ProtobufCMessageDescriptor *desc,
if (last_field == NULL || last_field->id != tag) { if (last_field == NULL || last_field->id != tag) {
/* lookup field */ /* lookup field */
int field_index = int field_index =
int_range_lookup(desc->n_field_ranges, int_range_lookup(desc->n_field_ranges, desc->field_ranges, tag);
desc->field_ranges,
tag);
if (field_index < 0) { if (field_index < 0) {
field = NULL; field = NULL;
n_unknown++; n_unknown++;
...@@ -3122,11 +2789,10 @@ protobuf_c_message_unpack(const ProtobufCMessageDescriptor *desc, ...@@ -3122,11 +2789,10 @@ protobuf_c_message_unpack(const ProtobufCMessageDescriptor *desc,
unsigned i; unsigned i;
for (i = 0; i < max_len; i++) for (i = 0; i < max_len; i++)
if ((at[i] & 0x80) == 0) if ((at[i] & 0x80) == 0) break;
break;
if (i == max_len) { if (i == max_len) {
PROTOBUF_C_UNPACK_ERROR("unterminated varint at offset %u", PROTOBUF_C_UNPACK_ERROR("unterminated varint at offset %u",
(unsigned) (at - data)); (unsigned)(at - data));
goto error_cleanup_during_scan; goto error_cleanup_during_scan;
} }
tmp.len = i + 1; tmp.len = i + 1;
...@@ -3135,7 +2801,7 @@ protobuf_c_message_unpack(const ProtobufCMessageDescriptor *desc, ...@@ -3135,7 +2801,7 @@ protobuf_c_message_unpack(const ProtobufCMessageDescriptor *desc,
case PROTOBUF_C_WIRE_TYPE_64BIT: case PROTOBUF_C_WIRE_TYPE_64BIT:
if (rem < 8) { if (rem < 8) {
PROTOBUF_C_UNPACK_ERROR("too short after 64bit wiretype at offset %u", PROTOBUF_C_UNPACK_ERROR("too short after 64bit wiretype at offset %u",
(unsigned) (at - data)); (unsigned)(at - data));
goto error_cleanup_during_scan; goto error_cleanup_during_scan;
} }
tmp.len = 8; tmp.len = 8;
...@@ -3154,20 +2820,19 @@ protobuf_c_message_unpack(const ProtobufCMessageDescriptor *desc, ...@@ -3154,20 +2820,19 @@ protobuf_c_message_unpack(const ProtobufCMessageDescriptor *desc,
case PROTOBUF_C_WIRE_TYPE_32BIT: case PROTOBUF_C_WIRE_TYPE_32BIT:
if (rem < 4) { if (rem < 4) {
PROTOBUF_C_UNPACK_ERROR("too short after 32bit wiretype at offset %u", PROTOBUF_C_UNPACK_ERROR("too short after 32bit wiretype at offset %u",
(unsigned) (at - data)); (unsigned)(at - data));
goto error_cleanup_during_scan; goto error_cleanup_during_scan;
} }
tmp.len = 4; tmp.len = 4;
break; break;
default: default:
PROTOBUF_C_UNPACK_ERROR("unsupported tag %u at offset %u", PROTOBUF_C_UNPACK_ERROR("unsupported tag %u at offset %u", wire_type,
wire_type, (unsigned) (at - data)); (unsigned)(at - data));
goto error_cleanup_during_scan; goto error_cleanup_during_scan;
} }
if (in_slab_index == (1UL << if (in_slab_index ==
(which_slab + FIRST_SCANNED_MEMBER_SLAB_SIZE_LOG2))) (1UL << (which_slab + FIRST_SCANNED_MEMBER_SLAB_SIZE_LOG2))) {
{
size_t size; size_t size;
in_slab_index = 0; in_slab_index = 0;
...@@ -3185,20 +2850,13 @@ protobuf_c_message_unpack(const ProtobufCMessageDescriptor *desc, ...@@ -3185,20 +2850,13 @@ protobuf_c_message_unpack(const ProtobufCMessageDescriptor *desc,
scanned_member_slabs[which_slab][in_slab_index++] = tmp; scanned_member_slabs[which_slab][in_slab_index++] = tmp;
if (field != NULL && field->label == PROTOBUF_C_LABEL_REPEATED) { if (field != NULL && field->label == PROTOBUF_C_LABEL_REPEATED) {
size_t *n = STRUCT_MEMBER_PTR(size_t, rv, size_t *n = STRUCT_MEMBER_PTR(size_t, rv, field->quantifier_offset);
field->quantifier_offset);
if (wire_type == PROTOBUF_C_WIRE_TYPE_LENGTH_PREFIXED && if (wire_type == PROTOBUF_C_WIRE_TYPE_LENGTH_PREFIXED &&
(0 != (field->flags & PROTOBUF_C_FIELD_FLAG_PACKED) || (0 != (field->flags & PROTOBUF_C_FIELD_FLAG_PACKED) ||
is_packable_type(field->type))) is_packable_type(field->type))) {
{
size_t count; size_t count;
if (!count_packed_elements(field->type, if (!count_packed_elements(field->type, tmp.len - tmp.length_prefix_len,
tmp.len - tmp.data + tmp.length_prefix_len, &count)) {
tmp.length_prefix_len,
tmp.data +
tmp.length_prefix_len,
&count))
{
PROTOBUF_C_UNPACK_ERROR("counting packed elements"); PROTOBUF_C_UNPACK_ERROR("counting packed elements");
goto error_cleanup_during_scan; goto error_cleanup_during_scan;
} }
...@@ -3212,26 +2870,23 @@ protobuf_c_message_unpack(const ProtobufCMessageDescriptor *desc, ...@@ -3212,26 +2870,23 @@ protobuf_c_message_unpack(const ProtobufCMessageDescriptor *desc,
rem -= tmp.len; rem -= tmp.len;
} }
/* allocate space for repeated fields, also check that all required fields have been set */ /* allocate space for repeated fields, also check that all required fields
* have been set */
for (f = 0; f < desc->n_fields; f++) { for (f = 0; f < desc->n_fields; f++) {
const ProtobufCFieldDescriptor *field = desc->fields + f; const ProtobufCFieldDescriptor *field = desc->fields + f;
if (field->label == PROTOBUF_C_LABEL_REPEATED) { if (field->label == PROTOBUF_C_LABEL_REPEATED) {
size_t siz = size_t siz = sizeof_elt_in_repeated_array(field->type);
sizeof_elt_in_repeated_array(field->type); size_t *n_ptr = STRUCT_MEMBER_PTR(size_t, rv, field->quantifier_offset);
size_t *n_ptr =
STRUCT_MEMBER_PTR(size_t, rv,
field->quantifier_offset);
if (*n_ptr != 0) { if (*n_ptr != 0) {
unsigned n = *n_ptr; unsigned n = *n_ptr;
void *a; void *a;
*n_ptr = 0; *n_ptr = 0;
assert(rv->descriptor != NULL); assert(rv->descriptor != NULL);
#define CLEAR_REMAINING_N_PTRS() \ #define CLEAR_REMAINING_N_PTRS() \
for(f++;f < desc->n_fields; f++) \ for (f++; f < desc->n_fields; f++) { \
{ \
field = desc->fields + f; \ field = desc->fields + f; \
if (field->label == PROTOBUF_C_LABEL_REPEATED) \ if (field->label == PROTOBUF_C_LABEL_REPEATED) \
STRUCT_MEMBER (size_t, rv, field->quantifier_offset) = 0; \ STRUCT_MEMBER(size_t, rv, field->quantifier_offset) = 0; \
} }
a = do_alloc(allocator, siz * n); a = do_alloc(allocator, siz * n);
if (!a) { if (!a) {
...@@ -3241,9 +2896,7 @@ protobuf_c_message_unpack(const ProtobufCMessageDescriptor *desc, ...@@ -3241,9 +2896,7 @@ protobuf_c_message_unpack(const ProtobufCMessageDescriptor *desc,
STRUCT_MEMBER(void *, rv, field->offset) = a; STRUCT_MEMBER(void *, rv, field->offset) = a;
} }
} else if (field->label == PROTOBUF_C_LABEL_REQUIRED) { } else if (field->label == PROTOBUF_C_LABEL_REQUIRED) {
if (field->default_value == NULL && if (field->default_value == NULL && !REQUIRED_FIELD_BITMAP_IS_SET(f)) {
!REQUIRED_FIELD_BITMAP_IS_SET(f))
{
CLEAR_REMAINING_N_PTRS(); CLEAR_REMAINING_N_PTRS();
PROTOBUF_C_UNPACK_ERROR("message '%s': missing required field '%s'", PROTOBUF_C_UNPACK_ERROR("message '%s': missing required field '%s'",
desc->name, field->name); desc->name, field->name);
...@@ -3255,128 +2908,109 @@ protobuf_c_message_unpack(const ProtobufCMessageDescriptor *desc, ...@@ -3255,128 +2908,109 @@ protobuf_c_message_unpack(const ProtobufCMessageDescriptor *desc,
/* allocate space for unknown fields */ /* allocate space for unknown fields */
if (n_unknown) { if (n_unknown) {
rv->unknown_fields = do_alloc(allocator, rv->unknown_fields =
n_unknown * sizeof(ProtobufCMessageUnknownField)); do_alloc(allocator, n_unknown * sizeof(ProtobufCMessageUnknownField));
if (rv->unknown_fields == NULL) if (rv->unknown_fields == NULL) goto error_cleanup;
goto error_cleanup;
} }
/* do real parsing */ /* do real parsing */
for (i_slab = 0; i_slab <= which_slab; i_slab++) { for (i_slab = 0; i_slab <= which_slab; i_slab++) {
unsigned max = (i_slab == which_slab) ? unsigned max =
in_slab_index : (1UL << (i_slab + 4)); (i_slab == which_slab) ? in_slab_index : (1UL << (i_slab + 4));
ScannedMember *slab = scanned_member_slabs[i_slab]; ScannedMember *slab = scanned_member_slabs[i_slab];
for (j = 0; j < max; j++) { for (j = 0; j < max; j++) {
if (!parse_member(slab + j, rv, allocator)) { if (!parse_member(slab + j, rv, allocator)) {
PROTOBUF_C_UNPACK_ERROR("error parsing member %s of %s", PROTOBUF_C_UNPACK_ERROR(
slab->field ? slab->field->name : "*unknown-field*", "error parsing member %s of %s",
desc->name); slab->field ? slab->field->name : "*unknown-field*", desc->name);
goto error_cleanup; goto error_cleanup;
} }
} }
} }
/* cleanup */ /* cleanup */
for (j = 1; j <= which_slab; j++) for (j = 1; j <= which_slab; j++) do_free(allocator, scanned_member_slabs[j]);
do_free(allocator, scanned_member_slabs[j]);
if (required_fields_bitmap_alloced) if (required_fields_bitmap_alloced)
do_free(allocator, required_fields_bitmap); do_free(allocator, required_fields_bitmap);
return rv; return rv;
error_cleanup: error_cleanup:
protobuf_c_message_free_unpacked(rv, allocator); protobuf_c_message_free_unpacked(rv, allocator);
for (j = 1; j <= which_slab; j++) for (j = 1; j <= which_slab; j++) do_free(allocator, scanned_member_slabs[j]);
do_free(allocator, scanned_member_slabs[j]);
if (required_fields_bitmap_alloced) if (required_fields_bitmap_alloced)
do_free(allocator, required_fields_bitmap); do_free(allocator, required_fields_bitmap);
return NULL; return NULL;
error_cleanup_during_scan: error_cleanup_during_scan:
do_free(allocator, rv); do_free(allocator, rv);
for (j = 1; j <= which_slab; j++) for (j = 1; j <= which_slab; j++) do_free(allocator, scanned_member_slabs[j]);
do_free(allocator, scanned_member_slabs[j]);
if (required_fields_bitmap_alloced) if (required_fields_bitmap_alloced)
do_free(allocator, required_fields_bitmap); do_free(allocator, required_fields_bitmap);
return NULL; return NULL;
} }
void void protobuf_c_message_free_unpacked(ProtobufCMessage *message,
protobuf_c_message_free_unpacked(ProtobufCMessage *message, ProtobufCAllocator *allocator) {
ProtobufCAllocator *allocator)
{
const ProtobufCMessageDescriptor *desc; const ProtobufCMessageDescriptor *desc;
unsigned f; unsigned f;
if (message == NULL) if (message == NULL) return;
return;
desc = message->descriptor; desc = message->descriptor;
ASSERT_IS_MESSAGE(message); ASSERT_IS_MESSAGE(message);
if (allocator == NULL) if (allocator == NULL) allocator = &protobuf_c__allocator;
allocator = &protobuf_c__allocator;
message->descriptor = NULL; message->descriptor = NULL;
for (f = 0; f < desc->n_fields; f++) { for (f = 0; f < desc->n_fields; f++) {
if (0 != (desc->fields[f].flags & PROTOBUF_C_FIELD_FLAG_ONEOF) && if (0 != (desc->fields[f].flags & PROTOBUF_C_FIELD_FLAG_ONEOF) &&
desc->fields[f].id != desc->fields[f].id !=
STRUCT_MEMBER(uint32_t, message, desc->fields[f].quantifier_offset)) STRUCT_MEMBER(uint32_t, message,
{ desc->fields[f].quantifier_offset)) {
/* This is not the selected oneof, skip it */ /* This is not the selected oneof, skip it */
continue; continue;
} }
if (desc->fields[f].label == PROTOBUF_C_LABEL_REPEATED) { if (desc->fields[f].label == PROTOBUF_C_LABEL_REPEATED) {
size_t n = STRUCT_MEMBER(size_t, size_t n =
message, STRUCT_MEMBER(size_t, message, desc->fields[f].quantifier_offset);
desc->fields[f].quantifier_offset); void *arr = STRUCT_MEMBER(void *, message, desc->fields[f].offset);
void *arr = STRUCT_MEMBER(void *,
message,
desc->fields[f].offset);
if (arr != NULL) { if (arr != NULL) {
if (desc->fields[f].type == PROTOBUF_C_TYPE_STRING) { if (desc->fields[f].type == PROTOBUF_C_TYPE_STRING) {
unsigned i; unsigned i;
for (i = 0; i < n; i++) for (i = 0; i < n; i++) do_free(allocator, ((char **)arr)[i]);
do_free(allocator, ((char **) arr)[i]);
} else if (desc->fields[f].type == PROTOBUF_C_TYPE_BYTES) { } else if (desc->fields[f].type == PROTOBUF_C_TYPE_BYTES) {
unsigned i; unsigned i;
for (i = 0; i < n; i++) for (i = 0; i < n; i++)
do_free(allocator, ((ProtobufCBinaryData *) arr)[i].data); do_free(allocator, ((ProtobufCBinaryData *)arr)[i].data);
} else if (desc->fields[f].type == PROTOBUF_C_TYPE_MESSAGE) { } else if (desc->fields[f].type == PROTOBUF_C_TYPE_MESSAGE) {
unsigned i; unsigned i;
for (i = 0; i < n; i++) for (i = 0; i < n; i++)
protobuf_c_message_free_unpacked( protobuf_c_message_free_unpacked(((ProtobufCMessage **)arr)[i],
((ProtobufCMessage **) arr)[i], allocator);
allocator
);
} }
do_free(allocator, arr); do_free(allocator, arr);
} }
} else if (desc->fields[f].type == PROTOBUF_C_TYPE_STRING) { } else if (desc->fields[f].type == PROTOBUF_C_TYPE_STRING) {
char *str = STRUCT_MEMBER(char *, message, char *str = STRUCT_MEMBER(char *, message, desc->fields[f].offset);
desc->fields[f].offset);
if (str && str != desc->fields[f].default_value) if (str && str != desc->fields[f].default_value) do_free(allocator, str);
do_free(allocator, str);
} else if (desc->fields[f].type == PROTOBUF_C_TYPE_BYTES) { } else if (desc->fields[f].type == PROTOBUF_C_TYPE_BYTES) {
void *data = STRUCT_MEMBER(ProtobufCBinaryData, message, void *data =
desc->fields[f].offset).data; STRUCT_MEMBER(ProtobufCBinaryData, message, desc->fields[f].offset)
.data;
const ProtobufCBinaryData *default_bd; const ProtobufCBinaryData *default_bd;
default_bd = desc->fields[f].default_value; default_bd = desc->fields[f].default_value;
if (data != NULL && if (data != NULL && (default_bd == NULL || default_bd->data != data)) {
(default_bd == NULL ||
default_bd->data != data))
{
do_free(allocator, data); do_free(allocator, data);
} }
} else if (desc->fields[f].type == PROTOBUF_C_TYPE_MESSAGE) { } else if (desc->fields[f].type == PROTOBUF_C_TYPE_MESSAGE) {
ProtobufCMessage *sm; ProtobufCMessage *sm;
sm = STRUCT_MEMBER(ProtobufCMessage *, message, sm = STRUCT_MEMBER(ProtobufCMessage *, message, desc->fields[f].offset);
desc->fields[f].offset);
if (sm && sm != desc->fields[f].default_value) if (sm && sm != desc->fields[f].default_value)
protobuf_c_message_free_unpacked(sm, allocator); protobuf_c_message_free_unpacked(sm, allocator);
} }
...@@ -3390,22 +3024,16 @@ protobuf_c_message_free_unpacked(ProtobufCMessage *message, ...@@ -3390,22 +3024,16 @@ protobuf_c_message_free_unpacked(ProtobufCMessage *message,
do_free(allocator, message); do_free(allocator, message);
} }
void void protobuf_c_message_init(const ProtobufCMessageDescriptor *descriptor,
protobuf_c_message_init(const ProtobufCMessageDescriptor * descriptor, void *message) {
void *message) descriptor->message_init((ProtobufCMessage *)(message));
{
descriptor->message_init((ProtobufCMessage *) (message));
} }
protobuf_c_boolean protobuf_c_boolean protobuf_c_message_check(const ProtobufCMessage *message) {
protobuf_c_message_check(const ProtobufCMessage *message)
{
unsigned i; unsigned i;
if (!message || if (!message || !message->descriptor ||
!message->descriptor || message->descriptor->magic != PROTOBUF_C__MESSAGE_DESCRIPTOR_MAGIC) {
message->descriptor->magic != PROTOBUF_C__MESSAGE_DESCRIPTOR_MAGIC)
{
return FALSE; return FALSE;
} }
...@@ -3413,56 +3041,51 @@ protobuf_c_message_check(const ProtobufCMessage *message) ...@@ -3413,56 +3041,51 @@ protobuf_c_message_check(const ProtobufCMessage *message)
const ProtobufCFieldDescriptor *f = message->descriptor->fields + i; const ProtobufCFieldDescriptor *f = message->descriptor->fields + i;
ProtobufCType type = f->type; ProtobufCType type = f->type;
ProtobufCLabel label = f->label; ProtobufCLabel label = f->label;
void *field = STRUCT_MEMBER_P (message, f->offset); void *field = STRUCT_MEMBER_P(message, f->offset);
if (label == PROTOBUF_C_LABEL_REPEATED) { if (label == PROTOBUF_C_LABEL_REPEATED) {
size_t *quantity = STRUCT_MEMBER_P (message, f->quantifier_offset); size_t *quantity = STRUCT_MEMBER_P(message, f->quantifier_offset);
if (*quantity > 0 && *(void **) field == NULL) { if (*quantity > 0 && *(void **)field == NULL) {
return FALSE; return FALSE;
} }
if (type == PROTOBUF_C_TYPE_MESSAGE) { if (type == PROTOBUF_C_TYPE_MESSAGE) {
ProtobufCMessage **submessage = *(ProtobufCMessage ***) field; ProtobufCMessage **submessage = *(ProtobufCMessage ***)field;
unsigned j; unsigned j;
for (j = 0; j < *quantity; j++) { for (j = 0; j < *quantity; j++) {
if (!protobuf_c_message_check(submessage[j])) if (!protobuf_c_message_check(submessage[j])) return FALSE;
return FALSE;
} }
} else if (type == PROTOBUF_C_TYPE_STRING) { } else if (type == PROTOBUF_C_TYPE_STRING) {
char **string = *(char ***) field; char **string = *(char ***)field;
unsigned j; unsigned j;
for (j = 0; j < *quantity; j++) { for (j = 0; j < *quantity; j++) {
if (!string[j]) if (!string[j]) return FALSE;
return FALSE;
} }
} else if (type == PROTOBUF_C_TYPE_BYTES) { } else if (type == PROTOBUF_C_TYPE_BYTES) {
ProtobufCBinaryData *bd = *(ProtobufCBinaryData **) field; ProtobufCBinaryData *bd = *(ProtobufCBinaryData **)field;
unsigned j; unsigned j;
for (j = 0; j < *quantity; j++) { for (j = 0; j < *quantity; j++) {
if (bd[j].len > 0 && bd[j].data == NULL) if (bd[j].len > 0 && bd[j].data == NULL) return FALSE;
return FALSE;
} }
} }
} else { /* PROTOBUF_C_LABEL_REQUIRED or PROTOBUF_C_LABEL_OPTIONAL */ } else { /* PROTOBUF_C_LABEL_REQUIRED or PROTOBUF_C_LABEL_OPTIONAL */
if (type == PROTOBUF_C_TYPE_MESSAGE) { if (type == PROTOBUF_C_TYPE_MESSAGE) {
ProtobufCMessage *submessage = *(ProtobufCMessage **) field; ProtobufCMessage *submessage = *(ProtobufCMessage **)field;
if (label == PROTOBUF_C_LABEL_REQUIRED || submessage != NULL) { if (label == PROTOBUF_C_LABEL_REQUIRED || submessage != NULL) {
if (!protobuf_c_message_check(submessage)) if (!protobuf_c_message_check(submessage)) return FALSE;
return FALSE;
} }
} else if (type == PROTOBUF_C_TYPE_STRING) { } else if (type == PROTOBUF_C_TYPE_STRING) {
char *string = *(char **) field; char *string = *(char **)field;
if (label == PROTOBUF_C_LABEL_REQUIRED && string == NULL) if (label == PROTOBUF_C_LABEL_REQUIRED && string == NULL) return FALSE;
return FALSE;
} else if (type == PROTOBUF_C_TYPE_BYTES) { } else if (type == PROTOBUF_C_TYPE_BYTES) {
protobuf_c_boolean *has = STRUCT_MEMBER_P (message, f->quantifier_offset); protobuf_c_boolean *has =
STRUCT_MEMBER_P(message, f->quantifier_offset);
ProtobufCBinaryData *bd = field; ProtobufCBinaryData *bd = field;
if (label == PROTOBUF_C_LABEL_REQUIRED || *has == TRUE) { if (label == PROTOBUF_C_LABEL_REQUIRED || *has == TRUE) {
if (bd->len > 0 && bd->data == NULL) if (bd->len > 0 && bd->data == NULL) return FALSE;
return FALSE;
} }
} }
} }
...@@ -3473,17 +3096,13 @@ protobuf_c_message_check(const ProtobufCMessage *message) ...@@ -3473,17 +3096,13 @@ protobuf_c_message_check(const ProtobufCMessage *message)
/* === services === */ /* === services === */
typedef void (*GenericHandler) (void *service, typedef void (*GenericHandler)(void *service, const ProtobufCMessage *input,
const ProtobufCMessage *input, ProtobufCClosure closure, void *closure_data);
ProtobufCClosure closure, void protobuf_c_service_invoke_internal(ProtobufCService *service,
void *closure_data);
void
protobuf_c_service_invoke_internal(ProtobufCService *service,
unsigned method_index, unsigned method_index,
const ProtobufCMessage *input, const ProtobufCMessage *input,
ProtobufCClosure closure, ProtobufCClosure closure,
void *closure_data) void *closure_data) {
{
GenericHandler *handlers; GenericHandler *handlers;
GenericHandler handler; GenericHandler handler;
...@@ -3498,7 +3117,7 @@ protobuf_c_service_invoke_internal(ProtobufCService *service, ...@@ -3498,7 +3117,7 @@ protobuf_c_service_invoke_internal(ProtobufCService *service,
* Get the array of virtual methods (which are enumerated by the * Get the array of virtual methods (which are enumerated by the
* generated code). * generated code).
*/ */
handlers = (GenericHandler *) (service + 1); handlers = (GenericHandler *)(service + 1);
/* /*
* Get our method and invoke it. * Get our method and invoke it.
...@@ -3508,11 +3127,9 @@ protobuf_c_service_invoke_internal(ProtobufCService *service, ...@@ -3508,11 +3127,9 @@ protobuf_c_service_invoke_internal(ProtobufCService *service,
(*handler)(service, input, closure, closure_data); (*handler)(service, input, closure, closure_data);
} }
void void protobuf_c_service_generated_init(
protobuf_c_service_generated_init(ProtobufCService *service, ProtobufCService *service, const ProtobufCServiceDescriptor *descriptor,
const ProtobufCServiceDescriptor *descriptor, ProtobufCServiceDestroy destroy) {
ProtobufCServiceDestroy destroy)
{
ASSERT_IS_SERVICE_DESCRIPTOR(descriptor); ASSERT_IS_SERVICE_DESCRIPTOR(descriptor);
service->descriptor = descriptor; service->descriptor = descriptor;
service->destroy = destroy; service->destroy = destroy;
...@@ -3520,22 +3137,18 @@ protobuf_c_service_generated_init(ProtobufCService *service, ...@@ -3520,22 +3137,18 @@ protobuf_c_service_generated_init(ProtobufCService *service,
memset(service + 1, 0, descriptor->n_methods * sizeof(GenericHandler)); memset(service + 1, 0, descriptor->n_methods * sizeof(GenericHandler));
} }
void protobuf_c_service_destroy(ProtobufCService *service) void protobuf_c_service_destroy(ProtobufCService *service) {
{
service->destroy(service); service->destroy(service);
} }
/* --- querying the descriptors --- */ /* --- querying the descriptors --- */
const ProtobufCEnumValue * const ProtobufCEnumValue *protobuf_c_enum_descriptor_get_value_by_name(
protobuf_c_enum_descriptor_get_value_by_name(const ProtobufCEnumDescriptor *desc, const ProtobufCEnumDescriptor *desc, const char *name) {
const char *name)
{
unsigned start = 0; unsigned start = 0;
unsigned count; unsigned count;
if (desc == NULL || desc->values_by_name == NULL) if (desc == NULL || desc->values_by_name == NULL) return NULL;
return NULL;
count = desc->n_value_names; count = desc->n_value_names;
...@@ -3550,33 +3163,26 @@ protobuf_c_enum_descriptor_get_value_by_name(const ProtobufCEnumDescriptor *desc ...@@ -3550,33 +3163,26 @@ protobuf_c_enum_descriptor_get_value_by_name(const ProtobufCEnumDescriptor *desc
} else } else
count = mid - start; count = mid - start;
} }
if (count == 0) if (count == 0) return NULL;
return NULL;
if (strcmp(desc->values_by_name[start].name, name) == 0) if (strcmp(desc->values_by_name[start].name, name) == 0)
return desc->values + desc->values_by_name[start].index; return desc->values + desc->values_by_name[start].index;
return NULL; return NULL;
} }
const ProtobufCEnumValue * const ProtobufCEnumValue *protobuf_c_enum_descriptor_get_value(
protobuf_c_enum_descriptor_get_value(const ProtobufCEnumDescriptor *desc, const ProtobufCEnumDescriptor *desc, int value) {
int value)
{
int rv = int_range_lookup(desc->n_value_ranges, desc->value_ranges, value); int rv = int_range_lookup(desc->n_value_ranges, desc->value_ranges, value);
if (rv < 0) if (rv < 0) return NULL;
return NULL;
return desc->values + rv; return desc->values + rv;
} }
const ProtobufCFieldDescriptor * const ProtobufCFieldDescriptor *protobuf_c_message_descriptor_get_field_by_name(
protobuf_c_message_descriptor_get_field_by_name(const ProtobufCMessageDescriptor *desc, const ProtobufCMessageDescriptor *desc, const char *name) {
const char *name)
{
unsigned start = 0; unsigned start = 0;
unsigned count; unsigned count;
const ProtobufCFieldDescriptor *field; const ProtobufCFieldDescriptor *field;
if (desc == NULL || desc->fields_sorted_by_name == NULL) if (desc == NULL || desc->fields_sorted_by_name == NULL) return NULL;
return NULL;
count = desc->n_fields; count = desc->n_fields;
...@@ -3593,33 +3199,26 @@ protobuf_c_message_descriptor_get_field_by_name(const ProtobufCMessageDescriptor ...@@ -3593,33 +3199,26 @@ protobuf_c_message_descriptor_get_field_by_name(const ProtobufCMessageDescriptor
} else } else
count = mid - start; count = mid - start;
} }
if (count == 0) if (count == 0) return NULL;
return NULL;
field = desc->fields + desc->fields_sorted_by_name[start]; field = desc->fields + desc->fields_sorted_by_name[start];
if (strcmp(field->name, name) == 0) if (strcmp(field->name, name) == 0) return field;
return field;
return NULL; return NULL;
} }
const ProtobufCFieldDescriptor * const ProtobufCFieldDescriptor *protobuf_c_message_descriptor_get_field(
protobuf_c_message_descriptor_get_field(const ProtobufCMessageDescriptor *desc, const ProtobufCMessageDescriptor *desc, unsigned value) {
unsigned value) int rv = int_range_lookup(desc->n_field_ranges, desc->field_ranges, value);
{ if (rv < 0) return NULL;
int rv = int_range_lookup(desc->n_field_ranges,desc->field_ranges, value);
if (rv < 0)
return NULL;
return desc->fields + rv; return desc->fields + rv;
} }
const ProtobufCMethodDescriptor * const ProtobufCMethodDescriptor *
protobuf_c_service_descriptor_get_method_by_name(const ProtobufCServiceDescriptor *desc, protobuf_c_service_descriptor_get_method_by_name(
const char *name) const ProtobufCServiceDescriptor *desc, const char *name) {
{
unsigned start = 0; unsigned start = 0;
unsigned count; unsigned count;
if (desc == NULL || desc->method_indices_by_name == NULL) if (desc == NULL || desc->method_indices_by_name == NULL) return NULL;
return NULL;
count = desc->n_methods; count = desc->n_methods;
...@@ -3629,8 +3228,7 @@ protobuf_c_service_descriptor_get_method_by_name(const ProtobufCServiceDescripto ...@@ -3629,8 +3228,7 @@ protobuf_c_service_descriptor_get_method_by_name(const ProtobufCServiceDescripto
const char *mid_name = desc->methods[mid_index].name; const char *mid_name = desc->methods[mid_index].name;
int rv = strcmp(mid_name, name); int rv = strcmp(mid_name, name);
if (rv == 0) if (rv == 0) return desc->methods + desc->method_indices_by_name[mid];
return desc->methods + desc->method_indices_by_name[mid];
if (rv < 0) { if (rv < 0) {
count = start + count - (mid + 1); count = start + count - (mid + 1);
start = mid + 1; start = mid + 1;
...@@ -3638,9 +3236,9 @@ protobuf_c_service_descriptor_get_method_by_name(const ProtobufCServiceDescripto ...@@ -3638,9 +3236,9 @@ protobuf_c_service_descriptor_get_method_by_name(const ProtobufCServiceDescripto
count = mid - start; count = mid - start;
} }
} }
if (count == 0) if (count == 0) return NULL;
return NULL; if (strcmp(desc->methods[desc->method_indices_by_name[start]].name, name) ==
if (strcmp(desc->methods[desc->method_indices_by_name[start]].name, name) == 0) 0)
return desc->methods + desc->method_indices_by_name[start]; return desc->methods + desc->method_indices_by_name[start];
return NULL; return NULL;
} }
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
TOTAL_ERRORS=0 TOTAL_ERRORS=0
# The trick to remove deleted files: https://stackoverflow.com/a/2413151 # The trick to remove deleted files: https://stackoverflow.com/a/2413151
for file in $(git diff --cached --name-status | awk '$1 != "D" {print $2}' | grep -v ".pb.cpp" | grep -v ".pb.h"); do for file in $(git diff --cached --name-status | awk '$1 != "D" {print $2}' | grep -v ".pb.cpp" | grep -v ".pb.h" | grep -v "protobuf-c.*"); do
cpplint $file; cpplint $file;
TOTAL_ERRORS=$(expr $TOTAL_ERRORS + $?); TOTAL_ERRORS=$(expr $TOTAL_ERRORS + $?);
done done
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册