static void swap_csa_tag(siemens_csa_tag * hdr) { swap_4((char *)&hdr->vm, 1); swap_4((char *)&hdr->syngodt, 1); swap_4((char *)&hdr->nitems, 1); swap_4((char *)&hdr->xx, 1); }
u8 *bagComputeFileSignature(char *name, u32 sigID, u8 *secKey) { #ifdef __BAG_BIG_ENDIAN__ swap_4((void*)&sigID); #endif return(ons_compute_signature(name, (u8*)&sigID, sizeof(u32), secKey)); }
u8 *bagComputeMessageDigest(char *file, u32 signatureID, u32 *nBytes) { #ifdef __BAG_BIG_ENDIAN__ swap_4((void*)&signatureID); #endif return(ons_gen_digest(file, (u8*)&signatureID, sizeof(u32), nBytes)); }
static void write_int(FILE *fp, int swap_flag, int x) { if (swap_flag) swap_4(&x); if (fwrite(&x, 4, 1, fp) != 1) G_fatal_error(_("Error writing data")); }
int main(int argc, char *argv[]) { int rv, len, nblocks, done = 0; char * data = NULL; if ( (rv = get_opts(&gopts, argc, argv)) != 0 ) return rv; /* register interrupt trap */ signal(SIGTERM, cleanup); /* so need global variables */ signal(SIGINT, cleanup); /* open or set input file */ if( gopts.in_fname ) { gcontr.infd = open(gopts.in_fname, O_RDONLY); if( gcontr.infd < 0 ) { fprintf(stderr,"** failed to open infile '%s'\n",gopts.in_fname); return 1; } } else gcontr.infd = 0; /* stdin */ /* allocate data block */ data = (char *)malloc(gopts.block_len * sizeof(char)); if( !data ) { fprintf(stderr,"** failed to alloc %d bytes for block\n", gopts.block_len); return 1; } if( gopts.swap == 2 ) swap_2(data, gopts.block_len/2); else if( gopts.swap == 4 ) swap_4(data, gopts.block_len/4); if( (rv = open_serial(&gopts, &gcontr)) != 0 ) return rv; nblocks = 0; while(! done && (gopts.nblocks <= 0 || nblocks < gopts.nblocks) ) { len = set_data(&gopts, &gcontr, data); if( len > 0 ) done = send_serial(&gopts, &gcontr, data, len); else done = 1; nblocks++; if( gopts.ms_sleep ) ms_sleep(gopts.ms_sleep); } free(data); close_ports(); if(gopts.debug) fprintf(stderr,"-d wrote %d blocks of data\n",nblocks); return 0; }
static int swap_element(int nbr, int tokensize) { if(*little_endian) { if(tokensize == FOUR_BYTES) swap_4(nbr); if(tokensize == EIGHT_BYTES) swap_8(nbr); } return nbr; }
static void convert_cell( unsigned char *out_cell, const DCELL in_cell, int is_fp, int bytes, int swap_flag) { if (is_fp) { switch (bytes) { case 4: *(float *) out_cell = (float) in_cell; break; case 8: *(double *) out_cell = (double) in_cell; break; } } else { switch (bytes) { case 1: *(unsigned char *) out_cell = (unsigned char) in_cell; break; case 2: *(short *) out_cell = (short) in_cell; break; case 4: *(int *) out_cell = (int) in_cell; break; #ifdef HAVE_LONG_LONG_INT case 8: *(long long *) out_cell = (long long) in_cell; break; #endif } } if (swap_flag) { switch (bytes) { case 1: break; case 2: swap_2(out_cell); break; case 4: swap_4(out_cell); break; case 8: swap_8(out_cell); break; } } }
void NetSocket::ReceiveData( void ) { char pTempBuffer[RECV_BUFFER_SIZE]; memset( pTempBuffer, 0, sizeof( RECV_BUFFER_SIZE ) ); // int iCurrentReceLen = 0; int result_select = TestingNet( 0, 1000, 1 );// 0 switch( result_select ) { case 0: //cocos2d::CCLog("receive select out time"); break; case 1: { //if( FD_ISSET(m_iDescriptor, &fds ) ) { int iCurrentReceLen = recv(m_iDescriptor, pTempBuffer, RECV_BUFFER_SIZE, 0 ); if( iCurrentReceLen > 0 ) { string tempbuffer(pTempBuffer, iCurrentReceLen ); m_strReceHeap.clear(); m_strReceHeap.append( tempbuffer ); cocos2d::CCLog("receive data ok"); } else if( iCurrentReceLen <= 0 ) { if( errno != EINTR ) { m_bIsConnected = false; this->CloseSocket(); ResetData(); cocos2d::CCLog("receive connect cut"); } } } } break; case NET_SOCKET_ERROR: { m_bIsConnected = false; this->CloseSocket(); ResetData(); cocos2d::CCLog("receive select error"); } break; } if( m_strReceHeap.empty() ) return; if( m_bRevePageageHead == false ) { string strHead; strHead.assign( m_strReceHeap, 0, 4 ); m_strReceHeap.erase( 0,4 ); swap_4( strHead ); int iHeadLen = *((int*)(strHead.c_str())); m_u32RecePageageLen = iHeadLen; m_bRevePageageHead = true; } if( m_bRevePageageHead == true && m_iReceivedLen < (int)m_u32RecePageageLen ) { int iLeavingLen = m_u32RecePageageLen - m_iReceivedLen; if( (int)m_strReceHeap.length() >= iLeavingLen ) { m_strReceivePackage.append( m_strReceHeap, 0, iLeavingLen ); m_strReceHeap.erase( 0, iLeavingLen ); m_iReceivedLen += iLeavingLen; } else { m_strReceivePackage.append( m_strReceHeap, 0, m_strReceHeap.length() ); m_strReceHeap.clear(); m_iReceivedLen += m_strReceHeap.length(); } } if( m_iReceivedLen == m_u32RecePageageLen ) { Recv_struct stRec; stRec.ieffectiveLength = m_u32RecePageageLen; memset(stRec.data, 0, RECV_BUFFER_SIZE ); memcpy(stRec.data, m_strReceivePackage.c_str(), m_u32RecePageageLen ); m_qReceiveData->push( stRec ); ResetReveData(); CCLog("received page finish!"); } }
void NetSocket::SendData( void ) { int test_result = TestingNet( 0, 1000, 2 ); // 0 switch( test_result ) { case 0: cocos2d::CCLog("send test out of time"); break; case 1: { char sendData[SEND_BUFFER_SIZE]; memset(sendData,0,SEND_BUFFER_SIZE); string strToSend = m_strToSend.assign(m_strToSend, m_iHaveSendedSize, m_iToSendSize );//InterceptStr( m_strToSend, m_strToSend.length() ); m_u32SendPackageLen = m_strToSend.length(); int iCurrentSendCount = 0; if( !m_bSendPageageHead ) { memset(sendData,0,SEND_BUFFER_SIZE); PacketProperty pHead; swap_4(m_u32SendPackageLen); pHead.length = m_u32SendPackageLen; //sprintf_s( sendData, "%d", pHead.length ); memcpy(sendData,(char*)&pHead,sizeof(PacketProperty)); memcpy(sendData + sizeof(PacketProperty),strToSend.c_str(),strToSend.length()); m_bSendPageageHead = true; CCLog("++++++++++++++++++++++++++++++++++senddataLengh%d",sizeof(PacketProperty) + strToSend.length() ); iCurrentSendCount = send( m_iDescriptor, sendData, sizeof(PacketProperty) + strToSend.length(),0 ); } else { memcpy(sendData,strToSend.c_str(),strToSend.length()); iCurrentSendCount = send( m_iDescriptor, sendData, strToSend.length(),0 ); } if( iCurrentSendCount == -1 ) { cocos2d::CCLog("send_return = -1 error"); m_bIsConnected = false; ResetSendData(); return; } if( iCurrentSendCount == 0 ) { CCLog("GXB: m m Huan chong qu man"); ResetSendData(); return; } if( iCurrentSendCount > 0 ) { m_iToSendSize -= iCurrentSendCount; m_iHaveSendedSize += iCurrentSendCount; if( m_iToSendSize == 0 ) { m_qSendData->pop(); ResetSendData(); } } } break; case -1: { m_bIsConnected = false; this->CloseSocket(); ResetData(); cocos2d::CCLog("send test error"); } break; } }
static void swap_csa1_header(siemens_csa1_header * hdr) { swap_4((char *)&hdr->ntags, 1); swap_4((char *)&hdr->unused, 1); }
static void swap_csa_item(siemens_csa_item * hdr) { swap_4((char *)&hdr->xx, 4); }
static void read_cell(DCELL *out_cell, int is_integer, int is_signed, int bytes, int byte_swap) { if (fread(in_cell, bytes, 1, fp) != 1) G_fatal_error(_("Error reading binary data")); if (byte_swap) { switch (bytes) { case 1: break; case 2: swap_2(in_cell); break; case 4: swap_4(in_cell); break; case 8: swap_8(in_cell); break; } } if (!is_integer) { switch (bytes) { case 4: *out_cell = (DCELL) *(float *) in_cell; break; case 8: *out_cell = (DCELL) *(double *) in_cell; break; } } else if (is_signed) { switch (bytes) { case 1: *out_cell = (DCELL) *(signed char *) in_cell; break; case 2: *out_cell = (DCELL) *(short *) in_cell; break; case 4: *out_cell = (DCELL) *(int *) in_cell; break; #ifdef HAVE_LONG_LONG_INT case 8: *out_cell = (DCELL) *(long long *) in_cell; break; #endif } } else { switch (bytes) { case 1: *out_cell = (DCELL) *(unsigned char *) in_cell; break; case 2: *out_cell = (DCELL) *(unsigned short *) in_cell; break; case 4: *out_cell = (DCELL) *(unsigned int *) in_cell; break; #ifdef HAVE_LONG_LONG_INT case 8: *out_cell = (DCELL) *(unsigned long long *) in_cell; break; #endif } } }
/* ---------------------------------------------------------------------- * read one set of motion parameters (extras?) and store in structure * * return 1 : finished * 0 : have data: continue * -1 : error * ---------------------------------------------------------------------- */ int read_socket(optiondata * opt, port_list * plist, motparm * mp) { static char * outstring = NULL; static int oslen = 0; int rv, len; if ( (rv = test_socket(plist->tdata_sd)) < 0 ) return -1; else if ( rv == 1 ) { if ( opt->debug > 0 ) fprintf(stderr,"++ found close request, mpcount = %d\n", mp->nread); return 1; } /* get motion params */ len = mp->nvals * sizeof(float); if ( (rv = recv(plist->tdata_sd, (void *)mp->data, len, 0)) < len ) { fprintf(stderr,"** read only %d of %d bytes on socket\n", rv, len); perror("recv mot parm"); return -1; } if ( opt->show_times && opt->debug > 2 ) show_time("received mp data"); if ( opt->swap ) swap_4(mp->data, mp->nvals); /* get extra floats */ if( mp->nex > 0 ) { len = mp->nex * sizeof(float); if ( (rv = recv(plist->tdata_sd, (void *)mp->extras, len, 0)) < len ) { fprintf(stderr,"** read only %d of %d Ebytes on socket\n", rv, len); perror("recv extra floats"); return -1; } if ( opt->swap ) swap_4(mp->extras, mp->nex); } mp->nread++; if ( opt->show_times ) { char mesg[32]; sprintf(mesg, "received mp data #%03d", mp->nread); show_time(mesg); } if ( opt->debug > 2 || opt->disp_all ) { rv = format_output(opt, mp, &outstring, &oslen); if( rv ) { fprintf(stderr,"** failed to format output string\n"); if( outstring ) { free(outstring); outstring = NULL; } return 1; } /* will probably want to send elsewhere, later */ fputs(outstring,stderr); fflush(stderr); /* may get buffered */ } return 0; }
/* ---------------------------------------------------------------------- * block until data comes return the open socket * * we expect to read g_magic_hi * ---------------------------------------------------------------------- */ int wait_for_socket(optiondata *opt, port_list * plist, motparm * mp) { struct sockaddr_in sin; char data[8]; int sd, len, ver, nex; len = sizeof(sin); /* block until a connection is made */ if ( (sd = accept(plist->tserver_sd, (struct sockaddr *)&sin, &len)) == -1 ) { perror("wait for socket: accept"); return -1; } plist->tdata_sd = sd; if ( opt->debug > 0 ) fprintf(stderr,"++ accepting call from '%s'\n",inet_ntoa(sin.sin_addr)); if ( opt->show_times ) show_time("accepted connection"); if ( (len = recv(sd, data, g_magic_len, 0)) == -1 ) { perror("wait for socket: recv"); return -1; } /* check the first 3 bytes of magic_hi, with the 4th determining version */ if ( strncmp(data, g_magic_hi, g_magic_len-1) != 0 ) { fprintf(stderr, "** bad data on socket: 0x%02hhx%02hhx%02hhx%02hhx\n", data[0], data[1], data[2], data[3] ); return -1; } /* Hey, they said the magic word! */ if ( opt->debug > 0 ) fprintf(stderr, "++ received hello on socket: 0x%02hhx%02hhx%02hhx%02hhx\n", data[0], data[1], data[2], data[3]); /* check the hello version */ ver = data[3] - (char)0xab; if( ver == 1 || ver == 2 ) { /* version 1: also receive num_extra over socket */ /* version 2: also receive disp_all voxels over socket */ if ( (len = recv(sd, (void *)&nex, sizeof(int), 0)) == -1 ) { perror("wait for socket: recv"); return -1; } else if ( len != sizeof(int) ) { fprintf(stderr,"** received only %d of 4 bytes for nextra\n",len); return -1; } if ( opt->show_times ) show_time("received num_extra"); /* have num extra, apply it */ if ( opt->swap ) swap_4(&nex, 1); /* modify num_extras and disp_all, depending on the version */ if( ver == 2 ) { opt->disp_all = 1; nex *= 8; } else opt->disp_all = 0; fprintf(stderr,"++ hello version %d, received nextra = %d (was %d)\n", ver, nex, mp->nex); /* we may want to alloc some/more/less memory for this */ if( mp->nex != nex && alloc_extras(mp, nex) ) return -1; } else if ( ver != 0 ) { fprintf(stderr,"** bad magic version from socket: %d\n",ver); return -1; } /* else, default hello version */ return 0; }
int main( int argc , char *argv[] ) { FILE *imfile ; int length , skip , swap=0 , gg ; char orients[8] , str[8] ; int nx , ny , bpp , cflag , hdroff , verb=0 , iarg=1 ; /*------ help? ------*/ if( argc < 2 || strcmp(argv[1],"-help") == 0 ){ printf("Usage: ge_header [-verb] file ...\n" "Prints out information from the GE image header of each file.\n" "Options:\n" " -verb: Print out some probably useless extra stuff.\n" ) ; exit(0) ; } if( strcmp(argv[iarg],"-verb") == 0 ){ verb++ ; iarg++ ; } if( strcmp(argv[iarg],"-verb") == 0 ){ verb++ ; iarg++ ; } /*----- loop over input files -----*/ for( gg=iarg ; gg < argc ; gg++ ){ imfile = fopen( argv[gg] , "r" ) ; if( imfile == NULL ){ printf("-----------------------------------\n" "Can't open file %s\n" , argv[gg] ) ; continue ; /* skip to next file */ } fseek( imfile , 0L , SEEK_END ) ; /* get the length of the file */ length = ftell( imfile ) ; /* (the AJ way) */ if( length < 1024 ){ printf("-----------------------------------\n" "File %s is too short to be an image\n" , argv[gg] ) ; fclose(imfile) ; continue ; } /*--- 03 Dec 2001: check for GEMS format file "IMGF" ---*/ /*[[[ Information herein from Medical Image Format FAQ ]]]*/ strcpy(str,"JUNK") ; /* initialize string */ rewind(imfile) ; fread(str,1,4,imfile) ; /* check for "IMGF" at start of file */ if( str[0]=='I' && str[1]=='M' && str[2]=='G' && str[3]=='F' ){ /* good */ /*-- read next 5 ints (after the "IMGF" string) --*/ fread( &skip , 4,1, imfile ) ; /* offset into file of image data */ fread( &nx , 4,1, imfile ) ; /* x-size */ fread( &ny , 4,1, imfile ) ; /* y-size */ fread( &bpp , 4,1, imfile ) ; /* bits per pixel (should be 16) */ fread( &cflag, 4,1, imfile ) ; /* compression flag (1=uncompressed)*/ /*-- check if nx is funny --*/ if( nx < 0 || nx > 8192 ){ /* have to byte swap these 5 ints */ swap = 1 ; /* flag to swap data, too */ swap_4(&skip); swap_4(&nx); swap_4(&ny); swap_4(&bpp); swap_4(&cflag); if( nx < 0 || nx > 8192 || ny < 0 || ny > 8192 ){ printf("-----------------------------------\n" "File %s: illegal nx hand/or ny in header\n" , argv[gg] ) ; fclose(imfile) ; continue ; } } else { swap = 0 ; /* data is ordered for this CPU */ } printf("-----------------------------------\n" "File %s\n" " nx=%d ny=%d skip=%d swap=%s compressed=%s bits-per-pixel=%d\n", argv[gg] , nx , ny , skip , (swap) ? "YES" : "NO" , (cflag==1) ? "NO" : "YES" , bpp ) ; /*-- try to read image header data as well --*/ length = fseek( imfile , 148L , SEEK_SET ) ; /* magic GEMS offset */ if( length == 0 ){ /* seek was good */ fread( &hdroff , 4,1 , imfile ) ; /* location of image header */ if( swap ) swap_4(&hdroff) ; if( hdroff > 0 ){ /* read from image header */ float dx,dy,dz, xyz[9], xx,yy,zz, tr ; int itr, ii,jj,kk ; /*-- get voxel grid sizes --*/ fseek( imfile , hdroff+26 , SEEK_SET ) ; /* dz */ fread( &dz , 4,1 , imfile ) ; fseek( imfile , hdroff+50 , SEEK_SET ) ; /* dx and dy */ fread( &dx , 4,1 , imfile ) ; fread( &dy , 4,1 , imfile ) ; if( swap ){ swap_4(&dx); swap_4(&dy); swap_4(&dz); } printf(" dx=%g dy=%g dz=%g" , dx,dy,dz ) ; /* grid orientation: from 3 sets of LPI corner coordinates: */ /* xyz[0..2] = top left hand corner of image (TLHC) */ /* xyz[3..5] = top right hand corner of image (TRHC) */ /* xyz[6..8] = bottom right hand corner of image (BRHC) */ /* GEMS coordinate orientation here is LPI */ fseek( imfile , hdroff+154 , SEEK_SET ) ; /* another magic number */ fread( xyz , 4,9 , imfile ) ; if( swap ){ swap_4(xyz+0); swap_4(xyz+1); swap_4(xyz+2); swap_4(xyz+3); swap_4(xyz+4); swap_4(xyz+5); swap_4(xyz+6); swap_4(xyz+7); swap_4(xyz+8); } /* x-axis orientation */ /* ii determines which spatial direction is x-axis */ /* and is the direction that has the biggest change */ /* between the TLHC and TRHC */ dx = fabs(xyz[3]-xyz[0]) ; ii = 1 ; dy = fabs(xyz[4]-xyz[1]) ; if( dy > dx ){ ii=2; dx=dy; } dz = fabs(xyz[5]-xyz[2]) ; if( dz > dx ){ ii=3; } dx = xyz[ii+2]-xyz[ii-1] ; if( dx < 0. ){ ii = -ii; } switch( ii ){ case 1: orients[0]= 'L'; orients[1]= 'R'; break; /* Left to Right */ case -1: orients[0]= 'R'; orients[1]= 'L'; break; /* Right to Left */ case 2: orients[0]= 'P'; orients[1]= 'A'; break; /* Posterior to Anterior */ case -2: orients[0]= 'A'; orients[1]= 'P'; break; /* Anterior to Posterior */ case 3: orients[0]= 'I'; orients[1]= 'S'; break; /* Inferior to Superior */ case -3: orients[0]= 'S'; orients[1]= 'I'; break; /* Superior to Inferior */ default: orients[0]='\0'; orients[1]='\0'; break; /* should never happen */ } /* y-axis orientation */ /* jj determines which spatial direction is y-axis */ /* and is the direction that has the biggest change */ /* between the BRHC and TRHC */ dx = fabs(xyz[6]-xyz[3]) ; jj = 1 ; dy = fabs(xyz[7]-xyz[4]) ; if( dy > dx ){ jj=2; dx=dy; } dz = fabs(xyz[8]-xyz[5]) ; if( dz > dx ){ jj=3; } dx = xyz[jj+5]-xyz[jj+2] ; if( dx < 0. ){ jj = -jj; } switch( jj ){ case 1: orients[2] = 'L'; orients[3] = 'R'; break; case -1: orients[2] = 'R'; orients[3] = 'L'; break; case 2: orients[2] = 'P'; orients[3] = 'A'; break; case -2: orients[2] = 'A'; orients[3] = 'P'; break; case 3: orients[2] = 'I'; orients[3] = 'S'; break; case -3: orients[2] = 'S'; orients[3] = 'I'; break; default: orients[2] ='\0'; orients[3] ='\0'; break; } orients[4] = '\0' ; /* terminate orientation string */ kk = 6 - abs(ii)-abs(jj) ; /* which spatial direction is z-axis */ /* where 1=LR, 2=PA, 3=IS */ /* (can't tell orientation from 1 slice) */ zz = xyz[kk-1] ; /* z-coordinate of this slice */ xx = xyz[abs(ii)-1] ; yy = xyz[abs(jj)-1] ; printf(" zoff=%g orient=%s xoff=%g yoff=%g", zz,orients,xx,yy ) ; /*-- get TR in seconds --*/ fseek( imfile , hdroff+194 , SEEK_SET ) ; fread( &itr , 4,1 , imfile ) ; /* note itr is an int */ if( swap ) swap_4(&itr) ; tr = 1.0e-6 * itr ; /* itr is in microsec */ printf(" TR=%gs",tr) ; /*-- get TE in milliseconds --*/ fseek( imfile , hdroff+202 , SEEK_SET ) ; fread( &itr , 4,1 , imfile ) ; /* itr is an int, in microsec */ if( swap ) swap_4(&itr) ; printf(" TE=%gms",1.0e-3*itr) ; /*-- end of image header printouts --*/ printf("\n") ; /*-- verbosity? --*/ if( verb ) printf(" TLHC= %g %g %g\n" " TRHC= %g %g %g\n" " BRHC= %g %g %g\n" , xyz[0],xyz[1],xyz[2],xyz[3],xyz[4],xyz[5],xyz[6],xyz[7],xyz[8] ) ; /*-- date/time stamp */ if( verb ){ int dt1 , dt2 ; short in1,in2 ; fseek( imfile , hdroff+10 , SEEK_SET ) ; fread( &in1 , 2,1 , imfile ) ; fread( &in2 , 2,1 , imfile ) ; fread( &dt1 , 4,1 , imfile ) ; fread( &dt2 , 4,1 , imfile ) ; if( swap ){ swap_4(&dt1);swap_4(&dt2);swap_2(&in1);swap_2(&in2); } printf(" image date/time stamps=%d,%d series#=%d im#=%d\n",dt1,dt2,in1,in2) ; } /* end of philately */ } /* end of actually reading image header */ } /* end of trying to read image header */ /* maybe read series header */ if( verb > 1 ){ int dt1 , dt2 ; short in1,in2 ; fseek( imfile , 140L , SEEK_SET ) ; fread( &hdroff , 4,1 , imfile ) ; /* location of series header */ if( swap ) swap_4(&hdroff) ; if( hdroff > 0 ){ fseek( imfile , hdroff+10 , SEEK_SET ) ; fread( &in1 , 2,1 , imfile ) ; fread( &dt1 , 4,1 , imfile ) ; fread( &dt2 , 4,1 , imfile ) ; if( swap ){ swap_4(&dt1);swap_4(&dt2);swap_2(&in1); } printf(" series date/time stamps=%d,%d series#=%d\n",dt1,dt2,in1) ; } } /* end of series header */ /***************************************************************** To actually read image data, do something like this: int npix=nx*ny , nb=bpp/8 ; data = malloc(nb*npix) ; fseek( imfile , skip , SEEK_SET ) ; fread( data , 1 , nb*npix , imfile ) ; if( swap ){ switch( nb ){ case 2: swap_twobytes ( npix , data ); break; case 4: swap_fourbytes( npix , data ); break; } } The latter 2 swap functions are in mri_swapbytes.c ******************************************************************/ } else { /*-------------- not a GEMS file --------------*/ printf("-----------------------------------\n" "File %s is not a GE image file [doesn't start with IMGF]\n" , argv[gg] ) ; } fclose(imfile) ; /* done with this file */ } /* end of loop over input files */ exit(0) ; }