Created
February 12, 2018 18:01
-
-
Save warmwaffles/fa911e6da95d2613241c844bc5b1102a to your computer and use it in GitHub Desktop.
Take a PGCOPY binary in via stdin and output via stdout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdint.h> | |
#include <stdio.h> | |
#include <stdbool.h> | |
#include <arpa/inet.h> | |
typedef struct buffer | |
{ | |
uint32_t length; | |
uint32_t capacity; | |
char* data; | |
} buffer_t; | |
int | |
main(void) | |
{ | |
{ | |
uint8_t skip[19]; | |
if (fread(skip, 19, 1, stdin) != 1) { | |
fprintf(stderr, "failed to read the skip\n"); | |
return EXIT_FAILURE; | |
} | |
} | |
buffer_t buffer; | |
buffer.length = 0; | |
buffer.capacity = 4096; | |
buffer.data = calloc(1, 4096 + 1); | |
while (true) { | |
uint16_t count; | |
uint32_t bytes; | |
if (fread(&count, sizeof(uint16_t), 1, stdin) != 1) { | |
fprintf(stderr, "failed to read tuple's field count\n"); | |
return EXIT_FAILURE; | |
} | |
if (count == 0xffff) { | |
free(buffer.data); | |
return EXIT_SUCCESS; | |
} | |
if (fread(&bytes, sizeof(uint32_t), 1, stdin) != 1) { | |
fprintf(stderr, "failed to read tuple's field bytes\n"); | |
return EXIT_FAILURE; | |
} | |
count = ntohs(count); | |
bytes = ntohl(bytes); | |
if (buffer.capacity < bytes) { | |
free(buffer.data); | |
buffer.data = calloc(1, bytes + 1); | |
buffer.capacity = bytes + 1; | |
} | |
if (fread(buffer.data, 1, bytes, stdin) != bytes) { | |
fprintf(stderr, "failed to read in row data\n"); | |
return EXIT_FAILURE; | |
} | |
buffer.length = bytes; | |
buffer.data[bytes] = '\0'; | |
fprintf(stdout, "%s\n", buffer.data); | |
} | |
free(buffer.data); | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment