gpt4 book ai didi

c - 使用 malloc 恢复.c

转载 作者:行者123 更新时间:2023-11-30 15:10:47 24 4
gpt4 key购买 nike

使用 clang 我收到 2 个警告:

warning: passing 'unsigned char *' to parameter of type 'char *' converts between pointers to
integer types with different sign [-Wpointer-sign]
strncpy(contents, &data[starting_byte], size);
^~~~~~~~
passing argument to parameter '__dest' here
extern char *strncpy (char *__restrict __dest,
^
warning: passing 'unsigned char *' to parameter of type 'const char *' converts between
pointers to integer types with different sign [-Wpointer-sign]
strncpy(contents, &data[starting_byte], size);
^~~~~~~~~~~~~~~~~~~~
passing argument to parameter '__src' here
const char *__restrict __src, size_t __n)

当我运行它时,没有恢复任何图像。我真的很感激任何有关代码的帮助。泰!

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdint.h>
#include <stdbool.h>


// Function prototypes. Don't change these.
unsigned char * read_card(char fname[], int *size);
void save_jpeg(unsigned char data[], int starting_byte, int size, char fname[]);
void recover(unsigned char data[], int size);

int main()
{
// Read the card.raw file into an array of bytes (unsigned char)
int card_length;
unsigned char *card = read_card("card.raw", &card_length);

// Recover the images
recover(card, card_length);
}
//read the card.raw file, returns array of unsigned char(bytes)
unsigned char * read_card(char fname[], int *size)
{

struct stat st;
if (stat(fname, &st) == -1)
{
fprintf(stderr, "Can't get info about %s\n", fname);
exit(1);
}
int len = st.st_size;
unsigned char *raw = (unsigned char *)malloc(len * sizeof(unsigned char));

FILE *fp = fopen(fname, "rb");
if (!fp)
{
fprintf(stderr, "Can't open %s for reading\n", fname);
exit(1);
}

char buf[512];
int r = 0;
int i = 0;
while (fread(buf, 1, 512, fp))
{
for (i = 0; i < 512; i++)
{
raw[r] = buf[i];
r++;
}
}
fclose(fp);

*size = len;
return raw;
}
//parameters: array of unsigned char(bytes),has data for a single pic
//length of the data and name of the file to save to
void save_jpeg(unsigned char data[], int starting_byte, int size, char fname[])
{
unsigned char *contents = (unsigned char *)malloc((size + 1) * sizeof(unsigned char));

strncpy(contents, &data[starting_byte], size);
contents[size + 1] = '\0';

FILE *fp = fopen(fname, "wb");
if (!fp)
{
fprintf(stderr, "Can't write to %s\n", fname);
exit(1);
}

fwrite(contents, 1, size, fp);
fclose(fp);
free(contents);
}

void recover(unsigned char data[], int size)
{
int count = 0;
int block = 512;
int byte_offset = 0;
int starting_byte = 0;
bool image_wraps = false;

for (int leading_byte = 0; leading_byte < size; leading_byte+=block)
{
if (image_wraps == false) {
//check for beginning signatures
if (data[leading_byte] == 0xff && data[leading_byte+1] == 0xd8 && data[leading_byte+2] == 0xff &&
(data[leading_byte+3] == 0xe0 || data[leading_byte+3] == 0xe1))
{
//store start
starting_byte = leading_byte;
for (int current_byte = leading_byte + 4; current_byte < leading_byte + block - 1; ++current_byte)
{
if (data[current_byte] == 0xff && data[current_byte+1] == 0xd9)
{
char filename[30];
snprintf(filename, sizeof(filename), "%03d.jpg", ++count);
save_jpeg(data, starting_byte, current_byte - leading_byte, filename);
}
}
image_wraps = true;
byte_offset = block;
}
}
// image wraps to multiple blocks
else {
for (int current_byte = leading_byte; current_byte < leading_byte + block - 1; ++current_byte)
{
if (data[current_byte] == 0xff && data[current_byte+1] == 0xd9)
{
char filename[30];
snprintf(filename, sizeof(filename), "%03d.jpg", ++count);
printf("leading %i, current %i, c-l+o %i\n", leading_byte, current_byte, byte_offset);
save_jpeg(data, starting_byte, current_byte - leading_byte + byte_offset, filename);
image_wraps = false;
starting_byte = 0;
byte_offset = 0;
}
}
if (byte_offset > 0) {
byte_offset += block;
}
}
}

printf("How many images?: %d\n", count);
}

最佳答案

strcpy 对 C 样式字符串进行操作。无符号字符数组不是 C 风格的字符串,以 null 结尾的字符数组才是。

这意味着您可能会更好地memcpy数组之间的数据(因为它们在类型上都匹配),而不是使用strcpy

关于c - 使用 malloc 恢复.c,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35995906/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com