/display.c

https://github.com/phhusson/QuasselC · C · 302 lines · 249 code · 28 blank · 25 comment · 22 complexity · be64d75fac2b4310265135441aeeb9cf MD5 · raw file

  1. /*
  2. This file is part of QuasselC.
  3. QuasselC is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 3.0 of the License, or (at your option) any later version.
  7. QuasselC is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with QuasselC. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <sys/types.h>
  15. #include <sys/socket.h>
  16. #include <netdb.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <unistd.h>
  20. #include <string.h>
  21. #include <iconv.h>
  22. #include "quasselc.h"
  23. int display_string(char *buf) {
  24. uint32_t size = *((uint32_t*)buf);
  25. size = ntohl(size);
  26. if(size==0xffffffff) {
  27. printf(" Str()");
  28. return 4;
  29. }
  30. buf+=4;
  31. int res=0;
  32. char *str=convert_back(buf, size, &res);
  33. printf(" Str('%s')", str);
  34. return size+4;
  35. }
  36. int display_bufferinfo(char *buf) {
  37. uint32_t id = *((uint32_t*)buf);
  38. id=ntohl(id);
  39. buf+=4;
  40. uint32_t networkID=*((uint32_t*)buf);
  41. networkID=ntohl(networkID);
  42. buf+=4;
  43. uint16_t type=*((uint16_t*)buf);
  44. type=ntohs(type);
  45. buf+=2;
  46. uint32_t groupID=*((uint32_t*)buf);
  47. groupID=ntohl(groupID);
  48. buf+=4;
  49. uint32_t size = *((uint32_t*)buf);
  50. size = ntohl(size);
  51. buf+=4;
  52. if(size==0xffffffff) {
  53. printf("Size was <0 !\n");
  54. size=0;
  55. }
  56. printf("BufferInfo(%d, %d, %d, %d, '%s')", id, networkID, type, groupID, buf);
  57. return 18+size;
  58. }
  59. int display_message(char *buf) {
  60. char *orig_buf=buf;
  61. uint32_t messageID=*((uint32_t*)buf);
  62. messageID=ntohl(messageID);
  63. buf+=4;
  64. uint32_t timestamp=*((uint32_t*)buf);
  65. timestamp=ntohl(timestamp);
  66. buf+=4;
  67. uint32_t type=*((uint32_t*)buf);
  68. type=ntohl(type);
  69. buf+=4;
  70. char flags=*buf;
  71. buf++;
  72. printf("Message(%d, %d, %d, %d, ", messageID, timestamp, type, flags);
  73. buf+=display_bufferinfo(buf);
  74. printf(", ");
  75. //Sender
  76. buf+=display_bytearray(buf);
  77. printf(", ");
  78. //Content
  79. buf+=display_bytearray(buf);
  80. printf(")\n");
  81. return buf-orig_buf;
  82. }
  83. int display_bytearray(char *buf) {
  84. uint32_t size = *((uint32_t*)buf);
  85. size = ntohl(size);
  86. if(size==0xffffffff) {
  87. printf("Bytearray()");
  88. return 4;
  89. } else {
  90. char *str=malloc(size+1);
  91. memcpy(str, buf+4, size);
  92. str[size]=0;
  93. printf("Bytearray('%s')", str);
  94. free(str);
  95. }
  96. return 4+size;
  97. }
  98. int display_usertype(char *buf) {
  99. char *orig_buf=buf;
  100. uint32_t size = *((uint32_t*)buf);
  101. size = ntohl(size);
  102. buf+=4;
  103. if(strcmp(buf, "NetworkId")==0 || strcmp(buf, "IdentityId")==0 || strcmp(buf, "BufferId")==0 || strcmp(buf, "MsgId")==0) {
  104. printf("%s(", buf);
  105. buf+=strlen(buf)+1;
  106. buf+=display_int(buf, 0);
  107. printf(")");
  108. } else if(strcmp(buf, "Identity")==0) {
  109. buf+=strlen(buf)+1;
  110. buf+=display_map(buf);
  111. } else if(strcmp(buf, "BufferInfo")==0) {
  112. buf+=strlen(buf)+1;
  113. buf+=display_bufferinfo(buf);
  114. } else if(strcmp(buf, "Message")==0) {
  115. buf+=strlen(buf)+1;
  116. buf+=display_message(buf);
  117. } else if(strcmp(buf, "Network::Server")==0) {
  118. buf+=strlen(buf)+1;
  119. buf+=display_map(buf);
  120. } else {
  121. printf(" Usertype('%s') \n", buf);
  122. printf("Unsupported.\n");
  123. exit(0);
  124. }
  125. return buf-orig_buf;
  126. }
  127. int display_int(char *buf, int type) {
  128. uint32_t size = *((uint32_t*)buf);
  129. size = ntohl(size);
  130. printf(" Int(%08x, %d) ", size, type);
  131. return 4;
  132. }
  133. int display_short(char *buf) {
  134. uint16_t size = *((uint16_t*)buf);
  135. size = ntohs(size);
  136. printf(" Short(%04x) ", size);
  137. return 2;
  138. }
  139. int display_date(char *buf) {
  140. uint32_t julianDay = *((uint32_t*)buf);
  141. julianDay=ntohl(julianDay);
  142. buf+=4;
  143. uint32_t secondSinceMidnight = *((uint32_t*)buf);
  144. secondSinceMidnight=ntohl(secondSinceMidnight);
  145. buf+=4;
  146. uint32_t isUTC = *buf;
  147. buf++;
  148. printf(" Date(%d, %d, %d) \n", julianDay, secondSinceMidnight, isUTC);
  149. return 9;
  150. }
  151. int display_time(char *buf) {
  152. uint32_t secondsSinceMidnight = *((uint32_t*)buf);
  153. secondsSinceMidnight=ntohl(secondsSinceMidnight);
  154. printf(" Time(%d) \n", secondsSinceMidnight);
  155. return 4;
  156. }
  157. int display_bool(char *buf) {
  158. printf(" Bool(%s) \n", *buf ? "TRUE" : "FALSE");
  159. return 1;
  160. }
  161. int display_map(char *buf) {
  162. char *orig_buf=buf;
  163. uint32_t elements = *((uint32_t*)buf);
  164. elements = ntohl(elements);
  165. printf("Got %d elements\n", elements);
  166. printf("Map(\n");
  167. buf+=4;
  168. uint32_t i;
  169. for(i=0;i<elements;++i) {
  170. //key
  171. printf("key= ");
  172. buf+=display_string(buf);
  173. printf(", ");
  174. //Value
  175. printf("value= ");
  176. buf+=display_qvariant(buf);
  177. printf("\n");
  178. }
  179. printf(");\n");
  180. return buf-orig_buf;
  181. }
  182. int display_list(char *buf) {
  183. char *orig_buf=buf;
  184. uint32_t elements = *((uint32_t*)buf);
  185. elements = ntohl(elements);
  186. printf("Got %d elements\n", elements);
  187. printf("List(\n");
  188. buf+=4;
  189. uint32_t i;
  190. for(i=0;i<elements;++i) {
  191. //Value
  192. printf("value= ");
  193. buf+=display_qvariant(buf);
  194. printf("\n");
  195. }
  196. printf(");\n");
  197. return buf-orig_buf;
  198. }
  199. int display_stringlist(char *buf) {
  200. char *orig_buf=buf;
  201. uint32_t elements = *((uint32_t*)buf);
  202. elements = ntohl(elements);
  203. printf("Got %d elements\n", elements);
  204. printf("StringList(\n");
  205. buf+=4;
  206. uint32_t i;
  207. for(i=0;i<elements;++i) {
  208. //Value
  209. printf("value= ");
  210. buf+=display_string(buf);
  211. }
  212. printf(");\n");
  213. return buf-orig_buf;
  214. }
  215. int display_qvariant(char *buf) {
  216. char *orig_buf=buf;
  217. uint32_t type = *((uint32_t*)buf);
  218. type=ntohl(type);
  219. buf+=4;
  220. char null=*buf;
  221. buf++;
  222. if(null) {
  223. //Nothing to do
  224. }
  225. switch(type) {
  226. case 1:
  227. buf+=display_bool(buf);
  228. break;
  229. case 2:
  230. case 3:
  231. buf+=display_int(buf, type);
  232. break;
  233. case 7:
  234. //UTF16 byte
  235. buf+=display_short(buf);
  236. break;
  237. case 8:
  238. buf+=display_map(buf);
  239. break;
  240. case 9:
  241. buf+=display_list(buf);
  242. break;
  243. case 10:
  244. buf+=display_string(buf);
  245. break;
  246. case 11:
  247. buf+=display_stringlist(buf);
  248. break;
  249. case 12:
  250. buf+=display_bytearray(buf);
  251. break;
  252. case 15:
  253. buf+=display_time(buf);
  254. break;
  255. case 16:
  256. buf+=display_date(buf);
  257. break;
  258. case 127:
  259. //User type !
  260. buf+=display_usertype(buf);
  261. break;
  262. case 133:
  263. buf+=display_short(buf);
  264. break;
  265. default:
  266. printf("Unknown QVariant type: %d\n", type);
  267. exit(-1);
  268. break;
  269. };
  270. return buf-orig_buf;
  271. }