From 42338cd6f1c39ff3852ea66ad6b3d51493ec4b9c Mon Sep 17 00:00:00 2001 From: "David M. Syzdek" Date: Tue, 24 May 2022 22:48:42 -0800 Subject: urlencoding: adding utility --- ChangeLog | 5 +- Makefile.am | 14 ++- acinclude.m4 | 25 ++++++ configure.ac | 2 + src/urlencoding.c | 256 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 299 insertions(+), 3 deletions(-) create mode 100644 src/urlencoding.c diff --git a/ChangeLog b/ChangeLog index c3f9ea9..2a54297 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,9 +1,10 @@ DMS Tools and Utilities -Copyright (C) 2008, 2009, 2010, 2021 David M. Syzdek +Copyright (C) 2008, 2009, 2010, 2021, 2022 David M. Syzdek 1.7 - - urldesc: adding utility (syzdek) + - urldesc: adding utility (syzdek) + - urlencoding: adding utility (syzdek) 1.6 2021/10/17 (syzdek) - numconvert: fixing output for char (syzdek) diff --git a/Makefile.am b/Makefile.am index 8be7bc0..3ba0e89 100644 --- a/Makefile.am +++ b/Makefile.am @@ -86,7 +86,9 @@ EXTRA = src/bindump \ src/posixregex \ src/recurse \ src/suicide \ - src/typedef-info + src/typedef-info \ + src/urldesc \ + src/urlencoding # lists @@ -257,6 +259,16 @@ if WANT_URLDESC endif +# macros for src/urlencoding +src_urlencoding_DEPENDENCIES = $(lib_LTLIBRARIES) lib/libbindle.a Makefile +src_urlencoding_CPPFLAGS = -DPROGRAM_NAME="\"urlencoding\"" $(AM_CPPFLAGS) +src_urlencoding_LDADD = lib/libbindle.a +src_urlencoding_SOURCES = $(noinst_HEADERS) src/urlencoding.c +if WANT_URLENCODING + bin_PROGRAMS += src/urlencoding +endif + + # Makefile includes GIT_PACKAGE_VERSION_DIR=include SUBST_EXPRESSIONS = -e 's,[@]RELEASE_MONTH[@],@RELEASE_MONTH@,g' diff --git a/acinclude.m4 b/acinclude.m4 index fa72812..104896d 100644 --- a/acinclude.m4 +++ b/acinclude.m4 @@ -456,4 +456,29 @@ AC_DEFUN([AC_DMS_TOOL_URLDESC],[dnl ])dnl +# AC_DMS_TOOL_URLENCODING +# ______________________________________________________________________________ +AC_DEFUN([AC_DMS_TOOL_URLENCODING],[dnl + + AC_REQUIRE([AC_DMS_TOOL_ALL]) + + enableval="" + AC_ARG_ENABLE( + urlencoding, + [AS_HELP_STRING([--enable-urlencoding], [enable building urlencoding])], + [ EURLENCODING=$enableval ], + [ EURLENCODING=$enableval ] + ) + + if test "x${EURLENCODING}" == "x";then + EURLENCODING="${EALL}" + fi + if test "x${EURLENCODING}" == "xauto";then + EURLENCODING=yes + fi + + AM_CONDITIONAL([WANT_URLENCODING], [test "$EURLENCODING" = "yes"]) +])dnl + + # end of M4 file diff --git a/configure.ac b/configure.ac index 6796549..b5d7101 100644 --- a/configure.ac +++ b/configure.ac @@ -198,6 +198,7 @@ AC_DMS_TOOL_RECURSE AC_DMS_TOOL_SUICIDE AC_DMS_TOOL_TYPEDEF_INFO AC_DMS_TOOL_URLDESC +AC_DMS_TOOL_URLENCODING # Creates outputs AC_CONFIG_FILES([Makefile]) @@ -221,6 +222,7 @@ AC_MSG_NOTICE([ build recurse $ERECURSE]) AC_MSG_NOTICE([ build suicide $ESUICIDE]) AC_MSG_NOTICE([ build typedef-info $ETYPEDEF_INFO]) AC_MSG_NOTICE([ build urldesc $EURLDESC]) +AC_MSG_NOTICE([ build urlencoding $EURLENCODING]) AC_MSG_NOTICE([ ]) AC_MSG_NOTICE([ run `make all']) AC_MSG_NOTICE([ ]) diff --git a/src/urlencoding.c b/src/urlencoding.c new file mode 100644 index 0000000..69079bc --- /dev/null +++ b/src/urlencoding.c @@ -0,0 +1,256 @@ +/* + * DMS Tools and Utilities + * Copyright (C) 2022 David M. Syzdek . + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of David M. Syzdek nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DAVID M. SYZDEK BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ +#define _SRC_URLENCODING_C 1 + +/////////////// +// // +// Headers // +// // +/////////////// + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + + +/////////////////// +// // +// Definitions // +// // +/////////////////// + +#ifndef PROGRAM_NAME +#define PROGRAM_NAME "urlencoding" +#endif +#ifndef PACKAGE_BUGREPORT +#define PACKAGE_BUGREPORT "david@syzdek.net" +#endif +#ifndef PACKAGE_COPYRIGHT +#define PACKAGE_COPYRIGHT "" +#endif +#ifndef PACKAGE_NAME +#define PACKAGE_NAME "" +#endif +#ifndef PACKAGE_VERSION +#define PACKAGE_VERSION "" +#endif + +#define MY_OPT_ENCODE 0x00000001UL +#define MY_OPT_DECODE 0x00000002UL +#define MY_OPT_VERBOSE 0x00010000UL +#define MY_OPT_QUIET 0x00020000UL + + +////////////////// +// // +// Prototypes // +// // +////////////////// + +int +main( + int argc, + char * argv[] ); + + +static int +my_error( + unsigned opts, + const char * fmt, + ... ); + + +///////////////// +// // +// Functions // +// // +///////////////// + +int +main( + int argc, + char * argv[] ) +{ + int c; + int opt_index; + unsigned opts; + char * buff; + void * ptr; + size_t size; + + // getopt options + static char short_opt[] = "dehVvqs"; + static struct option long_opt[] = + { + {"decode", no_argument, NULL, 'd' }, + {"encode", no_argument, NULL, 'e' }, + {"help", no_argument, NULL, 'h' }, + {"quiet", no_argument, NULL, 'q' }, + {"silent", no_argument, NULL, 'q' }, + {"version", no_argument, NULL, 'V' }, + {"verbose", no_argument, NULL, 'v' }, + { NULL, 0, NULL, 0 } + }; + + opts = 0; + + while((c = getopt_long(argc, argv, short_opt, long_opt, &opt_index)) != -1) + { + switch(c) + { + case -1: /* no more arguments */ + case 0: /* long options toggles */ + break; + + case 'd': + opts |= MY_OPT_DECODE; + opts &= ~MY_OPT_ENCODE; + break; + + case 'e': + opts |= MY_OPT_ENCODE; + opts &= ~MY_OPT_DECODE; + break; + + case 'h': + printf("Usage: %s [OPTIONS] string1 [ string2 [ ... [ stringN ] ] ]\n", PROGRAM_NAME); + printf("OPTIONS:\n"); + printf(" -d, --decode decode string\n"); + printf(" -e, --encode encode string\n"); + printf(" -h, --help print this help and exit\n"); + printf(" -q, --quiet, --silent do not print messages\n"); + printf(" -V, --version print version number and exit\n"); + printf(" -v, --verbose print verbose messages\n"); + printf("\n"); + return(0); + + case 'q': + opts |= MY_OPT_QUIET; + opts &= ~MY_OPT_VERBOSE; + break; + + case 'V': + printf("%s (%s) %s\n", PACKAGE_NAME, PACKAGE_NAME, PACKAGE_VERSION); + return(0); + + case 'v': + opts |= MY_OPT_VERBOSE; + opts &= ~MY_OPT_QUIET; + break; + + case '?': + fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME); + return(1); + + default: + fprintf(stderr, "%s: unrecognized option `--%c'\n", PROGRAM_NAME, c); + fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME); + return(1); + }; + }; + + if (optind == argc) + { + fprintf(stderr, "%s: missing required argument\n", PROGRAM_NAME); + fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME); + return(1); + }; + + // loop through URLs + buff = NULL; + while (optind < argc) + { + if ((opts & MY_OPT_DECODE)) + { + size = strlen(argv[optind]) + 1; + if ((ptr = realloc(buff, size)) == NULL) + { + my_error(opts, "out of virtual memory"); + free(buff); + return(1); + }; + buff = ptr; + dmstools_url_decode(argv[optind], buff, size); + printf("%s\n", buff); + } else + { + size = dmstools_url_encode_len(argv[optind]); + if ((ptr = realloc(buff, size)) == NULL) + { + my_error(opts, "out of virtual memory"); + free(buff); + return(1); + }; + buff = ptr; + dmstools_url_encode(argv[optind], buff, size); + printf("%s\n", buff); + }; + optind++; + }; + + free(buff); + + return(0); +} + + +int +my_error( + unsigned opts, + const char * fmt, + ... ) +{ + va_list args; + if ((opts & MY_OPT_QUIET)) + return(1); + fprintf(stderr, "%s: ", PROGRAM_NAME); + va_start(args, fmt); + vfprintf(stderr, fmt, args); + va_end(args); + fprintf(stderr, "\n"); + return(1); +} + + +// end of source file -- cgit v1.2.3