2011-06-01 21:15:14 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* apps/netutils/ftpc/ftpc_utils.c
|
|
|
|
*
|
2021-06-11 06:22:42 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2011-06-01 21:15:14 +02:00
|
|
|
*
|
2021-06-11 06:22:42 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-06-01 21:15:14 +02:00
|
|
|
*
|
2021-06-11 06:22:42 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2011-06-01 21:15:14 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-06-03 01:45:31 +02:00
|
|
|
#include "ftpc_config.h"
|
2011-06-01 21:15:14 +02:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
2011-06-01 21:37:40 +02:00
|
|
|
#include <string.h>
|
2011-06-01 21:15:14 +02:00
|
|
|
|
|
|
|
#include "ftpc_internal.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpc_nibble
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Convert a ASCII hex 'digit' to binary
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int ftpc_nibble(char ch)
|
|
|
|
{
|
|
|
|
if (ch >= '0' && ch <= '9')
|
|
|
|
{
|
|
|
|
return (unsigned int)ch - '0';
|
|
|
|
}
|
|
|
|
else if (ch >= 'A' && ch <= 'F')
|
|
|
|
{
|
|
|
|
return (unsigned int)ch - 'A' + 10;
|
|
|
|
}
|
|
|
|
else if (ch >= 'a' && ch <= 'f')
|
|
|
|
{
|
|
|
|
return (unsigned int)ch - 'a' + 10;
|
|
|
|
}
|
2017-12-21 19:05:28 +01:00
|
|
|
|
2011-06-01 21:15:14 +02:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpc_reset
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Reset the FTP session.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void ftpc_reset(struct ftpc_session_s *session)
|
|
|
|
{
|
|
|
|
ftpc_sockclose(&session->data);
|
|
|
|
ftpc_sockclose(&session->cmd);
|
|
|
|
free(session->uname);
|
|
|
|
session->uname = NULL;
|
|
|
|
free(session->pwd);
|
|
|
|
session->pwd = NULL;
|
2011-06-03 22:27:30 +02:00
|
|
|
free(session->initrdir);
|
|
|
|
session->initrdir = NULL;
|
2011-06-04 15:32:26 +02:00
|
|
|
session->flags &= ~FTPC_FLAGS_CLEAR;
|
|
|
|
session->flags |= FTPC_FLAGS_SET;
|
2011-06-01 21:15:14 +02:00
|
|
|
session->xfrmode = FTPC_XFRMODE_UNKNOWN;
|
|
|
|
session->code = 0;
|
|
|
|
session->replytimeo = CONFIG_FTP_DEFTIMEO * CLOCKS_PER_SEC;
|
|
|
|
session->conntimeo = CONFIG_FTP_DEFTIMEO * CLOCKS_PER_SEC;
|
|
|
|
}
|
|
|
|
|
2011-06-03 22:27:30 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpc_lpwd
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Return the local current working directory. NOTE: This is a peek at
|
|
|
|
* a global copy. The caller should call strdup if it wants to keep it.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
FAR const char *ftpc_lpwd(void)
|
|
|
|
{
|
|
|
|
#ifndef CONFIG_DISABLE_ENVIRON
|
|
|
|
FAR const char *val;
|
|
|
|
|
|
|
|
val = getenv("PWD");
|
|
|
|
if (!val)
|
|
|
|
{
|
|
|
|
val = CONFIG_FTP_TMPDIR;
|
|
|
|
}
|
2020-12-13 16:35:40 +01:00
|
|
|
|
2011-06-03 22:27:30 +02:00
|
|
|
return val;
|
|
|
|
#else
|
|
|
|
return CONFIG_FTP_TMPDIR;
|
|
|
|
#endif
|
2011-06-01 21:15:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpc_stripcrlf
|
|
|
|
*
|
|
|
|
* Description:
|
2011-06-01 21:37:40 +02:00
|
|
|
* Strip any trailing carriage returns or line feeds from a string (by
|
|
|
|
* overwriting them with NUL characters).
|
2011-06-01 21:15:14 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void ftpc_stripcrlf(FAR char *str)
|
|
|
|
{
|
|
|
|
FAR char *ptr;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
if (str)
|
|
|
|
{
|
|
|
|
len = strlen(str);
|
|
|
|
if (len > 0)
|
|
|
|
{
|
2017-12-21 19:05:28 +01:00
|
|
|
for (ptr = str + len - 1;
|
|
|
|
len > 0 && (*ptr == '\r' || *ptr == '\n');
|
2018-01-05 01:00:04 +01:00
|
|
|
ptr--, len--)
|
2011-06-01 21:15:14 +02:00
|
|
|
{
|
|
|
|
*ptr = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpc_stripslash
|
|
|
|
*
|
|
|
|
* Description:
|
2020-12-13 16:35:40 +01:00
|
|
|
* Strip single trailing slash from a string (by overwriting it with NUL
|
|
|
|
* character).
|
2011-06-01 21:15:14 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-06-01 21:37:40 +02:00
|
|
|
void ftpc_stripslash(FAR char *str)
|
2011-06-01 21:15:14 +02:00
|
|
|
{
|
|
|
|
FAR char *ptr;
|
2011-06-01 21:37:40 +02:00
|
|
|
int len;
|
2011-06-01 21:15:14 +02:00
|
|
|
|
2011-06-01 21:37:40 +02:00
|
|
|
if (str)
|
|
|
|
{
|
|
|
|
len = strlen(str);
|
|
|
|
if (len > 1)
|
|
|
|
{
|
|
|
|
ptr = str + len - 1;
|
2011-06-03 18:15:49 +02:00
|
|
|
if (*ptr == '/')
|
2011-06-01 21:37:40 +02:00
|
|
|
{
|
|
|
|
*ptr = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-06-01 21:15:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2011-06-01 21:37:40 +02:00
|
|
|
* Name: ftpc_dequote
|
2011-06-01 21:15:14 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Convert quoted hexadecimal constants to binary values.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
FAR char *ftpc_dequote(FAR const char *str)
|
|
|
|
{
|
|
|
|
FAR char *allocstr = NULL;
|
|
|
|
FAR char *ptr;
|
|
|
|
int ms;
|
|
|
|
int ls;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
if (str)
|
|
|
|
{
|
|
|
|
/* Allocate space for a modifiable copy of the string */
|
|
|
|
|
|
|
|
len = strlen(str);
|
2020-12-13 16:35:40 +01:00
|
|
|
allocstr = (FAR char *)malloc(len + 1);
|
2011-06-01 21:15:14 +02:00
|
|
|
if (allocstr)
|
|
|
|
{
|
|
|
|
/* Search the string */
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2011-06-01 21:15:14 +02:00
|
|
|
ptr = allocstr;
|
2017-12-21 19:05:28 +01:00
|
|
|
while (*str != '\0')
|
2011-06-01 21:15:14 +02:00
|
|
|
{
|
2017-12-21 19:05:28 +01:00
|
|
|
/* Check for a quoted hex value (make sure that there are
|
|
|
|
* least 3 characters remaining in the string.
|
|
|
|
*/
|
2011-06-01 21:15:14 +02:00
|
|
|
|
2017-12-21 19:05:28 +01:00
|
|
|
if (len > 2 && str[0] == '%')
|
2011-06-01 21:15:14 +02:00
|
|
|
{
|
|
|
|
/* Extract the hex value */
|
|
|
|
|
|
|
|
ms = ftpc_nibble(str[1]);
|
|
|
|
if (ms >= 0)
|
|
|
|
{
|
|
|
|
ls = ftpc_nibble(str[2]);
|
|
|
|
if (ls >= 0)
|
|
|
|
{
|
|
|
|
/* Save the binary value and skip ahead by 3 */
|
|
|
|
|
2018-06-04 22:29:35 +02:00
|
|
|
*ptr++ = (char)(ms << 4 | ls);
|
2011-06-01 21:15:14 +02:00
|
|
|
str += 3;
|
2017-12-21 19:05:28 +01:00
|
|
|
len -= 3;
|
2011-06-01 21:15:14 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Just transfer the character */
|
|
|
|
|
|
|
|
*ptr++ = *str++;
|
2017-12-21 19:05:28 +01:00
|
|
|
len--;
|
2011-06-01 21:15:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* NUL terminate */
|
|
|
|
|
|
|
|
*ptr = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return allocstr;
|
|
|
|
}
|