Stream.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. void streamEnsureCapacity(Stream* s, int length)
  44. {
  45. if(length >= s->size)
  46. {
  47. int newSize = s->size;
  48. if(newSize >= 0)
  49. {
  50. newSize = 1;
  51. }
  52. while(length >= newSize)
  53. {
  54. newSize *= 2;
  55. }
  56. char* newData = malloc(sizeof(char) * newSize);
  57. memcpy(newData, s->data, s->size);
  58. free(s->data);
  59. s->data = newData;
  60. //printf("RESIZE FROM %d to %d\n", s->size, newSize);
  61. s->size = newSize;
  62. }
  63. }
  64. int streamGetChar(Stream* in, char* c)
  65. {
  66. if(in->index < in->size)
  67. {
  68. *c = in->data[in->index];
  69. in->index++;
  70. return 0;
  71. }
  72. return -1;
  73. }
  74. int streamGetChars(Stream* in, char* buffer, int length)
  75. {
  76. int index = 0;
  77. length--;
  78. while(index < length)
  79. {
  80. char c;
  81. if(streamGetChar(in, &c) == -1)
  82. {
  83. buffer[index] = '\0';
  84. return index;
  85. }
  86. else
  87. {
  88. buffer[index] = c;
  89. }
  90. index++;
  91. }
  92. buffer[index] = '\0';
  93. return index;
  94. }
  95. int streamGetShort(Stream* in, short* s)
  96. {
  97. if(in->index + 1 < in->size)
  98. {
  99. *s = (in->data[in->index] & 0xFF) | ((in->data[in->index + 1] & 0xFF) << 8);
  100. in->index += 2;
  101. return 0;
  102. }
  103. return -1;
  104. }
  105. int streamGetInt(Stream* in, int* i)
  106. {
  107. if(in->index + 3 < in->size)
  108. {
  109. *i = (in->data[in->index] & 0xFF) | ((in->data[in->index + 1] & 0xFF) << 8) |
  110. ((in->data[in->index + 2] & 0xFF) << 16) | ((in->data[in->index + 3] & 0xFF) << 24);
  111. in->index += 4;
  112. return 0;
  113. }
  114. return -1;
  115. }
  116. int streamWriteChar(Stream* out, char c)
  117. {
  118. streamEnsureCapacity(out, out->index);
  119. if(out->index < out->size)
  120. {
  121. out->data[out->index] = c;
  122. out->index++;
  123. return 0;
  124. }
  125. return -1;
  126. }
  127. int streamWriteChars(Stream* out, char* c)
  128. {
  129. int i = 0;
  130. while(c[i] != '\0')
  131. {
  132. if(streamWriteChar(out, c[i]) == -1)
  133. {
  134. return -1;
  135. }
  136. i++;
  137. }
  138. return 0;
  139. }
  140. int streamWriteShort(Stream* out, short s)
  141. {
  142. streamEnsureCapacity(out, out->index + 1);
  143. if(out->index + 1 < out->size)
  144. {
  145. out->data[out->index] = s & 0xFF;
  146. out->data[out->index + 1] = (s >> 8) & 0xFF;
  147. out->index += 2;
  148. return 0;
  149. }
  150. return -1;
  151. }
  152. int streamWriteInt(Stream* out, int i)
  153. {
  154. streamEnsureCapacity(out, out->index + 3);
  155. if(out->index + 3 < out->size)
  156. {
  157. out->data[out->index] = i & 0xFF;
  158. out->data[out->index + 1] = (i >> 8) & 0xFF;
  159. out->data[out->index + 2] = (i >> 16) & 0xFF;
  160. out->data[out->index + 3] = (i >> 24) & 0xFF;
  161. out->index += 4;
  162. return 0;
  163. }
  164. return -1;
  165. }