首页 > 其他分享 >2023-2024-1 20211327 myxxd(课上测试)

2023-2024-1 20211327 myxxd(课上测试)

时间:2023-11-29 09:15:52浏览次数:49  
标签:string read hex 2024 课上 file 2023 data size

myxxd(课上测试)

学***d的使用,提交至少3个应用截图



xxd的主要功能是什么?需要使用什么系统调用来实现?写出你的推导过程,命令

xxd 主要用于查看文件的十六进制表示,并提供了一些额外的功能,如生成C语言风格的数组表示。它的主要功能包括:

  • 查看文件的十六进制表示: 显示文件内容的十六进制表示,每行显示16个字节,同时还显示ASCII字符。

  • 生成C语言风格的数组: 生成一个包含文件内容的C语言数组,可用于在程序中嵌入二进制数据。

  • 逆向操作: 将十六进制表示还原为二进制数据。

xxd 主要使用文件I/O和格式化输出相关的系统调用来实现其功能。推导过程

1.查看文件的十六进制表示:

  • 使用系统调用 open 打开文件。
  • 使用 read 从文件中读取数据。
  • 格式化数据并输出,可能使用系统调用 write
xxd filename

2.生成C语言风格的数组:

  • 使用系统调用 open 打开文件。
  • 使用 read 从文件中读取数据。
  • 格式化数据并输出,可能使用系统调用 write
xxd -i filename

3.逆向操作 - 将十六进制还原为二进制:

  • 使用系统调用 open 创建一个新文件。
  • 使用 write 将十六进制数据写入新文件。
xxd -r -p hexfile > binaryfile

写出实现xxd的伪代码

function xxd(filename):
    file = open(filename, 'rb')  # 使用二进制模式打开文件
    address = 0  # 内存地址,用于显示在输出中
    while True:
        data = file.read(16)  # 从文件读取16个字节
        if not data:
            break  # 文件读取完毕
        hex_string = to_hex_string(data)  # 将字节数据转换为十六进制字符串
        ascii_string = to_ascii_string(data)  # 将字节数据转换为ASCII字符串
        print(f'{address:08x}: {hex_string}  {ascii_string}')
        address += len(data)
    file.close()

function xxd_i(filename):
    file = open(filename, 'rb')  # 使用二进制模式打开文件
    data = file.read()
    hex_string = to_hex_string(data)  # 将字节数据转换为十六进制字符串
    print(f'static unsigned char {filename}_data[] = {{ {hex_string} }};')
    file.close()

function xxd_r(hexfile, binaryfile):
    hex_data = read_hex_file(hexfile)
    binary_data = hex_to_binary(hex_data)
    write_binary_file(binaryfile, binary_data)

function to_hex_string(data):
    return ' '.join(f'{byte:02x}' for byte in data)

function to_ascii_string(data):
    return ''.join(chr(byte) if 32 <= byte <= 126 else '.' for byte in data)

function read_hex_file(hexfile):
    with open(hexfile, 'r') as file:
        return file.read().replace(' ', '').replace('\n', '')

function hex_to_binary(hex_data):
    return bytes.fromhex(hex_data)

function write_binary_file(binaryfile, binary_data):
    with open(binaryfile, 'wb') as file:
        file.write(binary_data)

编写myxxd实现xxd的功能

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BUFFER_SIZE 16

void print_hex(const unsigned char *data, size_t size, size_t offset) {
    size_t i;

    printf("%08lx: ", offset);

    for (i = 0; i < size; i++) {
        printf("%02x ", data[i]);

        if ((i + 1) % 8 == 0)
            printf(" ");
    }

    for (; i < BUFFER_SIZE; i++) {
        printf("   ");
        if ((i + 1) % 8 == 0)
            printf(" ");
    }

    printf(" ");

    for (i = 0; i < size; i++) {
        if (data[i] >= 32 && data[i] <= 126)
            printf("%c", data[i]);
        else
            printf(".");
    }

    printf("\n");
}

void display_file(const char *filename, int display_type, size_t num_bytes) {
    FILE *file = fopen(filename, "rb");

    if (file == NULL) {
        perror("Error opening file");
        exit(EXIT_FAILURE);
    }

    fseek(file, 0, SEEK_END);
    long len = ftell(file);
    fseek(file, 0, SEEK_SET);

    unsigned char buffer[BUFFER_SIZE];
    size_t offset = 0;

    if (display_type == 1) {
        printf("File length: %ld\n", len);
        printf("Displaying first %ld bytes:\n", num_bytes);
        while (offset < num_bytes) {
            size_t read_size = fread(buffer, 1, BUFFER_SIZE, file);
            print_hex(buffer, read_size, offset);
            offset += read_size;
        }
    } else if (display_type == 2) {
        if (num_bytes > len) {
            fprintf(stderr, "Error: Display size exceeds file length.\n");
            exit(EXIT_FAILURE);
        }

        fseek(file, len - num_bytes, SEEK_SET);
        printf("File length: %ld\n", len);
        printf("Displaying last %ld bytes:\n", num_bytes);

        while (offset < num_bytes) {
            size_t read_size = fread(buffer, 1, BUFFER_SIZE, file);
            offset += read_size;
            if (offset > num_bytes) {
                // Move back to the correct position if we have read too much
                fseek(file, offset - num_bytes, SEEK_SET);
                offset = num_bytes;
            }
        }

        while (offset > 0) {
            size_t read_size = fread(buffer, 1, BUFFER_SIZE, file);
            print_hex(buffer, read_size, len - offset);
            offset -= read_size;
        }
    } else {
        fprintf(stderr, "Error: Invalid display type.\n");
        exit(EXIT_FAILURE);
    }

    fclose(file);
}

int main(int argc, char *argv[]) {
    if (argc != 4) {
        fprintf(stderr, "Usage: %s <filename> -h <n>|-t <n>\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    const char *filename = argv[1];
    const char *option = argv[2];
    size_t num_bytes = atoi(argv[3]);

    int display_type;

    if (strcmp(option, "-h") == 0) {
        display_type = 1;
    } else if (strcmp(option, "-t") == 0) {
        display_type = 2;
    } else {
        fprintf(stderr, "Error: Invalid option. Use -h or -t.\n");
        exit(EXIT_FAILURE);
    }

    display_file(filename, display_type, num_bytes);

    return 0;
}

myxxd 支持命令行传入参数-h n 显示前n个字节,-t n 显示最后面n 个字节,注意要先打印文件长度len, 保证n <= len



标签:string,read,hex,2024,课上,file,2023,data,size
From: https://www.cnblogs.com/shen-jianxiang/p/17863702.html

相关文章

  • 课上测试2 myxxd
    1.学***d的使用,提交至少3个应用截图2.xxd的主要功能是什么?需要使用什么系统调用来实现?写出你的推导过程,命令xxd的主要功能是以十六进制格式显示文件的内容,并提供将文件转换为十六进制格式或从十六进制格式转换回二进制格式的能力。它还可以用于编辑文件的十六进制内容。在推导......
  • 【专题】2023社群电商爆品营销白皮书报告PDF合集分享(附原数据表)
    原文链接:https://tecdat.cn/?p=34389原文出处:拓端数据部落公众号2023年是全球电商市场复苏的一年,也是充满机遇和激烈竞争的一年。对于出海电商品牌来说,在避免"内卷"的同时,寻找创新和可持续的经营策略和营销方法将变得至关重要。在新的出海环境下,由于其品效兼备的价值,"爆品"将展......
  • 20231128 - 重启Centos后无法远程连接,重启网络服务报错:Error:Failed to start LSB: Br
    1.https://blog.csdn.net/m0_74953387/article/details/1329143062.https://blog.csdn.net/weixin_45894220/article/details/130487066......
  • 2023年11月28日总结
    更好的观看总结听说明天就要有省选模拟了,哇酷哇酷。今天还是复习计算几何,还是很有难度的!今天做了一孔之见,最小圆覆盖,还有圆的面积并。复习了一下[[K-DTree]],看了一下书。收获很多,那个自适应辛普森很有趣呢。就这样吧!哈哈。细节没有什么好说的呢,感觉。一孔之见就那个每条边来......
  • 20231128
    请根据今日的课程内容,将口算题生成软件中的题目及习题保存到MySQL数据库中,并实现题目的保存和读取。提交实现效果截图及相关代码。packagetest;importjavax.swing.*;importjavax.swing.table.DefaultTableModel;importjava.awt.*;importjava.awt.event.ActionE......
  • CMO 2023 Day1T1
    \(n=2^{2024}\)时最优方案为\(2,2,\cdots,4\)此时\(\lambda_0=\frac{1}{1012}\)则\(\lambda_{\min}\geq\lambda_0\)。对于\(\lambda=\frac{1}{1012}\)构造,令\(n=\prodp_i\),\(p_i=n^{a_i}\)其中\(p\)均为素数。那么问题转化为:\(\suma_i=1\),其中\(a>......
  • 20231128 rock5b pwm驱动试玩
    根据https://doc.embedfire.com/linux/imx6/driver/zh/latest/linux_driver/pwm_sub_system.html使用rock5b开发板,radxa官方os,Macmini选择pin:现在pin5pwm14-M1 cd /usr/lib/linux-image-5.10.110-15-rockchip/rockchip/overlays 此文件夹里有rk3588......
  • 2023.11.28 随笔 了却君
    无聊。又来犯点无病呻吟之病。今日语文考时,绞尽脑汁,未背出下阙三、四段。特此默之,温习。《破阵子·为陈同甫赋壮词以寄之》辛弃疾醉里挑灯看剑,梦回吹角连营。八百里分麾下炙,五十弦翻塞外声。沙场秋点兵。马作的卢飞快,弓如霹雳弦惊。了却君王天下事,赢得生前身后名。可怜白发......
  • 复现LitCTF 2023的RE部分题
    [LitCTF2023]世界上最棒的程序员签到题pe查壳,无壳32位,拖入IDA中打开start函数[LitCTF2023]ez_XORpe查壳,无壳32位,拖入IDA中一道xor题,打开XOR函数编写脚本#include<stdio.h>#include<string.h>intmain(){ inti; constchar*s="E`}J]OrQF[V8zV:hzpV}fVF[t";......
  • 2023第十四届极客大挑战 — MISC WP
    Misc方向题解:来自本人cheekin请前往"三叶草小组Syclover"微信公众号输入flag获得flag 我的解答:关注公众号回复就可以得到一张图片,图片隐写zsteg即可。ez_smilemo游戏通关即可得到flag内容,需要自行添加`SYC{}`包含。例:flag内容为haha_haha则最终flag为SYC{haha_......