7zStream.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* 7zStream.c -- 7z Stream functions
  2. 2017-04-03 : Igor Pavlov : Public domain */
  3. #include "Precomp.h"
  4. #include <string.h>
  5. #include "7zTypes.h"
  6. SRes SeqInStream_Read2(const ISeqInStream *stream, void *buf, size_t size, SRes errorType)
  7. {
  8. while (size != 0)
  9. {
  10. size_t processed = size;
  11. RINOK(ISeqInStream_Read(stream, buf, &processed));
  12. if (processed == 0)
  13. return errorType;
  14. buf = (void *)((Byte *)buf + processed);
  15. size -= processed;
  16. }
  17. return SZ_OK;
  18. }
  19. SRes SeqInStream_Read(const ISeqInStream *stream, void *buf, size_t size)
  20. {
  21. return SeqInStream_Read2(stream, buf, size, SZ_ERROR_INPUT_EOF);
  22. }
  23. SRes SeqInStream_ReadByte(const ISeqInStream *stream, Byte *buf)
  24. {
  25. size_t processed = 1;
  26. RINOK(ISeqInStream_Read(stream, buf, &processed));
  27. return (processed == 1) ? SZ_OK : SZ_ERROR_INPUT_EOF;
  28. }
  29. SRes LookInStream_SeekTo(const ILookInStream *stream, UInt64 offset)
  30. {
  31. Int64 t = offset;
  32. return ILookInStream_Seek(stream, &t, SZ_SEEK_SET);
  33. }
  34. SRes LookInStream_LookRead(const ILookInStream *stream, void *buf, size_t *size)
  35. {
  36. const void *lookBuf;
  37. if (*size == 0)
  38. return SZ_OK;
  39. RINOK(ILookInStream_Look(stream, &lookBuf, size));
  40. memcpy(buf, lookBuf, *size);
  41. return ILookInStream_Skip(stream, *size);
  42. }
  43. SRes LookInStream_Read2(const ILookInStream *stream, void *buf, size_t size, SRes errorType)
  44. {
  45. while (size != 0)
  46. {
  47. size_t processed = size;
  48. RINOK(ILookInStream_Read(stream, buf, &processed));
  49. if (processed == 0)
  50. return errorType;
  51. buf = (void *)((Byte *)buf + processed);
  52. size -= processed;
  53. }
  54. return SZ_OK;
  55. }
  56. SRes LookInStream_Read(const ILookInStream *stream, void *buf, size_t size)
  57. {
  58. return LookInStream_Read2(stream, buf, size, SZ_ERROR_INPUT_EOF);
  59. }
  60. #define GET_LookToRead2 CLookToRead2 *p = CONTAINER_FROM_VTBL(pp, CLookToRead2, vt);
  61. static SRes LookToRead2_Look_Lookahead(const ILookInStream *pp, const void **buf, size_t *size)
  62. {
  63. SRes res = SZ_OK;
  64. GET_LookToRead2
  65. size_t size2 = p->size - p->pos;
  66. if (size2 == 0 && *size != 0)
  67. {
  68. p->pos = 0;
  69. p->size = 0;
  70. size2 = p->bufSize;
  71. res = ISeekInStream_Read(p->realStream, p->buf, &size2);
  72. p->size = size2;
  73. }
  74. if (*size > size2)
  75. *size = size2;
  76. *buf = p->buf + p->pos;
  77. return res;
  78. }
  79. static SRes LookToRead2_Look_Exact(const ILookInStream *pp, const void **buf, size_t *size)
  80. {
  81. SRes res = SZ_OK;
  82. GET_LookToRead2
  83. size_t size2 = p->size - p->pos;
  84. if (size2 == 0 && *size != 0)
  85. {
  86. p->pos = 0;
  87. p->size = 0;
  88. if (*size > p->bufSize)
  89. *size = p->bufSize;
  90. res = ISeekInStream_Read(p->realStream, p->buf, size);
  91. size2 = p->size = *size;
  92. }
  93. if (*size > size2)
  94. *size = size2;
  95. *buf = p->buf + p->pos;
  96. return res;
  97. }
  98. static SRes LookToRead2_Skip(const ILookInStream *pp, size_t offset)
  99. {
  100. GET_LookToRead2
  101. p->pos += offset;
  102. return SZ_OK;
  103. }
  104. static SRes LookToRead2_Read(const ILookInStream *pp, void *buf, size_t *size)
  105. {
  106. GET_LookToRead2
  107. size_t rem = p->size - p->pos;
  108. if (rem == 0)
  109. return ISeekInStream_Read(p->realStream, buf, size);
  110. if (rem > *size)
  111. rem = *size;
  112. memcpy(buf, p->buf + p->pos, rem);
  113. p->pos += rem;
  114. *size = rem;
  115. return SZ_OK;
  116. }
  117. static SRes LookToRead2_Seek(const ILookInStream *pp, Int64 *pos, ESzSeek origin)
  118. {
  119. GET_LookToRead2
  120. p->pos = p->size = 0;
  121. return ISeekInStream_Seek(p->realStream, pos, origin);
  122. }
  123. void LookToRead2_CreateVTable(CLookToRead2 *p, int lookahead)
  124. {
  125. p->vt.Look = lookahead ?
  126. LookToRead2_Look_Lookahead :
  127. LookToRead2_Look_Exact;
  128. p->vt.Skip = LookToRead2_Skip;
  129. p->vt.Read = LookToRead2_Read;
  130. p->vt.Seek = LookToRead2_Seek;
  131. }
  132. static SRes SecToLook_Read(const ISeqInStream *pp, void *buf, size_t *size)
  133. {
  134. CSecToLook *p = CONTAINER_FROM_VTBL(pp, CSecToLook, vt);
  135. return LookInStream_LookRead(p->realStream, buf, size);
  136. }
  137. void SecToLook_CreateVTable(CSecToLook *p)
  138. {
  139. p->vt.Read = SecToLook_Read;
  140. }
  141. static SRes SecToRead_Read(const ISeqInStream *pp, void *buf, size_t *size)
  142. {
  143. CSecToRead *p = CONTAINER_FROM_VTBL(pp, CSecToRead, vt);
  144. return ILookInStream_Read(p->realStream, buf, size);
  145. }
  146. void SecToRead_CreateVTable(CSecToRead *p)
  147. {
  148. p->vt.Read = SecToRead_Read;
  149. }