1
0

StreamFileWin32.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. ** Copyright (C) 2007-2012 Nullsoft, Inc.
  3. **
  4. ** This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held
  5. ** liable for any damages arising from the use of this software.
  6. **
  7. ** Permission is granted to anyone to use this software for any purpose, including commercial applications, and to
  8. ** alter it and redistribute it freely, subject to the following restrictions:
  9. **
  10. ** 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software.
  11. ** If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  12. **
  13. ** 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  14. **
  15. ** 3. This notice may not be removed or altered from any source distribution.
  16. **
  17. ** Author: Ben Allison [email protected]
  18. ** Created: March 1, 2007
  19. **
  20. */
  21. #include <windows.h>
  22. #include <FLAC/all.h>
  23. #include <assert.h>
  24. #include "StreamFileWin32.h"
  25. FLAC__StreamDecoderReadStatus Win32_Read(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
  26. {
  27. assert(*bytes <= 4294967295U);
  28. HANDLE file = ((Win32_State *)client_data)->handle;
  29. if(*bytes > 0)
  30. {
  31. assert(sizeof(FLAC__byte) == 1);
  32. DWORD bytesRead=0, bytesToRead=*bytes;
  33. BOOL result = ReadFile(file, buffer, bytesToRead, &bytesRead, NULL);
  34. *bytes = bytesRead;
  35. if (!result)
  36. return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
  37. else if(bytesRead == 0)
  38. {
  39. ((Win32_State *)client_data)->endOfFile = true;
  40. return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
  41. }
  42. else
  43. return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
  44. }
  45. else
  46. return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
  47. }
  48. __int64 Seek64(HANDLE hf, __int64 distance, DWORD MoveMethod)
  49. {
  50. LARGE_INTEGER li;
  51. li.QuadPart = distance;
  52. li.LowPart = SetFilePointer (hf, li.LowPart, &li.HighPart, MoveMethod);
  53. if (li.LowPart == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR)
  54. {
  55. li.QuadPart = -1;
  56. }
  57. return li.QuadPart;
  58. }
  59. FLAC__StreamDecoderSeekStatus Win32_Seek(const FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data)
  60. {
  61. HANDLE file = ((Win32_State *)client_data)->handle;
  62. __int64 result = Seek64(file, absolute_byte_offset, FILE_BEGIN);
  63. if (result == INVALID_SET_FILE_POINTER)
  64. return FLAC__STREAM_DECODER_SEEK_STATUS_ERROR;
  65. else
  66. {
  67. ((Win32_State *)client_data)->endOfFile = false;
  68. return FLAC__STREAM_DECODER_SEEK_STATUS_OK;
  69. }
  70. }
  71. FLAC__StreamDecoderTellStatus Win32_Tell(const FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
  72. {
  73. HANDLE file = ((Win32_State *)client_data)->handle;
  74. __int64 position = Seek64(file, 0, FILE_CURRENT);
  75. if (position == INVALID_SET_FILE_POINTER)
  76. return FLAC__STREAM_DECODER_TELL_STATUS_ERROR;
  77. else
  78. {
  79. *absolute_byte_offset=position;
  80. return FLAC__STREAM_DECODER_TELL_STATUS_OK;
  81. }
  82. }
  83. __int64 FileSize64(HANDLE file)
  84. {
  85. LARGE_INTEGER position;
  86. position.QuadPart=0;
  87. position.LowPart = GetFileSize(file, (LPDWORD)&position.HighPart);
  88. if (position.LowPart == INVALID_FILE_SIZE && GetLastError() != NO_ERROR)
  89. return INVALID_FILE_SIZE;
  90. else
  91. return position.QuadPart;
  92. }
  93. FLAC__StreamDecoderLengthStatus Win32_Length(const FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data)
  94. {
  95. HANDLE file = ((Win32_State *)client_data)->handle;
  96. LARGE_INTEGER position;
  97. position.QuadPart=0;
  98. position.LowPart = GetFileSize(file, (LPDWORD)&position.HighPart);
  99. if (position.LowPart == INVALID_FILE_SIZE && GetLastError() != NO_ERROR)
  100. return FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR;
  101. else
  102. {
  103. *stream_length = position.QuadPart;
  104. return FLAC__STREAM_DECODER_LENGTH_STATUS_OK;
  105. }
  106. }
  107. FLAC__bool Win32_EOF(const FLAC__StreamDecoder *decoder, void *client_data)
  108. {
  109. return ((Win32_State *)client_data)->endOfFile;
  110. }