1
0

util.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #include "util.h"
  2. uint32_t ID3v2::Util::Int28To32(uint32_t val)
  3. {
  4. // TODO: big endian safe?
  5. uint32_t ret;
  6. ret = (val & 0x7FU);
  7. val >>= 1;
  8. ret |= (val & 0x3F80U);
  9. val >>= 1;
  10. ret |= (val & 0x1FC000U);
  11. val >>= 1;
  12. ret |= (val & 0xFE00000U);
  13. return ret;
  14. /*
  15. uint8_t *bytes = (uint8_t *)&ret;
  16. const uint8_t *value = (const uint8_t *)&val;
  17. ret = (value[0] << 21) + (value[1] << 14) + (value[2] << 7) + (value[3]);
  18. // for (size_t i=0;i<sizeof(uint32_t);i++ )
  19. // value[sizeof(uint32_t)-1-i]=(uint8_t)(val>>(i*8)) & 0xFF;
  20. return ret;
  21. */
  22. }
  23. uint32_t ID3v2::Util::Int32To28(uint32_t val)
  24. {
  25. // TODO: big endian safe?
  26. uint32_t ret;
  27. ret = (val & 0x7FU);
  28. ret |= (val & 0x3F80U) << 1;
  29. ret |= (val & 0x1FC000U) << 2;
  30. ret |= (val & 0xFE00000U) << 3;
  31. return ret;
  32. }
  33. size_t ID3v2::Util::UnsynchroniseTo(void *_output, const void *_input, size_t bytes)
  34. {
  35. uint8_t *output = (uint8_t *)_output;
  36. const uint8_t *input = (const uint8_t *)_input;
  37. size_t bytes_read = 0;
  38. while (bytes)
  39. {
  40. if (input[0] == 0xFF && input[1] == 0)
  41. {
  42. *output++ = 0xFF;
  43. input+=2;
  44. bytes_read+=2;
  45. bytes--;
  46. }
  47. else
  48. {
  49. *output++=*input++;
  50. bytes_read++;
  51. bytes--;
  52. }
  53. }
  54. return bytes_read;
  55. }
  56. size_t ID3v2::Util::UnsynchronisedInputSize(const void *data, size_t output_bytes)
  57. {
  58. const uint8_t *input = (const uint8_t *)data;
  59. size_t bytes_read = 0;
  60. while (output_bytes)
  61. {
  62. if (input[0] == 0xFF && input[1] == 0)
  63. {
  64. input+=2;
  65. bytes_read+=2;
  66. output_bytes--;
  67. }
  68. else
  69. {
  70. input++;
  71. bytes_read++;
  72. output_bytes--;
  73. }
  74. }
  75. return bytes_read;
  76. }
  77. size_t ID3v2::Util::UnsynchronisedOutputSize(const void *data, size_t input_bytes)
  78. {
  79. const uint8_t *input = (const uint8_t *)data;
  80. size_t bytes_written = 0;
  81. while (input_bytes)
  82. {
  83. if (input[0] == 0xFF && input_bytes > 1 && input[1] == 0)
  84. {
  85. input+=2;
  86. bytes_written++;
  87. input_bytes-=2;
  88. }
  89. else
  90. {
  91. input++;
  92. bytes_written++;
  93. input_bytes--;
  94. }
  95. }
  96. return bytes_written;
  97. }
  98. // returns output bytes used
  99. size_t ID3v2::Util::SynchroniseTo(void *_output, const void *data, size_t bytes)
  100. {
  101. uint8_t *output = (uint8_t *)_output;
  102. const uint8_t *input = (const uint8_t *)data;
  103. size_t bytes_needed = 0;
  104. while (bytes)
  105. {
  106. *output++=*input;
  107. bytes_needed++;
  108. if (*input++ == 0xFF)
  109. {
  110. if (bytes == 1)
  111. {
  112. // if this is the last byte, we need to make room for an extra 0
  113. *output = 0;
  114. return bytes_needed + 1;
  115. }
  116. else if ((*input & 0xE0) == 0xE0 || *input == 0)
  117. {
  118. *output++ = 0;
  119. bytes_needed++;
  120. }
  121. }
  122. bytes--;
  123. }
  124. return bytes_needed;
  125. }
  126. size_t ID3v2::Util::SynchronisedSize(const void *data, size_t bytes)
  127. {
  128. const uint8_t *input = (const uint8_t *)data;
  129. size_t bytes_needed = 0;
  130. while (bytes)
  131. {
  132. bytes_needed++;
  133. if (*input++ == 0xFF)
  134. {
  135. if (bytes == 1)
  136. {
  137. // if this is the last byte, we need to make room for an extra 0
  138. return bytes_needed + 1;
  139. }
  140. else if ((*input & 0xE0) == 0xE0 || *input == 0)
  141. {
  142. bytes_needed++;
  143. }
  144. }
  145. bytes--;
  146. }
  147. return bytes_needed;
  148. }