You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by va...@apache.org on 2022/10/28 15:30:08 UTC
[couchdb-b64url] branch main updated: Add notice about integration into the main repo
This is an automated email from the ASF dual-hosted git repository.
vatamane pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/couchdb-b64url.git
The following commit(s) were added to refs/heads/main by this push:
new 5287a4e Add notice about integration into the main repo
5287a4e is described below
commit 5287a4e0350ba9c2a7d6e69fc42e81155b0f248c
Author: Nick Vatamaniuc <va...@gmail.com>
AuthorDate: Fri Oct 28 11:28:23 2022 -0400
Add notice about integration into the main repo
---
.asf.yaml | 21 --
.devcontainer/devcontainer.json | 8 -
.github/actions/msvc-dev-cmd | 1 -
.github/actions/setup-beam | 1 -
.github/actions/tmate | 1 -
.github/workflows/ci.yml | 70 -----
.gitignore | 10 -
.gitmodules | 9 -
LICENSE | 203 ------------
Makefile | 46 ---
README.md | 47 +--
c_src/b64url.c | 665 ----------------------------------------
rebar.config | 32 --
rebar.lock | 1 -
src/b64url.app.src | 18 --
src/b64url.erl | 88 ------
test/b64url_tests.erl | 144 ---------
test/benchmark.escript | 165 ----------
18 files changed, 2 insertions(+), 1528 deletions(-)
diff --git a/.asf.yaml b/.asf.yaml
deleted file mode 100644
index cc29d3d..0000000
--- a/.asf.yaml
+++ /dev/null
@@ -1,21 +0,0 @@
-github:
- description: "URL-safe Base64 encoder"
- labels:
- - couchdb
- - erlang
- - nif
- features:
- issues: true
- enabled_merge_buttons:
- squash: true
- rebase: true
- merge: true
- protected_branches:
- main:
- required_status_checks:
- strict: true
-
-notifications:
- commits: commits@couchdb.apache.org
- issues: notifications@couchdb.apache.org
- pullrequests: notifications@couchdb.apache.org
diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json
deleted file mode 100644
index c69d4b7..0000000
--- a/.devcontainer/devcontainer.json
+++ /dev/null
@@ -1,8 +0,0 @@
-{
- "image": "erlang:24",
-
- "extensions": [
- "erlang-ls.erlang-ls",
- "ms-vscode.cpptools"
- ]
-}
diff --git a/.github/actions/msvc-dev-cmd b/.github/actions/msvc-dev-cmd
deleted file mode 160000
index 9f8ae83..0000000
--- a/.github/actions/msvc-dev-cmd
+++ /dev/null
@@ -1 +0,0 @@
-Subproject commit 9f8ae839b01883414208f29e3e24524387f48e1f
diff --git a/.github/actions/setup-beam b/.github/actions/setup-beam
deleted file mode 160000
index c255c7e..0000000
--- a/.github/actions/setup-beam
+++ /dev/null
@@ -1 +0,0 @@
-Subproject commit c255c7e97f15c92985793fe937175930f5c056ca
diff --git a/.github/actions/tmate b/.github/actions/tmate
deleted file mode 160000
index 80a9fae..0000000
--- a/.github/actions/tmate
+++ /dev/null
@@ -1 +0,0 @@
-Subproject commit 80a9fae9f2f90408784fb3cfbc192b2de04b7432
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
deleted file mode 100644
index cd3b777..0000000
--- a/.github/workflows/ci.yml
+++ /dev/null
@@ -1,70 +0,0 @@
-name: CI
-
-on:
- push:
- branches: [ main ]
- pull_request:
-
-permissions:
- contents: read
-
-jobs:
-
- matrix_build:
- strategy:
- matrix:
- otp-version: ['22', '23', '24']
- # erlef/setup-beam action does not support macos yet
- os: [ubuntu-latest, windows-latest]
- rebar3-version: ['3.17']
- include:
- - otp-version: '21'
- os: ubuntu-latest
- rebar3-version: '3.15.2'
- - otp-version: '21'
- os: windows-latest
- rebar3-version: '3.15.2'
- - otp-version: '20'
- os: ubuntu-latest
- rebar3-version: '3.15.2'
-
- runs-on: ${{ matrix.os }}
- env:
- # Define to something other than empty string to enable verbose rebar3 logging
- DEBUG: ''
- steps:
- - name: Check out repository code
- uses: actions/checkout@v2
- with:
- persist-credentials: false
- submodules: recursive
- - name: Setup Erlang
- id: setup-beam
- uses: ./.github/actions/setup-beam
- with:
- otp-version: ${{ matrix.otp-version }}
- rebar3-version: ${{ matrix.rebar3-version }}
- - name: Setup MSVC toolchain
- if: ${{ matrix.os == 'windows-latest' }}
- uses: ./.github/actions/msvc-dev-cmd
- - name: Check coding style with erlfmt
- if: ${{ matrix.os == 'ubuntu-latest' }}
- run: rebar3 fmt --check
- continue-on-error: true
- - name: Compile
- run: rebar3 compile
- - name: EUnit tests
- run: rebar3 eunit
- - name: Restore dialyzer PLT from cache
- uses: actions/cache@v2
- with:
- path: ~/.cache/rebar3/rebar3_*_plt
- key: ${{ runner.os }}-${{ steps.setup-beam.outputs.otp-version }}
- - name: Run dialyzer analysis
- run: rebar3 as test dialyzer
- - name: Setup tmate session on job failure
- uses: ./.github/actions/tmate
- if: ${{ failure() }}
- with:
- limit-access-to-actor: true
-
diff --git a/.gitignore b/.gitignore
deleted file mode 100644
index a3fd77f..0000000
--- a/.gitignore
+++ /dev/null
@@ -1,10 +0,0 @@
-.eunit/
-c_src/*.o
-ebin/
-priv/
-.rebar/
-vc110.pdb
-compile_commands.json
-
-_build/
-c_src/*.d
\ No newline at end of file
diff --git a/.gitmodules b/.gitmodules
deleted file mode 100644
index d8e3f24..0000000
--- a/.gitmodules
+++ /dev/null
@@ -1,9 +0,0 @@
-[submodule ".github/actions/tmate"]
- path = .github/actions/tmate
- url = https://github.com/mxschmitt/action-tmate
-[submodule ".github/actions/msvc-dev-cmd"]
- path = .github/actions/msvc-dev-cmd
- url = https://github.com/ilammy/msvc-dev-cmd
-[submodule ".github/actions/setup-beam"]
- path = .github/actions/setup-beam
- url = https://github.com/erlef/setup-beam
diff --git a/LICENSE b/LICENSE
deleted file mode 100644
index 94ad231..0000000
--- a/LICENSE
+++ /dev/null
@@ -1,203 +0,0 @@
-
- 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/Makefile b/Makefile
deleted file mode 100644
index 754165e..0000000
--- a/Makefile
+++ /dev/null
@@ -1,46 +0,0 @@
-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/ $<
diff --git a/README.md b/README.md
index c3a3497..c75fbcd 100644
--- a/README.md
+++ b/README.md
@@ -1,46 +1,3 @@
-# Base64 encoder with URL-safe scheme
+## NOTE ##
-[![CI](https://github.com/apache/couchdb-b64url/actions/workflows/ci.yml/badge.svg)](https://github.com/apache/couchdb-b64url/actions/workflows/ci.yml)
-
-This is a simple NIF that is responsible for quickly encoding and
-decoding Base64 URL values:
-
-```erlang
-1> Thing = b64url:encode("Hello, CouchDB!").
-<<"SGVsbG8sIENvdWNoREIh">>
-2> b64url:decode(Thing).
-<<"Hello, CouchDB!">>
-```
-
-## Performance
-
-This implementation is significantly faster than the Erlang version it replaced
-in CouchDB. The `benchmark.escript` file contains the original implementation
-(using regular expressions to replace unsafe characters in the output of the
-`base64` module) and can be used to compare the two for strings of various
-lengths. For example:
-
-```
-ERL_LIBS=_build/default/lib/b64url/ ./test/benchmark.escript 4 10 100 30
-erl : 75491270 bytes / 30 seconds = 2516375.67 bps
-nif : 672299342 bytes / 30 seconds = 22409978.07 bps
-```
-
-This test invocation spawns four workers that generate random strings between 10
-and 100 bytes in length and then perform an encode/decode on them in a tight
-loop for 30 seconds, and then reports the aggregate encoded data volume. Note
-that the generator overhead (`crypto:strong_rand_bytes/1`) is included in these
-results, so the relative difference in encoder throughput is rather larger than
-what's reported here.
-
-## Timeslice Consumption
-
-NIF implementations must take care to avoid doing [lengthy
-work](https://www.erlang.org/doc/man/erl_nif.html#lengthy_work) in a single
-invocation. This library will yield back to the Erlang VM as needed when
-operating on a large string, maintaining a partial result until it can resume
-operation. The current implementation uses a conservative heuristic that
-estimates 64 bytes of encoding / decoding to consume 1% of a timeslice, so input
-strings shorter than ~6k should be encoded / decoded within a single invocation,
-and the library should not adversely affect the responsiveness of the VM in any
-way.
+Application was moved to the [main repository](https://github.com/apache/couchdb.git)
diff --git a/c_src/b64url.c b/c_src/b64url.c
deleted file mode 100644
index d9e2e04..0000000
--- a/c_src/b64url.c
+++ /dev/null
@@ -1,665 +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.
-
-#include <assert.h>
-#include <string.h>
-
-#include "erl_nif.h"
-
-#ifdef _WIN32
-#define INLINE __inline
-#else
-#define INLINE inline
-#endif
-
-
-typedef ERL_NIF_TERM ENTERM;
-
-
-typedef struct
-{
- ENTERM atom_ok;
- ENTERM atom_error;
- ENTERM atom_partial;
-
- ENTERM atom_nomem;
- ENTERM atom_bad_block;
-
- ErlNifResourceType* res_st;
-} b64url_priv;
-
-
-typedef struct
-{
- ErlNifPid pid;
- ErlNifBinary tgt;
- size_t len;
- size_t si;
- size_t ti;
- char res_released;
- char bin_released;
-} b64url_st;
-
-
-typedef enum
-{
- ST_OK,
- ST_ERROR,
- ST_PARTIAL
-} b64url_status;
-
-
-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
- 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 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
- 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, b64url_priv* priv, ENTERM value)
-{
- return enif_make_tuple2(env, priv->atom_ok, value);
-}
-
-
-static INLINE ENTERM
-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, b64url_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, b64url_priv* priv, ENTERM value)
-{
- return enif_make_tuple2(env, priv->atom_partial, value);
-}
-
-
-static INLINE int
-check_pid(ErlNifEnv* env, b64url_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 b64url_st*
-b64url_st_alloc(ErlNifEnv* env, b64url_priv* priv, ErlNifBinary* src, size_t tlen)
-{
- b64url_st* st = enif_alloc_resource(priv->res_st, sizeof(b64url_st));
- if(st == NULL) {
- goto error;
- }
-
- memset(st, '\0', sizeof(b64url_st));
- enif_self(env, &(st->pid));
- st->len = src->size;
- st->si = 0;
- st->ti = 0;
- st->res_released = 0;
- st->bin_released = 0;
-
- if(!enif_alloc_binary(tlen, &(st->tgt))) {
- goto error;
- }
-
- return st;
-
-error:
- if(st != NULL) {
- st->res_released = 1;
- enif_release_resource(st);
- }
- return NULL;
-}
-
-
-static void
-b64url_st_free(ErlNifEnv* env, void* obj)
-{
- b64url_st* st = (b64url_st*) obj;
-
- if(!st->bin_released) {
- enif_release_binary(&(st->tgt));
- }
-}
-
-static ENTERM
-b64url_st_enc_ret(ErlNifEnv* env, b64url_st* st, int status)
-{
- b64url_priv* priv = (b64url_priv*) enif_priv_data(env);
- ENTERM ret;
-
- if(status == ST_OK) {
- 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 {
- assert(0 == 1 && "invalid status encoder status");
- ret = enif_make_badarg(env);
- }
-
- if(!st->res_released) {
- st->res_released = 1;
- enif_release_resource(st);
- }
-
- return ret;
-}
-
-static ENTERM
-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)));
- st->bin_released = 1;
- } 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);
- }
-
- if(!st->res_released) {
- st->res_released = 1;
- enif_release_resource(st);
- }
-
- return ret;
-}
-
-static int
-load(ErlNifEnv* env, void** priv, ENTERM info)
-{
- int flags = ERL_NIF_RT_CREATE | ERL_NIF_RT_TAKEOVER;
- ErlNifResourceType* res;
-
- b64url_priv* new_priv = (b64url_priv*) enif_alloc(sizeof(b64url_priv));
- if(new_priv == NULL) {
- return 1;
- }
-
- res = enif_open_resource_type(
- env, NULL, "b64url_st", b64url_st_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");
- new_priv->atom_bad_block = make_atom(env, "bad_block");
-
- *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 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;
-
- 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;
- }
- }
- }
-
- 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(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 if(rem != 0) {
- assert(0 == 1 && "Inavlid length calculation");
- }
-
- return ST_OK;
-}
-
-
-static ENTERM
-b64url_encode_init(ErlNifEnv* env, int argc, const ENTERM argv[])
-{
- ErlNifBinary src;
- b64url_priv* priv = (b64url_priv*) enif_priv_data(env);
- b64url_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)) {
- ret = enif_make_badarg(env);
- 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;
- }
-
- st = b64url_st_alloc(env, priv, &src, tlen);
- if(st == NULL) {
- ret = make_error(env, priv, priv->atom_nomem);
- goto error;
- }
-
- status = b64url_encode(env, &src, st);
-
- return b64url_st_enc_ret(env, st, status);
-
-error:
- if(st != NULL) {
- st->res_released = 1;
- enif_release_resource(st);
- }
-
- return ret;
-}
-
-
-static ENTERM
-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) {
- 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, &res)) {
- return enif_make_badarg(env);
- }
-
- st = (b64url_st*) res;
-
- if(!check_pid(env, st)) {
- return enif_make_badarg(env);
- }
-
- if(src.size != st->len) {
- return enif_make_badarg(env);
- }
-
- status = b64url_encode(env, &src, st);
-
- return b64url_st_enc_ret(env, st, 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);
- size_t chunk_start = st->si;
- size_t rem;
- 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;
- }
- }
- }
-
- rem = src->size % 4;
- if(rem == 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(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]];
-
- 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 if(rem != 0) {
- assert(0 == 1 && "invalid source length");
- }
-
- return ST_OK;
-}
-
-
-static ENTERM
-b64url_decode_init(ErlNifEnv* env, int argc, const ENTERM argv[])
-{
- ErlNifBinary src;
- b64url_priv* priv = (b64url_priv*) enif_priv_data(env);
- b64url_st* st = NULL;
- size_t tlen;
- int status;
- ENTERM ret = priv->atom_error;
-
- 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 = b64url_st_alloc(env, priv, &src, tlen);
- if(st == NULL) {
- ret = make_error(env, priv, priv->atom_nomem);
- goto error;
- }
-
- status = b64url_decode(env, &src, st, &ret);
-
- return b64url_st_dec_ret(env, st, status, ret);
-
-error:
- if(st != NULL) {
- st->res_released = 1;
- enif_release_resource(st);
- }
-
- return ret;
-}
-
-
-static ENTERM
-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;
-
- 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, &res)) {
- return enif_make_badarg(env);
- }
-
- st = (b64url_st*) res;
-
- if(!check_pid(env, st)) {
- return enif_make_badarg(env);
- }
-
- if(src.size != st->len) {
- return enif_make_badarg(env);
- }
-
- status = b64url_decode(env, &src, st, &ret);
-
- return b64url_st_dec_ret(env, st, status, ret);
-}
-
-
-static unsigned char CHECK_A2B[64] =
- "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
-
-
-static ENTERM
-b64url_check_tables(ErlNifEnv* env, int argc, const ENTERM argv[])
-{
- b64url_priv* priv = (b64url_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, 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}
-};
-
-
-ERL_NIF_INIT(b64url, funcs, &load, &reload, &upgrade, &unload);
-
-
diff --git a/rebar.config b/rebar.config
deleted file mode 100644
index d9512ae..0000000
--- a/rebar.config
+++ /dev/null
@@ -1,32 +0,0 @@
-{plugins, [
- pc
-]}.
-
-{project_plugins, [
- erlfmt
-]}.
-
-{provider_hooks, [
- {pre, [
- {compile, {pc, compile}},
- {clean, {pc, clean}}
- ]}
-]}.
-
-{port_specs, [
- {"priv/b64url.so", ["c_src/*.c"]}
-]}.
-
-{port_env, [
- % Development compilation
- % {".*", "CFLAGS", "$CFLAGS -g -Wall -Werror -fPIC"}
-
- % Production compilation
- {"(linux|solaris|darwin|freebsd)", "CFLAGS", "$CFLAGS -Wall -Werror -DNDEBUG -O3"},
- {"win32", "CFLAGS", "$CFLAGS /O2 /DNDEBUG /Wall"}
-]}.
-
-{eunit_opts, [
- debug_info,
- verbose
-]}.
diff --git a/rebar.lock b/rebar.lock
deleted file mode 100644
index 57afcca..0000000
--- a/rebar.lock
+++ /dev/null
@@ -1 +0,0 @@
-[].
diff --git a/src/b64url.app.src b/src/b64url.app.src
deleted file mode 100644
index aaf8e9f..0000000
--- a/src/b64url.app.src
+++ /dev/null
@@ -1,18 +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.
-
-{application, b64url, [
- {description, "A small NIF for encoding Base64 URL encoding/decoding."},
- {vsn, git},
- {registered, []},
- {applications, [kernel, stdlib]}
-]}.
diff --git a/src/b64url.erl b/src/b64url.erl
deleted file mode 100644
index 6e6ddac..0000000
--- a/src/b64url.erl
+++ /dev/null
@@ -1,88 +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).
--on_load(init/0).
-
--export([
- encode/1,
- decode/1
-]).
-
-% Internal sanity checks
--export([
- check_tables/0
-]).
-
--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;
- {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
- {error, _} ->
- EbinDir = filename:dirname(code:which(?MODULE)),
- AppPath = filename:dirname(EbinDir),
- filename:join(AppPath, "priv");
- Path ->
- Path
- end,
- erlang:load_nif(filename:join(PrivDir, "b64url"), 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;
- {error, _} = Ret ->
- Ret;
- {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/b64url_tests.erl b/test/b64url_tests.erl
deleted file mode 100644
index 5e8e575..0000000
--- a/test/b64url_tests.erl
+++ /dev/null
@@ -1,144 +0,0 @@
--module(b64url_tests).
-
--include_lib("eunit/include/eunit.hrl").
-
--define(MAX_SIZE, 6401).
--define(NUM_TESTS, 500).
-
-table_test() ->
- ?assertEqual(ok, b64url:check_tables()).
-
-encode_binary_test() ->
- lists:foreach(
- fun(_) ->
- Bin = gen_binary(),
- A = couch_encode_base64url(Bin),
- B = b64url:encode(Bin),
- ?assertEqual(A, B)
- end,
- lists:seq(1, ?NUM_TESTS)
- ).
-
-encode_iolist_test() ->
- lists:foreach(
- fun(_) ->
- IoList = shallow_iolist(),
- A = couch_encode_base64url(iolist_to_binary(IoList)),
- B = b64url:encode(IoList),
- ?assertEqual(A, B)
- end,
- lists:seq(1, ?NUM_TESTS)
- ).
-
-decode_binary_test() ->
- lists:foreach(
- fun(_) ->
- Bin = gen_binary(),
- B64UrlBin = couch_encode_base64url(Bin),
- Dec = b64url:decode(B64UrlBin),
- ?assertEqual(Bin, Dec)
- end,
- lists:seq(1, ?NUM_TESTS)
- ).
-
-decode_iolist_test() ->
- lists:foreach(
- fun(_) ->
- IoList = shallow_b64_iolist(),
- A = couch_decode_base64url(iolist_to_binary(IoList)),
- B = b64url:decode(IoList),
- ?assertEqual(A, B)
- end,
- lists:seq(1, ?NUM_TESTS)
- ).
-
-decode_binary_error_test() ->
- lists:foreach(
- fun(_) ->
- {ErrBin, BlockPos} = bad_binary(),
- Dec = b64url:decode(ErrBin),
- ?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)
- ).
-
-gen_binary() ->
- % -1 to allow for 0 length
- Length = rand:uniform(?MAX_SIZE) - 1,
- crypto:strong_rand_bytes(Length).
-
-shallow_iolist() ->
- to_iolist(gen_binary()).
-
-shallow_b64_iolist() ->
- to_iolist(couch_encode_base64url(gen_binary())).
-
-bad_binary() ->
- insert_error(gen_binary()).
-
-bad_len_binary() ->
- make_bad_len(gen_binary()).
-
-to_iolist(<<>>) ->
- case rand:uniform(2) of
- 1 -> <<>>;
- 2 -> [<<>>]
- end;
-to_iolist(B) when is_binary(B), size(B) > 0 ->
- S = rand:uniform(size(B)),
- <<First:S/binary, Second/binary>> = B,
- case rand:uniform(3) of
- 1 ->
- [to_iolist(First), Second];
- 2 ->
- [First, to_iolist(Second)];
- 3 ->
- [First, Second]
- end.
-
-insert_error(B) when is_binary(B), size(B) < 2 ->
- 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 = rand: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>>.
-
-% 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])),
- 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>>).
diff --git a/test/benchmark.escript b/test/benchmark.escript
deleted file mode 100755
index 00a6f0d..0000000
--- a/test/benchmark.escript
+++ /dev/null
@@ -1,165 +0,0 @@
-#!/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) ->
- 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 + 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),
- 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) ->
- List0 = [{rand: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().
-