Skip to content

Commit

Permalink
Merge pull request #1304 from realglobe-Inc/dev/binary-decode_hex
Browse files Browse the repository at this point in the history
Add functions to load and dump hex string

These changes are made under both the "Apache 2.0" and the "GNU Lesser General
Public License 2.1 or later" license terms (dual license).

SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later

Closes #1287.
  • Loading branch information
bettio committed Oct 11, 2024
2 parents ff34010 + 11b23ab commit 6f5d064
Show file tree
Hide file tree
Showing 11 changed files with 355 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ also non string parameters (e.g. `Enum.join([1, 2], ",")`
[#1100](https://github.com/atomvm/AtomVM/issues/1100)
- Support for mounting/unmounting storage on ESP32 (such as SD or internal flash) using
`esp:mount/4` and `esp:umount/1`
- Support for `binary_to_integer/2`
- Support for `binary:decode_hex/1` and `binary:encode_hex/1,2`
- Support for Elixir `Base.decode16/2` and `Base.encode16/2`

### Changed

Expand Down
39 changes: 38 additions & 1 deletion libs/estdlib/src/binary.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
% This file is part of AtomVM.
%
% Copyright 2023 Paul Guyot <[email protected]>
% Copyright 2024 Yuto Oguchi <[email protected]>, Realglobe Inc.
%
% Licensed under the Apache License, Version 2.0 (the "License");
% you may not use this file except in compliance with the License.
Expand All @@ -24,7 +25,7 @@
%%-----------------------------------------------------------------------------
-module(binary).

-export([at/2, part/3, split/2, split/3]).
-export([at/2, decode_hex/1, encode_hex/1, encode_hex/2, part/3, split/2, split/3]).

%%-----------------------------------------------------------------------------
%% @param Binary binary to get a byte from
Expand All @@ -37,6 +38,42 @@
at(_Binary, _Index) ->
erlang:nif_error(undefined).

%%-----------------------------------------------------------------------------
%% @param Data hex encoded binary to decode
%% @returns decoded binary
%% @doc Decodes a hex encoded binary into a binary.
%% @end
%%-----------------------------------------------------------------------------
-spec decode_hex(Data :: <<_:_*16>>) -> binary().
decode_hex(Data) ->
case byte_size(Data) rem 2 of
0 -> <<<<(binary_to_integer(B, 16))>> || <<B:2/binary>> <= Data>>;
_ -> erlang:error(badarg)
end.

%%-----------------------------------------------------------------------------
%% @param Data binary data to convert into hex encoded binary
%% @returns hex encoded binary
%% @doc Encodes a binary into a hex encoded binary using the specified case for the hexadecimal digits "a" to "f".
%% @end
%%-----------------------------------------------------------------------------
-spec encode_hex(Data :: binary()) -> binary().
encode_hex(Data) ->
encode_hex(Data, uppercase).

%%-----------------------------------------------------------------------------
%% @param Data binary data to convert into hex encoded binary
%% @param Case which case to encode into
%% @returns hex encoded binary
%% @doc Encodes a binary into a hex encoded binary using the specified case for the hexadecimal digits "a" to "f".
%% @end
%%-----------------------------------------------------------------------------
-spec encode_hex(Data :: binary(), Case :: lowercase | uppercase) -> binary().
encode_hex(Data, uppercase) ->
<<(integer_to_binary(B, 16)) || <<B:4>> <= Data>>;
encode_hex(Data, lowercase) ->
<<<<(hd(string:to_lower(integer_to_list(B, 16)))):8>> || <<B:4>> <= Data>>.

%%-----------------------------------------------------------------------------
%% @param Binary binary to extract a subbinary from
%% @param Pos 0-based index of the subbinary to extract
Expand Down
11 changes: 11 additions & 0 deletions libs/estdlib/src/erlang.erl
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
binary_to_atom/1,
binary_to_atom/2,
binary_to_integer/1,
binary_to_integer/2,
binary_to_list/1,
atom_to_binary/1,
atom_to_binary/2,
Expand Down Expand Up @@ -651,6 +652,16 @@ binary_to_atom(_Binary, _Encoding) ->
binary_to_integer(_Binary) ->
erlang:nif_error(undefined).

%%-----------------------------------------------------------------------------
%% @param Binary Binary to parse for integer
%% @returns the integer represented by the binary
%% @doc Parse the text in a given binary as an integer.
%% @end
%%-----------------------------------------------------------------------------
-spec binary_to_integer(Binary :: binary(), Base :: 2..36) -> integer().
binary_to_integer(_Binary, Base) ->
erlang:nif_error(undefined).

%%-----------------------------------------------------------------------------
%% @param Binary Binary to convert to list
%% @returns a list of bytes from the binary
Expand Down
227 changes: 227 additions & 0 deletions libs/exavmlib/lib/Base.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
#
# This file is part of elixir-lang.
#
# Copyright 2014-2023 Elixir Contributors
# https://github.com/elixir-lang/elixir/commits/v1.17.3/lib/elixir/lib/base.ex
#
# Copyright 2024 Yuto Oguchi <[email protected]>, Realglobe Inc.
#
# 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.
#
# SPDX-License-Identifier: Apache-2.0
#

defmodule Base do
@moduledoc """
This module provides data encoding and decoding functions
according to [RFC 4648](https://tools.ietf.org/html/rfc4648).
This document defines the commonly used base 16, base 32, and base
64 encoding schemes.
## Base 16 alphabet
| Value | Encoding | Value | Encoding | Value | Encoding | Value | Encoding |
|------:|:---------|------:|:---------|------:|:---------|------:|:---------|
| 0 | 0 | 4 | 4 | 8 | 8 | 12 | C |
| 1 | 1 | 5 | 5 | 9 | 9 | 13 | D |
| 2 | 2 | 6 | 6 | 10 | A | 14 | E |
| 3 | 3 | 7 | 7 | 11 | B | 15 | F |
## Base 32 alphabet
| Value | Encoding | Value | Encoding | Value | Encoding | Value | Encoding |
|------:|:---------|------:|:---------|------:|:---------|------:|:---------|
| 0 | A | 9 | J | 18 | S | 27 | 3 |
| 1 | B | 10 | K | 19 | T | 28 | 4 |
| 2 | C | 11 | L | 20 | U | 29 | 5 |
| 3 | D | 12 | M | 21 | V | 30 | 6 |
| 4 | E | 13 | N | 22 | W | 31 | 7 |
| 5 | F | 14 | O | 23 | X | | |
| 6 | G | 15 | P | 24 | Y | (pad) | = |
| 7 | H | 16 | Q | 25 | Z | | |
| 8 | I | 17 | R | 26 | 2 | | |
## Base 32 (extended hex) alphabet
| Value | Encoding | Value | Encoding | Value | Encoding | Value | Encoding |
|------:|:---------|------:|:---------|------:|:---------|------:|:---------|
| 0 | 0 | 9 | 9 | 18 | I | 27 | R |
| 1 | 1 | 10 | A | 19 | J | 28 | S |
| 2 | 2 | 11 | B | 20 | K | 29 | T |
| 3 | 3 | 12 | C | 21 | L | 30 | U |
| 4 | 4 | 13 | D | 22 | M | 31 | V |
| 5 | 5 | 14 | E | 23 | N | | |
| 6 | 6 | 15 | F | 24 | O | (pad) | = |
| 7 | 7 | 16 | G | 25 | P | | |
| 8 | 8 | 17 | H | 26 | Q | | |
## Base 64 alphabet
| Value | Encoding | Value | Encoding | Value | Encoding | Value | Encoding |
|------:|:----------|------:|:---------|------:|:---------|------:|:---------|
| 0 | A | 17 | R | 34 | i | 51 | z |
| 1 | B | 18 | S | 35 | j | 52 | 0 |
| 2 | C | 19 | T | 36 | k | 53 | 1 |
| 3 | D | 20 | U | 37 | l | 54 | 2 |
| 4 | E | 21 | V | 38 | m | 55 | 3 |
| 5 | F | 22 | W | 39 | n | 56 | 4 |
| 6 | G | 23 | X | 40 | o | 57 | 5 |
| 7 | H | 24 | Y | 41 | p | 58 | 6 |
| 8 | I | 25 | Z | 42 | q | 59 | 7 |
| 9 | J | 26 | a | 43 | r | 60 | 8 |
| 10 | K | 27 | b | 44 | s | 61 | 9 |
| 11 | L | 28 | c | 45 | t | 62 | + |
| 12 | M | 29 | d | 46 | u | 63 | / |
| 13 | N | 30 | e | 47 | v | | |
| 14 | O | 31 | f | 48 | w | (pad) | = |
| 15 | P | 32 | g | 49 | x | | |
| 16 | Q | 33 | h | 50 | y | | |
## Base 64 (URL and filename safe) alphabet
| Value | Encoding | Value | Encoding | Value | Encoding | Value | Encoding |
|------:|:---------|------:|:---------|------:|:---------|------:|:---------|
| 0 | A | 17 | R | 34 | i | 51 | z |
| 1 | B | 18 | S | 35 | j | 52 | 0 |
| 2 | C | 19 | T | 36 | k | 53 | 1 |
| 3 | D | 20 | U | 37 | l | 54 | 2 |
| 4 | E | 21 | V | 38 | m | 55 | 3 |
| 5 | F | 22 | W | 39 | n | 56 | 4 |
| 6 | G | 23 | X | 40 | o | 57 | 5 |
| 7 | H | 24 | Y | 41 | p | 58 | 6 |
| 8 | I | 25 | Z | 42 | q | 59 | 7 |
| 9 | J | 26 | a | 43 | r | 60 | 8 |
| 10 | K | 27 | b | 44 | s | 61 | 9 |
| 11 | L | 28 | c | 45 | t | 62 | - |
| 12 | M | 29 | d | 46 | u | 63 | _ |
| 13 | N | 30 | e | 47 | v | | |
| 14 | O | 31 | f | 48 | w | (pad) | = |
| 15 | P | 32 | g | 49 | x | | |
| 16 | Q | 33 | h | 50 | y | | |
"""

@type encode_case :: :upper | :lower
@type decode_case :: :upper | :lower | :mixed

@doc """
Decodes a base 16 encoded string into a binary string.
## Options
The accepted options are:
* `:case` - specifies the character case to accept when decoding
The values for `:case` can be:
* `:upper` - only allows upper case characters (default)
* `:lower` - only allows lower case characters
* `:mixed` - allows mixed case characters
## Examples
iex> Base.decode16("666F6F626172")
{:ok, "foobar"}
iex> Base.decode16("666f6f626172", case: :lower)
{:ok, "foobar"}
iex> Base.decode16("666f6F626172", case: :mixed)
{:ok, "foobar"}
"""
@spec decode16(binary, case: decode_case) :: {:ok, binary} | :error
def decode16(string, ops \\ []) do
{:ok, decode16!(string, ops)}
rescue
ArgumentError -> :error
end

@doc """
Decodes a base 16 encoded string into a binary string.
## Options
The accepted options are:
* `:case` - specifies the character case to accept when decoding
The values for `:case` can be:
* `:upper` - only allows upper case characters (default)
* `:lower` - only allows lower case characters
* `:mixed` - allows mixed case characters
An `ArgumentError` exception is raised if the padding is incorrect or
a non-alphabet character is present in the string.
## Examples
iex> Base.decode16!("666F6F626172")
"foobar"
iex> Base.decode16!("666f6f626172", case: :lower)
"foobar"
iex> Base.decode16!("666f6F626172", case: :mixed)
"foobar"
"""
@spec decode16!(binary, case: decode_case) :: binary
def decode16!(string, opts \\ [])

def decode16!(string, _ops) when is_binary(string) and rem(byte_size(string), 2) == 0 do
# TODO: support :case option
:binary.decode_hex(string)
end

def decode16!(string, _opts) when is_binary(string) do
raise ArgumentError,
"string given to decode has wrong length. An even number of bytes was expected, got: #{byte_size(string)}. " <>
"Double check your string for unwanted characters or pad it accordingly"
end

@doc """
Encodes a binary string into a base 16 encoded string.
## Options
The accepted options are:
* `:case` - specifies the character case to use when encoding
The values for `:case` can be:
* `:upper` - uses upper case characters (default)
* `:lower` - uses lower case characters
## Examples
iex> Base.encode16("foobar")
"666F6F626172"
iex> Base.encode16("foobar", case: :lower)
"666f6f626172"
"""
@spec encode16(binary, case: encode_case) :: binary
def encode16(data, opts \\ []) do
case Keyword.get(opts, :case, :upper) do
:upper -> :binary.encode_hex(data, :uppercase)
:lower -> :binary.encode_hex(data, :lowercase)
end
end
end
1 change: 1 addition & 0 deletions libs/exavmlib/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ include(BuildElixir)

set(ELIXIR_MODULES
AVMPort
Base
Bitwise
Code
Console
Expand Down
26 changes: 18 additions & 8 deletions src/libAtomVM/nifs.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ static term nif_erlang_atom_to_binary(Context *ctx, int argc, term argv[]);
static term nif_erlang_atom_to_list_1(Context *ctx, int argc, term argv[]);
static term nif_erlang_binary_to_atom_2(Context *ctx, int argc, term argv[]);
static term nif_erlang_binary_to_float_1(Context *ctx, int argc, term argv[]);
static term nif_erlang_binary_to_integer_1(Context *ctx, int argc, term argv[]);
static term nif_erlang_binary_to_integer(Context *ctx, int argc, term argv[]);
static term nif_erlang_binary_to_list_1(Context *ctx, int argc, term argv[]);
static term nif_erlang_binary_to_existing_atom_2(Context *ctx, int argc, term argv[]);
static term nif_erlang_concat_2(Context *ctx, int argc, term argv[]);
Expand Down Expand Up @@ -275,7 +275,7 @@ static const struct Nif binary_to_float_nif =
static const struct Nif binary_to_integer_nif =
{
.base.type = NIFFunctionType,
.nif_ptr = nif_erlang_binary_to_integer_1
.nif_ptr = nif_erlang_binary_to_integer
};

static const struct Nif binary_to_list_nif =
Expand Down Expand Up @@ -1819,10 +1819,8 @@ static term nif_erlang_binary_to_atom_2(Context *ctx, int argc, term argv[])
return binary_to_atom(ctx, argc, argv, 1);
}

static term nif_erlang_binary_to_integer_1(Context *ctx, int argc, term argv[])
static term nif_erlang_binary_to_integer(Context *ctx, int argc, term argv[])
{
UNUSED(argc);

term bin_term = argv[0];
VALIDATE_VALUE(bin_term, term_is_binary);

Expand All @@ -1833,14 +1831,26 @@ static term nif_erlang_binary_to_integer_1(Context *ctx, int argc, term argv[])
RAISE_ERROR(BADARG_ATOM);
}

char null_terminated_buf[24];
uint8_t base = 10;

if (argc == 2) {
term int_term = argv[1];
VALIDATE_VALUE(int_term, term_is_uint8);
base = term_to_uint8(int_term);
}

if (UNLIKELY((base < 2) || (base > 36))) {
RAISE_ERROR(BADARG_ATOM);
}

char null_terminated_buf[65];
memcpy(null_terminated_buf, bin_data, bin_data_size);
null_terminated_buf[bin_data_size] = '\0';

//TODO: handle 64 bits numbers
//TODO: handle errors
//TODO: do not copy buffer, implement a custom strotoll
char *endptr;
uint64_t value = strtoll(null_terminated_buf, &endptr, 10);
uint64_t value = strtoll(null_terminated_buf, &endptr, base);
if (*endptr != '\0') {
RAISE_ERROR(BADARG_ATOM);
}
Expand Down
1 change: 1 addition & 0 deletions src/libAtomVM/nifs.gperf
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ erlang:binary_to_atom/1, &binary_to_atom_nif
erlang:binary_to_atom/2, &binary_to_atom_nif
erlang:binary_to_float/1, &binary_to_float_nif
erlang:binary_to_integer/1, &binary_to_integer_nif
erlang:binary_to_integer/2, &binary_to_integer_nif
erlang:binary_to_list/1, &binary_to_list_nif
erlang:binary_to_existing_atom/1, &binary_to_existing_atom_nif
erlang:binary_to_existing_atom/2, &binary_to_existing_atom_nif
Expand Down
Loading

0 comments on commit 6f5d064

Please sign in to comment.