From 4e4bfcb2794b7180fac510108a99b8ccabdc13d2 Mon Sep 17 00:00:00 2001 From: "Thomas \"Cakeisalie5\" Touhey" Date: Wed, 5 Jul 2017 20:22:07 +0200 Subject: [PATCH] Correcting some subtle things --- Makefile | 4 + configure | 7 +- include/libcasio.h | 2 +- include/libcasio/{encoding.h => char.h} | 10 +-- include/libcasio/stream.h | 13 ++-- include/libcasio/utils.h | 34 -------- src/{encoding/encoding.h => char/char.h} | 8 +- src/fs/builtin/posix/topath.c | 3 +- src/fs/builtin/windows/topath.c | 99 ++++++++++++++++-------- src/internals.h | 12 +++ src/link/seven/datastream.c | 2 +- src/stream/builtin/csum32.c | 2 +- src/stream/builtin/file.c | 2 +- src/stream/builtin/libusb/open.c | 2 +- src/stream/builtin/limited.c | 2 +- src/stream/builtin/memory.c | 2 +- src/stream/builtin/streams/open.c | 2 +- src/stream/builtin/windows/open.c | 2 +- src/stream/open.c | 7 +- 19 files changed, 120 insertions(+), 95 deletions(-) rename include/libcasio/{encoding.h => char.h} (86%) delete mode 100644 include/libcasio/utils.h rename src/{encoding/encoding.h => char/char.h} (85%) diff --git a/Makefile b/Makefile index 1273a0c..04e2732 100755 --- a/Makefile +++ b/Makefile @@ -82,6 +82,10 @@ dist: mrproper getversion: @echo $(VERSION) +# Check if is indev. + isindev: + @$(if $(INDEV),echo $(INDEV),true) + # Get the maintainer. getmaintainer: @echo "$(MAINTAINER_NAME) <$(MAINTAINER_MAIL)>" diff --git a/configure b/configure index b0e1a0b..639e5d4 100755 --- a/configure +++ b/configure @@ -7,6 +7,7 @@ cd "$(dirname $0)" [ -f Makefile.cfg ] && mv Makefile.cfg Makefile.cfg.tmp name="$(make -s getname)" version="$(make -s getversion)" +indev="$(make -s isindev)" maintainer="$(make -s getmaintainer)" [ -f Makefile.cfg.tmp ] && mv Makefile.cfg.tmp Makefile.cfg @@ -30,9 +31,11 @@ loglevel=none # none, info, warn, error, fatal # Installation directories root='' -prefix='${root}/opt/p7-project' +[ x"$indev" != x ] && prefix='${root}/opt/p7-project' \ + || prefix='${root}/usr' prefix_set= -hprefix='${root}/opt/p7-project' +[ x"$indev" != x ] && hprefix='${root}/opt/p7-project' \ + || hprefix='${root}/usr' bindir='${prefix}/bin' hbindir='${hprefix}/bin' libdir='${prefix}/lib'"$platform" diff --git a/include/libcasio.h b/include/libcasio.h index 8f3c57c..2d3e4eb 100644 --- a/include/libcasio.h +++ b/include/libcasio.h @@ -28,5 +28,5 @@ # include "libcasio/link.h" # include "libcasio/file.h" # include "libcasio/mcs.h" -# include "libcasio/utils.h" +# include "libcasio/char.h" #endif /* LIBCASIO_H */ diff --git a/include/libcasio/encoding.h b/include/libcasio/char.h similarity index 86% rename from include/libcasio/encoding.h rename to include/libcasio/char.h index 119b67c..4057adc 100644 --- a/include/libcasio/encoding.h +++ b/include/libcasio/char.h @@ -1,5 +1,5 @@ /* **************************************************************************** - * libcasio/encoding.h -- libcasio character encoding. + * libcasio/char.h -- libcasio character encoding. * Copyright (C) 2017 Thomas "Cakeisalie5" Touhey * * This file is part of libcasio. @@ -16,11 +16,11 @@ * You should have received a copy of the GNU Lesser General Public License * along with libcasio; if not, see . * ************************************************************************* */ -#ifndef LIBCASIO_ENCODING_H -# define LIBCASIO_ENCODING_H +#ifndef LIBCASIO_CHAR_H +# define LIBCASIO_CHAR_H # include "cdefs.h" -typedef int casio_encode_string_t OF((void *casio__dest, +typedef int casio_encode_t OF((void *casio__dest, const void *casio__data, size_t *casio__size)); CASIO_EXTERN int CASIO_EXPORT casio_register_conv @@ -31,4 +31,4 @@ CASIO_EXTERN int CASIO_EXPORT casio_encode_string OF((void *casio__dest, const void *casio__data, size_t *casio__size)); -#endif /* LIBCASIO_ENCODING_H */ +#endif /* LIBCASIO_CHAR_H */ diff --git a/include/libcasio/stream.h b/include/libcasio/stream.h index fdf6a16..f97dad2 100644 --- a/include/libcasio/stream.h +++ b/include/libcasio/stream.h @@ -59,10 +59,13 @@ typedef unsigned int casio_openmode_t; # define CASIO_OPENMODE_READ 0x0001 /* the stream is readable. */ # define CASIO_OPENMODE_WRITE 0x0002 /* the stream is writable. */ -# define CASIO_OPENMODE_SEEK 0x0004 /* the stream is seekable. */ -# define CASIO_OPENMODE_SERIAL 0x0008 /* the stream has serial ops. */ -# define CASIO_OPENMODE_SCSI 0x0010 /* the stream has SCSI ops. */ -# define CASIO_OPENMODE_USB 0x0020 /* the stream has USB ops. */ +# define CASIO_OPENMODE_TRUNC 0x0004 /* the file will be truncated. */ +# define CASIO_OPENMODE_APPEND 0x0008 /* will append to the file. */ + +# define CASIO_OPENMODE_SEEK 0x0010 /* the stream is seekable. */ +# define CASIO_OPENMODE_SERIAL 0x0020 /* the stream has serial ops. */ +# define CASIO_OPENMODE_SCSI 0x0040 /* the stream has SCSI ops. */ +# define CASIO_OPENMODE_USB 0x0080 /* the stream has USB ops. */ /* Here is the offset type, to move within a stream: */ @@ -278,7 +281,7 @@ CASIO_EXTERN int CASIO_EXPORT casio_comlist /* Open and close a stream. */ -CASIO_EXTERN int CASIO_EXPORT casio_open +CASIO_EXTERN int CASIO_EXPORT casio_open_stream OF((casio_stream_t **casio__stream, casio_openmode_t mode, void *casio__cookie, const casio_streamfuncs_t *casio__callbacks)); CASIO_EXTERN int CASIO_EXPORT casio_close diff --git a/include/libcasio/utils.h b/include/libcasio/utils.h deleted file mode 100644 index fdc319f..0000000 --- a/include/libcasio/utils.h +++ /dev/null @@ -1,34 +0,0 @@ -/* **************************************************************************** - * libcasio/utils.h -- libcasio utilities. - * Copyright (C) 2017 Thomas "Cakeisalie5" Touhey - * - * This file is part of libcasio. - * libcasio is free software; you can redistribute it and/or modify it - * under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation; either version 3.0 of the License, - * or (at your option) any later version. - * - * libcasio is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with libcasio; if not, see . - * ************************************************************************* */ -#ifndef LIBCASIO_UTILS_H -# define LIBCASIO_UTILS_H 1 -# include "cdefs.h" - -/* ASCII-HEX, ASCII-DEC utilities */ -CASIO_EXTERN void CASIO_EXPORT casio_putascii - OF((unsigned char *casio__p, unsigned long casio__i, int casio__n)); -CASIO_EXTERN unsigned long CASIO_EXPORT casio_getascii - OF((const unsigned char *casio__p, int casio__n)); - -CASIO_EXTERN unsigned long CASIO_EXPORT casio_getdec - OF((unsigned long casio__h)); -CASIO_EXTERN unsigned long CASIO_EXPORT casio_gethex - OF((unsigned long casio__d)); - -#endif /* LIBCASIO_UTILS_H */ diff --git a/src/encoding/encoding.h b/src/char/char.h similarity index 85% rename from src/encoding/encoding.h rename to src/char/char.h index cb24277..fc0ebb8 100644 --- a/src/encoding/encoding.h +++ b/src/char/char.h @@ -1,5 +1,5 @@ /* **************************************************************************** - * encoding/encoding.h -- libcasio internal macros for string encoding. + * char/char.h -- libcasio internal macros for string encoding. * Copyright (C) 2017 Thomas "Cakeisalie5" Touhey * * This file is part of libcasio. @@ -16,10 +16,10 @@ * You should have received a copy of the GNU Lesser General Public License * along with libcasio; if not, see . * ************************************************************************* */ -#ifndef LOCAL_ENCODING_H -# define LOCAL_ENCODING_H 1 +#ifndef LOCAL_CHAR_H +# define LOCAL_CHAR_H 1 # include "../internals.h" -#endif /* LOCAL_ENCODING_H */ +#endif /* LOCAL_CHAR_H */ diff --git a/src/fs/builtin/posix/topath.c b/src/fs/builtin/posix/topath.c index b1553b9..788a643 100644 --- a/src/fs/builtin/posix/topath.c +++ b/src/fs/builtin/posix/topath.c @@ -43,7 +43,8 @@ int CASIO_EXPORT casio_make_posix_path(void *cookie, size_t nodesize = node->casio_pathnode_size; /* Check that there is no forbidden characters. */ - if (memchr(node->casio_pathnode_name, 0, nodesize) + if (nodesize > 255 + || memchr(node->casio_pathnode_name, 0, nodesize) || memchr(node->casio_pathnode_name, '/', nodesize)) return (casio_error_invalid); diff --git a/src/fs/builtin/windows/topath.c b/src/fs/builtin/windows/topath.c index 41d107e..10e93ca 100644 --- a/src/fs/builtin/windows/topath.c +++ b/src/fs/builtin/windows/topath.c @@ -24,62 +24,97 @@ #ifndef LIBCASIO_DISABLED_WINDOWS /** - * validate_name: - * Validate element name. + * check_short: + * Check if is a short name. * - * We'll check that names respect the short name formats, also called the 8.3. - * Folder names are 8 max chars, and file names are 8 max chars of name - * plus 3 max chars of extensions. - * - * TODO: check for filesystems supporting long file names. + * We'll check that names respect the short name formats, also called + * the 8.3 format (linked to how FAT store metadata). * * @arg name the name. - * @arg size the name size. - * @arg flags the validation flags. - * @return if it is validated (0 if not). + * @arg usize the name size. + * @return the error code (0 if ok). */ -CASIO_LOCAL int validate_name(const char *name, size_t size, - unsigned int flags) +CASIO_LOCAL int check_short(const char *name, size_t usize) { - int i; - const char *nm; + int i, size; + + /* Check if is too much. */ + if (usize > 11) return (casio_error_invalid); + size = (int)usize; /* Check the short format. */ i = 8; if (i >= size) i = size; /* - Look for the dot marking the extension. */ for (; i >= 0 && name[i] != '.'; i--); + if (i < 0) { /* No extension; check if the file is 8 chars or under. */ if (size > 8) return (casio_error_invalid); } else { - /* Check if there is a dot after the limit for the extension. */ - if (memchr(&name[i], '.', size - i)) return (casio_error_invalid); - /* Check if the extension is longer than 3 chars. */ if (size > i + 3) return (casio_error_invalid); + + /* Check if there are dots after the "main" dot, in the section + * where there shouldn't be. */ + switch (i) { + case 8: + if (name[11] == '.') + return (casio_error_invalid); + /* FALLTHRU */ + case 7: + if (name[10] == '.') + return (casio_error_invalid); + /* FALLTHRU */ + case 6: + if (name[9] == '.') + return (casio_error_invalid); + break; + } } + return (0); +} + +/** + * check_name: + * Validate element name. + * + * TODO: check for filesystems supporting long file names. + * + * @arg name the name. + * @arg size the name size. + * @return the error code (0 if ok). + */ + +CASIO_LOCAL int check_name(const char *name, size_t size) +{ + int err; size_t i; + + /* Check if is a valid short name. */ + err = check_short(name, size); + if (err) return (err); + /* Look for reserved names. */ if (size == 3) { - if (!memcmp(name, "CON") || !memcmp(name, "PRN") - || !memcmp(name, "AUX") || !memcmp(name, "NUL")) - return (0); + if (!memcmp(name, "CON", 3) || !memcmp(name, "PRN", 3) + || !memcmp(name, "AUX", 3) || !memcmp(name, "NUL", 3)) + return (casio_error_invalid); } else if (size == 4) { - if ((!memcmp(name, "COM") && name[3] >= '1' && name[3] <= '9') - || (!memcmp(name, "LPT") && name[3] >= '1' && name[3] <= '8')) - return (0); + if ((!memcmp(name, "COM", 3) && name[3] >= '1' && name[3] <= '9') + || (!memcmp(name, "LPT", 3) && name[3] >= '1' && name[3] <= '8')) + return (casio_error_invalid); } /* Look for reserved characters. */ for (i = size; i; i--, name++) { if (*name < 0x20 || memchr("<>:\"/\\|?*", *name, 9)) - return (0); + return (casio_error_invalid); } /* We're good! */ - return (1); + return (0); } /** @@ -93,9 +128,9 @@ CASIO_LOCAL int validate_name(const char *name, size_t size, */ int CASIO_EXPORT casio_make_windows_path(void *cookie, - void *ppath, casio_path_t *array) + void **ppath, casio_path_t *array) { - size_t length = 0; + int err; size_t length = 0; char *path; casio_pathnode_t *node; @@ -113,12 +148,12 @@ int CASIO_EXPORT casio_make_windows_path(void *cookie, if (!node) return (casio_error_invalid); while (node) { - if (!validate_name(node->casio_pathnode_name, - node->casio_pathnode_size)) - return (casio_error_invalid); + err = check_name((char*)node->casio_pathnode_name, + node->casio_pathnode_size); + if (err) return (err); length += node->casio_pathnode_size; - node = node->casio_pathnode_size; + node = node->casio_pathnode_next; if (node) length++; /* '\\' */ if (length > 259) @@ -127,7 +162,7 @@ int CASIO_EXPORT casio_make_windows_path(void *cookie, /* Allocate the path. */ *ppath = casio_alloc(length + 1, 1); - path = *ppath; if (!path) return (casio_error_alloc); + path = (char*)*ppath; if (!path) return (casio_error_alloc); /* Fill the path. */ if (array->casio_path_device) { diff --git a/src/internals.h b/src/internals.h index cd931b2..2823301 100644 --- a/src/internals.h +++ b/src/internals.h @@ -63,4 +63,16 @@ extern casio_uint32_t casio_checksum32 OF((void *casio__mem, extern int casio_getext OF((const char *casio__path, char *casio__buf, size_t casio__n)); +/* ASCII-HEX, ASCII-DEC utilities */ + +CASIO_EXTERN void CASIO_EXPORT casio_putascii + OF((unsigned char *casio__p, unsigned long casio__i, int casio__n)); +CASIO_EXTERN unsigned long CASIO_EXPORT casio_getascii + OF((const unsigned char *casio__p, int casio__n)); + +CASIO_EXTERN unsigned long CASIO_EXPORT casio_getdec + OF((unsigned long casio__h)); +CASIO_EXTERN unsigned long CASIO_EXPORT casio_gethex + OF((unsigned long casio__d)); + #endif /* LOCAL_INTERNALS_H */ diff --git a/src/link/seven/datastream.c b/src/link/seven/datastream.c index 4a0481b..a4884eb 100644 --- a/src/link/seven/datastream.c +++ b/src/link/seven/datastream.c @@ -348,5 +348,5 @@ int CASIO_EXPORT casio_seven_open_data_stream(casio_stream_t **stream, } /* initialize the stream */ - return (casio_open(stream, mode, cookie, &seven_data_callbacks)); + return (casio_open_stream(stream, mode, cookie, &seven_data_callbacks)); } diff --git a/src/stream/builtin/csum32.c b/src/stream/builtin/csum32.c index 28a1e9b..3c19a32 100644 --- a/src/stream/builtin/csum32.c +++ b/src/stream/builtin/csum32.c @@ -98,5 +98,5 @@ int CASIO_EXPORT casio_open_csum32(casio_stream_t **stream, /* Initialize da stream. */ openmode = casio_get_openmode(original) & CASIO_OPENMODE_READ; - return (casio_open(stream, openmode, cookie, &csum32_callbacks)); + return (casio_open_stream(stream, openmode, cookie, &csum32_callbacks)); } diff --git a/src/stream/builtin/file.c b/src/stream/builtin/file.c index 1550605..776ca69 100644 --- a/src/stream/builtin/file.c +++ b/src/stream/builtin/file.c @@ -244,7 +244,7 @@ int CASIO_EXPORT casio_open_stream_file(casio_stream_t **stream, cookie->_wstream = wstream; /* initialize the stream */ - return (casio_open(stream, mode, cookie, &casio_file_callbacks)); + return (casio_open_stream(stream, mode, cookie, &casio_file_callbacks)); fail: if (rstream && rstream_cl) fclose(rstream); if (wstream != rstream && wstream_cl) fclose(wstream); diff --git a/src/stream/builtin/libusb/open.c b/src/stream/builtin/libusb/open.c index c5b60cd..2d480aa 100644 --- a/src/stream/builtin/libusb/open.c +++ b/src/stream/builtin/libusb/open.c @@ -171,7 +171,7 @@ int CASIO_EXPORT casio_openusb_libusb(casio_stream_t **stream) cookie->_end = -1; /* final call. */ - return (casio_open(stream, openmode, + return (casio_open_stream(stream, openmode, cookie, &casio_libusb_callbacks)); fail: if (cookie) casio_free(cookie); diff --git a/src/stream/builtin/limited.c b/src/stream/builtin/limited.c index 5f3dc37..4ed8279 100644 --- a/src/stream/builtin/limited.c +++ b/src/stream/builtin/limited.c @@ -102,7 +102,7 @@ int CASIO_EXPORT casio_open_limited(casio_stream_t **stream, cookie->_left = size; /* initialize da stream */ - return (casio_open(stream, + return (casio_open_stream(stream, casio_get_openmode(original) & CASIO_OPENMODE_READ, cookie, &casio_limited_callbacks)); } diff --git a/src/stream/builtin/memory.c b/src/stream/builtin/memory.c index 5150853..1682007 100644 --- a/src/stream/builtin/memory.c +++ b/src/stream/builtin/memory.c @@ -163,6 +163,6 @@ int CASIO_EXPORT casio_open_memory(casio_stream_t **stream, cookie->_offset = 0; /* initialize da stream */ - return (casio_open(stream, CASIO_OPENMODE_READ, cookie, + return (casio_open_stream(stream, CASIO_OPENMODE_READ, cookie, &casio_memory_callbacks)); } diff --git a/src/stream/builtin/streams/open.c b/src/stream/builtin/streams/open.c index 58adae0..2a67c11 100644 --- a/src/stream/builtin/streams/open.c +++ b/src/stream/builtin/streams/open.c @@ -139,7 +139,7 @@ int CASIO_EXPORT casio_open_stream_fd(casio_stream_t **stream, readfd, writefd)); /* final call */ - return (casio_open(stream, mode, cookie, + return (casio_open_stream(stream, mode, cookie, &casio_streams_callbacks)); fail: if (readfd >= 0 && closeread) diff --git a/src/stream/builtin/windows/open.c b/src/stream/builtin/windows/open.c index 04f91df..03b7777 100644 --- a/src/stream/builtin/windows/open.c +++ b/src/stream/builtin/windows/open.c @@ -94,7 +94,7 @@ int CASIO_EXPORT casio_opencom_windows(casio_stream_t **stream, /* initialize for real */ mode = CASIO_OPENMODE_READ | CASIO_OPENMODE_WRITE | CASIO_OPENMODE_SERIAL; - return (casio_open(stream, mode, cookie, &casio_windows_callbacks)); + return (casio_open_stream(stream, mode, cookie, &casio_windows_callbacks)); fail: if (fhandle != INVALID_HANDLE_VALUE) CloseHandle(fhandle); if (cookie) casio_free(cookie); diff --git a/src/stream/open.c b/src/stream/open.c index 0a476d0..68bdaf2 100644 --- a/src/stream/open.c +++ b/src/stream/open.c @@ -21,7 +21,7 @@ { if (CASIO__COND) {err = CASIO__ERR; goto fail;}} /** - * casio_open: + * casio_open_stream: * Open a libcasio stream. * * @arg pstream the stream to open. @@ -31,8 +31,9 @@ * @return the error code (0 if ok). */ -int CASIO_EXPORT casio_open(casio_stream_t **pstream, casio_openmode_t mode, - void *cookie, const casio_streamfuncs_t *callbacks) +int CASIO_EXPORT casio_open_stream(casio_stream_t **pstream, + casio_openmode_t mode, void *cookie, + const casio_streamfuncs_t *callbacks) { int err; casio_stream_t *stream = NULL; casio_streamfuncs_t *c;