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.
 
 
 

160 lines
5.1 KiB

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