diff options
| -rw-r--r-- | ChangeLog | 5 | ||||
| -rw-r--r-- | Makefile.am | 14 | ||||
| -rw-r--r-- | acinclude.m4 | 25 | ||||
| -rw-r--r-- | configure.ac | 2 | ||||
| -rw-r--r-- | src/urlencoding.c | 256 |
5 files changed, 299 insertions, 3 deletions
| @@ -1,9 +1,10 @@ | |||
| 1 | 1 | ||
| 2 | DMS Tools and Utilities | 2 | DMS Tools and Utilities |
| 3 | Copyright (C) 2008, 2009, 2010, 2021 David M. Syzdek <david@syzdek.net> | 3 | Copyright (C) 2008, 2009, 2010, 2021, 2022 David M. Syzdek <david@syzdek.net> |
| 4 | 4 | ||
| 5 | 1.7 | 5 | 1.7 |
| 6 | - urldesc: adding utility (syzdek) | 6 | - urldesc: adding utility (syzdek) |
| 7 | - urlencoding: adding utility (syzdek) | ||
| 7 | 8 | ||
| 8 | 1.6 2021/10/17 (syzdek) | 9 | 1.6 2021/10/17 (syzdek) |
| 9 | - numconvert: fixing output for char (syzdek) | 10 | - 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 \ | |||
| 86 | src/posixregex \ | 86 | src/posixregex \ |
| 87 | src/recurse \ | 87 | src/recurse \ |
| 88 | src/suicide \ | 88 | src/suicide \ |
| 89 | src/typedef-info | 89 | src/typedef-info \ |
| 90 | src/urldesc \ | ||
| 91 | src/urlencoding | ||
| 90 | 92 | ||
| 91 | 93 | ||
| 92 | # lists | 94 | # lists |
| @@ -257,6 +259,16 @@ if WANT_URLDESC | |||
| 257 | endif | 259 | endif |
| 258 | 260 | ||
| 259 | 261 | ||
| 262 | # macros for src/urlencoding | ||
| 263 | src_urlencoding_DEPENDENCIES = $(lib_LTLIBRARIES) lib/libbindle.a Makefile | ||
| 264 | src_urlencoding_CPPFLAGS = -DPROGRAM_NAME="\"urlencoding\"" $(AM_CPPFLAGS) | ||
| 265 | src_urlencoding_LDADD = lib/libbindle.a | ||
| 266 | src_urlencoding_SOURCES = $(noinst_HEADERS) src/urlencoding.c | ||
| 267 | if WANT_URLENCODING | ||
| 268 | bin_PROGRAMS += src/urlencoding | ||
| 269 | endif | ||
| 270 | |||
| 271 | |||
| 260 | # Makefile includes | 272 | # Makefile includes |
| 261 | GIT_PACKAGE_VERSION_DIR=include | 273 | GIT_PACKAGE_VERSION_DIR=include |
| 262 | SUBST_EXPRESSIONS = -e 's,[@]RELEASE_MONTH[@],@RELEASE_MONTH@,g' | 274 | 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 | |||
| 456 | ])dnl | 456 | ])dnl |
| 457 | 457 | ||
| 458 | 458 | ||
| 459 | # AC_DMS_TOOL_URLENCODING | ||
| 460 | # ______________________________________________________________________________ | ||
| 461 | AC_DEFUN([AC_DMS_TOOL_URLENCODING],[dnl | ||
| 462 | |||
| 463 | AC_REQUIRE([AC_DMS_TOOL_ALL]) | ||
| 464 | |||
| 465 | enableval="" | ||
| 466 | AC_ARG_ENABLE( | ||
| 467 | urlencoding, | ||
| 468 | [AS_HELP_STRING([--enable-urlencoding], [enable building urlencoding])], | ||
| 469 | [ EURLENCODING=$enableval ], | ||
| 470 | [ EURLENCODING=$enableval ] | ||
| 471 | ) | ||
| 472 | |||
| 473 | if test "x${EURLENCODING}" == "x";then | ||
| 474 | EURLENCODING="${EALL}" | ||
| 475 | fi | ||
| 476 | if test "x${EURLENCODING}" == "xauto";then | ||
| 477 | EURLENCODING=yes | ||
| 478 | fi | ||
| 479 | |||
| 480 | AM_CONDITIONAL([WANT_URLENCODING], [test "$EURLENCODING" = "yes"]) | ||
| 481 | ])dnl | ||
| 482 | |||
| 483 | |||
| 459 | # end of M4 file | 484 | # 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 | |||
| 198 | AC_DMS_TOOL_SUICIDE | 198 | AC_DMS_TOOL_SUICIDE |
| 199 | AC_DMS_TOOL_TYPEDEF_INFO | 199 | AC_DMS_TOOL_TYPEDEF_INFO |
| 200 | AC_DMS_TOOL_URLDESC | 200 | AC_DMS_TOOL_URLDESC |
| 201 | AC_DMS_TOOL_URLENCODING | ||
| 201 | 202 | ||
| 202 | # Creates outputs | 203 | # Creates outputs |
| 203 | AC_CONFIG_FILES([Makefile]) | 204 | AC_CONFIG_FILES([Makefile]) |
| @@ -221,6 +222,7 @@ AC_MSG_NOTICE([ build recurse $ERECURSE]) | |||
| 221 | AC_MSG_NOTICE([ build suicide $ESUICIDE]) | 222 | AC_MSG_NOTICE([ build suicide $ESUICIDE]) |
| 222 | AC_MSG_NOTICE([ build typedef-info $ETYPEDEF_INFO]) | 223 | AC_MSG_NOTICE([ build typedef-info $ETYPEDEF_INFO]) |
| 223 | AC_MSG_NOTICE([ build urldesc $EURLDESC]) | 224 | AC_MSG_NOTICE([ build urldesc $EURLDESC]) |
| 225 | AC_MSG_NOTICE([ build urlencoding $EURLENCODING]) | ||
| 224 | AC_MSG_NOTICE([ ]) | 226 | AC_MSG_NOTICE([ ]) |
| 225 | AC_MSG_NOTICE([ run `make all']) | 227 | AC_MSG_NOTICE([ run `make all']) |
| 226 | AC_MSG_NOTICE([ ]) | 228 | 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 @@ | |||
| 1 | /* | ||
| 2 | * DMS Tools and Utilities | ||
| 3 | * Copyright (C) 2022 David M. Syzdek <david@syzdek.net>. | ||
| 4 | * All rights reserved. | ||
| 5 | * | ||
| 6 | * Redistribution and use in source and binary forms, with or without | ||
| 7 | * modification, are permitted provided that the following conditions are | ||
| 8 | * met: | ||
| 9 | * | ||
| 10 | * * Redistributions of source code must retain the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer. | ||
| 12 | * * Redistributions in binary form must reproduce the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer in the | ||
| 14 | * documentation and/or other materials provided with the distribution. | ||
| 15 | * * Neither the name of David M. Syzdek nor the | ||
| 16 | * names of its contributors may be used to endorse or promote products | ||
| 17 | * derived from this software without specific prior written permission. | ||
| 18 | * | ||
| 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS | ||
| 20 | * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | ||
| 21 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| 22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DAVID M. SYZDEK BE LIABLE FOR | ||
| 23 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
| 25 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
| 26 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 29 | * SUCH DAMAGE. | ||
| 30 | */ | ||
| 31 | #define _SRC_URLENCODING_C 1 | ||
| 32 | |||
| 33 | /////////////// | ||
| 34 | // // | ||
| 35 | // Headers // | ||
| 36 | // // | ||
| 37 | /////////////// | ||
| 38 | |||
| 39 | #ifdef HAVE_CONFIG_H | ||
| 40 | #include <config.h> | ||
| 41 | #endif | ||
| 42 | |||
| 43 | #include <stdio.h> | ||
| 44 | #include <string.h> | ||
| 45 | #include <inttypes.h> | ||
| 46 | #include <stdarg.h> | ||
| 47 | #include <getopt.h> | ||
| 48 | #include <arpa/inet.h> | ||
| 49 | #include <sys/types.h> | ||
| 50 | #include <sys/socket.h> | ||
| 51 | #include <stdlib.h> | ||
| 52 | #include <netdb.h> | ||
| 53 | |||
| 54 | #include <bindle_prefix.h> | ||
| 55 | |||
| 56 | |||
| 57 | /////////////////// | ||
| 58 | // // | ||
| 59 | // Definitions // | ||
| 60 | // // | ||
| 61 | /////////////////// | ||
| 62 | |||
| 63 | #ifndef PROGRAM_NAME | ||
| 64 | #define PROGRAM_NAME "urlencoding" | ||
| 65 | #endif | ||
| 66 | #ifndef PACKAGE_BUGREPORT | ||
| 67 | #define PACKAGE_BUGREPORT "david@syzdek.net" | ||
| 68 | #endif | ||
| 69 | #ifndef PACKAGE_COPYRIGHT | ||
| 70 | #define PACKAGE_COPYRIGHT "" | ||
| 71 | #endif | ||
| 72 | #ifndef PACKAGE_NAME | ||
| 73 | #define PACKAGE_NAME "" | ||
| 74 | #endif | ||
| 75 | #ifndef PACKAGE_VERSION | ||
| 76 | #define PACKAGE_VERSION "" | ||
| 77 | #endif | ||
| 78 | |||
| 79 | #define MY_OPT_ENCODE 0x00000001UL | ||
| 80 | #define MY_OPT_DECODE 0x00000002UL | ||
| 81 | #define MY_OPT_VERBOSE 0x00010000UL | ||
| 82 | #define MY_OPT_QUIET 0x00020000UL | ||
| 83 | |||
| 84 | |||
| 85 | ////////////////// | ||
| 86 | // // | ||
| 87 | // Prototypes // | ||
| 88 | // // | ||
| 89 | ////////////////// | ||
| 90 | |||
| 91 | int | ||
| 92 | main( | ||
| 93 | int argc, | ||
| 94 | char * argv[] ); | ||
| 95 | |||
| 96 | |||
| 97 | static int | ||
| 98 | my_error( | ||
| 99 | unsigned opts, | ||
| 100 | const char * fmt, | ||
| 101 | ... ); | ||
| 102 | |||
| 103 | |||
| 104 | ///////////////// | ||
| 105 | // // | ||
| 106 | // Functions // | ||
| 107 | // // | ||
| 108 | ///////////////// | ||
| 109 | |||
| 110 | int | ||
| 111 | main( | ||
| 112 | int argc, | ||
| 113 | char * argv[] ) | ||
| 114 | { | ||
| 115 | int c; | ||
| 116 | int opt_index; | ||
| 117 | unsigned opts; | ||
| 118 | char * buff; | ||
| 119 | void * ptr; | ||
| 120 | size_t size; | ||
| 121 | |||
| 122 | // getopt options | ||
| 123 | static char short_opt[] = "dehVvqs"; | ||
| 124 | static struct option long_opt[] = | ||
| 125 | { | ||
| 126 | {"decode", no_argument, NULL, 'd' }, | ||
| 127 | {"encode", no_argument, NULL, 'e' }, | ||
| 128 | {"help", no_argument, NULL, 'h' }, | ||
| 129 | {"quiet", no_argument, NULL, 'q' }, | ||
| 130 | {"silent", no_argument, NULL, 'q' }, | ||
| 131 | {"version", no_argument, NULL, 'V' }, | ||
| 132 | {"verbose", no_argument, NULL, 'v' }, | ||
| 133 | { NULL, 0, NULL, 0 } | ||
| 134 | }; | ||
| 135 | |||
| 136 | opts = 0; | ||
| 137 | |||
| 138 | while((c = getopt_long(argc, argv, short_opt, long_opt, &opt_index)) != -1) | ||
| 139 | { | ||
| 140 | switch(c) | ||
| 141 | { | ||
| 142 | case -1: /* no more arguments */ | ||
| 143 | case 0: /* long options toggles */ | ||
| 144 | break; | ||
| 145 | |||
| 146 | case 'd': | ||
| 147 | opts |= MY_OPT_DECODE; | ||
| 148 | opts &= ~MY_OPT_ENCODE; | ||
| 149 | break; | ||
| 150 | |||
| 151 | case 'e': | ||
| 152 | opts |= MY_OPT_ENCODE; | ||
| 153 | opts &= ~MY_OPT_DECODE; | ||
| 154 | break; | ||
| 155 | |||
| 156 | case 'h': | ||
| 157 | printf("Usage: %s [OPTIONS] string1 [ string2 [ ... [ stringN ] ] ]\n", PROGRAM_NAME); | ||
| 158 | printf("OPTIONS:\n"); | ||
| 159 | printf(" -d, --decode decode string\n"); | ||
| 160 | printf(" -e, --encode encode string\n"); | ||
| 161 | printf(" -h, --help print this help and exit\n"); | ||
| 162 | printf(" -q, --quiet, --silent do not print messages\n"); | ||
| 163 | printf(" -V, --version print version number and exit\n"); | ||
| 164 | printf(" -v, --verbose print verbose messages\n"); | ||
| 165 | printf("\n"); | ||
| 166 | return(0); | ||
| 167 | |||
| 168 | case 'q': | ||
| 169 | opts |= MY_OPT_QUIET; | ||
| 170 | opts &= ~MY_OPT_VERBOSE; | ||
| 171 | break; | ||
| 172 | |||
| 173 | case 'V': | ||
| 174 | printf("%s (%s) %s\n", PACKAGE_NAME, PACKAGE_NAME, PACKAGE_VERSION); | ||
| 175 | return(0); | ||
| 176 | |||
| 177 | case 'v': | ||
| 178 | opts |= MY_OPT_VERBOSE; | ||
| 179 | opts &= ~MY_OPT_QUIET; | ||
| 180 | break; | ||
| 181 | |||
| 182 | case '?': | ||
| 183 | fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME); | ||
| 184 | return(1); | ||
| 185 | |||
| 186 | default: | ||
| 187 | fprintf(stderr, "%s: unrecognized option `--%c'\n", PROGRAM_NAME, c); | ||
| 188 | fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME); | ||
| 189 | return(1); | ||
| 190 | }; | ||
| 191 | }; | ||
| 192 | |||
| 193 | if (optind == argc) | ||
| 194 | { | ||
| 195 | fprintf(stderr, "%s: missing required argument\n", PROGRAM_NAME); | ||
| 196 | fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME); | ||
| 197 | return(1); | ||
| 198 | }; | ||
| 199 | |||
| 200 | // loop through URLs | ||
| 201 | buff = NULL; | ||
| 202 | while (optind < argc) | ||
| 203 | { | ||
| 204 | if ((opts & MY_OPT_DECODE)) | ||
| 205 | { | ||
| 206 | size = strlen(argv[optind]) + 1; | ||
| 207 | if ((ptr = realloc(buff, size)) == NULL) | ||
| 208 | { | ||
| 209 | my_error(opts, "out of virtual memory"); | ||
| 210 | free(buff); | ||
| 211 | return(1); | ||
| 212 | }; | ||
| 213 | buff = ptr; | ||
| 214 | dmstools_url_decode(argv[optind], buff, size); | ||
| 215 | printf("%s\n", buff); | ||
| 216 | } else | ||
| 217 | { | ||
| 218 | size = dmstools_url_encode_len(argv[optind]); | ||
| 219 | if ((ptr = realloc(buff, size)) == NULL) | ||
| 220 | { | ||
| 221 | my_error(opts, "out of virtual memory"); | ||
| 222 | free(buff); | ||
| 223 | return(1); | ||
| 224 | }; | ||
| 225 | buff = ptr; | ||
| 226 | dmstools_url_encode(argv[optind], buff, size); | ||
| 227 | printf("%s\n", buff); | ||
| 228 | }; | ||
| 229 | optind++; | ||
| 230 | }; | ||
| 231 | |||
| 232 | free(buff); | ||
| 233 | |||
| 234 | return(0); | ||
| 235 | } | ||
| 236 | |||
| 237 | |||
| 238 | int | ||
| 239 | my_error( | ||
| 240 | unsigned opts, | ||
| 241 | const char * fmt, | ||
| 242 | ... ) | ||
| 243 | { | ||
| 244 | va_list args; | ||
| 245 | if ((opts & MY_OPT_QUIET)) | ||
| 246 | return(1); | ||
| 247 | fprintf(stderr, "%s: ", PROGRAM_NAME); | ||
| 248 | va_start(args, fmt); | ||
| 249 | vfprintf(stderr, fmt, args); | ||
| 250 | va_end(args); | ||
| 251 | fprintf(stderr, "\n"); | ||
| 252 | return(1); | ||
| 253 | } | ||
| 254 | |||
| 255 | |||
| 256 | // end of source file | ||
