Compare commits

..

2 commits

10 changed files with 239 additions and 20 deletions

View file

@ -35,11 +35,10 @@ AC_CHECK_INCLUDES_DEFAULT
AC_C_CONST
AC_C_INLINE
AC_CHECK_FUNCS_ONCE(strtoull)
AC_CHECK_HEADERS(m4_normalize([
libgen.h
getopt.h
sys/random.h
]))
AC_CHECK_TYPES([struct option], [], [],
@ -49,6 +48,8 @@ AC_CHECK_TYPES([struct option], [], [],
AC_CHECK_FUNCS(m4_normalize([
getopt_long
strtoull
getrandom
]))
AC_CONFIG_FILES([Makefile

View file

@ -2,4 +2,4 @@ SUBDIRS = tests
noinst_LIBRARIES = libcrypto.a
AM_CFLAGS = -I$(top_srcdir)
libcrypto_a_SOURCES = hchacha.c xchacha.c
libcrypto_a_SOURCES = hchacha.c xchacha.c random.c

57
libcrypto/random.c Normal file
View file

@ -0,0 +1,57 @@
#include "random.h"
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif /* HAVE_CONFIG_H */
#ifdef _WIN32
# include <windows.h>
# include <ntsecapi.h>
#else
# ifdef HAVE_SYS_RANDOM_H
# include <sys/random.h>
# else
# include <stdio.h>
# endif /* HAVE_SYS_RANDOM_H */
#endif /* _WIN32 */
int
crypto_random(uint8_t *buff, size_t size)
{
#ifdef _WIN32
return (RtlGenRandom(buff, size));
#else
# ifdef HAVE_GETRANDOM
ssize_t ret;
ret = getrandom(buff, size, 0)
if (ret < size)
{
return (-1);
}
# else
FILE *fp;
size_t total;
size_t byte_read;
fp = fopen("/dev/urandom", "rb");
if (fp == NULL)
{
return (-1);
}
total = 0;
do {
byte_read = fread(buff + total, 1, size - total, fp);
if (byte_read == 0)
{
fclose(fp);
return (-1);
}
total += byte_read;
} while (total < size);
fclose(fp);
# endif /* HAVE_GETRANDOM */
#endif /* _WIN32 */
return (0);
}

8
libcrypto/random.h Normal file
View file

@ -0,0 +1,8 @@
#ifndef CRYPTO_RANDOM_H
# define CRYPTO_RANDOM_H 1
# include <stdint.h>
int crypto_random(uint8_t *buff, size_t size);
#endif /* CRYPTO_RANDOM_H */

View file

@ -1,5 +1,5 @@
TESTS = test_hchacha test_xchacha
check_PROGRAMS = test_hchacha test_xchacha
TESTS = test_hchacha test_xchacha test_random
check_PROGRAMS = test_hchacha test_xchacha test_random
AM_CFLAGS = -I../
LDADD = -lcmocka ../libcrypto.a
@ -7,3 +7,5 @@ LDADD = -lcmocka ../libcrypto.a
test_hchacha_SOURCES = test_hchacha.c
test_xchacha_SOURCES = test_xchacha.c
test_random_SOURCES = test_random.c

View file

@ -0,0 +1,28 @@
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include <setjmp.h>
#include <cmocka.h>
#include "random.h"
/* test from https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-xchacha-03#section-2.2 */
static void
test_random(void **state)
{
uint8_t buff[512];
assert_return_code(crypto_random(buff, 512), 0);
}
int
main(void)
{
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_random),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}

View file

@ -8,7 +8,7 @@ bin_PROGRAMS = mkfs.stpd tools.stpd inspect.stpd
mkfs_stpd_SOURCES = mkfs/main.c
mkfs_stpd_LDADD = ../libfs/libfs.a mkfs/bootsector.o
tools_stpd_SOURCES = tools/main.c tools/ls.c tools/copy.c tools/mkdir.c tools/rm.c
tools_stpd_SOURCES = tools/main.c tools/ls.c tools/ls/colors.c tools/copy.c tools/mkdir.c tools/rm.c
inspect_stpd_SOURCES = inspect/main.c

View file

@ -15,6 +15,7 @@
#endif /* HAVE_GETOPT_H */
#include <stupidfs.h>
#include "libfs/fs.h"
#include "ls/colors.h"
#ifdef HAVE_STRUCT_OPTION
static struct option long_options[] = {
@ -172,26 +173,59 @@ print_size(struct fs_inode *ip)
static void
print_name(struct stpdfs_dirent *dirent, struct fs_inode *ip)
{
enum COLORS type;
type = C_NORM;
if ((ip->inode.mode & STPDFS_S_IFMT) == STPDFS_S_IFREG)
{
type = C_FILE;
}
else if ((ip->inode.mode & STPDFS_S_IFMT) == STPDFS_S_IFDIR)
{
if (ip->inode.mode & (STPDFS_S_IWOTH | STPDFS_S_ISVTX))
{
type = C_STICKY_OTH_WRITABLE;
}
else if (ip->inode.mode & STPDFS_S_ISVTX)
{
type = C_STICKY;
}
else if (ip->inode.mode & STPDFS_S_IWOTH)
{
type = C_OTHER_WRITABLE;
}
else
{
type = C_DIR;
}
}
else if ((ip->inode.mode & STPDFS_S_IFMT) == STPDFS_S_IFLNK)
{
type = C_LINK;
}
else if (ip->inode.mode & STPDFS_S_ISUID)
{
type = C_SETUID;
}
else if (ip->inode.mode & STPDFS_S_ISGID)
{
type = C_SETGID;
}
else if (ip->inode.mode & STPDFS_S_IXUSR)
{
type = C_EXEC;
}
if (color)
{
if ((ip->inode.mode & STPDFS_S_IFMT) == STPDFS_S_IFDIR)
{
printf("\033[01;34m");
}
else if ((ip->inode.mode & STPDFS_S_IFMT) == STPDFS_S_IFLNK)
{
printf("\033[01;36m");
}
else if (ip->inode.mode & STPDFS_S_IXUSR)
{
printf("\033[01;32m");
}
printf("%s%s%s", ls_colors[C_LEFT], ls_colors[type], ls_colors[C_RIGHT]);
}
printf("%s\n", dirent->filename);
if (color)
{
printf("\033[0m");
printf("%s%s%s", ls_colors[C_LEFT], ls_colors[C_END], ls_colors[C_RIGHT]);
}
}
@ -294,6 +328,7 @@ cmd_ls(int argc, char **argv)
break;
case 'c':
ls_parse_colors();
color = 1;
break;

64
tools/tools/ls/colors.c Normal file
View file

@ -0,0 +1,64 @@
#include "colors.h"
#include <stdlib.h>
#include <string.h>
char *ls_colors[] = {
/* C_NORM */ "0",
/* C_FILE */ "0",
/* C_DIR */ "34",
/* C_LINK */ "35",
/* C_SETUID */ "30;41",
/* C_SETGID */ "30;46",
/* C_STICKY_OTH_WRITABLE */ "30;42",
/* C_OTHER_WRITABLE */ "30;43",
/* C_STICKY */ "0",
/* C_EXEC */ "31",
/* C_LEFT */ "\033[",
/* C_RIGHT */ "m",
/* C_END */ "0"
};
static const struct {
char *abrv;
enum COLORS type;
} abrv2type[] = {
{"no=", C_NORM},
{"fi=", C_FILE},
{"di=", C_DIR},
{"ln=", C_LINK},
{"su=", C_SETUID},
{"sg=", C_SETGID},
{"tw=", C_STICKY_OTH_WRITABLE},
{"ow=", C_OTHER_WRITABLE},
{"st=", C_STICKY},
{"ex=", C_EXEC},
{"lc=", C_LEFT},
{"rc=", C_RIGHT},
{"ec=", C_END},
{NULL, 0}
};
void
ls_parse_colors(void)
{
char *colors;
char *line;
int idx;
colors = getenv("LS_COLORS");
if (colors == NULL) return;
line = strtok(colors, ":");
while (line != NULL)
{
for (idx = 0; abrv2type[idx].abrv != NULL; idx++)
{
if (strncmp(abrv2type[idx].abrv, line, 3) == 0)
{
ls_colors[abrv2type[idx].type] = line + 3;
break;
}
}
line = strtok(NULL, ":");
}
}

24
tools/tools/ls/colors.h Normal file
View file

@ -0,0 +1,24 @@
#ifndef TOOLS_LS_COLORS_H
# define TOOLS_LS_COLORS_H 1
enum COLORS {
C_NORM,
C_FILE,
C_DIR,
C_LINK,
C_SETUID,
C_SETGID,
C_STICKY_OTH_WRITABLE,
C_OTHER_WRITABLE,
C_STICKY,
C_EXEC,
C_LEFT,
C_RIGHT,
C_END
};
extern char *ls_colors[];
void ls_parse_colors(void);
#endif /* !TOOLS_LS_COLORS_H */