int ImgIoUtilReadFromStdin(const uint8_t** data, size_t* data_size) { static const size_t kBlockSize = 16384; // default initial size size_t max_size = 0; size_t size = 0; uint8_t* input = NULL; if (data == NULL || data_size == NULL) return 0; *data = NULL; *data_size = 0; if (!ImgIoUtilSetBinaryMode(stdin)) return 0; while (!feof(stdin)) { // We double the buffer size each time and read as much as possible. const size_t extra_size = (max_size == 0) ? kBlockSize : max_size; void* const new_data = realloc(input, max_size + extra_size); if (new_data == NULL) goto Error; input = (uint8_t*)new_data; max_size += extra_size; size += fread(input + size, 1, extra_size, stdin); if (size < max_size) break; } if (ferror(stdin)) goto Error; *data = input; *data_size = size; return 1; Error: free(input); fprintf(stderr, "Could not read from stdin\n"); return 0; }
static int WriteData(const char* filename, const WebPData* const webpdata) { int ok = 0; FILE* fout = strcmp(filename, "-") ? fopen(filename, "wb") : ImgIoUtilSetBinaryMode(stdout); if (fout == NULL) { fprintf(stderr, "Error opening output WebP file %s!\n", filename); return 0; } if (fwrite(webpdata->bytes, webpdata->size, 1, fout) != 1) { fprintf(stderr, "Error writing file %s!\n", filename); } else { fprintf(stderr, "Saved file %s (%d bytes)\n", filename, (int)webpdata->size); ok = 1; } if (fout != stdout) fclose(fout); return ok; }