Linux C语言实现把微信Image的dat文件转成png图片

cpp 复制代码
#include <stdio.h>
#include <dirent.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>

#define BUFFER_SIZE 1024

void open_and_display_file(const char *filepath, const char *dir)
{
    unsigned char Decode = 0;
    static int image_id = 0;
    unsigned char imagePath[PATH_MAX] = {0};
    unsigned char buffer[BUFFER_SIZE] = {0};
    FILE *imageFp = NULL;
    FILE *file = NULL;
    int read_len = 0;
    int write_len = 0;
    sprintf(imagePath, "%s/%d.png", dir, image_id);
    image_id++;
    file = fopen(filepath, "r");
    if (file == NULL)
    {
        perror("无法打开文件:");
        return;
    }
    imageFp = fopen(imagePath, "wb+");
    if (imageFp == NULL)
    {
        perror("无法打开文件:");
        return;
    }
    ftruncate(fileno(imageFp), 0);
    fread(buffer, 1, 2, file);
    int mData1 = buffer[0] ^ 0xFF;
    int mData2 = buffer[1] ^ 0xD8;
    printf("mData1 %X  mData2  %x\n", mData1, mData2);
    if (mData1 == mData2)
    {
        Decode = mData1;
        printf("Decode JPEG found %X %x %x\n", buffer[0], buffer[1], Decode);
    }
    else
    {
        printf("Decode JPEG not found %X %x %x\n", buffer[0], buffer[1], Decode);
        mData1 = buffer[0] ^ 0x89;
        mData2 = buffer[1] ^ 0x50;
        if (mData1 == mData2)
        {
            Decode = mData1;
            printf("Decode PNG found %X %x %x\n", buffer[0], buffer[1], Decode);
        }else{
            printf("Decode PNG not found %X %x %x\n", buffer[0], buffer[1], Decode);
        }
    }

    fseek(file, 0, SEEK_SET);
    while (!feof(file))
    {
        read_len = fread(buffer, 1, BUFFER_SIZE, file);
        for (int i = 0; i < read_len; i++)
        {
            buffer[i] ^= Decode;
        }
        write_len = fwrite(buffer, 1, read_len, imageFp);
    }
    fclose(file);
    fclose(imageFp);
}

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        fprintf(stderr, "用法: %s 目录路径\n", argv[0]);
        return 1;
    }

    const char *directory_path = argv[1];
    DIR *dir;
    struct dirent *entry;

    if ((dir = opendir(directory_path)) == NULL)
    {
        perror("无法打开目录:");
        return 1;
    }

    while ((entry = readdir(dir)) != NULL)
    {
        if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0)
        {
            char filepath[PATH_MAX];
            snprintf(filepath, PATH_MAX, "%s/%s", directory_path, entry->d_name);
            open_and_display_file(filepath, directory_path);
        }
    }

    closedir(dir);
    return 0;
}

相关的桌面程序在我的资源列表里面可以下载

相关推荐
小毛驴85031 分钟前
Linux 后台启动java jar 程序 nohup java -jar
java·linux·jar
好好学习啊天天向上2 小时前
世上最全:ubuntu 上及天河超算上源码编译llvm遇到的坑,cmake,ninja完整过程
linux·运维·ubuntu·自动性能优化
tan180°3 小时前
MySQL表的操作(3)
linux·数据库·c++·vscode·后端·mysql
学不动CV了3 小时前
ARM单片机启动流程(二)(详细解析)
c语言·arm开发·stm32·单片机·51单片机
典学长编程3 小时前
Linux操作系统从入门到精通!第二天(命令行)
linux·运维·chrome
wuk9983 小时前
基于MATLAB编制的锂离子电池伪二维模型
linux·windows·github
猫猫的小茶馆5 小时前
【STM32】通用定时器基本原理
c语言·stm32·单片机·嵌入式硬件·mcu·51单片机
独行soc6 小时前
#渗透测试#批量漏洞挖掘#HSC Mailinspector 任意文件读取漏洞(CVE-2024-34470)
linux·科技·安全·网络安全·面试·渗透测试
BD_Marathon6 小时前
Ubuntu下Tomcat的配置
linux·ubuntu·tomcat
pumpkin845146 小时前
Rust 调用 C 函数的 FFI
c语言·算法·rust