You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by ja...@apache.org on 2019/02/23 03:57:41 UTC

[couchdb-b64url] branch master updated (71e064f -> d9220e2)

This is an automated email from the ASF dual-hosted git repository.

jaydoane pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/couchdb-b64url.git.


    from 71e064f  Merge branch 'rand-compat'
     new a48ee01  Change minimum supported Erlang version to OTP 19
     new 69b5308  Git ignore compile_commands.json
     new d9220e2  Merge pull request #7 from cloudant/minimum-erlang-otp-19

The 31 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .gitignore             |  1 +
 .travis.yml            | 11 +++++-----
 rebar.config           | 11 ----------
 test/b64url_rand.erl   | 57 --------------------------------------------------
 test/b64url_tests.erl  | 12 +++++------
 test/benchmark.escript |  4 ++--
 6 files changed, 15 insertions(+), 81 deletions(-)
 delete mode 100644 test/b64url_rand.erl


[couchdb-b64url] 09/31: Revert "Attempting to avoid a compiler warning on Jenkins"

Posted by ja...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jaydoane pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/couchdb-b64url.git

commit 6bd8868adad4d9d5b255fedb2a48cce9072ea3bf
Author: Paul J. Davis <pa...@gmail.com>
AuthorDate: Thu Nov 21 15:44:55 2013 -0600

    Revert "Attempting to avoid a compiler warning on Jenkins"
    
    This reverts commit edaf27bc96c9384558df4742e77a8bf85d989eda.
---
 c_src/b64url.c | 14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)

diff --git a/c_src/b64url.c b/c_src/b64url.c
index 3528d3b..0bc6ab8 100644
--- a/c_src/b64url.c
+++ b/c_src/b64url.c
@@ -546,32 +546,28 @@ b64url_decode_cont(ErlNifEnv* env, int argc, const ENTERM argv[])
     int status;
 
     if(argc != 2) {
-        goto badarg;
+        return enif_make_badarg(env);
     }
 
     if(!enif_inspect_iolist_as_binary(env, argv[0], &src)) {
-        goto badarg;
+        return enif_make_badarg(env);
     }
 
     if(!enif_get_resource(env, argv[1], priv->res_st, (void**) &st)) {
-        goto badarg;
+        return enif_make_badarg(env);
     }
 
     if(!check_pid(env, st)) {
-        goto badarg;
+        return enif_make_badarg(env);
     }
 
     if(src.size != st->len) {
-        goto badarg;
+        return enif_make_badarg(env);
     }
 
     status = b64url_decode(env, &src, st, &ret);
 
     return b64url_st_dec_ret(env, st, status, ret);
-
-badarg:
-    ret = enif_make_badarg(env);
-    return ret;
 }
 
 


[couchdb-b64url] 14/31: Rename the resource to b64url_st

Posted by ja...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jaydoane pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/couchdb-b64url.git

commit 48a3b66cab332d7b7e835488793596e31384db57
Author: Paul J. Davis <pa...@gmail.com>
AuthorDate: Wed Dec 4 14:00:26 2013 -0600

    Rename the resource to b64url_st
    
    Vestiges when this was in an app named couch_seqs.
---
 c_src/b64url.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/c_src/b64url.c b/c_src/b64url.c
index 604f21a..478468b 100644
--- a/c_src/b64url.c
+++ b/c_src/b64url.c
@@ -253,7 +253,7 @@ load(ErlNifEnv* env, void** priv, ENTERM info)
     }
 
     res = enif_open_resource_type(
-            env, NULL, "couch_seq", b64url_st_free, flags, NULL);
+            env, NULL, "b64url_st", b64url_st_free, flags, NULL);
     if(res == NULL) {
         return 1;
     }


[couchdb-b64url] 30/31: Git ignore compile_commands.json

Posted by ja...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jaydoane pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/couchdb-b64url.git

commit 69b530877b12dc3a8d5620fbb7f8febed0404a97
Author: Jay Doane <ja...@apache.org>
AuthorDate: Fri Feb 22 09:40:27 2019 -0800

    Git ignore compile_commands.json
---
 .gitignore | 1 +
 1 file changed, 1 insertion(+)

diff --git a/.gitignore b/.gitignore
index 24f9e66..d20aa47 100644
--- a/.gitignore
+++ b/.gitignore
@@ -4,3 +4,4 @@ ebin/
 priv/
 .rebar/
 vc110.pdb
+compile_commands.json


[couchdb-b64url] 16/31: Remove depdency on PropEr

Posted by ja...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jaydoane pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/couchdb-b64url.git

commit 0eabe448afb6d9d6b9f7f175a7f5873aa65c1bbc
Author: Paul J. Davis <pa...@gmail.com>
AuthorDate: Mon Aug 4 13:26:39 2014 -0500

    Remove depdency on PropEr
    
    PropEr is GPLv3 so I'm removing the dependency. It was really only a
    glorified test data generator so I just implemented a bare bones
    version.
---
 rebar.config          |  6 ++--
 test/b64url_tests.erl | 96 ++++++++++++++++++++++++++++-----------------------
 2 files changed, 54 insertions(+), 48 deletions(-)

diff --git a/rebar.config b/rebar.config
index 2dae006..534c049 100644
--- a/rebar.config
+++ b/rebar.config
@@ -2,6 +2,8 @@
     {"priv/b64url.so", ["c_src/*.c"]}
 ]}.
 
+{eunit_opts, [verbose]}.
+
 {port_env, [
     % Development compilation
     % {".*", "CFLAGS", "$CFLAGS -g -Wall -Werror -fPIC"}
@@ -9,7 +11,3 @@
     % Production compilation
     {".*", "CFLAGS", "$CFLAGS -Wall -Werror -DNDEBUG -O3"}
 ]}.
-
-{deps, [
-    {proper, ".*", {git, "https://github.com/manopapad/proper.git", "master"}}
-]}.
diff --git a/test/b64url_tests.erl b/test/b64url_tests.erl
index cac29f7..b3d7779 100644
--- a/test/b64url_tests.erl
+++ b/test/b64url_tests.erl
@@ -1,87 +1,95 @@
 -module(b64url_tests).
--compile(export_all).
 
--include_lib("proper/include/proper.hrl").
+
 -include_lib("eunit/include/eunit.hrl").
 
 
-table_test_() ->
-    ?_assertEqual(ok, b64url:check_tables()).
+-define(MAX_SIZE, 6401).
+-define(NUM_TESTS, 500).
 
 
-proper_test_() ->
-    PropErOpts = [
-        {to_file, user},
-        {max_size, 6401},
-        {numtests, 500}
-    ],
-    {timeout, 3600, ?_assertEqual([], proper:module(?MODULE, PropErOpts))}.
+table_test() ->
+    ?assertEqual(ok, b64url:check_tables()).
 
 
-prop_encode_binary() ->
-    ?FORALL(Bin, binary(), begin
+encode_binary_test() ->
+    lists:foreach(fun(_) ->
+        Bin = gen_binary(),
         A = couch_encode_base64url(Bin),
         B = b64url:encode(Bin),
-        A == B
-    end).
+        ?assertEqual(A, B)
+    end, lists:seq(1, ?NUM_TESTS)).
 
 
-prop_encode_iolist() ->
-    ?FORALL(IoList, shallow_iolist(), begin
+encode_iolist_test() ->
+    lists:foreach(fun(_) ->
+        IoList = shallow_iolist(),
         A = couch_encode_base64url(iolist_to_binary(IoList)),
         B = b64url:encode(IoList),
-        A == B
-    end).
+        ?assertEqual(A, B)
+    end, lists:seq(1, ?NUM_TESTS)).
 
 
-prop_decode_binary() ->
-    ?FORALL(Bin, binary(), begin
+decode_binary_test() ->
+    lists:foreach(fun(_) ->
+        Bin = gen_binary(),
         B64UrlBin = couch_encode_base64url(Bin),
         Dec = b64url:decode(B64UrlBin),
-        Dec == Bin
-    end).
+        ?assertEqual(Bin, Dec)
+    end, lists:seq(1, ?NUM_TESTS)).
 
 
-prop_decode_iolist() ->
-    ?FORALL(IoList, shallow_b64_iolist(), begin
+decode_iolist_test() ->
+    lists:foreach(fun(_) ->
+        IoList = shallow_b64_iolist(),
         A = couch_decode_base64url(iolist_to_binary(IoList)),
         B = b64url:decode(IoList),
-        A == B
-    end).
+        ?assertEqual(A, B)
+    end, lists:seq(1, ?NUM_TESTS)).
 
 
-prop_decode_binary_error() ->
-    ?FORALL({ErrBin, BlockPos}, bad_binary(), begin
+decode_binary_error_test() ->
+    lists:foreach(fun(_) ->
+        {ErrBin, BlockPos} = bad_binary(),
         Dec = b64url:decode(ErrBin),
-        Dec == {error, {bad_block, BlockPos}}
-    end).
+        ?assertEqual({error, {bad_block, BlockPos}}, Dec)
+    end, lists:seq(1, ?NUM_TESTS)).
+
+
+decode_bad_length_test() ->
+    lists:foreach(fun(_) ->
+        Bin = bad_len_binary(),
+        ?assertError(badarg, b64url:decode(Bin))
+    end, lists:seq(1, ?NUM_TESTS)).
 
 
-prop_decode_bad_length() ->
-    ?FORALL(Bin, bad_len_binary(), begin
-        try
-            b64url:decode(Bin),
-            false
-        catch error:badarg ->
-            true
-        end
-    end).
+gen_binary() ->
+    case get(random_seed) of
+        undefined ->
+            random:seed(os:timestamp());
+        _ ->
+            ok
+    end,
+    % -1 here so that zero is a possibility
+    Length = random:uniform(?MAX_SIZE) - 1,
+    Bytes = tl([random:uniform(256)-1 || _ <- lists:seq(0, Length)]),
+    list_to_binary(Bytes).
 
 
 shallow_iolist() ->
-    ?LET(Bin, binary(), to_iolist(Bin)).
+    to_iolist(gen_binary()).
 
 
 shallow_b64_iolist() ->
-    ?LET(Bin, binary(), to_iolist(couch_encode_base64url(Bin))).
+    to_iolist(couch_encode_base64url(gen_binary())).
 
 
 bad_binary() ->
-    ?LET(Bin, binary(), insert_error(Bin)).
+    insert_error(gen_binary()).
 
 
 bad_len_binary() ->
-    ?LET(Bin, binary(), make_bad_len(Bin)).
+    make_bad_len(gen_binary()).
 
 
 to_iolist(<<>>) ->


[couchdb-b64url] 27/31: Handle deprecated random module

Posted by ja...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jaydoane pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/couchdb-b64url.git

commit 686ebd0cd6a54dad0e17694ad960745a876b29c9
Author: Nick Vatamaniuc <va...@apache.org>
AuthorDate: Tue Oct 3 01:51:27 2017 -0400

    Handle deprecated random module
    
    Use a compile time check for platform versions, then a macro conditional in a
    separate rand module.
---
 rebar.config           | 15 +++++++++++--
 test/b64url_rand.erl   | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++
 test/b64url_tests.erl  | 27 ++++++++++++------------
 test/benchmark.escript |  4 ++--
 4 files changed, 86 insertions(+), 17 deletions(-)

diff --git a/rebar.config b/rebar.config
index 4732af8..a84ea97 100644
--- a/rebar.config
+++ b/rebar.config
@@ -2,8 +2,6 @@
     {"priv/b64url.so", ["c_src/*.c"]}
 ]}.
 
-{eunit_opts, [verbose]}.
-
 {port_env, [
     % Development compilation
     % {".*", "CFLAGS", "$CFLAGS -g -Wall -Werror -fPIC"}
@@ -13,3 +11,16 @@
     {"win32", "CFLAGS", "$CFLAGS /O2 /DNDEBUG /Wall"}
 
 ]}.
+
+{eunit_opts, [verbose]}.
+
+{erl_opts, [
+   {platform_define, "^R16", 'NORANDMODULE'},
+   {platform_define, "^17", 'NORANDMODULE'}
+]}.
+
+{eunit_compile_opts, [
+   {platform_define, "^R16", 'NORANDMODULE'},
+   {platform_define, "^17", 'NORANDMODULE'}
+]}.
+
diff --git a/test/b64url_rand.erl b/test/b64url_rand.erl
new file mode 100644
index 0000000..a397c3b
--- /dev/null
+++ b/test/b64url_rand.erl
@@ -0,0 +1,57 @@
+% 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.
+
+-module(b64url_rand).
+
+
+-export([
+    uniform/0,
+    uniform/1
+]).
+
+
+-ifdef(NORANDMODULE).
+
+
+uniform() ->
+    maybe_set_random_seed(),
+    random:uniform().
+
+
+uniform(N) ->
+    maybe_set_random_seed(),
+    random:uniform(N).
+
+
+maybe_set_random_seed() ->
+    case get(random_seed) of
+        undefined ->
+            {_, Sec, USec} = os:timestamp(),
+            Seed = {erlang:phash2(self()), Sec, USec},
+            random:seed(Seed);
+        _ ->
+            ok
+    end.
+
+
+-else.
+
+
+uniform() ->
+    rand:uniform().
+
+
+uniform(N) ->
+    rand:uniform(N).
+
+
+-endif.
diff --git a/test/b64url_tests.erl b/test/b64url_tests.erl
index 7b21459..c5831ef 100644
--- a/test/b64url_tests.erl
+++ b/test/b64url_tests.erl
@@ -64,7 +64,8 @@ decode_bad_length_test() ->
 
 
 gen_binary() ->
-    Length = crypto:rand_uniform(0, ?MAX_SIZE),
+    % -1 to allow for 0 length
+    Length = b64url_rand:uniform(?MAX_SIZE) - 1,
     crypto:strong_rand_bytes(Length).
 
 
@@ -85,31 +86,31 @@ bad_len_binary() ->
 
 
 to_iolist(<<>>) ->
-    case crypto:rand_uniform(0,2) of
-        0 -> <<>>;
-        1 -> [<<>>]
+    case b64url_rand:uniform(2) of
+        1 -> <<>>;
+        2 -> [<<>>]
     end;
 to_iolist(B) when is_binary(B), size(B) > 0 ->
-    S = crypto:rand_uniform(1, size(B) + 1),
+    S = b64url_rand:uniform(size(B)),
     <<First:S/binary, Second/binary>> = B,
-    case crypto:rand_uniform(0, 3) of
-        0 ->
-            [to_iolist(First), Second];
+    case b64url_rand:uniform(3) of
         1 ->
-            [First, to_iolist(Second)];
+            [to_iolist(First), Second];
         2 ->
+            [First, to_iolist(Second)];
+        3->
             [First, Second]
     end.
 
 
 insert_error(B) when is_binary(B), size(B) < 2 ->
-    case crypto:rand_uniform(0, 2) of
-        0 -> {<<122, 255>>, 0};
-        1 -> {<<122, 122, 255>>, 0}
+    case b64url_rand:uniform(2) of
+        1 -> {<<122, 255>>, 0};
+        2 -> {<<122, 122, 255>>, 0}
     end;
 insert_error(B) when is_binary(B) ->
     B64 = couch_encode_base64url(B),
-    S = crypto:rand_uniform(0, size(B64)),
+    S = b64url_rand:uniform(size(B64) - 1),
     <<First:S/binary, _:1/binary, Second/binary>> = B64,
     {<<First:S/binary, 255, Second/binary>>, 4 * (S div 4)}.
 
diff --git a/test/benchmark.escript b/test/benchmark.escript
index e1a1f70..75c2341 100755
--- a/test/benchmark.escript
+++ b/test/benchmark.escript
@@ -115,7 +115,7 @@ run_worker(St, Started) ->
 
 
 do_round_trip(St) ->
-    Size = crypto:rand_uniform(St#st.minsize,  St#st.maxsize + 1),
+    Size = St#st.minsize + b64url_rand:uniform(St#st.maxsize - St#st.minsize),
     Data = crypto:strong_rand_bytes(Size),
     Encoded = (St#st.module):encode(Data),
     Data = (St#st.module):decode(Encoded),
@@ -137,7 +137,7 @@ decode(Url64) ->
     base64:decode(<<Url2/binary, Padding/binary>>).
 
 randomize(List) ->
-    List0 = [{crypto:rand_uniform(0, 1 bsl 32), L} || L <- List],
+    List0 = [{b64url_rand:uniform(), L} || L <- List],
     List1 = lists:sort(List0),
     [L || {_, L} <- List1].
 


[couchdb-b64url] 11/31: Fix other compiler warning

Posted by ja...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jaydoane pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/couchdb-b64url.git

commit 504cbd010905c0fb9fe9bc133c81e2639f8ab6fe
Author: Paul J. Davis <pa...@gmail.com>
AuthorDate: Thu Nov 21 15:50:08 2013 -0600

    Fix other compiler warning
    
    Same issue, different function.
---
 c_src/b64url.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/c_src/b64url.c b/c_src/b64url.c
index f4067fb..5e48957 100644
--- a/c_src/b64url.c
+++ b/c_src/b64url.c
@@ -494,7 +494,7 @@ b64url_decode_init(ErlNifEnv* env, int argc, const ENTERM argv[])
     b64url_st* st = NULL;
     size_t tlen;
     int status;
-    ENTERM ret;
+    ENTERM ret = priv->atom_error;
 
     if(argc != 1) {
         return enif_make_badarg(env);


[couchdb-b64url] 20/31: Support Windows build target

Posted by ja...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jaydoane pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/couchdb-b64url.git

commit 3df89ea8c474d0adda833422c1c72fbea9fa98b9
Author: Joan Touzet <wo...@apache.org>
AuthorDate: Sat Jul 11 19:24:23 2015 -0400

    Support Windows build target
---
 .gitignore     |  4 +++-
 c_src/b64url.c | 24 +++++++++++++++---------
 rebar.config   |  4 +++-
 3 files changed, 21 insertions(+), 11 deletions(-)

diff --git a/.gitignore b/.gitignore
index d1356fa..24f9e66 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,6 @@
 .eunit/
 c_src/*.o
 ebin/
-priv/*.so
+priv/
+.rebar/
+vc110.pdb
diff --git a/c_src/b64url.c b/c_src/b64url.c
index 074d9a0..d9e2e04 100644
--- a/c_src/b64url.c
+++ b/c_src/b64url.c
@@ -15,6 +15,12 @@
 
 #include "erl_nif.h"
 
+#ifdef _WIN32
+#define INLINE __inline
+#else
+#define INLINE inline
+#endif
+
 
 typedef ERL_NIF_TERM ENTERM;
 
@@ -93,7 +99,7 @@ const unsigned char B64URL_A2B[256] = {
 
 #define BYTES_PER_PERCENT 64
 
-static inline int
+static INLINE int
 do_consume_timeslice(ErlNifEnv* env) {
 #if(ERL_NIF_MAJOR_VERSION >= 2 && ERL_NIF_MINOR_VERSION >= 4)
     return enif_consume_timeslice(env, 1);
@@ -103,7 +109,7 @@ do_consume_timeslice(ErlNifEnv* env) {
 }
 
 
-static inline ENTERM
+static INLINE ENTERM
 make_atom(ErlNifEnv* env, const char* name)
 {
     ENTERM ret;
@@ -114,21 +120,21 @@ make_atom(ErlNifEnv* env, const char* name)
 }
 
 
-static inline ENTERM
+static INLINE ENTERM
 make_ok(ErlNifEnv* env, b64url_priv* priv, ENTERM value)
 {
     return enif_make_tuple2(env, priv->atom_ok, value);
 }
 
 
-static inline ENTERM
+static INLINE ENTERM
 make_error(ErlNifEnv* env, b64url_priv* priv, ENTERM value)
 {
     return enif_make_tuple2(env, priv->atom_error, value);
 }
 
 
-static inline ENTERM
+static INLINE ENTERM
 make_bad_block(ErlNifEnv* env, b64url_priv* priv, size_t pos)
 {
     ENTERM pterm = enif_make_uint64(env, pos);
@@ -136,14 +142,14 @@ make_bad_block(ErlNifEnv* env, b64url_priv* priv, size_t pos)
 }
 
 
-static inline ENTERM
+static INLINE ENTERM
 make_partial(ErlNifEnv* env, b64url_priv* priv, ENTERM value)
 {
     return enif_make_tuple2(env, priv->atom_partial, value);
 }
 
 
-static inline int
+static INLINE int
 check_pid(ErlNifEnv* env, b64url_st* st)
 {
     ErlNifPid self_pid;
@@ -304,7 +310,7 @@ unload(ErlNifEnv* env, void* priv)
 }
 
 
-static inline b64url_status
+static INLINE b64url_status
 b64url_encode(ErlNifEnv* env, ErlNifBinary* src, b64url_st* st)
 {
     size_t chunk_start = st->si;
@@ -442,7 +448,7 @@ b64url_encode_cont(ErlNifEnv* env, int argc, const ENTERM argv[])
 }
 
 
-static inline b64url_status
+static INLINE b64url_status
 b64url_decode(ErlNifEnv* env, ErlNifBinary* src, b64url_st* st, ENTERM* ret)
 {
     b64url_priv* priv = (b64url_priv*) enif_priv_data(env);
diff --git a/rebar.config b/rebar.config
index 534c049..4732af8 100644
--- a/rebar.config
+++ b/rebar.config
@@ -9,5 +9,7 @@
     % {".*", "CFLAGS", "$CFLAGS -g -Wall -Werror -fPIC"}
 
     % Production compilation
-    {".*", "CFLAGS", "$CFLAGS -Wall -Werror -DNDEBUG -O3"}
+    {"(linux|solaris|darwin|freebsd)", "CFLAGS", "$CFLAGS -Wall -Werror -DNDEBUG -O3"},
+    {"win32", "CFLAGS", "$CFLAGS /O2 /DNDEBUG /Wall"}
+
 ]}.


[couchdb-b64url] 08/31: Attempting to avoid a compiler warning on Jenkins

Posted by ja...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jaydoane pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/couchdb-b64url.git

commit edaf27bc96c9384558df4742e77a8bf85d989eda
Author: Paul J. Davis <pa...@gmail.com>
AuthorDate: Thu Nov 21 15:29:13 2013 -0600

    Attempting to avoid a compiler warning on Jenkins
---
 c_src/b64url.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/c_src/b64url.c b/c_src/b64url.c
index 0bc6ab8..3528d3b 100644
--- a/c_src/b64url.c
+++ b/c_src/b64url.c
@@ -546,28 +546,32 @@ b64url_decode_cont(ErlNifEnv* env, int argc, const ENTERM argv[])
     int status;
 
     if(argc != 2) {
-        return enif_make_badarg(env);
+        goto badarg;
     }
 
     if(!enif_inspect_iolist_as_binary(env, argv[0], &src)) {
-        return enif_make_badarg(env);
+        goto badarg;
     }
 
     if(!enif_get_resource(env, argv[1], priv->res_st, (void**) &st)) {
-        return enif_make_badarg(env);
+        goto badarg;
     }
 
     if(!check_pid(env, st)) {
-        return enif_make_badarg(env);
+        goto badarg;
     }
 
     if(src.size != st->len) {
-        return enif_make_badarg(env);
+        goto badarg;
     }
 
     status = b64url_decode(env, &src, st, &ret);
 
     return b64url_st_dec_ret(env, st, status, ret);
+
+badarg:
+    ret = enif_make_badarg(env);
+    return ret;
 }
 
 


[couchdb-b64url] 13/31: Fixed enif_release_resource bug

Posted by ja...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jaydoane pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/couchdb-b64url.git

commit 7c6324232c4728c7290ae6ec73bab67ff56912ef
Author: Paul J. Davis <pa...@gmail.com>
AuthorDate: Wed Dec 4 13:57:56 2013 -0600

    Fixed enif_release_resource bug
    
    I was accidentally calling enif_release_resource too many times if the
    NIF call had previously yielded back to the Erlang VM. For some reason
    this works fine on R14B01 but not on R16B02. The fix was simply to add a
    field to the resource struct that tracks if its already been released.
    
    This also changes the target binary allocation to not have to allocate
    room for the ErlNifBinary struct since we're adding released flags. This
    is a minor performance optimization which avoids a malloc/free call per
    encode or decode invocation.
---
 c_src/b64url.c | 88 +++++++++++++++++++++++++++++++++-------------------------
 1 file changed, 50 insertions(+), 38 deletions(-)

diff --git a/c_src/b64url.c b/c_src/b64url.c
index 5e48957..604f21a 100644
--- a/c_src/b64url.c
+++ b/c_src/b64url.c
@@ -24,10 +24,12 @@ typedef struct
 typedef struct
 {
     ErlNifPid     pid;
-    ErlNifBinary* tgt;
+    ErlNifBinary  tgt;
     size_t        len;
     size_t        si;
     size_t        ti;
+    char          res_released;
+    char          bin_released;
 } b64url_st;
 
 
@@ -162,12 +164,10 @@ b64url_st_alloc(ErlNifEnv* env, b64url_priv* priv, ErlNifBinary* src, size_t tle
     st->len = src->size;
     st->si = 0;
     st->ti = 0;
-    st->tgt = (ErlNifBinary*) enif_alloc(sizeof(ErlNifBinary));
-    if(st->tgt == NULL) {
-        goto error;
-    }
+    st->res_released = 0;
+    st->bin_released = 0;
 
-    if(!enif_alloc_binary(tlen, st->tgt)) {
+    if(!enif_alloc_binary(tlen, &(st->tgt))) {
         goto error;
     }
 
@@ -175,6 +175,7 @@ b64url_st_alloc(ErlNifEnv* env, b64url_priv* priv, ErlNifBinary* src, size_t tle
 
 error:
     if(st != NULL) {
+        st->res_released = 1;
         enif_release_resource(st);
     }
     return NULL;
@@ -186,9 +187,8 @@ b64url_st_free(ErlNifEnv* env, void* obj)
 {
     b64url_st* st = (b64url_st*) obj;
 
-    if(st->tgt != NULL) {
-        enif_release_binary(st->tgt);
-        enif_free(st->tgt);
+    if(!st->bin_released) {
+        enif_release_binary(&(st->tgt));
     }
 }
 
@@ -199,9 +199,8 @@ b64url_st_enc_ret(ErlNifEnv* env, b64url_st* st, int status)
     ENTERM ret;
 
     if(status == ST_OK) {
-        ret = make_ok(env, priv, enif_make_binary(env, st->tgt));
-        enif_free(st->tgt);
-        st->tgt = NULL;
+        ret = make_ok(env, priv, enif_make_binary(env, &(st->tgt)));
+        st->bin_released = 1;
     } else if(status == ST_PARTIAL) {
         ret = make_partial(env, priv, enif_make_resource(env, st));
     } else {
@@ -209,7 +208,11 @@ b64url_st_enc_ret(ErlNifEnv* env, b64url_st* st, int status)
         ret = enif_make_badarg(env);
     }
 
-    enif_release_resource(st);
+    if(!st->res_released) {
+        st->res_released = 1;
+        enif_release_resource(st);
+    }
+
     return ret;
 }
 
@@ -219,9 +222,8 @@ b64url_st_dec_ret(ErlNifEnv* env, b64url_st* st, int status, ENTERM ret)
     b64url_priv* priv = (b64url_priv*) enif_priv_data(env);
 
     if(status == ST_OK) {
-        ret = make_ok(env, priv, enif_make_binary(env, st->tgt));
-        enif_free(st->tgt);
-        st->tgt = NULL;
+        ret = make_ok(env, priv, enif_make_binary(env, &(st->tgt)));
+        st->bin_released = 1;
     } else if(status == ST_ERROR) {
         ret = make_error(env, priv, ret);
     } else if(status == ST_PARTIAL) {
@@ -231,7 +233,11 @@ b64url_st_dec_ret(ErlNifEnv* env, b64url_st* st, int status, ENTERM ret)
         ret = enif_make_badarg(env);
     }
 
-    enif_release_resource(st);
+    if(!st->res_released) {
+        st->res_released = 1;
+        enif_release_resource(st);
+    }
+
     return ret;
 }
 
@@ -291,6 +297,7 @@ static inline b64url_status
 b64url_encode(ErlNifEnv* env, ErlNifBinary* src, b64url_st* st)
 {
     size_t chunk_start = st->si;
+    size_t rem;
     unsigned char c1;
     unsigned char c2;
     unsigned char c3;
@@ -303,10 +310,10 @@ b64url_encode(ErlNifEnv* env, ErlNifBinary* src, b64url_st* st)
         c2 = src->data[st->si++];
         c3 = src->data[st->si++];
 
-        st->tgt->data[st->ti++] = B64URL_B2A[(c1 >> 2) & 0x3F];
-        st->tgt->data[st->ti++] = B64URL_B2A[((c1 << 4) | (c2 >> 4)) & 0x3F];
-        st->tgt->data[st->ti++] = B64URL_B2A[((c2 << 2) | (c3 >> 6)) & 0x3F];
-        st->tgt->data[st->ti++] = B64URL_B2A[c3 & 0x3F];
+        st->tgt.data[st->ti++] = B64URL_B2A[(c1 >> 2) & 0x3F];
+        st->tgt.data[st->ti++] = B64URL_B2A[((c1 << 4) | (c2 >> 4)) & 0x3F];
+        st->tgt.data[st->ti++] = B64URL_B2A[((c2 << 2) | (c3 >> 6)) & 0x3F];
+        st->tgt.data[st->ti++] = B64URL_B2A[c3 & 0x3F];
 
         if(st->si - chunk_start > BYTES_PER_PERCENT) {
             if(do_consume_timeslice(env)) {
@@ -317,17 +324,18 @@ b64url_encode(ErlNifEnv* env, ErlNifBinary* src, b64url_st* st)
         }
     }
 
-    if(src->size % 3 == 1) {
+    rem = src->size % 3;
+    if(rem == 1) {
         c1 = src->data[st->si];
-        st->tgt->data[st->ti++] = B64URL_B2A[(c1 >> 2) & 0x3F];
-        st->tgt->data[st->ti++] = B64URL_B2A[(c1 << 4) & 0x3F];
-    } else if(src->size % 3 == 2) {
+        st->tgt.data[st->ti++] = B64URL_B2A[(c1 >> 2) & 0x3F];
+        st->tgt.data[st->ti++] = B64URL_B2A[(c1 << 4) & 0x3F];
+    } else if(rem == 2) {
         c1 = src->data[st->si];
         c2 = src->data[st->si+1];
-        st->tgt->data[st->ti++] = B64URL_B2A[(c1 >> 2) & 0x3F];
-        st->tgt->data[st->ti++] = B64URL_B2A[((c1 << 4) | (c2 >> 4)) & 0x3F];
-        st->tgt->data[st->ti++] = B64URL_B2A[(c2 << 2) & 0x3F];
-    } else {
+        st->tgt.data[st->ti++] = B64URL_B2A[(c1 >> 2) & 0x3F];
+        st->tgt.data[st->ti++] = B64URL_B2A[((c1 << 4) | (c2 >> 4)) & 0x3F];
+        st->tgt.data[st->ti++] = B64URL_B2A[(c2 << 2) & 0x3F];
+    } else if(rem != 0) {
         assert(0 == 1 && "Inavlid length calculation");
     }
 
@@ -378,6 +386,7 @@ b64url_encode_init(ErlNifEnv* env, int argc, const ENTERM argv[])
 
 error:
     if(st != NULL) {
+        st->res_released = 1;
         enif_release_resource(st);
     }
 
@@ -424,6 +433,7 @@ b64url_decode(ErlNifEnv* env, ErlNifBinary* src, b64url_st* st, ENTERM* ret)
 {
     b64url_priv* priv = (b64url_priv*) enif_priv_data(env);
     size_t chunk_start = st->si;
+    size_t rem;
     unsigned char c1;
     unsigned char c2;
     unsigned char c3;
@@ -443,9 +453,9 @@ b64url_decode(ErlNifEnv* env, ErlNifBinary* src, b64url_st* st, ENTERM* ret)
             return ST_ERROR;
         }
 
-        st->tgt->data[st->ti++] = (c1 << 2) | (c2 >> 4);
-        st->tgt->data[st->ti++] = (c2 << 4) | (c3 >> 2);
-        st->tgt->data[st->ti++] = (c3 << 6) | c4;
+        st->tgt.data[st->ti++] = (c1 << 2) | (c2 >> 4);
+        st->tgt.data[st->ti++] = (c2 << 4) | (c3 >> 2);
+        st->tgt.data[st->ti++] = (c3 << 6) | c4;
 
         if(st->si - chunk_start > BYTES_PER_PERCENT) {
             if(do_consume_timeslice(env)) {
@@ -456,7 +466,8 @@ b64url_decode(ErlNifEnv* env, ErlNifBinary* src, b64url_st* st, ENTERM* ret)
         }
     }
 
-    if(src->size % 4 == 2) {
+    rem = src->size % 4;
+    if(rem == 2) {
         c1 = B64URL_A2B[src->data[st->si]];
         c2 = B64URL_A2B[src->data[st->si+1]];
 
@@ -465,8 +476,8 @@ b64url_decode(ErlNifEnv* env, ErlNifBinary* src, b64url_st* st, ENTERM* ret)
             return ST_ERROR;
         }
 
-        st->tgt->data[st->ti++] = (c1 << 2) | (c2 >> 4);
-    } else if(src->size % 4 == 3) {
+        st->tgt.data[st->ti++] = (c1 << 2) | (c2 >> 4);
+    } else if(rem == 3) {
         c1 = B64URL_A2B[src->data[st->si]];
         c2 = B64URL_A2B[src->data[st->si+1]];
         c3 = B64URL_A2B[src->data[st->si+2]];
@@ -476,9 +487,9 @@ b64url_decode(ErlNifEnv* env, ErlNifBinary* src, b64url_st* st, ENTERM* ret)
             return ST_ERROR;
         }
 
-        st->tgt->data[st->ti++] = (c1 << 2) | (c2 >> 4);
-        st->tgt->data[st->ti++] = (c2 << 4) | (c3 >> 2);
-    } else {
+        st->tgt.data[st->ti++] = (c1 << 2) | (c2 >> 4);
+        st->tgt.data[st->ti++] = (c2 << 4) | (c3 >> 2);
+    } else if(rem != 0) {
         assert(0 == 1 && "invalid source length");
     }
 
@@ -529,6 +540,7 @@ b64url_decode_init(ErlNifEnv* env, int argc, const ENTERM argv[])
 
 error:
     if(st != NULL) {
+        st->res_released = 1;
         enif_release_resource(st);
     }
 


[couchdb-b64url] 18/31: Add README file

Posted by ja...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jaydoane pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/couchdb-b64url.git

commit d9c132068e0150295a9e024c42f6ee414b9aa0c7
Author: Alexander Shorin <kx...@apache.org>
AuthorDate: Fri Jan 9 18:48:42 2015 +0300

    Add README file
    
    COUCHDB-2539
---
 README.rst | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/README.rst b/README.rst
new file mode 100644
index 0000000..38484ae
--- /dev/null
+++ b/README.rst
@@ -0,0 +1,16 @@
+==============
+couchdb-b64url
+==============
+
+This is a pretty simple NIF that is just responsible for encoding and decoding
+Base46 URL values::
+
+    1> Thing = b64url:encode("Hello, CouchDB!").
+    <<"SGVsbG8sIENvdWNoREIh">>
+    2> b64url:decode(Thing).
+    <<"Hello, CouchDB!">>
+
+License
+=======
+
+Apache 2.0


[couchdb-b64url] 02/31: Encoder is passing tests.

Posted by ja...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jaydoane pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/couchdb-b64url.git

commit 795d11cb31e72992432bf2edb381a18886c57f43
Author: Paul J. Davis <pa...@gmail.com>
AuthorDate: Fri Nov 1 11:24:04 2013 -0500

    Encoder is passing tests.
---
 c_src/couch_seqs_b64.c           |  8 ++------
 rebar.config                     |  3 +++
 test/couch_seqs_b64url_tests.erl | 42 ++++++++++++++++++++++++++++++++++------
 3 files changed, 41 insertions(+), 12 deletions(-)

diff --git a/c_src/couch_seqs_b64.c b/c_src/couch_seqs_b64.c
index 8477508..a48d928 100644
--- a/c_src/couch_seqs_b64.c
+++ b/c_src/couch_seqs_b64.c
@@ -233,11 +233,11 @@ cseq_b64url_encode(ErlNifEnv* env, ErlNifBinary* src, cseq_st* st)
         }
     }
 
-    if(src->size - st->si == 2) {
+    if(src->size % 3 == 1) {
         c1 = src->data[st->si];
         st->tgt->data[st->ti++] = B64URL_B2A[(c1 >> 2) & 0x3F];
         st->tgt->data[st->ti++] = B64URL_B2A[(c1 << 4) & 0x3F];
-    } else if(src->size - st->si == 1) {
+    } else if(src->size % 3 == 2) {
         c1 = src->data[st->si];
         c2 = src->data[st->si+1];
         st->tgt->data[st->ti++] = B64URL_B2A[(c1 >> 2) & 0x3F];
@@ -270,10 +270,6 @@ cseq_b64url_encode_init(ErlNifEnv* env, int argc, const ENTERM argv[])
         return enif_make_badarg(env);
     }
 
-    if(src.size <= 0) {
-        return enif_make_badarg(env);
-    }
-
     st = (cseq_st*) enif_alloc_resource(priv->res_st, sizeof(cseq_st));
     if(st == NULL) {
         ret = make_error(env, priv, priv->atom_nomem);
diff --git a/rebar.config b/rebar.config
index 6f2ca46..5a9da0f 100644
--- a/rebar.config
+++ b/rebar.config
@@ -10,3 +10,6 @@
     {".*", "CFLAGS", "$CFLAGS -Wall -Werror -DNDEBUG -O3"}
 ]}.
 
+{deps, [
+    {proper, ".*", {git, "https://github.com/manopapad/proper.git", "master"}}
+]}.
diff --git a/test/couch_seqs_b64url_tests.erl b/test/couch_seqs_b64url_tests.erl
index 0e1089a..9fabe3e 100644
--- a/test/couch_seqs_b64url_tests.erl
+++ b/test/couch_seqs_b64url_tests.erl
@@ -8,28 +8,58 @@
 proper_test_() ->
     PropErOpts = [
         {to_file, user},
-        {max_size, 524288},
+        {max_size, 6401},
         {numtests, 1000}
     ],
     {timeout, 3600, ?_assertEqual([], proper:module(?MODULE, PropErOpts))}.
 
 
 prop_encode_binary() ->
-    ?FORALL(Bin, binary(),
-        couch_seqs_b64url:encode(Bin) == couch_encode_base64url(Bin)
-    ).
+    ?FORALL(Bin, binary(), begin
+        A = couch_encode_base64url(Bin),
+        B = couch_seqs_b64url:encode(Bin),
+        A == B
+    end).
 
 
-couch_encode_base64url(Data) ->
+prop_encode_iolist() ->
+    ?FORALL(Bin, binary(), begin
+        A = couch_encode_base64url(Bin),
+        B = couch_seqs_b64url:encode(to_iolist(Bin)),
+        A == B
+    end).
+
+
+couch_encode_base64url(Url) ->
     Url1 = iolist_to_binary(re:replace(base64:encode(Url), "=+$", "")),
     Url2 = iolist_to_binary(re:replace(Url1, "/", "_", [global])),
     iolist_to_binary(re:replace(Url2, "\\+", "-", [global])).
 
 
-couch_decode_base64url(Data) ->
+couch_decode_base64url(Url64) ->
     Url1 = re:replace(iolist_to_binary(Url64), "-", "+", [global]),
     Url2 = iolist_to_binary(
         re:replace(iolist_to_binary(Url1), "_", "/", [global])
     ),
     Padding = list_to_binary(lists:duplicate((4 - size(Url2) rem 4) rem 4, $=)),
     base64:decode(<<Url2/binary, Padding/binary>>).
+
+
+to_iolist(<<>>) ->
+    case random:uniform(2) of
+        1 -> <<>>;
+        2 -> [<<>>]
+    end;
+to_iolist(B) when is_binary(B), size(B) > 0 ->
+    S = random:uniform(size(B)),
+    <<First:S/binary, Second/binary>> = B,
+    case random:uniform(3) of
+        1 ->
+            [to_iolist(First), Second];
+        2 ->
+            [First, to_iolist(Second)];
+        3 ->
+            [First, Second]
+    end.
+
+


[couchdb-b64url] 24/31: Update .travis.yml

Posted by ja...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jaydoane pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/couchdb-b64url.git

commit 6895652d80f95cdf04efb14625abed868998f174
Author: Alexander Shorin <kx...@apache.org>
AuthorDate: Fri Jan 22 12:59:40 2016 +0300

    Update .travis.yml
    
    - Add license header
    - Remove builds against old Erlang releases
---
 .travis.yml | 27 ++++++++++++++++++++-------
 1 file changed, 20 insertions(+), 7 deletions(-)

diff --git a/.travis.yml b/.travis.yml
index 99e6cb3..e9367c6 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,10 +1,23 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You 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.
+
 language: erlang
 
 otp_release:
-   - 17.5
-   - 17.4
-   - 17.1
-   - 17.0
-   - R16B03-1
-   - R14B04
-   - R14B02
+  - 18.2
+  - 18.1
+  - 18.0
+  - 17.5
+  - R16B03-1


[couchdb-b64url] 06/31: Comment on the copied function definitions

Posted by ja...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jaydoane pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/couchdb-b64url.git

commit fc4df0d693545966e56c118380d7c76fde595d0d
Author: Paul J. Davis <pa...@gmail.com>
AuthorDate: Thu Nov 21 13:40:40 2013 -0600

    Comment on the copied function definitions
---
 test/b64url_tests.erl | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/test/b64url_tests.erl b/test/b64url_tests.erl
index 5b01606..cac29f7 100644
--- a/test/b64url_tests.erl
+++ b/test/b64url_tests.erl
@@ -124,6 +124,12 @@ make_bad_len(Bin) when size(Bin) rem 4 == 0 ->
     <<"A", Bin/binary>>.
 
 
+% These functions are copy/pasted from couch_util to avoid
+% the direct dependency. The goal of this project is to replace
+% these in couch_util anyway so when that happens they'll only
+% exist here for these tests.
+
+
 couch_encode_base64url(Url) ->
     Url1 = iolist_to_binary(re:replace(base64:encode(Url), "=+$", "")),
     Url2 = iolist_to_binary(re:replace(Url1, "/", "_", [global])),


[couchdb-b64url] 21/31: Merge branch 'windows_support'

Posted by ja...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jaydoane pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/couchdb-b64url.git

commit 4ee0dcc0bc68dddbd6394f133f64fc57be2d2ad4
Merge: 30a50f2 3df89ea
Author: Joan Touzet <wo...@apache.org>
AuthorDate: Fri Jul 17 00:14:14 2015 -0400

    Merge branch 'windows_support'

 .gitignore     |  4 +++-
 c_src/b64url.c | 24 +++++++++++++++---------
 rebar.config   |  4 +++-
 3 files changed, 21 insertions(+), 11 deletions(-)


[couchdb-b64url] 04/31: Whitespace

Posted by ja...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jaydoane pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/couchdb-b64url.git

commit 2b6d2a26b80dda7e7c4c9976cb90189a9a38aed8
Author: Paul J. Davis <pa...@gmail.com>
AuthorDate: Fri Nov 1 17:05:27 2013 -0500

    Whitespace
---
 test/couch_seqs_b64url_tests.erl | 1 +
 1 file changed, 1 insertion(+)

diff --git a/test/couch_seqs_b64url_tests.erl b/test/couch_seqs_b64url_tests.erl
index 3c27a18..aea784f 100644
--- a/test/couch_seqs_b64url_tests.erl
+++ b/test/couch_seqs_b64url_tests.erl
@@ -123,6 +123,7 @@ make_bad_len(Bin) when size(Bin) rem 4 == 3 ->
 make_bad_len(Bin) when size(Bin) rem 4 == 0 ->
     <<"A", Bin/binary>>.
 
+
 couch_encode_base64url(Url) ->
     Url1 = iolist_to_binary(re:replace(base64:encode(Url), "=+$", "")),
     Url2 = iolist_to_binary(re:replace(Url1, "/", "_", [global])),


[couchdb-b64url] 29/31: Change minimum supported Erlang version to OTP 19

Posted by ja...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jaydoane pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/couchdb-b64url.git

commit a48ee011c2212b82efab4ff15c55ca6ba58476da
Author: Jay Doane <ja...@apache.org>
AuthorDate: Fri Feb 22 09:40:15 2019 -0800

    Change minimum supported Erlang version to OTP 19
    
    Eliminate test/b64url_rand.erl in favor of direct rand:uniform calls
---
 .travis.yml            | 11 +++++-----
 rebar.config           | 11 ----------
 test/b64url_rand.erl   | 57 --------------------------------------------------
 test/b64url_tests.erl  | 12 +++++------
 test/benchmark.escript |  4 ++--
 5 files changed, 14 insertions(+), 81 deletions(-)

diff --git a/.travis.yml b/.travis.yml
index e9367c6..54b91fb 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -16,8 +16,9 @@
 language: erlang
 
 otp_release:
-  - 18.2
-  - 18.1
-  - 18.0
-  - 17.5
-  - R16B03-1
+  - 21.2
+  - 20.3
+  - 19.3
+
+script:
+  - make check
diff --git a/rebar.config b/rebar.config
index a84ea97..ba1d456 100644
--- a/rebar.config
+++ b/rebar.config
@@ -13,14 +13,3 @@
 ]}.
 
 {eunit_opts, [verbose]}.
-
-{erl_opts, [
-   {platform_define, "^R16", 'NORANDMODULE'},
-   {platform_define, "^17", 'NORANDMODULE'}
-]}.
-
-{eunit_compile_opts, [
-   {platform_define, "^R16", 'NORANDMODULE'},
-   {platform_define, "^17", 'NORANDMODULE'}
-]}.
-
diff --git a/test/b64url_rand.erl b/test/b64url_rand.erl
deleted file mode 100644
index a397c3b..0000000
--- a/test/b64url_rand.erl
+++ /dev/null
@@ -1,57 +0,0 @@
-% 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.
-
--module(b64url_rand).
-
-
--export([
-    uniform/0,
-    uniform/1
-]).
-
-
--ifdef(NORANDMODULE).
-
-
-uniform() ->
-    maybe_set_random_seed(),
-    random:uniform().
-
-
-uniform(N) ->
-    maybe_set_random_seed(),
-    random:uniform(N).
-
-
-maybe_set_random_seed() ->
-    case get(random_seed) of
-        undefined ->
-            {_, Sec, USec} = os:timestamp(),
-            Seed = {erlang:phash2(self()), Sec, USec},
-            random:seed(Seed);
-        _ ->
-            ok
-    end.
-
-
--else.
-
-
-uniform() ->
-    rand:uniform().
-
-
-uniform(N) ->
-    rand:uniform(N).
-
-
--endif.
diff --git a/test/b64url_tests.erl b/test/b64url_tests.erl
index c5831ef..27e69db 100644
--- a/test/b64url_tests.erl
+++ b/test/b64url_tests.erl
@@ -65,7 +65,7 @@ decode_bad_length_test() ->
 
 gen_binary() ->
     % -1 to allow for 0 length
-    Length = b64url_rand:uniform(?MAX_SIZE) - 1,
+    Length = rand:uniform(?MAX_SIZE) - 1,
     crypto:strong_rand_bytes(Length).
 
 
@@ -86,14 +86,14 @@ bad_len_binary() ->
 
 
 to_iolist(<<>>) ->
-    case b64url_rand:uniform(2) of
+    case rand:uniform(2) of
         1 -> <<>>;
         2 -> [<<>>]
     end;
 to_iolist(B) when is_binary(B), size(B) > 0 ->
-    S = b64url_rand:uniform(size(B)),
+    S = rand:uniform(size(B)),
     <<First:S/binary, Second/binary>> = B,
-    case b64url_rand:uniform(3) of
+    case rand:uniform(3) of
         1 ->
             [to_iolist(First), Second];
         2 ->
@@ -104,13 +104,13 @@ to_iolist(B) when is_binary(B), size(B) > 0 ->
 
 
 insert_error(B) when is_binary(B), size(B) < 2 ->
-    case b64url_rand:uniform(2) of
+    case rand:uniform(2) of
         1 -> {<<122, 255>>, 0};
         2 -> {<<122, 122, 255>>, 0}
     end;
 insert_error(B) when is_binary(B) ->
     B64 = couch_encode_base64url(B),
-    S = b64url_rand:uniform(size(B64) - 1),
+    S = rand:uniform(size(B64) - 1),
     <<First:S/binary, _:1/binary, Second/binary>> = B64,
     {<<First:S/binary, 255, Second/binary>>, 4 * (S div 4)}.
 
diff --git a/test/benchmark.escript b/test/benchmark.escript
index 75c2341..00a6f0d 100755
--- a/test/benchmark.escript
+++ b/test/benchmark.escript
@@ -115,7 +115,7 @@ run_worker(St, Started) ->
 
 
 do_round_trip(St) ->
-    Size = St#st.minsize + b64url_rand:uniform(St#st.maxsize - St#st.minsize),
+    Size = St#st.minsize + rand:uniform(St#st.maxsize - St#st.minsize),
     Data = crypto:strong_rand_bytes(Size),
     Encoded = (St#st.module):encode(Data),
     Data = (St#st.module):decode(Encoded),
@@ -137,7 +137,7 @@ decode(Url64) ->
     base64:decode(<<Url2/binary, Padding/binary>>).
 
 randomize(List) ->
-    List0 = [{b64url_rand:uniform(), L} || L <- List],
+    List0 = [{rand:uniform(), L} || L <- List],
     List1 = lists:sort(List0),
     [L || {_, L} <- List1].
 


[couchdb-b64url] 01/31: Initial commit

Posted by ja...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jaydoane pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/couchdb-b64url.git

commit 13e21f49d4dee19255b345bca5bfaab1f29b38db
Author: Paul J. Davis <pa...@gmail.com>
AuthorDate: Thu Oct 31 17:59:48 2013 -0500

    Initial commit
---
 .gitignore                       |   4 +
 c_src/couch_seqs_b64.c           | 409 +++++++++++++++++++++++++++++++++++++++
 rebar.config                     |  12 ++
 src/couch_seqs.app.src           |   6 +
 src/couch_seqs_b64url.erl        |  72 +++++++
 test/couch_seqs_b64url_tests.erl |  35 ++++
 6 files changed, 538 insertions(+)

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..d1356fa
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,4 @@
+.eunit/
+c_src/*.o
+ebin/
+priv/*.so
diff --git a/c_src/couch_seqs_b64.c b/c_src/couch_seqs_b64.c
new file mode 100644
index 0000000..8477508
--- /dev/null
+++ b/c_src/couch_seqs_b64.c
@@ -0,0 +1,409 @@
+
+#include <assert.h>
+#include <string.h>
+
+#include "erl_nif.h"
+
+
+typedef ERL_NIF_TERM ENTERM;
+
+
+typedef struct
+{
+    ENTERM atom_ok;
+    ENTERM atom_error;
+    ENTERM atom_partial;
+
+    ENTERM atom_nomem;
+
+    ErlNifResourceType* res_st;
+} cseq_priv;
+
+
+typedef struct
+{
+    ErlNifPid     pid;
+    ErlNifBinary* tgt;
+    size_t        len;
+    size_t        si;
+    size_t        ti;
+} cseq_st;
+
+
+typedef enum
+{
+    ST_OK,
+    ST_ERROR,
+    ST_PARTIAL
+} cseq_status;
+
+
+const char B64URL_B2A[256] = {
+   65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, //   0 -  15
+   81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 97, 98, 99,100,101,102, //  16 -  31
+  103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118, //  32 -  47
+  119,120,121,122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 45, 95, //  48 -  63
+  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, //  64 -  79
+  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, //  80 -  95
+  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, //  96 - 111
+  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // 112 - 127
+  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // 128 - 143
+  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // 144 - 159
+  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // 160 - 175
+  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // 176 - 191
+  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // 192 - 207
+  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // 208 - 223
+  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // 224 - 239
+  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255  // 240 - 255
+};
+
+const char B64URL_A2B[256] = {
+  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, //   0 -  15
+  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, //  16 -  31
+  255,255,255,255,255,255,255,255,255,255,255,255,255, 62,255,255, //  32 -  47
+   52, 53, 54, 55, 56, 57, 58, 59, 60, 61,255,255,255,255,255,255, //  48 -  63
+  255,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, //  64 -  79
+   15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,255,255,255,255, 63, //  80 -  95
+  255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, //  96 - 111
+   41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,255,255,255,255,255, // 112 - 127
+  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // 128 - 143
+  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // 144 - 159
+  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // 160 - 175
+  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // 176 - 191
+  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // 192 - 207
+  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // 208 - 223
+  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // 224 - 239
+  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255  // 240 - 255
+};
+
+
+#define BYTES_PER_PERCENT 64
+
+static inline int
+do_consume_timeslice(ErlNifEnv* env) {
+#if(ERL_NIF_MAJOR_VERSION >= 2 && ERL_NIF_MINOR_VERSION >= 4)
+    return enif_consume_timeslice(env, 1);
+#else
+    return 0;
+#endif
+}
+
+
+static inline ENTERM
+make_atom(ErlNifEnv* env, const char* name)
+{
+    ENTERM ret;
+    if(enif_make_existing_atom(env, name, &ret, ERL_NIF_LATIN1)) {
+        return ret;
+    }
+    return enif_make_atom(env, name);
+}
+
+
+static inline ENTERM
+make_ok(ErlNifEnv* env, cseq_priv* priv, ENTERM value)
+{
+    return enif_make_tuple2(env, priv->atom_ok, value);
+}
+
+
+static inline ENTERM
+make_error(ErlNifEnv* env, cseq_priv* priv, ENTERM value)
+{
+    return enif_make_tuple2(env, priv->atom_error, value);
+}
+
+
+static inline ENTERM
+make_partial(ErlNifEnv* env, cseq_priv* priv, ENTERM value)
+{
+    return enif_make_tuple2(env, priv->atom_partial, value);
+}
+
+
+static inline int
+check_pid(ErlNifEnv* env, cseq_st* st)
+{
+    ErlNifPid self_pid;
+    ENTERM self;
+    ENTERM orig;
+
+    enif_self(env, &self_pid);
+    self = enif_make_pid(env, &self_pid);
+    orig = enif_make_pid(env, &(st->pid));
+
+    if(enif_compare(self, orig) == 0) {
+        return 1;
+    }
+
+    return 0;
+}
+
+
+static void
+cseq_free(ErlNifEnv* env, void* obj)
+{
+    cseq_st* st = (cseq_st*) obj;
+
+    if(st->tgt != NULL) {
+        enif_release_binary(st->tgt);
+        enif_free(st->tgt);
+    }
+}
+
+
+static int
+load(ErlNifEnv* env, void** priv, ENTERM info)
+{
+    int flags = ERL_NIF_RT_CREATE | ERL_NIF_RT_TAKEOVER;
+    ErlNifResourceType* res;
+
+    cseq_priv* new_priv = (cseq_priv*) enif_alloc(sizeof(cseq_priv));
+    if(new_priv == NULL) {
+        return 1;
+    }
+
+    res = enif_open_resource_type(
+            env, NULL, "couch_seq", cseq_free, flags, NULL);
+    if(res == NULL) {
+        return 1;
+    }
+    new_priv->res_st = res;
+
+    new_priv->atom_ok = make_atom(env, "ok");
+    new_priv->atom_error = make_atom(env, "error");
+    new_priv->atom_partial = make_atom(env, "partial");
+
+    new_priv->atom_nomem = make_atom(env, "enomem");
+
+    *priv = (void*) new_priv;
+
+    return 0;
+}
+
+
+static int
+reload(ErlNifEnv* env, void** priv, ENTERM info)
+{
+    return 0;
+}
+
+
+static int
+upgrade(ErlNifEnv* env, void** priv, void** old_priv, ENTERM info)
+{
+    return 0;
+}
+
+
+static void
+unload(ErlNifEnv* env, void* priv)
+{
+    return;
+}
+
+
+static inline cseq_status
+cseq_b64url_encode(ErlNifEnv* env, ErlNifBinary* src, cseq_st* st)
+{
+    size_t chunk_start = st->si;
+    unsigned char c1;
+    unsigned char c2;
+    unsigned char c3;
+
+    assert(st->si % 3 == 0 && "invalid source index");
+    assert(st->ti % 4 == 0 && "invalid target index");
+
+    while(st->si + 3 <= src->size) {
+        c1 = src->data[st->si++];
+        c2 = src->data[st->si++];
+        c3 = src->data[st->si++];
+
+        st->tgt->data[st->ti++] = B64URL_B2A[(c1 >> 2) & 0x3F];
+        st->tgt->data[st->ti++] = B64URL_B2A[((c1 << 4) | (c2 >> 4)) & 0x3F];
+        st->tgt->data[st->ti++] = B64URL_B2A[((c2 << 2) | (c3 >> 6)) & 0x3F];
+        st->tgt->data[st->ti++] = B64URL_B2A[c3 & 0x3F];
+
+        if(st->si - chunk_start > BYTES_PER_PERCENT) {
+            if(do_consume_timeslice(env)) {
+                return ST_PARTIAL;
+            } else {
+                chunk_start = st->si;
+            }
+        }
+    }
+
+    if(src->size - st->si == 2) {
+        c1 = src->data[st->si];
+        st->tgt->data[st->ti++] = B64URL_B2A[(c1 >> 2) & 0x3F];
+        st->tgt->data[st->ti++] = B64URL_B2A[(c1 << 4) & 0x3F];
+    } else if(src->size - st->si == 1) {
+        c1 = src->data[st->si];
+        c2 = src->data[st->si+1];
+        st->tgt->data[st->ti++] = B64URL_B2A[(c1 >> 2) & 0x3F];
+        st->tgt->data[st->ti++] = B64URL_B2A[((c1 << 4) | (c2 >> 4)) & 0x3F];
+        st->tgt->data[st->ti++] = B64URL_B2A[(c2 << 2) & 0x3F];
+    } else {
+        assert(0 == 1 && "Inavlid length calculation");
+    }
+
+    return ST_OK;
+}
+
+
+static ENTERM
+cseq_b64url_encode_init(ErlNifEnv* env, int argc, const ENTERM argv[])
+{
+    ErlNifBinary src;
+    cseq_priv* priv = (cseq_priv*) enif_priv_data(env);
+    cseq_st* st = NULL;
+    size_t tlen;
+    size_t rem;
+    int status;
+    ENTERM ret;
+
+    if(argc != 1) {
+        return enif_make_badarg(env);
+    }
+
+    if(!enif_inspect_iolist_as_binary(env, argv[0], &src)) {
+        return enif_make_badarg(env);
+    }
+
+    if(src.size <= 0) {
+        return enif_make_badarg(env);
+    }
+
+    st = (cseq_st*) enif_alloc_resource(priv->res_st, sizeof(cseq_st));
+    if(st == NULL) {
+        ret = make_error(env, priv, priv->atom_nomem);
+        goto error;
+    }
+
+    memset(st, '\0', sizeof(cseq_st));
+    enif_self(env, &(st->pid));
+    st->len = src.size;
+    st->si = 0;
+    st->ti = 0;
+    st->tgt = (ErlNifBinary*) enif_alloc(sizeof(ErlNifBinary));
+    if(st->tgt == NULL) {
+        ret = make_error(env, priv, priv->atom_nomem);
+        goto error;
+    }
+
+    // The target length is defined as 4 * ceil(src_len/3) but we
+    // don't use '=' padding for URLs so we only add exactly the
+    // extra bytes we need.
+    tlen = (src.size / 3) * 4;
+    rem = src.size % 3;
+    if(rem == 1) {
+        tlen += 2;
+    } else if(rem == 2) {
+        tlen += 3;
+    }
+
+    if(!enif_alloc_binary(tlen, st->tgt)) {
+        ret = make_error(env, priv, priv->atom_nomem);
+        goto error;
+    }
+
+    status = cseq_b64url_encode(env, &src, st);
+
+    if(status == ST_OK) {
+        ret = enif_make_binary(env, st->tgt);
+        enif_free(st->tgt);
+        st->tgt = NULL;
+        enif_release_resource(st);
+        return make_ok(env, priv, ret);
+    } else {
+        assert(status == ST_PARTIAL && "invalid status");
+        ret = enif_make_resource(env, st);
+        enif_release_resource(st);
+        return make_partial(env, priv, ret);
+    }
+
+error:
+    if(st != NULL) {
+        enif_release_resource(st);
+    }
+
+    return ret;
+}
+
+
+static ENTERM
+cseq_b64url_encode_cont(ErlNifEnv* env, int argc, const ENTERM argv[])
+{
+    ErlNifBinary src;
+    cseq_priv* priv = (cseq_priv*) enif_priv_data(env);
+    cseq_st* st = NULL;
+    ENTERM ret;
+    int status;
+
+    if(argc != 2) {
+        return enif_make_badarg(env);
+    }
+
+    if(!enif_inspect_iolist_as_binary(env, argv[0], &src)) {
+        return enif_make_badarg(env);
+    }
+
+    if(src.size <= 0) {
+        return enif_make_badarg(env);
+    }
+
+    if(!enif_get_resource(env, argv[1], priv->res_st, (void**) &st)) {
+        return enif_make_badarg(env);
+    }
+
+    if(!check_pid(env, st)) {
+        return enif_make_badarg(env);
+    }
+
+    if(src.size != st->len) {
+        return enif_make_badarg(env);
+    }
+
+    status = cseq_b64url_encode(env, &src, st);
+
+    if(status == ST_OK) {
+        ret = enif_make_binary(env, st->tgt);
+        st->tgt = NULL;
+        enif_release_resource(st);
+        return make_ok(env, priv, ret);
+    } else {
+        assert(status == ST_PARTIAL && "invalid status");
+        ret = enif_make_resource(env, st);
+        enif_release_resource(st);
+        return make_partial(env, priv, ret);
+    }
+
+    assert(0 == 1 && "Invalid status from cseq_b64url_encode");
+}
+
+
+static ENTERM
+cseq_b64url_decode_init(ErlNifEnv* env, int argc, const ENTERM argv[])
+{
+    return enif_make_badarg(env);
+}
+
+
+static ENTERM
+cseq_b64url_decode_cont(ErlNifEnv* env, int argc, const ENTERM argv[])
+{
+    return enif_make_badarg(env);
+}
+
+
+static ErlNifFunc funcs[] = {
+    {"encode_init", 1, cseq_b64url_encode_init},
+    {"encode_cont", 2, cseq_b64url_encode_cont},
+    {"decode_init", 1, cseq_b64url_decode_init},
+    {"decode_cont", 2, cseq_b64url_decode_cont}
+};
+
+
+ERL_NIF_INIT(couch_seqs_b64url, funcs, &load, &reload, &upgrade, &unload);
+
+
diff --git a/rebar.config b/rebar.config
new file mode 100644
index 0000000..6f2ca46
--- /dev/null
+++ b/rebar.config
@@ -0,0 +1,12 @@
+{port_specs, [
+    {"priv/couch_seqs_b64.so", ["c_src/*.c"]}
+]}.
+
+{port_env, [
+    % Development compilation
+    % {".*", "CFLAGS", "$CFLAGS -g -Wall -Werror -fPIC"}
+
+    % Production compilation
+    {".*", "CFLAGS", "$CFLAGS -Wall -Werror -DNDEBUG -O3"}
+]}.
+
diff --git a/src/couch_seqs.app.src b/src/couch_seqs.app.src
new file mode 100644
index 0000000..44b6a3d
--- /dev/null
+++ b/src/couch_seqs.app.src
@@ -0,0 +1,6 @@
+{application, couch_seqs, [
+    {description, "A library for handling couch sequences"},
+    {vsn, git},
+    {registered, []},
+    {applications, [kernel]}
+]}.
diff --git a/src/couch_seqs_b64url.erl b/src/couch_seqs_b64url.erl
new file mode 100644
index 0000000..bc1830b
--- /dev/null
+++ b/src/couch_seqs_b64url.erl
@@ -0,0 +1,72 @@
+-module(couch_seqs_b64url).
+-on_load(init/0).
+
+
+-export([
+    encode/1,
+    decode/1
+]).
+
+
+-define(NOT_LOADED, not_loaded(?LINE)).
+
+
+-spec encode(iodata()) -> binary().
+encode(IoData) ->
+    case encode_init(IoData) of
+        {ok, Bin} ->
+            Bin;
+        {partial, St} ->
+            encode_loop(IoData, St)
+    end.
+
+
+-spec decode(iodata()) -> binary() | {error, any()}.
+decode(IoData) ->
+    case decode_init(IoData) of
+        {ok, Bin} ->
+            Bin;
+        {partial, St} ->
+            decode_loop(IoData, St)
+    end.
+
+
+init() ->
+    PrivDir = case code:priv_dir(?MODULE) of
+        {error, _} ->
+            EbinDir = filename:dirname(code:which(?MODULE)),
+            AppPath = filename:dirname(EbinDir),
+            filename:join(AppPath, "priv");
+        Path ->
+            Path
+    end,
+    erlang:load_nif(filename:join(PrivDir, "couch_seqs_b64"), 0).
+
+
+encode_loop(IoData, St) ->
+    case encode_cont(IoData, St) of
+        {ok, Bin} ->
+            Bin;
+        {partial, St} ->
+            encode_loop(IoData, St)
+    end.
+
+
+decode_loop(IoData, St) ->
+    case decode_cont(IoData, St) of
+        {ok, Bin} ->
+            Bin;
+        {partial, St} ->
+            decode_loop(IoData, St)
+    end.
+
+
+encode_init(_) -> ?NOT_LOADED.
+encode_cont(_, _) -> ?NOT_LOADED.
+decode_init(_) -> ?NOT_LOADED.
+decode_cont(_, _) -> ?NOT_LOADED.
+
+
+not_loaded(Line) ->
+    erlang:nif_error({not_loaded, [{module, ?MODULE}, {line, Line}]}).
+
diff --git a/test/couch_seqs_b64url_tests.erl b/test/couch_seqs_b64url_tests.erl
new file mode 100644
index 0000000..0e1089a
--- /dev/null
+++ b/test/couch_seqs_b64url_tests.erl
@@ -0,0 +1,35 @@
+-module(couch_seqs_b64url_tests).
+-compile(export_all).
+
+-include_lib("proper/include/proper.hrl").
+-include_lib("eunit/include/eunit.hrl").
+
+
+proper_test_() ->
+    PropErOpts = [
+        {to_file, user},
+        {max_size, 524288},
+        {numtests, 1000}
+    ],
+    {timeout, 3600, ?_assertEqual([], proper:module(?MODULE, PropErOpts))}.
+
+
+prop_encode_binary() ->
+    ?FORALL(Bin, binary(),
+        couch_seqs_b64url:encode(Bin) == couch_encode_base64url(Bin)
+    ).
+
+
+couch_encode_base64url(Data) ->
+    Url1 = iolist_to_binary(re:replace(base64:encode(Url), "=+$", "")),
+    Url2 = iolist_to_binary(re:replace(Url1, "/", "_", [global])),
+    iolist_to_binary(re:replace(Url2, "\\+", "-", [global])).
+
+
+couch_decode_base64url(Data) ->
+    Url1 = re:replace(iolist_to_binary(Url64), "-", "+", [global]),
+    Url2 = iolist_to_binary(
+        re:replace(iolist_to_binary(Url1), "_", "/", [global])
+    ),
+    Padding = list_to_binary(lists:duplicate((4 - size(Url2) rem 4) rem 4, $=)),
+    base64:decode(<<Url2/binary, Padding/binary>>).


[couchdb-b64url] 10/31: Just assign a default value to ret

Posted by ja...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jaydoane pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/couchdb-b64url.git

commit 406e9f79aa91f81aaf59e29f57d03dc56bee5002
Author: Paul J. Davis <pa...@gmail.com>
AuthorDate: Thu Nov 21 15:46:18 2013 -0600

    Just assign a default value to ret
    
    Not sure how gcc was detecting that ret might've been used uninitialized
    but rather than try and poke at it throught the commit, tag, update
    dbcore, run jenkins build cycle I'll just assign a default value.
---
 c_src/b64url.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/c_src/b64url.c b/c_src/b64url.c
index 0bc6ab8..f4067fb 100644
--- a/c_src/b64url.c
+++ b/c_src/b64url.c
@@ -542,7 +542,7 @@ b64url_decode_cont(ErlNifEnv* env, int argc, const ENTERM argv[])
     ErlNifBinary src;
     b64url_priv* priv = (b64url_priv*) enif_priv_data(env);
     b64url_st* st = NULL;
-    ENTERM ret;
+    ENTERM ret = priv->atom_error;
     int status;
 
     if(argc != 2) {


[couchdb-b64url] 07/31: Remove cseq references after rename

Posted by ja...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jaydoane pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/couchdb-b64url.git

commit 41f00c444c5e7aefd8fdfc62d682b6a439099e33
Author: Paul J. Davis <pa...@gmail.com>
AuthorDate: Thu Nov 21 15:22:27 2013 -0600

    Remove cseq references after rename
---
 c_src/b64url.c | 108 ++++++++++++++++++++++++++++-----------------------------
 1 file changed, 54 insertions(+), 54 deletions(-)

diff --git a/c_src/b64url.c b/c_src/b64url.c
index d1e5765..0bc6ab8 100644
--- a/c_src/b64url.c
+++ b/c_src/b64url.c
@@ -18,7 +18,7 @@ typedef struct
     ENTERM atom_bad_block;
 
     ErlNifResourceType* res_st;
-} cseq_priv;
+} b64url_priv;
 
 
 typedef struct
@@ -28,7 +28,7 @@ typedef struct
     size_t        len;
     size_t        si;
     size_t        ti;
-} cseq_st;
+} b64url_st;
 
 
 typedef enum
@@ -36,7 +36,7 @@ typedef enum
     ST_OK,
     ST_ERROR,
     ST_PARTIAL
-} cseq_status;
+} b64url_status;
 
 
 const unsigned char B64URL_B2A[256] = {
@@ -102,21 +102,21 @@ make_atom(ErlNifEnv* env, const char* name)
 
 
 static inline ENTERM
-make_ok(ErlNifEnv* env, cseq_priv* priv, ENTERM value)
+make_ok(ErlNifEnv* env, b64url_priv* priv, ENTERM value)
 {
     return enif_make_tuple2(env, priv->atom_ok, value);
 }
 
 
 static inline ENTERM
-make_error(ErlNifEnv* env, cseq_priv* priv, ENTERM value)
+make_error(ErlNifEnv* env, b64url_priv* priv, ENTERM value)
 {
     return enif_make_tuple2(env, priv->atom_error, value);
 }
 
 
 static inline ENTERM
-make_bad_block(ErlNifEnv* env, cseq_priv* priv, size_t pos)
+make_bad_block(ErlNifEnv* env, b64url_priv* priv, size_t pos)
 {
     ENTERM pterm = enif_make_uint64(env, pos);
     return enif_make_tuple2(env, priv->atom_bad_block, pterm);
@@ -124,14 +124,14 @@ make_bad_block(ErlNifEnv* env, cseq_priv* priv, size_t pos)
 
 
 static inline ENTERM
-make_partial(ErlNifEnv* env, cseq_priv* priv, ENTERM value)
+make_partial(ErlNifEnv* env, b64url_priv* priv, ENTERM value)
 {
     return enif_make_tuple2(env, priv->atom_partial, value);
 }
 
 
 static inline int
-check_pid(ErlNifEnv* env, cseq_st* st)
+check_pid(ErlNifEnv* env, b64url_st* st)
 {
     ErlNifPid self_pid;
     ENTERM self;
@@ -149,15 +149,15 @@ check_pid(ErlNifEnv* env, cseq_st* st)
 }
 
 
-static cseq_st*
-cseq_st_alloc(ErlNifEnv* env, cseq_priv* priv, ErlNifBinary* src, size_t tlen)
+static b64url_st*
+b64url_st_alloc(ErlNifEnv* env, b64url_priv* priv, ErlNifBinary* src, size_t tlen)
 {
-    cseq_st* st = enif_alloc_resource(priv->res_st, sizeof(cseq_st));
+    b64url_st* st = enif_alloc_resource(priv->res_st, sizeof(b64url_st));
     if(st == NULL) {
         goto error;
     }
 
-    memset(st, '\0', sizeof(cseq_st));
+    memset(st, '\0', sizeof(b64url_st));
     enif_self(env, &(st->pid));
     st->len = src->size;
     st->si = 0;
@@ -182,9 +182,9 @@ error:
 
 
 static void
-cseq_st_free(ErlNifEnv* env, void* obj)
+b64url_st_free(ErlNifEnv* env, void* obj)
 {
-    cseq_st* st = (cseq_st*) obj;
+    b64url_st* st = (b64url_st*) obj;
 
     if(st->tgt != NULL) {
         enif_release_binary(st->tgt);
@@ -193,9 +193,9 @@ cseq_st_free(ErlNifEnv* env, void* obj)
 }
 
 static ENTERM
-cseq_st_enc_ret(ErlNifEnv* env, cseq_st* st, int status)
+b64url_st_enc_ret(ErlNifEnv* env, b64url_st* st, int status)
 {
-    cseq_priv* priv = (cseq_priv*) enif_priv_data(env);
+    b64url_priv* priv = (b64url_priv*) enif_priv_data(env);
     ENTERM ret;
 
     if(status == ST_OK) {
@@ -214,9 +214,9 @@ cseq_st_enc_ret(ErlNifEnv* env, cseq_st* st, int status)
 }
 
 static ENTERM
-cseq_st_dec_ret(ErlNifEnv* env, cseq_st* st, int status, ENTERM ret)
+b64url_st_dec_ret(ErlNifEnv* env, b64url_st* st, int status, ENTERM ret)
 {
-    cseq_priv* priv = (cseq_priv*) enif_priv_data(env);
+    b64url_priv* priv = (b64url_priv*) enif_priv_data(env);
 
     if(status == ST_OK) {
         ret = make_ok(env, priv, enif_make_binary(env, st->tgt));
@@ -241,13 +241,13 @@ load(ErlNifEnv* env, void** priv, ENTERM info)
     int flags = ERL_NIF_RT_CREATE | ERL_NIF_RT_TAKEOVER;
     ErlNifResourceType* res;
 
-    cseq_priv* new_priv = (cseq_priv*) enif_alloc(sizeof(cseq_priv));
+    b64url_priv* new_priv = (b64url_priv*) enif_alloc(sizeof(b64url_priv));
     if(new_priv == NULL) {
         return 1;
     }
 
     res = enif_open_resource_type(
-            env, NULL, "couch_seq", cseq_st_free, flags, NULL);
+            env, NULL, "couch_seq", b64url_st_free, flags, NULL);
     if(res == NULL) {
         return 1;
     }
@@ -287,8 +287,8 @@ unload(ErlNifEnv* env, void* priv)
 }
 
 
-static inline cseq_status
-cseq_b64url_encode(ErlNifEnv* env, ErlNifBinary* src, cseq_st* st)
+static inline b64url_status
+b64url_encode(ErlNifEnv* env, ErlNifBinary* src, b64url_st* st)
 {
     size_t chunk_start = st->si;
     unsigned char c1;
@@ -336,11 +336,11 @@ cseq_b64url_encode(ErlNifEnv* env, ErlNifBinary* src, cseq_st* st)
 
 
 static ENTERM
-cseq_b64url_encode_init(ErlNifEnv* env, int argc, const ENTERM argv[])
+b64url_encode_init(ErlNifEnv* env, int argc, const ENTERM argv[])
 {
     ErlNifBinary src;
-    cseq_priv* priv = (cseq_priv*) enif_priv_data(env);
-    cseq_st* st = NULL;
+    b64url_priv* priv = (b64url_priv*) enif_priv_data(env);
+    b64url_st* st = NULL;
     size_t tlen;
     size_t rem;
     int status;
@@ -366,15 +366,15 @@ cseq_b64url_encode_init(ErlNifEnv* env, int argc, const ENTERM argv[])
         tlen += 3;
     }
 
-    st = cseq_st_alloc(env, priv, &src, tlen);
+    st = b64url_st_alloc(env, priv, &src, tlen);
     if(st == NULL) {
         ret = make_error(env, priv, priv->atom_nomem);
         goto error;
     }
 
-    status = cseq_b64url_encode(env, &src, st);
+    status = b64url_encode(env, &src, st);
 
-    return cseq_st_enc_ret(env, st, status);
+    return b64url_st_enc_ret(env, st, status);
 
 error:
     if(st != NULL) {
@@ -386,11 +386,11 @@ error:
 
 
 static ENTERM
-cseq_b64url_encode_cont(ErlNifEnv* env, int argc, const ENTERM argv[])
+b64url_encode_cont(ErlNifEnv* env, int argc, const ENTERM argv[])
 {
     ErlNifBinary src;
-    cseq_priv* priv = (cseq_priv*) enif_priv_data(env);
-    cseq_st* st = NULL;
+    b64url_priv* priv = (b64url_priv*) enif_priv_data(env);
+    b64url_st* st = NULL;
     int status;
 
     if(argc != 2) {
@@ -413,16 +413,16 @@ cseq_b64url_encode_cont(ErlNifEnv* env, int argc, const ENTERM argv[])
         return enif_make_badarg(env);
     }
 
-    status = cseq_b64url_encode(env, &src, st);
+    status = b64url_encode(env, &src, st);
 
-    return cseq_st_enc_ret(env, st, status);
+    return b64url_st_enc_ret(env, st, status);
 }
 
 
-static inline cseq_status
-cseq_b64url_decode(ErlNifEnv* env, ErlNifBinary* src, cseq_st* st, ENTERM* ret)
+static inline b64url_status
+b64url_decode(ErlNifEnv* env, ErlNifBinary* src, b64url_st* st, ENTERM* ret)
 {
-    cseq_priv* priv = (cseq_priv*) enif_priv_data(env);
+    b64url_priv* priv = (b64url_priv*) enif_priv_data(env);
     size_t chunk_start = st->si;
     unsigned char c1;
     unsigned char c2;
@@ -487,11 +487,11 @@ cseq_b64url_decode(ErlNifEnv* env, ErlNifBinary* src, cseq_st* st, ENTERM* ret)
 
 
 static ENTERM
-cseq_b64url_decode_init(ErlNifEnv* env, int argc, const ENTERM argv[])
+b64url_decode_init(ErlNifEnv* env, int argc, const ENTERM argv[])
 {
     ErlNifBinary src;
-    cseq_priv* priv = (cseq_priv*) enif_priv_data(env);
-    cseq_st* st = NULL;
+    b64url_priv* priv = (b64url_priv*) enif_priv_data(env);
+    b64url_st* st = NULL;
     size_t tlen;
     int status;
     ENTERM ret;
@@ -517,15 +517,15 @@ cseq_b64url_decode_init(ErlNifEnv* env, int argc, const ENTERM argv[])
         tlen += 2;
     }
 
-    st = cseq_st_alloc(env, priv, &src, tlen);
+    st = b64url_st_alloc(env, priv, &src, tlen);
     if(st == NULL) {
         ret = make_error(env, priv, priv->atom_nomem);
         goto error;
     }
 
-    status = cseq_b64url_decode(env, &src, st, &ret);
+    status = b64url_decode(env, &src, st, &ret);
 
-    return cseq_st_dec_ret(env, st, status, ret);
+    return b64url_st_dec_ret(env, st, status, ret);
 
 error:
     if(st != NULL) {
@@ -537,11 +537,11 @@ error:
 
 
 static ENTERM
-cseq_b64url_decode_cont(ErlNifEnv* env, int argc, const ENTERM argv[])
+b64url_decode_cont(ErlNifEnv* env, int argc, const ENTERM argv[])
 {
     ErlNifBinary src;
-    cseq_priv* priv = (cseq_priv*) enif_priv_data(env);
-    cseq_st* st = NULL;
+    b64url_priv* priv = (b64url_priv*) enif_priv_data(env);
+    b64url_st* st = NULL;
     ENTERM ret;
     int status;
 
@@ -565,9 +565,9 @@ cseq_b64url_decode_cont(ErlNifEnv* env, int argc, const ENTERM argv[])
         return enif_make_badarg(env);
     }
 
-    status = cseq_b64url_decode(env, &src, st, &ret);
+    status = b64url_decode(env, &src, st, &ret);
 
-    return cseq_st_dec_ret(env, st, status, ret);
+    return b64url_st_dec_ret(env, st, status, ret);
 }
 
 
@@ -576,9 +576,9 @@ static unsigned char CHECK_A2B[64] =
 
 
 static ENTERM
-cseq_b64url_check_tables(ErlNifEnv* env, int argc, const ENTERM argv[])
+b64url_check_tables(ErlNifEnv* env, int argc, const ENTERM argv[])
 {
-    cseq_priv* priv = (cseq_priv*) enif_priv_data(env);
+    b64url_priv* priv = (b64url_priv*) enif_priv_data(env);
     ENTERM pos;
     int i;
 
@@ -617,11 +617,11 @@ cseq_b64url_check_tables(ErlNifEnv* env, int argc, const ENTERM argv[])
 }
 
 static ErlNifFunc funcs[] = {
-    {"encode_init", 1, cseq_b64url_encode_init},
-    {"encode_cont", 2, cseq_b64url_encode_cont},
-    {"decode_init", 1, cseq_b64url_decode_init},
-    {"decode_cont", 2, cseq_b64url_decode_cont},
-    {"check_tables", 0, cseq_b64url_check_tables}
+    {"encode_init", 1, b64url_encode_init},
+    {"encode_cont", 2, b64url_encode_cont},
+    {"decode_init", 1, b64url_decode_init},
+    {"decode_cont", 2, b64url_decode_cont},
+    {"check_tables", 0, b64url_check_tables}
 };
 
 


[couchdb-b64url] 19/31: Fix "dereferencing type-punned pointer will break strict-aliasing" warn

Posted by ja...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jaydoane pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/couchdb-b64url.git

commit 30a50f268ffce680e523d5ac3ee6b60db25d4347
Author: Alexander Shorin <kx...@apache.org>
AuthorDate: Wed Jul 15 23:53:43 2015 +0300

    Fix "dereferencing type-punned pointer will break strict-aliasing" warn
    
    Thanks @davisp for help with the patch.
    
    COUCHDB-2588
---
 c_src/b64url.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/c_src/b64url.c b/c_src/b64url.c
index 842d82e..074d9a0 100644
--- a/c_src/b64url.c
+++ b/c_src/b64url.c
@@ -411,6 +411,7 @@ b64url_encode_cont(ErlNifEnv* env, int argc, const ENTERM argv[])
     ErlNifBinary src;
     b64url_priv* priv = (b64url_priv*) enif_priv_data(env);
     b64url_st* st = NULL;
+    void* res = NULL;
     int status;
 
     if(argc != 2) {
@@ -421,10 +422,12 @@ b64url_encode_cont(ErlNifEnv* env, int argc, const ENTERM argv[])
         return enif_make_badarg(env);
     }
 
-    if(!enif_get_resource(env, argv[1], priv->res_st, (void**) &st)) {
+    if(!enif_get_resource(env, argv[1], priv->res_st, &res)) {
         return enif_make_badarg(env);
     }
 
+    st = (b64url_st*) res;
+
     if(!check_pid(env, st)) {
         return enif_make_badarg(env);
     }
@@ -565,6 +568,7 @@ b64url_decode_cont(ErlNifEnv* env, int argc, const ENTERM argv[])
     ErlNifBinary src;
     b64url_priv* priv = (b64url_priv*) enif_priv_data(env);
     b64url_st* st = NULL;
+    void* res = NULL;
     ENTERM ret = priv->atom_error;
     int status;
 
@@ -576,10 +580,12 @@ b64url_decode_cont(ErlNifEnv* env, int argc, const ENTERM argv[])
         return enif_make_badarg(env);
     }
 
-    if(!enif_get_resource(env, argv[1], priv->res_st, (void**) &st)) {
+    if(!enif_get_resource(env, argv[1], priv->res_st, &res)) {
         return enif_make_badarg(env);
     }
 
+    st = (b64url_st*) res;
+
     if(!check_pid(env, st)) {
         return enif_make_badarg(env);
     }


[couchdb-b64url] 22/31: Add Makefile

Posted by ja...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jaydoane pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/couchdb-b64url.git

commit 5ea50ac06b3c2997e32e2178a2e01c966df053fc
Author: Alexander Shorin <kx...@apache.org>
AuthorDate: Fri Jul 24 05:19:55 2015 +0300

    Add Makefile
---
 Makefile | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 46 insertions(+)

diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..754165e
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,46 @@
+REBAR?=rebar
+
+
+.PHONY: all
+# target: all - Makes everything
+all: build
+
+
+.PHONY: build
+# target: build - Builds the project
+build:
+	$(REBAR) compile
+
+
+.PHONY: check
+# target: check - Checks if project builds and passes all the tests
+check: build eunit
+
+
+.PHONY: clean
+# target: clean - Removes build artifacts
+clean:
+	$(REBAR) clean
+	rm -f test/*.beam
+
+
+.PHONY: distclean
+# target: distclean - Removes all unversioned files
+distclean: clean
+	git clean -fxd
+
+
+.PHONY: eunit
+# target: eunit - Runs eunit test suite
+eunit:
+	$(REBAR) eunit
+
+
+.PHONY: help
+# target: help - Prints this help
+help:
+	@egrep "^# target:" Makefile | sed -e 's/^# target: //g' | sort
+
+
+%.beam: %.erl
+	erlc -o test/ $<


[couchdb-b64url] 12/31: A simplistic benchmark

Posted by ja...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jaydoane pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/couchdb-b64url.git

commit 75336f1390720c94abc94d94801b81c0f55aab20
Author: Paul J. Davis <pa...@gmail.com>
AuthorDate: Tue Dec 3 12:28:57 2013 -0600

    A simplistic benchmark
    
    This is a rather simple benchmark that just looks at how much data can
    be passed through the NIF vs the current implementation in CouchDB.
---
 test/benchmark.escript | 168 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 168 insertions(+)

diff --git a/test/benchmark.escript b/test/benchmark.escript
new file mode 100755
index 0000000..5c2bdd5
--- /dev/null
+++ b/test/benchmark.escript
@@ -0,0 +1,168 @@
+#!/usr/bin/env escript
+
+-mode(compile).
+
+
+-export([
+    encode/1,
+    decode/1,
+    run_worker/1
+]).
+
+
+-record(st, {
+    parent,
+    module,
+    workers,
+    minsize,
+    maxsize,
+    duration,
+    total_bytes
+}).
+
+
+main([Workers0, MinSize0, MaxSize0, Duration0]) ->
+    code:add_path("./ebin"),
+    code:add_path("../ebin"),
+    Workers = to_int(Workers0),
+    MinSize = to_int(MinSize0),
+    MaxSize = to_int(MaxSize0),
+    Duration = to_int(Duration0),
+    if Workers > 0 -> ok; true ->
+        die("Worker count must be positive~n")
+    end,
+    if MinSize > 0 -> ok; true ->
+        die("Minimum size must be positive.~n")
+    end,
+    if MaxSize > 0 -> ok; true ->
+        die("Maximum size must be positive.~n")
+    end,
+    if MinSize < MaxSize -> ok; true ->
+        die("Minimum size must be less than maximum size.~n")
+    end,
+    if Duration > 0 -> ok; true ->
+        die("Duration must be positive.~n")
+    end,
+    St = #st{
+        parent = self(),
+        workers = Workers,
+        minsize = MinSize,
+        maxsize = MaxSize,
+        duration = Duration
+    },
+    lists:foreach(fun(M) ->
+        run_test(St#st{module=M})
+    end, randomize([b64url, ?MODULE]));
+
+main(_) ->
+    Args = [escript:script_name()],
+    die("usage: ~s num_workers min_size max_size time_per_test~n", Args).
+
+
+run_test(St) ->
+    Workers = spawn_workers(St#st.workers, St),
+    start_workers(Workers),
+    Results = wait_for_workers(Workers),
+    report(St#st.module, St#st.duration, Results).
+
+
+start_workers(Pids) ->
+    lists:foreach(fun(P) ->
+        P ! start
+    end, Pids).
+
+
+wait_for_workers(Pids) ->
+    lists:map(fun(P) ->
+        receive
+            {P, TotalBytes} -> TotalBytes
+        end
+    end, Pids).
+
+
+report(Module, Duration, TotalByteList) ->
+    ModDesc = case Module of
+        ?MODULE -> "erl";
+        b64url -> "nif"
+    end,
+    TotalBytes = lists:sum(TotalByteList),
+    io:format("~s : ~14b bytes / ~3b seconds = ~14.2f bps~n", [
+        ModDesc, TotalBytes, Duration, TotalBytes / Duration]).
+
+
+spawn_workers(NumWorkers, St) ->
+    lists:map(fun(_) ->
+        spawn_link(?MODULE, run_worker, [St])
+    end, lists:seq(1, NumWorkers)).
+
+
+run_worker(St) ->
+    random:seed(erlang:now()),
+    receive
+        start -> ok
+    end,
+    run_worker(St#st{total_bytes=0}, os:timestamp()).
+
+
+run_worker(St, Started) ->
+    HasRun = timer:now_diff(os:timestamp(), Started),
+    case HasRun div 1000000 > St#st.duration of
+        true ->
+            St#st.parent ! {self(), St#st.total_bytes};
+        false ->
+            NewSt = do_round_trip(St),
+            run_worker(NewSt, Started)
+    end.
+
+
+do_round_trip(St) ->
+    Size = St#st.minsize + random:uniform(St#st.maxsize - St#st.minsize),
+    Data = crypto:rand_bytes(Size),
+    Encoded = (St#st.module):encode(Data),
+    Data = (St#st.module):decode(Encoded),
+    St#st{total_bytes=St#st.total_bytes+Size}.
+
+
+encode(Url) ->
+    Url1 = iolist_to_binary(re:replace(base64:encode(Url), "=+$", "")),
+    Url2 = iolist_to_binary(re:replace(Url1, "/", "_", [global])),
+    iolist_to_binary(re:replace(Url2, "\\+", "-", [global])).
+
+
+decode(Url64) ->
+    Url1 = re:replace(iolist_to_binary(Url64), "-", "+", [global]),
+    Url2 = iolist_to_binary(
+        re:replace(iolist_to_binary(Url1), "_", "/", [global])
+    ),
+    Padding = list_to_binary(lists:duplicate((4 - size(Url2) rem 4) rem 4, $=)),
+    base64:decode(<<Url2/binary, Padding/binary>>).
+
+
+randomize(List) ->
+    random:seed(erlang:now()),
+    List0 = [{random:uniform(), L} || L <- List],
+    List1 = lists:sort(List0),
+    [L || {_, L} <- List1].
+
+
+to_int(Val) when is_integer(Val) ->
+    Val;
+to_int(Val) when is_binary(Val) ->
+    to_int(binary_to_list(Val));
+to_int(Val) when is_list(Val) ->
+    try
+        list_to_integer(Val)
+    catch _:_ ->
+        die("Invalid integer: ~w~n", [Val])
+    end;
+to_int(Val) ->
+    die("Invalid integer: ~w~n", [Val]).
+
+
+die(Message) ->
+    die(Message, []).
+
+die(Format, Args) ->
+    io:format(Format, Args),
+    init:stop().
+


[couchdb-b64url] 15/31: Add ASF license

Posted by ja...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jaydoane pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/couchdb-b64url.git

commit 88eede873572d61d9c5a6884eaca4e2d87034663
Author: Robert Newson <rn...@apache.org>
AuthorDate: Wed Jul 16 17:07:45 2014 +0100

    Add ASF license
---
 LICENSE            | 202 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 c_src/b64url.c     |  11 +++
 src/b64url.app.src |  12 ++++
 src/b64url.erl     |  12 ++++
 4 files changed, 237 insertions(+)

diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..e06d208
--- /dev/null
+++ b/LICENSE
@@ -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/c_src/b64url.c b/c_src/b64url.c
index 478468b..842d82e 100644
--- a/c_src/b64url.c
+++ b/c_src/b64url.c
@@ -1,3 +1,14 @@
+// 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.
 
 #include <assert.h>
 #include <string.h>
diff --git a/src/b64url.app.src b/src/b64url.app.src
index 839e9b1..a2a8913 100644
--- a/src/b64url.app.src
+++ b/src/b64url.app.src
@@ -1,3 +1,15 @@
+% 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.
+
 {application, b64url, [
     {description, "A small NIF for encoding Base64 URL encoding/decoding."},
     {vsn, git},
diff --git a/src/b64url.erl b/src/b64url.erl
index e4b1f55..2eb4b82 100644
--- a/src/b64url.erl
+++ b/src/b64url.erl
@@ -1,3 +1,15 @@
+% 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.
+
 -module(b64url).
 -on_load(init/0).
 


[couchdb-b64url] 31/31: Merge pull request #7 from cloudant/minimum-erlang-otp-19

Posted by ja...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jaydoane pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/couchdb-b64url.git

commit d9220e2af86edde4a6e49286b12d79d02fcd5c3b
Merge: 71e064f 69b5308
Author: Jay Doane <ja...@gmail.com>
AuthorDate: Fri Feb 22 19:57:37 2019 -0800

    Merge pull request #7 from cloudant/minimum-erlang-otp-19
    
    Change minimum Erlang to OTP 19

 .gitignore             |  1 +
 .travis.yml            | 11 +++++-----
 rebar.config           | 11 ----------
 test/b64url_rand.erl   | 57 --------------------------------------------------
 test/b64url_tests.erl  | 12 +++++------
 test/benchmark.escript |  4 ++--
 6 files changed, 15 insertions(+), 81 deletions(-)


[couchdb-b64url] 03/31: Decoder with tests

Posted by ja...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jaydoane pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/couchdb-b64url.git

commit be1bb4e8551d102bf0e3faa78bf2984375ad08ca
Author: Paul J. Davis <pa...@gmail.com>
AuthorDate: Fri Nov 1 15:36:55 2013 -0500

    Decoder with tests
---
 c_src/couch_seqs_b64.c           | 329 ++++++++++++++++++++++++++++++++-------
 src/couch_seqs_b64url.erl        |  14 ++
 test/couch_seqs_b64url_tests.erl | 101 ++++++++++--
 3 files changed, 378 insertions(+), 66 deletions(-)

diff --git a/c_src/couch_seqs_b64.c b/c_src/couch_seqs_b64.c
index a48d928..a631792 100644
--- a/c_src/couch_seqs_b64.c
+++ b/c_src/couch_seqs_b64.c
@@ -15,6 +15,7 @@ typedef struct
     ENTERM atom_partial;
 
     ENTERM atom_nomem;
+    ENTERM atom_bad_block;
 
     ErlNifResourceType* res_st;
 } cseq_priv;
@@ -38,7 +39,7 @@ typedef enum
 } cseq_status;
 
 
-const char B64URL_B2A[256] = {
+const unsigned char B64URL_B2A[256] = {
    65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, //   0 -  15
    81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 97, 98, 99,100,101,102, //  16 -  31
   103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118, //  32 -  47
@@ -57,7 +58,7 @@ const char B64URL_B2A[256] = {
   255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255  // 240 - 255
 };
 
-const char B64URL_A2B[256] = {
+const unsigned char B64URL_A2B[256] = {
   255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, //   0 -  15
   255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, //  16 -  31
   255,255,255,255,255,255,255,255,255,255,255,255,255, 62,255,255, //  32 -  47
@@ -115,6 +116,14 @@ make_error(ErlNifEnv* env, cseq_priv* priv, ENTERM value)
 
 
 static inline ENTERM
+make_bad_block(ErlNifEnv* env, cseq_priv* priv, size_t pos)
+{
+    ENTERM pterm = enif_make_uint64(env, pos);
+    return enif_make_tuple2(env, priv->atom_bad_block, pterm);
+}
+
+
+static inline ENTERM
 make_partial(ErlNifEnv* env, cseq_priv* priv, ENTERM value)
 {
     return enif_make_tuple2(env, priv->atom_partial, value);
@@ -140,8 +149,40 @@ check_pid(ErlNifEnv* env, cseq_st* st)
 }
 
 
+static cseq_st*
+cseq_st_alloc(ErlNifEnv* env, cseq_priv* priv, ErlNifBinary* src, size_t tlen)
+{
+    cseq_st* st = enif_alloc_resource(priv->res_st, sizeof(cseq_st));
+    if(st == NULL) {
+        goto error;
+    }
+
+    memset(st, '\0', sizeof(cseq_st));
+    enif_self(env, &(st->pid));
+    st->len = src->size;
+    st->si = 0;
+    st->ti = 0;
+    st->tgt = (ErlNifBinary*) enif_alloc(sizeof(ErlNifBinary));
+    if(st->tgt == NULL) {
+        goto error;
+    }
+
+    if(!enif_alloc_binary(tlen, st->tgt)) {
+        goto error;
+    }
+
+    return st;
+
+error:
+    if(st != NULL) {
+        enif_release_resource(st);
+    }
+    return NULL;
+}
+
+
 static void
-cseq_free(ErlNifEnv* env, void* obj)
+cseq_st_free(ErlNifEnv* env, void* obj)
 {
     cseq_st* st = (cseq_st*) obj;
 
@@ -151,6 +192,48 @@ cseq_free(ErlNifEnv* env, void* obj)
     }
 }
 
+static ENTERM
+cseq_st_enc_ret(ErlNifEnv* env, cseq_st* st, int status)
+{
+    cseq_priv* priv = (cseq_priv*) enif_priv_data(env);
+    ENTERM ret;
+
+    if(status == ST_OK) {
+        ret = make_ok(env, priv, enif_make_binary(env, st->tgt));
+        enif_free(st->tgt);
+        st->tgt = NULL;
+    } else if(status == ST_PARTIAL) {
+        ret = make_partial(env, priv, enif_make_resource(env, st));
+    } else {
+        assert(0 == 1 && "invalid status encoder status");
+        ret = enif_make_badarg(env);
+    }
+
+    enif_release_resource(st);
+    return ret;
+}
+
+static ENTERM
+cseq_st_dec_ret(ErlNifEnv* env, cseq_st* st, int status, ENTERM ret)
+{
+    cseq_priv* priv = (cseq_priv*) enif_priv_data(env);
+
+    if(status == ST_OK) {
+        ret = make_ok(env, priv, enif_make_binary(env, st->tgt));
+        enif_free(st->tgt);
+        st->tgt = NULL;
+    } else if(status == ST_ERROR) {
+        ret = make_error(env, priv, ret);
+    } else if(status == ST_PARTIAL) {
+        ret = make_partial(env, priv, enif_make_resource(env, st));
+    } else {
+        assert(0 == 1 && "invalid status decoder status");
+        ret = enif_make_badarg(env);
+    }
+
+    enif_release_resource(st);
+    return ret;
+}
 
 static int
 load(ErlNifEnv* env, void** priv, ENTERM info)
@@ -164,7 +247,7 @@ load(ErlNifEnv* env, void** priv, ENTERM info)
     }
 
     res = enif_open_resource_type(
-            env, NULL, "couch_seq", cseq_free, flags, NULL);
+            env, NULL, "couch_seq", cseq_st_free, flags, NULL);
     if(res == NULL) {
         return 1;
     }
@@ -175,6 +258,7 @@ load(ErlNifEnv* env, void** priv, ENTERM info)
     new_priv->atom_partial = make_atom(env, "partial");
 
     new_priv->atom_nomem = make_atom(env, "enomem");
+    new_priv->atom_bad_block = make_atom(env, "bad_block");
 
     *priv = (void*) new_priv;
 
@@ -267,23 +351,7 @@ cseq_b64url_encode_init(ErlNifEnv* env, int argc, const ENTERM argv[])
     }
 
     if(!enif_inspect_iolist_as_binary(env, argv[0], &src)) {
-        return enif_make_badarg(env);
-    }
-
-    st = (cseq_st*) enif_alloc_resource(priv->res_st, sizeof(cseq_st));
-    if(st == NULL) {
-        ret = make_error(env, priv, priv->atom_nomem);
-        goto error;
-    }
-
-    memset(st, '\0', sizeof(cseq_st));
-    enif_self(env, &(st->pid));
-    st->len = src.size;
-    st->si = 0;
-    st->ti = 0;
-    st->tgt = (ErlNifBinary*) enif_alloc(sizeof(ErlNifBinary));
-    if(st->tgt == NULL) {
-        ret = make_error(env, priv, priv->atom_nomem);
+        ret = enif_make_badarg(env);
         goto error;
     }
 
@@ -298,25 +366,15 @@ cseq_b64url_encode_init(ErlNifEnv* env, int argc, const ENTERM argv[])
         tlen += 3;
     }
 
-    if(!enif_alloc_binary(tlen, st->tgt)) {
+    st = cseq_st_alloc(env, priv, &src, tlen);
+    if(st == NULL) {
         ret = make_error(env, priv, priv->atom_nomem);
         goto error;
     }
 
     status = cseq_b64url_encode(env, &src, st);
 
-    if(status == ST_OK) {
-        ret = enif_make_binary(env, st->tgt);
-        enif_free(st->tgt);
-        st->tgt = NULL;
-        enif_release_resource(st);
-        return make_ok(env, priv, ret);
-    } else {
-        assert(status == ST_PARTIAL && "invalid status");
-        ret = enif_make_resource(env, st);
-        enif_release_resource(st);
-        return make_partial(env, priv, ret);
-    }
+    return cseq_st_enc_ret(env, st, status);
 
 error:
     if(st != NULL) {
@@ -333,7 +391,6 @@ cseq_b64url_encode_cont(ErlNifEnv* env, int argc, const ENTERM argv[])
     ErlNifBinary src;
     cseq_priv* priv = (cseq_priv*) enif_priv_data(env);
     cseq_st* st = NULL;
-    ENTERM ret;
     int status;
 
     if(argc != 2) {
@@ -344,10 +401,6 @@ cseq_b64url_encode_cont(ErlNifEnv* env, int argc, const ENTERM argv[])
         return enif_make_badarg(env);
     }
 
-    if(src.size <= 0) {
-        return enif_make_badarg(env);
-    }
-
     if(!enif_get_resource(env, argv[1], priv->res_st, (void**) &st)) {
         return enif_make_badarg(env);
     }
@@ -362,41 +415,213 @@ cseq_b64url_encode_cont(ErlNifEnv* env, int argc, const ENTERM argv[])
 
     status = cseq_b64url_encode(env, &src, st);
 
-    if(status == ST_OK) {
-        ret = enif_make_binary(env, st->tgt);
-        st->tgt = NULL;
-        enif_release_resource(st);
-        return make_ok(env, priv, ret);
+    return cseq_st_enc_ret(env, st, status);
+}
+
+
+static inline cseq_status
+cseq_b64url_decode(ErlNifEnv* env, ErlNifBinary* src, cseq_st* st, ENTERM* ret)
+{
+    cseq_priv* priv = (cseq_priv*) enif_priv_data(env);
+    size_t chunk_start = st->si;
+    unsigned char c1;
+    unsigned char c2;
+    unsigned char c3;
+    unsigned char c4;
+
+    assert(st->si % 4 == 0 && "invalid source index");
+    assert(st->ti % 3 == 0 && "invalid target index");
+
+    while(st->si + 4 <= src->size) {
+        c1 = B64URL_A2B[src->data[st->si++]];
+        c2 = B64URL_A2B[src->data[st->si++]];
+        c3 = B64URL_A2B[src->data[st->si++]];
+        c4 = B64URL_A2B[src->data[st->si++]];
+
+        if(c1 == 255 || c2 == 255 || c3 == 255 || c4 == 255) {
+            *ret = make_bad_block(env, priv, st->si-4);
+            return ST_ERROR;
+        }
+
+        st->tgt->data[st->ti++] = (c1 << 2) | (c2 >> 4);
+        st->tgt->data[st->ti++] = (c2 << 4) | (c3 >> 2);
+        st->tgt->data[st->ti++] = (c3 << 6) | c4;
+
+        if(st->si - chunk_start > BYTES_PER_PERCENT) {
+            if(do_consume_timeslice(env)) {
+                return ST_PARTIAL;
+            } else {
+                chunk_start = st->si;
+            }
+        }
+    }
+
+    if(src->size % 4 == 2) {
+        c1 = B64URL_A2B[src->data[st->si]];
+        c2 = B64URL_A2B[src->data[st->si+1]];
+
+        if(c1 == 255 || c2 == 255) {
+            *ret = make_bad_block(env, priv, st->si);
+            return ST_ERROR;
+        }
+
+        st->tgt->data[st->ti++] = (c1 << 2) | (c2 >> 4);
+    } else if(src->size % 4 == 3) {
+        c1 = B64URL_A2B[src->data[st->si]];
+        c2 = B64URL_A2B[src->data[st->si+1]];
+        c3 = B64URL_A2B[src->data[st->si+2]];
+
+        if(c1 == 255 || c2 == 255 || c3 == 255) {
+            *ret = make_bad_block(env, priv, st->si);
+            return ST_ERROR;
+        }
+
+        st->tgt->data[st->ti++] = (c1 << 2) | (c2 >> 4);
+        st->tgt->data[st->ti++] = (c2 << 4) | (c3 >> 2);
     } else {
-        assert(status == ST_PARTIAL && "invalid status");
-        ret = enif_make_resource(env, st);
-        enif_release_resource(st);
-        return make_partial(env, priv, ret);
+        assert(0 == 1 && "invalid source length");
     }
 
-    assert(0 == 1 && "Invalid status from cseq_b64url_encode");
+    return ST_OK;
 }
 
 
 static ENTERM
 cseq_b64url_decode_init(ErlNifEnv* env, int argc, const ENTERM argv[])
 {
-    return enif_make_badarg(env);
+    ErlNifBinary src;
+    cseq_priv* priv = (cseq_priv*) enif_priv_data(env);
+    cseq_st* st = NULL;
+    size_t tlen;
+    int status;
+    ENTERM ret;
+
+    if(argc != 1) {
+        return enif_make_badarg(env);
+    }
+
+    if(!enif_inspect_iolist_as_binary(env, argv[0], &src)) {
+        return enif_make_badarg(env);
+    }
+
+    // We don't do use '=' padding for URLs so our target length
+    // will be the number of blocks of 4 bytes times 3, plus 1 or 2
+    // depending on the number of bytes in the last block.
+    tlen = (src.size / 4) * 3;
+    if(src.size % 4 == 1) {
+        ret = enif_make_badarg(env);
+        goto error;
+    } else if(src.size % 4 == 2) {
+        tlen += 1;
+    } else if(src.size % 4 == 3) {
+        tlen += 2;
+    }
+
+    st = cseq_st_alloc(env, priv, &src, tlen);
+    if(st == NULL) {
+        ret = make_error(env, priv, priv->atom_nomem);
+        goto error;
+    }
+
+    status = cseq_b64url_decode(env, &src, st, &ret);
+
+    return cseq_st_dec_ret(env, st, status, ret);
+
+error:
+    if(st != NULL) {
+        enif_release_resource(st);
+    }
+
+    return ret;
 }
 
 
 static ENTERM
 cseq_b64url_decode_cont(ErlNifEnv* env, int argc, const ENTERM argv[])
 {
-    return enif_make_badarg(env);
+    ErlNifBinary src;
+    cseq_priv* priv = (cseq_priv*) enif_priv_data(env);
+    cseq_st* st = NULL;
+    ENTERM ret;
+    int status;
+
+    if(argc != 2) {
+        return enif_make_badarg(env);
+    }
+
+    if(!enif_inspect_iolist_as_binary(env, argv[0], &src)) {
+        return enif_make_badarg(env);
+    }
+
+    if(!enif_get_resource(env, argv[1], priv->res_st, (void**) &st)) {
+        return enif_make_badarg(env);
+    }
+
+    if(!check_pid(env, st)) {
+        return enif_make_badarg(env);
+    }
+
+    if(src.size != st->len) {
+        return enif_make_badarg(env);
+    }
+
+    status = cseq_b64url_decode(env, &src, st, &ret);
+
+    return cseq_st_dec_ret(env, st, status, ret);
 }
 
 
+static unsigned char CHECK_A2B[64] =
+        "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
+
+
+static ENTERM
+cseq_b64url_check_tables(ErlNifEnv* env, int argc, const ENTERM argv[])
+{
+    cseq_priv* priv = (cseq_priv*) enif_priv_data(env);
+    ENTERM pos;
+    int i;
+
+    for(i = 0; i < 64; i++) {
+        if(B64URL_B2A[i] != CHECK_A2B[i]) {
+            pos = enif_make_int(env, i);
+            return enif_make_tuple2(env, priv->atom_error, pos);
+        }
+    }
+
+    for(i = 64; i < 256; i++) {
+        if(B64URL_B2A[i] != 255) {
+            pos = enif_make_int(env, i);
+            return enif_make_tuple2(env, priv->atom_error, pos);
+        }
+    }
+
+    for(i = 0; i < 64; i++) {
+        if(B64URL_A2B[CHECK_A2B[i]] != i) {
+            pos = enif_make_int(env, i);
+            return enif_make_tuple2(env, priv->atom_error, pos);
+        }
+    }
+
+    for(i = 0; i < 256; i++) {
+        if(B64URL_A2B[i] == 255) {
+            continue;
+        }
+        if(CHECK_A2B[B64URL_A2B[i]] != i) {
+            pos = enif_make_int(env, i);
+            return enif_make_tuple2(env, priv->atom_error, pos);
+        }
+    }
+
+    return priv->atom_ok;
+}
+
 static ErlNifFunc funcs[] = {
     {"encode_init", 1, cseq_b64url_encode_init},
     {"encode_cont", 2, cseq_b64url_encode_cont},
     {"decode_init", 1, cseq_b64url_decode_init},
-    {"decode_cont", 2, cseq_b64url_decode_cont}
+    {"decode_cont", 2, cseq_b64url_decode_cont},
+    {"check_tables", 0, cseq_b64url_check_tables}
 };
 
 
diff --git a/src/couch_seqs_b64url.erl b/src/couch_seqs_b64url.erl
index bc1830b..cd53f30 100644
--- a/src/couch_seqs_b64url.erl
+++ b/src/couch_seqs_b64url.erl
@@ -8,6 +8,12 @@
 ]).
 
 
+% Internal sanity checks
+-export([
+    check_tables/0
+]).
+
+
 -define(NOT_LOADED, not_loaded(?LINE)).
 
 
@@ -26,10 +32,16 @@ decode(IoData) ->
     case decode_init(IoData) of
         {ok, Bin} ->
             Bin;
+        {error, _} = Ret ->
+            Ret;
         {partial, St} ->
             decode_loop(IoData, St)
     end.
 
+-spec check_tables() -> ok | {error, non_neg_integer()}.
+check_tables() ->
+    ?NOT_LOADED.
+
 
 init() ->
     PrivDir = case code:priv_dir(?MODULE) of
@@ -56,6 +68,8 @@ decode_loop(IoData, St) ->
     case decode_cont(IoData, St) of
         {ok, Bin} ->
             Bin;
+        {error, _} = Ret ->
+            Ret;
         {partial, St} ->
             decode_loop(IoData, St)
     end.
diff --git a/test/couch_seqs_b64url_tests.erl b/test/couch_seqs_b64url_tests.erl
index 9fabe3e..3c27a18 100644
--- a/test/couch_seqs_b64url_tests.erl
+++ b/test/couch_seqs_b64url_tests.erl
@@ -5,11 +5,15 @@
 -include_lib("eunit/include/eunit.hrl").
 
 
+table_test_() ->
+    ?_assertEqual(ok, couch_seqs_b64url:check_tables()).
+
+
 proper_test_() ->
     PropErOpts = [
         {to_file, user},
         {max_size, 6401},
-        {numtests, 1000}
+        {numtests, 500}
     ],
     {timeout, 3600, ?_assertEqual([], proper:module(?MODULE, PropErOpts))}.
 
@@ -23,26 +27,61 @@ prop_encode_binary() ->
 
 
 prop_encode_iolist() ->
+    ?FORALL(IoList, shallow_iolist(), begin
+        A = couch_encode_base64url(iolist_to_binary(IoList)),
+        B = couch_seqs_b64url:encode(IoList),
+        A == B
+    end).
+
+
+prop_decode_binary() ->
     ?FORALL(Bin, binary(), begin
-        A = couch_encode_base64url(Bin),
-        B = couch_seqs_b64url:encode(to_iolist(Bin)),
+        B64UrlBin = couch_encode_base64url(Bin),
+        Dec = couch_seqs_b64url:decode(B64UrlBin),
+        Dec == Bin
+    end).
+
+
+prop_decode_iolist() ->
+    ?FORALL(IoList, shallow_b64_iolist(), begin
+        A = couch_decode_base64url(iolist_to_binary(IoList)),
+        B = couch_seqs_b64url:decode(IoList),
         A == B
     end).
 
 
-couch_encode_base64url(Url) ->
-    Url1 = iolist_to_binary(re:replace(base64:encode(Url), "=+$", "")),
-    Url2 = iolist_to_binary(re:replace(Url1, "/", "_", [global])),
-    iolist_to_binary(re:replace(Url2, "\\+", "-", [global])).
+prop_decode_binary_error() ->
+    ?FORALL({ErrBin, BlockPos}, bad_binary(), begin
+        Dec = couch_seqs_b64url:decode(ErrBin),
+        Dec == {error, {bad_block, BlockPos}}
+    end).
 
 
-couch_decode_base64url(Url64) ->
-    Url1 = re:replace(iolist_to_binary(Url64), "-", "+", [global]),
-    Url2 = iolist_to_binary(
-        re:replace(iolist_to_binary(Url1), "_", "/", [global])
-    ),
-    Padding = list_to_binary(lists:duplicate((4 - size(Url2) rem 4) rem 4, $=)),
-    base64:decode(<<Url2/binary, Padding/binary>>).
+prop_decode_bad_length() ->
+    ?FORALL(Bin, bad_len_binary(), begin
+        try
+            couch_seqs_b64url:decode(Bin),
+            false
+        catch error:badarg ->
+            true
+        end
+    end).
+
+
+shallow_iolist() ->
+    ?LET(Bin, binary(), to_iolist(Bin)).
+
+
+shallow_b64_iolist() ->
+    ?LET(Bin, binary(), to_iolist(couch_encode_base64url(Bin))).
+
+
+bad_binary() ->
+    ?LET(Bin, binary(), insert_error(Bin)).
+
+
+bad_len_binary() ->
+    ?LET(Bin, binary(), make_bad_len(Bin)).
 
 
 to_iolist(<<>>) ->
@@ -63,3 +102,37 @@ to_iolist(B) when is_binary(B), size(B) > 0 ->
     end.
 
 
+insert_error(B) when is_binary(B), size(B) < 2 ->
+    case random:uniform(2) of
+        1 -> {<<122, 255>>, 0};
+        2 -> {<<122, 122, 255>>, 0}
+    end;
+insert_error(B) when is_binary(B) ->
+    B64 = couch_encode_base64url(B),
+    S = random:uniform(size(B64)-1),
+    <<First:S/binary, _:1/binary, Second/binary>> = B64,
+    {<<First:S/binary, 255, Second/binary>>, 4 * (S div 4)}.
+
+
+make_bad_len(Bin) when size(Bin) rem 4 == 1 ->
+    Bin;
+make_bad_len(Bin) when size(Bin) rem 4 == 2 ->
+    <<"AAA", Bin/binary>>;
+make_bad_len(Bin) when size(Bin) rem 4 == 3 ->
+    <<"AA", Bin/binary>>;
+make_bad_len(Bin) when size(Bin) rem 4 == 0 ->
+    <<"A", Bin/binary>>.
+
+couch_encode_base64url(Url) ->
+    Url1 = iolist_to_binary(re:replace(base64:encode(Url), "=+$", "")),
+    Url2 = iolist_to_binary(re:replace(Url1, "/", "_", [global])),
+    iolist_to_binary(re:replace(Url2, "\\+", "-", [global])).
+
+
+couch_decode_base64url(Url64) ->
+    Url1 = re:replace(iolist_to_binary(Url64), "-", "+", [global]),
+    Url2 = iolist_to_binary(
+        re:replace(iolist_to_binary(Url1), "_", "/", [global])
+    ),
+    Padding = list_to_binary(lists:duplicate((4 - size(Url2) rem 4) rem 4, $=)),
+    base64:decode(<<Url2/binary, Padding/binary>>).


[couchdb-b64url] 05/31: Rename app to b64url

Posted by ja...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jaydoane pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/couchdb-b64url.git

commit 9acc9d2da7df4c24596f57abb8529c1efccacc5f
Author: Paul J. Davis <pa...@gmail.com>
AuthorDate: Thu Nov 21 13:34:40 2013 -0600

    Rename app to b64url
---
 c_src/{couch_seqs_b64.c => b64url.c}                   |  2 +-
 rebar.config                                           |  2 +-
 src/b64url.app.src                                     |  6 ++++++
 src/{couch_seqs_b64url.erl => b64url.erl}              |  4 ++--
 src/couch_seqs.app.src                                 |  6 ------
 test/{couch_seqs_b64url_tests.erl => b64url_tests.erl} | 16 ++++++++--------
 6 files changed, 18 insertions(+), 18 deletions(-)

diff --git a/c_src/couch_seqs_b64.c b/c_src/b64url.c
similarity index 99%
rename from c_src/couch_seqs_b64.c
rename to c_src/b64url.c
index a631792..d1e5765 100644
--- a/c_src/couch_seqs_b64.c
+++ b/c_src/b64url.c
@@ -625,6 +625,6 @@ static ErlNifFunc funcs[] = {
 };
 
 
-ERL_NIF_INIT(couch_seqs_b64url, funcs, &load, &reload, &upgrade, &unload);
+ERL_NIF_INIT(b64url, funcs, &load, &reload, &upgrade, &unload);
 
 
diff --git a/rebar.config b/rebar.config
index 5a9da0f..2dae006 100644
--- a/rebar.config
+++ b/rebar.config
@@ -1,5 +1,5 @@
 {port_specs, [
-    {"priv/couch_seqs_b64.so", ["c_src/*.c"]}
+    {"priv/b64url.so", ["c_src/*.c"]}
 ]}.
 
 {port_env, [
diff --git a/src/b64url.app.src b/src/b64url.app.src
new file mode 100644
index 0000000..839e9b1
--- /dev/null
+++ b/src/b64url.app.src
@@ -0,0 +1,6 @@
+{application, b64url, [
+    {description, "A small NIF for encoding Base64 URL encoding/decoding."},
+    {vsn, git},
+    {registered, []},
+    {applications, [kernel]}
+]}.
diff --git a/src/couch_seqs_b64url.erl b/src/b64url.erl
similarity index 94%
rename from src/couch_seqs_b64url.erl
rename to src/b64url.erl
index cd53f30..e4b1f55 100644
--- a/src/couch_seqs_b64url.erl
+++ b/src/b64url.erl
@@ -1,4 +1,4 @@
--module(couch_seqs_b64url).
+-module(b64url).
 -on_load(init/0).
 
 
@@ -52,7 +52,7 @@ init() ->
         Path ->
             Path
     end,
-    erlang:load_nif(filename:join(PrivDir, "couch_seqs_b64"), 0).
+    erlang:load_nif(filename:join(PrivDir, "b64url"), 0).
 
 
 encode_loop(IoData, St) ->
diff --git a/src/couch_seqs.app.src b/src/couch_seqs.app.src
deleted file mode 100644
index 44b6a3d..0000000
--- a/src/couch_seqs.app.src
+++ /dev/null
@@ -1,6 +0,0 @@
-{application, couch_seqs, [
-    {description, "A library for handling couch sequences"},
-    {vsn, git},
-    {registered, []},
-    {applications, [kernel]}
-]}.
diff --git a/test/couch_seqs_b64url_tests.erl b/test/b64url_tests.erl
similarity index 89%
rename from test/couch_seqs_b64url_tests.erl
rename to test/b64url_tests.erl
index aea784f..5b01606 100644
--- a/test/couch_seqs_b64url_tests.erl
+++ b/test/b64url_tests.erl
@@ -1,4 +1,4 @@
--module(couch_seqs_b64url_tests).
+-module(b64url_tests).
 -compile(export_all).
 
 -include_lib("proper/include/proper.hrl").
@@ -6,7 +6,7 @@
 
 
 table_test_() ->
-    ?_assertEqual(ok, couch_seqs_b64url:check_tables()).
+    ?_assertEqual(ok, b64url:check_tables()).
 
 
 proper_test_() ->
@@ -21,7 +21,7 @@ proper_test_() ->
 prop_encode_binary() ->
     ?FORALL(Bin, binary(), begin
         A = couch_encode_base64url(Bin),
-        B = couch_seqs_b64url:encode(Bin),
+        B = b64url:encode(Bin),
         A == B
     end).
 
@@ -29,7 +29,7 @@ prop_encode_binary() ->
 prop_encode_iolist() ->
     ?FORALL(IoList, shallow_iolist(), begin
         A = couch_encode_base64url(iolist_to_binary(IoList)),
-        B = couch_seqs_b64url:encode(IoList),
+        B = b64url:encode(IoList),
         A == B
     end).
 
@@ -37,7 +37,7 @@ prop_encode_iolist() ->
 prop_decode_binary() ->
     ?FORALL(Bin, binary(), begin
         B64UrlBin = couch_encode_base64url(Bin),
-        Dec = couch_seqs_b64url:decode(B64UrlBin),
+        Dec = b64url:decode(B64UrlBin),
         Dec == Bin
     end).
 
@@ -45,14 +45,14 @@ prop_decode_binary() ->
 prop_decode_iolist() ->
     ?FORALL(IoList, shallow_b64_iolist(), begin
         A = couch_decode_base64url(iolist_to_binary(IoList)),
-        B = couch_seqs_b64url:decode(IoList),
+        B = b64url:decode(IoList),
         A == B
     end).
 
 
 prop_decode_binary_error() ->
     ?FORALL({ErrBin, BlockPos}, bad_binary(), begin
-        Dec = couch_seqs_b64url:decode(ErrBin),
+        Dec = b64url:decode(ErrBin),
         Dec == {error, {bad_block, BlockPos}}
     end).
 
@@ -60,7 +60,7 @@ prop_decode_binary_error() ->
 prop_decode_bad_length() ->
     ?FORALL(Bin, bad_len_binary(), begin
         try
-            couch_seqs_b64url:decode(Bin),
+            b64url:decode(Bin),
             false
         catch error:badarg ->
             true


[couchdb-b64url] 28/31: Merge branch 'rand-compat'

Posted by ja...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jaydoane pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/couchdb-b64url.git

commit 71e064f0722b6a9449354af8e71b898d7e94d215
Merge: 2656839 686ebd0
Author: Nick Vatamaniuc <va...@apache.org>
AuthorDate: Thu Oct 5 13:21:34 2017 -0400

    Merge branch 'rand-compat'
    
    Fixes #6

 rebar.config           | 15 +++++++++++--
 test/b64url_rand.erl   | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++
 test/b64url_tests.erl  | 27 ++++++++++++------------
 test/benchmark.escript |  4 ++--
 4 files changed, 86 insertions(+), 17 deletions(-)


[couchdb-b64url] 23/31: Integrate with Travis CI

Posted by ja...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jaydoane pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/couchdb-b64url.git

commit 319fc604235ab1fde37047b38a432450161db750
Author: Alexander Shorin <kx...@apache.org>
AuthorDate: Fri Jul 24 05:20:50 2015 +0300

    Integrate with Travis CI
---
 .travis.yml | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..99e6cb3
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,10 @@
+language: erlang
+
+otp_release:
+   - 17.5
+   - 17.4
+   - 17.1
+   - 17.0
+   - R16B03-1
+   - R14B04
+   - R14B02


[couchdb-b64url] 26/31: Merge branch 'remove-deprecated-rand-and-crypto'

Posted by ja...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jaydoane pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/couchdb-b64url.git

commit 26568397ae6d84943503c22d33c3fe24930241da
Merge: 6895652 afa766f
Author: Nick Vatamaniuc <va...@apache.org>
AuthorDate: Mon Oct 2 15:26:25 2017 -0400

    Merge branch 'remove-deprecated-rand-and-crypto'
    
    Fixes #5

 test/b64url_tests.erl  | 36 ++++++++++++++----------------------
 test/benchmark.escript |  9 +++------
 2 files changed, 17 insertions(+), 28 deletions(-)


[couchdb-b64url] 25/31: Replace deprecated random module and crypto:rand_bytes calls

Posted by ja...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jaydoane pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/couchdb-b64url.git

commit afa766fd48fd5d5b34d684600e7a1d749a6e9dea
Author: Nick Vatamaniuc <va...@apache.org>
AuthorDate: Mon Oct 2 13:49:54 2017 -0400

    Replace deprecated random module and crypto:rand_bytes calls
---
 test/b64url_tests.erl  | 36 ++++++++++++++----------------------
 test/benchmark.escript |  9 +++------
 2 files changed, 17 insertions(+), 28 deletions(-)

diff --git a/test/b64url_tests.erl b/test/b64url_tests.erl
index b3d7779..7b21459 100644
--- a/test/b64url_tests.erl
+++ b/test/b64url_tests.erl
@@ -64,16 +64,8 @@ decode_bad_length_test() ->
 
 
 gen_binary() ->
-    case get(random_seed) of
-        undefined ->
-            random:seed(os:timestamp());
-        _ ->
-            ok
-    end,
-    % -1 here so that zero is a possibility
-    Length = random:uniform(?MAX_SIZE) - 1,
-    Bytes = tl([random:uniform(256)-1 || _ <- lists:seq(0, Length)]),
-    list_to_binary(Bytes).
+    Length = crypto:rand_uniform(0, ?MAX_SIZE),
+    crypto:strong_rand_bytes(Length).
 
 
 shallow_iolist() ->
@@ -93,31 +85,31 @@ bad_len_binary() ->
 
 
 to_iolist(<<>>) ->
-    case random:uniform(2) of
-        1 -> <<>>;
-        2 -> [<<>>]
+    case crypto:rand_uniform(0,2) of
+        0 -> <<>>;
+        1 -> [<<>>]
     end;
 to_iolist(B) when is_binary(B), size(B) > 0 ->
-    S = random:uniform(size(B)),
+    S = crypto:rand_uniform(1, size(B) + 1),
     <<First:S/binary, Second/binary>> = B,
-    case random:uniform(3) of
-        1 ->
+    case crypto:rand_uniform(0, 3) of
+        0 ->
             [to_iolist(First), Second];
-        2 ->
+        1 ->
             [First, to_iolist(Second)];
-        3 ->
+        2 ->
             [First, Second]
     end.
 
 
 insert_error(B) when is_binary(B), size(B) < 2 ->
-    case random:uniform(2) of
-        1 -> {<<122, 255>>, 0};
-        2 -> {<<122, 122, 255>>, 0}
+    case crypto:rand_uniform(0, 2) of
+        0 -> {<<122, 255>>, 0};
+        1 -> {<<122, 122, 255>>, 0}
     end;
 insert_error(B) when is_binary(B) ->
     B64 = couch_encode_base64url(B),
-    S = random:uniform(size(B64)-1),
+    S = crypto:rand_uniform(0, size(B64)),
     <<First:S/binary, _:1/binary, Second/binary>> = B64,
     {<<First:S/binary, 255, Second/binary>>, 4 * (S div 4)}.
 
diff --git a/test/benchmark.escript b/test/benchmark.escript
index 5c2bdd5..e1a1f70 100755
--- a/test/benchmark.escript
+++ b/test/benchmark.escript
@@ -97,7 +97,6 @@ spawn_workers(NumWorkers, St) ->
 
 
 run_worker(St) ->
-    random:seed(erlang:now()),
     receive
         start -> ok
     end,
@@ -116,8 +115,8 @@ run_worker(St, Started) ->
 
 
 do_round_trip(St) ->
-    Size = St#st.minsize + random:uniform(St#st.maxsize - St#st.minsize),
-    Data = crypto:rand_bytes(Size),
+    Size = crypto:rand_uniform(St#st.minsize,  St#st.maxsize + 1),
+    Data = crypto:strong_rand_bytes(Size),
     Encoded = (St#st.module):encode(Data),
     Data = (St#st.module):decode(Encoded),
     St#st{total_bytes=St#st.total_bytes+Size}.
@@ -137,10 +136,8 @@ decode(Url64) ->
     Padding = list_to_binary(lists:duplicate((4 - size(Url2) rem 4) rem 4, $=)),
     base64:decode(<<Url2/binary, Padding/binary>>).
 
-
 randomize(List) ->
-    random:seed(erlang:now()),
-    List0 = [{random:uniform(), L} || L <- List],
+    List0 = [{crypto:rand_uniform(0, 1 bsl 32), L} || L <- List],
     List1 = lists:sort(List0),
     [L || {_, L} <- List1].
 


[couchdb-b64url] 17/31: Fix LICENSE indention

Posted by ja...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jaydoane pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/couchdb-b64url.git

commit 772be54d058a940791aed688226c871a69cf73cf
Author: Alexander Shorin <kx...@apache.org>
AuthorDate: Mon Dec 22 18:09:59 2014 +0300

    Fix LICENSE indention
---
 LICENSE | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/LICENSE b/LICENSE
index e06d208..94ad231 100644
--- a/LICENSE
+++ b/LICENSE
@@ -1,4 +1,5 @@
-Apache License
+
+                                 Apache License
                            Version 2.0, January 2004
                         http://www.apache.org/licenses/