datastring.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #include "datastring.h"
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include "sha256.h"
  5. datastring datastring::substr(int start)
  6. {
  7. return substr(start,length-start);
  8. }
  9. datastring datastring::substr(int start,int len)
  10. {
  11. datastring output;
  12. if (start < 0) {
  13. start += length;
  14. }
  15. if ((start < 0) || (start >= length)) {
  16. output.data = nullptr;
  17. output.length = 0;
  18. output.null_terminated = false;
  19. } else {
  20. if (start + len > length) {
  21. len = length - start;
  22. }
  23. output.data = data+start;
  24. output.length = len;
  25. output.null_terminated == null_terminated;
  26. if (start + len != length) {
  27. output.null_terminated = false;
  28. }
  29. }
  30. return output;
  31. }
  32. void datastring::clear()
  33. {
  34. static char empty;
  35. empty = 0;
  36. length = 0;
  37. data = &empty;
  38. }
  39. datastring datastring::anemptystring()
  40. {
  41. static char x = 0;
  42. datastring output;
  43. output.null_terminated = true;
  44. output.length = 0;
  45. output.data = &x;
  46. return output;
  47. }
  48. datastring &datastring::operator=(const char *item)
  49. {
  50. data = (char *)item;
  51. length = strlen(item);
  52. null_terminated = true;
  53. return *this;
  54. }
  55. int datastring::compare(const char *item)
  56. {
  57. char ch2;
  58. char ch1;
  59. int start = 0;
  60. if (item == nullptr) {
  61. return length > 0 ? 1 : 0;
  62. }
  63. if ((data == nullptr) || (length == 0)) {
  64. return item == nullptr ? 0 : -1;
  65. }
  66. while(true) {
  67. ch2=*(item++);
  68. if (ch2 == 0) {
  69. break;
  70. }
  71. if (start >= length) {
  72. return -1;
  73. }
  74. ch1 = data[start];
  75. if (ch1 > ch2) {
  76. return 1;
  77. }
  78. if (ch1 < ch2) {
  79. return -1;
  80. }
  81. start++;
  82. }
  83. return start == length ? 0 : 1;
  84. }
  85. int datastring::compare(datastring &item)
  86. {
  87. int cmp;
  88. int cmp_length = length < item.length ? length : item.length;
  89. if (cmp_length == 0) {
  90. cmp = 0;
  91. } else {
  92. cmp = memcmp(data,item.data,cmp_length);
  93. }
  94. if (cmp == 0) {
  95. if (length > item.length) {
  96. cmp = 1;
  97. } else if (length < item.length) {
  98. cmp = -1;
  99. }
  100. }
  101. return cmp;
  102. }
  103. void datastring::remove_bytes_from_front(int number_of_bytes)
  104. {
  105. // removes some bytes from the front of the string.
  106. if (number_of_bytes > length) {
  107. number_of_bytes = length;
  108. }
  109. data += number_of_bytes;
  110. length -= number_of_bytes;
  111. return;
  112. }
  113. void datastring::print()
  114. {
  115. int loop;
  116. for(loop=0;loop<length;loop++) {
  117. printf("%c",data[loop]);
  118. }
  119. return;
  120. }
  121. void datastring::println()
  122. {
  123. print();
  124. printf("\n");
  125. return;
  126. }
  127. void datastring::null_terminate()
  128. {
  129. // Make sure this string is null terminated.
  130. if (data != nullptr) {
  131. data[length] = 0;
  132. }
  133. return;
  134. }
  135. void datastring::sha_256(datastring &output)
  136. {
  137. const char *hex = "0123456789abcdef";
  138. char *output_data;
  139. int loop;
  140. sha256 hash;
  141. uint8_t input;
  142. if (output.length == SHA256_BLOCK_SIZE << 1) {
  143. output_data = output.data;
  144. hash.sha256_init();
  145. hash.sha256_update((uint8_t *)data,length);
  146. hash.sha256_final((uint8_t *)output_data);
  147. // convert the output into hex.
  148. for(loop = SHA256_BLOCK_SIZE-1;loop>=0;loop--) {
  149. input = output_data[loop];
  150. output_data[loop << 1] = hex[input >> 4];
  151. output_data[(loop << 1) + 1] = hex[input & 15];
  152. }
  153. }
  154. return;
  155. }