Stream.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #include "Stream.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. void streamInit(Stream* s, int size)
  6. {
  7. s->index = 4;
  8. s->size = size;
  9. if(size > 0)
  10. {
  11. s->data = malloc(sizeof(char) * size);
  12. }
  13. else
  14. {
  15. s->data = NULL;
  16. }
  17. }
  18. void streamRemove(Stream* s)
  19. {
  20. s->index = 0;
  21. s->size = 0;
  22. free(s->data);
  23. s->data = NULL;
  24. }
  25. void streamUpdateIndex(Stream* s)
  26. {
  27. if(s->size >= 4)
  28. {
  29. s->data[0] = s->index & 0xFF;
  30. s->data[1] = (s->index >> 8) & 0xFF;
  31. s->data[2] = (s->index >> 16) & 0xFF;
  32. s->data[3] = (s->index >> 24) & 0xFF;
  33. }
  34. }
  35. int streamGetIndex(Stream* s)
  36. {
  37. if(s->size >= 4)
  38. {
  39. return (s->data[0] & 0xFF) | ((s->data[1] & 0xFF) << 8) | ((s->data[2] & 0xFF) << 16) | ((s->data[3] & 0xFF) << 24);
  40. }
  41. return -1;
  42. }
  43. int streamGetPosition(Stream* s)
  44. {
  45. return s->index;
  46. }
  47. void streamSetPosition(Stream* s, int pos)
  48. {
  49. if(pos >= 4 && pos < s->size)
  50. {
  51. s->index = pos;
  52. }
  53. }
  54. void streamEnsureCapacity(Stream* s, int length)
  55. {
  56. if(length >= s->size)
  57. {
  58. int newSize = s->size;
  59. if(newSize >= 0)
  60. {
  61. newSize = 1;
  62. }
  63. while(length >= newSize)
  64. {
  65. newSize *= 2;
  66. }
  67. char* newData = malloc(sizeof(char) * newSize);
  68. memcpy(newData, s->data, s->size);
  69. free(s->data);
  70. s->data = newData;
  71. //printf("RESIZE FROM %d to %d\n", s->size, newSize);
  72. s->size = newSize;
  73. }
  74. }
  75. int streamGetChar(Stream* in, char* c)
  76. {
  77. if(in->index < in->size)
  78. {
  79. *c = in->data[in->index];
  80. in->index++;
  81. return 0;
  82. }
  83. return -1;
  84. }
  85. int streamGetChars(Stream* in, char* buffer, int length)
  86. {
  87. int index = 0;
  88. length--;
  89. while(index < length)
  90. {
  91. char c;
  92. if(streamGetChar(in, &c) == -1)
  93. {
  94. buffer[index] = '\0';
  95. return index;
  96. }
  97. else
  98. {
  99. buffer[index] = c;
  100. }
  101. index++;
  102. }
  103. buffer[index] = '\0';
  104. return index;
  105. }
  106. int streamGetShort(Stream* in, short* s)
  107. {
  108. if(in->index + 1 < in->size)
  109. {
  110. *s = (in->data[in->index] & 0xFF) | ((in->data[in->index + 1] & 0xFF) << 8);
  111. in->index += 2;
  112. return 0;
  113. }
  114. return -1;
  115. }
  116. int streamGetInt(Stream* in, int* i)
  117. {
  118. if(in->index + 3 < in->size)
  119. {
  120. *i = (in->data[in->index] & 0xFF) | ((in->data[in->index + 1] & 0xFF) << 8) |
  121. ((in->data[in->index + 2] & 0xFF) << 16) | ((in->data[in->index + 3] & 0xFF) << 24);
  122. in->index += 4;
  123. return 0;
  124. }
  125. return -1;
  126. }
  127. int streamWriteChar(Stream* out, char c)
  128. {
  129. streamEnsureCapacity(out, out->index);
  130. if(out->index < out->size)
  131. {
  132. out->data[out->index] = c;
  133. out->index++;
  134. return 0;
  135. }
  136. return -1;
  137. }
  138. int streamWriteChars(Stream* out, char* c)
  139. {
  140. int i = 0;
  141. while(c[i] != '\0')
  142. {
  143. if(streamWriteChar(out, c[i]) == -1)
  144. {
  145. return -1;
  146. }
  147. i++;
  148. }
  149. return 0;
  150. }
  151. int streamWriteShort(Stream* out, short s)
  152. {
  153. streamEnsureCapacity(out, out->index + 1);
  154. if(out->index + 1 < out->size)
  155. {
  156. out->data[out->index] = s & 0xFF;
  157. out->data[out->index + 1] = (s >> 8) & 0xFF;
  158. out->index += 2;
  159. return 0;
  160. }
  161. return -1;
  162. }
  163. int streamWriteInt(Stream* out, int i)
  164. {
  165. streamEnsureCapacity(out, out->index + 3);
  166. if(out->index + 3 < out->size)
  167. {
  168. out->data[out->index] = i & 0xFF;
  169. out->data[out->index + 1] = (i >> 8) & 0xFF;
  170. out->data[out->index + 2] = (i >> 16) & 0xFF;
  171. out->data[out->index + 3] = (i >> 24) & 0xFF;
  172. out->index += 4;
  173. return 0;
  174. }
  175. return -1;
  176. }