1 // Copyright 2016 Google Inc. All Rights Reserved.
2 //
3 // 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.
8 // -----------------------------------------------------------------------------
9 //
10 //  Utility functions used by the image decoders.
11 //
12 
13 #include "./imageio_util.h"
14 
15 #if defined(_WIN32)
16 #include <fcntl.h>   // for _O_BINARY
17 #include <io.h>      // for _setmode()
18 #endif
19 #include <stdlib.h>
20 #include <string.h>
21 #include "../examples/unicode.h"
22 
23 // -----------------------------------------------------------------------------
24 // File I/O
25 
ImgIoUtilSetBinaryMode(FILE * file)26 FILE* ImgIoUtilSetBinaryMode(FILE* file) {
27 #if defined(_WIN32)
28   if (_setmode(_fileno(file), _O_BINARY) == -1) {
29     fprintf(stderr, "Failed to reopen file in O_BINARY mode.\n");
30     return NULL;
31   }
32 #endif
33   return file;
34 }
35 
ImgIoUtilReadFromStdin(const uint8_t ** data,size_t * data_size)36 int ImgIoUtilReadFromStdin(const uint8_t** data, size_t* data_size) {
37   static const size_t kBlockSize = 16384;  // default initial size
38   size_t max_size = 0;
39   size_t size = 0;
40   uint8_t* input = NULL;
41 
42   if (data == NULL || data_size == NULL) return 0;
43   *data = NULL;
44   *data_size = 0;
45 
46   if (!ImgIoUtilSetBinaryMode(stdin)) return 0;
47 
48   while (!feof(stdin)) {
49     // We double the buffer size each time and read as much as possible.
50     const size_t extra_size = (max_size == 0) ? kBlockSize : max_size;
51     // we allocate one extra byte for the \0 terminator
52     void* const new_data = realloc(input, max_size + extra_size + 1);
53     if (new_data == NULL) goto Error;
54     input = (uint8_t*)new_data;
55     max_size += extra_size;
56     size += fread(input + size, 1, extra_size, stdin);
57     if (size < max_size) break;
58   }
59   if (ferror(stdin)) goto Error;
60   if (input != NULL) input[size] = '\0';  // convenient 0-terminator
61   *data = input;
62   *data_size = size;
63   return 1;
64 
65  Error:
66   free(input);
67   fprintf(stderr, "Could not read from stdin\n");
68   return 0;
69 }
70 
ImgIoUtilReadFile(const char * const file_name,const uint8_t ** data,size_t * data_size)71 int ImgIoUtilReadFile(const char* const file_name,
72                       const uint8_t** data, size_t* data_size) {
73   int ok;
74   uint8_t* file_data;
75   size_t file_size;
76   FILE* in;
77   const int from_stdin = (file_name == NULL) || !WSTRCMP(file_name, "-");
78 
79   if (from_stdin) return ImgIoUtilReadFromStdin(data, data_size);
80 
81   if (data == NULL || data_size == NULL) return 0;
82   *data = NULL;
83   *data_size = 0;
84 
85   in = WFOPEN(file_name, "rb");
86   if (in == NULL) {
87     WFPRINTF(stderr, "cannot open input file '%s'\n", (const W_CHAR*)file_name);
88     return 0;
89   }
90   fseek(in, 0, SEEK_END);
91   file_size = ftell(in);
92   fseek(in, 0, SEEK_SET);
93   // we allocate one extra byte for the \0 terminator
94   file_data = (uint8_t*)WebPMalloc(file_size + 1);
95   if (file_data == NULL) {
96     fclose(in);
97     WFPRINTF(stderr, "memory allocation failure when reading file %s\n",
98              (const W_CHAR*)file_name);
99     return 0;
100   }
101   ok = (fread(file_data, file_size, 1, in) == 1);
102   fclose(in);
103 
104   if (!ok) {
105     WFPRINTF(stderr, "Could not read %d bytes of data from file %s\n",
106              (int)file_size, (const W_CHAR*)file_name);
107     WebPFree(file_data);
108     return 0;
109   }
110   file_data[file_size] = '\0';  // convenient 0-terminator
111   *data = file_data;
112   *data_size = file_size;
113   return 1;
114 }
115 
116 // -----------------------------------------------------------------------------
117 
ImgIoUtilWriteFile(const char * const file_name,const uint8_t * data,size_t data_size)118 int ImgIoUtilWriteFile(const char* const file_name,
119                        const uint8_t* data, size_t data_size) {
120   int ok;
121   FILE* out;
122   const int to_stdout = (file_name == NULL) || !WSTRCMP(file_name, "-");
123 
124   if (data == NULL) {
125     return 0;
126   }
127   out = to_stdout ? ImgIoUtilSetBinaryMode(stdout) : WFOPEN(file_name, "wb");
128   if (out == NULL) {
129     WFPRINTF(stderr, "Error! Cannot open output file '%s'\n",
130              (const W_CHAR*)file_name);
131     return 0;
132   }
133   ok = (fwrite(data, data_size, 1, out) == 1);
134   if (out != stdout) fclose(out);
135   return ok;
136 }
137 
138 // -----------------------------------------------------------------------------
139 
ImgIoUtilCopyPlane(const uint8_t * src,int src_stride,uint8_t * dst,int dst_stride,int width,int height)140 void ImgIoUtilCopyPlane(const uint8_t* src, int src_stride,
141                         uint8_t* dst, int dst_stride, int width, int height) {
142   while (height-- > 0) {
143     memcpy(dst, src, width * sizeof(*dst));
144     src += src_stride;
145     dst += dst_stride;
146   }
147 }
148 
149 // -----------------------------------------------------------------------------
150 
ImgIoUtilCheckSizeArgumentsOverflow(uint64_t nmemb,size_t size)151 int ImgIoUtilCheckSizeArgumentsOverflow(uint64_t nmemb, size_t size) {
152   const uint64_t total_size = nmemb * size;
153   int ok = (total_size == (size_t)total_size);
154 #if defined(WEBP_MAX_IMAGE_SIZE)
155   ok = ok && (total_size <= (uint64_t)WEBP_MAX_IMAGE_SIZE);
156 #endif
157   return ok;
158 }
159 
160 // -----------------------------------------------------------------------------
161