首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用malloc的Recover.c

使用malloc的Recover.c
EN

Stack Overflow用户
提问于 2016-03-15 03:02:48
回答 1查看 131关注 0票数 0

使用clang我得到两个警告:

代码语言:javascript
复制
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)

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

代码语言:javascript
复制
#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);
}
EN

回答 1

Stack Overflow用户

发布于 2016-03-15 03:13:55

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

这意味着与使用strcpy相比,在数组之间memcpy数据(因为它们在类型上都匹配)可能会做得更好。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35995906

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档