Modified some things, started implementing filesystems.

This commit is contained in:
Thomas Touhey 2017-06-28 11:12:08 +02:00
parent 9cc56194fe
commit c2bad868e6
50 changed files with 761 additions and 304 deletions

View File

@ -86,7 +86,7 @@ dist: mrproper
getmaintainer:
@echo "$(MAINTAINER_NAME) <$(MAINTAINER_MAIL)>"
.PHONY: getname getauthor getmail getversion
.PHONY: getname getversion getmaintainer
#*****************************************************************************#
# Library-specific targets #
#*****************************************************************************#
@ -99,10 +99,10 @@ dist: mrproper
# Make an object out of a source file.
define make-obj-rule
$(OBJDIR)/$1.o: $(SRCDIR)/$1.c $(INC) | $(dir $(OBJDIR)/$1)
$(OBJDIR)/$1.c.o: $(SRCDIR)/$1.c $(INC) $(INCI) | $(dir $(OBJDIR)/$1)
$(call bcmd,cc,$$@,$(CC) -c -o $$@ $$< $(CFLAGS))
endef
$(foreach src,$(SRC),\
$(foreach src,$(basename $(SRC)),\
$(eval $(call make-obj-rule,$(src))))
# Make the shared library.
@ -151,8 +151,8 @@ $(eval $(call make-obj-rule,$(src))))
$(call imsg,Installing development files.))
$(if $(INSTALL_DEVEL),\
$(call qcmd,$(INST) -m 755 -d $(patsubst %,\
"$(IINCDIR)/lib$(NAME)-$(VERSION)/%", $(sort $(dir $(INCPUB))))))
$(if $(INSTALL_DEVEL),$(foreach i,$(INCPUB),\
"$(IINCDIR)/lib$(NAME)-$(VERSION)/%", $(sort $(dir $(INCp))))))
$(if $(INSTALL_DEVEL),$(foreach i,$(INCp),\
$(call qcmd,$(INST) -m 644 $(INCDIR)/$(i) \
"$(IINCDIR)/lib$(NAME)-$(VERSION)/$(i)"$(\n))))

View File

@ -46,12 +46,12 @@
# Object names #
#*****************************************************************************#
# Dynamic library name
SONAME := $(if $(FOR_WINDOWS),lib$(NAME).dll,lib$(NAME).so.$(MAJOR))
SONAME := $(if $(FOR_WINDOWS),lib$(NAME).dll,lib$(NAME).so.$(MAJOR))
SONAMES := $(if $(FOR_WINDOWS),lib$(NAME).dll,lib$(NAME).so.*)
# Static library name
ANAME := $(if $(FOR_WINDOWS),lib$(NAME).lib,lib$(NAME).a)
ANAMES := lib$(NAME).lib lib$(NAME).a lib$(NAME).dll.a
ANAME := $(if $(FOR_WINDOWS),lib$(NAME).lib,lib$(NAME).a)
ANAMES := lib$(NAME).lib lib$(NAME).a lib$(NAME).dll.a
# Libs
DEPS := libfontcharacter
@ -120,17 +120,16 @@ endif
#*****************************************************************************#
# Look up the sources and includes #
#*****************************************************************************#
# Look up the sources
SRC := $(basename $(shell find $(SRCDIR) -mindepth 1 -type f \
-name "*.c" -printf "%P\n"))
# Look up the sources.
SRC := $(patsubst $(SRCDIR)/%,%,$(shell find $(SRCDIR) -name "*.c"))
DIRS := $(sort $(dir $(SRC)))
# Look up the includes
INCPUB := $(shell find $(INCDIR) -name "*.h" -and \
-not -path "*internals*" -printf "%P\n")
# Look up the includes.
INC := $(shell find $(INCDIR) -name "*.h")
INCp := $(INC:$(INCDIR)/%=%)
# Look up the includes
INC := $(shell find $(INCDIR) -name "*.h")
# Look up the source includes.
INCI := $(shell find $(SRCDIR) -name "*.h")
#*****************************************************************************#
# Look for manpages #
#*****************************************************************************#
@ -140,8 +139,7 @@ define check-man
MAN_SECTIONS += $1
MAN_$1 += $2
endef
$(foreach doc, $(basename $(shell find $(DOCDIR) \
-maxdepth 1 -mindepth 1 -printf "%P\n" -type f -or -type l -name "*.*.txt")),\
$(foreach doc,$(wildcard $(DOCDIR)/*.*.txt),\
$(eval $(call check-man,$(patsubst .%,%,$(suffix $(doc))),$(basename $(doc)))))
# Remove duplicate sections.

6
configure vendored
View File

@ -25,6 +25,7 @@ no_libusb=
static=
windows=
optimize_size=
no_log=
loglevel=none # none, info, warn, error, fatal
# Installation directories
@ -69,7 +70,8 @@ Build options:
--optimize-size optimize size instead of speed
--no-file do not use the libc FILE interface
--no-libusb do not use libusb
--loglevel=LOGLEVEL library log level [$loglevel]
--no-log disable logging
--loglevel=LOGLEVEL default library log level [$loglevel]
Installation options:
--no-manpages should not make and install manpages
@ -133,6 +135,7 @@ for arg ; do case "$arg" in
--optimize-size) optimize_size=y ;;
--no-file) no_file=y ;;
--no-libusb) no_libusb=y ;;
--no-log) no_log=y ;;
--loglevel=*)
level="${arg#*=}"
# check if is in array
@ -226,6 +229,7 @@ tools/write-header-config --version=${version} \
--maintainer="$(echo ${maintainer} | sed 's/"/\\"/g')" \
$([ "$no_file" ] && echo --no-file) \
$([ "$no_libusb" ] && echo --no-libusb) \
$([ "$no_log" ] && echo --no-log) \
>include/${name}/config.h
# Do it!

View File

@ -22,6 +22,10 @@
# include <stddef.h>
# include <time.h>
/* Check the library version. */
# define LIBCASIO_PREREQ(CASIO__MAJ, CASIO__MIN) \
((CASIO__MAJ) > (LIBCASIO_MAJOR) || \
((CASIO__MAJ) == (LIBCASIO_MAJOR) && (CASIO__MIN) >= (LIBCASIO_MINOR)))
/* ************************************************************************* */
/* Check for compilers */
/* ************************************************************************* */

View File

@ -27,9 +27,38 @@ struct casio_filesystem_s;
typedef struct casio_filesystem_s casio_filesystem_t;
struct casio_fsfuncs_s;
typedef struct casio_fsfuncs_s casio_fsfuncs_t;
struct casio_pathnode_s;
typedef struct casio_pathnode_s casio_pathnode_t;
struct casio_path_s;
typedef struct casio_path_s casio_path_t;
struct casio_stat_s;
typedef struct casio_stat_s casio_stat_t;
/* ************************************************************************* */
/* Filesystem file path */
/* ************************************************************************* */
/* We ought to make a file path abstraction, that take into account all of the
* existing file systems, with devices, namespaces, and their character
* encodings.
*
* The path node is a path element, generally a file/folder/else name.
* It is stored as a linked list. */
struct casio_pathnode_s {
casio_pathnode_t *casio_pathnode_next;
unsigned char casio_pathnode_name[2048];
};
/* And here is the main path structure. */
#define casio_pathflag_alloc 0x0001 /* path object is valid and allocated */
#define casio_pathflag_rel 0x0002 /* relative path */
struct casio_path_s {
unsigned int casio_path_flags;
const char *casio_path_device;
casio_pathnode_t *casio_path_nodes;
};
/* ************************************************************************* */
/* Filesystem file entry */
/* ************************************************************************* */
/* This structure defines file metadata. */
@ -50,32 +79,56 @@ typedef void casio_fs_list_func_t OF((void *casio__cookie,
/* ************************************************************************* */
/* Filesystem description */
/* ************************************************************************* */
/* The filesystem is a private structure that represents a file system. */
/* The filesystem is a private structure that represents a file system.
*
* The `casio_fs_stat` callback is to get information about a file or
* directory. */
typedef int casio_fs_stat_t
OF((void *casio__cookie, const char *casio__device,
const void *casio__path[], casio_stat_t *casio__stat));
/* The `casio_fs_makedir` callback creates a directory. */
typedef int casio_fs_makedir_t
OF((void *casio__cookie, const char *casio__device,
const void *casio__path[]));
/* The `casio_fs_del` callback deletes a file or directory. */
typedef int casio_fs_del_t
OF((void *casio__cookie, const char *casio__device,
const void *casio__path[]));
/* The `casio_fs_move` callback renames/moves a file into an
* other directory, eventually with an other name. */
typedef int casio_fs_move_t
OF((void *casio__cookie, const char *casio__device,
const void *casio__orig[], const void *casio__new[]));
/* The `casio_fs_open` callback makes a stream out of a file with a path. */
typedef int casio_fs_open_t
OF((void *casio__cookie, const void *casio__path, casio_off_t size,
casio_openmode_t casio__mode, casio_stream_t **stream));
/* The `casio_fs_list` callback lists files in a directory. */
typedef int casio_fs_list_t
OF((void *casio__cookie, const char *casio__device,
const void *casio__dirpath[],
casio_fs_list_func_t *casio__callback, void *casio__cbcookie));
/* The `casio_fs_close` callback is called when the filesystem interface
* is closed. */
typedef int casio_fs_close_t OF((void *casio__cookie));
/* And here is the structure with all of the functions.
* It is the one used when you want to open a libcasio filesystem interface
* abstraction. */
struct casio_fsfuncs_s {
casio_fs_stat_t *casio_fsfuncs_stat;
casio_fs_makedir_t *casio_fsfuncs_makedir;

View File

@ -20,29 +20,14 @@
# define LIBCASIO_LOG_H
# include "cdefs.h"
CASIO_BEGIN_NAMESPACE
/* Log levels -- prefer the functions to these values, that can change! */
typedef int casio_loglevel_t;
# define casio_loglevel_info 0
# define casio_loglevel_warn 10
# define casio_loglevel_error 20
# define casio_loglevel_fatal 30
# define casio_loglevel_none 40
CASIO_BEGIN_DECLS
/* Get and set the log level at runtime */
CASIO_EXTERN void CASIO_EXPORT casio_setlog
OF((casio_loglevel_t casio__level));
CASIO_EXTERN casio_loglevel_t CASIO_EXPORT casio_getlog
/* Get and set the log level at runtime. */
CASIO_EXTERN void CASIO_EXPORT casio_setlog
OF((const char *casio__level));
CASIO_EXTERN const char* CASIO_EXPORT casio_getlog
OF((void));
/* Get, set and list log level strings */
CASIO_EXTERN const char* CASIO_EXPORT casio_loglevel_tostring
OF((casio_loglevel_t casio__level));
CASIO_EXTERN casio_loglevel_t CASIO_EXPORT casio_loglevel_fromstring
OF((const char *casio__string));
/* List log levels */
typedef void casio_log_list_t OF((void *casio__cookie,

View File

@ -372,6 +372,12 @@ CASIO_EXTERN int CASIO_EXPORT casio_open_limited
casio_stream_t *casio__original, size_t casio__size));
CASIO_EXTERN int CASIO_EXPORT casio_empty_limited
OF((casio_stream_t *casio__stream));
/* Make a stream out of another, while calculating a 32-bit checksum. */
CASIO_EXTERN int CASIO_EXPORT casio_open_csum32
OF((casio_stream_t **casio__stream, casio_stream_t *casio__original,
casio_uint32_t *casio__csum));
/* ************************************************************************* */
/* USB or serial stream opening management */
/* ************************************************************************* */

View File

@ -31,7 +31,7 @@ const char* CASIO_EXPORT casio_error_strings[128] = {
/* Stream errors. */
"the stream is invalid",
"the stream is invalid/no (more) stream",
"a read operation on the stream has failed",
"a write operation on the stream has failed",
"a seek operation on the stream has failed",

View File

@ -55,16 +55,14 @@ CASIO_EXTERN int CASIO_EXPORT casio_decode_std_cg_##CASIO__NAME \
OF((casio_file_t **casio__file, casio_stream_t *casio__buffer, \
casio_standard_header_t *casio__std, \
casio_standard_subheader_t *casio__sub, \
casio_standard_prizm_subheader_t *casio__pzm, \
uint32_t *casio__check));
casio_standard_prizm_subheader_t *casio__pzm));
# define CASIO_CPFUNC(CASIO__NAME) \
CASIO_EXTERN int CASIO_EXPORT casio_decode_std_cp_##CASIO__NAME \
OF((casio_file_t **casio__handle, casio_stream_t *casio__buffer, \
casio_standard_header_t *casio__std, \
casio_standard_subheader_t *casio__sub, \
casio_standard_classpad_subheader_t *casio__cp, \
uint32_t *casio__check));
casio_standard_classpad_subheader_t *casio__cp));
# define CASIO_PICFUNC(CASIO__NAME) \
CASIO_EXTERN int CASIO_EXPORT casio_decode_std_##CASIO__NAME \

View File

@ -131,6 +131,9 @@ int CASIO_EXPORT casio_decode_std(casio_file_t **h, const char *path,
be32toh(std->casio_standard_header_filesize);
std->casio_standard_header_number =
be16toh(std->casio_standard_header_number);
msg((ll_info, "Standard Header filesize is 0x%08" CASIO_PRIX32 "o",
std->casio_standard_header_filesize));
/* get the type */
if (casio_maketype_std(path, std, &mflags, &platform, &type))
@ -152,6 +155,7 @@ int CASIO_EXPORT casio_decode_std(casio_file_t **h, const char *path,
/* check if there is a standard subheader */
if (mflags & casio_stdflag_sub) {
casio_standard_subheader_t hd;
casio_stream_t *csum_stream;
msg((ll_info, "Has a Standard Subheader!"));
DREAD(hd)
@ -175,8 +179,15 @@ int CASIO_EXPORT casio_decode_std(casio_file_t **h, const char *path,
check = casio_checksum32(&shd,
sizeof(casio_standard_classpad_subheader_t), check);
/* make the checksum stream */
msg((ll_info, "Opening the checksum stream"));
err = casio_open_csum32(&csum_stream, buffer, &check);
if (err) return (err);
/* decode the file content */
err = (*rd)(h, buffer, std, &hd, &shd, &check);
msg((ll_info, "Decoding the file using the specific function"));
err = (*rd)(h, csum_stream, std, &hd, &shd);
casio_close(csum_stream);
if (err) return (err);
} else if (platform == casio_filefor_cg) {
casio_standard_prizm_subheader_t shd;
@ -186,8 +197,15 @@ int CASIO_EXPORT casio_decode_std(casio_file_t **h, const char *path,
check = casio_checksum32(&shd,
sizeof(casio_standard_prizm_subheader_t), check);
/* make the checksum stream */
msg((ll_info, "Opening the checksum stream"));
err = casio_open_csum32(&csum_stream, buffer, &check);
if (err) return (err);
/* decode the file content */
err = (*rd)(h, buffer, std, &hd, &shd, &check);
msg((ll_info, "Decoding the file using the specific function"));
err = (*rd)(h, csum_stream, std, &hd, &shd);
casio_close(csum_stream);
if (err) return (err);
/* read the footer */
@ -200,8 +218,12 @@ int CASIO_EXPORT casio_decode_std(casio_file_t **h, const char *path,
/* check the sum */
err = casio_error_csum;
if (check != be32toh(hd.casio_standard_subheader_checksum))
if (check != be32toh(hd.casio_standard_subheader_checksum)) {
msg((ll_error, "Checksum mismatch: got 0x%08" CASIO_PRIX32
", expected 0x%08" CASIO_PRIX32,
check, be32toh(hd.casio_standard_subheader_checksum)));
goto fail;
}
/* no error */
return (0);

View File

@ -97,14 +97,12 @@ fail:
* @arg std the standard header.
* @arg sub the standard subheader.
* @arg cp the classpad-specific subheader.
* @arg check the checksum to feed.
* @return the error code (0 if ok).
*/
int CASIO_EXPORT casio_decode_std_cp_addin(casio_file_t **h,
casio_stream_t *buffer, casio_standard_header_t *std,
casio_standard_subheader_t *sub, casio_standard_classpad_subheader_t *cp,
casio_uint32_t *check)
casio_standard_subheader_t *sub, casio_standard_classpad_subheader_t *cp)
{
int err = 0;
casio_file_t *handle;
@ -114,8 +112,6 @@ int CASIO_EXPORT casio_decode_std_cp_addin(casio_file_t **h,
/* read the add-in subheader */
DREAD(cphd)
*check = casio_checksum32(&cphd,
sizeof(casio_classpad_addin_subheader_t), *check);
/* make the handle */
casio_decode_version(&version,
@ -152,8 +148,6 @@ int CASIO_EXPORT casio_decode_std_cp_addin(casio_file_t **h,
/* get content */
GREAD(handle->casio_file_content, handle->casio_file_size)
*check = casio_checksum32(handle->casio_file_content,
handle->casio_file_size, *check);
/* no error */
return (0);
@ -173,14 +167,12 @@ fail:
* @arg std the standard header.
* @arg sub the standard subheader.
* @arg prizm the prizm-specific subheader.
* @arg check the checksum to feed.
* @return the error code (0 if ok).
*/
int CASIO_EXPORT casio_decode_std_cg_addin(casio_file_t **h,
casio_stream_t *buffer, casio_standard_header_t *std,
casio_standard_subheader_t *sub, casio_standard_prizm_subheader_t *prizm,
casio_uint32_t *check)
casio_standard_subheader_t *sub, casio_standard_prizm_subheader_t *prizm)
{
int err = 0;
casio_prizm_addin_subheader_t cghd; size_t content_size;
@ -190,8 +182,6 @@ int CASIO_EXPORT casio_decode_std_cg_addin(casio_file_t **h,
/* read the add-in subheader */
DREAD(cghd)
*check = casio_checksum32(&cghd,
sizeof(casio_prizm_addin_subheader_t), *check);
/* make the handle */
casio_decode_version(&version,
@ -206,8 +196,9 @@ int CASIO_EXPORT casio_decode_std_cg_addin(casio_file_t **h,
+ sizeof(casio_uint32_t);
msg((ll_info, "Content size is %" CASIO_PRIuSIZE, content_size));
err = casio_make_addin(h, casio_filefor_cg, content_size,
(char*)sub->casio_standard_subheader_title,
(char*)sub->casio_standard_subheader_internal_name,
(char*)sub->casio_standard_subheader_title, &version, &created);
&version, &created);
if (err) return (err);
handle = *h;
@ -232,8 +223,6 @@ int CASIO_EXPORT casio_decode_std_cg_addin(casio_file_t **h,
/* read content */
GREAD(handle->casio_file_content, handle->casio_file_size)
*check = casio_checksum32(handle->casio_file_content,
handle->casio_file_size, *check);
/* no error */
return (0);

117
src/file/decode/std/dict.c Normal file
View File

@ -0,0 +1,117 @@
/* ****************************************************************************
* file/decode/std/dict.c -- dictionary decoding.
* Copyright (C) 2017 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
*
* 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 <http://www.gnu.org/licenses/>.
* ************************************************************************* */
#include "dict.h"
/**
* casio_decode_std_dict:
* Decode a dictionary.
*
* @arg buffer the stream from which to read from.
* @arg callback the callback.
* @arg cookie the callback cookie.
* @arg num the number of entries.
* @arg size the total size (index + content + alignment).
* @arg is32 32-bit offsets instead of 16-bit ones.
* @return the error code (0 if ok).
*/
int CASIO_EXPORT casio_decode_std_dict(casio_stream_t *buffer,
casio_decode_dict_entry_t *callback, void *cookie,
unsigned int num, unsigned long size, int is32)
{
int err, has_last_index = 0;
unsigned int i, last_index;
casio_uint16_t *index16 = NULL;
casio_uint32_t *index32 = NULL;
unsigned char *content = NULL;
unsigned long last_offset = -1;
unsigned long current_offset = 0;
/* Read the index. */
msg((ll_info, "Allocating and reading the index."));
if (is32) {
index32 = casio_alloc(num + 1, sizeof(casio_uint32_t));
if (!index32) return (casio_error_alloc);
GREAD(index32, (num + 1) * sizeof(casio_uint32_t))
size -= sizeof(casio_uint32_t) * (num + 1);
} else {
index16 = casio_alloc(num, sizeof(casio_uint16_t));
if (!index16) return (casio_error_alloc);
GREAD(index16, num * sizeof(casio_uint16_t))
size -= sizeof(casio_uint16_t) * num;
}
/* Read the entries. */
msg((ll_info, "Reading %u entries.", num));
for (i = 0; i < num; i++) {
/* Get the offset, check if it is valid. */
if ((!is32 && (current_offset = be16toh(index16[i])) == 0xFFFF)
|| ( is32 && (current_offset = be32toh(index32[i])) == 0xFFFFFFFF)) {
msg((ll_info, "Entry %u is invalid", i));
continue;
}
/* If there is a last content, manage it. */
if (has_last_index) {
unsigned long content_size = current_offset - last_offset;
/* Allocate and read the content. */
msg((ll_info, "Entry %u is valid!", i));
content = casio_alloc(content_size, 1);
if (!content) { err = casio_error_alloc; goto fail; }
GREAD(content, content_size)
/* Send it to the user. */
err = (*callback)(cookie, last_index, content, content_size);
if (err) goto fail;
/* Free the content. */
free(content);
}
/* Update the last offset. */
has_last_index = 1;
last_index = i;
last_offset = current_offset;
}
/* Read the last entry. */
if (has_last_index) {
unsigned long content_size = size - current_offset;
/* Allocate and read the content. */
content = casio_alloc(content_size, 1);
if (!content) { err = casio_error_alloc; goto fail; }
GREAD(content, content_size)
/* Send it to the user. */
err = (*callback)(cookie, last_index, content, content_size);
if (err) goto fail;
/* Free the content. */
free(content);
}
free(index16);
return (0);
fail:
free(index16);
free(content);
return (err);
}

View File

@ -0,0 +1,32 @@
/* ****************************************************************************
* file/decode/std/dict.h -- dictionary decoding.
* Copyright (C) 2017 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
*
* 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 <http://www.gnu.org/licenses/>.
* ************************************************************************* */
#ifndef LOCAL_FILE_DECODE_STD_DICT_H
# define LOCAL_FILE_DECODE_STD_DICT_H 1
# include "../decode.h"
typedef int casio_decode_dict_entry_t
OF((void *casio__cookie, unsigned int casio__id,
void *casio__mem, size_t casio__size));
CASIO_EXTERN int CASIO_EXPORT casio_decode_std_dict
OF((casio_stream_t *casio__buffer,
casio_decode_dict_entry_t *casio__callback, void *casio__cookie,
unsigned int casio__num, unsigned long casio__size, int casio__is32));
#endif /* LOCAL_FILE_DECODE_STD_DICT_H */

View File

@ -109,7 +109,7 @@ int CASIO_EXPORT casio_decode_std_fkey(casio_file_t **h,
for (handle->casio_file_count = 0; handle->casio_file_count < (int)num;
handle->casio_file_count++) {
int i = handle->casio_file_count;
if (offsets[i] == (uint16_t)-1)
if (offsets[i] == (casio_uint16_t)-1)
continue ;
offsets[i] = be16toh(offsets[i]);
@ -139,14 +139,12 @@ fail:
* @arg std the standard header.
* @arg sub the standard subheader.
* @arg prizm the prizm-specific subheader.
* @arg check the checksum to feed.
* @return the error code (0 if ok).
*/
int CASIO_EXPORT casio_decode_std_cg_fkey(casio_file_t **h,
casio_stream_t *buffer, casio_standard_header_t *std,
casio_standard_subheader_t *sub, casio_standard_prizm_subheader_t *prizm,
casio_uint32_t *check)
casio_standard_subheader_t *sub, casio_standard_prizm_subheader_t *prizm)
{
int err = casio_error_alloc;
casio_prizm_lang_subheader_t lhd; unsigned int num;
@ -157,8 +155,6 @@ int CASIO_EXPORT casio_decode_std_cg_fkey(casio_file_t **h,
/* read the subheader */
DREAD(lhd)
*check = casio_checksum32(&lhd,
sizeof(casio_prizm_lang_subheader_t), *check);
/* read the data */
data_size = be32toh(sub->casio_standard_subheader_filesize)
@ -168,7 +164,6 @@ int CASIO_EXPORT casio_decode_std_cg_fkey(casio_file_t **h,
- sizeof(casio_prizm_lang_subheader_t) - 4;
if (!(data = casio_alloc(data_size, 1))) goto fail;
READ(data, data_size)
*check = casio_checksum32(data, data_size, *check);
/* make the handle */
num = be32toh(lhd.casio_prizm_lang_subheader_count);
@ -178,20 +173,20 @@ int CASIO_EXPORT casio_decode_std_cg_fkey(casio_file_t **h,
/* setup the pointers */
offsets = (void*)data;
messages = (uint8_t*)&offsets[num + 1];
messages = (casio_uint8_t*)&offsets[num + 1];
/* read messages */
for (handle->casio_file_count = 0; handle->casio_file_count < (int)num;
handle->casio_file_count++) {
int i = handle->casio_file_count;
if (offsets[i] == (uint32_t)-1) {
if (offsets[i] == (casio_uint32_t)-1) {
msg((ll_info, "[#%d] -", i));
continue;
}
/* correct offset and log */
offsets[i] = be32toh(offsets[i]);
msg((ll_info, "[#%d] '%s' (0x%" PRIu32 ")", i,
msg((ll_info, "[#%d] '%s' (0x%" CASIO_PRIu32 ")", i,
&messages[offsets[i]], offsets[i]));
/* store */

View File

@ -17,7 +17,42 @@
* along with libcasio; if not, see <http://www.gnu.org/licenses/>.
* ************************************************************************* */
#include "../decode.h"
#include "dict.h"
/**
* decode_lang_entry:
* Decode a language file entry.
*
* @arg file the file.
* @arg id the entry ID.
* @arg mem the memory.
* @arg size the memory size.
* @return the error code (0 if ok).
*/
CASIO_LOCAL int decode_lang_entry(casio_file_t *handle,
unsigned int id, void *mem, size_t size)
{
size_t len; void *chr;
char *str;
/* Find the length. */
chr = memchr(mem, '\0', size);
len = chr ? (size_t)((char*)chr - (char*)mem) : size;
/* Allocate. */
handle->casio_file_messages[id] = casio_alloc(len + 1, 1);
str = handle->casio_file_messages[id];
if (!str) return (casio_error_alloc);
/* Copy the data. */
memcpy(str, mem, len);
str[len] = 0;
/* Log it and return. */
msg((ll_info, "[#%d] '%s'", id, str));
return (0);
}
/* ************************************************************************* */
/* fx language files parsing */
/* ************************************************************************* */
@ -33,69 +68,41 @@
int CASIO_EXPORT casio_decode_std_lang(casio_file_t **h,
casio_stream_t *buffer, casio_standard_header_t *std)
{
int err = casio_error_alloc;
casio_lang_subheader_t hd; unsigned int num;
char *data; size_t data_size;
casio_uint16_t *offsets; char *messages;
casio_file_t *handle;
int err; casio_file_t *handle;
casio_lang_subheader_t hd;
unsigned int num;
unsigned long dict_size;
(void)std;
(void)std; *h = NULL;
/* read the subheader */
DREAD(hd) num = be16toh(hd.casio_lang_subheader_count) + 1;
/* log */
msg((ll_info, "%u messages to read", num));
GDREAD(hd)
num = be16toh(hd.casio_lang_subheader_count) + 1;
/* beat the best, read the rest! */
data_size = std->casio_standard_header_filesize
dict_size = std->casio_standard_header_filesize
- sizeof(casio_standard_header_t)
- sizeof(casio_lang_subheader_t);
if (!(data = casio_alloc(data_size, 1))) goto fail;
READ(data, data_size)
/* make the handle */
err = casio_make_lang(h, casio_filefor_fx, num);
if (err) return (err);
handle = *h;
/* get the offset table */
offsets = (casio_uint16_t*)data;
messages = (char*)(offsets + num);
/* read messages */
for (handle->casio_file_count = 0; handle->casio_file_count < (int)num;
handle->casio_file_count++) {
int i = handle->casio_file_count;
if (offsets[i] == (casio_uint16_t)-1) {
msg((ll_info, "[#%d] -", i));
continue;
}
/* correct offset and log */
offsets[i] = be16toh(offsets[i]);
msg((ll_info, "[#%d] '%s' (0x%" CASIO_PRIu16 ")", i,
&messages[offsets[i]], offsets[i]));
/* store */
handle->casio_file_messages[i] = strdup(&messages[offsets[i]]);
if (!handle->casio_file_messages[i]) goto fail;
handle->casio_file_count++;
}
/* read using the dictionary decoding function. */
err = casio_decode_std_dict(buffer,
(casio_decode_dict_entry_t*)&decode_lang_entry, handle,
num, dict_size, 0);
if (err) goto fail;
/* no error */
casio_free(data);
return (0);
/* omg fail! */
fail:
casio_free(data);
casio_free_file(*h); *h = NULL;
return (err);
}
/* ************************************************************************* */
/* Decoding language files for fx-CG/Prizm (G3L) */
/* ************************************************************************* */
/**
* casio_decode_std_cg_lang:
* Decode fx-CG language file.
@ -105,73 +112,44 @@ fail:
* @arg std the standard header.
* @arg sub the standard subheader.
* @arg prizm the prizm-specific subheader.
* @arg check the checksum to feed.
* @return the error code (0 if ok).
*/
int CASIO_EXPORT casio_decode_std_cg_lang(casio_file_t **h,
casio_stream_t *buffer, casio_standard_header_t *std,
casio_standard_subheader_t *sub, casio_standard_prizm_subheader_t *prizm,
casio_uint32_t *check)
casio_standard_subheader_t *sub, casio_standard_prizm_subheader_t *prizm)
{
int err = casio_error_alloc;
casio_prizm_lang_subheader_t lhd; unsigned int num;
casio_uint8_t *data; size_t data_size;
casio_uint32_t *offsets; char *messages;
casio_file_t *handle;
(void)std; (void)prizm;
int err; casio_file_t *handle;
casio_prizm_lang_subheader_t lhd;
unsigned int num;
unsigned long dict_size;
(void)std; (void)prizm;
/* read the subheader */
DREAD(lhd)
*check = casio_checksum32(&lhd, sizeof(casio_prizm_lang_subheader_t),
*check);
/* read the data */
data_size =
be32toh(sub->casio_standard_subheader_filesize)
num = be32toh(lhd.casio_prizm_lang_subheader_count);
dict_size = be32toh(sub->casio_standard_subheader_filesize)
- sizeof(casio_standard_header_t)
- sizeof(casio_standard_subheader_t)
- sizeof(casio_standard_prizm_subheader_t)
- sizeof(casio_lang_subheader_t) - 4;
if (!(data = casio_alloc(data_size, 1))) goto fail;
READ(data, data_size)
*check = casio_checksum32(data, data_size, *check);
- sizeof(casio_lang_subheader_t)
- sizeof(casio_uint32_t);
/* make the handle */
num = be32toh(lhd.casio_prizm_lang_subheader_count);
err = casio_make_lang(h, casio_filefor_cg, num);
if (err) return (err);
handle = *h;
/* setup the pointers */
offsets = (void*)data;
messages = (char*)&offsets[num + 1];
/* read messages */
for (handle->casio_file_count = 0; handle->casio_file_count < (int)num;
handle->casio_file_count++) {
int i = handle->casio_file_count;
if (offsets[i] == (uint32_t)-1) {
msg((ll_info, "[#%d] -", i));
continue;
}
/* correct offset and log */
offsets[i] = be32toh(offsets[i]);
msg((ll_info, "[#%d] '%s' (0x%" PRIu32 ")", i,
&messages[offsets[i]], offsets[i]));
/* store */
handle->casio_file_messages[i] = strdup((char*)&messages[offsets[i]]);
if (!handle->casio_file_messages[i]) goto fail;
}
/* read using the dictionary decoding function. */
err = casio_decode_std_dict(buffer,
(casio_decode_dict_entry_t*)&decode_lang_entry, handle,
num, dict_size, 1);
if (err) goto fail;
/* done */
casio_free(data);
return (0);
fail:
casio_free(data);
casio_free_file(*h); *h = NULL;
return (err);
}

View File

@ -44,7 +44,7 @@ int CASIO_EXPORT casio_decode_std_mcs(casio_file_t **h,
handle = *h;
/* read all of the parts */
msg((ll_info, "%" PRIuFAST16 " total mcs files to browse", num));
msg((ll_info, "%u total mcs files to browse", num));
while (num) {
casio_mcs_subheader_t hd;

View File

@ -30,10 +30,10 @@
* @arg n the buffer size.
*/
CASIO_LOCAL void g3p_deobfuscate(uint8_t *buf, size_t n)
CASIO_LOCAL void g3p_deobfuscate(unsigned char *buf, size_t n)
{
while (n--) {
uint8_t byte = ~*buf;
unsigned char byte = ~*buf;
*buf++ = (byte << 5) | (byte >> 3);
}
}
@ -77,7 +77,8 @@ int CASIO_EXPORT casio_decode_std_g3p(casio_file_t **h, casio_stream_t *buffer,
case g3p_color_16bit: picfmt = casio_pictureformat_16bit;
coldesc = "16-bit"; break;
default:
msg((ll_error, "Unknown picture format: 0x%04" PRIX16, rawcol));
msg((ll_error, "Unknown picture format: 0x%04" CASIO_PRIX16,
rawcol));
err = casio_error_magic; goto fail;
}
msg((ll_info, "Picture format: %s", coldesc));

View File

@ -58,10 +58,11 @@
/* Picture utilities */
/* ************************************************************************* */
# define alloc_pixels(W, H) \
casio_alloc(sizeof(uint32_t*) * (H) + sizeof(uint32_t) * (W) * (H), 1)
casio_alloc(sizeof(casio_pixel_t*) * \
(H) + sizeof(casio_pixel_t) * (W) * (H), 1)
# define prepare_pixels(I, W, H) { \
unsigned int PIXPREP_y; \
uint32_t *PIXPREP_line = (uint32_t*)&(I)[(H)]; \
casio_pixel_t *PIXPREP_line = (casio_pixel_t*)&(I)[(H)]; \
for (PIXPREP_y = 0; PIXPREP_y < (H); PIXPREP_y++) { \
(I)[PIXPREP_y] = PIXPREP_line; \
PIXPREP_line += (W); \

View File

@ -111,9 +111,9 @@ int CASIO_EXPORT casio_make_fkey(casio_file_t **h,
/* allocate index */
if (count) {
handle->casio_file_fkeys = casio_alloc(count, sizeof(uint32_t**));
handle->casio_file_fkeys = casio_alloc(count, sizeof(casio_pixel_t**));
if (!handle->casio_file_fkeys) goto fail;
memset(handle->casio_file_fkeys, 0, sizeof(uint32_t**) * count);
memset(handle->casio_file_fkeys, 0, sizeof(casio_pixel_t**) * count);
handle->casio_file__size = count;
}
@ -187,12 +187,19 @@ int CASIO_EXPORT casio_make_addin(casio_file_t **h,
/* make checks */
if (platform != casio_filefor_fx && platform != casio_filefor_cg
&& platform != casio_filefor_cp)
&& platform != casio_filefor_cp) {
msg((ll_error, "Not for the FX, FX-CG or FX-CP platform!"));
return (casio_error_op);
if (platform == casio_filefor_fx && size > 512 * 1024)
}
if (platform == casio_filefor_fx && size > 512 * 1024) {
msg((ll_error, "FX Addin should be no more than 512 KiB!"));
return (casio_error_op);
if (!isupper(name[0]) || internal[0] != '@' || !isupper(internal[1]))
}
if (!isupper(name[0]) || internal[0] != '@' || !isupper(internal[1])) {
msg((ll_error, "Internal name was invalid! Got:"));
mem((ll_error, internal, strlen(internal)));
return (casio_error_op);
}
/* make the handle */
mkhandle();

100
src/fs/builtin/posix/path.c Normal file
View File

@ -0,0 +1,100 @@
/* ****************************************************************************
* fs/builtin/posix/path.c -- POSIX path conversions.
* Copyright (C) 2017 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
*
* 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 <http://www.gnu.org/licenses/>.
* ************************************************************************* */
#include "posix.h"
/**
* makepathnode:
* Make a directory node.
*
* @arg size the name size.
* @return the allocated path node.
*/
CASIO_LOCAL casio_pathnode_t *makepathnode(size_t size)
{
casio_pathnode_t *node;
node = malloc(offsetof(casio_pathnode_t, casio_pathnode_name) + size);
if (!node) return (NULL);
node->casio_pathnode_next = NULL;
return (node);
}
/**
* casio_make_posix_path_array:
* Make a path array from a POSIX path.
*
* POSIX paths are soooo well done. <3
*
* @arg ppath the final path.
* @arg rawpath the raw POSIX path.
* @return the error code (0 if ok).
*/
int CASIO_EXPORT casio_make_posix_path_array(casio_path_t **ppath,
const char *rawpath)
{
casio_path_t *path = NULL;
casio_pathnode_t **node = NULL;
casio_pathnode_t *localnode = NULL;
size_t nodelen;
/* Make and initialize the path structure. */
*ppath = malloc(sizeof(casio_path_t));
path = *ppath; if (!path) return (casio_error_alloc);
path->casio_path_flags = casio_pathflag_alloc | casio_pathflag_rel;
path->casio_path_device = NULL;
path->casio_path_nodes = NULL;
/* Check if it is an absolute path. */
if (*rawpath == '/') {
path->casio_path_flags &= ~casio_pathflag_rel;
rawpath++;
}
/* Split the path. */
node = &path->casio_path_nodes;
while (rawpath) {
rawpath += strspn(rawpath, "/");
if (!rawpath) break;
/* Get the entry size (to '/' or NUL). */
nodelen = strcspn(rawpath, "/");
/* Make the entry. */
localnode = makepathnode(nodelen + 1);
if (!localnode) goto fail;
memcpy(localnode->casio_pathnode_name, rawpath, nodelen);
localnode->casio_pathnode_name[nodelen] = 0;
*node = localnode;
node = &localnode->casio_pathnode_next;
/* Iterate. */
rawpath += nodelen;
}
return (0);
fail:
while (path->casio_path_nodes) {
localnode = path->casio_path_nodes;
path->casio_path_nodes = localnode->casio_pathnode_next;
free(localnode);
}
return (casio_error_alloc);
}

View File

@ -0,0 +1,26 @@
/* ****************************************************************************
* fs/builtin/posix/posix.h -- POSIX filesystem internals.
* Copyright (C) 2017 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
*
* 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 <http://www.gnu.org/licenses/>.
* ************************************************************************* */
#ifndef LOCAL_FS_BUILTIN_POSIX_H
# define LOCAL_FS_BUILTIN_POSIX_H 1
# include "../../../internals.h"
CASIO_EXTERN int CASIO_EXPORT casio_make_posix_path_array
OF((casio_path_t **casio__path, const char *casio__rawpath));
#endif /* LOCAL_FS_BUILTIN_POSIX_H */

View File

@ -1,5 +1,5 @@
/* ****************************************************************************
* filesystem/filesystem.h -- libcasio filesystem internals.
* fs/fs.h -- libcasio filesystem internals.
* Copyright (C) 2017 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
*
* This file is part of libcasio.
@ -16,8 +16,8 @@
* You should have received a copy of the GNU Lesser General Public License
* along with libcasio; if not, see <http://www.gnu.org/licenses/>.
* ************************************************************************* */
#ifndef LOCAL_FILESYSTEM_H
# define LOCAL_FILESYSTEM_H 1
#ifndef LOCAL_FS_H
# define LOCAL_FS_H 1
# include "../internals.h"
struct casio_filesystem_s {
@ -27,4 +27,4 @@ struct casio_filesystem_s {
casio_fsfuncs_t casio_filesystem_functions;
};
#endif /* LOCAL_FILESYSTEM_H */
#endif /* LOCAL_FS_H */

View File

@ -22,8 +22,6 @@
* custom stream, probably for its platform.
* ************************************************************************* */
#include "link.h"
#include <stdlib.h>
#include <string.h>
/**
* casio_open_link:

View File

@ -1,5 +1,5 @@
/* ****************************************************************************
* link/open_com.c -- open a serial communication.
* link/open/com.c -- open a serial communication.
* Copyright (C) 2016-2017 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
*
* This file is part of libcasio.
@ -16,7 +16,7 @@
* You should have received a copy of the GNU Lesser General Public License
* along with libcasio; if not, see <http://www.gnu.org/licenses/>.
* ************************************************************************* */
#include "link.h"
#include "../link.h"
/**
* casio_open_com:

View File

@ -1,5 +1,5 @@
/* ****************************************************************************
* link/open_usb.c -- open a USB communication.
* link/open/usb.c -- open a USB communication.
* Copyright (C) 2016-2017 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
*
* This file is part of libcasio.
@ -16,7 +16,7 @@
* You should have received a copy of the GNU Lesser General Public License
* along with libcasio; if not, see <http://www.gnu.org/licenses/>.
* ************************************************************************* */
#include "link.h"
#include "../link.h"
/**
* casio_open_usb:

View File

@ -35,7 +35,7 @@ int CASIO_EXPORT casio_getscreen(casio_link_t *handle,
casio_link_screen_t *callback, void *cbcookie)
{
int err = 0, y;
uint32_t **pixels = NULL, *base;
casio_pixel_t **pixels = NULL, *base;
unsigned int width, height;
/* make checks. */
@ -44,8 +44,8 @@ int CASIO_EXPORT casio_getscreen(casio_link_t *handle,
chk_passive(handle);
/* allocate pixels. */
pixels = casio_alloc(sizeof(uint32_t*) * HEIGHT
+ sizeof(uint32_t) * WIDTH * HEIGHT, 1);
pixels = casio_alloc(sizeof(casio_pixel_t*) * HEIGHT
+ sizeof(casio_pixel_t) * WIDTH * HEIGHT, 1);
if (!pixels) failure(alloc);
/* prepare pixels. */

82
src/log/conv.c Normal file
View File

@ -0,0 +1,82 @@
/* ****************************************************************************
* log/conv.c -- convert between strings and number log levels.
* Copyright (C) 2016-2017 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
*
* 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 <http://www.gnu.org/licenses/>.
* ************************************************************************* */
#include "log.h"
#if !defined(LIBCASIO_DISABLED_LOG)
/**
* casio_loglevel_tostring:
* Make a string out of a log level.
*
* @arg level the log level.
* @return the string.
*/
const char* CASIO_EXPORT casio_loglevel_tostring(casio_loglevel_t level)
{
if (level >= 0 && level < 10)
return ("info");
if (level >= 10 && level < 20)
return ("warn");
if (level >= 20 && level < 30)
return ("error");
if (level >= 30 && level < 40)
return ("fatal");
return ("none");
}
/**
* casio_loglevel_fromstring:
* Make a log level out of a string.
*
* @arg string the string.
* @return the log level.
*/
casio_loglevel_t CASIO_EXPORT casio_loglevel_fromstring(const char *string)
{
if (!strcmp(string, "info"))
return (casio_loglevel_info);
if (!strcmp(string, "warn"))
return (casio_loglevel_warn);
if (!strcmp(string, "error"))
return (casio_loglevel_error);
if (!strcmp(string, "fatal"))
return (casio_loglevel_fatal);
return (casio_loglevel_none);
}
#endif
/**
* casio_listlog:
* List log levels.
*
* @arg callback the callback.
* @arg cookie the callback cookie.
*/
void CASIO_EXPORT casio_listlog(casio_log_list_t *callback, void *cookie)
{
(*callback)(cookie, "none");
#if !defined(LIBCASIO_DISABLED_LOG)
(*callback)(cookie, "info");
(*callback)(cookie, "warn");
(*callback)(cookie, "error");
(*callback)(cookie, "fatal");
#endif
}

View File

@ -26,17 +26,30 @@
# include "../internals.h"
# include <stdio.h>
/* These definitions used to be public, but they went private in order to
* not have numbers *and* strings in the public interface.
* Strings allow an easier future compatibility. */
typedef int casio_loglevel_t;
# define casio_loglevel_info 0
# define casio_loglevel_warn 10
# define casio_loglevel_error 20
# define casio_loglevel_fatal 30
# define casio_loglevel_none 40
/* Cross-compiler and cross-standard `__func__` variable to display
* the function on logging. */
# if defined(__cplusplus) ? CASIO_GNUC_PREREQ(2, 6) \
# if defined(__cplusplus) ? CASIO_GNUC_PREREQ(2, 6) \
: !defined(__STRICT_ANSI__) && CASIO_GNUC_PREREQ(2, 4)
# define CASIO_LOGFUNC __PRETTY_FUNCTION__
# elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
# define CASIO_LOGFUNC __func__
# else
# define CASIO_LOGFUNC NULL
# endif
# define CASIO_LOGFUNC __PRETTY_FUNCTION__
# elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
# define CASIO_LOGFUNC __func__
# elif !defined(__STRICT_ANSI__) && CASIO_GNUC_PREREQ(2, 0)
# define CASIO_LOGFUNC __FUNCTION__
# else
# define CASIO_LOGFUNC NULL
# endif
/* Log levels */
# define ll_info casio_loglevel_info, CASIO_LOGFUNC
@ -98,6 +111,14 @@
# define elsemem( CASIO__ARGS) \
else mem(CASIO__ARGS)
/* Conversion functions between strings and numbers.
* Strings are for the external API, numbers for the internal one. */
CASIO_EXTERN const char* CASIO_EXPORT casio_loglevel_tostring
OF((casio_loglevel_t casio__level));
CASIO_EXTERN casio_loglevel_t CASIO_EXPORT casio_loglevel_fromstring
OF((const char *casio__string));
/* Here are the main functions. Don't use them directly, prefer the
* `msg` and `mem` macros as they are sensible to the fact that logging
* is enabled or not. */

View File

@ -1,5 +1,5 @@
/* ****************************************************************************
* log/manage.c -- manage libcasio logging utilities.
* log/manage.c -- fiddle with libcasio log settings.
* Copyright (C) 2016-2017 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
*
* This file is part of libcasio.
@ -17,15 +17,10 @@
* along with libcasio; if not, see <http://www.gnu.org/licenses/>.
* ************************************************************************* */
#include "log.h"
#undef islog
#define islog(CASIO__LOGLEVEL) \
(casio_getlog() <= (CASIO__LOGLEVEL))
/* ************************************************************************* */
/* Interact with the log settings at runtime */
/* ************************************************************************* */
#if !defined(LIBCASIO_DISABLED_LOG)
/* The log setting */
CASIO_LOCAL casio_loglevel_t log_setting = LOGLEVEL;
#endif
/**
* casio_setlog:
@ -34,9 +29,13 @@ CASIO_LOCAL casio_loglevel_t log_setting = LOGLEVEL;
* @arg level the level to set.
*/
void CASIO_EXPORT casio_setlog(casio_loglevel_t level)
void CASIO_EXPORT casio_setlog(const char *level)
{
log_setting = level;
#if !defined(LIBCASIO_DISABLED_LOG)
log_setting = casio_loglevel_fromstring(level);
#else
(void)level;
#endif
}
/**
@ -46,71 +45,16 @@ void CASIO_EXPORT casio_setlog(casio_loglevel_t level)
* @return the current level.
*/
casio_loglevel_t CASIO_EXPORT casio_getlog(void)
const char* CASIO_EXPORT casio_getlog(void)
{
return (log_setting);
}
/* ************************************************************************* */
/* Make conversions between settings values and strings, list settings */
/* ************************************************************************* */
/**
* casio_loglevel_tostring:
* Make a string out of a log level.
*
* @arg level the log level.
* @return the string.
*/
const char* CASIO_EXPORT casio_loglevel_tostring(casio_loglevel_t level)
{
if (level >= 0 && level < 10)
return ("info");
if (level >= 10 && level < 20)
return ("warn");
if (level >= 20 && level < 30)
return ("error");
if (level >= 30 && level < 40)
return ("fatal");
#if !defined(LIBCASIO_DISABLED_LOG)
return (casio_loglevel_tostring(log_setting));
#else
return ("none");
#endif
}
/**
* casio_loglevel_fromstring:
* Make a log level out of a string.
*
* @arg string the string.
* @return the log level.
*/
casio_loglevel_t CASIO_EXPORT casio_loglevel_fromstring(const char *string)
{
if (!strcmp(string, "info"))
return (casio_loglevel_info);
if (!strcmp(string, "warn"))
return (casio_loglevel_warn);
if (!strcmp(string, "error"))
return (casio_loglevel_error);
if (!strcmp(string, "fatal"))
return (casio_loglevel_fatal);
return (casio_loglevel_none);
}
/**
* casio_listlog:
* List log levels.
*
* @arg callback the callback.
* @arg cookie the callback cookie.
*/
void CASIO_EXPORT casio_listlog(casio_log_list_t *callback, void *cookie)
{
(*callback)(cookie, "info");
(*callback)(cookie, "warn");
(*callback)(cookie, "error");
(*callback)(cookie, "fatal");
}
#if !defined(LIBCASIO_DISABLED_LOG)
/**
* casio_islog:
* Get the log level out of the ll_* tuples.
@ -123,5 +67,6 @@ void CASIO_EXPORT casio_listlog(casio_log_list_t *callback, void *cookie)
int CASIO_EXPORT casio_islog(casio_loglevel_t level, const char *func)
{
(void)func;
return (islog(level));
return (log_setting <= level);
}
#endif

View File

@ -17,9 +17,6 @@
* along with libcasio; if not, see <http://www.gnu.org/licenses/>.
* ************************************************************************* */
#include "log.h"
#undef islog
#define islog(CASIO__LOGLEVEL) \
(casio_getlog() <= (CASIO__LOGLEVEL))
#ifndef LIBCASIO_DISABLED_LOG
/**
@ -88,7 +85,7 @@ void CASIO_EXPORT casio_log_mem(casio_loglevel_t loglevel, const char *func,
const unsigned char *p;
/* check if the log level is good */
if (!islog(loglevel)) return ;
if (!casio_islog(loglevel, NULL)) return ;
/* if nothing, print it directly */
if (!n) {

View File

@ -18,9 +18,6 @@
* ************************************************************************* */
#include "log.h"
#include <stdarg.h>
#undef islog
#define islog(CASIO__LOGLEVEL) \
(casio_getlog() <= (CASIO__LOGLEVEL))
#if !defined(LIBCASIO_DISABLED_LOG)
/**
@ -55,13 +52,15 @@ void CASIO_EXPORT casio_log_msg(casio_loglevel_t loglevel,
const char *func, const char *format, ...)
{
va_list args;
int shouldlog = casio_islog(loglevel, NULL);
/* put the prefix */
if (islog(loglevel)) casio_log_prefix(loglevel, func);
if (shouldlog)
casio_log_prefix(loglevel, func);
/* put the main part */
va_start(args, format);
if (islog(loglevel)) {
if (shouldlog) {
vfprintf(stderr, format, args);
fputc('\n', stderr);
}

View File

@ -20,7 +20,7 @@
* https://casetta.tuxfamily.org/formats/cas
* ************************************************************************* */
#include "decode.h"
#include "../reference/reference.h"
#include "../ref/ref.h"
#define FUNC(NAME) &casio_decode_caspart_##NAME
#define HFUNC(NAME) &casio_decode_cashpart_##NAME
@ -125,7 +125,7 @@ CASIO_LOCAL int decode_cas50(casio_mcshead_t *head, casio_stream_t *buffer,
len = end ? (size_t)(end - (char*)hd.casio_cas50_aux) : 8;
memcpy(head->casio_mcshead_password, hd.casio_cas50_aux, len);
head->casio_mcshead_password[len] = 0;
msg((ll_info, "Is a program of %" PRIuFAST32 " bytes",
msg((ll_info, "Is a program of %lu bytes",
head->casio_mcshead_size));
break;
case casio_mcstype_variable:

View File

@ -44,8 +44,8 @@ CASIO_LOCAL int decode_cell(casio_stream_t *buffer, casio_mcscell_t *cell,
/* read position */
DREAD(fx) *x = be16toh(fx) - 1;
DREAD(fy) *y = be16toh(fy) - 1;
csum = casio_checksum_cas(&fx, sizeof(uint16_t), csum);
csum = casio_checksum_cas(&fy, sizeof(uint16_t), csum);
csum = casio_checksum_cas(&fx, sizeof(casio_uint16_t), csum);
csum = casio_checksum_cas(&fy, sizeof(casio_uint16_t), csum);
/* read the parts */
DREAD(wkg)
@ -64,7 +64,6 @@ CASIO_LOCAL int decode_cell(casio_stream_t *buffer, casio_mcscell_t *cell,
/* no prob'! */
return (0);
}
/* ************************************************************************* */
/* Cell-reading CAS part decoding functions */
/* ************************************************************************* */

View File

@ -1,5 +1,5 @@
/* ****************************************************************************
* mcsfile/decode/cas/screenshot.c -- decode a CAS screenshot.
* mcsfile/decode/cas/sshot.c -- decode a CAS screenshot.
* Copyright (C) 2017 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
*
* This file is part of libcasio.

View File

@ -33,7 +33,7 @@ int CASIO_EXPORT casio_decode_mcs_capture(casio_mcsfile_t **h,
{
int err;
casio_mcs_captureheader_t hd;
uint8_t *pic_raw = NULL; size_t pic_size;
unsigned char *pic_raw = NULL; size_t pic_size;
casio_mcsfile_t *handle;
/* read header */

View File

@ -1,5 +1,5 @@
/* ****************************************************************************
* mcsfile/decode/mcs/spreadsheet.c -- decode an MCS spreadsheet.
* mcsfile/decode/mcs/ssheet.c -- decode an MCS spreadsheet.
* Copyright (C) 2017 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
*
* This file is part of libcasio.

View File

@ -32,8 +32,8 @@ int CASIO_EXPORT casio_decode_mcs_var(casio_mcsfile_t **handle,
casio_stream_t *buffer, casio_mcshead_t *head)
{
int err;
uint_fast32_t length = head->casio_mcshead_size;
uint8_t *buf = alloca(length);
unsigned long length = head->casio_mcshead_size;
unsigned char *buf = alloca(length);
const casio_mcsbcd_t *b;
casio_mcsfile_t *h;
int i;

View File

@ -1,5 +1,5 @@
/* ****************************************************************************
* mcsfile/duplicate.c -- duplicate an MCS file handle.
* mcsfile/dup.c -- duplicate an MCS file handle.
* Copyright (C) 2017 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
*
* This file is part of libcasio.

View File

@ -17,7 +17,7 @@
* along with libcasio; if not, see <http://www.gnu.org/licenses/>.
* ************************************************************************* */
#include "mcsfile.h"
#include "reference/reference.h"
#include "ref/ref.h"
/**
* casio_correct_mcshead:

View File

@ -1,5 +1,5 @@
/* ****************************************************************************
* mcsfile/reference/cas_app.c -- get the CAS app out of raw data.
* mcsfile/ref/cas_app.c -- get the CAS app out of raw data.
* Copyright (C) 2017 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
*
* This file is part of libcasio.
@ -16,7 +16,7 @@
* You should have received a copy of the GNU Lesser General Public License
* along with libcasio; if not, see <http://www.gnu.org/licenses/>.
* ************************************************************************* */
#include "reference.h"
#include "ref.h"
/* ************************************************************************* */
/* Local types */

View File

@ -1,5 +1,5 @@
/* ****************************************************************************
* mcsfile/reference/cas_data.c -- get the CAS type out of raw data.
* mcsfile/ref/cas_data.c -- get the CAS type out of raw data.
* Copyright (C) 2017 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
*
* This file is part of libcasio.
@ -16,7 +16,7 @@
* You should have received a copy of the GNU Lesser General Public License
* along with libcasio; if not, see <http://www.gnu.org/licenses/>.
* ************************************************************************* */
#include "reference.h"
#include "ref.h"
/* ************************************************************************* */
/* Local types */

View File

@ -1,5 +1,5 @@
/* ****************************************************************************
* mcsfile/reference/mcs.c -- get the MCS type out of raw data.
* mcsfile/ref/mcs.c -- get the MCS type out of raw data.
* Copyright (C) 2017 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
*
* This file is part of libcasio.
@ -16,7 +16,7 @@
* You should have received a copy of the GNU Lesser General Public License
* along with libcasio; if not, see <http://www.gnu.org/licenses/>.
* ************************************************************************* */
#include "reference.h"
#include "ref.h"
/* ************************************************************************* */
/* Local types */

View File

@ -1,5 +1,5 @@
/* ****************************************************************************
* mcsfile/reference/reference.h -- reference functions.
* mcsfile/ref/ref.h -- reference functions.
* Copyright (C) 2017 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
*
* This file is part of libcasio.
@ -16,8 +16,8 @@
* You should have received a copy of the GNU Lesser General Public License
* along with libcasio; if not, see <http://www.gnu.org/licenses/>.
* ************************************************************************* */
#ifndef LOCAL_MCSFILE_REFERENCE_H
# define LOCAL_MCSFILE_REFERENCE_H 1
#ifndef LOCAL_MCSFILE_REF_H
# define LOCAL_MCSFILE_REF_H 1
# include "../mcsfile.h"
/* MCS metadata. */
@ -39,4 +39,4 @@ CASIO_EXTERN int CASIO_EXPORT casio_correct_mcshead_to_casdata
CASIO_EXTERN int CASIO_EXPORT casio_check_cas_app
OF((int casio__ext, const char *casio__app));
#endif /* LOCAL_MCSFILE_REFERENCE_H */
#endif /* LOCAL_MCSFILE_REF_H */

101
src/stream/builtin/csum32.c Normal file
View File

@ -0,0 +1,101 @@
/* ****************************************************************************
* stream/builtin/csum32.c -- built-in stream for making a 32-bit checksum.
* Copyright (C) 2017 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
*
* 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 <http://www.gnu.org/licenses/>.
* ************************************************************************* */
#include "../../internals.h"
/* cookie structure */
struct thecookie {
casio_stream_t *_stream;
casio_uint32_t *_checksum;
};
/* ************************************************************************* */
/* Callbacks */
/* ************************************************************************* */
/**
* csum32_read:
* Read from this stream.
*
* @arg cookie the cookie.
* @arg data the data pointer.
* @arg size the data size.
* @return the error code (0 if ok).
*/
CASIO_LOCAL int csum32_read(struct thecookie *cookie,
unsigned char *dest, size_t size)
{
int err;
/* Make the call. */
err = casio_read(cookie->_stream, dest, size);
if (err) return (err);
/* Make the checksum. */
*cookie->_checksum = casio_checksum32(dest, size, *cookie->_checksum);
return (0);
}
/**
* csum32_close:
* Close the stream.
*
* @arg cookie the cookie.
* @return the error code (0 if ok).
*/
CASIO_LOCAL int csum32_close(struct thecookie *cookie)
{
casio_free(cookie);
return (0);
}
/* Callbacks. */
CASIO_LOCAL const casio_streamfuncs_t csum32_callbacks =
casio_stream_callbacks_for_virtual(csum32_close,
csum32_read, NULL, NULL);
/* ************************************************************************* */
/* Main functions */
/* ************************************************************************* */
/**
* casio_open_csum32:
* Open a 32-bit checksum stream.
*
* @arg stream the stream to make.
* @arg original the original stream.
* @arg csum the checksum pointer.
* @return the error code (0 if ok).
*/
int CASIO_EXPORT casio_open_csum32(casio_stream_t **stream,
casio_stream_t *original, casio_uint32_t *csum)
{
struct thecookie *cookie = NULL;
/* FIXME: check original stream */
/* allocate the cookie */
cookie = casio_alloc(1, sizeof(struct thecookie));
if (!cookie) return (casio_error_alloc);
/* fill the cookie */
cookie->_stream = original;
cookie->_checksum = csum;
/* Initialize da stream. */
return (casio_open(stream, casio_get_openmode(original),
casio_get_type(original), cookie, &csum32_callbacks));
}

View File

@ -45,7 +45,8 @@ typedef struct {
* @return the error code (0 if ok).
*/
CASIO_LOCAL int casio_libusb_settm(void *vcookie, const casio_timeouts_t *timeouts)
CASIO_LOCAL int casio_libusb_settm(void *vcookie,
const casio_timeouts_t *timeouts)
{
libusb_cookie_t *cookie = (void*)vcookie;
@ -69,7 +70,7 @@ CASIO_LOCAL int casio_libusb_close(void *vcookie)
{
libusb_cookie_t *cookie = (libusb_cookie_t*)vcookie;
if (cookie->_handle) libusb_close(cookie->_handle);
if (cookie->_handle) libusb_close(cookie->_handle);
if (cookie->_context) libusb_exit(cookie->_context);
casio_free(vcookie);
return (0);
@ -88,7 +89,8 @@ CASIO_LOCAL int casio_libusb_close(void *vcookie)
*/
# define ENDPOINT_IN (LIBUSB_ENDPOINT_IN | LIBUSB_TRANSFER_TYPE_BULK)
CASIO_LOCAL int casio_libusb_read(void *vcookie, unsigned char *dest, size_t size)
CASIO_LOCAL int casio_libusb_read(void *vcookie,
unsigned char *dest, size_t size)
{
int libusberr; libusb_cookie_t *cookie = (libusb_cookie_t*)vcookie;
size_t tocopy;
@ -117,6 +119,7 @@ CASIO_LOCAL int casio_libusb_read(void *vcookie, unsigned char *dest, size_t siz
case LIBUSB_ERROR_PIPE:
case LIBUSB_ERROR_NO_DEVICE:
case LIBUSB_ERROR_IO:
msg((ll_error, "The calculator is not here anymore :("));
return (casio_error_nocalc);
case LIBUSB_ERROR_TIMEOUT:
@ -171,6 +174,7 @@ CASIO_LOCAL int casio_libusb_write(void *vcookie,
case LIBUSB_ERROR_PIPE:
case LIBUSB_ERROR_NO_DEVICE:
msg((ll_error, "The calculator is not here anymore :("));
return (casio_error_nocalc);
default:
@ -284,7 +288,7 @@ int CASIO_EXPORT casio_openusb_libusb(casio_stream_t **stream)
/* disconnect any kernel driver */
msg((ll_info, "Detaching kernel driver, if any."));
uerr = libusb_detach_kernel_driver(dhandle, 0);
switch(uerr) {
switch (uerr) {
/* cases where it's okay */
case 0: case LIBUSB_ERROR_NOT_SUPPORTED:
case LIBUSB_ERROR_NOT_FOUND: break;
@ -309,7 +313,7 @@ int CASIO_EXPORT casio_openusb_libusb(casio_stream_t **stream)
/* claim the interface */
msg((ll_info, "Claiming the interface."));
uerr = libusb_claim_interface(dhandle, 0);
switch(uerr) {
switch (uerr) {
/* cases where it's okay (not a lot) */
case 0: break;

View File

@ -1,5 +1,5 @@
/* ****************************************************************************
* stream/main/com.c -- open a COM stream.
* stream/open/com.c -- open a COM stream.
* Copyright (C) 2017 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
*
* This file is part of libcasio.

View File

@ -1,5 +1,5 @@
/* ****************************************************************************
* stream/main/usb.c -- open a USB stream.
* stream/open/usb.c -- open a USB stream.
* Copyright (C) 2017 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
*
* This file is part of libcasio.

View File

@ -1,5 +1,5 @@
/* ****************************************************************************
* utils/extension.c -- file extension utilities.
* utils/ext.c -- file extension utilities.
* Copyright (C) 2017 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
*
* This file is part of libcasio.

View File

@ -70,11 +70,6 @@ cat <<_EOF
# define LIBCASIO_MAINTAINER \\
"${maintainer}"
/* Useful macros for checking libcasio's version */
# define LIBCASIO_PREREQ(CASIO__MAJ, CASIO__MIN) \\
((CASIO__MAJ) > (LIBCASIO_MAJOR) || \\
((CASIO__MAJ) == (LIBCASIO_MAJOR) && (CASIO__MIN) >= (LIBCASIO_MINOR)))
_EOF
# File part