static int write_log_entry(const char *logfile, struct log_entry_t *le) { #if defined(__unix__) || defined(__APPLE__) int fd = open(logfile, O_RDWR | O_CREAT, FILE_PERM); #else int fd = open(logfile, O_RDWR | O_CREAT); #endif if(fd == -1) { return 0; } if(!write_uint64(fd, le->term) || !write_uint64(fd, le->index) || !write_uint64(fd, le->req_id) || !write_uint32(fd, le->bufsize)) { DBG_LOG(LOG_ERROR, "error in file operation"); return 0; } int i = 0; while(i < le->bufsize) { int nwrite = write(fd, le->buffer + i, le->bufsize - i); if(nwrite == -1) { close(fd); return 0; } i += nwrite; } close(fd); return 1; }
void *write_pipe_(void *args) { uint16_t len = (uint16_t)(((FnArgs *)args)->len); uint8_t *buf = (uint8_t *)(((FnArgs *)args)->buf); // Construct and write out the IOQ. ioq_t ioq; ioq.v = 0; uint16_t words = len >> 3; if ((len & 7) != 0) words++; ioq.f.srcp = htons(1); ioq.f.wlen = htons(words); ioq.f.dstp = htons(1); ioq.f.blen = htons(len); uint8_t outctrl = 0xff; uint64_t outdata = ioq.v; write_uint64("in_data", outdata); write_uint8("in_ctrl", outctrl); printf("Wrote the IOQ (%llx).\n", outdata); // Write out full 64-bit words of data. outctrl = 0; uint16_t i; for (i = 0; i < words - 1; ++i) { word_t w; w.v = 0; int j; for (j = 0; j < 8; ++j) w.s.b[j] = buf[i * 8 + j]; write_uint64("in_data", w.v); write_uint8("in_ctrl", outctrl); printf("Wrote data word %u (%llx).\n", i, w.v); } // Write out the last (partial) word. // Control bus has one bit set to mark the last byte. // E.g. 2 bytes in the last word, outctrl = 01000000. outctrl = ((uint8_t)0x80) >> ((len & 7) - 1); word_t w; w.v = 0; int j; for (j = 0; j < 8; ++j) w.s.b[j] = buf[i * 8 + j]; write_uint64("in_data", w.v); write_uint8("in_ctrl", outctrl); printf("Wrote the last data word, #%u (%llx).\n", i, w.v); }
// shows the use of the pipeHandler. int main(int argc, char* argv[]) { // must be initialized before doing anything init_pipe_handler(); // register a FIFO pipe test_pipe with // depth 32, word-width 32. register_pipe("test_pipe",32,32,0); register_pipe("test_pipe_64",1,64,0); // write a integer to test_pipe. write_uint32("test_pipe",1); uint64_t tmp = 1; tmp = tmp | (tmp << 32); write_uint64("test_pipe_64",tmp); // read back and print integer. uint32_t v = read_uint32("test_pipe"); fprintf(stderr,"TEST: received uint32_t %d\n", v); uint64_t v64 = read_uint64("test_pipe_64"); fprintf(stderr,"TEST: received uint64_t %llu\n", v64); // write another integer write_uint32("test_pipe",1); // read back and print integer. v = read_uint32("test_pipe"); fprintf(stderr,"TEST: received %d\n", v); // close the handler. close_pipe_handler(); return(0); }
static int writer_uint64(lua_State* L) { writer_t* writer = lua_touserdata(L, 1); lua_Integer integer = luaL_checkinteger(L, 2); write_uint64(writer, (uint64_t)integer); return 0; }
static int write_header_v1(struct save_data *save_data, struct ipset_node_cache *cache, ipset_node_id root) { /* Output the magic number for an IP set, and the file format * version that we're going to write. */ rii_check(cork_stream_consumer_data(save_data->stream, NULL, 0, true)); rii_check(write_string(save_data->stream, MAGIC_NUMBER)); rii_check(write_uint16(save_data->stream, 0x0001)); /* Determine how many reachable nodes there are, to calculate the * size of the set. */ size_t nonterminal_count = ipset_node_reachable_count(cache, root); size_t set_size = MAGIC_NUMBER_LENGTH + /* magic number */ sizeof(uint16_t) + /* version number */ sizeof(uint64_t) + /* length of set */ sizeof(uint32_t) + /* number of nonterminals */ (nonterminal_count * /* for each nonterminal: */ (sizeof(uint8_t) + /* variable number */ sizeof(uint32_t) + /* low pointer */ sizeof(uint32_t) /* high pointer */ )); /* If the root is a terminal, we need to add 4 bytes to the set * size, for storing the terminal value. */ if (ipset_node_get_type(root) == IPSET_TERMINAL_NODE) { set_size += sizeof(uint32_t); } rii_check(write_uint64(save_data->stream, set_size)); rii_check(write_uint32(save_data->stream, nonterminal_count)); return 0; }
void *spice_marshaller_add_uint64(SpiceMarshaller *m, uint64_t v) { uint8_t *ptr; ptr = spice_marshaller_reserve_space(m, sizeof(uint64_t)); write_uint64(ptr, v); return (void *)ptr; }
void write_uint64_n(const char *id, uint64_t* buf, int buf_len) { int i; for(i = 0; i < buf_len; i++) { write_uint64((char*) id,buf[i]); } }
int key_value_write_lines(FILE *f, struct key_value_specification *kvs, void *base_address[]) { struct key_value_specification *k; int rc; int errs = 0; for (k = kvs; k->key; k++) { switch (k->type) { case KVS_STRING: rc = write_string(f, k, base_address); break; case KVS_INT64: rc = write_int64(f, k, base_address); break; case KVS_INT32: rc = write_int32(f, k, base_address); break; case KVS_INT16: rc = write_int16(f, k, base_address); break; case KVS_INT8: rc = write_int8(f, k, base_address); break; case KVS_UINT64: rc = write_uint64(f, k, base_address); break; case KVS_UINT32: rc = write_uint32(f, k, base_address); break; case KVS_UINT16: rc = write_uint16(f, k, base_address); break; case KVS_UINT8: rc = write_uint8(f, k, base_address); break; case KVS_DOUBLE: rc = write_double(f, k, base_address); break; case KVS_FLOAT: rc = write_float(f, k, base_address); break; default: fprintf(stderr, "%s:%d: unknown key type '%c' for key '%s'\n", __func__, __LINE__, k->type, k->key); rc = -1; break; } if (rc) errs++; } return errs; }
void Squeezer_chunk_header_t::write_to_file(FILE * out) const { write_uint8(out, chunk_mark[0]); write_uint8(out, chunk_mark[1]); write_uint8(out, chunk_mark[2]); write_uint8(out, chunk_mark[3]); write_uint64(out, number_of_bytes); write_uint32(out, number_of_samples); write_uint32(out, chunk_type); compression_error.write_to_file(out); }
static void save_state(struct server_context_t *s) { //TODO: implement atomic operation DBG_LOG(LOG_INFO, "[%s][%d] Saving state to disk", ss[s->state], s->current_term); char *raftstate = path_join(s->basedir, RAFT_STATE); if(raftstate) { #if defined(__unix__) || defined(__APPLE__) int fd = open(raftstate, O_RDWR | O_CREAT, FILE_PERM); #else int fd = open(raftstate, O_RDWR | O_CREAT); #endif if(fd == -1 || !write_uint64(fd, s->current_term) || !write_uint32(fd, s->current_leader)) { log_fatal_and_exit(s, "error in saving state to file"); } close(fd); free(raftstate); } }
void spice_marshaller_flush(SpiceMarshaller *m) { SpiceMarshaller *m2; uint8_t *ptr_pos; /* Only supported for root marshaller */ assert(m->data->marshallers == m); for (m2 = m; m2 != NULL; m2 = m2->next) { if (m2->pointer_ref.marshaller != NULL && m2->total_size > 0) { ptr_pos = lookup_ref(&m2->pointer_ref); if (m2->pointer_ref.is_64bit) { write_uint64(ptr_pos, spice_marshaller_get_offset(m2)); } else { write_uint32(ptr_pos, spice_marshaller_get_offset(m2)); } } } }
void *write_pipe_(void* a) { write_uint64("foo_in",*((uint64_t*)a)); write_uint64("foo_in",*((uint64_t*)a)); }
int journal_file_verify( JournalFile *f, const char *key, usec_t *first_contained, usec_t *last_validated, usec_t *last_contained, bool show_progress) { int r; Object *o; uint64_t p = 0, last_epoch = 0, last_tag_realtime = 0, last_sealed_realtime = 0; uint64_t entry_seqnum = 0, entry_monotonic = 0, entry_realtime = 0; sd_id128_t entry_boot_id; bool entry_seqnum_set = false, entry_monotonic_set = false, entry_realtime_set = false, found_main_entry_array = false; uint64_t n_weird = 0, n_objects = 0, n_entries = 0, n_data = 0, n_fields = 0, n_data_hash_tables = 0, n_field_hash_tables = 0, n_entry_arrays = 0, n_tags = 0; usec_t last_usec = 0; int data_fd = -1, entry_fd = -1, entry_array_fd = -1; unsigned i; bool found_last = false; #ifdef HAVE_GCRYPT uint64_t last_tag = 0; #endif assert(f); if (key) { #ifdef HAVE_GCRYPT r = journal_file_parse_verification_key(f, key); if (r < 0) { log_error("Failed to parse seed."); return r; } #else return -ENOTSUP; #endif } else if (f->seal) return -ENOKEY; data_fd = open_tmpfile("/var/tmp", O_RDWR | O_CLOEXEC); if (data_fd < 0) { log_error_errno(errno, "Failed to create data file: %m"); r = -errno; goto fail; } entry_fd = open_tmpfile("/var/tmp", O_RDWR | O_CLOEXEC); if (entry_fd < 0) { log_error_errno(errno, "Failed to create entry file: %m"); r = -errno; goto fail; } entry_array_fd = open_tmpfile("/var/tmp", O_RDWR | O_CLOEXEC); if (entry_array_fd < 0) { log_error_errno(errno, "Failed to create entry array file: %m"); r = -errno; goto fail; } if (le32toh(f->header->compatible_flags) & ~HEADER_COMPATIBLE_SUPPORTED) { log_error("Cannot verify file with unknown extensions."); r = -ENOTSUP; goto fail; } for (i = 0; i < sizeof(f->header->reserved); i++) if (f->header->reserved[i] != 0) { error(offsetof(Header, reserved[i]), "Reserved field is non-zero"); r = -EBADMSG; goto fail; } /* First iteration: we go through all objects, verify the * superficial structure, headers, hashes. */ p = le64toh(f->header->header_size); for (;;) { /* Early exit if there are no objects in the file, at all */ if (le64toh(f->header->tail_object_offset) == 0) break; if (show_progress) draw_progress(scale_progress(0x7FFF, p, le64toh(f->header->tail_object_offset)), &last_usec); r = journal_file_move_to_object(f, OBJECT_UNUSED, p, &o); if (r < 0) { error(p, "Invalid object"); goto fail; } if (p > le64toh(f->header->tail_object_offset)) { error(offsetof(Header, tail_object_offset), "Invalid tail object pointer"); r = -EBADMSG; goto fail; } n_objects ++; r = journal_file_object_verify(f, p, o); if (r < 0) { error(p, "Invalid object contents: %s", strerror(-r)); goto fail; } if ((o->object.flags & OBJECT_COMPRESSED_XZ) && (o->object.flags & OBJECT_COMPRESSED_LZ4)) { error(p, "Objected with double compression"); r = -EINVAL; goto fail; } if ((o->object.flags & OBJECT_COMPRESSED_XZ) && !JOURNAL_HEADER_COMPRESSED_XZ(f->header)) { error(p, "XZ compressed object in file without XZ compression"); r = -EBADMSG; goto fail; } if ((o->object.flags & OBJECT_COMPRESSED_LZ4) && !JOURNAL_HEADER_COMPRESSED_LZ4(f->header)) { error(p, "LZ4 compressed object in file without LZ4 compression"); r = -EBADMSG; goto fail; } switch (o->object.type) { case OBJECT_DATA: r = write_uint64(data_fd, p); if (r < 0) goto fail; n_data++; break; case OBJECT_FIELD: n_fields++; break; case OBJECT_ENTRY: if (JOURNAL_HEADER_SEALED(f->header) && n_tags <= 0) { error(p, "First entry before first tag"); r = -EBADMSG; goto fail; } r = write_uint64(entry_fd, p); if (r < 0) goto fail; if (le64toh(o->entry.realtime) < last_tag_realtime) { error(p, "Older entry after newer tag"); r = -EBADMSG; goto fail; } if (!entry_seqnum_set && le64toh(o->entry.seqnum) != le64toh(f->header->head_entry_seqnum)) { error(p, "Head entry sequence number incorrect"); r = -EBADMSG; goto fail; } if (entry_seqnum_set && entry_seqnum >= le64toh(o->entry.seqnum)) { error(p, "Entry sequence number out of synchronization"); r = -EBADMSG; goto fail; } entry_seqnum = le64toh(o->entry.seqnum); entry_seqnum_set = true; if (entry_monotonic_set && sd_id128_equal(entry_boot_id, o->entry.boot_id) && entry_monotonic > le64toh(o->entry.monotonic)) { error(p, "Entry timestamp out of synchronization"); r = -EBADMSG; goto fail; } entry_monotonic = le64toh(o->entry.monotonic); entry_boot_id = o->entry.boot_id; entry_monotonic_set = true; if (!entry_realtime_set && le64toh(o->entry.realtime) != le64toh(f->header->head_entry_realtime)) { error(p, "Head entry realtime timestamp incorrect"); r = -EBADMSG; goto fail; } entry_realtime = le64toh(o->entry.realtime); entry_realtime_set = true; n_entries ++; break; case OBJECT_DATA_HASH_TABLE: if (n_data_hash_tables > 1) { error(p, "More than one data hash table"); r = -EBADMSG; goto fail; } if (le64toh(f->header->data_hash_table_offset) != p + offsetof(HashTableObject, items) || le64toh(f->header->data_hash_table_size) != le64toh(o->object.size) - offsetof(HashTableObject, items)) { error(p, "header fields for data hash table invalid"); r = -EBADMSG; goto fail; } n_data_hash_tables++; break; case OBJECT_FIELD_HASH_TABLE: if (n_field_hash_tables > 1) { error(p, "More than one field hash table"); r = -EBADMSG; goto fail; } if (le64toh(f->header->field_hash_table_offset) != p + offsetof(HashTableObject, items) || le64toh(f->header->field_hash_table_size) != le64toh(o->object.size) - offsetof(HashTableObject, items)) { error(p, "Header fields for field hash table invalid"); r = -EBADMSG; goto fail; } n_field_hash_tables++; break; case OBJECT_ENTRY_ARRAY: r = write_uint64(entry_array_fd, p); if (r < 0) goto fail; if (p == le64toh(f->header->entry_array_offset)) { if (found_main_entry_array) { error(p, "More than one main entry array"); r = -EBADMSG; goto fail; } found_main_entry_array = true; } n_entry_arrays++; break; case OBJECT_TAG: if (!JOURNAL_HEADER_SEALED(f->header)) { error(p, "Tag object in file without sealing"); r = -EBADMSG; goto fail; } if (le64toh(o->tag.seqnum) != n_tags + 1) { error(p, "Tag sequence number out of synchronization"); r = -EBADMSG; goto fail; } if (le64toh(o->tag.epoch) < last_epoch) { error(p, "Epoch sequence out of synchronization"); r = -EBADMSG; goto fail; } #ifdef HAVE_GCRYPT if (f->seal) { uint64_t q, rt; debug(p, "Checking tag %"PRIu64"...", le64toh(o->tag.seqnum)); rt = f->fss_start_usec + o->tag.epoch * f->fss_interval_usec; if (entry_realtime_set && entry_realtime >= rt + f->fss_interval_usec) { error(p, "tag/entry realtime timestamp out of synchronization"); r = -EBADMSG; goto fail; } /* OK, now we know the epoch. So let's now set * it, and calculate the HMAC for everything * since the last tag. */ r = journal_file_fsprg_seek(f, le64toh(o->tag.epoch)); if (r < 0) goto fail; r = journal_file_hmac_start(f); if (r < 0) goto fail; if (last_tag == 0) { r = journal_file_hmac_put_header(f); if (r < 0) goto fail; q = le64toh(f->header->header_size); } else q = last_tag; while (q <= p) { r = journal_file_move_to_object(f, OBJECT_UNUSED, q, &o); if (r < 0) goto fail; r = journal_file_hmac_put_object(f, OBJECT_UNUSED, o, q); if (r < 0) goto fail; q = q + ALIGN64(le64toh(o->object.size)); } /* Position might have changed, let's reposition things */ r = journal_file_move_to_object(f, OBJECT_UNUSED, p, &o); if (r < 0) goto fail; if (memcmp(o->tag.tag, gcry_md_read(f->hmac, 0), TAG_LENGTH) != 0) { error(p, "Tag failed verification"); r = -EBADMSG; goto fail; } f->hmac_running = false; last_tag_realtime = rt; last_sealed_realtime = entry_realtime; } last_tag = p + ALIGN64(le64toh(o->object.size)); #endif last_epoch = le64toh(o->tag.epoch); n_tags ++; break; default: n_weird ++; } if (p == le64toh(f->header->tail_object_offset)) { found_last = true; break; } p = p + ALIGN64(le64toh(o->object.size)); }; if (!found_last && le64toh(f->header->tail_object_offset) != 0) { error(le64toh(f->header->tail_object_offset), "Tail object pointer dead"); r = -EBADMSG; goto fail; } if (n_objects != le64toh(f->header->n_objects)) { error(offsetof(Header, n_objects), "Object number mismatch"); r = -EBADMSG; goto fail; } if (n_entries != le64toh(f->header->n_entries)) { error(offsetof(Header, n_entries), "Entry number mismatch"); r = -EBADMSG; goto fail; } if (JOURNAL_HEADER_CONTAINS(f->header, n_data) && n_data != le64toh(f->header->n_data)) { error(offsetof(Header, n_data), "Data number mismatch"); r = -EBADMSG; goto fail; } if (JOURNAL_HEADER_CONTAINS(f->header, n_fields) && n_fields != le64toh(f->header->n_fields)) { error(offsetof(Header, n_fields), "Field number mismatch"); r = -EBADMSG; goto fail; } if (JOURNAL_HEADER_CONTAINS(f->header, n_tags) && n_tags != le64toh(f->header->n_tags)) { error(offsetof(Header, n_tags), "Tag number mismatch"); r = -EBADMSG; goto fail; } if (JOURNAL_HEADER_CONTAINS(f->header, n_entry_arrays) && n_entry_arrays != le64toh(f->header->n_entry_arrays)) { error(offsetof(Header, n_entry_arrays), "Entry array number mismatch"); r = -EBADMSG; goto fail; } if (!found_main_entry_array && le64toh(f->header->entry_array_offset) != 0) { error(0, "Missing entry array"); r = -EBADMSG; goto fail; } if (entry_seqnum_set && entry_seqnum != le64toh(f->header->tail_entry_seqnum)) { error(offsetof(Header, tail_entry_seqnum), "Invalid tail seqnum"); r = -EBADMSG; goto fail; } if (entry_monotonic_set && (!sd_id128_equal(entry_boot_id, f->header->boot_id) || entry_monotonic != le64toh(f->header->tail_entry_monotonic))) { error(0, "Invalid tail monotonic timestamp"); r = -EBADMSG; goto fail; } if (entry_realtime_set && entry_realtime != le64toh(f->header->tail_entry_realtime)) { error(0, "Invalid tail realtime timestamp"); r = -EBADMSG; goto fail; } /* Second iteration: we follow all objects referenced from the * two entry points: the object hash table and the entry * array. We also check that everything referenced (directly * or indirectly) in the data hash table also exists in the * entry array, and vice versa. Note that we do not care for * unreferenced objects. We only care that everything that is * referenced is consistent. */ r = verify_entry_array(f, data_fd, n_data, entry_fd, n_entries, entry_array_fd, n_entry_arrays, &last_usec, show_progress); if (r < 0) goto fail; r = verify_hash_table(f, data_fd, n_data, entry_fd, n_entries, entry_array_fd, n_entry_arrays, &last_usec, show_progress); if (r < 0) goto fail; if (show_progress) flush_progress(); mmap_cache_close_fd(f->mmap, data_fd); mmap_cache_close_fd(f->mmap, entry_fd); mmap_cache_close_fd(f->mmap, entry_array_fd); safe_close(data_fd); safe_close(entry_fd); safe_close(entry_array_fd); if (first_contained) *first_contained = le64toh(f->header->head_entry_realtime); if (last_validated) *last_validated = last_sealed_realtime; if (last_contained) *last_contained = le64toh(f->header->tail_entry_realtime); return 0; fail: if (show_progress) flush_progress(); log_error("File corruption detected at %s:"OFSfmt" (of %llu bytes, %"PRIu64"%%).", f->path, p, (unsigned long long) f->last_stat.st_size, 100 * p / f->last_stat.st_size); if (data_fd >= 0) { mmap_cache_close_fd(f->mmap, data_fd); safe_close(data_fd); } if (entry_fd >= 0) { mmap_cache_close_fd(f->mmap, entry_fd); safe_close(entry_fd); } if (entry_array_fd >= 0) { mmap_cache_close_fd(f->mmap, entry_array_fd); safe_close(entry_array_fd); } return r; }
int start (void) #endif { uint64_t apl64; uint32_t i, apl; uint16_t apl16; uint8_t apl8,my_apl8; void *sptr, *rptr; double ong64, my_ong64; float ong, my_ong; int failure = 0; i = 0; while (i < 10) { #ifdef RUN apl = rand(); #else apl = i + 1; #endif my_ong = (float)apl; my_ong64 = (double)apl; apl64 = (uint64_t)apl; apl16 = (uint16_t)apl; my_apl8 = (uint8_t)apl16; write_uint64("apples64", apl64); #ifdef RUN fprintf(stderr, "\n(%d.a) sent a 64-bit apple: %llu..", i, apl64); #endif ong64 = read_float64("oranges64"); #ifdef RUN fprintf(stderr, "\ngot a (double) orange: %le.", ong64); #endif failure |= (my_ong64 != ong64); write_uint32("apples32", apl); #ifdef RUN fprintf(stderr, "\n(%d.b) sent a 32-bit apple: %d.", i, apl); #endif ong = read_float32("oranges32"); #ifdef RUN fprintf(stderr, "\ngot a (float) orange: %f.", ong); #endif failure |= (my_ong != ong); write_uint16("apples16", apl16); #ifdef RUN fprintf(stderr, "\n(%d.c) sent a 16-bit apple: %d.", i, apl16); #endif apl8 = read_uint8("oranges8"); #ifdef RUN fprintf(stderr, "\ngot an 8-bit orange: %d.", apl8); #endif sptr = (void*) &apl; write_pointer("apples32", sptr); #ifdef RUN fprintf(stderr, "\n(%d.d) sent a pointer apple: %d.", i, (unsigned int)sptr); #endif rptr = read_pointer("oranges32"); #ifdef RUN fprintf(stderr, "\ngot a pointer orange: %d.",(unsigned int) rptr); #endif failure |= (sptr != rptr); ++i; } #ifdef RUN if (failure == 0) fprintf(stderr, "\nAll conversions successful.\n"); else fprintf(stderr, "\nSome conversion(s) failed.\n"); #endif return failure; }