home *** CD-ROM | disk | FTP | other *** search
/ Chip 2005 June / ccd0605.iso / LINUX / gopchop-1.1.7.tar.tar / gopchop-1.1.7.tar / gopchop-1.1.7 / src / file_buffer.h < prev    next >
C/C++ Source or Header  |  2005-04-30  |  3KB  |  117 lines

  1. /*
  2. #
  3. # Header for Utilities for seeking around in a file stream
  4. #
  5. # $Id: file_buffer.h,v 1.5 2005/04/30 20:22:37 keescook Exp $
  6. #
  7. # Copyright (C) 2001-2003 Kees Cook
  8. # kees@outflux.net, http://outflux.net/
  9. # This program is free software; you can redistribute it and/or
  10. # modify it under the terms of the GNU General Public License
  11. # as published by the Free Software Foundation; either version 2
  12. # of the License, or (at your option) any later version.
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. # GNU General Public License for more details.
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program; if not, write to the Free Software
  19. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  20. # http://www.gnu.org/copyleft/gpl.html
  21. #
  22. */
  23.  
  24. /*
  25.  * buffer management inspired by mplayer
  26.  */
  27. #ifndef _FILE_BUFFER_H_
  28. #define _FILE_BUFFER_H_
  29.  
  30.  
  31. #ifdef HAVE_CONFIG_H
  32. # include "config.h"
  33. # ifdef HAVE_STDINT_H
  34. #  include <stdint.h>
  35. # endif
  36. # ifdef HAVE_INTTYPES_H
  37. #  include <inttypes.h>
  38. # endif
  39. # ifdef HAVE_STRING_H
  40. #  include <string.h> /* memmove, memcpy */
  41. # endif
  42. # ifdef HAVE_STDLIB_H
  43. #  include <stdlib.h> /* off_t */
  44. # endif
  45. #else
  46. # include <stdint.h>
  47. # include <string.h>
  48. # include <stdlib.h> /* off_t */
  49. #endif
  50.  
  51. // This seems to be needed to catch the largefile functions in stdio.h?
  52. #define __USE_LARGEFILE
  53.  
  54. #include <stdio.h> /* fopen, fread, fseek*, fclose */
  55.  
  56. /* figure out which fseek/ftell we need */
  57. #undef FSEEK
  58. #undef FTELL
  59. #ifdef HAVE_FSEEKO
  60. # define FSEEK      fseeko
  61. # define FSEEK_NAME "fseeko"
  62. # define FTELL      ftello
  63. # define FTELL_NAME "ftello"
  64. #else
  65. # define FSEEK      fseek
  66. # define FSEEK_NAME "fseek"
  67. # define FTELL      ftell
  68. # define FTELL_NAME "ftell"
  69. #endif
  70.  
  71. /* figure out off_t formatting */
  72. #undef OFF_T_FORMAT
  73. #undef ATOL
  74. #if _FILE_OFFSET_BITS==64 || defined(__NetBSD__)
  75. # define OFF_T_FORMAT  "llu"
  76. # define ATOL(arg)     atoll(arg)
  77. #else
  78. # warning "Not compiling for large file (>2G) support!"
  79. # define OFF_T_FORMAT  "lu"
  80. # define ATOL(arg)     atol(arg)
  81. #endif
  82.  
  83. #define DEFAULT_FILE_BUFFER_SIZE        (1024*512)
  84.  
  85. #define FILE_BUF_OKAY                            0
  86. #define FILE_BUF_ERR_NULL_STRUCT                -1
  87. #define FILE_BUF_ERR_NULL_ARG                   -2
  88. #define FILE_BUF_ERR_READ                       -3
  89. #define FILE_BUF_ERR_SEEK                       -4
  90. #define FILE_BUF_ERR_OPEN                       -5
  91. #define FILE_BUF_ERR_PAST_END                   -6
  92. #define FILE_BUF_ERR_NEED_POSITIVE_COUNT        -7
  93. #define FILE_BUF_ERR_COUNT_EXCEEDS_BUFFER_SIZE  -8
  94.  
  95. typedef struct file_buf_t file_buf;
  96.  
  97. // A NULL return from "buffer_start" indicates that errno has been set,
  98. // and calls to "perror" and the like are valid.
  99. // Passing a previously-return file_buf into buffer_start will re-allocate it
  100. file_buf * buffer_start(file_buf *fb, char *filename, size_t size);
  101. int buffer_look_ahead(file_buf *fb, uint8_t * bytes, size_t count);
  102. int buffer_get_byte(file_buf *fb, uint8_t * byte);
  103. off_t buffer_tell(file_buf *fb);
  104. off_t buffer_file_size(file_buf *fb);
  105. int buffer_seek(file_buf *fb, off_t location);
  106. int buffer_end(file_buf *fb);
  107.  
  108. // can point to a file offset to refill from
  109. int buffer_refill(file_buf *fb);
  110.  
  111. #endif /* _FILE_BUFFER_H_ */
  112.  
  113. /* vi:set ai ts=4 sw=4 expandtab: */
  114.