// Write a value in a given register int sh_reg_write(int argc, char ** argv) { uint16_t reg; uint16_t val; int ok; unsigned int * p; cprintf("write\n\r"); if (argc < 3) { cprintf("error: missing argument\r\n"); cprintf("correct usage:\r\n"); cprintf("\t%s REGISTER VALUE\r\n", argv[0]); return -1; } reg = read_uint16(argv[1], &ok); val = read_uint16(argv[2], &ok); cprintf("r=%d v=%d\r\n", reg, val); p = (unsigned int *)reg; *p = val; return 0; }
void Squeezer_file_header_t::read_from_file(FILE * in) { file_type_mark[0] = read_uint8(in); file_type_mark[1] = read_uint8(in); file_type_mark[2] = read_uint8(in); file_type_mark[3] = read_uint8(in); floating_point_check = read_double(in); date_year = read_uint16(in); date_month = read_uint8(in); date_day = read_uint8(in); time_hour = read_uint8(in); time_minute = read_uint8(in); time_second = read_uint8(in); radiometer.horn = read_uint8(in); radiometer.arm = read_uint8(in); od = read_uint16(in); first_obt = read_double(in); last_obt = read_double(in); first_scet_in_ms = read_double(in); last_scet_in_ms = read_double(in); number_of_chunks = read_uint32(in); }
static void parse_destination_down_message(int s, const uint8_t* data_items, uint16_t len) { const uint8_t* data_item = data_items; printf("Received Destination Down message from modem:\n"); /* The message has been validated so just scan for the relevant data_items */ while (data_item < data_items + len) { /* Octets 0 and 1 are the data item type */ enum dlep_data_item item_id = read_uint16(data_item); /* Octets 2 and 3 are the data item length */ uint16_t item_len = read_uint16(data_item + 2); /* Increment data_item to point to the data */ data_item += 4; switch (item_id) { case DLEP_MAC_ADDRESS_DATA_ITEM: printf(" MAC Address: %02X:%02X:%02X:%02X:%02X:%02X\n",data_item[0],data_item[1],data_item[2],data_item[3],data_item[4],data_item[5]); send_destination_down_resp(s,data_item,DLEP_SC_SUCCESS); break; default: /* Others will be caught by the check function */ break; } /* Increment data_item to point to the next data item */ data_item += item_len; } }
int iff_parse_rsmp(IFFChunk * ChunkInfo, const uint8_t * Buffer){ IFFResourceMap *Map; bytestream b; unsigned i; if(ChunkInfo->Size < 20) return 0; set_bytestream(&b, Buffer, ChunkInfo->Size); ChunkInfo->FormattedData = calloc(1, sizeof(IFFResourceMap)); if(ChunkInfo->FormattedData == NULL) return 0; Map = ChunkInfo->FormattedData; Map->Reserved = read_uint32(&b); Map->Version = read_uint32(&b); memcpy(Map->MagicNumber, b.Buffer, 4); skipbytes(&b, 4); Map->IFFSize = read_uint32(&b); Map->TypeCount = read_uint32(&b); if(Map->Reserved != 0 || Map->Version > 1) return 0; Map->ResourceTypes = calloc(Map->TypeCount, sizeof(IFFResourceType)); if(Map->ResourceTypes == NULL) return 0; for(i=0; i<Map->TypeCount; i++){ IFFResourceType * Type = &Map->ResourceTypes[i]; unsigned j; if(b.Size < 8) return 0; memcpy(Type->Type, b.Buffer, 4); skipbytes(&b, 4); Type->ResourceCount = read_uint32(&b); Type->Resources = calloc(Type->ResourceCount, sizeof(IFFResource)); if(Type->Resources == NULL) return 0; for(j=0; j<Type->ResourceCount; j++){ IFFResource * Resource = &Type->Resources[j]; if(b.Size < ((Map->Version == 0) ? 9 : 11)) return 0; Resource->Offset = read_uint32(&b); Resource->ChunkID = (Map->Version == 0) ? read_uint16(&b) : read_uint32(&b); Resource->Flags = read_uint16(&b); if(Map->Version == 0){ if(!read_c_string(&b, &Resource->Label)) return 0; }else{ if(!read_pascal_string(&b, &Resource->Label)) return 0; } } } return 1; }
static int rdata_ilnp64_to_string(buffer_type *output, rdata_atom_type rdata, rr_type* ATTR_UNUSED(rr)) { uint8_t* data = rdata_atom_data(rdata); uint16_t a1 = read_uint16(data); uint16_t a2 = read_uint16(data+2); uint16_t a3 = read_uint16(data+4); uint16_t a4 = read_uint16(data+6); buffer_printf(output, "%.4x:%.4x:%.4x:%.4x", a1, a2, a3, a4); return 1; }
// Read and display the value of a given register int sh_reg_read(int argc, char ** argv) { uint16_t reg; uint16_t val; int ok; unsigned int * p; if (argc < 2) { cprintf("error: missing argument\r\n"); cprintf("correct usage:\r\n"); cprintf("\t%s REGISTER\r\n", argv[0]); return -1; } cprintf("read\n\r"); reg = read_uint16(argv[1], &ok); p = (unsigned int *)reg; val = *p; cprintf("r=%d v=%d\r\n", reg, val); return 0; }
int sh_speed(int argc, char ** argv) { uint16_t timeout = 0; int left; int right; int ok; if (argc < 3) { cprintf("correct usage:\r\n"); cprintf("\t%c [o] LEFT RIGHT <TIMEOUT>\r\n", argv[0]); return -1; } if (argc > 3) { timeout = read_uint16(argv[3], &ok); timer_start_cb(timer_id, timeout, 1); } left = read_int16(argv[1], &ok); right = read_int16(argv[2], &ok); cprintf("%d %d\r\n",left, right); // speed_start(speed_l); // speed_start(speed_r); speed_setPoint(speed_l, left); speed_setPoint(speed_r, right); return 0; }
static uint16_t SPICE_GNUC_UNUSED consume_uint16(uint8_t **ptr) { uint16_t val; val = read_uint16(*ptr); *ptr += 2; return val; }
/** * Reads a variable length integer. */ std::uint64_t read_var_int() { std::uint64_t ret = 0; std::uint8_t size = read_uint8(); if (size < 253) { ret = size; } else if (size == 253) { ret = read_uint16(); } else if (size == 254) { ret = read_uint32(); } else if (size == 255) { ret = read_uint64(); } else { assert(0); } return ret; }
static int send_rtp(const uint8_t *pkt, size_t len, void *arg){ struct sync_state* pss = (struct sync_state*)arg; pthread_mutex_lock(&pss->mutex); pss->event = true; pss->pt = read_uint8(&pkt[1]); uint16_t seq = read_uint16(&pkt[2]); uint32_t timestamp = read_uint32(&pkt[4]); pss->ssrc = read_uint32(&pkt[8]); pss->seq_diff = seq - pss->prev_seq; if(pss->seq_diff < 0) { pss->seq_diff += 0xffff; } pss->prev_seq = seq; pss->timestamp_diff = timestamp - pss->prev_timestamp; if(pss->timestamp_diff < 0) { pss->timestamp_diff += 0xfffffff; } pss->prev_timestamp = timestamp; memcpy(pss->packet_buf, pkt, len); pss->nbytes = len; pthread_cond_signal(&pss->cond); pthread_mutex_unlock(&pss->mutex); return 0; }
static gboolean read_old_length (const guchar **at, const guchar *end, guchar ctb, gsize *pkt_len) { gsize llen = ctb & 0x03; guint16 v16; guint32 v32; guint8 c; if (llen == 0) { if (!read_byte (at, end, &c)) return FALSE; *pkt_len = c; } else if (llen == 1) { if (!read_uint16 (at, end, &v16)) return FALSE; *pkt_len = v16; } else if (llen == 2) { if (!read_uint32 (at, end, &v32)) return FALSE; *pkt_len = v32; } else { *pkt_len = end - *at; } return TRUE; }
static gboolean read_mpi (const guchar **at, const guchar *end, guint16 *bits, guchar **value) { gsize bytes; guint16 b; g_assert (at); if (!bits) bits = &b; if (!read_uint16 (at, end, bits)) return FALSE; bytes = (*bits + 7) / 8; if (bytes == 0) return FALSE; if (value) *value = g_malloc (bytes); if (!read_bytes (at, end, value ? *value : NULL, bytes)) { if (value) g_free (*value); return FALSE; } return TRUE; }
void read_uint16_n(const char *id, uint16_t* buf, int buf_len) { int i; for(i = 0; i < buf_len; i++) { buf[i] = read_uint16((char*) id); } }
static int rdata_short_to_string(buffer_type *output, rdata_atom_type rdata, rr_type* ATTR_UNUSED(rr)) { uint16_t data = read_uint16(rdata_atom_data(rdata)); buffer_printf(output, "%lu", (unsigned long) data); return 1; }
static int rdata_rrtype_to_string(buffer_type *output, rdata_atom_type rdata, rr_type* ATTR_UNUSED(rr)) { uint16_t type = read_uint16(rdata_atom_data(rdata)); buffer_printf(output, "%s", rrtype_to_string(type)); return 1; }
//--------------------------------------------------------------------------------------------- void BitMessage::read_netaddr(os::NetAddress* addr) const { for (int i = 0; i < 4; i++) { addr->m_address[i] = read_uint8(); } addr->m_port = read_uint16(); }
static jl_fptr_t jl_deserialize_fptr(ios_t *s) { int fptr = read_uint16(s); if (fptr == 0) return NULL; void **pbp = ptrhash_bp(&id_to_fptr, (void*)(ptrint_t)fptr); if (*pbp == HT_NOTFOUND) jl_error("unknown function pointer ID"); return *(jl_fptr_t*)pbp; }
char* read_len_string(FILE *stream) { uint16_t len; if (!read_uint16(stream, &len)) { return NULL; } return read_string(stream, len); }
int main(int argc, char* argv[]) { FILE* ofile1; FILE* ofile2; char ostr[17]; uint16_t N[ORDER]; #ifdef SW init_pipe_handler(); PTHREAD_DECL(gcd_daemon); PTHREAD_CREATE(gcd_daemon); #endif ofile1 = fopen("gcd_inputs.txt","w"); ofile2 = fopen("gcd_result.txt","w"); int err = 0; uint16_t S = 1; if(argc > 1) S = atoi(argv[1]); fprintf(stderr,"Scale-factor = %d.\n",S); srand48(19); uint16_t i; for(i = 0; i < ORDER; i++) { N[i] = ((uint16_t) (0x0fff * drand48())) * S; fprintf(stderr,"N[%d] = %d\n", i, N[i]); write_uint16("in_data",N[i]); to_string(ostr,N[i]); fprintf(ofile1,"%s\n", ostr); } uint16_t g = read_uint16("out_data"); fprintf(stderr,"GCD = %d.\n",g); to_string(ostr,g); fprintf(ofile2,"%s\n",ostr); uint32_t et = read_uint32("elapsed_time"); fprintf(stderr,"Elapsed time = %u.\n", et); fclose(ofile1); fclose(ofile2); #ifdef SW PTHREAD_CANCEL(gcd_daemon); close_pipe_handler(); #endif return(0); }
void unpack_question(char *buf, dns_question_t* question) { char *qdata = buf; question->label = malloc(sizeof(dns_labels_t)); dns_labels_t *elem = question->label; dns_labels_t *last = NULL; size_t label_len; while ((label_len = *qdata++) != 0) { elem->label = malloc(label_len); memcpy(elem->label, qdata, label_len); qdata += label_len; last = elem; elem->next = malloc(sizeof(dns_labels_t)); elem = elem->next; } free(last->next); last->next = NULL; qdata = read_uint16(qdata, &question->qtype, true); qdata = read_uint16(qdata, &question->qclass, true); }
static int rdata_certificate_type_to_string(buffer_type *output, rdata_atom_type rdata, rr_type* ATTR_UNUSED(rr)) { uint16_t id = read_uint16(rdata_atom_data(rdata)); lookup_table_type *type = lookup_by_id(dns_certificate_types, id); if (type) { buffer_printf(output, "%s", type->name); } else { buffer_printf(output, "%u", (unsigned) id); } return 1; }
// returns list of hashtables of (int -> struct _openslide_tiffdump_item) GSList *_openslide_tiffdump_create(FILE *f) { // read and check magic uint16_t magic; fseeko(f, 0, SEEK_SET); if (fread(&magic, sizeof magic, 1, f) != 1) { return NULL; } if (magic != TIFF_BIGENDIAN && magic != TIFF_LITTLEENDIAN) { return NULL; } // g_debug("magic: %d", magic); int32_t version = read_uint16(f, magic); int64_t diroff = read_uint32(f, magic); // g_debug("version: %d", version); if (version != TIFF_VERSION) { return NULL; } // initialize loop detector GHashTable *loop_detector = g_hash_table_new_full(_openslide_int64_hash, _openslide_int64_equal, _openslide_int64_free, NULL); // read all the directories GSList *result = NULL; while (diroff != 0) { // read a directory GHashTable *ht = read_directory(f, &diroff, loop_detector, magic); // was the directory successfully read? if (ht == NULL) { // no, so destroy everything _openslide_tiffdump_destroy(result); result = NULL; break; } // add result to list result = g_slist_prepend(result, ht); } g_hash_table_unref(loop_detector); return g_slist_reverse(result); }
int main() { int idx = 5; pthread_t run_t; pthread_create(&run_t,NULL,&run_,NULL); while(idx >= 0) { fprintf(stdout,"Program output=%d\n", read_uint16("out_port")); idx--; } pthread_join(run_t,NULL); return(0); }
int test_enc () { char buf[1024]; char *buf_ptr = (char *) &buf; char *res_enc = NULL; char *res_dec = NULL; uint8_t v8; res_enc = write_uint8(buf_ptr, 42); res_dec = read_uint8(buf_ptr, &v8); assert(buf_ptr - res_dec == buf_ptr - res_enc); assert(v8 == 42); uint16_t v16; res_enc = write_uint16(buf_ptr, 4200); res_dec = read_uint16(buf_ptr, &v16); assert(buf_ptr - res_dec == buf_ptr - res_enc); assert(v16 == 4200); uint32_t v32; res_enc = write_uint32(buf_ptr, 420000); res_dec = read_uint32(buf_ptr, &v32); assert(buf_ptr - res_dec == buf_ptr - res_enc); assert(v32 == 420000); int32_t i32; res_enc = write_int32(buf_ptr, -420000); res_dec = read_int32(buf_ptr, &i32); assert(buf_ptr - res_dec == buf_ptr - res_enc); assert(i32 == -420000); char* str = "hello world"; uint16_t str_len; res_enc = write_string(buf_ptr, str, strlen(str)); res_dec = read_string(buf_ptr, &str, &str_len); assert(buf_ptr - res_dec == buf_ptr - res_enc); assert(!strcmp(str, "hello world")); assert(strlen(str) == str_len); char *tmp = "1234512345"; res_enc = write_buffer(buf_ptr, tmp, 10); assert(res_enc - buf_ptr == 10); for (int i = 0; i < 10; i++) assert(*(buf + i) == *(tmp + i)); return 0; }
/** * Reads a network address. * @param prefix_timestamp If false the timestamp will be omitted. */ protocol::network_address_t read_network_address( const bool & prefix_version, const bool & prefix_timestamp ) { protocol::network_address_t ret; if (prefix_version) { /** * Read the version. */ ret.version = read_uint32(); } if (prefix_timestamp) { /** * Read the timestamp. */ ret.timestamp = read_uint32(); } /** * Read the services. */ ret.services = read_uint64(); /** * Read the address. */ read_bytes( reinterpret_cast<char *>(&ret.address[0]), ret.address.size() ); /** * Read the port. */ ret.port = ntohs(read_uint16()); return ret; }
box_t *box_read(const unsigned char *buf, const long size) { box_t *box; if (size < 8) return NULL; box = malloc(sizeof(*box)); if (box) { box->length = read_uint32(buf); memcpy(box->type, buf + 4, 4); box->type[4] = '\0'; box->data = buf + 8; box->size = MIN(box->length, size); box->rez = NULL; box->next = NULL; if (!memcmp(box->type, "ftyp", 4)) { /* File type box */ box_ftyp_t *ftyp = malloc(sizeof(*ftyp)); if (ftyp) { memcpy(ftyp->brand, box->data, 4); ftyp->brand[4] = '\0'; ftyp->version = read_uint32(box->data + 4); } box->rez = ftyp; } else if (!memcmp(box->type, "ihdr", 4)) { /* image header box */ box_ihdr_t *ihdr = malloc(sizeof(*ihdr)); if (ihdr) { ihdr->height = read_uint32(box->data); ihdr->width = read_uint32(box->data + 4); ihdr->nc = read_uint16(box->data + 8); ihdr->bpc = (box->data[10] & 0x7f) + 1; ihdr->sign = box->data[10] >> 7; ihdr->C = box->data[11]; ihdr->UnkC = box->data[12]; ihdr->IPR = box->data[13]; } box->rez = ihdr; } }
static ssize_t recv_message(int s, uint8_t** msg) { ssize_t received = -1; /* Make sure we have room for the header */ uint8_t* new_msg = realloc(*msg,4); if (!new_msg) { printf("Failed to allocate message buffer"); return -1; } *msg = new_msg; /* Receive the header */ received = recv(s,*msg,4,0); if (received == 4) { /* Read the message length */ uint16_t reported_len = read_uint16((*msg)+2); if (reported_len) { /* Make more room in the message buffer */ new_msg = realloc(*msg,reported_len+4); if (!new_msg) { printf("Failed to allocate message buffer"); return -1; } *msg = new_msg; /* Receive the rest of the message */ received = recv(s,(*msg)+4,reported_len,0); if (received == -1) return -1; /* Include the header length in the return value */ received += 4; } } return received; }
static gboolean parse_v3_signature (const guchar **at, const guchar *end, GcrOpenpgpParseFlags flags, GPtrArray *records) { guchar keyid[8]; guint8 sig_type; guint8 sig_len; guint32 sig_time; guint8 key_algo; guint8 hash_algo; guint16 left_bits; GcrRecord *record; gchar *value; if (!read_byte (at, end, &sig_len) || sig_len != 5) return FALSE; if (!read_byte (at, end, &sig_type) || !read_uint32 (at, end, &sig_time) || !read_bytes (at, end, keyid, 8) || !read_byte (at, end, &key_algo) || !read_byte (at, end, &hash_algo) || !read_uint16 (at, end, &left_bits) || !skip_signature_mpis (at, end, key_algo)) return FALSE; if (flags & GCR_OPENPGP_PARSE_SIGNATURES) { record = _gcr_record_new (GCR_RECORD_SCHEMA_SIG, GCR_RECORD_SIG_MAX, ':'); _gcr_record_set_uint (record, GCR_RECORD_SIG_ALGO, key_algo); value = egg_hex_encode_full (keyid, sizeof (keyid), TRUE, 0, 0); _gcr_record_take_raw (record, GCR_RECORD_SIG_KEYID, value); _gcr_record_set_ulong (record, GCR_RECORD_SIG_TIMESTAMP, sig_time); value = g_strdup_printf ("%02xx", (guint)sig_type); _gcr_record_take_raw (record, GCR_RECORD_SIG_CLASS, value); g_ptr_array_add (records, record); } return TRUE; }
void unpack_header(char *buf, dns_header_t *header) { size_t header_len = sizeof(dns_header_t); char *buf_ = malloc(header_len); memcpy((void *)buf_, buf, header_len); #ifdef DEBUG puts("input header"); dump_buffer(buf_, header_len); #endif buf_ = read_uint16(buf_, &header->id, true); buf_ = read_uint16(buf_, (unsigned short *)&header->flags, true); buf_ = read_uint16(buf_, &header->qdcount, true); buf_ = read_uint16(buf_, &header->ancount, true); buf_ = read_uint16(buf_, &header->nscount, true); buf_ = read_uint16(buf_, &header->arcount, true); }
static void test_decompress( FILE* outFp, FILE* inpFp, size_t messageMaxBytes, size_t ringBufferBytes) { LZ4_streamDecode_t* const lz4StreamDecode = LZ4_createStreamDecode(); char* const cmpBuf = (char*) malloc(LZ4_COMPRESSBOUND(messageMaxBytes)); char* const decBuf = (char*) malloc(ringBufferBytes); int decOffset = 0; for ( ; ; ) { uint16_t cmpBytes = 0; if (read_uint16(inpFp, &cmpBytes) != 1) break; if (cmpBytes <= 0) break; if (read_bin(inpFp, cmpBuf, cmpBytes) != cmpBytes) break; { char* const decPtr = &decBuf[decOffset]; const int decBytes = LZ4_decompress_safe_continue( lz4StreamDecode, cmpBuf, decPtr, cmpBytes, (int) messageMaxBytes); if (decBytes <= 0) break; write_bin(outFp, decPtr, decBytes); // Add and wraparound the ringbuffer offset decOffset += decBytes; if ((size_t)decOffset >= ringBufferBytes - messageMaxBytes) decOffset = 0; } } free(decBuf); free(cmpBuf); LZ4_freeStreamDecode(lz4StreamDecode); }