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.
 
 
 

165 lines
5.4 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. // 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. while ((n = fread(buf, 1, BUFSIZE, stdin)) > 0) {
  34. bufp = buf;
  35. checkstate:
  36. DEBUG_PRINTF("Have %zu bytes of buffer (at %p)\n", n, (void*)bufp);
  37. DEBUG_PRINTF("Beginning of buffer: ");
  38. for (int i = 0; i < 64; ++i) DEBUG_PRINTF(isprint(*(bufp + i)) ? "%c" : "\\x%02x", *(bufp + i) & 0xFF);
  39. DEBUG_PRINTF("\n");
  40. if (n == 0) {
  41. break;
  42. }
  43. DEBUG_PRINTF("State: %d\n", state);
  44. if (state == STATE_BEFORE_RECORD) {
  45. if (n < 10) {
  46. fprintf(stderr, "Error: too little data before WARC headers\n");
  47. return 1;
  48. }
  49. if (memcmp(bufp, "WARC/1.0\r\n", 10) == 0 || memcmp(bufp, "WARC/1.1\r\n", 10) == 0) {
  50. // Got some headers; find the record type, content length, and end of headers
  51. m0 = memmem(bufp, n, "\r\nContent-Length:", 17);
  52. if (!m0) {
  53. fprintf(stderr, "Error: Content-Length missing\n");
  54. return 1;
  55. }
  56. DEBUG_PRINTF("Found Content-Length header at %p (offset %zu)\n", (void*)(m0 + 2), m0 + 2 - bufp);
  57. m1 = memmem(m0 + 1, n - (m0 + 1 - bufp), "\r\n", 2);
  58. if (!m1) {
  59. fprintf(stderr, "Error: CRLF after Content-Length missing\n");
  60. return 1;
  61. }
  62. m0 += 17;
  63. while (m0 < bufp + n && (*m0 == ' ' || *m0 == '\t')) ++m0;
  64. if (!sscanf(m0, "%zu%n", &record_length, &nscan)) {
  65. fprintf(stderr, "Error: invalid Content-Length\n");
  66. return 1;
  67. }
  68. if (nscan > n - (m0 - bufp)) {
  69. fprintf(stderr, "Error: buffer overread\n");
  70. return 1;
  71. }
  72. m0 += nscan;
  73. while (m0 < bufp + n && (*m0 == ' ' || *m0 == '\t')) ++m0;
  74. if (m0 != m1) {
  75. fprintf(stderr, "Error: invalid Content-Length (noise before EOL)\n");
  76. return 1;
  77. }
  78. DEBUG_PRINTF("Record body length: %zu\n", record_length);
  79. m0 = memmem(bufp, n, "\r\nWARC-Type:", 12);
  80. if (!m0) {
  81. fprintf(stderr, "Error: WARC-Type missing\n");
  82. return 1;
  83. }
  84. DEBUG_PRINTF("Found WARC-Type header at %p (offset %zu)\n", (void*)(m0 + 2), m0 + 2 - bufp);
  85. m1 = memmem(m0 + 1, n - (m0 + 1 - bufp), "\r\n", 2);
  86. if (!m1) {
  87. fprintf(stderr, "Error: CRLF after WARC-Type missing\n");
  88. return 1;
  89. }
  90. m0 += 12;
  91. while (m0 < bufp + n && (*m0 == ' ' || *m0 == '\t')) ++m0;
  92. if (memcmp(m0, "response", 8) == 0) {
  93. DEBUG_PRINTF("Response record\n");
  94. state = STATE_RESPONSE_RECORD;
  95. } else {
  96. DEBUG_PRINTF("Other record\n");
  97. state = STATE_OTHER_RECORD;
  98. }
  99. m0 = memmem(bufp, n, "\r\n\r\n", 4);
  100. if (!m0) {
  101. fprintf(stderr, "Error: end of headers not found\n");
  102. return 1;
  103. }
  104. m0 += 4;
  105. DEBUG_PRINTF("Record body begins at %p (offset %zu)\n", (void*)m0, m0 - bufp);
  106. DEBUG_PRINTF("Adjusting buffer pointer and n by %zu\n", m0 - bufp);
  107. n = n - (m0 - bufp);
  108. bufp = m0;
  109. record_bytes_read = 0;
  110. goto checkstate;
  111. } else {
  112. fprintf(stderr, "Error: expected header line, got something else\n");
  113. return 1;
  114. }
  115. } else if (state == STATE_RESPONSE_RECORD || state == STATE_OTHER_RECORD) {
  116. if (record_length + 4 - record_bytes_read > n) {
  117. // Only got part of the record body
  118. DEBUG_PRINTF("Partial record\n");
  119. if (state == STATE_RESPONSE_RECORD) {
  120. DEBUG_PRINTF("Copying %zu bytes to stdout\n", n);
  121. fwrite(bufp, 1, n, stdout);
  122. }
  123. record_bytes_read += n;
  124. DEBUG_PRINTF("%zu of %zu bytes from this record written\n", record_bytes_read, record_length);
  125. } else {
  126. // Remainder of the record is in the buffer. Same logic as above for small records fitting in the buffer with the headers.
  127. DEBUG_PRINTF("Full record\n");
  128. if (state == STATE_RESPONSE_RECORD) {
  129. DEBUG_PRINTF("Copying %zu bytes to stdout\n", record_length - record_bytes_read);
  130. fwrite(bufp, 1, record_length - record_bytes_read, stdout);
  131. fprintf(stdout, "\n");
  132. }
  133. if (memcmp(bufp + record_length - record_bytes_read, "\r\n\r\n", 4) != 0) {
  134. fprintf(stderr, "Error: end of block not found\n");
  135. return 1;
  136. }
  137. DEBUG_PRINTF("Adjusting buffer pointer and n by %zu\n", record_length + 4 - record_bytes_read);
  138. n = n - (record_length + 4 - record_bytes_read);
  139. bufp = bufp + record_length + 4 - record_bytes_read;
  140. if (n < BUFSIZE) {
  141. DEBUG_PRINTF("Buffer too small (%zu bytes), moving and refilling\n", n);
  142. memmove(buf, bufp, n);
  143. bufp = buf;
  144. n += fread(buf + n, 1, BUFSIZE, stdin);
  145. }
  146. state = STATE_BEFORE_RECORD;
  147. goto checkstate;
  148. }
  149. }
  150. }
  151. if (state != STATE_BEFORE_RECORD) {
  152. fprintf(stderr, "Error: incomplete record at the end of input\n");
  153. return 1;
  154. }
  155. }