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.
 
 
 

195 lines
6.6 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. // Read stdin, decode WARC, dump all response record bodies to stdout.
  20. // One LF is inserted at the end of each response to ensure that a new record always begins on a new line.
  21. // If the --meta option is given, one line is printed before each record consisting of the WARC-Target-URI, a space, the record length in bytes in decimal notation, and a LF.
  22. // Headers must fit into BUFSIZE.
  23. // Does not fully comply with the WARC spec. For example, headers must be capitalised canonically, and continuation lines are unsupported.
  24. char buf[2 * BUFSIZE];
  25. size_t n;
  26. int state = STATE_BEFORE_RECORD;
  27. char* bufp;
  28. char* m0;
  29. char* m1;
  30. size_t record_bytes_read;
  31. size_t record_length;
  32. size_t nscan;
  33. bool meta = false;
  34. if (argc == 2 && strcmp(argv[1], "--meta") == 0) {
  35. meta = true;
  36. }
  37. while ((n = fread(buf, 1, BUFSIZE, stdin)) > 0) {
  38. bufp = buf;
  39. checkstate:
  40. DEBUG_PRINTF("Have %zu bytes of buffer (at %p)\n", n, (void*)bufp);
  41. DEBUG_PRINTF("Beginning of buffer: ");
  42. for (int i = 0; i < 64; ++i) DEBUG_PRINTF(isprint(*(bufp + i)) ? "%c" : "\\x%02x", *(bufp + i) & 0xFF);
  43. DEBUG_PRINTF("\n");
  44. if (n == 0) {
  45. break;
  46. }
  47. DEBUG_PRINTF("State: %d\n", state);
  48. if (state == STATE_BEFORE_RECORD) {
  49. if (n < 10) {
  50. fprintf(stderr, "Error: too little data before WARC headers\n");
  51. return 1;
  52. }
  53. if (memcmp(bufp, "WARC/1.0\r\n", 10) == 0 || memcmp(bufp, "WARC/1.1\r\n", 10) == 0) {
  54. // Got some headers; find the record type, content length, and end of headers
  55. m0 = memmem(bufp, n, "\r\nContent-Length:", 17);
  56. if (!m0) {
  57. fprintf(stderr, "Error: Content-Length missing\n");
  58. return 1;
  59. }
  60. DEBUG_PRINTF("Found Content-Length header at %p (offset %zu)\n", (void*)(m0 + 2), m0 + 2 - bufp);
  61. m1 = memmem(m0 + 1, n - (m0 + 1 - bufp), "\r\n", 2);
  62. if (!m1) {
  63. fprintf(stderr, "Error: CRLF after Content-Length missing\n");
  64. return 1;
  65. }
  66. m0 += 17;
  67. while (m0 < bufp + n && (*m0 == ' ' || *m0 == '\t')) ++m0;
  68. if (!sscanf(m0, "%zu%n", &record_length, &nscan)) {
  69. fprintf(stderr, "Error: invalid Content-Length\n");
  70. return 1;
  71. }
  72. if (nscan > n - (m0 - bufp)) {
  73. fprintf(stderr, "Error: buffer overread\n");
  74. return 1;
  75. }
  76. m0 += nscan;
  77. while (m0 < bufp + n && (*m0 == ' ' || *m0 == '\t')) ++m0;
  78. if (m0 != m1) {
  79. fprintf(stderr, "Error: invalid Content-Length (noise before EOL)\n");
  80. return 1;
  81. }
  82. DEBUG_PRINTF("Record body length: %zu\n", record_length);
  83. m0 = memmem(bufp, n, "\r\nWARC-Type:", 12);
  84. if (!m0) {
  85. fprintf(stderr, "Error: WARC-Type missing\n");
  86. return 1;
  87. }
  88. DEBUG_PRINTF("Found WARC-Type header at %p (offset %zu)\n", (void*)(m0 + 2), m0 + 2 - bufp);
  89. m1 = memmem(m0 + 1, n - (m0 + 1 - bufp), "\r\n", 2);
  90. if (!m1) {
  91. fprintf(stderr, "Error: CRLF after WARC-Type missing\n");
  92. return 1;
  93. }
  94. m0 += 12;
  95. while (m0 < bufp + n && (*m0 == ' ' || *m0 == '\t')) ++m0;
  96. if (memcmp(m0, "response", 8) == 0) {
  97. DEBUG_PRINTF("Response record\n");
  98. state = STATE_RESPONSE_RECORD;
  99. } else {
  100. DEBUG_PRINTF("Other record\n");
  101. state = STATE_OTHER_RECORD;
  102. }
  103. if (meta && state == STATE_RESPONSE_RECORD) {
  104. m0 = memmem(bufp, n, "\r\nWARC-Target-URI:", 18);
  105. if (!m0) {
  106. fprintf(stderr, "Error: WARC-Target-URI missing\n");
  107. return 1;
  108. }
  109. DEBUG_PRINTF("Found WARC-Target-URI header at %p (offset %zu)\n", (void*)(m0 + 2), m0 + 2 - bufp);
  110. m1 = memmem(m0 + 1, n - (m0 + 1 - bufp), "\r\n", 2);
  111. if (!m1) {
  112. fprintf(stderr, "Error: CRLF after WARC-Target-URI missing\n");
  113. return 1;
  114. }
  115. m0 += 18;
  116. while (m0 < m1 && (*m0 == ' ' || *m0 == '\t')) ++m0;
  117. DEBUG_PRINTF("WARC-Target-URI value starts at %p (offset %zu)\n", (void*)m0, m0 - bufp);
  118. --m1;
  119. while (m1 > m0 && (*m1 == ' ' || *m1 == '\t')) --m1;
  120. DEBUG_PRINTF("WARC-Target-URI value ends at %p (offset %zu)\n", (void*)(m1 + 1), m1 + 1 - bufp);
  121. if (m1 <= m0) {
  122. fprintf(stderr, "Error: empty WARC-Target-URI\n");
  123. return 1;
  124. }
  125. fwrite(m0, 1, m1 + 1 - m0, stdout);
  126. fprintf(stdout, " %zu\n", record_length);
  127. }
  128. m0 = memmem(bufp, n, "\r\n\r\n", 4);
  129. if (!m0) {
  130. fprintf(stderr, "Error: end of headers not found\n");
  131. return 1;
  132. }
  133. m0 += 4;
  134. DEBUG_PRINTF("Record body begins at %p (offset %zu)\n", (void*)m0, m0 - bufp);
  135. DEBUG_PRINTF("Adjusting buffer pointer and n by %zu\n", m0 - bufp);
  136. n = n - (m0 - bufp);
  137. bufp = m0;
  138. record_bytes_read = 0;
  139. goto checkstate;
  140. } else {
  141. fprintf(stderr, "Error: expected header line, got something else\n");
  142. return 1;
  143. }
  144. } else if (state == STATE_RESPONSE_RECORD || state == STATE_OTHER_RECORD) {
  145. if (record_length + 4 - record_bytes_read > n) {
  146. // Only got part of the record body
  147. DEBUG_PRINTF("Partial record\n");
  148. if (state == STATE_RESPONSE_RECORD) {
  149. DEBUG_PRINTF("Copying %zu bytes to stdout\n", n);
  150. fwrite(bufp, 1, n, stdout);
  151. }
  152. record_bytes_read += n;
  153. DEBUG_PRINTF("%zu of %zu bytes from this record written\n", record_bytes_read, record_length);
  154. } else {
  155. // Remainder of the record is in the buffer. Same logic as above for small records fitting in the buffer with the headers.
  156. DEBUG_PRINTF("Full record\n");
  157. if (state == STATE_RESPONSE_RECORD) {
  158. DEBUG_PRINTF("Copying %zu bytes to stdout\n", record_length - record_bytes_read);
  159. fwrite(bufp, 1, record_length - record_bytes_read, stdout);
  160. fprintf(stdout, "\n");
  161. }
  162. if (memcmp(bufp + record_length - record_bytes_read, "\r\n\r\n", 4) != 0) {
  163. fprintf(stderr, "Error: end of block not found\n");
  164. return 1;
  165. }
  166. DEBUG_PRINTF("Adjusting buffer pointer and n by %zu\n", record_length + 4 - record_bytes_read);
  167. n = n - (record_length + 4 - record_bytes_read);
  168. bufp = bufp + record_length + 4 - record_bytes_read;
  169. if (n < BUFSIZE) {
  170. DEBUG_PRINTF("Buffer too small (%zu bytes), moving and refilling\n", n);
  171. memmove(buf, bufp, n);
  172. bufp = buf;
  173. n += fread(buf + n, 1, BUFSIZE, stdin);
  174. }
  175. state = STATE_BEFORE_RECORD;
  176. goto checkstate;
  177. }
  178. }
  179. }
  180. if (state != STATE_BEFORE_RECORD) {
  181. fprintf(stderr, "Error: incomplete record at the end of input\n");
  182. return 1;
  183. }
  184. }