コード例 #1
0
ファイル: artHeader.c プロジェクト: adrpar/densfield
extern void
artHeader_write(artHeader_t header, const char *headerFileName)
{
	assert(header != NULL);
	assert(headerFileName != NULL);

	FILE     *f;
	int      blocksize;
	endian_t systemEndianess = endian_getSystemEndianess();

	f         = xfopen(headerFileName, "wb");
	blocksize = ARTHEADER_HEADERSTRING_LENGTH + 121 * sizeof(float);
	if (header->fileEndianess != systemEndianess) {
		local_byteswapHeader(header);
		byteswap(&blocksize, sizeof(int));
	}

	xfwrite(&blocksize, sizeof(int), 1, f);
	xfwrite(header->headerString, sizeof(char),
	        ARTHEADER_HEADERSTRING_LENGTH, f);
	xfwrite(&(header->aexpn), sizeof(float), 121, f);
	xfwrite(&blocksize, sizeof(int), 1, f);

	if (header->fileEndianess != systemEndianess)
		local_byteswapHeader(header);

	xfclose(&f);
}
コード例 #2
0
ファイル: map_hdr_in.c プロジェクト: wjlei1990/WORKFLOW
/** 
 *  This routine maps the SAC file header into memory from
 *    a buffer containing it in disk file format.  
 *    In the disk file (deriving from the FORTRAN version of
 *    SAC) these strings are essentially concatenated without
 *    null termination.  In memory, they are carried about as
 *    null terminated strings.  This routine picks out the 
 *    strings and null terminates them, storing them in the
 *    in memory working storage area.
 * 
 * @param memarray 
 *    Output
 * @param buffer 
 *    Input
 * @param lswap 
 *    Swap the buffer if necessary
 *
 */
void 
map_hdr_in(float *memarray,
	   float *buffer,
	   int    lswap) {

  char *ptr1, *ptr2;
  int idx;

  /* First get the header values for the non character   
   * fields fhdr, nhdr, ihdr and lhdr.  These are copied 
   * straight across.                                    
   */
  ptr1 = (char *)memarray;
  ptr2 = (char *)buffer;

  memcpy(ptr1,ptr2, SAC_HEADER_NUMBERS * sizeof(float));

  /* byteswap numeric data if necessary. */
  if( lswap ){
    for( idx = 0 ; idx < SAC_HEADER_NUMBERS ; idx++, ptr1 += SAC_HEADER_SIZEOF_NUMBER )
      byteswap( (void *)ptr1, SAC_HEADER_SIZEOF_NUMBER ) ;
  }
  
  /* Now copy the character variables into the memory    
   * buffer, supplying the additional null termination   
   * character.                                          
   */
  map_chdr_in(memarray + SAC_HEADER_NUMBERS, 
              buffer + SAC_HEADER_NUMBERS);

  return;

}
コード例 #3
0
ファイル: scrypt.c プロジェクト: pavlog/cgminer
/*
 * Encode a length len/4 vector of (uint32_t) into a length len vector of
 * (unsigned char) in big-endian form.  Assumes len is a multiple of 4.
 */
static inline void
be32enc_vect(uint32_t *dst, const uint32_t *src, uint32_t len)
{
	uint32_t i;

	for (i = 0; i < len; i++)
		dst[i] = byteswap(src[i]);
}
コード例 #4
0
ファイル: ghash_pclmulqdq_impl.c プロジェクト: nomaster/fastd
/** Initializes the state used by this GHASH implementation */
fastd_mac_state_t* fastd_ghash_pclmulqdq_init(const uint8_t *key) {
	fastd_mac_state_t *state;
	if (posix_memalign((void**)&state, 16, sizeof(fastd_mac_state_t)))
		abort();

	memcpy(&state->H, key, sizeof(__m128i));
	state->H.v = byteswap(state->H.v);

	return state;
}
コード例 #5
0
ファイル: art.c プロジェクト: adrpar/densfield
static void
local_writeComponent(art_t    art,
                     uint64_t pSkip,
                     uint64_t pWrite,
                     stai_t   component,
                     bool     doByteswap)
{
	if ((component == NULL) || (pSkip == art->numParticlesInPage)) {
		xfseek(art->f, art->numParticlesInPage * sizeof(float), SEEK_CUR);
		return;
	}
	float *buffer;
	bool  bufferIsAllocated = false;

	if (stai_isLinear(component)
	    && (stai_getSizeOfElementInBytes(component) == sizeof(float))) {
		buffer = stai_getBase(component);
	} else {
		buffer            = xmalloc(sizeof(float) * pWrite);
		bufferIsAllocated = true;
		local_fillBufferFromStai(buffer, component, pWrite);
	}

	if (doByteswap) {
		for (int i = 0; i < pWrite; i++)
			byteswap(buffer + i, sizeof(float));
	}

	xfseek(art->f, (long)pSkip * sizeof(float), SEEK_CUR);
	xfwrite(buffer, sizeof(float), pWrite, art->f);
	xfseek(art->f,
	       (long)(art->numParticlesInPage - pSkip - pWrite) * sizeof(float),
	       SEEK_CUR);

	if (doByteswap && !bufferIsAllocated) {
		// Restore the original byte order of the data array.
		for (int i = 0; i < art->numParticlesInPage; i++)
			byteswap(buffer + i, sizeof(float));
	}
	if (bufferIsAllocated)
		xfree(buffer);
} /* local_writeComponent */
コード例 #6
0
ファイル: sha.cpp プロジェクト: tfussell/xlnt
void sha512(const std::vector<std::uint8_t> &input, std::vector<std::uint8_t> &output)
{
    static const auto sha512_bytes = 64;

    output.resize(sha512_bytes);
    auto output_pointer_u64 = reinterpret_cast<std::uint64_t *>(output.data());

    sha512_hash(input.data(), input.size(), output_pointer_u64);

    byteswap(output_pointer_u64, sha512_bytes / sizeof(std::uint64_t));
}
コード例 #7
0
ファイル: sha.cpp プロジェクト: tfussell/xlnt
void sha1(const std::vector<std::uint8_t> &input, std::vector<std::uint8_t> &output)
{
    static const auto sha1_bytes = 20;

    output.resize(sha1_bytes);
    auto output_pointer_u32 = reinterpret_cast<std::uint32_t *>(output.data());

    sha1_hash(input.data(), input.size(), output_pointer_u32);

    byteswap(output_pointer_u32, sha1_bytes / sizeof(std::uint32_t));
}
コード例 #8
0
ファイル: cubepm.c プロジェクト: satlank/ginnungagap
static void
local_readParticle(FILE  *f,
                   float *partData,
                   bool  doByteswap)
{
	xfread(partData, sizeof(float), 6, f);

	if (doByteswap) {
		for (int i = 0; i < 6; i++)
			byteswap(partData + i, sizeof(float));
	}
}
コード例 #9
0
ファイル: loadbin.c プロジェクト: NEKCEM/NekTools
static long readfile(double *data, long max, const char *name)
{
  const double magic = 3.14159;
  long n;
  FILE *f = fopen(name,"r");
  fseek(f,0,SEEK_END);
  n = ftell(f)/sizeof(double);
  if(n>max) mexWarnMsgTxt("file longer than expected"),n=max;
  fseek(f,0,SEEK_SET);
  fread(data,sizeof(double),n,f);
  fclose(f);
  if(n>0 && fabs(data[0]-magic)>0.000001) {
    long i;
    mexWarnMsgTxt("swapping byte order");
    if(fabs(byteswap(data[0])-magic)>0.000001) {
      mexWarnMsgTxt("magic number for endian test not found");
    } else
      for(i=0;i<n;++i) data[i]=byteswap(data[i]);
  }
  return n;
}
コード例 #10
0
ファイル: scrypt.c プロジェクト: pavlog/cgminer
/* Used externally as confirmation of correct OCL code */
bool scrypt_test(unsigned char *pdata, const unsigned char *ptarget, uint32_t nonce)
{
	uint32_t tmp_hash7, Htarg = ((const uint32_t *)ptarget)[7];
	char *scratchbuf;
	uint32_t data[20];

	be32enc_vect(data, (const uint32_t *)pdata, 19);
	data[19] = byteswap(nonce);
	scratchbuf = (char*)alloca(131584);
	tmp_hash7 = scrypt_1024_1_1_256_sp(data, scratchbuf);

	return (tmp_hash7 <= Htarg);
}
コード例 #11
0
ファイル: paramFile.c プロジェクト: seichter/uartoolkit
int    arParamLoad( const char *filename, int num, ARParam *param, ...)
{
    FILE        *fp;
    va_list     ap;
    ARParam     *param1;
    int         i;

    if( num < 1 ) return -1;

    fp = fopen( filename, "rb" );
    if( fp == NULL ) return -1;

    if( fread( param, sizeof(ARParam), 1, fp ) != 1 ) {
        fclose(fp);
        return -1;
    }
#ifdef AR_LITTLE_ENDIAN
    byteswap( param );
#endif

    va_start(ap, param);
    for( i = 1; i < num; i++ ) {
        param1 = va_arg(ap, ARParam *);
        if( fread( param1, sizeof(ARParam), 1, fp ) != 1 ) {
            fclose(fp);
            return -1;
        }
#ifdef AR_LITTLE_ENDIAN
        byteswap( param1 );
#endif
    }

    fclose(fp);

    return 0;
}
コード例 #12
0
ファイル: _quad.c プロジェクト: FrancescAlted/teaching
static void
npyquad_copyswap(void* dst, void* src, int swap, void* NPY_UNUSED(arr))
{
    qdouble *q;

    if (!src) {
        return;
    }
    q = (qdouble*)dst;
    /* FIXME: memmove vs memcpy */
    memmove(q, src, sizeof(*q));
    if (swap) {
        byteswap(q);
    }
}
コード例 #13
0
ファイル: cubepm.c プロジェクト: satlank/ginnungagap
static void
local_writeParticle(FILE        *f,
                    const float *partData,
                    bool        doByteswap)
{
	for (int i = 0; i < 6; i++) {
		if (isnan(partData[i]))
			xfseek(f, sizeof(float), SEEK_CUR);
		else {
			float val = partData[i];
			if (doByteswap)
				byteswap(&val, sizeof(float));
			xfwrite(&val, sizeof(float), 1, f);
		}
	}
}
コード例 #14
0
ファイル: ide.c プロジェクト: kelihlodversson/emutos
/*
 * get data from IDE device
 */
static void ide_get_data(volatile struct IDE *interface,UBYTE *buffer,ULONG bufferlen,int need_byteswap)
{
    XFERWIDTH *p = (XFERWIDTH *)buffer;
    XFERWIDTH *end = (XFERWIDTH *)(buffer + bufferlen);

    KDEBUG(("ide_get_data(0x%08lx, 0x%08lx, %lu, %d)\n", (ULONG)interface, (ULONG)buffer, bufferlen, need_byteswap));

    while (p < end)
        *p++ = interface->data;

    if (need_byteswap)
    {
        KDEBUG(("byteswap(0x%08lx, %lu)\n", (ULONG)buffer, bufferlen));
        byteswap(buffer,bufferlen);
    }
}
コード例 #15
0
ファイル: scrypt.c プロジェクト: pavlog/cgminer
static inline uint32_t
PBKDF2_SHA256_80_128_32(const uint32_t * passwd, const uint32_t * salt)
{
	uint32_t tstate[8];
	uint32_t ostate[8];
	uint32_t ihash[8];
	uint32_t i;

	/* Compute HMAC state after processing P and S. */
	uint32_t pad[16];
	
	static const uint32_t ihash_finalblk[16] = {0x00000001,0x80000000,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0x00000620};

	/* If Klen > 64, the key is really SHA256(K). */
	SHA256_InitState(tstate);
	SHA256_Transform(tstate, passwd, 1);
	memcpy(pad, passwd+16, 16);
	memcpy(pad+4, passwdpad, 48);
	SHA256_Transform(tstate, pad, 1);
	memcpy(ihash, tstate, 32);

	SHA256_InitState(ostate);
	for (i = 0; i < 8; i++)
		pad[i] = ihash[i] ^ 0x5c5c5c5c;
	for (; i < 16; i++)
		pad[i] = 0x5c5c5c5c;
	SHA256_Transform(ostate, pad, 0);

	SHA256_InitState(tstate);
	for (i = 0; i < 8; i++)
		pad[i] = ihash[i] ^ 0x36363636;
	for (; i < 16; i++)
		pad[i] = 0x36363636;
	SHA256_Transform(tstate, pad, 0);
	SHA256_Transform(tstate, salt, 1);
	SHA256_Transform(tstate, salt+16, 1);
	SHA256_Transform(tstate, ihash_finalblk, 0);
	memcpy(pad, tstate, 32);
	memcpy(pad+8, outerpad, 32);

	/* Feed the inner hash to the outer SHA256 operation. */
	SHA256_Transform(ostate, pad, 0);
	/* Finish the outer SHA256 operation. */
	return byteswap(ostate[7]);
}
コード例 #16
0
ファイル: mprof.c プロジェクト: groleo/mpatrol
static
void
getentry(void *d, size_t l, size_t n, int b)
{
    size_t i;

    if (fread(d, l, n, proffile) != n)
    {
        fprintf(stderr, "%s: Error reading file\n", progname);
        exit(EXIT_FAILURE);
    }
    /* Byte-swap all of the elements if necessary.
     */
    if (b != 0)
        for (i = 0; i < n; i++)
        {
            byteswap(d, l);
            d = (char *) d + l;
        }
}
コード例 #17
0
ファイル: scrypt.c プロジェクト: pavlog/cgminer
bool scanhash_scrypt(struct thr_info *thr, const unsigned char __maybe_unused *pmidstate,
		     unsigned char *pdata, unsigned char __maybe_unused *phash1,
		     unsigned char __maybe_unused *phash, const unsigned char *ptarget,
		     uint32_t max_nonce, uint32_t *last_nonce, uint32_t n)
{
	uint32_t *nonce = (uint32_t *)(pdata + 76);
	char *scratchbuf;
	uint32_t data[20];
	uint32_t tmp_hash7;
	uint32_t Htarg = ((const uint32_t *)ptarget)[7];
	bool ret = false;

	be32enc_vect(data, (const uint32_t *)pdata, 19);

	scratchbuf = (char*)malloc(131583);
	if (unlikely(!scratchbuf)) {
		applog(LOG_ERR, "Failed to malloc scratchbuf in scanhash_scrypt");
		return ret;
	}

	while(1) {
		*nonce = ++n;
		data[19] = n;
		tmp_hash7 = scrypt_1024_1_1_256_sp(data, scratchbuf);

		if (unlikely(tmp_hash7 <= Htarg)) {
			((uint32_t *)pdata)[19] = byteswap(n);
			*last_nonce = n;
			ret = true;
			break;
		}

		if (unlikely((n >= max_nonce) || thr->work_restart)) {
			*last_nonce = n;
			break;
		}
	}

	free(scratchbuf);;
	return ret;
}
コード例 #18
0
ファイル: art.c プロジェクト: adrpar/densfield
static void
local_readComponent(art_t    art,
                    uint64_t pSkip,
                    uint64_t pRead,
                    stai_t   component,
                    bool     doByteswap)
{
	if ((component == NULL) || (pSkip == art->numParticlesInPage)) {
		xfseek(art->f, art->numParticlesInPage * sizeof(float), SEEK_CUR);
		return;
	}
	float *buffer;
	bool  bufferIsAllocated;

	if (stai_isLinear(component)
	    && (stai_getSizeOfElementInBytes(component) == sizeof(float))) {
		buffer = stai_getBase(component);
	} else {
		buffer            = xmalloc(sizeof(float) * pRead);
		bufferIsAllocated = true;
	}

	xfseek(art->f, (long)pSkip * sizeof(float), SEEK_CUR);
	xfread(buffer, sizeof(float), pRead, art->f);
	xfseek(art->f,
	       (long)(art->numParticlesInPage - pSkip - pRead) * sizeof(float),
	       SEEK_CUR);
	if (doByteswap) {
		for (int i = 0; i < pRead; i++)
			byteswap(buffer + i, sizeof(float));
	}

	if (bufferIsAllocated) {
		local_copyBufferToStai(buffer, component, pRead);
		xfree(buffer);
	}
}
コード例 #19
0
static
void
getentry(void *d, size_t l, size_t n, int b)
{
    size_t i, s;

    s = l * n;
    if (refill(s) < s)
    {
        fprintf(stderr, "%s: Error reading file\n", progname);
        exit(EXIT_FAILURE);
    }
    memcpy(d, bufferpos, s);
    bufferpos += s;
    bufferlen -= s;
    /* Byte-swap all of the elements if necessary.
     */
    if (b != 0)
        for (i = 0; i < n; i++)
        {
            byteswap(d, l);
            d = (char *) d + l;
        }
}
コード例 #20
0
ファイル: fbuf.c プロジェクト: carsten-clauss/MP-MPICH
/*  Withdraw the array type of object from the file buffer  */
fbuf_uint32_t fbuf_withdraw(       void           *dest,
                             const fbuf_uint32_t   elem_sz,
                             const fbuf_uint32_t   Nelem,
                                   filebuf_t      *fbuf )
{
#if ! defined( WORDS_BIGENDIAN )
    void          *buf;
#endif
    fbuf_uint32_t  total_bytesize;
    fbuf_uint32_t  total_fbufunit;

    total_bytesize = elem_sz * Nelem;
    total_fbufunit = byte2fbufunit( total_bytesize );
    if ( fbuf->ptr + total_fbufunit <= fbuf->end ) {
#if ! defined( WORDS_BIGENDIAN )
#  if ! defined( HAVE_ALLOCA )
        buf = ( char * ) malloc( total_bytesize );
#  else
        buf = ( char * ) alloca( total_bytesize );
#  endif
        if ( buf == NULL ) return (fbuf_uint32_t) 0;
        memcpy( buf, fbuf->ptr, byte2char( total_bytesize ) );
        byteswap( Nelem, elem_sz, buf );
        memcpy( dest, buf, byte2char( total_bytesize ) );
#  if ! defined( HAVE_ALLOCA )
        free( buf );
#  endif
#else
        memcpy( dest, fbuf->ptr, byte2char( total_bytesize ) );
#endif
        fbuf->ptr += total_fbufunit;
        return Nelem;
    }
    else 
        return (fbuf_uint32_t) 0;
}
コード例 #21
0
ファイル: artHeader.c プロジェクト: adrpar/densfield
static void
local_byteswapHeader(artHeader_t header)
{
	for (int i = 0; i < 121; i++)
		byteswap(&(header->aexpn) + i, sizeof(float));
}
コード例 #22
0
ファイル: scrypt.c プロジェクト: pavlog/cgminer
/*
 * SHA256 block compression function.  The 256-bit state is transformed via
 * the 512-bit input block to produce a new state.
 */
static void
SHA256_Transform(uint32_t * state, const uint32_t block[16], int swap)
{
	uint32_t W[64];
	uint32_t S[8];
	uint32_t t0, t1;
	int i;

	/* 1. Prepare message schedule W. */
	if(swap)
		for (i = 0; i < 16; i++)
			W[i] = byteswap(block[i]);
	else
		memcpy(W, block, 64);
	for (i = 16; i < 64; i += 2) {
		W[i] = s1(W[i - 2]) + W[i - 7] + s0(W[i - 15]) + W[i - 16];
		W[i+1] = s1(W[i - 1]) + W[i - 6] + s0(W[i - 14]) + W[i - 15];
	}

	/* 2. Initialize working variables. */
	memcpy(S, state, 32);

	/* 3. Mix. */
	RNDr(S, W, 0, 0x428a2f98);
	RNDr(S, W, 1, 0x71374491);
	RNDr(S, W, 2, 0xb5c0fbcf);
	RNDr(S, W, 3, 0xe9b5dba5);
	RNDr(S, W, 4, 0x3956c25b);
	RNDr(S, W, 5, 0x59f111f1);
	RNDr(S, W, 6, 0x923f82a4);
	RNDr(S, W, 7, 0xab1c5ed5);
	RNDr(S, W, 8, 0xd807aa98);
	RNDr(S, W, 9, 0x12835b01);
	RNDr(S, W, 10, 0x243185be);
	RNDr(S, W, 11, 0x550c7dc3);
	RNDr(S, W, 12, 0x72be5d74);
	RNDr(S, W, 13, 0x80deb1fe);
	RNDr(S, W, 14, 0x9bdc06a7);
	RNDr(S, W, 15, 0xc19bf174);
	RNDr(S, W, 16, 0xe49b69c1);
	RNDr(S, W, 17, 0xefbe4786);
	RNDr(S, W, 18, 0x0fc19dc6);
	RNDr(S, W, 19, 0x240ca1cc);
	RNDr(S, W, 20, 0x2de92c6f);
	RNDr(S, W, 21, 0x4a7484aa);
	RNDr(S, W, 22, 0x5cb0a9dc);
	RNDr(S, W, 23, 0x76f988da);
	RNDr(S, W, 24, 0x983e5152);
	RNDr(S, W, 25, 0xa831c66d);
	RNDr(S, W, 26, 0xb00327c8);
	RNDr(S, W, 27, 0xbf597fc7);
	RNDr(S, W, 28, 0xc6e00bf3);
	RNDr(S, W, 29, 0xd5a79147);
	RNDr(S, W, 30, 0x06ca6351);
	RNDr(S, W, 31, 0x14292967);
	RNDr(S, W, 32, 0x27b70a85);
	RNDr(S, W, 33, 0x2e1b2138);
	RNDr(S, W, 34, 0x4d2c6dfc);
	RNDr(S, W, 35, 0x53380d13);
	RNDr(S, W, 36, 0x650a7354);
	RNDr(S, W, 37, 0x766a0abb);
	RNDr(S, W, 38, 0x81c2c92e);
	RNDr(S, W, 39, 0x92722c85);
	RNDr(S, W, 40, 0xa2bfe8a1);
	RNDr(S, W, 41, 0xa81a664b);
	RNDr(S, W, 42, 0xc24b8b70);
	RNDr(S, W, 43, 0xc76c51a3);
	RNDr(S, W, 44, 0xd192e819);
	RNDr(S, W, 45, 0xd6990624);
	RNDr(S, W, 46, 0xf40e3585);
	RNDr(S, W, 47, 0x106aa070);
	RNDr(S, W, 48, 0x19a4c116);
	RNDr(S, W, 49, 0x1e376c08);
	RNDr(S, W, 50, 0x2748774c);
	RNDr(S, W, 51, 0x34b0bcb5);
	RNDr(S, W, 52, 0x391c0cb3);
	RNDr(S, W, 53, 0x4ed8aa4a);
	RNDr(S, W, 54, 0x5b9cca4f);
	RNDr(S, W, 55, 0x682e6ff3);
	RNDr(S, W, 56, 0x748f82ee);
	RNDr(S, W, 57, 0x78a5636f);
	RNDr(S, W, 58, 0x84c87814);
	RNDr(S, W, 59, 0x8cc70208);
	RNDr(S, W, 60, 0x90befffa);
	RNDr(S, W, 61, 0xa4506ceb);
	RNDr(S, W, 62, 0xbef9a3f7);
	RNDr(S, W, 63, 0xc67178f2);

	/* 4. Mix local working variables into global state */
	for (i = 0; i < 8; i++)
		state[i] += S[i];
}
コード例 #23
0
ファイル: bigend_int.hpp プロジェクト: Halfbin/Rk-Core
 under get () const {
   return byteswap (val);
 }
コード例 #24
0
ファイル: RenderSettings.cpp プロジェクト: hernando/Livre
template<> inline void byteswap( livre::RendererType& value )
    { byteswap( reinterpret_cast< uint32_t& >( value )); }
コード例 #25
0
int savebmp(char *filename, unsigned char *buf, int w, int h,
	enum BMPPIXELFORMAT f, int srcpitch, int srcbottomup)
{
	int fd=-1, byteswritten, dstpitch, retcode=0;
	int flags=O_RDWR|O_CREAT|O_TRUNC;
	unsigned char *tempbuf=NULL;  char *temp;
	bmphdr bh;  int mode;

	#ifdef _WIN32
	flags|=O_BINARY;  mode=_S_IREAD|_S_IWRITE;
	#else
	mode=S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
	#endif
	if(!filename || !buf || w<1 || h<1 || f<0 || f>BMPPIXELFORMATS-1 || srcpitch<0)
		_throw("bad argument to savebmp()");

	if(srcpitch==0) srcpitch=w*ps[f];

	if((temp=strrchr(filename, '.'))!=NULL)
	{
		if(!stricmp(temp, ".ppm"))
			return saveppm(filename, buf, w, h, f, srcpitch, srcbottomup);
	}

	_unix(fd=open(filename, flags, mode));
	dstpitch=((w*3)+3)&(~3);

	bh.bfType=0x4d42;
	bh.bfSize=BMPHDRSIZE+dstpitch*h;
	bh.bfReserved1=0;  bh.bfReserved2=0;
	bh.bfOffBits=BMPHDRSIZE;
	bh.biSize=40;
	bh.biWidth=w;  bh.biHeight=h;
	bh.biPlanes=0;  bh.biBitCount=24;
	bh.biCompression=BI_RGB;  bh.biSizeImage=0;
	bh.biXPelsPerMeter=0;  bh.biYPelsPerMeter=0;
	bh.biClrUsed=0;  bh.biClrImportant=0;

	if(!littleendian())
	{
		bh.bfType=byteswap16(bh.bfType);
		bh.bfSize=byteswap(bh.bfSize);
		bh.bfOffBits=byteswap(bh.bfOffBits);
		bh.biSize=byteswap(bh.biSize);
		bh.biWidth=byteswap(bh.biWidth);
		bh.biHeight=byteswap(bh.biHeight);
		bh.biPlanes=byteswap16(bh.biPlanes);
		bh.biBitCount=byteswap16(bh.biBitCount);
		bh.biCompression=byteswap(bh.biCompression);
		bh.biSizeImage=byteswap(bh.biSizeImage);
		bh.biXPelsPerMeter=byteswap(bh.biXPelsPerMeter);
		bh.biYPelsPerMeter=byteswap(bh.biYPelsPerMeter);
		bh.biClrUsed=byteswap(bh.biClrUsed);
		bh.biClrImportant=byteswap(bh.biClrImportant);
	}

	writeme(fd, &bh.bfType, sizeof(unsigned short));
	writeme(fd, &bh.bfSize, sizeof(unsigned int));
	writeme(fd, &bh.bfReserved1, sizeof(unsigned short));
	writeme(fd, &bh.bfReserved2, sizeof(unsigned short));
	writeme(fd, &bh.bfOffBits, sizeof(unsigned int));
	writeme(fd, &bh.biSize, sizeof(unsigned int));
	writeme(fd, &bh.biWidth, sizeof(int));
	writeme(fd, &bh.biHeight, sizeof(int));
	writeme(fd, &bh.biPlanes, sizeof(unsigned short));
	writeme(fd, &bh.biBitCount, sizeof(unsigned short));
	writeme(fd, &bh.biCompression, sizeof(unsigned int));
	writeme(fd, &bh.biSizeImage, sizeof(unsigned int));
	writeme(fd, &bh.biXPelsPerMeter, sizeof(int));
	writeme(fd, &bh.biYPelsPerMeter, sizeof(int));
	writeme(fd, &bh.biClrUsed, sizeof(unsigned int));
	writeme(fd, &bh.biClrImportant, sizeof(unsigned int));

	if((tempbuf=(unsigned char *)malloc(dstpitch*h))==NULL)
		_throw("Memory allocation error");

	pixelconvert(buf, f, srcpitch, tempbuf, BMP_BGR, dstpitch, w, h, 
		!srcbottomup);

	if((byteswritten=write(fd, tempbuf, dstpitch*h))!=dstpitch*h)
		_throw(strerror(errno));

	finally:
	if(tempbuf) free(tempbuf);
	if(fd!=-1) close(fd);
	return retcode;
}
コード例 #26
0
int loadbmp(char *filename, unsigned char **buf, int *w, int *h, 
	enum BMPPIXELFORMAT f, int align, int dstbottomup)
{
	int fd=-1, bytesread, srcpitch, srcbottomup=1, srcps, dstpitch,
		retcode=0;
	unsigned char *tempbuf=NULL;
	bmphdr bh;  int flags=O_RDONLY;

	dstbottomup=dstbottomup? 1:0;
	#ifdef _WIN32
	flags|=O_BINARY;
	#endif
	if(!filename || !buf || !w || !h || f<0 || f>BMPPIXELFORMATS-1 || align<1)
		_throw("invalid argument to loadbmp()");
	if((align&(align-1))!=0)
		_throw("Alignment must be a power of 2");
	_unix(fd=open(filename, flags));

	readme(fd, &bh.bfType, sizeof(unsigned short));
	if(!littleendian())	bh.bfType=byteswap16(bh.bfType);

	if(bh.bfType==0x3650)
	{
		_catch(loadppm(&fd, buf, w, h, f, align, dstbottomup, 0));
		goto finally;
	}
	if(bh.bfType==0x3350)
	{
		_catch(loadppm(&fd, buf, w, h, f, align, dstbottomup, 1));
		goto finally;
	}

	readme(fd, &bh.bfSize, sizeof(unsigned int));
	readme(fd, &bh.bfReserved1, sizeof(unsigned short));
	readme(fd, &bh.bfReserved2, sizeof(unsigned short));
	readme(fd, &bh.bfOffBits, sizeof(unsigned int));
	readme(fd, &bh.biSize, sizeof(unsigned int));
	readme(fd, &bh.biWidth, sizeof(int));
	readme(fd, &bh.biHeight, sizeof(int));
	readme(fd, &bh.biPlanes, sizeof(unsigned short));
	readme(fd, &bh.biBitCount, sizeof(unsigned short));
	readme(fd, &bh.biCompression, sizeof(unsigned int));
	readme(fd, &bh.biSizeImage, sizeof(unsigned int));
	readme(fd, &bh.biXPelsPerMeter, sizeof(int));
	readme(fd, &bh.biYPelsPerMeter, sizeof(int));
	readme(fd, &bh.biClrUsed, sizeof(unsigned int));
	readme(fd, &bh.biClrImportant, sizeof(unsigned int));

	if(!littleendian())
	{
		bh.bfSize=byteswap(bh.bfSize);
		bh.bfOffBits=byteswap(bh.bfOffBits);
		bh.biSize=byteswap(bh.biSize);
		bh.biWidth=byteswap(bh.biWidth);
		bh.biHeight=byteswap(bh.biHeight);
		bh.biPlanes=byteswap16(bh.biPlanes);
		bh.biBitCount=byteswap16(bh.biBitCount);
		bh.biCompression=byteswap(bh.biCompression);
		bh.biSizeImage=byteswap(bh.biSizeImage);
		bh.biXPelsPerMeter=byteswap(bh.biXPelsPerMeter);
		bh.biYPelsPerMeter=byteswap(bh.biYPelsPerMeter);
		bh.biClrUsed=byteswap(bh.biClrUsed);
		bh.biClrImportant=byteswap(bh.biClrImportant);
	}

	if(bh.bfType!=0x4d42 || bh.bfOffBits<BMPHDRSIZE
	|| bh.biWidth<1 || bh.biHeight==0)
		_throw("Corrupt bitmap header");
	if((bh.biBitCount!=24 && bh.biBitCount!=32) || bh.biCompression!=BI_RGB)
		_throw("Only uncompessed RGB bitmaps are supported");

	*w=bh.biWidth;  *h=bh.biHeight;  srcps=bh.biBitCount/8;
	if(*h<0) {*h=-(*h);  srcbottomup=0;}
	srcpitch=(((*w)*srcps)+3)&(~3);
	dstpitch=(((*w)*ps[f])+(align-1))&(~(align-1));

	if(srcpitch*(*h)+bh.bfOffBits!=bh.bfSize) _throw("Corrupt bitmap header");
	if((tempbuf=(unsigned char *)malloc(srcpitch*(*h)))==NULL
	|| (*buf=(unsigned char *)malloc(dstpitch*(*h)))==NULL)
		_throw("Memory allocation error");
	if(lseek(fd, (long)bh.bfOffBits, SEEK_SET)!=(long)bh.bfOffBits)
		_throw(strerror(errno));
	_unix(bytesread=read(fd, tempbuf, srcpitch*(*h)));
	if(bytesread!=srcpitch*(*h)) _throw("Read error");

	pixelconvert(tempbuf, BMP_BGR, srcpitch, *buf, f, dstpitch, *w, *h, 
		srcbottomup!=dstbottomup);

	finally:
	if(tempbuf) free(tempbuf);
	if(fd!=-1) close(fd);
	return retcode;
}
コード例 #27
0
ファイル: slpack.c プロジェクト: ebichu/dd-wrt
void _pSLunpack (char *format, SLang_BString_Type *bs)
{
   Format_Type ft;
   unsigned char *b;
   unsigned int len;
   unsigned int num_bytes;

   check_native_byte_order ();

   if (-1 == compute_size_for_format (format, &num_bytes))
     return;

   b = SLbstring_get_pointer (bs, &len);
   if (b == NULL)
     return;

   if (len < num_bytes)
     {
	_pSLang_verror (SL_INVALID_PARM,
		      "unpack format %s is too large for input string",
		      format);
	return;
     }

   while (1 == parse_a_format (&format, &ft))
     {
	char *str, *s;

	if (ft.repeat == 0)
	  continue;

	if (ft.data_type == 0)
	  {			       /* skip padding */
	     b += ft.repeat;
	     continue;
	  }

	if (ft.is_scalar)
	  {
	     SLang_Array_Type *at;
	     SLindex_Type dims;

	     if (ft.repeat == 1)
	       {
		  SLang_Class_Type *cl;

		  cl = _pSLclass_get_class (ft.data_type);
		  memcpy ((char *)cl->cl_transfer_buf, (char *)b, ft.sizeof_type);
		  if (ft.byteorder != NATIVE_ORDER)
		    byteswap (ft.byteorder, (unsigned char *)cl->cl_transfer_buf, ft.sizeof_type, 1);

		  if (-1 == (cl->cl_apush (ft.data_type, cl->cl_transfer_buf)))
		    return;
		  b += ft.sizeof_type;
		  continue;
	       }

	     dims = (SLindex_Type) ft.repeat;
	     at = SLang_create_array (ft.data_type, 0, NULL, &dims, 1);
	     if (at == NULL)
	       return;

	     num_bytes = ft.repeat * ft.sizeof_type;
	     memcpy ((char *)at->data, (char *)b, num_bytes);
	     if (ft.byteorder != NATIVE_ORDER)
	       byteswap (ft.byteorder, (unsigned char *)at->data, ft.sizeof_type, ft.repeat);

	     if (-1 == SLang_push_array (at, 1))
	       return;

	     b += num_bytes;
	     continue;
	  }
	
	/* string type: s, S, or Z */
	if (ft.format_type == 's')
	  len = ft.repeat;
	else
	  len = get_unpadded_strlen ((char *)b, ft.pad, ft.repeat);

	str = SLmalloc (len + 1);
	if (str == NULL)
	  return;
	memcpy ((char *) str, (char *)b, len);
	str [len] = 0;

	/* Avoid a bstring if possible */
	s = SLmemchr (str, 0, len);
	if (s == NULL)
	  {
	     if (-1 == SLang_push_malloced_string (str))
	       return;
	  }
	else
	  {
	     SLang_BString_Type *new_bs;

	     new_bs = SLbstring_create_malloced ((unsigned char *)str, len, 1);
	     if (new_bs == NULL)
	       return;

	     if (-1 == SLang_push_bstring (new_bs))
	       {
		  SLfree (str);
		  return;
	       }
	     SLbstring_free (new_bs);
	  }

	b += ft.repeat;
     }
}
コード例 #28
0
ファイル: slpack.c プロジェクト: ebichu/dd-wrt
static SLang_BString_Type *
pack_according_to_format (char *format, unsigned int nitems)
{
   unsigned int size, num;
   unsigned char *buf, *b;
   SLang_BString_Type *bs;
   Format_Type ft;

   buf = NULL;

   if (-1 == compute_size_for_format (format, &size))
     goto return_error;

   if (NULL == (buf = (unsigned char *) SLmalloc (size + 1)))
     goto return_error;

   b = buf;

   while (1 == parse_a_format (&format, &ft))
     {
	unsigned char *ptr;
	unsigned int repeat;

	repeat = ft.repeat;
	if (ft.data_type == 0)
	  {
	     memset ((char *) b, ft.pad, repeat);
	     b += repeat;
	     continue;
	  }

	if (ft.is_scalar)
	  {
	     unsigned char *bstart;
	     num = repeat;

	     bstart = b;
	     while (repeat != 0)
	       {
		  unsigned int nelements;
		  SLang_Array_Type *at;

		  if (nitems == 0)
		    {
		       _pSLang_verror (SL_INVALID_PARM,
				     "Not enough items for pack format");
		       goto return_error;
		    }

		  if (-1 == SLang_pop_array_of_type (&at, ft.data_type))
		    goto return_error;

		  nelements = at->num_elements;
		  if (repeat < nelements)
		    nelements = repeat;
		  repeat -= nelements;

		  nelements = nelements * ft.sizeof_type;
		  memcpy ((char *)b, (char *)at->data, nelements);

		  b += nelements;
		  SLang_free_array (at);
		  nitems--;
	       }

	     if (ft.byteorder != NATIVE_ORDER)
	       byteswap (ft.byteorder, bstart, ft.sizeof_type, num);

	     continue;
	  }

	/* Otherwise we have a string */
	if (-1 == SLang_pop_bstring (&bs))
	  goto return_error;

	ptr = SLbstring_get_pointer (bs, &num);
	if (repeat < num) num = repeat;
	memcpy ((char *)b, (char *)ptr, num);
	b += num;
	repeat -= num;
	if ((repeat == 0) && (ft.format_type == 'z'))
	  {
	     if (num) *(b-1) = 0;
	  }
	else memset ((char *)b, ft.pad, repeat);
	SLbstring_free (bs);
	b += repeat;
	nitems--;
     }

   *b = 0;
   bs = SLbstring_create_malloced (buf, size, 0);
   if (bs == NULL)
     goto return_error;

   SLdo_pop_n (nitems);
   return bs;

   return_error:
   SLdo_pop_n (nitems);
   if (buf != NULL)
     SLfree ((char *) buf);

   return NULL;
}
コード例 #29
0
ファイル: cubepm.c プロジェクト: satlank/ginnungagap
static void
local_byteswapHeaderValues(cubepm_t cubepm)
{
	for (int i = 0; i < cubepm->numFiles; i++)
		byteswap(cubepm->np_local + i, sizeof(int32_t));
	byteswap(&(cubepm->a), sizeof(float));
	byteswap(&(cubepm->t), sizeof(float));
	byteswap(&(cubepm->tau), sizeof(float));
	byteswap(&(cubepm->nts), sizeof(int32_t));
	byteswap(&(cubepm->dt_f_acc), sizeof(float));
	byteswap(&(cubepm->dt_pp_acc), sizeof(float));
	byteswap(&(cubepm->dt_c_acc), sizeof(float));
	byteswap(&(cubepm->cur_checkpoint), sizeof(int32_t));
	byteswap(&(cubepm->cur_projection), sizeof(int32_t));
	byteswap(&(cubepm->cur_halofind), sizeof(int32_t));
	byteswap(&(cubepm->mass_p), sizeof(float));
}
コード例 #30
0
ファイル: apprentice.c プロジェクト: EliaGeretto/radare2
/*
 * handle a compiled file.
 */
static int apprentice_map(RMagic *ms, struct r_magic **magicp, ut32 *nmagicp, const char *fn) {
	int fd;
	struct stat st;
	ut32 *ptr;
	ut32 version;
	int needsbyteswap;
	char *dbname = NULL;
	void *mm = NULL;

	dbname = mkdbname (fn, 0);
	if (!dbname)
		goto error2;

	if ((fd = r_sandbox_open (dbname, O_RDONLY|O_BINARY, 0)) == -1)
		goto error2;

	if (fstat(fd, &st) == -1) {
		file_error (ms, errno, "cannot stat `%s'", dbname);
		goto error1;
	}
	if (st.st_size < 8) {
		file_error (ms, 0, "file `%s' is too small", dbname);
		goto error1;
	}

#ifdef QUICK
	if ((mm = mmap (0, (size_t)st.st_size, PROT_READ, //OPENBSDBUG  |PROT_WRITE,
	    MAP_PRIVATE|MAP_FILE, fd, (off_t)0)) == MAP_FAILED) {
		file_error (ms, errno, "cannot map `%s'"); //, dbname);
		goto error1;
	}
#define RET	2
#else
	if (!(mm = malloc ((size_t)st.st_size))) {
		file_oomem(ms, (size_t)st.st_size);
		goto error1;
	}
	if (read (fd, mm, (size_t)st.st_size) != (size_t)st.st_size) {
		file_badread(ms);
		goto error1;
	}
#define RET	1
#endif
	*magicp = mm;
	(void)close (fd);
	fd = -1;
	ptr = (ut32 *)(void *)*magicp;

	if (*ptr != MAGICNO) {
		if (swap4(*ptr) != MAGICNO) {
		//OPENBSDBUG file_error(ms, 0, "bad magic in `%s'");
			file_error(ms, 0, "bad magic in `%s'", dbname);
			goto error1;
		}
		needsbyteswap = 1;
	} else needsbyteswap = 0;

	version = needsbyteswap? swap4(ptr[1]): ptr[1];
	if (version != VERSIONNO) {
		file_error(ms, 0, "File %d.%d supports only %d version magic "
		    "files. `%s' is version %d", FILE_VERSION_MAJOR, patchlevel,
		    VERSIONNO, dbname, version);
		goto error1;
	}
	*nmagicp = (ut32)(st.st_size / sizeof (struct r_magic));
	if (*nmagicp > 0)
		(*nmagicp)--;
	(*magicp)++;
	if (needsbyteswap)
		byteswap (*magicp, *nmagicp);
	free (dbname);
	return RET;

error1:
	if (fd != -1)
		(void)close (fd);
	if (mm) {
#ifdef QUICK
		(void)munmap((void *)mm, (size_t)st.st_size);
#else
		free(mm);
#endif
	} else {
		*magicp = NULL;
		*nmagicp = 0;
	}
error2:
	free (dbname);
	return -1;
}