2013-01-15 22:01:37 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* apps/nshlib/nsh_codeccmd.c
|
|
|
|
*
|
|
|
|
* This file is part of NuttX, contributed by Darcy Gong
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 Gregory Nutt. All rights reserved.
|
|
|
|
* Author: Darcy Gong 2012-10-30
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. 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.
|
|
|
|
* 3. Neither the name NuttX 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 THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
#ifdef CONFIG_NETUTILS_CODECS
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sched.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <libgen.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#if defined(CONFIG_NSH_DISABLE_URLENCODE) && defined(CONFIG_NSH_DISABLE_URLDECODE)
|
|
|
|
# undef CONFIG_CODECS_URLCODE
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_CODECS_URLCODE
|
2016-07-11 18:11:18 +02:00
|
|
|
#include "netutils/urldecode.h"
|
2013-01-15 22:01:37 +01:00
|
|
|
#endif
|
|
|
|
|
2017-08-12 20:11:22 +02:00
|
|
|
#if defined(CONFIG_NSH_DISABLE_BASE64ENC) && defined(CONFIG_NSH_DISABLE_BASE64DEC)
|
2013-01-15 22:01:37 +01:00
|
|
|
# undef CONFIG_CODECS_BASE64
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_CODECS_BASE64
|
2016-07-11 18:11:18 +02:00
|
|
|
#include "netutils/base64.h"
|
2013-01-15 22:01:37 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(CONFIG_CODECS_HASH_MD5) && !defined(CONFIG_NSH_DISABLE_MD5)
|
2016-07-11 18:11:18 +02:00
|
|
|
#include "netutils/md5.h"
|
2013-01-15 22:01:37 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "nsh.h"
|
|
|
|
#include "nsh_console.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_NSH_CODECS_BUFSIZE
|
|
|
|
# define CONFIG_NSH_CODECS_BUFSIZE 128
|
|
|
|
#endif
|
|
|
|
|
2017-08-12 20:09:01 +02:00
|
|
|
#undef NEED_CMD_CODECS_PROC
|
|
|
|
#undef HAVE_CODECS_URLENCODE
|
|
|
|
#undef HAVE_CODECS_URLDECODE
|
|
|
|
#undef HAVE_CODECS_BASE64ENC
|
|
|
|
#undef HAVE_CODECS_BASE64DEC
|
|
|
|
#undef HAVE_CODECS_HASH_MD5
|
|
|
|
|
|
|
|
#if defined(CONFIG_CODECS_URLCODE) && !defined(CONFIG_NSH_DISABLE_URLENCODE)
|
|
|
|
# define HAVE_CODECS_URLENCODE 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(CONFIG_CODECS_URLCODE) && !defined(CONFIG_NSH_DISABLE_URLDECODE)
|
|
|
|
# define HAVE_CODECS_URLDECODE 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(CONFIG_CODECS_BASE64) && !defined(CONFIG_NSH_DISABLE_BASE64ENC)
|
|
|
|
# define HAVE_CODECS_BASE64ENC 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(CONFIG_CODECS_BASE64) && !defined(CONFIG_NSH_DISABLE_BASE64DEC)
|
|
|
|
# define HAVE_CODECS_BASE64DEC 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(CONFIG_CODECS_HASH_MD5) && !defined(CONFIG_NSH_DISABLE_MD5)
|
|
|
|
# define HAVE_CODECS_HASH_MD5 1
|
|
|
|
#endif
|
|
|
|
|
2017-08-12 20:11:22 +02:00
|
|
|
#if defined(HAVE_CODECS_URLENCODE) || defined(HAVE_CODECS_URLDECODE) || \
|
|
|
|
defined(HAVE_CODECS_BASE64ENC) || defined(HAVE_CODECS_BASE64DEC) || \
|
|
|
|
defined(HAVE_CODECS_HASH_MD5)
|
2017-08-12 20:09:01 +02:00
|
|
|
# define NEED_CMD_CODECS_PROC 1
|
|
|
|
#endif
|
|
|
|
|
2013-01-15 22:01:37 +01:00
|
|
|
#define CODEC_MODE_URLENCODE 1
|
|
|
|
#define CODEC_MODE_URLDECODE 2
|
|
|
|
#define CODEC_MODE_BASE64ENC 3
|
|
|
|
#define CODEC_MODE_BASE64DEC 4
|
|
|
|
#define CODEC_MODE_HASH_MD5 5
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-01-17 14:29:50 +01:00
|
|
|
typedef void (*codec_callback_t)(FAR char *src, int srclen, FAR char *dest,
|
|
|
|
FAR int *destlen, int mode);
|
2013-01-15 22:01:37 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: urlencode_cb
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-08-12 20:11:22 +02:00
|
|
|
#ifdef HAVE_CODECS_URLENCODE
|
2016-01-17 14:29:50 +01:00
|
|
|
static void urlencode_cb(FAR char *src, int srclen, FAR char *dest,
|
|
|
|
FAR int *destlen, int mode)
|
2013-01-15 22:01:37 +01:00
|
|
|
{
|
2016-01-17 14:29:50 +01:00
|
|
|
urlencode(src, srclen, dest, destlen);
|
2013-01-15 22:01:37 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: urldecode_cb
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-08-12 20:11:22 +02:00
|
|
|
#ifdef HAVE_CODECS_URLDECODE
|
2016-01-17 14:29:50 +01:00
|
|
|
static void urldecode_cb(FAR char *src, int srclen, FAR char *dest,
|
|
|
|
FAR int *destlen, int mode)
|
2013-01-15 22:01:37 +01:00
|
|
|
{
|
2020-04-16 12:36:21 +02:00
|
|
|
urldecode(src, srclen, dest, destlen);
|
2013-01-15 22:01:37 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: b64enc_cb
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-08-12 20:11:22 +02:00
|
|
|
#ifdef HAVE_CODECS_BASE64ENC
|
2016-01-17 14:29:50 +01:00
|
|
|
static void b64enc_cb(FAR char *src, int srclen, FAR char *dest,
|
|
|
|
FAR int *destlen, int mode)
|
2013-01-15 22:01:37 +01:00
|
|
|
{
|
|
|
|
if (mode == 0)
|
|
|
|
{
|
2016-01-17 14:29:50 +01:00
|
|
|
base64_encode((unsigned char *)src, srclen,
|
|
|
|
(unsigned char *)dest, (size_t *)destlen);
|
2013-01-15 22:01:37 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-01-17 14:29:50 +01:00
|
|
|
base64w_encode((unsigned char *)src, srclen,
|
|
|
|
(unsigned char *)dest, (size_t *)destlen);
|
2013-01-15 22:01:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: b64dec_cb
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-08-12 20:11:22 +02:00
|
|
|
#ifdef HAVE_CODECS_BASE64DEC
|
2016-01-17 14:29:50 +01:00
|
|
|
static void b64dec_cb(FAR char *src, int srclen, FAR char *dest,
|
|
|
|
FAR int *destlen, int mode)
|
2013-01-15 22:01:37 +01:00
|
|
|
{
|
|
|
|
if (mode == 0)
|
|
|
|
{
|
2016-01-17 14:29:50 +01:00
|
|
|
base64_decode((unsigned char *)src, srclen,
|
|
|
|
(unsigned char *)dest, (size_t *)destlen);
|
2013-01-15 22:01:37 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-01-17 14:29:50 +01:00
|
|
|
base64w_decode((unsigned char *)src, srclen,
|
2020-04-16 15:28:12 +02:00
|
|
|
(unsigned char *)dest, (size_t *)destlen);
|
2013-01-15 22:01:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: md5_cb
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-08-12 20:11:22 +02:00
|
|
|
#ifdef HAVE_CODECS_HASH_MD5
|
2016-01-17 14:29:50 +01:00
|
|
|
static void md5_cb(FAR char *src, int srclen, FAR char *dest,
|
|
|
|
FAR int *destlen, int mode)
|
2013-01-15 22:01:37 +01:00
|
|
|
{
|
2020-04-16 15:28:12 +02:00
|
|
|
md5_update((MD5_CTX *)dest, (unsigned char *)src, srclen);
|
2013-01-15 22:01:37 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: calc_codec_buffsize
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-08-13 20:25:22 +02:00
|
|
|
#ifdef NEED_CMD_CODECS_PROC
|
2016-01-17 14:29:50 +01:00
|
|
|
static int calc_codec_buffsize(int srclen, uint8_t mode)
|
2013-01-15 22:01:37 +01:00
|
|
|
{
|
|
|
|
switch (mode)
|
|
|
|
{
|
|
|
|
case CODEC_MODE_URLENCODE:
|
2016-01-17 14:29:50 +01:00
|
|
|
return srclen * 3 + 1;
|
|
|
|
|
2013-01-15 22:01:37 +01:00
|
|
|
case CODEC_MODE_URLDECODE:
|
2016-01-17 14:29:50 +01:00
|
|
|
return srclen + 1;
|
|
|
|
|
2013-01-15 22:01:37 +01:00
|
|
|
case CODEC_MODE_BASE64ENC:
|
2016-01-17 14:29:50 +01:00
|
|
|
return ((srclen + 2) / 3 * 4) + 1;
|
|
|
|
|
2013-01-15 22:01:37 +01:00
|
|
|
case CODEC_MODE_BASE64DEC:
|
2016-01-17 14:29:50 +01:00
|
|
|
return (srclen / 4 * 3 + 2) + 1;
|
|
|
|
|
2013-01-15 22:01:37 +01:00
|
|
|
case CODEC_MODE_HASH_MD5:
|
2016-01-17 14:29:50 +01:00
|
|
|
return 32 + 1;
|
|
|
|
|
2013-01-15 22:01:37 +01:00
|
|
|
default:
|
2016-01-17 14:29:50 +01:00
|
|
|
return srclen + 1;
|
2013-01-15 22:01:37 +01:00
|
|
|
}
|
|
|
|
}
|
2017-08-13 20:25:22 +02:00
|
|
|
#endif
|
2013-01-15 22:01:37 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_codecs_proc
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-08-12 20:09:01 +02:00
|
|
|
#ifdef NEED_CMD_CODECS_PROC
|
2020-04-17 04:02:46 +02:00
|
|
|
static int cmd_codecs_proc(FAR struct nsh_vtbl_s *vtbl, int argc,
|
|
|
|
char **argv, uint8_t mode,
|
|
|
|
codec_callback_t func)
|
2013-01-15 22:01:37 +01:00
|
|
|
{
|
2017-08-12 20:11:22 +02:00
|
|
|
#ifdef HAVE_CODECS_HASH_MD5
|
2016-01-17 14:29:50 +01:00
|
|
|
static const unsigned char hexchars[] = "0123456789abcdef";
|
2013-01-15 22:01:37 +01:00
|
|
|
MD5_CTX ctx;
|
|
|
|
unsigned char mac[16];
|
2021-01-11 07:34:58 +01:00
|
|
|
FAR unsigned char *src;
|
2016-01-17 14:29:50 +01:00
|
|
|
FAR char *dest;
|
2013-01-15 22:01:37 +01:00
|
|
|
#endif
|
|
|
|
|
2016-01-17 14:29:50 +01:00
|
|
|
FAR char *localfile = NULL;
|
|
|
|
FAR char *srcbuf = NULL;
|
|
|
|
FAR char *destbuf = NULL;
|
|
|
|
FAR char *fullpath = NULL;
|
|
|
|
FAR const char *fmt;
|
|
|
|
FAR char *sdata;
|
2013-01-15 22:01:37 +01:00
|
|
|
bool badarg = false;
|
2016-01-17 14:29:50 +01:00
|
|
|
bool isfile = false;
|
|
|
|
bool iswebsafe = false;
|
2013-01-15 22:01:37 +01:00
|
|
|
int option;
|
|
|
|
int fd = -1;
|
2016-01-17 14:29:50 +01:00
|
|
|
int buflen = 0;
|
|
|
|
int srclen = 0;
|
2013-01-15 22:01:37 +01:00
|
|
|
int ret = OK;
|
|
|
|
|
|
|
|
/* Get the command options */
|
|
|
|
|
|
|
|
while ((option = getopt(argc, argv, ":fw")) != ERROR)
|
|
|
|
{
|
|
|
|
switch (option)
|
|
|
|
{
|
|
|
|
case 'f':
|
2016-01-17 14:29:50 +01:00
|
|
|
isfile = true;
|
2013-01-15 22:01:37 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
#ifdef CONFIG_CODECS_BASE64
|
|
|
|
case 'w':
|
2016-01-17 14:29:50 +01:00
|
|
|
iswebsafe = true;
|
2013-01-15 22:01:37 +01:00
|
|
|
|
2020-04-17 04:02:46 +02:00
|
|
|
if (!(mode == CODEC_MODE_BASE64ENC ||
|
|
|
|
mode == CODEC_MODE_BASE64DEC))
|
2013-01-15 22:01:37 +01:00
|
|
|
{
|
|
|
|
badarg = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case ':':
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtargrequired, argv[0]);
|
2013-01-15 22:01:37 +01:00
|
|
|
badarg = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '?':
|
|
|
|
default:
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtarginvalid, argv[0]);
|
2013-01-15 22:01:37 +01:00
|
|
|
badarg = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-17 04:02:46 +02:00
|
|
|
/* If a bad argument was encountered, then return without processing
|
|
|
|
* the command
|
|
|
|
*/
|
2013-01-15 22:01:37 +01:00
|
|
|
|
|
|
|
if (badarg)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* There should be exactly on parameter left on the command-line */
|
|
|
|
|
2020-04-16 15:28:12 +02:00
|
|
|
if (optind == argc - 1)
|
2013-01-15 22:01:37 +01:00
|
|
|
{
|
2016-01-17 14:29:50 +01:00
|
|
|
sdata = argv[optind];
|
2013-01-15 22:01:37 +01:00
|
|
|
}
|
|
|
|
else if (optind >= argc)
|
|
|
|
{
|
|
|
|
fmt = g_fmttoomanyargs;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fmt = g_fmtargrequired;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2017-08-12 20:11:22 +02:00
|
|
|
#ifdef HAVE_CODECS_HASH_MD5
|
2013-01-15 22:01:37 +01:00
|
|
|
if (mode == CODEC_MODE_HASH_MD5)
|
|
|
|
{
|
2020-04-16 15:28:12 +02:00
|
|
|
md5_init(&ctx);
|
2013-01-15 22:01:37 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-01-17 14:29:50 +01:00
|
|
|
if (isfile)
|
2013-01-15 22:01:37 +01:00
|
|
|
{
|
|
|
|
/* Get the local file name */
|
|
|
|
|
2016-01-17 14:29:50 +01:00
|
|
|
localfile = sdata;
|
2013-01-15 22:01:37 +01:00
|
|
|
|
|
|
|
/* Get the full path to the local file */
|
|
|
|
|
|
|
|
fullpath = nsh_getfullpath(vtbl, localfile);
|
|
|
|
|
2015-04-20 10:46:21 +02:00
|
|
|
/* Open the local file for reading */
|
2013-01-15 22:01:37 +01:00
|
|
|
|
2015-04-20 10:46:21 +02:00
|
|
|
fd = open(fullpath, O_RDONLY);
|
2013-01-15 22:01:37 +01:00
|
|
|
if (fd < 0)
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "open", NSH_ERRNO);
|
2013-01-15 22:01:37 +01:00
|
|
|
ret = ERROR;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2020-04-16 12:36:21 +02:00
|
|
|
srcbuf = malloc(CONFIG_NSH_CODECS_BUFSIZE + 2);
|
|
|
|
if (!srcbuf)
|
|
|
|
{
|
|
|
|
fmt = g_fmtcmdoutofmemory;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2017-08-12 20:11:22 +02:00
|
|
|
#ifdef HAVE_CODECS_BASE64ENC
|
2013-01-15 22:01:37 +01:00
|
|
|
if (mode == CODEC_MODE_BASE64ENC)
|
|
|
|
{
|
2016-01-17 14:29:50 +01:00
|
|
|
srclen = CONFIG_NSH_CODECS_BUFSIZE / 3 * 3;
|
2013-01-15 22:01:37 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2016-01-17 14:29:50 +01:00
|
|
|
srclen = CONFIG_NSH_CODECS_BUFSIZE;
|
2013-01-15 22:01:37 +01:00
|
|
|
}
|
|
|
|
|
2020-04-16 12:36:21 +02:00
|
|
|
buflen = calc_codec_buffsize(srclen + 2, mode);
|
2016-01-17 14:29:50 +01:00
|
|
|
destbuf = malloc(buflen);
|
2020-04-16 12:36:21 +02:00
|
|
|
if (!destbuf)
|
|
|
|
{
|
|
|
|
fmt = g_fmtcmdoutofmemory;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2014-04-12 21:09:48 +02:00
|
|
|
while (true)
|
2013-01-15 22:01:37 +01:00
|
|
|
{
|
2020-04-16 12:36:21 +02:00
|
|
|
memset(srcbuf, 0, srclen + 2);
|
|
|
|
ret = read(fd, srcbuf, srclen);
|
2013-01-15 22:01:37 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "read", NSH_ERRNO);
|
2013-01-15 22:01:37 +01:00
|
|
|
ret = ERROR;
|
|
|
|
goto exit;
|
|
|
|
}
|
2020-04-16 12:36:21 +02:00
|
|
|
else if (ret == 0)
|
2013-01-15 22:01:37 +01:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-08-12 20:11:22 +02:00
|
|
|
#ifdef HAVE_CODECS_URLDECODE
|
2013-01-15 22:01:37 +01:00
|
|
|
if (mode == CODEC_MODE_URLDECODE)
|
|
|
|
{
|
2020-04-16 12:36:21 +02:00
|
|
|
if (srcbuf[srclen - 1] == '%')
|
2013-01-15 22:01:37 +01:00
|
|
|
{
|
2020-04-16 12:36:21 +02:00
|
|
|
ret += read(fd, &srcbuf[srclen], 2);
|
2013-01-15 22:01:37 +01:00
|
|
|
}
|
2020-04-16 12:36:21 +02:00
|
|
|
else if (srcbuf[srclen - 2] == '%')
|
2013-01-15 22:01:37 +01:00
|
|
|
{
|
2020-04-16 12:36:21 +02:00
|
|
|
ret += read(fd, &srcbuf[srclen], 1);
|
2013-01-15 22:01:37 +01:00
|
|
|
}
|
|
|
|
}
|
2020-04-16 15:28:12 +02:00
|
|
|
|
2013-01-15 22:01:37 +01:00
|
|
|
#endif
|
2016-01-17 14:29:50 +01:00
|
|
|
memset(destbuf, 0, buflen);
|
2013-01-15 22:01:37 +01:00
|
|
|
if (func)
|
|
|
|
{
|
2017-08-12 20:11:22 +02:00
|
|
|
#ifdef HAVE_CODECS_HASH_MD5
|
2013-01-15 22:01:37 +01:00
|
|
|
if (mode == CODEC_MODE_HASH_MD5)
|
|
|
|
{
|
2020-04-16 12:36:21 +02:00
|
|
|
func(srcbuf, ret, (char *)&ctx, &buflen, 0);
|
2013-01-15 22:01:37 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2020-04-16 12:36:21 +02:00
|
|
|
func(srcbuf, ret, destbuf, &buflen, iswebsafe ? 1 : 0);
|
2016-01-17 14:29:50 +01:00
|
|
|
nsh_output(vtbl, "%s", destbuf);
|
2013-01-15 22:01:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-16 12:36:21 +02:00
|
|
|
buflen = calc_codec_buffsize(srclen + 2, mode);
|
2013-01-15 22:01:37 +01:00
|
|
|
}
|
|
|
|
|
2017-08-12 20:11:22 +02:00
|
|
|
#ifdef HAVE_CODECS_HASH_MD5
|
2013-01-15 22:01:37 +01:00
|
|
|
if (mode == CODEC_MODE_HASH_MD5)
|
|
|
|
{
|
2017-08-12 20:09:01 +02:00
|
|
|
int i;
|
|
|
|
|
2020-04-16 15:28:12 +02:00
|
|
|
md5_final(mac, &ctx);
|
2021-01-11 07:34:58 +01:00
|
|
|
src = mac;
|
2016-01-17 14:29:50 +01:00
|
|
|
dest = destbuf;
|
|
|
|
for (i = 0; i < 16; i++, src++)
|
2013-01-15 22:01:37 +01:00
|
|
|
{
|
2016-01-17 14:29:50 +01:00
|
|
|
*dest++ = hexchars[(*src) >> 4];
|
|
|
|
*dest++ = hexchars[(*src) & 0x0f];
|
2013-01-15 22:01:37 +01:00
|
|
|
}
|
|
|
|
|
2016-01-17 14:29:50 +01:00
|
|
|
*dest = '\0';
|
|
|
|
nsh_output(vtbl, "%s\n", destbuf);
|
2013-01-15 22:01:37 +01:00
|
|
|
}
|
2020-04-16 15:28:12 +02:00
|
|
|
|
2013-01-15 22:01:37 +01:00
|
|
|
#endif
|
|
|
|
ret = OK;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-01-17 14:29:50 +01:00
|
|
|
srcbuf = sdata;
|
|
|
|
srclen = strlen(sdata);
|
|
|
|
buflen = calc_codec_buffsize(srclen, mode);
|
|
|
|
destbuf = malloc(buflen);
|
|
|
|
if (!destbuf)
|
2013-01-15 22:01:37 +01:00
|
|
|
{
|
|
|
|
fmt = g_fmtcmdoutofmemory;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2016-01-17 14:29:50 +01:00
|
|
|
memset(destbuf, 0, buflen);
|
2013-01-15 22:01:37 +01:00
|
|
|
if (func)
|
|
|
|
{
|
2017-08-12 20:11:22 +02:00
|
|
|
#ifdef HAVE_CODECS_HASH_MD5
|
2013-01-15 22:01:37 +01:00
|
|
|
if (mode == CODEC_MODE_HASH_MD5)
|
|
|
|
{
|
2017-08-12 20:09:01 +02:00
|
|
|
int i;
|
|
|
|
|
2016-01-17 14:29:50 +01:00
|
|
|
func(srcbuf, srclen, (char *)&ctx, &buflen, 0);
|
2020-04-16 15:28:12 +02:00
|
|
|
md5_final(mac, &ctx);
|
2021-01-11 07:34:58 +01:00
|
|
|
src = mac;
|
2016-01-17 14:29:50 +01:00
|
|
|
dest = destbuf;
|
|
|
|
for (i = 0; i < 16; i++, src++)
|
2013-01-15 22:01:37 +01:00
|
|
|
{
|
2016-01-17 14:29:50 +01:00
|
|
|
*dest++ = hexchars[(*src) >> 4];
|
|
|
|
*dest++ = hexchars[(*src) & 0x0f];
|
2013-01-15 22:01:37 +01:00
|
|
|
}
|
|
|
|
|
2016-01-17 14:29:50 +01:00
|
|
|
*dest = '\0';
|
2013-01-15 22:01:37 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2020-04-16 12:36:21 +02:00
|
|
|
func(srcbuf, srclen, destbuf, &buflen, iswebsafe ? 1 : 0);
|
2013-01-15 22:01:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-16 12:36:21 +02:00
|
|
|
nsh_output(vtbl, "%s\n", destbuf);
|
2016-01-17 14:29:50 +01:00
|
|
|
srcbuf = NULL;
|
2013-01-15 22:01:37 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
if (fd >= 0)
|
|
|
|
{
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fullpath)
|
|
|
|
{
|
|
|
|
free(fullpath);
|
|
|
|
}
|
|
|
|
|
2016-01-17 14:29:50 +01:00
|
|
|
if (srcbuf)
|
2013-01-15 22:01:37 +01:00
|
|
|
{
|
2016-01-17 14:29:50 +01:00
|
|
|
free(srcbuf);
|
2013-01-15 22:01:37 +01:00
|
|
|
}
|
|
|
|
|
2016-01-17 14:29:50 +01:00
|
|
|
if (destbuf)
|
2013-01-15 22:01:37 +01:00
|
|
|
{
|
2016-01-17 14:29:50 +01:00
|
|
|
free(destbuf);
|
2013-01-15 22:01:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
errout:
|
|
|
|
nsh_output(vtbl, fmt, argv[0]);
|
|
|
|
ret = ERROR;
|
|
|
|
goto exit;
|
|
|
|
}
|
2017-08-12 20:09:01 +02:00
|
|
|
#endif
|
2013-01-15 22:01:37 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_urlencode
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-08-12 20:11:22 +02:00
|
|
|
#ifdef HAVE_CODECS_URLENCODE
|
2013-01-15 22:01:37 +01:00
|
|
|
int cmd_urlencode(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
|
|
|
{
|
2020-04-17 04:02:46 +02:00
|
|
|
return cmd_codecs_proc(vtbl, argc, argv, CODEC_MODE_URLENCODE,
|
|
|
|
urlencode_cb);
|
2013-01-15 22:01:37 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_urldecode
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-08-12 20:11:22 +02:00
|
|
|
#ifdef HAVE_CODECS_URLDECODE
|
2013-01-15 22:01:37 +01:00
|
|
|
int cmd_urldecode(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
|
|
|
{
|
2020-04-17 04:02:46 +02:00
|
|
|
return cmd_codecs_proc(vtbl, argc, argv, CODEC_MODE_URLDECODE,
|
|
|
|
urldecode_cb);
|
2013-01-15 22:01:37 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_base64encode
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-08-12 20:11:22 +02:00
|
|
|
#ifdef HAVE_CODECS_BASE64ENC
|
2013-01-15 22:01:37 +01:00
|
|
|
int cmd_base64encode(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
|
|
|
{
|
|
|
|
return cmd_codecs_proc(vtbl, argc, argv, CODEC_MODE_BASE64ENC, b64enc_cb);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_base64decode
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-08-12 20:11:22 +02:00
|
|
|
#ifdef HAVE_CODECS_BASE64DEC
|
2013-01-15 22:01:37 +01:00
|
|
|
int cmd_base64decode(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
|
|
|
{
|
|
|
|
return cmd_codecs_proc(vtbl, argc, argv, CODEC_MODE_BASE64DEC, b64dec_cb);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_md5
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-08-12 20:11:22 +02:00
|
|
|
#ifdef HAVE_CODECS_HASH_MD5
|
2013-01-15 22:01:37 +01:00
|
|
|
int cmd_md5(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
|
|
|
{
|
2016-01-17 14:29:50 +01:00
|
|
|
return cmd_codecs_proc(vtbl, argc, argv, CODEC_MODE_HASH_MD5, md5_cb);
|
2013-01-15 22:01:37 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* CONFIG_NETUTILS_CODECS */
|