The little things give you away... A collection of various small helper stuff
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

164 lines
5.2 KiB

  1. #define _GNU_SOURCE
  2. #include <ctype.h>
  3. #include <stdbool.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #ifndef BUFSIZE
  8. #define BUFSIZE 1048576
  9. #endif
  10. #define STATE_BEFORE_RECORD 0
  11. #define STATE_RESPONSE_RECORD 1
  12. #define STATE_OTHER_RECORD 2
  13. #ifdef DEBUG
  14. #define DEBUG_PRINTF(...) do { fprintf(stderr, __VA_ARGS__); } while (false)
  15. #else
  16. #define DEBUG_PRINTF(...) do {} while (false)
  17. #endif
  18. int main(int argc, char* argv[]) {
  19. //TODO --meta or a similar way to get something like that?
  20. // Read stdin, decode WARC, dump all response record bodies to stdout.
  21. // One LF is inserted at the end of each response to ensure that a new record always begins on a new line.
  22. // Headers must fit into BUFSIZE.
  23. char buf[2 * BUFSIZE];
  24. size_t n;
  25. int state = STATE_BEFORE_RECORD;
  26. char* bufp;
  27. char* m0;
  28. char* m1;
  29. size_t record_bytes_read;
  30. size_t record_length;
  31. size_t nscan;
  32. while ((n = fread(buf, 1, BUFSIZE, stdin)) > 0) {
  33. bufp = buf;
  34. checkstate:
  35. DEBUG_PRINTF("Have %zu bytes of buffer (at %p)\n", n, (void*)bufp);
  36. DEBUG_PRINTF("Beginning of buffer: ");
  37. for (int i = 0; i < 64; ++i) DEBUG_PRINTF(isprint(*(bufp + i)) ? "%c" : "\\x%02x", *(bufp + i) & 0xFF);
  38. DEBUG_PRINTF("\n");
  39. if (n == 0) {
  40. break;
  41. }
  42. DEBUG_PRINTF("State: %d\n", state);
  43. if (state == STATE_BEFORE_RECORD) {
  44. if (n < 10) {
  45. fprintf(stderr, "Error: too little data before WARC headers\n");
  46. return 1;
  47. }
  48. if (memcmp(bufp, "WARC/1.0\r\n", 10) == 0 || memcmp(bufp, "WARC/1.1\r\n", 10) == 0) {
  49. // Got some headers; find the record type, content length, and end of headers
  50. m0 = memmem(bufp, n, "\r\nContent-Length:", 17);
  51. if (!m0) {
  52. fprintf(stderr, "Error: Content-Length missing\n");
  53. return 1;
  54. }
  55. DEBUG_PRINTF("Found Content-Length header at %p (offset %zu)\n", (void*)(m0 + 2), m0 + 2 - bufp);
  56. m1 = memmem(m0 + 1, n - (m0 + 1 - bufp), "\r\n", 2);
  57. if (!m1) {
  58. fprintf(stderr, "Error: CRLF after Content-Length missing\n");
  59. return 1;
  60. }
  61. m0 += 17;
  62. while (m0 < bufp + n && (*m0 == ' ' || *m0 == '\t')) ++m0;
  63. if (!sscanf(m0, "%zu%n", &record_length, &nscan)) {
  64. fprintf(stderr, "Error: invalid Content-Length\n");
  65. return 1;
  66. }
  67. if (nscan > n - (m0 - bufp)) {
  68. fprintf(stderr, "Error: buffer overread\n");
  69. return 1;
  70. }
  71. m0 += nscan;
  72. while (m0 < bufp + n && (*m0 == ' ' || *m0 == '\t')) ++m0;
  73. if (m0 != m1) {
  74. fprintf(stderr, "Error: invalid Content-Length (noise before EOL)\n");
  75. return 1;
  76. }
  77. DEBUG_PRINTF("Record body length: %zu\n", record_length);
  78. m0 = memmem(bufp, n, "\r\nWARC-Type:", 12);
  79. if (!m0) {
  80. fprintf(stderr, "Error: WARC-Type missing\n");
  81. return 1;
  82. }
  83. DEBUG_PRINTF("Found WARC-Type header at %p (offset %zu)\n", (void*)(m0 + 2), m0 + 2 - bufp);
  84. m1 = memmem(m0, n - (m0 - bufp), "\r\n", 2);
  85. if (!m1) {
  86. fprintf(stderr, "Error: CRLF after WARC-Type missing\n");
  87. return 1;
  88. }
  89. m0 += 12;
  90. while (m0 < bufp + n && (*m0 == ' ' || *m0 == '\t')) ++m0;
  91. if (memcmp(m0, "response", 8) == 0) {
  92. DEBUG_PRINTF("Response record\n");
  93. state = STATE_RESPONSE_RECORD;
  94. } else {
  95. DEBUG_PRINTF("Other record\n");
  96. state = STATE_OTHER_RECORD;
  97. }
  98. m0 = memmem(bufp, n, "\r\n\r\n", 4);
  99. if (!m0) {
  100. fprintf(stderr, "Error: end of headers not found\n");
  101. return 1;
  102. }
  103. m0 += 4;
  104. DEBUG_PRINTF("Record body begins at %p (offset %zu)\n", (void*)m0, m0 - bufp);
  105. DEBUG_PRINTF("Adjusting buffer pointer and n by %zu\n", m0 - bufp);
  106. n = n - (m0 - bufp);
  107. bufp = m0;
  108. record_bytes_read = 0;
  109. goto checkstate;
  110. } else {
  111. fprintf(stderr, "Error: expected header line, got something else\n");
  112. return 1;
  113. }
  114. } else if (state == STATE_RESPONSE_RECORD || state == STATE_OTHER_RECORD) {
  115. if (record_length + 4 - record_bytes_read > n) {
  116. // Only got part of the record body
  117. DEBUG_PRINTF("Partial record\n");
  118. if (state == STATE_RESPONSE_RECORD) {
  119. DEBUG_PRINTF("Copying %zu bytes to stdout\n", n);
  120. fwrite(bufp, 1, n, stdout);
  121. }
  122. record_bytes_read += n;
  123. DEBUG_PRINTF("%zu of %zu bytes from this record written\n", record_bytes_read, record_length);
  124. } else {
  125. // Remainder of the record is in the buffer. Same logic as above for small records fitting in the buffer with the headers.
  126. DEBUG_PRINTF("Full record\n");
  127. if (state == STATE_RESPONSE_RECORD) {
  128. DEBUG_PRINTF("Copying %zu bytes to stdout\n", record_length - record_bytes_read);
  129. fwrite(bufp, 1, record_length - record_bytes_read, stdout);
  130. fprintf(stdout, "\n");
  131. }
  132. if (memcmp(bufp + record_length - record_bytes_read, "\r\n\r\n", 4) != 0) {
  133. fprintf(stderr, "Error: end of block not found\n");
  134. return 1;
  135. }
  136. DEBUG_PRINTF("Adjusting buffer pointer and n by %zu\n", record_length + 4 - record_bytes_read);
  137. n = n - (record_length + 4 - record_bytes_read);
  138. bufp = bufp + record_length + 4 - record_bytes_read;
  139. if (n < BUFSIZE) {
  140. DEBUG_PRINTF("Buffer too small (%zu bytes), moving and refilling\n", n);
  141. memmove(buf, bufp, n);
  142. bufp = buf;
  143. n += fread(buf + n, 1, BUFSIZE, stdin);
  144. }
  145. state = STATE_BEFORE_RECORD;
  146. goto checkstate;
  147. }
  148. }
  149. }
  150. if (state != STATE_BEFORE_RECORD) {
  151. fprintf(stderr, "Error: incomplete record at the end of input\n");
  152. return 1;
  153. }
  154. }