98 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			C
		
	
	
		
			Executable File
		
	
	
	
	
			
		
		
	
	
			98 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			C
		
	
	
		
			Executable File
		
	
	
	
	
| #include <stdio.h>
 | |
| #include <stdint.h>
 | |
| #include <unistd.h>
 | |
| #include <fcntl.h>
 | |
| #include <string.h>
 | |
| #include <malloc.h>
 | |
| #include <stdlib.h>
 | |
| 
 | |
| void print_binary(FILE *fp, uint32_t n)
 | |
| {
 | |
|     uint8_t arr[32] = {0};
 | |
|     int len = 0;
 | |
|     for(len = 0; len < 32; len++){
 | |
|         arr[len] = (n&1)+'0';
 | |
|         n = n>>1;
 | |
| 
 | |
|     }
 | |
| 
 | |
|     len--;
 | |
|     while (len >= 0 ){
 | |
|         fprintf(fp,"%c", arr[len]);
 | |
|         len--;
 | |
|     }
 | |
|     fprintf(fp,"\n");
 | |
| }
 | |
| 
 | |
| int main( int argc, char *argv[] )
 | |
| {
 | |
|     uint8_t buf[128];
 | |
|     FILE *fin = NULL, *fout = NULL;
 | |
|     int i = 0;
 | |
|     uint8_t val;
 | |
|     off_t filesize, offset;
 | |
|     size_t st = 0;
 | |
|     uint32_t tmp = 0;
 | |
| 
 | |
|     if( ( fin = fopen( argv[1], "rb" ) ) == NULL ){
 | |
|         printf("fopen(%s,r) failed\n", argv[1] );
 | |
|         goto exit;
 | |
|     }
 | |
| 
 | |
|     if( ( fout = fopen( argv[2], "w+" ) ) == NULL ){
 | |
|         printf("fopen(%s,w+) failed\n", argv[2] );
 | |
|         goto exit;
 | |
|     }
 | |
| 
 | |
|     if( argc > 3){
 | |
|         filesize = atoi(argv[3]);
 | |
|     } else{
 | |
|         if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 ){
 | |
|             perror( "lseek" );
 | |
|             goto exit;
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     if( fseek( fin, 0, SEEK_SET ) < 0 ){
 | |
|          printf( "fseek(0,SEEK_SET) failed\n" );
 | |
|          goto exit;
 | |
|     }
 | |
|     memset(buf, 0x0,128);
 | |
|     for( offset = 0; offset < filesize; offset += 1 ){
 | |
|         st = fread( buf, 1, 4, fin );
 | |
|         if ( st == 0) {
 | |
|             break;
 | |
|         }
 | |
|         #if 0
 | |
|         for(i = 0; i < 4; i++){
 | |
|             val = buf[3-i];
 | |
|             printf("%02x\n", val);
 | |
|             printf("%02x\n", val);
 | |
|             fprintf(fout, "%02x\n", val);
 | |
|         }
 | |
|         #else
 | |
|         if ( st == 4) {
 | |
|             tmp = (uint32_t)(((uint32_t)buf[3] <<24)|((uint32_t)buf[2]<<16) |(uint32_t)(buf[1]<<8)|(uint32_t)buf[0]);
 | |
|         } else if (st == 3) {
 | |
|             tmp = (uint32_t)(((uint32_t)buf[2]<<16) |(uint32_t)(buf[1]<<8)|(uint32_t)buf[0]);
 | |
|         } else if (st == 2) {
 | |
|             tmp = (uint32_t)((uint32_t)(buf[1]<<8)|(uint32_t)buf[0]);
 | |
|         } else if (st == 1) {
 | |
|             tmp = (uint32_t)((uint32_t)buf[0]);
 | |
|         }
 | |
|         //printf("%02x %02x %02x %02x\n", buf[3], buf[2], buf[1], buf[0]);
 | |
|         //printf("%08x\n", tmp);
 | |
|         fprintf(fout, "%08x\n", tmp);
 | |
|         #endif
 | |
|     }
 | |
| 
 | |
| exit:
 | |
|     if(fin){
 | |
|         fclose(fin);
 | |
|     }
 | |
| 
 | |
|     if(fout){
 | |
|         fclose(fout);
 | |
|     }
 | |
| }
 |