Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MessageOptions: ensure decoding size #53

Open
jeanparpaillon opened this issue Dec 8, 2020 · 1 comment
Open

MessageOptions: ensure decoding size #53

jeanparpaillon opened this issue Dec 8, 2020 · 1 comment

Comments

@jeanparpaillon
Copy link
Collaborator

See TODO:

    defp decode_extended(delta_sum, delta, length, tail) do
      {tail1, key} =
        cond do
          delta < 13 ->
            {tail, delta_sum + delta}

          delta == 13 ->
            # TODO: size here `::size(4)`?
            <<key, new_tail1::binary>> = tail
            {new_tail1, delta_sum + key + 13}

          delta == 14 ->
            <<key::size(16), new_tail1::binary>> = tail
            {new_tail1, delta_sum + key + 269}
        end

      {tail2, option_length} =
        cond do
          length < 13 ->
            {tail1, length}

          length == 13 ->
            # TODO: size here `::size(4)`?
            <<extended_option_length, new_tail2::binary>> = tail1
            {new_tail2, extended_option_length + 13}

          length == 14 ->
            <<extended_option_length::size(16), new_tail2::binary>> = tail1
            {new_tail2, extended_option_length + 269}
        end

      {key, option_length, tail2}
    end
@jeanparpaillon
Copy link
Collaborator Author

See also:

    defp encode([{key, value} | option_list], delta_sum, acc) do
      {delta, extended_number} =
        cond do
          key - delta_sum >= 269 ->
            {14, <<key - delta_sum - 269::size(16)>>}

          key - delta_sum >= 13 ->
            {13, <<key - delta_sum - 13>>}

          true ->
            {key - delta_sum, <<>>}
        end

      {length, extended_length} =
        cond do
          byte_size(value) >= 269 ->
            {14, <<byte_size(value) - 269::size(16)>>}

          byte_size(value) >= 13 ->
            {13, <<byte_size(value) - 13>>}

          true ->
            {byte_size(value), <<>>}
        end

      acc2 = <<
        acc::binary,
        delta::size(4),
        length::size(4),
        # TODO: what size should this be?
        extended_number::binary,
        extended_length::binary,
        value::binary
      >>

      encode(option_list, key, acc2)
    end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant