main.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #include <string.h>
  2. #include <sys/stat.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include "protobuf.h"
  6. /**
  7. * opens a file to give a list of fields
  8. */
  9. size_t os_utils_file_size(const char* path) {
  10. size_t file_size = 0;
  11. // open file
  12. FILE* in_file = fopen(path, "r");
  13. if (in_file == NULL)
  14. return 0;
  15. // determine size
  16. fseek(in_file, 0L, SEEK_END);
  17. file_size = ftell(in_file);
  18. fclose(in_file);
  19. return file_size;
  20. }
  21. char* getWireType(enum WireType type) {
  22. switch(type) {
  23. case(WIRETYPE_VARINT):
  24. return "Varint";
  25. case (WIRETYPE_LENGTH_DELIMITED):
  26. return "Length Delimited";
  27. case (WIRETYPE_64BIT):
  28. return "64 Bit";
  29. case (WIRETYPE_START_GROUP):
  30. return "Start Group";
  31. case (WIRETYPE_END_GROUP):
  32. return "End Group";
  33. case (WIRETYPE_32BIT):
  34. return "32 Bit";
  35. }
  36. return "Wire Type Invalid";
  37. }
  38. void stripit(int argc, char** argv) {
  39. char tmp[strlen(argv[argc])];
  40. strcpy(tmp, &argv[argc][1]);
  41. tmp[strlen(tmp)-1] = 0;
  42. strcpy(argv[argc], tmp);
  43. return;
  44. }
  45. void strip_quotes(int argc, char** argv) {
  46. for(int i = 0; i < argc; i++) {
  47. if (argv[i][0] == '\'' && argv[i][strlen(argv[i])-1] == '\'') {
  48. stripit(i, argv);
  49. }
  50. }
  51. }
  52. int main(int argc, char** argv) {
  53. strip_quotes(argc, argv);
  54. if (argc <= 1) {
  55. printf("Syntax: %s [filename] [start_pos]\n", argv[0]);
  56. return 0;
  57. }
  58. char* fileName = argv[1];
  59. size_t numBytes = os_utils_file_size(fileName);
  60. printf("Number of bytes in file: %lu\n", numBytes);
  61. if (numBytes == 0)
  62. return 0;
  63. FILE* file = fopen(fileName, "rb");
  64. unsigned char buffer[numBytes];
  65. if (fread(buffer, 1, numBytes, file) != numBytes) {
  66. printf("Unexpected number of bytes.\n");
  67. return 0;
  68. }
  69. //skip ahead
  70. size_t skip = 0;
  71. if (argc > 2) {
  72. skip = atoi(argv[2]);
  73. if (skip > 0) {
  74. printf("Skipping first %lu bytes\n", skip);
  75. }
  76. }
  77. int fieldCounter = 0;
  78. size_t pos = skip;
  79. while(pos < numBytes) {
  80. size_t bytes_read = 0;
  81. int field_no = 0;
  82. enum WireType wire_type = WIRETYPE_64BIT;
  83. if (protobuf_decode_field_and_type(&buffer[pos], numBytes - pos, &field_no, &wire_type, &bytes_read) == 0) {
  84. printf("Unexpected return value from protobuf_decode_field_and_type at pos %lu\n", pos);
  85. return 0;
  86. }
  87. printf("Field counter: %d at position %lu, Field Number: %d, Wire Type: %s", fieldCounter, pos, field_no, getWireType(wire_type));
  88. pos += bytes_read;
  89. // read the value
  90. switch(wire_type) {
  91. case(WIRETYPE_VARINT): {
  92. unsigned long long varint = 0;
  93. protobuf_decode_varint(&buffer[pos], numBytes-pos, &varint, &bytes_read);
  94. pos += bytes_read;
  95. printf(" Value: %llu. Next read position at %lu\n", varint, pos);
  96. break;
  97. }
  98. case (WIRETYPE_LENGTH_DELIMITED): {
  99. unsigned long long varint = 0;
  100. protobuf_decode_varint(&buffer[pos], numBytes-pos, &varint, &bytes_read);
  101. pos += bytes_read;
  102. printf(" Field width: %llu. Next read position at %lu\n", varint, pos);
  103. pos += varint;
  104. break;
  105. }
  106. default:
  107. pos++;
  108. }
  109. fieldCounter++;
  110. }
  111. return 0;
  112. }