w32tex
About: TeX Live provides a comprehensive TeX system including all the major TeX-related programs, macro packages, and fonts that are free software. Windows sources.
  Fossies Dox: w32tex-src.tar.xz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

FoFiBase.cc
Go to the documentation of this file.
1 //========================================================================
2 //
3 // FoFiBase.cc
4 //
5 // Copyright 1999-2003 Glyph & Cog, LLC
6 //
7 //========================================================================
8 
9 //========================================================================
10 //
11 // Modified under the Poppler project - http://poppler.freedesktop.org
12 //
13 // All changes made under the Poppler project to this file are licensed
14 // under GPL version 2 or later
15 //
16 // Copyright (C) 2008 Ed Avis <eda@waniasset.com>
17 // Copyright (C) 2011 Jim Meyering <jim@meyering.net>
18 // Copyright (C) 2016, 2018, 2020 Albert Astals Cid <aacid@kde.org>
19 // Copyright (C) 2019 Christian Persch <chpe@src.gnome.org>
20 // Copyright (C) 2019 LE GARREC Vincent <legarrec.vincent@gmail.com>
21 //
22 // To see a description of the changes please see the Changelog file that
23 // came with your tarball or type make ChangeLog if you are building from git
24 //
25 //========================================================================
26 
27 #include <config.h>
28 
29 #include <cstdio>
30 #include <climits>
31 #include "goo/gfile.h"
32 #include "goo/gmem.h"
33 #include "poppler/Error.h"
34 #include "FoFiBase.h"
35 
36 //------------------------------------------------------------------------
37 // FoFiBase
38 //------------------------------------------------------------------------
39 
40 FoFiBase::FoFiBase(const char *fileA, int lenA, bool freeFileDataA)
41 {
42  file = (const unsigned char *)fileA;
43  len = lenA;
44  freeFileData = freeFileDataA;
45 }
46 
48 {
49  if (freeFileData) {
50  gfree((char *)file);
51  }
52 }
53 
54 char *FoFiBase::readFile(const char *fileName, int *fileLen)
55 {
56  FILE *f;
57  char *buf;
58  int n;
59 
60  if (!(f = openFile(fileName, "rb"))) {
61  error(errIO, -1, "Cannot open '{0:s}'", fileName);
62  return nullptr;
63  }
64  if (fseek(f, 0, SEEK_END) != 0) {
65  error(errIO, -1, "Cannot seek to end of '{0:s}'", fileName);
66  fclose(f);
67  return nullptr;
68  }
69  n = (int)ftell(f);
70  if (n < 0) {
71  error(errIO, -1, "Cannot determine length of '{0:s}'", fileName);
72  fclose(f);
73  return nullptr;
74  }
75  if (fseek(f, 0, SEEK_SET) != 0) {
76  error(errIO, -1, "Cannot seek to start of '{0:s}'", fileName);
77  fclose(f);
78  return nullptr;
79  }
80  buf = (char *)gmalloc(n);
81  if ((int)fread(buf, 1, n, f) != n) {
82  gfree(buf);
83  fclose(f);
84  return nullptr;
85  }
86  fclose(f);
87  *fileLen = n;
88  return buf;
89 }
90 
91 int FoFiBase::getS8(int pos, bool *ok) const
92 {
93  int x;
94 
95  if (pos < 0 || pos >= len) {
96  *ok = false;
97  return 0;
98  }
99  x = file[pos];
100  if (x & 0x80) {
101  x |= ~0xff;
102  }
103  return x;
104 }
105 
106 int FoFiBase::getU8(int pos, bool *ok) const
107 {
108  if (pos < 0 || pos >= len) {
109  *ok = false;
110  return 0;
111  }
112  return file[pos];
113 }
114 
115 int FoFiBase::getS16BE(int pos, bool *ok) const
116 {
117  int x;
118 
119  if (pos < 0 || pos > INT_MAX - 1 || pos + 1 >= len) {
120  *ok = false;
121  return 0;
122  }
123  x = file[pos];
124  x = (x << 8) + file[pos + 1];
125  if (x & 0x8000) {
126  x |= ~0xffff;
127  }
128  return x;
129 }
130 
131 int FoFiBase::getU16BE(int pos, bool *ok) const
132 {
133  int x;
134 
135  if (pos < 0 || pos > INT_MAX - 1 || pos + 1 >= len) {
136  *ok = false;
137  return 0;
138  }
139  x = file[pos];
140  x = (x << 8) + file[pos + 1];
141  return x;
142 }
143 
144 int FoFiBase::getS32BE(int pos, bool *ok) const
145 {
146  int x;
147 
148  if (pos < 0 || pos > INT_MAX - 3 || pos + 3 >= len) {
149  *ok = false;
150  return 0;
151  }
152  x = file[pos];
153  x = (x << 8) + file[pos + 1];
154  x = (x << 8) + file[pos + 2];
155  x = (x << 8) + file[pos + 3];
156  if (x & 0x80000000) {
157  x |= ~0xffffffff;
158  }
159  return x;
160 }
161 
162 unsigned int FoFiBase::getU32BE(int pos, bool *ok) const
163 {
164  unsigned int x;
165 
166  if (pos < 0 || pos > INT_MAX - 3 || pos + 3 >= len) {
167  *ok = false;
168  return 0;
169  }
170  x = file[pos];
171  x = (x << 8) + file[pos + 1];
172  x = (x << 8) + file[pos + 2];
173  x = (x << 8) + file[pos + 3];
174  return x;
175 }
176 
177 unsigned int FoFiBase::getU32LE(int pos, bool *ok) const
178 {
179  unsigned int x;
180 
181  if (pos < 0 || pos > INT_MAX - 3 || pos + 3 >= len) {
182  *ok = false;
183  return 0;
184  }
185  x = file[pos + 3];
186  x = (x << 8) + file[pos + 2];
187  x = (x << 8) + file[pos + 1];
188  x = (x << 8) + file[pos];
189  return x;
190 }
191 
192 unsigned int FoFiBase::getUVarBE(int pos, int size, bool *ok) const
193 {
194  unsigned int x;
195  int i;
196 
197  if (pos < 0 || pos > INT_MAX - size || pos + size > len) {
198  *ok = false;
199  return 0;
200  }
201  x = 0;
202  for (i = 0; i < size; ++i) {
203  x = (x << 8) + file[pos + i];
204  }
205  return x;
206 }
207 
208 bool FoFiBase::checkRegion(int pos, int size) const
209 {
210  return pos >= 0 && size >= 0 && pos < INT_MAX - size && size < INT_MAX - pos && pos + size >= pos && pos + size <= len;
211 }
int getU16BE(int pos, GBool *ok)
Definition: FoFiBase.cc:100
int getS8(int pos, GBool *ok)
Definition: FoFiBase.cc:63
int getS16BE(int pos, GBool *ok)
Definition: FoFiBase.cc:85
Guint getU32BE(int pos, GBool *ok)
Definition: FoFiBase.cc:129
int len
Definition: FoFiBase.h:54
virtual ~FoFiBase()
Definition: FoFiBase.cc:31
GBool checkRegion(int pos, int size)
Definition: FoFiBase.cc:172
Guint getU32LE(int pos, GBool *ok)
Definition: FoFiBase.cc:143
static char * readFile(char *fileName, int *fileLen)
Definition: FoFiBase.cc:37
FoFiBase(char *fileA, int lenA, GBool freeFileDataA)
Definition: FoFiBase.cc:25
Guint getUVarBE(int pos, int size, GBool *ok)
Definition: FoFiBase.cc:157
GBool freeFileData
Definition: FoFiBase.h:55
int getS32BE(int pos, GBool *ok)
Definition: FoFiBase.cc:112
int getU8(int pos, GBool *ok)
Definition: FoFiBase.cc:77
#define n
Definition: t4ht.c:1290
#define gfree(p)
Definition: dt2dv.c:326
#define fread
Definition: xxstdio.h:25
#define fseek
Definition: xxstdio.h:30
#define ftell
Definition: xxstdio.h:31
#define error(a)
Definition: dviinfo.c:48
mpz_t * f
Definition: gen-fib.c:34
void * gmalloc(int size)
Definition: gmem.cc:139
#define SEEK_SET
Definition: jmemansi.c:26
small capitals from c petite p scientific i
Definition: afcover.h:80
#define SEEK_END
Definition: ftzconf.h:251
FILE * openFile(const char *path, const char *mode)
Definition: gfile.cc:612
@ errIO
Definition: Error.h:30
#define buf
#define INT_MAX
Definition: c-minmax.h:53
#define fclose
Definition: debug.h:100
const int * pos
Definition: combiners.h:905
float x
Definition: cordic.py:15
static int size
Definition: ppmlabel.c:24
Definition: filedef.h:30
#define FILE
Definition: t1stdio.h:34
return() int(((double) *(font_tbl[cur_fnt].wtbl+(int)(*(font_tbl[cur_fnt].char_wi+(int)(ch - font_tbl[cur_fnt].char_f)% 256)))/(double)(1L<< 20)) *(double) font_tbl[cur_fnt].scale)
const char * fileName
Definition: ugrep.cpp:52
int fileLen
Definition: ugrep.cpp:53