summaryrefslogtreecommitdiff
path: root/core/src
diff options
context:
space:
mode:
Diffstat (limited to 'core/src')
-rw-r--r--core/src/fxcodec/jbig2/JBig2_BitStream.cpp178
-rw-r--r--core/src/fxcodec/jbig2/JBig2_BitStream.h280
-rw-r--r--core/src/fxcodec/jbig2/JBig2_HuffmanDecoder.cpp3
-rw-r--r--core/src/fxcodec/jbig2/JBig2_HuffmanTable.cpp1
4 files changed, 204 insertions, 258 deletions
diff --git a/core/src/fxcodec/jbig2/JBig2_BitStream.cpp b/core/src/fxcodec/jbig2/JBig2_BitStream.cpp
new file mode 100644
index 0000000000..4a4ed992c7
--- /dev/null
+++ b/core/src/fxcodec/jbig2/JBig2_BitStream.cpp
@@ -0,0 +1,178 @@
+// Copyright 2015 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#include "JBig2_BitStream.h"
+
+#include <algorithm>
+
+CJBig2_BitStream::CJBig2_BitStream(const uint8_t* pBuffer, FX_DWORD dwLength)
+ : m_pBuf(pBuffer), m_dwLength(dwLength), m_dwByteIdx(0), m_dwBitIdx(0) {
+ if (m_dwLength > 256 * 1024 * 1024) {
+ m_dwLength = 0;
+ m_pBuf = nullptr;
+ }
+}
+
+CJBig2_BitStream::~CJBig2_BitStream() {
+}
+
+int32_t CJBig2_BitStream::readNBits(FX_DWORD dwBits, FX_DWORD* dwResult) {
+ FX_DWORD dwBitPos = getBitPos();
+ if (dwBitPos > LengthInBits())
+ return -1;
+
+ *dwResult = 0;
+ if (dwBitPos + dwBits <= LengthInBits())
+ dwBitPos = dwBits;
+ else
+ dwBitPos = LengthInBits() - dwBitPos;
+
+ for (; dwBitPos > 0; --dwBitPos) {
+ *dwResult =
+ (*dwResult << 1) | ((m_pBuf[m_dwByteIdx] >> (7 - m_dwBitIdx)) & 0x01);
+ AdvanceBit();
+ }
+ return 0;
+}
+
+int32_t CJBig2_BitStream::readNBits(FX_DWORD dwBits, int32_t* nResult) {
+ FX_DWORD dwBitPos = getBitPos();
+ if (dwBitPos > LengthInBits())
+ return -1;
+
+ *nResult = 0;
+ if (dwBitPos + dwBits <= LengthInBits())
+ dwBitPos = dwBits;
+ else
+ dwBitPos = LengthInBits() - dwBitPos;
+
+ for (; dwBitPos > 0; --dwBitPos) {
+ *nResult =
+ (*nResult << 1) | ((m_pBuf[m_dwByteIdx] >> (7 - m_dwBitIdx)) & 0x01);
+ AdvanceBit();
+ }
+ return 0;
+}
+
+int32_t CJBig2_BitStream::read1Bit(FX_DWORD* dwResult) {
+ if (!IsInBound())
+ return -1;
+
+ *dwResult = (m_pBuf[m_dwByteIdx] >> (7 - m_dwBitIdx)) & 0x01;
+ AdvanceBit();
+ return 0;
+}
+
+int32_t CJBig2_BitStream::read1Bit(FX_BOOL* bResult) {
+ if (!IsInBound())
+ return -1;
+
+ *bResult = (m_pBuf[m_dwByteIdx] >> (7 - m_dwBitIdx)) & 0x01;
+ AdvanceBit();
+ return 0;
+}
+
+int32_t CJBig2_BitStream::read1Byte(uint8_t* cResult) {
+ if (!IsInBound())
+ return -1;
+
+ *cResult = m_pBuf[m_dwByteIdx];
+ ++m_dwByteIdx;
+ return 0;
+}
+
+int32_t CJBig2_BitStream::readInteger(FX_DWORD* dwResult) {
+ if (m_dwByteIdx + 3 >= m_dwLength)
+ return -1;
+
+ *dwResult = (m_pBuf[m_dwByteIdx] << 24) | (m_pBuf[m_dwByteIdx + 1] << 16) |
+ (m_pBuf[m_dwByteIdx + 2] << 8) | m_pBuf[m_dwByteIdx + 3];
+ m_dwByteIdx += 4;
+ return 0;
+}
+
+int32_t CJBig2_BitStream::readShortInteger(FX_WORD* dwResult) {
+ if (m_dwByteIdx + 1 >= m_dwLength)
+ return -1;
+
+ *dwResult = (m_pBuf[m_dwByteIdx] << 8) | m_pBuf[m_dwByteIdx + 1];
+ m_dwByteIdx += 2;
+ return 0;
+}
+
+void CJBig2_BitStream::alignByte() {
+ if (m_dwBitIdx != 0) {
+ ++m_dwByteIdx;
+ m_dwBitIdx = 0;
+ }
+}
+
+uint8_t CJBig2_BitStream::getCurByte() const {
+ return IsInBound() ? m_pBuf[m_dwByteIdx] : 0;
+}
+
+void CJBig2_BitStream::incByteIdx() {
+ if (IsInBound())
+ ++m_dwByteIdx;
+}
+
+uint8_t CJBig2_BitStream::getCurByte_arith() const {
+ return IsInBound() ? m_pBuf[m_dwByteIdx] : 0xFF;
+}
+
+uint8_t CJBig2_BitStream::getNextByte_arith() const {
+ return m_dwByteIdx + 1 < m_dwLength ? m_pBuf[m_dwByteIdx + 1] : 0xFF;
+}
+
+FX_DWORD CJBig2_BitStream::getOffset() const {
+ return m_dwByteIdx;
+}
+
+void CJBig2_BitStream::setOffset(FX_DWORD dwOffset) {
+ m_dwByteIdx = std::min(dwOffset, m_dwLength);
+}
+
+FX_DWORD CJBig2_BitStream::getBitPos() const {
+ return (m_dwByteIdx << 3) + m_dwBitIdx;
+}
+
+void CJBig2_BitStream::setBitPos(FX_DWORD dwBitPos) {
+ m_dwByteIdx = dwBitPos >> 3;
+ m_dwBitIdx = dwBitPos & 7;
+}
+
+const uint8_t* CJBig2_BitStream::getBuf() const {
+ return m_pBuf;
+}
+
+const uint8_t* CJBig2_BitStream::getPointer() const {
+ return m_pBuf + m_dwByteIdx;
+}
+
+void CJBig2_BitStream::offset(FX_DWORD dwOffset) {
+ m_dwByteIdx += dwOffset;
+}
+
+FX_DWORD CJBig2_BitStream::getByteLeft() const {
+ return m_dwLength - m_dwByteIdx;
+}
+
+void CJBig2_BitStream::AdvanceBit() {
+ if (m_dwBitIdx == 7) {
+ ++m_dwByteIdx;
+ m_dwBitIdx = 0;
+ } else {
+ ++m_dwBitIdx;
+ }
+}
+
+bool CJBig2_BitStream::IsInBound() const {
+ return m_dwByteIdx < m_dwLength;
+}
+
+FX_DWORD CJBig2_BitStream::LengthInBits() const {
+ return m_dwLength << 3;
+}
diff --git a/core/src/fxcodec/jbig2/JBig2_BitStream.h b/core/src/fxcodec/jbig2/JBig2_BitStream.h
index 85c992f4ff..9a3d8b0a18 100644
--- a/core/src/fxcodec/jbig2/JBig2_BitStream.h
+++ b/core/src/fxcodec/jbig2/JBig2_BitStream.h
@@ -4,287 +4,51 @@
// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-#ifndef _JBIG2_BIT_STREAM_H_
-#define _JBIG2_BIT_STREAM_H_
+#ifndef CORE_SRC_FXCODEC_JBIG2_JBIG2_BITSTREAM_H_
+#define CORE_SRC_FXCODEC_JBIG2_JBIG2_BITSTREAM_H_
-#include "JBig2_Define.h"
+#include "../../../include/fxcrt/fx_basic.h"
class CJBig2_BitStream {
public:
CJBig2_BitStream(const uint8_t* pBuffer, FX_DWORD dwLength);
-
- CJBig2_BitStream(CJBig2_BitStream& bs);
-
~CJBig2_BitStream();
+ // TODO(thestig): readFoo() should return bool.
int32_t readNBits(FX_DWORD nBits, FX_DWORD* dwResult);
-
int32_t readNBits(FX_DWORD nBits, int32_t* nResult);
-
int32_t read1Bit(FX_DWORD* dwResult);
-
int32_t read1Bit(FX_BOOL* bResult);
-
int32_t read1Byte(uint8_t* cResult);
-
int32_t readInteger(FX_DWORD* dwResult);
-
int32_t readShortInteger(FX_WORD* wResult);
-
void alignByte();
-
- void align4Byte();
-
- uint8_t getAt(FX_DWORD dwOffset);
-
- uint8_t getCurByte();
-
- uint8_t getNextByte();
-
- int32_t incByteIdx();
-
- uint8_t getCurByte_arith();
-
- uint8_t getNextByte_arith();
-
- FX_DWORD getOffset();
-
+ uint8_t getCurByte() const;
+ void incByteIdx();
+ uint8_t getCurByte_arith() const;
+ uint8_t getNextByte_arith() const;
+ FX_DWORD getOffset() const;
void setOffset(FX_DWORD dwOffset);
-
- FX_DWORD getBitPos();
-
+ FX_DWORD getBitPos() const;
void setBitPos(FX_DWORD dwBitPos);
-
- const uint8_t* getBuf();
-
- FX_DWORD getLength() { return m_dwLength; }
-
- const uint8_t* getPointer();
-
+ const uint8_t* getBuf() const;
+ FX_DWORD getLength() const { return m_dwLength; }
+ const uint8_t* getPointer() const;
void offset(FX_DWORD dwOffset);
-
- FX_DWORD getByteLeft();
+ FX_DWORD getByteLeft() const;
private:
- const uint8_t* m_pBuf;
+ void AdvanceBit();
+ bool IsInBound() const;
+ FX_DWORD LengthInBits() const;
+ const uint8_t* m_pBuf;
FX_DWORD m_dwLength;
-
FX_DWORD m_dwByteIdx;
-
FX_DWORD m_dwBitIdx;
-};
-inline CJBig2_BitStream::CJBig2_BitStream(const uint8_t* pBuffer,
- FX_DWORD dwLength) {
- m_pBuf = pBuffer;
- m_dwLength = dwLength;
- m_dwByteIdx = 0;
- m_dwBitIdx = 0;
- if (m_dwLength > 256 * 1024 * 1024) {
- m_dwLength = 0;
- m_pBuf = NULL;
- }
-}
-inline CJBig2_BitStream::CJBig2_BitStream(CJBig2_BitStream& bs) {
- m_pBuf = bs.m_pBuf;
- m_dwLength = bs.m_dwLength;
- m_dwByteIdx = bs.m_dwByteIdx;
- m_dwBitIdx = bs.m_dwBitIdx;
-}
-inline CJBig2_BitStream::~CJBig2_BitStream() {}
-inline int32_t CJBig2_BitStream::readNBits(FX_DWORD dwBits,
- FX_DWORD* dwResult) {
- FX_DWORD dwTemp = (m_dwByteIdx << 3) + m_dwBitIdx;
- if (dwTemp <= (m_dwLength << 3)) {
- *dwResult = 0;
- if (dwTemp + dwBits <= (m_dwLength << 3)) {
- dwTemp = dwBits;
- } else {
- dwTemp = (m_dwLength << 3) - dwTemp;
- }
- while (dwTemp > 0) {
- *dwResult =
- (*dwResult << 1) | ((m_pBuf[m_dwByteIdx] >> (7 - m_dwBitIdx)) & 0x01);
- if (m_dwBitIdx == 7) {
- m_dwByteIdx++;
- m_dwBitIdx = 0;
- } else {
- m_dwBitIdx++;
- }
- dwTemp--;
- }
- return 0;
- } else {
- return -1;
- }
-}
-inline int32_t CJBig2_BitStream::readNBits(FX_DWORD dwBits, int32_t* nResult) {
- FX_DWORD dwTemp = (m_dwByteIdx << 3) + m_dwBitIdx;
- if (dwTemp <= (m_dwLength << 3)) {
- *nResult = 0;
- if (dwTemp + dwBits <= (m_dwLength << 3)) {
- dwTemp = dwBits;
- } else {
- dwTemp = (m_dwLength << 3) - dwTemp;
- }
- while (dwTemp > 0) {
- *nResult =
- (*nResult << 1) | ((m_pBuf[m_dwByteIdx] >> (7 - m_dwBitIdx)) & 0x01);
- if (m_dwBitIdx == 7) {
- m_dwByteIdx++;
- m_dwBitIdx = 0;
- } else {
- m_dwBitIdx++;
- }
- dwTemp--;
- }
- return 0;
- } else {
- return -1;
- }
-}
-
-inline int32_t CJBig2_BitStream::read1Bit(FX_DWORD* dwResult) {
- if (m_dwByteIdx < m_dwLength) {
- *dwResult = (m_pBuf[m_dwByteIdx] >> (7 - m_dwBitIdx)) & 0x01;
- if (m_dwBitIdx == 7) {
- m_dwByteIdx++;
- m_dwBitIdx = 0;
- } else {
- m_dwBitIdx++;
- }
- return 0;
- } else {
- return -1;
- }
-}
-inline int32_t CJBig2_BitStream::read1Bit(FX_BOOL* bResult) {
- if (m_dwByteIdx < m_dwLength) {
- *bResult = (m_pBuf[m_dwByteIdx] >> (7 - m_dwBitIdx)) & 0x01;
- if (m_dwBitIdx == 7) {
- m_dwByteIdx++;
- m_dwBitIdx = 0;
- } else {
- m_dwBitIdx++;
- }
- return 0;
- } else {
- return -1;
- }
-}
-inline int32_t CJBig2_BitStream::read1Byte(uint8_t* cResult) {
- if (m_dwByteIdx < m_dwLength) {
- *cResult = m_pBuf[m_dwByteIdx];
- m_dwByteIdx++;
- return 0;
- } else {
- return -1;
- }
-}
-
-inline int32_t CJBig2_BitStream::readInteger(FX_DWORD* dwResult) {
- if (m_dwByteIdx + 3 < m_dwLength) {
- *dwResult = (m_pBuf[m_dwByteIdx] << 24) | (m_pBuf[m_dwByteIdx + 1] << 16) |
- (m_pBuf[m_dwByteIdx + 2] << 8) | m_pBuf[m_dwByteIdx + 3];
- m_dwByteIdx += 4;
- return 0;
- } else {
- return -1;
- }
-}
+ CJBig2_BitStream(const CJBig2_BitStream&) = delete;
+ void operator=(const CJBig2_BitStream&) = delete;
+};
-inline int32_t CJBig2_BitStream::readShortInteger(FX_WORD* dwResult) {
- if (m_dwByteIdx + 1 < m_dwLength) {
- *dwResult = (m_pBuf[m_dwByteIdx] << 8) | m_pBuf[m_dwByteIdx + 1];
- m_dwByteIdx += 2;
- return 0;
- } else {
- return -1;
- }
-}
-inline void CJBig2_BitStream::alignByte() {
- if (m_dwBitIdx != 0) {
- m_dwByteIdx++;
- m_dwBitIdx = 0;
- }
-}
-inline void CJBig2_BitStream::align4Byte() {
- if (m_dwBitIdx != 0) {
- m_dwByteIdx++;
- m_dwBitIdx = 0;
- }
- m_dwByteIdx = (m_dwByteIdx + 3) & -4;
-}
-inline uint8_t CJBig2_BitStream::getAt(FX_DWORD dwOffset) {
- if (dwOffset < m_dwLength) {
- return m_pBuf[dwOffset];
- } else {
- return 0;
- }
-}
-inline uint8_t CJBig2_BitStream::getCurByte() {
- if (m_dwByteIdx < m_dwLength) {
- return m_pBuf[m_dwByteIdx];
- } else {
- return 0;
- }
-}
-inline uint8_t CJBig2_BitStream::getNextByte() {
- if (m_dwByteIdx + 1 < m_dwLength) {
- return m_pBuf[m_dwByteIdx + 1];
- } else {
- return 0;
- }
-}
-inline int32_t CJBig2_BitStream::incByteIdx() {
- if (m_dwByteIdx < m_dwLength) {
- m_dwByteIdx++;
- return 0;
- } else {
- return -1;
- }
-}
-inline uint8_t CJBig2_BitStream::getCurByte_arith() {
- if (m_dwByteIdx < m_dwLength) {
- return m_pBuf[m_dwByteIdx];
- } else {
- return 0xff;
- }
-}
-inline uint8_t CJBig2_BitStream::getNextByte_arith() {
- if (m_dwByteIdx + 1 < m_dwLength) {
- return m_pBuf[m_dwByteIdx + 1];
- } else {
- return 0xff;
- }
-}
-inline FX_DWORD CJBig2_BitStream::getOffset() {
- return m_dwByteIdx;
-}
-inline void CJBig2_BitStream::setOffset(FX_DWORD dwOffset) {
- if (dwOffset > m_dwLength) {
- dwOffset = m_dwLength;
- }
- m_dwByteIdx = dwOffset;
-}
-inline FX_DWORD CJBig2_BitStream::getBitPos() {
- return (m_dwByteIdx << 3) + m_dwBitIdx;
-}
-inline void CJBig2_BitStream::setBitPos(FX_DWORD dwBitPos) {
- m_dwByteIdx = dwBitPos >> 3;
- m_dwBitIdx = dwBitPos & 7;
-}
-inline const uint8_t* CJBig2_BitStream::getBuf() {
- return m_pBuf;
-}
-inline const uint8_t* CJBig2_BitStream::getPointer() {
- return m_pBuf + m_dwByteIdx;
-}
-inline void CJBig2_BitStream::offset(FX_DWORD dwOffset) {
- m_dwByteIdx += dwOffset;
-}
-inline FX_DWORD CJBig2_BitStream::getByteLeft() {
- return m_dwLength - m_dwByteIdx;
-}
-#endif
+#endif // CORE_SRC_FXCODEC_JBIG2_JBIG2_BITSTREAM_H_
diff --git a/core/src/fxcodec/jbig2/JBig2_HuffmanDecoder.cpp b/core/src/fxcodec/jbig2/JBig2_HuffmanDecoder.cpp
index d5acc362fd..61a45bebae 100644
--- a/core/src/fxcodec/jbig2/JBig2_HuffmanDecoder.cpp
+++ b/core/src/fxcodec/jbig2/JBig2_HuffmanDecoder.cpp
@@ -5,6 +5,9 @@
// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
#include "JBig2_HuffmanDecoder.h"
+
+#include "JBig2_Define.h"
+
CJBig2_HuffmanDecoder::CJBig2_HuffmanDecoder(CJBig2_BitStream* pStream) {
m_pStream = pStream;
}
diff --git a/core/src/fxcodec/jbig2/JBig2_HuffmanTable.cpp b/core/src/fxcodec/jbig2/JBig2_HuffmanTable.cpp
index 60b5f3ca37..a09d152052 100644
--- a/core/src/fxcodec/jbig2/JBig2_HuffmanTable.cpp
+++ b/core/src/fxcodec/jbig2/JBig2_HuffmanTable.cpp
@@ -12,6 +12,7 @@
#include "../../../include/fxcrt/fx_memory.h"
#include "JBig2_BitStream.h"
+#include "JBig2_Define.h"
CJBig2_HuffmanTable::CJBig2_HuffmanTable(const JBig2TableLine* pTable,
int nLines,