Exemplo n.º 1
0
// Read weights and load into factor graph
long long read_weights(string filename, dd::FactorGraph &fg)
{
	ifstream file;
    file.open(filename.c_str(), ios::in | ios::binary);
    long long count = 0;
    long long id;
    bool isfixed;
    char padding;
    double initial_value;
    while (file.good()) {
    	// read fields
        file.read((char *)&id, 8);
        file.read((char *)&padding, 1);
        if (!file.read((char *)&initial_value, 8)) break;
        // convert endian
        id = bswap_64(id);
        isfixed = padding;
        long long tmp = bswap_64(*(uint64_t *)&initial_value);
        initial_value = *(double *)&tmp;
        // load into factor graph
        fg.weights[fg.c_nweight] = dd::Weight(id, initial_value, isfixed);
		fg.c_nweight++;
		count++;
    }
    file.close();
    return count;
}
Exemplo n.º 2
0
int eMPEGStreamInformation::load(const char *filename)
{
	m_filename = filename;
	if (m_structure_read)
		fclose(m_structure_read);
	m_structure_read = fopen((std::string(m_filename) + ".sc").c_str(), "rb");
	FILE *f = fopen((std::string(m_filename) + ".ap").c_str(), "rb");
	if (!f)
		return -1;
	m_access_points.clear();
	m_pts_to_offset.clear();
	while (1)
	{
		unsigned long long d[2];
		if (fread(d, sizeof(d), 1, f) < 1)
			break;
		
#if BYTE_ORDER == LITTLE_ENDIAN
		d[0] = bswap_64(d[0]);
		d[1] = bswap_64(d[1]);
#endif
		m_access_points[d[0]] = d[1];
		m_pts_to_offset.insert(std::pair<pts_t,off_t>(d[1], d[0]));
	}
	fclose(f);
	fixupDiscontinuties();
	return 0;
}
Exemplo n.º 3
0
static fsal_status_t wire_to_host(struct fsal_export *exp_hdl,
				  fsal_digesttype_t in_type,
				  struct gsh_buffdesc *fh_desc,
				  int flags)
{
	size_t fh_min;
	uint64_t *hashkey;
	ushort *len;

	fh_min = 1;

	if (fh_desc->len < fh_min) {
		LogMajor(COMPONENT_FSAL,
			 "Size mismatch for handle.  should be >= %zu, got %zu",
			 fh_min, fh_desc->len);
		return fsalstat(ERR_FSAL_SERVERFAULT, 0);
	}
	hashkey = (uint64_t *)fh_desc->addr;
	len = (ushort *)((char *)hashkey + sizeof(uint64_t));
	if (flags & FH_FSAL_BIG_ENDIAN) {
#if (BYTE_ORDER != BIG_ENDIAN)
		*len = bswap_16(*len);
		*hashkey = bswap_64(*hashkey);
#endif
	} else {
#if (BYTE_ORDER == BIG_ENDIAN)
		*len = bswap_16(*len);
		*hashkey = bswap_64(*hashkey);
#endif
	}
	return fsalstat(ERR_FSAL_NO_ERROR, 0);
}
Exemplo n.º 4
0
long long read_factors(string filename, dd::FactorGraph &fg)
{
    ifstream file;
    file.open(filename.c_str(), ios::in | ios::binary);
    long long count = 0;
    long long id;
    long long weightid;
    short type;
    long long edge_count;
    while (file.good()) {
        file.read((char *)&id, 8);
        file.read((char *)&weightid, 8);
        file.read((char *)&type, 2);
        if (!file.read((char *)&edge_count, 8)) break;
        id = bswap_64(id);
        weightid = bswap_64(weightid);
        type = bswap_16(type);
        edge_count = bswap_64(edge_count);
        count++;
        fg.factors[fg.c_nfactor] = dd::Factor(id, weightid, type, edge_count);
        fg.c_nfactor ++;
    }
    file.close();
    return count;
}
Exemplo n.º 5
0
int eMPEGStreamInformation::stopSave(void)
{
	if (m_structure_write)
	{
		fclose(m_structure_write);
		m_structure_write = 0;
	}
	if (m_filename == "")
		return -1;
	FILE *f = fopen((m_filename + ".ap").c_str(), "wb");
	if (!f)
		return -1;
	
	for (std::map<off_t, pts_t>::const_iterator i(m_access_points.begin()); i != m_access_points.end(); ++i)
	{
		unsigned long long d[2];
#if BYTE_ORDER == BIG_ENDIAN
		d[0] = i->first;
		d[1] = i->second;
#else
		d[0] = bswap_64(i->first);
		d[1] = bswap_64(i->second);
#endif
		fwrite(d, sizeof(d), 1, f);
	}
	fclose(f);
	
	return 0;
}
Exemplo n.º 6
0
 int64_t bigint::to_int64() const
 {
   FC_ASSERT(BN_num_bits(n) <= 63);
   size_t size = BN_num_bytes(n);
   uint64_t abs_value = 0;
   BN_bn2bin(n, (unsigned char*)&abs_value + (sizeof(uint64_t) - size));
   return BN_is_negative(n) ? -(int64_t)bswap_64(abs_value) : bswap_64(abs_value);
 }
Exemplo n.º 7
0
static void ocfs2_swap_extent_block_header(struct ocfs2_extent_block *eb)
{

	eb->h_suballoc_slot = bswap_16(eb->h_suballoc_slot);
	eb->h_suballoc_bit  = bswap_16(eb->h_suballoc_bit);
	eb->h_fs_generation = bswap_32(eb->h_fs_generation);
	eb->h_blkno         = bswap_64(eb->h_blkno);
	eb->h_next_leaf_blk = bswap_64(eb->h_next_leaf_blk);
}
Exemplo n.º 8
0
void Memcached::dump_extra(const PacketHeader *header, const unsigned char *b, size_t len)
{
  static char extra[1024];
  switch (header->opcode_) {
    case MC_GET:
    case MC_GETQ:
    case MC_GETK:
    case MC_GETKQ:
      snprintf(extra, sizeof(extra),
          "extra: { "
          "flags: 0x%x "
          "}\n",
          bswap_32(*reinterpret_cast<const uint32_t*>(b)));
      break;
    case MC_SET:
    case MC_ADD:
    case MC_REP:
    case MC_SETQ:
    case MC_ADDQ:
    case MC_REPQ:
      snprintf(extra, sizeof(extra),
          "extra: { "
          "flags: 0x%x, "
          "expire: %d "
          "}\n",
          bswap_32(*reinterpret_cast<const uint32_t*>(b)),
          bswap_32(*reinterpret_cast<const int32_t*>(b + 4)));
      break;
    case MC_INC:
    case MC_DEC:
    case MC_INCQ:
    case MC_DECQ:
      snprintf(extra, sizeof(extra),
          "extra: { "
          "delta: %lu, "
          "init: %lu, "
          "expire: %d "
          "}\n",
          bswap_64(*reinterpret_cast<const uint64_t*>(b)),
          bswap_64(*reinterpret_cast<const uint64_t*>(b + 8)),
          bswap_32(*reinterpret_cast<const int32_t*>(b + 12)));
      break;
    case MC_FLUSH:
    case MC_FLUSHQ:
    case MC_TOUCH:
    case MC_GAT:
    case MC_GATQ:
      snprintf(extra, sizeof(extra),
          "extra: { "
          "expire: %d "
          "}\n",
          bswap_32(*reinterpret_cast<const int32_t*>(b)));
      break;
  }
  fprintf(stdout, "%s\n", extra);
}
Exemplo n.º 9
0
static void store_chunk(struct analyze_ctx *analyze,
			const struct scan_chunk_data *chunk_data) {
    int i;
    sha256_ctx sha256;
    unsigned char sha256_digest[32];
    char sha256_digest_string[65];
    int chunk_size = 0;

    sha256_init(&sha256);
    for (i=0; i<2; i++) {
	chunk_size +=  chunk_data[i].size;
	if (chunk_data[i].buf && chunk_data[i].size)
	    sha256_update(&sha256, chunk_data[i].buf, chunk_data[i].size);
    }
    sha256_final(&sha256, sha256_digest);
    sprintf(sha256_digest_string, "%016" PRIx64
	                          "%016" PRIx64
	                          "%016" PRIx64
	                          "%016" PRIx64,
	    bswap_64(*(uint64_t *)&sha256_digest[0]),
	    bswap_64(*(uint64_t *)&sha256_digest[8]),
	    bswap_64(*(uint64_t *)&sha256_digest[16]),
	    bswap_64(*(uint64_t *)&sha256_digest[24]));
    if (sqlite3_bind_text(analyze->db_insert_stmt, 1, sha256_digest_string, -1,
		SQLITE_STATIC) != SQLITE_OK) {
	print_sqlite3_error(analyze->db);
	abort();
    }
    if (sqlite3_bind_int64(analyze->db_insert_stmt, 3, analyze->offset) !=
	    SQLITE_OK) {
	print_sqlite3_error(analyze->db);
	abort();
    }
    if (sqlite3_bind_int(analyze->db_insert_stmt, 4, chunk_size) != SQLITE_OK) {
	print_sqlite3_error(analyze->db);
	abort();
    }
    if (sqlite3_step(analyze->db_insert_stmt) != SQLITE_DONE) {
	print_sqlite3_error(analyze->db);
	abort();
    }
    if (sqlite3_reset(analyze->db_insert_stmt) != SQLITE_OK) {
	print_sqlite3_error(analyze->db);
	abort();
    }
#if 0
    printf("%016lx%016lx%016lx%016lx %d\n",
	    bswap64(*(uint64_t *)&sha256_digest[0]),
	    bswap64(*(uint64_t *)&sha256_digest[8]),
	    bswap64(*(uint64_t *)&sha256_digest[16]),
	    bswap64(*(uint64_t *)&sha256_digest[24]),
	    chunk_size);
#endif

    analyze->offset += chunk_size;
}
Exemplo n.º 10
0
void ocfs2_swap_disk_heartbeat_block(struct o2hb_disk_heartbeat_block *hb)
{
	if (cpu_is_little_endian)
		return;

	hb->hb_seq        = bswap_64(hb->hb_seq);
	hb->hb_cksum      = bswap_32(hb->hb_cksum);
	hb->hb_generation = bswap_64(hb->hb_generation);
	hb->hb_dead_ms    = bswap_32(hb->hb_dead_ms);
}
Exemplo n.º 11
0
static void ocfs2_swap_group_desc_header(struct ocfs2_group_desc *gd)
{
	gd->bg_size = bswap_16(gd->bg_size);
	gd->bg_bits = bswap_16(gd->bg_bits);
	gd->bg_free_bits_count = bswap_16(gd->bg_free_bits_count);
	gd->bg_chain = bswap_16(gd->bg_chain);
	gd->bg_generation = bswap_32(gd->bg_generation);
	gd->bg_next_group = bswap_64(gd->bg_next_group);
	gd->bg_parent_dinode = bswap_64(gd->bg_parent_dinode);
	gd->bg_blkno = bswap_64(gd->bg_blkno);
}
Exemplo n.º 12
0
static
void KColumnIdx1Swap ( KColBlockLoc *buffer, uint32_t count )
{
    uint32_t i;
    for ( i = 0; i < count; ++ i )
    {
        buffer [ i ] . pg = bswap_64 ( buffer [ i ] . pg );
        buffer [ i ] . u . gen = bswap_32 ( buffer [ i ] . u . gen );
        buffer [ i ] . id_range = bswap_32 ( buffer [ i ] . id_range );
        buffer [ i ] . start_id = bswap_64 ( buffer [ i ] . start_id );
    }
}
Exemplo n.º 13
0
Arquivo: if.c Projeto: FairSky/ggnfs
int
write_u64(FILE*ofile,u64_t*buffer,size_t count)
{
size_t i;
int res;

for(i= 0;i<count;i++)
buffer[i]= bswap_64(buffer[i]);
res= fwrite(buffer,sizeof(*buffer),count,ofile);
for(i= 0;i<count;i++)
buffer[i]= bswap_64(buffer[i]);
return res;
}
Exemplo n.º 14
0
void eMPEGStreamInformation::writeStructureEntry(off_t offset, structure_data data)
{
	unsigned long long d[2];
#if BYTE_ORDER == BIG_ENDIAN
	d[0] = offset;
	d[1] = data;
#else
	d[0] = bswap_64(offset);
	d[1] = bswap_64(data);
#endif
	if (m_structure_write)
		fwrite(d, sizeof(d), 1, m_structure_write);
}
Exemplo n.º 15
0
void ocfs2_swap_group_desc(struct ocfs2_group_desc *gd)
{
	if (cpu_is_little_endian)
		return;

	gd->bg_size = bswap_16(gd->bg_size);
	gd->bg_bits = bswap_16(gd->bg_bits);
	gd->bg_free_bits_count = bswap_16(gd->bg_free_bits_count);
	gd->bg_chain = bswap_16(gd->bg_chain);
	gd->bg_generation = bswap_32(gd->bg_generation);
	gd->bg_next_group = bswap_64(gd->bg_next_group);
	gd->bg_parent_dinode = bswap_64(gd->bg_parent_dinode);
	gd->bg_blkno = bswap_64(gd->bg_blkno);
}
Exemplo n.º 16
0
long long read_edges(string filename, dd::FactorGraph &fg)
{
    ifstream file;
    file.open(filename.c_str(), ios::in | ios::binary);
    long long count = 0;
    long long variable_id;
    long long factor_id;
    long long position;
    bool ispositive;
    char padding;
    long long equal_predicate;
    while (file.good()) {
        // read fields
        file.read((char *)&variable_id, 8);
        file.read((char *)&factor_id, 8);
        file.read((char *)&position, 8);
        file.read((char *)&padding, 1);
        if (!file.read((char *)&equal_predicate, 8)) break;
        variable_id = bswap_64(variable_id);
        factor_id = bswap_64(factor_id);
        position = bswap_64(position);
        ispositive = padding;
        equal_predicate = bswap_64(equal_predicate);
        count++;

        // wrong id
    	if(variable_id >= fg.n_var || variable_id < 0){
    	  assert(false);
    	}

    	if(factor_id >= fg.n_factor || factor_id < 0){
    	  std::cout << "wrong fid = " << factor_id << std::endl;
    	  assert(false);
        }

        // add variables to factors
        if (fg.variables[variable_id].domain_type == DTYPE_BOOLEAN) {
            fg.factors[factor_id].tmp_variables.push_back(
                dd::VariableInFactor(variable_id, fg.variables[variable_id].upper_bound, variable_id, position, ispositive));
        } else {
            fg.factors[factor_id].tmp_variables.push_back(
                dd::VariableInFactor(variable_id, position, ispositive, equal_predicate));
        }
        fg.variables[variable_id].tmp_factor_ids.push_back(factor_id);

    }
    file.close();
    return count;   
}
Exemplo n.º 17
0
void Memcached::dump_body(const PacketHeader *header,
                                const unsigned char *b, size_t len)
{
  if (header->ext_len_ > 0) {
    if (len < header->ext_len_) return ;
    dump_extra(header, b, len);
  }
  b += header->ext_len_;
  len -= header->ext_len_;

  if (header->key_len_ > 0) {
    if (len < header->key_len_) return ;
    dump_kv("key", b, header->key_len_);
  }
  b += header->key_len_;
  len -= header->key_len_;
  
  size_t val_size = header->total_len_ - header->key_len_ - header->ext_len_;
  if (val_size > 0) {
    if (len < val_size) return ;
    if (header->opcode_ == MC_INC
        || header->opcode_ == MC_DEC
        || header->opcode_ == MC_INCQ
        || header->opcode_ == MC_DECQ) {
      fprintf(stdout,
          "value: { "
          "result: %lu "
          "}\n",
          bswap_64(*reinterpret_cast<const uint64_t*>(b)));
    } else {
      dump_kv("value", b, val_size);
    }
  }
}
Exemplo n.º 18
0
uint64_t elf_access_unsigned(struct elf_binary * elf, elf_ptrval base,
                             uint64_t moreoffset, size_t size)
{
    elf_ptrval ptrval = base + moreoffset;
    bool need_swap = elf_swap(elf);
    const uint8_t *u8;
    const uint16_t *u16;
    const uint32_t *u32;
    const uint64_t *u64;

    if ( !elf_access_ok(elf, ptrval, size) )
        return 0;

    switch ( size )
    {
    case 1:
        u8 = (const void*)ptrval;
        return *u8;
    case 2:
        u16 = (const void*)ptrval;
        return need_swap ? bswap_16(*u16) : *u16;
    case 4:
        u32 = (const void*)ptrval;
        return need_swap ? bswap_32(*u32) : *u32;
    case 8:
        u64 = (const void*)ptrval;
        return need_swap ? bswap_64(*u64) : *u64;
    default:
        return 0;
    }
}
Exemplo n.º 19
0
/* 
 * read position of index, which is stored in the last 8 bytes of the file 
 * byte order of the number is big endian
 */
uint64_t libmaus::huffman::IndexLoaderBase::getIndexPos(std::string const & filename)
{
	::libmaus::aio::CheckedInputStream indexistr(filename);

	// read position of index (last 8 bytes of file)
	// and convert byte order if necessary
	indexistr.seekg(-8,std::ios::end);
	uint64_t v;
	indexistr.read( reinterpret_cast<char *>(&v) , 8 );
	
	#if defined(LIBMAUS_BYTE_ORDER_LITTLE_ENDIAN)
	#if defined(_WIN32)
	uint64_t const indexpos = _byteswap_uint64(v);
	#elif defined(__FreeBSD__)
	uint64_t const indexpos = bswap64(v);
	#elif defined(__linux__)
	uint64_t const indexpos = bswap_64(v);
	#else
	uint64_t const indexpos = ::libmaus::util::ReverseByteOrder::reverseByteOrder<uint64_t>(v);
	#endif
	#else
	uint64_t const indexpos = v;

	#endif
	// std::cerr << "Index at position " << indexpos << " file length " << ::libmaus::util::GetFileSize::getFileSize(filename) << std::endl;

	return indexpos;
}
Exemplo n.º 20
0
size_t EndianStream::writeUInt64(uint64_t value) {
	debug_assertp(_stream!=NULL, this, "Wrapped stream cannot be NULL");
	if (_order!=DUCT_BYTEORDER) {
		value=bswap_64(value);
	}
	return _stream->writeUInt64(value);
}
Exemplo n.º 21
0
static inline double __bswap_d(double x)
{
    union { double d; uint64_t i; } u_double = { x };
    u_double.i = bswap_64(u_double.i);

    return u_double.d;
}
Exemplo n.º 22
0
END_TEST

START_TEST (test_bswap_64)
{
  struct
  {
    uint64_t in;
    uint64_t ref;
    uint64_t out;
  } vector [] =
      {
        { 0x123456789ABCDEF1LL, 0xF1DEBC9A78563412LL, 0 },
        { 0x0, 0x0, 0 },
        { 0xFFFFFFFFFFFFFFFFLL, 0xFFFFFFFFFFFFFFFFLL, 0 },
        { 0xFFFF, 0xFFFF000000000000LL, 0 },
        { 0x8000, 0x0080000000000000LL, 0 },
        { 0x8000, 0x0080000000000000LL, 0 },
        { 0x0000000080000000LL, 0x0000008000000000LL, 0 }
      };
  int i = 0;
  for (i = 0; i < LENGTH(vector); i++)
    {
      vector[i].out = bswap_64(vector[i].in);
      fail_unless (vector[i].out == vector[i].ref);
    }
}
Exemplo n.º 23
0
Arquivo: sstrip.c Projeto: deater/tb1
/* commitchanges() writes the new headers back to the original file
 * and sets the file to its new size.
 */
static int commitchanges(int fd, Elf_Ehdr const *ehdr, Elf_Phdr *phdrs,
			 unsigned long newsize)
{
    size_t	n;
    unsigned long e_phoff;
    short e_phnum;
   
    e_phnum=ehdr->e_phnum;
    if (swap_bytes) e_phnum=bswap_16(e_phnum);
   
    e_phoff=ehdr->e_phoff;
#if (ELF_CLASS==ELFCLASS64)
        if (swap_bytes) {
           e_phoff=bswap_64(e_phoff);
        }
#else
        if (swap_bytes) {
           e_phoff=bswap_32(e_phoff);
        }
#endif       	   
   
   
    /* Save the changes to the ELF header, if any.
     */
    if (lseek(fd, 0, SEEK_SET))
	return ferr("could not rewind file");
    errno = 0;
    if (write(fd, ehdr, sizeof *ehdr) != sizeof *ehdr)
	return err("could not modify file");

    /* Save the changes to the program segment header table, if any.
     */
    if (lseek(fd, e_phoff, SEEK_SET) == (off_t)-1) {
	err("could not seek in file.");
	goto warning;
    }
    n = e_phnum * sizeof *phdrs;
    if (write(fd, phdrs, n) != (ssize_t)n) {
	err("could not write to file");
	goto warning;
    }

    /* Eleventh-hour sanity check: don't truncate before the end of
     * the program segment header table.
     */
    if (newsize < e_phoff + n)
	newsize = e_phoff + n;

    /* Chop off the end of the file.
     */
    if (ftruncate(fd, newsize)) {
	err("could not resize file");
	goto warning;
    }

    return 1;

  warning:
    return err("ELF file may have been corrupted!");
}
Exemplo n.º 24
0
static long htonl64(long x) {
#if BYTE_ORDER == LITTLE_ENDIAN
    return bswap_64(x);
#else
#error
#endif
}
Exemplo n.º 25
0
static __inline__ uint64_t byteswap64_to_host(uint64_t value)
{
	if (byteswap == 1) {
		return (bswap_64(value));
	} else {
		return (value);
	}
}
Exemplo n.º 26
0
uint64_t EndianStream::readUInt64() {
	debug_assertp(_stream!=NULL, this, "Wrapped stream cannot be NULL");
	uint64_t v=_stream->readUInt64();
	if (_order!=DUCT_BYTEORDER) {
		v=bswap_64(v);
	}
	return v;
}
Exemplo n.º 27
0
static bool read64(FILE *input, uint64_t * ret)
{
  union { uint64_t v; char bytes[8]; } u;
  size_t l = fread(u.bytes,sizeof(char),8,input);
  if( l != 8 || feof(input) ) return false;
  *ret = bswap_64(u.v);
  return true;
}
Exemplo n.º 28
0
const void *
Buffer::read_64() const throw(KafkaError) {
    //const void *mem = get(8);
    //bswap_64(*reinterpret_cast<const uint64_t *>(mem));
    //return mem;
    uint64_t *mem = const_cast<uint64_t *>(reinterpret_cast<const uint64_t *>(get(8)));
    *mem = bswap_64(*mem);
    return mem;
}
Exemplo n.º 29
0
//-----------------------------------------------------------------------------
CRplMacAddress::CRplMacAddress( const unsigned char *p_pucMacAddress )
{
	// parameter comes in big-endian order
	memcpy( &m_ullMac, p_pucMacAddress, sizeof( unsigned long long ) );

#if defined(BYTE_ORDER) && (BYTE_ORDER == LITTLE_ENDIAN)
	m_ullMac = bswap_64( m_ullMac );
#endif
}
Exemplo n.º 30
0
void mem_bswap_64(void *src, int byte_size)
{
	u64 *m = src;

	while (byte_size > 0) {
		*m = bswap_64(*m);
		byte_size -= sizeof(u64);
		++m;
	}
}