├── .gitignore ├── CONTRIBUTORS.txt ├── LICENSE ├── README.md ├── test ├── test_import.proto ├── test.proto └── pb_run_test.m ├── protobuflib ├── pblib_helpers_first.m ├── pblib_write_tag.m ├── pblib_encoded_tag_size.m ├── pblib_read_tag.m ├── pblib_read_varint32.m ├── pblib_read_varint64.m ├── pblib_matlab_type_to_string.m ├── pblib_helpers_iff.m ├── pblib_write_wire_type.m ├── pblib_set.m ├── pblib_encoded_varint_size.m ├── pblib_read_wire_type.m ├── pblib_write_varint.m ├── pblib_get_serialized_size.m ├── pblib_type_to_estimated_encoded_length.m ├── pblib_encoded_field_size.m ├── pblib_generic_serialize_to_string.m └── pblib_generic_parse_from_string.m └── src ├── farsounder └── protobuf │ └── compiler │ └── matlab │ ├── matlab_plugin.cc │ ├── matlab_generator.h │ └── matlab_generator.cc ├── google └── protobuf │ └── compiler │ └── main.cc └── Makefile.am /.gitignore: -------------------------------------------------------------------------------- 1 | TAGS 2 | -------------------------------------------------------------------------------- /CONTRIBUTORS.txt: -------------------------------------------------------------------------------- 1 | This file contains a list of contributors to the protobuf-matlab project. 2 | 3 | Original design and implementation: 4 | Fedor Labounko 5 | 6 | Maintainer: 7 | Evan Lapisky 8 | 9 | Patch contributors: 10 | Przemek Lach 11 | Torsten Pfuetzenreuter 12 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | protobuf-matlab - FarSounder's Protocol Buffer support for Matlab 2 | Copyright (c) 2008, FarSounder Inc. All rights reserved. 3 | http://code.google.com/p/protobuf-matlab/ 4 | 5 | Redistribution and use in source and binary forms, with or without 6 | modification, are permitted provided that the following conditions are met: 7 | 8 | * Redistributions of source code must retain the above copyright 9 | notice, this list of conditions and the following disclaimer. 10 | 11 | * Redistributions in binary form must reproduce the above copyright 12 | notice, this list of conditions and the following disclaimer in the 13 | documentation and/or other materials provided with the distribution. 14 | 15 | * Neither the name of the FarSounder Inc. nor the names of its 16 | contributors may be used to endorse or promote products derived from this 17 | software without specific prior written permission. 18 | 19 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | POSSIBILITY OF SUCH DAMAGE. 30 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # protobuf-matlab - FarSounder's Protobuf compiler for Matlab 2 | ## Copyright 2011 FarSounder, Inc. 3 | 4 | http://code.google.com/p/protobuf-matlab/ 5 | 6 | **NOTE(Heath - 04/2024):** this is not maintained as we're not really using matlab anymore (we 7 | haven't in a long time). Please feel free to use for whatever you want if it helps you get 8 | protobuf working with matlab. I'm not sure we'll ever update to latest protobuf version as 9 | I don't think I have access to a current matlab license to test any changes. If you're inclined 10 | to PR something, feel free to open and issue first and 'at' me (@heathhenley) or email 11 | sw@farsounder.com to check so you don't waste any effort. It looks like the most recent fork 12 | with version updates is [here](https://github.com/rez10191/protobuf-matlab) - maybe that will 13 | help if you ended up here. 👍 14 | 15 | Overview 16 | ======== 17 | 18 | This package provides a Matlab code generator for version 2.4.1 of Google's 19 | Protocol Buffers compiler (protoc) as well as support libraries for the 20 | generated Matlab code. 21 | 22 | 23 | Building protoc with Matlab support 24 | =================================== 25 | 26 | 1. Get the protobuf source: 27 | svn co http://protobuf.googlecode.com/svn/tags/2.4.1 protobuf 28 | 29 | 2. Get the protobuf-matlab source: 30 | git clone https://code.google.com/p/protobuf-matlab/ 31 | 32 | 3. Add the protobuf-matlab src files to the Google Protobuf src: 33 | cp -r protobuf-matlab/src protobuf 34 | 35 | 4. Compile the modified protobuf project. 36 | 37 | This should yield a protoc executable with a --matlab_out option. You can now 38 | use protoc to generate Matlab reading and writing code for your .proto file(s). 39 | 40 | 41 | Matlab support library setup 42 | ============================ 43 | 44 | In order to use the generated Matlab code, you'll need to add the protobuflib 45 | directory to your Matlab path. protobuflib is a collection of .m utility files 46 | used by the generated code. 47 | -------------------------------------------------------------------------------- /test/test_import.proto: -------------------------------------------------------------------------------- 1 | // protobuf-matlab - FarSounder's Protocol Buffer support for Matlab 2 | // Copyright (c) 2008, FarSounder Inc. All rights reserved. 3 | // http://code.google.com/p/protobuf-matlab/ 4 | // 5 | // Redistribution and use in source and binary forms, with or without 6 | // modification, are permitted provided that the following conditions are met: 7 | // 8 | // * Redistributions of source code must retain the above copyright 9 | // notice, this list of conditions and the following disclaimer. 10 | // 11 | // * Redistributions in binary form must reproduce the above copyright 12 | // notice, this list of conditions and the following disclaimer in the 13 | // documentation and/or other materials provided with the distribution. 14 | // 15 | // * Neither the name of the FarSounder Inc. nor the names of its 16 | // contributors may be used to endorse or promote products derived from this 17 | // software without specific prior written permission. 18 | // 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 | // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 | // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | // POSSIBILITY OF SUCH DAMAGE. 30 | 31 | // Author: fedor.labounko@gmail.com (Fedor Labounko) 32 | // 33 | // A proto file used for some unit tests. 34 | 35 | package test_import; 36 | 37 | message ImportMessage { 38 | optional int32 d = 1; 39 | } 40 | 41 | enum ImportEnum { 42 | IMPORT_FOO = 7; 43 | IMPORT_BAR = 8; 44 | IMPORT_BAZ = 9; 45 | } 46 | -------------------------------------------------------------------------------- /protobuflib/pblib_helpers_first.m: -------------------------------------------------------------------------------- 1 | function elem = pblib_helpers_first(vect) 2 | %pblib_helpers_first 3 | % elem = pblib_helpers_first(vect) 4 | % 5 | % Returns the first element of a vector in a functional way 6 | 7 | % protobuf-matlab - FarSounder's Protocol Buffer support for Matlab 8 | % Copyright (c) 2008, FarSounder Inc. All rights reserved. 9 | % http://code.google.com/p/protobuf-matlab/ 10 | % 11 | % Redistribution and use in source and binary forms, with or without 12 | % modification, are permitted provided that the following conditions are met: 13 | % 14 | % * Redistributions of source code must retain the above copyright 15 | % notice, this list of conditions and the following disclaimer. 16 | % 17 | % * Redistributions in binary form must reproduce the above copyright 18 | % notice, this list of conditions and the following disclaimer in the 19 | % documentation and/or other materials provided with the distribution. 20 | % 21 | % * Neither the name of the FarSounder Inc. nor the names of its 22 | % contributors may be used to endorse or promote products derived from this 23 | % software without specific prior written permission. 24 | % 25 | % THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 26 | % AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 | % IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 | % ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 29 | % LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 30 | % CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 31 | % SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 32 | % INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 33 | % CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 34 | % ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 35 | % POSSIBILITY OF SUCH DAMAGE. 36 | 37 | % Author: fedor.labounko@gmail.com (Fedor Labounko) 38 | % Support function used by Protobuf compiler generated .m files. 39 | 40 | elem = vect(1); 41 | -------------------------------------------------------------------------------- /protobuflib/pblib_write_tag.m: -------------------------------------------------------------------------------- 1 | function [tag] = pblib_write_tag(number, wire_type) 2 | %pblib_write_tag 3 | % function [tag] = pblib_write_tag(number, wire_type) 4 | 5 | % protobuf-matlab - FarSounder's Protocol Buffer support for Matlab 6 | % Copyright (c) 2008, FarSounder Inc. All rights reserved. 7 | % http://code.google.com/p/protobuf-matlab/ 8 | % 9 | % Redistribution and use in source and binary forms, with or without 10 | % modification, are permitted provided that the following conditions are met: 11 | % 12 | % * Redistributions of source code must retain the above copyright 13 | % notice, this list of conditions and the following disclaimer. 14 | % 15 | % * Redistributions in binary form must reproduce the above copyright 16 | % notice, this list of conditions and the following disclaimer in the 17 | % documentation and/or other materials provided with the distribution. 18 | % 19 | % * Neither the name of the FarSounder Inc. nor the names of its 20 | % contributors may be used to endorse or promote products derived from this 21 | % software without specific prior written permission. 22 | % 23 | % THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 24 | % AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 | % IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 | % ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 27 | % LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 | % CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 | % SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 | % INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 | % CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 | % ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 | % POSSIBILITY OF SUCH DAMAGE. 34 | 35 | % Author: fedor.labounko@gmail.com (Fedor Labounko) 36 | % Support function used by Protobuf compiler generated .m files. 37 | 38 | value_to_encode = number * 8 + wire_type; 39 | tag = pblib_write_varint(value_to_encode); 40 | -------------------------------------------------------------------------------- /protobuflib/pblib_encoded_tag_size.m: -------------------------------------------------------------------------------- 1 | function [len] = pblib_encoded_tag_size(number, wire_type) 2 | %pblib_encoded_tag_size 3 | % [len] = pblib_encoded_tag_size(number, wire_type) 4 | 5 | % protobuf-matlab - FarSounder's Protocol Buffer support for Matlab 6 | % Copyright (c) 2008, FarSounder Inc. All rights reserved. 7 | % http://code.google.com/p/protobuf-matlab/ 8 | % 9 | % Redistribution and use in source and binary forms, with or without 10 | % modification, are permitted provided that the following conditions are met: 11 | % 12 | % * Redistributions of source code must retain the above copyright 13 | % notice, this list of conditions and the following disclaimer. 14 | % 15 | % * Redistributions in binary form must reproduce the above copyright 16 | % notice, this list of conditions and the following disclaimer in the 17 | % documentation and/or other materials provided with the distribution. 18 | % 19 | % * Neither the name of the FarSounder Inc. nor the names of its 20 | % contributors may be used to endorse or promote products derived from this 21 | % software without specific prior written permission. 22 | % 23 | % THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 24 | % AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 | % IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 | % ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 27 | % LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 | % CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 | % SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 | % INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 | % CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 | % ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 | % POSSIBILITY OF SUCH DAMAGE. 34 | 35 | % Author: fedor.labounko@gmail.com (Fedor Labounko) 36 | % Support function used by Protobuf compiler generated .m files. 37 | 38 | value_to_encode = number * 8 + wire_type; 39 | len = pblib_encoded_varint_size(value_to_encode); 40 | -------------------------------------------------------------------------------- /protobuflib/pblib_read_tag.m: -------------------------------------------------------------------------------- 1 | function [number, type, num_read] = pblib_read_tag(buffer, offset) 2 | %pblib_read_tag 3 | % function [number, type, num_read] = pblib_read_tag(buffer, offset) 4 | 5 | % protobuf-matlab - FarSounder's Protocol Buffer support for Matlab 6 | % Copyright (c) 2008, FarSounder Inc. All rights reserved. 7 | % http://code.google.com/p/protobuf-matlab/ 8 | % 9 | % Redistribution and use in source and binary forms, with or without 10 | % modification, are permitted provided that the following conditions are met: 11 | % 12 | % * Redistributions of source code must retain the above copyright 13 | % notice, this list of conditions and the following disclaimer. 14 | % 15 | % * Redistributions in binary form must reproduce the above copyright 16 | % notice, this list of conditions and the following disclaimer in the 17 | % documentation and/or other materials provided with the distribution. 18 | % 19 | % * Neither the name of the FarSounder Inc. nor the names of its 20 | % contributors may be used to endorse or promote products derived from this 21 | % software without specific prior written permission. 22 | % 23 | % THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 24 | % AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 | % IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 | % ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 27 | % LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 | % CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 | % SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 | % INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 | % CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 | % ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 | % POSSIBILITY OF SUCH DAMAGE. 34 | 35 | % Author: fedor.labounko@gmail.com (Fedor Labounko) 36 | % Support function used by Protobuf compiler generated .m files. 37 | 38 | [num, num_read] = pblib_read_varint32(buffer, offset); 39 | number = bitshift(num, -3); 40 | type = bitand(num, 7); 41 | 42 | -------------------------------------------------------------------------------- /src/farsounder/protobuf/compiler/matlab/matlab_plugin.cc: -------------------------------------------------------------------------------- 1 | // Driver for protobuf-matlab compiler 2 | // Copyright (c) 2012, All rights reserved. 3 | // http://code.google.com/p/protobuf-matlab/ 4 | // 5 | // Redistribution and use in source and binary forms, with or without 6 | // modification, are permitted provided that the following conditions are met: 7 | // 8 | // * Redistributions of source code must retain the above copyright 9 | // notice, this list of conditions and the following disclaimer. 10 | // 11 | // * Redistributions in binary form must reproduce the above copyright 12 | // notice, this list of conditions and the following disclaimer in the 13 | // documentation and/or other materials provided with the distribution. 14 | // 15 | // * Neither the name of the author nor the names of its 16 | // contributors may be used to endorse or promote products derived from this 17 | // software without specific prior written permission. 18 | // 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 | // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 | // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | // POSSIBILITY OF SUCH DAMAGE. 30 | 31 | // Author: torsten.pf@gmail.com (Torsten Pfuetzenreuter) 32 | // Plugin driver for Farsounder's matlab code generator, based on Google's C++ 33 | // Protobuf dummy code generator plugin. 34 | 35 | #include 36 | #include 37 | #include 38 | #include "matlab_generator.h" 39 | 40 | int main(int argc, char* argv[]) { 41 | #ifdef _MSC_VER 42 | // Don't print a silly message or stick a modal dialog box in my face, 43 | // please. 44 | _set_abort_behavior(0, ~0); 45 | #endif // !_MSC_VER 46 | 47 | farsounder::protobuf::compiler::matlab::MatlabGenerator generator; 48 | return google::protobuf::compiler::PluginMain(argc, argv, &generator); 49 | } 50 | -------------------------------------------------------------------------------- /protobuflib/pblib_read_varint32.m: -------------------------------------------------------------------------------- 1 | function [num, num_read] = pb_read_varint32(buffer, offset) 2 | %pb_read_varint32 3 | % function [num, num_read] = pb_read_varint32(buffer, offset) 4 | 5 | % protobuf-matlab - FarSounder's Protocol Buffer support for Matlab 6 | % Copyright (c) 2008, FarSounder Inc. All rights reserved. 7 | % http://code.google.com/p/protobuf-matlab/ 8 | % 9 | % Redistribution and use in source and binary forms, with or without 10 | % modification, are permitted provided that the following conditions are met: 11 | % 12 | % * Redistributions of source code must retain the above copyright 13 | % notice, this list of conditions and the following disclaimer. 14 | % 15 | % * Redistributions in binary form must reproduce the above copyright 16 | % notice, this list of conditions and the following disclaimer in the 17 | % documentation and/or other materials provided with the distribution. 18 | % 19 | % * Neither the name of the FarSounder Inc. nor the names of its 20 | % contributors may be used to endorse or promote products derived from this 21 | % software without specific prior written permission. 22 | % 23 | % THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 24 | % AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 | % IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 | % ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 27 | % LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 | % CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 | % SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 | % INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 | % CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 | % ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 | % POSSIBILITY OF SUCH DAMAGE. 34 | 35 | % Author: fedor.labounko@gmail.com (Fedor Labounko) 36 | % Support function used by Protobuf compiler generated .m files. 37 | 38 | current_byte = uint32(buffer(offset)); 39 | num = bitset(current_byte, 8, 0); 40 | index = 1; 41 | while (current_byte > 127) 42 | current_byte = uint32(buffer(index + offset)); 43 | num = bitor(num, bitshift(bitset(current_byte, 8, 0), 7*index)); 44 | index = index + 1; 45 | end 46 | num_read = index; 47 | 48 | -------------------------------------------------------------------------------- /protobuflib/pblib_read_varint64.m: -------------------------------------------------------------------------------- 1 | function [num, num_read] = pblib_read_varint64(buffer, offset) 2 | %pblib_read_varint64 3 | % function [num, num_read] = pblib_read_varint64(buffer, offset) 4 | 5 | % protobuf-matlab - FarSounder's Protocol Buffer support for Matlab 6 | % Copyright (c) 2008, FarSounder Inc. All rights reserved. 7 | % http://code.google.com/p/protobuf-matlab/ 8 | % 9 | % Redistribution and use in source and binary forms, with or without 10 | % modification, are permitted provided that the following conditions are met: 11 | % 12 | % * Redistributions of source code must retain the above copyright 13 | % notice, this list of conditions and the following disclaimer. 14 | % 15 | % * Redistributions in binary form must reproduce the above copyright 16 | % notice, this list of conditions and the following disclaimer in the 17 | % documentation and/or other materials provided with the distribution. 18 | % 19 | % * Neither the name of the FarSounder Inc. nor the names of its 20 | % contributors may be used to endorse or promote products derived from this 21 | % software without specific prior written permission. 22 | % 23 | % THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 24 | % AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 | % IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 | % ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 27 | % LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 | % CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 | % SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 | % INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 | % CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 | % ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 | % POSSIBILITY OF SUCH DAMAGE. 34 | 35 | % Author: fedor.labounko@gmail.com (Fedor Labounko) 36 | % Support function used by Protobuf compiler generated .m files. 37 | 38 | current_byte = uint64(buffer(offset)); 39 | num = bitset(current_byte, 8, 0); 40 | index = 1; 41 | while (current_byte > 127) 42 | current_byte = uint64(buffer(offset + index)); 43 | num = bitor(bitshift(bitset(current_byte, 8, 0), 7*index), num); 44 | index = index + 1; 45 | end 46 | num_read = index; 47 | 48 | -------------------------------------------------------------------------------- /protobuflib/pblib_matlab_type_to_string.m: -------------------------------------------------------------------------------- 1 | function [matlab_type_str] = pblib_matlab_type_to_string(matlab_type) 2 | %pblib_matlab_type_to_string 3 | % function [matlab_type_str] = pblib_matlab_type_to_string(matlab_type) 4 | 5 | % protobuf-matlab - FarSounder's Protocol Buffer support for Matlab 6 | % Copyright (c) 2008, FarSounder Inc. All rights reserved. 7 | % http://code.google.com/p/protobuf-matlab/ 8 | % 9 | % Redistribution and use in source and binary forms, with or without 10 | % modification, are permitted provided that the following conditions are met: 11 | % 12 | % * Redistributions of source code must retain the above copyright 13 | % notice, this list of conditions and the following disclaimer. 14 | % 15 | % * Redistributions in binary form must reproduce the above copyright 16 | % notice, this list of conditions and the following disclaimer in the 17 | % documentation and/or other materials provided with the distribution. 18 | % 19 | % * Neither the name of the FarSounder Inc. nor the names of its 20 | % contributors may be used to endorse or promote products derived from this 21 | % software without specific prior written permission. 22 | % 23 | % THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 24 | % AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 | % IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 | % ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 27 | % LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 | % CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 | % SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 | % INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 | % CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 | % ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 | % POSSIBILITY OF SUCH DAMAGE. 34 | 35 | % Author: fedor.labounko@gmail.com (Fedor Labounko) 36 | % Support function used by Protobuf compiler generated .m files. 37 | 38 | matlab_type_to_string = { ... 39 | 'int32', ... 40 | 'int64', ... 41 | 'uint32', ... 42 | 'uint64', ... 43 | 'double', ... 44 | 'single', ... 45 | 'char', ... 46 | 'uint8', ... 47 | 'uint8', ... 48 | 'int32' 49 | }; 50 | matlab_type_str = matlab_type_to_string{matlab_type}; 51 | -------------------------------------------------------------------------------- /protobuflib/pblib_helpers_iff.m: -------------------------------------------------------------------------------- 1 | function val = pblib_helpers_iff(condition, if_true, if_else) 2 | %pblib_helpers_iff Simulates an iff statement. 3 | % function val = pblib_helpers_iff(condition, if_true, if_else) 4 | % 5 | % Helper functions that turns the if else construct into one expression to be 6 | % used in an anonymous function 7 | % 8 | % If given an array of conditionals, then it selects from if_true or if_else 9 | % on a per element basis. 10 | 11 | % protobuf-matlab - FarSounder's Protocol Buffer support for Matlab 12 | % Copyright (c) 2008, FarSounder Inc. All rights reserved. 13 | % http://code.google.com/p/protobuf-matlab/ 14 | % 15 | % Redistribution and use in source and binary forms, with or without 16 | % modification, are permitted provided that the following conditions are met: 17 | % 18 | % * Redistributions of source code must retain the above copyright 19 | % notice, this list of conditions and the following disclaimer. 20 | % 21 | % * Redistributions in binary form must reproduce the above copyright 22 | % notice, this list of conditions and the following disclaimer in the 23 | % documentation and/or other materials provided with the distribution. 24 | % 25 | % * Neither the name of the FarSounder Inc. nor the names of its 26 | % contributors may be used to endorse or promote products derived from this 27 | % software without specific prior written permission. 28 | % 29 | % THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 30 | % AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 31 | % IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 32 | % ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 33 | % LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 34 | % CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 35 | % SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 36 | % INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 37 | % CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 38 | % ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 39 | % POSSIBILITY OF SUCH DAMAGE. 40 | 41 | % Author: fedor.labounko@gmail.com (Fedor Labounko) 42 | % Support function used by Protobuf compiler generated .m files. 43 | 44 | val = if_true; 45 | for i=1:length(condition) 46 | if condition(i) 47 | val(i) = if_true(i); 48 | else 49 | val(i) = if_else(i); 50 | end 51 | end 52 | 53 | -------------------------------------------------------------------------------- /protobuflib/pblib_write_wire_type.m: -------------------------------------------------------------------------------- 1 | function [buffer] = pblib_write_wire_type(value, wire_type) 2 | %pblib_write_wire_type 3 | % 4 | % buffer = pblib_write_wire_type(value, wire_type) 5 | % 6 | % These values must match the WireType enum in 7 | % http://protobuf.googlecode.com/svn/trunk/src/google/protobuf/wire_format.h 8 | % 9 | % All Wire Types Are (in order, 0 based): 10 | % 0: 'varint' 11 | % 1: '64bit' 12 | % 2: 'length_delimited' 13 | % 3: 'start_group' 14 | % 4: 'end_group' 15 | % 5: '32bit' 16 | % 17 | 18 | % protobuf-matlab - FarSounder's Protocol Buffer support for Matlab 19 | % Copyright (c) 2008, FarSounder Inc. All rights reserved. 20 | % http://code.google.com/p/protobuf-matlab/ 21 | % 22 | % Redistribution and use in source and binary forms, with or without 23 | % modification, are permitted provided that the following conditions are met: 24 | % 25 | % * Redistributions of source code must retain the above copyright 26 | % notice, this list of conditions and the following disclaimer. 27 | % 28 | % * Redistributions in binary form must reproduce the above copyright 29 | % notice, this list of conditions and the following disclaimer in the 30 | % documentation and/or other materials provided with the distribution. 31 | % 32 | % * Neither the name of the FarSounder Inc. nor the names of its 33 | % contributors may be used to endorse or promote products derived from this 34 | % software without specific prior written permission. 35 | % 36 | % THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 37 | % AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 38 | % IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 39 | % ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 40 | % LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 41 | % CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 42 | % SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 43 | % INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 44 | % CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45 | % ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 46 | % POSSIBILITY OF SUCH DAMAGE. 47 | 48 | % Author: fedor.labounko@gmail.com (Fedor Labounko) 49 | % Support function used by Protobuf compiler generated .m files. 50 | 51 | switch wire_type 52 | case 0 53 | buffer = pblib_write_varint(value); 54 | case 1 55 | buffer = value; 56 | case 2 57 | len_buffer = pblib_write_varint(uint32(length(value))); 58 | buffer = [len_buffer value]; 59 | case 3 60 | warning Start Group not implemented 61 | case 4 62 | warning End Group not implemented 63 | case 5 64 | buffer = value; 65 | end 66 | 67 | -------------------------------------------------------------------------------- /protobuflib/pblib_set.m: -------------------------------------------------------------------------------- 1 | function [msg] = pblib_set(msg, field_name, value) 2 | %pblib_set 3 | % function [msg] = pblib_set(msg, field_name, value) 4 | % 5 | % Sets a value in the proto message msg and updates the has_field hash table. BEWARE: 6 | % This function potentially makes a full copy of your msg because it gets modified. I 7 | % have no idea how smart matlab is and how big of a copy happens and haven't tested it. 8 | % This function should therefore only be used if speed is not an issue or you are going 9 | % to test the speed yourself. Otherwise simply call the contents of this functions 10 | % inline in your own workspace. 11 | % 12 | % To use this function if you a have proto message msg, call it with 13 | % msg = pblib_set(msg, 'some_field_name', some_field_value); 14 | % 15 | % If you would like to set the field of a message field, you need to do msg.some_field = 16 | % pblib_set(msg.some_field, 'some_other_subfield', 'some_subfield_value'); 17 | % 18 | % See also pblib_generic_serialize_to_string 19 | 20 | % protobuf-matlab - FarSounder's Protocol Buffer support for Matlab 21 | % Copyright (c) 2008, FarSounder Inc. All rights reserved. 22 | % http://code.google.com/p/protobuf-matlab/ 23 | % 24 | % Redistribution and use in source and binary forms, with or without 25 | % modification, are permitted provided that the following conditions are met: 26 | % 27 | % * Redistributions of source code must retain the above copyright 28 | % notice, this list of conditions and the following disclaimer. 29 | % 30 | % * Redistributions in binary form must reproduce the above copyright 31 | % notice, this list of conditions and the following disclaimer in the 32 | % documentation and/or other materials provided with the distribution. 33 | % 34 | % * Neither the name of the FarSounder Inc. nor the names of its 35 | % contributors may be used to endorse or promote products derived from this 36 | % software without specific prior written permission. 37 | % 38 | % THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 39 | % AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 40 | % IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 41 | % ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 42 | % LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 43 | % CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 44 | % SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 45 | % INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 46 | % CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 47 | % ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 48 | % POSSIBILITY OF SUCH DAMAGE. 49 | 50 | % Author: fedor.labounko@gmail.com (Fedor Labounko) 51 | % Support function used by Protobuf compiler generated .m files. 52 | 53 | msg.(field_name) = value; 54 | put(msg.has_field, field_name, 1); 55 | -------------------------------------------------------------------------------- /src/google/protobuf/compiler/main.cc: -------------------------------------------------------------------------------- 1 | // Protocol Buffers - Google's data interchange format 2 | // Copyright 2008 Google Inc. All rights reserved. 3 | // http://code.google.com/p/protobuf/ 4 | // 5 | // Redistribution and use in source and binary forms, with or without 6 | // modification, are permitted provided that the following conditions are 7 | // met: 8 | // 9 | // * Redistributions of source code must retain the above copyright 10 | // notice, this list of conditions and the following disclaimer. 11 | // * Redistributions in binary form must reproduce the above 12 | // copyright notice, this list of conditions and the following disclaimer 13 | // in the documentation and/or other materials provided with the 14 | // distribution. 15 | // * Neither the name of Google Inc. nor the names of its 16 | // contributors may be used to endorse or promote products derived from 17 | // this software without specific prior written permission. 18 | // 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | // Author: kenton@google.com (Kenton Varda) 32 | 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | 39 | 40 | int main(int argc, char* argv[]) { 41 | 42 | google::protobuf::compiler::CommandLineInterface cli; 43 | cli.AllowPlugins("protoc-"); 44 | 45 | // Proto2 C++ 46 | google::protobuf::compiler::cpp::CppGenerator cpp_generator; 47 | cli.RegisterGenerator("--cpp_out", &cpp_generator, 48 | "Generate C++ header and source."); 49 | 50 | // Proto2 Java 51 | google::protobuf::compiler::java::JavaGenerator java_generator; 52 | cli.RegisterGenerator("--java_out", &java_generator, 53 | "Generate Java source file."); 54 | 55 | // Proto2 Python 56 | google::protobuf::compiler::python::Generator py_generator; 57 | cli.RegisterGenerator("--python_out", &py_generator, 58 | "Generate Python source file."); 59 | 60 | // Proto2 Matlab 61 | farsounder::protobuf::compiler::matlab::MatlabGenerator matlab_generator; 62 | cli.RegisterGenerator("--matlab_out", &matlab_generator, 63 | "Generate Matlab M files."); 64 | 65 | return cli.Run(argc, argv); 66 | } 67 | -------------------------------------------------------------------------------- /protobuflib/pblib_encoded_varint_size.m: -------------------------------------------------------------------------------- 1 | function [len] = pblib_encoded_varint_size(value) 2 | %pblib_encoded_varint_size 3 | % function [len] = pblib_encoded_varint_size(value) 4 | % 5 | % This function will give you the length required to encode value as a 6 | % varint. 7 | 8 | % protobuf-matlab - FarSounder's Protocol Buffer support for Matlab 9 | % Copyright (c) 2008, FarSounder Inc. All rights reserved. 10 | % http://code.google.com/p/protobuf-matlab/ 11 | % 12 | % Redistribution and use in source and binary forms, with or without 13 | % modification, are permitted provided that the following conditions are met: 14 | % 15 | % * Redistributions of source code must retain the above copyright 16 | % notice, this list of conditions and the following disclaimer. 17 | % 18 | % * Redistributions in binary form must reproduce the above copyright 19 | % notice, this list of conditions and the following disclaimer in the 20 | % documentation and/or other materials provided with the distribution. 21 | % 22 | % * Neither the name of the FarSounder Inc. nor the names of its 23 | % contributors may be used to endorse or promote products derived from this 24 | % software without specific prior written permission. 25 | % 26 | % THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 27 | % AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 | % IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 | % ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 30 | % LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 | % CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 | % SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 | % INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 | % CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 | % ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 | % POSSIBILITY OF SUCH DAMAGE. 37 | 38 | % Author: fedor.labounko@gmail.com (Fedor Labounko) 39 | % Support function used by Protobuf compiler generated .m files. 40 | 41 | if (value < uint64(268435456)) % 2^28 42 | if (value < uint64(16384)) % 2^14 43 | if (value < uint64(128)) % 2^7 44 | len = 1; 45 | else 46 | len = 2; 47 | end 48 | else 49 | if (value < uint64(2097152)) % 2^21 50 | len = 3; 51 | else 52 | len = 4; 53 | end 54 | end 55 | else 56 | if (value < uint64(4398046511104)) % 2^42 57 | if (value < uint64(34359738368)) % 2^35 58 | len = 5; 59 | else 60 | len = 6; 61 | end 62 | else 63 | if (value < uint64(72057594037927936)) % 2^56 64 | if (value < uint64(562949953421312)) % 2^49 65 | len = 7; 66 | else 67 | len = 8; 68 | end 69 | else 70 | if (value < uint64(9223372036854775808)) % 2^63 71 | len = 9; 72 | else 73 | len = 10; 74 | end 75 | end 76 | end 77 | end 78 | -------------------------------------------------------------------------------- /protobuflib/pblib_read_wire_type.m: -------------------------------------------------------------------------------- 1 | function [wire_value, num_read] = pblib_read_wire_type(buffer, offset, wire_type) 2 | %pblib_read_wire_type 3 | % function [wire_value, num_read] = pblib_read_wire_type(buffer, offset, wire_type) 4 | % 5 | % These values must match the WireType enum in 6 | % http://protobuf.googlecode.com/svn/trunk/src/google/protobuf/wire_format.h 7 | % 8 | % All Wire Types Are (in order, 0 based): 9 | % - 0: 'varint' 10 | % - 1: '64bit' 11 | % - 2: 'length_delimited' 12 | % - 3: 'start_group' 13 | % - 4: 'end_group' 14 | % - 5: '32bit' 15 | 16 | % protobuf-matlab - FarSounder's Protocol Buffer support for Matlab 17 | % Copyright (c) 2008, FarSounder Inc. All rights reserved. 18 | % http://code.google.com/p/protobuf-matlab/ 19 | % 20 | % Redistribution and use in source and binary forms, with or without 21 | % modification, are permitted provided that the following conditions are met: 22 | % 23 | % * Redistributions of source code must retain the above copyright 24 | % notice, this list of conditions and the following disclaimer. 25 | % 26 | % * Redistributions in binary form must reproduce the above copyright 27 | % notice, this list of conditions and the following disclaimer in the 28 | % documentation and/or other materials provided with the distribution. 29 | % 30 | % * Neither the name of the FarSounder Inc. nor the names of its 31 | % contributors may be used to endorse or promote products derived from this 32 | % software without specific prior written permission. 33 | % 34 | % THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 35 | % AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 36 | % IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 37 | % ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 38 | % LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 39 | % CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 40 | % SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 41 | % INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 42 | % CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 43 | % ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 44 | % POSSIBILITY OF SUCH DAMAGE. 45 | 46 | % Author: fedor.labounko@gmail.com (Fedor Labounko) 47 | % Support function used by Protobuf compiler generated .m files. 48 | 49 | switch wire_type 50 | case 0 51 | [wire_value, num_read] = pblib_read_varint64(buffer, offset); 52 | case 1 53 | num_read = 8; 54 | wire_value = buffer(offset : offset + num_read - 1); 55 | case 2 56 | [len, len_len] = pblib_read_varint32(buffer, offset); 57 | num_read = len + len_len; 58 | if len == 0 59 | wire_value = {uint8([]), 1, 0}; 60 | else 61 | wire_value = {buffer, offset + len_len, offset + num_read - 1}; 62 | end 63 | case 3 64 | error('proto:lib:read_wire_type', 'Start Group not implemented.'); 65 | case 4 66 | error('proto:lib:read_wire_type', 'End Group not implemented.'); 67 | case 5 68 | num_read = 4; 69 | wire_value = buffer(offset : offset + num_read - 1); 70 | otherwise 71 | error('proto:lib:read_wire_type', 'Invalid wire value. This is likely due to a malformed message.'); 72 | end 73 | 74 | 75 | 76 | 77 | -------------------------------------------------------------------------------- /protobuflib/pblib_write_varint.m: -------------------------------------------------------------------------------- 1 | function [buffer] = pblib_write_varint(value) 2 | %pblib_write_varint 3 | % buffer = pblib_write_varint(value) 4 | % 5 | % Encodes a uint value as a varint. 6 | % 7 | % The value passed in can be any uint up to uint64. We do a hand coded 8 | % binary search tree like Google's C++ code generation to quickly decide how 9 | % many bytes we'll need for the encoding, or at least estimate. 10 | 11 | % protobuf-matlab - FarSounder's Protocol Buffer support for Matlab 12 | % Copyright (c) 2008, FarSounder Inc. All rights reserved. 13 | % http://code.google.com/p/protobuf-matlab/ 14 | % 15 | % Redistribution and use in source and binary forms, with or without 16 | % modification, are permitted provided that the following conditions are met: 17 | % 18 | % * Redistributions of source code must retain the above copyright 19 | % notice, this list of conditions and the following disclaimer. 20 | % 21 | % * Redistributions in binary form must reproduce the above copyright 22 | % notice, this list of conditions and the following disclaimer in the 23 | % documentation and/or other materials provided with the distribution. 24 | % 25 | % * Neither the name of the FarSounder Inc. nor the names of its 26 | % contributors may be used to endorse or promote products derived from this 27 | % software without specific prior written permission. 28 | % 29 | % THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 30 | % AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 31 | % IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 32 | % ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 33 | % LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 34 | % CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 35 | % SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 36 | % INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 37 | % CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 38 | % ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 39 | % POSSIBILITY OF SUCH DAMAGE. 40 | 41 | % Author: fedor.labounko@gmail.com (Fedor Labounko) 42 | % Support function used by Protobuf compiler generated .m files. 43 | 44 | 45 | if (value < uint64(268435456)) % 2^28 46 | if (value < uint64(16384)) % 2^14 47 | if (value < uint64(128)) % 2^7 48 | buffer = zeros([1 1], 'uint8'); 49 | else 50 | buffer = zeros([1 2], 'uint8'); 51 | end 52 | else 53 | if (value < uint64(2097152)) % 2^21 54 | buffer = zeros([1 3], 'uint8'); 55 | else 56 | buffer = zeros([1 4], 'uint8'); 57 | end 58 | end 59 | else 60 | if (value < uint64(4398046511104)) % 2^42 61 | if (value < uint64(34359738368)) % 2^35 62 | buffer = zeros([1 5], 'uint8'); 63 | else 64 | buffer = zeros([1 6], 'uint8'); 65 | end 66 | else 67 | if (value < uint64(72057594037927936)) % 2^56 68 | buffer = zeros([1 8], 'uint8'); 69 | else 70 | buffer = zeros([1 10], 'uint8'); 71 | end 72 | end 73 | end 74 | num_bytes = 0; 75 | while (value > 127) 76 | num_bytes = num_bytes + 1; 77 | buffer(num_bytes) = bitset(bitand(value, 127), 8); 78 | value = bitshift(value, -7); 79 | end 80 | num_bytes = num_bytes + 1; 81 | buffer(num_bytes) = value; 82 | buffer = buffer(1 : num_bytes); 83 | -------------------------------------------------------------------------------- /protobuflib/pblib_get_serialized_size.m: -------------------------------------------------------------------------------- 1 | function [msg_size] = pblib_get_serialized_size(msg) 2 | %pblib_get_serialized_size 3 | % function [msg_size] = pblib_get_serialized_size(msg) 4 | % 5 | % Estimates the size a message will take when serialized. 6 | % 7 | % Will go through a message and estimate serialized sizes of valid fields. 8 | % Estimates generally include tag size plus encoded field size. 9 | % 10 | % See also pblib_generic_serialize_to_string, pblib_write_wire_type 11 | 12 | % protobuf-matlab - FarSounder's Protocol Buffer support for Matlab 13 | % Copyright (c) 2008, FarSounder Inc. All rights reserved. 14 | % http://code.google.com/p/protobuf-matlab/ 15 | % 16 | % Redistribution and use in source and binary forms, with or without 17 | % modification, are permitted provided that the following conditions are met: 18 | % 19 | % * Redistributions of source code must retain the above copyright 20 | % notice, this list of conditions and the following disclaimer. 21 | % 22 | % * Redistributions in binary form must reproduce the above copyright 23 | % notice, this list of conditions and the following disclaimer in the 24 | % documentation and/or other materials provided with the distribution. 25 | % 26 | % * Neither the name of the FarSounder Inc. nor the names of its 27 | % contributors may be used to endorse or promote products derived from this 28 | % software without specific prior written permission. 29 | % 30 | % THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 31 | % AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 32 | % IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 33 | % ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 34 | % LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 35 | % CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 36 | % SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 37 | % INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 38 | % CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 39 | % ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 40 | % POSSIBILITY OF SUCH DAMAGE. 41 | 42 | % Author: fedor.labounko@gmail.com (Fedor Labounko) 43 | % Support function used by Protobuf compiler generated .m files. 44 | 45 | 46 | LABEL_REPEATED = 3; 47 | WIRE_TYPE_LENGTH_DELIMITED = 2; 48 | msg_size = 0; 49 | descriptor = msg.descriptor_function(); 50 | for i=1:length(descriptor.fields) 51 | field = descriptor.fields(i); 52 | if (get(msg.has_field, field.name) == 0 || isempty(msg.(field.name))) 53 | continue; 54 | end 55 | 56 | if (field.options.packed) 57 | tag_length = pblib_encoded_tag_size(... 58 | field.number, WIRE_TYPE_LENGTH_DELIMITED); 59 | else 60 | tag_length = pblib_encoded_tag_size(... 61 | field.number, field.wire_type); 62 | end 63 | 64 | % need this extra if to make sure repeated strings/bytes are done correctly 65 | if (field.label == LABEL_REPEATED) 66 | msg_size = msg_size + tag_length + ... 67 | (1 - field.options.packed) * ... 68 | (length(msg.(field.name)) - 1) * tag_length; 69 | else 70 | msg_size = msg_size + tag_length; 71 | end 72 | msg_size = msg_size + pblib_encoded_field_size(msg.(field.name), field); 73 | end 74 | 75 | % Now add the space required by the stored unknown fields 76 | for i=1:length(msg.unknown_fields) 77 | msg_size = msg_size + length(msg.unknown_fields(i).raw_data); 78 | end 79 | 80 | 81 | 82 | -------------------------------------------------------------------------------- /protobuflib/pblib_type_to_estimated_encoded_length.m: -------------------------------------------------------------------------------- 1 | function len = pblib_type_to_estimated_encoded_length(type) 2 | % pblib_type_to_estimated_encoded_length 3 | % function len = pblib_type_to_estimated_encoded_length(type) 4 | % 5 | % Converts from proto type to estimated encoded length. 6 | % 7 | % This function is currently only used in the local function read_packed_field in 8 | % pblib_generic_parse_from_string.m. It is used to estimate how much space we need to 9 | % store the incoming packed values to avoid many reallocations. We can probably improve 10 | % this to switch on wire type and read all the values in, without storing them, to 11 | % calculate how many values are encoded in a buffer, but this is good enough until it 12 | % isn't. 13 | 14 | % protobuf-matlab - FarSounder's Protocol Buffer support for Matlab 15 | % Copyright (c) 2008, FarSounder Inc. All rights reserved. 16 | % http://code.google.com/p/protobuf-matlab/ 17 | % 18 | % Redistribution and use in source and binary forms, with or without 19 | % modification, are permitted provided that the following conditions are met: 20 | % 21 | % * Redistributions of source code must retain the above copyright 22 | % notice, this list of conditions and the following disclaimer. 23 | % 24 | % * Redistributions in binary form must reproduce the above copyright 25 | % notice, this list of conditions and the following disclaimer in the 26 | % documentation and/or other materials provided with the distribution. 27 | % 28 | % * Neither the name of the FarSounder Inc. nor the names of its 29 | % contributors may be used to endorse or promote products derived from this 30 | % software without specific prior written permission. 31 | % 32 | % THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 33 | % AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 34 | % IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 35 | % ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 36 | % LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 37 | % CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 38 | % SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 39 | % INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 40 | % CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 41 | % ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 42 | % POSSIBILITY OF SUCH DAMAGE. 43 | 44 | % Author: fedor.labounko@gmail.com (Fedor Labounko) 45 | % Support function used by Protobuf compiler generated .m files. 46 | 47 | type_to_estimated_encoded_length = [... 48 | 8, ... % double => always 8 bytes 49 | 4, ... % float => always 4 bytes 50 | 7, ... % int64 => probably large or else int32 would be used (varint can be as large as 10 bytes) 51 | 7, ... % uint64 => same as int64 52 | 2, ... % int32 => values < 16384 take up 2 bytes 53 | 8, ... % fixed64 => always 8 bytes 54 | 4, ... % fixed32 => always 4 bytes 55 | 1, ... % bool => probably either 0 or 1, which is 1 varint encoded byte 56 | -1, ... % string => this function shouldn't be used to estimate string length 57 | -1, ... % group => groups are unsupported in matlab 58 | -1, ... % message => this function shouldn't be used to estimate message length 59 | -1, ... % bytes => this function shouldn't be used to estimate byte length 60 | 2, ... % uint32 => values < 16384 take up 2 bytes 61 | 1, ... % enum => values expected to be < 128, which is 1 varint encoded byte 62 | 4, ... % sfixed32 => always 4 bytes 63 | 8, ... % sfixed64 => always 8 bytes 64 | 2, ... % sint32 => signed values abs value < 8192 take up 2 varint encoded bytes 65 | 7]; % sint64 => see int64 66 | len = type_to_estimated_encoded_length(type); 67 | -------------------------------------------------------------------------------- /protobuflib/pblib_encoded_field_size.m: -------------------------------------------------------------------------------- 1 | function [len] = pblib_encoded_field_size(field_value, field_descriptor) 2 | %pblib_encoded_field_size 3 | % [len] = pblib_encoded_field_size(field_value, field_descriptor) 4 | % 5 | % Returns the amount of space an encoded field with this value will take up, 6 | % NOT INCLUDING the tag. If a field is a repeated field, you should pass the 7 | % whole field to this function, not each individual element. 8 | % 9 | % This function assumes this field is not empty and probably will not work 10 | % correctly if it is as those cases haven't been tested. 11 | 12 | % protobuf-matlab - FarSounder's Protocol Buffer support for Matlab 13 | % Copyright (c) 2008, FarSounder Inc. All rights reserved. 14 | % http://code.google.com/p/protobuf-matlab/ 15 | % 16 | % Redistribution and use in source and binary forms, with or without 17 | % modification, are permitted provided that the following conditions are met: 18 | % 19 | % * Redistributions of source code must retain the above copyright 20 | % notice, this list of conditions and the following disclaimer. 21 | % 22 | % * Redistributions in binary form must reproduce the above copyright 23 | % notice, this list of conditions and the following disclaimer in the 24 | % documentation and/or other materials provided with the distribution. 25 | % 26 | % * Neither the name of the FarSounder Inc. nor the names of its 27 | % contributors may be used to endorse or promote products derived from this 28 | % software without specific prior written permission. 29 | % 30 | % THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 31 | % AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 32 | % IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 33 | % ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 34 | % LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 35 | % CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 36 | % SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 37 | % INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 38 | % CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 39 | % ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 40 | % POSSIBILITY OF SUCH DAMAGE. 41 | 42 | % Author: fedor.labounko@gmail.com (Fedor Labounko) 43 | % Support function used by Protobuf compiler generated .m files. 44 | 45 | 46 | LABEL_REPEATED = 3; 47 | switch(field_descriptor.wire_type) 48 | case 0 % 'varint' 49 | len = 0; 50 | for j=1:length(field_value) 51 | len = len + pblib_encoded_varint_size(... 52 | field_descriptor.write_function(field_value(j))); 53 | end 54 | case 1 % '64bit' 55 | len = length(field_value) * 8; 56 | case 2 % 'length_delimited' 57 | switch (field_descriptor.matlab_type) 58 | case {7, 8} % 'string' or 'bytes' 59 | if (field_descriptor.label == LABEL_REPEATED) 60 | len = 0; 61 | for j=1:length(field_value) 62 | temp_len = length(field_value{j}); 63 | len = len + ... 64 | pblib_encoded_varint_size(temp_len) + ... 65 | temp_len; 66 | end 67 | else 68 | temp_len = length(field_value); 69 | len = pblib_encoded_varint_size(temp_len) + ... 70 | temp_len; 71 | end 72 | case 9 % 'message' 73 | len = 0; 74 | for j=1:length(field_value) 75 | temp_len = pblib_get_serialized_size(field_value(j)); 76 | len = len + ... 77 | pblib_encoded_varint_size(temp_len) + ... 78 | temp_len; 79 | end 80 | otherwise 81 | error('proto:pblib_get_serialized_size', ... 82 | ['Unhandled case statement: wire_type ' ... 83 | num2str(field_descriptor.wire_type)]); 84 | end 85 | case 3 % 'start_group' 86 | error('proto:pblib_get_serialized_size', 'start_group unsupported in matlab'); 87 | case 4 % 'end_group' 88 | error('proto:pblib_get_serialized_size', 'end_group unsupported in matlab'); 89 | case 5 % '32bit' 90 | len = length(field_value) * 4; 91 | end 92 | 93 | 94 | -------------------------------------------------------------------------------- /protobuflib/pblib_generic_serialize_to_string.m: -------------------------------------------------------------------------------- 1 | function [buffer] = pblib_generic_serialize_to_string(msg) 2 | %pblib_generic_serialize_to_string 3 | % function [buffer] = pblib_generic_serialize_to_string(msg) 4 | 5 | % protobuf-matlab - FarSounder's Protocol Buffer support for Matlab 6 | % Copyright (c) 2008, FarSounder Inc. All rights reserved. 7 | % http://code.google.com/p/protobuf-matlab/ 8 | % 9 | % Redistribution and use in source and binary forms, with or without 10 | % modification, are permitted provided that the following conditions are met: 11 | % 12 | % * Redistributions of source code must retain the above copyright 13 | % notice, this list of conditions and the following disclaimer. 14 | % 15 | % * Redistributions in binary form must reproduce the above copyright 16 | % notice, this list of conditions and the following disclaimer in the 17 | % documentation and/or other materials provided with the distribution. 18 | % 19 | % * Neither the name of the FarSounder Inc. nor the names of its 20 | % contributors may be used to endorse or promote products derived from this 21 | % software without specific prior written permission. 22 | % 23 | % THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 24 | % AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 | % IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 | % ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 27 | % LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 | % CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 | % SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 | % INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 | % CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 | % ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 | % POSSIBILITY OF SUCH DAMAGE. 34 | 35 | % Author: fedor.labounko@gmail.com (Fedor Labounko) 36 | % Support function used by Protobuf compiler generated .m files. 37 | 38 | % enum values we use 39 | WIRE_TYPE_LENGTH_DELIMITED = 2; 40 | LABEL_REPEATED = 3; 41 | 42 | descriptor = msg.descriptor_function(); 43 | buffer = zeros([1 pblib_get_serialized_size(msg)], 'uint8'); 44 | num_written = 0; 45 | for i=1:length(descriptor.fields) 46 | field = descriptor.fields(i); 47 | if (get(msg.has_field, field.name) == 0) 48 | continue; 49 | end 50 | if (field.label == LABEL_REPEATED) 51 | if (field.options.packed) 52 | % two is the length delimited wire_type 53 | tag = pblib_write_tag(field.number, WIRE_TYPE_LENGTH_DELIMITED); 54 | buffer(num_written + 1 : num_written + length(tag)) = tag; 55 | num_written = num_written + length(tag); 56 | 57 | wire_values = write_packed_field(msg.(field.name), field); 58 | wire_value = pblib_write_wire_type(wire_values, WIRE_TYPE_LENGTH_DELIMITED); 59 | buffer(num_written + 1 : num_written + length(wire_value)) = wire_value; 60 | num_written = num_written + length(wire_value); 61 | else 62 | tag = pblib_write_tag(field.number, field.wire_type); 63 | for j=1:length(msg.(field.name)) 64 | buffer(num_written + 1 : num_written + length(tag)) = tag; 65 | num_written = num_written + length(tag); 66 | if (field.matlab_type == 7 || field.matlab_type == 8) % 'string' or 'bytes' 67 | value = msg.(field.name){j}; 68 | else 69 | value = msg.(field.name)(j); 70 | end 71 | wire_values = pblib_write_wire_type(field.write_function(value), field.wire_type); 72 | buffer(num_written + 1 : num_written + length(wire_values)) = wire_values; 73 | num_written = num_written + length(wire_values); 74 | end 75 | end 76 | else 77 | tag = pblib_write_tag(field.number, field.wire_type); 78 | buffer(num_written + 1 : num_written + length(tag)) = tag; 79 | num_written = num_written + length(tag); 80 | 81 | value = msg.(field.name); 82 | wire_value = pblib_write_wire_type(field.write_function(value), field.wire_type); 83 | buffer(num_written + 1 : num_written + length(wire_value)) = wire_value; 84 | num_written = num_written + length(wire_value); 85 | end 86 | end 87 | % now write the unknown fields 88 | for i=1:length(msg.unknown_fields) 89 | buffer(num_read + 1 : num_read + length(msg.unknown_fields(i).raw_data)) = ... 90 | msg.unknown_fields(i).raw_data; 91 | num_read = num_read + length(msg.unknown_fields(i).raw_data); 92 | end 93 | if (num_written ~= length(buffer)) 94 | error('proto:pblib_generic_serialize_to_string', ... 95 | ['num_written, ' num2str(num_written) ... 96 | ', is different from precalculated length ' ... 97 | num2str(length(buffer))]); 98 | end 99 | 100 | 101 | function [wire_values] = write_packed_field(values, field) 102 | wire_values = zeros([1 pblib_encoded_field_size(values, field)], 'uint8'); 103 | values = field.write_function(values); 104 | bytes_written = 0; 105 | for i=1:length(values) 106 | encoded_value = pblib_write_wire_type(values(i), field.wire_type); 107 | wire_values(bytes_written + 1 : bytes_written + length(encoded_value)) = encoded_value; 108 | bytes_written = bytes_written + length(encoded_value); 109 | end 110 | wire_values = wire_values(1 : bytes_written); 111 | 112 | -------------------------------------------------------------------------------- /protobuflib/pblib_generic_parse_from_string.m: -------------------------------------------------------------------------------- 1 | function [msg, num_read] = pblib_generic_parse_from_string(... 2 | buffer, descriptor, buffer_start, buffer_end) 3 | %pblib_generic_parse_from_string 4 | % [msg, num_read] = pblib_generic_parse_from_string(buffer, descriptor, buffer_start, buffer_end) 5 | % 6 | % INPUTS: 7 | % buffer : buffer to parse proto message from 8 | % descriptor : a proto message descriptor, as generated by one of the read functions 9 | % buffer_start : optional buffer start index, used so we can avoid reallocating the buffer 10 | % buffer_end : optional buffer end index, used so we can avoid reallocating the buffer 11 | 12 | % protobuf-matlab - FarSounder's Protocol Buffer support for Matlab 13 | % Copyright (c) 2008, FarSounder Inc. All rights reserved. 14 | % http://code.google.com/p/protobuf-matlab/ 15 | % 16 | % Redistribution and use in source and binary forms, with or without 17 | % modification, are permitted provided that the following conditions are met: 18 | % 19 | % * Redistributions of source code must retain the above copyright 20 | % notice, this list of conditions and the following disclaimer. 21 | % 22 | % * Redistributions in binary form must reproduce the above copyright 23 | % notice, this list of conditions and the following disclaimer in the 24 | % documentation and/or other materials provided with the distribution. 25 | % 26 | % * Neither the name of the FarSounder Inc. nor the names of its 27 | % contributors may be used to endorse or promote products derived from this 28 | % software without specific prior written permission. 29 | % 30 | % THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 31 | % AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 32 | % IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 33 | % ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 34 | % LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 35 | % CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 36 | % SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 37 | % INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 38 | % CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 39 | % ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 40 | % POSSIBILITY OF SUCH DAMAGE. 41 | 42 | % Author: fedor.labounko@gmail.com (Fedor Labounko) 43 | % Support function used by Protobuf compiler generated .m files. 44 | 45 | if (nargin < 3) 46 | buffer_start = 1; 47 | end 48 | if (nargin < 4) 49 | buffer_end = length(buffer); 50 | end 51 | 52 | % Label enum 53 | LABEL_OPTIONAL = 1; 54 | LABEL_REQUIRED = 2; 55 | LABEL_REPEATED = 3; 56 | 57 | % Create the has_field map and set default values 58 | msg.has_field = java.util.HashMap; 59 | for field=descriptor.fields 60 | put(msg.has_field, field.name, 0); 61 | msg.(field.name) = field.default_value; 62 | end 63 | 64 | msg.unknown_fields = []; 65 | num_read = buffer_start - 1; 66 | while (num_read < buffer_end) 67 | [number, wire_type, tag_len] = pblib_read_tag(buffer, num_read + 1); 68 | index = get(descriptor.field_indeces_by_number, number); 69 | [wire_value, temp_num_read] = pblib_read_wire_type(buffer, num_read + tag_len + 1, wire_type); 70 | if (~isempty(index)) 71 | field = descriptor.fields(index); 72 | if (field.wire_type ~= wire_type && ~field.options.packed) 73 | error('proto:read:wire_type_mismatch', ... 74 | ['Wire type mismatch while reading ' field.name ... 75 | '. Got ' num2str(wire_type) ' but expected ' ... 76 | num2str(field.wire_type)]); 77 | end 78 | if field.label == LABEL_REPEATED 79 | if field.options.packed 80 | msg.(field.name) = read_packed_field(field, wire_value); 81 | else 82 | % strings and byte arrays must be stored in cell arrays 83 | % and so need special treatment 84 | if (field.matlab_type == 7 || field.matlab_type == 8) % 'string' or 'bytes' 85 | if (get(msg.has_field, field.name)) 86 | msg.(field.name) = [msg.(field.name) field.read_function(wire_value)]; 87 | else 88 | msg.(field.name) = {field.read_function(wire_value)}; 89 | end 90 | else 91 | msg.(field.name) = [msg.(field.name) field.read_function(wire_value)]; 92 | end 93 | end 94 | else 95 | msg.(field.name) = field.read_function(wire_value); 96 | end 97 | put(msg.has_field, field.name, 1); 98 | else 99 | msg.unknown_fields = [... 100 | msg.unknown_fields struct(... 101 | 'number', number, 'wire_type', wire_type, ... 102 | 'raw_data', buffer(num_read + 1 : num_read + tag_len + temp_num_read))]; 103 | end 104 | num_read = num_read + tag_len + temp_num_read; 105 | end 106 | 107 | % Check to make sure required fields have been read in We will only issue a warning if 108 | % they haven't so that debugging the final message would be easier 109 | for field=descriptor.fields 110 | if field.label == LABEL_REQUIRED && ~get(msg.has_field, field.name) 111 | warning('proto:read:required_enforcement', ... 112 | 'Required field not set while parsing. This is an error.') 113 | end 114 | end 115 | 116 | function [values] = read_packed_field(field, wire_value) 117 | [wire_value, buffer_start, buffer_end] = deal(wire_value{:}); 118 | wire_values_length = buffer_end - buffer_start + 1; 119 | matlab_type_str = pblib_matlab_type_to_string(field.matlab_type); 120 | values = zeros(... 121 | [1 ceil(wire_values_length / ... 122 | pblib_type_to_estimated_encoded_length(field.type))], ... 123 | matlab_type_str); 124 | bytes_read_in = buffer_start - 1; 125 | num_values = 0; 126 | while bytes_read_in < buffer_end 127 | [num, num_read] = pblib_read_wire_type(wire_value, bytes_read_in + 1, field.wire_type); 128 | value = field.read_function(num); 129 | num_values = num_values + 1; 130 | values(num_values) = value; 131 | bytes_read_in = bytes_read_in + num_read; 132 | end 133 | values = values(1:num_values); 134 | -------------------------------------------------------------------------------- /src/farsounder/protobuf/compiler/matlab/matlab_generator.h: -------------------------------------------------------------------------------- 1 | // protobuf-matlab - FarSounder's Protocol Buffer support for Matlab 2 | // Copyright (c) 2008, FarSounder Inc. All rights reserved. 3 | // http://code.google.com/p/protobuf-matlab/ 4 | // 5 | // Redistribution and use in source and binary forms, with or without 6 | // modification, are permitted provided that the following conditions are met: 7 | // 8 | // * Redistributions of source code must retain the above copyright 9 | // notice, this list of conditions and the following disclaimer. 10 | // 11 | // * Redistributions in binary form must reproduce the above copyright 12 | // notice, this list of conditions and the following disclaimer in the 13 | // documentation and/or other materials provided with the distribution. 14 | // 15 | // * Neither the name of the FarSounder Inc. nor the names of its 16 | // contributors may be used to endorse or promote products derived from this 17 | // software without specific prior written permission. 18 | // 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 | // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 | // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | // POSSIBILITY OF SUCH DAMAGE. 30 | 31 | // Author: fedor.labounko@gmail.com (Fedor Labounko) 32 | // Based on Google's C++ Protobuf compiler. 33 | // 34 | // Generates Matlab code for a given .proto file. 35 | 36 | #ifndef FARSOUNDER_PROTOBUF_COMPILER_MATLAB_GENERATOR_H__ 37 | #define FARSOUNDER_PROTOBUF_COMPILER_MATLAB_GENERATOR_H__ 38 | 39 | #include 40 | #include 41 | #include 42 | #include 43 | 44 | namespace google { 45 | namespace protobuf { 46 | class Descriptor; 47 | class FileDescriptor; 48 | namespace io { 49 | class Printer; 50 | } // namespace io 51 | } // namespace protobuf 52 | } // namespace google 53 | 54 | namespace farsounder { 55 | namespace protobuf { 56 | namespace compiler { 57 | namespace matlab { 58 | 59 | // CodeGenerator implementation which generates a C++ source file and 60 | // header. If you create your own protocol compiler binary and you want 61 | // it to support C++ output, you can do so by registering an instance of this 62 | // CodeGenerator with the CommandLineInterface in your main() function. 63 | class LIBPROTOC_EXPORT MatlabGenerator : 64 | public ::google::protobuf::compiler::CodeGenerator { 65 | public: 66 | MatlabGenerator(); 67 | ~MatlabGenerator(); 68 | 69 | // implements CodeGenerator ---------------------------------------- 70 | bool Generate(const ::google::protobuf::FileDescriptor* file, 71 | const ::std::string& parameter, 72 | ::google::protobuf::compiler::GeneratorContext* output_directory, 73 | ::std::string* error) const; 74 | 75 | enum MatlabType { 76 | MATLABTYPE_INT32 = 1, // TYPE_SINT32, TYPE_INT32, TYPE_SFIXED32 77 | MATLABTYPE_INT64 = 2, // TYPE_INT64, TYPE_SINT64, TYPE_SFIXED64 78 | MATLABTYPE_UINT32 = 3, // TYPE_UINT32, TYPE_FIXED32, TYPE_BOOL 79 | MATLABTYPE_UINT64 = 4, // TYPE_UINT64, TYPE_FIXED64 80 | MATLABTYPE_DOUBLE = 5, // TYPE_DOUBLE 81 | MATLABTYPE_SINGLE = 6, // TYPE_FLOAT 82 | MATLABTYPE_STRING = 7, // TYPE_STRING 83 | MATLABTYPE_BYTES = 8, // TYPE_BYTES 84 | MATLABTYPE_MESSAGE = 9, // TYPE_MESSAGE, TYPE_GROUP 85 | MATLABTYPE_ENUM = 10, // TYPE_ENUM 86 | 87 | MAX_MATLABTYPE = 10 88 | 89 | }; 90 | static const MatlabType kTypeToMatlabTypeMap[ 91 | ::google::protobuf::FieldDescriptor::MAX_TYPE + 1]; 92 | static const ::std::string kMatlabTypeToString[MAX_MATLABTYPE + 1]; 93 | 94 | private: 95 | void PrintMessageFunctions() const; 96 | 97 | void PrintDescriptorFunction( 98 | const ::google::protobuf::Descriptor & descriptor) const; 99 | void PrintDescriptorHeader( 100 | ::google::protobuf::io::Printer & printer, 101 | const ::google::protobuf::Descriptor & descriptor) const; 102 | void PrintDescriptorComment( 103 | ::google::protobuf::io::Printer & printer, 104 | const ::google::protobuf::Descriptor & descriptor) const; 105 | void PrintDescriptorBody( 106 | ::google::protobuf::io::Printer & printer, 107 | const ::google::protobuf::Descriptor & descriptor) const; 108 | void PrintFieldDescriptors( 109 | ::google::protobuf::io::Printer & printer, 110 | const ::google::protobuf::Descriptor & descriptor) const; 111 | void PrintFieldIndecesByNumber( 112 | ::google::protobuf::io::Printer & printer, 113 | const ::google::protobuf::Descriptor & descriptor) const; 114 | 115 | void PrintReadFunction( 116 | const ::google::protobuf::Descriptor & descriptor) const; 117 | void PrintReadHeader( 118 | ::google::protobuf::io::Printer & printer, 119 | const ::google::protobuf::Descriptor & descriptor) const; 120 | void PrintReadComment( 121 | ::google::protobuf::io::Printer & printer, 122 | const ::google::protobuf::Descriptor & descriptor) const; 123 | void PrintReadBody( 124 | ::google::protobuf::io::Printer & printer, 125 | const ::google::protobuf::Descriptor & descriptor) const; 126 | 127 | ::std::string DefaultValueToString( 128 | const ::google::protobuf::FieldDescriptor & field) const; 129 | 130 | ::std::string MakeReadFunctionHandle( 131 | const ::google::protobuf::FieldDescriptor & field) const; 132 | ::std::string MakeWriteFunctionHandle( 133 | const ::google::protobuf::FieldDescriptor & field) const; 134 | 135 | ::std::string DescriptorFunctionName( 136 | const ::google::protobuf::Descriptor & descriptor) const; 137 | ::std::string ReadFunctionName( 138 | const ::google::protobuf::Descriptor & descriptor) const; 139 | 140 | // Very coarse-grained lock to ensure that Generate() is reentrant. 141 | // Guards file_ and printer_. 142 | mutable ::google::protobuf::internal::Mutex mutex_; 143 | mutable const ::google::protobuf::FileDescriptor* 144 | file_; // Set in Generate(). Under mutex_. 145 | mutable ::google::protobuf::compiler::GeneratorContext* 146 | output_directory_; // Set in Generate(). 147 | 148 | GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MatlabGenerator); 149 | }; 150 | 151 | } // namespace cpp 152 | } // namespace compiler 153 | } // namespace protobuf 154 | 155 | } // namespace farsounder 156 | #endif // FARSOUNDER_PROTOBUF_COMPILER_MATLAB_GENERATOR_H__ 157 | -------------------------------------------------------------------------------- /test/test.proto: -------------------------------------------------------------------------------- 1 | // protobuf-matlab - FarSounder's Protocol Buffer support for Matlab 2 | // Copyright (c) 2008, FarSounder Inc. All rights reserved. 3 | // http://code.google.com/p/protobuf-matlab/ 4 | // 5 | // Redistribution and use in source and binary forms, with or without 6 | // modification, are permitted provided that the following conditions are met: 7 | // 8 | // * Redistributions of source code must retain the above copyright 9 | // notice, this list of conditions and the following disclaimer. 10 | // 11 | // * Redistributions in binary form must reproduce the above copyright 12 | // notice, this list of conditions and the following disclaimer in the 13 | // documentation and/or other materials provided with the distribution. 14 | // 15 | // * Neither the name of the FarSounder Inc. nor the names of its 16 | // contributors may be used to endorse or promote products derived from this 17 | // software without specific prior written permission. 18 | // 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 | // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 | // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | // POSSIBILITY OF SUCH DAMAGE. 30 | 31 | // Author: fedor.labounko@gmail.com (Fedor Labounko) 32 | // 33 | // A proto file used for some unit tests. 34 | 35 | package test; 36 | import "test_import.proto"; 37 | 38 | option optimize_for = SPEED; 39 | 40 | // This proto includes every type of field in both singular and repeated 41 | // forms. 42 | message TestAllTypes { 43 | message NestedMessage { 44 | // The field name "b" fails to compile in proto1 because it conflicts with 45 | // a local variable named "b" in one of the generated methods. This file 46 | // needs to compile in proto1 to test backwards-compatibility. 47 | optional int32 bb = 1; 48 | } 49 | 50 | enum NestedEnum { 51 | FOO = 1; 52 | BAR = 2; 53 | BAZ = 3; 54 | } 55 | 56 | // Singular 57 | optional int32 optional_int32 = 1; 58 | optional int64 optional_int64 = 2; 59 | optional uint32 optional_uint32 = 3; 60 | optional uint64 optional_uint64 = 4; 61 | optional sint32 optional_sint32 = 5; 62 | optional sint64 optional_sint64 = 6; 63 | optional fixed32 optional_fixed32 = 7; 64 | optional fixed64 optional_fixed64 = 8; 65 | optional sfixed32 optional_sfixed32 = 9; 66 | optional sfixed64 optional_sfixed64 = 10; 67 | optional float optional_float = 11; 68 | optional double optional_double = 12; 69 | optional bool optional_bool = 13; 70 | optional string optional_string = 14; 71 | optional bytes optional_bytes = 15; 72 | 73 | optional NestedMessage optional_nested_message = 18; 74 | optional ForeignMessage optional_foreign_message = 19; 75 | optional test_import.ImportMessage optional_import_message = 20; 76 | 77 | optional NestedEnum optional_nested_enum = 21; 78 | optional ForeignEnum optional_foreign_enum = 22; 79 | optional test_import.ImportEnum optional_import_enum = 23; 80 | 81 | optional string optional_string_piece = 24 [ctype=STRING_PIECE]; 82 | optional string optional_cord = 25 [ctype=CORD]; 83 | 84 | // Repeated 85 | repeated int32 repeated_int32 = 31 [packed=true]; 86 | repeated int64 repeated_int64 = 32; 87 | repeated uint32 repeated_uint32 = 33; 88 | repeated uint64 repeated_uint64 = 34; 89 | repeated sint32 repeated_sint32 = 35; 90 | repeated sint64 repeated_sint64 = 36 [packed=true]; 91 | repeated fixed32 repeated_fixed32 = 37; 92 | repeated fixed64 repeated_fixed64 = 38; 93 | repeated sfixed32 repeated_sfixed32 = 39; 94 | repeated sfixed64 repeated_sfixed64 = 40 [packed=true]; 95 | repeated float repeated_float = 41; 96 | repeated double repeated_double = 42; 97 | repeated bool repeated_bool = 43; 98 | repeated string repeated_string = 44; 99 | repeated bytes repeated_bytes = 45; 100 | 101 | repeated NestedMessage repeated_nested_message = 48; 102 | repeated ForeignMessage repeated_foreign_message = 49; 103 | repeated test_import.ImportMessage repeated_import_message = 50; 104 | 105 | repeated NestedEnum repeated_nested_enum = 51; 106 | repeated ForeignEnum repeated_foreign_enum = 52; 107 | repeated test_import.ImportEnum repeated_import_enum = 53; 108 | 109 | repeated string repeated_string_piece = 54 [ctype=STRING_PIECE]; 110 | repeated string repeated_cord = 55 [ctype=CORD]; 111 | 112 | // Singular with defaults 113 | optional int32 default_int32 = 61 [default = 41 ]; 114 | optional int64 default_int64 = 62 [default = 42 ]; 115 | optional uint32 default_uint32 = 63 [default = 43 ]; 116 | optional uint64 default_uint64 = 64 [default = 44 ]; 117 | optional sint32 default_sint32 = 65 [default = -45 ]; 118 | optional sint64 default_sint64 = 66 [default = 46 ]; 119 | optional fixed32 default_fixed32 = 67 [default = 47 ]; 120 | optional fixed64 default_fixed64 = 68 [default = 48 ]; 121 | optional sfixed32 default_sfixed32 = 69 [default = 49 ]; 122 | optional sfixed64 default_sfixed64 = 70 [default = -50 ]; 123 | optional float default_float = 71 [default = 51.5 ]; 124 | optional double default_double = 72 [default = 52e3 ]; 125 | optional bool default_bool = 73 [default = true ]; 126 | optional string default_string = 74 [default = "hello"]; 127 | optional bytes default_bytes = 75 [default = "world"]; 128 | 129 | optional NestedEnum default_nested_enum = 81 [default = BAR ]; 130 | optional ForeignEnum default_foreign_enum = 82 [default = FOREIGN_BAR]; 131 | optional test_import.ImportEnum 132 | default_import_enum = 83 [default = IMPORT_BAR ]; 133 | 134 | optional string default_string_piece = 84 [ctype=STRING_PIECE,default="abc"]; 135 | optional string default_cord = 85 [ctype=CORD,default="123"]; 136 | } 137 | 138 | // Define these after TestAllTypes to make sure the compiler can handle 139 | // that. 140 | message ForeignMessage { 141 | optional int32 c = 1; 142 | } 143 | 144 | enum ForeignEnum { 145 | FOREIGN_FOO = 4; 146 | FOREIGN_BAR = 5; 147 | FOREIGN_BAZ = 6; 148 | } 149 | -------------------------------------------------------------------------------- /test/pb_run_test.m: -------------------------------------------------------------------------------- 1 | function [msg, new_msg, buffer] = pb_run_test() 2 | %pb_run_test 3 | % function [msg, new_msg, buffer] = pb_run_test() 4 | % 5 | % Runs a basic write/read comparison test of basic proto functionality. This 6 | % is in no way a comprehensive test but just to catch obvious and common 7 | % errors. It might report float roundoff errors due to the original being 8 | % stored as doubles. 9 | 10 | % protobuf-matlab - FarSounder's Protocol Buffer support for Matlab 11 | % Copyright (c) 2008, FarSounder Inc. All rights reserved. 12 | % http://code.google.com/p/protobuf-matlab/ 13 | % 14 | % Redistribution and use in source and binary forms, with or without 15 | % modification, are permitted provided that the following conditions are met: 16 | % 17 | % * Redistributions of source code must retain the above copyright 18 | % notice, this list of conditions and the following disclaimer. 19 | % 20 | % * Redistributions in binary form must reproduce the above copyright 21 | % notice, this list of conditions and the following disclaimer in the 22 | % documentation and/or other materials provided with the distribution. 23 | % 24 | % * Neither the name of the FarSounder Inc. nor the names of its 25 | % contributors may be used to endorse or promote products derived from this 26 | % software without specific prior written permission. 27 | % 28 | % THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 29 | % AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30 | % IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31 | % ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 32 | % LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 33 | % CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 34 | % SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 35 | % INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 36 | % CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 37 | % ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 38 | % POSSIBILITY OF SUCH DAMAGE. 39 | 40 | % Author: fedor.labounko@gmail.com (Fedor Labounko) 41 | 42 | msg = pb_read_test__TestAllTypes([]); 43 | msg = pblib_set(msg, 'optional_int32', 33); 44 | msg = pblib_set(msg, 'optional_int64', 234980098); 45 | msg = pblib_set(msg, 'optional_uint32', 23098); 46 | msg = pblib_set(msg, 'optional_uint64', 35098723058); 47 | msg = pblib_set(msg, 'optional_sint32', 25); 48 | msg = pblib_set(msg, 'optional_sint64', 98482); 49 | msg = pblib_set(msg, 'optional_fixed32', 2309); 50 | msg = pblib_set(msg, 'optional_fixed64', 24350809); 51 | msg = pblib_set(msg, 'optional_sfixed32', 2309); 52 | msg = pblib_set(msg, 'optional_sfixed64', 24350809); 53 | msg = pblib_set(msg, 'optional_float', 23089.235); 54 | msg = pblib_set(msg, 'optional_double', 24305.2342); 55 | msg = pblib_set(msg, 'optional_bool', 130498); 56 | msg = pblib_set(msg, 'optional_string', 'asldfkj'); 57 | msg = pblib_set(msg, 'optional_bytes', uint8('aslkdjlj')); 58 | msg = pblib_set(msg, 'optional_nested_message', pb_read_test__TestAllTypes__NestedMessage([])); 59 | msg.optional_nested_message = pblib_set(msg.optional_nested_message, 'bb', -2); 60 | msg = pblib_set(msg, 'optional_foreign_message', pb_read_test__ForeignMessage([])); 61 | msg.optional_foreign_message = pblib_set(msg.optional_foreign_message, 'c', 14098); 62 | msg = pblib_set(msg, 'optional_import_message', pb_read_test_import__ImportMessage([])); 63 | msg.optional_import_message = pblib_set(msg.optional_import_message, 'd', 98); 64 | 65 | msg = pblib_set(msg, 'repeated_int32', [23 33]); 66 | msg = pblib_set(msg, 'repeated_int64', [9823408 234980098]); 67 | msg = pblib_set(msg, 'repeated_uint32', [23098 23098]); 68 | msg = pblib_set(msg, 'repeated_uint64', [23408 35098723058]); 69 | msg = pblib_set(msg, 'repeated_sint32', [-48 25]); 70 | msg = pblib_set(msg, 'repeated_sint64', [-234080 98482]); 71 | msg = pblib_set(msg, 'repeated_fixed32', [2309 230489]); 72 | msg = pblib_set(msg, 'repeated_fixed64', [23408 24350809]); 73 | msg = pblib_set(msg, 'repeated_sfixed32', [-2309 230489]); 74 | msg = pblib_set(msg, 'repeated_sfixed64', [-23408 24350809]); 75 | msg = pblib_set(msg, 'repeated_float', [23089.235 245789 30948.23508]); 76 | msg = pblib_set(msg, 'repeated_double', [24305.2342 20398.234089]); 77 | msg = pblib_set(msg, 'repeated_bool', [130498 9038]); 78 | msg = pblib_set(msg, 'repeated_string', {'asldfkj', 'asdlkfj'}); 79 | msg = pblib_set(msg, 'repeated_bytes', {uint8('aslkdjlj'), uint8('hgsh')}); 80 | msg = pblib_set(msg, 'repeated_nested_message', ... 81 | [pb_read_test__TestAllTypes__NestedMessage([]) ... 82 | pb_read_test__TestAllTypes__NestedMessage([])]); 83 | msg.repeated_nested_message(1) = pblib_set(msg.repeated_nested_message(1), 'bb', -2); 84 | msg.repeated_nested_message(2) = pblib_set(msg.repeated_nested_message(2), 'bb', 12394087); 85 | msg = pblib_set(msg, 'repeated_foreign_message', ... 86 | [pb_read_test__ForeignMessage([]) ... 87 | pb_read_test__ForeignMessage([])]); 88 | msg.repeated_foreign_message(1) = pblib_set(msg.repeated_foreign_message(1), 'c', 14098); 89 | msg.repeated_foreign_message(2) = pblib_set(msg.repeated_foreign_message(2), 'c', -90); 90 | msg = pblib_set(msg, 'repeated_import_message', ... 91 | [pb_read_test_import__ImportMessage([]) ... 92 | pb_read_test_import__ImportMessage([])]); 93 | msg.repeated_import_message(1) = pblib_set(msg.repeated_import_message(1), 'd', 98); 94 | msg.repeated_import_message(2) = pblib_set(msg.repeated_import_message(2), 'd', 98); 95 | 96 | buffer = pblib_generic_serialize_to_string(msg); 97 | new_msg = pb_read_test__TestAllTypes(buffer); 98 | 99 | check_msg_equal(msg, new_msg); 100 | 101 | function check_msg_equal(old_msg, new_msg) 102 | d = new_msg.descriptor_function(); 103 | for i=1:length(d.fields) 104 | field = d.fields(i); 105 | if (field.label == 3) % repeated 106 | for j=1:length(old_msg.(field.name)) 107 | if (field.matlab_type == 7 || field.matlab_type == 8) 108 | old_val = old_msg.(field.name){j}; 109 | new_val = new_msg.(field.name){j}; 110 | else 111 | old_val = old_msg.(field.name)(j); 112 | new_val = new_msg.(field.name)(j); 113 | end 114 | if (field.matlab_type == 9) 115 | check_msg_equal(old_val, new_val); 116 | elseif (old_val ~= new_val) 117 | disp([field.name ': ' num2str(old_val) ' != ' num2str(new_val)]); 118 | end 119 | end 120 | else 121 | if (field.matlab_type == 9) 122 | check_msg_equal(old_msg.(field.name), new_msg.(field.name)); 123 | elseif (old_msg.(field.name) ~= new_msg.(field.name)) 124 | disp([field.name ': ' num2str(old_msg.(field.name)) ' != ' ... 125 | num2str(new_msg.(field.name))]); 126 | end 127 | end 128 | end 129 | 130 | -------------------------------------------------------------------------------- /src/Makefile.am: -------------------------------------------------------------------------------- 1 | ## Process this file with automake to produce Makefile.in 2 | 3 | if HAVE_ZLIB 4 | GZCHECKPROGRAMS = zcgzip zcgunzip 5 | GZHEADERS = google/protobuf/io/gzip_stream.h 6 | GZTESTS = google/protobuf/io/gzip_stream_unittest.sh 7 | else 8 | GZCHECKPROGRAMS = 9 | GZHEADERS = 10 | GZTESTS = 11 | endif 12 | 13 | if GCC 14 | # These are good warnings to turn on by default 15 | NO_OPT_CXXFLAGS = $(PTHREAD_CFLAGS) -Wall -Wwrite-strings -Woverloaded-virtual -Wno-sign-compare 16 | else 17 | NO_OPT_CXXFLAGS = $(PTHREAD_CFLAGS) 18 | endif 19 | 20 | AM_CXXFLAGS = $(NO_OPT_CXXFLAGS) $(PROTOBUF_OPT_FLAG) 21 | 22 | AM_LDFLAGS = $(PTHREAD_CFLAGS) 23 | 24 | # If I say "dist_include_DATA", automake complains that $(includedir) is not 25 | # a "legitimate" directory for DATA. Screw you, automake. 26 | protodir = $(includedir) 27 | nobase_dist_proto_DATA = google/protobuf/descriptor.proto \ 28 | google/protobuf/compiler/plugin.proto 29 | 30 | # Not sure why these don't get cleaned automatically. 31 | clean-local: 32 | rm -f *.loT 33 | 34 | CLEANFILES = $(protoc_outputs) unittest_proto_middleman \ 35 | testzip.jar testzip.list testzip.proto testzip.zip 36 | 37 | MAINTAINERCLEANFILES = \ 38 | Makefile.in 39 | 40 | nobase_include_HEADERS = \ 41 | google/protobuf/stubs/common.h \ 42 | google/protobuf/stubs/once.h \ 43 | google/protobuf/descriptor.h \ 44 | google/protobuf/descriptor.pb.h \ 45 | google/protobuf/descriptor_database.h \ 46 | google/protobuf/dynamic_message.h \ 47 | google/protobuf/extension_set.h \ 48 | google/protobuf/generated_message_util.h \ 49 | google/protobuf/generated_message_reflection.h \ 50 | google/protobuf/message.h \ 51 | google/protobuf/message_lite.h \ 52 | google/protobuf/reflection_ops.h \ 53 | google/protobuf/repeated_field.h \ 54 | google/protobuf/service.h \ 55 | google/protobuf/text_format.h \ 56 | google/protobuf/unknown_field_set.h \ 57 | google/protobuf/wire_format.h \ 58 | google/protobuf/wire_format_lite.h \ 59 | google/protobuf/wire_format_lite_inl.h \ 60 | google/protobuf/io/coded_stream.h \ 61 | $(GZHEADERS) \ 62 | google/protobuf/io/printer.h \ 63 | google/protobuf/io/tokenizer.h \ 64 | google/protobuf/io/zero_copy_stream.h \ 65 | google/protobuf/io/zero_copy_stream_impl.h \ 66 | google/protobuf/io/zero_copy_stream_impl_lite.h \ 67 | google/protobuf/compiler/code_generator.h \ 68 | google/protobuf/compiler/command_line_interface.h \ 69 | google/protobuf/compiler/importer.h \ 70 | google/protobuf/compiler/parser.h \ 71 | google/protobuf/compiler/plugin.h \ 72 | google/protobuf/compiler/plugin.pb.h \ 73 | google/protobuf/compiler/cpp/cpp_generator.h \ 74 | google/protobuf/compiler/java/java_generator.h \ 75 | google/protobuf/compiler/python/python_generator.h \ 76 | farsounder/protobuf/compiler/matlab/matlab_generator.h 77 | 78 | lib_LTLIBRARIES = libprotobuf-lite.la libprotobuf.la libprotoc.la 79 | 80 | libprotobuf_lite_la_LIBADD = $(PTHREAD_LIBS) 81 | libprotobuf_lite_la_LDFLAGS = -version-info 7:0:0 -export-dynamic -no-undefined 82 | libprotobuf_lite_la_SOURCES = \ 83 | google/protobuf/stubs/common.cc \ 84 | google/protobuf/stubs/once.cc \ 85 | google/protobuf/stubs/hash.h \ 86 | google/protobuf/stubs/map-util.h \ 87 | google/protobuf/stubs/stl_util-inl.h \ 88 | google/protobuf/extension_set.cc \ 89 | google/protobuf/generated_message_util.cc \ 90 | google/protobuf/message_lite.cc \ 91 | google/protobuf/repeated_field.cc \ 92 | google/protobuf/wire_format_lite.cc \ 93 | google/protobuf/io/coded_stream.cc \ 94 | google/protobuf/io/coded_stream_inl.h \ 95 | google/protobuf/io/zero_copy_stream.cc \ 96 | google/protobuf/io/zero_copy_stream_impl_lite.cc 97 | 98 | libprotobuf_la_LIBADD = $(PTHREAD_LIBS) 99 | libprotobuf_la_LDFLAGS = -version-info 7:0:0 -export-dynamic -no-undefined 100 | libprotobuf_la_SOURCES = \ 101 | $(libprotobuf_lite_la_SOURCES) \ 102 | google/protobuf/stubs/strutil.cc \ 103 | google/protobuf/stubs/strutil.h \ 104 | google/protobuf/stubs/substitute.cc \ 105 | google/protobuf/stubs/substitute.h \ 106 | google/protobuf/stubs/structurally_valid.cc \ 107 | google/protobuf/descriptor.cc \ 108 | google/protobuf/descriptor.pb.cc \ 109 | google/protobuf/descriptor_database.cc \ 110 | google/protobuf/dynamic_message.cc \ 111 | google/protobuf/extension_set_heavy.cc \ 112 | google/protobuf/generated_message_reflection.cc \ 113 | google/protobuf/message.cc \ 114 | google/protobuf/reflection_ops.cc \ 115 | google/protobuf/service.cc \ 116 | google/protobuf/text_format.cc \ 117 | google/protobuf/unknown_field_set.cc \ 118 | google/protobuf/wire_format.cc \ 119 | google/protobuf/io/gzip_stream.cc \ 120 | google/protobuf/io/printer.cc \ 121 | google/protobuf/io/tokenizer.cc \ 122 | google/protobuf/io/zero_copy_stream_impl.cc \ 123 | google/protobuf/compiler/importer.cc \ 124 | google/protobuf/compiler/parser.cc 125 | 126 | libprotoc_la_LIBADD = $(PTHREAD_LIBS) libprotobuf.la 127 | libprotoc_la_LDFLAGS = -version-info 7:0:0 -export-dynamic -no-undefined 128 | libprotoc_la_SOURCES = \ 129 | google/protobuf/compiler/code_generator.cc \ 130 | google/protobuf/compiler/command_line_interface.cc \ 131 | google/protobuf/compiler/plugin.cc \ 132 | google/protobuf/compiler/plugin.pb.cc \ 133 | google/protobuf/compiler/subprocess.cc \ 134 | google/protobuf/compiler/subprocess.h \ 135 | google/protobuf/compiler/zip_writer.cc \ 136 | google/protobuf/compiler/zip_writer.h \ 137 | google/protobuf/compiler/cpp/cpp_enum.cc \ 138 | google/protobuf/compiler/cpp/cpp_enum.h \ 139 | google/protobuf/compiler/cpp/cpp_enum_field.cc \ 140 | google/protobuf/compiler/cpp/cpp_enum_field.h \ 141 | google/protobuf/compiler/cpp/cpp_extension.cc \ 142 | google/protobuf/compiler/cpp/cpp_extension.h \ 143 | google/protobuf/compiler/cpp/cpp_field.cc \ 144 | google/protobuf/compiler/cpp/cpp_field.h \ 145 | google/protobuf/compiler/cpp/cpp_file.cc \ 146 | google/protobuf/compiler/cpp/cpp_file.h \ 147 | google/protobuf/compiler/cpp/cpp_generator.cc \ 148 | google/protobuf/compiler/cpp/cpp_helpers.cc \ 149 | google/protobuf/compiler/cpp/cpp_helpers.h \ 150 | google/protobuf/compiler/cpp/cpp_message.cc \ 151 | google/protobuf/compiler/cpp/cpp_message.h \ 152 | google/protobuf/compiler/cpp/cpp_message_field.cc \ 153 | google/protobuf/compiler/cpp/cpp_message_field.h \ 154 | google/protobuf/compiler/cpp/cpp_primitive_field.cc \ 155 | google/protobuf/compiler/cpp/cpp_primitive_field.h \ 156 | google/protobuf/compiler/cpp/cpp_service.cc \ 157 | google/protobuf/compiler/cpp/cpp_service.h \ 158 | google/protobuf/compiler/cpp/cpp_string_field.cc \ 159 | google/protobuf/compiler/cpp/cpp_string_field.h \ 160 | google/protobuf/compiler/java/java_enum.cc \ 161 | google/protobuf/compiler/java/java_enum.h \ 162 | google/protobuf/compiler/java/java_enum_field.cc \ 163 | google/protobuf/compiler/java/java_enum_field.h \ 164 | google/protobuf/compiler/java/java_extension.cc \ 165 | google/protobuf/compiler/java/java_extension.h \ 166 | google/protobuf/compiler/java/java_field.cc \ 167 | google/protobuf/compiler/java/java_field.h \ 168 | google/protobuf/compiler/java/java_file.cc \ 169 | google/protobuf/compiler/java/java_file.h \ 170 | google/protobuf/compiler/java/java_generator.cc \ 171 | google/protobuf/compiler/java/java_helpers.cc \ 172 | google/protobuf/compiler/java/java_helpers.h \ 173 | google/protobuf/compiler/java/java_message.cc \ 174 | google/protobuf/compiler/java/java_message.h \ 175 | google/protobuf/compiler/java/java_message_field.cc \ 176 | google/protobuf/compiler/java/java_message_field.h \ 177 | google/protobuf/compiler/java/java_primitive_field.cc \ 178 | google/protobuf/compiler/java/java_primitive_field.h \ 179 | google/protobuf/compiler/java/java_service.cc \ 180 | google/protobuf/compiler/java/java_service.h \ 181 | google/protobuf/compiler/java/java_string_field.cc \ 182 | google/protobuf/compiler/java/java_string_field.h \ 183 | google/protobuf/compiler/python/python_generator.cc \ 184 | farsounder/protobuf/compiler/matlab/matlab_generator.cc 185 | 186 | bin_PROGRAMS = protoc 187 | protoc_LDADD = $(PTHREAD_LIBS) libprotobuf.la libprotoc.la 188 | protoc_SOURCES = google/protobuf/compiler/main.cc 189 | 190 | # Tests ============================================================== 191 | 192 | protoc_inputs = \ 193 | google/protobuf/unittest.proto \ 194 | google/protobuf/unittest_empty.proto \ 195 | google/protobuf/unittest_import.proto \ 196 | google/protobuf/unittest_mset.proto \ 197 | google/protobuf/unittest_optimize_for.proto \ 198 | google/protobuf/unittest_embed_optimize_for.proto \ 199 | google/protobuf/unittest_custom_options.proto \ 200 | google/protobuf/unittest_lite.proto \ 201 | google/protobuf/unittest_import_lite.proto \ 202 | google/protobuf/unittest_lite_imports_nonlite.proto \ 203 | google/protobuf/unittest_no_generic_services.proto \ 204 | google/protobuf/compiler/cpp/cpp_test_bad_identifiers.proto 205 | 206 | EXTRA_DIST = \ 207 | $(protoc_inputs) \ 208 | solaris/libstdc++.la \ 209 | google/protobuf/io/gzip_stream.h \ 210 | google/protobuf/io/gzip_stream_unittest.sh \ 211 | google/protobuf/testdata/golden_message \ 212 | google/protobuf/testdata/golden_packed_fields_message \ 213 | google/protobuf/testdata/text_format_unittest_data.txt \ 214 | google/protobuf/testdata/text_format_unittest_extensions_data.txt \ 215 | google/protobuf/package_info.h \ 216 | google/protobuf/io/package_info.h \ 217 | google/protobuf/compiler/package_info.h \ 218 | google/protobuf/compiler/zip_output_unittest.sh \ 219 | google/protobuf/unittest_enormous_descriptor.proto 220 | 221 | protoc_lite_outputs = \ 222 | google/protobuf/unittest_lite.pb.cc \ 223 | google/protobuf/unittest_lite.pb.h \ 224 | google/protobuf/unittest_import_lite.pb.cc \ 225 | google/protobuf/unittest_import_lite.pb.h 226 | 227 | protoc_outputs = \ 228 | $(protoc_lite_outputs) \ 229 | google/protobuf/unittest.pb.cc \ 230 | google/protobuf/unittest.pb.h \ 231 | google/protobuf/unittest_empty.pb.cc \ 232 | google/protobuf/unittest_empty.pb.h \ 233 | google/protobuf/unittest_import.pb.cc \ 234 | google/protobuf/unittest_import.pb.h \ 235 | google/protobuf/unittest_mset.pb.cc \ 236 | google/protobuf/unittest_mset.pb.h \ 237 | google/protobuf/unittest_optimize_for.pb.cc \ 238 | google/protobuf/unittest_optimize_for.pb.h \ 239 | google/protobuf/unittest_embed_optimize_for.pb.cc \ 240 | google/protobuf/unittest_embed_optimize_for.pb.h \ 241 | google/protobuf/unittest_custom_options.pb.cc \ 242 | google/protobuf/unittest_custom_options.pb.h \ 243 | google/protobuf/unittest_lite_imports_nonlite.pb.cc \ 244 | google/protobuf/unittest_lite_imports_nonlite.pb.h \ 245 | google/protobuf/unittest_no_generic_services.pb.cc \ 246 | google/protobuf/unittest_no_generic_services.pb.h \ 247 | google/protobuf/compiler/cpp/cpp_test_bad_identifiers.pb.cc \ 248 | google/protobuf/compiler/cpp/cpp_test_bad_identifiers.pb.h 249 | 250 | BUILT_SOURCES = $(protoc_outputs) 251 | 252 | if USE_EXTERNAL_PROTOC 253 | 254 | unittest_proto_middleman: $(protoc_inputs) 255 | $(PROTOC) -I$(srcdir) --cpp_out=. $^ 256 | touch unittest_proto_middleman 257 | 258 | else 259 | 260 | # We have to cd to $(srcdir) before executing protoc because $(protoc_inputs) is 261 | # relative to srcdir, which may not be the same as the current directory when 262 | # building out-of-tree. 263 | unittest_proto_middleman: protoc$(EXEEXT) $(protoc_inputs) 264 | oldpwd=`pwd` && ( cd $(srcdir) && $$oldpwd/protoc$(EXEEXT) -I. --cpp_out=$$oldpwd $(protoc_inputs) ) 265 | touch unittest_proto_middleman 266 | 267 | endif 268 | 269 | $(protoc_outputs): unittest_proto_middleman 270 | 271 | COMMON_TEST_SOURCES = \ 272 | google/protobuf/test_util.cc \ 273 | google/protobuf/test_util.h \ 274 | google/protobuf/testing/googletest.cc \ 275 | google/protobuf/testing/googletest.h \ 276 | google/protobuf/testing/file.cc \ 277 | google/protobuf/testing/file.h 278 | 279 | check_PROGRAMS = protoc protobuf-test protobuf-lazy-descriptor-test \ 280 | protobuf-lite-test test_plugin $(GZCHECKPROGRAMS) 281 | protobuf_test_LDADD = $(PTHREAD_LIBS) libprotobuf.la libprotoc.la \ 282 | $(top_builddir)/gtest/lib/libgtest.la \ 283 | $(top_builddir)/gtest/lib/libgtest_main.la 284 | protobuf_test_CPPFLAGS = -I$(top_srcdir)/gtest/include \ 285 | -I$(top_builddir)/gtest/include 286 | # Disable optimization for tests unless the user explicitly asked for it, 287 | # since test_util.cc takes forever to compile with optimization (with GCC). 288 | # See configure.ac for more info. 289 | protobuf_test_CXXFLAGS = $(NO_OPT_CXXFLAGS) 290 | protobuf_test_SOURCES = \ 291 | google/protobuf/stubs/common_unittest.cc \ 292 | google/protobuf/stubs/once_unittest.cc \ 293 | google/protobuf/stubs/strutil_unittest.cc \ 294 | google/protobuf/stubs/structurally_valid_unittest.cc \ 295 | google/protobuf/descriptor_database_unittest.cc \ 296 | google/protobuf/descriptor_unittest.cc \ 297 | google/protobuf/dynamic_message_unittest.cc \ 298 | google/protobuf/extension_set_unittest.cc \ 299 | google/protobuf/generated_message_reflection_unittest.cc \ 300 | google/protobuf/message_unittest.cc \ 301 | google/protobuf/reflection_ops_unittest.cc \ 302 | google/protobuf/repeated_field_unittest.cc \ 303 | google/protobuf/text_format_unittest.cc \ 304 | google/protobuf/unknown_field_set_unittest.cc \ 305 | google/protobuf/wire_format_unittest.cc \ 306 | google/protobuf/io/coded_stream_unittest.cc \ 307 | google/protobuf/io/printer_unittest.cc \ 308 | google/protobuf/io/tokenizer_unittest.cc \ 309 | google/protobuf/io/zero_copy_stream_unittest.cc \ 310 | google/protobuf/compiler/command_line_interface_unittest.cc \ 311 | google/protobuf/compiler/importer_unittest.cc \ 312 | google/protobuf/compiler/mock_code_generator.cc \ 313 | google/protobuf/compiler/mock_code_generator.h \ 314 | google/protobuf/compiler/parser_unittest.cc \ 315 | google/protobuf/compiler/cpp/cpp_bootstrap_unittest.cc \ 316 | google/protobuf/compiler/cpp/cpp_unittest.cc \ 317 | google/protobuf/compiler/cpp/cpp_plugin_unittest.cc \ 318 | google/protobuf/compiler/java/java_plugin_unittest.cc \ 319 | google/protobuf/compiler/python/python_plugin_unittest.cc \ 320 | $(COMMON_TEST_SOURCES) 321 | nodist_protobuf_test_SOURCES = $(protoc_outputs) 322 | 323 | # Run cpp_unittest again with PROTOBUF_TEST_NO_DESCRIPTORS defined. 324 | protobuf_lazy_descriptor_test_LDADD = $(PTHREAD_LIBS) libprotobuf.la \ 325 | $(top_builddir)/gtest/lib/libgtest.la \ 326 | $(top_builddir)/gtest/lib/libgtest_main.la 327 | protobuf_lazy_descriptor_test_CPPFLAGS = -I$(top_srcdir)/gtest/include \ 328 | -I$(top_builddir)/gtest/include \ 329 | -DPROTOBUF_TEST_NO_DESCRIPTORS 330 | protobuf_lazy_descriptor_test_CXXFLAGS = $(NO_OPT_CXXFLAGS) 331 | protobuf_lazy_descriptor_test_SOURCES = \ 332 | google/protobuf/compiler/cpp/cpp_unittest.cc \ 333 | $(COMMON_TEST_SOURCES) 334 | nodist_protobuf_lazy_descriptor_test_SOURCES = $(protoc_outputs) 335 | 336 | # Build lite_unittest separately, since it doesn't use gtest. 337 | protobuf_lite_test_LDADD = $(PTHREAD_LIBS) libprotobuf-lite.la 338 | protobuf_lite_test_CXXFLAGS = $(NO_OPT_CXXFLAGS) 339 | protobuf_lite_test_SOURCES = \ 340 | google/protobuf/lite_unittest.cc \ 341 | google/protobuf/test_util_lite.cc \ 342 | google/protobuf/test_util_lite.h 343 | nodist_protobuf_lite_test_SOURCES = $(protoc_lite_outputs) 344 | 345 | # Test plugin binary. 346 | test_plugin_LDADD = $(PTHREAD_LIBS) libprotobuf.la libprotoc.la \ 347 | $(top_builddir)/gtest/lib/libgtest.la 348 | test_plugin_CPPFLAGS = -I$(top_srcdir)/gtest/include \ 349 | -I$(top_builddir)/gtest/include 350 | test_plugin_SOURCES = \ 351 | google/protobuf/compiler/mock_code_generator.cc \ 352 | google/protobuf/testing/file.cc \ 353 | google/protobuf/testing/file.h \ 354 | google/protobuf/compiler/test_plugin.cc 355 | 356 | if HAVE_ZLIB 357 | zcgzip_LDADD = $(PTHREAD_LIBS) libprotobuf.la 358 | zcgzip_SOURCES = google/protobuf/testing/zcgzip.cc 359 | 360 | zcgunzip_LDADD = $(PTHREAD_LIBS) libprotobuf.la 361 | zcgunzip_SOURCES = google/protobuf/testing/zcgunzip.cc 362 | endif 363 | 364 | TESTS = protobuf-test protobuf-lazy-descriptor-test protobuf-lite-test \ 365 | google/protobuf/compiler/zip_output_unittest.sh $(GZTESTS) 366 | -------------------------------------------------------------------------------- /src/farsounder/protobuf/compiler/matlab/matlab_generator.cc: -------------------------------------------------------------------------------- 1 | // protobuf-matlab - FarSounder's Protocol Buffer support for Matlab 2 | // Copyright (c) 2008, FarSounder Inc. All rights reserved. 3 | // http://code.google.com/p/protobuf-matlab/ 4 | // 5 | // Redistribution and use in source and binary forms, with or without 6 | // modification, are permitted provided that the following conditions are met: 7 | // 8 | // * Redistributions of source code must retain the above copyright 9 | // notice, this list of conditions and the following disclaimer. 10 | // 11 | // * Redistributions in binary form must reproduce the above copyright 12 | // notice, this list of conditions and the following disclaimer in the 13 | // documentation and/or other materials provided with the distribution. 14 | // 15 | // * Neither the name of the FarSounder Inc. nor the names of its 16 | // contributors may be used to endorse or promote products derived from this 17 | // software without specific prior written permission. 18 | // 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 | // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 | // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | // POSSIBILITY OF SUCH DAMAGE. 30 | 31 | // Author: fedor.labounko@gmail.com (Fedor Labounko) 32 | // Based on Google's C++ Protobuf compiler. 33 | // 34 | // Generates Matlab code for a given .proto file. 35 | 36 | #include 37 | 38 | #include 39 | #include 40 | #include 41 | #include 42 | #include 43 | #include 44 | #include 45 | 46 | #include 47 | #include 48 | #include 49 | #include 50 | #include 51 | 52 | namespace farsounder { 53 | namespace protobuf { 54 | namespace compiler { 55 | namespace matlab { 56 | 57 | using ::google::protobuf::Descriptor; 58 | using ::google::protobuf::FieldDescriptor; 59 | using ::google::protobuf::FieldDescriptor; 60 | using ::google::protobuf::FileDescriptor; 61 | using ::google::protobuf::LowerString; 62 | using ::google::protobuf::SimpleDtoa; 63 | using ::google::protobuf::SimpleFtoa; 64 | using ::google::protobuf::SimpleItoa; 65 | using ::google::protobuf::StringReplace; 66 | using ::google::protobuf::compiler::GeneratorContext; 67 | using ::google::protobuf::internal::MutexLock; 68 | using ::google::protobuf::io::Printer; 69 | 70 | using ::std::make_pair; 71 | using ::std::map; 72 | using ::std::max; 73 | using ::std::pair; 74 | using ::std::set; 75 | using ::std::string; 76 | using ::std::stringstream; 77 | using ::std::vector; 78 | 79 | namespace { 80 | // CamelToLower taken from Dave Benson's modification for a C implementation 81 | // of Protocol Buffers 82 | string CamelToLower(const string &name) { 83 | string new_name; 84 | if (name.size() < 2) { 85 | new_name = name; 86 | LowerString(&new_name); 87 | return new_name; 88 | } 89 | new_name += tolower(name[0]); 90 | for (int i = 1; i < name.size(); ++i) { 91 | if (isupper(name[i])) 92 | new_name += "_"; 93 | new_name += tolower(name[i]); 94 | } 95 | return new_name; 96 | } 97 | } // namespace 98 | 99 | // See Type enum in descriptor.h 100 | const MatlabGenerator::MatlabType 101 | MatlabGenerator::kTypeToMatlabTypeMap[FieldDescriptor::MAX_TYPE + 1] = { 102 | static_cast(-1), //invalid 103 | MATLABTYPE_DOUBLE, // TYPE_DOUBLE 104 | MATLABTYPE_SINGLE, // TYPE_FLOAT 105 | MATLABTYPE_INT64, // TYPE_INT64 106 | MATLABTYPE_UINT64, // TYPE_UINT64 107 | MATLABTYPE_INT32, // TYPE_INT32 108 | MATLABTYPE_UINT64, // TYPE_FIXED64 109 | MATLABTYPE_UINT32, // TYPE_FIXED32 110 | MATLABTYPE_UINT32, // TYPE_BOOL 111 | MATLABTYPE_STRING, // TYPE_STRING 112 | MATLABTYPE_MESSAGE, // TYPE_GROUP 113 | MATLABTYPE_MESSAGE, // TYPE_MESSAGE 114 | MATLABTYPE_BYTES, // TYPE_BYTES 115 | MATLABTYPE_UINT32, // TYPE_UINT32 116 | MATLABTYPE_ENUM, // TYPE_ENUM 117 | MATLABTYPE_INT32, // TYPE_SFIXED32 118 | MATLABTYPE_INT64, // TYPE_SFIXED64 119 | MATLABTYPE_INT32, // TYPE_SINT32 120 | MATLABTYPE_INT64 // TYPE_SINT64 121 | }; 122 | 123 | const string 124 | MatlabGenerator::kMatlabTypeToString[MatlabGenerator::MAX_MATLABTYPE + 1] = { 125 | "invalid", // invalid index 126 | "int32", // MATLABTYPE_INT32 127 | "int64", // MATLABTYPE_INT64 128 | "uint32", // MATLABTYPE_UINT32 129 | "uint64", // MATLABTYPE_UINT64 130 | "double", // MATLABTYPE_DOUBLE 131 | "single", // MATLABTYPE_SINGLE 132 | "string", // MATLABTYPE_STRING 133 | "uint8 vector", // MATLABTYPE_BYTES 134 | "message", // MATLABTYPE_MESSAGE 135 | "enum", // MATLABTYPE_ENUM 136 | }; 137 | 138 | MatlabGenerator::MatlabGenerator() {} 139 | MatlabGenerator::~MatlabGenerator() {} 140 | 141 | 142 | bool MatlabGenerator::Generate(const FileDescriptor* file, 143 | const string& parameter, 144 | GeneratorContext* output_directory, 145 | string* error) const { 146 | MutexLock lock(&mutex_); 147 | file_ = file; 148 | output_directory_ = output_directory; 149 | PrintMessageFunctions(); 150 | return true; 151 | } 152 | 153 | 154 | void MatlabGenerator::PrintMessageFunctions() const { 155 | for (int i = 0; i < file_->message_type_count(); ++i) { 156 | PrintDescriptorFunction(*file_->message_type(i)); 157 | PrintReadFunction(*file_->message_type(i)); 158 | } 159 | } 160 | 161 | 162 | void MatlabGenerator::PrintDescriptorFunction( 163 | const Descriptor & descriptor) const { 164 | // Print nested messages 165 | for (int i = 0; i < descriptor.nested_type_count(); ++i) { 166 | PrintDescriptorFunction(*descriptor.nested_type(i)); 167 | } 168 | 169 | string filename = DescriptorFunctionName(descriptor); 170 | filename += ".m"; 171 | google::protobuf::internal::scoped_ptr< 172 | google::protobuf::io::ZeroCopyOutputStream> 173 | output(output_directory_->Open(filename)); 174 | Printer printer(output.get(), '$'); 175 | 176 | PrintDescriptorHeader(printer, descriptor); 177 | PrintDescriptorComment(printer, descriptor); 178 | printer.Indent(); 179 | PrintDescriptorBody(printer, descriptor); 180 | printer.Outdent(); 181 | } 182 | 183 | 184 | void MatlabGenerator::PrintDescriptorHeader( 185 | Printer & printer, const Descriptor & descriptor) const { 186 | string function_name = DescriptorFunctionName(descriptor); 187 | printer.Print("function [descriptor] = $function_name$()\n", 188 | "function_name", function_name); 189 | } 190 | 191 | 192 | void MatlabGenerator::PrintDescriptorComment( 193 | Printer & printer, const Descriptor & descriptor) const { 194 | string name = descriptor.name(); 195 | string function_name = DescriptorFunctionName(descriptor); 196 | printer.Print("%$function_name$ Returns the descriptor for message $name$.\n", 197 | "name", name, "function_name", function_name); 198 | printer.Print("% "); 199 | PrintDescriptorHeader(printer, descriptor); 200 | printer.Print("%\n"); 201 | printer.Print("% See also $read_function$", 202 | "read_function", ReadFunctionName(descriptor)); 203 | printer.Print("\n"); 204 | } 205 | 206 | 207 | void MatlabGenerator::PrintDescriptorBody( 208 | Printer & printer, const Descriptor & descriptor) const { 209 | printer.Print("\n"); 210 | printer.Print("descriptor = struct( ...\n"); 211 | printer.Indent(); 212 | printer.Print("'name', '$name$', ...\n", "name", descriptor.name()); 213 | printer.Print("'full_name', '$full_name$', ...\n", "full_name", 214 | descriptor.full_name()); 215 | printer.Print("'filename', '$filename$', ...\n", "filename", 216 | descriptor.file()->name()); 217 | printer.Print("'containing_type', '$containing_type$', ...\n", 218 | "containing_type", 219 | descriptor.containing_type() == NULL ? "" : 220 | descriptor.containing_type()->full_name()); 221 | 222 | printer.Print("'fields', [ ...\n"); 223 | printer.Indent(); 224 | PrintFieldDescriptors(printer, descriptor); 225 | printer.Outdent(); 226 | printer.Print("], ...\n"); 227 | 228 | printer.Print("'extensions', [ ... % Not Implemented\n"); 229 | printer.Indent(); 230 | // PrintExtensions(printer, descriptor); 231 | printer.Outdent(); 232 | printer.Print("], ...\n"); 233 | 234 | printer.Print("'nested_types', [ ... % Not implemented\n"); 235 | printer.Indent(); 236 | // PrintNestedTypes(printer, descriptor); 237 | printer.Outdent(); 238 | printer.Print("], ...\n"); 239 | 240 | printer.Print("'enum_types', [ ... % Not Implemented\n"); 241 | printer.Indent(); 242 | // PrintEnumTypes(printer, descriptor); 243 | printer.Outdent(); 244 | printer.Print("], ...\n"); 245 | 246 | printer.Print("'options', [ ... % Not Implemented\n"); 247 | printer.Indent(); 248 | // PrintOptions(printer, descriptor); 249 | printer.Outdent(); 250 | printer.Print("] ...\n"); 251 | 252 | printer.Outdent(); 253 | printer.Print(");\n\n"); 254 | 255 | PrintFieldIndecesByNumber(printer, descriptor); 256 | } 257 | 258 | 259 | void MatlabGenerator::PrintFieldDescriptors( 260 | Printer & printer, const Descriptor & descriptor) const { 261 | vector > sorted_fields; 262 | for (int i = 0; i < descriptor.field_count(); ++i) { 263 | sorted_fields.push_back(make_pair(descriptor.field(i)->number(), i)); 264 | } 265 | sort(sorted_fields.begin(), sorted_fields.end()); 266 | 267 | const FieldDescriptor * field; 268 | map m; 269 | for (int i = 0; i < descriptor.field_count(); ++i) { 270 | field = descriptor.field(sorted_fields[i].second); 271 | m.clear(); 272 | printer.Print("struct( ...\n"); 273 | printer.Indent(); 274 | m["name"] = field->name(); 275 | m["full_name"] = field->full_name(); 276 | m["index"] = SimpleItoa(i + 1); 277 | m["number"] = SimpleItoa(field->number()); 278 | m["type"] = SimpleItoa(field->type()); 279 | m["matlab_type"] = SimpleItoa(kTypeToMatlabTypeMap[field->type()]); 280 | m["wire_type"] = SimpleItoa( 281 | google::protobuf::internal::WireFormat::WireTypeForFieldType( 282 | field->type())); 283 | m["label"] = SimpleItoa(field->label()); 284 | m["default_value"] = DefaultValueToString(*field); 285 | 286 | m["read_function"] = MakeReadFunctionHandle(*field); 287 | m["write_function"] = MakeWriteFunctionHandle(*field); 288 | if (field->options().packed()) { 289 | m["packed"] = "true"; 290 | } else { 291 | m["packed"] = "false"; 292 | } 293 | printer.Print(m, 294 | "'name', '$name$', ...\n" 295 | "'full_name', '$full_name$', ...\n" 296 | "'index', $index$, ...\n" 297 | "'number', uint32($number$), ...\n" 298 | "'type', uint32($type$), ...\n" 299 | "'matlab_type', uint32($matlab_type$), ...\n" 300 | "'wire_type', uint32($wire_type$), ...\n" 301 | "'label', uint32($label$), ...\n" 302 | "'default_value', $default_value$, ...\n" 303 | "'read_function', $read_function$, ...\n" 304 | "'write_function', $write_function$, ...\n" 305 | "'options', struct('packed', $packed$) ...\n" 306 | ); 307 | printer.Outdent(); 308 | 309 | if (i != descriptor.field_count() - 1) 310 | printer.Print("), ...\n"); 311 | else 312 | printer.Print(") ...\n"); 313 | } 314 | } 315 | 316 | 317 | void MatlabGenerator::PrintFieldIndecesByNumber( 318 | Printer & printer, const Descriptor & descriptor) const { 319 | // Assumes the fields are entered into an array by increasing tag values 320 | printer.Print("descriptor.field_indeces_by_number = java.util.HashMap;\n"); 321 | const FieldDescriptor * field; 322 | map map_values; 323 | vector > sorted_fields; 324 | for (int i = 0; i < descriptor.field_count(); ++i) { 325 | sorted_fields.push_back(make_pair(descriptor.field(i)->number(), i)); 326 | } 327 | sort(sorted_fields.begin(), sorted_fields.end()); 328 | for (int i = 0; i < descriptor.field_count(); ++i) { 329 | field = descriptor.field(sorted_fields[i].second); 330 | printer.Print("put(descriptor.field_indeces_by_number, uint32($number$), $index$);\n", 331 | "number", SimpleItoa(field->number()), 332 | "index", SimpleItoa(i + 1)); 333 | } 334 | printer.Print("\n"); 335 | } 336 | 337 | 338 | void MatlabGenerator::PrintReadFunction(const Descriptor & descriptor) const { 339 | // Print nested messages 340 | for (int i = 0; i < descriptor.nested_type_count(); ++i) { 341 | PrintReadFunction(*descriptor.nested_type(i)); 342 | } 343 | 344 | string filename = ReadFunctionName(descriptor); 345 | filename += ".m"; 346 | google::protobuf::internal::scoped_ptr< 347 | google::protobuf::io::ZeroCopyOutputStream> 348 | output(output_directory_->Open(filename)); 349 | Printer printer(output.get(), '$'); 350 | 351 | PrintReadHeader(printer, descriptor); 352 | PrintReadComment(printer, descriptor); 353 | printer.Indent(); 354 | PrintReadBody(printer, descriptor); 355 | printer.Outdent(); 356 | } 357 | 358 | 359 | void MatlabGenerator::PrintReadHeader(Printer & printer, 360 | const Descriptor & descriptor) const { 361 | string name = CamelToLower(descriptor.name()); 362 | string function_name = ReadFunctionName(descriptor); 363 | printer.Print("function [$name$] = $function_name$(buffer, buffer_start, buffer_end)\n", 364 | "name", name, 365 | "function_name", function_name); 366 | } 367 | 368 | 369 | void MatlabGenerator::PrintReadComment(Printer & printer, 370 | const Descriptor & descriptor) const { 371 | string name = descriptor.name(); 372 | string function_name = ReadFunctionName(descriptor); 373 | printer.Print("%$function_name$ Reads the protobuf message $name$.\n", 374 | "name", name, 375 | "function_name", function_name); 376 | printer.Print("% "); 377 | PrintReadHeader(printer, descriptor); 378 | printer.Print("%\n"); 379 | printer.Print("% INPUTS:\n" 380 | "% buffer : a buffer of uint8's to parse\n" 381 | "% buffer_start : optional starting index to consider of the buffer\n" 382 | "% defaults to 1\n" 383 | "% buffer_end : optional ending index to consider of the buffer\n" 384 | "% defaults to length(buffer)\n" 385 | "%\n" 386 | "% MEMBERS:\n"); 387 | for (int i = 0; i < descriptor.field_count(); ++i) { 388 | const FieldDescriptor & field = *descriptor.field(i); 389 | string buffer_space(max(0, static_cast(15 - field.name().size())), ' '); 390 | printer.Print("% $name$$buffer$: ", 391 | "name", field.name(), 392 | "buffer", buffer_space); 393 | switch(field.label()) { 394 | case FieldDescriptor::LABEL_OPTIONAL: 395 | printer.Print("optional "); 396 | break; 397 | case FieldDescriptor::LABEL_REQUIRED: 398 | printer.Print("required "); 399 | break; 400 | case FieldDescriptor::LABEL_REPEATED: 401 | printer.Print("repeated "); 402 | break; 403 | default: 404 | GOOGLE_LOG(FATAL)<<"Unhandled case in print comment."; 405 | } 406 | if (field.type() == FieldDescriptor::TYPE_MESSAGE) { 407 | printer.Print("$type$", 408 | "read_function", ReadFunctionName(*field.message_type()), 409 | "type", field.message_type()->full_name()); 410 | } else { 411 | printer.Print("$type$", 412 | "type", kMatlabTypeToString[kTypeToMatlabTypeMap[field.type()]]); 413 | } 414 | printer.Print(", defaults to $default$.\n", "default", DefaultValueToString(field)); 415 | } 416 | bool first_to_print = true; 417 | string beginning_string = "%\n% See also"; 418 | 419 | // Will store all the types we'll print in the See also help string 420 | // Used so we don't print duplicates 421 | int num = 0; 422 | set used_types; 423 | 424 | #define PRINT_SEE_ALSO(DESCRIPTOR) \ 425 | if (used_types.count(DESCRIPTOR) == 0) { \ 426 | if (first_to_print) { \ 427 | printer.Print("$beginning$ $function$", \ 428 | "beginning", beginning_string, \ 429 | "function", ReadFunctionName(*(DESCRIPTOR))); \ 430 | first_to_print = false; \ 431 | } else { \ 432 | printer.Print(", $function$", \ 433 | "function", ReadFunctionName(*(DESCRIPTOR))); \ 434 | } \ 435 | used_types.insert(DESCRIPTOR); \ 436 | } 437 | 438 | // Add the containing type 439 | if (descriptor.containing_type() != NULL) { 440 | PRINT_SEE_ALSO(descriptor.containing_type()); 441 | } 442 | 443 | // Add types used as fields in this message 444 | for (int i = 0; i < descriptor.field_count(); ++i) { 445 | if (descriptor.field(i)->type() != 446 | ::google::protobuf::FieldDescriptor::TYPE_MESSAGE) { 447 | continue; 448 | } 449 | PRINT_SEE_ALSO(descriptor.field(i)->message_type()); 450 | } 451 | 452 | // Add nested types defined in this message 453 | for (int i = 0; i < descriptor.nested_type_count(); ++i) { 454 | PRINT_SEE_ALSO(descriptor.nested_type(i)); 455 | } 456 | 457 | // Print other message types defined in the same file 458 | for (int i = 0; i < file_->message_type_count(); ++i) { 459 | if (file_->message_type(i) == &descriptor) 460 | continue; 461 | PRINT_SEE_ALSO(file_->message_type(i)); 462 | } 463 | #undef PRINT_SEE_ALSO 464 | 465 | if (!first_to_print) { 466 | printer.Print(".\n"); 467 | } 468 | } 469 | 470 | 471 | void MatlabGenerator::PrintReadBody(Printer & printer, 472 | const Descriptor & descriptor) const { 473 | printer.Print("\n"); 474 | printer.Print("if (nargin < 1)\n" 475 | " buffer = uint8([]);\n" 476 | "end\n" 477 | "if (nargin < 2)\n" 478 | " buffer_start = 1;\n" 479 | "end\n" 480 | "if (nargin < 3)\n" 481 | " buffer_end = length(buffer);\n" 482 | "end\n" 483 | "\n"); 484 | string name = CamelToLower(descriptor.name()); 485 | string descriptor_function = DescriptorFunctionName(descriptor); 486 | printer.Print("descriptor = $descriptor_function$();\n", 487 | "descriptor_function", descriptor_function); 488 | printer.Print("$name$ = pblib_generic_parse_from_string(buffer, descriptor, buffer_start, buffer_end);\n", 489 | "name", name); 490 | printer.Print("$name$.descriptor_function = @$descriptor_function$;\n", 491 | "name", name, "descriptor_function", descriptor_function); 492 | } 493 | 494 | 495 | string MatlabGenerator::DefaultValueToString( 496 | const FieldDescriptor & field) const { 497 | MatlabType type = kTypeToMatlabTypeMap[field.type()]; 498 | field.has_default_value(); 499 | stringstream s; 500 | if (field.is_repeated()) { 501 | switch(type) { 502 | case MATLABTYPE_INT32: 503 | return "int32([])"; 504 | case MATLABTYPE_INT64: 505 | return "int64([])"; 506 | case MATLABTYPE_UINT32: 507 | return "uint32([])"; 508 | case MATLABTYPE_UINT64: 509 | return "uint64([])"; 510 | case MATLABTYPE_DOUBLE: 511 | return "double([])"; 512 | case MATLABTYPE_SINGLE: 513 | return "single([])"; 514 | case MATLABTYPE_MESSAGE: 515 | return "struct([])"; 516 | case MATLABTYPE_ENUM: 517 | return "int32([])"; 518 | case MATLABTYPE_STRING: 519 | return "char([])"; 520 | case MATLABTYPE_BYTES: 521 | return "uint8([])"; 522 | } 523 | } else { 524 | switch(type) { 525 | case MATLABTYPE_INT32: 526 | s << "int32(" << SimpleItoa(field.default_value_int32()) << ")"; 527 | return s.str(); 528 | case MATLABTYPE_INT64: 529 | s << "int64(" << SimpleItoa(field.default_value_int64()) << ")"; 530 | return s.str(); 531 | case MATLABTYPE_UINT32: 532 | s << "uint32("; 533 | // This is needed as the default values are in a union and if the bool 534 | // value is set it doesn't set the full 32 bits 535 | if (field.type() == FieldDescriptor::TYPE_BOOL) { 536 | s << SimpleItoa(field.default_value_bool()); 537 | } else { 538 | s << SimpleItoa(field.default_value_uint32()); 539 | } 540 | s << ")"; 541 | return s.str(); 542 | case MATLABTYPE_UINT64: 543 | s << "uint64(" << SimpleItoa(field.default_value_uint64()) << ")"; 544 | return s.str(); 545 | case MATLABTYPE_DOUBLE: 546 | s << "double(" << SimpleDtoa(field.default_value_double()) << ")"; 547 | return s.str(); 548 | case MATLABTYPE_SINGLE: 549 | s << "single(" << SimpleFtoa(field.default_value_float()) << ")"; 550 | return s.str(); 551 | case MATLABTYPE_MESSAGE: 552 | return "struct([])"; 553 | case MATLABTYPE_ENUM: 554 | s << "int32(" << SimpleItoa(field.default_value_enum()->number())<< ")"; 555 | return s.str(); 556 | case MATLABTYPE_STRING: 557 | s << "'" << StringReplace(field.default_value_string(), "'", "''", true) << "'"; 558 | return s.str(); 559 | case MATLABTYPE_BYTES: 560 | s << "uint8('" << StringReplace(field.default_value_string(), "'", "''", true) << "')"; 561 | return s.str(); 562 | } 563 | } 564 | GOOGLE_LOG(FATAL) << "Not reached."; 565 | return "''"; 566 | } 567 | 568 | 569 | string MatlabGenerator::MakeReadFunctionHandle( 570 | const FieldDescriptor & field) const { 571 | MatlabType type = kTypeToMatlabTypeMap[field.type()]; 572 | FieldDescriptor::Type proto_type = field.type(); 573 | string function_handle; 574 | switch(type) { 575 | case MATLABTYPE_INT32: 576 | // We must call pblib_helpers_first because the standard varint 577 | // will put the result into a uint64 578 | // 579 | // We must also figure out whether this is encoded using the ZigZag 580 | // encoding, which is the case if its type was specificied as sint32 or 581 | // sint64 582 | if (proto_type == FieldDescriptor::TYPE_SINT32) { 583 | return "@(x) pblib_helpers_first(typecast(pblib_helpers_iff(bitget(x, 1), bitset(bitshift(bitxor(intmax('uint64'), x), -1), 64), bitshift(x, -1)), 'int32'))"; 584 | } else { 585 | return "@(x) pblib_helpers_first(typecast(x, 'int32'))"; 586 | } 587 | case MATLABTYPE_INT64: 588 | // We must figure out whether this is encoded using the ZigZag encoding, 589 | // which is the case if its type was specificied as sint32 or sint64 590 | if (proto_type == FieldDescriptor::TYPE_SINT64) { 591 | return "@(x) typecast(pblib_helpers_iff(bitget(x, 1), bitset(bitshift(bitxor(intmax('uint64'), x), -1), 64), bitshift(x, -1)), 'int64')"; 592 | } else { 593 | return "@(x) typecast(x, 'int64')"; 594 | } 595 | case MATLABTYPE_UINT32: 596 | // We must call pblib_helpers_first because the standard varint 597 | // will put the result into a uint64 598 | return "@(x) pblib_helpers_first(typecast(x, 'uint32'))"; 599 | case MATLABTYPE_UINT64: 600 | return "@(x) typecast(x, 'uint64')"; 601 | case MATLABTYPE_DOUBLE: 602 | return "@(x) typecast(x, 'double')"; 603 | case MATLABTYPE_SINGLE: 604 | return "@(x) typecast(x, 'single')"; 605 | case MATLABTYPE_STRING: 606 | return "@(x) char(x{1}(x{2} : x{3}))"; 607 | case MATLABTYPE_BYTES: 608 | return "@(x) uint8(x{1}(x{2} : x{3}))"; 609 | case MATLABTYPE_MESSAGE: 610 | return "@(x) " + ReadFunctionName(*field.message_type()) + "(x{1}, x{2}, x{3})"; 611 | case MATLABTYPE_ENUM: 612 | // We must call pblib_helpers_first because the standard varint 613 | // will put the result into a uint64 614 | return "@(x) pblib_helpers_first(typecast(x, 'int32'))"; 615 | } 616 | GOOGLE_LOG(FATAL) << "Shouldn't get here since switch should catch all cases."; 617 | return ""; 618 | } 619 | 620 | 621 | string MatlabGenerator::MakeWriteFunctionHandle(const FieldDescriptor & field) const { 622 | // These functions should undo the work done by the read functions so as to 623 | // pass as input to write_varint the same values that read_varint would pass 624 | // back as output. That's why, in particular, we typecast fixed32, 625 | // sfixed32, etc. into uint8 as that's how read_varint would return them to 626 | // us. 627 | MatlabType matlab_type = kTypeToMatlabTypeMap[field.type()]; 628 | FieldDescriptor::Type type = field.type(); 629 | string function_handle; 630 | switch(matlab_type) { 631 | case MATLABTYPE_INT32: 632 | // We must figure out whether this is encoded using the ZigZag encoding, 633 | // which is the case if its type was specificied as sint32 or sint64 I 634 | // am not able to find an arithmetic shift in Matlab, but if one is 635 | // found we can do the zigzag with (n<<1) ^ (n>>31) 636 | switch (type) { 637 | case FieldDescriptor::TYPE_INT32: 638 | return "@(x) typecast(int32(x), 'uint32')"; 639 | case FieldDescriptor::TYPE_SFIXED32: 640 | return "@(x) typecast(int32(x), 'uint8')"; 641 | case FieldDescriptor::TYPE_SINT32: 642 | return "@(x) typecast(pblib_helpers_iff(int32(x) < 0, -2 * int32(x) - 1, 2 * int32(x)), 'uint32')"; 643 | } 644 | GOOGLE_LOG(DFATAL)<<"Unhandled matlabtype_int32 type "<>63) 651 | switch(type) { 652 | case FieldDescriptor::TYPE_INT64: 653 | return "@(x) typecast(int64(x), 'uint64')"; 654 | case FieldDescriptor::TYPE_SFIXED64: 655 | return "@(x) typecast(int64(x), 'uint8')"; 656 | case FieldDescriptor::TYPE_SINT64: 657 | return "@(x) pblib_helpers_iff(int64(x) < 0, bitxor(bitshift(typecast(int64(x), 'uint64'), 1), intmax('uint64')), bitshift(typecast(int64(x), 'uint64'), 1))"; 658 | } 659 | GOOGLE_LOG(DFATAL)<<"Unhandled matlabtype_int32 type "<