From eb4546168ae41767c4571df7302d8524825454cf Mon Sep 17 00:00:00 2001 From: chad-earthscope Date: Wed, 24 Jan 2024 10:12:52 -0800 Subject: [PATCH] Update libmseed to v3.1.1 --- ChangeLog | 3 + libmseed/ChangeLog | 24 ++ libmseed/Makefile | 2 +- libmseed/Makefile.win | 2 +- libmseed/example/Makefile.win | 5 +- libmseed/example/lm_pack_rollingbuffer.c | 1 + libmseed/example/lm_read_recordlist.c | 10 +- libmseed/extraheaders.c | 2 +- libmseed/fileutils.c | 16 +- libmseed/genutils.c | 47 ++-- libmseed/libmseed.def | 5 +- libmseed/libmseed.h | 20 +- libmseed/logging.c | 6 +- libmseed/msio.c | 2 + libmseed/msrutils.c | 2 +- libmseed/pack.c | 335 ++++++++++++----------- libmseed/packdata.c | 124 ++++----- libmseed/packdata.h | 35 ++- libmseed/parseutils.c | 45 +-- libmseed/selection.c | 2 + libmseed/test/test-extraheaders.c | 4 +- libmseed/test/test-runner.c | 6 +- libmseed/tracelist.c | 35 ++- libmseed/unpack.c | 32 ++- libmseed/unpackdata.c | 173 ++++++------ libmseed/unpackdata.h | 49 ++-- src/msi.c | 2 +- 27 files changed, 519 insertions(+), 470 deletions(-) diff --git a/ChangeLog b/ChangeLog index 5306786..6c75c22 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,6 @@ +2024.024: 4.2 + - Update libmseed to v3.1.1. + 2024.006: 4.1 - Update libmseed to v3.0.18. diff --git a/libmseed/ChangeLog b/libmseed/ChangeLog index 994f48f..5c53f5b 100644 --- a/libmseed/ChangeLog +++ b/libmseed/ChangeLog @@ -1,3 +1,27 @@ +2024.024: 3.1.1 + - Change library compatibility version in Makefile to MAJOR.1.0, as this is now + incompatible with the x.0.0 releases. + +2024.024: 3.1.0 + BREAKING CHANGES, data structure and an API changes: + - `MS3Record.encoding` now int16_t instead of int8_t. + - `MS3Record.datalength` now uint32_t instead of uint16_t (change in 3.0.18). + - `MS3Record.datasize` and `MS3TraceSeg.datasize` now uint64_t instead of size_t. + - `ms3_detect()` now returns int64_t instead of int. + - `mstl3_unpack_recordlist()`'s `outputsize` argument is now uint64_t instead of size_t. + - `mseh_get_ptr_r()`'s `maxlength` is now uint32_t instead of size_t. + + - Fix to handle full range of allowed encoding values from 0-255. + - Fix to handle detection of huge record lengths beyond MAXRECLEN gracefully. + - Add more checks to avoid writing impossible values to miniSEED v2 blockette fields. + - Fix all compiler warnings at level -Wextra for clang, gcc, and \W3 for MSVC. + +2024.015: + - Fix build of DLL target on Windows by defining exports in libmseed.def. + +2024.007: + - Improve formatting of gap list produced by mstl3_printgaplist(). + 2024.006: 3.0.18 - ms_nslc2sid() requires all codes except location to be set to reduce misuse. - Fix raw, diagnostic generation of SIDs from miniSEED v2 when full codes are used. diff --git a/libmseed/Makefile b/libmseed/Makefile index 87d7cb7..0752de9 100644 --- a/libmseed/Makefile +++ b/libmseed/Makefile @@ -9,7 +9,7 @@ # Extract version from libmseed.h, expected line should include LIBMSEED_VERSION "#.#.#" MAJOR_VER = $(shell grep LIBMSEED_VERSION libmseed.h | grep -Eo '[0-9]+.[0-9]+.[0-9]+' | cut -d . -f 1) FULL_VER = $(shell grep LIBMSEED_VERSION libmseed.h | grep -Eo '[0-9]+.[0-9]+.[0-9]+') -COMPAT_VER = $(MAJOR_VER).0.0 +COMPAT_VER = $(MAJOR_VER).1.0 # Default settings for install target PREFIX ?= /usr/local diff --git a/libmseed/Makefile.win b/libmseed/Makefile.win index 76cfed3..0b352da 100644 --- a/libmseed/Makefile.win +++ b/libmseed/Makefile.win @@ -34,7 +34,7 @@ lib: $(OBJS) link.exe /lib /nologo /OUT:$(LM_LIB) $(OBJS) dll: $(OBJS) - link.exe /dll /nologo /OUT:$(LM_DLL) $(OBJS) + link.exe /dll /nologo /DEF:$(LM_DEF) /OUT:$(LM_DLL) $(OBJS) .c.obj: $(CC) /nologo $(CFLAGS) $(INCS) $(OPTS) /c $< diff --git a/libmseed/example/Makefile.win b/libmseed/example/Makefile.win index a39284e..de39697 100644 --- a/libmseed/example/Makefile.win +++ b/libmseed/example/Makefile.win @@ -9,7 +9,7 @@ OPTS = /O2 /D_CRT_SECURE_NO_WARNINGS SRCS = lm_pack.c \ lm_pack_rollingbuffer.c \ lm_parse.c \ - lm_read_buffer.c \ + lm_read_buffer.c \ lm_read_recordlist.c \ lm_read_selection.c \ lm_sids.c \ @@ -25,4 +25,5 @@ all: $(BINS) # Clean-up directives clean: - -del *.obj *.exe *% *~ \ No newline at end of file + -del *.obj *.exe *% *~ + diff --git a/libmseed/example/lm_pack_rollingbuffer.c b/libmseed/example/lm_pack_rollingbuffer.c index e52fabb..08b2752 100644 --- a/libmseed/example/lm_pack_rollingbuffer.c +++ b/libmseed/example/lm_pack_rollingbuffer.c @@ -37,6 +37,7 @@ void record_handler (char *record, int reclen, void *handlerdata) { + (void)handlerdata; MS3Record *msr = NULL; if (!msr3_parse (record, reclen, &msr, 0, 0)) diff --git a/libmseed/example/lm_read_recordlist.c b/libmseed/example/lm_read_recordlist.c index 9716e85..7fcf51c 100644 --- a/libmseed/example/lm_read_recordlist.c +++ b/libmseed/example/lm_read_recordlist.c @@ -41,15 +41,15 @@ main (int argc, char **argv) char fileptrstr[30]; uint32_t flags = 0; int8_t verbose = 0; - size_t idx; + uint64_t idx; int rv; char printdata = 0; int64_t unpacked; uint8_t samplesize; char sampletype; - size_t lineidx; - size_t lines; + uint64_t lineidx; + uint64_t lines; int col; void *sptr; @@ -62,7 +62,7 @@ main (int argc, char **argv) mseedfile = argv[1]; /* Simplistic argument parsing */ - for (idx = 2; idx < argc; idx++) + for (idx = 2; (int)idx < argc; idx++) { if (strncmp (argv[idx], "-v", 2) == 0) verbose += strspn (&argv[idx][1], "v"); @@ -166,7 +166,7 @@ main (int argc, char **argv) for (idx = 0, lineidx = 0; lineidx < lines; lineidx++) { - for (col = 0; col < 6 && idx < seg->numsamples; col++) + for (col = 0; col < 6 && (int64_t)idx < seg->numsamples; col++) { sptr = (char *)seg->datasamples + (idx * samplesize); diff --git a/libmseed/extraheaders.c b/libmseed/extraheaders.c index 98cfa52..ae9f896 100644 --- a/libmseed/extraheaders.c +++ b/libmseed/extraheaders.c @@ -148,7 +148,7 @@ parse_json (char *jsonstring, size_t length, LM_PARSED_JSON *parsed) ***************************************************************************/ int mseh_get_ptr_r (const MS3Record *msr, const char *ptr, - void *value, char type, size_t maxlength, + void *value, char type, uint32_t maxlength, LM_PARSED_JSON **parsestate) { LM_PARSED_JSON *parsed = (parsestate) ? *parsestate : NULL; diff --git a/libmseed/fileutils.c b/libmseed/fileutils.c index 8268a3a..43cd33e 100644 --- a/libmseed/fileutils.c +++ b/libmseed/fileutils.c @@ -768,6 +768,8 @@ int ms3_url_useragent (const char *program, const char *version) { #if !defined(LIBMSEED_URL) + (void)program; /* Unused */ + (void)version; /* Unused */ ms_log (2, "URL support not included in library\n"); return -1; #else @@ -796,6 +798,7 @@ int ms3_url_userpassword (const char *userpassword) { #if !defined(LIBMSEED_URL) + (void) userpassword; /* Unused */ ms_log (2, "URL support not included in library\n"); return -1; #else @@ -824,6 +827,7 @@ int ms3_url_addheader (const char *header) { #if !defined(LIBMSEED_URL) + (void)header; /* Unused */ ms_log (2, "URL support not included in library\n"); return -1; #else @@ -1008,12 +1012,12 @@ mstl3_writemseed (MS3TraceList *mstl, const char *mspath, int8_t overwrite, char * parse_pathname_range (const char *string, int64_t *start, int64_t *end) { - char startstr[21] = {0}; /* Maximum of 20 digit value */ - char endstr[21] = {0}; /* Maximum of 20 digit value */ - int startdigits = 0; - int enddigits = 0; - char *dash = NULL; - char *at = NULL; + char startstr[21] = {0}; /* Maximum of 20 digit value */ + char endstr[21] = {0}; /* Maximum of 20 digit value */ + uint8_t startdigits = 0; + uint8_t enddigits = 0; + char *dash = NULL; + char *at = NULL; char *ptr; if (!string || (!start || !end)) diff --git a/libmseed/genutils.c b/libmseed/genutils.c index ef017e7..45b430f 100644 --- a/libmseed/genutils.c +++ b/libmseed/genutils.c @@ -139,7 +139,7 @@ static const int monthdays_leap[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, #define VALIDSEC(sec) (sec >= 0 && sec <= 60) /* Check that a nanosecond is in a valid range */ -#define VALIDNANOSEC(nanosec) (nanosec >= 0 && nanosec <= 999999999) +#define VALIDNANOSEC(nanosec) (nanosec <= 999999999) /** @endcond */ @@ -314,6 +314,7 @@ int ms_nslc2sid (char *sid, int sidlen, uint16_t flags, const char *net, const char *sta, const char *loc, const char *chan) { + (void)flags; /* Unused */ char *sptr = sid; char xchan[6] = {0}; int needed = 0; @@ -411,7 +412,7 @@ ms_nslc2sid (char *sid, int sidlen, uint16_t flags, return -1; } - return (sptr - sid); + return (int)(sptr - sid); } /* End of ms_nslc2sid() */ /**********************************************************************/ /** @@ -1344,7 +1345,7 @@ ms_timestr2nstime (const char *timestr) } } - length = cp - timestr; + length = (int)(cp - timestr); /* If the time string is all number-like characters assume it is an epoch time. * Unless it is 4 characters, which could be a year, unless it starts with a sign. */ @@ -1431,15 +1432,15 @@ nstime_t ms_mdtimestr2nstime (const char *timestr) { int fields; - int year = 0; - int mon = 1; - int mday = 1; - int yday = 1; - int hour = 0; - int min = 0; - int sec = 0; - double fsec = 0.0; - int nsec = 0; + int year = 0; + int mon = 1; + int mday = 1; + int yday = 1; + int hour = 0; + int min = 0; + int sec = 0; + double fsec = 0.0; + uint32_t nsec = 0; if (!timestr) { @@ -1453,7 +1454,7 @@ ms_mdtimestr2nstime (const char *timestr) /* Convert fractional seconds to nanoseconds */ if (fsec != 0.0) { - nsec = (int)(fsec * 1000000000.0 + 0.5); + nsec = (uint32_t)(fsec * 1000000000.0 + 0.5); } if (fields < 1) @@ -1500,7 +1501,7 @@ ms_mdtimestr2nstime (const char *timestr) if (!VALIDNANOSEC (nsec)) { - ms_log (2, "fractional second (%d) is out of range\n", nsec); + ms_log (2, "fractional second (%u) is out of range\n", nsec); return NSTERROR; } @@ -1539,13 +1540,13 @@ nstime_t ms_seedtimestr2nstime (const char *seedtimestr) { int fields; - int year = 0; - int yday = 1; - int hour = 0; - int min = 0; - int sec = 0; - double fsec = 0.0; - int nsec = 0; + int year = 0; + int yday = 1; + int hour = 0; + int min = 0; + int sec = 0; + double fsec = 0.0; + uint32_t nsec = 0; if (!seedtimestr) { @@ -1559,7 +1560,7 @@ ms_seedtimestr2nstime (const char *seedtimestr) /* Convert fractional seconds to nanoseconds */ if (fsec != 0.0) { - nsec = (int)(fsec * 1000000000.0 + 0.5); + nsec = (uint32_t)(fsec * 1000000000.0 + 0.5); } if (fields < 1) @@ -1600,7 +1601,7 @@ ms_seedtimestr2nstime (const char *seedtimestr) if (!VALIDNANOSEC (nsec)) { - ms_log (2, "fractional second (%d) is out of range\n", nsec); + ms_log (2, "fractional second (%u) is out of range\n", nsec); return NSTERROR; } diff --git a/libmseed/libmseed.def b/libmseed/libmseed.def index 9a211ea..5e9264a 100644 --- a/libmseed/libmseed.def +++ b/libmseed/libmseed.def @@ -61,6 +61,7 @@ EXPORTS msr3_writemseed mstl3_writemseed libmseed_url_support + ms3_mstl_init_fd ms_sid2nslc ms_nslc2sid ms_seedchan2xchan @@ -76,6 +77,7 @@ EXPORTS mseh_add_recenter_r mseh_serialize mseh_free_parsestate + mseh_replace mseh_print ms_rlog ms_rlog_l @@ -93,8 +95,5 @@ EXPORTS ms_dabs ms_bigendianhost ms_crc32c - ms_gswap2 - ms_gswap4 - ms_gswap8 leapsecondlist libmseed_memory diff --git a/libmseed/libmseed.h b/libmseed/libmseed.h index 3b13cec..7a2963c 100644 --- a/libmseed/libmseed.h +++ b/libmseed/libmseed.h @@ -28,8 +28,8 @@ extern "C" { #endif -#define LIBMSEED_VERSION "3.0.18" //!< Library version -#define LIBMSEED_RELEASE "2024.006" //!< Library release date +#define LIBMSEED_VERSION "3.1.1" //!< Library version +#define LIBMSEED_RELEASE "2024.024" //!< Library release date /** @defgroup io-functions File and URL I/O */ /** @defgroup miniseed-record Record Handling */ @@ -365,7 +365,7 @@ typedef struct MS3Record { uint8_t flags; //!< Record-level bit flags nstime_t starttime; //!< Record start time (first sample) double samprate; //!< Nominal sample rate as samples/second (Hz) or period (s) - int8_t encoding; //!< Data encoding format, see @ref encoding-values + int16_t encoding; //!< Data encoding format, see @ref encoding-values uint8_t pubversion; //!< Publication version int64_t samplecnt; //!< Number of samples in record uint32_t crc; //!< CRC of entire record @@ -375,7 +375,7 @@ typedef struct MS3Record { /* Data sample fields */ void *datasamples; //!< Data samples, \a numsamples of type \a sampletype - size_t datasize; //!< Size of datasamples buffer in bytes + uint64_t datasize; //!< Size of datasamples buffer in bytes int64_t numsamples; //!< Number of data samples in datasamples char sampletype; //!< Sample type code: t, i, f, d @ref sample-types } MS3Record; @@ -398,8 +398,8 @@ extern int64_t msr3_unpack_data (MS3Record *msr, int8_t verbose); extern int msr3_data_bounds (const MS3Record *msr, uint32_t *dataoffset, uint32_t *datasize); -extern int64_t ms_decode_data (const void *input, size_t inputsize, uint8_t encoding, - int64_t samplecount, void *output, size_t outputsize, +extern int64_t ms_decode_data (const void *input, uint64_t inputsize, uint8_t encoding, + uint64_t samplecount, void *output, uint64_t outputsize, char *sampletype, int8_t swapflag, const char *sid, int8_t verbose); extern MS3Record* msr3_init (MS3Record *msr); @@ -411,7 +411,7 @@ extern int msr3_resize_buffer (MS3Record *msr); extern double msr3_sampratehz (const MS3Record *msr); extern double msr3_host_latency (const MS3Record *msr); -extern int ms3_detect (const char *record, uint64_t recbuflen, uint8_t *formatversion); +extern int64_t ms3_detect (const char *record, uint64_t recbuflen, uint8_t *formatversion); extern int ms_parse_raw3 (const char *record, int maxreclen, int8_t details); extern int ms_parse_raw2 (const char *record, int maxreclen, int8_t details, int8_t swapflag); /** @} */ @@ -551,7 +551,7 @@ typedef struct MS3TraceSeg { double samprate; //!< Nominal sample rate (Hz) int64_t samplecnt; //!< Number of samples in trace coverage void *datasamples; //!< Data samples, \a numsamples of type \a sampletype - size_t datasize; //!< Size of datasamples buffer in bytes + uint64_t datasize; //!< Size of datasamples buffer in bytes int64_t numsamples; //!< Number of data samples in datasamples char sampletype; //!< Sample type code, see @ref sample-types void *prvtptr; //!< Private pointer for general use, unused by library @@ -639,7 +639,7 @@ extern int64_t mstl3_readbuffer_selection (MS3TraceList **ppmstl, const ch const MS3Tolerance *tolerance, const MS3Selections *selections, int8_t verbose); extern int64_t mstl3_unpack_recordlist (MS3TraceID *id, MS3TraceSeg *seg, void *output, - size_t outputsize, int8_t verbose); + uint64_t outputsize, int8_t verbose); extern int mstl3_convertsamples (MS3TraceSeg *seg, char type, int8_t truncate); extern int mstl3_resize_buffers (MS3TraceList *mstl); extern int64_t mstl3_pack (MS3TraceList *mstl, void (*record_handler) (char *, int, void *), @@ -940,7 +940,7 @@ typedef struct LM_PARSED_JSON_s LM_PARSED_JSON; (!mseh_get_ptr_r (msr, ptr, NULL, 0, 0, NULL)) extern int mseh_get_ptr_r (const MS3Record *msr, const char *ptr, - void *value, char type, size_t maxlength, + void *value, char type, uint32_t maxlength, LM_PARSED_JSON **parsestate); /** @def mseh_set diff --git a/libmseed/logging.c b/libmseed/logging.c index cfe9134..32b3859 100644 --- a/libmseed/logging.c +++ b/libmseed/logging.c @@ -366,7 +366,7 @@ rlog_int (MSLogParam *logp, const char *function, int level, strncpy (message, "Error: ", MAX_LOG_MSG_LENGTH); } - presize = strlen (message); + presize = (int)strlen (message); printed = vsnprintf (&message[presize], MAX_LOG_MSG_LENGTH - presize, format, *varlist); @@ -381,7 +381,7 @@ rlog_int (MSLogParam *logp, const char *function, int level, message[MAX_LOG_MSG_LENGTH - 1] = '\0'; } - presize = strlen (message); + presize = (int)strlen (message); printed = vsnprintf (&message[presize], MAX_LOG_MSG_LENGTH - presize, format, *varlist); @@ -396,7 +396,7 @@ rlog_int (MSLogParam *logp, const char *function, int level, message[MAX_LOG_MSG_LENGTH - 1] = '\0'; } - presize = strlen (message); + presize = (int)strlen (message); printed = vsnprintf (&message[presize], MAX_LOG_MSG_LENGTH - presize, format, *varlist); diff --git a/libmseed/msio.c b/libmseed/msio.c index e05950e..8eacc9f 100644 --- a/libmseed/msio.c +++ b/libmseed/msio.c @@ -202,6 +202,7 @@ msio_fopen (LMIO *io, const char *path, const char *mode, if (!knownfile && strstr (path, "://")) { #if !defined(LIBMSEED_URL) + (void)endoffset; /* Unused */ ms_log (2, "URL support not included in library for %s\n", path); return -1; #else @@ -642,6 +643,7 @@ msio_url_useragent (const char *program, const char *version) } #if !defined(LIBMSEED_URL) + (void)version; /* Unused */ ms_log (2, "URL support not included in library\n"); return -1; #else diff --git a/libmseed/msrutils.c b/libmseed/msrutils.c index d5bc7b2..78090ba 100644 --- a/libmseed/msrutils.c +++ b/libmseed/msrutils.c @@ -273,7 +273,7 @@ msr3_print (const MS3Record *msr, int8_t details) ms_log (0, " extra header length: %d bytes\n", msr->extralength); ms_log (0, " data payload length: %d bytes\n", msr->datalength); ms_log (0, " payload encoding: %s (val: %d)\n", - (char *)ms_encodingstr (msr->encoding), msr->encoding); + (char *)ms_encodingstr ((uint8_t)msr->encoding), msr->encoding); if (details > 1 && msr->extralength > 0 && msr->extra) { diff --git a/libmseed/pack.c b/libmseed/pack.c index 84eebdc..245e0a6 100644 --- a/libmseed/pack.c +++ b/libmseed/pack.c @@ -41,13 +41,13 @@ static int msr3_pack_mseed2 (const MS3Record *msr, void (*record_handler) (char void *handlerdata, int64_t *packedsamples, uint32_t flags, int8_t verbose); -static int msr_pack_data (void *dest, void *src, int maxsamples, int maxdatabytes, - char sampletype, int8_t encoding, int8_t swapflag, - uint32_t *byteswritten, const char *sid, int8_t verbose); +static int64_t msr_pack_data (void *dest, void *src, uint64_t maxsamples, uint64_t maxdatabytes, + char sampletype, int8_t encoding, int8_t swapflag, + uint32_t *byteswritten, const char *sid, int8_t verbose); static int ms_genfactmult (double samprate, int16_t *factor, int16_t *multiplier); -static uint32_t ms_timestr2btime (const char *timestr, uint8_t *btime, const char *sid, int8_t swapflag); +static int64_t ms_timestr2btime (const char *timestr, uint8_t *btime, const char *sid, int8_t swapflag); /**********************************************************************/ /** @@ -165,8 +165,8 @@ msr3_pack_mseed3 (const MS3Record *msr, void (*record_handler) (char *, int, voi uint32_t maxdatabytes; uint32_t maxsamples; int recordcnt = 0; - int packsamples; - int packoffset; + int64_t packsamples; + int64_t packoffset; int64_t totalpackedsamples; uint32_t reclen; uint32_t maxreclen; @@ -266,11 +266,11 @@ msr3_pack_mseed3 (const MS3Record *msr, void (*record_handler) (char *, int, voi if (encoding == DE_STEIM1) { - maxsamples = (int)(maxdatabytes / 64) * STEIM1_FRAME_MAX_SAMPLES; + maxsamples = (uint32_t)(maxdatabytes / 64) * STEIM1_FRAME_MAX_SAMPLES; } else if (encoding == DE_STEIM2) { - maxsamples = (int)(maxdatabytes / 64) * STEIM2_FRAME_MAX_SAMPLES; + maxsamples = (uint32_t)(maxdatabytes / 64) * STEIM2_FRAME_MAX_SAMPLES; } else { @@ -319,7 +319,7 @@ msr3_pack_mseed3 (const MS3Record *msr, void (*record_handler) (char *, int, voi memcpy (rawrec + dataoffset, encoded, datalength); /* Update number of samples and data length */ - *pMS3FSDH_NUMSAMPLES(rawrec) = HO4u (packsamples, swapflag); + *pMS3FSDH_NUMSAMPLES(rawrec) = HO4u ((uint32_t)packsamples, swapflag); *pMS3FSDH_DATALENGTH(rawrec) = HO4u (datalength, swapflag); /* Calculate CRC (with CRC field set to 0) and set */ @@ -328,7 +328,7 @@ msr3_pack_mseed3 (const MS3Record *msr, void (*record_handler) (char *, int, voi *pMS3FSDH_CRC(rawrec) = HO4u (crc, swapflag); if (verbose >= 1) - ms_log (0, "%s: Packed %d samples into %u byte record\n", msr->sid, packsamples, reclen); + ms_log (0, "%s: Packed %" PRId64 " samples into %u byte record\n", msr->sid, packsamples, reclen); /* Send record to handler */ record_handler (rawrec, reclen, handlerdata); @@ -546,7 +546,7 @@ msr3_pack_header3 (const MS3Record *msr, char *record, uint32_t recbuflen, int8_ return -1; } - extraoffset = MS3FSDH_LENGTH + sidlength; + extraoffset = MS3FSDH_LENGTH + (int)sidlength; /* Build fixed header */ record[0] = 'M'; @@ -575,7 +575,7 @@ msr3_pack_header3 (const MS3Record *msr, char *record, uint32_t recbuflen, int8_ if (msr->extralength > 0) memcpy (record + extraoffset, msr->extra, msr->extralength); - return (MS3FSDH_LENGTH + sidlength + msr->extralength); + return (MS3FSDH_LENGTH + (int)sidlength + msr->extralength); } /* End of msr3_pack_header3() */ /*************************************************************************** @@ -605,8 +605,8 @@ msr3_pack_mseed2 (const MS3Record *msr, void (*record_handler) (char *, int, voi uint32_t maxdatabytes; uint32_t maxsamples; int recordcnt = 0; - int packsamples; - int packoffset; + int64_t packsamples; + int64_t packoffset; int64_t totalpackedsamples; uint32_t datalength; @@ -772,7 +772,7 @@ msr3_pack_mseed2 (const MS3Record *msr, void (*record_handler) (char *, int, voi if (packsamples > UINT16_MAX) { - ms_log (2, "%s: Too many samples packed (%d) for a single v2 record)\n", + ms_log (2, "%s: Too many samples packed (%" PRId64 ") for a single v2 record)\n", msr->sid, packsamples); libmseed_memory.free (encoded); libmseed_memory.free (rawrec); @@ -785,7 +785,7 @@ msr3_pack_mseed2 (const MS3Record *msr, void (*record_handler) (char *, int, voi memcpy (rawrec + dataoffset, encoded, datalength); /* Update number of samples */ - *pMS2FSDH_NUMSAMPLES(rawrec) = HO2u (packsamples, swapflag); + *pMS2FSDH_NUMSAMPLES(rawrec) = HO2u ((uint16_t)packsamples, swapflag); /* Zero any space between encoded data and end of record */ content = dataoffset + datalength; @@ -793,7 +793,7 @@ msr3_pack_mseed2 (const MS3Record *msr, void (*record_handler) (char *, int, voi memset (rawrec + content, 0, reclen - content); if (verbose >= 1) - ms_log (0, "%s: Packed %d samples into %u byte record\n", msr->sid, packsamples, reclen); + ms_log (0, "%s: Packed %" PRId64 " samples into %u byte record\n", msr->sid, packsamples, reclen); /* Send record to handler */ record_handler (rawrec, reclen, handlerdata); @@ -890,13 +890,13 @@ msr3_pack_header2 (const MS3Record *msr, char *record, uint32_t recbuflen, int8_ yyjson_val *eharr; yyjson_arr_iter ehiter; yyjson_val *ehiterval; - yyjson_val *ehval; const char *header_string; double header_number; + uint64_t header_uint; bool header_boolean; - int blockette_type; - int blockette_length; + uint16_t blockette_type; + uint16_t blockette_length; if (!msr || !record) { @@ -1069,7 +1069,7 @@ msr3_pack_header2 (const MS3Record *msr, char *record, uint32_t recbuflen, int8_ if (yyjson_ptr_get_num (ehroot, "/FDSN/Time/Correction", &header_number)) { - *pMS2FSDH_TIMECORRECT (record) = HO4d (header_number * 10000, swapflag); + *pMS2FSDH_TIMECORRECT (record) = HO4d ((int32_t)(header_number * 10000 + 0.5), swapflag); /* Set time correction applied bit in activity flags. Rationale: V3 records do not allow unapplied time corrections and unapplied @@ -1100,7 +1100,7 @@ msr3_pack_header2 (const MS3Record *msr, char *record, uint32_t recbuflen, int8_ written += 8; /* Add Blockette 1001 if microsecond offset or timing quality is present */ - if (yyjson_ptr_get_num (ehroot, "/FDSN/Time/Quality", &header_number) || msec_offset) + if (yyjson_ptr_get_uint (ehroot, "/FDSN/Time/Quality", &header_uint) || msec_offset) { *next_blockette = HO2u ((uint16_t)written, swapflag); next_blockette = pMS2B1001_NEXT (record + written); @@ -1109,8 +1109,8 @@ msr3_pack_header2 (const MS3Record *msr, char *record, uint32_t recbuflen, int8_ *pMS2B1001_TYPE (record + written) = HO2u (1001, swapflag); *pMS2B1001_NEXT (record + written) = 0; - if (yyjson_ptr_get_num (ehroot, "/FDSN/Time/Quality", &header_number)) - *pMS2B1001_TIMINGQUALITY (record + written) = (uint8_t) (header_number + 0.5); + if (yyjson_ptr_get_uint (ehroot, "/FDSN/Time/Quality", &header_uint) && header_uint <= UINT8_MAX) + *pMS2B1001_TIMINGQUALITY (record + written) = (uint8_t) (header_uint); else *pMS2B1001_TIMINGQUALITY (record + written) = 0; @@ -1130,7 +1130,7 @@ msr3_pack_header2 (const MS3Record *msr, char *record, uint32_t recbuflen, int8_ *pMS2B100_TYPE (record + written) = HO2u (100, swapflag); *pMS2B100_NEXT (record + written) = 0; - *pMS2B100_SAMPRATE (record + written) = HO4f (msr->samprate, swapflag); + *pMS2B100_SAMPRATE (record + written) = HO4f ((float)msr->samprate, swapflag); *pMS2B100_FLAGS (record + written) = 0; memset (pMS2B100_RESERVED (record + written), 0, 3); @@ -1164,47 +1164,47 @@ msr3_pack_header2 (const MS3Record *msr, char *record, uint32_t recbuflen, int8_ *pMS2B500_TYPE (record + written) = HO2u (500, swapflag); *pMS2B500_NEXT (record + written) = 0; - if ((ehval = yyjson_ptr_get (ehiterval, "/VCOCorrection")) && yyjson_is_num (ehval)) - *pMS2B500_VCOCORRECTION (record + written) = HO4f (yyjson_get_num (ehval), swapflag); + if (yyjson_ptr_get_num (ehiterval, "/VCOCorrection", &header_number)) + *pMS2B500_VCOCORRECTION (record + written) = HO4f ((float)header_number, swapflag); - if ((ehval = yyjson_ptr_get (ehiterval, "/Time")) && yyjson_is_str (ehval)) + if (yyjson_ptr_get_str (ehiterval, "/Time", &header_string)) { - uint32_t l_nsec; + int64_t l_nsec; uint16_t l_fsec; int8_t l_msec_offset; - l_nsec = ms_timestr2btime (yyjson_get_str (ehval), + l_nsec = ms_timestr2btime (header_string, (uint8_t *)pMS2B500_YEAR (record + written), msr->sid, swapflag); if (l_nsec == -1) { - ms_log (2, "%s: Cannot convert B500 time: %s\n", msr->sid, yyjson_get_str (ehval)); + ms_log (2, "%s: Cannot convert B500 time: %s\n", msr->sid, header_string); yyjson_doc_free (ehdoc); return -1; } /* Calculate time at fractional 100usec resolution and microsecond offset */ - l_fsec = l_nsec / 100000; - l_msec_offset = ((l_nsec / 1000) - (l_fsec * 100)); + l_fsec = (uint16_t)(l_nsec / 100000); + l_msec_offset = (int8_t)((l_nsec / 1000) - (l_fsec * 100)); *pMS2B500_MICROSECOND (record + written) = l_msec_offset; } - if ((ehval = yyjson_ptr_get (ehiterval, "/ReceptionQuality")) && yyjson_is_num (ehval)) - *pMS2B500_RECEPTIONQUALITY (record + written) = (uint8_t)yyjson_get_num (ehval); + if (yyjson_ptr_get_uint (ehiterval, "/ReceptionQuality", &header_uint) && header_uint <= UINT8_MAX) + *pMS2B500_RECEPTIONQUALITY (record + written) = (uint8_t)header_uint; - if ((ehval = yyjson_ptr_get (ehiterval, "/Count")) && yyjson_is_num (ehval)) - *pMS2B500_EXCEPTIONCOUNT (record + written) = HO4d (yyjson_get_num (ehval), swapflag); + if (yyjson_ptr_get_uint (ehiterval, "/Count", &header_uint) && header_uint <= UINT32_MAX) + *pMS2B500_EXCEPTIONCOUNT (record + written) = HO4d ((uint32_t)header_uint, swapflag); - if ((ehval = yyjson_ptr_get (ehiterval, "/Type")) && yyjson_is_str (ehval)) - ms_strncpopen (pMS2B500_EXCEPTIONTYPE (record + written), yyjson_get_str (ehval), 16); + if (yyjson_ptr_get_str (ehiterval, "/Type", &header_string)) + ms_strncpopen (pMS2B500_EXCEPTIONTYPE (record + written), header_string, 16); - if ((ehval = yyjson_ptr_get (ehroot, "/FDSN/Clock/Model")) && yyjson_is_str (ehval)) - ms_strncpopen (pMS2B500_CLOCKMODEL (record + written), yyjson_get_str (ehval), 32); + if (yyjson_ptr_get_str (ehroot, "/FDSN/Clock/Model", &header_string)) + ms_strncpopen (pMS2B500_CLOCKMODEL (record + written), header_string, 32); - if ((ehval = yyjson_ptr_get (ehiterval, "/ClockStatus")) && yyjson_is_str (ehval)) - ms_strncpopen (pMS2B500_CLOCKSTATUS (record + written), yyjson_get_str (ehval), 128); + if (yyjson_ptr_get_str (ehiterval, "/ClockStatus", &header_string)) + ms_strncpopen (pMS2B500_CLOCKSTATUS (record + written), header_string, 128); written += blockette_length; } @@ -1221,8 +1221,8 @@ msr3_pack_header2 (const MS3Record *msr, char *record, uint32_t recbuflen, int8_ continue; /* Determine which detection type: MURDOCK versus the generic type */ - if ((ehval = yyjson_ptr_get (ehiterval, "/Type")) && yyjson_is_str (ehval) && - strncasecmp (yyjson_get_str (ehval), "MURDOCK", 7) == 0) + if (yyjson_ptr_get_str (ehiterval, "/Type", &header_string) && + strncasecmp (header_string, "MURDOCK", 7) == 0) { blockette_type = 201; blockette_length = 60; @@ -1235,7 +1235,7 @@ msr3_pack_header2 (const MS3Record *msr, char *record, uint32_t recbuflen, int8_ if ((recbuflen - written) < blockette_length ) { - ms_log (2, "%s: Record length not large enough for B%d\n", msr->sid, blockette_type); + ms_log (2, "%s: Record length not large enough for B%u\n", msr->sid, blockette_type); yyjson_doc_free (ehdoc); return -1; } @@ -1249,19 +1249,19 @@ msr3_pack_header2 (const MS3Record *msr, char *record, uint32_t recbuflen, int8_ *pMS2B200_TYPE (record + written) = HO2u (blockette_type, swapflag); *pMS2B200_NEXT (record + written) = 0; - if ((ehval = yyjson_ptr_get (ehiterval, "/SignalAmplitude")) && yyjson_is_num (ehval)) - *pMS2B200_AMPLITUDE (record + written) = HO4f (yyjson_get_num (ehval), swapflag); + if (yyjson_ptr_get_num (ehiterval, "/SignalAmplitude", &header_number)) + *pMS2B200_AMPLITUDE (record + written) = HO4f ((float)header_number, swapflag); - if ((ehval = yyjson_ptr_get (ehiterval, "/SignalPeriod")) && yyjson_is_num (ehval)) - *pMS2B200_PERIOD (record + written) = HO4f (yyjson_get_num (ehval), swapflag); + if (yyjson_ptr_get_num (ehiterval, "/SignalPeriod", &header_number)) + *pMS2B200_PERIOD (record + written) = HO4f ((float)header_number, swapflag); - if ((ehval = yyjson_ptr_get (ehiterval, "/BackgroundEstimate")) && yyjson_is_num (ehval)) - *pMS2B200_BACKGROUNDEST (record + written) = HO4f (yyjson_get_num (ehval), swapflag); + if (yyjson_ptr_get_num (ehiterval, "/BackgroundEstimate", &header_number)) + *pMS2B200_BACKGROUNDEST (record + written) = HO4f ((float)header_number, swapflag); /* Determine which wave: DILATATION versus (assumed) COMPRESSION */ - if ((ehval = yyjson_ptr_get (ehiterval, "/Wave"))) + if (yyjson_ptr_get_str (ehiterval, "/Wave", &header_string)) { - if (yyjson_is_str (ehval) && strncasecmp (yyjson_get_str (ehval), "DILATATION", 10) == 0) + if (strncasecmp (header_string, "DILATATION", 10) == 0) *pMS2B200_FLAGS (record + written) |= 0x01; } else if (blockette_type == 200) @@ -1270,16 +1270,16 @@ msr3_pack_header2 (const MS3Record *msr, char *record, uint32_t recbuflen, int8_ } if (blockette_type == 200 && - (ehval = yyjson_ptr_get (ehiterval, "/Units")) && yyjson_is_str (ehval) && - strncasecmp (yyjson_get_str (ehval), "COUNT", 5) != 0) + yyjson_ptr_get_str (ehiterval, "/Units", &header_string) && + strncasecmp (header_string, "COUNT", 5) != 0) *pMS2B200_FLAGS (record + written) |= 0x02; - if ((ehval = yyjson_ptr_get (ehiterval, "/OnsetTime")) && yyjson_is_str (ehval)) + if (yyjson_ptr_get_str (ehiterval, "/OnsetTime", &header_string)) { - if (ms_timestr2btime (yyjson_get_str (ehval), (uint8_t *)pMS2B200_YEAR (record + written), + if (ms_timestr2btime (header_string, (uint8_t *)pMS2B200_YEAR (record + written), msr->sid, swapflag) == -1) { - ms_log (2, "%s: Cannot convert B%d time: %s\n", msr->sid, blockette_type, yyjson_get_str (ehval)); + ms_log (2, "%s: Cannot convert B%u time: %s\n", msr->sid, blockette_type, header_string); yyjson_doc_free (ehdoc); return -1; } @@ -1287,8 +1287,8 @@ msr3_pack_header2 (const MS3Record *msr, char *record, uint32_t recbuflen, int8_ if (blockette_type == 200) { - if ((ehval = yyjson_ptr_get (ehiterval, "/Detector")) && yyjson_is_str (ehval)) - ms_strncpopen (pMS2B200_DETECTOR (record + written), yyjson_get_str (ehval), 24); + if (yyjson_ptr_get_str (ehiterval, "/Detector", &header_string)) + ms_strncpopen (pMS2B200_DETECTOR (record + written), header_string, 24); } else /* Blockette 201 */ { @@ -1310,14 +1310,14 @@ msr3_pack_header2 (const MS3Record *msr, char *record, uint32_t recbuflen, int8_ } } - if ((ehval = yyjson_ptr_get (ehiterval, "/MEDLookback")) && yyjson_is_num (ehval)) - *pMS2B201_LOOPBACK (record + written) = (uint8_t)yyjson_get_num (ehval); + if (yyjson_ptr_get_uint (ehiterval, "/MEDLookback", &header_uint) && header_uint < UINT8_MAX) + *pMS2B201_LOOPBACK (record + written) = (uint8_t)header_uint; - if ((ehval = yyjson_ptr_get (ehiterval, "/MEDPickAlgorithm")) && yyjson_is_num (ehval)) - *pMS2B201_PICKALGORITHM (record + written) = (uint8_t)yyjson_get_num (ehval); + if (yyjson_ptr_get_uint (ehiterval, "/MEDPickAlgorithm", &header_uint) && header_uint < UINT8_MAX) + *pMS2B201_PICKALGORITHM (record + written) = (uint8_t)header_uint; - if ((ehval = yyjson_ptr_get (ehiterval, "/Detector")) && yyjson_is_str (ehval)) - ms_strncpopen (pMS2B201_DETECTOR (record + written), yyjson_get_str (ehval), 24); + if (yyjson_ptr_get_str (ehiterval, "/Detector", &header_string)) + ms_strncpopen (pMS2B201_DETECTOR (record + written), header_string, 24); } written += blockette_length; @@ -1338,30 +1338,30 @@ msr3_pack_header2 (const MS3Record *msr, char *record, uint32_t recbuflen, int8_ /* Determine which calibration type: STEP, SINE, PSEUDORANDOM, GENERIC */ blockette_type = 0; blockette_length = 0; - if ((ehval = yyjson_ptr_get (ehiterval, "/Type")) && yyjson_is_str (ehval)) + if (yyjson_ptr_get_str (ehiterval, "/Type", &header_string)) { - if (strncasecmp (yyjson_get_str (ehval), "STEP", 4) == 0) + if (strncasecmp (header_string, "STEP", 4) == 0) { blockette_type = 300; blockette_length = 60; } - else if (strncasecmp (yyjson_get_str (ehval), "SINE", 4) == 0) + else if (strncasecmp (header_string, "SINE", 4) == 0) { blockette_type = 310; blockette_length = 60; } - else if (strncasecmp (yyjson_get_str (ehval), "PSEUDORANDOM", 12) == 0) + else if (strncasecmp (header_string, "PSEUDORANDOM", 12) == 0) { blockette_type = 320; blockette_length = 64; } - else if (strncasecmp (yyjson_get_str (ehval), "GENERIC", 7) == 0) + else if (strncasecmp (header_string, "GENERIC", 7) == 0) { blockette_type = 390; blockette_length = 28; } } - else if ((ehval = yyjson_ptr_get (ehiterval, "/EndTime"))) + else if (yyjson_ptr_get (ehiterval, "/EndTime")) { blockette_type = 395; blockette_length = 16; @@ -1376,7 +1376,7 @@ msr3_pack_header2 (const MS3Record *msr, char *record, uint32_t recbuflen, int8_ if ((recbuflen - written) < blockette_length ) { - ms_log (2, "%s: Record length not large enough for B%d\n", msr->sid, blockette_type); + ms_log (2, "%s: Record length not large enough for B%u\n", msr->sid, blockette_type); yyjson_doc_free (ehdoc); return -1; } @@ -1393,12 +1393,12 @@ msr3_pack_header2 (const MS3Record *msr, char *record, uint32_t recbuflen, int8_ *pMS2B300_TYPE (record + written) = HO2u (blockette_type, swapflag); *pMS2B300_NEXT (record + written) = 0; - if ((ehval = yyjson_ptr_get (ehiterval, "/BeginTime")) && yyjson_is_str (ehval)) + if (yyjson_ptr_get_str (ehiterval, "/BeginTime", &header_string)) { - if (ms_timestr2btime (yyjson_get_str (ehval), (uint8_t *)pMS2B300_YEAR (record + written), + if (ms_timestr2btime (header_string, (uint8_t *)pMS2B300_YEAR (record + written), msr->sid, swapflag) == -1) { - ms_log (2, "%s: Cannot convert B%d time: %s\n", msr->sid, blockette_type, yyjson_get_str (ehval)); + ms_log (2, "%s: Cannot convert B%u time: %s\n", msr->sid, blockette_type, header_string); yyjson_doc_free (ehdoc); return -1; } @@ -1406,148 +1406,148 @@ msr3_pack_header2 (const MS3Record *msr, char *record, uint32_t recbuflen, int8_ if (blockette_type == 300) { - if ((ehval = yyjson_ptr_get (ehiterval, "/Steps")) && yyjson_is_num (ehval)) - *pMS2B300_NUMCALIBRATIONS (record + written) = (uint8_t)yyjson_get_num (ehval); + if (yyjson_ptr_get_uint (ehiterval, "/Steps", &header_uint) && header_uint <= UINT8_MAX) + *pMS2B300_NUMCALIBRATIONS (record + written) = (uint8_t)header_uint; - if ((ehval = yyjson_ptr_get (ehiterval, "/StepFirstPulsePositive")) && yyjson_get_bool (ehval)) + if (yyjson_ptr_get_bool (ehiterval, "/StepFirstPulsePositive", &header_boolean) && header_boolean) *pMS2B300_FLAGS (record + written) |= 0x01; - if ((ehval = yyjson_ptr_get (ehiterval, "/StepAlternateSign")) && yyjson_get_bool (ehval)) + if (yyjson_ptr_get_bool (ehiterval, "/StepAlternateSign", &header_boolean) && header_boolean) *pMS2B300_FLAGS (record + written) |= 0x02; - if ((ehval = yyjson_ptr_get (ehiterval, "/Trigger")) && yyjson_is_str (ehval) && - strncasecmp (yyjson_get_str (ehval), "AUTOMATIC", 9) == 0) + if (yyjson_ptr_get_str (ehiterval, "/Trigger", &header_string) && + strncasecmp (header_string, "AUTOMATIC", 9) == 0) *pMS2B300_FLAGS (record + written) |= 0x04; - if ((ehval = yyjson_ptr_get (ehiterval, "/Continued")) && yyjson_get_bool (ehval)) + if (yyjson_ptr_get_bool (ehiterval, "/Continued", &header_boolean) && header_boolean) *pMS2B300_FLAGS (record + written) |= 0x08; - if ((ehval = yyjson_ptr_get (ehiterval, "/Duration")) && yyjson_is_num (ehval)) - *pMS2B300_STEPDURATION (record + written) = HO4u (yyjson_get_num (ehval) * 10000, swapflag); + if (yyjson_ptr_get_num (ehiterval, "/Duration", &header_number)) + *pMS2B300_STEPDURATION (record + written) = HO4u ((uint32_t)(header_number * 10000 + 0.5), swapflag); - if ((ehval = yyjson_ptr_get (ehiterval, "/StepBetween")) && yyjson_is_num (ehval)) - *pMS2B300_INTERVALDURATION (record + written) = HO4u (yyjson_get_num (ehval) * 10000, swapflag); + if (yyjson_ptr_get_num (ehiterval, "/StepBetween", &header_number)) + *pMS2B300_INTERVALDURATION (record + written) = HO4u ((uint32_t)(header_number * 10000 + 0.5), swapflag); - if ((ehval = yyjson_ptr_get (ehiterval, "/Amplitude")) && yyjson_is_num (ehval)) - *pMS2B300_AMPLITUDE (record + written) = HO4f (yyjson_get_num (ehval), swapflag); + if (yyjson_ptr_get_num (ehiterval, "/Amplitude", &header_number)) + *pMS2B300_AMPLITUDE (record + written) = HO4f ((float)header_number, swapflag); - if ((ehval = yyjson_ptr_get (ehiterval, "/InputChannel")) && yyjson_is_str (ehval)) - ms_strncpopen (pMS2B300_INPUTCHANNEL (record + written), yyjson_get_str (ehval), 3); + if (yyjson_ptr_get_str (ehiterval, "/InputChannel", &header_string)) + ms_strncpopen (pMS2B300_INPUTCHANNEL (record + written), header_string, 3); - if ((ehval = yyjson_ptr_get (ehiterval, "/ReferenceAmplitude")) && yyjson_is_num (ehval)) - *pMS2B300_REFERENCEAMPLITUDE (record + written) = HO4u (yyjson_get_num (ehval), swapflag); + if (yyjson_ptr_get_num (ehiterval, "/ReferenceAmplitude", &header_number)) + *pMS2B300_REFERENCEAMPLITUDE (record + written) = HO4u ((uint32_t)(header_number + 0.5), swapflag); - if ((ehval = yyjson_ptr_get (ehiterval, "/Coupling")) && yyjson_is_str (ehval)) - ms_strncpopen (pMS2B300_COUPLING (record + written), yyjson_get_str (ehval), 12); + if (yyjson_ptr_get_str (ehiterval, "/Coupling", &header_string)) + ms_strncpopen (pMS2B300_COUPLING (record + written), header_string, 12); - if ((ehval = yyjson_ptr_get (ehiterval, "/Rolloff")) && yyjson_is_str (ehval)) - ms_strncpopen (pMS2B300_ROLLOFF (record + written), yyjson_get_str (ehval), 12); + if (yyjson_ptr_get_str (ehiterval, "/Rolloff", &header_string)) + ms_strncpopen (pMS2B300_ROLLOFF (record + written), header_string, 12); } else if (blockette_type == 310) { - if ((ehval = yyjson_ptr_get (ehiterval, "/Trigger")) && yyjson_is_str (ehval) && - strncasecmp (yyjson_get_str (ehval), "AUTOMATIC", 9) == 0) + if (yyjson_ptr_get_str (ehiterval, "/Trigger", &header_string) && + strncasecmp (header_string, "AUTOMATIC", 9) == 0) *pMS2B310_FLAGS (record + written) |= 0x04; - if ((ehval = yyjson_ptr_get (ehiterval, "/Continued")) && yyjson_get_bool (ehval)) + if (yyjson_ptr_get_bool (ehiterval, "/Continued", &header_boolean) && header_boolean) *pMS2B310_FLAGS (record + written) |= 0x08; - if ((ehval = yyjson_ptr_get (ehiterval, "/AmplitudeRange")) && yyjson_is_str (ehval)) + if (yyjson_ptr_get_str (ehiterval, "/AmplitudeRange", &header_string)) { - if (strncasecmp (yyjson_get_str (ehval), "PEAKTOPEAK", 10) == 0) + if (strncasecmp (header_string, "PEAKTOPEAK", 10) == 0) *pMS2B310_FLAGS (record + written) |= 0x10; - if (strncasecmp (yyjson_get_str (ehval), "ZEROTOPEAK", 10) == 0) + if (strncasecmp (header_string, "ZEROTOPEAK", 10) == 0) *pMS2B310_FLAGS (record + written) |= 0x20; - if (strncasecmp (yyjson_get_str (ehval), "RMS", 3) == 0) + if (strncasecmp (header_string, "RMS", 3) == 0) *pMS2B310_FLAGS (record + written) |= 0x40; } - if ((ehval = yyjson_ptr_get (ehiterval, "/Duration")) && yyjson_is_num (ehval)) - *pMS2B310_DURATION (record + written) = HO4u (yyjson_get_num (ehval) * 10000, swapflag); + if (yyjson_ptr_get_num (ehiterval, "/Duration", &header_number)) + *pMS2B310_DURATION (record + written) = HO4u ((uint32_t)(header_number * 10000 + 0.5), swapflag); - if ((ehval = yyjson_ptr_get (ehiterval, "/SinePeriod")) && yyjson_is_num (ehval)) - *pMS2B310_PERIOD (record + written) = HO4f (yyjson_get_num (ehval), swapflag); + if (yyjson_ptr_get_num (ehiterval, "/SinePeriod", &header_number)) + *pMS2B310_PERIOD (record + written) = HO4f ((float)header_number, swapflag); - if ((ehval = yyjson_ptr_get (ehiterval, "/Amplitude")) && yyjson_is_num (ehval)) - *pMS2B310_AMPLITUDE (record + written) = HO4f (yyjson_get_num (ehval), swapflag); + if (yyjson_ptr_get_num (ehiterval, "/Amplitude", &header_number)) + *pMS2B310_AMPLITUDE (record + written) = HO4f ((float)header_number, swapflag); - if ((ehval = yyjson_ptr_get (ehiterval, "/InputChannel")) && yyjson_is_str (ehval)) - ms_strncpopen (pMS2B310_INPUTCHANNEL (record + written), yyjson_get_str (ehval), 3); + if (yyjson_ptr_get_str (ehiterval, "/InputChannel", &header_string)) + ms_strncpopen (pMS2B310_INPUTCHANNEL (record + written), header_string, 3); - if ((ehval = yyjson_ptr_get (ehiterval, "/ReferenceAmplitude")) && yyjson_is_num (ehval)) - *pMS2B310_REFERENCEAMPLITUDE (record + written) = HO4u (yyjson_get_num (ehval), swapflag); + if (yyjson_ptr_get_num (ehiterval, "/ReferenceAmplitude", &header_number)) + *pMS2B310_REFERENCEAMPLITUDE (record + written) = HO4u ((uint32_t)(header_number + 0.5), swapflag); - if ((ehval = yyjson_ptr_get (ehiterval, "/Coupling")) && yyjson_is_str (ehval)) - ms_strncpopen (pMS2B320_COUPLING (record + written), yyjson_get_str (ehval), 12); + if (yyjson_ptr_get_str (ehiterval, "/Coupling", &header_string)) + ms_strncpopen (pMS2B310_COUPLING (record + written), header_string, 12); - if ((ehval = yyjson_ptr_get (ehiterval, "/Rolloff")) && yyjson_is_str (ehval)) - ms_strncpopen (pMS2B320_ROLLOFF (record + written), yyjson_get_str (ehval), 12); + if (yyjson_ptr_get_str (ehiterval, "/Rolloff", &header_string)) + ms_strncpopen (pMS2B310_ROLLOFF (record + written), header_string, 12); } else if (blockette_type == 320) { - if ((ehval = yyjson_ptr_get (ehiterval, "/Trigger")) && yyjson_is_str (ehval) && - strncasecmp (yyjson_get_str (ehval), "AUTOMATIC", 9) == 0) + if (yyjson_ptr_get_str (ehiterval, "/Trigger", &header_string) && + strncasecmp (header_string, "AUTOMATIC", 9) == 0) *pMS2B320_FLAGS (record + written) |= 0x04; - if ((ehval = yyjson_ptr_get (ehiterval, "/Continued")) && yyjson_get_bool (ehval)) + if (yyjson_ptr_get_bool (ehiterval, "/Continued", &header_boolean) && header_boolean) *pMS2B320_FLAGS (record + written) |= 0x08; - if ((ehval = yyjson_ptr_get (ehiterval, "/AmplitudeRange")) && yyjson_is_str (ehval) && - strncasecmp (yyjson_get_str (ehval), "RANDOM", 6) == 0) + if (yyjson_ptr_get_str (ehiterval, "/AmplitudeRange", &header_string) && + strncasecmp (header_string, "RANDOM", 6) == 0) *pMS2B320_FLAGS (record + written) |= 0x10; - if ((ehval = yyjson_ptr_get (ehiterval, "/Duration")) && yyjson_is_num (ehval)) - *pMS2B320_DURATION (record + written) = HO4u (yyjson_get_num (ehval) * 10000, swapflag); + if (yyjson_ptr_get_num (ehiterval, "/Duration", &header_number)) + *pMS2B320_DURATION (record + written) = HO4u ((uint32_t)(header_number * 10000 + 0.5), swapflag); - if ((ehval = yyjson_ptr_get (ehiterval, "/Amplitude")) && yyjson_is_num (ehval)) - *pMS2B320_PTPAMPLITUDE (record + written) = HO4f (yyjson_get_num (ehval), swapflag); + if (yyjson_ptr_get_num (ehiterval, "/Amplitude", &header_number)) + *pMS2B320_PTPAMPLITUDE (record + written) = HO4f ((float)header_number, swapflag); - if ((ehval = yyjson_ptr_get (ehiterval, "/InputChannel")) && yyjson_is_str (ehval)) - ms_strncpopen (pMS2B320_INPUTCHANNEL (record + written), yyjson_get_str (ehval), 3); + if (yyjson_ptr_get_str (ehiterval, "/InputChannel", &header_string)) + ms_strncpopen (pMS2B320_INPUTCHANNEL (record + written), header_string, 3); - if ((ehval = yyjson_ptr_get (ehiterval, "/ReferenceAmplitude")) && yyjson_is_num (ehval)) - *pMS2B320_REFERENCEAMPLITUDE (record + written) = HO4u (yyjson_get_num (ehval), swapflag); + if (yyjson_ptr_get_num (ehiterval, "/ReferenceAmplitude", &header_number)) + *pMS2B320_REFERENCEAMPLITUDE (record + written) = HO4u ((uint32_t)(header_number + 0.5), swapflag); - if ((ehval = yyjson_ptr_get (ehiterval, "/Coupling")) && yyjson_is_str (ehval)) - ms_strncpopen (pMS2B320_COUPLING (record + written), yyjson_get_str (ehval), 12); + if (yyjson_ptr_get_str (ehiterval, "/Coupling", &header_string)) + ms_strncpopen (pMS2B320_COUPLING (record + written), header_string, 12); - if ((ehval = yyjson_ptr_get (ehiterval, "/Rolloff")) && yyjson_is_str (ehval)) - ms_strncpopen (pMS2B320_ROLLOFF (record + written), yyjson_get_str (ehval), 12); + if (yyjson_ptr_get_str (ehiterval, "/Rolloff", &header_string)) + ms_strncpopen (pMS2B320_ROLLOFF (record + written), header_string, 12); - if ((ehval = yyjson_ptr_get (ehiterval, "/Noise")) && yyjson_is_str (ehval)) - ms_strncpopen (pMS2B320_NOISETYPE (record + written), yyjson_get_str (ehval), 8); + if (yyjson_ptr_get_str (ehiterval, "/Noise", &header_string)) + ms_strncpopen (pMS2B320_NOISETYPE (record + written), header_string, 8); } else if (blockette_type == 390) { - if ((ehval = yyjson_ptr_get (ehiterval, "/Trigger")) && yyjson_is_str (ehval) && - strncasecmp (yyjson_get_str (ehval), "AUTOMATIC", 9) == 0) + if (yyjson_ptr_get_str (ehiterval, "/Trigger", &header_string) && + strncasecmp (header_string, "AUTOMATIC", 9) == 0) *pMS2B390_FLAGS (record + written) |= 0x04; - if ((ehval = yyjson_ptr_get (ehiterval, "/Continued")) && yyjson_get_bool (ehval)) + if (yyjson_ptr_get_bool (ehiterval, "/Continued", &header_boolean) && header_boolean) *pMS2B390_FLAGS (record + written) |= 0x08; - if ((ehval = yyjson_ptr_get (ehiterval, "/Duration")) && yyjson_is_num (ehval)) - *pMS2B390_DURATION (record + written) = HO4u (yyjson_get_num (ehval) * 10000, swapflag); + if (yyjson_ptr_get_num (ehiterval, "/Duration", &header_number)) + *pMS2B390_DURATION (record + written) = HO4u ((uint32_t)(header_number * 10000 + 0.5), swapflag); - if ((ehval = yyjson_ptr_get (ehiterval, "/Amplitude")) && yyjson_is_num (ehval)) - *pMS2B390_AMPLITUDE (record + written) = HO4f (yyjson_get_num (ehval), swapflag); + if (yyjson_ptr_get_num (ehiterval, "/Amplitude", &header_number)) + *pMS2B390_AMPLITUDE (record + written) = HO4f ((float)header_number, swapflag); - if ((ehval = yyjson_ptr_get (ehiterval, "/InputChannel")) && yyjson_is_str (ehval)) - ms_strncpopen (pMS2B390_INPUTCHANNEL (record + written), yyjson_get_str (ehval), 3); + if (yyjson_ptr_get_str (ehiterval, "/InputChannel", &header_string)) + ms_strncpopen (pMS2B390_INPUTCHANNEL (record + written), header_string, 3); } written += blockette_length; } /* Add Blockette 395 if EndTime is included */ - if ((ehval = yyjson_ptr_get (ehiterval, "/EndTime")) && yyjson_is_str (ehval)) + if (yyjson_ptr_get_str (ehiterval, "/EndTime", &header_string)) { blockette_type = 395; blockette_length = 16; if ((recbuflen - written) < blockette_length) { - ms_log (2, "%s: Record length not large enough for B%d\n", msr->sid, blockette_type); + ms_log (2, "%s: Record length not large enough for B%u\n", msr->sid, blockette_type); yyjson_doc_free (ehdoc); return -1; } @@ -1560,10 +1560,10 @@ msr3_pack_header2 (const MS3Record *msr, char *record, uint32_t recbuflen, int8_ *pMS2B395_TYPE (record + written) = HO2u (blockette_type, swapflag); *pMS2B395_NEXT (record + written) = 0; - if (ms_timestr2btime (yyjson_get_str (ehval), (uint8_t *)pMS2B395_YEAR (record + written), + if (ms_timestr2btime (header_string, (uint8_t *)pMS2B395_YEAR (record + written), msr->sid, swapflag) == -1) { - ms_log (2, "%s: Cannot convert B%d time: %s\n", msr->sid, blockette_type, yyjson_get_str (ehval)); + ms_log (2, "%s: Cannot convert B%u time: %s\n", msr->sid, blockette_type, header_string); yyjson_doc_free (ehdoc); return -1; } @@ -1589,12 +1589,12 @@ msr3_pack_header2 (const MS3Record *msr, char *record, uint32_t recbuflen, int8_ * * \ref MessageOnError - this function logs a message on error ************************************************************************/ -static int -msr_pack_data (void *dest, void *src, int maxsamples, int maxdatabytes, +static int64_t +msr_pack_data (void *dest, void *src, uint64_t maxsamples, uint64_t maxdatabytes, char sampletype, int8_t encoding, int8_t swapflag, uint32_t *byteswritten, const char *sid, int8_t verbose) { - int nsamples; + int64_t nsamples; if (byteswritten) *byteswritten = 0; @@ -1616,7 +1616,7 @@ msr_pack_data (void *dest, void *src, int maxsamples, int maxdatabytes, nsamples = msr_encode_text ((char *)src, maxsamples, (char *)dest, maxdatabytes); if (byteswritten && nsamples > 0) - *byteswritten = nsamples; + *byteswritten = (uint32_t)nsamples; break; @@ -1630,7 +1630,7 @@ msr_pack_data (void *dest, void *src, int maxsamples, int maxdatabytes, if (maxdatabytes < sizeof(int16_t)) { - ms_log (2, "%s: Not enough space in record (%d) for INT16 encoding, need at least %"PRIsize_t" bytes\n", + ms_log (2, "%s: Not enough space in record (%" PRIu64 ") for INT16 encoding, need at least %"PRIsize_t" bytes\n", sid, maxdatabytes, sizeof(int16_t)); return -1; } @@ -1641,7 +1641,7 @@ msr_pack_data (void *dest, void *src, int maxsamples, int maxdatabytes, nsamples = msr_encode_int16 ((int32_t *)src, maxsamples, (int16_t *)dest, maxdatabytes, swapflag); if (byteswritten && nsamples > 0) - *byteswritten = nsamples * 2; + *byteswritten = (uint32_t)(nsamples * 2); break; @@ -1655,7 +1655,7 @@ msr_pack_data (void *dest, void *src, int maxsamples, int maxdatabytes, if (maxdatabytes < sizeof(int32_t)) { - ms_log (2, "%s: Not enough space in record (%d) for INT32 encoding, need at least %"PRIsize_t" bytes\n", + ms_log (2, "%s: Not enough space in record (%" PRIu64 ") for INT32 encoding, need at least %"PRIsize_t" bytes\n", sid, maxdatabytes, sizeof(int32_t)); return -1; } @@ -1666,7 +1666,7 @@ msr_pack_data (void *dest, void *src, int maxsamples, int maxdatabytes, nsamples = msr_encode_int32 ((int32_t *)src, maxsamples, (int32_t *)dest, maxdatabytes, swapflag); if (byteswritten && nsamples > 0) - *byteswritten = nsamples * 4; + *byteswritten = (uint32_t)(nsamples * 4); break; @@ -1680,7 +1680,7 @@ msr_pack_data (void *dest, void *src, int maxsamples, int maxdatabytes, if (maxdatabytes < sizeof(float)) { - ms_log (2, "%s: Not enough space in record (%d) for FLOAT32 encoding, need at least %"PRIsize_t" bytes\n", + ms_log (2, "%s: Not enough space in record (%" PRIu64 ") for FLOAT32 encoding, need at least %"PRIsize_t" bytes\n", sid, maxdatabytes, sizeof(float)); return -1; } @@ -1691,7 +1691,7 @@ msr_pack_data (void *dest, void *src, int maxsamples, int maxdatabytes, nsamples = msr_encode_float32 ((float *)src, maxsamples, (float *)dest, maxdatabytes, swapflag); if (byteswritten && nsamples > 0) - *byteswritten = nsamples * 4; + *byteswritten = (uint32_t)(nsamples * 4); break; @@ -1705,7 +1705,7 @@ msr_pack_data (void *dest, void *src, int maxsamples, int maxdatabytes, if (maxdatabytes < sizeof(double)) { - ms_log (2, "%s: Not enough space in record (%d) for FLOAT64 encoding, need at least %"PRIsize_t" bytes\n", + ms_log (2, "%s: Not enough space in record (%" PRIu64 ") for FLOAT64 encoding, need at least %"PRIsize_t" bytes\n", sid, maxdatabytes, sizeof(double)); return -1; } @@ -1716,7 +1716,7 @@ msr_pack_data (void *dest, void *src, int maxsamples, int maxdatabytes, nsamples = msr_encode_float64 ((double *)src, maxsamples, (double *)dest, maxdatabytes, swapflag); if (byteswritten && nsamples > 0) - *byteswritten = nsamples * 8; + *byteswritten = (uint32_t)(nsamples * 8); break; @@ -1730,7 +1730,7 @@ msr_pack_data (void *dest, void *src, int maxsamples, int maxdatabytes, if (maxdatabytes < 64) { - ms_log (2, "%s: Not enough space in record (%d) for STEIM1 encoding, need at least 64 bytes\n", + ms_log (2, "%s: Not enough space in record (%" PRIu64 ") for STEIM1 encoding, need at least 64 bytes\n", sid, maxdatabytes); return -1; } @@ -1755,7 +1755,7 @@ msr_pack_data (void *dest, void *src, int maxsamples, int maxdatabytes, if (maxdatabytes < 64) { - ms_log (2, "%s: Not enough space in record (%d) for STEIM2 encoding, need at least 64 bytes\n", + ms_log (2, "%s: Not enough space in record (%" PRIu64 ") for STEIM2 encoding, need at least 64 bytes\n", sid, maxdatabytes); return -1; } @@ -2069,7 +2069,7 @@ ms_genfactmult (double samprate, int16_t *factor, int16_t *multiplier) * * \ref MessageOnError - this function logs a message on error ***************************************************************************/ -static inline uint32_t +static inline int64_t ms_timestr2btime (const char *timestr, uint8_t *btime, const char *sid, int8_t swapflag) { uint16_t year; @@ -2082,7 +2082,8 @@ ms_timestr2btime (const char *timestr, uint8_t *btime, const char *sid, int8_t s if (!timestr || !btime) { - ms_log (2, "%s(): Required input not defined: 'timestr' or 'btime'\n", __func__); + ms_log (2, "%s(%s): Required input not defined: 'timestr' or 'btime'\n", + sid, __func__); return -1; } diff --git a/libmseed/packdata.c b/libmseed/packdata.c index 745015d..8187f94 100644 --- a/libmseed/packdata.c +++ b/libmseed/packdata.c @@ -33,16 +33,16 @@ * * Return number of samples in output buffer on success, -1 on failure. ************************************************************************/ -int -msr_encode_text (char *input, int samplecount, char *output, - int outputlength) +int64_t +msr_encode_text (char *input, uint64_t samplecount, char *output, + uint64_t outputlength) { - int length; + uint64_t length; - if (samplecount <= 0) + if (samplecount == 0) return 0; - if (!input || !output || outputlength <= 0) + if (!input || !output || outputlength == 0) return -1; /* Determine minimum of input or output */ @@ -61,19 +61,19 @@ msr_encode_text (char *input, int samplecount, char *output, * * Return number of samples in output buffer on success, -1 on failure. ************************************************************************/ -int -msr_encode_int16 (int32_t *input, int samplecount, int16_t *output, - int outputlength, int swapflag) +int64_t +msr_encode_int16 (int32_t *input, uint64_t samplecount, int16_t *output, + uint64_t outputlength, int swapflag) { - int idx; + uint64_t idx; - if (samplecount <= 0) + if (samplecount == 0) return 0; - if (!input || !output || outputlength <= 0) + if (!input || !output || outputlength == 0) return -1; - for (idx = 0; idx < samplecount && outputlength >= (int)sizeof (int16_t); idx++) + for (idx = 0; idx < samplecount && outputlength >= sizeof (int16_t); idx++) { output[idx] = (int16_t)input[idx]; @@ -94,19 +94,19 @@ msr_encode_int16 (int32_t *input, int samplecount, int16_t *output, * * Return number of samples in output buffer on success, -1 on failure. ************************************************************************/ -int -msr_encode_int32 (int32_t *input, int samplecount, int32_t *output, - int outputlength, int swapflag) +int64_t +msr_encode_int32 (int32_t *input, uint64_t samplecount, int32_t *output, + uint64_t outputlength, int swapflag) { - int idx; + uint64_t idx; - if (samplecount <= 0) + if (samplecount == 0) return 0; - if (!input || !output || outputlength <= 0) + if (!input || !output || outputlength == 0) return -1; - for (idx = 0; idx < samplecount && outputlength >= (int)sizeof (int32_t); idx++) + for (idx = 0; idx < samplecount && outputlength >= sizeof (int32_t); idx++) { output[idx] = input[idx]; @@ -127,19 +127,19 @@ msr_encode_int32 (int32_t *input, int samplecount, int32_t *output, * * Return number of samples in output buffer on success, -1 on failure. ************************************************************************/ -int -msr_encode_float32 (float *input, int samplecount, float *output, - int outputlength, int swapflag) +int64_t +msr_encode_float32 (float *input, uint64_t samplecount, float *output, + uint64_t outputlength, int swapflag) { - int idx; + uint64_t idx; - if (samplecount <= 0) + if (samplecount == 0) return 0; - if (!input || !output || outputlength <= 0) + if (!input || !output || outputlength == 0) return -1; - for (idx = 0; idx < samplecount && outputlength >= (int)sizeof (float); idx++) + for (idx = 0; idx < samplecount && outputlength >= sizeof (float); idx++) { output[idx] = input[idx]; @@ -160,19 +160,19 @@ msr_encode_float32 (float *input, int samplecount, float *output, * * Return number of samples in output buffer on success, -1 on failure. ************************************************************************/ -int -msr_encode_float64 (double *input, int samplecount, double *output, - int outputlength, int swapflag) +int64_t +msr_encode_float64 (double *input, uint64_t samplecount, double *output, + uint64_t outputlength, int swapflag) { - int idx; + uint64_t idx; - if (samplecount <= 0) + if (samplecount == 0) return 0; - if (!input || !output || outputlength <= 0) + if (!input || !output || outputlength == 0) return -1; - for (idx = 0; idx < samplecount && outputlength >= (int)sizeof (double); idx++) + for (idx = 0; idx < samplecount && outputlength >= sizeof (double); idx++) { output[idx] = input[idx]; @@ -221,21 +221,21 @@ msr_encode_float64 (double *input, int samplecount, double *output, * * \ref MessageOnError - this function logs a message on error ************************************************************************/ -int -msr_encode_steim1 (int32_t *input, int samplecount, int32_t *output, - int outputlength, int32_t diff0, uint32_t *byteswritten, +int64_t +msr_encode_steim1 (int32_t *input, uint64_t samplecount, int32_t *output, + uint64_t outputlength, int32_t diff0, uint32_t *byteswritten, int swapflag) { int32_t *frameptr; /* Frame pointer in output */ int32_t *Xnp = NULL; /* Reverse integration constant, aka last sample */ int32_t diffs[4]; int32_t bitwidth[4]; + uint64_t inputidx = 0; + uint64_t outputsamples = 0; + uint64_t maxframes = outputlength / 64; + uint64_t frameidx; int diffcount = 0; - int inputidx = 0; - int outputsamples = 0; - int maxframes = outputlength / 64; int packedsamples = 0; - int frameidx; int startnibble; int widx; int idx; @@ -246,18 +246,18 @@ msr_encode_steim1 (int32_t *input, int samplecount, int32_t *output, int32_t d32; } * word; - if (samplecount <= 0) + if (samplecount == 0) return 0; - if (!input || !output || outputlength <= 0) + if (!input || !output || outputlength == 0) { - ms_log (2, "%s(): Required input not defined: 'input', 'output' or 'outputlength' <= 0\n", + ms_log (2, "%s(): Required input not defined: 'input', 'output' or 'outputlength' == 0\n", __func__); return -1; } #if ENCODE_DEBUG - ms_log (0, "Encoding Steim1 frames, samples: %d, max frames: %d, swapflag: %d\n", + ms_log (0, "Encoding Steim1 frames, samples: %" PRIu64 ", max frames: %" PRIu64 ", swapflag: %d\n", samplecount, maxframes, swapflag); #endif @@ -286,7 +286,7 @@ msr_encode_steim1 (int32_t *input, int samplecount, int32_t *output, startnibble = 3; /* First frame: skip nibbles, X0, and Xn */ #if ENCODE_DEBUG - ms_log (0, "Frame %d: X0=%d\n", frameidx, input[0]); + ms_log (0, "Frame %" PRIu64 ": X0=%d\n", frameidx, input[0]); #endif } else @@ -294,7 +294,7 @@ msr_encode_steim1 (int32_t *input, int samplecount, int32_t *output, startnibble = 1; /* Subsequent frames: skip nibbles */ #if ENCODE_DEBUG - ms_log (0, "Frame %d\n", frameidx); + ms_log (0, "Frame %" PRIu64 "\n", frameidx); #endif } @@ -402,7 +402,7 @@ msr_encode_steim1 (int32_t *input, int samplecount, int32_t *output, ms_gswap4 (Xnp); if (byteswritten) - *byteswritten = frameidx * 64; + *byteswritten = (uint32_t)(frameidx * 64); return outputsamples; } /* End of msr_encode_steim1() */ @@ -421,21 +421,21 @@ msr_encode_steim1 (int32_t *input, int samplecount, int32_t *output, * * \ref MessageOnError - this function logs a message on error ************************************************************************/ -int -msr_encode_steim2 (int32_t *input, int samplecount, int32_t *output, - int outputlength, int32_t diff0, uint32_t *byteswritten, +int64_t +msr_encode_steim2 (int32_t *input, uint64_t samplecount, int32_t *output, + uint64_t outputlength, int32_t diff0, uint32_t *byteswritten, const char *sid, int swapflag) { uint32_t *frameptr; /* Frame pointer in output */ int32_t *Xnp = NULL; /* Reverse integration constant, aka last sample */ int32_t diffs[7]; int32_t bitwidth[7]; + uint64_t inputidx = 0; + uint64_t outputsamples = 0; + uint64_t maxframes = outputlength / 64; + uint64_t frameidx; int diffcount = 0; - int inputidx = 0; - int outputsamples = 0; - int maxframes = outputlength / 64; int packedsamples = 0; - int frameidx; int startnibble; int widx; int idx; @@ -446,18 +446,18 @@ msr_encode_steim2 (int32_t *input, int samplecount, int32_t *output, int32_t d32; } * word; - if (samplecount <= 0) + if (samplecount == 0) return 0; - if (!input || !output || outputlength <= 0) + if (!input || !output || outputlength == 0) { - ms_log (2, "%s(): Required input not defined: 'input', 'output' or 'outputlength' <= 0\n", + ms_log (2, "%s(): Required input not defined: 'input', 'output' or 'outputlength' == 0\n", __func__); return -1; } #if ENCODE_DEBUG - ms_log (0, "Encoding Steim2 frames, samples: %d, max frames: %d, swapflag: %d\n", + ms_log (0, "Encoding Steim2 frames, samples: %" PRIu64 ", max frames: %" PRIu64 ", swapflag: %d\n", samplecount, maxframes, swapflag); #endif @@ -480,7 +480,7 @@ msr_encode_steim2 (int32_t *input, int samplecount, int32_t *output, frameptr[1] = input[0]; #if ENCODE_DEBUG - ms_log (0, "Frame %d: X0=%d\n", frameidx, input[0]); + ms_log (0, "Frame %" PRIu64 ": X0=%d\n", frameidx, input[0]); #endif if (swapflag) @@ -495,7 +495,7 @@ msr_encode_steim2 (int32_t *input, int samplecount, int32_t *output, startnibble = 1; /* Subsequent frames: skip nibbles */ #if ENCODE_DEBUG - ms_log (0, "Frame %d\n", frameidx); + ms_log (0, "Frame %" PRIu64 "\n", frameidx); #endif } @@ -718,7 +718,7 @@ msr_encode_steim2 (int32_t *input, int samplecount, int32_t *output, ms_gswap4 (Xnp); if (byteswritten) - *byteswritten = frameidx * 64; + *byteswritten = (uint32_t)(frameidx * 64); return outputsamples; } /* End of msr_encode_steim2() */ diff --git a/libmseed/packdata.h b/libmseed/packdata.h index ae499d8..43dc49b 100644 --- a/libmseed/packdata.h +++ b/libmseed/packdata.h @@ -31,25 +31,22 @@ extern "C" { #define STEIM1_FRAME_MAX_SAMPLES 60 #define STEIM2_FRAME_MAX_SAMPLES 105 -/* Control for printing debugging information, declared in packdata.c */ -extern int libmseed_encodedebug; - -extern int msr_encode_text (char *input, int samplecount, char *output, - int outputlength); -extern int msr_encode_int16 (int32_t *input, int samplecount, int16_t *output, - int outputlength, int swapflag); -extern int msr_encode_int32 (int32_t *input, int samplecount, int32_t *output, - int outputlength, int swapflag); -extern int msr_encode_float32 (float *input, int samplecount, float *output, - int outputlength, int swapflag); -extern int msr_encode_float64 (double *input, int samplecount, double *output, - int outputlength, int swapflag); -extern int msr_encode_steim1 (int32_t *input, int samplecount, int32_t *output, - int outputlength, int32_t diff0, uint32_t *byteswritten, - int swapflag); -extern int msr_encode_steim2 (int32_t *input, int samplecount, int32_t *output, - int outputlength, int32_t diff0, uint32_t *byteswritten, - const char *sid, int swapflag); +extern int64_t msr_encode_text (char *input, uint64_t samplecount, char *output, + uint64_t outputlength); +extern int64_t msr_encode_int16 (int32_t *input, uint64_t samplecount, int16_t *output, + uint64_t outputlength, int swapflag); +extern int64_t msr_encode_int32 (int32_t *input, uint64_t samplecount, int32_t *output, + uint64_t outputlength, int swapflag); +extern int64_t msr_encode_float32 (float *input, uint64_t samplecount, float *output, + uint64_t outputlength, int swapflag); +extern int64_t msr_encode_float64 (double *input, uint64_t samplecount, double *output, + uint64_t outputlength, int swapflag); +extern int64_t msr_encode_steim1 (int32_t *input, uint64_t samplecount, int32_t *output, + uint64_t outputlength, int32_t diff0, uint32_t *byteswritten, + int swapflag); +extern int64_t msr_encode_steim2 (int32_t *input, uint64_t samplecount, int32_t *output, + uint64_t outputlength, int32_t diff0, uint32_t *byteswritten, + const char *sid, int swapflag); #ifdef __cplusplus } diff --git a/libmseed/parseutils.c b/libmseed/parseutils.c index ce18645..0ce29ec 100644 --- a/libmseed/parseutils.c +++ b/libmseed/parseutils.c @@ -61,8 +61,8 @@ int msr3_parse (const char *record, uint64_t recbuflen, MS3Record **ppmsr, uint32_t flags, int8_t verbose) { - int reclen = 0; - int retcode = MS_NOERROR; + int64_t reclen = 0; + int64_t retcode = MS_NOERROR; uint8_t formatversion = 0; if (!ppmsr || !record) @@ -88,7 +88,7 @@ msr3_parse (const char *record, uint64_t recbuflen, MS3Record **ppmsr, (recbuflen & (recbuflen - 1)) == 0 && recbuflen <= MAXRECLEN) { - reclen = (int)recbuflen; + reclen = (int64_t)recbuflen; } /* No data record detected */ @@ -105,36 +105,37 @@ msr3_parse (const char *record, uint64_t recbuflen, MS3Record **ppmsr, if (verbose > 2) { - ms_log (0, "Detected record length of %d bytes\n", reclen); + ms_log (0, "Detected record length of %" PRId64 " bytes\n", reclen); } /* Check that record length is in supported range */ if (reclen < MINRECLEN || reclen > MAXRECLEN) { - ms_log (2, "Record length of %d is out of range allowed: %d to %d)\n", + ms_log (2, "Record length of %" PRId64 " is out of range allowed: %d to %d)\n", reclen, MINRECLEN, MAXRECLEN); return MS_OUTOFRANGE; } - /* Check if more data is required, return hint */ - if (reclen > recbuflen) + else if ((uint64_t)reclen > recbuflen) { + uint64_t need = reclen - recbuflen; + if (verbose > 2) - ms_log (0, "Detected %d byte record, need %d more bytes\n", - reclen, (int)(reclen - recbuflen)); + ms_log (0, "Detected %" PRId64 " byte record, need %" PRIu64 " more bytes\n", + reclen, need); - return (int)(reclen - recbuflen); + return (need > MAXRECLEN) ? MAXRECLEN : (int)need; } /* Unpack record */ if (formatversion == 3) { - retcode = msr3_unpack_mseed3 (record, reclen, ppmsr, flags, verbose); + retcode = msr3_unpack_mseed3 (record, (int)reclen, ppmsr, flags, verbose); } else if (formatversion == 2) { - retcode = msr3_unpack_mseed2 (record, reclen, ppmsr, flags, verbose); + retcode = msr3_unpack_mseed2 (record, (int)reclen, ppmsr, flags, verbose); } else { @@ -147,7 +148,7 @@ msr3_parse (const char *record, uint64_t recbuflen, MS3Record **ppmsr, { msr3_free (ppmsr); - return retcode; + return (int)retcode; } return MS_NOERROR; @@ -174,12 +175,12 @@ msr3_parse (const char *record, uint64_t recbuflen, MS3Record **ppmsr, * * \ref MessageOnError - this function logs a message on error *********************************************************************/ -int +int64_t ms3_detect (const char *record, uint64_t recbuflen, uint8_t *formatversion) { uint8_t swapflag = 0; /* Byte swapping flag */ uint8_t foundlen = 0; /* Found record length */ - int32_t reclen = -1; /* Size of record in bytes */ + int64_t reclen = -1; /* Size of record in bytes */ uint16_t blkt_offset; /* Byte offset for next blockette */ uint16_t blkt_type; @@ -236,13 +237,13 @@ ms3_detect (const char *record, uint64_t recbuflen, uint8_t *formatversion) /* Found a 1000 blockette, not truncated */ if (blkt_type == 1000 && - (int)(blkt_offset + 8) <= recbuflen) + (uint64_t)(blkt_offset + 8) <= recbuflen) { foundlen = 1; /* Field 3 of B1000 is a uint8_t value describing the record * length as 2^(value). Calculate 2-raised with a shift. */ - reclen = (unsigned int)1 << *pMS2B1000_RECLEN (record + blkt_offset); + reclen = (uint64_t)1 << *pMS2B1000_RECLEN (record + blkt_offset); break; } @@ -265,7 +266,7 @@ ms3_detect (const char *record, uint64_t recbuflen, uint8_t *formatversion) nextfsdh = record + 64; /* Check for record header or blank/noise record at MINRECLEN byte offsets */ - while (((nextfsdh - record) + 48) < recbuflen) + while (((uint64_t)(nextfsdh - record) + 48) < recbuflen) { if (MS2_ISVALIDHEADER (nextfsdh)) { @@ -974,7 +975,7 @@ ms_parse_raw2 (const char *record, int maxreclen, int8_t details, int8_t swapfla ms_log (0, " step duration: %u\n", HO4u(*pMS2B300_STEPDURATION (record + blkt_offset), swapflag)); ms_log (0, " interval duration: %u\n", HO4u(*pMS2B300_INTERVALDURATION (record + blkt_offset), swapflag)); ms_log (0, " signal amplitude: %g\n", HO4f(*pMS2B300_AMPLITUDE (record + blkt_offset), swapflag)); - ms_log (0, " input signal channel: %.3s", pMS2B300_INPUTCHANNEL (record + blkt_offset)); + ms_log (0, " input signal channel: %.3s\n", pMS2B300_INPUTCHANNEL (record + blkt_offset)); if (details > 1) ms_log (0, " reserved byte: %u\n", *pMS2B300_RESERVED (record + blkt_offset)); ms_log (0, " reference amplitude: %u\n", HO4u(*pMS2B300_REFERENCEAMPLITUDE (record + blkt_offset), swapflag)); @@ -1016,7 +1017,7 @@ ms_parse_raw2 (const char *record, int maxreclen, int8_t details, int8_t swapfla ms_log (0, " calibration duration: %u\n", HO4u(*pMS2B310_DURATION (record + blkt_offset), swapflag)); ms_log (0, " signal period: %g\n", HO4f(*pMS2B310_PERIOD (record + blkt_offset), swapflag)); ms_log (0, " signal amplitude: %g\n", HO4f(*pMS2B310_AMPLITUDE (record + blkt_offset), swapflag)); - ms_log (0, " input signal channel: %.3s", pMS2B310_INPUTCHANNEL (record + blkt_offset)); + ms_log (0, " input signal channel: %.3s\n", pMS2B310_INPUTCHANNEL (record + blkt_offset)); if (details > 1) ms_log (0, " reserved byte: %u\n", *pMS2B310_RESERVED2 (record + blkt_offset)); ms_log (0, " reference amplitude: %u\n", HO4u(*pMS2B310_REFERENCEAMPLITUDE (record + blkt_offset), swapflag)); @@ -1053,7 +1054,7 @@ ms_parse_raw2 (const char *record, int maxreclen, int8_t details, int8_t swapfla ms_log (0, " calibration duration: %u\n", HO4u(*pMS2B320_DURATION (record + blkt_offset), swapflag)); ms_log (0, " peak-to-peak amplitude: %g\n", HO4f(*pMS2B320_PTPAMPLITUDE (record + blkt_offset), swapflag)); - ms_log (0, " input signal channel: %.3s", pMS2B320_INPUTCHANNEL (record + blkt_offset)); + ms_log (0, " input signal channel: %.3s\n", pMS2B320_INPUTCHANNEL (record + blkt_offset)); if (details > 1) ms_log (0, " reserved byte: %u\n", *pMS2B320_RESERVED2 (record + blkt_offset)); ms_log (0, " reference amplitude: %u\n", HO4u(*pMS2B320_REFERENCEAMPLITUDE (record + blkt_offset), swapflag)); @@ -1089,7 +1090,7 @@ ms_parse_raw2 (const char *record, int maxreclen, int8_t details, int8_t swapfla ms_log (0, " calibration duration: %u\n", HO4u(*pMS2B390_DURATION (record + blkt_offset), swapflag)); ms_log (0, " signal amplitude: %g\n", HO4f(*pMS2B390_AMPLITUDE (record + blkt_offset), swapflag)); - ms_log (0, " input signal channel: %.3s", pMS2B390_INPUTCHANNEL (record + blkt_offset)); + ms_log (0, " input signal channel: %.3s\n", pMS2B390_INPUTCHANNEL (record + blkt_offset)); if (details > 1) ms_log (0, " reserved byte: %u\n", *pMS2B390_RESERVED2 (record + blkt_offset)); } diff --git a/libmseed/selection.c b/libmseed/selection.c index 377a87c..cd7cb31 100644 --- a/libmseed/selection.c +++ b/libmseed/selection.c @@ -916,6 +916,8 @@ ms_globmatch (const char *string, const char *pattern) case '\\': if (*pattern) c = *pattern++; + break; + default: if (c != *string) return GLOBMATCH_FALSE; diff --git a/libmseed/test/test-extraheaders.c b/libmseed/test/test-extraheaders.c index 4bd0349..8c5c9c8 100644 --- a/libmseed/test/test-extraheaders.c +++ b/libmseed/test/test-extraheaders.c @@ -187,7 +187,7 @@ TEST (extraheaders, replace) /* Replace extra headers with new, compact doc */ newdoc = "{\"new\":{\"string\":\"Updated value\"}}"; rv = mseh_replace (msr, newdoc); - CHECK (rv == strlen(newdoc), "mseh_replace() returned unexpected error"); + CHECK (rv == (int)strlen(newdoc), "mseh_replace() returned unexpected error"); REQUIRE (msr->extra != NULL, "msr->extra cannot be NULL"); CHECK_SUBSTREQ (msr->extra, newdoc, strlen(newdoc)); @@ -195,7 +195,7 @@ TEST (extraheaders, replace) newdoc = "{\"new\":{\"string\":\"Updated value\"}}"; newdoc_uncompact = "{ \"new\":\n { \"string\" : \n \"Updated value\" } }"; rv = mseh_replace (msr, newdoc_uncompact); - CHECK (rv == strlen(newdoc), "mseh_replace() returned unexpected error"); + CHECK (rv == (int)strlen(newdoc), "mseh_replace() returned unexpected error"); REQUIRE (msr->extra != NULL, "msr->extra cannot be NULL"); CHECK_SUBSTREQ (msr->extra, newdoc, strlen(newdoc)); diff --git a/libmseed/test/test-runner.c b/libmseed/test/test-runner.c index 8bae2c5..19e0642 100644 --- a/libmseed/test/test-runner.c +++ b/libmseed/test/test-runner.c @@ -94,7 +94,7 @@ cmpfiles (char *fileA, char *fileB) int cmpint32s (int32_t *arrayA, int32_t *arrayB, size_t length) { - int idx; + size_t idx; for (idx = 0; idx < length; idx++) { @@ -112,7 +112,7 @@ cmpint32s (int32_t *arrayA, int32_t *arrayB, size_t length) int cmpfloats (float *arrayA, float *arrayB, size_t length) { - int idx; + size_t idx; for (idx = 0; idx < length; idx++) { @@ -130,7 +130,7 @@ cmpfloats (float *arrayA, float *arrayB, size_t length) int cmpdoubles (double *arrayA, double *arrayB, size_t length) { - int idx; + size_t idx; for (idx = 0; idx < length; idx++) { diff --git a/libmseed/tracelist.c b/libmseed/tracelist.c index 4ddc98e..026793d 100644 --- a/libmseed/tracelist.c +++ b/libmseed/tracelist.c @@ -362,6 +362,7 @@ mstl3_addmsr_recordptr (MS3TraceList *mstl, const MS3Record *msr, MS3RecordPtr * int8_t splitversion, int8_t autoheal, uint32_t flags, const MS3Tolerance *tolerance) { + (void)flags; /* Unused */ MS3TraceID *id = 0; MS3TraceID *previd[MSTRACEID_SKIPLIST_HEIGHT] = {NULL}; @@ -1118,7 +1119,9 @@ mstl3_addmsrtoseg (MS3TraceSeg *seg, const MS3Record *msr, nstime_t endtime, int if (libmseed_prealloc_block_size) { - newdatasamples = libmseed_memory_prealloc (seg->datasamples, newdatasize, &(seg->datasize)); + size_t current_size = seg->datasize; + newdatasamples = libmseed_memory_prealloc (seg->datasamples, newdatasize, ¤t_size); + seg->datasize = current_size; } else { @@ -1219,7 +1222,9 @@ mstl3_addsegtoseg (MS3TraceSeg *seg1, MS3TraceSeg *seg2) if (libmseed_prealloc_block_size) { - newdatasamples = libmseed_memory_prealloc (seg1->datasamples, newdatasize, &(seg1->datasize)); + size_t current_size = seg1->datasize; + newdatasamples = libmseed_memory_prealloc (seg1->datasamples, newdatasize, ¤t_size); + seg1->datasize = current_size; } else { @@ -1644,17 +1649,17 @@ mstl3_resize_buffers (MS3TraceList *mstl) ***************************************************************************/ int64_t mstl3_unpack_recordlist (MS3TraceID *id, MS3TraceSeg *seg, void *output, - size_t outputsize, int8_t verbose) + uint64_t outputsize, int8_t verbose) { MS3RecordPtr *recordptr = NULL; int64_t unpackedsamples = 0; int64_t totalunpackedsamples = 0; char *filebuffer = NULL; - size_t filebuffersize = 0; + int64_t filebuffersize = 0; - size_t outputoffset = 0; - size_t decodedsize = 0; + uint64_t outputoffset = 0; + uint64_t decodedsize = 0; uint8_t samplesize = 0; char sampletype = 0; char recsampletype = 0; @@ -1685,7 +1690,7 @@ mstl3_unpack_recordlist (MS3TraceID *id, MS3TraceSeg *seg, void *output, recordptr = seg->recordlist->first; - if (ms_encoding_sizetype(recordptr->msr->encoding, &samplesize, &sampletype)) + if (ms_encoding_sizetype((uint8_t)recordptr->msr->encoding, &samplesize, &sampletype)) { ms_log (2, "%s: Cannot determine sample size and type for encoding: %u\n", id->sid, recordptr->msr->encoding); @@ -1693,14 +1698,14 @@ mstl3_unpack_recordlist (MS3TraceID *id, MS3TraceSeg *seg, void *output, } /* Calculate buffer size needed for unpacked samples */ - decodedsize = (size_t)seg->samplecnt * samplesize; + decodedsize = seg->samplecnt * samplesize; /* If output buffer is supplied, check needed size */ if (output) { if (decodedsize > outputsize) { - ms_log (2, "%s: Output buffer (%"PRIsize_t" bytes) is not large enough for decoded data (%"PRIsize_t" bytes)\n", + ms_log (2, "%s: Output buffer (%" PRIu64 " bytes) is not large enough for decoded data (%" PRIu64 " bytes)\n", id->sid, decodedsize, outputsize); return -1; } @@ -1714,7 +1719,7 @@ mstl3_unpack_recordlist (MS3TraceID *id, MS3TraceSeg *seg, void *output, /* Otherwise allocate new buffer */ else { - if ((output = libmseed_memory.malloc (decodedsize)) == NULL) + if ((output = libmseed_memory.malloc ((size_t)decodedsize)) == NULL) { ms_log (2, "%s: Cannot allocate memory for segment data samples\n", id->sid); return -1; @@ -1735,7 +1740,7 @@ mstl3_unpack_recordlist (MS3TraceID *id, MS3TraceSeg *seg, void *output, continue; } - if (ms_encoding_sizetype(recordptr->msr->encoding, NULL, &recsampletype)) + if (ms_encoding_sizetype((uint8_t)recordptr->msr->encoding, NULL, &recsampletype)) { ms_log (2, "%s: Cannot determine sample type for encoding: %u\n", id->sid, recordptr->msr->encoding); @@ -1830,7 +1835,7 @@ mstl3_unpack_recordlist (MS3TraceID *id, MS3TraceSeg *seg, void *output, } /* Read record into buffer */ - if (fread (filebuffer, 1, recordptr->msr->reclen, fileptr) != recordptr->msr->reclen) + if (fread (filebuffer, 1, recordptr->msr->reclen, fileptr) != (size_t)recordptr->msr->reclen) { ms_log (2, "%s: Cannot read record from file: %s (%s)\n", id->sid, @@ -1854,7 +1859,7 @@ mstl3_unpack_recordlist (MS3TraceID *id, MS3TraceSeg *seg, void *output, /* Decode data from buffer */ unpackedsamples = ms_decode_data (input, recordptr->msr->reclen - recordptr->dataoffset, - recordptr->msr->encoding, recordptr->msr->samplecnt, + (uint8_t)recordptr->msr->encoding, recordptr->msr->samplecnt, (unsigned char *)output + outputoffset, decodedsize - outputoffset, &sampletype, recordptr->msr->swapflag, id->sid, verbose); @@ -2373,7 +2378,7 @@ mstl3_printgaplist (const MS3TraceList *mstl, ms_timeformat_t timeformat, return; } - ms_log (0, " SourceID Last Sample Next Sample Gap Samples\n"); + ms_log (0, " SourceID Last Sample Next Sample Gap Samples\n"); id = mstl->traces.next[0]; while (id) @@ -2436,7 +2441,7 @@ mstl3_printgaplist (const MS3TraceList *mstl, ms_timeformat_t timeformat, if (ms_nstime2timestr (seg->next->starttime, time2, timeformat, NANO_MICRO) == NULL) ms_log (2, "Cannot convert trace end time for %s\n", id->sid); - ms_log (0, "%-17s %-24s %-24s %-4s %-.8g\n", + ms_log (0, "%-27s %-28s %-28s %-4s %-.8g\n", id->sid, time1, time2, gapstr, nsamples); gapcnt++; diff --git a/libmseed/unpack.c b/libmseed/unpack.c index 91f1a01..f6ce604 100644 --- a/libmseed/unpack.c +++ b/libmseed/unpack.c @@ -1035,7 +1035,7 @@ msr3_data_bounds (const MS3Record *msr, uint32_t *dataoffset, uint32_t *datasize /* Determine offset to data */ if (msr->formatversion == 3) { - *dataoffset = MS3FSDH_LENGTH + strlen (msr->sid) + msr->extralength; + *dataoffset = MS3FSDH_LENGTH + (uint32_t)strlen (msr->sid) + msr->extralength; *datasize = msr->datalength; } else if (msr->formatversion == 2) @@ -1120,7 +1120,7 @@ msr3_data_bounds (const MS3Record *msr, uint32_t *dataoffset, uint32_t *datasize int64_t msr3_unpack_data (MS3Record *msr, int8_t verbose) { - uint32_t datasize; /* byte size of data samples in record */ + uint32_t datasize; /* length of data payload in bytes */ int64_t nsamples; /* number of samples unpacked */ size_t unpacksize; /* byte size of unpacked samples */ uint8_t samplesize = 0; /* size of the data samples in bytes */ @@ -1181,7 +1181,7 @@ msr3_unpack_data (MS3Record *msr, int8_t verbose) msr->encoding = DE_STEIM1; } - if (ms_encoding_sizetype(msr->encoding, &samplesize, NULL)) + if (ms_encoding_sizetype((uint8_t)msr->encoding, &samplesize, NULL)) { ms_log (2, "%s: Cannot determine sample size for encoding: %u\n", msr->sid, msr->encoding); return MS_GENERROR; @@ -1210,7 +1210,9 @@ msr3_unpack_data (MS3Record *msr, int8_t verbose) { if (libmseed_prealloc_block_size) { - msr->datasamples = libmseed_memory_prealloc (msr->datasamples, unpacksize, &(msr->datasize)); + size_t current_size = msr->datasize; + msr->datasamples = libmseed_memory_prealloc (msr->datasamples, unpacksize, ¤t_size); + msr->datasize = current_size; } else { @@ -1239,7 +1241,7 @@ msr3_unpack_data (MS3Record *msr, int8_t verbose) if (verbose > 2) ms_log (0, "%s: Unpacking %" PRId64 " samples\n", msr->sid, msr->samplecnt); - nsamples = ms_decode_data (encoded, datasize, msr->encoding, msr->samplecnt, + nsamples = ms_decode_data (encoded, datasize, (uint8_t)msr->encoding, msr->samplecnt, msr->datasamples, msr->datasize, &(msr->sampletype), (msr->swapflag & MSSWAP_PAYLOAD), msr->sid, verbose); @@ -1271,12 +1273,12 @@ msr3_unpack_data (MS3Record *msr, int8_t verbose) * \ref MessageOnError - this function logs a message on error ************************************************************************/ int64_t -ms_decode_data (const void *input, size_t inputsize, uint8_t encoding, - int64_t samplecount, void *output, size_t outputsize, +ms_decode_data (const void *input, uint64_t inputsize, uint8_t encoding, + uint64_t samplecount, void *output, uint64_t outputsize, char *sampletype, int8_t swapflag, const char *sid, int8_t verbose) { - size_t decodedsize; /* byte size of decodeded samples */ - int32_t nsamples; /* number of samples unpacked */ + uint64_t decodedsize; /* byte size of decodeded samples */ + int64_t nsamples; /* number of samples unpacked */ uint8_t samplesize = 0; /* size of the data samples in bytes */ if (!input || !output || !sampletype) @@ -1286,18 +1288,18 @@ ms_decode_data (const void *input, size_t inputsize, uint8_t encoding, return MS_GENERROR; } - if (samplecount <= 0) + if (samplecount == 0) return 0; if (ms_encoding_sizetype(encoding, &samplesize, sampletype)) samplesize = 0; /* Calculate buffer size needed for unpacked samples */ - decodedsize = (size_t)samplecount * samplesize; + decodedsize = samplecount * samplesize; if (decodedsize > outputsize) { - ms_log (2, "%s: Output buffer (%"PRIsize_t" bytes) is not large enought for decoded data (%"PRIsize_t" bytes)\n", + ms_log (2, "%s: Output buffer (%"PRIu64" bytes) is not large enought for decoded data (%"PRIu64" bytes)\n", (sid) ? sid : "", decodedsize, outputsize); return MS_GENERROR; } @@ -1309,7 +1311,7 @@ ms_decode_data (const void *input, size_t inputsize, uint8_t encoding, if (verbose > 1) ms_log (0, "%s: Decoding TEXT data\n", (sid) ? sid : ""); - nsamples = (int32_t)samplecount; + nsamples = (int64_t)samplecount; if (nsamples > 0) { memcpy (output, input, nsamples); @@ -1431,9 +1433,9 @@ ms_decode_data (const void *input, size_t inputsize, uint8_t encoding, break; } - if (nsamples >= 0 && nsamples != samplecount) + if (nsamples >= 0 && (uint64_t)nsamples != samplecount) { - ms_log (2, "%s: only decoded %d samples of %" PRId64 " expected\n", + ms_log (2, "%s: only decoded %" PRId64 " samples of %" PRIu64 " expected\n", (sid) ? sid : "", nsamples, samplecount); return MS_GENERROR; } diff --git a/libmseed/unpackdata.c b/libmseed/unpackdata.c index a0564bc..0414b67 100644 --- a/libmseed/unpackdata.c +++ b/libmseed/unpackdata.c @@ -43,20 +43,20 @@ * * Return number of samples in output buffer on success, -1 on error. ************************************************************************/ -int -msr_decode_int16 (int16_t *input, int64_t samplecount, int32_t *output, - int64_t outputlength, int swapflag) +int64_t +msr_decode_int16 (int16_t *input, uint64_t samplecount, int32_t *output, + uint64_t outputlength, int swapflag) { int16_t sample; - int idx; + uint32_t idx; - if (samplecount <= 0) + if (samplecount == 0) return 0; - if (!input || !output || outputlength <= 0) + if (!input || !output || outputlength < sizeof (int32_t)) return -1; - for (idx = 0; idx < samplecount && outputlength >= (int)sizeof (int32_t); idx++) + for (idx = 0; idx < samplecount && outputlength >= sizeof (int32_t); idx++) { sample = input[idx]; @@ -79,20 +79,20 @@ msr_decode_int16 (int16_t *input, int64_t samplecount, int32_t *output, * * Return number of samples in output buffer on success, -1 on error. ************************************************************************/ -int -msr_decode_int32 (int32_t *input, int64_t samplecount, int32_t *output, - int64_t outputlength, int swapflag) +int64_t +msr_decode_int32 (int32_t *input, uint64_t samplecount, int32_t *output, + uint64_t outputlength, int swapflag) { int32_t sample; - int idx; + uint32_t idx; - if (samplecount <= 0) + if (samplecount == 0) return 0; - if (!input || !output || outputlength <= 0) + if (!input || !output || outputlength < sizeof (int32_t)) return -1; - for (idx = 0; idx < samplecount && outputlength >= (int)sizeof (int32_t); idx++) + for (idx = 0; idx < samplecount && outputlength >= sizeof (int32_t); idx++) { sample = input[idx]; @@ -115,20 +115,20 @@ msr_decode_int32 (int32_t *input, int64_t samplecount, int32_t *output, * * Return number of samples in output buffer on success, -1 on error. ************************************************************************/ -int -msr_decode_float32 (float *input, int64_t samplecount, float *output, - int64_t outputlength, int swapflag) +int64_t +msr_decode_float32 (float *input, uint64_t samplecount, float *output, + uint64_t outputlength, int swapflag) { float sample; - int idx; + uint32_t idx; - if (samplecount <= 0) + if (samplecount == 0) return 0; - if (!input || !output || outputlength <= 0) + if (!input || !output || outputlength < sizeof (float)) return -1; - for (idx = 0; idx < samplecount && outputlength >= (int)sizeof (float); idx++) + for (idx = 0; idx < samplecount && outputlength >= sizeof (float); idx++) { memcpy (&sample, &input[idx], sizeof (float)); @@ -151,20 +151,20 @@ msr_decode_float32 (float *input, int64_t samplecount, float *output, * * Return number of samples in output buffer on success, -1 on error. ************************************************************************/ -int -msr_decode_float64 (double *input, int64_t samplecount, double *output, - int64_t outputlength, int swapflag) +int64_t +msr_decode_float64 (double *input, uint64_t samplecount, double *output, + uint64_t outputlength, int swapflag) { double sample; - int idx; + uint32_t idx; - if (samplecount <= 0) + if (samplecount == 0) return 0; - if (!input || !output || outputlength <= 0) + if (!input || !output || outputlength < sizeof (double)) return -1; - for (idx = 0; idx < samplecount && outputlength >= (int)sizeof (double); idx++) + for (idx = 0; idx < samplecount && outputlength >= sizeof (double); idx++) { memcpy (&sample, &input[idx], sizeof (double)); @@ -187,18 +187,18 @@ msr_decode_float64 (double *input, int64_t samplecount, double *output, * * Return number of samples in output buffer on success, -1 on error. ************************************************************************/ -int -msr_decode_steim1 (int32_t *input, int inputlength, int64_t samplecount, - int32_t *output, int64_t outputlength, const char *srcname, +int64_t +msr_decode_steim1 (int32_t *input, uint64_t inputlength, uint64_t samplecount, + int32_t *output, uint64_t outputlength, const char *srcname, int swapflag) { uint32_t frame[16]; /* Frame, 16 x 32-bit quantities = 64 bytes */ int32_t diff[60]; /* Difference values for a frame, max is 15 x 4 (8-bit samples) */ int32_t Xn = 0; /* Reverse integration constant, aka last sample */ - int64_t outputidx; - int maxframes = inputlength / 64; + uint64_t outputidx; + uint64_t maxframes = inputlength / 64; + uint64_t frameidx; int diffidx; - int frameidx; int startnibble; int nibble; int widx; @@ -210,10 +210,10 @@ msr_decode_steim1 (int32_t *input, int inputlength, int64_t samplecount, int32_t d32; } *word; - if (inputlength <= 0) + if (maxframes == 0) return 0; - if (!input || !output || outputlength <= 0 || maxframes <= 0) + if (!input || !output || outputlength == 0) return -1; /* Make sure output buffer is sufficient for all output samples */ @@ -225,7 +225,7 @@ msr_decode_steim1 (int32_t *input, int inputlength, int64_t samplecount, } #if DECODE_DEBUG - ms_log (0, "Decoding %d Steim1 frames, swapflag: %d, srcname: %s\n", + ms_log (0, "Decoding %"PRIu64" Steim1 frames, swapflag: %d, srcname: %s\n", maxframes, swapflag, (srcname) ? srcname : ""); #endif @@ -254,7 +254,7 @@ msr_decode_steim1 (int32_t *input, int inputlength, int64_t samplecount, startnibble = 3; /* First frame: skip nibbles, X0, and Xn */ #if DECODE_DEBUG - ms_log (0, "Frame %d: X0=%d Xn=%d\n", frameidx, output[0], Xn); + ms_log (0, "Frame %"PRIu64": X0=%d Xn=%d\n", frameidx, output[0], Xn); #endif } else @@ -262,7 +262,7 @@ msr_decode_steim1 (int32_t *input, int inputlength, int64_t samplecount, startnibble = 1; /* Subsequent frames: skip nibbles */ #if DECODE_DEBUG - ms_log (0, "Frame %d\n", frameidx); + ms_log (0, "Frame %"PRIu64"\n", frameidx); #endif } @@ -346,7 +346,7 @@ msr_decode_steim1 (int32_t *input, int inputlength, int64_t samplecount, srcname, output[outputidx - 1], Xn); } - return (int)outputidx; + return outputidx; } /* End of msr_decode_steim1() */ /************************************************************************ @@ -357,18 +357,18 @@ msr_decode_steim1 (int32_t *input, int inputlength, int64_t samplecount, * * Return number of samples in output buffer on success, -1 on error. ************************************************************************/ -int -msr_decode_steim2 (int32_t *input, int inputlength, int64_t samplecount, - int32_t *output, int64_t outputlength, const char *srcname, +int64_t +msr_decode_steim2 (int32_t *input, uint64_t inputlength, uint64_t samplecount, + int32_t *output, uint64_t outputlength, const char *srcname, int swapflag) { uint32_t frame[16]; /* Frame, 16 x 32-bit quantities = 64 bytes */ int32_t diff[105]; /* Difference values for a frame, max is 15 x 7 (4-bit samples) */ int32_t Xn = 0; /* Reverse integration constant, aka last sample */ - int64_t outputidx; - int maxframes = inputlength / 64; + uint64_t outputidx; + uint64_t maxframes = inputlength / 64; + uint64_t frameidx; int diffidx; - int frameidx; int startnibble; int nibble; int widx; @@ -388,10 +388,10 @@ msr_decode_steim2 (int32_t *input, int inputlength, int64_t samplecount, struct {signed int x:15;} s15; struct {signed int x:30;} s30; - if (inputlength <= 0) + if (maxframes == 0) return 0; - if (!input || !output || outputlength <= 0 || maxframes <= 0) + if (!input || !output || outputlength == 0) return -1; /* Make sure output buffer is sufficient for all output samples */ @@ -403,7 +403,7 @@ msr_decode_steim2 (int32_t *input, int inputlength, int64_t samplecount, } #if DECODE_DEBUG - ms_log (0, "Decoding %d Steim2 frames, swapflag: %d, srcname: %s\n", + ms_log (0, "Decoding %"PRIu64" Steim2 frames, swapflag: %d, srcname: %s\n", maxframes, swapflag, (srcname) ? srcname : ""); #endif @@ -432,7 +432,7 @@ msr_decode_steim2 (int32_t *input, int inputlength, int64_t samplecount, startnibble = 3; /* First frame: skip nibbles, X0, and Xn */ #if DECODE_DEBUG - ms_log (0, "Frame %d: X0=%d Xn=%d\n", frameidx, output[0], Xn); + ms_log (0, "Frame %"PRIu64": X0=%d Xn=%d\n", frameidx, output[0], Xn); #endif } else @@ -440,7 +440,7 @@ msr_decode_steim2 (int32_t *input, int inputlength, int64_t samplecount, startnibble = 1; /* Subsequent frames: skip nibbles */ #if DECODE_DEBUG - ms_log (0, "Frame %d\n", frameidx); + ms_log (0, "Frame %"PRIu64"\n", frameidx); #endif } @@ -596,7 +596,7 @@ msr_decode_steim2 (int32_t *input, int inputlength, int64_t samplecount, srcname, output[outputidx - 1], Xn); } - return (int)outputidx; + return outputidx; } /* End of msr_decode_steim2() */ /* Defines for GEOSCOPE encoding */ @@ -615,16 +615,16 @@ msr_decode_steim2 (int32_t *input, int inputlength, int64_t samplecount, * * \ref MessageOnError - this function logs a message on error ************************************************************************/ -int -msr_decode_geoscope (char *input, int64_t samplecount, float *output, - int64_t outputlength, int encoding, +int64_t +msr_decode_geoscope (char *input, uint64_t samplecount, float *output, + uint64_t outputlength, int encoding, const char *srcname, int swapflag) { - int idx = 0; - int mantissa; /* mantissa from SEED data */ - int gainrange; /* gain range factor */ - int exponent; /* total exponent */ - int k; + uint32_t idx = 0; + int32_t mantissa; /* mantissa from SEED data */ + int32_t gainrange; /* gain range factor */ + int32_t exponent; /* total exponent */ + int32_t k; uint64_t exp2val; int16_t sint; double dsample = 0.0; @@ -634,10 +634,10 @@ msr_decode_geoscope (char *input, int64_t samplecount, float *output, uint32_t i; } sample32; - if (!input || !output) - return -1; + if (samplecount == 0) + return 0; - if (samplecount <= 0 || outputlength <= 0) + if (!input || !output || outputlength == 0) return -1; /* Make sure we recognize this as a GEOSCOPE encoding format */ @@ -650,7 +650,7 @@ msr_decode_geoscope (char *input, int64_t samplecount, float *output, return -1; } - for (idx = 0; idx < samplecount && outputlength >= (int)sizeof (float); idx++) + for (idx = 0; idx < samplecount && outputlength >= sizeof (float); idx++) { switch (encoding) { @@ -771,21 +771,24 @@ msr_decode_geoscope (char *input, int64_t samplecount, float *output, * * Return number of samples in output buffer on success, -1 on error. ************************************************************************/ -int -msr_decode_cdsn (int16_t *input, int64_t samplecount, int32_t *output, - int64_t outputlength, int swapflag) +int64_t +msr_decode_cdsn (int16_t *input, uint64_t samplecount, int32_t *output, + uint64_t outputlength, int swapflag) { - int32_t idx = 0; + uint32_t idx = 0; int32_t mantissa; /* mantissa */ int32_t gainrange; /* gain range factor */ int32_t mult = -1; /* multiplier for gain range */ uint16_t sint; int32_t sample; - if (samplecount <= 0) + if (samplecount == 0) return 0; - for (idx = 0; idx < samplecount && outputlength >= (int)sizeof (int32_t); idx++) + if (!input || !output || outputlength == 0) + return -1; + + for (idx = 0; idx < samplecount && outputlength >= sizeof (int32_t); idx++) { memcpy (&sint, &input[idx], sizeof (int16_t)); if (swapflag) @@ -857,11 +860,11 @@ msr_decode_cdsn (int16_t *input, int64_t samplecount, int32_t *output, * * Return number of samples in output buffer on success, -1 on error. ************************************************************************/ -int -msr_decode_sro (int16_t *input, int64_t samplecount, int32_t *output, - int64_t outputlength, const char *srcname, int swapflag) +int64_t +msr_decode_sro (int16_t *input, uint64_t samplecount, int32_t *output, + uint64_t outputlength, const char *srcname, int swapflag) { - int32_t idx = 0; + uint32_t idx = 0; int32_t mantissa; /* mantissa */ int32_t gainrange; /* gain range factor */ int32_t add2gr; /* added to gainrage factor */ @@ -871,14 +874,17 @@ msr_decode_sro (int16_t *input, int64_t samplecount, int32_t *output, uint16_t sint; int32_t sample; - if (samplecount <= 0) + if (samplecount == 0) return 0; + if (!input || !output || outputlength == 0) + return -1; + add2gr = 0; mult = -1; add2result = 10; - for (idx = 0; idx < samplecount && outputlength >= (int)sizeof (int32_t); idx++) + for (idx = 0; idx < samplecount && outputlength >= sizeof (int32_t); idx++) { memcpy (&sint, &input[idx], sizeof (int16_t)); if (swapflag) @@ -920,18 +926,21 @@ msr_decode_sro (int16_t *input, int64_t samplecount, int32_t *output, * * Return number of samples in output buffer on success, -1 on error. ************************************************************************/ -int -msr_decode_dwwssn (int16_t *input, int64_t samplecount, int32_t *output, - int64_t outputlength, int swapflag) +int64_t +msr_decode_dwwssn (int16_t *input, uint64_t samplecount, int32_t *output, + uint64_t outputlength, int swapflag) { - int32_t idx = 0; + uint32_t idx = 0; int32_t sample; uint16_t sint; - if (samplecount < 0) + if (samplecount == 0) return 0; - for (idx = 0; idx < samplecount && outputlength >= (int)sizeof (int32_t); idx++) + if (!input || !output || outputlength == 0) + return -1; + + for (idx = 0; idx < samplecount && outputlength >= sizeof (int32_t); idx++) { memcpy (&sint, &input[idx], sizeof (uint16_t)); if (swapflag) diff --git a/libmseed/unpackdata.h b/libmseed/unpackdata.h index 92f72c0..3afa46c 100644 --- a/libmseed/unpackdata.h +++ b/libmseed/unpackdata.h @@ -28,32 +28,29 @@ extern "C" { #include "libmseed.h" -/* Control for printing debugging information, declared in unpackdata.c */ -extern int libmseed_decodedebug; - -extern int msr_decode_int16 (int16_t *input, int64_t samplecount, int32_t *output, - int64_t outputlength, int swapflag); -extern int msr_decode_int32 (int32_t *input, int64_t samplecount, int32_t *output, - int64_t outputlength, int swapflag); -extern int msr_decode_float32 (float *input, int64_t samplecount, float *output, - int64_t outputlength, int swapflag); -extern int msr_decode_float64 (double *input, int64_t samplecount, double *output, - int64_t outputlength, int swapflag); -extern int msr_decode_steim1 (int32_t *input, int inputlength, int64_t samplecount, - int32_t *output, int64_t outputlength, const char *srcname, - int swapflag); -extern int msr_decode_steim2 (int32_t *input, int inputlength, int64_t samplecount, - int32_t *output, int64_t outputlength, const char *srcname, - int swapflag); -extern int msr_decode_geoscope (char *input, int64_t samplecount, float *output, - int64_t outputlength, int encoding, const char *srcname, - int swapflag); -extern int msr_decode_cdsn (int16_t *input, int64_t samplecount, int32_t *output, - int64_t outputlength, int swapflag); -extern int msr_decode_sro (int16_t *input, int64_t samplecount, int32_t *output, - int64_t outputlength, const char *srcname, int swapflag); -extern int msr_decode_dwwssn (int16_t *input, int64_t samplecount, int32_t *output, - int64_t outputlength, int swapflag); +extern int64_t msr_decode_int16 (int16_t *input, uint64_t samplecount, int32_t *output, + uint64_t outputlength, int swapflag); +extern int64_t msr_decode_int32 (int32_t *input, uint64_t samplecount, int32_t *output, + uint64_t outputlength, int swapflag); +extern int64_t msr_decode_float32 (float *input, uint64_t samplecount, float *output, + uint64_t outputlength, int swapflag); +extern int64_t msr_decode_float64 (double *input, uint64_t samplecount, double *output, + uint64_t outputlength, int swapflag); +extern int64_t msr_decode_steim1 (int32_t *input, uint64_t inputlength, uint64_t samplecount, + int32_t *output, uint64_t outputlength, const char *srcname, + int swapflag); +extern int64_t msr_decode_steim2 (int32_t *input, uint64_t inputlength, uint64_t samplecount, + int32_t *output, uint64_t outputlength, const char *srcname, + int swapflag); +extern int64_t msr_decode_geoscope (char *input, uint64_t samplecount, float *output, + uint64_t outputlength, int encoding, const char *srcname, + int swapflag); +extern int64_t msr_decode_cdsn (int16_t *input, uint64_t samplecount, int32_t *output, + uint64_t outputlength, int swapflag); +extern int64_t msr_decode_sro (int16_t *input, uint64_t samplecount, int32_t *output, + uint64_t outputlength, const char *srcname, int swapflag); +extern int64_t msr_decode_dwwssn (int16_t *input, uint64_t samplecount, int32_t *output, + uint64_t outputlength, int swapflag); #ifdef __cplusplus } diff --git a/src/msi.c b/src/msi.c index b3714c4..605b3f0 100644 --- a/src/msi.c +++ b/src/msi.c @@ -28,7 +28,7 @@ static int addlistfile (char *filename); static int my_globmatch (const char *string, const char *pattern); static void usage (void); -#define VERSION "4.1" +#define VERSION "4.2" #define PACKAGE "msi" static int8_t verbose = 0;