C++ 可执行文件输入参数

1. 输入参数选项处理

在生成 elf 可执行文件时,对输入的参数做解析。

解析代码:

int main(int argc, char *argv[])  {
        std::string source, output;
    int isencode = 0;
    int c;
    while((c = getopt_long(argc, argv, short_options, long_options, nullptr)) != -1) {
        printf("[+]get options : %d \n", c);
        switch (c) {
            case 'e':
                source = optarg;
                isencode = 1;
                break;
            case 'd':
                source = optarg;
                isencode = 2;
                break;
            case 'o':
                output = optarg;
                break;
            case 'h':
                useage();
                break;
            default:
                break;
        }
    }
}

以上就是对参数命令的解析。同时需要设置命令选项:

const char* short_options = "hdm:d:e:o:";    // 带参数的,要使用的命令,需要在这里声明
const struct option long_options[] = {
        {"help", 0, NULL, 'h'},
        {"decode", 1, NULL, 'd'},
        {"encode", 1, NULL, 'e'},
    {"output", 1, NULL, 'o'},
        {nullptr, 0, nullptr, 0}
};
void useage();

void useage() {
    printf(" Options are:\n");

    printf("  -d --decode                                DecryptionAES  \n");
    printf("  -e --memso memBaseAddr(16bit format)       EncryptionAES \n");
    printf("  -o --output generateFilePath               Generate file path\n");
    printf("  -h --help                                  Display this information\n");
}

long_options[] 对应 getopt_long 函数,这个标准函数如下:

参数longopts,其实是一个结构的实例:
struct option {
const char *name; //name表示的是长参数名
int has_arg; //has_arg有3个值,no_argument(或者是0),表示该参数后面不跟参数值
// required_argument(或者是1),表示该参数后面一定要跟个参数值
// optional_argument(或者是2),表示该参数后面可以跟,也可以不跟参数值
int *flag;
//用来决定,getopt_long()的返回值到底是什么。如果flag是null(通常情况),则函数会返回与该项option匹配的val值;如果flag不是NULL,则将val值赋予flag所指向的内存,并且返回值设置为0。
int val; //和flag联合决定返回值
}

就是为 1 时,后面可跟参数。

2. 完整示例如下:

#include <iostream>  
#include "AES.h"  
#include "Base64.h"  

#include <getopt.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <sys/types.h>
#include <unistd.h>

using namespace std;

const char* short_options = "hdm:d:e:o:";
const struct option long_options[] = {
        {"help", 0, NULL, 'h'},
        {"decode", 1, NULL, 'd'},
        {"encode", 1, NULL, 'e'},
        {"output", 1, NULL, 'o'},
        {nullptr, 0, nullptr, 0}
};
void useage();
int main(int argc, char *argv[])  
{  
/*    string str1;
    cin >> str1; 
    cout << "加密前:" << str1 << endl;
    string str2 = EncryptionAES(str1);  
    cout << "加密后:" << str2 << endl;  
    string str3 = DecryptionAES(str2);  
    cout << "解密后:" << str3 << endl;  */
    std::string source, output;
    int isencode = 0;
    int c;
    while((c = getopt_long(argc, argv, short_options, long_options, nullptr)) != -1) {
        printf("[+]get options : %d \n", c);
        switch (c) {
            case 'e':
                source = optarg;
                isencode = 1;
                break;
            case 'd':
                source = optarg;
                isencode = 2;
                break;
            case 'o':
                output = optarg;
                break;
            case 'h':
                useage();
                break;
            default:
                break;
        }
    }
    printf("[-]open  %s gooooo, cnum : %d \n",source.c_str(), c);
    
    int fd=open(source.c_str(), O_RDWR);
    if(!fd) {
        printf("source so file cannot found!!!\n");
        return -1;
    }
    struct stat buf={0};
    printf("step 1 \n");
    int status=stat(source.c_str(),&buf);            // 获取文件信息
    printf("[+]fstat %s path, size : %d \n",source.c_str(), buf.st_size);
    if (status==-1) {
        printf("[-]fstat %s failed \n",source.c_str());
        return -1;
    }
    printf("step 2 \n");
    int g_dexSize = buf.st_size;
    printf("step 3 \n");
    char *readbuf = new char[g_dexSize];
    printf("step 4 \n");
    int ret = read(fd, readbuf, g_dexSize);
    printf("step 5 ret: %d \n", ret);
    if (ret != g_dexSize) {
        printf("[-]read %d failed",g_dexSize);
    }
    printf("step 6 \n");
    char *rbuf = readbuf;
    string s = rbuf;
    string rstr = "";
    int lenss = s.length();
    printf("step 7 lensss : %d\n", lenss);
    if (isencode == 1) {
        rstr = EncryptionAES(s);
        printf("step 8 rstr\n");
    }
    if (isencode == 2) {
        rstr = DecryptionAES(s);
    }
    printf("step 9 \n");
    const char *t = rstr.data();
    printf("step 10 t : %c \n", t);
    int len = rstr.length();
    printf("[+]encode %d length \n", len);
    
    
    auto wfd=fopen(output.c_str(), "wb+");
    if(nullptr == wfd) {
        printf("output so file cannot write !!!\n");
        return -1;
    }
    printf("step 11 wfd\n");
    //int wsize = write(wfd, t, len);
    int wsize = fwrite(t, len, 1, wfd);
    printf("step 12 wfd : %d \n", wsize);
    if (wsize != 1) {
        printf("[-]encode %d error", wsize);
    }
    close(fd);
    fclose(wfd);
    printf("step 13 end \n");
    return 0;
}

void useage() {
    printf(" Options are:\n");

    printf("  -d --decode                                DecryptionAES  \n");
    printf("  -e --memso memBaseAddr(16bit format)       EncryptionAES \n");
    printf("  -o --output generateFilePath               Generate file path\n");
    printf("  -h --help                                  Display this information\n");
}
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,110评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,443评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,474评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,881评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,902评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,698评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,418评论 3 419
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,332评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,796评论 1 316
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,968评论 3 337
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,110评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,792评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,455评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,003评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,130评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,348评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,047评论 2 355

推荐阅读更多精彩内容