コード例 #1
0
ファイル: export.c プロジェクト: hongjil5/nfs-ganesha
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);
}
コード例 #2
0
ファイル: args.c プロジェクト: GaloisInc/camkes-tool
void ffiget_arg_length(unsigned char *c, long clen, unsigned char *a, long alen) {
    uint16_t arg;
    memcpy(&arg, a, sizeof(arg));
    arg = bswap_16(arg);
    uint16_t len_result = bswap_16(strlen(argv[arg]));
    memcpy(a, &len_result, sizeof(len_result));
}
コード例 #3
0
ファイル: pci_powerpc.c プロジェクト: OS2World/MM-SOUND-xine
static int pci_get_vendor(
	  unsigned char bus,
	  unsigned char dev,
	  int func)
{
    int retval;
    char path[100];
    int fd;
    short vendor, device;
    sprintf(path,"/proc/bus/pci/%02d/%02x.0", bus, dev);
    fd = open(path,O_RDONLY|O_SYNC);
    if (fd == -1) {
	    retval=0xFFFF;
    }
    else if (pread(fd, &vendor, 2, PCI_VENDOR_ID) == 2 &&
             pread(fd, &device, 2, PCI_DEVICE_ID) == 2) {
	    vendor = bswap_16(vendor);
	    device = bswap_16(device);
	    retval = vendor + (device<<16); /*no worries about byte order, 
	    				      all ppc are bigendian*/
    } else {
	    retval = 0xFFFF;
    }   
    if (fd > 0) {
	    close(fd);
    }
    return retval;
}
int check_elf_header(Elf32_Ehdr *const ehdr)
{
	if (! ehdr || strncmp((void *)ehdr, ELFMAG, SELFMAG) != 0 ||  
			ehdr->e_ident[EI_CLASS] != ELFCLASS32 ||
			ehdr->e_ident[EI_VERSION] != EV_CURRENT) 
	{
		return 1;
	}

	/* Check if the target endianness matches the host's endianness */
	byteswap = 0;
#if __BYTE_ORDER == __LITTLE_ENDIAN
	if (ehdr->e_ident[5] == ELFDATA2MSB) {
		/* Ick -- we will have to byte-swap everything */
		byteswap = 1;
	}
#elif __BYTE_ORDER == __BIG_ENDIAN
	if (ehdr->e_ident[5] == ELFDATA2LSB) {
		byteswap = 1;
	}
#else
#error Unknown host byte order!
#endif
	/* Be vary lazy, and only byteswap the stuff we use */
	if (byteswap==1) {
		ehdr->e_type=bswap_16(ehdr->e_type);
		ehdr->e_machine=bswap_16(ehdr->e_machine);
		ehdr->e_phoff=bswap_32(ehdr->e_phoff);
		ehdr->e_shoff=bswap_32(ehdr->e_shoff);
		ehdr->e_phnum=bswap_16(ehdr->e_phnum);
		ehdr->e_shnum=bswap_16(ehdr->e_shnum);
	}
	return 0;
}
コード例 #5
0
ファイル: squashfs.c プロジェクト: OPSF/uClinux
int volume_id_probe_squashfs(struct volume_id *id, uint64_t off, uint64_t size)
{
	struct squashfs_super *sqs;

	info("probing at offset 0x%" PRIx64 "\n", off);

	sqs = (struct squashfs_super *) volume_id_get_buffer(id, off, 0x200);
	if (sqs == NULL)
		return -1;

	if (sqs->s_magic == SQUASHFS_MAGIC || sqs->s_magic == SQUASHFS_MAGIC_LZMA) {
		snprintf(id->type_version, sizeof(id->type_version), "%u.%u",
			 sqs->s_major, sqs->s_minor);
		goto found;
	}
	if (sqs->s_magic == bswap_32(SQUASHFS_MAGIC) || sqs->s_magic == bswap_32(SQUASHFS_MAGIC_LZMA)) {
		snprintf(id->type_version, sizeof(id->type_version), "%u.%u",
			 bswap_16(sqs->s_major), bswap_16(sqs->s_minor));
		goto found;
	}

	return -1;

found:
	volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);
	id->type = "squashfs";
	return 0;
}
コード例 #6
0
ファイル: deepstarcomponent.cpp プロジェクト: KDE/kstars
void DeepStarComponent::byteSwap( deepStarData *stardata ) {
    stardata->RA = bswap_32( stardata->RA );
    stardata->Dec = bswap_32( stardata->Dec );
    stardata->dRA = bswap_16( stardata->dRA );
    stardata->dDec = bswap_16( stardata->dDec );
    stardata->B = bswap_16( stardata->B );
    stardata->V = bswap_16( stardata->V );
}
コード例 #7
0
ファイル: extents.c プロジェクト: ystk/debian-ocfs2-tools
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);
}
コード例 #8
0
ファイル: lynx.c プロジェクト: GunioRobot/quickdev16
int
lynx_init (st_rominfo_t *rominfo)
{
  int result = -1;

  rominfo->console_usage = lynx_usage[0].help;
  rominfo->copier_usage = unknown_usage[0].help;

  ucon64_fread (&lnx_header, 0, LNX_HEADER_LEN, ucon64.rom);
  if (!strncmp (lnx_header.magic, "LYNX", 4))
    result = 0;
  else
    result = -1;
  if (ucon64.console == UCON64_LYNX)
    result = 0;

  if (!strncmp (lnx_header.magic, "LYNX", 4))
    {
      rominfo->buheader_len = UCON64_ISSET (ucon64.buheader_len) ?
        ucon64.buheader_len : (int) LNX_HEADER_LEN;

      if (UCON64_ISSET (ucon64.buheader_len) && !ucon64.buheader_len)
        return ucon64.console == UCON64_LYNX ? 0 : result;

      ucon64_fread (&lnx_header, 0, LNX_HEADER_LEN, ucon64.rom);
      rominfo->buheader = &lnx_header;

      // internal ROM name
      strcpy (rominfo->name, lnx_header.cartname);

      // ROM maker
      rominfo->maker = lnx_header.manufname;

      // misc stuff
      sprintf (rominfo->misc,
        "Internal Size: Bank0 %hd Bytes (%.4f Mb)\n"
        "               Bank1 %hd Bytes (%.4f Mb)\n"
        "Version: %hd\n"
        "Rotation: %s",
#ifdef  WORDS_BIGENDIAN
        bswap_16 (lnx_header.page_size_bank0) * 256,
        TOMBIT_F (bswap_16 (lnx_header.page_size_bank0) * 256),
        bswap_16 (lnx_header.page_size_bank1) * 256,
        TOMBIT_F (bswap_16 (lnx_header.page_size_bank1) * 256),
        bswap_16 (lnx_header.version),
#else
        lnx_header.page_size_bank0 * 256,
        TOMBIT_F (lnx_header.page_size_bank0 * 256),
        lnx_header.page_size_bank1 * 256,
        TOMBIT_F (lnx_header.page_size_bank1 * 256),
        lnx_header.version,
#endif
        (!lnx_header.rotation) ? "No" : ((lnx_header.rotation == 1) ? "Left" : "Right"));
    }

  return result;
}
コード例 #9
0
ファイル: YapClient.cpp プロジェクト: dkirker/BrowserServer
bool YapClient::sendSyncCommand()
{
    char pktHeader[4] = { 0, 0, 0, 0 };
    uint16_t pktLen;
    char * ppp = 0;

    if (d->cmdPacket->length() == 0) {
        fprintf(stderr, "Command is empty\n");
        return false;
    }

    pktLen = 0;
    ::memset(pktHeader, 0, sizeof(pktHeader));

    pktLen = d->cmdPacket->length();
    pktLen = bswap_16(pktLen);
    ::memcpy(pktHeader, &pktLen, 2);
    pktHeader[3] = kPacketFlagSyncMask;

    if (!writeSocket(d->cmdSocketFd, pktHeader, 4))
        goto Detached;

    if (!writeSocket(d->cmdSocketFd, (char*) d->cmdBuffer, d->cmdPacket->length()))
        goto Detached;


    pktLen = 0;
    ::memset(pktHeader, 0, sizeof(pktHeader));

    if (!readSocketSync(d->cmdSocketFd, pktHeader, 4))
        goto Detached;

    ppp = ((char *)&pktHeader[0]);
    pktLen   = *((uint16_t*) ppp);
    pktLen   = bswap_16(pktLen);
    if (pktLen > kMaxMsgLen) {
        fprintf(stderr, "YAP: Message too large %u > %u\n", pktLen, kMaxMsgLen);
        goto Detached;
    }

    if (!readSocketSync(d->cmdSocketFd, (char*) d->replyBuffer, pktLen))
        goto Detached;

    d->replyPacket->reset();
    d->replyPacket->setReadTotalLength(pktLen);

    return true;

 Detached:

    serverDisconnected();
    closeMsgSocket();
    closeCmdSocket();

    return false;
}
コード例 #10
0
ファイル: deepstarcomponent.cpp プロジェクト: KDE/kstars
void DeepStarComponent::byteSwap( starData *stardata ) {
    stardata->RA = bswap_32( stardata->RA );
    stardata->Dec = bswap_32( stardata->Dec );
    stardata->dRA = bswap_32( stardata->dRA );
    stardata->dDec = bswap_32( stardata->dDec );
    stardata->parallax = bswap_32( stardata->parallax );
    stardata->HD = bswap_32( stardata->HD );
    stardata->mag = bswap_16( stardata->mag );
    stardata->bv_index = bswap_16( stardata->bv_index );
}
コード例 #11
0
ファイル: chain.c プロジェクト: djs55/ocfs2-tools
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);
}
コード例 #12
0
ファイル: values.c プロジェクト: buffovich/pcfgm
int cfg_blob_copy( blob_t *from, void *to ) {
	size_t len = from->options & BLOB_LENGTH_MASK;
	
	if( to != NULL ) {
		if( is_host_endian( from ) ||
			( ( from->options & BLOB_LENGTH_MASK ) == 1 )
		)
			__copy( from, to );
		else {
			__copy( from, to );
			if( from->options & BLOB_ARRAY )
				switch( len ) {
					case 2:
						for( char *limit = to + 2 * from->array.length, *cyc = to;
							cyc < limit;
							cyc += 2
						)
							bswap_16( *( ( uint16_t* ) cyc ) );
						break;
					case 4:
						for( char *limit = to + 4 * from->array.length, *cyc = to;
							cyc < limit;
							cyc += 4
						)
							bswap_32( *( ( uint32_t* ) cyc ) );
						break;
					default:
						for( char *limit = to + len * from->array.length, *cyc = to;
							cyc < limit;
							cyc += len
						)
							__swap_bytes( cyc, len );
				}
			else
				switch( len ) {
					case 2:
						bswap_16( *( ( uint16_t* ) to ) );
						break;
					case 4:
						bswap_32( *( ( uint32_t* ) to ) );
						break;
					default:
						__swap_bytes( to, len );
				};
		}
	}

	if( from->options & BLOB_ARRAY )
		return ( len * from->array.length )
	else
		return len;
}
コード例 #13
0
ファイル: minix.c プロジェクト: OPSF/uClinux
int volume_id_probe_minix(struct volume_id *id, uint64_t off, uint64_t size)
{
	uint8_t *buf;
	struct minix_super_block *ms;
	struct minix3_super_block *m3s;

	info("probing at offset 0x%" PRIx64 "\n", off);

	buf = volume_id_get_buffer(id, off + MINIX_SUPERBLOCK_OFFSET, 0x200);
	if (buf == NULL)
		return -1;

	ms = (struct minix_super_block *) buf;

	if (ms->s_magic == MINIX_SUPER_MAGIC ||
	    ms->s_magic == bswap_16(MINIX_SUPER_MAGIC)) {
		strcpy(id->type_version, "1");
		goto found;
	}
	if (ms->s_magic == MINIX_SUPER_MAGIC2 ||
	    ms->s_magic == bswap_16(MINIX_SUPER_MAGIC2)) {
		strcpy(id->type_version, "1");
		goto found;
	}
	if (ms->s_magic == MINIX2_SUPER_MAGIC ||
	    ms->s_magic == bswap_16(MINIX2_SUPER_MAGIC)) {
		strcpy(id->type_version, "2");
		goto found;
	}
	if (ms->s_magic == MINIX2_SUPER_MAGIC2 ||
	    ms->s_magic == bswap_16(MINIX2_SUPER_MAGIC2)) {
		strcpy(id->type_version, "2");
		goto found;
	}

	m3s = (struct minix3_super_block *) buf;
	if (m3s->s_magic == MINIX3_SUPER_MAGIC ||
	    m3s->s_magic == bswap_16(MINIX3_SUPER_MAGIC)) {
		strcpy(id->type_version, "3");
		goto found;
	}
	goto exit;

found:
	volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);
	id->type = "minix";
	return 0;

exit:
	return -1;
}
コード例 #14
0
ファイル: chain.c プロジェクト: ystk/debian-ocfs2-tools
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);
}
コード例 #15
0
ファイル: YapClient.cpp プロジェクト: dkirker/BrowserServer
bool YapClient::connect()
{
    // connect to remote server

    struct sockaddr_un socketAddr;

    d->cmdSocketFd = ::socket(PF_LOCAL, SOCK_STREAM, 0);
    if (d->cmdSocketFd < 0)
        return false;

    memset(&socketAddr, 0, sizeof(socketAddr));
    socketAddr.sun_family = AF_LOCAL;
    strncpy(socketAddr.sun_path, d->cmdSocketPath, G_N_ELEMENTS(socketAddr.sun_path));
    socketAddr.sun_path[G_N_ELEMENTS(socketAddr.sun_path)-1] = '\0';

    if (::connect(d->cmdSocketFd, (struct sockaddr*) &socketAddr,
                  SUN_LEN(&socketAddr)) != 0) {
        close(d->cmdSocketFd);
        d->cmdSocketFd = -1;
        fprintf(stderr, "YAP: Failed to connect to server\n");
        return false;
    }

    // send our msg server socket path
    int16_t strLen = ::strlen(d->msgServerSocketPath);
    int16_t pktLen = bswap_16(strLen);
    if (!writeSocket(d->cmdSocketFd, (char*) &pktLen, 2))
        return false;

    if (!writeSocket(d->cmdSocketFd, d->msgServerSocketPath, strLen))
        return false;

    strLen = ::strlen(d->msgServerSocketPostfix);
    pktLen = bswap_16(strLen);

    if (!writeSocket(d->cmdSocketFd, (char*) &pktLen, 2))
        return false;

    if (!writeSocket(d->cmdSocketFd, d->msgServerSocketPostfix, strLen))
        return false;

    // Add io channel to know when the command socket is disconnected.
    d->cmdIoChannel = g_io_channel_unix_new(d->cmdSocketFd);
    d->cmdIoSource  = g_io_create_watch(d->cmdIoChannel, (GIOCondition) (G_IO_HUP));

    g_source_set_callback(d->cmdIoSource, (GSourceFunc) YapClientPriv::ioCallback, this, NULL);
    g_source_attach(d->cmdIoSource, d->mainCtxt);

    return true;
}
コード例 #16
0
ファイル: deepstarcomponent.cpp プロジェクト: KDE/kstars
bool DeepStarComponent::openDataFile() {

    if( starReader.getFileHandle() )
        return true;

    starReader.openFile( dataFileName );
    fileOpened = false;
    if( !starReader.getFileHandle() )
        qDebug() << "WARNING: Failed to open deep star catalog " << dataFileName << ". Disabling it." << endl;
    else if( !starReader.readHeader() )
        qDebug() << "WARNING: Header read error for deep star catalog " << dataFileName << "!! Disabling it!" << endl;
    else {
        qint16 faintmag;
        quint8 htm_level;
        fread( &faintmag, 2, 1, starReader.getFileHandle() );
        if( starReader.getByteSwap() )
            faintmag = bswap_16( faintmag );
        if( starReader.guessRecordSize() == 16 )
            m_FaintMagnitude = faintmag / 1000.0;
        else
            m_FaintMagnitude = faintmag / 100.0;
        fread( &htm_level, 1, 1, starReader.getFileHandle() );
        qDebug() << "Processing " << dataFileName << ", HTMesh Level" << htm_level;
        m_skyMesh = SkyMesh::Instance( htm_level );
        if( !m_skyMesh ) {
            if( !( m_skyMesh = SkyMesh::Create( htm_level ) ) ) {
                qDebug() << "Could not create HTMesh of level " << htm_level << " for catalog " << dataFileName << ". Skipping it.";
                return false;
            }
        }
        meshLevel = htm_level;
        fread( &MSpT, 2, 1, starReader.getFileHandle() );
        if( starReader.getByteSwap() )
            MSpT = bswap_16( MSpT );
        fileOpened = true;
        qDebug() << "  Sky Mesh Size: " << m_skyMesh->size();
        for (long int i = 0; i < m_skyMesh->size(); i++) {
            StarBlockList *sbl = new StarBlockList( i, this );
            if( !sbl ) {
                qDebug() << "NULL starBlockList. Expect trouble!";
            }
            m_starBlockList.append( sbl );
        }
        m_zoomMagLimit = 0.06;
    }

    return fileOpened;
}
コード例 #17
0
static int
vars_get_variable_size(efi_guid_t guid, const char *name, size_t *size)
{
	int errno_value;
	int ret = -1;

	char *path = NULL;
	int rc = asprintf(&path, VARS_PATH "%s-"GUID_FORMAT"/size",
			  name, guid.a, guid.b, guid.c, bswap_16(guid.d),
			  guid.e[0], guid.e[1], guid.e[2], guid.e[3],
			  guid.e[4], guid.e[5]);
	if (rc < 0)
		goto err;

	long long int retsize = 0;
	rc = get_size_from_file(path, &retsize);
	if (rc >= 0) {
		ret = 0;
		*size = (size_t) retsize;
	}
err:
	errno_value = errno;

	if (path)
		free(path);

	errno = errno_value;
	return ret;
}
コード例 #18
0
ファイル: kexec.c プロジェクト: CrazyXen/XEN_CODE
static void tpm_hash2pcr(struct xc_dom_image *dom, char *cmdline)
{
	struct tpmfront_dev* tpm = init_tpmfront(NULL);
	uint8_t *resp;
	size_t resplen = 0;
	struct pcr_extend_cmd cmd;

	/* If all guests have access to a vTPM, it may be useful to replace this
	 * with ASSERT(tpm) to prevent configuration errors from allowing a guest
	 * to boot without a TPM (or with a TPM that has not been sent any
	 * measurements, which could allow forging the measurements).
	 */
	if (!tpm)
		return;

	cmd.tag = bswap_16(TPM_TAG_RQU_COMMAND);
	cmd.size = bswap_32(sizeof(cmd));
	cmd.ord = bswap_32(TPM_ORD_Extend);
	cmd.pcr = bswap_32(4); // PCR #4 for kernel
	sha1(dom->kernel_blob, dom->kernel_size, cmd.hash);

	tpmfront_cmd(tpm, (void*)&cmd, sizeof(cmd), &resp, &resplen);

	cmd.pcr = bswap_32(5); // PCR #5 for cmdline
	sha1(cmdline, strlen(cmdline), cmd.hash);
	tpmfront_cmd(tpm, (void*)&cmd, sizeof(cmd), &resp, &resplen);

	cmd.pcr = bswap_32(5); // PCR #5 for initrd
	sha1(dom->ramdisk_blob, dom->ramdisk_size, cmd.hash);
	tpmfront_cmd(tpm, (void*)&cmd, sizeof(cmd), &resp, &resplen);

	shutdown_tpmfront(tpm);
}
コード例 #19
0
ファイル: nservice.c プロジェクト: painterjd/zerovm-2.0
/* serialize channels data to the parcel. return parcel and its "size" */
static void *ParcelCtor(const struct Manifest *manifest,
    uint32_t *size, uint32_t binds, uint32_t connects)
{
  struct NSParcel *p;
  int64_t end = binds + connects;
  int64_t m = 0, n = 0;
  int64_t i;

  /* allocate parcel */
  *size = sizeof *p + (end - 1) * sizeof(struct NSRecord);
  p = g_malloc(*size);

  /* parcel header */
  p->node = bswap_32(manifest->node);
  p->bind_number = bswap_32(binds);
  p->connect_number = bswap_32(connects);

  /* populate parcel with bind/connect records */
  for(i = 0; i < end; ++i)
  {
    struct Connection *c;
    NEXT_SRC();
    p->records[i].host = bswap_32(c->host);
    p->records[i].port = bswap_16(c->port);
  }

  return (void*)p;
}
コード例 #20
0
ファイル: sstrip.c プロジェクト: 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!");
}
コード例 #21
0
ファイル: lynx.c プロジェクト: GunioRobot/quickdev16
static int
lynx_b (st_rominfo_t *rominfo, int bank, const char *value)
{
  st_lnx_header_t header;
  short int *bankvar;
  char dest_name[FILENAME_MAX];

  if (!rominfo->buheader_len)
    {
      fprintf (stderr, "ERROR: This is no LNX file\n\n");
      return -1;
    }

  ucon64_fread (&header, 0, sizeof (st_lnx_header_t), ucon64.rom);

  bankvar = (bank == 0 ? &header.page_size_bank0 : &header.page_size_bank1);
  if ((atol (value) % 64) != 0 || (atol (value) > 512))
    *bankvar = 0;
  else
#ifdef  WORDS_BIGENDIAN
    *bankvar = bswap_16 (atol (value) * 4);
#else
    *bankvar = atol (value) * 4;
#endif

  strcpy (dest_name, ucon64.rom);
  ucon64_file_handler (dest_name, NULL, 0);
  fcopy (ucon64.rom, 0, ucon64.file_size, dest_name, "wb");
  ucon64_fwrite (&header, 0, sizeof (st_lnx_header_t), dest_name, "r+b");

  printf (ucon64_msg[WROTE], dest_name);
  return 0;
}
コード例 #22
0
size_t EndianStream::writeUInt16(uint16_t value) {
	debug_assertp(_stream!=NULL, this, "Wrapped stream cannot be NULL");
	if (_order!=DUCT_BYTEORDER) {
		value=bswap_16(value);
	}
	return _stream->writeUInt16(value);
}
コード例 #23
0
ファイル: binary_parser.cpp プロジェクト: thodrek/sampler
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;
}
コード例 #24
0
static void unpack_nbuf16_swap(nbuf *x) {
    unsigned i;
    
    for (i = x->used; i; --i) {
        x->data.raw[i - 1] = bswap_16(x->data.u16[i - 1]) + x->min;
    }
}
コード例 #25
0
ファイル: indirect_util.c プロジェクト: 4eremuxa/xserver
/**
 * Send a GLX reply to the client.
 *
 * Technically speaking, there are several different ways to encode a GLX
 * reply.  The primary difference is whether or not certain fields (e.g.,
 * retval, size, and "pad3") are set.  This function gets around that by
 * always setting all of the fields to "reasonable" values.  This does no
 * harm to clients, but it does make the server-side code much more compact.
 *
 * \warning
 * This function assumes that values stored in \c data will be byte-swapped
 * by the caller if necessary.
 */
void
__glXSendReplySwap( ClientPtr client, const void * data, size_t elements,
    size_t element_size, GLboolean always_array, CARD32 retval )
{
    size_t reply_ints = 0;

    if ( __glXErrorOccured() ) {
        elements = 0;
    }
    else if ( (elements > 1) || always_array ) {
        reply_ints = bytes_to_int32(elements * element_size);
    }

    __glXReply.length =         bswap_32( reply_ints );
    __glXReply.type =           X_Reply;
    __glXReply.sequenceNumber = bswap_16( client->sequence );
    __glXReply.size =           bswap_32( elements );
    __glXReply.retval =         bswap_32( retval );


    /* It is faster on almost always every architecture to just copy the 8
     * bytes, even when not necessary, than check to see of the value of
     * elements requires it.  Copying the data when not needed will do no
     * harm.
     */

    (void) memcpy( & __glXReply.pad3, data, 8 );
    WriteToClient( client, sz_xGLXSingleReply, (char *) & __glXReply );

    if ( reply_ints != 0 ) {
        WriteToClient( client, reply_ints * 4, (char *) data );
    }
}
コード例 #26
0
ファイル: uuid.c プロジェクト: linrjing/corelibs-arduino101
static inline void u16_to_uuid128(void *dst, uint16_t u16)
{
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
	u16 = bswap_16(u16);
#endif
	memcpy(dst, &u16, 2);
}
コード例 #27
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;
    }
}
コード例 #28
0
ファイル: ws.c プロジェクト: NeeMeese/mplayer-ce
// ----------------------------------------------------------------------------------------------
//    Put 'Image' to window.
// ----------------------------------------------------------------------------------------------
void wsConvert( wsTWindow * win,unsigned char * Image,unsigned int Size )
{
  const uint8_t *src[4] = { Image, NULL, NULL, NULL };
  int src_stride[4] = { 4 * win->xImage->width, 0, 0, 0 };
  uint8_t *dst[4] = { win->ImageData, NULL, NULL, NULL };
  int dst_stride[4];
  int i;
  sws_ctx = sws_getCachedContext(sws_ctx, win->xImage->width, win->xImage->height, PIX_FMT_RGB32,
                                          win->xImage->width, win->xImage->height, out_pix_fmt,
                                          SWS_POINT, NULL, NULL, NULL);
  av_image_fill_linesizes(dst_stride, out_pix_fmt, win->xImage->width);
  sws_scale(sws_ctx, src, src_stride, 0, win->xImage->height, dst, dst_stride);
  if (!wsNonNativeOrder) return;
  switch (win->xImage->bits_per_pixel) {
    case 32:
    {
      uint32_t *d = (uint32_t *) win->ImageData;
      for (i = 0; i < win->xImage->width * win->xImage->height; i++)
        d[i] = bswap_32(d[i]);
      break;
    }
    case 16:
    case 15:
    {
      uint16_t *d = (uint16_t *) win->ImageData;
      for (i = 0; i < win->xImage->width * win->xImage->height; i++)
        d[i] = bswap_16(d[i]);
      break;
    }
  }
}
コード例 #29
0
ファイル: video_recording.cpp プロジェクト: habnabit/macemu
void video_recording_state_t::write_audio_frame(uint8 *buffer)
{
	AVCodecContext *c = audio_stream->codec;
	av_samples_fill_arrays(audio_frame->data, audio_frame->linesize, buffer, 2, 2048, AV_SAMPLE_FMT_S16, 0);
	uint16 *base = (uint16 *)audio_frame->data[0];
	for (int i = 0; i < 4096; ++i) {
		*base = bswap_16(*base);
		++base;
	}
	AVPacket pkt;
	int got_packet = 0;
	av_init_packet(&pkt);
	pkt.data = NULL;
	pkt.size = 0;
	if (avcodec_encode_audio2(c, &pkt, audio_frame, &got_packet) < 0) {
		fprintf(stderr, "Error encoding an audio frame\n");
	}
	if (got_packet) {
		pkt.pts = pkt.dts = audio_frame->pts++;
		pkt.stream_index = audio_stream->index;
		if (av_interleaved_write_frame(output_context, &pkt) < 0) {
			fprintf(stderr, "Error writing an audio frame\n");
		}
		if (av_interleaved_write_frame(output_context, NULL) < 0) {
			fprintf(stderr, "Error flushing output buffer\n");
		}
	}
}
コード例 #30
0
ファイル: endian.c プロジェクト: hjelmn/upod
void bswap_block (void *ptr, size_t membsize, size_t nmemb) {
  int i;

#if BYTE_ORDER==BIG_ENDIAN
  for (i = 0 ; i < nmemb ; i++)
    switch (membsize) {
    case 2:
      {
        u_int16_t *r = (u_int16_t *)ptr;

        for (i = 0 ; i < nmemb ; i++)
          r[i] = bswap_16 (r[i]);

        break;
      }

    case 4:
      {
        u_int32_t *r = (u_int32_t *)ptr;

        for (i = 0 ; i < nmemb ; i++)
          r[i] = bswap_32 (r[i]);

        break;
      }
  }
#endif
}