7zStream.c 4.0 KB

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