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