diff --git a/meshms.c b/meshms.c index 6b5f1d8f..1f51ae26 100644 --- a/meshms.c +++ b/meshms.c @@ -49,7 +49,7 @@ struct ply_read{ // details of the current record uint64_t record_end_offset; uint16_t record_length; - int buffer_size; + size_t buffer_size; char type; // raw record data unsigned char *buffer; @@ -78,8 +78,7 @@ static int get_my_conversation_bundle(const sid_t *my_sidp, rhizome_manifest *m) alloca_tohex(keyring->contexts[cn]->identities[in] ->keypairs[kp]->private_key, crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES)); - int ret = rhizome_get_bundle_from_seed(m, seed); - if (ret<0) + if (rhizome_get_bundle_from_seed(m, seed) == -1) return -1; // always consider the content encrypted, we don't need to rely on the manifest itself. @@ -225,9 +224,9 @@ static int ply_read_open(struct ply_read *ply, const rhizome_bid_t *bid, rhizome if (rhizome_retrieve_manifest(bid, m)) return -1; int ret = rhizome_open_decrypt_read(m, NULL, &ply->read); - if (ret>0) + if (ret == 1) WARNF("Payload was not found for manifest %s, %"PRId64, alloca_tohex_rhizome_bid_t(m->cryptoSignPublic), m->version); - if (ret) + if (ret != 0) return ret; ply->read.offset = ply->read.length = m->fileLength; return 0; @@ -399,11 +398,11 @@ static int update_conversation(const sid_t *my_sid, struct conversations *conv){ goto end; ret = ply_find_next(&ply, MESHMS_BLOCK_TYPE_ACK); - if (ret<0) + if (ret == -1) goto end; if (ret==0){ - if (unpack_uint(ply.buffer, ply.record_length, &previous_ack)<0) + if (unpack_uint(ply.buffer, ply.record_length, &previous_ack) == -1) previous_ack=0; } if (config.debug.meshms) @@ -478,7 +477,7 @@ static int read_known_conversations(rhizome_manifest *m, const sid_t *their_sid, bzero(&buff, sizeof(buff)); int ret = rhizome_open_decrypt_read(m, NULL, &read); - if (ret<0) + if (ret == -1) goto end; unsigned char version=0xFF; @@ -527,8 +526,9 @@ end: return 0; } -static int write_conversation(struct rhizome_write *write, struct conversations *conv){ - int len=0; +static ssize_t write_conversation(struct rhizome_write *write, struct conversations *conv) +{ + size_t len=0; if (!conv) return len; { @@ -541,14 +541,14 @@ static int write_conversation(struct rhizome_write *write, struct conversations len+=pack_uint(&buffer[len], conv->read_offset); len+=pack_uint(&buffer[len], conv->their_size); int ret=rhizome_write_buffer(write, buffer, len); - if (ret<0) + if (ret == -1) return ret; }else{ len+=measure_packed_uint(conv->their_last_message); len+=measure_packed_uint(conv->read_offset); len+=measure_packed_uint(conv->their_size); } - DEBUGF("len %s, %"PRId64", %"PRId64", %"PRId64" = %d", + DEBUGF("len %s, %"PRId64", %"PRId64", %"PRId64" = %zu", alloca_tohex_sid_t(conv->them), conv->their_last_message, conv->read_offset, @@ -556,14 +556,14 @@ static int write_conversation(struct rhizome_write *write, struct conversations len); } // write the two child nodes - int ret=write_conversation(write, conv->_left); - if (ret<0) + ssize_t ret = write_conversation(write, conv->_left); + if (ret == -1) return ret; - len+=ret; - ret=write_conversation(write, conv->_right); - if (ret<0) + len += (size_t) ret; + ret = write_conversation(write, conv->_right); + if (ret == -1) return ret; - len+=ret; + len += (size_t) ret; return len; } @@ -578,22 +578,22 @@ static int write_known_conversations(rhizome_manifest *m, struct conversations * // TODO rebalance tree... // measure the final payload first - int len=write_conversation(NULL, conv); - if (len<0) + ssize_t len=write_conversation(NULL, conv); + if (len == -1) goto end; // then write it m->version++; rhizome_manifest_set_ll(m,"version",m->version); - m->fileLength = len+1; + m->fileLength = (size_t) len + 1; rhizome_manifest_set_ll(m,"filesize",m->fileLength); - if (rhizome_write_open_manifest(&write, m)) + if (rhizome_write_open_manifest(&write, m) == -1) goto end; unsigned char version=1; - if (rhizome_write_buffer(&write, &version, 1)<0) + if (rhizome_write_buffer(&write, &version, 1) == -1) goto end; - if (write_conversation(&write, conv)<0) + if (write_conversation(&write, conv) == -1) goto end; if (rhizome_finish_write(&write)) goto end; @@ -795,7 +795,7 @@ int app_meshms_list_messages(const struct cli_parsed *parsed, struct cli_context // find their last ACK so we know if messages have been received int r = ply_find_next(&read_theirs, MESHMS_BLOCK_TYPE_ACK); if (r==0){ - if (unpack_uint(read_theirs.buffer, read_theirs.record_length, &their_last_ack)<0) + if (unpack_uint(read_theirs.buffer, read_theirs.record_length, &their_last_ack) == -1) their_last_ack=0; else their_ack_offset = read_theirs.record_end_offset; @@ -822,11 +822,11 @@ int app_meshms_list_messages(const struct cli_parsed *parsed, struct cli_context // read their message list, and insert all messages that are included in the ack range if (conv->found_their_ply){ int ofs=unpack_uint(read_ours.buffer, read_ours.record_length, (uint64_t*)&read_theirs.read.offset); - if (ofs<0) + if (ofs == -1) break; uint64_t end_range; int x = unpack_uint(read_ours.buffer+ofs, read_ours.record_length - ofs, &end_range); - if (x<0) + if (x == -1) end_range=0; else end_range = read_theirs.read.offset - end_range; diff --git a/rhizome.h b/rhizome.h index 54201f44..4188327d 100644 --- a/rhizome.h +++ b/rhizome.h @@ -472,8 +472,8 @@ struct rhizome_write_buffer { struct rhizome_write_buffer *_next; int64_t offset; - int buffer_size; - int data_size; + size_t buffer_size; + size_t data_size; unsigned char data[0]; }; @@ -488,7 +488,7 @@ struct rhizome_write int64_t written_offset; int64_t file_length; struct rhizome_write_buffer *buffer_list; - int buffer_size; + size_t buffer_size; int crypt; unsigned char key[RHIZOME_CRYPT_KEY_BYTES]; @@ -503,7 +503,7 @@ struct rhizome_write struct rhizome_read_buffer{ uint64_t offset; unsigned char data[RHIZOME_CRYPT_PAGE_SIZE]; - int len; + size_t len; }; struct rhizome_read @@ -600,12 +600,12 @@ typedef struct rhizome_direct_bundle_cursor { rhizome_bid_t bid_low; rhizome_bid_t bid_high; unsigned char *buffer; - int buffer_size; - int buffer_used; - int buffer_offset_bytes; + size_t buffer_size; + size_t buffer_used; + size_t buffer_offset_bytes; } rhizome_direct_bundle_cursor; -rhizome_direct_bundle_cursor *rhizome_direct_bundle_iterator(int buffer_size); +rhizome_direct_bundle_cursor *rhizome_direct_bundle_iterator(size_t buffer_size); void rhizome_direct_bundle_iterator_unlimit(rhizome_direct_bundle_cursor *r); int rhizome_direct_bundle_iterator_pickle_range(rhizome_direct_bundle_cursor *r, unsigned char *pickled, @@ -669,7 +669,7 @@ rhizome_direct_sync_request *rhizome_direct_new_sync_request( void (*transport_specific_dispatch_function) (struct rhizome_direct_sync_request *), - int buffer_size,int interval, int mode, + size_t buffer_size, int interval, int mode, void *transport_specific_state); int rhizome_direct_continue_sync_request(rhizome_direct_sync_request *r); int rhizome_direct_conclude_sync_request(rhizome_direct_sync_request *r); @@ -720,14 +720,14 @@ int unpack_http_response(char *response, struct http_response_parts *parts); int rhizome_exists(const rhizome_filehash_t *hashp); int rhizome_open_write(struct rhizome_write *write, const rhizome_filehash_t *expectedHashp, int64_t file_length, int priority); -int rhizome_write_buffer(struct rhizome_write *write_state, unsigned char *buffer, int data_size); -int rhizome_random_write(struct rhizome_write *write_state, int64_t offset, unsigned char *buffer, int data_size); +int rhizome_write_buffer(struct rhizome_write *write_state, unsigned char *buffer, size_t data_size); +int rhizome_random_write(struct rhizome_write *write_state, int64_t offset, unsigned char *buffer, size_t data_size); int rhizome_write_open_manifest(struct rhizome_write *write, rhizome_manifest *m); int rhizome_write_file(struct rhizome_write *write, const char *filename); int rhizome_fail_write(struct rhizome_write *write); int rhizome_finish_write(struct rhizome_write *write); int rhizome_import_file(rhizome_manifest *m, const char *filepath); -int rhizome_import_buffer(rhizome_manifest *m, unsigned char *buffer, int length); +int rhizome_import_buffer(rhizome_manifest *m, unsigned char *buffer, size_t length); int rhizome_stat_file(rhizome_manifest *m, const char *filepath); int rhizome_add_file(rhizome_manifest *m, const char *filepath); int rhizome_derive_key(rhizome_manifest *m, rhizome_bk_t *bsk); @@ -737,17 +737,17 @@ int rhizome_append_journal_buffer(rhizome_manifest *m, rhizome_bk_t *bsk, uint64 int rhizome_append_journal_file(rhizome_manifest *m, rhizome_bk_t *bsk, uint64_t advance_by, const char *filename); int rhizome_journal_pipe(struct rhizome_write *write, const rhizome_filehash_t *hashp, uint64_t start_offset, uint64_t length); -int rhizome_crypt_xor_block(unsigned char *buffer, int buffer_size, int64_t stream_offset, +int rhizome_crypt_xor_block(unsigned char *buffer, size_t buffer_size, int64_t stream_offset, const unsigned char *key, const unsigned char *nonce); int rhizome_open_read(struct rhizome_read *read, const rhizome_filehash_t *hashp); -int rhizome_read(struct rhizome_read *read, unsigned char *buffer, int buffer_length); -int rhizome_read_buffered(struct rhizome_read *read, struct rhizome_read_buffer *buffer, unsigned char *data, int len); +int rhizome_read(struct rhizome_read *read, unsigned char *buffer, size_t buffer_length); +int rhizome_read_buffered(struct rhizome_read *read, struct rhizome_read_buffer *buffer, unsigned char *data, size_t len); int rhizome_read_close(struct rhizome_read *read); int rhizome_open_decrypt_read(rhizome_manifest *m, rhizome_bk_t *bsk, struct rhizome_read *read_state); int rhizome_extract_file(rhizome_manifest *m, const char *filepath, rhizome_bk_t *bsk); int rhizome_dump_file(const rhizome_filehash_t *hashp, const char *filepath, int64_t *length); int rhizome_read_cached(const rhizome_bid_t *bid, uint64_t version, time_ms_t timeout, - uint64_t fileOffset, unsigned char *buffer, int length); + uint64_t fileOffset, unsigned char *buffer, size_t length); int rhizome_cache_close(); int rhizome_database_filehash_from_id(const rhizome_bid_t *bidp, uint64_t version, rhizome_filehash_t *hashp); diff --git a/rhizome_crypto.c b/rhizome_crypto.c index 7d9dd7e0..3cbb2a32 100644 --- a/rhizome_crypto.c +++ b/rhizome_crypto.c @@ -72,13 +72,13 @@ int rhizome_get_bundle_from_seed(rhizome_manifest *m, const char *seed) return -1; int ret=rhizome_retrieve_manifest(&key.Public, m); - if (ret<0) + if (ret == -1) return -1; m->haveSecret=(ret==0)?EXISTING_BUNDLE_ID:NEW_BUNDLE_ID; m->cryptoSignPublic = key.Public; bcopy(key.Private, m->cryptoSignSecret, sizeof m->cryptoSignSecret); - if (ret>0) + if (ret == 1) rhizome_manifest_set(m, "id", alloca_tohex_rhizome_bid_t(m->cryptoSignPublic)); return ret; } @@ -590,22 +590,22 @@ static void add_nonce(unsigned char *nonce, int64_t value){ /* crypt a block of a stream, allowing for offsets that don't align perfectly to block boundaries * for efficiency the caller should use a buffer size of (n*RHIZOME_CRYPT_PAGE_SIZE) */ -int rhizome_crypt_xor_block(unsigned char *buffer, int buffer_size, int64_t stream_offset, +int rhizome_crypt_xor_block(unsigned char *buffer, size_t buffer_size, int64_t stream_offset, const unsigned char *key, const unsigned char *nonce){ if (stream_offset<0) return WHY("Invalid stream offset"); int64_t nonce_offset = stream_offset & ~(RHIZOME_CRYPT_PAGE_SIZE -1); - int offset=0; + size_t offset=0; unsigned char block_nonce[crypto_stream_xsalsa20_NONCEBYTES]; bcopy(nonce, block_nonce, sizeof(block_nonce)); add_nonce(block_nonce, nonce_offset); if (nonce_offset < stream_offset){ - int padding = stream_offset & (RHIZOME_CRYPT_PAGE_SIZE -1); - int size = RHIZOME_CRYPT_PAGE_SIZE - padding; + size_t padding = stream_offset & (RHIZOME_CRYPT_PAGE_SIZE -1); + size_t size = RHIZOME_CRYPT_PAGE_SIZE - padding; if (size>buffer_size) size=buffer_size; @@ -619,11 +619,11 @@ int rhizome_crypt_xor_block(unsigned char *buffer, int buffer_size, int64_t stre } while(offset < buffer_size){ - int size = buffer_size - offset; + size_t size = buffer_size - offset; if (size>RHIZOME_CRYPT_PAGE_SIZE) size=RHIZOME_CRYPT_PAGE_SIZE; - crypto_stream_xsalsa20_xor(buffer+offset, buffer+offset, size, block_nonce, key); + crypto_stream_xsalsa20_xor(buffer+offset, buffer+offset, (unsigned long long) size, block_nonce, key); add_nonce(block_nonce, RHIZOME_CRYPT_PAGE_SIZE); offset+=size; diff --git a/rhizome_direct.c b/rhizome_direct.c index b4ce9631..02d91833 100644 --- a/rhizome_direct.c +++ b/rhizome_direct.c @@ -135,7 +135,7 @@ rhizome_direct_sync_request *rhizome_direct_new_sync_request( void (*transport_specific_dispatch_function) (struct rhizome_direct_sync_request *), - int buffer_size,int interval, int mode, void *state) + size_t buffer_size, int interval, int mode, void *state) { assert(mode&3); @@ -287,7 +287,7 @@ rhizome_direct_bundle_cursor *rhizome_direct_get_fill_response(unsigned char *bu } DEBUGF("unpickled size_high=%"PRId64", limit_size_high=%"PRId64, c->size_high,c->limit_size_high); - DEBUGF("c->buffer_size=%d",c->buffer_size); + DEBUGF("c->buffer_size=%zu",c->buffer_size); /* Get our list of BARs for the same cursor range */ int us_count=rhizome_direct_bundle_iterator_fill(c,-1); @@ -533,7 +533,7 @@ int app_rhizome_direct_sync(const struct cli_parsed *parsed, struct cli_context } } -rhizome_direct_bundle_cursor *rhizome_direct_bundle_iterator(int buffer_size) +rhizome_direct_bundle_cursor *rhizome_direct_bundle_iterator(size_t buffer_size) { rhizome_direct_bundle_cursor *r=calloc(sizeof(rhizome_direct_bundle_cursor),1); assert(r!=NULL); diff --git a/rhizome_store.c b/rhizome_store.c index b9d8fa51..bbf6a51c 100644 --- a/rhizome_store.c +++ b/rhizome_store.c @@ -187,7 +187,8 @@ static int write_get_lock(struct rhizome_write *write_state){ } // write data to disk -static int write_data(struct rhizome_write *write_state, uint64_t file_offset, unsigned char *buffer, int data_size){ +static int write_data(struct rhizome_write *write_state, uint64_t file_offset, unsigned char *buffer, size_t data_size) +{ if (data_size<=0) return 0; @@ -253,7 +254,8 @@ static int write_release_lock(struct rhizome_write *write_state){ // Write data buffers in any order, the data will be cached and streamed into the database in file order. // Though there is an upper bound on the amount of cached data -int rhizome_random_write(struct rhizome_write *write_state, int64_t offset, unsigned char *buffer, int data_size){ +int rhizome_random_write(struct rhizome_write *write_state, int64_t offset, unsigned char *buffer, size_t data_size) +{ if (offset + data_size > write_state->file_length) data_size = write_state->file_length - offset; @@ -266,7 +268,7 @@ int rhizome_random_write(struct rhizome_write *write_state, int64_t offset, unsi }else{ // cache up to RHIZOME_BUFFER_MAXIMUM_SIZE or file length before attempting to write everything in one go. // (Not perfect if the range overlaps) - int64_t new_size = write_state->written_offset + write_state->buffer_size + data_size; + uint64_t new_size = write_state->written_offset + write_state->buffer_size + data_size; if (new_size>=write_state->file_length || new_size>=RHIZOME_BUFFER_MAXIMUM_SIZE) should_write = 1; } @@ -325,7 +327,7 @@ int rhizome_random_write(struct rhizome_write *write_state, int64_t offset, unsi if (!*ptr || offset < (*ptr)->offset){ // found the insert position in the list - int64_t size = data_size; + size_t size = data_size; // allow for buffers to overlap, we may need to split the incoming buffer into multiple pieces. if (*ptr && offset+size > (*ptr)->offset) @@ -349,7 +351,7 @@ int rhizome_random_write(struct rhizome_write *write_state, int64_t offset, unsi break; if (config.debug.rhizome) - DEBUGF("Caching block @%"PRId64", %"PRId64, offset, size); + DEBUGF("Caching block @%"PRId64", %zu", offset, size); struct rhizome_write_buffer *i = emalloc(size + sizeof(struct rhizome_write_buffer)); if (!i){ ret=-1; @@ -378,7 +380,8 @@ int rhizome_random_write(struct rhizome_write *write_state, int64_t offset, unsi return ret; } -int rhizome_write_buffer(struct rhizome_write *write_state, unsigned char *buffer, int data_size){ +int rhizome_write_buffer(struct rhizome_write *write_state, unsigned char *buffer, size_t data_size) +{ return rhizome_random_write(write_state, write_state->file_offset, buffer, data_size); } @@ -399,8 +402,8 @@ int rhizome_write_file(struct rhizome_write *write, const char *filename){ if (write->file_offset + size > write->file_length) size=write->file_length - write->file_offset; - int r = fread(buffer, 1, size, f); - if (r==-1){ + size_t r = fread(buffer, 1, size, f); + if (ferror(f)){ ret = WHY_perror("fread"); goto end; } @@ -573,12 +576,12 @@ int rhizome_import_file(rhizome_manifest *m, const char *filepath) } // store a whole payload from a single buffer -int rhizome_import_buffer(rhizome_manifest *m, unsigned char *buffer, int length) +int rhizome_import_buffer(rhizome_manifest *m, unsigned char *buffer, size_t length) { if (m->fileLength<=0) return 0; if (length!=m->fileLength) - return WHYF("Expected %"PRId64" bytes, got %d", m->fileLength, length); + return WHYF("Expected %"PRId64" bytes, got %zu", m->fileLength, length); /* Import the file first, checking the hash as we go */ struct rhizome_write write; @@ -779,7 +782,7 @@ static ssize_t rhizome_read_retry(sqlite_retry_state *retry, struct rhizome_read /* Read content from the store, hashing and decrypting as we go. Random access is supported, but hashing requires all payload contents to be read sequentially. */ // returns the number of bytes read -int rhizome_read(struct rhizome_read *read_state, unsigned char *buffer, int buffer_length) +ssize_t rhizome_read(struct rhizome_read *read_state, unsigned char *buffer, size_t buffer_length) { IN(); // hash check failed, just return an error @@ -823,9 +826,9 @@ int rhizome_read(struct rhizome_read *read_state, unsigned char *buffer, int buf } /* Read len bytes from read->offset into data, using *buffer to cache any reads */ -int rhizome_read_buffered(struct rhizome_read *read, struct rhizome_read_buffer *buffer, unsigned char *data, int len) +int rhizome_read_buffered(struct rhizome_read *read, struct rhizome_read_buffer *buffer, unsigned char *data, size_t len) { - int bytes_copied=0; + size_t bytes_copied=0; while (len>0){ // make sure we only attempt to read data that actually exists @@ -835,7 +838,7 @@ int rhizome_read_buffered(struct rhizome_read *read, struct rhizome_read_buffer // if we can supply either the beginning or end of the data from cache, do that first. uint64_t ofs=read->offset - buffer->offset; if (ofs>=0 && ofs<=buffer->len){ - int size=len; + size_t size=len; if (size > buffer->len - ofs) size = buffer->len - ofs; if (size>0){ @@ -851,7 +854,7 @@ int rhizome_read_buffered(struct rhizome_read *read, struct rhizome_read_buffer ofs = (read->offset+len) - buffer->offset; if (ofs>0 && ofs<=buffer->len){ - int size=len; + size_t size=len; if (size > ofs) size = ofs; if (size>0){ @@ -867,10 +870,12 @@ int rhizome_read_buffered(struct rhizome_read *read, struct rhizome_read_buffer // remember the requested read offset so we can put it back ofs = read->offset; buffer->offset = read->offset = ofs & ~(RHIZOME_CRYPT_PAGE_SIZE -1); - buffer->len = rhizome_read(read, buffer->data, sizeof(buffer->data)); + ssize_t len = rhizome_read(read, buffer->data, sizeof(buffer->data)); read->offset = ofs; - if (buffer->len<=0) - return buffer->len; + buffer->len = 0; + if (len == -1) + return -1; + buffer->len = (size_t) len; } return bytes_copied; } @@ -991,7 +996,7 @@ int rhizome_cache_count() } // read a block of data, caching meta data for reuse -int rhizome_read_cached(const rhizome_bid_t *bidp, uint64_t version, time_ms_t timeout, uint64_t fileOffset, unsigned char *buffer, int length) +int rhizome_read_cached(const rhizome_bid_t *bidp, uint64_t version, time_ms_t timeout, uint64_t fileOffset, unsigned char *buffer, size_t length) { // look for a cached entry struct cache_entry **ptr = find_entry_location(&root, bidp, version); @@ -1133,17 +1138,17 @@ static int rhizome_pipe(struct rhizome_read *read, struct rhizome_write *write, unsigned char buffer[RHIZOME_CRYPT_PAGE_SIZE]; while(length>0){ - int size=sizeof(buffer); + size_t size=sizeof(buffer); if (size > length) size=length; - int r = rhizome_read(read, buffer, size); - if (r<0) + ssize_t r = rhizome_read(read, buffer, size); + if (r == -1) return r; - length -= r; + length -= (size_t) r; - if (rhizome_write_buffer(write, buffer, r)) + if (rhizome_write_buffer(write, buffer, (size_t) r)) return -1; } @@ -1207,12 +1212,12 @@ failure: return ret; } -int rhizome_append_journal_buffer(rhizome_manifest *m, rhizome_bk_t *bsk, uint64_t advance_by, unsigned char *buffer, int len) +int rhizome_append_journal_buffer(rhizome_manifest *m, rhizome_bk_t *bsk, uint64_t advance_by, unsigned char *buffer, size_t len) { struct rhizome_write write; bzero(&write, sizeof write); - int ret = rhizome_write_open_journal(&write, m, bsk, advance_by, len); + int ret = rhizome_write_open_journal(&write, m, bsk, advance_by, (uint64_t) len); if (ret) return -1;