1 // Copyright 2018 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 // giflib doesn't have a Unicode DGifOpenFileName(). Let's make one.
11 //
12 // Author: Yannis Guyon (yguyon@google.com)
13 
14 #ifndef WEBP_EXAMPLES_UNICODE_GIF_H_
15 #define WEBP_EXAMPLES_UNICODE_GIF_H_
16 
17 #include "./unicode.h"
18 #ifdef HAVE_CONFIG_H
19 #include "webp/config.h"  // For WEBP_HAVE_GIF
20 #endif
21 
22 #if defined(WEBP_HAVE_GIF)
23 
24 #ifdef _WIN32
25 #include <fcntl.h>  // Not standard, needed for _topen and flags.
26 #include <io.h>
27 #endif
28 
29 #include <gif_lib.h>
30 #include <string.h>
31 #include "./gifdec.h"
32 
33 #if !defined(STDIN_FILENO)
34 #define STDIN_FILENO 0
35 #endif
36 
DGifOpenFileUnicode(const W_CHAR * file_name,int * error)37 static GifFileType* DGifOpenFileUnicode(const W_CHAR* file_name, int* error) {
38   if (!WSTRCMP(file_name, "-")) {
39 #if LOCAL_GIF_PREREQ(5, 0)
40     return DGifOpenFileHandle(STDIN_FILENO, error);
41 #else
42     (void)error;
43     return DGifOpenFileHandle(STDIN_FILENO);
44 #endif
45   }
46 
47 #if defined(_WIN32) && defined(_UNICODE)
48 
49   int file_handle = _wopen(file_name, _O_RDONLY | _O_BINARY);
50   if (file_handle == -1) {
51     if (error != NULL) *error = D_GIF_ERR_OPEN_FAILED;
52     return NULL;
53   }
54 
55 #if LOCAL_GIF_PREREQ(5, 0)
56   return DGifOpenFileHandle(file_handle, error);
57 #else
58   return DGifOpenFileHandle(file_handle);
59 #endif
60 
61 #else
62 
63 #if LOCAL_GIF_PREREQ(5, 0)
64   return DGifOpenFileName(file_name, error);
65 #else
66   return DGifOpenFileName(file_name);
67 #endif
68 
69 #endif  // defined(_WIN32) && defined(_UNICODE)
70   // DGifCloseFile() is called later.
71 }
72 
73 #endif  // defined(WEBP_HAVE_GIF)
74 
75 #endif  // WEBP_EXAMPLES_UNICODE_GIF_H_
76