int parse_fan_id(char *line) { unsigned int input_val, offset, i; offset = fan_id_num * FAN_BLOCK_SIZE; fan_id_num++; if (scan_line(line, &input_val) == ERROR) return (ERROR); for (i = 0; i < fan_id_num-1; i++) { if (!memcmp(&input_val, &id_header.fan_block[i*FAN_BLOCK_SIZE], FAN_ID_SIZE)) { printf("ERROR: Two fans with id = %x\n", input_val); return (ERROR); } } write_bytes(input_val, FAN_ID_SIZE, offset, id_header.fan_block); offset += FAN_ID_SIZE; write_bytes(running_offset, FAN_OFF_SIZE, offset, id_header.fan_block); return (NO_ERROR); }
/* Store a 32-bit integer into the cache file according to the file format. */ static krb5_error_code store32(krb5_context context, krb5_ccache id, uint32_t i) { unsigned char buf[4]; k5_cc_mutex_assert_locked(context, &((fcc_data *)id->data)->lock); if (version(id) < 3) store_32_n(i, buf); else store_32_be(i, buf); return write_bytes(context, id, buf, 4); }
void llvm_gcda_emit_arcs(uint32_t num_counters, uint64_t *counters) { uint32_t i; uint64_t *old_ctrs = NULL; uint32_t val = 0; uint64_t save_cur_pos = cur_pos; if (!output_file) return; val = read_32bit_value(); if (val != (uint32_t)-1) { /* There are counters present in the file. Merge them. */ if (val != 0x01a10000) { fprintf(stderr, "profiling: %s: cannot merge previous GCDA file: " "corrupt arc tag (0x%08x)\n", filename, val); return; } val = read_32bit_value(); if (val == (uint32_t)-1 || val / 2 != num_counters) { fprintf(stderr, "profiling: %s: cannot merge previous GCDA file: " "mismatched number of counters (%d)\n", filename, val); return; } old_ctrs = malloc(sizeof(uint64_t) * num_counters); for (i = 0; i < num_counters; ++i) old_ctrs[i] = read_64bit_value(); } cur_pos = save_cur_pos; /* Counter #1 (arcs) tag */ write_bytes("\0\0\xa1\1", 4); write_32bit_value(num_counters * 2); for (i = 0; i < num_counters; ++i) { counters[i] += (old_ctrs ? old_ctrs[i] : 0); write_64bit_value(counters[i]); } free(old_ctrs); #ifdef DEBUG_GCDAPROFILING fprintf(stderr, "llvmgcda: %u arcs\n", num_counters); for (i = 0; i < num_counters; ++i) fprintf(stderr, "llvmgcda: %llu\n", (unsigned long long)counters[i]); #endif }
int parse_id(char *line) { unsigned int input_val, offset, i; offset = id_num * SENSOR_BLOCK_SIZE; id_num++; if (scan_line(line, &input_val) == ERROR) return (ERROR); for (i = 0; i < id_num-1; i++) { if (!memcmp(&input_val, &id_header.id_block[i*SENSOR_BLOCK_SIZE], ID_SIZE)) { printf("ERROR: Two sensors with id = %x\n", input_val); return (ERROR); } } write_bytes(input_val, ID_SIZE, offset, id_header.id_block); offset += ID_SIZE; write_bytes(running_offset, OFFSET_SIZE, offset, id_header.id_block); return (NO_ERROR); }
int tu_file::printf(const char* fmt, ...) // Use printf-like semantics to send output to this stream. { // Workspace for vsnprintf formatting. static const int BUFFER_SIZE = 1000; char buffer[BUFFER_SIZE]; va_list ap; va_start(ap, fmt); vsnprintf(buffer, BUFFER_SIZE, fmt, ap); va_end(ap); return write_bytes(buffer, (int) strlen(buffer)); }
static int write_708_cc( obe_user_data_t *user_data, uint8_t *start, int cc_count ) { bs_t s; uint8_t temp[1000]; const char *user_identifier = "GA94"; const int data_type_code = 0x03; /* TODO: when MPEG-2 is added make this do the right thing */ /* FIXME: enable echostar captions and add more types */ bs_init( &s, temp, 1000 ); /* N.B MPEG-4 only */ write_itu_t_codes( &s ); for( int i = 0; i < 4; i++ ) bs_write( &s, 8, user_identifier[i] ); // user_identifier bs_write( &s, 8, data_type_code ); // user_data_type_code // user_data_type_structure (echostar) // cc_data bs_write1( &s, 1 ); // reserved bs_write1( &s, 1 ); // process_cc_data_flag bs_write1( &s, 0 ); // zero_bit / additional_data_flag bs_write( &s, 5, cc_count ); // cc_count bs_write( &s, 8, 0xff ); // reserved write_bytes( &s, start, cc_count*3 ); bs_write( &s, 8, 0xff ); // marker_bits bs_flush( &s ); user_data->type = USER_DATA_AVC_REGISTERED_ITU_T35; user_data->len = bs_pos( &s ) >> 3; free( user_data->data ); user_data->data = malloc( user_data->len ); if( !user_data->data ) { syslog( LOG_ERR, "Malloc failed\n" ); return -1; } memcpy( user_data->data, temp, user_data->len ); return 0; }
static krb5_error_code store_principal(krb5_context context, krb5_ccache id, krb5_principal princ) { krb5_error_code ret; unsigned char *bytes; size_t len; k5_cc_mutex_assert_locked(context, &((fcc_data *)id->data)->lock); ret = k5_marshal_princ(princ, version(id), &bytes, &len); if (ret) return ret; ret = write_bytes(context, id, bytes, len); free(bytes); return ret; }
void FileMapInfo::align_file_position() { long new_file_offset = align_size_up(_file_offset, os::vm_allocation_granularity()); if (new_file_offset != _file_offset) { _file_offset = new_file_offset; if (_file_open) { // Seek one byte back from the target and write a byte to insure // that the written file is the correct length. _file_offset -= 1; if (lseek(_fd, _file_offset, SEEK_SET) < 0) { fail_stop("Unable to seek.", NULL); } char zero = 0; write_bytes(&zero, 1); } } }
lref_t lwrite_binary_string(lref_t string, lref_t port) { if (!STRINGP(string)) vmerror_wrong_type_n(1, string); if (!BINARY_PORTP(port)) vmerror_wrong_type_n(2, port); size_t sz = (string->as.string.dim * sizeof(_TCHAR)); size_t written = write_bytes(port, string->as.string.data, sz); if (written != sz) vmerror_io_error(_T("error writing to port."), port); return port; }
void do_page_fault(void *addr, size_t len, int fault_mode) { switch (fault_mode) { case PG_FAULT_READ: read_bytes(len, addr); break; case PG_FAULT_WRITE: write_bytes(len, addr); break; case PG_FAULT_RWLOOP: fault_rwloop(len, addr); break; case PG_FAULT_NONE: default: break; } }
lref_t lbinary_write_flonum(lref_t v, lref_t port) { if (!NUMBERP(v)) vmerror_wrong_type_n(1, v); if (!BINARY_PORTP(port)) vmerror_wrong_type_n(2, port); uint8_t bytes[sizeof(flonum_t)]; io_encode_flonum(bytes, get_c_flonum(v)); if (write_bytes(port, bytes, sizeof(flonum_t)) != sizeof(flonum_t)) vmerror_io_error(_T("error writing to port."), port); return port; }
static int do_data_transfer(struct mmc *dev, struct mmc_cmd *cmd, struct mmc_data *data) { int error = -ETIMEDOUT; struct pl180_mmc_host *host = dev->priv; u32 blksz = 0; u32 data_ctrl = 0; u32 data_len = (u32) (data->blocks * data->blocksize); if (!host->version2) { blksz = (ffs(data->blocksize) - 1); data_ctrl |= ((blksz << 4) & SDI_DCTRL_DBLKSIZE_MASK); } else { blksz = data->blocksize; data_ctrl |= (blksz << SDI_DCTRL_DBLOCKSIZE_V2_SHIFT); } data_ctrl |= SDI_DCTRL_DTEN | SDI_DCTRL_BUSYMODE; writel(SDI_DTIMER_DEFAULT, &host->base->datatimer); writel(data_len, &host->base->datalength); udelay(DATA_REG_DELAY); if (data->flags & MMC_DATA_READ) { data_ctrl |= SDI_DCTRL_DTDIR_IN; writel(data_ctrl, &host->base->datactrl); error = do_command(dev, cmd); if (error) return error; error = read_bytes(dev, (u32 *)data->dest, (u32)data->blocks, (u32)data->blocksize); } else if (data->flags & MMC_DATA_WRITE) { error = do_command(dev, cmd); if (error) return error; writel(data_ctrl, &host->base->datactrl); error = write_bytes(dev, (u32 *)data->src, (u32)data->blocks, (u32)data->blocksize); } return error; }
/** * Goes back and writes out the subroutine list */ void write_index() { fseek(dest_file, 3, SEEK_SET); int bytesRemaining = indexSize - 3; for (int i = 0; i < subroutinesCount; ++i) { int entrySize = strlen(subroutinesTable[i].name) + 5; // Ensure there is enough remaining space if ((bytesRemaining - entrySize) < 0) { printf("Index has exceeded allowable size.\n"); token = ERROR; } // Write out the name and the file offset write_bytes((byte *)&subroutinesTable[i].name, strlen(subroutinesTable[i].name) + 1); write_long(subroutinesTable[i].fileOffset); } }
void dynamic_inflate(reader_t *reader) { size_t hlit = read_HLIT(reader) + 257; size_t hdist = read_HDIST(reader) + 1; size_t hclen = read_HCLEN(reader) + 4; /*printf("hclen = %d, hlit = %d, hdist = %d\n", hclen, hlit, hdist);*/ huffman_code litlen_codes[hlit]; huffman_code offset_codes[hdist]; read_alphabets(reader, litlen_codes, hlit, offset_codes, hdist, hclen); io *io_s; init_io(&io_s); byte bytes[LEN_MAX]; size_t litlen; bool is_end_of_block = false; size_t block_size = 0; while (!is_end_of_block) { litlen = read_next_huffman_code(reader, litlen_codes, hlit); block_size++; if (litlen < END_OF_BLOCK) write_byte(io_s, litlen); else if (litlen == END_OF_BLOCK) is_end_of_block = true; else { two_bytes length = decode_length(reader, litlen); two_bytes offcode = read_next_huffman_code(reader, offset_codes, hdist); two_bytes distance = decode_distance(reader, offcode); get_cyclic_queue(io_s->output, bytes, length, distance); write_bytes(io_s, bytes, length); block_size = block_size - 1 + length; } } /*printf("block_size = %d\n", block_size);*/ write_to_output(io_s, reader->output); delete_io(&io_s); }
void msg_send(uint8_t chan, mavlink_message_t *p_msg) { uint8_t buf[1024]; uint16_t len; uint16_t write_len; len = mavlink_msg_to_send_buffer(buf, p_msg); switch(chan) { case 0: write_len = write_bytes((char *)buf, (uint32_t)len); if( write_len != len ) printf("wlen %d : len %d\r\n", write_len, len); break; case 1: break; } }
int update_fan_table(char *parameter, char *line) { int i = 0, offset = 0; unsigned int input_val; int size; unsigned char *data_ptr; while (strcmp(fan_parameters[i].name, parameter)) { offset += fan_parameters[i++].size; if (fan_parameters[i].name == NULL) { fprintf(stderr, "%s is not a valid fan parameter\n", parameter); return (ERROR); } } size = fan_parameters[i].size; data_ptr = cur_fan_table->fan_data; if (strcmp(parameter, "fan-min-range") == 0) { offset = FAN_MIN_RANGE_SZ * current_fan_pair; current_fan_pair++; data_ptr = cur_fan_table->fan_ctl_data; if ((fan_min_range_scan(line, &input_val) == ERROR)) return (ERROR); } else if ((scan_line(line, &input_val) == ERROR)) return (ERROR); if (strcmp(parameter, "num-ctl-pairs") == 0) { num_fan_ctl_pairs = input_val; cur_fan_table->fan_ctl_data = (unsigned char *) malloc(FAN_MIN_RANGE_SZ * num_fan_ctl_pairs); cur_fan_table->fan_pair_size = num_fan_ctl_pairs * FAN_MIN_RANGE_SZ; } write_bytes(input_val, size, offset, data_ptr); return (NO_ERROR); }
int main(void) { void *addr; int ret; addr = mmap(ADDR, LENGTH, PROTECTION, FLAGS, 0, 0); if (addr == MAP_FAILED) { perror("mmap"); exit(1); } printf("Returned address is %p\n", addr); check_bytes(addr); write_bytes(addr); ret = read_bytes(addr); munmap(addr, LENGTH); return ret; }
int update_env_table(char *parameter, char *line) { int i = 0, offset = 0; unsigned int input_val; int size; unsigned char *data_ptr; while (strcmp(env_parameters[i].name, parameter)) { offset += env_parameters[i++].size; if (env_parameters[i].name == NULL) { fprintf(stderr, "%s is not a valid env parameter\n", parameter); return (ERROR); } } size = env_parameters[i].size; data_ptr = cur_env_table->env_data; if (strcmp(parameter, "correction") == 0) { offset = COR_SIZE * current_correction; current_correction++; size = COR_SIZE; data_ptr = cur_env_table->correction_data; if ((correction_scan(line, &input_val) == ERROR)) return (ERROR); } else if ((scan_line(line, &input_val) == ERROR)) return (ERROR); if (strcmp(parameter, "num-corrections") == 0) { num_corrections = input_val; cur_env_table->correction_data = (unsigned char *) malloc(COR_SIZE * num_corrections); cur_env_table->correction_size = num_corrections * COR_SIZE; } write_bytes(input_val, size, offset, data_ptr); return (NO_ERROR); }
static int rjs_start_cmd(int fd) { rjs_process_t* p; int pid; p = rjs_proc_alloc(sizeof *p); if (p == NULL) return -1; memset(p, 0, sizeof *p); p->tag = read_string(fd); if (p->tag == NULL) goto err; p->file = read_string(fd); if (p->file == NULL) goto err; p->args = read_string(fd); if (p->args == NULL) goto err; p->env = read_string(fd); if (p->env == NULL) goto err; pid = spawn(p->file, p->args, p->env, 1); if (pid < 0) pid = -errno; pid = rjs_proc_start(p); if (pid < 0) goto err; write_bytes(fd, &pid, sizeof pid); return 0; err: rjs_proc_free(p); return -1; }
static void output_space(FILE * file, int id, lispobj * addr, lispobj * end) { int words, bytes, data; static char *names[] = { NULL, "Dynamic", "Static", "Read-Only" }; putw(id, file); words = end - addr; putw(words, file); bytes = words * sizeof(lispobj); printf("Writing %d bytes from the %s space at 0x%08lX.\n", bytes, names[id], (unsigned long) addr); data = write_bytes(file, (char *) addr, bytes); putw(data, file); putw((long) addr / CORE_PAGESIZE, file); putw((bytes + CORE_PAGESIZE - 1) / CORE_PAGESIZE, file); }
TEST(DATA_BUFFER_TEST, READ_AND_WRITE){ MEM_POOL_PTR pMemPool = mem_pool_init(MB_SIZE); struct data_buffer* buffer = init_data_buffer(); buffer->data_len = 256; buffer->data = (uint8*) mem_pool_malloc(pMemPool, 256); uint8 tmp8; uint16 tmp16; uint32 tmp32; uint64 tmp64; char* str = (char*) mem_pool_malloc(pMemPool, 100); char* result = (char*) mem_pool_malloc(pMemPool, 100); strcpy(str, "hello world!"); write_int8(122, buffer); write_int16(1111, buffer); write_int32(324, buffer); write_int64(2321, buffer); write_bytes((uint8*)str, strlen(str)+1, buffer); tmp8 = read_int8(buffer); ASSERT_EQ(tmp8, 122); tmp16 = read_int16(buffer); ASSERT_EQ(tmp16, 1111); tmp32 = read_int32(buffer); ASSERT_EQ(tmp32, 324); tmp64 = read_int64(buffer); ASSERT_EQ(tmp64, 2321); read_bytes(buffer, (uint8*)result, strlen(str)+1); ASSERT_EQ(0, strcmp(str, result)); destroy_data_buffer(buffer); mem_pool_destroy(pMemPool); }
/* \brief Private helper. Writes key part of OBJECT item * * \param writer binson_writer* * \param key const char* * \param force_no_separator int * \return binson_res */ binson_res write_key( binson_writer *writer, const char* key, int force_no_separator ) { binson_res res = BINSON_RES_OK; #ifdef WITH_BINSON_JSON_OUTPUT /* write comma separator if needed */ if (writer->format == BINSON_WRITER_FORMAT_JSON || writer->format == BINSON_WRITER_FORMAT_JSON_NICE) { if (!force_no_separator && writer->depth > 0 && writer->idx_stack[writer->depth] > 0) binson_io_write_str( writer->io, ", ", true ); if (writer->format == BINSON_WRITER_FORMAT_JSON_NICE) { int i; binson_io_write_byte( writer->io, (uint8_t)'\n' ); for (i=0; i<writer->depth*BINSON_WRITER_INDENT_FACTOR; i++) /* Indent white spaces */ binson_io_write_byte( writer->io, (uint8_t)' ' ); binson_io_write_byte( writer->io, '\0' ); } if (FAILED(res)) return res; } #endif if (key /*&& key[0] != '\0'*/) { res = write_bytes( writer, (uint8_t *)key, strlen(key), BINSON_SIG_STRING_8 ); if (FAILED(res)) return res; #ifdef WITH_BINSON_JSON_OUTPUT if (writer->format == BINSON_WRITER_FORMAT_JSON || writer->format == BINSON_WRITER_FORMAT_JSON_NICE) binson_io_write_str( writer->io, ": ", true ); #endif } return res; }
void llvm_gcda_end_file() { /* Write out EOF record. */ if (output_file) { write_bytes("\0\0\0\0\0\0\0\0", 8); if (new_file) { fwrite(write_buffer, cur_pos, 1, output_file); free(write_buffer); } else { unmap_file(); } fclose(output_file); output_file = NULL; write_buffer = NULL; } free(filename); #ifdef DEBUG_GCDAPROFILING fprintf(stderr, "llvmgcda: -----\n"); #endif }
int main(int argc, char** argv) { #ifdef DEBUG printf("Debug mode\n"); #endif printf("Opening port...\n"); SerialPort maestro; open_serial_port(&maestro, "/dev/ttyACM0"); printf("Port open with fd: %d\n", maestro.file_descriptor); unsigned const char data[] = "012341234"; printf("Sending %d bytes... ", (int)sizeof(data)); bool result; result = write_bytes(&maestro, data, sizeof(data)); if (result) { printf("Success\n"); } else { printf("Failure\n"); } printf("Closing port...\n"); close_serial_port(&maestro); printf("Port closed.\n"); return 0; }
/*--------------------------------------------------------------------------- TITLE : cmd_flash_fw_send_block_multi WORK : ---------------------------------------------------------------------------*/ err_code_t Dialog::cmd_flash_fw_send_block_multi( uint8_t block_count ) { err_code_t err_code = OK; mavlink_message_t tx_msg; mavlink_message_t rx_msg; mavlink_ack_t ack_msg; uint8_t buf[256]; uint8_t tx_buf[16*1024]; uint8_t resp = 0; uint8_t i; uint32_t len; len = 0; for( i=0; i<block_count; i++ ) { mavlink_msg_flash_fw_write_packet_pack(0, 0, &tx_msg, resp, 0, 128, buf); len += mavlink_msg_to_send_buffer(&tx_buf[len], &tx_msg); } write_bytes((char *)tx_buf, len); return err_code; }
void Dialog::msg_send(uint8_t chan, mavlink_message_t *p_msg) { uint8_t buf[1024]; uint16_t len; uint16_t write_len; QString textPrint; ui->led_Tx->turnOn(); len = mavlink_msg_to_send_buffer(buf, p_msg); switch(chan) { case 0: write_len = write_bytes((char *)buf, (uint32_t)len); if( write_len != len ) onTextBoxLogPrint(textPrint.sprintf("wlen %d : len %d\r\n", write_len, len)); break; case 1: break; } //ui->led_Tx->turnOff(); }
struct data_buffer *gen_ldb_control_packet(char *cmd, MEM_POOL *mem_pool) { // make cmd to full command while (strtok(NULL, " ")); struct data_buffer *dbuf = init_data_buffer(); // header write_int32(0, dbuf); write_int8(1, dbuf); write_int8(1, dbuf); write_int16(MT_CD_EXE_LDB_CONTROL, dbuf); write_int32(0, dbuf); //timeout write_int32(0, dbuf); write_int32(strlen(cmd), dbuf); write_bytes((uint8_t *)cmd, strlen(cmd), dbuf); fill_int32(dbuf->data_len, dbuf, 0); return dbuf; }
struct data_buffer* gen_unload_packet(char* cmd,MEM_POOL* mem_pool) { char* table_name = NULL; char* token = NULL; uint32_t tblen; uint16_t sid; if((table_name = strtok(NULL," ")) == NULL) return NULL; tblen = strlen(table_name); if((token = strtok(NULL," ")) == NULL) return NULL; sid = atoi(token); struct data_buffer *dbuf = init_data_buffer(); size_t data_len = tblen + 6 * sizeof(uint8_t) + MESSAGE_HEAD_LEN + 2*sizeof(uint32_t); databuf_resize(dbuf, data_len); //写入数据长度 write_int32(data_len,dbuf); write_int8(1, dbuf); write_int8(1, dbuf); write_int16(MT_CD_EXE_UNLOAD, dbuf); write_int32(0, dbuf); //timeout write_int32(0, dbuf); //拼接协议包 write_int32(tblen,dbuf); write_bytes((uint8_t*)table_name,tblen,dbuf); write_int16(sid,dbuf); return dbuf; }
void FileMapInfo::write_bytes_aligned(const void* buffer, int nbytes) { align_file_position(); write_bytes(buffer, nbytes); align_file_position(); }
static void write_string(const char *s) { uint32_t len = length_of_string(s); write_32bit_value(len); write_bytes(s, strlen(s)); write_bytes("\0\0\0\0", 4 - (strlen(s) % 4)); }