Add nuttx to the system framework, which is 10.1.0
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
#
|
||||
# For a description of the syntax of this configuration file,
|
||||
# see the file kconfig-language.txt in the NuttX tools repository.
|
||||
#
|
||||
|
||||
config NETUTILS_CODECS
|
||||
bool "CODEC Library"
|
||||
default n
|
||||
---help---
|
||||
Enables the netutils/code library: Base64 coding, URL coding, MD5.
|
||||
|
||||
if NETUTILS_CODECS
|
||||
|
||||
config CODECS_BASE64
|
||||
bool "Base 64 Support"
|
||||
default n
|
||||
---help---
|
||||
Enables support for the following interfaces: base64_encode(),
|
||||
base64_decode(), base64w_encode(), and base64w_decode(),
|
||||
|
||||
Contributed NuttX by Darcy Gong.
|
||||
|
||||
config CODECS_HASH_MD5
|
||||
bool "MD5 Support"
|
||||
default n
|
||||
---help---
|
||||
Enables support for the following interfaces: md5_init(),
|
||||
md5_update(), md5_final(), md5_transform(), md5_sum() and
|
||||
md5_hash()
|
||||
|
||||
Contributed NuttX by Darcy Gong.
|
||||
|
||||
config CODECS_URLCODE
|
||||
bool "URL Decode Support"
|
||||
default n
|
||||
---help---
|
||||
Enables support for the following interfaces: urlencode() and
|
||||
urldecode()
|
||||
|
||||
Contributed NuttX by Darcy Gong.
|
||||
|
||||
config CODECS_URLCODE_NEWMEMORY
|
||||
bool "URL Allocating Decode Support"
|
||||
default n
|
||||
---help---
|
||||
Enables support for the following interfaces: url_encode() and
|
||||
url_decode()
|
||||
|
||||
Contributed NuttX by Darcy Gong.
|
||||
|
||||
config CODECS_AVR_URLCODE
|
||||
bool "URL Raw Decode Support"
|
||||
default n
|
||||
---help---
|
||||
Enables support for the following interfaces: urlrawdecode() and
|
||||
urlrawencode()
|
||||
|
||||
Contributed NuttX by Darcy Gong.
|
||||
|
||||
endif
|
||||
@@ -0,0 +1,38 @@
|
||||
# apps/netutils/codecs/Make.defs
|
||||
# Adds selected applications to apps/ build
|
||||
#
|
||||
# Copyright (C) 2016 Gregory Nutt. All rights reserved.
|
||||
# Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
############################################################################
|
||||
|
||||
ifeq ($(CONFIG_NETUTILS_CODECS),y)
|
||||
CONFIGURED_APPS += $(APPDIR)/netutils/codecs
|
||||
endif
|
||||
@@ -0,0 +1,40 @@
|
||||
############################################################################
|
||||
# apps/netutils/codecs/Makefile
|
||||
#
|
||||
# Copyright (C) 2012 Gregory Nutt. All rights reserved.
|
||||
# Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
############################################################################
|
||||
|
||||
include $(APPDIR)/Make.defs
|
||||
|
||||
CSRCS = urldecode.c base64.c md5.c
|
||||
|
||||
include $(APPDIR)/Application.mk
|
||||
@@ -0,0 +1,320 @@
|
||||
/****************************************************************************
|
||||
* apps/netutils/codecs/base64.c
|
||||
*
|
||||
* This file is part of the NuttX RTOS:
|
||||
*
|
||||
* Copyright (C) 2012, 2018 Gregory Nutt. All rights reserved.
|
||||
* Author: Darcy Gong
|
||||
*
|
||||
* Reference:
|
||||
*
|
||||
* Base64 encoding/decoding (RFC1341)
|
||||
* Copyright (c) 2005, Jouni Malinen <jkmaline@cc.hut.fi>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License version 2 as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* Alternatively, this software may be distributed under the terms of BSD
|
||||
* license.
|
||||
*
|
||||
* See README and COPYING for more details.
|
||||
*
|
||||
* And is re-released under the NuttX modified BSD license:
|
||||
*
|
||||
* 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 of the Institute 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 INSTITUTE 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 INSTITUTE 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 <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "netutils/base64.h"
|
||||
|
||||
#ifdef CONFIG_CODECS_BASE64
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: base64_tab
|
||||
****************************************************************************/
|
||||
|
||||
static FAR const char *base64_tab(bool websafe)
|
||||
{
|
||||
static FAR const char *_tab =
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
static FAR const char *_tab_w =
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789__";
|
||||
|
||||
return websafe ? _tab_w : _tab;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: _base64_encode
|
||||
*
|
||||
* Description:
|
||||
* Base64 encode
|
||||
*
|
||||
* Caller is responsible for freeing the returned buffer. Returned buffer
|
||||
* is NUL terminated to make it easier to use as a C string. The NUL
|
||||
* terminator is not included in out_len.
|
||||
*
|
||||
* Input Parameters:
|
||||
* src: Data to be encoded
|
||||
* len: Length of the data to be encoded
|
||||
* out_len: Pointer to output length variable, or NULL if not used
|
||||
*
|
||||
* Returned Value:
|
||||
* Returns: Allocated buffer of out_len bytes of encoded data,
|
||||
* or NULL on failure
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
static FAR unsigned char *_base64_encode(FAR const unsigned char *src,
|
||||
size_t len,
|
||||
FAR unsigned char *dst,
|
||||
FAR size_t *out_len,
|
||||
bool websafe)
|
||||
{
|
||||
FAR unsigned char *out;
|
||||
FAR unsigned char *pos;
|
||||
FAR const unsigned char *end;
|
||||
FAR const unsigned char *in;
|
||||
FAR const char *base64_table;
|
||||
char ch = '=';
|
||||
size_t olen;
|
||||
|
||||
if (websafe)
|
||||
{
|
||||
ch = '.';
|
||||
}
|
||||
|
||||
base64_table = base64_tab(websafe);
|
||||
olen = (len + 2) / 3 * 4 + 1; /* 3-byte blocks to 4-byte */
|
||||
|
||||
end = src + len;
|
||||
in = src;
|
||||
|
||||
if (dst)
|
||||
{
|
||||
pos = out = dst;
|
||||
}
|
||||
else
|
||||
{
|
||||
pos = out = malloc(olen);
|
||||
if (out == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
while (end - in >= 3)
|
||||
{
|
||||
*pos++ = base64_table[in[0] >> 2];
|
||||
*pos++ = base64_table[((in[0] & 0x03) << 4) | (in[1] >> 4)];
|
||||
*pos++ = base64_table[((in[1] & 0x0f) << 2) | (in[2] >> 6)];
|
||||
*pos++ = base64_table[in[2] & 0x3f];
|
||||
in += 3;
|
||||
}
|
||||
|
||||
if (end - in != 0)
|
||||
{
|
||||
*pos++ = base64_table[in[0] >> 2];
|
||||
if (end - in == 1)
|
||||
{
|
||||
*pos++ = base64_table[(in[0] & 0x03) << 4];
|
||||
*pos++ = ch; /* *pos++ = '='; */
|
||||
}
|
||||
else
|
||||
{
|
||||
*pos++ = base64_table[((in[0] & 0x03) << 4) | (in[1] >> 4)];
|
||||
*pos++ = base64_table[(in[1] & 0x0f) << 2];
|
||||
}
|
||||
|
||||
*pos++ = ch; /* *pos++ = '='; */
|
||||
}
|
||||
|
||||
*pos = '\0';
|
||||
if (out_len)
|
||||
{
|
||||
*out_len = pos - out;
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: _base64_decode
|
||||
*
|
||||
* Description:
|
||||
* Base64 decode
|
||||
*
|
||||
* Caller is responsible for freeing the returned buffer.
|
||||
*
|
||||
* Input Parameters:
|
||||
* src: Data to be decoded
|
||||
* len: Length of the data to be decoded
|
||||
* out_len: Pointer to output length variable
|
||||
*
|
||||
* Returned Value:
|
||||
* Returns: Allocated buffer of out_len bytes of decoded data,
|
||||
* or NULL on failure
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
static unsigned char *_base64_decode(FAR const unsigned char *src,
|
||||
size_t len, FAR unsigned char *dst,
|
||||
FAR size_t *out_len, bool websafe)
|
||||
{
|
||||
FAR unsigned char *out;
|
||||
FAR unsigned char *pos;
|
||||
FAR unsigned char block[4];
|
||||
FAR const char *base64_table;
|
||||
char ch = '=';
|
||||
FAR char *tmp;
|
||||
size_t count;
|
||||
size_t i;
|
||||
|
||||
if (websafe)
|
||||
{
|
||||
ch = '.';
|
||||
}
|
||||
|
||||
base64_table = base64_tab(websafe);
|
||||
|
||||
if (dst)
|
||||
{
|
||||
pos = out = dst;
|
||||
}
|
||||
else
|
||||
{
|
||||
pos = out = malloc(len / 4 * 3);
|
||||
if (out == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
count = 0;
|
||||
for (i = 0; i < len; i++)
|
||||
{
|
||||
tmp = strchr(base64_table, src[i]);
|
||||
block[count] = tmp ? tmp - base64_table : 0;
|
||||
count++;
|
||||
|
||||
if (count == 4)
|
||||
{
|
||||
*pos++ = (block[0] << 2) | (block[1] >> 4);
|
||||
if (src[i - 1] == ch)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
*pos++ = (block[1] << 4) | (block[2] >> 2);
|
||||
if (src[i] == ch)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
*pos++ = (block[2] << 6) | block[3];
|
||||
count = 0;
|
||||
}
|
||||
}
|
||||
|
||||
*out_len = pos - out;
|
||||
return out;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: base64_encode_length
|
||||
****************************************************************************/
|
||||
|
||||
size_t base64_encode_length(size_t len)
|
||||
{
|
||||
return (len + 2) / 3 * 4;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: base64_encode
|
||||
****************************************************************************/
|
||||
|
||||
FAR void *base64_encode(FAR const void *src, size_t len, FAR void *dst,
|
||||
FAR size_t *out_len)
|
||||
{
|
||||
return _base64_encode(src, len, dst, out_len, false);
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: base64_decode_length
|
||||
****************************************************************************/
|
||||
|
||||
size_t base64_decode_length(size_t len)
|
||||
{
|
||||
return len * 3 / 4;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: base64_decode
|
||||
****************************************************************************/
|
||||
|
||||
FAR void *base64_decode(FAR const void *src, size_t len, FAR void *dst,
|
||||
FAR size_t *out_len)
|
||||
{
|
||||
return _base64_decode(src, len, dst, out_len, false);
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: base64w_encode
|
||||
****************************************************************************/
|
||||
|
||||
FAR void *base64w_encode(FAR const void *src, size_t len, FAR void *dst,
|
||||
FAR size_t *out_len)
|
||||
{
|
||||
return _base64_encode(src, len, dst, out_len, true);
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: base64w_decode
|
||||
****************************************************************************/
|
||||
|
||||
FAR void *base64w_decode(FAR const void *src, size_t len, FAR void *dst,
|
||||
FAR size_t *out_len)
|
||||
{
|
||||
return _base64_decode(src, len, dst, out_len, true);
|
||||
}
|
||||
|
||||
#endif /* CONFIG_CODECS_BASE64 */
|
||||
@@ -0,0 +1,409 @@
|
||||
/****************************************************************************
|
||||
* apps/netutils/codecs/md5.c
|
||||
*
|
||||
* This file is part of the NuttX RTOS:
|
||||
*
|
||||
* Copyright (C) 2012 Gregory Nutt. All rights reserved.
|
||||
* Author: Darcy Gong
|
||||
*
|
||||
* Reference:
|
||||
*
|
||||
* This code implements the MD5 message-digest algorithm.
|
||||
* The algorithm is due to Ron Rivest. This code was
|
||||
* written by Colin Plumb in 1993, no copyright is claimed.
|
||||
* This code is in the public domain; do with it what you wish.
|
||||
*
|
||||
* Equivalent code is available from RSA Data Security, Inc.
|
||||
* This code has been tested against that, and is equivalent,
|
||||
* except that you don't need to include two pages of legalese
|
||||
* with every copy.
|
||||
*
|
||||
* To compute the message digest of a chunk of bytes, declare an
|
||||
* md5_context_s structure, pass it to md5_init, call md5_update as
|
||||
* needed on buffers full of bytes, and then call md5_final, which
|
||||
* will fill a supplied 16-byte array with the digest.
|
||||
*
|
||||
* See README and COPYING for more details.
|
||||
*
|
||||
* And is re-released under the NuttX modified BSD license:
|
||||
*
|
||||
* 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 of the Institute 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 INSTITUTE 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 INSTITUTE 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>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "netutils/md5.h"
|
||||
|
||||
#ifdef CONFIG_CODECS_HASH_MD5
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/* The four core functions - F1 is optimized somewhat */
|
||||
|
||||
/* #define F1(x, y, z) (x & y | ~x & z) */
|
||||
# define F1(x, y, z) (z ^ (x & (y ^ z)))
|
||||
# define F2(x, y, z) F1(z, x, y)
|
||||
# define F3(x, y, z) (x ^ y ^ z)
|
||||
# define F4(x, y, z) (y ^ (x | ~z))
|
||||
|
||||
/* This is the central step in the MD5 algorithm. */
|
||||
|
||||
# define MD5STEP(f, w, x, y, z, data, s) \
|
||||
( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: byte_reverse
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef CONFIG_ENDIAN_BIG
|
||||
# define byte_reverse(buf, len)
|
||||
#else
|
||||
static void byte_reverse(FAR unsigned char *buf, unsigned longs)
|
||||
{
|
||||
uint32_t t;
|
||||
do
|
||||
{
|
||||
t = ((uint32_t)buf[3] << 24) |
|
||||
((uint32_t)buf[2] << 16) |
|
||||
((uint32_t)buf[1] << 8) |
|
||||
(uint32_t)buf[0];
|
||||
|
||||
*(uint32_t *)buf = t;
|
||||
buf += 4;
|
||||
}
|
||||
while (--longs);
|
||||
}
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: md5_init
|
||||
*
|
||||
* Description:
|
||||
* Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
|
||||
* initialization constants.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
void md5_init(struct md5_context_s *ctx)
|
||||
{
|
||||
ctx->buf[0] = 0x67452301;
|
||||
ctx->buf[1] = 0xefcdab89;
|
||||
ctx->buf[2] = 0x98badcfe;
|
||||
ctx->buf[3] = 0x10325476;
|
||||
|
||||
ctx->bits[0] = 0;
|
||||
ctx->bits[1] = 0;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: md5_update
|
||||
*
|
||||
* Description:
|
||||
* Update context to reflect the concatenation of another buffer full
|
||||
* of bytes.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
void md5_update(struct md5_context_s *ctx, unsigned char const *buf,
|
||||
unsigned len)
|
||||
{
|
||||
uint32_t t;
|
||||
|
||||
/* Update bitcount */
|
||||
|
||||
t = ctx->bits[0];
|
||||
if ((ctx->bits[0] = t + ((uint32_t) len << 3)) < t)
|
||||
{
|
||||
/* Carry from low to high */
|
||||
|
||||
ctx->bits[1]++;
|
||||
}
|
||||
|
||||
ctx->bits[1] += len >> 29;
|
||||
|
||||
/* Bytes already in shsInfo->data */
|
||||
|
||||
t = (t >> 3) & 0x3f;
|
||||
|
||||
/* Handle any leading odd-sized chunks */
|
||||
|
||||
if (t)
|
||||
{
|
||||
unsigned char *p = (unsigned char *)ctx->in + t;
|
||||
|
||||
t = 64 - t;
|
||||
if (len < t)
|
||||
{
|
||||
memcpy(p, buf, len);
|
||||
return;
|
||||
}
|
||||
|
||||
memcpy(p, buf, t);
|
||||
byte_reverse(ctx->in, 16);
|
||||
md5_transform(ctx->buf, (uint32_t *) ctx->in);
|
||||
buf += t;
|
||||
len -= t;
|
||||
}
|
||||
|
||||
/* Process data in 64-byte chunks */
|
||||
|
||||
while (len >= 64)
|
||||
{
|
||||
memcpy(ctx->in, buf, 64);
|
||||
byte_reverse(ctx->in, 16);
|
||||
md5_transform(ctx->buf, (uint32_t *) ctx->in);
|
||||
buf += 64;
|
||||
len -= 64;
|
||||
}
|
||||
|
||||
/* Handle any remaining bytes of data. */
|
||||
|
||||
memcpy(ctx->in, buf, len);
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: md5_final
|
||||
*
|
||||
* Description:
|
||||
* Final wrapup - pad to 64-byte boundary with the bit pattern
|
||||
* 1 0* (64-bit count of bits processed, MSB-first)
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
void md5_final(unsigned char digest[16], struct md5_context_s *ctx)
|
||||
{
|
||||
unsigned count;
|
||||
unsigned char *p;
|
||||
|
||||
/* Compute number of bytes mod 64 */
|
||||
|
||||
count = (ctx->bits[0] >> 3) & 0x3f;
|
||||
|
||||
/* Set the first char of padding to 0x80. This is safe since there is
|
||||
* always at least one byte free.
|
||||
*/
|
||||
|
||||
p = ctx->in + count;
|
||||
*p++ = 0x80;
|
||||
|
||||
/* Bytes of padding needed to make 64 bytes */
|
||||
|
||||
count = 64 - 1 - count;
|
||||
|
||||
/* Pad out to 56 mod 64 */
|
||||
|
||||
if (count < 8)
|
||||
{
|
||||
/* Two lots of padding: Pad the first block to 64 bytes */
|
||||
|
||||
memset(p, 0, count);
|
||||
byte_reverse(ctx->in, 16);
|
||||
md5_transform(ctx->buf, (uint32_t *) ctx->in);
|
||||
|
||||
/* Now fill the next block with 56 bytes */
|
||||
|
||||
memset(ctx->in, 0, 56);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Pad block to 56 bytes */
|
||||
|
||||
memset(p, 0, count - 8);
|
||||
}
|
||||
|
||||
byte_reverse(ctx->in, 14);
|
||||
|
||||
/* Append length in bits and transform */
|
||||
|
||||
((uint32_t *)ctx->in)[14] = ctx->bits[0];
|
||||
((uint32_t *)ctx->in)[15] = ctx->bits[1];
|
||||
|
||||
md5_transform(ctx->buf, (uint32_t *) ctx->in);
|
||||
byte_reverse((unsigned char *)ctx->buf, 4);
|
||||
memcpy(digest, ctx->buf, 16);
|
||||
memset(ctx, 0, sizeof(struct md5_context_s)); /* In case it's sensitive */
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: md5_transform
|
||||
*
|
||||
* Description:
|
||||
* The core of the MD5 algorithm, this alters an existing MD5 hash to
|
||||
* reflect the addition of 16 longwords of new data. md5_update blocks
|
||||
* the data and converts bytes into longwords for this routine.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
void md5_transform(uint32_t buf[4], uint32_t const in[16])
|
||||
{
|
||||
register uint32_t a;
|
||||
register uint32_t b;
|
||||
register uint32_t c;
|
||||
register uint32_t d;
|
||||
|
||||
a = buf[0];
|
||||
b = buf[1];
|
||||
c = buf[2];
|
||||
d = buf[3];
|
||||
|
||||
MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
|
||||
MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
|
||||
MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
|
||||
MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
|
||||
MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
|
||||
MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
|
||||
MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
|
||||
MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
|
||||
MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
|
||||
MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
|
||||
MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
|
||||
MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
|
||||
MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
|
||||
MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
|
||||
MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
|
||||
MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
|
||||
|
||||
MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
|
||||
MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
|
||||
MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
|
||||
MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
|
||||
MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
|
||||
MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
|
||||
MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
|
||||
MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
|
||||
MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
|
||||
MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
|
||||
MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
|
||||
MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
|
||||
MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
|
||||
MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
|
||||
MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
|
||||
MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
|
||||
|
||||
MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
|
||||
MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
|
||||
MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
|
||||
MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
|
||||
MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
|
||||
MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
|
||||
MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
|
||||
MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
|
||||
MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
|
||||
MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
|
||||
MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
|
||||
MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
|
||||
MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
|
||||
MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
|
||||
MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
|
||||
MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
|
||||
|
||||
MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
|
||||
MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
|
||||
MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
|
||||
MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
|
||||
MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
|
||||
MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
|
||||
MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
|
||||
MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
|
||||
MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
|
||||
MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
|
||||
MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
|
||||
MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
|
||||
MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
|
||||
MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
|
||||
MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
|
||||
MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
|
||||
|
||||
buf[0] += a;
|
||||
buf[1] += b;
|
||||
buf[2] += c;
|
||||
buf[3] += d;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: md5_sum
|
||||
*
|
||||
* Description:
|
||||
* MD5 hash for a data block
|
||||
*
|
||||
* Input Parameters:
|
||||
* addr: Pointers to the data area
|
||||
* len: Lengths of the data block
|
||||
* mac: Buffer for the hash
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
void md5_sum(const uint8_t * addr, const size_t len, uint8_t * mac)
|
||||
{
|
||||
MD5_CTX ctx;
|
||||
|
||||
md5_init(&ctx);
|
||||
md5_update(&ctx, addr, len);
|
||||
md5_final(mac, &ctx);
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: md5_hash
|
||||
****************************************************************************/
|
||||
|
||||
char *md5_hash(const uint8_t * addr, const size_t len)
|
||||
{
|
||||
uint8_t digest[16];
|
||||
char *hash;
|
||||
int i;
|
||||
|
||||
hash = malloc(33);
|
||||
md5_sum(addr, len, digest);
|
||||
for (i = 0; i < 16; i++)
|
||||
{
|
||||
sprintf(&hash[i * 2], "%02x", digest[i]);
|
||||
}
|
||||
|
||||
hash[32] = 0;
|
||||
return hash;
|
||||
}
|
||||
|
||||
#endif /* CONFIG_CODECS_HASH_MD5 */
|
||||
@@ -0,0 +1,496 @@
|
||||
/****************************************************************************
|
||||
* apps/netutils/codecs/urldecode.c
|
||||
*
|
||||
* This file is part of the NuttX RTOS:
|
||||
*
|
||||
* Copyright (C) 2012 Gregory Nutt. All rights reserved.
|
||||
* Author: Darcy Gong
|
||||
*
|
||||
* 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 of the Institute 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 INSTITUTE 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 INSTITUTE 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 <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#include "netutils/urldecode.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor Definitions
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef CONFIG_CODECS_URLCODE
|
||||
# define IS_HEX_CHAR(ch) \
|
||||
((ch >= '0' && ch <= '9') || \
|
||||
(ch >= 'a' && ch <= 'f') || \
|
||||
(ch >= 'A' && ch <= 'F'))
|
||||
|
||||
# define HEX_VALUE(ch, value) \
|
||||
if (ch >= '0' && ch <= '9') \
|
||||
{ \
|
||||
value = ch - '0'; \
|
||||
} \
|
||||
else if (ch >= 'a' && ch <= 'f') \
|
||||
{ \
|
||||
value = ch - 'a' + 10; \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
value = ch - 'A' + 10; \
|
||||
}
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: from_hex
|
||||
*
|
||||
* Description:
|
||||
* Converts a hex character to its integer value,
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef CONFIG_CODECS_URLCODE_NEWMEMORY
|
||||
static char from_hex(char ch)
|
||||
{
|
||||
return isdigit(ch) ? ch - '0' : tolower(ch) - 'a' + 10;
|
||||
}
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Name: to_hex
|
||||
*
|
||||
* Description:
|
||||
* Converts an integer value to its hex character,
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef CONFIG_CODECS_URLCODE_NEWMEMORY
|
||||
static char to_hex(char code)
|
||||
{
|
||||
static const char hex[] = "0123456789abcdef";
|
||||
return hex[code & 15];
|
||||
}
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Name: int2h
|
||||
*
|
||||
* Description:
|
||||
* Convert a single character to a 2 digit hex str a terminating '\0' is
|
||||
* added
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef CONFIG_CODECS_AVR_URLCODE
|
||||
static void int2h(char c, char *hstr)
|
||||
{
|
||||
hstr[1] = (c & 0xf) + '0';
|
||||
if ((c & 0xf) > 9)
|
||||
{
|
||||
hstr[1] = (c & 0xf) - 10 + 'a';
|
||||
}
|
||||
|
||||
c = (c >> 4) & 0xf;
|
||||
hstr[0] = c + '0';
|
||||
if (c > 9)
|
||||
{
|
||||
hstr[0] = c - 10 + 'a';
|
||||
}
|
||||
|
||||
hstr[2] = '\0';
|
||||
}
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Name: h2int
|
||||
*
|
||||
* Description:
|
||||
* Convert a single hex digit character to its integer value.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef CONFIG_CODECS_AVR_URLCODE
|
||||
static unsigned char h2int(char c)
|
||||
{
|
||||
if (c >= '0' && c <= '9')
|
||||
{
|
||||
return ((unsigned char)c - '0');
|
||||
}
|
||||
|
||||
if (c >= 'a' && c <= 'f')
|
||||
{
|
||||
return ((unsigned char)c - 'a' + 10);
|
||||
}
|
||||
|
||||
if (c >= 'A' && c <= 'F')
|
||||
{
|
||||
return ((unsigned char)c - 'A' + 10);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: url_encode
|
||||
*
|
||||
* Description:
|
||||
* Returns a url-encoded version of str.
|
||||
*
|
||||
* IMPORTANT: be sure to free() the returned string after use.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef CONFIG_CODECS_URLCODE_NEWMEMORY
|
||||
char *url_encode(char *str)
|
||||
{
|
||||
char *pstr = str;
|
||||
char *buf = malloc(strlen(str) * 3 + 1);
|
||||
char *pbuf = buf;
|
||||
|
||||
while (*pstr)
|
||||
{
|
||||
if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~')
|
||||
{
|
||||
*pbuf++ = *pstr;
|
||||
}
|
||||
else if (*pstr == ' ')
|
||||
{
|
||||
*pbuf++ = '+';
|
||||
}
|
||||
else
|
||||
{
|
||||
*pbuf++ = '%';
|
||||
*pbuf++ = to_hex(*pstr >> 4);
|
||||
*pbuf++ = to_hex(*pstr & 15);
|
||||
}
|
||||
|
||||
pstr++;
|
||||
}
|
||||
|
||||
*pbuf = '\0';
|
||||
return buf;
|
||||
}
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Name: url_decode
|
||||
*
|
||||
* Description:
|
||||
* Returns a url-decoded version of str.
|
||||
*
|
||||
* IMPORTANT: be sure to free() the returned string after use.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef CONFIG_CODECS_URLCODE_NEWMEMORY
|
||||
char *url_decode(char *str)
|
||||
{
|
||||
char *pstr = str;
|
||||
char *buf = malloc(strlen(str) + 1);
|
||||
char *pbuf = buf;
|
||||
|
||||
while (*pstr)
|
||||
{
|
||||
if (*pstr == '%')
|
||||
{
|
||||
if (pstr[1] && pstr[2])
|
||||
{
|
||||
*pbuf++ = from_hex(pstr[1]) << 4 | from_hex(pstr[2]);
|
||||
pstr += 2;
|
||||
}
|
||||
}
|
||||
else if (*pstr == '+')
|
||||
{
|
||||
*pbuf++ = ' ';
|
||||
}
|
||||
else
|
||||
{
|
||||
*pbuf++ = *pstr;
|
||||
}
|
||||
|
||||
pstr++;
|
||||
}
|
||||
|
||||
*pbuf = '\0';
|
||||
return buf;
|
||||
}
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Name: urlencode
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef CONFIG_CODECS_URLCODE
|
||||
char *urlencode(const char *src, const int src_len, char *dest, int *dest_len)
|
||||
{
|
||||
static const unsigned char hex_chars[] = "0123456789ABCDEF";
|
||||
const unsigned char *pSrc;
|
||||
const unsigned char *pEnd;
|
||||
char *pDest;
|
||||
|
||||
pDest = dest;
|
||||
pEnd = (unsigned char *)src + src_len;
|
||||
for (pSrc = (unsigned char *)src; pSrc < pEnd; pSrc++)
|
||||
{
|
||||
if ((*pSrc >= '0' && *pSrc <= '9') ||
|
||||
(*pSrc >= 'a' && *pSrc <= 'z') ||
|
||||
(*pSrc >= 'A' && *pSrc <= 'Z') ||
|
||||
(*pSrc == '_' || *pSrc == '-' || *pSrc == '.' || *pSrc == '~'))
|
||||
{
|
||||
*pDest++ = *pSrc;
|
||||
}
|
||||
else if (*pSrc == ' ')
|
||||
{
|
||||
*pDest++ = '+';
|
||||
}
|
||||
else
|
||||
{
|
||||
*pDest++ = '%';
|
||||
*pDest++ = hex_chars[(*pSrc) >> 4];
|
||||
*pDest++ = hex_chars[(*pSrc) & 0x0F];
|
||||
}
|
||||
}
|
||||
|
||||
*pDest = '\0';
|
||||
*dest_len = pDest - dest;
|
||||
return dest;
|
||||
}
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Name: urldecode
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef CONFIG_CODECS_URLCODE
|
||||
char *urldecode(const char *src, const int src_len, char *dest, int *dest_len)
|
||||
{
|
||||
const unsigned char *pSrc;
|
||||
const unsigned char *pEnd;
|
||||
char *pDest;
|
||||
unsigned char cHigh;
|
||||
unsigned char cLow;
|
||||
int valHigh;
|
||||
int valLow;
|
||||
|
||||
pDest = dest;
|
||||
pSrc = (unsigned char *)src;
|
||||
pEnd = (unsigned char *)src + src_len;
|
||||
while (pSrc < pEnd)
|
||||
{
|
||||
if (*pSrc == '%' && pSrc + 2 < pEnd)
|
||||
{
|
||||
cHigh = *(pSrc + 1);
|
||||
cLow = *(pSrc + 2);
|
||||
|
||||
if (IS_HEX_CHAR(cHigh) && IS_HEX_CHAR(cLow))
|
||||
{
|
||||
HEX_VALUE(cHigh, valHigh)
|
||||
HEX_VALUE(cLow, valLow)
|
||||
*pDest++ = (valHigh << 4) | valLow;
|
||||
pSrc += 3;
|
||||
}
|
||||
else
|
||||
{
|
||||
*pDest++ = *pSrc;
|
||||
pSrc++;
|
||||
}
|
||||
}
|
||||
else if (*pSrc == '+')
|
||||
{
|
||||
*pDest++ = ' ';
|
||||
pSrc++;
|
||||
}
|
||||
else
|
||||
{
|
||||
*pDest++ = *pSrc;
|
||||
pSrc++;
|
||||
}
|
||||
}
|
||||
|
||||
*pDest = '\0';
|
||||
*dest_len = pDest - dest;
|
||||
return dest;
|
||||
}
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Name: urlencode_len
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef CONFIG_CODECS_URLCODE
|
||||
int urlencode_len(const char *src, const int src_len)
|
||||
{
|
||||
const unsigned char *pSrc;
|
||||
const unsigned char *pEnd;
|
||||
int len = 0;
|
||||
|
||||
pEnd = (unsigned char *)src + src_len;
|
||||
for (pSrc = (unsigned char *)src; pSrc < pEnd; pSrc++)
|
||||
{
|
||||
if ((*pSrc >= '0' && *pSrc <= '9') ||
|
||||
(*pSrc >= 'a' && *pSrc <= 'z') ||
|
||||
(*pSrc >= 'A' && *pSrc <= 'Z') ||
|
||||
(*pSrc == '_' || *pSrc == '-' || *pSrc == '.' || *pSrc == '~' || *pSrc == ' '))
|
||||
{
|
||||
len++;
|
||||
}
|
||||
else
|
||||
{
|
||||
len+=3;
|
||||
}
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Name: urldecode_len
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef CONFIG_CODECS_URLCODE
|
||||
int urldecode_len(const char *src, const int src_len)
|
||||
{
|
||||
const unsigned char *pSrc;
|
||||
const unsigned char *pEnd;
|
||||
int len = 0;
|
||||
unsigned char cHigh;
|
||||
unsigned char cLow;
|
||||
|
||||
pSrc = (unsigned char *)src;
|
||||
pEnd = (unsigned char *)src + src_len;
|
||||
while (pSrc < pEnd)
|
||||
{
|
||||
if (*pSrc == '%' && pSrc + 2 < pEnd)
|
||||
{
|
||||
cHigh = *(pSrc + 1);
|
||||
cLow = *(pSrc + 2);
|
||||
|
||||
if (IS_HEX_CHAR(cHigh) && IS_HEX_CHAR(cLow))
|
||||
{
|
||||
pSrc += 2;
|
||||
}
|
||||
}
|
||||
|
||||
len++;
|
||||
pSrc++;
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Name: urlrawdecode
|
||||
*
|
||||
* Description:
|
||||
* decode a url string e.g "hello%20joe" or "hello+joe" becomes "hello joe"
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef CONFIG_CODECS_AVR_URLCODE
|
||||
void urlrawdecode(char *urlbuf)
|
||||
{
|
||||
char c;
|
||||
char *dst;
|
||||
dst = urlbuf;
|
||||
while ((c = *urlbuf))
|
||||
{
|
||||
if (c == '+')
|
||||
{
|
||||
c = ' ';
|
||||
}
|
||||
|
||||
if (c == '%')
|
||||
{
|
||||
urlbuf++;
|
||||
c = *urlbuf;
|
||||
urlbuf++;
|
||||
c = (h2int(c) << 4) | h2int(*urlbuf);
|
||||
}
|
||||
|
||||
*dst = c;
|
||||
dst++;
|
||||
urlbuf++;
|
||||
}
|
||||
|
||||
*dst = '\0';
|
||||
}
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Name: urlrawencode
|
||||
*
|
||||
* Description:
|
||||
* There must be enoug space in urlbuf. In the worst case that is 3 times
|
||||
* the length of str
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef CONFIG_CODECS_AVR_URLCODE
|
||||
void urlrawencode(char *str, char *urlbuf)
|
||||
{
|
||||
char c;
|
||||
while ((c = *str))
|
||||
{
|
||||
if (c == ' ' || isalnum(c) || c == '_' || c == '-' || c == '.' || c == '~')
|
||||
{
|
||||
if (c == ' ')
|
||||
{
|
||||
c = '+';
|
||||
}
|
||||
|
||||
*urlbuf = c;
|
||||
str++;
|
||||
urlbuf++;
|
||||
continue;
|
||||
}
|
||||
|
||||
*urlbuf = '%';
|
||||
urlbuf++;
|
||||
int2h(c, urlbuf);
|
||||
urlbuf++;
|
||||
urlbuf++;
|
||||
str++;
|
||||
}
|
||||
|
||||
*urlbuf = '\0';
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user