diff options
| author | David M. Syzdek <david@syzdek.net> | 2022-05-24 22:48:42 -0800 |
|---|---|---|
| committer | David M. Syzdek <david@syzdek.net> | 2022-05-24 22:48:42 -0800 |
| commit | 42338cd6f1c39ff3852ea66ad6b3d51493ec4b9c (patch) | |
| tree | bbfa073133013d3de86ba425af2c4bb5f8410fb1 /src | |
| parent | 356490db3cb784a252e4b775412b7434aa6c149e (diff) | |
| download | dmstools-42338cd6f1c39ff3852ea66ad6b3d51493ec4b9c.tar.gz dmstools-42338cd6f1c39ff3852ea66ad6b3d51493ec4b9c.tar.bz2 dmstools-42338cd6f1c39ff3852ea66ad6b3d51493ec4b9c.tar.xz dmstools-42338cd6f1c39ff3852ea66ad6b3d51493ec4b9c.zip | |
urlencoding: adding utility
Diffstat (limited to 'src')
| -rw-r--r-- | src/urlencoding.c | 256 |
1 files changed, 256 insertions, 0 deletions
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 | ||
