HexToString.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : ChunkView *
  23. * *
  24. * $Archive:: /Commando/Code/Tools/ChunkView/HexToString.cpp $*
  25. * *
  26. * Author:: Greg Hjelstrom *
  27. * *
  28. * $Modtime:: 9/28/99 9:48a $*
  29. * *
  30. * $Revision:: 2 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "stdafx.h"
  36. #include "HexToString.h"
  37. #include <ctype.h>
  38. const int BYTES_PER_LINE = 16;
  39. const int SHORTS_PER_LINE = 8;
  40. const int LONGS_PER_LINE = 4;
  41. HexToStringClass::HexToStringClass(const uint8 * data,uint32 size) :
  42. Data(data),
  43. Size(size)
  44. {
  45. }
  46. /*******************************************************************************************
  47. **
  48. ** HexToStringByteClass
  49. **
  50. *******************************************************************************************/
  51. HexToStringByteClass::HexToStringByteClass(const uint8 * data,uint32 size) :
  52. HexToStringClass(data,size)
  53. {
  54. Reset();
  55. }
  56. void HexToStringByteClass::Reset(void)
  57. {
  58. CurPos = Data;
  59. }
  60. bool HexToStringByteClass::Is_Done(void)
  61. {
  62. return CurPos >= Data + Size;
  63. }
  64. CString HexToStringByteClass::Get_Next_Line(void)
  65. {
  66. if (Is_Done()) return CString("");
  67. int i;
  68. CString line_string;
  69. CString tmp_string;
  70. const uint8 * workptr = CurPos;
  71. uint32 offset = (uint32)(CurPos - Data);
  72. int bytes_to_eat = min(BYTES_PER_LINE,Size - offset);
  73. // print hex dump
  74. line_string.Format("%08x: ",offset);
  75. for (i=0; i<bytes_to_eat; i++) {
  76. tmp_string.Format("%02X ",*workptr++);
  77. line_string += tmp_string;
  78. }
  79. // print blanks at end of buffer
  80. for (i=0; i<BYTES_PER_LINE - bytes_to_eat; i++)
  81. {
  82. line_string += CString(" ");
  83. }
  84. // spaces separate the hex from the characters
  85. line_string += CString(" ");
  86. workptr = CurPos;
  87. // print the characters
  88. for (i=0; i<bytes_to_eat; i++) {
  89. if (isalnum(*workptr)) {
  90. tmp_string.Format("%c",*workptr);
  91. } else {
  92. tmp_string.Format(".");
  93. }
  94. line_string += tmp_string;
  95. workptr++;
  96. }
  97. CurPos = workptr;
  98. return line_string;
  99. }
  100. /*******************************************************************************************
  101. **
  102. ** HexToStringShortClass
  103. **
  104. *******************************************************************************************/
  105. HexToStringShortClass::HexToStringShortClass(const uint8 * data,uint32 size) :
  106. HexToStringClass(data,size)
  107. {
  108. // Round size down to the nearest word
  109. Size &= ~1;
  110. Reset();
  111. }
  112. void HexToStringShortClass::Reset(void)
  113. {
  114. CurPos = (uint16*)Data;
  115. }
  116. bool HexToStringShortClass::Is_Done(void)
  117. {
  118. uint32 offset = (uint32)((uint8*)CurPos - Data);
  119. return offset >= Size;
  120. }
  121. CString HexToStringShortClass::Get_Next_Line(void)
  122. {
  123. if (Is_Done()) return CString("");
  124. int i;
  125. CString line_string;
  126. CString tmp_string;
  127. const uint16 * workptr = CurPos;
  128. uint32 offset = (uint32)((uint8*)CurPos - Data);
  129. int shorts_to_eat = min(SHORTS_PER_LINE,(Size - offset) / sizeof(uint16)); //yeah shorts_to_eat!
  130. // print hex dump
  131. line_string.Format("%08x: ",offset);
  132. for (i=0; i<shorts_to_eat; i++) {
  133. tmp_string.Format("%04X ",*workptr++);
  134. line_string += tmp_string;
  135. }
  136. CurPos = workptr;
  137. return line_string;
  138. }
  139. /*******************************************************************************************
  140. **
  141. ** HexToStringLongClass
  142. **
  143. *******************************************************************************************/
  144. HexToStringLongClass::HexToStringLongClass(const uint8 * data,uint32 size) :
  145. HexToStringClass(data,size)
  146. {
  147. // Round size down to the nearest long
  148. Size &= ~3;
  149. Reset();
  150. }
  151. void HexToStringLongClass::Reset(void)
  152. {
  153. CurPos = (uint32*)Data;
  154. }
  155. bool HexToStringLongClass::Is_Done(void)
  156. {
  157. uint32 offset = (uint32)((uint8*)CurPos - Data);
  158. return offset >= Size;
  159. }
  160. CString HexToStringLongClass::Get_Next_Line(void)
  161. {
  162. if (Is_Done()) return CString("");
  163. int i;
  164. CString line_string;
  165. CString tmp_string;
  166. const uint32 * workptr = CurPos;
  167. uint32 offset = (uint32)((uint8*)CurPos - Data);
  168. int longs_to_eat = min(LONGS_PER_LINE,(Size - offset)/sizeof(uint32));
  169. // print hex dump
  170. line_string.Format("%08x: ",offset);
  171. for (i=0; i<longs_to_eat; i++) {
  172. tmp_string.Format("%08X ",*workptr++);
  173. line_string += tmp_string;
  174. }
  175. CurPos = workptr;
  176. return line_string;
  177. }