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