1
0

read.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #include "read.h"
  2. #include "vint.h"
  3. #include "ebml_float.h"
  4. #include "ebml_unsigned.h"
  5. #include "ebml_signed.h"
  6. #include <limits.h>
  7. #ifdef WA_VALIDATE
  8. extern uint64_t max_id_length;
  9. extern uint64_t max_size_length;
  10. #endif
  11. // returns bytes read. 0 means EOF
  12. uint64_t read_vint(nsmkv::MKVReader *reader, uint64_t *val)
  13. {
  14. uint8_t data[9] = {0};
  15. size_t bytes_read = 0;
  16. reader->Read(data, 1, &bytes_read);
  17. if (bytes_read != 1)
  18. return 0;
  19. uint8_t length = vint_get_number_bytes(data[0]);
  20. reader->Read(data+1, length, &bytes_read);
  21. if (bytes_read != length)
  22. return 0;
  23. *val = vint_read_ptr(data);
  24. return bytes_read+1;
  25. }
  26. // returns bytes read. 0 means EOF
  27. uint64_t read_ebml_node(nsmkv::MKVReader *reader, ebml_node *node)
  28. {
  29. uint64_t bytes_read = read_vint(reader, &node->id);
  30. if (!bytes_read)
  31. return 0;
  32. bytes_read += read_vint(reader, &node->size);
  33. return bytes_read;
  34. }
  35. uint64_t read_utf8(nsmkv::MKVReader *reader, uint64_t size, char **utf8)
  36. {
  37. if (utf8)
  38. {
  39. if (size == SIZE_MAX) // prevent integer overflow
  40. return 0;
  41. char *&val = *utf8;
  42. val = (char *)calloc((size_t)size + 1, sizeof(char));
  43. if (val)
  44. {
  45. val[size]=0;
  46. size_t bytes_read;
  47. reader->Read(val, (size_t)size, &bytes_read);
  48. if (bytes_read != (size_t)size)
  49. {
  50. free(val);
  51. return 0;
  52. }
  53. return size;
  54. }
  55. return 0; // actually, out of memory and not EOF, but still we should abort ASAP
  56. }
  57. else
  58. {
  59. reader->Skip(size);
  60. return size;
  61. }
  62. }
  63. #if 0
  64. int fseek64(nsmkv::MKVReader *reader, int64_t pos, int whence)
  65. {
  66. switch(whence)
  67. {
  68. case SEEK_SET:
  69. return fsetpos(f, &pos);
  70. case SEEK_CUR:
  71. {
  72. fpos_t curpos=0;
  73. int ret = fgetpos(f, &curpos);
  74. if (ret != 0)
  75. return ret;
  76. pos+=curpos;
  77. return fsetpos(f, &pos);
  78. }
  79. case SEEK_END:
  80. {
  81. return _fseeki64(f, pos, SEEK_END);
  82. }
  83. }
  84. return 1;
  85. }
  86. int64_t ftell64(nsmkv::MKVReader *reader)
  87. {
  88. fpos_t pos;
  89. if (fgetpos(f, &pos) == 0)
  90. return pos;
  91. else
  92. return -1L;
  93. }
  94. #endif
  95. uint64_t read_unsigned(nsmkv::MKVReader *reader, uint64_t size, uint64_t *val)
  96. {
  97. uint8_t data[8] = {0};
  98. if (size == 0 || size > 8)
  99. return 0;
  100. size_t bytes_read = 0;
  101. reader->Read(data, (size_t)size, &bytes_read);
  102. if (bytes_read != size)
  103. {
  104. return 0;
  105. }
  106. *val = unsigned_read_ptr_len(size, data);
  107. return size;
  108. }
  109. uint64_t read_float(nsmkv::MKVReader *reader, uint64_t size, double *val)
  110. {
  111. uint8_t data[10] = {0};
  112. if (size == 0 || size > 10)
  113. return 0;
  114. size_t bytes_read = 0;
  115. reader->Read(data, (size_t)size, &bytes_read);
  116. if (bytes_read != size)
  117. {
  118. return 0;
  119. }
  120. *val = float_read_ptr_len(size, data);
  121. return size;
  122. }
  123. uint64_t read_signed(nsmkv::MKVReader *reader, uint64_t size, int64_t *val)
  124. {
  125. uint8_t data[8] = {0};
  126. if (size == 0 || size > 8)
  127. return 0;
  128. size_t bytes_read = 0;
  129. reader->Read(data, (size_t)size, &bytes_read);
  130. if (bytes_read != size)
  131. {
  132. return 0;
  133. }
  134. *val = signed_read_ptr_len(size, data);
  135. return size;
  136. }