blob: f2d497509bcb333804c2ea6c8f2834eb30da65d2 [file] [log] [blame]
James Zern061263a2012-05-11 16:00:57 -07001// Copyright 2012 Google Inc. All Rights Reserved.
2//
James Zernd6406142013-06-06 23:05:58 -07003// Use of this source code is governed by a BSD-style license
4// that can be found in the COPYING file in the root of the source
5// tree. An additional intellectual property rights grant can be found
6// in the file PATENTS. All contributing project authors may
7// be found in the AUTHORS file in the root of the source tree.
James Zern061263a2012-05-11 16:00:57 -07008// -----------------------------------------------------------------------------
9//
10// Utility functions used by the example programs.
11//
12
13#include "./example_util.h"
14#include <stdio.h>
15#include <stdlib.h>
skal2bcad892014-03-12 19:32:16 +010016#include <string.h>
James Zern061263a2012-05-11 16:00:57 -070017
James Zern061263a2012-05-11 16:00:57 -070018// -----------------------------------------------------------------------------
19// File I/O
20
skal2bcad892014-03-12 19:32:16 +010021static const size_t kBlockSize = 16384; // default initial size
22
23int ExUtilReadFromStdin(const uint8_t** data, size_t* data_size) {
24 size_t max_size = 0;
25 size_t size = 0;
26 uint8_t* input = NULL;
27
28 if (data == NULL || data_size == NULL) return 0;
29 *data = NULL;
30 *data_size = 0;
31
32 while (!feof(stdin)) {
33 // We double the buffer size each time and read as much as possible.
34 const size_t extra_size = (max_size == 0) ? kBlockSize : max_size;
35 void* const new_data = realloc(input, max_size + extra_size);
36 if (new_data == NULL) goto Error;
37 input = (uint8_t*)new_data;
38 max_size += extra_size;
39 size += fread(input + size, 1, extra_size, stdin);
40 if (size < max_size) break;
41 }
42 if (ferror(stdin)) goto Error;
43 *data = input;
44 *data_size = size;
45 return 1;
46
47 Error:
48 free(input);
49 fprintf(stderr, "Could not read from stdin\n");
50 return 0;
51}
52
James Zern061263a2012-05-11 16:00:57 -070053int ExUtilReadFile(const char* const file_name,
54 const uint8_t** data, size_t* data_size) {
55 int ok;
56 void* file_data;
57 size_t file_size;
58 FILE* in;
skal2bcad892014-03-12 19:32:16 +010059 const int from_stdin = (file_name == NULL) || !strcmp(file_name, "-");
James Zern061263a2012-05-11 16:00:57 -070060
skal2bcad892014-03-12 19:32:16 +010061 if (from_stdin) return ExUtilReadFromStdin(data, data_size);
62
63 if (data == NULL || data_size == NULL) return 0;
James Zern061263a2012-05-11 16:00:57 -070064 *data = NULL;
65 *data_size = 0;
66
67 in = fopen(file_name, "rb");
68 if (in == NULL) {
69 fprintf(stderr, "cannot open input file '%s'\n", file_name);
70 return 0;
71 }
72 fseek(in, 0, SEEK_END);
73 file_size = ftell(in);
74 fseek(in, 0, SEEK_SET);
75 file_data = malloc(file_size);
76 if (file_data == NULL) return 0;
77 ok = (fread(file_data, file_size, 1, in) == 1);
78 fclose(in);
79
80 if (!ok) {
James Zern14d42af2013-03-20 16:59:35 -070081 fprintf(stderr, "Could not read %d bytes of data from file %s\n",
82 (int)file_size, file_name);
James Zern061263a2012-05-11 16:00:57 -070083 free(file_data);
84 return 0;
85 }
86 *data = (uint8_t*)file_data;
87 *data_size = file_size;
88 return 1;
89}
90
Urvang Joshie9a15a32012-11-07 14:41:15 -080091int ExUtilWriteFile(const char* const file_name,
92 const uint8_t* data, size_t data_size) {
93 int ok;
94 FILE* out;
skal2bcad892014-03-12 19:32:16 +010095 const int to_stdout = (file_name == NULL) || !strcmp(file_name, "-");
Urvang Joshie9a15a32012-11-07 14:41:15 -080096
skal2bcad892014-03-12 19:32:16 +010097 if (data == NULL) {
Urvang Joshie9a15a32012-11-07 14:41:15 -080098 return 0;
99 }
skal2bcad892014-03-12 19:32:16 +0100100 out = to_stdout ? stdout : fopen(file_name, "wb");
Urvang Joshie9a15a32012-11-07 14:41:15 -0800101 if (out == NULL) {
102 fprintf(stderr, "Error! Cannot open output file '%s'\n", file_name);
103 return 0;
104 }
105 ok = (fwrite(data, data_size, 1, out) == 1);
skal2bcad892014-03-12 19:32:16 +0100106 if (out != stdout) fclose(out);
Urvang Joshie9a15a32012-11-07 14:41:15 -0800107 return ok;
108}
109