/*
* This software is free, open source software.
* The copyright of the software (including the source code and the binary release version) is owned by the public.
* You are free to use and distribute the software.
* You may also use the software (including source code and binary release versions) in any form or for any purpose, without any copyright restrictions.
* =====================
* Author: Sun Mingpao
* Email: [Email protected]
*/
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define Errquit (FMT, args ...) \
Do \
{ \
printf ("errquit:%s (%d)-%s:\n" FMT "\n:%s\n", __file__,__line__,__function__,# #args, Strerror (errno)); \
Exit (1); \
} while (0)
void Bin2text (int fd_s, int fd_d)
{
Char file_data_s[1024];
Char file_data_d[sizeof (file_data_s);
int I, Read_len, Write_len;
while ((Read_len=read (fd_s, file_data_s, sizeof (file_data_s))) >0)
{
for (i = 0; i < Read_len; i++)
{
sprintf (File_data_d + i*3, "%02HHX", File_data_s[i]);
}
Write_len=write (Fd_d, File_data_d, read_len*3);
if (write_len<0)
Errquit ("Write Dest file failed");
}
if (read_len<0)
Errquit ("read src file failed");
}
void Text2bin (int fd_s, int fd_d)
{
Char file_data_s[1024*3];
Char file_data_d[1024];
int I, Read_len, Write_len;
while ((Read_len=read (fd_s, file_data_s, sizeof (file_data_s))) >0)
{
for (i = 0; i < Read_len; i+=3)
{
FILE_DATA_S[I+2] = 0;
File_data_d[i/3]=strtol (file_data_s + i, NULL, 16);
}
Write_len=write (Fd_d, File_data_d, READ_LEN/3);
if (write_len<0)
Errquit ("Write Dest file failed");
}
if (read_len<0)
Errquit ("read src file failed");
}
Char *src_file;
Char *output_file;
Char *trans_type;
void Parse_args (int argc, char *argv[])
{
int opt;
while (opt = getopt (argc, argv, "O:o:s:s:t:t:")))!! =-1)
{
Switch (OPT)
{
Case ' s ':
Case ' S ':
Src_file = Optarg;
Break
Case ' O ':
Case ' O ':
Output_file = Optarg;
Break
Case ' t ':
Case ' T ':
Trans_type = Optarg;
Break
Default:/* '? ' * *
printf ("Usage:%s \ n",
Argv[0]);
}
}
}
int main (int argc, char *argv[])
{
int fd_s, fd_d;
Parse_args (argc, argv);
fd_s = open (Src_file, o_rdonly);
if (fd_s<0)
Errquit ("Open src file failed");
Fd_d = open (Output_file, o_wronly| o_append| O_creat
, S_IRUSR | S_IWUSR | S_IRGRP);
if (fd_d<0)
Errquit ("Open dest file failed");
if (memcmp (Trans_type, "b2t", 3) ==0)
Bin2text (fd_s, fd_d);
Else
Text2bin (fd_s, fd_d);
return 0;
}
binary files with 16 hexadecimal text file Transfer tool