mirror of
https://git.libssh.org/projects/libssh.git
synced 2025-11-30 13:01:23 +03:00
misc: Add ssh_tmpname()
Signed-Off-By: Andreas Schneider <asn@cryptomilk.org> Reviewed-by: Jakub Jelen <jjelen@redhat.com>
This commit is contained in:
@@ -96,5 +96,6 @@ int ssh_mkdirs(const char *pathname, mode_t mode);
|
|||||||
|
|
||||||
int ssh_quote_file_name(const char *file_name, char *buf, size_t buf_len);
|
int ssh_quote_file_name(const char *file_name, char *buf, size_t buf_len);
|
||||||
int ssh_newline_vis(const char *string, char *buf, size_t buf_len);
|
int ssh_newline_vis(const char *string, char *buf, size_t buf_len);
|
||||||
|
int ssh_tmpname(char *template);
|
||||||
|
|
||||||
#endif /* MISC_H_ */
|
#endif /* MISC_H_ */
|
||||||
|
|||||||
51
src/misc.c
51
src/misc.c
@@ -1735,4 +1735,55 @@ int ssh_newline_vis(const char *string, char *buf, size_t buf_len)
|
|||||||
return out - buf;
|
return out - buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
*
|
||||||
|
* @brief Replaces the last 6 characters of a string from 'X' to 6 random hexdigits.
|
||||||
|
*
|
||||||
|
* @param[in] template Any input string with last 6 characters as 'X'.
|
||||||
|
* @returns -1 as error when the last 6 characters of the input to be replaced are not 'X'
|
||||||
|
* 0 otherwise.
|
||||||
|
*/
|
||||||
|
int ssh_tmpname(char *template)
|
||||||
|
{
|
||||||
|
char *tmp = NULL;
|
||||||
|
size_t i = 0;
|
||||||
|
|
||||||
|
if (template == NULL) {
|
||||||
|
goto err;
|
||||||
|
}
|
||||||
|
|
||||||
|
tmp = template + strlen(template) - 6;
|
||||||
|
if (tmp < template) {
|
||||||
|
goto err;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (i = 0; i < 6; i++) {
|
||||||
|
if (tmp[i] != 'X') {
|
||||||
|
SSH_LOG(SSH_LOG_WARNING,
|
||||||
|
"Invalid input. Last six characters of the input must be \'X\'");
|
||||||
|
goto err;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
srand(time(NULL));
|
||||||
|
|
||||||
|
for (i = 0; i < 6; ++i) {
|
||||||
|
#ifdef _WIN32
|
||||||
|
/* in win32 MAX_RAND is 32767, thus we can not shift that far,
|
||||||
|
* otherwise the last three chars are 0 */
|
||||||
|
int hexdigit = (rand() >> (i * 2)) & 0x1f;
|
||||||
|
#else
|
||||||
|
int hexdigit = (rand() >> (i * 5)) & 0x1f;
|
||||||
|
#endif
|
||||||
|
tmp[i] = hexdigit > 9 ? hexdigit + 'a' - 10 : hexdigit + '0';
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
err:
|
||||||
|
errno = EINVAL;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
/** @} */
|
/** @} */
|
||||||
|
|||||||
Reference in New Issue
Block a user