Commit 0c693538 authored by Bas Couwenberg's avatar Bas Couwenberg

Imported Upstream version 1.0.0

parents
coverage
*.gcov
out
protozero.Makefile
tests.target.mk
#Visual Studio files and folders
Release
Debug
deps
*.sdf
*.sln
*.suo
*.vcxproj*
#-----------------------------------------------------------------------------
#
# Configuration for continuous integration service at travis-ci.org
#
#-----------------------------------------------------------------------------
language: cpp
sudo: false
matrix:
include:
- os: linux
compiler: clang
env: INSTALL_CXX=clang++-3.5
addons:
apt:
sources: ['ubuntu-toolchain-r-test', 'llvm-toolchain-precise-3.5']
packages: ['clang-3.5','libprotobuf-dev','protobuf-compiler']
- os: linux
compiler: clang
env: INSTALL_CXX=clang++-3.6
#env: INSTALL_CXX=clang++-3.6 COVERAGE=llvm-cov-3.6
addons:
apt:
sources: ['ubuntu-toolchain-r-test', 'llvm-toolchain-precise-3.6']
packages: ['clang-3.6','libprotobuf-dev','protobuf-compiler']
- os: linux
compiler: gcc
env: INSTALL_CXX=g++-4.8
addons:
apt:
sources: ['ubuntu-toolchain-r-test']
packages: ['g++-4.8','libprotobuf-dev','protobuf-compiler']
- os: linux
compiler: gcc
env: INSTALL_CXX=g++-4.9 COVERAGE=gcov-4.9
addons:
apt:
sources: ['ubuntu-toolchain-r-test']
packages: ['g++-4.9','libprotobuf-dev','protobuf-compiler']
- os: osx
compiler: clang
before_script:
- if [ -n "${INSTALL_CXX}" ]; then CXX=${INSTALL_CXX}; fi
- if [[ $(uname -s) == 'Darwin' ]]; then brew install protobuf; fi;
- make test
script:
- if [ -n "${COVERAGE}" ]; then
make clean;
CXXFLAGS="--coverage" LDFLAGS="--coverage" make test;
${COVERAGE} -lp test/*tests.o test/t/*/*test_cases.o;
pip install --user cpp-coveralls;
~/.local/bin/coveralls --no-gcov -i include/protozero;
fi
This diff is collapsed.
protozero copyright (c) Mapbox.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# first inherit from env
CXX := $(CXX)
CXXFLAGS := $(CXXFLAGS)
LDFLAGS := $(LDFLAGS)
WARNING_FLAGS := -Wall -Wextra -pedantic -Wsign-compare -Wsign-conversion -Wunused-parameter -Wno-float-equal
ifneq ($(findstring clang,$(CXX)),)
WARNING_FLAGS += -Wno-reserved-id-macro -Weverything -Wno-weak-vtables -Wno-c++98-compat -Wno-c++98-compat-pedantic -Wno-exit-time-destructors -Wno-switch-enum -Wno-padded
endif
COMMON_FLAGS := -fvisibility-inlines-hidden -std=c++11 $(WARNING_FLAGS)
RELEASE_FLAGS := -O3 -DNDEBUG -march=native
DEBUG_FLAGS := -O0 -g -fno-inline-functions
OS:=$(shell uname -s)
ifeq ($(OS),Darwin)
CXXFLAGS += -stdlib=libc++
LDFLAGS += -stdlib=libc++
endif
TEST_CASES := $(wildcard test/t/*/test_cases.cpp)
TEST_CASES_O := $(subst .cpp,.o,$(TEST_CASES))
WRITER_TEST_CASES := $(wildcard test/t/*/writer_test_cases.cpp)
WRITER_TEST_CASES_O := $(subst .cpp,.o,$(WRITER_TEST_CASES))
PROTO_FILES := $(subst writer_test_cases.cpp,testcase.proto,$(WRITER_TEST_CASES))
PROTO_FILES_CC := $(subst .proto,.pb.cc,$(PROTO_FILES))
PROTO_FILES_H := $(subst .proto,.pb.h,$(PROTO_FILES))
PROTO_FILES_O := $(subst .proto,.pb.o,$(PROTO_FILES))
HPP_FILES := include/protozero/exception.hpp \
include/protozero/varint.hpp \
include/protozero/pbf_types.hpp \
include/protozero/pbf_reader.hpp \
include/protozero/pbf_writer.hpp
CFLAGS_PROTOBUF := $(shell pkg-config protobuf --cflags)
LDFLAGS_PROTOBUF := $(shell pkg-config protobuf --libs-only-L)
all: ./test/tests test/writer_tests
./test/t/%/test_cases.o: test/t/%/test_cases.cpp $(HPP_FILES)
$(CXX) -c -Iinclude -Itest/include $(CXXFLAGS) $(COMMON_FLAGS) $(DEBUG_FLAGS) $< -o $@
./test/tests.o: test/tests.cpp $(HPP_FILES)
$(CXX) -c -Iinclude -Itest/include $(CXXFLAGS) $(COMMON_FLAGS) $(DEBUG_FLAGS) $< -o $@
./test/tests: test/tests.o $(TEST_CASES_O)
$(CXX) $(LDFLAGS) $^ -o $@
./test/t/%/testcase.pb.cc: ./test/t/%/testcase.proto
protoc --cpp_out=. $^
./test/t/%/testcase.pb.o: ./test/t/%/testcase.pb.cc
$(CXX) -c -I. -Iinclude -Itest/include $(CXXFLAGS) $(CFLAGS_PROTOBUF) -std=c++11 $(DEBUG_FLAGS) $< -o $@
./test/t/%/writer_test_cases.o: ./test/t/%/writer_test_cases.cpp $(HPP_FILES)
$(CXX) -c -I. -Iinclude -Itest/include $(CXXFLAGS) $(CFLAGS_PROTOBUF) $(COMMON_FLAGS) $(DEBUG_FLAGS) $< -o $@
./test/writer_tests.o: test/writer_tests.cpp $(HPP_FILES) $(PROTO_FILES_CC)
$(CXX) -c -I. -Iinclude -Itest/include $(CXXFLAGS) $(COMMON_FLAGS) $(DEBUG_FLAGS) $< -o $@
./test/writer_tests: test/writer_tests.o $(PROTO_FILES_O) $(WRITER_TEST_CASES_O)
$(CXX) $(LDFLAGS) $(LDFLAGS_PROTOBUF) $^ -lprotobuf-lite -pthread -o $@
test: all
./test/tests
./test/writer_tests
iwyu: $(HPP_FILES) test/tests.cpp test/writer_tests.cpp
iwyu -Xiwyu -- -std=c++11 -Iinclude include/protozero/exception.hpp || true
iwyu -Xiwyu -- -std=c++11 -Iinclude include/protozero/varint.hpp || true
iwyu -Xiwyu -- -std=c++11 -Iinclude include/protozero/pbf_types.hpp || true
iwyu -Xiwyu -- -std=c++11 -Iinclude include/protozero/pbf_reader.hpp || true
iwyu -Xiwyu -- -std=c++11 -Iinclude include/protozero/pbf_writer.hpp || true
iwyu -Xiwyu -- -std=c++11 -Iinclude -Itest/include test/tests.cpp || true
iwyu -Xiwyu -- -std=c++11 -Iinclude -Itest/include test/writer_tests.cpp || true
check: $(HPP_FILES) test/tests.cpp test/include/test.hpp test/include/testcase.hpp test/t/*/testcase.cpp $(TEST_CASES)
cppcheck -Uassert --std=c++11 --enable=all --suppress=incorrectStringBooleanError $^
doc: doc/Doxyfile README.md tutorial.md $(HPP_FILES)
doxygen doc/Doxyfile
clean:
rm -f ./test/tests
rm -f ./test/tests.o
rm -f ./test/tests.gc*
rm -f ./test/writer_tests
rm -f ./test/writer_tests.o
rm -f ./test/writer_tests.gc*
rm -f ./test/t/*/testcase.pb.cc
rm -f ./test/t/*/testcase.pb.h
rm -f ./test/t/*/testcase.pb.o
rm -f ./test/t/*/testcase.pb.gc*
rm -f ./test/t/*/testcase.o
rm -f ./test/t/*/testcase
rm -f ./test/t/*/test_cases.o
rm -f ./test/t/*/test_cases.gc*
rm -f ./test/t/*/writer_test_cases.o
rm -f ./test/t/*/writer_test_cases.gc*
rm -f ./*.gcov
rm -fr doc/doxygen_sqlite3.db doc/html coverage
.PHONY: all test iwyu check doc
# protozero
Minimalistic protocol buffer decoder and encoder in C++.
Designed for high performance. Suitable for writing zero copy parsers and
encoders with minimal need for run-time allocation of memory.
Low-level: this is designed to be a building block for writing a very
customized decoder for a stable protobuf schema. If your protobuf schema
is changing frequently or lazy decoding is not critical for your application
then this approach offers no value: just use the decoding API available via the
C++ API that can be generated via the Google Protobufs `protoc` program.
[![Travis Build Status](https://travis-ci.org/mapbox/protozero.svg?branch=master)](https://travis-ci.org/mapbox/protozero)
[![Appveyor Build Status](https://ci.appveyor.com/api/projects/status/o354pq10y96mnr6d?svg=true)](https://ci.appveyor.com/project/Mapbox/protozero)
[![Coverage Status](https://coveralls.io/repos/mapbox/protozero/badge.svg?branch=master&service=github)](https://coveralls.io/github/mapbox/protozero?branch=master)
## Depends
- C++11 compiler
- A working knowledge of how
[protocol buffer encoding works](https://developers.google.com/protocol-buffers/docs/encoding).
## How it works
The protozero code does **not** read `.proto` files used by the usual Protobuf
implementations. The developer using protozero has to manually "translate" the
`.proto` description into code. This means there is no way to access any of the
information from the `.proto` description. This results in a few restrictions:
* The names of the fields are not available.
* Enum names are not available, you'll have to use the values they are defined
with.
* Default values are not available.
* Field types have to be hardcoded. The library does not know which types to
expect, so the user of the library has to supply the right types. Some checks
are made using `assert()`, but mostly the user has to take care of that.
The library will make sure not to overrun the buffer it was given, but
basically all other checks have to be made in user code!
See the [tutorial](tutorial.md) for more information on how to use it.
Call `make doc` to build the Doxygen documentation. (You'll need
[Doxygen](http://www.stack.nl/~dimitri/doxygen/) installed.) Then open
`doc/html/index.html` in your browser to read it.
## Limitations
* The current implementation does not support big-endian machines. Fixed sized
integers and floats/doubles will not decode properly.
* A protobuf message has to fit into memory completely, otherwise it can not
be parsed with this library. There is no streaming support.
* The length of a string, bytes, or submessage can't be more than 2^31-1.
* The Google Protobuf spec documents that a non-repeated field can actually
appear several times in a message and the implementation is required to
return the value of the last version of that field in this case.
`pbf_reader.hpp` does not enforce this. If this feature is needed in your
case, you have to do this yourself.
* There is no specific support for maps but they can be used as described in
the "Backwards compatibility" section of
https://developers.google.com/protocol-buffers/docs/proto3#maps.
## Tests
Extensive tests are included. Call
make test
to build all tests and run them.
See `test/README.md` for more details about the test.
You can also use `gyp` to build the reader tests:
gyp --depth=. --build=Release
./out/Release/tests
This will clobber the `Makefile` from the repository! Instead of `Release` you
can use `Debug` for a debug build.
## Coverage report
To get a coverage report compile and link with `--coverage`:
CXXFLAGS="--coverage" LDFLAGS="--coverage" make test
If you are using `g++` use `gcov` to generate a report (results are in `*.gcov`
files):
gcov -lp test/*tests.o test/t/*/*test_cases.o
If you are using `clang++` use `llvm-cov` instead:
llvm-cov gcov -lp test/*tests.o test/t/*/*test_cases.o
If you are using `g++` you can use `gcovr` to generate nice HTML output:
mkdir -p coverage
gcovr -r . --html --html-details -o coverage/index.html
Open `coverage/index.html` in your browser to see the report.
## Cppcheck
For extra checks with [Cppcheck](http://cppcheck.sourceforge.net/) you can call
make check
os: Visual Studio 2015 RC
platform:
- x64
- x86
configuration:
- Debug
- Release
install:
- SET PATH=c:\python27;%PATH%
- SET PATH=C:\Program Files (x86)\MSBuild\14.0\bin\;%PATH%
- git clone --quiet --depth 1 https://chromium.googlesource.com/external/gyp.git deps/gyp
# note windows requires --generator-output to be absolute
- python deps/gyp/gyp_main.py protozero.gyp --depth=. -f msvs -G msvs_version=2015
- set MSBUILD_PLATFORM=%platform%
- if "%MSBUILD_PLATFORM%" == "x86" set MSBUILD_PLATFORM=Win32
- msbuild protozero.sln /nologo /p:Configuration=%configuration%;Platform=%MSBUILD_PLATFORM%
- .\%configuration%\tests.exe
build: off
test: off
deploy: off
#test
{
"conditions": [
["OS=='win'", {
"target_defaults": {
"default_configuration": "Release_x64",
#"msbuild_toolset":"CTP_Nov2013",
"msvs_settings": {
"VCCLCompilerTool": {
"ExceptionHandling": 1, # /EHsc
"RuntimeTypeInfo": "true" # /GR
}
},
"configurations": {
"Debug_Win32": {
"msvs_configuration_platform": "Win32",
"defines": [ "DEBUG","_DEBUG"],
"msvs_settings": {
"VCCLCompilerTool": {
"RuntimeLibrary": "1", # static debug /MTd
"Optimization": 0, # /Od, no optimization
"MinimalRebuild": "false",
"OmitFramePointers": "false",
"BasicRuntimeChecks": 3 # /RTC1
}
}
},
"Debug_x64": {
"msvs_configuration_platform": "x64",
"defines": [ "DEBUG","_DEBUG"],
"msvs_settings": {
"VCCLCompilerTool": {
"RuntimeLibrary": "1", # static debug /MTd
"Optimization": 0, # /Od, no optimization
"MinimalRebuild": "false",
"OmitFramePointers": "false",
"BasicRuntimeChecks": 3 # /RTC1
}
}
},
"Release_Win32": {
"msvs_configuration_platform": "Win32",
"defines": [ "NDEBUG"],
"msvs_settings": {
"VCCLCompilerTool": {
"RuntimeLibrary": 0, # static release
"Optimization": 3, # /Ox, full optimization
"FavorSizeOrSpeed": 1, # /Ot, favour speed over size
"InlineFunctionExpansion": 2, # /Ob2, inline anything eligible
"WholeProgramOptimization": "true", # /GL, whole program optimization, needed for LTCG
"OmitFramePointers": "true",
"EnableFunctionLevelLinking": "true",
"EnableIntrinsicFunctions": "true",
#"AdditionalOptions": [
# "/MP", # compile across multiple CPUs
#],
"DebugInformationFormat": "0"
},
"VCLibrarianTool": {
"AdditionalOptions": [
"/LTCG" # link time code generation
],
},
"VCLinkerTool": {
"LinkTimeCodeGeneration": 1, # link-time code generation
"OptimizeReferences": 2, # /OPT:REF
"EnableCOMDATFolding": 2, # /OPT:ICF
"LinkIncremental": 1, # disable incremental linking
"GenerateDebugInformation": "false"
}
}
},
"Release_x64": {
"msvs_configuration_platform": "x64",
"defines": [ "NDEBUG"],
"msvs_settings": {
"VCCLCompilerTool": {
"RuntimeLibrary": 0, # static release
"Optimization": 3, # /Ox, full optimization
"FavorSizeOrSpeed": 1, # /Ot, favour speed over size
"InlineFunctionExpansion": 2, # /Ob2, inline anything eligible
"WholeProgramOptimization": "true", # /GL, whole program optimization, needed for LTCG
"OmitFramePointers": "true",
"EnableFunctionLevelLinking": "true",
"EnableIntrinsicFunctions": "true",
#"AdditionalOptions": [
# "/MP", # compile across multiple CPUs
#],
"DebugInformationFormat": "0"
},
"VCLibrarianTool": {
"AdditionalOptions": [
"/LTCG" # link time code generation
],
},
"VCLinkerTool": {
"LinkTimeCodeGeneration": 1, # link-time code generation
"OptimizeReferences": 2, # /OPT:REF
"EnableCOMDATFolding": 2, # /OPT:ICF
"LinkIncremental": 1, # disable incremental linking
"GenerateDebugInformation": "false"
}
}
}
}
}
}, {
"target_defaults": {
"default_configuration": "Release",
"xcode_settings": {
"CLANG_CXX_LIBRARY": "libc++",
"CLANG_CXX_LANGUAGE_STANDARD":"c++11",
"GCC_VERSION": "com.apple.compilers.llvm.clang.1_0",
},
"cflags_cc": ["-std=c++11"],
"configurations": {
"Debug": {
"defines": [
"DEBUG"
],
"xcode_settings": {
"GCC_OPTIMIZATION_LEVEL": "0",
"GCC_GENERATE_DEBUGGING_SYMBOLS": "YES",
"OTHER_CPLUSPLUSFLAGS": [ "-Wall", "-Wextra", "-pedantic", "-g", "-O0" ]
}
},
"Release": {
"defines": [
"NDEBUG"
],
"xcode_settings": {
"GCC_OPTIMIZATION_LEVEL": "3",
"GCC_GENERATE_DEBUGGING_SYMBOLS": "NO",
"DEAD_CODE_STRIPPING": "YES",
"GCC_INLINES_ARE_PRIVATE_EXTERN": "YES",
"OTHER_CPLUSPLUSFLAGS": [ "-Wall", "-Wextra", "-pedantic", "-O3" ]
}
}
}
}
}]
]
}
doxygen_sqlite3.db
html
This source diff could not be displayed because it is too large. You can view the blob instead.
#ifndef PROTOZERO_EXCEPTION_HPP
#define PROTOZERO_EXCEPTION_HPP
/*****************************************************************************
protozero - Minimalistic protocol buffer decoder and encoder in C++.
This file is from https://github.com/mapbox/protozero where you can find more
documentation.
*****************************************************************************/
/**
* @file exception.hpp
*
* @brief Contains the exceptions used in the protozero library.
*/
#include <exception>
/**
* @brief All parts of the protozero header-only library are in this namespace.
*/
namespace protozero {
/**
* All exceptions explicitly thrown by the functions of the protozero library
* derive from this exception.
*/
struct exception : std::exception {
/// Returns the explanatory string.
const char *what() const noexcept { return "pbf exception"; }
};
/**
* This exception is thrown when parsing a varint thats larger than allowed.
* This should never happen unless the data is corrupted.
*/
struct varint_too_long_exception : exception {
/// Returns the explanatory string.
const char *what() const noexcept { return "varint too long exception"; }
};
/**
* This exception is thrown when the wire type of a pdf field is unknown.
* This should never happen unless the data is corrupted.
*/
struct unknown_pbf_wire_type_exception : exception {
/// Returns the explanatory string.
const char *what() const noexcept { return "unknown pbf field type exception"; }
};
/**
* This exception is thrown when we are trying to read a field and there
* are not enough bytes left in the buffer to read it. Almost all functions
* of the pbf_reader class can throw this exception.
*
* This should never happen unless the data is corrupted or you have
* initialized the pbf_reader object with incomplete data.
*/
struct end_of_buffer_exception : exception {
/// Returns the explanatory string.
const char *what() const noexcept { return "end of buffer exception"; }
};
} // end namespace protozero
#endif // PROTOZERO_EXCEPTION_HPP
This diff is collapsed.
#ifndef PROTOZERO_PBF_TYPES_HPP
#define PROTOZERO_PBF_TYPES_HPP
/*****************************************************************************
protozero - Minimalistic protocol buffer decoder and encoder in C++.
This file is from https://github.com/mapbox/protozero where you can find more
documentation.
*****************************************************************************/
/**
* @file pbf_types.hpp
*
* @brief Contains the declaration of low-level types used in the pbf format.
*/
#include <cstdint>
namespace protozero {
/**
* The type used for field tags (field numbers).
*/
typedef uint32_t pbf_tag_type;
/**
* The type used to encode type information.
* See the table on
* https://developers.google.com/protocol-buffers/docs/encoding
*/
enum class pbf_wire_type : uint32_t {
varint = 0, // int32/64, uint32/64, sint32/64, bool, enum
fixed64 = 1, // fixed64, sfixed64, double
length_delimited = 2, // string, bytes, embedded messages,
// packed repeated fields
fixed32 = 5, // fixed32, sfixed32, float
unknown = 99 // used for default setting in this library
};
/**
* The type used for length values, such as the length of a field.
*/
typedef uint32_t pbf_length_type;
} // end namespace protozero
#endif // PROTOZERO_PBF_TYPES_HPP
This diff is collapsed.
#ifndef PROTOZERO_VARINT_HPP
#define PROTOZERO_VARINT_HPP
/*****************************************************************************
protozero - Minimalistic protocol buffer decoder and encoder in C++.
This file is from https://github.com/mapbox/protozero where you can find more
documentation.
*****************************************************************************/
/**
* @file varint.hpp
*
* @brief Contains low-level varint and zigzag encoding and decoding functions.
*/
#if __BYTE_ORDER != __LITTLE_ENDIAN
# error "This code only works on little endian machines."
#endif
#include <cstdint>
#include <protozero/exception.hpp>