From 45ceb4fdf94566a67dbeac28cebb0f3154b843cf Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Tue, 2 Mar 2021 17:16:18 +0100 Subject: add base32 encoder/decoder --- debian/libgnunet.install | 1 + doc/man/Makefile.am | 1 + doc/man/gnunet-base32.1 | 74 +++++++++++++++++++++++ src/util/.gitignore | 1 + src/util/Makefile.am | 9 ++- src/util/gnunet-base32.c | 152 +++++++++++++++++++++++++++++++++++++++++++++++ src/util/gnunet-scrypt.c | 7 ++- 7 files changed, 241 insertions(+), 4 deletions(-) create mode 100644 doc/man/gnunet-base32.1 create mode 100644 src/util/gnunet-base32.c diff --git a/debian/libgnunet.install b/debian/libgnunet.install index 7cf63853c..dd216d5ba 100644 --- a/debian/libgnunet.install +++ b/debian/libgnunet.install @@ -7,6 +7,7 @@ usr/lib/*/libgnunetutil.so.* usr/share/locale/ usr/share/gnunet/config.d/util.conf usr/bin/gnunet-arm +usr/bin/gnunet-base32 usr/bin/gnunet-bugreport usr/bin/gnunet-config usr/bin/gnunet-ecc diff --git a/doc/man/Makefile.am b/doc/man/Makefile.am index a0283cf44..e7045e8bf 100644 --- a/doc/man/Makefile.am +++ b/doc/man/Makefile.am @@ -50,6 +50,7 @@ man_MANS = \ gnunet-arm.1 \ gnunet-ats.1 \ gnunet-auto-share.1 \ + gnunet-base32.1 \ gnunet-bcd.1 \ gnunet-bugreport.1 \ gnunet-config.1 \ diff --git a/doc/man/gnunet-base32.1 b/doc/man/gnunet-base32.1 new file mode 100644 index 000000000..2e1224279 --- /dev/null +++ b/doc/man/gnunet-base32.1 @@ -0,0 +1,74 @@ +.\" This file is part of GNUnet. +.\" Copyright (C) 2021 GNUnet e.V. +.\" +.\" Permission is granted to copy, distribute and/or modify this document +.\" under the terms of the GNU Free Documentation License, Version 1.3 or +.\" any later version published by the Free Software Foundation; with no +.\" Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A +.\" copy of the license is included in the file +.\" FDL-1.3. +.\" +.\" A copy of the license is also available from the Free Software +.\" Foundation Web site at http://www.gnu.org/licenses/fdl.html}. +.\" +.\" Alternately, this document is also available under the General +.\" Public License, version 3 or later, as published by the Free Software +.\" Foundation. A copy of the license is included in the file +.\" GPL3. +.\" +.\" A copy of the license is also available from the Free Software +.\" Foundation Web site at http://www.gnu.org/licenses/gpl.html +.\" +.\" SPDX-License-Identifier: GPL3.0-or-later OR FDL1.3-or-later +.\" +.Dd March 5, 2021 +.Dt GNUNET-BASE32 1 +.Os +.Sh NAME +.Nm gnunet-base32 +.Nd encode/decode to Crockford base32 encoding +.Sh SYNOPSIS +.Nm +.Op Fl d | -decode +.sp +.Sh DESCRIPTION +.Nm +By default, encodes data read from standard input into +Crockford base32 and writes the result to standard output. +The inverse operation is performed if the option +.Fl --decode +is given. Note that the tool keeps all of the data in +memory and is largely intended for quick debugging and +not for high performance. +.Bl -tag -width indent +.It Fl d | -decode +Run in decode mode instead of in encode mode. +.It Fl h | -help +Print short help on options. +.It Fl v | -version +Print GNUnet version number. +.El +.Sh SEE ALSO +The full documentation for gnunet is maintained as a Texinfo manual. +If the +.Xr info 1 +and gnunet programs are properly installed at your site, the command +.Pp +.Dl info gnunet +.Pp +should give you access to the complete handbook, +.Pp +.Dl info gnunet-c-tutorial +.Pp +will give you access to a tutorial for developers. +.sp +Depending on your installation, this information is also available in +.Xr gnunet 7 and +.Xr gnunet-c-tutorial 7 . +.\".Sh HISTORY +.\".Sh AUTHORS +.Sh BUGS +Report bugs by using +.Lk https://bugs.gnunet.org +or by sending electronic mail to +.Aq Mt gnunet-developers@gnu.org . diff --git a/src/util/.gitignore b/src/util/.gitignore index 8556ee7b8..7c7b7045d 100644 --- a/src/util/.gitignore +++ b/src/util/.gitignore @@ -80,3 +80,4 @@ python27_location perf_malloc perf_mq perf_scheduler +gnunet-base32 diff --git a/src/util/Makefile.am b/src/util/Makefile.am index 33fe26e34..6b9e083a7 100644 --- a/src/util/Makefile.am +++ b/src/util/Makefile.am @@ -152,9 +152,10 @@ libexec_PROGRAMS = \ gnunet-timeout bin_PROGRAMS = \ - gnunet-resolver \ + gnunet-base32 \ gnunet-config \ gnunet-crypto-tvg \ + gnunet-resolver \ $(GNUNET_ECC) \ $(GNUNET_SCRYPT) \ gnunet-uri @@ -203,6 +204,12 @@ gnunet_ecc_LDADD = \ libgnunetutil.la \ $(GN_LIBINTL) -lgcrypt +gnunet_base32_SOURCES = \ + gnunet-base32.c +gnunet_base32_LDADD = \ + libgnunetutil.la \ + $(GN_LIBINTL) + gnunet_scrypt_SOURCES = \ gnunet-scrypt.c gnunet_scrypt_LDADD = \ diff --git a/src/util/gnunet-base32.c b/src/util/gnunet-base32.c new file mode 100644 index 000000000..2c797f56e --- /dev/null +++ b/src/util/gnunet-base32.c @@ -0,0 +1,152 @@ +/* + This file is part of GNUnet. + Copyright (C) 2021 GNUnet e.V. + + GNUnet is free software: you can redistribute it and/or modify it + under the terms of the GNU Affero General Public License as published + by the Free Software Foundation, either version 3 of the License, + or (at your option) any later version. + + GNUnet is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . + + SPDX-License-Identifier: AGPL3.0-or-later + */ + +/** + * @file util/gnunet-base32.c + * @brief tool to encode/decode from/to the Crockford Base32 encoding GNUnet uses + * @author Christian Grothoff + */ +#include "platform.h" +#include "gnunet_util_lib.h" + + +/** + * The main function of gnunet-base32 + * + * @param argc number of arguments from the command line + * @param argv command line arguments + * @return 0 ok, 1 on error + */ +int +main (int argc, + char *const *argv) +{ + int decode = 0; + const struct GNUNET_GETOPT_CommandLineOption options[] = { + GNUNET_GETOPT_option_flag ('d', + "decode", + gettext_noop ("run decoder modus, otherwise runs as encoder"), + &decode), + GNUNET_GETOPT_option_help ("Crockford base32 encoder/decoder"), + GNUNET_GETOPT_option_version (PACKAGE_VERSION), + GNUNET_GETOPT_OPTION_END + }; + int ret; + char *in; + unsigned int in_size; + ssize_t iret; + char *out; + size_t out_size; + + if (GNUNET_OK != + GNUNET_STRINGS_get_utf8_args (argc, argv, + &argc, &argv)) + return 2; + ret = GNUNET_GETOPT_run ("gnunet-base32", + options, + argc, + argv); + if (ret < 0) + return 1; + if (0 == ret) + return 0; + in_size = 0; + in = NULL; + iret = 1; + while (iret > 0) + { + /* read in blocks of 4k */ + char buf[4092]; + + iret = read (0, + buf, + sizeof (buf)); + if (iret < 0) + { + GNUNET_free (in); + return 2; + } + if (iret > 0) + { + if (iret + in_size < in_size) + { + GNUNET_break (0); + GNUNET_free (in); + return 1; + } + GNUNET_array_grow (in, + in_size, + in_size + iret); + memcpy (&in[in_size - iret], + buf, + iret); + } + } + if (decode) + { + /* This formula can overestimate by 1 byte, so we try both + out_size and out_size-1 below */ + out_size = in_size * 5 / 8; + out = GNUNET_malloc (out_size); + if (GNUNET_OK != + GNUNET_STRINGS_string_to_data (in, + in_size, + out, + out_size)) + { + out_size--; + if (GNUNET_OK != + GNUNET_STRINGS_string_to_data (in, + in_size, + out, + out_size)) + { + GNUNET_free (out); + GNUNET_free (in); + return 3; + } + } + } + else + { + out = GNUNET_STRINGS_data_to_string_alloc (in, + in_size); + out_size = strlen (out); + } + { + size_t pos = 0; + + while (pos < out_size) + { + iret = write (1, + &out[pos], + out_size - pos); + if (iret <= 0) + return 4; + pos += iret; + } + } + GNUNET_free (out); + GNUNET_free_nz ((void *) argv); + return 0; +} + + +/* end of gnunet-uri.c */ diff --git a/src/util/gnunet-scrypt.c b/src/util/gnunet-scrypt.c index 5c3dfc12e..fe8b6769f 100644 --- a/src/util/gnunet-scrypt.c +++ b/src/util/gnunet-scrypt.c @@ -290,8 +290,8 @@ run (void *cls, int main (int argc, char *const *argv) { - struct GNUNET_GETOPT_CommandLineOption options[] = - { GNUNET_GETOPT_option_ulong ( + struct GNUNET_GETOPT_CommandLineOption options[] = { + GNUNET_GETOPT_option_ulong ( 'b', "bits", "BITS", @@ -315,7 +315,8 @@ main (int argc, char *const *argv) gettext_noop ( "time to wait between calculations"), &proof_find_delay), - GNUNET_GETOPT_OPTION_END }; + GNUNET_GETOPT_OPTION_END + }; int ret; if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv)) -- cgit v1.2.3