From ffafae86b17a99703639a7702921f129d4015621 Mon Sep 17 00:00:00 2001 From: Andrew Bettison Date: Mon, 7 Oct 2013 05:54:46 +1030 Subject: [PATCH] Issue #11: replace long long with int64_t --- commandline.c | 10 ++++----- conf.c | 4 ++-- config_test.c | 4 ++-- fakeradio.c | 12 +++++------ mdp_client.c | 2 +- meshms.c | 6 +++--- os.h | 3 ++- overlay_interface.c | 6 +++--- performance_timing.c | 6 +++--- rhizome_bundle.c | 40 ++++++++++++++--------------------- rhizome_database.c | 28 ++++++++++++------------- rhizome_direct.c | 8 +++---- rhizome_direct_http.c | 17 +++++++-------- rhizome_fetch.c | 26 +++++++++++------------ rhizome_packetformats.c | 4 ++-- rhizome_store.c | 2 +- slip.c | 2 +- str.c | 46 ++++++++++++++++++++++++++++++++++------- str.h | 13 ++++++++++++ 19 files changed, 135 insertions(+), 104 deletions(-) diff --git a/commandline.c b/commandline.c index 5767f6e9..5654d91a 100644 --- a/commandline.c +++ b/commandline.c @@ -826,7 +826,7 @@ int app_server_start(const struct cli_parsed *parsed, struct cli_context *contex if (post_sleep) { time_ms_t milliseconds = atof(post_sleep) * 1000; if (milliseconds > 0) { - INFOF("Sleeping for %lld milliseconds", (long long) milliseconds); + INFOF("Sleeping for %"PRId64" milliseconds", (int64_t) milliseconds); sleep_ms(milliseconds); } } @@ -1015,10 +1015,10 @@ int app_mdp_ping(const struct cli_parsed *parsed, struct cli_context *context) time_ms_t txtime = read_uint64(&mdp.in.payload[4]); int hop_count = 64 - mdp.in.ttl; time_ms_t delay = gettime_ms() - txtime; - cli_printf(context, "%s: seq=%d time=%lldms hops=%d %s%s", + cli_printf(context, "%s: seq=%d time=%"PRId64"ms hops=%d %s%s", alloca_tohex_sid(mdp.in.src.sid), (*rxseq)-firstSeq+1, - (long long)delay, + (int64_t)delay, hop_count, mdp.packetTypeAndFlags&MDP_NOCRYPT?"":" ENCRYPTED", mdp.packetTypeAndFlags&MDP_NOSIGN?"":" SIGNED"); @@ -2088,8 +2088,8 @@ int app_crypt_test(const struct cli_parsed *parsed, struct cli_context *context) } time_ms_t end = gettime_ms(); double each=(end - start) * 1.0 / i; - cli_printf(context, "%d bytes - %d tests took %lldms - mean time = %.2fms\n", - len, i, (long long) end - start, each); + cli_printf(context, "%d bytes - %d tests took %"PRId64"ms - mean time = %.2fms\n", + len, i, (int64_t)(end - start), each); /* Auto-reduce number of repeats so that it doesn't take too long on the phone */ if (each>1.00) count/=2; } diff --git a/conf.c b/conf.c index f4d5726c..a6667ecd 100644 --- a/conf.c +++ b/conf.c @@ -100,9 +100,9 @@ static int reload(const char *path, int *resultp) } if (fread(buf, meta.size, 1, f) != 1) { if (ferror(f)) - WHYF_perror("fread(%s, %llu)", path, (unsigned long long) meta.size); + WHYF_perror("fread(%s, %"PRIu64")", path, (uint64_t) meta.size); else - WHYF("fread(%s, %llu) hit EOF", path, (unsigned long long) meta.size); + WHYF("fread(%s, %"PRIu64") hit EOF", path, (uint64_t) meta.size); free(buf); fclose(f); return -1; diff --git a/config_test.c b/config_test.c index c00c38ed..362c8f53 100644 --- a/config_test.c +++ b/config_test.c @@ -7,7 +7,7 @@ #include "str.h" #include "log.h" -#include "config.h" +#include "conf.h" int main(int argc, char **argv) { @@ -45,7 +45,7 @@ int main(int argc, char **argv) DEBUGF("config.log.show_pid = %d", config.log.show_pid); DEBUGF("config.log.show_time = %d", config.log.show_time); DEBUGF("config.server.chdir = %s", alloca_str_toprint(config.server.chdir)); - DEBUGF("config.debug = %llx", (unsigned long long) config.debug); + DEBUGF("config.debug = %"PRIx64, (uint64_t) config.debug); DEBUGF("config.directory.service = %s", alloca_tohex(config.directory.service.binary, SID_SIZE)); DEBUGF("config.rhizome.api.addfile.allow_host = %s", inet_ntoa(config.rhizome.api.addfile.allow_host)); int j; diff --git a/fakeradio.c b/fakeradio.c index 5a441d15..b310fbe3 100644 --- a/fakeradio.c +++ b/fakeradio.c @@ -28,8 +28,8 @@ struct radio_state { int wait_count; unsigned char rxbuffer[512]; int rxb_len; - long long last_char_ms; - long long next_rssi_time_ms; + int64_t last_char_ms; + int64_t next_rssi_time_ms; int rssi_output; unsigned char seqnum; }; @@ -40,7 +40,7 @@ struct radio_state { #define STATE_PLUSPLUSPLUS 3 #define STATE_COMMAND 4 -long long gettime_ms() +int64_t gettime_ms() { struct timeval nowtv; // If gettimeofday() fails or returns an invalid value, all else is lost! @@ -203,7 +203,7 @@ int write_bytes(struct radio_state *s) } int transmitter=0; -long long next_transmit_time=0; +int64_t next_transmit_time=0; #define MAVLINK10_STX 254 #define RADIO_SOURCE_SYSTEM '3' @@ -394,8 +394,8 @@ int main(int argc,char **argv) while(1) { // what events do we need to poll for? how long can we block? - long long now = gettime_ms(); - long long next_event = now+10000; + int64_t now = gettime_ms(); + int64_t next_event = now+10000; for (i=0;i<2;i++){ // always watch for incoming data, though we will throw it away if we run out of buffer space diff --git a/mdp_client.c b/mdp_client.c index c4340d1e..3767680a 100644 --- a/mdp_client.c +++ b/mdp_client.c @@ -187,7 +187,7 @@ int overlay_mdp_recv(int mdp_sockfd, overlay_mdp_frame *mdp, int port, int *ttl) int expected_len = overlay_mdp_relevant_bytes(mdp); if (len < expected_len) - return WHYF("Expected packet length of %d, received only %lld bytes", expected_len, (long long) len); + return WHYF("Expected packet length of %d, received only %zd bytes", expected_len, len); /* Valid packet received */ return 0; diff --git a/meshms.c b/meshms.c index 17afd0d7..266f0d70 100644 --- a/meshms.c +++ b/meshms.c @@ -135,9 +135,9 @@ static int get_database_conversations(const sid_t *my_sid, const sid_t *their_si } while (sqlite_step_retry(&retry, statement) == SQLITE_ROW) { const char *id_hex = (const char *)sqlite3_column_text(statement, 0); - long long version = sqlite3_column_int64(statement, 1); - long long size = sqlite3_column_int64(statement, 2); - long long tail = sqlite3_column_int64(statement, 3); + int64_t version = sqlite3_column_int64(statement, 1); + int64_t size = sqlite3_column_int64(statement, 2); + int64_t tail = sqlite3_column_int64(statement, 3); const char *sender = (const char *)sqlite3_column_text(statement, 4); const char *recipient = (const char *)sqlite3_column_text(statement, 5); if (config.debug.meshms) diff --git a/os.h b/os.h index 897e25c5..e5da065b 100644 --- a/os.h +++ b/os.h @@ -22,6 +22,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include #include +#include #include #include @@ -52,7 +53,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * then = now; * } */ -typedef long long time_ms_t; +typedef int64_t time_ms_t; time_ms_t gettime_ms(); time_ms_t sleep_ms(time_ms_t milliseconds); diff --git a/overlay_interface.c b/overlay_interface.c index ef356b52..47aa1a6e 100644 --- a/overlay_interface.c +++ b/overlay_interface.c @@ -639,7 +639,7 @@ static void interface_read_file(struct overlay_interface *interface) struct file_packet packet; /* Read from interface file */ - long long length=lseek(interface->alarm.poll.fd,0,SEEK_END); + off_t length = lseek(interface->alarm.poll.fd, (off_t)0, SEEK_END); int new_packets = (length - interface->recv_offset) / sizeof packet; if (new_packets > 20) @@ -653,7 +653,7 @@ static void interface_read_file(struct overlay_interface *interface) } if (config.debug.overlayinterfaces) - DEBUGF("Read interface %s (size=%lld) at offset=%d",interface->name, length, interface->recv_offset); + DEBUGF("Read interface %s (size=%"PRId64") at offset=%d",interface->name, (int64_t)length, interface->recv_offset); ssize_t nread = read(interface->alarm.poll.fd, &packet, sizeof packet); if (nread == -1){ @@ -958,7 +958,7 @@ int overlay_broadcast_ensemble(struct network_destination *destination, struct o return WHY_perror("lseek"); DEBUGF("Write to interface %s at unknown offset", interface->name); } else - DEBUGF("Write to interface %s at offset=%llu", interface->name, (long long)fsize); + DEBUGF("Write to interface %s at offset=%"PRId64, interface->name, (int64_t)fsize); } ssize_t nwrite = write(interface->alarm.poll.fd, &packet, sizeof(packet)); if (nwrite == -1) diff --git a/performance_timing.c b/performance_timing.c index 86c4c6f1..7c989b18 100644 --- a/performance_timing.c +++ b/performance_timing.c @@ -40,11 +40,11 @@ int fd_tallystats(struct profile_total *total,struct profile_total *a) int fd_showstat(struct profile_total *total, struct profile_total *a) { - INFOF("%lldms (%2.1f%%) in %d calls (max %lldms, avg %.1fms, +child avg %.1fms) : %s", - (long long) a->total_time, + INFOF("%"PRId64"ms (%2.1f%%) in %d calls (max %"PRId64"ms, avg %.1fms, +child avg %.1fms) : %s", + (int64_t) a->total_time, a->total_time*100.0/total->total_time, a->calls, - (long long) a->max_time, + (int64_t) a->max_time, a->total_time*1.00/a->calls, (a->total_time+a->child_time)*1.00/a->calls, a->name); diff --git a/rhizome_bundle.c b/rhizome_bundle.c index b8017175..c002c422 100644 --- a/rhizome_bundle.c +++ b/rhizome_bundle.c @@ -180,9 +180,8 @@ int rhizome_manifest_parse(rhizome_manifest *m) } } else if (strcasecmp(var, "filesize") == 0) { have_filesize = 1; - char *ep = value; - long long filesize = strtoll(value, &ep, 10); - if (ep == value || *ep || filesize < 0) { + uint64_t filesize; + if (!str_to_uint64(value, 10, &filesize, NULL)) { if (config.debug.rejecteddata) WARNF("Invalid filesize: %s", value); m->errors++; @@ -191,24 +190,20 @@ int rhizome_manifest_parse(rhizome_manifest *m) } } else if (strcasecmp(var, "version") == 0) { have_version = 1; - char *ep = value; - long long version = strtoll(value, &ep, 10); - if (ep == value || *ep || version < 0) { + uint64_t version; + if (!str_to_uint64(value, 10, &version, NULL)) { if (config.debug.rejecteddata) WARNF("Invalid version: %s", value); m->errors++; } else { m->version = version; } - // since rhizome *MUST* be able to carry future manifest versions // if any of these fields are not well formed, the manifest can still be imported and exported // but the bundle should not be added or exported - } else if (strcasecmp(var, "tail") == 0) { - char *ep = value; - long long tail = strtoll(value, &ep, 10); - if (ep == value || *ep || tail < 0) { + uint64_t tail; + if (!str_to_uint64(value, 10, &tail, NULL)) { if (config.debug.rejecteddata) WARNF("Invalid tail: %s", value); m->warnings++; @@ -236,9 +231,8 @@ int rhizome_manifest_parse(rhizome_manifest *m) } } else if (strcasecmp(var, "date") == 0) { have_date = 1; - char *ep = value; - long long date = strtoll(value, &ep, 10); - if (ep == value || *ep || date < 0) { + int64_t date; + if (!str_to_int64(value, 10, &date, NULL)) { if (config.debug.rejecteddata) WARNF("Invalid date: %s", value); m->warnings++; @@ -419,10 +413,8 @@ int64_t rhizome_manifest_get_ll(rhizome_manifest *m, const char *var) int i; for (i = 0; i < m->var_count; ++i) if (!strcmp(m->vars[i], var)) { - char *vp = m->values[i]; - char *ep = vp; - long long val = strtoll(vp, &ep, 10); - return (ep != vp && *ep == '\0') ? val : -1; + int64_t val; + return str_to_int64(m->values[i], 10, &val, NULL) ? val : -1; } return -1; } @@ -484,11 +476,9 @@ int rhizome_manifest_set(rhizome_manifest *m, const char *var, const char *value int rhizome_manifest_set_ll(rhizome_manifest *m, char *var, int64_t value) { - char svalue[100]; - - snprintf(svalue,100, "%" PRId64, value); - - return rhizome_manifest_set(m,var,svalue); + char str[50]; + snprintf(str, sizeof str, "%" PRId64, value); + return rhizome_manifest_set(m, var, str); } rhizome_manifest manifests[MAX_RHIZOME_MANIFESTS]; @@ -743,7 +733,7 @@ int rhizome_fill_manifest(rhizome_manifest *m, const char *filepath, const sid_t } if (rhizome_manifest_get(m, "date", NULL, 0) == NULL) { - rhizome_manifest_set_ll(m, "date", (long long) gettime_ms()); + rhizome_manifest_set_ll(m, "date", (int64_t) gettime_ms()); if (config.debug.rhizome) DEBUGF("missing 'date', set default date=%s", rhizome_manifest_get(m, "date", NULL, 0)); } @@ -816,7 +806,7 @@ int rhizome_fill_manifest(rhizome_manifest *m, const char *filepath, const sid_t if (config.debug.rhizome) DEBUGF("Implicitly adding payload encryption due to presense of sender & recipient fields"); m->payloadEncryption=1; - rhizome_manifest_set_ll(m,"crypt",1); + rhizome_manifest_set_ll(m,"crypt",1LL); } } } diff --git a/rhizome_database.c b/rhizome_database.c index 8720ebad..6a554c41 100644 --- a/rhizome_database.c +++ b/rhizome_database.c @@ -229,9 +229,8 @@ int rhizome_opendb() int loglevel = (config.debug.rhizome) ? LOG_LEVEL_DEBUG : LOG_LEVEL_SILENT; /* Read Rhizome configuration */ - if (config.debug.rhizome) { - DEBUGF("Rhizome will use %lluB of storage for its database.", (unsigned long long) config.rhizome.database_size); - } + if (config.debug.rhizome) + DEBUGF("Rhizome will use %"PRIu64"B of storage for its database.", (uint64_t) config.rhizome.database_size); sqlite_retry_state retry = SQLITE_RETRY_STATE_DEFAULT; @@ -831,7 +830,7 @@ static int _sqlite_vexec_int64(struct __sourceloc __whence, sqlite_retry_state * if (!sqlite_code_ok(stepcode) || ret == -1) return -1; if (sqlite_trace_func()) - DEBUGF("rowcount=%d changes=%d result=%lld", rowcount, sqlite3_changes(rhizome_db), (long long)*result); + DEBUGF("rowcount=%d changes=%d result=%"PRId64, rowcount, sqlite3_changes(rhizome_db), *result); return rowcount; } @@ -1045,13 +1044,13 @@ int rhizome_cleanup(struct rhizome_cleanup_report *report) OUT(); } -int rhizome_make_space(int group_priority, long long bytes) +int rhizome_make_space(int group_priority, uint64_t bytes) { /* Asked for impossibly large amount */ if (bytes>=(config.rhizome.database_size-65536)) - return WHYF("bytes=%lld is too large", bytes); + return WHYF("bytes=%"PRIu64" is too large", bytes); - long long db_used = rhizome_database_used_bytes(); + int64_t db_used = rhizome_database_used_bytes(); if (db_used == -1) return -1; @@ -1094,7 +1093,8 @@ int rhizome_make_space(int group_priority, long long bytes) } sqlite3_finalize(statement); - //long long equal_priority_larger_file_space_used = sqlite_exec_int64("SELECT COUNT(length) FROM FILES WHERE highestpriority=%d and length>%lld",group_priority,bytes); + //int64_t equal_priority_larger_file_space_used = sqlite_exec_int64("SELECT COUNT(length) FROM + //FILES WHERE highestpriority = ? and length > ?", INT, group_priority, INT64, bytes, END); /* XXX Get rid of any equal priority files that are larger than this one */ /* XXX Get rid of any higher priority files that are not relevant in this time or location */ @@ -1239,7 +1239,7 @@ int rhizome_store_bundle(rhizome_manifest *m) if (!( sqlite_code_ok(sqlite3_bind_text(stmt, 1, manifestid, -1, SQLITE_STATIC)) && sqlite_code_ok(sqlite3_bind_blob(stmt, 2, m->manifestdata, m->manifest_bytes, SQLITE_STATIC)) && sqlite_code_ok(sqlite3_bind_int64(stmt, 3, m->version)) - && sqlite_code_ok(sqlite3_bind_int64(stmt, 4, (long long) gettime_ms())) + && sqlite_code_ok(sqlite3_bind_int64(stmt, 4, (int64_t) gettime_ms())) && sqlite_code_ok(sqlite3_bind_blob(stmt, 5, bar, RHIZOME_BAR_BYTES, SQLITE_STATIC)) && sqlite_code_ok(sqlite3_bind_int64(stmt, 6, m->fileLength)) && sqlite_code_ok(sqlite3_bind_text(stmt, 7, filehash, -1, SQLITE_STATIC)) @@ -1413,15 +1413,15 @@ int rhizome_list_manifests(struct cli_context *context, const char *service, con const char *q_manifestid = (const char *) sqlite3_column_text(statement, 0); const char *manifestblob = (char *) sqlite3_column_blob(statement, 1); size_t manifestblobsize = sqlite3_column_bytes(statement, 1); // must call after sqlite3_column_blob() - long long q_version = sqlite3_column_int64(statement, 2); - long long q_inserttime = sqlite3_column_int64(statement, 3); + int64_t q_version = sqlite3_column_int64(statement, 2); + int64_t q_inserttime = sqlite3_column_int64(statement, 3); const char *q_author = (const char *) sqlite3_column_text(statement, 4); - long long rowid = sqlite3_column_int64(statement, 5); + int64_t rowid = sqlite3_column_int64(statement, 5); if (rhizome_read_manifest_file(m, manifestblob, manifestblobsize) == -1) { WARNF("MANIFESTS row id=%s has invalid manifest blob -- skipped", q_manifestid); } else { - long long blob_version = rhizome_manifest_get_ll(m, "version"); + int64_t blob_version = rhizome_manifest_get_ll(m, "version"); if (blob_version != q_version) WARNF("MANIFESTS row id=%s version=%lld does not match manifest blob.version=%lld", q_manifestid, q_version, blob_version); int match = 1; @@ -1442,7 +1442,7 @@ int rhizome_list_manifests(struct cli_context *context, const char *service, con if (match) { const char *blob_name = rhizome_manifest_get(m, "name", NULL, 0); - long long blob_date = rhizome_manifest_get_ll(m, "date"); + int64_t blob_date = rhizome_manifest_get_ll(m, "date"); const char *blob_filehash = rhizome_manifest_get(m, "filehash", NULL, 0); int from_here = 0; unsigned char senderSid[SID_SIZE]; diff --git a/rhizome_direct.c b/rhizome_direct.c index d816fc00..518b02d0 100644 --- a/rhizome_direct.c +++ b/rhizome_direct.c @@ -351,10 +351,8 @@ rhizome_direct_bundle_cursor *rhizome_direct_get_fill_response rhizome_bar_bidprefix_ll(&usbuffer[10+us*RHIZOME_BAR_BYTES])); } else { /* We each have a version of this bundle, so see whose is newer */ - long long them_version - =rhizome_bar_version(&buffer[10+them*RHIZOME_BAR_BYTES]); - long long us_version - =rhizome_bar_version(&usbuffer[10+us*RHIZOME_BAR_BYTES]); + int64_t them_version = rhizome_bar_version(&buffer[10+them*RHIZOME_BAR_BYTES]); + int64_t us_version = rhizome_bar_version(&usbuffer[10+us*RHIZOME_BAR_BYTES]); if (them_version>us_version) { /* They have the newer version of the bundle */ c->buffer[c->buffer_offset_bytes+c->buffer_used]=0x01; /* Please send */ @@ -584,7 +582,7 @@ int rhizome_direct_bundle_iterator_pickle_range(rhizome_direct_bundle_cursor *r, ranges, which will happen with every rhizome direct sync. */ - long long v; + int64_t v; int ltwov=0; v=r->start_size_high; diff --git a/rhizome_direct_http.c b/rhizome_direct_http.c index 99de479f..bdf21988 100644 --- a/rhizome_direct_http.c +++ b/rhizome_direct_http.c @@ -113,7 +113,7 @@ int rhizome_direct_form_received(rhizome_http_request *r) } unsigned char *addr = mmap(NULL, stat.st_size, PROT_READ, MAP_SHARED, fd, 0); if (addr==MAP_FAILED) { - WHYF_perror("mmap(NULL, %lld, PROT_READ, MAP_SHARED, %d, 0)", (long long) stat.st_size, fd); + WHYF_perror("mmap(NULL, %"PRId64", PROT_READ, MAP_SHARED, %d, 0)", (int64_t) stat.st_size, fd); /* Clean up after ourselves */ close(fd); rhizome_direct_clear_temporary_files(r); @@ -614,7 +614,7 @@ int rhizome_direct_parse_http_request(rhizome_http_request *r) if (!ct_str) return rhizome_server_simple_http_response(r,400,"

Missing or unsupported Content-Type header

\r\n"); /* ok, we have content-type and content-length, now make sure they are well formed. */ - long long content_length; + int64_t content_length; if (sscanf(cl_str,"Content-Length: %lld",&content_length)!=1) return rhizome_server_simple_http_response(r,400,"

Malformed Content-Length header

\r\n"); char boundary_string[1024]; @@ -626,7 +626,7 @@ int rhizome_direct_parse_http_request(rhizome_http_request *r) if (i<4||i>128) return rhizome_server_simple_http_response(r,400,"

Malformed Content-Type header

\r\n"); - DEBUGF("content_length=%lld, boundary_string=%s contentlen=%d", (long long) content_length, alloca_str_toprint(boundary_string), contentlen); + DEBUGF("content_length=%"PRId64", boundary_string=%s contentlen=%d", (int64_t) content_length, alloca_str_toprint(boundary_string), contentlen); /* Now start receiving and parsing multi-part data. If we already received some of the post-header data, process that first. Tell the HTTP request that it has moved to multipart @@ -894,10 +894,10 @@ void rhizome_direct_http_dispatch(rhizome_direct_sync_request *r) DEBUGF("bundle id = %s", alloca_tohex_rhizome_bid_t(m->cryptoSignPublic)); const char *hash = rhizome_manifest_get(m, "filehash", NULL, 0); DEBUGF("bundle file hash = '%s'",hash); - long long filesize = rhizome_manifest_get_ll(m, "filesize"); - DEBUGF("file size = %lld",filesize); - long long version = rhizome_manifest_get_ll(m, "version"); - DEBUGF("version = %lld",version); + int64_t filesize = rhizome_manifest_get_ll(m, "filesize"); + DEBUGF("file size = %"PRId64,filesize); + int64_t version = rhizome_manifest_get_ll(m, "version"); + DEBUGF("version = %"PRId64,version); /* We now have everything we need to compose the POST request and send it. */ @@ -1039,8 +1039,7 @@ void rhizome_direct_http_dispatch(rhizome_direct_sync_request *r) DEBUGF("Couldn't unpickle range. This should never happen. Assuming near and far cursor ranges match."); } else { - DEBUGF("unpickled size_high=%lld, limit_size_high=%lld", - c->size_high,c->limit_size_high); + DEBUGF("unpickled size_high=%"PRId64", limit_size_high=%"PRId64, c->size_high, c->limit_size_high); DEBUGF("c->buffer_size=%d",c->buffer_size); r->cursor->size_low=c->limit_size_high; /* Set tail of BID to all high, as we assume the far end has returned all diff --git a/rhizome_fetch.c b/rhizome_fetch.c index 3a056fb1..78ad6665 100644 --- a/rhizome_fetch.c +++ b/rhizome_fetch.c @@ -202,7 +202,7 @@ int rhizome_fetch_status_html(struct strbuf *b) strbuf_puts(b, "inactive"); } int candidates=0; - long long candidate_size=0; + uint64_t candidate_size = 0; for (j=0; j< q->candidate_queue_size;j++){ if (q->candidate_queue[j].manifest){ candidates++; @@ -210,7 +210,7 @@ int rhizome_fetch_status_html(struct strbuf *b) } } if (candidates) - strbuf_sprintf(b, ", %d candidates [%lld bytes]", candidates, candidate_size); + strbuf_sprintf(b, ", %d candidates [%"PRIu64" bytes]", candidates, candidate_size); } return 0; } @@ -224,7 +224,7 @@ static struct profile_total fetch_stats = { .name="rhizome_fetch_poll" }; * * @author Andrew Bettison */ -static struct rhizome_fetch_queue *rhizome_find_queue(long long size) +static struct rhizome_fetch_queue *rhizome_find_queue(uint64_t size) { int i; unsigned char log_size = log2ll(size); @@ -242,7 +242,7 @@ static struct rhizome_fetch_queue *rhizome_find_queue(long long size) * * @author Andrew Bettison */ -static struct rhizome_fetch_slot *rhizome_find_fetch_slot(long long size) +static struct rhizome_fetch_slot *rhizome_find_fetch_slot(uint64_t size) { int i; unsigned char log_size = log2ll(size); @@ -459,8 +459,8 @@ static int rhizome_import_received_bundle(struct rhizome_manifest *m) m->finalised = 1; m->manifest_bytes = m->manifest_all_bytes; // store the signatures too if (config.debug.rhizome_rx) { - DEBUGF("manifest len=%d has %d signatories. Associated file = %lld bytes", - m->manifest_bytes, m->sig_count,(long long)m->fileLength); + DEBUGF("manifest len=%d has %d signatories. Associated file = %"PRId64" bytes", + m->manifest_bytes, m->sig_count, m->fileLength); dump("manifest", m->manifestdata, m->manifest_all_bytes); } return rhizome_add_manifest(m, m->ttl - 1 /* TTL */); @@ -958,11 +958,11 @@ int rhizome_suggest_queue_manifest_import(rhizome_manifest *m, const struct sock struct rhizome_fetch_candidate *c = &q->candidate_queue[j]; if (!c->manifest) break; - DEBUGF("%d:%d manifest=%p bid=%s priority=%d size=%lld", i, j, + DEBUGF("%d:%d manifest=%p bid=%s priority=%d size=%"PRId64, i, j, c->manifest, alloca_tohex_rhizome_bid_t(c->manifest->cryptoSignPublic), c->priority, - (long long) c->manifest->fileLength + c->manifest->fileLength ); } } @@ -1022,7 +1022,7 @@ static void rhizome_fetch_mdp_slot_callback(struct sched_ent *alarm) IN(); struct rhizome_fetch_slot *slot=(struct rhizome_fetch_slot*)alarm; - long long now=gettime_ms(); + time_ms_t now = gettime_ms(); if (now-slot->last_write_time>slot->mdpIdleTimeout) { DEBUGF("MDP connection timed out: last RX %lldms ago (read %"PRId64" of %"PRId64" bytes)", now-slot->last_write_time, @@ -1306,10 +1306,10 @@ int rhizome_write_complete(struct rhizome_fetch_slot *slot) time_ms_t interval = now - slot->start_time; if (interval <= 0) interval = 1; - DEBUGF("Closing rhizome fetch slot = 0x%p. Received %lld bytes in %lldms (%lldKB/sec).", - slot,(long long)slot->write_state.file_offset, - (long long)interval, - (long long)((slot->write_state.file_offset) / interval)); + DEBUGF("Closing rhizome fetch slot = 0x%p. Received %"PRId64" bytes in %"PRId64"ms (%"PRId64"KB/sec).", + slot, slot->write_state.file_offset, + (int64_t)interval, + (int64_t)((slot->write_state.file_offset) / interval)); } rhizome_fetch_close(slot); diff --git a/rhizome_packetformats.c b/rhizome_packetformats.c index ff669468..37ad1d72 100644 --- a/rhizome_packetformats.c +++ b/rhizome_packetformats.c @@ -338,8 +338,8 @@ int overlay_rhizome_saw_advertisements(int i, struct decode_context *context, st /* trim manifest ID to a prefix for ease of debugging (that is the only use of this */ if (config.debug.rhizome_ads){ - long long version = rhizome_manifest_get_ll(m, "version"); - DEBUGF("manifest id=%s version=%lld", alloca_tohex_rhizome_bid_t(m->cryptoSignPublic), version); + int64_t version = rhizome_manifest_get_ll(m, "version"); + DEBUGF("manifest id=%s version=%"PRId64, alloca_tohex_rhizome_bid_t(m->cryptoSignPublic), version); } /* Crude signature presence test */ diff --git a/rhizome_store.c b/rhizome_store.c index 7d7c0807..1703bcf8 100644 --- a/rhizome_store.c +++ b/rhizome_store.c @@ -603,7 +603,7 @@ int rhizome_import_buffer(rhizome_manifest *m, unsigned char *buffer, int length int rhizome_stat_file(rhizome_manifest *m, const char *filepath) { - long long existing = rhizome_manifest_get_ll(m, "filesize"); + int64_t existing = rhizome_manifest_get_ll(m, "filesize"); m->fileLength = 0; if (filepath[0]) { diff --git a/slip.c b/slip.c index cdd136ff..daa37052 100644 --- a/slip.c +++ b/slip.c @@ -184,7 +184,7 @@ int slip_encode(int format, } -unsigned long long last_rssi_time=0; +time_ms_t last_rssi_time=0; int last_radio_rssi=-999; int last_radio_temperature=-999; int last_radio_rxpackets=0; diff --git a/str.c b/str.c index a6c79dc7..3430c78e 100644 --- a/str.c +++ b/str.c @@ -228,6 +228,40 @@ char *str_str(char *haystack, const char *needle, int haystack_len) return NULL; } +int str_to_int64(const char *str, int base, int64_t *result, const char **afterp) +{ + if (isspace(*str)) + return 0; + const char *end = str; + long long value = strtoll(str, (char**)&end, base); + if (end == str) + return 0; + if (afterp) + *afterp = end; + else if (*end) + return 0; + if (result) + *result = value; + return 1; +} + +int str_to_uint64(const char *str, int base, uint64_t *result, const char **afterp) +{ + if (isspace(*str)) + return 0; + const char *end = str; + unsigned long long value = strtoull(str, (char**)&end, base); + if (end == str) + return 0; + if (afterp) + *afterp = end; + else if (*end) + return 0; + if (result) + *result = value; + return 1; +} + static struct scale_factor { char symbol; uint64_t factor; @@ -260,11 +294,9 @@ uint64_t scale_factor(const char *str, const char **afterp) int str_to_int64_scaled(const char *str, int base, int64_t *result, const char **afterp) { - if (isspace(*str)) - return 0; + int64_t value; const char *end = str; - long long value = strtoll(str, (char**)&end, base); - if (end == str) + if (!str_to_int64(str, base, &value, &end)) return 0; value *= scale_factor(end, &end); if (afterp) @@ -278,11 +310,9 @@ int str_to_int64_scaled(const char *str, int base, int64_t *result, const char * int str_to_uint64_scaled(const char *str, int base, uint64_t *result, const char **afterp) { - if (isspace(*str)) - return 0; + uint64_t value; const char *end = str; - unsigned long long value = strtoull(str, (char**)&end, base); - if (end == str) + if (!str_to_uint64(str, base, &value, &end)) return 0; value *= scale_factor(end, &end); if (afterp) diff --git a/str.h b/str.h index ae50a729..78990237 100644 --- a/str.h +++ b/str.h @@ -196,6 +196,19 @@ int strn_str_casecmp(const char *str1, size_t len1, const char *str2); */ char *str_str(char *haystack, const char *needle, int haystack_len); +/* Parse a string as an integer in ASCII radix notation in the given 'base' (eg, base=10 means + * decimal). + * + * Return 1 if a valid integer was parsed, storing the value in *result (unless result is NULL) and + * storing a pointer to the immediately succeeding character in *afterp (unless afterp is NULL, in + * which case returns 1 only if the immediately succeeding character is a nul '\0'). Returns 0 + * otherwise, leaving *result and *afterp unchanged. + * + * @author Andrew Bettison + */ +int str_to_int64(const char *str, int base, int64_t *result, const char **afterp); +int str_to_uint64(const char *str, int base, uint64_t *result, const char **afterp); + /* Parse a string as an integer in ASCII radix notation in the given 'base' (eg, base=10 means * decimal) and scale the result by a factor given by an optional suffix "scaling" character in the * set {kKmMgG}: 'k' = 1e3, 'K' = 1<<10, 'm' = 1e6, 'M' = 1<<20, 'g' = 1e9, 'G' = * 1<<30.