Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

171 lines
3.9 KiB

  1. /* 7zStream.c -- 7z Stream functions
  2. 2013-11-12 : Igor Pavlov : Public domain */
  3. #include "Precomp.h"
  4. #include <string.h>
  5. #include "7zTypes.h"
  6. SRes SeqInStream_Read2(ISeqInStream *stream, void *buf, size_t size, SRes errorType)
  7. {
  8. while (size != 0)
  9. {
  10. size_t processed = size;
  11. RINOK(stream->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(ISeqInStream *stream, void *buf, size_t size)
  20. {
  21. return SeqInStream_Read2(stream, buf, size, SZ_ERROR_INPUT_EOF);
  22. }
  23. SRes SeqInStream_ReadByte(ISeqInStream *stream, Byte *buf)
  24. {
  25. size_t processed = 1;
  26. RINOK(stream->Read(stream, buf, &processed));
  27. return (processed == 1) ? SZ_OK : SZ_ERROR_INPUT_EOF;
  28. }
  29. SRes LookInStream_SeekTo(ILookInStream *stream, UInt64 offset)
  30. {
  31. Int64 t = offset;
  32. return stream->Seek(stream, &t, SZ_SEEK_SET);
  33. }
  34. SRes LookInStream_LookRead(ILookInStream *stream, void *buf, size_t *size)
  35. {
  36. const void *lookBuf;
  37. if (*size == 0)
  38. return SZ_OK;
  39. RINOK(stream->Look(stream, &lookBuf, size));
  40. memcpy(buf, lookBuf, *size);
  41. return stream->Skip(stream, *size);
  42. }
  43. SRes LookInStream_Read2(ILookInStream *stream, void *buf, size_t size, SRes errorType)
  44. {
  45. while (size != 0)
  46. {
  47. size_t processed = size;
  48. RINOK(stream->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(ILookInStream *stream, void *buf, size_t size)
  57. {
  58. return LookInStream_Read2(stream, buf, size, SZ_ERROR_INPUT_EOF);
  59. }
  60. static SRes LookToRead_Look_Lookahead(void *pp, const void **buf, size_t *size)
  61. {
  62. SRes res = SZ_OK;
  63. CLookToRead *p = (CLookToRead *)pp;
  64. size_t size2 = p->size - p->pos;
  65. if (size2 == 0 && *size > 0)
  66. {
  67. p->pos = 0;
  68. size2 = LookToRead_BUF_SIZE;
  69. res = p->realStream->Read(p->realStream, p->buf, &size2);
  70. p->size = size2;
  71. }
  72. if (size2 < *size)
  73. *size = size2;
  74. *buf = p->buf + p->pos;
  75. return res;
  76. }
  77. static SRes LookToRead_Look_Exact(void *pp, const void **buf, size_t *size)
  78. {
  79. SRes res = SZ_OK;
  80. CLookToRead *p = (CLookToRead *)pp;
  81. size_t size2 = p->size - p->pos;
  82. if (size2 == 0 && *size > 0)
  83. {
  84. p->pos = 0;
  85. if (*size > LookToRead_BUF_SIZE)
  86. *size = LookToRead_BUF_SIZE;
  87. res = p->realStream->Read(p->realStream, p->buf, size);
  88. size2 = p->size = *size;
  89. }
  90. if (size2 < *size)
  91. *size = size2;
  92. *buf = p->buf + p->pos;
  93. return res;
  94. }
  95. static SRes LookToRead_Skip(void *pp, size_t offset)
  96. {
  97. CLookToRead *p = (CLookToRead *)pp;
  98. p->pos += offset;
  99. return SZ_OK;
  100. }
  101. static SRes LookToRead_Read(void *pp, void *buf, size_t *size)
  102. {
  103. CLookToRead *p = (CLookToRead *)pp;
  104. size_t rem = p->size - p->pos;
  105. if (rem == 0)
  106. return p->realStream->Read(p->realStream, buf, size);
  107. if (rem > *size)
  108. rem = *size;
  109. memcpy(buf, p->buf + p->pos, rem);
  110. p->pos += rem;
  111. *size = rem;
  112. return SZ_OK;
  113. }
  114. static SRes LookToRead_Seek(void *pp, Int64 *pos, ESzSeek origin)
  115. {
  116. CLookToRead *p = (CLookToRead *)pp;
  117. p->pos = p->size = 0;
  118. return p->realStream->Seek(p->realStream, pos, origin);
  119. }
  120. void LookToRead_CreateVTable(CLookToRead *p, int lookahead)
  121. {
  122. p->s.Look = lookahead ?
  123. LookToRead_Look_Lookahead :
  124. LookToRead_Look_Exact;
  125. p->s.Skip = LookToRead_Skip;
  126. p->s.Read = LookToRead_Read;
  127. p->s.Seek = LookToRead_Seek;
  128. }
  129. void LookToRead_Init(CLookToRead *p)
  130. {
  131. p->pos = p->size = 0;
  132. }
  133. static SRes SecToLook_Read(void *pp, void *buf, size_t *size)
  134. {
  135. CSecToLook *p = (CSecToLook *)pp;
  136. return LookInStream_LookRead(p->realStream, buf, size);
  137. }
  138. void SecToLook_CreateVTable(CSecToLook *p)
  139. {
  140. p->s.Read = SecToLook_Read;
  141. }
  142. static SRes SecToRead_Read(void *pp, void *buf, size_t *size)
  143. {
  144. CSecToRead *p = (CSecToRead *)pp;
  145. return p->realStream->Read(p->realStream, buf, size);
  146. }
  147. void SecToRead_CreateVTable(CSecToRead *p)
  148. {
  149. p->s.Read = SecToRead_Read;
  150. }