libopenmpt_stream_callbacks_buffer.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * libopenmpt_stream_callbacks_buffer.h
  3. * ------------------------------------
  4. * Purpose: libopenmpt public c interface
  5. * Notes : (currently none)
  6. * Authors: OpenMPT Devs
  7. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  8. */
  9. #ifndef LIBOPENMPT_STREAM_CALLBACKS_BUFFER_H
  10. #define LIBOPENMPT_STREAM_CALLBACKS_BUFFER_H
  11. #include "libopenmpt.h"
  12. /* The use of this header requires:
  13. #include <libopenmpt/libopenmpt.h>
  14. #if defined( LIBOPENMPT_STREAM_CALLBACKS_BUFFER )
  15. #include <libopenmpt/libopenmpt_stream_callbacks_buffer.h>
  16. #else
  17. #error "libopenmpt too old."
  18. #endif
  19. */
  20. #include <stdint.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. /*! \addtogroup libopenmpt_c
  24. * @{
  25. */
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. typedef struct openmpt_stream_buffer {
  30. const void * file_data; /* or prefix data IFF prefix_size < file_size */
  31. int64_t file_size;
  32. int64_t file_pos;
  33. int64_t prefix_size;
  34. int overflow;
  35. } openmpt_stream_buffer;
  36. static size_t openmpt_stream_buffer_read_func( void * stream, void * dst, size_t bytes ) {
  37. openmpt_stream_buffer * s = (openmpt_stream_buffer*)stream;
  38. int64_t offset = 0;
  39. int64_t begpos = 0;
  40. int64_t endpos = 0;
  41. size_t valid_bytes = 0;
  42. if ( !s ) {
  43. return 0;
  44. }
  45. offset = bytes;
  46. begpos = s->file_pos;
  47. endpos = s->file_pos;
  48. valid_bytes = 0;
  49. endpos = (uint64_t)endpos + (uint64_t)offset;
  50. if ( ( offset > 0 ) && !( (uint64_t)endpos > (uint64_t)begpos ) ) {
  51. /* integer wrapped */
  52. return 0;
  53. }
  54. if ( bytes == 0 ) {
  55. return 0;
  56. }
  57. if ( begpos >= s->file_size ) {
  58. return 0;
  59. }
  60. if ( endpos > s->file_size ) {
  61. /* clip to eof */
  62. bytes = bytes - (size_t)( endpos - s->file_size );
  63. endpos = endpos - ( endpos - s->file_size );
  64. }
  65. memset( dst, 0, bytes );
  66. if ( begpos >= s->prefix_size ) {
  67. s->overflow = 1;
  68. valid_bytes = 0;
  69. } else if ( endpos > s->prefix_size ) {
  70. s->overflow = 1;
  71. valid_bytes = bytes - (size_t)( endpos - s->prefix_size );
  72. } else {
  73. valid_bytes = bytes;
  74. }
  75. memcpy( dst, (const char*)s->file_data + s->file_pos, valid_bytes );
  76. s->file_pos = s->file_pos + bytes;
  77. return bytes;
  78. }
  79. static int openmpt_stream_buffer_seek_func( void * stream, int64_t offset, int whence ) {
  80. openmpt_stream_buffer * s = (openmpt_stream_buffer*)stream;
  81. int result = -1;
  82. if ( !s ) {
  83. return -1;
  84. }
  85. switch ( whence ) {
  86. case OPENMPT_STREAM_SEEK_SET:
  87. if ( offset < 0 ) {
  88. return -1;
  89. }
  90. if ( offset > s->file_size ) {
  91. return -1;
  92. }
  93. s->file_pos = offset;
  94. result = 0;
  95. break;
  96. case OPENMPT_STREAM_SEEK_CUR:
  97. do {
  98. int64_t oldpos = s->file_pos;
  99. int64_t pos = s->file_pos;
  100. pos = (uint64_t)pos + (uint64_t)offset;
  101. if ( ( offset > 0 ) && !( (uint64_t)pos > (uint64_t)oldpos ) ) {
  102. /* integer wrapped */
  103. return -1;
  104. }
  105. if ( ( offset < 0 ) && !( (uint64_t)pos < (uint64_t)oldpos ) ) {
  106. /* integer wrapped */
  107. return -1;
  108. }
  109. s->file_pos = pos;
  110. } while(0);
  111. result = 0;
  112. break;
  113. case OPENMPT_STREAM_SEEK_END:
  114. if ( offset > 0 ) {
  115. return -1;
  116. }
  117. do {
  118. int64_t oldpos = s->file_pos;
  119. int64_t pos = s->file_pos;
  120. pos = s->file_size;
  121. pos = (uint64_t)pos + (uint64_t)offset;
  122. if ( ( offset < 0 ) && !( (uint64_t)pos < (uint64_t)oldpos ) ) {
  123. /* integer wrapped */
  124. return -1;
  125. }
  126. s->file_pos = pos;
  127. } while(0);
  128. result = 0;
  129. break;
  130. }
  131. return result;
  132. }
  133. static int64_t openmpt_stream_buffer_tell_func( void * stream ) {
  134. openmpt_stream_buffer * s = (openmpt_stream_buffer*)stream;
  135. if ( !s ) {
  136. return -1;
  137. }
  138. return s->file_pos;
  139. }
  140. static void openmpt_stream_buffer_init( openmpt_stream_buffer * buffer, const void * file_data, int64_t file_size ) {
  141. memset( buffer, 0, sizeof( openmpt_stream_buffer ) );
  142. buffer->file_data = file_data;
  143. buffer->file_size = file_size;
  144. buffer->file_pos = 0;
  145. buffer->prefix_size = file_size;
  146. buffer->overflow = 0;
  147. }
  148. #define openmpt_stream_buffer_init_prefix_only( buffer_, prefix_data_, prefix_size_, file_size_ ) do { \
  149. openmpt_stream_buffer_init( (buffer_), (prefix_data_), (file_size_) ); \
  150. (buffer_)->prefix_size = (prefix_size_); \
  151. } while(0)
  152. #define openmpt_stream_buffer_overflowed( buffer_ ) ( (buffer_)->overflow )
  153. /*! \brief Provide openmpt_stream_callbacks for in-memoy buffers
  154. *
  155. * Fills openmpt_stream_callbacks suitable for passing an in-memory buffer as a stream parameter to functions doing file input/output.
  156. *
  157. * \remarks The stream argument must be passed as `(void*)(openmpt_stream_buffer*)stream_buffer`.
  158. * \sa \ref libopenmpt_c_fileio
  159. * \sa openmpt_stream_callbacks
  160. * \sa openmpt_could_open_probability2
  161. * \sa openmpt_probe_file_header_from_stream
  162. * \sa openmpt_module_create2
  163. */
  164. static openmpt_stream_callbacks openmpt_stream_get_buffer_callbacks(void) {
  165. openmpt_stream_callbacks retval;
  166. memset( &retval, 0, sizeof( openmpt_stream_callbacks ) );
  167. retval.read = openmpt_stream_buffer_read_func;
  168. retval.seek = openmpt_stream_buffer_seek_func;
  169. retval.tell = openmpt_stream_buffer_tell_func;
  170. return retval;
  171. }
  172. #ifdef __cplusplus
  173. }
  174. #endif
  175. /*!
  176. * @}
  177. */
  178. #endif /* LIBOPENMPT_STREAM_CALLBACKS_BUFFER_H */