Skip to content
Snippets Groups Projects
  1. Oct 26, 2022
    • Vladimir Davydov's avatar
      msgpack: fix crash on decode of 0xc1 · e48a8f4b
      Vladimir Davydov authored
      0xc1 isn't a valid MsgPack header, but it was allowed by mp_check.
      As a result, msgpack.decode crashed while trying to decode it.
      This commit updates the msgpuck library to fix this issue.
      
      Closes #7818
      
      NO_DOC=bug fix
      
      (cherry picked from commit ced405af)
      e48a8f4b
  2. Mar 14, 2022
    • Vladimir Davydov's avatar
      msgpack: add iterator.take_array · 68bf3885
      Vladimir Davydov authored
      NO_CHANGELOG=feature was not released
      
      Closes #6823
      
      @TarantoolBot document
      Title: Document msgpack object iterator take_array method
      
      The new method copies the given number of msgpack values starting from
      the iterator cursor position to a new msgpack array object. On success
      it returns the new msgpack object and advances the iterator cursor. If
      there isn't enough values to decode, it raises a Lua error and leaves
      the iterator cursor unchanged.
      
      This function could be implemented in Lua like this:
      
      ```lua
      function take_array(iter, count)
          local array = {}
          for _ = 1, count do
              table.insert(array, iter:take())
          end
          return msgpack.object(array)
      end
      ```
      
      Usage example:
      
      ```lua
      local mp = msgpack.object({10, 20, 30, 40})
      local it = mp:iterator()
      it:decode_array_header()       -- returns 4
      it:decode()                    -- returns 10
      local mp2 = it:take_array(2)
      mp2:decode()                   -- returns {20, 30}
      it:decode()                    -- returns 40
      ```
      68bf3885
  3. Dec 20, 2021
    • Vladimir Davydov's avatar
      msgpack: introduce msgpack.object · 36da3962
      Vladimir Davydov authored
      Closes #1629
      Closes #3909
      Closes #5316
      Needed for #3349
      Needed for #4861
      
      @TarantoolBot document
      Title: Document msgpack.object
      
      The following new methods were added to the Lua `msgpack` module:
       - `msgpack.object()`: Encodes an arbitrary Lua object given in its only
         argument in MsgPack and returns the encoded MsgPack data encapsulated
         in a MsgPack object.
       - `msgpack.object_from_raw()`: Creates a MsgPack object from raw data
         given either as a string or as a pointer and size.
       - `msgpack.is_object()`: Checks is the given argument is a MsgPack
         object.
      
      Example:
      
      ```lua
      local buffer = require('buffer')
      local msgpack = require('msgpack')
      -- Create an object from a Lua object.
      local mp = msgpack.object(123)
      local mp = msgpack.object("foobar")
      local mp = msgpack.object({1, 2, 3})
      local mp = msgpack.object({foo = 1, bar = 2})
      local mp = msgpack.object(box.tuple.new(1, 2, 3))
      -- Create an object from raw data given as a string.
      local data = msgpack.encode({1, 2, 3})
      local mp = msgpack.object_from_raw(data)
      -- Create an object from raw data given as ptr and size.
      local buf = buffer.ibuf()
      msgpack.encode({1, 2, 3}, buf)
      local mp = msgpack.object_from_raw(buf.buf, buf:size())
      -- Check object.
      msgpack.is_object(mp) -- true
      msgpack.is_object({}) -- false
      ```
      
      A MsgPack object has the following methods:
      
       - `mp:decode()`: Decodes MsgPack and returns a Lua object.
       - `mp:iterator()`: Returns an iterator over MsgPack data.
      
      A MsgPack iterator has the following methods:
      
       - `it:decode_array_header()`: Decodes a MsgPack array header under the
         cursor and returns the number of elements in the array. After calling
         this function the iterator points to the first element of the array
         or to the value following the array if the array is empty. Raises an
         error if the type of the value under the iterator cursor is not
         `MP_ARRAY`.
       - `it:decode_map_header()`: Decodes a MsgPack map header under the
         cursor and returns the number of key value pairs in the map. After
         calling this function the iterator points to the first key stored in
         the map or to the value following the map if the map is empty. Raises
         an error if the type of the value under the iterator cursor is not
         `MP_MAP`.
       - `it:decode()`: Decodes a MsgPack value under the iterator cursor and
         advances the cursor. Returns a Lua object corresponding to the
         MsgPack value. Raises a Lua error if there's no data to decode.
       - `it:take()`: Returns a MsgPack value under the iterator cursor as a
         MsgPack object (without decoding). Raises a Lua error if there's no
         data to decode. This function doesn't copy MsgPack data - instead it
         takes a reference to the original object.
       - `it:skip()`: Advances the iterator cursor by skipping one MsgPack
         value under the cursor. Returns nothing. Raises a Lua error if
         there's not data to skip.
      
      Usage example:
      
      ```lua
      local msgpack = require('msgpack')
      local mp = msgpack.object({foo = 123, bar = {1, 2, 3}})
      local it = mp:iterator()
      it:decode_map_header()  -- returns 2
      it:decode()             -- returns 'foo'
      it:decode()             -- returns 123
      it:skip()               -- returns none, skips 'bar'
      local mp2 = it:take()
      mp2:decode()            -- returns {1, 2, 3}
      ```
      
      For more examples, see `test/app-luatest/msgpack_test.lua`.
      
      A MsgPack object can be passed to MsgPack encoder with the effect being
      the same as passing the original Lua object:
      
      ```lua
      local msgpack = require('msgpack')
      local mp = msgpack.object(123)
      msgpack.object({mp, mp}):decode()         -- returns {123, 123}
      msgpack.decode(msgpack.encode({mp, mp}))  -- returns {123, 123}
      ```
      
      In particular, this means that if a MsgPack object stores an array,
      it can be inserted into a database space:
      
      ```lua
      box.space.my_space:insert(msgpack.object({1, 2, 3}))
      ```
      36da3962
  4. Oct 26, 2021
Loading