diff --git a/cpp/ChangeLog b/cpp/ChangeLog index 53a99910..71c7d5bf 100644 --- a/cpp/ChangeLog +++ b/cpp/ChangeLog @@ -1,4 +1,31 @@ +2010-08-29 version 0.5.4: + + * includes msgpack_vc2008.vcproj file in source package + * fixes type::fix_int types + +2010-08-27 version 0.5.3: + + * adds type::fix_{u,}int{8,16,32,64} types + * adds msgpack_pack_fix_{u,}int{8,16,32,64} functions + * adds packer::pack_fix_{u,}int{8,16,32,64} functions + * fixes include paths + +2010-07-14 version 0.5.2: + + * type::raw::str(), operator==, operator!=, operator< and operator> are now const + * generates version.h using AC_OUTPUT macro in ./configure + +2010-07-06 version 0.5.1: + + * Add msgpack_vrefbuffer_new and msgpack_vrefbuffer_free + * Add msgpack_sbuffer_new and msgpack_sbuffer_free + * Add msgpack_unpacker_next and msgpack_unpack_next + * msgpack::unpack returns void + * Add MSGPACK_VERSION{,_MAJOR,_MINOR} macros to check header version + * Add msgpack_version{,_major,_minor} functions to check library version + * ./configure supports --disable-cxx option not to build C++ API + 2010-04-29 version 0.5.0: * msgpack_object_type is changed. MSGPACK_OBJECT_NIL is now 0x00. diff --git a/cpp/Doxyfile b/cpp/Doxyfile new file mode 100644 index 00000000..ca772301 --- /dev/null +++ b/cpp/Doxyfile @@ -0,0 +1,1552 @@ +# Doxyfile 1.6.2 + +# This file describes the settings to be used by the documentation system +# doxygen (www.doxygen.org) for a project +# +# All text after a hash (#) is considered a comment and will be ignored +# The format is: +# TAG = value [value, ...] +# For lists items can also be appended using: +# TAG += value [value, ...] +# Values that contain spaces should be placed between quotes (" ") + +#--------------------------------------------------------------------------- +# Project related configuration options +#--------------------------------------------------------------------------- + +# This tag specifies the encoding used for all characters in the config file +# that follow. The default is UTF-8 which is also the encoding used for all +# text before the first occurrence of this tag. Doxygen uses libiconv (or the +# iconv built into libc) for the transcoding. See +# http://www.gnu.org/software/libiconv for the list of possible encodings. + +DOXYFILE_ENCODING = UTF-8 + +# The PROJECT_NAME tag is a single word (or a sequence of words surrounded +# by quotes) that should identify the project. + +PROJECT_NAME = "MessagePack" + +# The PROJECT_NUMBER tag can be used to enter a project or revision number. +# This could be handy for archiving the generated documentation or +# if some version control system is used. + +PROJECT_NUMBER = + +# The OUTPUT_DIRECTORY tag is used to specify the (relative or absolute) +# base path where the generated documentation will be put. +# If a relative path is entered, it will be relative to the location +# where doxygen was started. If left blank the current directory will be used. + +OUTPUT_DIRECTORY = doc + +# If the CREATE_SUBDIRS tag is set to YES, then doxygen will create +# 4096 sub-directories (in 2 levels) under the output directory of each output +# format and will distribute the generated files over these directories. +# Enabling this option can be useful when feeding doxygen a huge amount of +# source files, where putting all generated files in the same directory would +# otherwise cause performance problems for the file system. + +CREATE_SUBDIRS = NO + +# The OUTPUT_LANGUAGE tag is used to specify the language in which all +# documentation generated by doxygen is written. Doxygen will use this +# information to generate all constant output in the proper language. +# The default language is English, other supported languages are: +# Afrikaans, Arabic, Brazilian, Catalan, Chinese, Chinese-Traditional, +# Croatian, Czech, Danish, Dutch, Esperanto, Farsi, Finnish, French, German, +# Greek, Hungarian, Italian, Japanese, Japanese-en (Japanese with English +# messages), Korean, Korean-en, Lithuanian, Norwegian, Macedonian, Persian, +# Polish, Portuguese, Romanian, Russian, Serbian, Serbian-Cyrilic, Slovak, +# Slovene, Spanish, Swedish, Ukrainian, and Vietnamese. + +OUTPUT_LANGUAGE = English + +# If the BRIEF_MEMBER_DESC tag is set to YES (the default) Doxygen will +# include brief member descriptions after the members that are listed in +# the file and class documentation (similar to JavaDoc). +# Set to NO to disable this. + +BRIEF_MEMBER_DESC = YES + +# If the REPEAT_BRIEF tag is set to YES (the default) Doxygen will prepend +# the brief description of a member or function before the detailed description. +# Note: if both HIDE_UNDOC_MEMBERS and BRIEF_MEMBER_DESC are set to NO, the +# brief descriptions will be completely suppressed. + +REPEAT_BRIEF = YES + +# This tag implements a quasi-intelligent brief description abbreviator +# that is used to form the text in various listings. Each string +# in this list, if found as the leading text of the brief description, will be +# stripped from the text and the result after processing the whole list, is +# used as the annotated text. Otherwise, the brief description is used as-is. +# If left blank, the following values are used ("$name" is automatically +# replaced with the name of the entity): "The $name class" "The $name widget" +# "The $name file" "is" "provides" "specifies" "contains" +# "represents" "a" "an" "the" + +ABBREVIATE_BRIEF = + +# If the ALWAYS_DETAILED_SEC and REPEAT_BRIEF tags are both set to YES then +# Doxygen will generate a detailed section even if there is only a brief +# description. + +ALWAYS_DETAILED_SEC = NO + +# If the INLINE_INHERITED_MEMB tag is set to YES, doxygen will show all +# inherited members of a class in the documentation of that class as if those +# members were ordinary class members. Constructors, destructors and assignment +# operators of the base classes will not be shown. + +INLINE_INHERITED_MEMB = NO + +# If the FULL_PATH_NAMES tag is set to YES then Doxygen will prepend the full +# path before files name in the file list and in the header files. If set +# to NO the shortest path that makes the file name unique will be used. + +FULL_PATH_NAMES = YES + +# If the FULL_PATH_NAMES tag is set to YES then the STRIP_FROM_PATH tag +# can be used to strip a user-defined part of the path. Stripping is +# only done if one of the specified strings matches the left-hand part of +# the path. The tag can be used to show relative paths in the file list. +# If left blank the directory from which doxygen is run is used as the +# path to strip. + +STRIP_FROM_PATH = + +# The STRIP_FROM_INC_PATH tag can be used to strip a user-defined part of +# the path mentioned in the documentation of a class, which tells +# the reader which header file to include in order to use a class. +# If left blank only the name of the header file containing the class +# definition is used. Otherwise one should specify the include paths that +# are normally passed to the compiler using the -I flag. + +STRIP_FROM_INC_PATH = + +# If the SHORT_NAMES tag is set to YES, doxygen will generate much shorter +# (but less readable) file names. This can be useful is your file systems +# doesn't support long names like on DOS, Mac, or CD-ROM. + +SHORT_NAMES = NO + +# If the JAVADOC_AUTOBRIEF tag is set to YES then Doxygen +# will interpret the first line (until the first dot) of a JavaDoc-style +# comment as the brief description. If set to NO, the JavaDoc +# comments will behave just like regular Qt-style comments +# (thus requiring an explicit @brief command for a brief description.) + +JAVADOC_AUTOBRIEF = YES + +# If the QT_AUTOBRIEF tag is set to YES then Doxygen will +# interpret the first line (until the first dot) of a Qt-style +# comment as the brief description. If set to NO, the comments +# will behave just like regular Qt-style comments (thus requiring +# an explicit \brief command for a brief description.) + +QT_AUTOBRIEF = NO + +# The MULTILINE_CPP_IS_BRIEF tag can be set to YES to make Doxygen +# treat a multi-line C++ special comment block (i.e. a block of //! or /// +# comments) as a brief description. This used to be the default behaviour. +# The new default is to treat a multi-line C++ comment block as a detailed +# description. Set this tag to YES if you prefer the old behaviour instead. + +MULTILINE_CPP_IS_BRIEF = NO + +# If the INHERIT_DOCS tag is set to YES (the default) then an undocumented +# member inherits the documentation from any documented member that it +# re-implements. + +INHERIT_DOCS = YES + +# If the SEPARATE_MEMBER_PAGES tag is set to YES, then doxygen will produce +# a new page for each member. If set to NO, the documentation of a member will +# be part of the file/class/namespace that contains it. + +SEPARATE_MEMBER_PAGES = NO + +# The TAB_SIZE tag can be used to set the number of spaces in a tab. +# Doxygen uses this value to replace tabs by spaces in code fragments. + +TAB_SIZE = 8 + +# This tag can be used to specify a number of aliases that acts +# as commands in the documentation. An alias has the form "name=value". +# For example adding "sideeffect=\par Side Effects:\n" will allow you to +# put the command \sideeffect (or @sideeffect) in the documentation, which +# will result in a user-defined paragraph with heading "Side Effects:". +# You can put \n's in the value part of an alias to insert newlines. + +ALIASES = + +# Set the OPTIMIZE_OUTPUT_FOR_C tag to YES if your project consists of C +# sources only. Doxygen will then generate output that is more tailored for C. +# For instance, some of the names that are used will be different. The list +# of all members will be omitted, etc. + +OPTIMIZE_OUTPUT_FOR_C = NO + +# Set the OPTIMIZE_OUTPUT_JAVA tag to YES if your project consists of Java +# sources only. Doxygen will then generate output that is more tailored for +# Java. For instance, namespaces will be presented as packages, qualified +# scopes will look different, etc. + +OPTIMIZE_OUTPUT_JAVA = NO + +# Set the OPTIMIZE_FOR_FORTRAN tag to YES if your project consists of Fortran +# sources only. Doxygen will then generate output that is more tailored for +# Fortran. + +OPTIMIZE_FOR_FORTRAN = NO + +# Set the OPTIMIZE_OUTPUT_VHDL tag to YES if your project consists of VHDL +# sources. Doxygen will then generate output that is tailored for +# VHDL. + +OPTIMIZE_OUTPUT_VHDL = NO + +# Doxygen selects the parser to use depending on the extension of the files it parses. +# With this tag you can assign which parser to use for a given extension. +# Doxygen has a built-in mapping, but you can override or extend it using this tag. +# The format is ext=language, where ext is a file extension, and language is one of +# the parsers supported by doxygen: IDL, Java, Javascript, C#, C, C++, D, PHP, +# Objective-C, Python, Fortran, VHDL, C, C++. For instance to make doxygen treat +# .inc files as Fortran files (default is PHP), and .f files as C (default is Fortran), +# use: inc=Fortran f=C. Note that for custom extensions you also need to set FILE_PATTERNS otherwise the files are not read by doxygen. + +EXTENSION_MAPPING = + +# If you use STL classes (i.e. std::string, std::vector, etc.) but do not want +# to include (a tag file for) the STL sources as input, then you should +# set this tag to YES in order to let doxygen match functions declarations and +# definitions whose arguments contain STL classes (e.g. func(std::string); v.s. +# func(std::string) {}). This also make the inheritance and collaboration +# diagrams that involve STL classes more complete and accurate. + +BUILTIN_STL_SUPPORT = NO + +# If you use Microsoft's C++/CLI language, you should set this option to YES to +# enable parsing support. + +CPP_CLI_SUPPORT = NO + +# Set the SIP_SUPPORT tag to YES if your project consists of sip sources only. +# Doxygen will parse them like normal C++ but will assume all classes use public +# instead of private inheritance when no explicit protection keyword is present. + +SIP_SUPPORT = NO + +# For Microsoft's IDL there are propget and propput attributes to indicate getter +# and setter methods for a property. Setting this option to YES (the default) +# will make doxygen to replace the get and set methods by a property in the +# documentation. This will only work if the methods are indeed getting or +# setting a simple type. If this is not the case, or you want to show the +# methods anyway, you should set this option to NO. + +IDL_PROPERTY_SUPPORT = YES + +# If member grouping is used in the documentation and the DISTRIBUTE_GROUP_DOC +# tag is set to YES, then doxygen will reuse the documentation of the first +# member in the group (if any) for the other members of the group. By default +# all members of a group must be documented explicitly. + +DISTRIBUTE_GROUP_DOC = NO + +# Set the SUBGROUPING tag to YES (the default) to allow class member groups of +# the same type (for instance a group of public functions) to be put as a +# subgroup of that type (e.g. under the Public Functions section). Set it to +# NO to prevent subgrouping. Alternatively, this can be done per class using +# the \nosubgrouping command. + +SUBGROUPING = YES + +# When TYPEDEF_HIDES_STRUCT is enabled, a typedef of a struct, union, or enum +# is documented as struct, union, or enum with the name of the typedef. So +# typedef struct TypeS {} TypeT, will appear in the documentation as a struct +# with name TypeT. When disabled the typedef will appear as a member of a file, +# namespace, or class. And the struct will be named TypeS. This can typically +# be useful for C code in case the coding convention dictates that all compound +# types are typedef'ed and only the typedef is referenced, never the tag name. + +TYPEDEF_HIDES_STRUCT = NO + +# The SYMBOL_CACHE_SIZE determines the size of the internal cache use to +# determine which symbols to keep in memory and which to flush to disk. +# When the cache is full, less often used symbols will be written to disk. +# For small to medium size projects (<1000 input files) the default value is +# probably good enough. For larger projects a too small cache size can cause +# doxygen to be busy swapping symbols to and from disk most of the time +# causing a significant performance penality. +# If the system has enough physical memory increasing the cache will improve the +# performance by keeping more symbols in memory. Note that the value works on +# a logarithmic scale so increasing the size by one will rougly double the +# memory usage. The cache size is given by this formula: +# 2^(16+SYMBOL_CACHE_SIZE). The valid range is 0..9, the default is 0, +# corresponding to a cache size of 2^16 = 65536 symbols + +SYMBOL_CACHE_SIZE = 0 + +#--------------------------------------------------------------------------- +# Build related configuration options +#--------------------------------------------------------------------------- + +# If the EXTRACT_ALL tag is set to YES doxygen will assume all entities in +# documentation are documented, even if no documentation was available. +# Private class members and static file members will be hidden unless +# the EXTRACT_PRIVATE and EXTRACT_STATIC tags are set to YES + +EXTRACT_ALL = NO + +# If the EXTRACT_PRIVATE tag is set to YES all private members of a class +# will be included in the documentation. + +EXTRACT_PRIVATE = NO + +# If the EXTRACT_STATIC tag is set to YES all static members of a file +# will be included in the documentation. + +EXTRACT_STATIC = NO + +# If the EXTRACT_LOCAL_CLASSES tag is set to YES classes (and structs) +# defined locally in source files will be included in the documentation. +# If set to NO only classes defined in header files are included. + +EXTRACT_LOCAL_CLASSES = YES + +# This flag is only useful for Objective-C code. When set to YES local +# methods, which are defined in the implementation section but not in +# the interface are included in the documentation. +# If set to NO (the default) only methods in the interface are included. + +EXTRACT_LOCAL_METHODS = NO + +# If this flag is set to YES, the members of anonymous namespaces will be +# extracted and appear in the documentation as a namespace called +# 'anonymous_namespace{file}', where file will be replaced with the base +# name of the file that contains the anonymous namespace. By default +# anonymous namespace are hidden. + +EXTRACT_ANON_NSPACES = NO + +# If the HIDE_UNDOC_MEMBERS tag is set to YES, Doxygen will hide all +# undocumented members of documented classes, files or namespaces. +# If set to NO (the default) these members will be included in the +# various overviews, but no documentation section is generated. +# This option has no effect if EXTRACT_ALL is enabled. + +HIDE_UNDOC_MEMBERS = NO + +# If the HIDE_UNDOC_CLASSES tag is set to YES, Doxygen will hide all +# undocumented classes that are normally visible in the class hierarchy. +# If set to NO (the default) these classes will be included in the various +# overviews. This option has no effect if EXTRACT_ALL is enabled. + +HIDE_UNDOC_CLASSES = NO + +# If the HIDE_FRIEND_COMPOUNDS tag is set to YES, Doxygen will hide all +# friend (class|struct|union) declarations. +# If set to NO (the default) these declarations will be included in the +# documentation. + +HIDE_FRIEND_COMPOUNDS = NO + +# If the HIDE_IN_BODY_DOCS tag is set to YES, Doxygen will hide any +# documentation blocks found inside the body of a function. +# If set to NO (the default) these blocks will be appended to the +# function's detailed documentation block. + +HIDE_IN_BODY_DOCS = NO + +# The INTERNAL_DOCS tag determines if documentation +# that is typed after a \internal command is included. If the tag is set +# to NO (the default) then the documentation will be excluded. +# Set it to YES to include the internal documentation. + +INTERNAL_DOCS = NO + +# If the CASE_SENSE_NAMES tag is set to NO then Doxygen will only generate +# file names in lower-case letters. If set to YES upper-case letters are also +# allowed. This is useful if you have classes or files whose names only differ +# in case and if your file system supports case sensitive file names. Windows +# and Mac users are advised to set this option to NO. + +CASE_SENSE_NAMES = NO + +# If the HIDE_SCOPE_NAMES tag is set to NO (the default) then Doxygen +# will show members with their full class and namespace scopes in the +# documentation. If set to YES the scope will be hidden. + +HIDE_SCOPE_NAMES = NO + +# If the SHOW_INCLUDE_FILES tag is set to YES (the default) then Doxygen +# will put a list of the files that are included by a file in the documentation +# of that file. + +SHOW_INCLUDE_FILES = YES + +# If the FORCE_LOCAL_INCLUDES tag is set to YES then Doxygen +# will list include files with double quotes in the documentation +# rather than with sharp brackets. + +FORCE_LOCAL_INCLUDES = NO + +# If the INLINE_INFO tag is set to YES (the default) then a tag [inline] +# is inserted in the documentation for inline members. + +INLINE_INFO = YES + +# If the SORT_MEMBER_DOCS tag is set to YES (the default) then doxygen +# will sort the (detailed) documentation of file and class members +# alphabetically by member name. If set to NO the members will appear in +# declaration order. + +SORT_MEMBER_DOCS = YES + +# If the SORT_BRIEF_DOCS tag is set to YES then doxygen will sort the +# brief documentation of file, namespace and class members alphabetically +# by member name. If set to NO (the default) the members will appear in +# declaration order. + +SORT_BRIEF_DOCS = NO + +# If the SORT_MEMBERS_CTORS_1ST tag is set to YES then doxygen will sort the (brief and detailed) documentation of class members so that constructors and destructors are listed first. If set to NO (the default) the constructors will appear in the respective orders defined by SORT_MEMBER_DOCS and SORT_BRIEF_DOCS. This tag will be ignored for brief docs if SORT_BRIEF_DOCS is set to NO and ignored for detailed docs if SORT_MEMBER_DOCS is set to NO. + +SORT_MEMBERS_CTORS_1ST = NO + +# If the SORT_GROUP_NAMES tag is set to YES then doxygen will sort the +# hierarchy of group names into alphabetical order. If set to NO (the default) +# the group names will appear in their defined order. + +SORT_GROUP_NAMES = NO + +# If the SORT_BY_SCOPE_NAME tag is set to YES, the class list will be +# sorted by fully-qualified names, including namespaces. If set to +# NO (the default), the class list will be sorted only by class name, +# not including the namespace part. +# Note: This option is not very useful if HIDE_SCOPE_NAMES is set to YES. +# Note: This option applies only to the class list, not to the +# alphabetical list. + +SORT_BY_SCOPE_NAME = NO + +# The GENERATE_TODOLIST tag can be used to enable (YES) or +# disable (NO) the todo list. This list is created by putting \todo +# commands in the documentation. + +GENERATE_TODOLIST = YES + +# The GENERATE_TESTLIST tag can be used to enable (YES) or +# disable (NO) the test list. This list is created by putting \test +# commands in the documentation. + +GENERATE_TESTLIST = YES + +# The GENERATE_BUGLIST tag can be used to enable (YES) or +# disable (NO) the bug list. This list is created by putting \bug +# commands in the documentation. + +GENERATE_BUGLIST = YES + +# The GENERATE_DEPRECATEDLIST tag can be used to enable (YES) or +# disable (NO) the deprecated list. This list is created by putting +# \deprecated commands in the documentation. + +GENERATE_DEPRECATEDLIST= YES + +# The ENABLED_SECTIONS tag can be used to enable conditional +# documentation sections, marked by \if sectionname ... \endif. + +ENABLED_SECTIONS = + +# The MAX_INITIALIZER_LINES tag determines the maximum number of lines +# the initial value of a variable or define consists of for it to appear in +# the documentation. If the initializer consists of more lines than specified +# here it will be hidden. Use a value of 0 to hide initializers completely. +# The appearance of the initializer of individual variables and defines in the +# documentation can be controlled using \showinitializer or \hideinitializer +# command in the documentation regardless of this setting. + +MAX_INITIALIZER_LINES = 30 + +# Set the SHOW_USED_FILES tag to NO to disable the list of files generated +# at the bottom of the documentation of classes and structs. If set to YES the +# list will mention the files that were used to generate the documentation. + +SHOW_USED_FILES = YES + +# If the sources in your project are distributed over multiple directories +# then setting the SHOW_DIRECTORIES tag to YES will show the directory hierarchy +# in the documentation. The default is NO. + +SHOW_DIRECTORIES = YES + +# Set the SHOW_FILES tag to NO to disable the generation of the Files page. +# This will remove the Files entry from the Quick Index and from the +# Folder Tree View (if specified). The default is YES. + +SHOW_FILES = YES + +# Set the SHOW_NAMESPACES tag to NO to disable the generation of the +# Namespaces page. +# This will remove the Namespaces entry from the Quick Index +# and from the Folder Tree View (if specified). The default is YES. + +SHOW_NAMESPACES = YES + +# The FILE_VERSION_FILTER tag can be used to specify a program or script that +# doxygen should invoke to get the current version for each file (typically from +# the version control system). Doxygen will invoke the program by executing (via +# popen()) the command , where is the value of +# the FILE_VERSION_FILTER tag, and is the name of an input file +# provided by doxygen. Whatever the program writes to standard output +# is used as the file version. See the manual for examples. + +FILE_VERSION_FILTER = + +# The LAYOUT_FILE tag can be used to specify a layout file which will be parsed by +# doxygen. The layout file controls the global structure of the generated output files +# in an output format independent way. The create the layout file that represents +# doxygen's defaults, run doxygen with the -l option. You can optionally specify a +# file name after the option, if omitted DoxygenLayout.xml will be used as the name +# of the layout file. + +LAYOUT_FILE = + +#--------------------------------------------------------------------------- +# configuration options related to warning and progress messages +#--------------------------------------------------------------------------- + +# The QUIET tag can be used to turn on/off the messages that are generated +# by doxygen. Possible values are YES and NO. If left blank NO is used. + +QUIET = NO + +# The WARNINGS tag can be used to turn on/off the warning messages that are +# generated by doxygen. Possible values are YES and NO. If left blank +# NO is used. + +WARNINGS = YES + +# If WARN_IF_UNDOCUMENTED is set to YES, then doxygen will generate warnings +# for undocumented members. If EXTRACT_ALL is set to YES then this flag will +# automatically be disabled. + +WARN_IF_UNDOCUMENTED = YES + +# If WARN_IF_DOC_ERROR is set to YES, doxygen will generate warnings for +# potential errors in the documentation, such as not documenting some +# parameters in a documented function, or documenting parameters that +# don't exist or using markup commands wrongly. + +WARN_IF_DOC_ERROR = YES + +# This WARN_NO_PARAMDOC option can be abled to get warnings for +# functions that are documented, but have no documentation for their parameters +# or return value. If set to NO (the default) doxygen will only warn about +# wrong or incomplete parameter documentation, but not about the absence of +# documentation. + +WARN_NO_PARAMDOC = NO + +# The WARN_FORMAT tag determines the format of the warning messages that +# doxygen can produce. The string should contain the $file, $line, and $text +# tags, which will be replaced by the file and line number from which the +# warning originated and the warning text. Optionally the format may contain +# $version, which will be replaced by the version of the file (if it could +# be obtained via FILE_VERSION_FILTER) + +WARN_FORMAT = "$file:$line: $text" + +# The WARN_LOGFILE tag can be used to specify a file to which warning +# and error messages should be written. If left blank the output is written +# to stderr. + +WARN_LOGFILE = + +#--------------------------------------------------------------------------- +# configuration options related to the input files +#--------------------------------------------------------------------------- + +# The INPUT tag can be used to specify the files and/or directories that contain +# documented source files. You may enter file names like "myfile.cpp" or +# directories like "/usr/src/myproject". Separate the files or directories +# with spaces. + +INPUT = + +# This tag can be used to specify the character encoding of the source files +# that doxygen parses. Internally doxygen uses the UTF-8 encoding, which is +# also the default input encoding. Doxygen uses libiconv (or the iconv built +# into libc) for the transcoding. See http://www.gnu.org/software/libiconv for +# the list of possible encodings. + +INPUT_ENCODING = UTF-8 + +# If the value of the INPUT tag contains directories, you can use the +# FILE_PATTERNS tag to specify one or more wildcard pattern (like *.cpp +# and *.h) to filter out the source-files in the directories. If left +# blank the following patterns are tested: +# *.c *.cc *.cxx *.cpp *.c++ *.java *.ii *.ixx *.ipp *.i++ *.inl *.h *.hh *.hxx +# *.hpp *.h++ *.idl *.odl *.cs *.php *.php3 *.inc *.m *.mm *.py *.f90 + +FILE_PATTERNS = *.hpp *.h + +# The RECURSIVE tag can be used to turn specify whether or not subdirectories +# should be searched for input files as well. Possible values are YES and NO. +# If left blank NO is used. + +#RECURSIVE = NO +RECURSIVE = YES + +# The EXCLUDE tag can be used to specify files and/or directories that should +# excluded from the INPUT source files. This way you can easily exclude a +# subdirectory from a directory tree whose root is specified with the INPUT tag. + +EXCLUDE = + +# The EXCLUDE_SYMLINKS tag can be used select whether or not files or +# directories that are symbolic links (a Unix filesystem feature) are excluded +# from the input. + +EXCLUDE_SYMLINKS = NO + +# If the value of the INPUT tag contains directories, you can use the +# EXCLUDE_PATTERNS tag to specify one or more wildcard patterns to exclude +# certain files from those directories. Note that the wildcards are matched +# against the file with absolute path, so to exclude all test directories +# for example use the pattern */test/* + +EXCLUDE_PATTERNS = + +# The EXCLUDE_SYMBOLS tag can be used to specify one or more symbol names +# (namespaces, classes, functions, etc.) that should be excluded from the +# output. The symbol name can be a fully qualified name, a word, or if the +# wildcard * is used, a substring. Examples: ANamespace, AClass, +# AClass::ANamespace, ANamespace::*Test + +EXCLUDE_SYMBOLS = + +# The EXAMPLE_PATH tag can be used to specify one or more files or +# directories that contain example code fragments that are included (see +# the \include command). + +EXAMPLE_PATH = + +# If the value of the EXAMPLE_PATH tag contains directories, you can use the +# EXAMPLE_PATTERNS tag to specify one or more wildcard pattern (like *.cpp +# and *.h) to filter out the source-files in the directories. If left +# blank all files are included. + +EXAMPLE_PATTERNS = + +# If the EXAMPLE_RECURSIVE tag is set to YES then subdirectories will be +# searched for input files to be used with the \include or \dontinclude +# commands irrespective of the value of the RECURSIVE tag. +# Possible values are YES and NO. If left blank NO is used. + +EXAMPLE_RECURSIVE = NO + +# The IMAGE_PATH tag can be used to specify one or more files or +# directories that contain image that are included in the documentation (see +# the \image command). + +IMAGE_PATH = + +# The INPUT_FILTER tag can be used to specify a program that doxygen should +# invoke to filter for each input file. Doxygen will invoke the filter program +# by executing (via popen()) the command , where +# is the value of the INPUT_FILTER tag, and is the name of an +# input file. Doxygen will then use the output that the filter program writes +# to standard output. +# If FILTER_PATTERNS is specified, this tag will be +# ignored. + +INPUT_FILTER = + +# The FILTER_PATTERNS tag can be used to specify filters on a per file pattern +# basis. +# Doxygen will compare the file name with each pattern and apply the +# filter if there is a match. +# The filters are a list of the form: +# pattern=filter (like *.cpp=my_cpp_filter). See INPUT_FILTER for further +# info on how filters are used. If FILTER_PATTERNS is empty, INPUT_FILTER +# is applied to all files. + +FILTER_PATTERNS = + +# If the FILTER_SOURCE_FILES tag is set to YES, the input filter (if set using +# INPUT_FILTER) will be used to filter the input files when producing source +# files to browse (i.e. when SOURCE_BROWSER is set to YES). + +FILTER_SOURCE_FILES = NO + +#--------------------------------------------------------------------------- +# configuration options related to source browsing +#--------------------------------------------------------------------------- + +# If the SOURCE_BROWSER tag is set to YES then a list of source files will +# be generated. Documented entities will be cross-referenced with these sources. +# Note: To get rid of all source code in the generated output, make sure also +# VERBATIM_HEADERS is set to NO. + +SOURCE_BROWSER = NO + +# Setting the INLINE_SOURCES tag to YES will include the body +# of functions and classes directly in the documentation. + +INLINE_SOURCES = NO + +# Setting the STRIP_CODE_COMMENTS tag to YES (the default) will instruct +# doxygen to hide any special comment blocks from generated source code +# fragments. Normal C and C++ comments will always remain visible. + +STRIP_CODE_COMMENTS = YES + +# If the REFERENCED_BY_RELATION tag is set to YES +# then for each documented function all documented +# functions referencing it will be listed. + +REFERENCED_BY_RELATION = YES + +# If the REFERENCES_RELATION tag is set to YES +# then for each documented function all documented entities +# called/used by that function will be listed. + +REFERENCES_RELATION = YES + +# If the REFERENCES_LINK_SOURCE tag is set to YES (the default) +# and SOURCE_BROWSER tag is set to YES, then the hyperlinks from +# functions in REFERENCES_RELATION and REFERENCED_BY_RELATION lists will +# link to the source code. +# Otherwise they will link to the documentation. + +REFERENCES_LINK_SOURCE = YES + +# If the USE_HTAGS tag is set to YES then the references to source code +# will point to the HTML generated by the htags(1) tool instead of doxygen +# built-in source browser. The htags tool is part of GNU's global source +# tagging system (see http://www.gnu.org/software/global/global.html). You +# will need version 4.8.6 or higher. + +USE_HTAGS = NO + +# If the VERBATIM_HEADERS tag is set to YES (the default) then Doxygen +# will generate a verbatim copy of the header file for each class for +# which an include is specified. Set to NO to disable this. + +VERBATIM_HEADERS = YES + +#--------------------------------------------------------------------------- +# configuration options related to the alphabetical class index +#--------------------------------------------------------------------------- + +# If the ALPHABETICAL_INDEX tag is set to YES, an alphabetical index +# of all compounds will be generated. Enable this if the project +# contains a lot of classes, structs, unions or interfaces. + +ALPHABETICAL_INDEX = NO + +# If the alphabetical index is enabled (see ALPHABETICAL_INDEX) then +# the COLS_IN_ALPHA_INDEX tag can be used to specify the number of columns +# in which this list will be split (can be a number in the range [1..20]) + +COLS_IN_ALPHA_INDEX = 5 + +# In case all classes in a project start with a common prefix, all +# classes will be put under the same header in the alphabetical index. +# The IGNORE_PREFIX tag can be used to specify one or more prefixes that +# should be ignored while generating the index headers. + +IGNORE_PREFIX = + +#--------------------------------------------------------------------------- +# configuration options related to the HTML output +#--------------------------------------------------------------------------- + +# If the GENERATE_HTML tag is set to YES (the default) Doxygen will +# generate HTML output. + +GENERATE_HTML = YES + +# The HTML_OUTPUT tag is used to specify where the HTML docs will be put. +# If a relative path is entered the value of OUTPUT_DIRECTORY will be +# put in front of it. If left blank `html' will be used as the default path. + +HTML_OUTPUT = html + +# The HTML_FILE_EXTENSION tag can be used to specify the file extension for +# each generated HTML page (for example: .htm,.php,.asp). If it is left blank +# doxygen will generate files with .html extension. + +HTML_FILE_EXTENSION = .html + +# The HTML_HEADER tag can be used to specify a personal HTML header for +# each generated HTML page. If it is left blank doxygen will generate a +# standard header. + +HTML_HEADER = + +# The HTML_FOOTER tag can be used to specify a personal HTML footer for +# each generated HTML page. If it is left blank doxygen will generate a +# standard footer. + +HTML_FOOTER = + +# The HTML_STYLESHEET tag can be used to specify a user-defined cascading +# style sheet that is used by each HTML page. It can be used to +# fine-tune the look of the HTML output. If the tag is left blank doxygen +# will generate a default style sheet. Note that doxygen will try to copy +# the style sheet file to the HTML output directory, so don't put your own +# stylesheet in the HTML output directory as well, or it will be erased! + +HTML_STYLESHEET = + +# If the HTML_TIMESTAMP tag is set to YES then the footer of each generated HTML +# page will contain the date and time when the page was generated. Setting +# this to NO can help when comparing the output of multiple runs. + +HTML_TIMESTAMP = NO + +# If the HTML_ALIGN_MEMBERS tag is set to YES, the members of classes, +# files or namespaces will be aligned in HTML using tables. If set to +# NO a bullet list will be used. + +HTML_ALIGN_MEMBERS = YES + +# If the HTML_DYNAMIC_SECTIONS tag is set to YES then the generated HTML +# documentation will contain sections that can be hidden and shown after the +# page has loaded. For this to work a browser that supports +# JavaScript and DHTML is required (for instance Mozilla 1.0+, Firefox +# Netscape 6.0+, Internet explorer 5.0+, Konqueror, or Safari). + +HTML_DYNAMIC_SECTIONS = NO + +# If the GENERATE_DOCSET tag is set to YES, additional index files +# will be generated that can be used as input for Apple's Xcode 3 +# integrated development environment, introduced with OSX 10.5 (Leopard). +# To create a documentation set, doxygen will generate a Makefile in the +# HTML output directory. Running make will produce the docset in that +# directory and running "make install" will install the docset in +# ~/Library/Developer/Shared/Documentation/DocSets so that Xcode will find +# it at startup. +# See http://developer.apple.com/tools/creatingdocsetswithdoxygen.html for more information. + +GENERATE_DOCSET = NO + +# When GENERATE_DOCSET tag is set to YES, this tag determines the name of the +# feed. A documentation feed provides an umbrella under which multiple +# documentation sets from a single provider (such as a company or product suite) +# can be grouped. + +DOCSET_FEEDNAME = "Doxygen generated docs" + +# When GENERATE_DOCSET tag is set to YES, this tag specifies a string that +# should uniquely identify the documentation set bundle. This should be a +# reverse domain-name style string, e.g. com.mycompany.MyDocSet. Doxygen +# will append .docset to the name. + +DOCSET_BUNDLE_ID = org.doxygen.Project + +# If the GENERATE_HTMLHELP tag is set to YES, additional index files +# will be generated that can be used as input for tools like the +# Microsoft HTML help workshop to generate a compiled HTML help file (.chm) +# of the generated HTML documentation. + +GENERATE_HTMLHELP = NO + +# If the GENERATE_HTMLHELP tag is set to YES, the CHM_FILE tag can +# be used to specify the file name of the resulting .chm file. You +# can add a path in front of the file if the result should not be +# written to the html output directory. + +CHM_FILE = + +# If the GENERATE_HTMLHELP tag is set to YES, the HHC_LOCATION tag can +# be used to specify the location (absolute path including file name) of +# the HTML help compiler (hhc.exe). If non-empty doxygen will try to run +# the HTML help compiler on the generated index.hhp. + +HHC_LOCATION = + +# If the GENERATE_HTMLHELP tag is set to YES, the GENERATE_CHI flag +# controls if a separate .chi index file is generated (YES) or that +# it should be included in the master .chm file (NO). + +GENERATE_CHI = NO + +# If the GENERATE_HTMLHELP tag is set to YES, the CHM_INDEX_ENCODING +# is used to encode HtmlHelp index (hhk), content (hhc) and project file +# content. + +CHM_INDEX_ENCODING = + +# If the GENERATE_HTMLHELP tag is set to YES, the BINARY_TOC flag +# controls whether a binary table of contents is generated (YES) or a +# normal table of contents (NO) in the .chm file. + +BINARY_TOC = NO + +# The TOC_EXPAND flag can be set to YES to add extra items for group members +# to the contents of the HTML help documentation and to the tree view. + +TOC_EXPAND = NO + +# If the GENERATE_QHP tag is set to YES and both QHP_NAMESPACE and QHP_VIRTUAL_FOLDER +# are set, an additional index file will be generated that can be used as input for +# Qt's qhelpgenerator to generate a Qt Compressed Help (.qch) of the generated +# HTML documentation. + +GENERATE_QHP = NO + +# If the QHG_LOCATION tag is specified, the QCH_FILE tag can +# be used to specify the file name of the resulting .qch file. +# The path specified is relative to the HTML output folder. + +QCH_FILE = + +# The QHP_NAMESPACE tag specifies the namespace to use when generating +# Qt Help Project output. For more information please see +# http://doc.trolltech.com/qthelpproject.html#namespace + +QHP_NAMESPACE = org.doxygen.Project + +# The QHP_VIRTUAL_FOLDER tag specifies the namespace to use when generating +# Qt Help Project output. For more information please see +# http://doc.trolltech.com/qthelpproject.html#virtual-folders + +QHP_VIRTUAL_FOLDER = doc + +# If QHP_CUST_FILTER_NAME is set, it specifies the name of a custom filter to add. +# For more information please see +# http://doc.trolltech.com/qthelpproject.html#custom-filters + +QHP_CUST_FILTER_NAME = + +# The QHP_CUST_FILT_ATTRS tag specifies the list of the attributes of the custom filter to add.For more information please see +# Qt Help Project / Custom Filters. + +QHP_CUST_FILTER_ATTRS = + +# The QHP_SECT_FILTER_ATTRS tag specifies the list of the attributes this project's +# filter section matches. +# Qt Help Project / Filter Attributes. + +QHP_SECT_FILTER_ATTRS = + +# If the GENERATE_QHP tag is set to YES, the QHG_LOCATION tag can +# be used to specify the location of Qt's qhelpgenerator. +# If non-empty doxygen will try to run qhelpgenerator on the generated +# .qhp file. + +QHG_LOCATION = + +# If the GENERATE_ECLIPSEHELP tag is set to YES, additional index files +# will be generated, which together with the HTML files, form an Eclipse help +# plugin. To install this plugin and make it available under the help contents +# menu in Eclipse, the contents of the directory containing the HTML and XML +# files needs to be copied into the plugins directory of eclipse. The name of +# the directory within the plugins directory should be the same as +# the ECLIPSE_DOC_ID value. After copying Eclipse needs to be restarted before the help appears. + +GENERATE_ECLIPSEHELP = NO + +# A unique identifier for the eclipse help plugin. When installing the plugin +# the directory name containing the HTML and XML files should also have +# this name. + +ECLIPSE_DOC_ID = org.doxygen.Project + +# The DISABLE_INDEX tag can be used to turn on/off the condensed index at +# top of each HTML page. The value NO (the default) enables the index and +# the value YES disables it. + +DISABLE_INDEX = NO + +# This tag can be used to set the number of enum values (range [1..20]) +# that doxygen will group on one line in the generated HTML documentation. + +ENUM_VALUES_PER_LINE = 4 + +# The GENERATE_TREEVIEW tag is used to specify whether a tree-like index +# structure should be generated to display hierarchical information. +# If the tag value is set to YES, a side panel will be generated +# containing a tree-like index structure (just like the one that +# is generated for HTML Help). For this to work a browser that supports +# JavaScript, DHTML, CSS and frames is required (i.e. any modern browser). +# Windows users are probably better off using the HTML help feature. + +GENERATE_TREEVIEW = NO + +# By enabling USE_INLINE_TREES, doxygen will generate the Groups, Directories, +# and Class Hierarchy pages using a tree view instead of an ordered list. + +USE_INLINE_TREES = NO + +# If the treeview is enabled (see GENERATE_TREEVIEW) then this tag can be +# used to set the initial width (in pixels) of the frame in which the tree +# is shown. + +TREEVIEW_WIDTH = 250 + +# Use this tag to change the font size of Latex formulas included +# as images in the HTML documentation. The default is 10. Note that +# when you change the font size after a successful doxygen run you need +# to manually remove any form_*.png images from the HTML output directory +# to force them to be regenerated. + +FORMULA_FONTSIZE = 10 + +# When the SEARCHENGINE tag is enabled doxygen will generate a search box for the HTML output. The underlying search engine uses javascript +# and DHTML and should work on any modern browser. Note that when using HTML help (GENERATE_HTMLHELP), Qt help (GENERATE_QHP), or docsets (GENERATE_DOCSET) there is already a search function so this one should +# typically be disabled. For large projects the javascript based search engine +# can be slow, then enabling SERVER_BASED_SEARCH may provide a better solution. + +SEARCHENGINE = YES + +# When the SERVER_BASED_SEARCH tag is enabled the search engine will be implemented using a PHP enabled web server instead of at the web client using Javascript. Doxygen will generate the search PHP script and index +# file to put on the web server. The advantage of the server based approach is that it scales better to large projects and allows full text search. The disadvances is that it is more difficult to setup +# and does not have live searching capabilities. + +SERVER_BASED_SEARCH = NO + +#--------------------------------------------------------------------------- +# configuration options related to the LaTeX output +#--------------------------------------------------------------------------- + +# If the GENERATE_LATEX tag is set to YES (the default) Doxygen will +# generate Latex output. + +GENERATE_LATEX = YES + +# The LATEX_OUTPUT tag is used to specify where the LaTeX docs will be put. +# If a relative path is entered the value of OUTPUT_DIRECTORY will be +# put in front of it. If left blank `latex' will be used as the default path. + +LATEX_OUTPUT = latex + +# The LATEX_CMD_NAME tag can be used to specify the LaTeX command name to be +# invoked. If left blank `latex' will be used as the default command name. +# Note that when enabling USE_PDFLATEX this option is only used for +# generating bitmaps for formulas in the HTML output, but not in the +# Makefile that is written to the output directory. + +LATEX_CMD_NAME = latex + +# The MAKEINDEX_CMD_NAME tag can be used to specify the command name to +# generate index for LaTeX. If left blank `makeindex' will be used as the +# default command name. + +MAKEINDEX_CMD_NAME = makeindex + +# If the COMPACT_LATEX tag is set to YES Doxygen generates more compact +# LaTeX documents. This may be useful for small projects and may help to +# save some trees in general. + +COMPACT_LATEX = NO + +# The PAPER_TYPE tag can be used to set the paper type that is used +# by the printer. Possible values are: a4, a4wide, letter, legal and +# executive. If left blank a4wide will be used. + +PAPER_TYPE = a4wide + +# The EXTRA_PACKAGES tag can be to specify one or more names of LaTeX +# packages that should be included in the LaTeX output. + +EXTRA_PACKAGES = + +# The LATEX_HEADER tag can be used to specify a personal LaTeX header for +# the generated latex document. The header should contain everything until +# the first chapter. If it is left blank doxygen will generate a +# standard header. Notice: only use this tag if you know what you are doing! + +LATEX_HEADER = + +# If the PDF_HYPERLINKS tag is set to YES, the LaTeX that is generated +# is prepared for conversion to pdf (using ps2pdf). The pdf file will +# contain links (just like the HTML output) instead of page references +# This makes the output suitable for online browsing using a pdf viewer. + +PDF_HYPERLINKS = YES + +# If the USE_PDFLATEX tag is set to YES, pdflatex will be used instead of +# plain latex in the generated Makefile. Set this option to YES to get a +# higher quality PDF documentation. + +USE_PDFLATEX = YES + +# If the LATEX_BATCHMODE tag is set to YES, doxygen will add the \\batchmode. +# command to the generated LaTeX files. This will instruct LaTeX to keep +# running if errors occur, instead of asking the user for help. +# This option is also used when generating formulas in HTML. + +LATEX_BATCHMODE = NO + +# If LATEX_HIDE_INDICES is set to YES then doxygen will not +# include the index chapters (such as File Index, Compound Index, etc.) +# in the output. + +LATEX_HIDE_INDICES = NO + +# If LATEX_SOURCE_CODE is set to YES then doxygen will include source code with syntax highlighting in the LaTeX output. Note that which sources are shown also depends on other settings such as SOURCE_BROWSER. + +LATEX_SOURCE_CODE = NO + +#--------------------------------------------------------------------------- +# configuration options related to the RTF output +#--------------------------------------------------------------------------- + +# If the GENERATE_RTF tag is set to YES Doxygen will generate RTF output +# The RTF output is optimized for Word 97 and may not look very pretty with +# other RTF readers or editors. + +GENERATE_RTF = NO + +# The RTF_OUTPUT tag is used to specify where the RTF docs will be put. +# If a relative path is entered the value of OUTPUT_DIRECTORY will be +# put in front of it. If left blank `rtf' will be used as the default path. + +RTF_OUTPUT = rtf + +# If the COMPACT_RTF tag is set to YES Doxygen generates more compact +# RTF documents. This may be useful for small projects and may help to +# save some trees in general. + +COMPACT_RTF = NO + +# If the RTF_HYPERLINKS tag is set to YES, the RTF that is generated +# will contain hyperlink fields. The RTF file will +# contain links (just like the HTML output) instead of page references. +# This makes the output suitable for online browsing using WORD or other +# programs which support those fields. +# Note: wordpad (write) and others do not support links. + +RTF_HYPERLINKS = NO + +# Load stylesheet definitions from file. Syntax is similar to doxygen's +# config file, i.e. a series of assignments. You only have to provide +# replacements, missing definitions are set to their default value. + +RTF_STYLESHEET_FILE = + +# Set optional variables used in the generation of an rtf document. +# Syntax is similar to doxygen's config file. + +RTF_EXTENSIONS_FILE = + +#--------------------------------------------------------------------------- +# configuration options related to the man page output +#--------------------------------------------------------------------------- + +# If the GENERATE_MAN tag is set to YES (the default) Doxygen will +# generate man pages + +GENERATE_MAN = NO + +# The MAN_OUTPUT tag is used to specify where the man pages will be put. +# If a relative path is entered the value of OUTPUT_DIRECTORY will be +# put in front of it. If left blank `man' will be used as the default path. + +MAN_OUTPUT = man + +# The MAN_EXTENSION tag determines the extension that is added to +# the generated man pages (default is the subroutine's section .3) + +MAN_EXTENSION = .3 + +# If the MAN_LINKS tag is set to YES and Doxygen generates man output, +# then it will generate one additional man file for each entity +# documented in the real man page(s). These additional files +# only source the real man page, but without them the man command +# would be unable to find the correct page. The default is NO. + +MAN_LINKS = NO + +#--------------------------------------------------------------------------- +# configuration options related to the XML output +#--------------------------------------------------------------------------- + +# If the GENERATE_XML tag is set to YES Doxygen will +# generate an XML file that captures the structure of +# the code including all documentation. + +GENERATE_XML = NO + +# The XML_OUTPUT tag is used to specify where the XML pages will be put. +# If a relative path is entered the value of OUTPUT_DIRECTORY will be +# put in front of it. If left blank `xml' will be used as the default path. + +XML_OUTPUT = xml + +# The XML_SCHEMA tag can be used to specify an XML schema, +# which can be used by a validating XML parser to check the +# syntax of the XML files. + +XML_SCHEMA = + +# The XML_DTD tag can be used to specify an XML DTD, +# which can be used by a validating XML parser to check the +# syntax of the XML files. + +XML_DTD = + +# If the XML_PROGRAMLISTING tag is set to YES Doxygen will +# dump the program listings (including syntax highlighting +# and cross-referencing information) to the XML output. Note that +# enabling this will significantly increase the size of the XML output. + +XML_PROGRAMLISTING = YES + +#--------------------------------------------------------------------------- +# configuration options for the AutoGen Definitions output +#--------------------------------------------------------------------------- + +# If the GENERATE_AUTOGEN_DEF tag is set to YES Doxygen will +# generate an AutoGen Definitions (see autogen.sf.net) file +# that captures the structure of the code including all +# documentation. Note that this feature is still experimental +# and incomplete at the moment. + +GENERATE_AUTOGEN_DEF = NO + +#--------------------------------------------------------------------------- +# configuration options related to the Perl module output +#--------------------------------------------------------------------------- + +# If the GENERATE_PERLMOD tag is set to YES Doxygen will +# generate a Perl module file that captures the structure of +# the code including all documentation. Note that this +# feature is still experimental and incomplete at the +# moment. + +GENERATE_PERLMOD = NO + +# If the PERLMOD_LATEX tag is set to YES Doxygen will generate +# the necessary Makefile rules, Perl scripts and LaTeX code to be able +# to generate PDF and DVI output from the Perl module output. + +PERLMOD_LATEX = NO + +# If the PERLMOD_PRETTY tag is set to YES the Perl module output will be +# nicely formatted so it can be parsed by a human reader. +# This is useful +# if you want to understand what is going on. +# On the other hand, if this +# tag is set to NO the size of the Perl module output will be much smaller +# and Perl will parse it just the same. + +PERLMOD_PRETTY = YES + +# The names of the make variables in the generated doxyrules.make file +# are prefixed with the string contained in PERLMOD_MAKEVAR_PREFIX. +# This is useful so different doxyrules.make files included by the same +# Makefile don't overwrite each other's variables. + +PERLMOD_MAKEVAR_PREFIX = + +#--------------------------------------------------------------------------- +# Configuration options related to the preprocessor +#--------------------------------------------------------------------------- + +# If the ENABLE_PREPROCESSING tag is set to YES (the default) Doxygen will +# evaluate all C-preprocessor directives found in the sources and include +# files. + +ENABLE_PREPROCESSING = YES + +# If the MACRO_EXPANSION tag is set to YES Doxygen will expand all macro +# names in the source code. If set to NO (the default) only conditional +# compilation will be performed. Macro expansion can be done in a controlled +# way by setting EXPAND_ONLY_PREDEF to YES. + +MACRO_EXPANSION = NO + +# If the EXPAND_ONLY_PREDEF and MACRO_EXPANSION tags are both set to YES +# then the macro expansion is limited to the macros specified with the +# PREDEFINED and EXPAND_AS_DEFINED tags. + +EXPAND_ONLY_PREDEF = NO + +# If the SEARCH_INCLUDES tag is set to YES (the default) the includes files +# in the INCLUDE_PATH (see below) will be search if a #include is found. + +SEARCH_INCLUDES = YES + +# The INCLUDE_PATH tag can be used to specify one or more directories that +# contain include files that are not input files but should be processed by +# the preprocessor. + +INCLUDE_PATH = + +# You can use the INCLUDE_FILE_PATTERNS tag to specify one or more wildcard +# patterns (like *.h and *.hpp) to filter out the header-files in the +# directories. If left blank, the patterns specified with FILE_PATTERNS will +# be used. + +INCLUDE_FILE_PATTERNS = + +# The PREDEFINED tag can be used to specify one or more macro names that +# are defined before the preprocessor is started (similar to the -D option of +# gcc). The argument of the tag is a list of macros of the form: name +# or name=definition (no spaces). If the definition and the = are +# omitted =1 is assumed. To prevent a macro definition from being +# undefined via #undef or recursively expanded use the := operator +# instead of the = operator. + +PREDEFINED = + +# If the MACRO_EXPANSION and EXPAND_ONLY_PREDEF tags are set to YES then +# this tag can be used to specify a list of macro names that should be expanded. +# The macro definition that is found in the sources will be used. +# Use the PREDEFINED tag if you want to use a different macro definition. + +EXPAND_AS_DEFINED = + +# If the SKIP_FUNCTION_MACROS tag is set to YES (the default) then +# doxygen's preprocessor will remove all function-like macros that are alone +# on a line, have an all uppercase name, and do not end with a semicolon. Such +# function macros are typically used for boiler-plate code, and will confuse +# the parser if not removed. + +SKIP_FUNCTION_MACROS = YES + +#--------------------------------------------------------------------------- +# Configuration::additions related to external references +#--------------------------------------------------------------------------- + +# The TAGFILES option can be used to specify one or more tagfiles. +# Optionally an initial location of the external documentation +# can be added for each tagfile. The format of a tag file without +# this location is as follows: +# +# TAGFILES = file1 file2 ... +# Adding location for the tag files is done as follows: +# +# TAGFILES = file1=loc1 "file2 = loc2" ... +# where "loc1" and "loc2" can be relative or absolute paths or +# URLs. If a location is present for each tag, the installdox tool +# does not have to be run to correct the links. +# Note that each tag file must have a unique name +# (where the name does NOT include the path) +# If a tag file is not located in the directory in which doxygen +# is run, you must also specify the path to the tagfile here. + +TAGFILES = + +# When a file name is specified after GENERATE_TAGFILE, doxygen will create +# a tag file that is based on the input files it reads. + +GENERATE_TAGFILE = + +# If the ALLEXTERNALS tag is set to YES all external classes will be listed +# in the class index. If set to NO only the inherited external classes +# will be listed. + +ALLEXTERNALS = NO + +# If the EXTERNAL_GROUPS tag is set to YES all external groups will be listed +# in the modules index. If set to NO, only the current project's groups will +# be listed. + +EXTERNAL_GROUPS = YES + +# The PERL_PATH should be the absolute path and name of the perl script +# interpreter (i.e. the result of `which perl'). + +PERL_PATH = /usr/bin/perl + +#--------------------------------------------------------------------------- +# Configuration options related to the dot tool +#--------------------------------------------------------------------------- + +# If the CLASS_DIAGRAMS tag is set to YES (the default) Doxygen will +# generate a inheritance diagram (in HTML, RTF and LaTeX) for classes with base +# or super classes. Setting the tag to NO turns the diagrams off. Note that +# this option is superseded by the HAVE_DOT option below. This is only a +# fallback. It is recommended to install and use dot, since it yields more +# powerful graphs. + +CLASS_DIAGRAMS = YES + +# You can define message sequence charts within doxygen comments using the \msc +# command. Doxygen will then run the mscgen tool (see +# http://www.mcternan.me.uk/mscgen/) to produce the chart and insert it in the +# documentation. The MSCGEN_PATH tag allows you to specify the directory where +# the mscgen tool resides. If left empty the tool is assumed to be found in the +# default search path. + +MSCGEN_PATH = + +# If set to YES, the inheritance and collaboration graphs will hide +# inheritance and usage relations if the target is undocumented +# or is not a class. + +HIDE_UNDOC_RELATIONS = YES + +# If you set the HAVE_DOT tag to YES then doxygen will assume the dot tool is +# available from the path. This tool is part of Graphviz, a graph visualization +# toolkit from AT&T and Lucent Bell Labs. The other options in this section +# have no effect if this option is set to NO (the default) + +HAVE_DOT = NO + +# By default doxygen will write a font called FreeSans.ttf to the output +# directory and reference it in all dot files that doxygen generates. This +# font does not include all possible unicode characters however, so when you need +# these (or just want a differently looking font) you can specify the font name +# using DOT_FONTNAME. You need need to make sure dot is able to find the font, +# which can be done by putting it in a standard location or by setting the +# DOTFONTPATH environment variable or by setting DOT_FONTPATH to the directory +# containing the font. + +DOT_FONTNAME = FreeSans + +# The DOT_FONTSIZE tag can be used to set the size of the font of dot graphs. +# The default size is 10pt. + +DOT_FONTSIZE = 10 + +# By default doxygen will tell dot to use the output directory to look for the +# FreeSans.ttf font (which doxygen will put there itself). If you specify a +# different font using DOT_FONTNAME you can set the path where dot +# can find it using this tag. + +DOT_FONTPATH = + +# If the CLASS_GRAPH and HAVE_DOT tags are set to YES then doxygen +# will generate a graph for each documented class showing the direct and +# indirect inheritance relations. Setting this tag to YES will force the +# the CLASS_DIAGRAMS tag to NO. + +CLASS_GRAPH = YES + +# If the COLLABORATION_GRAPH and HAVE_DOT tags are set to YES then doxygen +# will generate a graph for each documented class showing the direct and +# indirect implementation dependencies (inheritance, containment, and +# class references variables) of the class with other documented classes. + +COLLABORATION_GRAPH = YES + +# If the GROUP_GRAPHS and HAVE_DOT tags are set to YES then doxygen +# will generate a graph for groups, showing the direct groups dependencies + +GROUP_GRAPHS = YES + +# If the UML_LOOK tag is set to YES doxygen will generate inheritance and +# collaboration diagrams in a style similar to the OMG's Unified Modeling +# Language. + +UML_LOOK = NO + +# If set to YES, the inheritance and collaboration graphs will show the +# relations between templates and their instances. + +TEMPLATE_RELATIONS = NO + +# If the ENABLE_PREPROCESSING, SEARCH_INCLUDES, INCLUDE_GRAPH, and HAVE_DOT +# tags are set to YES then doxygen will generate a graph for each documented +# file showing the direct and indirect include dependencies of the file with +# other documented files. + +INCLUDE_GRAPH = YES + +# If the ENABLE_PREPROCESSING, SEARCH_INCLUDES, INCLUDED_BY_GRAPH, and +# HAVE_DOT tags are set to YES then doxygen will generate a graph for each +# documented header file showing the documented files that directly or +# indirectly include this file. + +INCLUDED_BY_GRAPH = YES + +# If the CALL_GRAPH and HAVE_DOT options are set to YES then +# doxygen will generate a call dependency graph for every global function +# or class method. Note that enabling this option will significantly increase +# the time of a run. So in most cases it will be better to enable call graphs +# for selected functions only using the \callgraph command. + +CALL_GRAPH = NO + +# If the CALLER_GRAPH and HAVE_DOT tags are set to YES then +# doxygen will generate a caller dependency graph for every global function +# or class method. Note that enabling this option will significantly increase +# the time of a run. So in most cases it will be better to enable caller +# graphs for selected functions only using the \callergraph command. + +CALLER_GRAPH = NO + +# If the GRAPHICAL_HIERARCHY and HAVE_DOT tags are set to YES then doxygen +# will graphical hierarchy of all classes instead of a textual one. + +GRAPHICAL_HIERARCHY = YES + +# If the DIRECTORY_GRAPH, SHOW_DIRECTORIES and HAVE_DOT tags are set to YES +# then doxygen will show the dependencies a directory has on other directories +# in a graphical way. The dependency relations are determined by the #include +# relations between the files in the directories. + +DIRECTORY_GRAPH = YES + +# The DOT_IMAGE_FORMAT tag can be used to set the image format of the images +# generated by dot. Possible values are png, jpg, or gif +# If left blank png will be used. + +DOT_IMAGE_FORMAT = png + +# The tag DOT_PATH can be used to specify the path where the dot tool can be +# found. If left blank, it is assumed the dot tool can be found in the path. + +DOT_PATH = + +# The DOTFILE_DIRS tag can be used to specify one or more directories that +# contain dot files that are included in the documentation (see the +# \dotfile command). + +DOTFILE_DIRS = + +# The DOT_GRAPH_MAX_NODES tag can be used to set the maximum number of +# nodes that will be shown in the graph. If the number of nodes in a graph +# becomes larger than this value, doxygen will truncate the graph, which is +# visualized by representing a node as a red box. Note that doxygen if the +# number of direct children of the root node in a graph is already larger than +# DOT_GRAPH_MAX_NODES then the graph will not be shown at all. Also note +# that the size of a graph can be further restricted by MAX_DOT_GRAPH_DEPTH. + +DOT_GRAPH_MAX_NODES = 50 + +# The MAX_DOT_GRAPH_DEPTH tag can be used to set the maximum depth of the +# graphs generated by dot. A depth value of 3 means that only nodes reachable +# from the root by following a path via at most 3 edges will be shown. Nodes +# that lay further from the root node will be omitted. Note that setting this +# option to 1 or 2 may greatly reduce the computation time needed for large +# code bases. Also note that the size of a graph can be further restricted by +# DOT_GRAPH_MAX_NODES. Using a depth of 0 means no depth restriction. + +MAX_DOT_GRAPH_DEPTH = 0 + +# Set the DOT_TRANSPARENT tag to YES to generate images with a transparent +# background. This is disabled by default, because dot on Windows does not +# seem to support this out of the box. Warning: Depending on the platform used, +# enabling this option may lead to badly anti-aliased labels on the edges of +# a graph (i.e. they become hard to read). + +DOT_TRANSPARENT = NO + +# Set the DOT_MULTI_TARGETS tag to YES allow dot to generate multiple output +# files in one run (i.e. multiple -o and -T options on the command line). This +# makes dot run faster, but since only newer versions of dot (>1.8.10) +# support this, this feature is disabled by default. + +DOT_MULTI_TARGETS = NO + +# If the GENERATE_LEGEND tag is set to YES (the default) Doxygen will +# generate a legend page explaining the meaning of the various boxes and +# arrows in the dot generated graphs. + +GENERATE_LEGEND = YES + +# If the DOT_CLEANUP tag is set to YES (the default) Doxygen will +# remove the intermediate dot files that are used to generate +# the various graphs. + +DOT_CLEANUP = YES diff --git a/cpp/Makefile.am b/cpp/Makefile.am index 08eb7a5c..ecec1b56 100644 --- a/cpp/Makefile.am +++ b/cpp/Makefile.am @@ -1,93 +1,20 @@ +SUBDIRS = src test -lib_LTLIBRARIES = libmsgpack.la - -libmsgpack_la_SOURCES = \ - unpack.c \ - objectc.c \ - vrefbuffer.c \ - zone.c \ - object.cpp - -# -version-info CURRENT:REVISION:AGE -libmsgpack_la_LDFLAGS = -version-info 3:0:0 - - -# backward compatibility -lib_LTLIBRARIES += libmsgpackc.la - -libmsgpackc_la_SOURCES = \ - unpack.c \ - objectc.c \ - vrefbuffer.c \ - zone.c - -libmsgpackc_la_LDFLAGS = -version-info 2:0:0 - -# work around for duplicated file name -kumo_manager_CFLAGS = $(AM_CFLAGS) -kumo_manager_CXXFLAGS = $(AM_CXXFLAGS) - - -nobase_include_HEADERS = \ - msgpack/pack_define.h \ - msgpack/pack_template.h \ - msgpack/unpack_define.h \ - msgpack/unpack_template.h \ - msgpack/sysdep.h \ - msgpack.h \ - msgpack/sbuffer.h \ - msgpack/vrefbuffer.h \ - msgpack/zbuffer.h \ - msgpack/pack.h \ - msgpack/unpack.h \ - msgpack/object.h \ - msgpack/zone.h \ - msgpack.hpp \ - msgpack/sbuffer.hpp \ - msgpack/vrefbuffer.hpp \ - msgpack/zbuffer.hpp \ - msgpack/pack.hpp \ - msgpack/unpack.hpp \ - msgpack/object.hpp \ - msgpack/zone.hpp \ - msgpack/type.hpp \ - msgpack/type/bool.hpp \ - msgpack/type/float.hpp \ - msgpack/type/int.hpp \ - msgpack/type/list.hpp \ - msgpack/type/deque.hpp \ - msgpack/type/map.hpp \ - msgpack/type/nil.hpp \ - msgpack/type/pair.hpp \ - msgpack/type/raw.hpp \ - msgpack/type/set.hpp \ - msgpack/type/string.hpp \ - msgpack/type/vector.hpp \ - msgpack/type/tuple.hpp \ - msgpack/type/define.hpp \ - msgpack/type/tr1/unordered_map.hpp \ - msgpack/type/tr1/unordered_set.hpp - - -EXTRA_DIST = \ +DOC_FILES = \ README.md \ LICENSE \ NOTICE \ msgpack_vc8.vcproj \ msgpack_vc8.sln \ - msgpack_vc8.postbuild.bat + msgpack_vc2008.vcproj \ + msgpack_vc2008.sln \ + msgpack_vc.postbuild.bat -SUBDIRS = test +EXTRA_DIST = \ + $(DOC_FILES) -check_PROGRAMS = \ - msgpackc_test \ - msgpack_test - -msgpackc_test_SOURCES = msgpackc_test.cpp -msgpackc_test_LDADD = libmsgpack.la -lgtest_main - -msgpack_test_SOURCES = msgpack_test.cpp -msgpack_test_LDADD = libmsgpack.la -lgtest_main - -TESTS = $(check_PROGRAMS) +doxygen: + ./preprocess clean + cd src && $(MAKE) doxygen + ./preprocess diff --git a/cpp/README.md b/cpp/README.md index 454ce1af..eac77935 100644 --- a/cpp/README.md +++ b/cpp/README.md @@ -13,9 +13,10 @@ On UNIX-like platform, run ./configure && make && sudo make install: $ make $ sudo make install -On Windows, open msgpack_vc8.vcproj file and build it using batch build. DLLs are built on lib folder, and the headers are built on include folder. +On Windows, open msgpack_vc8.vcproj or msgpack_vc2008 file and build it using batch build. DLLs are built on lib folder, +and the headers are built on include folder. -To use the library in your program, include msgpack.hpp header and link msgpack and msgpackc library. +To use the library in your program, include msgpack.hpp header and link "msgpack" library. ## Example @@ -34,15 +35,9 @@ To use the library in your program, include msgpack.hpp header and link msgpack msgpack::pack(&buffer, target); // Deserialize the serialized data. - msgpack::zone mempool; // this manages the life of deserialized object - msgpack::object obj; - msgpack::unpack_return ret = - msgpack::unpack(buffer.data, buffer.size, NULL, &mempool, &obj); - - if(ret != msgapck::UNPACK_SUCCESS) { - // error check - exit(1); - } + msgpack::unpacked msg; // includes memory pool and deserialized object + msgpack::unpack(&msg, sbuf.data(), sbuf.size()); + msgpack::object obj = msg.get(); // Print the deserialized object to stdout. std::cout << obj << std::endl; // ["Hello," "World!"] @@ -55,24 +50,24 @@ To use the library in your program, include msgpack.hpp header and link msgpack obj.as(); // type is mismatched, msgpack::type_error is thrown } -API document and other example codes are available at the [wiki.](http://msgpack.sourceforge.net/start) +API documents and other example codes are available at the [wiki.](http://redmine.msgpack.org/projects/msgpack/wiki) ## License -Copyright (C) 2008-2010 FURUHASHI Sadayuki - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. + Copyright (C) 2008-2010 FURUHASHI Sadayuki + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. See also NOTICE file. diff --git a/cpp/configure.in b/cpp/configure.in index 61fde4f7..2dd92d18 100644 --- a/cpp/configure.in +++ b/cpp/configure.in @@ -1,6 +1,6 @@ -AC_INIT(object.cpp) +AC_INIT(src/object.cpp) AC_CONFIG_AUX_DIR(ac) -AM_INIT_AUTOMAKE(msgpack, 0.5.0) +AM_INIT_AUTOMAKE(msgpack, 0.5.4) AC_CONFIG_HEADER(config.h) AC_SUBST(CFLAGS) @@ -9,17 +9,39 @@ CFLAGS="-O4 -Wall $CFLAGS" AC_SUBST(CXXFLAGS) CXXFLAGS="-O4 -Wall $CXXFLAGS" + AC_PROG_CC -AC_PROG_CXX + + +AC_MSG_CHECKING([if C++ API is enabled]) +AC_ARG_ENABLE(cxx, + AS_HELP_STRING([--disable-cxx], + [don't build C++ API]) ) +AC_MSG_RESULT([$enable_cxx]) +if test "$enable_cxx" != "no"; then + AC_PROG_CXX + AM_PROG_CC_C_O +fi +AM_CONDITIONAL(ENABLE_CXX, test "$enable_cxx" != "no") + AC_PROG_LIBTOOL AM_PROG_AS -AM_PROG_CC_C_O -AC_LANG_PUSH([C++]) -AC_CHECK_HEADERS(tr1/unordered_map) -AC_CHECK_HEADERS(tr1/unordered_set) -AC_LANG_POP([C++]) + +AC_MSG_CHECKING([if debug option is enabled]) +AC_ARG_ENABLE(debug, + AS_HELP_STRING([--disable-debug], + [disable assert macros and omit -g option]) ) +AC_MSG_RESULT([$enable_debug]) +if test "$enable_debug" != "no"; then + CXXFLAGS="$CXXFLAGS -g" + CFLAGS="$CFLAGS -g" +else + CXXFLAGS="$CXXFLAGS -DNDEBUG" + CFLAGS="$CFLAGS -DNDEBUG" +fi + AC_CACHE_CHECK([for __sync_* atomic operations], msgpack_cv_atomic_ops, [ AC_TRY_LINK([ @@ -39,5 +61,15 @@ add CFLAGS="--march=i686" and CXXFLAGS="-march=i686" options to ./configure as f ]) fi -AC_OUTPUT([Makefile test/Makefile]) + +major=`echo $VERSION | sed 's/\([[0-9]]*\)\.\([[0-9]]*\).*/\1/'` +minor=`echo $VERSION | sed 's/\([[0-9]]*\)\.\([[0-9]]*\).*/\2/'` +AC_SUBST(VERSION_MAJOR, $major) +AC_SUBST(VERSION_MINOR, $minor) + + +AC_OUTPUT([Makefile + src/Makefile + src/msgpack/version.h + test/Makefile]) diff --git a/cpp/msgpack/type.hpp b/cpp/msgpack/type.hpp deleted file mode 100644 index fafa6740..00000000 --- a/cpp/msgpack/type.hpp +++ /dev/null @@ -1,15 +0,0 @@ -#include "msgpack/type/bool.hpp" -#include "msgpack/type/float.hpp" -#include "msgpack/type/int.hpp" -#include "msgpack/type/list.hpp" -#include "msgpack/type/deque.hpp" -#include "msgpack/type/map.hpp" -#include "msgpack/type/nil.hpp" -#include "msgpack/type/pair.hpp" -#include "msgpack/type/raw.hpp" -#include "msgpack/type/set.hpp" -#include "msgpack/type/string.hpp" -#include "msgpack/type/vector.hpp" -#include "msgpack/type/tuple.hpp" -#include "msgpack/type/define.hpp" - diff --git a/cpp/msgpack/unpack.h b/cpp/msgpack/unpack.h deleted file mode 100644 index e17d0d85..00000000 --- a/cpp/msgpack/unpack.h +++ /dev/null @@ -1,123 +0,0 @@ -/* - * MessagePack for C unpacking routine - * - * Copyright (C) 2008-2009 FURUHASHI Sadayuki - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#ifndef MSGPACK_UNPACKER_H__ -#define MSGPACK_UNPACKER_H__ - -#include "msgpack/zone.h" -#include "msgpack/object.h" - -#ifdef __cplusplus -extern "C" { -#endif - - -typedef struct msgpack_unpacker { - char* buffer; - size_t used; - size_t free; - size_t off; - size_t parsed; - msgpack_zone* z; - size_t initial_buffer_size; - void* ctx; -} msgpack_unpacker; - - -bool msgpack_unpacker_init(msgpack_unpacker* mpac, size_t initial_buffer_size); -void msgpack_unpacker_destroy(msgpack_unpacker* mpac); - -msgpack_unpacker* msgpack_unpacker_new(size_t initial_buffer_size); -void msgpack_unpacker_free(msgpack_unpacker* mpac); - -static inline bool msgpack_unpacker_reserve_buffer(msgpack_unpacker* mpac, size_t size); -static inline char* msgpack_unpacker_buffer(msgpack_unpacker* mpac); -static inline size_t msgpack_unpacker_buffer_capacity(const msgpack_unpacker* mpac); -static inline void msgpack_unpacker_buffer_consumed(msgpack_unpacker* mpac, size_t size); - - -int msgpack_unpacker_execute(msgpack_unpacker* mpac); - -msgpack_object msgpack_unpacker_data(msgpack_unpacker* mpac); - -msgpack_zone* msgpack_unpacker_release_zone(msgpack_unpacker* mpac); - -void msgpack_unpacker_reset_zone(msgpack_unpacker* mpac); - -void msgpack_unpacker_reset(msgpack_unpacker* mpac); - -static inline size_t msgpack_unpacker_message_size(const msgpack_unpacker* mpac); - - - -typedef enum { - MSGPACK_UNPACK_SUCCESS = 2, - MSGPACK_UNPACK_EXTRA_BYTES = 1, - MSGPACK_UNPACK_CONTINUE = 0, - MSGPACK_UNPACK_PARSE_ERROR = -1, -} msgpack_unpack_return; - -msgpack_unpack_return -msgpack_unpack(const char* data, size_t len, size_t* off, - msgpack_zone* z, msgpack_object* result); - - -static inline size_t msgpack_unpacker_parsed_size(const msgpack_unpacker* mpac); - -bool msgpack_unpacker_flush_zone(msgpack_unpacker* mpac); - -bool msgpack_unpacker_expand_buffer(msgpack_unpacker* mpac, size_t size); - -bool msgpack_unpacker_reserve_buffer(msgpack_unpacker* mpac, size_t size) -{ - if(mpac->free >= size) { return true; } - return msgpack_unpacker_expand_buffer(mpac, size); -} - -char* msgpack_unpacker_buffer(msgpack_unpacker* mpac) -{ - return mpac->buffer + mpac->used; -} - -size_t msgpack_unpacker_buffer_capacity(const msgpack_unpacker* mpac) -{ - return mpac->free; -} - -void msgpack_unpacker_buffer_consumed(msgpack_unpacker* mpac, size_t size) -{ - mpac->used += size; - mpac->free -= size; -} - -size_t msgpack_unpacker_message_size(const msgpack_unpacker* mpac) -{ - return mpac->parsed - mpac->off + mpac->used; -} - -size_t msgpack_unpacker_parsed_size(const msgpack_unpacker* mpac) -{ - return mpac->parsed; -} - - -#ifdef __cplusplus -} -#endif - -#endif /* msgpack/unpack.h */ - diff --git a/cpp/msgpack_vc.postbuild.bat b/cpp/msgpack_vc.postbuild.bat new file mode 100644 index 00000000..33ff232b --- /dev/null +++ b/cpp/msgpack_vc.postbuild.bat @@ -0,0 +1,45 @@ +IF NOT EXIST include MKDIR include +IF NOT EXIST include\msgpack MKDIR include\msgpack +IF NOT EXIST include\msgpack\type MKDIR include\msgpack\type +IF NOT EXIST include\msgpack\type\tr1 MKDIR include\msgpack\type\tr1 +copy src\msgpack\pack_define.h include\msgpack\ +copy src\msgpack\pack_template.h include\msgpack\ +copy src\msgpack\unpack_define.h include\msgpack\ +copy src\msgpack\unpack_template.h include\msgpack\ +copy src\msgpack\sysdep.h include\msgpack\ +copy src\msgpack.h include\ +copy src\msgpack\sbuffer.h include\msgpack\ +copy src\msgpack\version.h include\msgpack\ +copy src\msgpack\vrefbuffer.h include\msgpack\ +copy src\msgpack\zbuffer.h include\msgpack\ +copy src\msgpack\pack.h include\msgpack\ +copy src\msgpack\unpack.h include\msgpack\ +copy src\msgpack\object.h include\msgpack\ +copy src\msgpack\zone.h include\msgpack\ +copy src\msgpack.hpp include\ +copy src\msgpack\sbuffer.hpp include\msgpack\ +copy src\msgpack\vrefbuffer.hpp include\msgpack\ +copy src\msgpack\zbuffer.hpp include\msgpack\ +copy src\msgpack\pack.hpp include\msgpack\ +copy src\msgpack\unpack.hpp include\msgpack\ +copy src\msgpack\object.hpp include\msgpack\ +copy src\msgpack\zone.hpp include\msgpack\ +copy src\msgpack\type.hpp include\msgpack\type\ +copy src\msgpack\type\bool.hpp include\msgpack\type\ +copy src\msgpack\type\deque.hpp include\msgpack\type\ +copy src\msgpack\type\fixint.hpp include\msgpack\type\ +copy src\msgpack\type\float.hpp include\msgpack\type\ +copy src\msgpack\type\int.hpp include\msgpack\type\ +copy src\msgpack\type\list.hpp include\msgpack\type\ +copy src\msgpack\type\map.hpp include\msgpack\type\ +copy src\msgpack\type\nil.hpp include\msgpack\type\ +copy src\msgpack\type\pair.hpp include\msgpack\type\ +copy src\msgpack\type\raw.hpp include\msgpack\type\ +copy src\msgpack\type\set.hpp include\msgpack\type\ +copy src\msgpack\type\string.hpp include\msgpack\type\ +copy src\msgpack\type\vector.hpp include\msgpack\type\ +copy src\msgpack\type\tuple.hpp include\msgpack\type\ +copy src\msgpack\type\define.hpp include\msgpack\type\ +copy src\msgpack\type\tr1\unordered_map.hpp include\msgpack\type\ +copy src\msgpack\type\tr1\unordered_set.hpp include\msgpack\type\ + diff --git a/cpp/msgpack_vc8.postbuild.bat b/cpp/msgpack_vc8.postbuild.bat deleted file mode 100644 index bae13f37..00000000 --- a/cpp/msgpack_vc8.postbuild.bat +++ /dev/null @@ -1,43 +0,0 @@ -IF NOT EXIST include MKDIR include -IF NOT EXIST include\msgpack MKDIR include\msgpack -IF NOT EXIST include\msgpack\type MKDIR include\msgpack\type -IF NOT EXIST include\msgpack\type\tr1 MKDIR include\msgpack\type\tr1 -copy msgpack\pack_define.h include\msgpack\ -copy msgpack\pack_template.h include\msgpack\ -copy msgpack\unpack_define.h include\msgpack\ -copy msgpack\unpack_template.h include\msgpack\ -copy msgpack\sysdep.h include\msgpack\ -copy msgpack.h include\ -copy msgpack\sbuffer.h include\msgpack\ -copy msgpack\vrefbuffer.h include\msgpack\ -copy msgpack\zbuffer.h include\msgpack\ -copy msgpack\pack.h include\msgpack\ -copy msgpack\unpack.h include\msgpack\ -copy msgpack\object.h include\msgpack\ -copy msgpack\zone.h include\msgpack\ -copy msgpack.hpp include\ -copy msgpack\sbuffer.hpp include\msgpack\ -copy msgpack\vrefbuffer.hpp include\msgpack\ -copy msgpack\zbuffer.hpp include\msgpack\ -copy msgpack\pack.hpp include\msgpack\ -copy msgpack\unpack.hpp include\msgpack\ -copy msgpack\object.hpp include\msgpack\ -copy msgpack\zone.hpp include\msgpack\ -copy msgpack\type.hpp include\msgpack\type\ -copy msgpack\type\bool.hpp include\msgpack\type\ -copy msgpack\type\float.hpp include\msgpack\type\ -copy msgpack\type\int.hpp include\msgpack\type\ -copy msgpack\type\list.hpp include\msgpack\type\ -copy msgpack\type\deque.hpp include\msgpack\type\ -copy msgpack\type\map.hpp include\msgpack\type\ -copy msgpack\type\nil.hpp include\msgpack\type\ -copy msgpack\type\pair.hpp include\msgpack\type\ -copy msgpack\type\raw.hpp include\msgpack\type\ -copy msgpack\type\set.hpp include\msgpack\type\ -copy msgpack\type\string.hpp include\msgpack\type\ -copy msgpack\type\vector.hpp include\msgpack\type\ -copy msgpack\type\tuple.hpp include\msgpack\type\ -copy msgpack\type\define.hpp include\msgpack\type\ -copy msgpack\type\tr1\unordered_map.hpp include\msgpack\type\ -copy msgpack\type\tr1\unordered_set.hpp include\msgpack\type\ - diff --git a/cpp/msgpack_vc8.vcproj b/cpp/msgpack_vc8.vcproj index 58047909..72d47b6b 100644 --- a/cpp/msgpack_vc8.vcproj +++ b/cpp/msgpack_vc8.vcproj @@ -28,7 +28,7 @@ + + + + + + + + @@ -247,23 +267,23 @@ UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}" > diff --git a/cpp/preprocess b/cpp/preprocess index 80a8357a..e51c61cf 100755 --- a/cpp/preprocess +++ b/cpp/preprocess @@ -6,17 +6,29 @@ preprocess() { echo "" echo "** preprocess failed **" echo "" + exit 1 else mv $1.tmp $1 fi } -preprocess msgpack/type/tuple.hpp -preprocess msgpack/type/define.hpp -preprocess msgpack/zone.hpp -cp -f ../msgpack/sysdep.h msgpack/ -cp -f ../msgpack/pack_define.h msgpack/ -cp -f ../msgpack/pack_template.h msgpack/ -cp -f ../msgpack/unpack_define.h msgpack/ -cp -f ../msgpack/unpack_template.h msgpack/ +if [ "$1" == "clean" ];then + rm -f src/msgpack/type/tuple.hpp + rm -f src/msgpack/type/define.hpp + rm -f src/msgpack/zone.hpp +else + preprocess src/msgpack/type/tuple.hpp + preprocess src/msgpack/type/define.hpp + preprocess src/msgpack/zone.hpp +fi +cp -f ../msgpack/sysdep.h src/msgpack/ +cp -f ../msgpack/pack_define.h src/msgpack/ +cp -f ../msgpack/pack_template.h src/msgpack/ +cp -f ../msgpack/unpack_define.h src/msgpack/ +cp -f ../msgpack/unpack_template.h src/msgpack/ +cp -f ../test/cases.mpac test/ +cp -f ../test/cases_compact.mpac test/ + +sed -e 's/8\.00/9.00/' < msgpack_vc8.vcproj > msgpack_vc2008.vcproj +sed -e 's/9\.00/10.00/' -e 's/msgpack_vc8/msgpack_vc2008/' < msgpack_vc8.sln > msgpack_vc2008.sln diff --git a/cpp/src/Makefile.am b/cpp/src/Makefile.am new file mode 100644 index 00000000..0979d235 --- /dev/null +++ b/cpp/src/Makefile.am @@ -0,0 +1,101 @@ + +lib_LTLIBRARIES = libmsgpack.la + +libmsgpack_la_SOURCES = \ + unpack.c \ + objectc.c \ + version.c \ + vrefbuffer.c \ + zone.c + +if ENABLE_CXX +libmsgpack_la_SOURCES += \ + object.cpp +endif + +# -version-info CURRENT:REVISION:AGE +libmsgpack_la_LDFLAGS = -version-info 3:0:0 + + +# backward compatibility +lib_LTLIBRARIES += libmsgpackc.la + +libmsgpackc_la_SOURCES = \ + unpack.c \ + objectc.c \ + version.c \ + vrefbuffer.c \ + zone.c + +libmsgpackc_la_LDFLAGS = -version-info 2:0:0 + + +nobase_include_HEADERS = \ + msgpack/pack_define.h \ + msgpack/pack_template.h \ + msgpack/unpack_define.h \ + msgpack/unpack_template.h \ + msgpack/sysdep.h \ + msgpack.h \ + msgpack/sbuffer.h \ + msgpack/version.h \ + msgpack/vrefbuffer.h \ + msgpack/zbuffer.h \ + msgpack/pack.h \ + msgpack/unpack.h \ + msgpack/object.h \ + msgpack/zone.h + +if ENABLE_CXX +nobase_include_HEADERS += \ + msgpack.hpp \ + msgpack/sbuffer.hpp \ + msgpack/vrefbuffer.hpp \ + msgpack/zbuffer.hpp \ + msgpack/pack.hpp \ + msgpack/unpack.hpp \ + msgpack/object.hpp \ + msgpack/zone.hpp \ + msgpack/type.hpp \ + msgpack/type/bool.hpp \ + msgpack/type/deque.hpp \ + msgpack/type/float.hpp \ + msgpack/type/fixint.hpp \ + msgpack/type/int.hpp \ + msgpack/type/list.hpp \ + msgpack/type/map.hpp \ + msgpack/type/nil.hpp \ + msgpack/type/pair.hpp \ + msgpack/type/raw.hpp \ + msgpack/type/set.hpp \ + msgpack/type/string.hpp \ + msgpack/type/vector.hpp \ + msgpack/type/tuple.hpp \ + msgpack/type/define.hpp \ + msgpack/type/tr1/unordered_map.hpp \ + msgpack/type/tr1/unordered_set.hpp +endif + +EXTRA_DIST = \ + msgpack/version.h.in \ + msgpack/zone.hpp.erb \ + msgpack/type/define.hpp.erb \ + msgpack/type/tuple.hpp.erb + + +doxygen_c: + cat ../Doxyfile > Doxyfile_c + echo "FILE_PATTERNS = *.h" >> Doxyfile_c + echo "OUTPUT_DIRECTORY = doc_c" >> Doxyfile_c + echo "PROJECT_NAME = \"MessagePack for C\"" >> Doxyfile_c + doxygen Doxyfile_c + +doxygen_cpp: + cat ../Doxyfile > Doxyfile_cpp + echo "FILE_PATTERNS = *.hpp" >> Doxyfile_cpp + echo "OUTPUT_DIRECTORY = doc_cpp" >> Doxyfile_cpp + echo "PROJECT_NAME = \"MessagePack for C++\"" >> Doxyfile_cpp + doxygen Doxyfile_cpp + +doxygen: doxygen_c doxygen_cpp + diff --git a/cpp/msgpack.h b/cpp/src/msgpack.h similarity index 90% rename from cpp/msgpack.h rename to cpp/src/msgpack.h index 21729f44..08f27688 100644 --- a/cpp/msgpack.h +++ b/cpp/src/msgpack.h @@ -15,9 +15,16 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +/** + * @defgroup msgpack MessagePack C + * @{ + * @} + */ #include "msgpack/object.h" #include "msgpack/zone.h" #include "msgpack/pack.h" #include "msgpack/unpack.h" #include "msgpack/sbuffer.h" #include "msgpack/vrefbuffer.h" +#include "msgpack/version.h" + diff --git a/cpp/msgpack.hpp b/cpp/src/msgpack.hpp similarity index 100% rename from cpp/msgpack.hpp rename to cpp/src/msgpack.hpp diff --git a/cpp/msgpack/object.h b/cpp/src/msgpack/object.h similarity index 94% rename from cpp/msgpack/object.h rename to cpp/src/msgpack/object.h index 71a27bb8..baeeb9b2 100644 --- a/cpp/msgpack/object.h +++ b/cpp/src/msgpack/object.h @@ -18,7 +18,7 @@ #ifndef MSGPACK_OBJECT_H__ #define MSGPACK_OBJECT_H__ -#include "msgpack/zone.h" +#include "zone.h" #include #ifdef __cplusplus @@ -26,6 +26,12 @@ extern "C" { #endif +/** + * @defgroup msgpack_object Dynamically typed object + * @ingroup msgpack + * @{ + */ + typedef enum { MSGPACK_OBJECT_NIL = 0x00, MSGPACK_OBJECT_BOOLEAN = 0x01, @@ -81,6 +87,8 @@ void msgpack_object_print(FILE* out, msgpack_object o); bool msgpack_object_equal(const msgpack_object x, const msgpack_object y); +/** @} */ + #ifdef __cplusplus } diff --git a/cpp/msgpack/object.hpp b/cpp/src/msgpack/object.hpp similarity index 98% rename from cpp/msgpack/object.hpp rename to cpp/src/msgpack/object.hpp index f80a390d..96c026ef 100644 --- a/cpp/msgpack/object.hpp +++ b/cpp/src/msgpack/object.hpp @@ -18,9 +18,9 @@ #ifndef MSGPACK_OBJECT_HPP__ #define MSGPACK_OBJECT_HPP__ -#include "msgpack/object.h" -#include "msgpack/pack.hpp" -#include "msgpack/zone.hpp" +#include "object.h" +#include "pack.hpp" +#include "zone.hpp" #include #include #include diff --git a/cpp/msgpack/pack.h b/cpp/src/msgpack/pack.h similarity index 79% rename from cpp/msgpack/pack.h rename to cpp/src/msgpack/pack.h index 1525e0f2..c1564963 100644 --- a/cpp/msgpack/pack.h +++ b/cpp/src/msgpack/pack.h @@ -18,8 +18,8 @@ #ifndef MSGPACK_PACK_H__ #define MSGPACK_PACK_H__ -#include "msgpack/pack_define.h" -#include "msgpack/object.h" +#include "pack_define.h" +#include "object.h" #include #ifdef __cplusplus @@ -27,6 +27,19 @@ extern "C" { #endif +/** + * @defgroup msgpack_buffer Buffers + * @ingroup msgpack + * @{ + * @} + */ + +/** + * @defgroup msgpack_pack Serializer + * @ingroup msgpack + * @{ + */ + typedef int (*msgpack_packer_write)(void* data, const char* buf, unsigned int len); typedef struct msgpack_packer { @@ -57,6 +70,15 @@ static int msgpack_pack_int16(msgpack_packer* pk, int16_t d); static int msgpack_pack_int32(msgpack_packer* pk, int32_t d); static int msgpack_pack_int64(msgpack_packer* pk, int64_t d); +static int msgpack_pack_fix_uint8(msgpack_packer* pk, uint8_t d); +static int msgpack_pack_fix_uint16(msgpack_packer* pk, uint16_t d); +static int msgpack_pack_fix_uint32(msgpack_packer* pk, uint32_t d); +static int msgpack_pack_fix_uint64(msgpack_packer* pk, uint64_t d); +static int msgpack_pack_fix_int8(msgpack_packer* pk, int8_t d); +static int msgpack_pack_fix_int16(msgpack_packer* pk, int16_t d); +static int msgpack_pack_fix_int32(msgpack_packer* pk, int32_t d); +static int msgpack_pack_fix_int64(msgpack_packer* pk, int64_t d); + static int msgpack_pack_float(msgpack_packer* pk, float d); static int msgpack_pack_double(msgpack_packer* pk, double d); @@ -74,6 +96,8 @@ static int msgpack_pack_raw_body(msgpack_packer* pk, const void* b, size_t l); int msgpack_pack_object(msgpack_packer* pk, msgpack_object d); +/** @} */ + #define msgpack_pack_inline_func(name) \ inline int msgpack_pack ## name @@ -81,12 +105,18 @@ int msgpack_pack_object(msgpack_packer* pk, msgpack_object d); #define msgpack_pack_inline_func_cint(name) \ inline int msgpack_pack ## name +#define msgpack_pack_inline_func_cint(name) \ + inline int msgpack_pack ## name + +#define msgpack_pack_inline_func_fixint(name) \ + inline int msgpack_pack_fix ## name + #define msgpack_pack_user msgpack_packer* #define msgpack_pack_append_buffer(user, buf, len) \ return (*(user)->callback)((user)->data, (const char*)buf, len) -#include "msgpack/pack_template.h" +#include "pack_template.h" inline void msgpack_packer_init(msgpack_packer* pk, void* data, msgpack_packer_write callback) { diff --git a/cpp/msgpack/pack.hpp b/cpp/src/msgpack/pack.hpp similarity index 78% rename from cpp/msgpack/pack.hpp rename to cpp/src/msgpack/pack.hpp index ee906900..0090b961 100644 --- a/cpp/msgpack/pack.hpp +++ b/cpp/src/msgpack/pack.hpp @@ -18,7 +18,7 @@ #ifndef MSGPACK_PACK_HPP__ #define MSGPACK_PACK_HPP__ -#include "msgpack/pack_define.h" +#include "pack_define.h" #include #include @@ -45,6 +45,15 @@ public: packer& pack_int32(int32_t d); packer& pack_int64(int64_t d); + packer& pack_fix_uint8(uint8_t d); + packer& pack_fix_uint16(uint16_t d); + packer& pack_fix_uint32(uint32_t d); + packer& pack_fix_uint64(uint64_t d); + packer& pack_fix_int8(int8_t d); + packer& pack_fix_int16(int16_t d); + packer& pack_fix_int32(int32_t d); + packer& pack_fix_int64(int64_t d); + packer& pack_short(short d); packer& pack_int(int d); packer& pack_long(long d); @@ -78,6 +87,15 @@ private: static void _pack_int32(Stream& x, int32_t d); static void _pack_int64(Stream& x, int64_t d); + static void _pack_fix_uint8(Stream& x, uint8_t d); + static void _pack_fix_uint16(Stream& x, uint16_t d); + static void _pack_fix_uint32(Stream& x, uint32_t d); + static void _pack_fix_uint64(Stream& x, uint64_t d); + static void _pack_fix_int8(Stream& x, int8_t d); + static void _pack_fix_int16(Stream& x, int16_t d); + static void _pack_fix_int32(Stream& x, int32_t d); + static void _pack_fix_int64(Stream& x, int64_t d); + static void _pack_short(Stream& x, short d); static void _pack_int(Stream& x, int d); static void _pack_long(Stream& x, long d); @@ -133,11 +151,15 @@ inline void pack(Stream& s, const T& v) template \ inline void packer::_pack ## name +#define msgpack_pack_inline_func_fixint(name) \ + template \ + inline void packer::_pack_fix ## name + #define msgpack_pack_user Stream& #define msgpack_pack_append_buffer append_buffer -#include "msgpack/pack_template.h" +#include "pack_template.h" template @@ -149,6 +171,7 @@ packer::packer(Stream& s) : m_stream(s) { } template packer::~packer() { } + template inline packer& packer::pack_uint8(uint8_t d) { _pack_uint8(m_stream, d); return *this; } @@ -182,6 +205,39 @@ inline packer& packer::pack_int64(int64_t d) { _pack_int64(m_stream, d); return *this;} +template +inline packer& packer::pack_fix_uint8(uint8_t d) +{ _pack_fix_uint8(m_stream, d); return *this; } + +template +inline packer& packer::pack_fix_uint16(uint16_t d) +{ _pack_fix_uint16(m_stream, d); return *this; } + +template +inline packer& packer::pack_fix_uint32(uint32_t d) +{ _pack_fix_uint32(m_stream, d); return *this; } + +template +inline packer& packer::pack_fix_uint64(uint64_t d) +{ _pack_fix_uint64(m_stream, d); return *this; } + +template +inline packer& packer::pack_fix_int8(int8_t d) +{ _pack_fix_int8(m_stream, d); return *this; } + +template +inline packer& packer::pack_fix_int16(int16_t d) +{ _pack_fix_int16(m_stream, d); return *this; } + +template +inline packer& packer::pack_fix_int32(int32_t d) +{ _pack_fix_int32(m_stream, d); return *this; } + +template +inline packer& packer::pack_fix_int64(int64_t d) +{ _pack_fix_int64(m_stream, d); return *this;} + + template inline packer& packer::pack_short(short d) { _pack_short(m_stream, d); return *this; } diff --git a/cpp/msgpack/sbuffer.h b/cpp/src/msgpack/sbuffer.h similarity index 81% rename from cpp/msgpack/sbuffer.h rename to cpp/src/msgpack/sbuffer.h index bc0a8fd0..778dea71 100644 --- a/cpp/msgpack/sbuffer.h +++ b/cpp/src/msgpack/sbuffer.h @@ -21,15 +21,17 @@ #include #include -#ifndef MSGPACK_SBUFFER_INIT_SIZE -#define MSGPACK_SBUFFER_INIT_SIZE 8192 -#endif - #ifdef __cplusplus extern "C" { #endif +/** + * @defgroup msgpack_sbuffer Simple buffer + * @ingroup msgpack_buffer + * @{ + */ + typedef struct msgpack_sbuffer { size_t size; char* data; @@ -46,6 +48,22 @@ static inline void msgpack_sbuffer_destroy(msgpack_sbuffer* sbuf) free(sbuf->data); } +static inline msgpack_sbuffer* msgpack_sbuffer_new(void) +{ + return (msgpack_sbuffer*)calloc(1, sizeof(msgpack_sbuffer)); +} + +static inline void msgpack_sbuffer_free(msgpack_sbuffer* sbuf) +{ + if(sbuf == NULL) { return; } + msgpack_sbuffer_destroy(sbuf); + free(sbuf); +} + +#ifndef MSGPACK_SBUFFER_INIT_SIZE +#define MSGPACK_SBUFFER_INIT_SIZE 8192 +#endif + static inline int msgpack_sbuffer_write(void* data, const char* buf, unsigned int len) { msgpack_sbuffer* sbuf = (msgpack_sbuffer*)data; @@ -77,6 +95,13 @@ static inline char* msgpack_sbuffer_release(msgpack_sbuffer* sbuf) return tmp; } +static inline void msgpack_sbuffer_clear(msgpack_sbuffer* sbuf) +{ + sbuf->size = 0; +} + +/** @} */ + #ifdef __cplusplus } diff --git a/cpp/msgpack/sbuffer.hpp b/cpp/src/msgpack/sbuffer.hpp similarity index 96% rename from cpp/msgpack/sbuffer.hpp rename to cpp/src/msgpack/sbuffer.hpp index ca06884e..a9efc6db 100644 --- a/cpp/msgpack/sbuffer.hpp +++ b/cpp/src/msgpack/sbuffer.hpp @@ -18,7 +18,7 @@ #ifndef MSGPACK_SBUFFER_HPP__ #define MSGPACK_SBUFFER_HPP__ -#include "msgpack/sbuffer.h" +#include "sbuffer.h" #include namespace msgpack { @@ -72,6 +72,11 @@ public: return msgpack_sbuffer_release(this); } + void clear() + { + msgpack_sbuffer_clear(this); + } + private: void expand_buffer(size_t len) { diff --git a/cpp/src/msgpack/type.hpp b/cpp/src/msgpack/type.hpp new file mode 100644 index 00000000..bca69bfd --- /dev/null +++ b/cpp/src/msgpack/type.hpp @@ -0,0 +1,16 @@ +#include "type/bool.hpp" +#include "type/deque.hpp" +#include "type/fixint.hpp" +#include "type/float.hpp" +#include "type/int.hpp" +#include "type/list.hpp" +#include "type/map.hpp" +#include "type/nil.hpp" +#include "type/pair.hpp" +#include "type/raw.hpp" +#include "type/set.hpp" +#include "type/string.hpp" +#include "type/vector.hpp" +#include "type/tuple.hpp" +#include "type/define.hpp" + diff --git a/cpp/msgpack/type/bool.hpp b/cpp/src/msgpack/type/bool.hpp similarity index 100% rename from cpp/msgpack/type/bool.hpp rename to cpp/src/msgpack/type/bool.hpp diff --git a/cpp/msgpack/type/define.hpp.erb b/cpp/src/msgpack/type/define.hpp.erb similarity index 100% rename from cpp/msgpack/type/define.hpp.erb rename to cpp/src/msgpack/type/define.hpp.erb diff --git a/cpp/msgpack/type/deque.hpp b/cpp/src/msgpack/type/deque.hpp similarity index 100% rename from cpp/msgpack/type/deque.hpp rename to cpp/src/msgpack/type/deque.hpp diff --git a/cpp/src/msgpack/type/fixint.hpp b/cpp/src/msgpack/type/fixint.hpp new file mode 100644 index 00000000..ebe2696f --- /dev/null +++ b/cpp/src/msgpack/type/fixint.hpp @@ -0,0 +1,172 @@ +// +// MessagePack for C++ static resolution routine +// +// Copyright (C) 2020 FURUHASHI Sadayuki +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +#ifndef MSGPACK_TYPE_FIXINT_HPP__ +#define MSGPACK_TYPE_FIXINT_HPP__ + +#include "msgpack/object.hpp" +#include "msgpack/type/int.hpp" + +namespace msgpack { + +namespace type { + + +template +struct fix_int { + fix_int() : value(0) { } + fix_int(T value) : value(value) { } + + operator T() const { return value; } + + T get() const { return value; } + +private: + T value; +}; + + +typedef fix_int fix_uint8; +typedef fix_int fix_uint16; +typedef fix_int fix_uint32; +typedef fix_int fix_uint64; + +typedef fix_int fix_int8; +typedef fix_int fix_int16; +typedef fix_int fix_int32; +typedef fix_int fix_int64; + + +} // namespace type + + +inline type::fix_int8& operator>> (object o, type::fix_int8& v) + { v = type::detail::convert_integer(o); return v; } + +inline type::fix_int16& operator>> (object o, type::fix_int16& v) + { v = type::detail::convert_integer(o); return v; } + +inline type::fix_int32& operator>> (object o, type::fix_int32& v) + { v = type::detail::convert_integer(o); return v; } + +inline type::fix_int64& operator>> (object o, type::fix_int64& v) + { v = type::detail::convert_integer(o); return v; } + + +inline type::fix_uint8& operator>> (object o, type::fix_uint8& v) + { v = type::detail::convert_integer(o); return v; } + +inline type::fix_uint16& operator>> (object o, type::fix_uint16& v) + { v = type::detail::convert_integer(o); return v; } + +inline type::fix_uint32& operator>> (object o, type::fix_uint32& v) + { v = type::detail::convert_integer(o); return v; } + +inline type::fix_uint64& operator>> (object o, type::fix_uint64& v) + { v = type::detail::convert_integer(o); return v; } + + +template +inline packer& operator<< (packer& o, const type::fix_int8& v) + { o.pack_fix_int8(v); return o; } + +template +inline packer& operator<< (packer& o, const type::fix_int16& v) + { o.pack_fix_int16(v); return o; } + +template +inline packer& operator<< (packer& o, const type::fix_int32& v) + { o.pack_fix_int32(v); return o; } + +template +inline packer& operator<< (packer& o, const type::fix_int64& v) + { o.pack_fix_int64(v); return o; } + + +template +inline packer& operator<< (packer& o, const type::fix_uint8& v) + { o.pack_fix_uint8(v); return o; } + +template +inline packer& operator<< (packer& o, const type::fix_uint16& v) + { o.pack_fix_uint16(v); return o; } + +template +inline packer& operator<< (packer& o, const type::fix_uint32& v) + { o.pack_fix_uint32(v); return o; } + +template +inline packer& operator<< (packer& o, const type::fix_uint64& v) + { o.pack_fix_uint64(v); return o; } + + +inline void operator<< (object& o, type::fix_int8 v) + { v.get() < 0 ? o.type = type::NEGATIVE_INTEGER, o.via.i64 = v.get() : o.type = type::POSITIVE_INTEGER, o.via.u64 = v.get(); } + +inline void operator<< (object& o, type::fix_int16 v) + { v.get() < 0 ? o.type = type::NEGATIVE_INTEGER, o.via.i64 = v.get() : o.type = type::POSITIVE_INTEGER, o.via.u64 = v.get(); } + +inline void operator<< (object& o, type::fix_int32 v) + { v.get() < 0 ? o.type = type::NEGATIVE_INTEGER, o.via.i64 = v.get() : o.type = type::POSITIVE_INTEGER, o.via.u64 = v.get(); } + +inline void operator<< (object& o, type::fix_int64 v) + { v.get() < 0 ? o.type = type::NEGATIVE_INTEGER, o.via.i64 = v.get() : o.type = type::POSITIVE_INTEGER, o.via.u64 = v.get(); } + + +inline void operator<< (object& o, type::fix_uint8 v) + { o.type = type::POSITIVE_INTEGER, o.via.u64 = v.get(); } + +inline void operator<< (object& o, type::fix_uint16 v) + { o.type = type::POSITIVE_INTEGER, o.via.u64 = v.get(); } + +inline void operator<< (object& o, type::fix_uint32 v) + { o.type = type::POSITIVE_INTEGER, o.via.u64 = v.get(); } + +inline void operator<< (object& o, type::fix_uint64 v) + { o.type = type::POSITIVE_INTEGER, o.via.u64 = v.get(); } + + +inline void operator<< (object::with_zone& o, type::fix_int8 v) + { static_cast(o) << v; } + +inline void operator<< (object::with_zone& o, type::fix_int16 v) + { static_cast(o) << v; } + +inline void operator<< (object::with_zone& o, type::fix_int32 v) + { static_cast(o) << v; } + +inline void operator<< (object::with_zone& o, type::fix_int64 v) + { static_cast(o) << v; } + + +inline void operator<< (object::with_zone& o, type::fix_uint8 v) + { static_cast(o) << v; } + +inline void operator<< (object::with_zone& o, type::fix_uint16 v) + { static_cast(o) << v; } + +inline void operator<< (object::with_zone& o, type::fix_uint32 v) + { static_cast(o) << v; } + +inline void operator<< (object::with_zone& o, type::fix_uint64 v) + { static_cast(o) << v; } + + +} // namespace msgpack + +#endif /* msgpack/type/fixint.hpp */ + diff --git a/cpp/msgpack/type/float.hpp b/cpp/src/msgpack/type/float.hpp similarity index 100% rename from cpp/msgpack/type/float.hpp rename to cpp/src/msgpack/type/float.hpp diff --git a/cpp/msgpack/type/int.hpp b/cpp/src/msgpack/type/int.hpp similarity index 100% rename from cpp/msgpack/type/int.hpp rename to cpp/src/msgpack/type/int.hpp diff --git a/cpp/msgpack/type/list.hpp b/cpp/src/msgpack/type/list.hpp similarity index 100% rename from cpp/msgpack/type/list.hpp rename to cpp/src/msgpack/type/list.hpp diff --git a/cpp/msgpack/type/map.hpp b/cpp/src/msgpack/type/map.hpp similarity index 100% rename from cpp/msgpack/type/map.hpp rename to cpp/src/msgpack/type/map.hpp diff --git a/cpp/msgpack/type/nil.hpp b/cpp/src/msgpack/type/nil.hpp similarity index 93% rename from cpp/msgpack/type/nil.hpp rename to cpp/src/msgpack/type/nil.hpp index e58bc9d4..f44e45e4 100644 --- a/cpp/msgpack/type/nil.hpp +++ b/cpp/src/msgpack/type/nil.hpp @@ -51,6 +51,14 @@ inline void operator<< (object::with_zone& o, type::nil v) { static_cast(o) << v; } +template <> +inline void object::as() const +{ + msgpack::type::nil v; + convert(&v); +} + + } // namespace msgpack #endif /* msgpack/type/nil.hpp */ diff --git a/cpp/msgpack/type/pair.hpp b/cpp/src/msgpack/type/pair.hpp similarity index 100% rename from cpp/msgpack/type/pair.hpp rename to cpp/src/msgpack/type/pair.hpp diff --git a/cpp/msgpack/type/raw.hpp b/cpp/src/msgpack/type/raw.hpp similarity index 89% rename from cpp/msgpack/type/raw.hpp rename to cpp/src/msgpack/type/raw.hpp index 21d9a0d9..87d188f6 100644 --- a/cpp/msgpack/type/raw.hpp +++ b/cpp/src/msgpack/type/raw.hpp @@ -33,25 +33,25 @@ struct raw_ref { uint32_t size; const char* ptr; - std::string str() { return std::string(ptr, size); } + std::string str() const { return std::string(ptr, size); } - bool operator== (const raw_ref& x) + bool operator== (const raw_ref& x) const { return size == x.size && memcmp(ptr, x.ptr, size) == 0; } - bool operator!= (const raw_ref& x) + bool operator!= (const raw_ref& x) const { return !(*this != x); } - bool operator< (const raw_ref& x) + bool operator< (const raw_ref& x) const { if(size == x.size) { return memcmp(ptr, x.ptr, size) < 0; } else { return size < x.size; } } - bool operator> (const raw_ref& x) + bool operator> (const raw_ref& x) const { if(size == x.size) { return memcmp(ptr, x.ptr, size) > 0; } else { return size > x.size; } diff --git a/cpp/msgpack/type/set.hpp b/cpp/src/msgpack/type/set.hpp similarity index 100% rename from cpp/msgpack/type/set.hpp rename to cpp/src/msgpack/type/set.hpp diff --git a/cpp/msgpack/type/string.hpp b/cpp/src/msgpack/type/string.hpp similarity index 100% rename from cpp/msgpack/type/string.hpp rename to cpp/src/msgpack/type/string.hpp diff --git a/cpp/msgpack/type/tr1/unordered_map.hpp b/cpp/src/msgpack/type/tr1/unordered_map.hpp similarity index 100% rename from cpp/msgpack/type/tr1/unordered_map.hpp rename to cpp/src/msgpack/type/tr1/unordered_map.hpp diff --git a/cpp/msgpack/type/tr1/unordered_set.hpp b/cpp/src/msgpack/type/tr1/unordered_set.hpp similarity index 100% rename from cpp/msgpack/type/tr1/unordered_set.hpp rename to cpp/src/msgpack/type/tr1/unordered_set.hpp diff --git a/cpp/msgpack/type/tuple.hpp.erb b/cpp/src/msgpack/type/tuple.hpp.erb similarity index 90% rename from cpp/msgpack/type/tuple.hpp.erb rename to cpp/src/msgpack/type/tuple.hpp.erb index 1b0c172c..ebef8163 100644 --- a/cpp/msgpack/type/tuple.hpp.erb +++ b/cpp/src/msgpack/type/tuple.hpp.erb @@ -141,7 +141,7 @@ type::tuple, A<%=j%><%}%>>& operator>> ( if(o.type != type::ARRAY) { throw type_error(); } if(o.via.array.size < <%=i+1%>) { throw type_error(); } <%0.upto(i) {|j|%> - o.via.array.ptr[<%=j%>].convert>(&v.template get<<%=j%>>());<%}%> + o.via.array.ptr[<%=j%>].convert>::type>(&v.template get<<%=j%>>());<%}%> return v; } <%}%> @@ -187,5 +187,20 @@ inline void operator<< ( } // namespace msgpack + +//inline std::ostream& operator<< (std::ostream& o, const msgpack::type::tuple<>& v) { +// return o << "[]"; +//} +//<%0.upto(GENERATION_LIMIT) {|i|%> +//template , typename A<%=j%><%}%>> +//inline std::ostream& operator<< (std::ostream& o, +// const msgpack::type::tuple, A<%=j%><%}%>>& v) { +// return o << "[" +// <%0.upto(i) {|j|%> +// <<<%if j != 0 then%> ", " <<<%end%> v.template get<<%=j%>>()<%}%> +// << "]"; +//} +//<%}%> + #endif /* msgpack/type/tuple.hpp */ diff --git a/cpp/msgpack/type/vector.hpp b/cpp/src/msgpack/type/vector.hpp similarity index 100% rename from cpp/msgpack/type/vector.hpp rename to cpp/src/msgpack/type/vector.hpp diff --git a/cpp/src/msgpack/unpack.h b/cpp/src/msgpack/unpack.h new file mode 100644 index 00000000..bea7d922 --- /dev/null +++ b/cpp/src/msgpack/unpack.h @@ -0,0 +1,260 @@ +/* + * MessagePack for C unpacking routine + * + * Copyright (C) 2008-2009 FURUHASHI Sadayuki + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef MSGPACK_UNPACKER_H__ +#define MSGPACK_UNPACKER_H__ + +#include "zone.h" +#include "object.h" +#include + +#ifdef __cplusplus +extern "C" { +#endif + + +/** + * @defgroup msgpack_unpack Deserializer + * @ingroup msgpack + * @{ + */ + +typedef struct msgpack_unpacked { + msgpack_zone* zone; + msgpack_object data; +} msgpack_unpacked; + +bool msgpack_unpack_next(msgpack_unpacked* result, + const char* data, size_t len, size_t* off); + +/** @} */ + + +/** + * @defgroup msgpack_unpacker Streaming deserializer + * @ingroup msgpack + * @{ + */ + +typedef struct msgpack_unpacker { + char* buffer; + size_t used; + size_t free; + size_t off; + size_t parsed; + msgpack_zone* z; + size_t initial_buffer_size; + void* ctx; +} msgpack_unpacker; + + +#ifndef MSGPACK_UNPACKER_INIT_BUFFER_SIZE +#define MSGPACK_UNPACKER_INIT_BUFFER_SIZE (64*1024) +#endif + +/** + * Initializes a streaming deserializer. + * The initialized deserializer must be destroyed by msgpack_unpacker_destroy(msgpack_unpacker*). + */ +bool msgpack_unpacker_init(msgpack_unpacker* mpac, size_t initial_buffer_size); + +/** + * Destroys a streaming deserializer initialized by msgpack_unpacker_init(msgpack_unpacker*, size_t). + */ +void msgpack_unpacker_destroy(msgpack_unpacker* mpac); + + +/** + * Creates a streaming deserializer. + * The created deserializer must be destroyed by msgpack_unpacker_free(msgpack_unpacker*). + */ +msgpack_unpacker* msgpack_unpacker_new(size_t initial_buffer_size); + +/** + * Frees a streaming deserializer created by msgpack_unpacker_new(size_t). + */ +void msgpack_unpacker_free(msgpack_unpacker* mpac); + + +#ifndef MSGPACK_UNPACKER_RESERVE_SIZE +#define MSGPACK_UNPACKER_RESERVE_SIZE (32*1024) +#endif + +/** + * Reserves free space of the internal buffer. + * Use this function to fill the internal buffer with + * msgpack_unpacker_buffer(msgpack_unpacker*), + * msgpack_unpacker_buffer_capacity(const msgpack_unpacker*) and + * msgpack_unpacker_buffer_consumed(msgpack_unpacker*). + */ +static inline bool msgpack_unpacker_reserve_buffer(msgpack_unpacker* mpac, size_t size); + +/** + * Gets pointer to the free space of the internal buffer. + * Use this function to fill the internal buffer with + * msgpack_unpacker_reserve_buffer(msgpack_unpacker*, size_t), + * msgpack_unpacker_buffer_capacity(const msgpack_unpacker*) and + * msgpack_unpacker_buffer_consumed(msgpack_unpacker*). + */ +static inline char* msgpack_unpacker_buffer(msgpack_unpacker* mpac); + +/** + * Gets size of the free space of the internal buffer. + * Use this function to fill the internal buffer with + * msgpack_unpacker_reserve_buffer(msgpack_unpacker*, size_t), + * msgpack_unpacker_buffer(const msgpack_unpacker*) and + * msgpack_unpacker_buffer_consumed(msgpack_unpacker*). + */ +static inline size_t msgpack_unpacker_buffer_capacity(const msgpack_unpacker* mpac); + +/** + * Notifies the deserializer that the internal buffer filled. + * Use this function to fill the internal buffer with + * msgpack_unpacker_reserve_buffer(msgpack_unpacker*, size_t), + * msgpack_unpacker_buffer(msgpack_unpacker*) and + * msgpack_unpacker_buffer_capacity(const msgpack_unpacker*). + */ +static inline void msgpack_unpacker_buffer_consumed(msgpack_unpacker* mpac, size_t size); + + +/** + * Deserializes one object. + * Returns true if it successes. Otherwise false is returned. + * @param pac pointer to an initialized msgpack_unpacked object. + */ +bool msgpack_unpacker_next(msgpack_unpacker* mpac, msgpack_unpacked* pac); + +/** + * Initializes a msgpack_unpacked object. + * The initialized object must be destroyed by msgpack_unpacked_destroy(msgpack_unpacker*). + * Use the object with msgpack_unpacker_next(msgpack_unpacker*, msgpack_unpacked*) or + * msgpack_unpack_next(msgpack_unpacked*, const char*, size_t, size_t*). + */ +static inline void msgpack_unpacked_init(msgpack_unpacked* result); + +/** + * Destroys a streaming deserializer initialized by msgpack_unpacked(). + */ +static inline void msgpack_unpacked_destroy(msgpack_unpacked* result); + +/** + * Releases the memory zone from msgpack_unpacked object. + * The released zone must be freed by msgpack_zone_free(msgpack_zone*). + */ +static inline msgpack_zone* msgpack_unpacked_release_zone(msgpack_unpacked* result); + + +int msgpack_unpacker_execute(msgpack_unpacker* mpac); + +msgpack_object msgpack_unpacker_data(msgpack_unpacker* mpac); + +msgpack_zone* msgpack_unpacker_release_zone(msgpack_unpacker* mpac); + +void msgpack_unpacker_reset_zone(msgpack_unpacker* mpac); + +void msgpack_unpacker_reset(msgpack_unpacker* mpac); + +static inline size_t msgpack_unpacker_message_size(const msgpack_unpacker* mpac); + + +/** @} */ + + +// obsolete +typedef enum { + MSGPACK_UNPACK_SUCCESS = 2, + MSGPACK_UNPACK_EXTRA_BYTES = 1, + MSGPACK_UNPACK_CONTINUE = 0, + MSGPACK_UNPACK_PARSE_ERROR = -1, +} msgpack_unpack_return; + +// obsolete +msgpack_unpack_return +msgpack_unpack(const char* data, size_t len, size_t* off, + msgpack_zone* result_zone, msgpack_object* result); + + +static inline size_t msgpack_unpacker_parsed_size(const msgpack_unpacker* mpac); + +bool msgpack_unpacker_flush_zone(msgpack_unpacker* mpac); + +bool msgpack_unpacker_expand_buffer(msgpack_unpacker* mpac, size_t size); + +bool msgpack_unpacker_reserve_buffer(msgpack_unpacker* mpac, size_t size) +{ + if(mpac->free >= size) { return true; } + return msgpack_unpacker_expand_buffer(mpac, size); +} + +char* msgpack_unpacker_buffer(msgpack_unpacker* mpac) +{ + return mpac->buffer + mpac->used; +} + +size_t msgpack_unpacker_buffer_capacity(const msgpack_unpacker* mpac) +{ + return mpac->free; +} + +void msgpack_unpacker_buffer_consumed(msgpack_unpacker* mpac, size_t size) +{ + mpac->used += size; + mpac->free -= size; +} + +size_t msgpack_unpacker_message_size(const msgpack_unpacker* mpac) +{ + return mpac->parsed - mpac->off + mpac->used; +} + +size_t msgpack_unpacker_parsed_size(const msgpack_unpacker* mpac) +{ + return mpac->parsed; +} + + +void msgpack_unpacked_init(msgpack_unpacked* result) +{ + memset(result, 0, sizeof(msgpack_unpacked)); +} + +void msgpack_unpacked_destroy(msgpack_unpacked* result) +{ + if(result->zone != NULL) { + msgpack_zone_free(result->zone); + result->zone = NULL; + memset(&result->data, 0, sizeof(msgpack_object)); + } +} + +msgpack_zone* msgpack_unpacked_release_zone(msgpack_unpacked* result) +{ + if(result->zone != NULL) { + msgpack_zone* z = result->zone; + result->zone = NULL; + return z; + } + return NULL; +} + + +#ifdef __cplusplus +} +#endif + +#endif /* msgpack/unpack.h */ + diff --git a/cpp/msgpack/unpack.hpp b/cpp/src/msgpack/unpack.hpp similarity index 94% rename from cpp/msgpack/unpack.hpp rename to cpp/src/msgpack/unpack.hpp index 56ce0f65..51580b6e 100644 --- a/cpp/msgpack/unpack.hpp +++ b/cpp/src/msgpack/unpack.hpp @@ -18,14 +18,15 @@ #ifndef MSGPACK_UNPACK_HPP__ #define MSGPACK_UNPACK_HPP__ -#include "msgpack/unpack.h" -#include "msgpack/object.hpp" -#include "msgpack/zone.hpp" +#include "unpack.h" +#include "object.hpp" +#include "zone.hpp" #include #include +// backward compatibility #ifndef MSGPACK_UNPACKER_DEFAULT_INITIAL_BUFFER_SIZE -#define MSGPACK_UNPACKER_DEFAULT_INITIAL_BUFFER_SIZE (32*1024) +#define MSGPACK_UNPACKER_DEFAULT_INITIAL_BUFFER_SIZE MSGPACK_UNPACKER_INIT_BUFFER_SIZE #endif namespace msgpack { @@ -64,12 +65,12 @@ private: class unpacker : public msgpack_unpacker { public: - unpacker(size_t init_buffer_size = MSGPACK_UNPACKER_DEFAULT_INITIAL_BUFFER_SIZE); + unpacker(size_t init_buffer_size = MSGPACK_UNPACKER_INIT_BUFFER_SIZE); ~unpacker(); public: /*! 1. reserve buffer. at least `size' bytes of capacity will be ready */ - void reserve_buffer(size_t size); + void reserve_buffer(size_t size = MSGPACK_UNPACKER_RESERVE_SIZE); /*! 2. read data to the buffer() up to buffer_capacity() bytes */ char* buffer(); @@ -160,7 +161,7 @@ private: }; -static bool unpack(unpacked* result, +static void unpack(unpacked* result, const char* data, size_t len, size_t* offset = NULL); @@ -312,7 +313,7 @@ inline void unpacker::remove_nonparsed_buffer() } -inline bool unpack(unpacked* result, +inline void unpack(unpacked* result, const char* data, size_t len, size_t* offset) { msgpack::object obj; @@ -326,12 +327,12 @@ inline bool unpack(unpacked* result, case UNPACK_SUCCESS: result->get() = obj; result->zone() = z; - return false; + return; case UNPACK_EXTRA_BYTES: result->get() = obj; result->zone() = z; - return true; + return; case UNPACK_CONTINUE: throw unpack_error("insufficient bytes"); diff --git a/cpp/src/msgpack/version.h.in b/cpp/src/msgpack/version.h.in new file mode 100644 index 00000000..f1feb331 --- /dev/null +++ b/cpp/src/msgpack/version.h.in @@ -0,0 +1,40 @@ +/* + * MessagePack for C version information + * + * Copyright (C) 2008-2009 FURUHASHI Sadayuki + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef MSGPACK_VERSION_H__ +#define MSGPACK_VERSION_H__ + +#ifdef __cplusplus +extern "C" { +#endif + + +const char* msgpack_version(void); +int msgpack_version_major(void); +int msgpack_version_minor(void); + +#define MSGPACK_VERSION "@VERSION@" +#define MSGPACK_VERSION_MAJOR @VERSION_MAJOR@ +#define MSGPACK_VERSION_MINOR @VERSION_MINOR@ + + +#ifdef __cplusplus +} +#endif + +#endif /* msgpack/version.h */ + diff --git a/cpp/msgpack/vrefbuffer.h b/cpp/src/msgpack/vrefbuffer.h similarity index 77% rename from cpp/msgpack/vrefbuffer.h rename to cpp/src/msgpack/vrefbuffer.h index 38ead67b..0643927e 100644 --- a/cpp/msgpack/vrefbuffer.h +++ b/cpp/src/msgpack/vrefbuffer.h @@ -18,7 +18,8 @@ #ifndef MSGPACK_VREFBUFFER_H__ #define MSGPACK_VREFBUFFER_H__ -#include "msgpack/zone.h" +#include "zone.h" +#include #ifndef _WIN32 #include @@ -29,19 +30,17 @@ struct iovec { }; #endif -#ifndef MSGPACK_VREFBUFFER_REF_SIZE -#define MSGPACK_VREFBUFFER_REF_SIZE 32 -#endif - -#ifndef MSGPACK_VREFBUFFER_CHUNK_SIZE -#define MSGPACK_VREFBUFFER_CHUNK_SIZE 8192 -#endif - #ifdef __cplusplus extern "C" { #endif +/** + * @defgroup msgpack_vrefbuffer Vectored Referencing buffer + * @ingroup msgpack_buffer + * @{ + */ + struct msgpack_vrefbuffer_chunk; typedef struct msgpack_vrefbuffer_chunk msgpack_vrefbuffer_chunk; @@ -63,10 +62,21 @@ typedef struct msgpack_vrefbuffer { } msgpack_vrefbuffer; +#ifndef MSGPACK_VREFBUFFER_REF_SIZE +#define MSGPACK_VREFBUFFER_REF_SIZE 32 +#endif + +#ifndef MSGPACK_VREFBUFFER_CHUNK_SIZE +#define MSGPACK_VREFBUFFER_CHUNK_SIZE 8192 +#endif + bool msgpack_vrefbuffer_init(msgpack_vrefbuffer* vbuf, size_t ref_size, size_t chunk_size); void msgpack_vrefbuffer_destroy(msgpack_vrefbuffer* vbuf); +static inline msgpack_vrefbuffer* msgpack_vrefbuffer_new(size_t ref_size, size_t chunk_size); +static inline void msgpack_vrefbuffer_free(msgpack_vrefbuffer* vbuf); + static inline int msgpack_vrefbuffer_write(void* data, const char* buf, unsigned int len); static inline const struct iovec* msgpack_vrefbuffer_vec(const msgpack_vrefbuffer* vref); @@ -80,6 +90,28 @@ int msgpack_vrefbuffer_append_ref(msgpack_vrefbuffer* vbuf, int msgpack_vrefbuffer_migrate(msgpack_vrefbuffer* vbuf, msgpack_vrefbuffer* to); +void msgpack_vrefbuffer_clear(msgpack_vrefbuffer* vref); + +/** @} */ + + +msgpack_vrefbuffer* msgpack_vrefbuffer_new(size_t ref_size, size_t chunk_size) +{ + msgpack_vrefbuffer* vbuf = (msgpack_vrefbuffer*)malloc(sizeof(msgpack_vrefbuffer)); + if(!msgpack_vrefbuffer_init(vbuf, ref_size, chunk_size)) { + free(vbuf); + return NULL; + } + return vbuf; +} + +void msgpack_vrefbuffer_free(msgpack_vrefbuffer* vbuf) +{ + if(vbuf == NULL) { return; } + msgpack_vrefbuffer_destroy(vbuf); + free(vbuf); +} + int msgpack_vrefbuffer_write(void* data, const char* buf, unsigned int len) { msgpack_vrefbuffer* vbuf = (msgpack_vrefbuffer*)data; diff --git a/cpp/msgpack/vrefbuffer.hpp b/cpp/src/msgpack/vrefbuffer.hpp similarity index 96% rename from cpp/msgpack/vrefbuffer.hpp rename to cpp/src/msgpack/vrefbuffer.hpp index c8eca7b6..82335277 100644 --- a/cpp/msgpack/vrefbuffer.hpp +++ b/cpp/src/msgpack/vrefbuffer.hpp @@ -18,7 +18,7 @@ #ifndef MSGPACK_VREFBUFFER_HPP__ #define MSGPACK_VREFBUFFER_HPP__ -#include "msgpack/vrefbuffer.h" +#include "vrefbuffer.h" #include namespace msgpack { @@ -78,6 +78,11 @@ public: } } + void clear() + { + msgpack_vrefbuffer_clear(this); + } + private: typedef msgpack_vrefbuffer base; diff --git a/cpp/msgpack/zbuffer.h b/cpp/src/msgpack/zbuffer.h similarity index 86% rename from cpp/msgpack/zbuffer.h rename to cpp/src/msgpack/zbuffer.h index 2a322063..efdd3049 100644 --- a/cpp/msgpack/zbuffer.h +++ b/cpp/src/msgpack/zbuffer.h @@ -18,35 +18,39 @@ #ifndef MSGPACK_ZBUFFER_H__ #define MSGPACK_ZBUFFER_H__ -#include "msgpack/sysdep.h" +#include "sysdep.h" #include #include #include -#ifndef MSGPACK_ZBUFFER_INIT_SIZE -#define MSGPACK_ZBUFFER_INIT_SIZE 8192 -#endif - -#ifndef MSGPACK_ZBUFFER_RESERVE_SIZE -#define MSGPACK_ZBUFFER_RESERVE_SIZE 512 -#endif - #ifdef __cplusplus extern "C" { #endif +/** + * @defgroup msgpack_zbuffer Compressed buffer + * @ingroup msgpack_buffer + * @{ + */ + typedef struct msgpack_zbuffer { z_stream stream; char* data; size_t init_size; } msgpack_zbuffer; +#ifndef MSGPACK_ZBUFFER_INIT_SIZE +#define MSGPACK_ZBUFFER_INIT_SIZE 8192 +#endif static inline bool msgpack_zbuffer_init(msgpack_zbuffer* zbuf, int level, size_t init_size); static inline void msgpack_zbuffer_destroy(msgpack_zbuffer* zbuf); +static inline msgpack_zbuffer* msgpack_zbuffer_new(int level, size_t init_size); +static inline void msgpack_zbuffer_free(msgpack_zbuffer* zbuf); + static inline char* msgpack_zbuffer_flush(msgpack_zbuffer* zbuf); static inline const char* msgpack_zbuffer_data(const msgpack_zbuffer* zbuf); @@ -57,6 +61,10 @@ static inline void msgpack_zbuffer_reset_buffer(msgpack_zbuffer* zbuf); static inline char* msgpack_zbuffer_release_buffer(msgpack_zbuffer* zbuf); +#ifndef MSGPACK_ZBUFFER_RESERVE_SIZE +#define MSGPACK_ZBUFFER_RESERVE_SIZE 512 +#endif + static inline int msgpack_zbuffer_write(void* data, const char* buf, unsigned int len); static inline bool msgpack_zbuffer_expand(msgpack_zbuffer* zbuf); @@ -80,6 +88,23 @@ void msgpack_zbuffer_destroy(msgpack_zbuffer* zbuf) free(zbuf->data); } +msgpack_zbuffer* msgpack_zbuffer_new(int level, size_t init_size) +{ + msgpack_zbuffer* zbuf = (msgpack_zbuffer*)malloc(sizeof(msgpack_zbuffer)); + if(!msgpack_zbuffer_init(zbuf, level, init_size)) { + free(zbuf); + return NULL; + } + return zbuf; +} + +void msgpack_zbuffer_free(msgpack_zbuffer* zbuf) +{ + if(zbuf == NULL) { return; } + msgpack_zbuffer_destroy(zbuf); + free(zbuf); +} + bool msgpack_zbuffer_expand(msgpack_zbuffer* zbuf) { size_t used = (char*)zbuf->stream.next_out - zbuf->data; @@ -171,6 +196,8 @@ char* msgpack_zbuffer_release_buffer(msgpack_zbuffer* zbuf) return tmp; } +/** @} */ + #ifdef __cplusplus } diff --git a/cpp/msgpack/zbuffer.hpp b/cpp/src/msgpack/zbuffer.hpp similarity index 98% rename from cpp/msgpack/zbuffer.hpp rename to cpp/src/msgpack/zbuffer.hpp index 278f076e..08e6fd03 100644 --- a/cpp/msgpack/zbuffer.hpp +++ b/cpp/src/msgpack/zbuffer.hpp @@ -18,7 +18,7 @@ #ifndef MSGPACK_ZBUFFER_HPP__ #define MSGPACK_ZBUFFER_HPP__ -#include "msgpack/zbuffer.h" +#include "zbuffer.h" #include namespace msgpack { diff --git a/cpp/msgpack/zone.h b/cpp/src/msgpack/zone.h similarity index 96% rename from cpp/msgpack/zone.h rename to cpp/src/msgpack/zone.h index ce5be6d8..d8c60b68 100644 --- a/cpp/msgpack/zone.h +++ b/cpp/src/msgpack/zone.h @@ -18,13 +18,19 @@ #ifndef MSGPACK_ZONE_H__ #define MSGPACK_ZONE_H__ -#include "msgpack/sysdep.h" +#include "sysdep.h" #ifdef __cplusplus extern "C" { #endif +/** + * @defgroup msgpack_zone Memory zone + * @ingroup msgpack + * @{ + */ + typedef struct msgpack_zone_finalizer { void (*func)(void* data); void* data; @@ -71,6 +77,7 @@ bool msgpack_zone_is_empty(msgpack_zone* zone); void msgpack_zone_clear(msgpack_zone* zone); +/** @} */ #ifndef MSGPACK_ZONE_ALIGN diff --git a/cpp/msgpack/zone.hpp.erb b/cpp/src/msgpack/zone.hpp.erb similarity index 99% rename from cpp/msgpack/zone.hpp.erb rename to cpp/src/msgpack/zone.hpp.erb index 8e69aa44..1cef05ec 100644 --- a/cpp/msgpack/zone.hpp.erb +++ b/cpp/src/msgpack/zone.hpp.erb @@ -18,7 +18,7 @@ #ifndef MSGPACK_ZONE_HPP__ #define MSGPACK_ZONE_HPP__ -#include "msgpack/zone.h" +#include "zone.h" #include #include #include diff --git a/cpp/object.cpp b/cpp/src/object.cpp similarity index 100% rename from cpp/object.cpp rename to cpp/src/object.cpp diff --git a/cpp/objectc.c b/cpp/src/objectc.c similarity index 100% rename from cpp/objectc.c rename to cpp/src/objectc.c diff --git a/cpp/unpack.c b/cpp/src/unpack.c similarity index 89% rename from cpp/unpack.c rename to cpp/src/unpack.c index 34016fd1..0c21780b 100644 --- a/cpp/unpack.c +++ b/cpp/src/unpack.c @@ -323,7 +323,7 @@ msgpack_object msgpack_unpacker_data(msgpack_unpacker* mpac) msgpack_zone* msgpack_unpacker_release_zone(msgpack_unpacker* mpac) { if(!msgpack_unpacker_flush_zone(mpac)) { - return false; + return NULL; } msgpack_zone* r = msgpack_zone_new(MSGPACK_ZONE_CHUNK_SIZE); @@ -363,20 +363,46 @@ void msgpack_unpacker_reset(msgpack_unpacker* mpac) mpac->parsed = 0; } +bool msgpack_unpacker_next(msgpack_unpacker* mpac, msgpack_unpacked* result) +{ + if(result->zone != NULL) { + msgpack_zone_free(result->zone); + } + + int ret = msgpack_unpacker_execute(mpac); + + if(ret <= 0) { + result->zone = NULL; + memset(&result->data, 0, sizeof(msgpack_object)); + return false; + } + + result->zone = msgpack_unpacker_release_zone(mpac); + result->data = msgpack_unpacker_data(mpac); + msgpack_unpacker_reset(mpac); + + return true; +} + msgpack_unpack_return msgpack_unpack(const char* data, size_t len, size_t* off, - msgpack_zone* z, msgpack_object* result) + msgpack_zone* result_zone, msgpack_object* result) { + size_t noff = 0; + if(off != NULL) { noff = *off; } + + if(len <= noff) { + // FIXME + return MSGPACK_UNPACK_CONTINUE; + } + template_context ctx; template_init(&ctx); - ctx.user.z = z; + ctx.user.z = result_zone; ctx.user.referenced = false; - size_t noff = 0; - if(off != NULL) { noff = *off; } - int e = template_execute(&ctx, data, len, &noff); if(e < 0) { return MSGPACK_UNPACK_PARSE_ERROR; @@ -397,3 +423,37 @@ msgpack_unpack(const char* data, size_t len, size_t* off, return MSGPACK_UNPACK_SUCCESS; } +bool msgpack_unpack_next(msgpack_unpacked* result, + const char* data, size_t len, size_t* off) +{ + msgpack_unpacked_destroy(result); + + size_t noff = 0; + if(off != NULL) { noff = *off; } + + if(len <= noff) { + return false; + } + + msgpack_zone* z = msgpack_zone_new(MSGPACK_ZONE_CHUNK_SIZE); + + template_context ctx; + template_init(&ctx); + + ctx.user.z = z; + ctx.user.referenced = false; + + int e = template_execute(&ctx, data, len, &noff); + if(e <= 0) { + msgpack_zone_free(z); + return false; + } + + if(off != NULL) { *off = noff; } + + result->zone = z; + result->data = template_data(&ctx); + + return true; +} + diff --git a/cpp/src/version.c b/cpp/src/version.c new file mode 100644 index 00000000..3d956f18 --- /dev/null +++ b/cpp/src/version.c @@ -0,0 +1,17 @@ +#include "msgpack.h" + +const char* msgpack_version(void) +{ + return MSGPACK_VERSION; +} + +int msgpack_version_major(void) +{ + return MSGPACK_VERSION_MAJOR; +} + +int msgpack_version_minor(void) +{ + return MSGPACK_VERSION_MINOR; +} + diff --git a/cpp/vrefbuffer.c b/cpp/src/vrefbuffer.c similarity index 90% rename from cpp/vrefbuffer.c rename to cpp/src/vrefbuffer.c index 136372f6..a27b138e 100644 --- a/cpp/vrefbuffer.c +++ b/cpp/src/vrefbuffer.c @@ -75,6 +75,25 @@ void msgpack_vrefbuffer_destroy(msgpack_vrefbuffer* vbuf) free(vbuf->array); } +void msgpack_vrefbuffer_clear(msgpack_vrefbuffer* vbuf) +{ + msgpack_vrefbuffer_chunk* c = vbuf->inner_buffer.head->next; + msgpack_vrefbuffer_chunk* n; + while(c != NULL) { + n = c->next; + free(c); + c = n; + } + + msgpack_vrefbuffer_inner_buffer* const ib = &vbuf->inner_buffer; + msgpack_vrefbuffer_chunk* chunk = ib->head; + chunk->next = NULL; + ib->free = vbuf->chunk_size; + ib->ptr = ((char*)chunk) + sizeof(msgpack_vrefbuffer_chunk); + + vbuf->tail = vbuf->array; +} + int msgpack_vrefbuffer_append_ref(msgpack_vrefbuffer* vbuf, const char* buf, unsigned int len) { diff --git a/cpp/zone.c b/cpp/src/zone.c similarity index 99% rename from cpp/zone.c rename to cpp/src/zone.c index 3d0634ea..8cc8b0de 100644 --- a/cpp/zone.c +++ b/cpp/src/zone.c @@ -204,7 +204,7 @@ msgpack_zone* msgpack_zone_new(size_t chunk_size) if(!init_chunk_list(&zone->chunk_list, chunk_size)) { free(zone); - return false; + return NULL; } init_finalizer_array(&zone->finalizer_array); @@ -214,6 +214,7 @@ msgpack_zone* msgpack_zone_new(size_t chunk_size) void msgpack_zone_free(msgpack_zone* zone) { + if(zone == NULL) { return; } msgpack_zone_destroy(zone); free(zone); } diff --git a/cpp/test.mk b/cpp/test.mk deleted file mode 100644 index f1beac55..00000000 --- a/cpp/test.mk +++ /dev/null @@ -1,9 +0,0 @@ - -CXXFLAGS += -Wall -g -I. -I.. -O4 -LDFLAGS += - -all: test - -test: test.o unpack.o zone.o object.o pack.hpp unpack.hpp zone.hpp object.hpp - $(CXX) test.o unpack.o zone.o object.o $(CXXFLAGS) $(LDFLAGS) -o $@ - diff --git a/cpp/test/Makefile.am b/cpp/test/Makefile.am index 2b966699..5225f28b 100644 --- a/cpp/test/Makefile.am +++ b/cpp/test/Makefile.am @@ -1,15 +1,23 @@ -AM_CPPFLAGS = -I.. -AM_C_CPPFLAGS = -I.. -AM_LDFLAGS = ../libmsgpack.la -lgtest_main +AM_CPPFLAGS = -I../src +AM_C_CPPFLAGS = -I../src +AM_LDFLAGS = ../src/libmsgpack.la -lgtest_main check_PROGRAMS = \ zone \ pack_unpack \ + pack_unpack_c \ streaming \ + streaming_c \ object \ convert \ - buffer + buffer \ + cases \ + fixint \ + fixint_c \ + version \ + msgpackc_test \ + msgpack_test TESTS = $(check_PROGRAMS) @@ -17,8 +25,12 @@ zone_SOURCES = zone.cc pack_unpack_SOURCES = pack_unpack.cc +pack_unpack_c_SOURCES = pack_unpack_c.cc + streaming_SOURCES = streaming.cc +streaming_c_SOURCES = streaming_c.cc + object_SOURCES = object.cc convert_SOURCES = convert.cc @@ -26,3 +38,17 @@ convert_SOURCES = convert.cc buffer_SOURCES = buffer.cc buffer_LDADD = -lz +cases_SOURCES = cases.cc + +fixint_SOURCES = fixint.cc + +fixint_c_SOURCES = fixint_c.cc + +version_SOURCES = version.cc + +msgpackc_test_SOURCES = msgpackc_test.cpp + +msgpack_test_SOURCES = msgpack_test.cpp + +EXTRA_DIST = cases.mpac cases_compact.mpac + diff --git a/cpp/test/buffer.cc b/cpp/test/buffer.cc index a2e90378..aff0699e 100644 --- a/cpp/test/buffer.cc +++ b/cpp/test/buffer.cc @@ -12,6 +12,14 @@ TEST(buffer, sbuffer) EXPECT_EQ(3, sbuf.size()); EXPECT_TRUE( memcmp(sbuf.data(), "aaa", 3) == 0 ); + + sbuf.clear(); + sbuf.write("a", 1); + sbuf.write("a", 1); + sbuf.write("a", 1); + + EXPECT_EQ(3, sbuf.size()); + EXPECT_TRUE( memcmp(sbuf.data(), "aaa", 3) == 0 ); } @@ -32,6 +40,23 @@ TEST(buffer, vrefbuffer) EXPECT_EQ(3, sbuf.size()); EXPECT_TRUE( memcmp(sbuf.data(), "aaa", 3) == 0 ); + + + vbuf.clear(); + vbuf.write("a", 1); + vbuf.write("a", 1); + vbuf.write("a", 1); + + vec = vbuf.vector(); + veclen = vbuf.vector_size(); + + sbuf.clear(); + for(size_t i=0; i < veclen; ++i) { + sbuf.write((const char*)vec[i].iov_base, vec[i].iov_len); + } + + EXPECT_EQ(3, sbuf.size()); + EXPECT_TRUE( memcmp(sbuf.data(), "aaa", 3) == 0 ); } diff --git a/cpp/test/cases.cc b/cpp/test/cases.cc new file mode 100644 index 00000000..eb1286c7 --- /dev/null +++ b/cpp/test/cases.cc @@ -0,0 +1,38 @@ +#include +#include +#include + +static void feed_file(msgpack::unpacker& pac, const char* path) +{ + std::ifstream fin(path); + while(true) { + pac.reserve_buffer(32*1024); + fin.read(pac.buffer(), pac.buffer_capacity()); + if(fin.bad()) { + throw std::runtime_error("read failed"); + } + pac.buffer_consumed(fin.gcount()); + if(fin.fail()) { + break; + } + } +} + +TEST(cases, format) +{ + msgpack::unpacker pac; + msgpack::unpacker pac_compact; + + feed_file(pac, "cases.mpac"); + feed_file(pac_compact, "cases_compact.mpac"); + + msgpack::unpacked result; + while(pac.next(&result)) { + msgpack::unpacked result_compact; + EXPECT_TRUE( pac_compact.next(&result_compact) ); + EXPECT_EQ(result_compact.get(), result.get()); + } + + EXPECT_FALSE( pac_compact.next(&result) ); +} + diff --git a/cpp/test/fixint.cc b/cpp/test/fixint.cc new file mode 100644 index 00000000..63288a1b --- /dev/null +++ b/cpp/test/fixint.cc @@ -0,0 +1,55 @@ +#include +#include + +template +void check_size(size_t size) { + T v(0); + msgpack::sbuffer sbuf; + msgpack::pack(sbuf, v); + EXPECT_EQ(size, sbuf.size()); +} + +TEST(fixint, size) +{ + check_size(2); + check_size(3); + check_size(5); + check_size(9); + + check_size(2); + check_size(3); + check_size(5); + check_size(9); +} + + +template +void check_convert() { + T v1(-11); + msgpack::sbuffer sbuf; + msgpack::pack(sbuf, v1); + + msgpack::unpacked msg; + msgpack::unpack(&msg, sbuf.data(), sbuf.size()); + + T v2; + msg.get().convert(&v2); + + EXPECT_EQ(v1.get(), v2.get()); + + EXPECT_EQ(msg.get(), msgpack::object(T(v1.get()))); +} + +TEST(fixint, convert) +{ + check_convert(); + check_convert(); + check_convert(); + check_convert(); + + check_convert(); + check_convert(); + check_convert(); + check_convert(); +} + diff --git a/cpp/test/fixint_c.cc b/cpp/test/fixint_c.cc new file mode 100644 index 00000000..caa4d262 --- /dev/null +++ b/cpp/test/fixint_c.cc @@ -0,0 +1,32 @@ +#include +#include + +TEST(fixint, size) +{ + msgpack_sbuffer* sbuf = msgpack_sbuffer_new(); + msgpack_packer* pk = msgpack_packer_new(sbuf, msgpack_sbuffer_write); + + size_t sum = 0; + + EXPECT_EQ(0, msgpack_pack_fix_int8(pk, 0)); + EXPECT_EQ(sum+=2, sbuf->size); + EXPECT_EQ(0, msgpack_pack_fix_int16(pk, 0)); + EXPECT_EQ(sum+=3, sbuf->size); + EXPECT_EQ(0, msgpack_pack_fix_int32(pk, 0)); + EXPECT_EQ(sum+=5, sbuf->size); + EXPECT_EQ(0, msgpack_pack_fix_int64(pk, 0)); + EXPECT_EQ(sum+=9, sbuf->size); + + EXPECT_EQ(0, msgpack_pack_fix_uint8(pk, 0)); + EXPECT_EQ(sum+=2, sbuf->size); + EXPECT_EQ(0, msgpack_pack_fix_uint16(pk, 0)); + EXPECT_EQ(sum+=3, sbuf->size); + EXPECT_EQ(0, msgpack_pack_fix_uint32(pk, 0)); + EXPECT_EQ(sum+=5, sbuf->size); + EXPECT_EQ(0, msgpack_pack_fix_uint64(pk, 0)); + EXPECT_EQ(sum+=9, sbuf->size); + + msgpack_sbuffer_free(sbuf); + msgpack_packer_free(pk); +} + diff --git a/cpp/msgpack_test.cpp b/cpp/test/msgpack_test.cpp similarity index 100% rename from cpp/msgpack_test.cpp rename to cpp/test/msgpack_test.cpp diff --git a/cpp/msgpackc_test.cpp b/cpp/test/msgpackc_test.cpp similarity index 100% rename from cpp/msgpackc_test.cpp rename to cpp/test/msgpackc_test.cpp diff --git a/cpp/test/pack_unpack.cc b/cpp/test/pack_unpack.cc index ca9b7d51..fe4625a5 100644 --- a/cpp/test/pack_unpack.cc +++ b/cpp/test/pack_unpack.cc @@ -77,21 +77,17 @@ TEST(unpack, sequence) msgpack::pack(sbuf, 2); msgpack::pack(sbuf, 3); - bool cont; size_t offset = 0; msgpack::unpacked msg; - cont = msgpack::unpack(&msg, sbuf.data(), sbuf.size(), &offset); - EXPECT_TRUE(cont); + msgpack::unpack(&msg, sbuf.data(), sbuf.size(), &offset); EXPECT_EQ(1, msg.get().as()); - cont = msgpack::unpack(&msg, sbuf.data(), sbuf.size(), &offset); - EXPECT_TRUE(cont); + msgpack::unpack(&msg, sbuf.data(), sbuf.size(), &offset); EXPECT_EQ(2, msg.get().as()); - cont = msgpack::unpack(&msg, sbuf.data(), sbuf.size(), &offset); - EXPECT_FALSE(cont); + msgpack::unpack(&msg, sbuf.data(), sbuf.size(), &offset); EXPECT_EQ(3, msg.get().as()); } diff --git a/cpp/test/pack_unpack_c.cc b/cpp/test/pack_unpack_c.cc new file mode 100644 index 00000000..e9a03892 --- /dev/null +++ b/cpp/test/pack_unpack_c.cc @@ -0,0 +1,70 @@ +#include +#include +#include + +TEST(pack, num) +{ + msgpack_sbuffer* sbuf = msgpack_sbuffer_new(); + msgpack_packer* pk = msgpack_packer_new(sbuf, msgpack_sbuffer_write); + + EXPECT_EQ(0, msgpack_pack_int(pk, 1)); + + msgpack_sbuffer_free(sbuf); + msgpack_packer_free(pk); +} + + +TEST(pack, array) +{ + msgpack_sbuffer* sbuf = msgpack_sbuffer_new(); + msgpack_packer* pk = msgpack_packer_new(sbuf, msgpack_sbuffer_write); + + EXPECT_EQ(0, msgpack_pack_array(pk, 3)); + EXPECT_EQ(0, msgpack_pack_int(pk, 1)); + EXPECT_EQ(0, msgpack_pack_int(pk, 2)); + EXPECT_EQ(0, msgpack_pack_int(pk, 3)); + + msgpack_sbuffer_free(sbuf); + msgpack_packer_free(pk); +} + + +TEST(unpack, sequence) +{ + msgpack_sbuffer* sbuf = msgpack_sbuffer_new(); + msgpack_packer* pk = msgpack_packer_new(sbuf, msgpack_sbuffer_write); + + EXPECT_EQ(0, msgpack_pack_int(pk, 1)); + EXPECT_EQ(0, msgpack_pack_int(pk, 2)); + EXPECT_EQ(0, msgpack_pack_int(pk, 3)); + + msgpack_packer_free(pk); + + bool success; + size_t offset = 0; + + msgpack_unpacked msg; + msgpack_unpacked_init(&msg); + + success = msgpack_unpack_next(&msg, sbuf->data, sbuf->size, &offset); + EXPECT_TRUE(success); + EXPECT_EQ(MSGPACK_OBJECT_POSITIVE_INTEGER, msg.data.type); + EXPECT_EQ(1, msg.data.via.u64); + + success = msgpack_unpack_next(&msg, sbuf->data, sbuf->size, &offset); + EXPECT_TRUE(success); + EXPECT_EQ(MSGPACK_OBJECT_POSITIVE_INTEGER, msg.data.type); + EXPECT_EQ(2, msg.data.via.u64); + + success = msgpack_unpack_next(&msg, sbuf->data, sbuf->size, &offset); + EXPECT_TRUE(success); + EXPECT_EQ(MSGPACK_OBJECT_POSITIVE_INTEGER, msg.data.type); + EXPECT_EQ(3, msg.data.via.u64); + + success = msgpack_unpack_next(&msg, sbuf->data, sbuf->size, &offset); + EXPECT_FALSE(success); + + msgpack_sbuffer_free(sbuf); + msgpack_unpacked_destroy(&msg); +} + diff --git a/cpp/test/streaming.cc b/cpp/test/streaming.cc index c01b8be1..e80c671b 100644 --- a/cpp/test/streaming.cc +++ b/cpp/test/streaming.cc @@ -2,28 +2,33 @@ #include #include - TEST(streaming, basic) { - std::ostringstream stream; - msgpack::packer pk(&stream); + msgpack::sbuffer buffer; + msgpack::packer pk(&buffer); pk.pack(1); pk.pack(2); pk.pack(3); - std::istringstream input(stream.str()); + const char* input = buffer.data(); + const char* const eof = input + buffer.size(); msgpack::unpacker pac; + msgpack::unpacked result; int count = 0; while(count < 3) { pac.reserve_buffer(32*1024); - size_t len = input.readsome(pac.buffer(), pac.buffer_capacity()); + // read buffer into pac.buffer() upto + // pac.buffer_capacity() bytes. + size_t len = 1; + memcpy(pac.buffer(), input, len); + input += len; + pac.buffer_consumed(len); - msgpack::unpacked result; while(pac.next(&result)) { msgpack::object obj = result.get(); switch(count++) { @@ -38,6 +43,8 @@ TEST(streaming, basic) return; } } + + EXPECT_TRUE(input < eof); } } diff --git a/cpp/test/streaming_c.cc b/cpp/test/streaming_c.cc new file mode 100644 index 00000000..6c87ac6d --- /dev/null +++ b/cpp/test/streaming_c.cc @@ -0,0 +1,57 @@ +#include +#include +#include + +TEST(streaming, basic) +{ + msgpack_sbuffer* buffer = msgpack_sbuffer_new(); + + msgpack_packer* pk = msgpack_packer_new(buffer, msgpack_sbuffer_write); + EXPECT_EQ(0, msgpack_pack_int(pk, 1)); + EXPECT_EQ(0, msgpack_pack_int(pk, 2)); + EXPECT_EQ(0, msgpack_pack_int(pk, 3)); + msgpack_packer_free(pk); + + const char* input = buffer->data; + const char* const eof = input + buffer->size; + + msgpack_unpacker pac; + msgpack_unpacker_init(&pac, MSGPACK_UNPACKER_INIT_BUFFER_SIZE); + + msgpack_unpacked result; + msgpack_unpacked_init(&result); + + int count = 0; + while(count < 3) { + msgpack_unpacker_reserve_buffer(&pac, 32*1024); + + /* read buffer into msgpack_unapcker_buffer(&pac) upto + * msgpack_unpacker_buffer_capacity(&pac) bytes. */ + size_t len = 1; + memcpy(msgpack_unpacker_buffer(&pac), input, len); + input += len; + + msgpack_unpacker_buffer_consumed(&pac, len); + + while(msgpack_unpacker_next(&pac, &result)) { + msgpack_object obj = result.data; + switch(count++) { + case 0: + EXPECT_EQ(MSGPACK_OBJECT_POSITIVE_INTEGER, result.data.type); + EXPECT_EQ(1, result.data.via.u64); + break; + case 1: + EXPECT_EQ(MSGPACK_OBJECT_POSITIVE_INTEGER, result.data.type); + EXPECT_EQ(2, result.data.via.u64); + break; + case 2: + EXPECT_EQ(MSGPACK_OBJECT_POSITIVE_INTEGER, result.data.type); + EXPECT_EQ(3, result.data.via.u64); + return; + } + } + + EXPECT_TRUE(input < eof); + } +} + diff --git a/cpp/test/version.cc b/cpp/test/version.cc new file mode 100644 index 00000000..9357271e --- /dev/null +++ b/cpp/test/version.cc @@ -0,0 +1,13 @@ +#include +#include + +TEST(version, print) +{ + printf("MSGPACK_VERSION : %s\n", MSGPACK_VERSION); + printf("MSGPACK_VERSION_MAJOR : %d\n", MSGPACK_VERSION_MAJOR); + printf("MSGPACK_VERSION_MINOR : %d\n", MSGPACK_VERSION_MINOR); + printf("msgpack_version() : %s\n", msgpack_version()); + printf("msgpack_version_major() : %d\n", msgpack_version_major()); + printf("msgpack_version_minor() : %d\n", msgpack_version_minor()); +} + diff --git a/erlang/.gitignore b/erlang/.gitignore new file mode 100644 index 00000000..0f7faad4 --- /dev/null +++ b/erlang/.gitignore @@ -0,0 +1,5 @@ +MANIFEST +*.beam +.omakedb* +*.omc +*~ \ No newline at end of file diff --git a/erlang/OMakefile b/erlang/OMakefile new file mode 100644 index 00000000..21079400 --- /dev/null +++ b/erlang/OMakefile @@ -0,0 +1,51 @@ +######################################################################## +# Permission is hereby granted, free of charge, to any person +# obtaining a copy of this file, to deal in the File without +# restriction, including without limitation the rights to use, +# copy, modify, merge, publish, distribute, sublicense, and/or +# sell copies of the File, and to permit persons to whom the +# File is furnished to do so, subject to the following condition: +# +# THE FILE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE FILE OR +# THE USE OR OTHER DEALINGS IN THE FILE. + +######################################################################## +# The standard OMakefile. +# You will usually need to modify this file for your project. + +######################################################################## +# Phony targets are scoped, so you probably want to declare them first. +# + +.PHONY: all clean test edoc dialyzer #install + +######################################################################## +# Subdirectories. +# You may want to include some subdirectories in this project. +# If so, define the subdirectory targets and uncomment this section. +# + +.DEFAULT: msgpack.beam + +msgpack.beam: msgpack.erl + erlc -Wall +debug_info $< + +msgpack.html: msgpack.erl + erl -noshell -run edoc_run file $< + +test: msgpack.beam + erl -noshell -s msgpack test -s init stop + +edoc: msgpack.erl + erl -noshell -eval 'ok=edoc:files(["msgpack.erl"], [{dir, "edoc"}]).' -s init stop + +dialyzer: msgpack.erl + dialyzer --src $< + +clean: + -rm -f *.beam *.html diff --git a/erlang/OMakeroot b/erlang/OMakeroot new file mode 100644 index 00000000..35c219da --- /dev/null +++ b/erlang/OMakeroot @@ -0,0 +1,45 @@ +######################################################################## +# Permission is hereby granted, free of charge, to any person +# obtaining a copy of this file, to deal in the File without +# restriction, including without limitation the rights to use, +# copy, modify, merge, publish, distribute, sublicense, and/or +# sell copies of the File, and to permit persons to whom the +# File is furnished to do so, subject to the following condition: +# +# THE FILE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE FILE OR +# THE USE OR OTHER DEALINGS IN THE FILE. + +######################################################################## +# The standard OMakeroot file. +# You will not normally need to modify this file. +# By default, your changes should be placed in the +# OMakefile in this directory. +# +# If you decide to modify this file, note that it uses exactly +# the same syntax as the OMakefile. +# + +# +# Include the standard installed configuration files. +# Any of these can be deleted if you are not using them, +# but you probably want to keep the Common file. +# +open build/C +open build/OCaml +open build/LaTeX + +# +# The command-line variables are defined *after* the +# standard configuration has been loaded. +# +DefineCommandVars() + +# +# Include the OMakefile in this directory. +# +.SUBDIRS: . diff --git a/erlang/README.md b/erlang/README.md new file mode 100644 index 00000000..8616d5ec --- /dev/null +++ b/erlang/README.md @@ -0,0 +1,9 @@ +MessagePack for Erlang +====================== +Binary-based efficient object serialization library. + +see wiki ( http://redmine.msgpack.org/projects/msgpack/wiki/QuickStartErlang ) for details + +# Status + +0.1.0 released. diff --git a/erlang/edoc/.gitignore b/erlang/edoc/.gitignore new file mode 100644 index 00000000..97f4246c --- /dev/null +++ b/erlang/edoc/.gitignore @@ -0,0 +1,4 @@ +*.html +*.css +*.png +edoc-info diff --git a/erlang/msgpack.erl b/erlang/msgpack.erl new file mode 100644 index 00000000..8c85458e --- /dev/null +++ b/erlang/msgpack.erl @@ -0,0 +1,395 @@ +%% +%% MessagePack for Erlang +%% +%% Copyright (C) 2009-2010 UENISHI Kota +%% +%% Licensed under the Apache License, Version 2.0 (the "License"); +%% you may not use this file except in compliance with the License. +%% You may obtain a copy of the License at +%% +%% http://www.apache.org/licenses/LICENSE-2.0 +%% +%% Unless required by applicable law or agreed to in writing, software +%% distributed under the License is distributed on an "AS IS" BASIS, +%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +%% See the License for the specific language governing permissions and +%% limitations under the License. + + +%% @doc MessagePack codec for Erlang. +%% +%% APIs are almost compatible with C API +%% except for buffering functions (both copying and zero-copying), which are unavailable. +%% +%% +%% +%% +%% +%% +%% +%% +%% +%% +%% +%%
Equivalence between Erlang and Msgpack type :
erlang msgpack
integer() pos_fixnum/neg_fixnum/uint8/uint16/uint32/uint64/int8/int16/int32/int64
float() float/double
nil nil
boolean() boolean
binary() fix_raw/raw16/raw32
list() fix_array/array16/array32
{proplist()} fix_map/map16/map32
+%% @end + +-module(msgpack). +-author('kuenishi+msgpack@gmail.com'). + +-export([pack/1, unpack/1, unpack_all/1]). + +% @type msgpack_term() = [msgpack_term()] +% | {[{msgpack_term(),msgpack_term()}]} +% | integer() | float() | binary(). +% Erlang representation of msgpack data. +-type msgpack_term() :: [msgpack_term()] + | {[{msgpack_term(),msgpack_term()}]} + | integer() | float() | binary(). + + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% external APIs +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +% @doc Encode an erlang term into an msgpack binary. +% Returns {error, {badarg, term()}} if the input is illegal. +% @spec pack(Term::msgpack_term()) -> binary() | {error, {badarg, term()}} +-spec pack(Term::msgpack_term()) -> binary() | {error, {badarg, term()}}. +pack(Term)-> + try + pack_(Term) + catch + throw:Exception -> + {error, Exception} + end. + +% @doc Decode an msgpack binary into an erlang term. +% It only decodes the first msgpack packet contained in the binary; the rest is returned as is. +% Returns {error, {badarg, term()}} if the input is corrupted. +% Returns {error, incomplete} if the input is not a full msgpack packet (caller should gather more data and try again). +% @spec unpack(Bin::binary()) -> {msgpack_term(), binary()} | {error, incomplete} | {error, {badarg, term()}} +-spec unpack(Bin::binary()) -> {msgpack_term(), binary()} | {error, incomplete} | {error, {badarg, term()}}. +unpack(Bin) when is_binary(Bin) -> + try + unpack_(Bin) + catch + throw:Exception -> + {error, Exception} + end; +unpack(Other) -> + {error, {badarg, Other}}. + +% @doc Decode an msgpack binary into an erlang terms. +% It only decodes ALL msgpack packets contained in the binary. No packets should not remain. +% Returns {error, {badarg, term()}} if the input is corrupted. +% Returns {error, incomplete} if the input is not a full msgpack packet (caller should gather more data and try again). +% @spec unpack_all(binary()) -> [msgpack_term()] | {error, incomplete} | {error, {badarg, term()}} +-spec unpack_all(binary()) -> [msgpack_term()] | {error, incomplete} | {error, {badarg, term()}}. +unpack_all(Data)-> + try + unpack_all_(Data) + catch + throw:Exception -> + {error, Exception} + end. +unpack_all_(Data)-> + case unpack_(Data) of + { Term, <<>> } -> + [Term]; + { Term, Binary } when is_binary(Binary) -> + [Term|unpack_all_(Binary)] + end. + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% internal APIs +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +% pack them all +-spec pack_(msgpack_term()) -> binary() | no_return(). +pack_(I) when is_integer(I) andalso I < 0 -> + pack_int_(I); +pack_(I) when is_integer(I) -> + pack_uint_(I); +pack_(F) when is_float(F) -> + pack_double(F); +pack_(nil) -> + << 16#C0:8 >>; +pack_(true) -> + << 16#C3:8 >>; +pack_(false) -> + << 16#C2:8 >>; +pack_(Bin) when is_binary(Bin) -> + pack_raw(Bin); +pack_(List) when is_list(List) -> + pack_array(List); +pack_({Map}) when is_list(Map) -> + pack_map(Map); +pack_(Other) -> + throw({badarg, Other}). + + +-spec pack_uint_(non_neg_integer()) -> binary(). +% positive fixnum +pack_uint_(N) when N < 128 -> + << 2#0:1, N:7 >>; +% uint 8 +pack_uint_(N) when N < 256 -> + << 16#CC:8, N:8 >>; +% uint 16 +pack_uint_(N) when N < 65536 -> + << 16#CD:8, N:16/big-unsigned-integer-unit:1 >>; +% uint 32 +pack_uint_(N) when N < 16#FFFFFFFF-> + << 16#CE:8, N:32/big-unsigned-integer-unit:1 >>; +% uint 64 +pack_uint_(N) -> + << 16#CF:8, N:64/big-unsigned-integer-unit:1 >>. + +-spec pack_int_(integer()) -> binary(). +% negative fixnum +pack_int_(N) when N >= -32-> + << 2#111:3, N:5 >>; +% int 8 +pack_int_(N) when N > -128 -> + << 16#D0:8, N:8/big-signed-integer-unit:1 >>; +% int 16 +pack_int_(N) when N > -32768 -> + << 16#D1:8, N:16/big-signed-integer-unit:1 >>; +% int 32 +pack_int_(N) when N > -16#FFFFFFFF -> + << 16#D2:8, N:32/big-signed-integer-unit:1 >>; +% int 64 +pack_int_(N) -> + << 16#D3:8, N:64/big-signed-integer-unit:1 >>. + + +-spec pack_double(float()) -> binary(). +% float : erlang's float is always IEEE 754 64bit format. +% pack_float(F) when is_float(F)-> +% << 16#CA:8, F:32/big-float-unit:1 >>. +% pack_double(F). +% double +pack_double(F) -> + << 16#CB:8, F:64/big-float-unit:1 >>. + + +-spec pack_raw(binary()) -> binary(). +% raw bytes +pack_raw(Bin) -> + case byte_size(Bin) of + Len when Len < 6-> + << 2#101:3, Len:5, Bin/binary >>; + Len when Len < 16#10000 -> % 65536 + << 16#DA:8, Len:16/big-unsigned-integer-unit:1, Bin/binary >>; + Len -> + << 16#DB:8, Len:32/big-unsigned-integer-unit:1, Bin/binary >> + end. + + +-spec pack_array([msgpack_term()]) -> binary() | no_return(). +% list +pack_array(L) -> + case length(L) of + Len when Len < 16 -> + << 2#1001:4, Len:4/integer-unit:1, (pack_array_(L, <<>>))/binary >>; + Len when Len < 16#10000 -> % 65536 + << 16#DC:8, Len:16/big-unsigned-integer-unit:1, (pack_array_(L, <<>>))/binary >>; + Len -> + << 16#DD:8, Len:32/big-unsigned-integer-unit:1, (pack_array_(L, <<>>))/binary >> + end. + +pack_array_([], Acc) -> Acc; +pack_array_([Head|Tail], Acc) -> + pack_array_(Tail, <>). + +% Users SHOULD NOT send too long list: this uses lists:reverse/1 +-spec unpack_array_(binary(), non_neg_integer(), [msgpack_term()]) -> {[msgpack_term()], binary()} | no_return(). +unpack_array_(Bin, 0, Acc) -> {lists:reverse(Acc), Bin}; +unpack_array_(Bin, Len, Acc) -> + {Term, Rest} = unpack_(Bin), + unpack_array_(Rest, Len-1, [Term|Acc]). + + +-spec pack_map(M::[{msgpack_term(),msgpack_term()}]) -> binary() | no_return(). +pack_map(M)-> + case length(M) of + Len when Len < 16 -> + << 2#1000:4, Len:4/integer-unit:1, (pack_map_(M, <<>>))/binary >>; + Len when Len < 16#10000 -> % 65536 + << 16#DE:8, Len:16/big-unsigned-integer-unit:1, (pack_map_(M, <<>>))/binary >>; + Len -> + << 16#DF:8, Len:32/big-unsigned-integer-unit:1, (pack_map_(M, <<>>))/binary >> + end. + +pack_map_([], Acc) -> Acc; +pack_map_([{Key,Value}|Tail], Acc) -> + pack_map_(Tail, << Acc/binary, (pack_(Key))/binary, (pack_(Value))/binary>>). + +% Users SHOULD NOT send too long list: this uses lists:reverse/1 +-spec unpack_map_(binary(), non_neg_integer(), [{msgpack_term(), msgpack_term()}]) -> + {{[{msgpack_term(), msgpack_term()}]}, binary()} | no_return(). +unpack_map_(Bin, 0, Acc) -> {{lists:reverse(Acc)}, Bin}; +unpack_map_(Bin, Len, Acc) -> + {Key, Rest} = unpack_(Bin), + {Value, Rest2} = unpack_(Rest), + unpack_map_(Rest2, Len-1, [{Key,Value}|Acc]). + +% unpack them all +-spec unpack_(Bin::binary()) -> {msgpack_term(), binary()} | no_return(). +unpack_(Bin) -> + case Bin of +% ATOMS + <<16#C0, Rest/binary>> -> {nil, Rest}; + <<16#C2, Rest/binary>> -> {false, Rest}; + <<16#C3, Rest/binary>> -> {true, Rest}; +% Floats + <<16#CA, V:32/float-unit:1, Rest/binary>> -> {V, Rest}; + <<16#CB, V:64/float-unit:1, Rest/binary>> -> {V, Rest}; +% Unsigned integers + <<16#CC, V:8/unsigned-integer, Rest/binary>> -> {V, Rest}; + <<16#CD, V:16/big-unsigned-integer-unit:1, Rest/binary>> -> {V, Rest}; + <<16#CE, V:32/big-unsigned-integer-unit:1, Rest/binary>> -> {V, Rest}; + <<16#CF, V:64/big-unsigned-integer-unit:1, Rest/binary>> -> {V, Rest}; +% Signed integers + <<16#D0, V:8/signed-integer, Rest/binary>> -> {V, Rest}; + <<16#D1, V:16/big-signed-integer-unit:1, Rest/binary>> -> {V, Rest}; + <<16#D2, V:32/big-signed-integer-unit:1, Rest/binary>> -> {V, Rest}; + <<16#D3, V:64/big-signed-integer-unit:1, Rest/binary>> -> {V, Rest}; +% Raw bytes + <<16#DA, L:16/unsigned-integer-unit:1, V:L/binary, Rest/binary>> -> {V, Rest}; + <<16#DB, L:32/unsigned-integer-unit:1, V:L/binary, Rest/binary>> -> {V, Rest}; +% Arrays + <<16#DC, L:16/big-unsigned-integer-unit:1, Rest/binary>> -> unpack_array_(Rest, L, []); + <<16#DD, L:32/big-unsigned-integer-unit:1, Rest/binary>> -> unpack_array_(Rest, L, []); +% Maps + <<16#DE, L:16/big-unsigned-integer-unit:1, Rest/binary>> -> unpack_map_(Rest, L, []); + <<16#DF, L:32/big-unsigned-integer-unit:1, Rest/binary>> -> unpack_map_(Rest, L, []); + +% Tag-encoded lengths (kept last, for speed) + <<0:1, V:7, Rest/binary>> -> {V, Rest}; % positive int + <<2#111:3, V:5, Rest/binary>> -> {V - 2#100000, Rest}; % negative int + <<2#101:3, L:5, V:L/binary, Rest/binary>> -> {V, Rest}; % raw bytes + <<2#1001:4, L:4, Rest/binary>> -> unpack_array_(Rest, L, []); % array + <<2#1000:4, L:4, Rest/binary>> -> unpack_map_(Rest, L, []); % map + +% Invalid data + <> when F==16#C1; + F==16#C4; F==16#C5; F==16#C6; F==16#C7; F==16#C8; F==16#C9; + F==16#D4; F==16#D5; F==16#D6; F==16#D7; F==16#D8; F==16#D9 -> + throw({badarg, <>}); +% Incomplete data (we've covered every complete/invalid case; anything left is incomplete) + _ -> + throw(incomplete) + end. + + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% unit tests +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +-include_lib("eunit/include/eunit.hrl"). +-ifdef(EUNIT). + +compare_all([], [])-> ok; +compare_all([], R)-> {toomuchrhs, R}; +compare_all(L, [])-> {toomuchlhs, L}; +compare_all([LH|LTL], [RH|RTL]) -> + ?assertEqual(LH, RH), + compare_all(LTL, RTL). + +port_receive(Port) -> + port_receive(Port, <<>>). +port_receive(Port, Acc) -> + receive + {Port, {data, Data}} -> port_receive(Port, <>); + {Port, eof} -> Acc + after 1000 -> Acc + end. + +test_([]) -> 0; +test_([Term|Rest])-> + Pack = msgpack:pack(Term), + ?assertEqual({Term, <<>>}, msgpack:unpack( Pack )), + 1+test_(Rest). + +test_data()-> + [true, false, nil, + 0, 1, 2, 123, 512, 1230, 678908, 16#FFFFFFFFFF, + -1, -23, -512, -1230, -567898, -16#FFFFFFFFFF, + 123.123, -234.4355, 1.0e-34, 1.0e64, + [23, 234, 0.23], + <<"hogehoge">>, <<"243546rf7g68h798j", 0, 23, 255>>, + <<"hoasfdafdas][">>, + [0,42, <<"sum">>, [1,2]], [1,42, nil, [3]], + -234, -40000, -16#10000000, -16#100000000, + 42 + ]. + +basic_test()-> + Tests = test_data(), + Passed = test_(Tests), + Passed = length(Tests). + +port_test()-> + Tests = test_data(), + ?assertEqual({[Tests],<<>>}, msgpack:unpack(msgpack:pack([Tests]))), + + Port = open_port({spawn, "ruby ../test/crosslang.rb"}, [binary, eof]), + true = port_command(Port, msgpack:pack(Tests)), + ?assertEqual({Tests, <<>>}, msgpack:unpack(port_receive(Port))), + port_close(Port). + +test_p(Len,Term,OrigBin,Len) -> + {Term, <<>>}=msgpack:unpack(OrigBin); +test_p(I,_,OrigBin,Len) when I < Len-> + <> = OrigBin, + ?assertEqual({error,incomplete}, msgpack:unpack(Bin)). + +partial_test()-> % error handling test. + Term = lists:seq(0, 45), + Bin=msgpack:pack(Term), + BinLen = byte_size(Bin), + [test_p(X, Term, Bin, BinLen) || X <- lists:seq(0,BinLen)]. + +long_test()-> + Longer = lists:seq(0, 655), + {Longer, <<>>} = msgpack:unpack(msgpack:pack(Longer)). + +map_test()-> + Ints = lists:seq(0, 65), + Map = {[ {X, X*2} || X <- Ints ] ++ [{<<"hage">>, 324}, {43542, [nil, true, false]}]}, + {Map2, <<>>} = msgpack:unpack(msgpack:pack(Map)), + ?assertEqual(Map, Map2), + ok. + +unknown_test()-> + Port = open_port({spawn, "ruby testcase_generator.rb"}, [binary, eof]), + Tests = [0, 1, 2, 123, 512, 1230, 678908, + -1, -23, -512, -1230, -567898, + <<"hogehoge">>, <<"243546rf7g68h798j">>, + 123.123, + -234.4355, 1.0e-34, 1.0e64, + [23, 234, 0.23], + [0,42,<<"sum">>, [1,2]], [1,42, nil, [3]], + {[{1,2},{<<"hoge">>,nil}]}, % map + -234, -50000, + 42 + ], + ?assertEqual(ok, compare_all(Tests, msgpack:unpack_all(port_receive(Port)))), + port_close(Port). + +other_test()-> + ?assertEqual({error,incomplete},msgpack:unpack(<<>>)). + +benchmark_test()-> + Data=[test_data() || _ <- lists:seq(0, 10000)], + S=?debugTime(" serialize", msgpack:pack(Data)), + {Data,<<>>}=?debugTime("deserialize", msgpack:unpack(S)), + ?debugFmt("for ~p KB test data.", [byte_size(S) div 1024]). + +error_test()-> + ?assertEqual({error,{badarg, atom}}, msgpack:pack(atom)), + Term = {"hoge", "hage", atom}, + ?assertEqual({error,{badarg, Term}}, msgpack:pack(Term)). + +-endif. diff --git a/erlang/testcase_generator.rb b/erlang/testcase_generator.rb new file mode 100644 index 00000000..8445bdd5 --- /dev/null +++ b/erlang/testcase_generator.rb @@ -0,0 +1,65 @@ +begin +require 'rubygems' +rescue LoadError +end +require 'msgpack' + +def usage + puts < (default: stdout) + +EOF + exit 1 +end + +code = 1 +outio = $stdout + +if ARGV.length > 2 + usage +end + +if fname = ARGV[0] + unless fname == "-" + begin + outio = File.open(fname, "w") + rescue + puts "can't open output file: #{$!}" + exit 1 + end + end +end + +objs = [0, 1, 2, 123, 512, 1230, 678908, + -1, -23, -512, -1230, -567898, + "hogehoge", "243546rf7g68h798j", + 123.123, + -234.4355, 1.0e-34, 1.0e64, + [23, 234, 0.23], + [0,42,"sum", [1,2]], [1,42, nil, [3]], + { 1 => 2, "hoge" => nil }, + -234, -50000, + 42 + ] +begin + objs.each do |obj| + outio.write MessagePack.pack(obj) + outio.flush + end +rescue EOFError + code=0 +rescue + $stderr.puts $! + code=1 +end + +outio.close +exit code + diff --git a/haskell/msgpack.cabal b/haskell/msgpack.cabal index 31cad3bc..82cdb525 100644 --- a/haskell/msgpack.cabal +++ b/haskell/msgpack.cabal @@ -1,5 +1,5 @@ Name: msgpack -Version: 0.2.1 +Version: 0.2.2 License: BSD3 License-File: LICENSE Author: Hideyuki Tanaka diff --git a/haskell/src/Data/MessagePack/Base.hsc b/haskell/src/Data/MessagePack/Base.hsc index 72c421ce..b6cdc287 100644 --- a/haskell/src/Data/MessagePack/Base.hsc +++ b/haskell/src/Data/MessagePack/Base.hsc @@ -506,20 +506,22 @@ peekObjectRAW ptr = do peekObjectArray :: Ptr a -> IO Object peekObjectArray ptr = do - size <- (#peek msgpack_object, via.array.size) ptr - p <- (#peek msgpack_object, via.array.ptr) ptr - objs <- mapM (\i -> peekObject $ p `plusPtr` + csize <- (#peek msgpack_object, via.array.size) ptr + let size = fromIntegral (csize :: Word32) + p <- (#peek msgpack_object, via.array.ptr) ptr + objs <- mapM (\i -> peekObject $ p `plusPtr` ((#size msgpack_object) * i)) - [0..size-1] + [0..size-1] return $ ObjectArray objs peekObjectMap :: Ptr a -> IO Object peekObjectMap ptr = do - size <- (#peek msgpack_object, via.map.size) ptr - p <- (#peek msgpack_object, via.map.ptr) ptr - dat <- mapM (\i -> peekObjectKV $ p `plusPtr` + csize <- (#peek msgpack_object, via.map.size) ptr + let size = fromIntegral (csize :: Word32) + p <- (#peek msgpack_object, via.map.ptr) ptr + dat <- mapM (\i -> peekObjectKV $ p `plusPtr` ((#size msgpack_object_kv) * i)) - [0..size-1] + [0..size-1] return $ ObjectMap dat peekObjectKV :: Ptr a -> IO (Object, Object) @@ -560,6 +562,7 @@ unpackObject z dat = allocaBytes (#size msgpack_object) $ \ptr -> BS.useAsCStringLen dat $ \(str, len) -> alloca $ \poff -> do + poke poff 0 ret <- msgpack_unpack str (fromIntegral len) poff z ptr case ret of (#const MSGPACK_UNPACK_SUCCESS) -> do diff --git a/haskell/src/Data/MessagePack/Monad.hs b/haskell/src/Data/MessagePack/Monad.hs index c718b8ad..15f21fe0 100644 --- a/haskell/src/Data/MessagePack/Monad.hs +++ b/haskell/src/Data/MessagePack/Monad.hs @@ -82,7 +82,7 @@ packToString m = do _ <- runPackerT m pc liftIO $ simpleBufferData sb --- | Execcute given serializer and write byte sequence to Handle. +-- | Execute given serializer and write byte sequence to Handle. packToHandle :: MonadIO m => Handle -> PackerT m r -> m () packToHandle h m = do sb <- packToString m diff --git a/java/CHANGES.txt b/java/CHANGES.txt new file mode 100644 index 00000000..77e93188 --- /dev/null +++ b/java/CHANGES.txt @@ -0,0 +1,9 @@ +Release 0.3 - 2010/05/23 + NEW FEATURES + Added Unbuffered API + Direct Conversion API to the Unpacker. + + BUG FIXES + Zero-length Array and Map is deserialized as List and Map, instead of the + array of the Object. + + fixed the bug around Packer.packByte(). diff --git a/java/LICENSE.txt b/java/LICENSE.txt new file mode 100644 index 00000000..d6456956 --- /dev/null +++ b/java/LICENSE.txt @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/java/pom.xml b/java/pom.xml index d1f6c34b..44806d51 100755 --- a/java/pom.xml +++ b/java/pom.xml @@ -3,11 +3,11 @@ 4.0.0 org.msgpack msgpack - 0.2 + 0.3 MessagePack for Java MessagePack for Java - http://msgpack.sourceforge.net/ + http://msgpack.org/ @@ -97,29 +97,24 @@ - msgpack.sourceforge.net + msgpack.org MessagePack Maven2 Repository - http://msgpack.sourceforge.net/maven2 - - - msgpack.sourceforge.net - MessagePack Maven2 Snapshot Repository - http://msgpack.sourceforge.net/maven2-snapshot + http://msgpack.org/maven2 false - shell.sourceforge.net - Repository at sourceforge.net - scp://shell.sourceforge.net/home/groups/m/ms/msgpack/htdocs/maven2/ + msgpack.org + Repository at msgpack.org + file://${project.build.directory}/website/maven2/ true - shell.sourceforge.net - Repository Name - scp://shell.sourceforge.net/home/groups/m/ms/msgpack/htdocs/maven2-snapshot/ + msgpack.org + Repository at msgpack.org + file://${project.build.directory}/website/maven2/ diff --git a/java/src/main/java/org/msgpack/BufferedUnpackerImpl.java b/java/src/main/java/org/msgpack/BufferedUnpackerImpl.java new file mode 100644 index 00000000..5b449c79 --- /dev/null +++ b/java/src/main/java/org/msgpack/BufferedUnpackerImpl.java @@ -0,0 +1,445 @@ +// +// MessagePack for Java +// +// Copyright (C) 2009-2010 FURUHASHI Sadayuki +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +package org.msgpack; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.math.BigInteger; + +abstract class BufferedUnpackerImpl extends UnpackerImpl { + int offset = 0; + int filled = 0; + byte[] buffer = null; + private ByteBuffer castBuffer = ByteBuffer.allocate(8); + + abstract boolean fill() throws IOException; + + final boolean next(UnpackResult result) throws IOException, UnpackException { + if(filled == 0) { + if(!fill()) { + return false; + } + } + + do { + int noffset = super.execute(buffer, offset, filled); + if(noffset <= offset) { + if(!fill()) { + return false; + } + continue; + } + offset = noffset; + } while(!super.isFinished()); + + MessagePackObject obj = super.getData(); + super.reset(); + result.done(obj); + + return true; + } + + private final void more(int require) throws IOException, UnpackException { + while(filled - offset < require) { + if(!fill()) { + // FIXME + throw new UnpackException("insufficient buffer"); + } + } + } + + private final boolean tryMore(int require) throws IOException, UnpackException { + while(filled - offset < require) { + if(!fill()) { + return false; + } + } + return true; + } + + private final void advance(int length) { + offset += length; + } + + final byte unpackByte() throws IOException, MessageTypeException { + int o = unpackInt(); + if(0x7f < o || o < -0x80) { + throw new MessageTypeException(); + } + return (byte)o; + } + + final short unpackShort() throws IOException, MessageTypeException { + int o = unpackInt(); + if(0x7fff < o || o < -0x8000) { + throw new MessageTypeException(); + } + return (short)o; + } + + final int unpackInt() throws IOException, MessageTypeException { + more(1); + int b = buffer[offset]; + if((b & 0x80) == 0 || (b & 0xe0) == 0xe0) { // Fixnum + advance(1); + return (int)b; + } + switch(b & 0xff) { + case 0xcc: // unsigned int 8 + more(2); + advance(2); + return (int)((short)(buffer[offset-1]) & 0xff); + case 0xcd: // unsigned int 16 + more(3); + castBuffer.rewind(); + castBuffer.put(buffer, offset+1, 2); + advance(3); + return (int)((int)castBuffer.getShort(0) & 0xffff); + case 0xce: // unsigned int 32 + more(5); + castBuffer.rewind(); + castBuffer.put(buffer, offset+1, 4); + { + int o = castBuffer.getInt(0); + if(o < 0) { + throw new MessageTypeException(); + } + advance(5); + return o; + } + case 0xcf: // unsigned int 64 + more(9); + castBuffer.rewind(); + castBuffer.put(buffer, offset+1, 8); + { + long o = castBuffer.getLong(0); + if(o < 0 || o > 0x7fffffffL) { + throw new MessageTypeException(); + } + advance(9); + return (int)o; + } + case 0xd0: // signed int 8 + more(2); + advance(2); + return (int)buffer[offset-1]; + case 0xd1: // signed int 16 + more(3); + castBuffer.rewind(); + castBuffer.put(buffer, offset+1, 2); + advance(3); + return (int)castBuffer.getShort(0); + case 0xd2: // signed int 32 + more(4); + castBuffer.rewind(); + castBuffer.put(buffer, offset+1, 4); + advance(4); + return (int)castBuffer.getInt(0); + case 0xd3: // signed int 64 + more(9); + castBuffer.rewind(); + castBuffer.put(buffer, offset+1, 8); + { + long o = castBuffer.getLong(0); + if(0x7fffffffL < o || o < -0x80000000L) { + throw new MessageTypeException(); + } + advance(9); + return (int)o; + } + default: + throw new MessageTypeException(); + } + } + + final long unpackLong() throws IOException, MessageTypeException { + more(1); + int b = buffer[offset]; + if((b & 0x80) == 0 || (b & 0xe0) == 0xe0) { // Fixnum + advance(1); + return (long)b; + } + switch(b & 0xff) { + case 0xcc: // unsigned int 8 + more(2); + advance(2); + return (long)((short)(buffer[offset-1]) & 0xff); + case 0xcd: // unsigned int 16 + more(3); + castBuffer.rewind(); + castBuffer.put(buffer, offset+1, 2); + advance(3); + return (long)((int)castBuffer.getShort(0) & 0xffff); + case 0xce: // unsigned int 32 + more(5); + castBuffer.rewind(); + castBuffer.put(buffer, offset+1, 4); + advance(5); + return ((long)castBuffer.getInt(0) & 0xffffffffL); + case 0xcf: // unsigned int 64 + more(9); + castBuffer.rewind(); + castBuffer.put(buffer, offset+1, 8); + { + long o = castBuffer.getLong(0); + if(o < 0) { + throw new MessageTypeException(); + } + advance(9); + return o; + } + case 0xd0: // signed int 8 + more(2); + advance(2); + return (long)buffer[offset-1]; + case 0xd1: // signed int 16 + more(3); + castBuffer.rewind(); + castBuffer.put(buffer, offset+1, 2); + advance(3); + return (long)castBuffer.getShort(0); + case 0xd2: // signed int 32 + more(4); + castBuffer.rewind(); + castBuffer.put(buffer, offset+1, 4); + advance(4); + return (long)castBuffer.getInt(0); + case 0xd3: // signed int 64 + more(9); + castBuffer.rewind(); + castBuffer.put(buffer, offset+1, 8); + advance(9); + return (long)castBuffer.getLong(0); + default: + throw new MessageTypeException(); + } + } + + final BigInteger unpackBigInteger() throws IOException, MessageTypeException { + more(1); + int b = buffer[offset]; + if((b & 0xff) != 0xcf) { + return BigInteger.valueOf(unpackLong()); + } + + // unsigned int 64 + more(9); + castBuffer.rewind(); + castBuffer.put(buffer, offset+1, 8); + advance(9); + long o = castBuffer.getLong(0); + if(o < 0) { + return new BigInteger(1, castBuffer.array()); + } else { + return BigInteger.valueOf(o); + } + } + + final float unpackFloat() throws IOException, MessageTypeException { + more(1); + int b = buffer[offset]; + switch(b & 0xff) { + case 0xca: // float + more(5); + castBuffer.rewind(); + castBuffer.put(buffer, offset+1, 4); + advance(5); + return castBuffer.getFloat(0); + case 0xcb: // double + more(9); + castBuffer.rewind(); + castBuffer.put(buffer, offset+1, 8); + advance(9); + // FIXME overflow check + return (float)castBuffer.getDouble(0); + default: + throw new MessageTypeException(); + } + } + + final double unpackDouble() throws IOException, MessageTypeException { + more(1); + int b = buffer[offset]; + switch(b & 0xff) { + case 0xca: // float + more(5); + castBuffer.rewind(); + castBuffer.put(buffer, offset+1, 4); + advance(5); + return (double)castBuffer.getFloat(0); + case 0xcb: // double + more(9); + castBuffer.rewind(); + castBuffer.put(buffer, offset+1, 8); + advance(9); + return castBuffer.getDouble(0); + default: + throw new MessageTypeException(); + } + } + + final Object unpackNull() throws IOException, MessageTypeException { + more(1); + int b = buffer[offset] & 0xff; + if(b != 0xc0) { // nil + throw new MessageTypeException(); + } + advance(1); + return null; + } + + final boolean tryUnpackNull() throws IOException { + if(!tryMore(1)) { + return false; + } + int b = buffer[offset] & 0xff; + if(b != 0xc0) { // nil + return false; + } + advance(1); + return true; + } + + final boolean unpackBoolean() throws IOException, MessageTypeException { + more(1); + int b = buffer[offset] & 0xff; + if(b == 0xc2) { // false + advance(1); + return false; + } else if(b == 0xc3) { // true + advance(1); + return true; + } else { + throw new MessageTypeException(); + } + } + + final int unpackArray() throws IOException, MessageTypeException { + more(1); + int b = buffer[offset]; + if((b & 0xf0) == 0x90) { // FixArray + advance(1); + return (int)(b & 0x0f); + } + switch(b & 0xff) { + case 0xdc: // array 16 + more(3); + castBuffer.rewind(); + castBuffer.put(buffer, offset+1, 2); + advance(3); + return (int)castBuffer.getShort(0) & 0xffff; + case 0xdd: // array 32 + more(5); + castBuffer.rewind(); + castBuffer.put(buffer, offset+1, 4); + advance(5); + // FIXME overflow check + return castBuffer.getInt(0) & 0x7fffffff; + default: + throw new MessageTypeException(); + } + } + + final int unpackMap() throws IOException, MessageTypeException { + more(1); + int b = buffer[offset]; + if((b & 0xf0) == 0x80) { // FixMap + advance(1); + return (int)(b & 0x0f); + } + switch(b & 0xff) { + case 0xde: // map 16 + more(3); + castBuffer.rewind(); + castBuffer.put(buffer, offset+1, 2); + advance(3); + return (int)castBuffer.getShort(0) & 0xffff; + case 0xdf: // map 32 + more(5); + castBuffer.rewind(); + castBuffer.put(buffer, offset+1, 4); + advance(5); + // FIXME overflow check + return castBuffer.getInt(0) & 0x7fffffff; + default: + throw new MessageTypeException(); + } + } + + final int unpackRaw() throws IOException, MessageTypeException { + more(1); + int b = buffer[offset]; + if((b & 0xe0) == 0xa0) { // FixRaw + advance(1); + return (int)(b & 0x1f); + } + switch(b & 0xff) { + case 0xda: // raw 16 + more(3); + castBuffer.rewind(); + castBuffer.put(buffer, offset+1, 2); + advance(3); + return (int)castBuffer.getShort(0) & 0xffff; + case 0xdb: // raw 32 + more(5); + castBuffer.rewind(); + castBuffer.put(buffer, offset+1, 4); + advance(5); + // FIXME overflow check + return castBuffer.getInt(0) & 0x7fffffff; + default: + throw new MessageTypeException(); + } + } + + final byte[] unpackRawBody(int length) throws IOException { + more(length); + byte[] bytes = new byte[length]; + System.arraycopy(buffer, offset, bytes, 0, length); + advance(length); + return bytes; + } + + final byte[] unpackByteArray() throws IOException, MessageTypeException { + int length = unpackRaw(); + return unpackRawBody(length); + } + + final String unpackString() throws IOException, MessageTypeException { + int length = unpackRaw(); + more(length); + String s; + try { + s = new String(buffer, offset, length, "UTF-8"); + } catch (Exception e) { + throw new MessageTypeException(); + } + advance(length); + return s; + } + + final MessagePackObject unpackObject() throws IOException { + UnpackResult result = new UnpackResult(); + if(!next(result)) { + super.reset(); + throw new UnpackException("insufficient buffer"); + } + return result.getData(); + } +} + diff --git a/java/src/main/java/org/msgpack/MessageMergeable.java b/java/src/main/java/org/msgpack/MessageConvertable.java similarity index 85% rename from java/src/main/java/org/msgpack/MessageMergeable.java rename to java/src/main/java/org/msgpack/MessageConvertable.java index e5a5b451..8acf1f28 100644 --- a/java/src/main/java/org/msgpack/MessageMergeable.java +++ b/java/src/main/java/org/msgpack/MessageConvertable.java @@ -17,7 +17,7 @@ // package org.msgpack; -public interface MessageMergeable { - public void messageMerge(Object obj) throws MessageTypeException; +public interface MessageConvertable { + public void messageConvert(MessagePackObject obj) throws MessageTypeException; } diff --git a/java/src/main/java/org/msgpack/MessagePackObject.java b/java/src/main/java/org/msgpack/MessagePackObject.java new file mode 100644 index 00000000..2424446f --- /dev/null +++ b/java/src/main/java/org/msgpack/MessagePackObject.java @@ -0,0 +1,136 @@ +// +// MessagePack for Java +// +// Copyright (C) 2009-2010 FURUHASHI Sadayuki +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +package org.msgpack; + +import java.util.List; +import java.util.Set; +import java.util.Map; +import java.math.BigInteger; + +public abstract class MessagePackObject implements Cloneable, MessagePackable { + public boolean isNil() { + return false; + } + + public boolean isBooleanType() { + return false; + } + + public boolean isIntegerType() { + return false; + } + + public boolean isFloatType() { + return false; + } + + public boolean isArrayType() { + return false; + } + + public boolean isMapType() { + return false; + } + + public boolean isRawType() { + return false; + } + + public boolean asBoolean() { + throw new MessageTypeException("type error"); + } + + public byte asByte() { + throw new MessageTypeException("type error"); + } + + public short asShort() { + throw new MessageTypeException("type error"); + } + + public int asInt() { + throw new MessageTypeException("type error"); + } + + public long asLong() { + throw new MessageTypeException("type error"); + } + + public BigInteger asBigInteger() { + throw new MessageTypeException("type error"); + } + + public float asFloat() { + throw new MessageTypeException("type error"); + } + + public double asDouble() { + throw new MessageTypeException("type error"); + } + + public byte[] asByteArray() { + throw new MessageTypeException("type error"); + } + + public String asString() { + throw new MessageTypeException("type error"); + } + + public MessagePackObject[] asArray() { + throw new MessageTypeException("type error"); + } + + public List asList() { + throw new MessageTypeException("type error"); + } + + public Map asMap() { + throw new MessageTypeException("type error"); + } + + public byte byteValue() { + throw new MessageTypeException("type error"); + } + + public short shortValue() { + throw new MessageTypeException("type error"); + } + + public int intValue() { + throw new MessageTypeException("type error"); + } + + public long longValue() { + throw new MessageTypeException("type error"); + } + + public BigInteger bigIntegerValue() { + throw new MessageTypeException("type error"); + } + + public float floatValue() { + throw new MessageTypeException("type error"); + } + + public double doubleValue() { + throw new MessageTypeException("type error"); + } + + abstract public Object clone(); +} + diff --git a/java/src/main/java/org/msgpack/MessageTypeException.java b/java/src/main/java/org/msgpack/MessageTypeException.java index feb6c088..698ef6dd 100644 --- a/java/src/main/java/org/msgpack/MessageTypeException.java +++ b/java/src/main/java/org/msgpack/MessageTypeException.java @@ -17,23 +17,11 @@ // package org.msgpack; -import java.io.IOException; - -public class MessageTypeException extends IOException { +public class MessageTypeException extends RuntimeException { public MessageTypeException() { } public MessageTypeException(String s) { super(s); } - - public static MessageTypeException invalidConvert(Object from, Schema to) { - return new MessageTypeException(from.getClass().getName()+" cannot be convert to "+to.getExpression()); - } - - /* FIXME - public static MessageTypeException schemaMismatch(Schema to) { - return new MessageTypeException("schema mismatch "+to.getExpression()); - } - */ } diff --git a/java/src/main/java/org/msgpack/schema/IArraySchema.java b/java/src/main/java/org/msgpack/MessageUnpackable.java similarity index 79% rename from java/src/main/java/org/msgpack/schema/IArraySchema.java rename to java/src/main/java/org/msgpack/MessageUnpackable.java index 67e9f55a..cc206e7a 100644 --- a/java/src/main/java/org/msgpack/schema/IArraySchema.java +++ b/java/src/main/java/org/msgpack/MessageUnpackable.java @@ -15,12 +15,11 @@ // See the License for the specific language governing permissions and // limitations under the License. // -package org.msgpack.schema; +package org.msgpack; -import org.msgpack.Schema; +import java.io.IOException; -public interface IArraySchema { - public Schema getElementSchema(int index); - public Object createFromArray(Object[] obj); +public interface MessageUnpackable { + public void messageUnpack(Unpacker pac) throws IOException, MessageTypeException; } diff --git a/java/src/main/java/org/msgpack/Packer.java b/java/src/main/java/org/msgpack/Packer.java index 935728d2..139b3b15 100644 --- a/java/src/main/java/org/msgpack/Packer.java +++ b/java/src/main/java/org/msgpack/Packer.java @@ -22,7 +22,24 @@ import java.io.IOException; import java.nio.ByteBuffer; import java.util.List; import java.util.Map; +import java.math.BigInteger; +/** + * Packer enables you to serialize objects into OutputStream. + * + *
+ * // create a packer with output stream
+ * Packer pk = new Packer(System.out);
+ *
+ * // store an object with pack() method.
+ * pk.pack(1);
+ *
+ * // you can store String, List, Map, byte[] and primitive types.
+ * pk.pack(new ArrayList());
+ * 
+ * + * You can serialize objects that implements {@link MessagePackable} interface. + */ public class Packer { protected byte[] castBytes = new byte[9]; protected ByteBuffer castBuffer = ByteBuffer.wrap(castBytes); @@ -34,7 +51,7 @@ public class Packer { public Packer packByte(byte d) throws IOException { if(d < -(1<<5)) { - castBytes[0] = (byte)0xd1; + castBytes[0] = (byte)0xd0; castBytes[1] = d; out.write(castBytes, 0, 2); } else { @@ -178,6 +195,27 @@ public class Packer { return this; } + public Packer packBigInteger(BigInteger d) throws IOException { + if(d.bitLength() <= 63) { + return packLong(d.longValue()); + } else if(d.bitLength() <= 64 && d.signum() >= 0) { + castBytes[0] = (byte)0xcf; + byte[] barray = d.toByteArray(); + castBytes[1] = barray[barray.length-8]; + castBytes[2] = barray[barray.length-7]; + castBytes[3] = barray[barray.length-6]; + castBytes[4] = barray[barray.length-5]; + castBytes[5] = barray[barray.length-4]; + castBytes[6] = barray[barray.length-3]; + castBytes[7] = barray[barray.length-2]; + castBytes[8] = barray[barray.length-1]; + out.write(castBytes); + return this; + } else { + throw new MessageTypeException("can't pack BigInteger larger than 0xffffffffffffffff"); + } + } + public Packer packFloat(float d) throws IOException { castBytes[0] = (byte)0xca; castBuffer.putFloat(1, d); @@ -207,6 +245,10 @@ public class Packer { return this; } + public Packer packBoolean(boolean d) throws IOException { + return d ? packTrue() : packFalse(); + } + public Packer packArray(int n) throws IOException { if(n < 16) { final int d = 0x90 | n; @@ -266,12 +308,6 @@ public class Packer { } - public Packer packWithSchema(Object o, Schema s) throws IOException { - s.pack(this, o); - return this; - } - - public Packer packString(String s) throws IOException { byte[] b = ((String)s).getBytes("UTF-8"); packRaw(b.length); @@ -279,39 +315,36 @@ public class Packer { } - public Packer pack(String o) throws IOException { - if(o == null) { return packNil(); } - return packString(o); - } - - public Packer pack(MessagePackable o) throws IOException { - if(o == null) { return packNil(); } - o.messagePack(this); - return this; - } - - public Packer pack(byte[] o) throws IOException { - if(o == null) { return packNil(); } - packRaw(o.length); - return packRawBody(o); - } - - public Packer pack(List o) throws IOException { - if(o == null) { return packNil(); } - packArray(o.size()); - for(Object i : o) { pack(i); } - return this; - } - - @SuppressWarnings("unchecked") - public Packer pack(Map o) throws IOException { - if(o == null) { return packNil(); } - packMap(o.size()); - for(Map.Entry e : ((Map)o).entrySet()) { - pack(e.getKey()); - pack(e.getValue()); + public Packer pack(boolean o) throws IOException { + if(o) { + return packTrue(); + } else { + return packFalse(); } - return this; + } + + public Packer pack(byte o) throws IOException { + return packByte(o); + } + + public Packer pack(short o) throws IOException { + return packShort(o); + } + + public Packer pack(int o) throws IOException { + return packInt(o); + } + + public Packer pack(long o) throws IOException { + return packLong(o); + } + + public Packer pack(float o) throws IOException { + return packFloat(o); + } + + public Packer pack(double o) throws IOException { + return packDouble(o); } public Packer pack(Boolean o) throws IOException { @@ -343,6 +376,11 @@ public class Packer { return packLong(o); } + public Packer pack(BigInteger o) throws IOException { + if(o == null) { return packNil(); } + return packBigInteger(o); + } + public Packer pack(Float o) throws IOException { if(o == null) { return packNil(); } return packFloat(o); @@ -353,8 +391,41 @@ public class Packer { return packDouble(o); } + public Packer pack(String o) throws IOException { + if(o == null) { return packNil(); } + return packString(o); + } + + public Packer pack(MessagePackable o) throws IOException { + if(o == null) { return packNil(); } + o.messagePack(this); + return this; + } + + public Packer pack(byte[] o) throws IOException { + if(o == null) { return packNil(); } + packRaw(o.length); + return packRawBody(o); + } + + public Packer pack(List o) throws IOException { + if(o == null) { return packNil(); } + packArray(o.size()); + for(Object i : o) { pack(i); } + return this; + } + + public Packer pack(Map o) throws IOException { + if(o == null) { return packNil(); } + packMap(o.size()); + for(Map.Entry e : ((Map)o).entrySet()) { + pack(e.getKey()); + pack(e.getValue()); + } + return this; + } + - @SuppressWarnings("unchecked") public Packer pack(Object o) throws IOException { if(o == null) { return packNil(); @@ -377,7 +448,7 @@ public class Packer { } else if(o instanceof Map) { Map m = (Map)o; packMap(m.size()); - for(Map.Entry e : m.entrySet()) { + for(Map.Entry e : m.entrySet()) { pack(e.getKey()); pack(e.getValue()); } @@ -400,8 +471,10 @@ public class Packer { return packFloat((Float)o); } else if(o instanceof Double) { return packDouble((Double)o); + } else if(o instanceof BigInteger) { + return packBigInteger((BigInteger)o); } else { - throw new IOException("unknown object "+o+" ("+o.getClass()+")"); + throw new MessageTypeException("unknown object "+o+" ("+o.getClass()+")"); } } } diff --git a/java/src/main/java/org/msgpack/Schema.java b/java/src/main/java/org/msgpack/Schema.java deleted file mode 100644 index f191f7a9..00000000 --- a/java/src/main/java/org/msgpack/Schema.java +++ /dev/null @@ -1,133 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack; - -import java.io.Writer; -import java.io.IOException; -import org.msgpack.schema.SSchemaParser; -import org.msgpack.schema.ClassGenerator; - -public abstract class Schema { - private String expression; - private String name; - - public Schema(String name) { - this.expression = expression; - this.name = name; - } - - public String getName() { - return name; - } - - public String getFullName() { - return name; - } - - public String getExpression() { - return name; - } - - public static Schema parse(String source) { - return SSchemaParser.parse(source); - } - - public static Schema load(String source) { - return SSchemaParser.load(source); - } - - public void write(Writer output) throws IOException { - ClassGenerator.write(this, output); - } - - public abstract void pack(Packer pk, Object obj) throws IOException; - - public abstract Object convert(Object obj) throws MessageTypeException; - - - public Object createFromNil() { - return null; - } - - public Object createFromBoolean(boolean v) { - throw new RuntimeException("type error"); - } - - public Object createFromByte(byte v) { - throw new RuntimeException("type error"); - } - - public Object createFromShort(short v) { - throw new RuntimeException("type error"); - } - - public Object createFromInt(int v) { - throw new RuntimeException("type error"); - } - - public Object createFromLong(long v) { - throw new RuntimeException("type error"); - } - - public Object createFromFloat(float v) { - throw new RuntimeException("type error"); - } - - public Object createFromDouble(double v) { - throw new RuntimeException("type error"); - } - - public Object createFromRaw(byte[] b, int offset, int length) { - throw new RuntimeException("type error"); - } - - /* FIXME - public Object createFromBoolean(boolean v) { - throw MessageTypeException.schemaMismatch(this); - } - - public Object createFromByte(byte v) { - throw MessageTypeException.schemaMismatch(this); - } - - public Object createFromShort(short v) { - throw MessageTypeException.schemaMismatch(this); - } - - public Object createFromInt(int v) { - throw MessageTypeException.schemaMismatch(this); - } - - public Object createFromLong(long v) { - throw MessageTypeException.schemaMismatch(this); - } - - public Object createFromFloat(float v) { - throw MessageTypeException.schemaMismatch(this); - } - - public Object createFromDouble(double v) { - throw MessageTypeException.schemaMismatch(this); - } - - public Object createFromRaw(byte[] b, int offset, int length) { - throw MessageTypeException.schemaMismatch(this); - } - */ -} - diff --git a/java/src/main/java/org/msgpack/UnbufferedUnpacker.java b/java/src/main/java/org/msgpack/UnbufferedUnpacker.java deleted file mode 100644 index b4279730..00000000 --- a/java/src/main/java/org/msgpack/UnbufferedUnpacker.java +++ /dev/null @@ -1,82 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack; - -import java.lang.Iterable; -import java.io.InputStream; -import java.io.IOException; -import java.util.Iterator; -import org.msgpack.impl.UnpackerImpl; - -public class UnbufferedUnpacker extends UnpackerImpl { - private int offset; - private boolean finished; - private Object data; - - public UnbufferedUnpacker() { - super(); - this.offset = 0; - this.finished = false; - } - - public UnbufferedUnpacker useSchema(Schema s) { - super.setSchema(s); - return this; - } - - public Object getData() { - return data; - } - - public boolean isFinished() { - return finished; - } - - public void reset() { - super.reset(); - this.offset = 0; - } - - int getOffset() { - return offset; - } - - void setOffset(int offset) { - this.offset = offset; - } - - public int execute(byte[] buffer) throws UnpackException { - return execute(buffer, 0, buffer.length); - } - - // FIXME - public int execute(byte[] buffer, int offset, int length) throws UnpackException - { - int noffset = super.execute(buffer, offset + this.offset, length); - this.offset = noffset - offset; - if(super.isFinished()) { - this.data = super.getData(); - this.finished = true; - super.reset(); - } else { - this.finished = false; - } - return noffset; - } -} - diff --git a/java/src/main/java/org/msgpack/UnpackIterator.java b/java/src/main/java/org/msgpack/UnpackIterator.java index 0a78e83a..8c778b67 100644 --- a/java/src/main/java/org/msgpack/UnpackIterator.java +++ b/java/src/main/java/org/msgpack/UnpackIterator.java @@ -21,41 +21,28 @@ import java.io.IOException; import java.util.Iterator; import java.util.NoSuchElementException; -public class UnpackIterator implements Iterator { +public class UnpackIterator extends UnpackResult implements Iterator { private Unpacker pac; - private boolean have; - private Object data; UnpackIterator(Unpacker pac) { + super(); this.pac = pac; - this.have = false; } public boolean hasNext() { - if(have) { return true; } + if(finished) { return true; } try { - while(true) { - if(pac.execute()) { - data = pac.getData(); - pac.reset(); - have = true; - return true; - } - - if(!pac.fill()) { - return false; - } - } + return pac.next(this); } catch (IOException e) { return false; } } - public Object next() { - if(!have && !hasNext()) { + public MessagePackObject next() { + if(!finished && !hasNext()) { throw new NoSuchElementException(); } - have = false; + finished = false; return data; } diff --git a/java/src/main/java/org/msgpack/schema/FieldSchema.java b/java/src/main/java/org/msgpack/UnpackResult.java similarity index 62% rename from java/src/main/java/org/msgpack/schema/FieldSchema.java rename to java/src/main/java/org/msgpack/UnpackResult.java index 66c2ff23..bb981c12 100644 --- a/java/src/main/java/org/msgpack/schema/FieldSchema.java +++ b/java/src/main/java/org/msgpack/UnpackResult.java @@ -15,29 +15,28 @@ // See the License for the specific language governing permissions and // limitations under the License. // -package org.msgpack.schema; +package org.msgpack; -import org.msgpack.Schema; +public class UnpackResult { + protected boolean finished = false; + protected MessagePackObject data = null; -public class FieldSchema { - private String name; - private Schema schema; - - public FieldSchema(String name, Schema schema) { - this.name = name; - this.schema = schema; + public boolean isFinished() { + return finished; } - public final String getName() { - return name; + public MessagePackObject getData() { + return data; } - public final Schema getSchema() { - return schema; + public void reset() { + finished = false; + data = null; } - public String getExpression() { - return "(field "+name+" "+schema.getExpression()+")"; + void done(MessagePackObject obj) { + finished = true; + data = obj; } } diff --git a/java/src/main/java/org/msgpack/Unpacker.java b/java/src/main/java/org/msgpack/Unpacker.java index c8a88234..3cae502e 100644 --- a/java/src/main/java/org/msgpack/Unpacker.java +++ b/java/src/main/java/org/msgpack/Unpacker.java @@ -22,144 +22,382 @@ import java.io.InputStream; import java.io.IOException; import java.util.Iterator; import java.nio.ByteBuffer; -import org.msgpack.impl.UnpackerImpl; +import java.math.BigInteger; -public class Unpacker extends UnpackerImpl implements Iterable { +/** + * Unpacker enables you to deserialize objects from stream. + * + * Unpacker provides Buffered API, Unbuffered API and + * Direct Conversion API. + * + * Buffered API uses the internal buffer of the Unpacker. + * Following code uses Buffered API with an InputStream: + *
+ * // create an unpacker with input stream
+ * Unpacker pac = new Unpacker(System.in);
+ *
+ * // take a object out using next() method, or ...
+ * UnpackResult result = pac.next();
+ *
+ * // use an iterator.
+ * for(MessagePackObject obj : pac) {
+ *   // use MessageConvertable interface to convert the
+ *   // the generic object to the specific type.
+ * }
+ * 
+ * + * Following code doesn't use the input stream and feeds buffer + * using {@link feed(byte[])} method. This is useful to use + * special stream like zlib or event-driven I/O library. + *
+ * // create an unpacker without input stream
+ * Unpacker pac = new Unpacker();
+ *
+ * // feed buffer to the internal buffer.
+ * pac.feed(input_bytes);
+ *
+ * // use next() method or iterators.
+ * for(MessagePackObject obj : pac) {
+ *   // ...
+ * }
+ * 
+ * + * The combination of {@link reserveBuffer()}, {@link getBuffer()}, + * {@link getBufferOffset()}, {@link getBufferCapacity()} and + * {@link bufferConsumed()} is useful to omit copying. + *
+ * // create an unpacker without input stream
+ * Unpacker pac = new Unpacker();
+ *
+ * // reserve internal buffer at least 1024 bytes.
+ * pac.reserveBuffer(1024);
+ *
+ * // feed buffer to the internal buffer upto pac.getBufferCapacity() bytes.
+ * System.in.read(pac.getBuffer(), pac.getBufferOffset(), pac.getBufferCapacity());
+ *
+ * // use next() method or iterators.
+ * for(MessagePackObject obj : pac) {
+ *     // ...
+ * }
+ * 
+ * + * Unbuffered API doesn't initialize the internal buffer. + * You can manage the buffer manually. + *
+ * // create an unpacker with input stream
+ * Unpacker pac = new Unpacker(System.in);
+ *
+ * // manage the buffer manually.
+ * byte[] buffer = new byte[1024];
+ * int filled = System.in.read(buffer);
+ * int offset = 0;
+ *
+ * // deserialize objects using execute() method.
+ * int nextOffset = pac.execute(buffer, offset, filled);
+ *
+ * // take out object if deserialized object is ready.
+ * if(pac.isFinished()) {
+ *     MessagePackObject obj = pac.getData();
+ *     // ...
+ * }
+ * 
+ */ +public class Unpacker implements Iterable { - public static final int DEFAULT_BUFFER_SIZE = 32*1024; + // buffer: + // +---------------------------------------------+ + // | [object] | [obje| unparsed ... | unused ...| + // +---------------------------------------------+ + // ^ parsed + // ^ offset + // ^ filled + // ^ buffer.length - private int used; - private int offset; - private int parsed; - private byte[] buffer; - private int bufferReserveSize; - private InputStream stream; + private static final int DEFAULT_BUFFER_SIZE = 32*1024; + protected int parsed; + protected int bufferReserveSize; + protected InputStream stream; + + final class BufferedUnpackerMixin extends BufferedUnpackerImpl { + boolean fill() throws IOException { + if(stream == null) { + return false; + } + reserveBuffer(bufferReserveSize); + int rl = stream.read(buffer, filled, buffer.length - filled); + // equals: stream.read(getBuffer(), getBufferOffset(), getBufferCapacity()); + if(rl <= 0) { + return false; + } + bufferConsumed(rl); + return true; + } + }; + + final BufferedUnpackerMixin impl = new BufferedUnpackerMixin(); + + + /** + * Calls {@link Unpacker(DEFAULT_BUFFER_SIZE)} + */ public Unpacker() { this(DEFAULT_BUFFER_SIZE); } + /** + * Calls {@link Unpacker(null, bufferReserveSize)} + */ public Unpacker(int bufferReserveSize) { this(null, bufferReserveSize); } + /** + * Calls {@link Unpacker(stream, DEFAULT_BUFFER_SIZE)} + */ public Unpacker(InputStream stream) { this(stream, DEFAULT_BUFFER_SIZE); } + /** + * Constructs the unpacker. + * The stream is used to fill the buffer when more buffer is required by {@link next()} or {@link UnpackIterator#hasNext()} method. + * @param stream input stream to fill the buffer + * @param bufferReserveSize threshold size to expand the size of buffer + */ public Unpacker(InputStream stream, int bufferReserveSize) { - super(); - this.used = 0; - this.offset = 0; this.parsed = 0; - this.buffer = new byte[bufferReserveSize]; this.bufferReserveSize = bufferReserveSize/2; this.stream = stream; } - public Unpacker useSchema(Schema s) { - super.setSchema(s); - return this; + + /** + * Gets the input stream. + * @return the input stream. it may be null. + */ + public InputStream getStream() { + return this.stream; } - public void reserveBuffer(int size) { - if(buffer.length - used >= size) { - return; - } - /* - if(used == parsed && buffer.length >= size) { - // rewind buffer - used = 0; - offset = 0; - return; - } - */ - - int nextSize = buffer.length * 2; - while(nextSize < size + used) { - nextSize *= 2; - } - - byte[] tmp = new byte[nextSize]; - System.arraycopy(buffer, offset, tmp, 0, used - offset); - - buffer = tmp; - used -= offset; - offset = 0; + /** + * Sets the input stream. + * @param stream the input stream to set. + */ + public void setStream(InputStream stream) { + this.stream = stream; } - public byte[] getBuffer() { - return buffer; - } - - public int getBufferOffset() { - return used; - } - - public int getBufferCapacity() { - return buffer.length - used; - } - - public void bufferConsumed(int size) { - used += size; - } - - public void feed(ByteBuffer buffer) { - int length = buffer.remaining(); - if (length == 0) return; - reserveBuffer(length); - buffer.get(this.buffer, this.offset, length); - bufferConsumed(length); - } + /** + * Fills the buffer with the specified buffer. + */ public void feed(byte[] buffer) { feed(buffer, 0, buffer.length); } + /** + * Fills the buffer with the specified buffer. + */ public void feed(byte[] buffer, int offset, int length) { reserveBuffer(length); - System.arraycopy(buffer, offset, this.buffer, this.offset, length); + System.arraycopy(buffer, offset, impl.buffer, impl.offset, length); bufferConsumed(length); } - public boolean fill() throws IOException { - if(stream == null) { - return false; - } - reserveBuffer(bufferReserveSize); - int rl = stream.read(getBuffer(), getBufferOffset(), getBufferCapacity()); - if(rl <= 0) { - return false; - } - bufferConsumed(rl); - return true; + /** + * Fills the buffer with the specified buffer. + */ + public void feed(ByteBuffer buffer) { + int length = buffer.remaining(); + if (length == 0) return; + reserveBuffer(length); + buffer.get(impl.buffer, impl.offset, length); + bufferConsumed(length); } - public Iterator iterator() { + /** + * Swaps the internal buffer with the specified buffer. + * This method doesn't copy the buffer and the its contents will be rewritten by {@link fill()} or {@link feed(byte[])} method. + */ + public void wrap(byte[] buffer) { + wrap(buffer, 0, buffer.length); + } + + /** + * Swaps the internal buffer with the specified buffer. + * This method doesn't copy the buffer and the its contents will be rewritten by {@link fill()} or {@link feed(byte[])} method. + */ + public void wrap(byte[] buffer, int offset, int length) { + impl.buffer = buffer; + impl.offset = offset; + impl.filled = length; + } + + /** + * Fills the internal using the input stream. + * @return false if the stream is null or stream.read returns <= 0. + */ + public boolean fill() throws IOException { + return impl.fill(); + } + + + /** + * Returns the iterator that calls {@link next()} method repeatedly. + */ + public Iterator iterator() { return new UnpackIterator(this); } + /** + * Deserializes one object and returns it. + * @return {@link UnpackResult#isFinished()} returns false if the buffer is insufficient to deserialize one object. + */ + public UnpackResult next() throws IOException, UnpackException { + UnpackResult result = new UnpackResult(); + impl.next(result); + return result; + } + + /** + * Deserializes one object and returns it. + * @return false if the buffer is insufficient to deserialize one object. + */ + public boolean next(UnpackResult result) throws IOException, UnpackException { + return impl.next(result); + } + + + /** + * Reserve free space of the internal buffer at least specified size and expands {@link getBufferCapacity()}. + */ + public void reserveBuffer(int require) { + if(impl.buffer == null) { + int nextSize = (bufferReserveSize < require) ? require : bufferReserveSize; + impl.buffer = new byte[nextSize]; + return; + } + + if(impl.filled <= impl.offset) { + // rewind the buffer + impl.filled = 0; + impl.offset = 0; + } + + if(impl.buffer.length - impl.filled >= require) { + return; + } + + int nextSize = impl.buffer.length * 2; + int notParsed = impl.filled - impl.offset; + while(nextSize < require + notParsed) { + nextSize *= 2; + } + + byte[] tmp = new byte[nextSize]; + System.arraycopy(impl.buffer, impl.offset, tmp, 0, impl.filled - impl.offset); + + impl.buffer = tmp; + impl.filled = notParsed; + impl.offset = 0; + } + + /** + * Returns the internal buffer. + */ + public byte[] getBuffer() { + return impl.buffer; + } + + /** + * Returns the size of free space of the internal buffer. + */ + public int getBufferCapacity() { + return impl.buffer.length - impl.filled; + } + + /** + * Returns the offset of free space in the internal buffer. + */ + public int getBufferOffset() { + return impl.filled; + } + + /** + * Moves front the offset of the free space in the internal buffer. + * Call this method after fill the buffer manually using {@link reserveBuffer()}, {@link getBuffer()}, {@link getBufferOffset()} and {@link getBufferCapacity()} methods. + */ + public void bufferConsumed(int size) { + impl.filled += size; + } + + /** + * Deserializes one object upto the offset of the internal buffer. + * Call {@link reset()} method before calling this method again. + * @return true if one object is deserialized. Use {@link getData()} to get the deserialized object. + */ public boolean execute() throws UnpackException { - int noffset = super.execute(buffer, offset, used); - if(noffset <= offset) { + int noffset = impl.execute(impl.buffer, impl.offset, impl.filled); + if(noffset <= impl.offset) { return false; } - parsed += noffset - offset; - offset = noffset; - return super.isFinished(); + parsed += noffset - impl.offset; + impl.offset = noffset; + return impl.isFinished(); } - public Object getData() { - return super.getData(); + + /** + * Deserializes one object over the specified buffer. + * This method doesn't use the internal buffer. + * Use {@link isFinished()} method to known a object is ready to get. + * Call {@link reset()} method before calling this method again. + * @return offset position that is parsed. + */ + public int execute(byte[] buffer) throws UnpackException { + return execute(buffer, 0, buffer.length); } + /** + * Deserializes one object over the specified buffer. + * This method doesn't use the internal buffer. + * Use {@link isFinished()} method to known a object is ready to get. + * Call {@link reset()} method before calling this method again. + * @return offset position that is parsed. + */ + public int execute(byte[] buffer, int offset, int length) throws UnpackException { + int noffset = impl.execute(buffer, offset + impl.offset, length); + impl.offset = noffset - offset; + if(impl.isFinished()) { + impl.resetState(); + } + return noffset; + } + + /** + * Gets the object deserialized by {@link execute(byte[])} method. + */ + public MessagePackObject getData() { + return impl.getData(); + } + + /** + * Returns true if an object is ready to get with {@link getData()} method. + */ + public boolean isFinished() { + return impl.isFinished(); + } + + /** + * Resets the internal state of the unpacker. + */ public void reset() { - super.reset(); - parsed = 0; + impl.reset(); } public int getMessageSize() { - return parsed - offset + used; + return parsed - impl.offset + impl.filled; } public int getParsedSize() { @@ -167,88 +405,168 @@ public class Unpacker extends UnpackerImpl implements Iterable { } public int getNonParsedSize() { - return used - offset; + return impl.filled - impl.offset; } public void skipNonparsedBuffer(int size) { - offset += size; + impl.offset += size; } public void removeNonparsedBuffer() { - used = offset; + impl.filled = impl.offset; } - /* - public static class Context { - private boolean finished; - private Object data; - private int offset; - private UnpackerImpl impl; - public Context() - { - this.finished = false; - this.impl = new UnpackerImpl(); - } - - public boolean isFinished() - { - return finished; - } - - public Object getData() - { - return data; - } - - int getOffset() - { - return offset; - } - - void setFinished(boolean finished) - { - this.finished = finished; - } - - void setData(Object data) - { - this.data = data; - } - - void setOffset(int offset) - { - this.offset = offset; - } - - UnpackerImpl getImpl() - { - return impl; - } + /** + * Gets one {@code byte} value from the buffer. + * This method calls {@link fill()} method if needed. + * @throws MessageTypeException the first value of the buffer is not a {@code byte}. + */ + public byte unpackByte() throws IOException, MessageTypeException { + return impl.unpackByte(); } - public static int unpack(Context ctx, byte[] buffer) throws UnpackException - { - return unpack(ctx, buffer, 0, buffer.length); + /** + * Gets one {@code short} value from the buffer. + * This method calls {@link fill()} method if needed. + * @throws MessageTypeException the first value of the buffer is not a {@code short}. + */ + public short unpackShort() throws IOException, MessageTypeException { + return impl.unpackShort(); } - public static int unpack(Context ctx, byte[] buffer, int offset, int length) throws UnpackException - { - UnpackerImpl impl = ctx.getImpl(); - int noffset = impl.execute(buffer, offset + ctx.getOffset(), length); - ctx.setOffset(noffset - offset); - if(impl.isFinished()) { - ctx.setData(impl.getData()); - ctx.setFinished(false); - impl.reset(); - } else { - ctx.setData(null); - ctx.setFinished(true); - } - int parsed = noffset - offset; - ctx.setOffset(parsed); - return noffset; + /** + * Gets one {@code int} value from the buffer. + * This method calls {@link fill()} method if needed. + * @throws MessageTypeException the first value of the buffer is not a {@code int}. + */ + public int unpackInt() throws IOException, MessageTypeException { + return impl.unpackInt(); + } + + /** + * Gets one {@code long} value from the buffer. + * This method calls {@link fill()} method if needed. + * @throws MessageTypeException the first value of the buffer is not a {@code long}. + */ + public long unpackLong() throws IOException, MessageTypeException { + return impl.unpackLong(); + } + + /** + * Gets one {@code BigInteger} value from the buffer. + * This method calls {@link fill()} method if needed. + * @throws MessageTypeException the first value of the buffer is not a {@code BigInteger}. + */ + public BigInteger unpackBigInteger() throws IOException, MessageTypeException { + return impl.unpackBigInteger(); + } + + /** + * Gets one {@code float} value from the buffer. + * This method calls {@link fill()} method if needed. + * @throws MessageTypeException the first value of the buffer is not a {@code float}. + */ + public float unpackFloat() throws IOException, MessageTypeException { + return impl.unpackFloat(); + } + + /** + * Gets one {@code double} value from the buffer. + * This method calls {@link fill()} method if needed. + * @throws MessageTypeException the first value of the buffer is not a {@code double}. + */ + public double unpackDouble() throws IOException, MessageTypeException { + return impl.unpackDouble(); + } + + /** + * Gets one {@code null} value from the buffer. + * This method calls {@link fill()} method if needed. + * @throws MessageTypeException the first value of the buffer is not a {@code null}. + */ + public Object unpackNull() throws IOException, MessageTypeException { + return impl.unpackNull(); + } + + /** + * Gets one {@code boolean} value from the buffer. + * This method calls {@link fill()} method if needed. + * @throws MessageTypeException the first value of the buffer is not a {@code boolean}. + */ + public boolean unpackBoolean() throws IOException, MessageTypeException { + return impl.unpackBoolean(); + } + + /** + * Gets one array header from the buffer. + * This method calls {@link fill()} method if needed. + * @return the length of the map. There are {@code retval} objects to get. + * @throws MessageTypeException the first value of the buffer is not a array. + */ + public int unpackArray() throws IOException, MessageTypeException { + return impl.unpackArray(); + } + + /** + * Gets one map header from the buffer. + * This method calls {@link fill()} method if needed. + * @return the length of the map. There are {@code retval * 2} objects to get. + * @throws MessageTypeException the first value of the buffer is not a map. + */ + public int unpackMap() throws IOException, MessageTypeException { + return impl.unpackMap(); + } + + /** + * Gets one raw header from the buffer. + * This method calls {@link fill()} method if needed. + * @return the length of the raw bytes. There are {@code retval} bytes to get. + * @throws MessageTypeException the first value of the buffer is not a raw bytes. + */ + public int unpackRaw() throws IOException, MessageTypeException { + return impl.unpackRaw(); + } + + /** + * Gets one raw body from the buffer. + * This method calls {@link fill()} method if needed. + */ + public byte[] unpackRawBody(int length) throws IOException { + return impl.unpackRawBody(length); + } + + /** + * Gets one raw bytes from the buffer. + * This method calls {@link fill()} method if needed. + */ + public byte[] unpackByteArray() throws IOException { + return impl.unpackByteArray(); + } + + /** + * Gets one {@code String} value from the buffer. + * This method calls {@link fill()} method if needed. + * @throws MessageTypeException the first value of the buffer is not a {@code String}. + */ + final public String unpackString() throws IOException, MessageTypeException { + return impl.unpackString(); + } + + /** + * Gets one {@code Object} value from the buffer. + * This method calls {@link fill()} method if needed. + */ + final public MessagePackObject unpackObject() throws IOException { + return impl.unpackObject(); + } + + final public void unpack(MessageUnpackable obj) throws IOException, MessageTypeException { + obj.messageUnpack(this); + } + + final public boolean tryUnpackNull() throws IOException { + return impl.tryUnpackNull(); } - */ } diff --git a/java/src/main/java/org/msgpack/impl/UnpackerImpl.java b/java/src/main/java/org/msgpack/UnpackerImpl.java similarity index 70% rename from java/src/main/java/org/msgpack/impl/UnpackerImpl.java rename to java/src/main/java/org/msgpack/UnpackerImpl.java index adc62b09..6a7085fb 100644 --- a/java/src/main/java/org/msgpack/impl/UnpackerImpl.java +++ b/java/src/main/java/org/msgpack/UnpackerImpl.java @@ -15,14 +15,11 @@ // See the License for the specific language governing permissions and // limitations under the License. // -package org.msgpack.impl; +package org.msgpack; import java.nio.ByteBuffer; -//import java.math.BigInteger; -import org.msgpack.*; -import org.msgpack.schema.GenericSchema; -import org.msgpack.schema.IMapSchema; -import org.msgpack.schema.IArraySchema; +import java.math.BigInteger; +import org.msgpack.object.*; public class UnpackerImpl { static final int CS_HEADER = 0x00; @@ -47,7 +44,7 @@ public class UnpackerImpl { static final int CT_MAP_KEY = 0x01; static final int CT_MAP_VALUE = 0x02; - static final int MAX_STACK_SIZE = 16; + static final int MAX_STACK_SIZE = 32; private int cs; private int trail; @@ -55,53 +52,45 @@ public class UnpackerImpl { private int[] stack_ct = new int[MAX_STACK_SIZE]; private int[] stack_count = new int[MAX_STACK_SIZE]; private Object[] stack_obj = new Object[MAX_STACK_SIZE]; - private Schema[] stack_schema = new Schema[MAX_STACK_SIZE]; private int top_ct; private int top_count; private Object top_obj; - private Schema top_schema; private ByteBuffer castBuffer = ByteBuffer.allocate(8); private boolean finished = false; - private Object data = null; + private MessagePackObject data = null; - private static final Schema GENERIC_SCHEMA = new GenericSchema(); - private Schema rootSchema; - - protected UnpackerImpl() + public UnpackerImpl() { - setSchema(GENERIC_SCHEMA); - } - - protected void setSchema(Schema schema) - { - this.rootSchema = schema; reset(); } - protected Object getData() + public final MessagePackObject getData() { return data; } - protected boolean isFinished() + public final boolean isFinished() { return finished; } - protected void reset() - { + public final void resetState() { cs = CS_HEADER; top = -1; - finished = false; - data = null; top_ct = 0; top_count = 0; top_obj = null; - top_schema = rootSchema; + } + + public final void reset() + { + resetState(); + finished = false; + data = null; } @SuppressWarnings("unchecked") - protected int execute(byte[] src, int off, int length) throws UnpackException + public final int execute(byte[] src, int off, int length) throws UnpackException { if(off >= length) { return off; } @@ -123,20 +112,20 @@ public class UnpackerImpl { if((b & 0x80) == 0) { // Positive Fixnum //System.out.println("positive fixnum "+b); - obj = top_schema.createFromByte((byte)b); + obj = IntegerType.create((byte)b); break _push; } if((b & 0xe0) == 0xe0) { // Negative Fixnum //System.out.println("negative fixnum "+b); - obj = top_schema.createFromByte((byte)b); + obj = IntegerType.create((byte)b); break _push; } if((b & 0xe0) == 0xa0) { // FixRaw trail = b & 0x1f; if(trail == 0) { - obj = top_schema.createFromRaw(new byte[0], 0, 0); + obj = RawType.create(new byte[0]); break _push; } cs = ACS_RAW_VALUE; @@ -147,22 +136,20 @@ public class UnpackerImpl { if(top >= MAX_STACK_SIZE) { throw new UnpackException("parse error"); } - if(!(top_schema instanceof IArraySchema)) { - throw new RuntimeException("type error"); - } count = b & 0x0f; //System.out.println("fixarray count:"+count); - obj = new Object[count]; - if(count == 0) { break _push; } // FIXME check IArraySchema + obj = new MessagePackObject[count]; + if(count == 0) { + obj = ArrayType.create((MessagePackObject[])obj); + break _push; + } ++top; stack_obj[top] = top_obj; stack_ct[top] = top_ct; stack_count[top] = top_count; - stack_schema[top] = top_schema; top_obj = obj; top_ct = CT_ARRAY_ITEM; top_count = count; - top_schema = ((IArraySchema)top_schema).getElementSchema(0); break _header_again; } @@ -170,34 +157,32 @@ public class UnpackerImpl { if(top >= MAX_STACK_SIZE) { throw new UnpackException("parse error"); } - if(!(top_schema instanceof IMapSchema)) { - throw new RuntimeException("type error"); - } count = b & 0x0f; - obj = new Object[count*2]; - if(count == 0) { break _push; } // FIXME check IMapSchema + obj = new MessagePackObject[count*2]; + if(count == 0) { + obj = MapType.create((MessagePackObject[])obj); + break _push; + } //System.out.println("fixmap count:"+count); ++top; stack_obj[top] = top_obj; stack_ct[top] = top_ct; stack_count[top] = top_count; - stack_schema[top] = top_schema; top_obj = obj; top_ct = CT_MAP_KEY; top_count = count; - top_schema = ((IMapSchema)top_schema).getKeySchema(); break _header_again; } switch(b & 0xff) { // FIXME case 0xc0: // nil - obj = top_schema.createFromNil(); + obj = NilType.create(); break _push; case 0xc2: // false - obj = top_schema.createFromBoolean(false); + obj = BooleanType.create(false); break _push; case 0xc3: // true - obj = top_schema.createFromBoolean(true); + obj = BooleanType.create(true); break _push; case 0xca: // float case 0xcb: // double @@ -241,13 +226,13 @@ public class UnpackerImpl { case CS_FLOAT: castBuffer.rewind(); castBuffer.put(src, n, 4); - obj = top_schema.createFromFloat( castBuffer.getFloat(0) ); + obj = FloatType.create( castBuffer.getFloat(0) ); //System.out.println("float "+obj); break _push; case CS_DOUBLE: castBuffer.rewind(); castBuffer.put(src, n, 8); - obj = top_schema.createFromDouble( castBuffer.getDouble(0) ); + obj = FloatType.create( castBuffer.getDouble(0) ); //System.out.println("double "+obj); break _push; case CS_UINT_8: @@ -255,7 +240,7 @@ public class UnpackerImpl { //System.out.println(src[n]); //System.out.println(src[n+1]); //System.out.println(src[n-1]); - obj = top_schema.createFromShort( (short)((src[n]) & 0xff) ); + obj = IntegerType.create( (short)((src[n]) & 0xff) ); //System.out.println("uint8 "+obj); break _push; case CS_UINT_16: @@ -263,13 +248,13 @@ public class UnpackerImpl { //System.out.println(src[n+1]); castBuffer.rewind(); castBuffer.put(src, n, 2); - obj = top_schema.createFromInt( ((int)castBuffer.getShort(0)) & 0xffff ); + obj = IntegerType.create( ((int)castBuffer.getShort(0)) & 0xffff ); //System.out.println("uint 16 "+obj); break _push; case CS_UINT_32: castBuffer.rewind(); castBuffer.put(src, n, 4); - obj = top_schema.createFromLong( ((long)castBuffer.getInt(0)) & 0xffffffffL ); + obj = IntegerType.create( ((long)castBuffer.getInt(0)) & 0xffffffffL ); //System.out.println("uint 32 "+obj); break _push; case CS_UINT_64: @@ -278,38 +263,36 @@ public class UnpackerImpl { { long o = castBuffer.getLong(0); if(o < 0) { - // FIXME - //obj = GenericBigInteger.valueOf(o & 0x7fffffffL).setBit(31); - throw new UnpackException("uint 64 bigger than 0x7fffffff is not supported"); + obj = IntegerType.create(new BigInteger(1, castBuffer.array())); } else { - obj = top_schema.createFromLong( o ); + obj = IntegerType.create(o); } } break _push; case CS_INT_8: - obj = top_schema.createFromByte( src[n] ); + obj = IntegerType.create( src[n] ); break _push; case CS_INT_16: castBuffer.rewind(); castBuffer.put(src, n, 2); - obj = top_schema.createFromShort( castBuffer.getShort(0) ); + obj = IntegerType.create( castBuffer.getShort(0) ); break _push; case CS_INT_32: castBuffer.rewind(); castBuffer.put(src, n, 4); - obj = top_schema.createFromInt( castBuffer.getInt(0) ); + obj = IntegerType.create( castBuffer.getInt(0) ); break _push; case CS_INT_64: castBuffer.rewind(); castBuffer.put(src, n, 8); - obj = top_schema.createFromLong( castBuffer.getLong(0) ); + obj = IntegerType.create( castBuffer.getLong(0) ); break _push; case CS_RAW_16: castBuffer.rewind(); castBuffer.put(src, n, 2); trail = ((int)castBuffer.getShort(0)) & 0xffff; if(trail == 0) { - obj = top_schema.createFromRaw(new byte[0], 0, 0); + obj = RawType.create(new byte[0]); break _push; } cs = ACS_RAW_VALUE; @@ -320,104 +303,100 @@ public class UnpackerImpl { // FIXME overflow check trail = castBuffer.getInt(0) & 0x7fffffff; if(trail == 0) { - obj = top_schema.createFromRaw(new byte[0], 0, 0); + obj = RawType.create(new byte[0]); break _push; } cs = ACS_RAW_VALUE; - case ACS_RAW_VALUE: - obj = top_schema.createFromRaw(src, n, trail); + break _fixed_trail_again; + case ACS_RAW_VALUE: { + byte[] raw = new byte[trail]; + System.arraycopy(src, n, raw, 0, trail); + obj = RawType.create(raw); + } break _push; case CS_ARRAY_16: if(top >= MAX_STACK_SIZE) { throw new UnpackException("parse error"); } - if(!(top_schema instanceof IArraySchema)) { - throw new RuntimeException("type error"); - } castBuffer.rewind(); castBuffer.put(src, n, 2); count = ((int)castBuffer.getShort(0)) & 0xffff; - obj = new Object[count]; - if(count == 0) { break _push; } // FIXME check IArraySchema + obj = new MessagePackObject[count]; + if(count == 0) { + obj = ArrayType.create((MessagePackObject[])obj); + break _push; + } ++top; stack_obj[top] = top_obj; stack_ct[top] = top_ct; stack_count[top] = top_count; - stack_schema[top] = top_schema; top_obj = obj; top_ct = CT_ARRAY_ITEM; top_count = count; - top_schema = ((IArraySchema)top_schema).getElementSchema(0); break _header_again; case CS_ARRAY_32: if(top >= MAX_STACK_SIZE) { throw new UnpackException("parse error"); } - if(!(top_schema instanceof IArraySchema)) { - throw new RuntimeException("type error"); - } castBuffer.rewind(); castBuffer.put(src, n, 4); // FIXME overflow check count = castBuffer.getInt(0) & 0x7fffffff; - obj = new Object[count]; - if(count == 0) { break _push; } // FIXME check IArraySchema + obj = new MessagePackObject[count]; + if(count == 0) { + obj = ArrayType.create((MessagePackObject[])obj); + break _push; + } ++top; stack_obj[top] = top_obj; stack_ct[top] = top_ct; stack_count[top] = top_count; - stack_schema[top] = top_schema; top_obj = obj; top_ct = CT_ARRAY_ITEM; top_count = count; - top_schema = ((IArraySchema)top_schema).getElementSchema(0); break _header_again; case CS_MAP_16: if(top >= MAX_STACK_SIZE) { throw new UnpackException("parse error"); } - if(!(top_schema instanceof IMapSchema)) { - throw new RuntimeException("type error"); - } castBuffer.rewind(); castBuffer.put(src, n, 2); count = ((int)castBuffer.getShort(0)) & 0xffff; - obj = new Object[count*2]; - if(count == 0) { break _push; } // FIXME check IMapSchema + obj = new MessagePackObject[count*2]; + if(count == 0) { + obj = MapType.create((MessagePackObject[])obj); + break _push; + } //System.out.println("fixmap count:"+count); ++top; stack_obj[top] = top_obj; stack_ct[top] = top_ct; stack_count[top] = top_count; - stack_schema[top] = top_schema; top_obj = obj; top_ct = CT_MAP_KEY; top_count = count; - top_schema = ((IMapSchema)top_schema).getKeySchema(); break _header_again; case CS_MAP_32: if(top >= MAX_STACK_SIZE) { throw new UnpackException("parse error"); } - if(!(top_schema instanceof IMapSchema)) { - throw new RuntimeException("type error"); - } castBuffer.rewind(); castBuffer.put(src, n, 4); // FIXME overflow check count = castBuffer.getInt(0) & 0x7fffffff; - obj = new Object[count*2]; - if(count == 0) { break _push; } // FIXME check IMapSchema + obj = new MessagePackObject[count*2]; + if(count == 0) { + obj = MapType.create((MessagePackObject[])obj); + break _push; + } //System.out.println("fixmap count:"+count); ++top; stack_obj[top] = top_obj; stack_ct[top] = top_ct; stack_count[top] = top_count; - stack_schema[top] = top_schema; top_obj = obj; top_ct = CT_MAP_KEY; top_count = count; - top_schema = ((IMapSchema)top_schema).getKeySchema(); break _header_again; default: throw new UnpackException("parse error"); @@ -432,7 +411,7 @@ public class UnpackerImpl { //System.out.println("push top:"+top); if(top == -1) { ++i; - data = obj; + data = (MessagePackObject)obj; finished = true; break _out; } @@ -446,14 +425,10 @@ public class UnpackerImpl { top_obj = stack_obj[top]; top_ct = stack_ct[top]; top_count = stack_count[top]; - top_schema = stack_schema[top]; - obj = ((IArraySchema)top_schema).createFromArray(ar); + obj = ArrayType.create((MessagePackObject[])ar); stack_obj[top] = null; - stack_schema[top] = null; --top; break _push; - } else { - top_schema = ((IArraySchema)stack_schema[top]).getElementSchema(ar.length - top_count); } break _header_again; } @@ -462,7 +437,6 @@ public class UnpackerImpl { Object[] mp = (Object[])top_obj; mp[mp.length - top_count*2] = obj; top_ct = CT_MAP_VALUE; - top_schema = ((IMapSchema)stack_schema[top]).getValueSchema(); break _header_again; } case CT_MAP_VALUE: { @@ -473,10 +447,8 @@ public class UnpackerImpl { top_obj = stack_obj[top]; top_ct = stack_ct[top]; top_count = stack_count[top]; - top_schema = stack_schema[top]; - obj = ((IMapSchema)top_schema).createFromMap(mp); + obj = MapType.create((MessagePackObject[])mp); stack_obj[top] = null; - stack_schema[top] = null; --top; break _push; } diff --git a/java/src/main/java/org/msgpack/object/ArrayType.java b/java/src/main/java/org/msgpack/object/ArrayType.java new file mode 100644 index 00000000..36134dc7 --- /dev/null +++ b/java/src/main/java/org/msgpack/object/ArrayType.java @@ -0,0 +1,81 @@ +// +// MessagePack for Java +// +// Copyright (C) 2009-2010 FURUHASHI Sadayuki +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +package org.msgpack.object; + +import java.util.List; +import java.util.Arrays; +import java.io.IOException; +import org.msgpack.*; + +public class ArrayType extends MessagePackObject { + private MessagePackObject[] array; + + ArrayType(MessagePackObject[] array) { + this.array = array; + } + + public static ArrayType create(MessagePackObject[] array) { + return new ArrayType(array); + } + + @Override + public boolean isArrayType() { + return true; + } + + @Override + public MessagePackObject[] asArray() { + return array; + } + + @Override + public List asList() { + return Arrays.asList(array); + } + + @Override + public void messagePack(Packer pk) throws IOException { + pk.packArray(array.length); + for(int i=0; i < array.length; i++) { + array[i].messagePack(pk); + } + } + + @Override + public boolean equals(Object obj) { + if(obj.getClass() != getClass()) { + return false; + } + return Arrays.equals(((ArrayType)obj).array, array); + } + + @Override + public int hashCode() { + return array.hashCode(); + } + + @Override + public Object clone() { + MessagePackObject[] copy = new MessagePackObject[array.length]; + for(int i=0; i < array.length; i++) { + copy[i] = (MessagePackObject)array[i].clone(); + } + return copy; + } +} + diff --git a/java/src/main/java/org/msgpack/object/BigIntegerTypeIMPL.java b/java/src/main/java/org/msgpack/object/BigIntegerTypeIMPL.java new file mode 100644 index 00000000..b29879f4 --- /dev/null +++ b/java/src/main/java/org/msgpack/object/BigIntegerTypeIMPL.java @@ -0,0 +1,131 @@ +// +// MessagePack for Java +// +// Copyright (C) 2009-2010 FURUHASHI Sadayuki +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +package org.msgpack.object; + +import java.math.BigInteger; +import java.io.IOException; +import org.msgpack.*; + +class BigIntegerTypeIMPL extends IntegerType { + private BigInteger value; + + BigIntegerTypeIMPL(BigInteger value) { + this.value = value; + } + + @Override + public byte asByte() { + if(value.compareTo(BigInteger.valueOf((long)Byte.MAX_VALUE)) > 0) { + throw new MessageTypeException("type error"); + } + return value.byteValue(); + } + + @Override + public short asShort() { + if(value.compareTo(BigInteger.valueOf((long)Short.MAX_VALUE)) > 0) { + throw new MessageTypeException("type error"); + } + return value.shortValue(); + } + + @Override + public int asInt() { + if(value.compareTo(BigInteger.valueOf((long)Integer.MAX_VALUE)) > 0) { + throw new MessageTypeException("type error"); + } + return value.intValue(); + } + + @Override + public long asLong() { + if(value.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) > 0) { + throw new MessageTypeException("type error"); + } + return value.longValue(); + } + + @Override + public BigInteger asBigInteger() { + return value; + } + + @Override + public byte byteValue() { + return value.byteValue(); + } + + @Override + public short shortValue() { + return value.shortValue(); + } + + @Override + public int intValue() { + return value.intValue(); + } + + @Override + public long longValue() { + return value.longValue(); + } + + @Override + public BigInteger bigIntegerValue() { + return value; + } + + @Override + public float floatValue() { + return value.floatValue(); + } + + @Override + public double doubleValue() { + return value.doubleValue(); + } + + @Override + public void messagePack(Packer pk) throws IOException { + pk.packBigInteger(value); + } + + @Override + public boolean equals(Object obj) { + if(obj.getClass() != getClass()) { + if(obj.getClass() == ShortIntegerTypeIMPL.class) { + return BigInteger.valueOf(((ShortIntegerTypeIMPL)obj).longValue()).equals(value); + } else if(obj.getClass() == LongIntegerTypeIMPL.class) { + return BigInteger.valueOf(((LongIntegerTypeIMPL)obj).longValue()).equals(value); + } + return false; + } + return ((BigIntegerTypeIMPL)obj).value.equals(value); + } + + @Override + public int hashCode() { + return value.hashCode(); + } + + @Override + public Object clone() { + return new BigIntegerTypeIMPL(value); + } +} + diff --git a/java/src/main/java/org/msgpack/object/BooleanType.java b/java/src/main/java/org/msgpack/object/BooleanType.java new file mode 100644 index 00000000..65bd42b4 --- /dev/null +++ b/java/src/main/java/org/msgpack/object/BooleanType.java @@ -0,0 +1,71 @@ +// +// MessagePack for Java +// +// Copyright (C) 2009-2010 FURUHASHI Sadayuki +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +package org.msgpack.object; + +import java.io.IOException; +import org.msgpack.*; + +public class BooleanType extends MessagePackObject { + private boolean value; + + BooleanType(boolean value) { + this.value = value; + } + + public static BooleanType create(boolean value) { + return new BooleanType(value); + } + + @Override + public boolean isBooleanType() { + return true; + } + + @Override + public boolean asBoolean() { + return value; + } + + @Override + public void messagePack(Packer pk) throws IOException { + pk.packBoolean(value); + } + + @Override + public boolean equals(Object obj) { + if(obj.getClass() != getClass()) { + return false; + } + return ((BooleanType)obj).value == value; + } + + @Override + public int hashCode() { + if(value) { + return 1231; + } else { + return 1237; + } + } + + @Override + public Object clone() { + return new BooleanType(value); + } +} + diff --git a/java/src/main/java/org/msgpack/object/DoubleTypeIMPL.java b/java/src/main/java/org/msgpack/object/DoubleTypeIMPL.java new file mode 100644 index 00000000..fd38089e --- /dev/null +++ b/java/src/main/java/org/msgpack/object/DoubleTypeIMPL.java @@ -0,0 +1,101 @@ +// +// MessagePack for Java +// +// Copyright (C) 2009-2010 FURUHASHI Sadayuki +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +package org.msgpack.object; + +import java.math.BigInteger; +import java.io.IOException; +import org.msgpack.*; + +class DoubleTypeIMPL extends FloatType { + private double value; + + public DoubleTypeIMPL(double value) { + this.value = value; + } + + @Override + public float asFloat() { + // FIXME check overflow, underflow? + return (float)value; + } + + @Override + public double asDouble() { + return value; + } + + @Override + public byte byteValue() { + return (byte)value; + } + + @Override + public short shortValue() { + return (short)value; + } + + @Override + public int intValue() { + return (int)value; + } + + @Override + public long longValue() { + return (long)value; + } + + @Override + public BigInteger bigIntegerValue() { + return BigInteger.valueOf((long)value); + } + + @Override + public float floatValue() { + return (float)value; + } + + @Override + public double doubleValue() { + return (double)value; + } + + @Override + public void messagePack(Packer pk) throws IOException { + pk.packDouble(value); + } + + @Override + public boolean equals(Object obj) { + if(obj.getClass() != getClass()) { + return false; + } + return ((DoubleTypeIMPL)obj).value == value; + } + + @Override + public int hashCode() { + long v = Double.doubleToLongBits(value); + return (int)(v^(v>>>32)); + } + + @Override + public Object clone() { + return new DoubleTypeIMPL(value); + } +} + diff --git a/java/src/main/java/org/msgpack/schema/IMapSchema.java b/java/src/main/java/org/msgpack/object/FloatType.java similarity index 65% rename from java/src/main/java/org/msgpack/schema/IMapSchema.java rename to java/src/main/java/org/msgpack/object/FloatType.java index 3a2f556c..514efd5b 100644 --- a/java/src/main/java/org/msgpack/schema/IMapSchema.java +++ b/java/src/main/java/org/msgpack/object/FloatType.java @@ -15,13 +15,22 @@ // See the License for the specific language governing permissions and // limitations under the License. // -package org.msgpack.schema; +package org.msgpack.object; -import org.msgpack.Schema; +import org.msgpack.*; -public interface IMapSchema { - public Schema getKeySchema(); - public Schema getValueSchema(); - public Object createFromMap(Object[] obj); +public abstract class FloatType extends MessagePackObject { + @Override + public boolean isFloatType() { + return true; + } + + public static FloatType create(float value) { + return new FloatTypeIMPL(value); + } + + public static FloatType create(double value) { + return new DoubleTypeIMPL(value); + } } diff --git a/java/src/main/java/org/msgpack/object/FloatTypeIMPL.java b/java/src/main/java/org/msgpack/object/FloatTypeIMPL.java new file mode 100644 index 00000000..1d799611 --- /dev/null +++ b/java/src/main/java/org/msgpack/object/FloatTypeIMPL.java @@ -0,0 +1,94 @@ +// +// MessagePack for Java +// +// Copyright (C) 2009-2010 FURUHASHI Sadayuki +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +package org.msgpack.object; + +import java.math.BigInteger; +import java.io.IOException; +import org.msgpack.*; + +class FloatTypeIMPL extends FloatType { + private float value; + + public FloatTypeIMPL(float value) { + this.value = value; + } + + @Override + public float asFloat() { + return value; + } + + @Override + public double asDouble() { + return (double)value; + } + + @Override + public byte byteValue() { + return (byte)value; + } + + @Override + public short shortValue() { + return (short)value; + } + + @Override + public int intValue() { + return (int)value; + } + + @Override + public long longValue() { + return (long)value; + } + + @Override + public float floatValue() { + return (float)value; + } + + @Override + public double doubleValue() { + return (double)value; + } + + @Override + public void messagePack(Packer pk) throws IOException { + pk.packFloat(value); + } + + @Override + public boolean equals(Object obj) { + if(obj.getClass() != getClass()) { + return false; + } + return ((FloatTypeIMPL)obj).value == value; + } + + @Override + public int hashCode() { + return Float.floatToIntBits(value); + } + + @Override + public Object clone() { + return new FloatTypeIMPL(value); + } +} + diff --git a/java/src/main/java/org/msgpack/object/IntegerType.java b/java/src/main/java/org/msgpack/object/IntegerType.java new file mode 100644 index 00000000..43357e8e --- /dev/null +++ b/java/src/main/java/org/msgpack/object/IntegerType.java @@ -0,0 +1,49 @@ +// +// MessagePack for Java +// +// Copyright (C) 2009-2010 FURUHASHI Sadayuki +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +package org.msgpack.object; + +import java.math.BigInteger; +import org.msgpack.*; + +public abstract class IntegerType extends MessagePackObject { + @Override + public boolean isIntegerType() { + return true; + } + + public static IntegerType create(byte value) { + return new ShortIntegerTypeIMPL((int)value); + } + + public static IntegerType create(short value) { + return new ShortIntegerTypeIMPL((int)value); + } + + public static IntegerType create(int value) { + return new ShortIntegerTypeIMPL(value); + } + + public static IntegerType create(long value) { + return new LongIntegerTypeIMPL(value); + } + + public static IntegerType create(BigInteger value) { + return new BigIntegerTypeIMPL(value); + } +} + diff --git a/java/src/main/java/org/msgpack/object/LongIntegerTypeIMPL.java b/java/src/main/java/org/msgpack/object/LongIntegerTypeIMPL.java new file mode 100644 index 00000000..d052e77a --- /dev/null +++ b/java/src/main/java/org/msgpack/object/LongIntegerTypeIMPL.java @@ -0,0 +1,128 @@ +// +// MessagePack for Java +// +// Copyright (C) 2009-2010 FURUHASHI Sadayuki +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +package org.msgpack.object; + +import java.math.BigInteger; +import java.io.IOException; +import org.msgpack.*; + +class LongIntegerTypeIMPL extends IntegerType { + private long value; + + LongIntegerTypeIMPL(long value) { + this.value = value; + } + + @Override + public byte asByte() { + if(value > (long)Byte.MAX_VALUE) { + throw new MessageTypeException("type error"); + } + return (byte)value; + } + + @Override + public short asShort() { + if(value > (long)Short.MAX_VALUE) { + throw new MessageTypeException("type error"); + } + return (short)value; + } + + @Override + public int asInt() { + if(value > (long)Integer.MAX_VALUE) { + throw new MessageTypeException("type error"); + } + return (int)value; + } + + @Override + public long asLong() { + return value; + } + + @Override + public BigInteger asBigInteger() { + return BigInteger.valueOf(value); + } + + @Override + public byte byteValue() { + return (byte)value; + } + + @Override + public short shortValue() { + return (short)value; + } + + @Override + public int intValue() { + return (int)value; + } + + @Override + public long longValue() { + return (long)value; + } + + @Override + public BigInteger bigIntegerValue() { + return BigInteger.valueOf(value); + } + + @Override + public float floatValue() { + return (float)value; + } + + @Override + public double doubleValue() { + return (double)value; + } + + @Override + public void messagePack(Packer pk) throws IOException { + pk.packLong(value); + } + + @Override + public boolean equals(Object obj) { + if(obj.getClass() != getClass()) { + if(obj.getClass() == ShortIntegerTypeIMPL.class) { + return value == ((ShortIntegerTypeIMPL)obj).longValue(); + } else if(obj.getClass() == BigIntegerTypeIMPL.class) { + return BigInteger.valueOf(value).equals(((BigIntegerTypeIMPL)obj).bigIntegerValue()); + } + return false; + } + return ((LongIntegerTypeIMPL)obj).value == value; + } + + @Override + public int hashCode() { + return (int)(value^(value>>>32)); + } + + @Override + public Object clone() { + return new LongIntegerTypeIMPL(value); + } +} + diff --git a/java/src/main/java/org/msgpack/object/MapType.java b/java/src/main/java/org/msgpack/object/MapType.java new file mode 100644 index 00000000..148c0bf4 --- /dev/null +++ b/java/src/main/java/org/msgpack/object/MapType.java @@ -0,0 +1,84 @@ +// +// MessagePack for Java +// +// Copyright (C) 2009-2010 FURUHASHI Sadayuki +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +package org.msgpack.object; + +import java.util.HashMap; +import java.util.Map; +import java.util.Arrays; +import java.io.IOException; +import org.msgpack.*; + +public class MapType extends MessagePackObject { + private MessagePackObject[] map; + + MapType(MessagePackObject[] map) { + this.map = map; + } + + public static MapType create(MessagePackObject[] map) { + return new MapType(map); + } + + @Override + public boolean isMapType() { + return true; + } + + @Override + public Map asMap() { + HashMap m = new HashMap(map.length / 2); + int i = 0; + while(i < map.length) { + MessagePackObject k = map[i++]; + MessagePackObject v = map[i++]; + m.put(k, v); + } + return m; + } + + @Override + public void messagePack(Packer pk) throws IOException { + pk.packMap(map.length / 2); + for(int i=0; i < map.length; i++) { + map[i].messagePack(pk); + } + } + + @Override + public boolean equals(Object obj) { + if(obj.getClass() != getClass()) { + return false; + } + return Arrays.equals(((MapType)obj).map, map); + } + + @Override + public int hashCode() { + return map.hashCode(); + } + + @Override + public Object clone() { + MessagePackObject[] copy = new MessagePackObject[map.length]; + for(int i=0; i < map.length; i++) { + copy[i] = (MessagePackObject)map[i].clone(); + } + return copy; + } +} + diff --git a/java/src/main/java/org/msgpack/object/NilType.java b/java/src/main/java/org/msgpack/object/NilType.java new file mode 100644 index 00000000..c443db18 --- /dev/null +++ b/java/src/main/java/org/msgpack/object/NilType.java @@ -0,0 +1,60 @@ +// +// MessagePack for Java +// +// Copyright (C) 2009-2010 FURUHASHI Sadayuki +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +package org.msgpack.object; + +import java.io.IOException; +import org.msgpack.*; + +public class NilType extends MessagePackObject { + private final static NilType INSTANCE = new NilType(); + + public static NilType create() { + return INSTANCE; + } + + private NilType() { } + + @Override + public boolean isNil() { + return true; + } + + @Override + public void messagePack(Packer pk) throws IOException { + pk.packNil(); + } + + @Override + public boolean equals(Object obj) { + if(obj.getClass() != getClass()) { + return false; + } + return true; + } + + @Override + public int hashCode() { + return 0; + } + + @Override + public Object clone() { + return INSTANCE; + } +} + diff --git a/java/src/main/java/org/msgpack/object/RawType.java b/java/src/main/java/org/msgpack/object/RawType.java new file mode 100644 index 00000000..26f6e0d9 --- /dev/null +++ b/java/src/main/java/org/msgpack/object/RawType.java @@ -0,0 +1,90 @@ +// +// MessagePack for Java +// +// Copyright (C) 2009-2010 FURUHASHI Sadayuki +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +package org.msgpack.object; + +import java.util.Arrays; +import java.io.IOException; +import org.msgpack.*; + +public class RawType extends MessagePackObject { + private byte[] bytes; + + RawType(byte[] bytes) { + this.bytes = bytes; + } + + RawType(String str) { + try { + this.bytes = str.getBytes("UTF-8"); + } catch (Exception e) { + throw new MessageTypeException("type error"); + } + } + + public static RawType create(byte[] bytes) { + return new RawType(bytes); + } + + public static RawType create(String str) { + return new RawType(str); + } + + @Override + public boolean isRawType() { + return true; + } + + @Override + public byte[] asByteArray() { + return bytes; + } + + @Override + public String asString() { + try { + return new String(bytes, "UTF-8"); + } catch (Exception e) { + throw new MessageTypeException("type error"); + } + } + + @Override + public void messagePack(Packer pk) throws IOException { + pk.packRaw(bytes.length); + pk.packRawBody(bytes); + } + + @Override + public boolean equals(Object obj) { + if(obj.getClass() != getClass()) { + return false; + } + return Arrays.equals(((RawType)obj).bytes, bytes); + } + + @Override + public int hashCode() { + return bytes.hashCode(); + } + + @Override + public Object clone() { + return new RawType((byte[])bytes.clone()); + } +} + diff --git a/java/src/main/java/org/msgpack/object/ShortIntegerTypeIMPL.java b/java/src/main/java/org/msgpack/object/ShortIntegerTypeIMPL.java new file mode 100644 index 00000000..dbed4263 --- /dev/null +++ b/java/src/main/java/org/msgpack/object/ShortIntegerTypeIMPL.java @@ -0,0 +1,125 @@ +// +// MessagePack for Java +// +// Copyright (C) 2009-2010 FURUHASHI Sadayuki +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +package org.msgpack.object; + +import java.math.BigInteger; +import java.io.IOException; +import org.msgpack.*; + +class ShortIntegerTypeIMPL extends IntegerType { + private int value; + + ShortIntegerTypeIMPL(int value) { + this.value = value; + } + + @Override + public byte asByte() { + if(value > (int)Byte.MAX_VALUE) { + throw new MessageTypeException("type error"); + } + return (byte)value; + } + + @Override + public short asShort() { + if(value > (int)Short.MAX_VALUE) { + throw new MessageTypeException("type error"); + } + return (short)value; + } + + @Override + public int asInt() { + return value; + } + + @Override + public long asLong() { + return value; + } + + @Override + public BigInteger asBigInteger() { + return BigInteger.valueOf((long)value); + } + + @Override + public byte byteValue() { + return (byte)value; + } + + @Override + public short shortValue() { + return (short)value; + } + + @Override + public int intValue() { + return (int)value; + } + + @Override + public long longValue() { + return (long)value; + } + + @Override + public BigInteger bigIntegerValue() { + return BigInteger.valueOf((long)value); + } + + @Override + public float floatValue() { + return (float)value; + } + + @Override + public double doubleValue() { + return (double)value; + } + + @Override + public void messagePack(Packer pk) throws IOException { + pk.packInt(value); + } + + @Override + public boolean equals(Object obj) { + if(obj.getClass() != getClass()) { + if(obj.getClass() == LongIntegerTypeIMPL.class) { + return (long)value == ((LongIntegerTypeIMPL)obj).longValue(); + } else if(obj.getClass() == BigIntegerTypeIMPL.class) { + return ((BigIntegerTypeIMPL)obj).bigIntegerValue().equals(BigInteger.valueOf((long)value)); + } + return false; + } + return ((ShortIntegerTypeIMPL)obj).value == value; + } + + @Override + public int hashCode() { + return value; + } + + @Override + public Object clone() { + return new ShortIntegerTypeIMPL(value); + } +} + diff --git a/java/src/main/java/org/msgpack/package-info.java b/java/src/main/java/org/msgpack/package-info.java new file mode 100644 index 00000000..7e9b8a27 --- /dev/null +++ b/java/src/main/java/org/msgpack/package-info.java @@ -0,0 +1,8 @@ +/** + * MessagePack is a binary-based efficient object serialization library. + * It enables to exchange structured objects between many languages like JSON. + * But unlike JSON, it is very fast and small. + * + * Use {@link Packer} to serialize and {@link Unpacker} to deserialize. + */ +package org.msgpack; diff --git a/java/src/main/java/org/msgpack/schema/ArraySchema.java b/java/src/main/java/org/msgpack/schema/ArraySchema.java deleted file mode 100644 index fd471439..00000000 --- a/java/src/main/java/org/msgpack/schema/ArraySchema.java +++ /dev/null @@ -1,125 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.schema; - -import java.util.Arrays; -import java.util.Collection; -import java.util.Set; -import java.util.List; -import java.util.ArrayList; -import java.util.RandomAccess; -import java.io.IOException; -import org.msgpack.*; - -public class ArraySchema extends Schema implements IArraySchema { - private Schema elementSchema; - - public ArraySchema(Schema elementSchema) - { - super("array"); - this.elementSchema = elementSchema; - } - - @Override - public String getFullName() - { - return "List<"+elementSchema.getFullName()+">"; - } - - @Override - public String getExpression() - { - return "(array "+elementSchema.getExpression()+")"; - } - - @Override - @SuppressWarnings("unchecked") - public void pack(Packer pk, Object obj) throws IOException - { - if(obj instanceof List) { - ArrayList d = (ArrayList)obj; - pk.packArray(d.size()); - if(obj instanceof RandomAccess) { - for(int i=0; i < d.size(); ++i) { - elementSchema.pack(pk, d.get(i)); - } - } else { - for(Object e : d) { - elementSchema.pack(pk, e); - } - } - - } else if(obj instanceof Set) { - Set d = (Set)obj; - pk.packArray(d.size()); - for(Object e : d) { - elementSchema.pack(pk, e); - } - - } else if(obj == null) { - pk.packNil(); - - } else { - throw MessageTypeException.invalidConvert(obj, this); - } - } - - @Override - @SuppressWarnings("unchecked") - public Object convert(Object obj) throws MessageTypeException - { - if(obj instanceof List) { - List d = (List)obj; - ArrayList ar = new ArrayList(d.size()); - if(obj instanceof RandomAccess) { - for(int i=0; i < d.size(); ++i) { - ar.add( elementSchema.convert(d.get(i)) ); - } - } else { - for(Object e : d) { - ar.add( elementSchema.convert(e) ); - } - } - return ar; - - } else if(obj instanceof Collection) { - Collection d = (Collection)obj; - ArrayList ar = new ArrayList(d.size()); - for(Object e : (Collection)obj) { - ar.add( elementSchema.convert(e) ); - } - return ar; - - } else { - throw MessageTypeException.invalidConvert(obj, this); - } - } - - @Override - public Schema getElementSchema(int index) - { - return elementSchema; - } - - @Override - public Object createFromArray(Object[] obj) - { - return Arrays.asList(obj); - } -} - diff --git a/java/src/main/java/org/msgpack/schema/ByteSchema.java b/java/src/main/java/org/msgpack/schema/ByteSchema.java deleted file mode 100644 index 9ee6a82b..00000000 --- a/java/src/main/java/org/msgpack/schema/ByteSchema.java +++ /dev/null @@ -1,89 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.schema; - -import java.io.IOException; -import org.msgpack.*; - -public class ByteSchema extends Schema { - public ByteSchema() { - super("Byte"); - } - - @Override - public String getExpression() { - return "byte"; - } - - @Override - public void pack(Packer pk, Object obj) throws IOException { - if(obj instanceof Number) { - pk.packByte( ((Number)obj).byteValue() ); - - } else if(obj == null) { - pk.packNil(); - - } else { - throw MessageTypeException.invalidConvert(obj, this); - } - } - - @Override - public Object convert(Object obj) throws MessageTypeException { - if(obj instanceof Byte) { - return obj; - - } else if(obj instanceof Number) { - return ((Number)obj).byteValue(); - - } else { - throw MessageTypeException.invalidConvert(obj, this); - } - } - - @Override - public Object createFromByte(byte v) { - return (byte)v; - } - - @Override - public Object createFromShort(short v) { - return (byte)v; - } - - @Override - public Object createFromInt(int v) { - return (byte)v; - } - - @Override - public Object createFromLong(long v) { - return (byte)v; - } - - @Override - public Object createFromFloat(float v) { - return (byte)v; - } - - @Override - public Object createFromDouble(double v) { - return (byte)v; - } -} - diff --git a/java/src/main/java/org/msgpack/schema/ClassGenerator.java b/java/src/main/java/org/msgpack/schema/ClassGenerator.java deleted file mode 100644 index 061dcbb3..00000000 --- a/java/src/main/java/org/msgpack/schema/ClassGenerator.java +++ /dev/null @@ -1,241 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.schema; - -import java.util.ArrayList; -import java.util.List; -import java.io.IOException; -import java.io.File; -import java.io.Writer; -import org.msgpack.*; - -public class ClassGenerator { - private ClassSchema schema; - private Writer writer; - private int indent; - - private ClassGenerator(Writer writer) { - this.writer = writer; - this.indent = 0; - } - - public static void write(Schema schema, Writer dest) throws IOException { - if(!(schema instanceof ClassSchema)) { - throw new RuntimeException("schema is not class schema"); - } - ClassSchema cs = (ClassSchema)schema; - new ClassGenerator(dest).run(cs); - } - - private void run(ClassSchema cs) throws IOException { - List subclasses = new ArrayList(); - for(FieldSchema f : cs.getFields()) { - findSubclassSchema(subclasses, f.getSchema()); - } - - for(ClassSchema sub : subclasses) { - sub.setNamespace(cs.getNamespace()); - sub.setImports(cs.getImports()); - } - - this.schema = cs; - - writeHeader(); - - writeClass(); - - for(ClassSchema sub : subclasses) { - this.schema = sub; - writeSubclass(); - } - - writeFooter(); - - this.schema = null; - writer.flush(); - } - - private void findSubclassSchema(List dst, Schema s) { - if(s instanceof ClassSchema) { - ClassSchema cs = (ClassSchema)s; - if(!dst.contains(cs)) { dst.add(cs); } - for(FieldSchema f : cs.getFields()) { - findSubclassSchema(dst, f.getSchema()); - } - } else if(s instanceof ArraySchema) { - ArraySchema as = (ArraySchema)s; - findSubclassSchema(dst, as.getElementSchema(0)); - } else if(s instanceof MapSchema) { - MapSchema as = (MapSchema)s; - findSubclassSchema(dst, as.getKeySchema()); - findSubclassSchema(dst, as.getValueSchema()); - } - } - - private void writeHeader() throws IOException { - if(schema.getNamespace() != null) { - line("package "+schema.getNamespace()+";"); - line(); - } - line("import java.util.*;"); - line("import java.io.*;"); - line("import org.msgpack.*;"); - line("import org.msgpack.schema.ClassSchema;"); - line("import org.msgpack.schema.FieldSchema;"); - } - - private void writeFooter() throws IOException { - line(); - } - - private void writeClass() throws IOException { - line(); - line("public final class "+schema.getName()+" implements MessagePackable, MessageMergeable"); - line("{"); - pushIndent(); - writeSchema(); - writeMemberVariables(); - writeMemberFunctions(); - popIndent(); - line("}"); - } - - private void writeSubclass() throws IOException { - line(); - line("final class "+schema.getName()+" implements MessagePackable, MessageMergeable"); - line("{"); - pushIndent(); - writeSchema(); - writeMemberVariables(); - writeMemberFunctions(); - popIndent(); - line("}"); - } - - private void writeSchema() throws IOException { - line("private static final ClassSchema _SCHEMA = (ClassSchema)Schema.load(\""+schema.getExpression()+"\");"); - line("public static ClassSchema getSchema() { return _SCHEMA; }"); - } - - private void writeMemberVariables() throws IOException { - line(); - for(FieldSchema f : schema.getFields()) { - line("public "+f.getSchema().getFullName()+" "+f.getName()+";"); - } - } - - private void writeMemberFunctions() throws IOException { - // void messagePack(Packer pk) - // boolean equals(Object obj) - // int hashCode() - // void set(int _index, Object _value) - // Object get(int _index); - // getXxx() - // setXxx(Xxx xxx) - writeConstructors(); - writeAccessors(); - writePackFunction(); - writeMergeFunction(); - writeFactoryFunction(); - } - - private void writeConstructors() throws IOException { - line(); - line("public "+schema.getName()+"() { }"); - } - - private void writeAccessors() throws IOException { - // FIXME - //line(); - //for(FieldSchema f : schema.getFields()) { - // line(""); - //} - } - - private void writePackFunction() throws IOException { - line(); - line("@Override"); - line("public void messagePack(Packer _pk) throws IOException"); - line("{"); - pushIndent(); - line("_pk.packArray("+schema.getFields().length+");"); - line("FieldSchema[] _fields = _SCHEMA.getFields();"); - int i = 0; - for(FieldSchema f : schema.getFields()) { - line("_fields["+i+"].getSchema().pack(_pk, "+f.getName()+");"); - ++i; - } - popIndent(); - line("}"); - } - - private void writeMergeFunction() throws IOException { - line(); - line("@Override"); - line("@SuppressWarnings(\"unchecked\")"); - line("public void messageMerge(Object obj) throws MessageTypeException"); - line("{"); - pushIndent(); - line("Object[] _source = ((List)obj).toArray();"); - line("FieldSchema[] _fields = _SCHEMA.getFields();"); - int i = 0; - for(FieldSchema f : schema.getFields()) { - line("if(_source.length <= "+i+") { return; } this."+f.getName()+" = ("+f.getSchema().getFullName()+")_fields["+i+"].getSchema().convert(_source["+i+"]);"); - ++i; - } - popIndent(); - line("}"); - } - - private void writeFactoryFunction() throws IOException { - line(); - line("@SuppressWarnings(\"unchecked\")"); - line("public static "+schema.getName()+" createFromMessage(Object[] _message)"); - line("{"); - pushIndent(); - line(schema.getName()+" _self = new "+schema.getName()+"();"); - int i = 0; - for(FieldSchema f : schema.getFields()) { - line("if(_message.length <= "+i+") { return _self; } _self."+f.getName()+" = ("+f.getSchema().getFullName()+")_message["+i+"];"); - ++i; - } - line("return _self;"); - popIndent(); - line("}"); - } - - private void line(String str) throws IOException { - for(int i=0; i < indent; ++i) { - writer.write("\t"); - } - writer.write(str+"\n"); - } - - private void line() throws IOException { - writer.write("\n"); - } - - private void pushIndent() { - indent += 1; - } - - private void popIndent() { - indent -= 1; - } -} - diff --git a/java/src/main/java/org/msgpack/schema/ClassSchema.java b/java/src/main/java/org/msgpack/schema/ClassSchema.java deleted file mode 100644 index cd5c008e..00000000 --- a/java/src/main/java/org/msgpack/schema/ClassSchema.java +++ /dev/null @@ -1,95 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.schema; - -import java.util.Arrays; -import java.util.List; -import org.msgpack.*; - -public abstract class ClassSchema extends Schema implements IArraySchema { - protected FieldSchema[] fields; - protected List imports; - protected String namespace; - protected String fqdn; - - public ClassSchema( - String name, String namespace, - List imports, List fields) { - super(name); - this.namespace = namespace; - this.imports = imports; // FIXME clone? - this.fields = new FieldSchema[fields.size()]; - System.arraycopy(fields.toArray(), 0, this.fields, 0, fields.size()); - if(namespace == null) { - this.fqdn = name; - } else { - this.fqdn = namespace+"."+name; - } - } - - public final FieldSchema[] getFields() { - return fields; - } - - String getNamespace() { - return namespace; - } - - List getImports() { - return imports; - } - - void setNamespace(String namespace) { - this.namespace = namespace; - } - - void setImports(List imports) { - this.imports = imports; // FIXME clone? - } - - //@Override - //public String getFullName() - //{ - // if(namespace == null) { - // return getName(); - // } else { - // return namespace+"."+getName(); - // } - //} - - @Override - public String getExpression() { - StringBuffer b = new StringBuffer(); - b.append("(class "); - b.append(getName()); - if(namespace != null) { - b.append(" (package "+namespace+")"); - } - for(FieldSchema f : fields) { - b.append(" "+f.getExpression()); - } - b.append(")"); - return b.toString(); - } - - public boolean equals(SpecificClassSchema o) { - return (namespace != null ? namespace.equals(o.getNamespace()) : o.getNamespace() == null) && - getName().equals(o.getName()); - } -} - diff --git a/java/src/main/java/org/msgpack/schema/DoubleSchema.java b/java/src/main/java/org/msgpack/schema/DoubleSchema.java deleted file mode 100644 index d53e47d5..00000000 --- a/java/src/main/java/org/msgpack/schema/DoubleSchema.java +++ /dev/null @@ -1,84 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.schema; - -import java.io.IOException; -import org.msgpack.*; - -public class DoubleSchema extends Schema { - public DoubleSchema() { - super("Double"); - } - - @Override - public String getExpression() { - return "double"; - } - - @Override - public void pack(Packer pk, Object obj) throws IOException { - if(obj instanceof Number) { - pk.packDouble( ((Number)obj).doubleValue() ); - - } else if(obj == null) { - pk.packNil(); - - } else { - throw MessageTypeException.invalidConvert(obj, this); - } - } - - @Override - public Object convert(Object obj) throws MessageTypeException { - if(obj instanceof Double) { - return obj; - - } else if(obj instanceof Number) { - return ((Number)obj).doubleValue(); - - } else { - throw MessageTypeException.invalidConvert(obj, this); - } - } - - @Override - public Object createFromByte(byte v) { - return (double)v; - } - - @Override - public Object createFromShort(short v) { - return (double)v; - } - - @Override - public Object createFromInt(int v) { - return (double)v; - } - - @Override - public Object createFromFloat(float v) { - return (double)v; - } - - @Override - public Object createFromDouble(double v) { - return (double)v; - } -} - diff --git a/java/src/main/java/org/msgpack/schema/FloatSchema.java b/java/src/main/java/org/msgpack/schema/FloatSchema.java deleted file mode 100644 index 27775218..00000000 --- a/java/src/main/java/org/msgpack/schema/FloatSchema.java +++ /dev/null @@ -1,84 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.schema; - -import java.io.IOException; -import org.msgpack.*; - -public class FloatSchema extends Schema { - public FloatSchema() { - super("Float"); - } - - @Override - public String getExpression() { - return "float"; - } - - @Override - public void pack(Packer pk, Object obj) throws IOException { - if(obj instanceof Number) { - pk.packFloat( ((Number)obj).floatValue() ); - - } else if(obj == null) { - pk.packNil(); - - } else { - throw MessageTypeException.invalidConvert(obj, this); - } - } - - @Override - public Object convert(Object obj) throws MessageTypeException { - if(obj instanceof Float) { - return obj; - - } else if(obj instanceof Number) { - return ((Number)obj).floatValue(); - - } else { - throw MessageTypeException.invalidConvert(obj, this); - } - } - - @Override - public Object createFromByte(byte v) { - return (float)v; - } - - @Override - public Object createFromShort(short v) { - return (float)v; - } - - @Override - public Object createFromInt(int v) { - return (float)v; - } - - @Override - public Object createFromFloat(float v) { - return (float)v; - } - - @Override - public Object createFromDouble(double v) { - return (float)v; - } -} - diff --git a/java/src/main/java/org/msgpack/schema/GenericClassSchema.java b/java/src/main/java/org/msgpack/schema/GenericClassSchema.java deleted file mode 100644 index ffdd4aba..00000000 --- a/java/src/main/java/org/msgpack/schema/GenericClassSchema.java +++ /dev/null @@ -1,91 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.schema; - -import java.util.Collection; -import java.util.List; -import java.util.Map; -import java.util.HashMap; -import java.io.IOException; -import org.msgpack.*; - -public class GenericClassSchema extends ClassSchema { - public GenericClassSchema( - String name, String namespace, - List imports, List fields) { - super(name, namespace, imports, fields); - } - - @Override - @SuppressWarnings("unchecked") - public void pack(Packer pk, Object obj) throws IOException { - if(obj instanceof Map) { - Map d = (Map)obj; - pk.packArray(fields.length); - for(int i=0; i < fields.length; ++i) { - FieldSchema f = fields[i]; - f.getSchema().pack(pk, d.get(f.getName())); - } - - } else if(obj == null) { - pk.packNil(); - - } else { - throw MessageTypeException.invalidConvert(obj, this); - } - } - - @Override - public Object convert(Object obj) throws MessageTypeException { - if(obj instanceof Collection) { - // FIXME optimize - return createFromArray( ((Collection)obj).toArray() ); - - } else if(obj instanceof Map) { - HashMap m = new HashMap(fields.length); - Map d = (Map)obj; - for(int i=0; i < fields.length; ++i) { - FieldSchema f = fields[i]; - String fieldName = f.getName(); - m.put(fieldName, f.getSchema().convert(d.get(fieldName))); - } - return m; - - } else { - throw MessageTypeException.invalidConvert(obj, this); - } - } - - public Schema getElementSchema(int index) { - // FIXME check index < fields.length - return fields[index].getSchema(); - } - - public Object createFromArray(Object[] obj) { - HashMap m = new HashMap(fields.length); - int i=0; - for(; i < obj.length; ++i) { - m.put(fields[i].getName(), obj[i]); - } - for(; i < fields.length; ++i) { - m.put(fields[i].getName(), null); - } - return m; - } -} - diff --git a/java/src/main/java/org/msgpack/schema/GenericSchema.java b/java/src/main/java/org/msgpack/schema/GenericSchema.java deleted file mode 100644 index 0adf8980..00000000 --- a/java/src/main/java/org/msgpack/schema/GenericSchema.java +++ /dev/null @@ -1,192 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.schema; - -import java.util.Arrays; -import java.util.List; -import java.util.HashMap; -import java.io.IOException; -import org.msgpack.*; -//import org.msgpack.generic.*; - -public class GenericSchema extends Schema implements IArraySchema, IMapSchema { - public GenericSchema() { - super("Object"); - } - - @Override - public String getExpression() { - return "object"; - } - - @Override - public void pack(Packer pk, Object obj) throws IOException { - pk.pack(obj); - } - - @Override - public Object convert(Object obj) throws MessageTypeException { - return obj; - } - - @Override - public Schema getElementSchema(int index) { - return this; - } - - @Override - public Schema getKeySchema() { - return this; - } - - @Override - public Schema getValueSchema() { - return this; - } - - @Override - public Object createFromNil() { - return null; - } - - @Override - public Object createFromBoolean(boolean v) { - return v; - } - - @Override - public Object createFromByte(byte v) { - return v; - } - - @Override - public Object createFromShort(short v) { - return v; - } - - @Override - public Object createFromInt(int v) { - return v; - } - - @Override - public Object createFromLong(long v) { - return v; - } - - @Override - public Object createFromFloat(float v) { - return v; - } - - @Override - public Object createFromDouble(double v) { - return v; - } - - @Override - public Object createFromRaw(byte[] b, int offset, int length) { - byte[] bytes = new byte[length]; - System.arraycopy(b, offset, bytes, 0, length); - return bytes; - } - - @Override - public Object createFromArray(Object[] obj) { - return Arrays.asList(obj); - } - - @Override - @SuppressWarnings("unchecked") - public Object createFromMap(Object[] obj) { - HashMap m = new HashMap(obj.length / 2); - int i = 0; - while(i < obj.length) { - Object k = obj[i++]; - Object v = obj[i++]; - m.put(k, v); - } - return m; - } - - /* - @Override - public Object createFromNil() { - return null; - } - - @Override - public Object createFromBoolean(boolean v) { - return new GenericBoolean(v); - } - - @Override - public Object createFromFromByte(byte v) { - return new GenericByte(v); - } - - @Override - public Object createFromShort(short v) { - return new GenericShort(v); - } - - @Override - public Object createFromInt(int v) { - return new GenericInt(v); - } - - @Override - public Object createFromLong(long v) { - return new GenericLong(v); - } - - @Override - public Object createFromFloat(float v) { - return new GenericFloat(v); - } - - @Override - public Object createFromDouble(double v) { - return new GenericDouble(v); - } - - @Override - public Object createFromRaw(byte[] b, int offset, int length) { - return new GenericRaw(b, offset, length); - } - - @Override - public Object createFromArray(Object[] obj) { - // FIXME GenericArray - return Arrays.asList(obj); - } - - @Override - public Object createFromMap(Object[] obj) { - GenericMap m = new GenericMap(obj.length / 2); - int i = 0; - while(i < obj.length) { - Object k = obj[i++]; - Object v = obj[i++]; - m.put(k, v); - } - return m; - } - */ -} - diff --git a/java/src/main/java/org/msgpack/schema/IntSchema.java b/java/src/main/java/org/msgpack/schema/IntSchema.java deleted file mode 100644 index 5a7e2815..00000000 --- a/java/src/main/java/org/msgpack/schema/IntSchema.java +++ /dev/null @@ -1,89 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.schema; - -import java.io.IOException; -import org.msgpack.*; - -public class IntSchema extends Schema { - public IntSchema() { - super("Integer"); - } - - @Override - public String getExpression() { - return "int"; - } - - @Override - public void pack(Packer pk, Object obj) throws IOException { - if(obj instanceof Number) { - pk.packInt( ((Number)obj).intValue() ); - - } else if(obj == null) { - pk.packNil(); - - } else { - throw MessageTypeException.invalidConvert(obj, this); - } - } - - @Override - public Object convert(Object obj) throws MessageTypeException { - if(obj instanceof Integer) { - return obj; - - } else if(obj instanceof Number) { - return ((Number)obj).intValue(); - - } else { - throw MessageTypeException.invalidConvert(obj, this); - } - } - - @Override - public Object createFromByte(byte v) { - return (int)v; - } - - @Override - public Object createFromShort(short v) { - return (int)v; - } - - @Override - public Object createFromInt(int v) { - return (int)v; - } - - @Override - public Object createFromLong(long v) { - return (int)v; - } - - @Override - public Object createFromFloat(float v) { - return (int)v; - } - - @Override - public Object createFromDouble(double v) { - return (int)v; - } -} - diff --git a/java/src/main/java/org/msgpack/schema/LongSchema.java b/java/src/main/java/org/msgpack/schema/LongSchema.java deleted file mode 100644 index 83a30e37..00000000 --- a/java/src/main/java/org/msgpack/schema/LongSchema.java +++ /dev/null @@ -1,89 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.schema; - -import java.io.IOException; -import org.msgpack.*; - -public class LongSchema extends Schema { - public LongSchema() { - super("Long"); - } - - @Override - public String getExpression() { - return "long"; - } - - @Override - public void pack(Packer pk, Object obj) throws IOException { - if(obj instanceof Number) { - pk.packLong( ((Number)obj).longValue() ); - - } else if(obj == null) { - pk.packNil(); - - } else { - throw MessageTypeException.invalidConvert(obj, this); - } - } - - @Override - public Object convert(Object obj) throws MessageTypeException { - if(obj instanceof Long) { - return obj; - - } else if(obj instanceof Number) { - return ((Number)obj).longValue(); - - } else { - throw MessageTypeException.invalidConvert(obj, this); - } - } - - @Override - public Object createFromByte(byte v) { - return (long)v; - } - - @Override - public Object createFromShort(short v) { - return (long)v; - } - - @Override - public Object createFromInt(int v) { - return (long)v; - } - - @Override - public Object createFromLong(long v) { - return (long)v; - } - - @Override - public Object createFromFloat(float v) { - return (long)v; - } - - @Override - public Object createFromDouble(double v) { - return (long)v; - } -} - diff --git a/java/src/main/java/org/msgpack/schema/MapSchema.java b/java/src/main/java/org/msgpack/schema/MapSchema.java deleted file mode 100644 index ba759939..00000000 --- a/java/src/main/java/org/msgpack/schema/MapSchema.java +++ /dev/null @@ -1,103 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.schema; - -import java.util.Map; -import java.util.HashMap; -import java.io.IOException; -import org.msgpack.*; - -public class MapSchema extends Schema implements IMapSchema { - private Schema keySchema; - private Schema valueSchema; - - public MapSchema(Schema keySchema, Schema valueSchema) { - super("map"); - this.keySchema = keySchema; - this.valueSchema = valueSchema; - } - - @Override - public String getFullName() { - return "HashList<"+keySchema.getFullName()+", "+valueSchema.getFullName()+">"; - } - - @Override - public String getExpression() { - return "(map "+keySchema.getExpression()+" "+valueSchema.getExpression()+")"; - } - - @Override - @SuppressWarnings("unchecked") - public void pack(Packer pk, Object obj) throws IOException { - if(obj instanceof Map) { - Map d = (Map)obj; - pk.packMap(d.size()); - for(Map.Entry e : d.entrySet()) { - keySchema.pack(pk, e.getKey()); - valueSchema.pack(pk, e.getValue()); - } - - } else if(obj == null) { - pk.packNil(); - - } else { - throw MessageTypeException.invalidConvert(obj, this); - } - } - - @Override - @SuppressWarnings("unchecked") - public Object convert(Object obj) throws MessageTypeException { - if(obj instanceof Map) { - Map d = (Map)obj; - Map m = new HashMap(); - for(Map.Entry e : d.entrySet()) { - m.put(keySchema.convert(e.getKey()), valueSchema.convert(e.getValue())); - } - return m; - - } else { - throw MessageTypeException.invalidConvert(obj, this); - } - } - - @Override - public Schema getKeySchema() { - return keySchema; - } - - @Override - public Schema getValueSchema() { - return valueSchema; - } - - @Override - @SuppressWarnings("unchecked") - public Object createFromMap(Object[] obj) { - HashMap m = new HashMap(obj.length / 2); - int i = 0; - while(i < obj.length) { - Object k = obj[i++]; - Object v = obj[i++]; - m.put(k, v); - } - return m; - } -} - diff --git a/java/src/main/java/org/msgpack/schema/RawSchema.java b/java/src/main/java/org/msgpack/schema/RawSchema.java deleted file mode 100644 index f621e4c4..00000000 --- a/java/src/main/java/org/msgpack/schema/RawSchema.java +++ /dev/null @@ -1,105 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.schema; - -import java.nio.ByteBuffer; -import java.io.IOException; -import java.io.UnsupportedEncodingException; -import org.msgpack.*; - -public class RawSchema extends Schema { - public RawSchema() { - super("raw"); - } - - public String getFullName() { - return "byte[]"; - } - - @Override - public void pack(Packer pk, Object obj) throws IOException { - // FIXME instanceof GenericObject - if(obj instanceof byte[]) { - byte[] d = (byte[])obj; - pk.packRaw(d.length); - pk.packRawBody(d); - - } else if(obj instanceof ByteBuffer) { - ByteBuffer d = (ByteBuffer)obj; - if(!d.hasArray()) { - throw MessageTypeException.invalidConvert(obj, this); - } - pk.packRaw(d.capacity()); - pk.packRawBody(d.array(), d.position(), d.capacity()); - - } else if(obj instanceof String) { - try { - byte[] d = ((String)obj).getBytes("UTF-8"); - pk.packRaw(d.length); - pk.packRawBody(d); - } catch (UnsupportedEncodingException e) { - throw MessageTypeException.invalidConvert(obj, this); - } - - } else if(obj == null) { - pk.packNil(); - - } else { - throw MessageTypeException.invalidConvert(obj, this); - } - } - - @Override - public Object convert(Object obj) throws MessageTypeException { - // FIXME instanceof GenericObject - if(obj instanceof byte[]) { - // FIXME copy? - //byte[] d = (byte[])obj; - //byte[] v = new byte[d.length]; - //System.arraycopy(d, 0, v, 0, d.length); - //return v; - return obj; - - } else if(obj instanceof ByteBuffer) { - ByteBuffer d = (ByteBuffer)obj; - byte[] v = new byte[d.capacity()]; - int pos = d.position(); - d.get(v); - d.position(pos); - return v; - - } else if(obj instanceof String) { - try { - return ((String)obj).getBytes("UTF-8"); - } catch (UnsupportedEncodingException e) { - throw MessageTypeException.invalidConvert(obj, this); - } - - } else { - throw MessageTypeException.invalidConvert(obj, this); - } - } - - @Override - public Object createFromRaw(byte[] b, int offset, int length) { - byte[] d = new byte[length]; - System.arraycopy(b, offset, d, 0, length); - return d; - } -} - diff --git a/java/src/main/java/org/msgpack/schema/ReflectionClassSchema.java b/java/src/main/java/org/msgpack/schema/ReflectionClassSchema.java deleted file mode 100644 index fb94adfa..00000000 --- a/java/src/main/java/org/msgpack/schema/ReflectionClassSchema.java +++ /dev/null @@ -1,64 +0,0 @@ -package org.msgpack.schema; - -import java.util.Arrays; -import java.util.List; -import java.lang.reflect.*; -import org.msgpack.*; - -// FIXME -public abstract class ReflectionClassSchema extends ClassSchema { - private Constructor constructorCache; - - public ReflectionClassSchema(String name, List fields, String namespace, List imports) { - super(name, namespace, imports, fields); - } - - /* - Schema getElementSchema(int index) - { - // FIXME check index < fields.length - fields[index].getSchema(); - } - - Object createFromArray(Object[] obj) - { - Object o = newInstance(); - ((MessageConvertable)o).messageConvert(obj); - return o; - } - - Object newInstance() - { - if(constructorCache == null) { - cacheConstructor(); - } - try { - return constructorCache.newInstance((Object[])null); - } catch (InvocationTargetException e) { - throw new RuntimeException("can't instantiate "+fqdn+": "+e.getMessage()); - } catch (InstantiationException e) { - throw new RuntimeException("can't instantiate "+fqdn+": "+e.getMessage()); - } catch (IllegalAccessException e) { - throw new RuntimeException("can't instantiate "+fqdn+": "+e.getMessage()); - } - } - - private void cacheConstructor() - { - try { - Class c = Class.forName(fqdn); - int index = 0; - for(SpecificFieldSchema f : fields) { - f.cacheField(c, index++); - } - constructorCache = c.getDeclaredConstructor((Class[])null); - constructorCache.setAccessible(true); - } catch(ClassNotFoundException e) { - throw new RuntimeException("class not found: "+fqdn); - } catch (NoSuchMethodException e) { - throw new RuntimeException("class not found: "+fqdn+": "+e.getMessage()); - } - } - */ -} - diff --git a/java/src/main/java/org/msgpack/schema/SSchemaParser.java b/java/src/main/java/org/msgpack/schema/SSchemaParser.java deleted file mode 100644 index 4ae8a4b5..00000000 --- a/java/src/main/java/org/msgpack/schema/SSchemaParser.java +++ /dev/null @@ -1,254 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.schema; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; -import java.util.Stack; -import java.util.regex.Pattern; -import java.util.regex.Matcher; -import org.msgpack.*; - -// FIXME exception class - -public class SSchemaParser { - public static Schema parse(String source) { - return new SSchemaParser(false).run(source); - } - - public static Schema load(String source) { - return new SSchemaParser(true).run(source); - } - - private static abstract class SExp { - boolean isAtom() { return false; } - public String getAtom() { return null; } - - boolean isTuple() { return false; } - public SExp getTuple(int i) { return null; } - public int size() { return 0; } - public boolean empty() { return size() == 0; } - Iterator iterator(int offset) { return null; } - } - - private static class SAtom extends SExp { - private String atom; - - SAtom(String atom) { this.atom = atom; } - - boolean isAtom() { return true; } - public String getAtom() { return atom; } - - public String toString() { return atom; } - } - - private static class STuple extends SExp { - private List tuple; - - STuple() { this.tuple = new ArrayList(); } - - public void add(SExp e) { tuple.add(e); } - - boolean isTuple() { return true; } - public SExp getTuple(int i) { return tuple.get(i); } - public int size() { return tuple.size(); } - - Iterator iterator(int skip) { - Iterator i = tuple.iterator(); - for(int s=0; s < skip; ++s) { i.next(); } - return i; - } - - public String toString() { - if(tuple.isEmpty()) { return "()"; } - Iterator i = tuple.iterator(); - StringBuffer o = new StringBuffer(); - o.append("(").append(i.next()); - while(i.hasNext()) { o.append(" ").append(i.next()); } - o.append(")"); - return o.toString(); - } - } - - boolean specificClass; - - private SSchemaParser(boolean specificClass) { - this.specificClass = specificClass; - } - - private static Pattern pattern = Pattern.compile( - "(?:\\s+)|([\\(\\)]|[\\d\\w\\.]+)"); - - private Schema run(String source) { - Matcher m = pattern.matcher(source); - - Stack stack = new Stack(); - String token; - - while(true) { - while(true) { - if(!m.find()) { throw new RuntimeException("unexpected end of file"); } - token = m.group(1); - if(token != null) { break; } - } - - if(token.equals("(")) { - stack.push(new STuple()); - } else if(token.equals(")")) { - STuple top = stack.pop(); - if(stack.empty()) { - stack.push(top); - break; - } - stack.peek().add(top); - } else { - if(stack.empty()) { - throw new RuntimeException("unexpected token '"+token+"'"); - } - stack.peek().add(new SAtom(token)); - } - } - - while(true) { - if(!m.find()) { break; } - token = m.group(1); - if(token != null) { throw new RuntimeException("unexpected token '"+token+"'"); } - } - - return readType( stack.pop() ); - } - - private Schema readType(SExp exp) { - if(exp.isAtom()) { - String type = exp.getAtom(); - if(type.equals("string")) { - return new StringSchema(); - } else if(type.equals("raw")) { - return new RawSchema(); - } else if(type.equals("byte")) { - return new ByteSchema(); - } else if(type.equals("short")) { - return new ShortSchema(); - } else if(type.equals("int")) { - return new IntSchema(); - } else if(type.equals("long")) { - return new LongSchema(); - } else if(type.equals("float")) { - return new FloatSchema(); - } else if(type.equals("double")) { - return new DoubleSchema(); - } else if(type.equals("object")) { - return new GenericSchema(); - } else { - throw new RuntimeException("byte, short, int, long, float, double, raw, string or object is expected but got '"+type+"': "+exp); - } - } else { - String type = exp.getTuple(0).getAtom(); - if(type.equals("class")) { - return parseClass(exp); - } else if(type.equals("array")) { - return parseArray(exp); - } else if(type.equals("map")) { - return parseMap(exp); - } else { - throw new RuntimeException("class, array or map is expected but got '"+type+"': "+exp); - } - } - } - - private ClassSchema parseClass(SExp exp) { - if(exp.size() < 3 || !exp.getTuple(1).isAtom()) { - throw new RuntimeException("class is (class NAME CLASS_BODY): "+exp); - } - - String namespace = null; - List imports = new ArrayList(); - String name = exp.getTuple(1).getAtom(); - List fields = new ArrayList(); - - for(Iterator i=exp.iterator(2); i.hasNext();) { - SExp subexp = i.next(); - if(!subexp.isTuple() || subexp.empty() || !subexp.getTuple(0).isAtom()) { - throw new RuntimeException("field, package or import is expected: "+subexp); - } - String type = subexp.getTuple(0).getAtom(); - if(type.equals("field")) { - fields.add( parseField(subexp) ); - } else if(type.equals("package")) { - if(namespace != null) { - throw new RuntimeException("duplicated package definition: "+subexp); - } - namespace = parseNamespace(subexp); - } else if(type.equals("import")) { - imports.add( parseImport(subexp) ); - } else { - throw new RuntimeException("field, package or import is expected but got '"+type+"': "+subexp); - } - } - - if(specificClass) { - return new SpecificClassSchema(name, namespace, imports, fields); - } else { - return new GenericClassSchema(name, namespace, imports, fields); - } - } - - private ArraySchema parseArray(SExp exp) { - if(exp.size() != 2) { - throw new RuntimeException("array is (array ELEMENT_TYPE): "+exp); - } - Schema elementType = readType(exp.getTuple(1)); - return new ArraySchema(elementType); - } - - private MapSchema parseMap(SExp exp) { - if(exp.size() != 3 || !exp.getTuple(1).isAtom()) { - throw new RuntimeException("map is (map KEY_TYPE VALUE_TYPE): "+exp); - } - Schema keyType = readType(exp.getTuple(1)); - Schema valueType = readType(exp.getTuple(2)); - return new MapSchema(keyType, valueType); - } - - private String parseNamespace(SExp exp) { - if(exp.size() != 2 || !exp.getTuple(1).isAtom()) { - throw new RuntimeException("package is (package NAME): "+exp); - } - String name = exp.getTuple(1).getAtom(); - return name; - } - - private String parseImport(SExp exp) { - if(exp.size() != 2 || !exp.getTuple(1).isAtom()) { - throw new RuntimeException("import is (import NAME): "+exp); - } - String name = exp.getTuple(1).getAtom(); - return name; - } - - private FieldSchema parseField(SExp exp) { - if(exp.size() != 3 || !exp.getTuple(1).isAtom()) { - throw new RuntimeException("field is (field NAME TYPE): "+exp); - } - String name = exp.getTuple(1).getAtom(); - Schema type = readType(exp.getTuple(2)); - return new FieldSchema(name, type); - } -} - diff --git a/java/src/main/java/org/msgpack/schema/ShortSchema.java b/java/src/main/java/org/msgpack/schema/ShortSchema.java deleted file mode 100644 index f32ab411..00000000 --- a/java/src/main/java/org/msgpack/schema/ShortSchema.java +++ /dev/null @@ -1,89 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.schema; - -import java.io.IOException; -import org.msgpack.*; - -public class ShortSchema extends Schema { - public ShortSchema() { - super("Short"); - } - - @Override - public String getExpression() { - return "short"; - } - - @Override - public void pack(Packer pk, Object obj) throws IOException { - if(obj instanceof Number) { - pk.packShort( ((Number)obj).shortValue() ); - - } else if(obj == null) { - pk.packNil(); - - } else { - throw MessageTypeException.invalidConvert(obj, this); - } - } - - @Override - public Object convert(Object obj) throws MessageTypeException { - if(obj instanceof Short) { - return obj; - - } else if(obj instanceof Number) { - return ((Number)obj).shortValue(); - - } else { - throw MessageTypeException.invalidConvert(obj, this); - } - } - - @Override - public Object createFromByte(byte v) { - return (short)v; - } - - @Override - public Object createFromShort(short v) { - return (short)v; - } - - @Override - public Object createFromInt(int v) { - return (short)v; - } - - @Override - public Object createFromLong(long v) { - return (short)v; - } - - @Override - public Object createFromFloat(float v) { - return (short)v; - } - - @Override - public Object createFromDouble(double v) { - return (short)v; - } -} - diff --git a/java/src/main/java/org/msgpack/schema/SpecificClassSchema.java b/java/src/main/java/org/msgpack/schema/SpecificClassSchema.java deleted file mode 100644 index 30bd9e1b..00000000 --- a/java/src/main/java/org/msgpack/schema/SpecificClassSchema.java +++ /dev/null @@ -1,122 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.schema; - -import java.util.Collection; -import java.util.List; -import java.lang.reflect.*; -import java.io.IOException; -import org.msgpack.*; - -public class SpecificClassSchema extends ClassSchema { - private Class classCache; - private Method factoryCache; - private Constructor constructorCache; - - public SpecificClassSchema( - String name, String namespace, - List imports, List fields) { - super(name, namespace, imports, fields); - } - - @Override - @SuppressWarnings("unchecked") - public void pack(Packer pk, Object obj) throws IOException { - if(obj == null) { - pk.packNil(); - return; - } - if(classCache == null) { - cacheFactory(); - } - if(classCache.isInstance(obj)) { - ((MessagePackable)obj).messagePack(pk); - } else { - // FIXME Map - throw MessageTypeException.invalidConvert(obj, this); - } - } - - @Override - public Object convert(Object obj) throws MessageTypeException { - if(obj instanceof Collection) { - if(constructorCache == null) { - cacheConstructor(); - } - try { - MessageMergeable o = (MessageMergeable)constructorCache.newInstance((Object[])null); - o.messageMerge(obj); - return o; - } catch (InvocationTargetException e) { - throw new RuntimeException("can't instantiate "+fqdn+": "+e.getMessage()); - } catch (InstantiationException e) { - throw new RuntimeException("can't instantiate "+fqdn+": "+e.getMessage()); - } catch (IllegalAccessException e) { - throw new RuntimeException("can't instantiate "+fqdn+": "+e.getMessage()); - } - - } else { - throw MessageTypeException.invalidConvert(obj, this); - } - } - - public Schema getElementSchema(int index) { - // FIXME check index < fields.length - return fields[index].getSchema(); - } - - public Object createFromArray(Object[] obj) { - if(factoryCache == null) { - cacheFactory(); - } - try { - return factoryCache.invoke(null, new Object[]{obj}); - } catch (InvocationTargetException e) { - throw new RuntimeException("can't instantiate "+fqdn+": "+e.getCause()); - } catch (IllegalAccessException e) { - throw new RuntimeException("can't instantiate "+fqdn+": "+e.getMessage()); - } - } - - @SuppressWarnings("unchecked") - private void cacheFactory() { - try { - classCache = Class.forName(fqdn); - factoryCache = classCache.getDeclaredMethod("createFromMessage", new Class[]{Object[].class}); - factoryCache.setAccessible(true); - } catch(ClassNotFoundException e) { - throw new RuntimeException("class not found: "+fqdn); - } catch (NoSuchMethodException e) { - throw new RuntimeException("class not found: "+fqdn+": "+e.getMessage()); - } - } - - @SuppressWarnings("unchecked") - private void cacheConstructor() { - try { - classCache = Class.forName(fqdn); - constructorCache = classCache.getDeclaredConstructor((Class[])null); - constructorCache.setAccessible(true); - } catch(ClassNotFoundException e) { - throw new RuntimeException("class not found: "+fqdn); - } catch (NoSuchMethodException e) { - throw new RuntimeException("class not found: "+fqdn+": "+e.getMessage()); - } - } -} - diff --git a/java/src/main/java/org/msgpack/schema/StringSchema.java b/java/src/main/java/org/msgpack/schema/StringSchema.java deleted file mode 100644 index 46d515b4..00000000 --- a/java/src/main/java/org/msgpack/schema/StringSchema.java +++ /dev/null @@ -1,111 +0,0 @@ -// -// MessagePack for Java -// -// Copyright (C) 2009-2010 FURUHASHI Sadayuki -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -package org.msgpack.schema; - -import java.nio.ByteBuffer; -import java.io.IOException; -import java.io.UnsupportedEncodingException; -import org.msgpack.*; - -public class StringSchema extends Schema { - public StringSchema() { - super("string"); - } - - @Override - public String getFullName() { - return "String"; - } - - @Override - public void pack(Packer pk, Object obj) throws IOException { - // FIXME instanceof GenericObject - if(obj instanceof String) { - try { - byte[] d = ((String)obj).getBytes("UTF-8"); - pk.packRaw(d.length); - pk.packRawBody(d); - } catch (UnsupportedEncodingException e) { - throw MessageTypeException.invalidConvert(obj, this); - } - - } else if(obj instanceof byte[]) { - byte[] d = (byte[])obj; - pk.packRaw(d.length); - pk.packRawBody(d); - - } else if(obj instanceof ByteBuffer) { - ByteBuffer d = (ByteBuffer)obj; - if(!d.hasArray()) { - throw MessageTypeException.invalidConvert(obj, this); - } - pk.packRaw(d.capacity()); - pk.packRawBody(d.array(), d.position(), d.capacity()); - - } else if(obj == null) { - pk.packNil(); - - } else { - throw MessageTypeException.invalidConvert(obj, this); - } - } - - @Override - public Object convert(Object obj) throws MessageTypeException { - // FIXME instanceof GenericObject - if(obj instanceof String) { - return obj; - - } else if(obj instanceof byte[]) { - try { - return new String((byte[])obj, "UTF-8"); - } catch (UnsupportedEncodingException e) { - throw MessageTypeException.invalidConvert(obj, this); - } - - } else if(obj instanceof ByteBuffer) { - ByteBuffer d = (ByteBuffer)obj; - try { - if(d.hasArray()) { - return new String(d.array(), d.position(), d.capacity(), "UTF-8"); - } else { - byte[] v = new byte[d.capacity()]; - int pos = d.position(); - d.get(v); - d.position(pos); - return new String(v, "UTF-8"); - } - } catch (UnsupportedEncodingException e) { - throw MessageTypeException.invalidConvert(obj, this); - } - - } else { - throw MessageTypeException.invalidConvert(obj, this); - } - } - - @Override - public Object createFromRaw(byte[] b, int offset, int length) { - try { - return new String(b, offset, length, "UTF-8"); - } catch (Exception e) { - throw new RuntimeException(e.getMessage()); - } - } -} - diff --git a/java/src/test/java/org/msgpack/Image.java b/java/src/test/java/org/msgpack/Image.java new file mode 100644 index 00000000..3fcfe388 --- /dev/null +++ b/java/src/test/java/org/msgpack/Image.java @@ -0,0 +1,50 @@ +package org.msgpack; + +import org.msgpack.*; +import java.io.*; +import java.util.*; + +public class Image implements MessagePackable, MessageUnpackable { + public String uri = ""; + public String title = ""; + public int width = 0; + public int height = 0; + public int size = 0; + + public void messagePack(Packer pk) throws IOException { + pk.packArray(5); + pk.pack(uri); + pk.pack(title); + pk.pack(width); + pk.pack(height); + pk.pack(size); + } + + public void messageUnpack(Unpacker pac) throws IOException, MessageTypeException { + int length = pac.unpackArray(); + if(length != 5) { + throw new MessageTypeException(); + } + uri = pac.unpackString(); + title = pac.unpackString(); + width = pac.unpackInt(); + height = pac.unpackInt(); + size = pac.unpackInt(); + } + + public boolean equals(Image obj) { + return uri.equals(obj.uri) && + title.equals(obj.title) && + width == obj.width && + height == obj.height && + size == obj.size; + } + + public boolean equals(Object obj) { + if(obj.getClass() != Image.class) { + return false; + } + return equals((Image)obj); + } +} + diff --git a/java/src/test/java/org/msgpack/TestCases.java b/java/src/test/java/org/msgpack/TestCases.java new file mode 100644 index 00000000..c368972f --- /dev/null +++ b/java/src/test/java/org/msgpack/TestCases.java @@ -0,0 +1,40 @@ +package org.msgpack; + +import java.io.*; +import java.util.*; + +import org.junit.Test; +import static org.junit.Assert.*; + +public class TestCases { + public void feedFile(Unpacker pac, String path) throws Exception { + FileInputStream input = new FileInputStream(path); + byte[] buffer = new byte[32*1024]; + while(true) { + int count = input.read(buffer); + if(count < 0) { + break; + } + pac.feed(buffer, 0, count); + } + } + + @Test + public void testCases() throws Exception { + Unpacker pac = new Unpacker(); + Unpacker pac_compact = new Unpacker(); + + feedFile(pac, "src/test/resources/cases.mpac"); + feedFile(pac_compact, "src/test/resources/cases_compact.mpac"); + + UnpackResult result = new UnpackResult(); + while(pac.next(result)) { + UnpackResult result_compact = new UnpackResult(); + assertTrue( pac_compact.next(result_compact) ); + assertTrue( result.getData().equals(result_compact.getData()) ); + } + + assertFalse( pac_compact.next(result) ); + } +}; + diff --git a/java/src/test/java/org/msgpack/TestDirectConversion.java b/java/src/test/java/org/msgpack/TestDirectConversion.java new file mode 100644 index 00000000..1822ecb4 --- /dev/null +++ b/java/src/test/java/org/msgpack/TestDirectConversion.java @@ -0,0 +1,279 @@ +package org.msgpack; + +import org.msgpack.*; +import java.io.*; +import java.util.*; +import java.math.BigInteger; + +import org.junit.Test; +import static org.junit.Assert.*; + +public class TestDirectConversion { + @Test + public void testInt() throws Exception { + testInt(0); + testInt(-1); + testInt(1); + testInt(Integer.MIN_VALUE); + testInt(Integer.MAX_VALUE); + Random rand = new Random(); + for (int i = 0; i < 1000; i++) + testInt(rand.nextInt()); + } + public void testInt(int val) throws Exception { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(val); + ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); + Unpacker pac = new Unpacker(in); + assertEquals(val, pac.unpackInt()); + } + + @Test + public void testLong() throws Exception { + testLong(0); + testLong(-1); + testLong(1); + testLong(Integer.MIN_VALUE); + testLong(Integer.MAX_VALUE); + testLong(Long.MIN_VALUE); + testLong(Long.MAX_VALUE); + Random rand = new Random(); + for (int i = 0; i < 1000; i++) + testLong(rand.nextLong()); + } + public void testLong(long val) throws Exception { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(val); + ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); + Unpacker pac = new Unpacker(in); + assertEquals(val, pac.unpackLong()); + } + + @Test + public void testBigInteger() throws Exception { + testBigInteger(BigInteger.valueOf(0)); + testBigInteger(BigInteger.valueOf(-1)); + testBigInteger(BigInteger.valueOf(1)); + testBigInteger(BigInteger.valueOf(Integer.MIN_VALUE)); + testBigInteger(BigInteger.valueOf(Integer.MAX_VALUE)); + testBigInteger(BigInteger.valueOf(Long.MIN_VALUE)); + testBigInteger(BigInteger.valueOf(Long.MAX_VALUE)); + BigInteger max = BigInteger.valueOf(Long.MAX_VALUE).setBit(63); + testBigInteger(max); + Random rand = new Random(); + for (int i = 0; i < 1000; i++) + testBigInteger( max.subtract(BigInteger.valueOf( Math.abs(rand.nextLong()) )) ); + } + public void testBigInteger(BigInteger val) throws Exception { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(val); + ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); + Unpacker pac = new Unpacker(in); + assertEquals(val, pac.unpackBigInteger()); + } + + @Test + public void testFloat() throws Exception { + testFloat((float)0.0); + testFloat((float)-0.0); + testFloat((float)1.0); + testFloat((float)-1.0); + testFloat((float)Float.MAX_VALUE); + testFloat((float)Float.MIN_VALUE); + testFloat((float)Float.NaN); + testFloat((float)Float.NEGATIVE_INFINITY); + testFloat((float)Float.POSITIVE_INFINITY); + Random rand = new Random(); + for (int i = 0; i < 1000; i++) + testFloat(rand.nextFloat()); + } + public void testFloat(float val) throws Exception { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(val); + ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); + Unpacker pac = new Unpacker(in); + assertEquals(val, pac.unpackFloat(), 10e-10); + } + + @Test + public void testDouble() throws Exception { + testDouble((double)0.0); + testDouble((double)-0.0); + testDouble((double)1.0); + testDouble((double)-1.0); + testDouble((double)Double.MAX_VALUE); + testDouble((double)Double.MIN_VALUE); + testDouble((double)Double.NaN); + testDouble((double)Double.NEGATIVE_INFINITY); + testDouble((double)Double.POSITIVE_INFINITY); + Random rand = new Random(); + for (int i = 0; i < 1000; i++) + testDouble(rand.nextDouble()); + } + public void testDouble(double val) throws Exception { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(val); + ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); + Unpacker pac = new Unpacker(in); + assertEquals(val, pac.unpackDouble(), 10e-10); + } + + @Test + public void testNil() throws Exception { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).packNil(); + ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); + Unpacker pac = new Unpacker(in); + assertEquals(null, pac.unpackNull()); + } + + @Test + public void testBoolean() throws Exception { + testBoolean(false); + testBoolean(true); + } + public void testBoolean(boolean val) throws Exception { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(val); + ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); + Unpacker pac = new Unpacker(in); + assertEquals(val, pac.unpackBoolean()); + } + + @Test + public void testString() throws Exception { + testString(""); + testString("a"); + testString("ab"); + testString("abc"); + + // small size string + for (int i = 0; i < 100; i++) { + StringBuilder sb = new StringBuilder(); + int len = (int)Math.random() % 31 + 1; + for (int j = 0; j < len; j++) + sb.append('a' + ((int)Math.random()) & 26); + testString(sb.toString()); + } + + // medium size string + for (int i = 0; i < 100; i++) { + StringBuilder sb = new StringBuilder(); + int len = (int)Math.random() % 100 + (1 << 15); + for (int j = 0; j < len; j++) + sb.append('a' + ((int)Math.random()) & 26); + testString(sb.toString()); + } + + // large size string + for (int i = 0; i < 10; i++) { + StringBuilder sb = new StringBuilder(); + int len = (int)Math.random() % 100 + (1 << 31); + for (int j = 0; j < len; j++) + sb.append('a' + ((int)Math.random()) & 26); + testString(sb.toString()); + } + } + public void testString(String val) throws Exception { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(val); + ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); + Unpacker pac = new Unpacker(in); + assertEquals(val, pac.unpackString()); + } + + @Test + public void testArray() throws Exception { + List emptyList = new ArrayList(); + { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(emptyList); + ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); + Unpacker pac = new Unpacker(in); + int ulen = pac.unpackArray(); + assertEquals(0, ulen); + } + + for (int i = 0; i < 1000; i++) { + List l = new ArrayList(); + int len = (int)Math.random() % 1000 + 1; + for (int j = 0; j < len; j++) + l.add(j); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(l); + ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); + Unpacker pac = new Unpacker(in); + int ulen = pac.unpackArray(); + assertEquals(len, ulen); + for (int j = 0; j < len; j++) { + assertEquals(l.get(j).intValue(), pac.unpackInt()); + } + } + + for (int i = 0; i < 1000; i++) { + List l = new ArrayList(); + int len = (int)Math.random() % 1000 + 1; + for (int j = 0; j < len; j++) + l.add(Integer.toString(j)); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(l); + ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); + Unpacker pac = new Unpacker(in); + int ulen = pac.unpackArray(); + assertEquals(len, ulen); + for (int j = 0; j < len; j++) { + assertEquals(l.get(j), pac.unpackString()); + } + } + } + + @Test + public void testMap() throws Exception { + Map emptyMap = new HashMap(); + { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(emptyMap); + ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); + Unpacker pac = new Unpacker(in); + int ulen = pac.unpackMap(); + assertEquals(0, ulen); + } + + for (int i = 0; i < 1000; i++) { + Map m = new HashMap(); + int len = (int)Math.random() % 1000 + 1; + for (int j = 0; j < len; j++) + m.put(j, j); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(m); + ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); + Unpacker pac = new Unpacker(in); + int ulen = pac.unpackMap(); + assertEquals(len, ulen); + for (int j = 0; j < len; j++) { + Integer val = m.get(pac.unpackInt()); + assertNotNull(val); + assertEquals(val.intValue(), pac.unpackInt()); + } + } + + for (int i = 0; i < 1000; i++) { + Map m = new HashMap(); + int len = (int)Math.random() % 1000 + 1; + for (int j = 0; j < len; j++) + m.put(Integer.toString(j), j); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(m); + ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); + Unpacker pac = new Unpacker(in); + int ulen = pac.unpackMap(); + assertEquals(len, ulen); + for (int j = 0; j < len; j++) { + Integer val = m.get(pac.unpackString()); + assertNotNull(val); + assertEquals(val.intValue(), pac.unpackInt()); + } + } + } +}; + diff --git a/java/src/test/java/org/msgpack/TestMessageUnpackable.java b/java/src/test/java/org/msgpack/TestMessageUnpackable.java new file mode 100644 index 00000000..32917c7a --- /dev/null +++ b/java/src/test/java/org/msgpack/TestMessageUnpackable.java @@ -0,0 +1,32 @@ +package org.msgpack; + +import org.msgpack.*; +import java.io.*; +import java.util.*; +import java.math.BigInteger; + +import org.junit.Test; +import static org.junit.Assert.*; + +public class TestMessageUnpackable { + @Test + public void testImage() throws Exception { + Image src = new Image(); + src.title = "msgpack"; + src.uri = "http://msgpack.org/"; + src.width = 2560; + src.height = 1600; + src.size = 4096000; + + ByteArrayOutputStream out = new ByteArrayOutputStream(); + src.messagePack(new Packer(out)); + + Image dst = new Image(); + + ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); + dst.messageUnpack(new Unpacker(in)); + + assertEquals(src, dst); + } +} + diff --git a/java/src/test/java/org/msgpack/TestObjectEquals.java b/java/src/test/java/org/msgpack/TestObjectEquals.java new file mode 100644 index 00000000..25fe9271 --- /dev/null +++ b/java/src/test/java/org/msgpack/TestObjectEquals.java @@ -0,0 +1,96 @@ +package org.msgpack; + +import org.msgpack.*; +import org.msgpack.object.*; +import java.math.BigInteger; +import java.util.*; + +import org.junit.Test; +import static org.junit.Assert.*; + +public class TestObjectEquals { + @Test + public void testInt() throws Exception { + testInt(0); + testInt(-1); + testInt(1); + testInt(Integer.MIN_VALUE); + testInt(Integer.MAX_VALUE); + Random rand = new Random(); + for (int i = 0; i < 1000; i++) + testInt(rand.nextInt()); + } + public void testInt(int val) throws Exception { + MessagePackObject objInt = IntegerType.create(val); + MessagePackObject objLong = IntegerType.create((long)val); + MessagePackObject objBigInt = IntegerType.create(BigInteger.valueOf((long)val)); + assertTrue(objInt.equals(objInt)); + assertTrue(objInt.equals(objLong)); + assertTrue(objInt.equals(objBigInt)); + assertTrue(objLong.equals(objInt)); + assertTrue(objLong.equals(objLong)); + assertTrue(objLong.equals(objBigInt)); + assertTrue(objBigInt.equals(objInt)); + assertTrue(objBigInt.equals(objLong)); + assertTrue(objBigInt.equals(objBigInt)); + } + + @Test + public void testLong() throws Exception { + testLong(0); + testLong(-1); + testLong(1); + testLong(Integer.MIN_VALUE); + testLong(Integer.MAX_VALUE); + testLong(Long.MIN_VALUE); + testLong(Long.MAX_VALUE); + Random rand = new Random(); + for (int i = 0; i < 1000; i++) + testLong(rand.nextLong()); + } + public void testLong(long val) throws Exception { + MessagePackObject objInt = IntegerType.create((int)val); + MessagePackObject objLong = IntegerType.create(val); + MessagePackObject objBigInt = IntegerType.create(BigInteger.valueOf(val)); + if(val > (long)Integer.MAX_VALUE || val < (long)Integer.MIN_VALUE) { + assertTrue(objInt.equals(objInt)); + assertFalse(objInt.equals(objLong)); + assertFalse(objInt.equals(objBigInt)); + assertFalse(objLong.equals(objInt)); + assertTrue(objLong.equals(objLong)); + assertTrue(objLong.equals(objBigInt)); + assertFalse(objBigInt.equals(objInt)); + assertTrue(objBigInt.equals(objLong)); + assertTrue(objBigInt.equals(objBigInt)); + } else { + assertTrue(objInt.equals(objInt)); + assertTrue(objInt.equals(objLong)); + assertTrue(objInt.equals(objBigInt)); + assertTrue(objLong.equals(objInt)); + assertTrue(objLong.equals(objLong)); + assertTrue(objLong.equals(objBigInt)); + assertTrue(objBigInt.equals(objInt)); + assertTrue(objBigInt.equals(objLong)); + assertTrue(objBigInt.equals(objBigInt)); + } + } + + @Test + public void testNil() throws Exception { + assertTrue(NilType.create().equals(NilType.create())); + assertFalse(NilType.create().equals(IntegerType.create(0))); + assertFalse(NilType.create().equals(BooleanType.create(false))); + } + + @Test + public void testString() throws Exception { + testString(""); + testString("a"); + testString("ab"); + testString("abc"); + } + public void testString(String str) throws Exception { + assertTrue(RawType.create(str).equals(RawType.create(str))); + } +} + diff --git a/java/src/test/java/org/msgpack/TestPackUnpack.java b/java/src/test/java/org/msgpack/TestPackUnpack.java index 68778539..494c8a8a 100644 --- a/java/src/test/java/org/msgpack/TestPackUnpack.java +++ b/java/src/test/java/org/msgpack/TestPackUnpack.java @@ -3,239 +3,273 @@ package org.msgpack; import org.msgpack.*; import java.io.*; import java.util.*; +import java.math.BigInteger; import org.junit.Test; import static org.junit.Assert.*; public class TestPackUnpack { - protected Object unpackOne(ByteArrayOutputStream out) { - return unpackOne(out, null); - } - protected Object unpackOne(ByteArrayOutputStream out, Schema schema) { - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - Unpacker upk = new Unpacker(in); - if (schema != null) - upk = upk.useSchema(schema); - Iterator it = upk.iterator(); - assertEquals(true, it.hasNext()); - Object obj = it.next(); - assertEquals(false, it.hasNext()); - return obj; - } + public MessagePackObject unpackOne(ByteArrayOutputStream out) { + ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); + Unpacker pac = new Unpacker(in); + Iterator it = pac.iterator(); + assertEquals(true, it.hasNext()); + MessagePackObject obj = it.next(); + assertEquals(false, it.hasNext()); + return obj; + } - @Test - public void testInt() throws Exception { - testInt(0); - testInt(-1); - testInt(1); - testInt(Integer.MIN_VALUE); - testInt(Integer.MAX_VALUE); - Random rand = new Random(); - for (int i = 0; i < 1000; i++) - testInt(rand.nextInt()); - } - public void testInt(int val) throws Exception { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(val); - Object obj = unpackOne(out); - if (obj instanceof Byte) - assertEquals(val, ((Byte)obj).intValue()); - else if (obj instanceof Integer) - assertEquals(val, ((Integer)obj).intValue()); - else if (obj instanceof Short) - assertEquals(val, ((Short)obj).intValue()); - else if (obj instanceof Long) - assertEquals(val, ((Long)obj).intValue()); - else { - System.out.println("Got unexpected class: " + obj.getClass()); - assertTrue(false); - } - } + @Test + public void testInt() throws Exception { + testInt(0); + testInt(-1); + testInt(1); + testInt(Integer.MIN_VALUE); + testInt(Integer.MAX_VALUE); + Random rand = new Random(); + for (int i = 0; i < 1000; i++) + testInt(rand.nextInt()); + } + public void testInt(int val) throws Exception { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(val); + MessagePackObject obj = unpackOne(out); + assertEquals(val, obj.asInt()); + } - @Test - public void testFloat() throws Exception { - testFloat((float)0.0); - testFloat((float)-0.0); - testFloat((float)1.0); - testFloat((float)-1.0); - testFloat((float)Float.MAX_VALUE); - testFloat((float)Float.MIN_VALUE); - testFloat((float)Float.NaN); - testFloat((float)Float.NEGATIVE_INFINITY); - testFloat((float)Float.POSITIVE_INFINITY); - Random rand = new Random(); - for (int i = 0; i < 1000; i++) - testFloat(rand.nextFloat()); - } - public void testFloat(float val) throws Exception { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(val); - Object obj = unpackOne(out); - if (obj instanceof Float) - assertEquals(val, ((Float)obj).floatValue(), 10e-10); - else { - System.out.println("Got unexpected class: " + obj.getClass()); - assertTrue(false); - } - } + @Test + public void testLong() throws Exception { + testLong(0); + testLong(-1); + testLong(1); + testLong(Integer.MIN_VALUE); + testLong(Integer.MAX_VALUE); + testLong(Long.MIN_VALUE); + testLong(Long.MAX_VALUE); + Random rand = new Random(); + for (int i = 0; i < 1000; i++) + testLong(rand.nextLong()); + } + public void testLong(long val) throws Exception { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(val); + MessagePackObject obj = unpackOne(out); + assertEquals(val, obj.asLong()); + } - @Test - public void testDouble() throws Exception { - testDouble((double)0.0); - testDouble((double)-0.0); - testDouble((double)1.0); - testDouble((double)-1.0); - testDouble((double)Double.MAX_VALUE); - testDouble((double)Double.MIN_VALUE); - testDouble((double)Double.NaN); - testDouble((double)Double.NEGATIVE_INFINITY); - testDouble((double)Double.POSITIVE_INFINITY); - Random rand = new Random(); - for (int i = 0; i < 1000; i++) - testDouble(rand.nextDouble()); - } - public void testDouble(double val) throws Exception { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(val); - Object obj = unpackOne(out); - if (obj instanceof Double) - assertEquals(val, ((Double)obj).doubleValue(), 10e-10); - else { - System.out.println("Got unexpected class: " + obj.getClass()); - assertTrue(false); - } - } + @Test + public void testBigInteger() throws Exception { + testBigInteger(BigInteger.valueOf(0)); + testBigInteger(BigInteger.valueOf(-1)); + testBigInteger(BigInteger.valueOf(1)); + testBigInteger(BigInteger.valueOf(Integer.MIN_VALUE)); + testBigInteger(BigInteger.valueOf(Integer.MAX_VALUE)); + testBigInteger(BigInteger.valueOf(Long.MIN_VALUE)); + testBigInteger(BigInteger.valueOf(Long.MAX_VALUE)); + BigInteger max = BigInteger.valueOf(Long.MAX_VALUE).setBit(63); + testBigInteger(max); + Random rand = new Random(); + for (int i = 0; i < 1000; i++) + testBigInteger( max.subtract(BigInteger.valueOf( Math.abs(rand.nextLong()) )) ); + } + public void testBigInteger(BigInteger val) throws Exception { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(val); + MessagePackObject obj = unpackOne(out); + assertEquals(val, obj.asBigInteger()); + } - @Test - public void testNil() throws Exception { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).packNil(); - Object obj = unpackOne(out); - assertEquals(null, obj); - } + @Test + public void testFloat() throws Exception { + testFloat((float)0.0); + testFloat((float)-0.0); + testFloat((float)1.0); + testFloat((float)-1.0); + testFloat((float)Float.MAX_VALUE); + testFloat((float)Float.MIN_VALUE); + testFloat((float)Float.NaN); + testFloat((float)Float.NEGATIVE_INFINITY); + testFloat((float)Float.POSITIVE_INFINITY); + Random rand = new Random(); + for (int i = 0; i < 1000; i++) + testFloat(rand.nextFloat()); + } + public void testFloat(float val) throws Exception { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(val); + MessagePackObject obj = unpackOne(out); + float f = obj.asFloat(); + assertEquals(val, f, 10e-10); + } - @Test - public void testBoolean() throws Exception { - testBoolean(false); - testBoolean(true); - } - public void testBoolean(boolean val) throws Exception { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(val); - Object obj = unpackOne(out); - if (obj instanceof Boolean) - assertEquals(val, ((Boolean)obj).booleanValue()); - else { - System.out.println("Got unexpected class: " + obj.getClass()); - assertTrue(false); - } - } + @Test + public void testDouble() throws Exception { + testDouble((double)0.0); + testDouble((double)-0.0); + testDouble((double)1.0); + testDouble((double)-1.0); + testDouble((double)Double.MAX_VALUE); + testDouble((double)Double.MIN_VALUE); + testDouble((double)Double.NaN); + testDouble((double)Double.NEGATIVE_INFINITY); + testDouble((double)Double.POSITIVE_INFINITY); + Random rand = new Random(); + for (int i = 0; i < 1000; i++) + testDouble(rand.nextDouble()); + } + public void testDouble(double val) throws Exception { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(val); + MessagePackObject obj = unpackOne(out); + double f = obj.asDouble(); + assertEquals(val, f, 10e-10); + } - @Test - public void testString() throws Exception { - testString(""); - testString("a"); - testString("ab"); - testString("abc"); - // small size string - for (int i = 0; i < 100; i++) { - StringBuilder sb = new StringBuilder(); - int len = (int)Math.random() % 31 + 1; - for (int j = 0; j < len; j++) - sb.append('a' + ((int)Math.random()) & 26); - testString(sb.toString()); - } - // medium size string - for (int i = 0; i < 100; i++) { - StringBuilder sb = new StringBuilder(); - int len = (int)Math.random() % 100 + (1 << 15); - for (int j = 0; j < len; j++) - sb.append('a' + ((int)Math.random()) & 26); - testString(sb.toString()); - } - // large size string - for (int i = 0; i < 10; i++) { - StringBuilder sb = new StringBuilder(); - int len = (int)Math.random() % 100 + (1 << 31); - for (int j = 0; j < len; j++) - sb.append('a' + ((int)Math.random()) & 26); - testString(sb.toString()); - } - } - public void testString(String val) throws Exception { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(val); - Object obj = unpackOne(out); - if (obj instanceof byte[]) - assertEquals(val, new String((byte[])obj)); - else { - System.out.println("obj=" + obj); - System.out.println("Got unexpected class: " + obj.getClass()); - assertTrue(false); - } - } + @Test + public void testNil() throws Exception { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).packNil(); + MessagePackObject obj = unpackOne(out); + assertTrue(obj.isNil()); + } - @Test - public void testArray() throws Exception { - for (int i = 0; i < 1000; i++) { - Schema schema = Schema.parse("(array int)"); - List l = new ArrayList(); - int len = (int)Math.random() % 1000 + 1; - for (int j = 0; j < len; j++) - l.add(j); - testArray(l, schema); - } - for (int i = 0; i < 1000; i++) { - Schema schema = Schema.parse("(array string)"); - List l = new ArrayList(); - int len = (int)Math.random() % 1000 + 1; - for (int j = 0; j < len; j++) - l.add(Integer.toString(j)); - testArray(l, schema); - } - } - public void testArray(List val, Schema schema) throws Exception { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(val); - Object obj = unpackOne(out, schema); - if (obj instanceof List) - assertTrue(val.equals(obj)); - else { - System.out.println("obj=" + obj); - System.out.println("Got unexpected class: " + obj.getClass()); - assertTrue(false); - } - } + @Test + public void testBoolean() throws Exception { + testBoolean(false); + testBoolean(true); + } + public void testBoolean(boolean val) throws Exception { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(val); + MessagePackObject obj = unpackOne(out); + assertEquals(val, obj.asBoolean()); + } - @Test - public void testMap() throws Exception { - for (int i = 0; i < 1000; i++) { - Schema schema = Schema.parse("(map int int)"); - Map m = new HashMap(); - int len = (int)Math.random() % 1000 + 1; - for (int j = 0; j < len; j++) - m.put(j, j); - testMap(m, schema); - } - for (int i = 0; i < 1000; i++) { - Schema schema = Schema.parse("(map string int)"); - Map m = new HashMap(); - int len = (int)Math.random() % 1000 + 1; - for (int j = 0; j < len; j++) - m.put(Integer.toString(j), j); - testMap(m, schema); - } - } - public void testMap(Map val, Schema schema) throws Exception { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(val); - Object obj = unpackOne(out, schema); - if (obj instanceof Map) - assertTrue(val.equals(obj)); - else { - System.out.println("obj=" + obj); - System.out.println("Got unexpected class: " + obj.getClass()); - assertTrue(false); - } - } + @Test + public void testString() throws Exception { + testString(""); + testString("a"); + testString("ab"); + testString("abc"); + + // small size string + for (int i = 0; i < 100; i++) { + StringBuilder sb = new StringBuilder(); + int len = (int)Math.random() % 31 + 1; + for (int j = 0; j < len; j++) + sb.append('a' + ((int)Math.random()) & 26); + testString(sb.toString()); + } + + // medium size string + for (int i = 0; i < 100; i++) { + StringBuilder sb = new StringBuilder(); + int len = (int)Math.random() % 100 + (1 << 15); + for (int j = 0; j < len; j++) + sb.append('a' + ((int)Math.random()) & 26); + testString(sb.toString()); + } + + // large size string + for (int i = 0; i < 10; i++) { + StringBuilder sb = new StringBuilder(); + int len = (int)Math.random() % 100 + (1 << 31); + for (int j = 0; j < len; j++) + sb.append('a' + ((int)Math.random()) & 26); + testString(sb.toString()); + } + } + public void testString(String val) throws Exception { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(val); + MessagePackObject obj = unpackOne(out); + assertEquals(val, obj.asString()); + } + + @Test + public void testArray() throws Exception { + List emptyList = new ArrayList(); + { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(emptyList); + MessagePackObject obj = unpackOne(out); + assertEquals(emptyList, obj.asList()); + } + + for (int i = 0; i < 1000; i++) { + List l = new ArrayList(); + int len = (int)Math.random() % 1000 + 1; + for (int j = 0; j < len; j++) + l.add(j); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(l); + MessagePackObject obj = unpackOne(out); + List list = obj.asList(); + assertEquals(l.size(), list.size()); + for (int j = 0; j < len; j++) { + assertEquals(l.get(j).intValue(), list.get(j).asInt()); + } + } + + for (int i = 0; i < 1000; i++) { + List l = new ArrayList(); + int len = (int)Math.random() % 1000 + 1; + for (int j = 0; j < len; j++) + l.add(Integer.toString(j)); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(l); + MessagePackObject obj = unpackOne(out); + List list = obj.asList(); + assertEquals(l.size(), list.size()); + for (int j = 0; j < len; j++) { + assertEquals(l.get(j), list.get(j).asString()); + } + } + } + + @Test + public void testMap() throws Exception { + Map emptyMap = new HashMap(); + { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(emptyMap); + MessagePackObject obj = unpackOne(out); + assertEquals(emptyMap, obj.asMap()); + } + + for (int i = 0; i < 1000; i++) { + Map m = new HashMap(); + int len = (int)Math.random() % 1000 + 1; + for (int j = 0; j < len; j++) + m.put(j, j); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(m); + MessagePackObject obj = unpackOne(out); + Map map = obj.asMap(); + assertEquals(m.size(), map.size()); + for (Map.Entry pair : map.entrySet()) { + Integer val = m.get(pair.getKey().asInt()); + assertNotNull(val); + assertEquals(val.intValue(), pair.getValue().asInt()); + } + } + + for (int i = 0; i < 1000; i++) { + Map m = new HashMap(); + int len = (int)Math.random() % 1000 + 1; + for (int j = 0; j < len; j++) + m.put(Integer.toString(j), j); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(m); + MessagePackObject obj = unpackOne(out); + Map map = obj.asMap(); + assertEquals(m.size(), map.size()); + for (Map.Entry pair : map.entrySet()) { + Integer val = m.get(pair.getKey().asString()); + assertNotNull(val); + assertEquals(val.intValue(), pair.getValue().asInt()); + } + } + } }; + diff --git a/java/src/test/resources/cases.json b/java/src/test/resources/cases.json new file mode 100644 index 00000000..fd390d48 --- /dev/null +++ b/java/src/test/resources/cases.json @@ -0,0 +1 @@ +[false,true,null,0,0,0,0,0,0,0,0,0,-1,-1,-1,-1,-1,127,127,255,65535,4294967295,-32,-32,-128,-32768,-2147483648,0.0,-0.0,1.0,-1.0,"a","a","a","","","",[0],[0],[0],[],[],[],{},{},{},{"a":97},{"a":97},{"a":97},[[]],[["a"]]] \ No newline at end of file diff --git a/java/src/test/resources/cases.mpac b/java/src/test/resources/cases.mpac new file mode 100644 index 00000000..5ec08c6a Binary files /dev/null and b/java/src/test/resources/cases.mpac differ diff --git a/java/src/test/resources/cases_compact.mpac b/java/src/test/resources/cases_compact.mpac new file mode 100644 index 00000000..8812442d Binary files /dev/null and b/java/src/test/resources/cases_compact.mpac differ diff --git a/java/test/README b/java/test/README index 4e164542..9b98d912 100644 --- a/java/test/README +++ b/java/test/README @@ -1,7 +1,7 @@ #!/bin/sh svn checkout -r114 http://thrift-protobuf-compare.googlecode.com/svn/trunk/ thrift-protobuf-compare-base cp -rf thrift-protobuf-compare/tpc thrift-protobuf-compare-base -cp ../dist/msgpack.jar thrift-protobuf-compare-base/tpc/lib/ +cp ../target/msgpack*.jar thrift-protobuf-compare-base/tpc/lib/msgpack.jar cd thrift-protobuf-compare-base/tpc/ ant compile ./run-benchmark.sh diff --git a/java/test/thrift-protobuf-compare/tpc/src/serializers/BenchmarkRunner.java b/java/test/thrift-protobuf-compare/tpc/src/serializers/BenchmarkRunner.java index b17dfb2f..fa88b6bf 100644 --- a/java/test/thrift-protobuf-compare/tpc/src/serializers/BenchmarkRunner.java +++ b/java/test/thrift-protobuf-compare/tpc/src/serializers/BenchmarkRunner.java @@ -11,6 +11,7 @@ import java.util.Map; import java.util.Set; import java.util.Map.Entry; +import serializers.msgpack.MessagePackDirectSerializer; import serializers.msgpack.MessagePackSpecificSerializer; import serializers.msgpack.MessagePackIndirectSerializer; import serializers.msgpack.MessagePackDynamicSerializer; @@ -39,6 +40,7 @@ public class BenchmarkRunner BenchmarkRunner runner = new BenchmarkRunner(); // binary codecs first + runner.addObjectSerializer(new MessagePackDirectSerializer()); runner.addObjectSerializer(new MessagePackSpecificSerializer()); runner.addObjectSerializer(new MessagePackIndirectSerializer()); runner.addObjectSerializer(new MessagePackDynamicSerializer()); diff --git a/java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MediaContent.java b/java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MediaContent.java index 5dfbc8d2..e750b5a9 100644 --- a/java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MediaContent.java +++ b/java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MediaContent.java @@ -6,7 +6,7 @@ import org.msgpack.*; import org.msgpack.schema.ClassSchema; import org.msgpack.schema.FieldSchema; -public final class MediaContent implements MessagePackable, MessageMergeable +public final class MediaContent implements MessagePackable, MessageConvertable, MessageUnpackable { private static final ClassSchema _SCHEMA = (ClassSchema)Schema.load("(class MediaContent (package serializers.msgpack) (field image (array (class Image (package serializers.msgpack) (field uri string) (field title string) (field width int) (field height int) (field size int)))) (field media (class Media (package serializers.msgpack) (field uri string) (field title string) (field width int) (field height int) (field format string) (field duration long) (field size long) (field bitrate int) (field person (array string)) (field player int) (field copyright string))))"); public static ClassSchema getSchema() { return _SCHEMA; } @@ -27,7 +27,7 @@ public final class MediaContent implements MessagePackable, MessageMergeable @Override @SuppressWarnings("unchecked") - public void messageMerge(Object obj) throws MessageTypeException + public void messageConvert(Object obj) throws MessageTypeException { Object[] _source = ((List)obj).toArray(); FieldSchema[] _fields = _SCHEMA.getFields(); @@ -35,6 +35,23 @@ public final class MediaContent implements MessagePackable, MessageMergeable if(_source.length <= 1) { return; } this.media = (Media)_fields[1].getSchema().convert(_source[1]); } + @Override + public void messageUnpack(Unpacker _pac) throws IOException, MessageTypeException { + int _length = _pac.unpackArray(); + if(_length <= 0) { return; } + int _image_length = _pac.unpackArray(); + this.image = new ArrayList(_image_length); + for(int _i=0; _i < _image_length; ++_i) { + Image _image_i = new Image(); + _image_i.messageUnpack(_pac); + this.image.add(_image_i); + } + if(_length <= 1) { return; } + this.media = new Media(); + this.media.messageUnpack(_pac); + for(int _i=2; _i < _length; ++_i) { _pac.unpackObject(); } + } + @SuppressWarnings("unchecked") public static MediaContent createFromMessage(Object[] _message) { @@ -45,7 +62,7 @@ public final class MediaContent implements MessagePackable, MessageMergeable } } -final class Image implements MessagePackable, MessageMergeable +final class Image implements MessagePackable, MessageConvertable, MessageUnpackable { private static final ClassSchema _SCHEMA = (ClassSchema)Schema.load("(class Image (package serializers.msgpack) (field uri string) (field title string) (field width int) (field height int) (field size int))"); public static ClassSchema getSchema() { return _SCHEMA; } @@ -72,7 +89,7 @@ final class Image implements MessagePackable, MessageMergeable @Override @SuppressWarnings("unchecked") - public void messageMerge(Object obj) throws MessageTypeException + public void messageConvert(Object obj) throws MessageTypeException { Object[] _source = ((List)obj).toArray(); FieldSchema[] _fields = _SCHEMA.getFields(); @@ -83,6 +100,22 @@ final class Image implements MessagePackable, MessageMergeable if(_source.length <= 4) { return; } this.size = (Integer)_fields[4].getSchema().convert(_source[4]); } + @Override + public void messageUnpack(Unpacker _pac) throws IOException, MessageTypeException { + int _length = _pac.unpackArray(); + if(_length <= 0) { return; } + this.uri = _pac.unpackString(); + if(_length <= 1) { return; } + this.title = _pac.unpackString(); + if(_length <= 2) { return; } + this.width = _pac.unpackInt(); + if(_length <= 3) { return; } + this.height = _pac.unpackInt(); + if(_length <= 4) { return; } + this.size = _pac.unpackInt(); + for(int _i=5; _i < _length; ++_i) { _pac.unpackObject(); } + } + @SuppressWarnings("unchecked") public static Image createFromMessage(Object[] _message) { @@ -96,7 +129,7 @@ final class Image implements MessagePackable, MessageMergeable } } -final class Media implements MessagePackable, MessageMergeable +final class Media implements MessagePackable, MessageConvertable, MessageUnpackable { private static final ClassSchema _SCHEMA = (ClassSchema)Schema.load("(class Media (package serializers.msgpack) (field uri string) (field title string) (field width int) (field height int) (field format string) (field duration long) (field size long) (field bitrate int) (field person (array string)) (field player int) (field copyright string))"); public static ClassSchema getSchema() { return _SCHEMA; } @@ -135,7 +168,7 @@ final class Media implements MessagePackable, MessageMergeable @Override @SuppressWarnings("unchecked") - public void messageMerge(Object obj) throws MessageTypeException + public void messageConvert(Object obj) throws MessageTypeException { Object[] _source = ((List)obj).toArray(); FieldSchema[] _fields = _SCHEMA.getFields(); @@ -152,6 +185,39 @@ final class Media implements MessagePackable, MessageMergeable if(_source.length <= 10) { return; } this.copyright = (String)_fields[10].getSchema().convert(_source[10]); } + @Override + public void messageUnpack(Unpacker _pac) throws IOException, MessageTypeException { + int _length = _pac.unpackArray(); + if(_length <= 0) { return; } + this.uri = _pac.unpackString(); + if(_length <= 1) { return; } + this.title = _pac.unpackString(); + if(_length <= 2) { return; } + this.width = _pac.unpackInt(); + if(_length <= 3) { return; } + this.height = _pac.unpackInt(); + if(_length <= 4) { return; } + this.format = _pac.unpackString(); + if(_length <= 5) { return; } + this.duration = _pac.unpackLong(); + if(_length <= 6) { return; } + this.size = _pac.unpackLong(); + if(_length <= 7) { return; } + this.bitrate = _pac.unpackInt(); + if(_length <= 8) { return; } + int _person_length = _pac.unpackArray(); + this.person = new ArrayList(_person_length); + for(int _i=0; _i < _person_length; ++_i) { + String _person_i = _pac.unpackString(); + this.person.add(_person_i); + } + if(_length <= 9) { return; } + this.player = _pac.unpackInt(); + if(_length <= 10) { return; } + this.copyright = _pac.unpackString(); + for(int _i=11; _i < _length; ++_i) { _pac.unpackObject(); } + } + @SuppressWarnings("unchecked") public static Media createFromMessage(Object[] _message) { diff --git a/java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MessagePackDirectSerializer.java b/java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MessagePackDirectSerializer.java new file mode 100644 index 00000000..25f932b1 --- /dev/null +++ b/java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MessagePackDirectSerializer.java @@ -0,0 +1,68 @@ +package serializers.msgpack; + +import java.io.*; +import java.util.*; +import org.msgpack.*; +import serializers.ObjectSerializer; + +public class MessagePackDirectSerializer implements ObjectSerializer +{ + public String getName() { + return "msgpack-direct"; + } + + public MediaContent create() throws Exception { + Media media = new Media(); + media.uri = "http://javaone.com/keynote.mpg"; + media.format = "video/mpg4"; + media.title = "Javaone Keynote"; + media.duration = 1234567L; + media.bitrate = 0; + media.person = new ArrayList(2); + media.person.add("Bill Gates"); + media.person.add("Steve Jobs"); + media.player = 0; + media.height = 0; + media.width = 0; + media.size = 123L; + media.copyright = ""; + + Image image1 = new Image(); + image1.uri = "http://javaone.com/keynote_large.jpg"; + image1.width = 0; + image1.height = 0; + image1.size = 2; + image1.title = "Javaone Keynote"; + + Image image2 = new Image(); + image2.uri = "http://javaone.com/keynote_thumbnail.jpg"; + image2.width = 0; + image2.height = 0; + image2.size = 1; + image2.title = "Javaone Keynote"; + + MediaContent content = new MediaContent(); + content.media = media; + content.image = new ArrayList(2); + content.image.add(image1); + content.image.add(image2); + + return content; + } + + public byte[] serialize(MediaContent content) throws Exception { + ByteArrayOutputStream os = new ByteArrayOutputStream(); + Packer pk = new Packer(os); + pk.pack(content); + return os.toByteArray(); + } + + public MediaContent deserialize(byte[] array) throws Exception { + Unpacker pac = new Unpacker(); + pac.wrap(array); + MediaContent obj = new MediaContent(); + obj.messageUnpack(pac); + return obj; + } +} + diff --git a/java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MessagePackDynamicSerializer.java b/java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MessagePackDynamicSerializer.java index c8a88acc..9c8ccbec 100644 --- a/java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MessagePackDynamicSerializer.java +++ b/java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MessagePackDynamicSerializer.java @@ -60,7 +60,7 @@ public class MessagePackDynamicSerializer implements ObjectSerializer } public Object deserialize(byte[] array) throws Exception { - UnbufferedUnpacker pac = new UnbufferedUnpacker(); + Unpacker pac = new Unpacker(); pac.execute(array); return (Object)pac.getData(); } diff --git a/java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MessagePackGenericSerializer.java b/java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MessagePackGenericSerializer.java index 49358996..316389a9 100644 --- a/java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MessagePackGenericSerializer.java +++ b/java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MessagePackGenericSerializer.java @@ -62,7 +62,7 @@ public class MessagePackGenericSerializer implements ObjectSerializer } public Object deserialize(byte[] array) throws Exception { - UnbufferedUnpacker pac = new UnbufferedUnpacker().useSchema(MEDIA_CONTENT_SCHEMA); + Unpacker pac = new Unpacker().useSchema(MEDIA_CONTENT_SCHEMA); pac.execute(array); return (Object)pac.getData(); } diff --git a/java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MessagePackIndirectSerializer.java b/java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MessagePackIndirectSerializer.java index 2767474c..e24472bc 100644 --- a/java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MessagePackIndirectSerializer.java +++ b/java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MessagePackIndirectSerializer.java @@ -58,7 +58,7 @@ public class MessagePackIndirectSerializer implements ObjectSerializercatfile('..', 'msgpack')) { } } -requires 'Test::More' => 0.95; # done_testing +requires 'Test::More' => 0.94; # done_testing test_requires('Test::Requires'); auto_set_repository(); diff --git a/perl/lib/Data/MessagePack.pm b/perl/lib/Data/MessagePack.pm index dcc713d5..276353a2 100644 --- a/perl/lib/Data/MessagePack.pm +++ b/perl/lib/Data/MessagePack.pm @@ -4,7 +4,7 @@ use warnings; use XSLoader; use 5.008001; -our $VERSION = '0.12'; +our $VERSION = '0.15'; our $PreferInteger = 0; our $true = do { bless \(my $dummy = 1), "Data::MessagePack::Boolean" }; diff --git a/perl/pack.c b/perl/pack.c index 589cee8b..93b2e2f4 100644 --- a/perl/pack.c +++ b/perl/pack.c @@ -49,6 +49,8 @@ static void need(enc_t *enc, STRLEN len); # error "msgpack only supports IVSIZE = 8,4,2 environment." #endif +#define ERR_NESTING_EXCEEDED "perl structure exceeds maximum nesting level (max_depth set too low?)" + static void need(enc_t *enc, STRLEN len) { @@ -146,9 +148,10 @@ static int try_int(enc_t* enc, const char *p, size_t len) { } -static void _msgpack_pack_rv(enc_t *enc, SV* sv); +static void _msgpack_pack_rv(enc_t *enc, SV* sv, int depth); -static void _msgpack_pack_sv(enc_t *enc, SV* sv) { +static void _msgpack_pack_sv(enc_t *enc, SV* sv, int depth) { + if (depth <= 0) Perl_croak(aTHX_ ERR_NESTING_EXCEEDED); SvGETMAGIC(sv); if (sv==NULL) { @@ -171,7 +174,7 @@ static void _msgpack_pack_sv(enc_t *enc, SV* sv) { } else if (SvIOKp(sv)) { PACK_IV(enc, SvIV(sv)); } else if (SvROK(sv)) { - _msgpack_pack_rv(enc, SvRV(sv)); + _msgpack_pack_rv(enc, SvRV(sv), depth-1); } else if (!SvOK(sv)) { msgpack_pack_nil(enc); } else if (isGV(sv)) { @@ -182,8 +185,9 @@ static void _msgpack_pack_sv(enc_t *enc, SV* sv) { } } -static void _msgpack_pack_rv(enc_t *enc, SV* sv) { +static void _msgpack_pack_rv(enc_t *enc, SV* sv, int depth) { svtype svt; + if (depth <= 0) Perl_croak(aTHX_ ERR_NESTING_EXCEEDED); SvGETMAGIC(sv); svt = SvTYPE(sv); @@ -207,8 +211,8 @@ static void _msgpack_pack_rv(enc_t *enc, SV* sv) { msgpack_pack_map(enc, count); while (he = hv_iternext(hval)) { - _msgpack_pack_sv(enc, hv_iterkeysv(he)); - _msgpack_pack_sv(enc, HeVAL(he)); + _msgpack_pack_sv(enc, hv_iterkeysv(he), depth); + _msgpack_pack_sv(enc, HeVAL(he), depth); } } else if (svt == SVt_PVAV) { AV* ary = (AV*)sv; @@ -218,7 +222,7 @@ static void _msgpack_pack_rv(enc_t *enc, SV* sv) { for (i=0; ipack($dat)"); + if (items < 2) { + Perl_croak(aTHX_ "Usage: Data::MessagePack->pack($dat [,$max_depth])"); } - SV* val = ST(1); + SV* val = ST(1); + int depth = 512; + if (items >= 3) depth = SvIV(ST(2)); enc_t enc; enc.sv = sv_2mortal(NEWSV(0, INIT_SIZE)); @@ -256,7 +262,7 @@ XS(xs_pack) { enc.end = SvEND(enc.sv); SvPOK_only(enc.sv); - _msgpack_pack_sv(&enc, val); + _msgpack_pack_sv(&enc, val, depth); SvCUR_set(enc.sv, enc.cur - SvPVX (enc.sv)); *SvEND (enc.sv) = 0; /* many xs functions expect a trailing 0 for text strings */ diff --git a/perl/t/06_stream_unpack2.t b/perl/t/06_stream_unpack2.t index dc82c411..eaf2cb4b 100644 --- a/perl/t/06_stream_unpack2.t +++ b/perl/t/06_stream_unpack2.t @@ -1,7 +1,7 @@ use strict; use warnings; use Data::MessagePack; -use Test::More; +use Test::More tests => 6; my $input = [(undef)x16]; my $packed = Data::MessagePack->pack($input); @@ -22,5 +22,4 @@ is_deeply(Data::MessagePack->unpack($packed), $input); is_deeply $up->data, $input; } -done_testing; diff --git a/perl/t/08_cycle.t b/perl/t/08_cycle.t new file mode 100644 index 00000000..2bd66c10 --- /dev/null +++ b/perl/t/08_cycle.t @@ -0,0 +1,28 @@ +use t::Util; +use Test::More; +use Data::MessagePack; + +plan tests => 6; + +my $aref = [0]; +$aref->[1] = $aref; +eval { Data::MessagePack->pack($aref) }; +ok $@, $@; + +my $href = {}; +$href->{cycle} = $href; +eval { Data::MessagePack->pack($aref) }; +ok $@, $@; + +$aref = [0,[1,2]]; +eval { Data::MessagePack->pack($aref) }; +ok !$@; + +eval { Data::MessagePack->pack($aref, 3) }; +ok !$@; + +eval { Data::MessagePack->pack($aref, 2) }; +ok $@, $@; + +eval { Data::MessagePack->pack($aref, -1) }; +ok $@, $@; diff --git a/python/msgpack/__init__.py b/python/msgpack/__init__.py index 9593714d..86786a27 100644 --- a/python/msgpack/__init__.py +++ b/python/msgpack/__init__.py @@ -1,3 +1,10 @@ # coding: utf-8 from msgpack._msgpack import * +# alias for compatibility to simplejson/marshal/pickle. +load = unpack +loads = unpackb + +dump = pack +dumps = packb + diff --git a/python/setup.py b/python/setup.py index 8d8a6f45..64e71ede 100755 --- a/python/setup.py +++ b/python/setup.py @@ -14,7 +14,7 @@ except ImportError: from distutils.command.build_ext import build_ext have_cython = False -version = '0.1.3' +version = '0.1.4' # take care of extension modules. if have_cython: diff --git a/ruby/ChangeLog b/ruby/ChangeLog index e69de29b..d3a72829 100644 --- a/ruby/ChangeLog +++ b/ruby/ChangeLog @@ -0,0 +1,6 @@ + +2010-06-29 version 0.4.3: + + * Adds MessagePack::VERSION constant + * Fixes SEGV problem caused by GC bug at MessagePack_Unpacker_mark + diff --git a/ruby/README b/ruby/README index 859ae2b7..051a769a 100644 --- a/ruby/README +++ b/ruby/README @@ -1,29 +1,37 @@ = MessagePack - == Description +MessagePack is a binary-based efficient object serialization library. +It enables to exchange structured objects between many languages like JSON. +But unlike JSON, it is very fast and small. + +Simple usage is as follows: + + require 'msgpack' + msg = [1,2,3].to_msgpack #=> "\x93\x01\x02\x03" + MessagePack.unpack(msg) #=> [1,2,3] + +Use MessagePack::Unpacker for streaming deserialization. + == Installation === Archive Installation - rake install + ruby extconf.rb + make + make install === Gem Installation - gem install msgpack - - -== Features/Problems - - -== Synopsis + gem install msgpack == Copyright Author:: frsyuki -Copyright:: Copyright (c) 2008-2009 frsyuki +Copyright:: Copyright (c) 2008-2010 FURUHASHI Sadayuki License:: Apache License, Version 2.0 + diff --git a/ruby/compat.h b/ruby/compat.h new file mode 100644 index 00000000..d7a2ca78 --- /dev/null +++ b/ruby/compat.h @@ -0,0 +1,77 @@ +/* + * MessagePack for Ruby + * + * Copyright (C) 2008-2010 FURUHASHI Sadayuki + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef COMPAT_H__ +#define COMPAT_H__ + + +#ifdef HAVE_RUBY_ENCODING_H +#include "ruby/encoding.h" +#define COMPAT_HAVE_ENCODING +extern int s_enc_utf8; +extern int s_enc_ascii8bit; +extern int s_enc_usascii; +extern VALUE s_enc_utf8_value; +#endif + +#ifdef RUBY_VM +#define COMPAT_RERAISE rb_exc_raise(rb_errinfo()) +#else +#define COMPAT_RERAISE rb_exc_raise(ruby_errinfo) +#endif + + +/* ruby 1.8 and Rubinius */ +#ifndef RBIGNUM_POSITIVE_P +# ifdef RUBINIUS +# define RBIGNUM_POSITIVE_P(b) (rb_funcall(b, rb_intern(">="), 1, INT2FIX(0)) == Qtrue) +# else +# define RBIGNUM_POSITIVE_P(b) (RBIGNUM(b)->sign) +# endif +#endif + + +/* Rubinius */ +#ifdef RUBINIUS +static inline void rb_gc_enable() { return; } +static inline void rb_gc_disable() { return; } +#endif + + +/* ruby 1.8.5 */ +#ifndef RSTRING_PTR +#define RSTRING_PTR(s) (RSTRING(s)->ptr) +#endif + +/* ruby 1.8.5 */ +#ifndef RSTRING_LEN +#define RSTRING_LEN(s) (RSTRING(s)->len) +#endif + +/* ruby 1.8.5 */ +#ifndef RARRAY_PTR +#define RARRAY_PTR(s) (RARRAY(s)->ptr) +#endif + +/* ruby 1.8.5 */ +#ifndef RARRAY_LEN +#define RARRAY_LEN(s) (RARRAY(s)->len) +#endif + + +#endif /* compat.h */ + diff --git a/ruby/extconf.rb b/ruby/extconf.rb index e6d4bd6d..f1d44ec1 100644 --- a/ruby/extconf.rb +++ b/ruby/extconf.rb @@ -1,4 +1,5 @@ require 'mkmf' -$CFLAGS << " -I.. -Wall -O4" +require './version.rb' +$CFLAGS << %[ -I.. -Wall -O4 -DMESSAGEPACK_VERSION=\\"#{MessagePack::VERSION}\\" -g] create_makefile('msgpack') diff --git a/ruby/makegem.sh b/ruby/makegem.sh index 5ea66f19..bf30cd47 100755 --- a/ruby/makegem.sh +++ b/ruby/makegem.sh @@ -8,15 +8,20 @@ cp pack.h ext/ cp rbinit.c ext/ cp unpack.c ext/ cp unpack.h ext/ +cp version.rb ext/ cp ../msgpack/pack_define.h msgpack/ cp ../msgpack/pack_template.h msgpack/ cp ../msgpack/unpack_define.h msgpack/ cp ../msgpack/unpack_template.h msgpack/ cp ../msgpack/sysdep.h msgpack/ -cat msgpack_test.rb | sed "s/require ['\"]msgpack['\"]/require File.dirname(__FILE__) + '\/test_helper.rb'/" > test/msgpack_test.rb +cp ../test/cases.mpac test/ +cp ../test/cases_compact.mpac test/ +cp ../test/cases.json test/ gem build msgpack.gemspec +rdoc rbinit.c pack.c unpack.c + if [ $? -eq 0 ]; then rm -rf ext msgpack test/msgpack_test.rb fi diff --git a/ruby/msgpack.gemspec b/ruby/msgpack.gemspec index c5e8c8c1..95a2bd0a 100644 --- a/ruby/msgpack.gemspec +++ b/ruby/msgpack.gemspec @@ -1,14 +1,15 @@ +require './version.rb' Gem::Specification.new do |s| s.platform = Gem::Platform::RUBY s.name = "msgpack" - s.version = "0.3.9" + s.version = MessagePack::VERSION s.summary = "MessagePack, a binary-based efficient data interchange format." s.author = "FURUHASHI Sadayuki" s.email = "frsyuki@users.sourceforge.jp" s.homepage = "http://msgpack.sourceforge.net/" s.rubyforge_project = "msgpack" - s.has_rdoc = false - s.extra_rdoc_files = ["README", "ChangeLog", "AUTHORS"] + s.has_rdoc = true + s.rdoc_options = ["ext"] s.require_paths = ["lib"] s.files = Dir["ext/**/*", "msgpack/**/*", "test/**/*"] s.test_files = Dir["test/test_*.rb"] diff --git a/ruby/pack.c b/ruby/pack.c index 387bab6a..8ce46aaf 100644 --- a/ruby/pack.c +++ b/ruby/pack.c @@ -16,6 +16,8 @@ * limitations under the License. */ #include "ruby.h" +#include "compat.h" + #include "msgpack/pack_define.h" static ID s_to_msgpack; @@ -51,6 +53,15 @@ static ID s_append; rb_raise(rb_eArgError, "wrong number of arguments (%d for 0)", argc); \ } + +/* + * Document-method: NilClass#to_msgpack + * + * call-seq: + * nil.to_msgpack(out = '') -> String + * + * Serializes the nil into raw bytes. + */ static VALUE MessagePack_NilClass_to_msgpack(int argc, VALUE *argv, VALUE self) { ARG_BUFFER(out, argc, argv); @@ -58,6 +69,15 @@ static VALUE MessagePack_NilClass_to_msgpack(int argc, VALUE *argv, VALUE self) return out; } + +/* + * Document-method: TrueClass#to_msgpack + * + * call-seq: + * true.to_msgpack(out = '') -> String + * + * Serializes the true into raw bytes. + */ static VALUE MessagePack_TrueClass_to_msgpack(int argc, VALUE *argv, VALUE self) { ARG_BUFFER(out, argc, argv); @@ -65,6 +85,15 @@ static VALUE MessagePack_TrueClass_to_msgpack(int argc, VALUE *argv, VALUE self) return out; } + +/* + * Document-method: FalseClass#to_msgpack + * + * call-seq: + * false.to_msgpack(out = '') -> String + * + * Serializes false into raw bytes. + */ static VALUE MessagePack_FalseClass_to_msgpack(int argc, VALUE *argv, VALUE self) { ARG_BUFFER(out, argc, argv); @@ -73,6 +102,14 @@ static VALUE MessagePack_FalseClass_to_msgpack(int argc, VALUE *argv, VALUE self } +/* + * Document-method: Fixnum#to_msgpack + * + * call-seq: + * fixnum.to_msgpack(out = '') -> String + * + * Serializes the Fixnum into raw bytes. + */ static VALUE MessagePack_Fixnum_to_msgpack(int argc, VALUE *argv, VALUE self) { ARG_BUFFER(out, argc, argv); @@ -81,22 +118,34 @@ static VALUE MessagePack_Fixnum_to_msgpack(int argc, VALUE *argv, VALUE self) } -#ifndef RBIGNUM_SIGN // Ruby 1.8 -#define RBIGNUM_SIGN(b) (RBIGNUM(b)->sign) -#endif - +/* + * Document-method: Bignum#to_msgpack + * + * call-seq: + * bignum.to_msgpack(out = '') -> String + * + * Serializes the Bignum into raw bytes. + */ static VALUE MessagePack_Bignum_to_msgpack(int argc, VALUE *argv, VALUE self) { ARG_BUFFER(out, argc, argv); - // FIXME bignum - if(RBIGNUM_SIGN(self)) { // positive + if(RBIGNUM_POSITIVE_P(self)) { msgpack_pack_uint64(out, rb_big2ull(self)); - } else { // negative + } else { msgpack_pack_int64(out, rb_big2ll(self)); } return out; } + +/* + * Document-method: Float#to_msgpack + * + * call-seq: + * float.to_msgpack(out = '') -> String + * + * Serializes the Float into raw bytes. + */ static VALUE MessagePack_Float_to_msgpack(int argc, VALUE *argv, VALUE self) { ARG_BUFFER(out, argc, argv); @@ -104,28 +153,69 @@ static VALUE MessagePack_Float_to_msgpack(int argc, VALUE *argv, VALUE self) return out; } + +/* + * Document-method: String#to_msgpack + * + * call-seq: + * string.to_msgpack(out = '') -> String + * + * Serializes the String into raw bytes. + */ static VALUE MessagePack_String_to_msgpack(int argc, VALUE *argv, VALUE self) { ARG_BUFFER(out, argc, argv); +#ifdef COMPAT_HAVE_ENCODING + int enc = ENCODING_GET(self); + if(enc != s_enc_utf8 && enc != s_enc_ascii8bit && enc != s_enc_usascii) { + if(!ENC_CODERANGE_ASCIIONLY(self)) { + self = rb_str_encode(self, s_enc_utf8_value, 0, Qnil); + } + } +#endif msgpack_pack_raw(out, RSTRING_LEN(self)); msgpack_pack_raw_body(out, RSTRING_PTR(self), RSTRING_LEN(self)); return out; } + +/* + * Document-method: Symbol#to_msgpack + * + * call-seq: + * symbol.to_msgpack(out = '') -> String + * + * Serializes the Symbol into raw bytes. + */ static VALUE MessagePack_Symbol_to_msgpack(int argc, VALUE *argv, VALUE self) { +#ifdef COMPAT_HAVE_ENCODING + return MessagePack_String_to_msgpack(argc, argv, rb_id2str(SYM2ID(self))); +#else ARG_BUFFER(out, argc, argv); const char* name = rb_id2name(SYM2ID(self)); size_t len = strlen(name); msgpack_pack_raw(out, len); msgpack_pack_raw_body(out, name, len); return out; +#endif } + +/* + * Document-method: Array#to_msgpack + * + * call-seq: + * array.to_msgpack(out = '') -> String + * + * Serializes the Array into raw bytes. + * This calls to_msgpack method reflectively for internal elements. + */ static VALUE MessagePack_Array_to_msgpack(int argc, VALUE *argv, VALUE self) { ARG_BUFFER(out, argc, argv); - msgpack_pack_array(out, RARRAY_LEN(self)); + // FIXME check sizeof(long) > sizeof(unsigned int) && RARRAY_LEN(self) > UINT_MAX + msgpack_pack_array(out, (unsigned int)RARRAY_LEN(self)); VALUE* p = RARRAY_PTR(self); VALUE* const pend = p + RARRAY_LEN(self); for(;p != pend; ++p) { @@ -134,6 +224,7 @@ static VALUE MessagePack_Array_to_msgpack(int argc, VALUE *argv, VALUE self) return out; } + #ifndef RHASH_SIZE // Ruby 1.8 #define RHASH_SIZE(h) (RHASH(h)->tbl ? RHASH(h)->tbl->num_entries : 0) #endif @@ -146,15 +237,36 @@ static int MessagePack_Hash_to_msgpack_foreach(VALUE key, VALUE value, VALUE out return ST_CONTINUE; } +/* + * Document-method: Hash#to_msgpack + * + * call-seq: + * hash.to_msgpack(out = '') -> String + * + * Serializes the Hash into raw bytes. + * This calls to_msgpack method reflectively for internal keys and values. + */ static VALUE MessagePack_Hash_to_msgpack(int argc, VALUE *argv, VALUE self) { ARG_BUFFER(out, argc, argv); - msgpack_pack_map(out, RHASH_SIZE(self)); + // FIXME check sizeof(st_index_t) > sizeof(unsigned int) && RARRAY_LEN(self) > UINT_MAX + msgpack_pack_map(out, (unsigned int)RHASH_SIZE(self)); rb_hash_foreach(self, MessagePack_Hash_to_msgpack_foreach, out); return out; } +/** + * Document-method: MessagePack.pack + * + * call-seq: + * MessagePack.pack(object, out = '') -> String + * + * Serializes the object into raw bytes. The encoding of the string is ASCII-8BIT on Ruby 1.9. + * This method is same as object.to_msgpack(out = ''). + * + * _out_ is an object that implements *<<* method like String or IO. + */ static VALUE MessagePack_pack(int argc, VALUE* argv, VALUE self) { VALUE out; @@ -173,16 +285,22 @@ void Init_msgpack_pack(VALUE mMessagePack) { s_to_msgpack = rb_intern("to_msgpack"); s_append = rb_intern("<<"); - rb_define_method_id(rb_cNilClass, s_to_msgpack, MessagePack_NilClass_to_msgpack, -1); - rb_define_method_id(rb_cTrueClass, s_to_msgpack, MessagePack_TrueClass_to_msgpack, -1); - rb_define_method_id(rb_cFalseClass, s_to_msgpack, MessagePack_FalseClass_to_msgpack, -1); - rb_define_method_id(rb_cFixnum, s_to_msgpack, MessagePack_Fixnum_to_msgpack, -1); - rb_define_method_id(rb_cBignum, s_to_msgpack, MessagePack_Bignum_to_msgpack, -1); - rb_define_method_id(rb_cFloat, s_to_msgpack, MessagePack_Float_to_msgpack, -1); - rb_define_method_id(rb_cString, s_to_msgpack, MessagePack_String_to_msgpack, -1); - rb_define_method_id(rb_cArray, s_to_msgpack, MessagePack_Array_to_msgpack, -1); - rb_define_method_id(rb_cHash, s_to_msgpack, MessagePack_Hash_to_msgpack, -1); - rb_define_method_id(rb_cSymbol, s_to_msgpack, MessagePack_Symbol_to_msgpack, -1); + + rb_define_method(rb_cNilClass, "to_msgpack", MessagePack_NilClass_to_msgpack, -1); + rb_define_method(rb_cTrueClass, "to_msgpack", MessagePack_TrueClass_to_msgpack, -1); + rb_define_method(rb_cFalseClass, "to_msgpack", MessagePack_FalseClass_to_msgpack, -1); + rb_define_method(rb_cFixnum, "to_msgpack", MessagePack_Fixnum_to_msgpack, -1); + rb_define_method(rb_cBignum, "to_msgpack", MessagePack_Bignum_to_msgpack, -1); + rb_define_method(rb_cFloat, "to_msgpack", MessagePack_Float_to_msgpack, -1); + rb_define_method(rb_cString, "to_msgpack", MessagePack_String_to_msgpack, -1); + rb_define_method(rb_cArray, "to_msgpack", MessagePack_Array_to_msgpack, -1); + rb_define_method(rb_cHash, "to_msgpack", MessagePack_Hash_to_msgpack, -1); + rb_define_method(rb_cSymbol, "to_msgpack", MessagePack_Symbol_to_msgpack, -1); + + /** + * MessagePack module is defined in rbinit.c file. + * mMessagePack = rb_define_module("MessagePack"); + */ rb_define_module_function(mMessagePack, "pack", MessagePack_pack, -1); } diff --git a/ruby/pack.h b/ruby/pack.h index c9b08a4e..f162a86d 100644 --- a/ruby/pack.h +++ b/ruby/pack.h @@ -1,7 +1,7 @@ /* * MessagePack for Ruby packing routine * - * Copyright (C) 2008-2009 FURUHASHI Sadayuki + * Copyright (C) 2008-2010 FURUHASHI Sadayuki * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/ruby/rbinit.c b/ruby/rbinit.c index 80d1d8cb..1d1cbc69 100644 --- a/ruby/rbinit.c +++ b/ruby/rbinit.c @@ -1,7 +1,7 @@ /* * MessagePack for Ruby * - * Copyright (C) 2008-2009 FURUHASHI Sadayuki + * Copyright (C) 2008-2010 FURUHASHI Sadayuki * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,13 +17,50 @@ */ #include "pack.h" #include "unpack.h" +#include "compat.h" static VALUE mMessagePack; +#ifdef COMPAT_HAVE_ENCODING +int s_enc_utf8; +int s_enc_ascii8bit; +int s_enc_usascii; +VALUE s_enc_utf8_value; +#endif + +/** + * Document-module: MessagePack + * + * MessagePack is a binary-based efficient object serialization library. + * It enables to exchange structured objects between many languages like JSON. + * But unlike JSON, it is very fast and small. + * + * You can install MessagePack with rubygems. + * + * gem install msgpack + * + * Simple usage is as follows: + * + * require 'msgpack' + * msg = [1,2,3].to_msgpack #=> "\x93\x01\x02\x03" + * MessagePack.unpack(msg) #=> [1,2,3] + * + * Use Unpacker class for streaming deserialization. + * + */ void Init_msgpack(void) { mMessagePack = rb_define_module("MessagePack"); + + rb_define_const(mMessagePack, "VERSION", rb_str_new2(MESSAGEPACK_VERSION)); + +#ifdef COMPAT_HAVE_ENCODING + s_enc_ascii8bit = rb_ascii8bit_encindex(); + s_enc_utf8 = rb_utf8_encindex(); + s_enc_usascii = rb_usascii_encindex(); + s_enc_utf8_value = rb_enc_from_encoding(rb_utf8_encoding()); +#endif + Init_msgpack_unpack(mMessagePack); Init_msgpack_pack(mMessagePack); } - diff --git a/ruby/test/test_cases.rb b/ruby/test/test_cases.rb new file mode 100644 index 00000000..bfb752e2 --- /dev/null +++ b/ruby/test/test_cases.rb @@ -0,0 +1,46 @@ +#!/usr/bin/env ruby +here = File.dirname(__FILE__) +require "#{here}/test_helper" + +begin +require 'json' +rescue LoadError +require 'rubygems' +require 'json' +end + +CASES_PATH = "#{here}/cases.mpac" +CASES_COMPACT_PATH = "#{here}/cases_compact.mpac" +CASES_JSON_PATH = "#{here}/cases.json" + +class MessagePackTestCases < Test::Unit::TestCase + def feed_file(path) + pac = MessagePack::Unpacker.new + pac.feed File.read(path) + pac + end + + def test_compare_compact + pac = feed_file(CASES_PATH) + cpac = feed_file(CASES_COMPACT_PATH) + + objs = []; pac.each {| obj| objs << obj } + cobjs = []; cpac.each {|cobj| cobjs << cobj } + + objs.zip(cobjs).each {|obj, cobj| + assert_equal(obj, cobj) + } + end + + def test_compare_json + pac = feed_file(CASES_PATH) + + objs = []; pac.each {|obj| objs << obj } + jobjs = JSON.load File.read(CASES_JSON_PATH) + + objs.zip(jobjs) {|obj, jobj| + assert_equal(obj, jobj) + } + end +end + diff --git a/ruby/test/test_encoding.rb b/ruby/test/test_encoding.rb new file mode 100644 index 00000000..2cf0767c --- /dev/null +++ b/ruby/test/test_encoding.rb @@ -0,0 +1,68 @@ +#!/usr/bin/env ruby +require File.dirname(__FILE__)+'/test_helper' + +if RUBY_VERSION < "1.9" + exit +end + +class MessagePackTestEncoding < Test::Unit::TestCase + def self.it(name, &block) + define_method("test_#{name}", &block) + end + + it "US-ASCII" do + check_unpack "abc".force_encoding("US-ASCII") + end + + it "UTF-8 ascii" do + check_unpack "abc".force_encoding("UTF-8") + end + + it "UTF-8 mbstr" do + check_unpack "\xE3\x81\x82".force_encoding("UTF-8") + end + + it "UTF-8 invalid" do + check_unpack "\xD0".force_encoding("UTF-8") + end + + it "ASCII-8BIT" do + check_unpack "\xD0".force_encoding("ASCII-8BIT") + end + + it "EUC-JP" do + x = "\xA4\xA2".force_encoding("EUC-JP") + check_unpack(x) + end + + it "EUC-JP invalid" do + begin + "\xD0".force_encoding("EUC-JP").to_msgpack + assert(false) + rescue Encoding::InvalidByteSequenceError + assert(true) + end + end + + private + def check_unpack(str) + if str.encoding.to_s == "ASCII-8BIT" + should_str = str.dup.force_encoding("UTF-8") + else + should_str = str.encode("UTF-8") + end + + raw = str.to_msgpack + r = MessagePack.unpack(str.to_msgpack) + assert_equal(r.encoding.to_s, "UTF-8") + assert_equal(r, should_str.force_encoding("UTF-8")) + + if str.valid_encoding? + sym = str.to_sym + r = MessagePack.unpack(sym.to_msgpack) + assert_equal(r.encoding.to_s, "UTF-8") + assert_equal(r, should_str.force_encoding("UTF-8")) + end + end +end + diff --git a/ruby/test/test_helper.rb b/ruby/test/test_helper.rb index 6a634893..4def861a 100644 --- a/ruby/test/test_helper.rb +++ b/ruby/test/test_helper.rb @@ -1,3 +1,10 @@ require 'test/unit' +begin +require File.dirname(__FILE__) + '/../msgpack' +rescue LoadError require File.dirname(__FILE__) + '/../lib/msgpack' +end +if ENV["GC_STRESS"] + GC.stress = true +end diff --git a/ruby/msgpack_test.rb b/ruby/test/test_pack_unpack.rb similarity index 72% rename from ruby/msgpack_test.rb rename to ruby/test/test_pack_unpack.rb index 8cbb5868..545e5939 100644 --- a/ruby/msgpack_test.rb +++ b/ruby/test/test_pack_unpack.rb @@ -1,8 +1,7 @@ #!/usr/bin/env ruby -require 'msgpack' -require 'test/unit' +require File.dirname(__FILE__)+'/test_helper' -class MessagePackTestFormat < Test::Unit::TestCase +class MessagePackTestPackUnpack < Test::Unit::TestCase def self.it(name, &block) define_method("test_#{name}", &block) end @@ -154,7 +153,8 @@ class MessagePackTestFormat < Test::Unit::TestCase end it "{1=>1}" do - match ({1=>1}), "\x81\x01\x01" + obj = {1=>1} + match obj, "\x81\x01\x01" end it "1.0" do @@ -166,27 +166,32 @@ class MessagePackTestFormat < Test::Unit::TestCase end it "[0, 1, ..., 14]" do - match (0..14).to_a, "\x9f\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e" + obj = (0..14).to_a + match obj, "\x9f\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e" end it "[0, 1, ..., 15]" do - match (0..15).to_a, "\xdc\x00\x10\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" + obj = (0..15).to_a + match obj, "\xdc\x00\x10\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" end it "{}" do - match ({}), "\x80" + obj = {} + match obj, "\x80" end - it "{0=>0, 1=>1, ..., 14=>14}" do - a = (0..14).to_a; - match Hash[*a.zip(a).flatten], "\x8f\x05\x05\x0b\x0b\x00\x00\x06\x06\x0c\x0c\x01\x01\x07\x07\x0d\x0d\x02\x02\x08\x08\x0e\x0e\x03\x03\x09\x09\x04\x04\x0a\x0a" - end - - it "{0=>0, 1=>1, ..., 15=>15}" do - a = (0..15).to_a; - match Hash[*a.zip(a).flatten], "\xde\x00\x10\x05\x05\x0b\x0b\x00\x00\x06\x06\x0c\x0c\x01\x01\x07\x07\x0d\x0d\x02\x02\x08\x08\x0e\x0e\x03\x03\x09\x09\x0f\x0f\x04\x04\x0a\x0a" - end +## FIXME +# it "{0=>0, 1=>1, ..., 14=>14}" do +# a = (0..14).to_a; +# match Hash[*a.zip(a).flatten], "\x8f\x05\x05\x0b\x0b\x00\x00\x06\x06\x0c\x0c\x01\x01\x07\x07\x0d\x0d\x02\x02\x08\x08\x0e\x0e\x03\x03\x09\x09\x04\x04\x0a\x0a" +# end +# +# it "{0=>0, 1=>1, ..., 15=>15}" do +# a = (0..15).to_a; +# match Hash[*a.zip(a).flatten], "\xde\x00\x10\x05\x05\x0b\x0b\x00\x00\x06\x06\x0c\x0c\x01\x01\x07\x07\x0d\x0d\x02\x02\x08\x08\x0e\x0e\x03\x03\x09\x09\x0f\x0f\x04\x04\x0a\x0a" +# end +## FIXME # it "fixmap" do # check_map 1, 0 # check_map 1, (1<<4)-1 @@ -202,6 +207,37 @@ class MessagePackTestFormat < Test::Unit::TestCase # #check_map 5, (1<<32)-1 # memory error # end + it "buffer" do + str = "a"*32*1024*4 + raw = str.to_msgpack + pac = MessagePack::Unpacker.new + + len = 0 + parsed = false + + n = 655 + time = raw.size / n + time += 1 unless raw.size % n == 0 + off = 0 + + time.times do + assert(!parsed) + + fe = raw[off, n] + assert(fe.length > 0) + off += fe.length + + pac.feed fe + pac.each {|obj| + assert(!parsed) + assert_equal(obj, str) + parsed = true + } + end + + assert(parsed) + end + it "gc mark" do obj = [{["a","b"]=>["c","d"]}, ["e","f"], "d"] num = 4 @@ -244,6 +280,10 @@ class MessagePackTestFormat < Test::Unit::TestCase assert_equal(parsed, num) end + it "MessagePack::VERSION constant" do + p MessagePack::VERSION + end + private def check(len, obj) v = obj.to_msgpack diff --git a/ruby/unpack.c b/ruby/unpack.c index 3a95e5a4..2d10e75d 100644 --- a/ruby/unpack.c +++ b/ruby/unpack.c @@ -16,22 +16,24 @@ * limitations under the License. */ #include "ruby.h" +#include "compat.h" #include "msgpack/unpack_define.h" static ID s_sysread; static ID s_readpartial; -#ifdef HAVE_RUBY_ENCODING_H -#include "ruby/encoding.h" -int s_ascii_8bit; -#endif +struct unpack_buffer { + size_t size; + size_t free; + char* ptr; +}; typedef struct { int finished; VALUE source; size_t offset; - VALUE buffer; + struct unpack_buffer buffer; VALUE stream; VALUE streambuf; ID stream_append_method; @@ -124,7 +126,17 @@ static inline int template_callback_map_item(unpack_user* u, VALUE* c, VALUE k, #endif static inline int template_callback_raw(unpack_user* u, const char* b, const char* p, unsigned int l, VALUE* o) -{ *o = (l <= COW_MIN_SIZE) ? rb_str_new(p, l) : rb_str_substr(u->source, p - b, l); return 0; } +{ + if(u->source == Qnil || l <= COW_MIN_SIZE) { + *o = rb_str_new(p, l); + } else { + *o = rb_str_substr(u->source, p - b, l); + } +#ifdef COMPAT_HAVE_ENCODING + ENCODING_SET(*o, s_enc_utf8); +#endif + return 0; +} #include "msgpack/unpack_template.h" @@ -147,11 +159,7 @@ static inline int template_callback_raw(unpack_user* u, const char* b, const cha static VALUE template_execute_rescue(VALUE nouse) { rb_gc_enable(); -#ifdef RUBY_VM - rb_exc_raise(rb_errinfo()); -#else - rb_exc_raise(ruby_errinfo); -#endif + COMPAT_RERAISE; } static VALUE template_execute_do(VALUE argv) @@ -178,12 +186,6 @@ static int template_execute_wrap(msgpack_unpack_t* mp, (VALUE)from, }; -#ifdef HAVE_RUBY_ENCODING_H - // FIXME encodingをASCII-8BITにする - int enc_orig = rb_enc_get_index(str); - rb_enc_set_index(str, s_ascii_8bit); -#endif - // FIXME execute実行中はmp->topが更新されないのでGC markが機能しない rb_gc_disable(); @@ -192,33 +194,68 @@ static int template_execute_wrap(msgpack_unpack_t* mp, int ret = (int)rb_rescue(template_execute_do, (VALUE)args, template_execute_rescue, Qnil); - mp->user.source = Qnil; - rb_gc_enable(); -#ifdef HAVE_RUBY_ENCODING_H - rb_enc_set_index(str, enc_orig); -#endif + return ret; +} + +static int template_execute_wrap_each(msgpack_unpack_t* mp, + const char* ptr, size_t dlen, size_t* from) +{ + VALUE args[4] = { + (VALUE)mp, + (VALUE)ptr, + (VALUE)dlen, + (VALUE)from, + }; + + // FIXME execute実行中はmp->topが更新されないのでGC markが機能しない + rb_gc_disable(); + + mp->user.source = Qnil; + + int ret = (int)rb_rescue(template_execute_do, (VALUE)args, + template_execute_rescue, Qnil); + + rb_gc_enable(); return ret; } static VALUE cUnpacker; + + +/** + * Document-module: MessagePack::UnpackerError + * + */ static VALUE eUnpackError; +#ifndef MSGPACK_UNPACKER_BUFFER_INIT_SIZE +#define MSGPACK_UNPACKER_BUFFER_INIT_SIZE (32*1024) +#endif + +#ifndef MSGPACK_UNPACKER_BUFFER_RESERVE_SIZE +#define MSGPACK_UNPACKER_BUFFER_RESERVE_SIZE (8*1024) +#endif + static void MessagePack_Unpacker_free(void* data) { - if(data) { free(data); } + if(data) { + msgpack_unpack_t* mp = (msgpack_unpack_t*)data; + free(mp->user.buffer.ptr); + free(mp); + } } static void MessagePack_Unpacker_mark(msgpack_unpack_t *mp) { unsigned int i; - rb_gc_mark(mp->user.buffer); rb_gc_mark(mp->user.stream); rb_gc_mark(mp->user.streambuf); + rb_gc_mark_maybe(template_data(mp)); for(i=0; i < mp->top; ++i) { rb_gc_mark(mp->stack[i].obj); rb_gc_mark_maybe(mp->stack[i].map_key); @@ -229,6 +266,17 @@ static VALUE MessagePack_Unpacker_alloc(VALUE klass) { VALUE obj; msgpack_unpack_t* mp = ALLOC_N(msgpack_unpack_t, 1); + + // rb_gc_mark (not _maybe) is used for following member objects. + mp->user.stream = Qnil; + mp->user.streambuf = Qnil; + + mp->user.finished = 0; + mp->user.offset = 0; + mp->user.buffer.size = 0; + mp->user.buffer.free = 0; + mp->user.buffer.ptr = NULL; + obj = Data_Wrap_Struct(klass, MessagePack_Unpacker_mark, MessagePack_Unpacker_free, mp); return obj; @@ -243,6 +291,22 @@ static ID append_method_of(VALUE stream) } } +/** + * Document-method: MessagePack::Unpacker#initialize + * + * call-seq: + * MessagePack::Unpacker.new(stream = nil) + * + * Creates instance of MessagePack::Unpacker. + * + * You can specify a _stream_ for input stream. + * It is required to implement *sysread* or *readpartial* method. + * + * With the input stream, buffers will be feeded into the deserializer automatically. + * + * Without the input stream, use *feed* method manually. Or you can manage the buffer manually + * with *execute*, *finished?*, *data* and *reset* methods. + */ static VALUE MessagePack_Unpacker_initialize(int argc, VALUE *argv, VALUE self) { VALUE stream; @@ -259,21 +323,36 @@ static VALUE MessagePack_Unpacker_initialize(int argc, VALUE *argv, VALUE self) UNPACKER(self, mp); template_init(mp); - mp->user.finished = 0; - mp->user.offset = 0; - mp->user.buffer = rb_str_new("",0); mp->user.stream = stream; - mp->user.streambuf = rb_str_new("",0); + mp->user.streambuf = rb_str_buf_new(MSGPACK_UNPACKER_BUFFER_RESERVE_SIZE); mp->user.stream_append_method = append_method_of(stream); + return self; } + +/** + * Document-method: MessagePack::Unpacker#stream + * + * call-seq: + * unpacker.stream + * + * Gets the input stream. + */ static VALUE MessagePack_Unpacker_stream_get(VALUE self) { UNPACKER(self, mp); return mp->user.stream; } +/** + * Document-method: MessagePack::Unpacker#stream= + * + * call-seq: + * unpacker.stream = stream + * + * Resets the input stream. You can set nil not to use input stream. + */ static VALUE MessagePack_Unpacker_stream_set(VALUE self, VALUE val) { UNPACKER(self, mp); @@ -282,14 +361,103 @@ static VALUE MessagePack_Unpacker_stream_set(VALUE self, VALUE val) return val; } + +static void reserve_buffer(msgpack_unpack_t* mp, size_t require) +{ + struct unpack_buffer* buffer = &mp->user.buffer; + + if(buffer->size == 0) { + size_t nsize = MSGPACK_UNPACKER_BUFFER_INIT_SIZE; + while(nsize < require) { + nsize *= 2; + } + char* tmp = ALLOC_N(char, nsize); + buffer->ptr = tmp; + buffer->free = nsize; + buffer->size = 0; + return; + } + + if(buffer->size <= mp->user.offset) { + /* clear buffer and rewind offset */ + buffer->free += buffer->size; + buffer->size = 0; + mp->user.offset = 0; + } + + if(require <= buffer->free) { + /* enough free space */ + return; + } + + size_t nsize = (buffer->size + buffer->free) * 2; + + if(mp->user.offset <= buffer->size / 2) { + /* parsed less than half: realloc only */ + while(nsize < buffer->size + require) { + nsize *= 2; + } + char* tmp = REALLOC_N(buffer->ptr, char, nsize); + buffer->free = nsize - buffer->size; + buffer->ptr = tmp; + + } else { + /* parsed more than half: realloc and move */ + size_t not_parsed = buffer->size - mp->user.offset; + while(nsize < not_parsed + require) { + nsize *= 2; + } + char* tmp = REALLOC_N(buffer->ptr, char, nsize); + memcpy(tmp, tmp + mp->user.offset, not_parsed); + buffer->free = nsize - buffer->size; + buffer->size = not_parsed; + buffer->ptr = tmp; + mp->user.offset = 0; + } +} + +static inline void feed_buffer(msgpack_unpack_t* mp, const char* ptr, size_t len) +{ + struct unpack_buffer* buffer = &mp->user.buffer; + + if(buffer->free < len) { + reserve_buffer(mp, len); + } + memcpy(buffer->ptr + buffer->size, ptr, len); + buffer->size += len; + buffer->free -= len; +} + +/** + * Document-method: MessagePack::Unpacker#feed + * + * call-seq: + * unpacker.feed(data) + * + * Fills the internal buffer with the specified buffer. + */ static VALUE MessagePack_Unpacker_feed(VALUE self, VALUE data) { UNPACKER(self, mp); StringValue(data); - rb_str_cat(mp->user.buffer, RSTRING_PTR(data), RSTRING_LEN(data)); + feed_buffer(mp, RSTRING_PTR(data), RSTRING_LEN(data)); return Qnil; } +/** + * Document-method: MessagePack::Unpacker#fill + * + * call-seq: + * unpacker.fill -> length of read data + * + * Fills the internal buffer using the input stream. + * + * If the input stream is not specified, it returns nil. + * You can set it on *initialize* or *stream=* methods. + * + * This methods raises exceptions that _stream.sysread_ or + * _stream.readpartial_ method raises. + */ static VALUE MessagePack_Unpacker_fill(VALUE self) { UNPACKER(self, mp); @@ -298,21 +466,28 @@ static VALUE MessagePack_Unpacker_fill(VALUE self) return Qnil; } - long len; - if(RSTRING_LEN(mp->user.buffer) == 0) { - rb_funcall(mp->user.stream, mp->user.stream_append_method, 2, - LONG2FIX(64*1024), mp->user.buffer); - len = RSTRING_LEN(mp->user.buffer); - } else { - rb_funcall(mp->user.stream, mp->user.stream_append_method, 2, - LONG2FIX(64*1024), mp->user.streambuf); - len = RSTRING_LEN(mp->user.streambuf); - rb_str_cat(mp->user.buffer, RSTRING_PTR(mp->user.streambuf), RSTRING_LEN(mp->user.streambuf)); - } + rb_funcall(mp->user.stream, mp->user.stream_append_method, 2, + LONG2FIX(MSGPACK_UNPACKER_BUFFER_RESERVE_SIZE), + mp->user.streambuf); + + size_t len = RSTRING_LEN(mp->user.streambuf); + feed_buffer(mp, RSTRING_PTR(mp->user.streambuf), len); return LONG2FIX(len); } + +/** + * Document-method: MessagePack::Unpacker#each + * + * call-seq: + * unpacker.each {|object| } + * + * Deserializes objects repeatedly. This calls *fill* method automatically. + * + * UnpackError is throw when parse error is occured. + * This method raises exceptions that *fill* method raises. + */ static VALUE MessagePack_Unpacker_each(VALUE self) { UNPACKER(self, mp); @@ -323,7 +498,7 @@ static VALUE MessagePack_Unpacker_each(VALUE self) #endif while(1) { - if(RSTRING_LEN(mp->user.buffer) <= mp->user.offset) { + if(mp->user.buffer.size <= mp->user.offset) { do_fill: { VALUE len = MessagePack_Unpacker_fill(self); @@ -333,8 +508,9 @@ static VALUE MessagePack_Unpacker_each(VALUE self) } } - ret = template_execute_wrap(mp, mp->user.buffer, - RSTRING_LEN(mp->user.buffer), &mp->user.offset); + ret = template_execute_wrap_each(mp, + mp->user.buffer.ptr, mp->user.buffer.size, + &mp->user.offset); if(ret < 0) { rb_raise(eUnpackError, "parse error."); @@ -352,6 +528,7 @@ static VALUE MessagePack_Unpacker_each(VALUE self) return Qnil; } + static inline VALUE MessagePack_unpack_impl(VALUE self, VALUE data, unsigned long dlen) { msgpack_unpack_t mp; @@ -376,12 +553,34 @@ static inline VALUE MessagePack_unpack_impl(VALUE self, VALUE data, unsigned lon } } +/** + * Document-method: MessagePack::Unpacker.unpack_limit + * + * call-seq: + * MessagePack::Unpacker.unpack_limit(data, limit) -> object + * + * Deserializes one object over the specified buffer upto _limit_ bytes. + * + * UnpackError is throw when parse error is occured, the buffer is insufficient + * to deserialize one object or there are extra bytes. + */ static VALUE MessagePack_unpack_limit(VALUE self, VALUE data, VALUE limit) { CHECK_STRING_TYPE(data); return MessagePack_unpack_impl(self, data, NUM2ULONG(limit)); } +/** + * Document-method: MessagePack::Unpacker.unpack + * + * call-seq: + * MessagePack::Unpacker.unpack(data) -> object + * + * Deserializes one object over the specified buffer. + * + * UnpackError is throw when parse error is occured, the buffer is insufficient + * to deserialize one object or there are extra bytes. + */ static VALUE MessagePack_unpack(VALUE self, VALUE data) { CHECK_STRING_TYPE(data); @@ -411,7 +610,20 @@ static VALUE MessagePack_Unpacker_execute_impl(VALUE self, VALUE data, } } -/* compat */ +/** + * Document-method: MessagePack::Unpacker#execute_limit + * + * call-seq: + * unpacker.execute_limit(data, offset, limit) -> next offset + * + * Deserializes one object over the specified buffer from _offset_ bytes upto _limit_ bytes. + * + * This method doesn't use the internal buffer. + * + * Call *reset()* method before calling this method again. + * + * UnpackError is throw when parse error is occured. + */ static VALUE MessagePack_Unpacker_execute_limit(VALUE self, VALUE data, VALUE off, VALUE limit) { @@ -420,7 +632,24 @@ static VALUE MessagePack_Unpacker_execute_limit(VALUE self, VALUE data, (size_t)NUM2ULONG(off), (size_t)NUM2ULONG(limit)); } -/* compat */ +/** + * Document-method: MessagePack::Unpacker#execute + * + * call-seq: + * unpacker.execute(data, offset) -> next offset + * + * Deserializes one object over the specified buffer from _offset_ bytes. + * + * This method doesn't use the internal buffer. + * + * Call *reset()* method before calling this method again. + * + * This returns offset that was parsed to. + * Use *finished?* method to check an object is deserialized and call *data* + * method if it returns true. + * + * UnpackError is throw when parse error is occured. + */ static VALUE MessagePack_Unpacker_execute(VALUE self, VALUE data, VALUE off) { CHECK_STRING_TYPE(data); @@ -428,7 +657,16 @@ static VALUE MessagePack_Unpacker_execute(VALUE self, VALUE data, VALUE off) (size_t)NUM2ULONG(off), (size_t)RSTRING_LEN(data)); } -/* compat */ +/** + * Document-method: MessagePack::Unpacker#finished? + * + * call-seq: + * unpacker.finished? + * + * Returns true if an object is ready to get with data method. + * + * Use this method with execute method. + */ static VALUE MessagePack_Unpacker_finished_p(VALUE self) { UNPACKER(self, mp); @@ -438,14 +676,30 @@ static VALUE MessagePack_Unpacker_finished_p(VALUE self) return Qfalse; } -/* compat */ +/** + * Document-method: MessagePack::Unpacker#data + * + * call-seq: + * unpacker.data + * + * Gets the object deserialized by execute method. + * + * Use this method with execute method. + */ static VALUE MessagePack_Unpacker_data(VALUE self) { UNPACKER(self, mp); return template_data(mp); } -/* compat */ +/** + * Document-method: MessagePack::Unpacker#reset + * + * call-seq: + * unpacker.reset + * + * Resets the internal state of the unpacker. + */ static VALUE MessagePack_Unpacker_reset(VALUE self) { UNPACKER(self, mp); @@ -460,27 +714,100 @@ void Init_msgpack_unpack(VALUE mMessagePack) s_sysread = rb_intern("sysread"); s_readpartial = rb_intern("readpartial"); -#ifdef HAVE_RUBY_ENCODING_H - s_ascii_8bit = rb_enc_find_index("ASCII-8BIT"); -#endif - eUnpackError = rb_define_class_under(mMessagePack, "UnpackError", rb_eStandardError); cUnpacker = rb_define_class_under(mMessagePack, "Unpacker", rb_cObject); rb_define_alloc_func(cUnpacker, MessagePack_Unpacker_alloc); + rb_define_method(cUnpacker, "initialize", MessagePack_Unpacker_initialize, -1); + + /* Buffered API */ rb_define_method(cUnpacker, "feed", MessagePack_Unpacker_feed, 1); rb_define_method(cUnpacker, "fill", MessagePack_Unpacker_fill, 0); rb_define_method(cUnpacker, "each", MessagePack_Unpacker_each, 0); rb_define_method(cUnpacker, "stream", MessagePack_Unpacker_stream_get, 0); rb_define_method(cUnpacker, "stream=", MessagePack_Unpacker_stream_set, 1); - rb_define_module_function(mMessagePack, "unpack", MessagePack_unpack, 1); - rb_define_module_function(mMessagePack, "unpack_limit", MessagePack_unpack_limit, 2); - /* backward compatibility */ + /* Unbuffered API */ rb_define_method(cUnpacker, "execute", MessagePack_Unpacker_execute, 2); rb_define_method(cUnpacker, "execute_limit", MessagePack_Unpacker_execute_limit, 3); rb_define_method(cUnpacker, "finished?", MessagePack_Unpacker_finished_p, 0); rb_define_method(cUnpacker, "data", MessagePack_Unpacker_data, 0); rb_define_method(cUnpacker, "reset", MessagePack_Unpacker_reset, 0); + + /** + * MessagePack module is defined in rbinit.c file. + * mMessagePack = rb_define_module("MessagePack"); + */ + rb_define_module_function(mMessagePack, "unpack", MessagePack_unpack, 1); + rb_define_module_function(mMessagePack, "unpack_limit", MessagePack_unpack_limit, 2); } +/** + * Document-module: MessagePack::Unpacker + * + * Deserializer class that includes Buffered API and Unbuffered API. + * + * + * Buffered API uses the internal buffer of the Unpacker. + * Following code uses Buffered API with an input stream: + * + * # create an unpacker with input stream. + * pac = MessagePack::Unpacker.new(STDIN) + * + * # deserialize object one after another. + * pac.each {|obj| + * # ... + * } + * + * + * Following code doesn't use the input stream and feeds buffer + * manually. This is useful to use special stream or with + * event-driven I/O library. + * + * # create an unpacker without input stream. + * pac = MessagePack::Unpacker.new() + * + * # feed buffer to the internal buffer. + * pac.feed(input_bytes) + * + * # deserialize object one after another. + * pac.each {|obj| + * # ... + * } + * + * + * You can manage the buffer manually with the combination of + * *execute*, *finished?*, *data* and *reset* method. + * + * # create an unpacker. + * pac = MessagePack::Unpacker.new() + * + * # manage buffer and offset manually. + * offset = 0 + * buffer = '' + * + * # read some data into the buffer. + * buffer << [1,2,3].to_msgpack + * buffer << [4,5,6].to_msgpack + * + * while true + * offset = pac.execute(buffer, offset) + * + * if pac.finished? + * obj = pac.data + * + * buffer.slice!(0, offset) + * offset = 0 + * pac.reset + * + * # do something with the object + * # ... + * + * # repeat execution if there are more data. + * next unless buffer.empty? + * end + * + * break + * end + */ + diff --git a/ruby/unpack.h b/ruby/unpack.h index ce2a8de0..91d3eb7c 100644 --- a/ruby/unpack.h +++ b/ruby/unpack.h @@ -1,7 +1,7 @@ /* * MessagePack for Ruby unpacking routine * - * Copyright (C) 2008-2009 FURUHASHI Sadayuki + * Copyright (C) 2008-2010 FURUHASHI Sadayuki * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/ruby/version.rb b/ruby/version.rb new file mode 100644 index 00000000..b1566203 --- /dev/null +++ b/ruby/version.rb @@ -0,0 +1,3 @@ +module MessagePack + VERSION = "0.4.3" +end diff --git a/test/README.md b/test/README.md new file mode 100644 index 00000000..80270022 --- /dev/null +++ b/test/README.md @@ -0,0 +1,38 @@ +MessagePack cross-language test cases +===================================== + +## cases + +Valid serialized data are stored in "cases.mpac" and "cases_compact.mpac". +These files describe same objects. And "cases.json" describes an array of the described objects. + +Thus you can verify your implementations as comparing the objects. + + +## crosslang + +The *crosslang* tool reads serialized data from stdin and writes re-serialize data to stdout. + +There are C++ and Ruby implementation of crosslang tool. You can verify your implementation +as comparing that implementations. + +### C++ version + + $ cd ../cpp && ./configure && make && make install + or + $ port install msgpack # MacPorts + + $ g++ -Wall -lmsgpack crosslang.cc -o crosslang + + $ ./crosslang + Usage: ./crosslang [in-file] [out-file] + +### Ruby version + + $ gem install msgpack + or + $ port install rb_msgpack # MacPorts + + $ ruby crosslang.rb + Usage: crosslang.rb [in-file] [out-file] + diff --git a/test/cases.json b/test/cases.json new file mode 100644 index 00000000..fd390d48 --- /dev/null +++ b/test/cases.json @@ -0,0 +1 @@ +[false,true,null,0,0,0,0,0,0,0,0,0,-1,-1,-1,-1,-1,127,127,255,65535,4294967295,-32,-32,-128,-32768,-2147483648,0.0,-0.0,1.0,-1.0,"a","a","a","","","",[0],[0],[0],[],[],[],{},{},{},{"a":97},{"a":97},{"a":97},[[]],[["a"]]] \ No newline at end of file diff --git a/test/cases.mpac b/test/cases.mpac new file mode 100644 index 00000000..5ec08c6a Binary files /dev/null and b/test/cases.mpac differ diff --git a/test/cases_compact.mpac b/test/cases_compact.mpac new file mode 100644 index 00000000..8812442d Binary files /dev/null and b/test/cases_compact.mpac differ diff --git a/test/cases_gen.rb b/test/cases_gen.rb new file mode 100644 index 00000000..95662fbb --- /dev/null +++ b/test/cases_gen.rb @@ -0,0 +1,99 @@ +# +# MessagePack format test case +# +begin +require 'rubygems' +rescue LoadError +end +require 'msgpack' +require 'json' + +source = <97} FixMap +de 00 01 a1 61 61 # {"a"=>97} map 16 +df 00 00 00 01 a1 61 61 # {"a"=>97} map 32 +91 90 # [[]] +91 91 a1 61 # [["a"]] +EOF + +source.gsub!(/\#.+$/,'') +bytes = source.strip.split(/\s+/).map {|x| x.to_i(16) }.pack('C*') + +objs = [] +compact_bytes = "" + +pac = MessagePack::Unpacker.new +pac.feed(bytes) +pac.each {|obj| + p obj + objs << obj + compact_bytes << obj.to_msgpack +} + +json = objs.to_json + +# self check +cpac = MessagePack::Unpacker.new +cpac.feed(compact_bytes) +cpac.each {|cobj| + obj = objs.shift + if obj != cobj + puts "** SELF CHECK FAILED **" + puts "expected: #{obj.inspect}" + puts "actual: #{cobj.inspect}" + exit 1 + end +} + +File.open("cases.mpac","w") {|f| f.write(bytes) } +File.open("cases_compact.mpac","w") {|f| f.write(compact_bytes) } +File.open("cases.json","w") {|f| f.write(json) } + diff --git a/test/crosslang.cc b/test/crosslang.cc new file mode 100644 index 00000000..be521b36 --- /dev/null +++ b/test/crosslang.cc @@ -0,0 +1,133 @@ +// +// MessagePack cross-language test tool +// +// $ cd ../cpp && ./configure && make && make install +// or +// $ port install msgpack # MacPorts +// +// $ g++ -Wall -lmsgpack crosslang.cc +// +#include +#include +#include +#include +#include +#include +#include + +static int run(int infd, int outfd) +try { + msgpack::unpacker pac; + + while(true) { + pac.reserve_buffer(32*1024); + + ssize_t count = + read(infd, pac.buffer(), pac.buffer_capacity()); + + if(count <= 0) { + if(count == 0) { + return 0; + } + if(errno == EAGAIN || errno == EINTR) { + continue; + } + return 1; + } + + pac.buffer_consumed(count); + + msgpack::unpacked result; + while(pac.next(&result)) { + msgpack::sbuffer sbuf; + msgpack::pack(sbuf, result.get()); + + const char* p = sbuf.data(); + const char* const pend = p + sbuf.size(); + while(p < pend) { + ssize_t bytes = write(outfd, p, pend-p); + + if(bytes <= 0) { + if(count == 0) { + return 0; + } + if(errno == EAGAIN || errno == EINTR) { + continue; + } + return 1; + } + + p += bytes; + } + + } + } + + return 0; + +} catch (std::exception& e) { + std::cerr << e.what() << std::endl; + return 1; +} + +static void usage(const char* prog) +{ + printf( + "Usage: %s [in-file] [out-file]\n" + "\n" + "This tool is for testing of MessagePack implementation.\n" + "This does following behavior:\n" + "\n" + " 1. Reads objects serialized by MessagePack from (default: stdin)\n" + " 2. Re-serializes the objects using C++ implementation of MessagePack (Note that C++ implementation is considered valid)\n" + " 3. Writes the re-serialized objects into (default: stdout)\n" + "\n" + , prog); + exit(1); +} + +int main(int argc, char* argv[]) +{ + int infd = 0; + int outfd = 1; + + if(argc < 1 || argc > 3) { + usage(argv[0]); + } + + for(int i=1; i < argc; ++i) { + if(strlen(argv[i]) > 1 && argv[i][0] == '-') { + usage(argv[0]); + } + } + + if(argc >= 2) { + const char* fname = argv[1]; + if(strcmp(fname, "-") != 0) { + infd = open(fname, O_RDONLY); + if(infd < 0) { + perror("can't open input file"); + exit(1); + } + } + } + + if(argc >= 3) { + const char* fname = argv[2]; + if(strcmp(fname, "-") != 0) { + outfd = open(fname, O_WRONLY | O_CREAT| O_TRUNC, 0666); + if(outfd < 0) { + perror("can't open output file"); + exit(1); + } + } + } + + int code = run(infd, outfd); + + close(infd); + close(outfd); + + return code; +} + diff --git a/test/crosslang.rb b/test/crosslang.rb new file mode 100644 index 00000000..7aa44820 --- /dev/null +++ b/test/crosslang.rb @@ -0,0 +1,88 @@ +# +# MessagePack cross-language test tool +# +# $ gem install msgpack +# or +# $ port install rb_msgpack # MacPorts +# +begin +require 'rubygems' +rescue LoadError +end +require 'msgpack' + +def run(inio, outio) + pac = MessagePack::Unpacker.new(inio) + + begin + pac.each {|obj| + outio.write MessagePack.pack(obj) + outio.flush + } + rescue EOFError + return 0 + rescue + $stderr.puts $! + return 1 + end + + return 0 +end + +def usage + puts < (default: stdin) + 2. Re-serializes the objects using Ruby implementation of MessagePack (Note that Ruby implementation is considered valid) + 3. Writes the re-serialized objects into (default: stdout) + +EOF + exit 1 +end + +inio = $stdin +outio = $stdout + +if ARGV.length > 2 + usage +end + +ARGV.each {|str| + if str.size > 1 && str[0] == ?- + usage + end +} + +if fname = ARGV[0] + unless fname == "-" + begin + inio = File.open(fname) + rescue + puts "can't open output file: #{$!}" + exit 1 + end + end +end + +if fname = ARGV[1] + unless fname == "-" + begin + outio = File.open(fname, "w") + rescue + puts "can't open output file: #{$!}" + exit 1 + end + end +end + +code = run(inio, outio) + +inio.close +outio.close + +exit code +