一个小命令行程序

时间:2022-09-02 20:41:40
//这段程序的用途是在一个二进制文件的头部添加一个128字节长的空间,并将用户输入的一组16进制字符串填充至128字节空间的头部,虽然还有瑕疵,但是整份代码看起来很顺,对不对?
#include <stdio.h>
#include <string.h>

void printUsage();
char *hex2Bin(unsigned char *bin, char *hex);
char *addHeaderToFile(char *filename, unsigned char *bin);

int main(int argc, char *argv[])
{
    char *error = NULL;

    if(argc != 3)
    {
        printUsage();
        return -1;
    }

    unsigned char buf[128];
    error = hex2Bin(buf, argv[2]);
    if(error != NULL) goto EXIT;

    error = addHeaderToFile(argv[1], buf);
    if(error != NULL) goto EXIT;

    return 0;

EXIT:
    printf(error);
    return -1;
}

void printUsage()
{
    printf("Usage:\n");
    printf("AddHeader <filename> <hex_str>\n");
    printf("...\n");
    printf("Add a 128 bytes header to file.");
}

char *hex2Bin(unsigned char *bin, char *hex)
{
    int i;

    memset(bin, 0, 128); //reset hexfer

    if(hex == NULL) return NULL;

    //hex string validate
    if(strlen(hex)>128*2) return "hex string is too long!";

    //hex 2 bin translate
    unsigned char data = 0;
    for(i = 0; i<strlen(hex); ++i)
    {
        data<<=4;
        if(hex[i]>='0' && hex[i]<='9') data |= hex[i] - '0';
        else if(hex[i]>='a' && hex[i]<='f') data |= hex[i] - 'a' + 10;
        else if(hex[i]>='A' && hex[i]<='F') data |= hex[i] - 'A' + 10;
        else return "illegal character detected!";
        if(i%2) *(bin++) = data;
    }
    if(i%2)
    {
        data = 0x0f&data;
        *(bin++) = data;
    }
    return NULL;
}

char *addHeaderToFile(char *filename, unsigned char *bin)
{
    int len;

    char tmp_file[1024];
    strcpy(tmp_file, filename);
    strcat(tmp_file, ".tmp");

    FILE *in = fopen(filename, "rb");
    FILE *out = fopen(tmp_file, "wb");
    if(in == NULL || out == NULL) return "file access error!";

    fwrite(bin, 128, 1, out);
    while(!feof(in))
    {
        len = fread(bin, 1, 128, in);
        fwrite(bin, 1, len, out);
    }
    fclose(out);
    fclose(in);
    remove(filename);
    rename(tmp_file, filename);
    return NULL;
}