在freebsd 7上快速修复32位(2GB限制)fseek / ftell

前端之家收集整理的这篇文章主要介绍了在freebsd 7上快速修复32位(2GB限制)fseek / ftell前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在FreeBSD上有旧的32位C/C++程序,它被数百名用户远程使用,其作者无法修复它.它是以不安全的方式编写的,所有文件偏移都在内部存储为无符号32位偏移量,并且ftell / fseek函数在使用时.在FreeBSD 7(软件的主机平台)中,它是 means that ftell and fseek uses 32-bit signed long
  1. int fseek(FILE *stream,long offset,int whence);
  2.  
  3. long ftell(FILE *stream);

我需要快速修复程序,因为一些内部数据文件在收集数据13年后突然达到2 ^ 31文件大小(2 147 483 7yy字节),并且内部fseek / ftell断言现在对任何请求都失败了.

在FreeBSD7世界中,有2GB文件的fseeko / ftello hack.

  1. int
  2. fseeko(FILE *stream,off_t offset,int whence);
  3.  
  4. off_t
  5. ftello(FILE *stream);

这里的off_t类型没有明确定义;我现在所知道的,它有8字节的大小,看起来像长long或无符号long long(我不知道哪一个).

是否足够(可以处理多达4 GB的文件)并且可以安全地搜索和替换所有ftell到ftello,并且所有fseek到fseeko(sed -i’s / ftell / ftello’,如果可能的话)它们的用法是:

  1. unsigned long offset1,offset2; //32bit
  2. offset1 = (compute + it) * in + some - arithmetic;
  3. fseek(file,SEEK_END);
  4. fseek(file,4,SEEK_END); // or other small int constant
  5.  
  6. offset2 = ftell(file);
  7. fseek(file,offset1,SEEK_SET); // No usage of SEEK_CUR

和这些电话的组合.

off_t的签名是什么?
将64位off_t分配到无符号32位偏移量是安全的吗?它适用于2 GB到4 GB范围内的字节吗?

除了ftell / fseek之外,哪些函数可以用于偏移?

解决方法

FreeBSD fseeko() and ftello()记录为POSIX.1-2001兼容,即 off_t is a signed integer type.

在FreeBSD 7上,你可以放心地做到:

  1. off_t actual_offset;
  2. unsigned long stored_offset;
  3.  
  4. if (actual_offset >= (off_t)0 && actual_offset < (off_t)4294967296.0)
  5. stored_offset = (unsigned long)actual_offset;
  6. else
  7. some_fatal_error("Unsupportable file offset!");

(在LP64体系结构上,上面的内容很愚蠢,因为off_t和long都是64位有符号整数.即使这样也会安全;只是愚蠢,因为可以支持所有可能的文件偏移.)

人们经常被这种情况所困扰的是,必须使用off_t完成偏移计算.也就是说,将结果转换为off_t是不够的,必须将算术中使用的值转换为off_t. (从技术上讲,你只需要确保每个算术运算都是以off_t精度完成的,但是我发现如果我只是试图并转换所有的操作数就更容易记住规则.)例如:

  1. off_t offset;
  2. unsigned long some,values,used;
  3.  
  4. offset = (off_t)some * (off_t)value + (off_t)used;
  5. fseeko(file,offset,SEEK_SET);

通常,偏移计算用于查找特定记录中的字段;算术趋于保持不变.我真的建议你将搜索操作移动到辅助函数,如果可能的话:

  1. int fseek_to(FILE *const file,const unsigned long some,const unsigned long values,const unsigned long used)
  2. {
  3. const off_t offset = (off_t)some * (off_t)value + (off_t)used;
  4. if (offset < (off_t)0 || offset >= (off_t)4294967296.0)
  5. fatal_error("Offset exceeds 4GB; I must abort!");
  6. return fseeko(file,SEEK_SET);
  7. }

现在,如果你碰巧处于一个幸运的位置,你知道所有的偏移都是对齐的(对于某个整数,比如4),你可以给自己几年的时间来重写应用程序,通过使用以上:

  1. #define BIG_N 4
  2.  
  3. int fseek_to(FILE *const file,const unsigned long used)
  4. {
  5. const off_t offset = (off_t)some * (off_t)value + (off_t)used;
  6. if (offset < (off_t)0)
  7. fatal_error("Offset is negative; I must abort!");
  8. if (offset >= (off_t)(BIG_N * 2147483648.0))
  9. fatal_error("Offset is too large; I must abort!");
  10. if ((offset % BIG_N) && (offset >= (off_t)2147483648.0))
  11. fatal_error("Offset is not a multiple of BIG_N; I must abort!");
  12. return fseeko(file,SEEK_SET);
  13. }
  14.  
  15. int fseek_big(FILE *const file,const unsigned long position)
  16. {
  17. off_t offset;
  18. if (position >= 2147483648UL)
  19. offset = (off_t)2147483648UL
  20. + (off_t)BIG_N * (off_t)(position - 2147483648UL);
  21. else
  22. offset = (off_t)position;
  23. return fseeko(file,SEEK_SET);
  24. }
  25.  
  26. unsigned long ftell_big(FILE *const file)
  27. {
  28. off_t offset;
  29. offset = ftello(file);
  30. if (offset < (off_t)0)
  31. fatal_error("Offset is negative; I must abort!");
  32. if (offset < (off_t)2147483648UL)
  33. return (unsigned long)offset;
  34. if (offset % BIG_N)
  35. fatal_error("Offset is not a multiple of BIG_N; I must abort!");
  36. if (offset >= (off_t)(BIG_N * 2147483648.0))
  37. fatal_error("Offset is too large; I must abort!");
  38. return (unsigned long)2147483648UL
  39. + (unsigned long)((offset - (off_t)2147483648UL) / (off_t)BIG_N);
  40. }

逻辑很简单:如果offset小于231,则按原样使用.否则,它由值231 BIG_N×(偏移-231)表示.唯一的要求是偏移231及以上始终是BIG_N的倍数.

显然,你必须只使用上面三个函数加上你需要的fseek_to()变体,只要它们做同样的检查,只需使用不同的参数和公式进行偏移计算 – 你可以支持文件大小高达2147483648 BIG_N×2147483647.对于BIG_N == 4,即10 GiB(少于4个字节;准确地说是10,737,418,236个字节).

有问题吗?

编辑澄清:

首先用调用fseek_pos(文件,位置)替换你的fseek(文件,位置,SEEK_SET),

  1. static inline void fseek_pos(FILE *const file,const unsigned long position)
  2. {
  3. if (fseeko(file,(off_t)position,SEEK_SET))
  4. fatal_error("Cannot set file position!");
  5. }

和fseek(文件,SEEK_END)调用fseek_end(文件,位置)(对称性 – 我假设这个位置通常是一个文字整数常量),

  1. static inline void fseek_end(FILE *const file,const off_t relative)
  2. {
  3. if (fseeko(file,relative,SEEK_END))
  4. fatal_error("Cannot set file position!");
  5. }

最后,调用ftell_pos(文件)的ftell(文件):

  1. static inline unsigned long ftell_pos(FILE *const file)
  2. {
  3. off_t position;
  4. position = ftello(file);
  5. if (position == (off_t)-1)
  6. fatal_error("Lost file position!");
  7. if (position < (off_t)0 || position >= (off_t)4294967296.0)
  8. fatal_error("File position outside the 4GB range!");
  9. return (unsigned long)position;
  10. }

由于在您的体系结构和OS上,unsigned long是32位无符号整数类型而off_t是64位有符号整数类型,因此这为您提供了完整的4GB范围.

对于偏移计算,定义一个或多个类似的函数

  1. static inline void fseek_to(FILE *const file,const off_t term1,const off_t term2,const off_t term3)
  2. {
  3. const off_t position = term1 * term2 + term3;
  4.  
  5. if (position < (off_t)0 || position >= (off_t)4294967296.0)
  6. fatal_error("File position outside the 4GB range!");
  7. if (fseeko(file,position,SEEK_SET))
  8. fatal_error("Cannot set file position!");
  9. }

对于每个偏移计算算法,定义一个fseek_to变量.命名参数以使算法有意义.如上所述,使参数const off_t,因此算术中不需要额外的强制转换.只有参数和const off_t position =定义计算算法的行在变量函数之间变化.

有问题吗?

猜你在找的C&C++相关文章