Cleanup: Do not check pointers before deleting them.
[pdfium.git] / core / src / fxcodec / jbig2 / JBig2_Context.h
index 369dba3..6e47a8a 100644 (file)
-// Copyright 2014 PDFium Authors. All rights reserved.\r
-// Use of this source code is governed by a BSD-style license that can be\r
-// found in the LICENSE file.\r
\r
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com\r
-\r
-#ifndef _JBIG2_CONTEXT_H_\r
-#define _JBIG2_CONTEXT_H_\r
-#include "JBig2_Module.h"\r
-#include "JBig2_List.h"\r
-#include "JBig2_Segment.h"\r
-#include "JBig2_Page.h"\r
-#include "JBig2_GeneralDecoder.h"\r
-#include "../../../include/fxcodec/fx_codec_def.h"\r
-#include "../../../include/fxcrt/fx_basic.h"\r
-typedef enum {\r
-    JBIG2_OUT_OF_PAGE = 0,\r
-    JBIG2_IN_PAGE,\r
-} JBig2State;\r
-#define JBIG2_SUCCESS                   0\r
-#define JBIG2_FAILED                   -1\r
-#define JBIG2_ERROR_TOO_SHORT  -2\r
-#define JBIG2_ERROR_FETAL              -3\r
-#define JBIG2_END_OF_PAGE               2\r
-#define JBIG2_END_OF_FILE               3\r
-#define JBIG2_ERROR_FILE_FORMAT -4\r
-#define JBIG2_ERROR_STREAM_TYPE -5\r
-#define JBIG2_ERROR_LIMIT              -6\r
-#define JBIG2_FILE_STREAM                      0\r
-#define JBIG2_SQUENTIAL_STREAM         1\r
-#define JBIG2_RANDOM_STREAM                    2\r
-#define JBIG2_EMBED_STREAM                     3\r
-#define JBIG2_MIN_SEGMENT_SIZE                                 11\r
-class CJBig2_Context : public CJBig2_Object\r
-{\r
-public:\r
-\r
-    static CJBig2_Context *CreateContext(CJBig2_Module *pModule, FX_BYTE *pGlobalData, FX_DWORD dwGlobalLength,\r
-                                         FX_BYTE *pData, FX_DWORD dwLength, FX_INT32 nStreamType, IFX_Pause* pPause = NULL);\r
-\r
-    static void DestroyContext(CJBig2_Context *pContext);\r
-\r
-    FX_INT32 getFirstPage(FX_BYTE *pBuf, FX_INT32 width, FX_INT32 height, FX_INT32 stride, IFX_Pause* pPause);\r
-\r
-    FX_INT32 getNextPage(FX_BYTE *pBuf, FX_INT32 width, FX_INT32 height, FX_INT32 stride, IFX_Pause* pPause);\r
-\r
-    FX_INT32 getFirstPage(CJBig2_Image **image, IFX_Pause* pPause);\r
-\r
-    FX_INT32 getNextPage(CJBig2_Image **image, IFX_Pause* pPause);\r
-    FX_INT32 Continue(IFX_Pause* pPause);\r
-    FXCODEC_STATUS GetProcessiveStatus()\r
-    {\r
-        return m_ProcessiveStatus;\r
-    };\r
-private:\r
-\r
-    CJBig2_Context(FX_BYTE *pGlobalData, FX_DWORD dwGlobalLength,\r
-                   FX_BYTE *pData, FX_DWORD dwLength, FX_INT32 nStreamType, IFX_Pause* pPause);\r
-\r
-    ~CJBig2_Context();\r
-\r
-    FX_INT32 decodeFile(IFX_Pause* pPause);\r
-\r
-    FX_INT32 decode_SquentialOrgnazation(IFX_Pause* pPause);\r
-\r
-    FX_INT32 decode_EmbedOrgnazation(IFX_Pause* pPause);\r
-\r
-    FX_INT32 decode_RandomOrgnazation_FirstPage(IFX_Pause* pPause);\r
-\r
-    FX_INT32 decode_RandomOrgnazation(IFX_Pause* pPause);\r
-\r
-    CJBig2_Segment *findSegmentByNumber(FX_DWORD dwNumber);\r
-\r
-    CJBig2_Segment *findReferredSegmentByTypeAndIndex(CJBig2_Segment *pSegment, FX_BYTE cType, FX_INT32 nIndex);\r
-\r
-    FX_INT32 parseSegmentHeader(CJBig2_Segment *pSegment);\r
-\r
-    FX_INT32 parseSegmentData(CJBig2_Segment *pSegment, IFX_Pause* pPause);\r
-    FX_INT32 ProcessiveParseSegmentData(CJBig2_Segment *pSegment, IFX_Pause* pPause);\r
-\r
-    FX_INT32 parseSymbolDict(CJBig2_Segment *pSegment, IFX_Pause* pPause);\r
-\r
-    FX_INT32 parseTextRegion(CJBig2_Segment *pSegment);\r
-\r
-    FX_INT32 parsePatternDict(CJBig2_Segment *pSegment, IFX_Pause* pPause);\r
-\r
-    FX_INT32 parseHalftoneRegion(CJBig2_Segment *pSegment, IFX_Pause* pPause);\r
-\r
-    FX_INT32 parseGenericRegion(CJBig2_Segment *pSegment, IFX_Pause* pPause);\r
-\r
-    FX_INT32 parseGenericRefinementRegion(CJBig2_Segment *pSegment);\r
-\r
-    FX_INT32 parseTable(CJBig2_Segment *pSegment);\r
-\r
-    FX_INT32 parseRegionInfo(JBig2RegionInfo *pRI);\r
-\r
-\r
-\r
-    JBig2HuffmanCode *decodeSymbolIDHuffmanTable(CJBig2_BitStream *pStream, FX_DWORD SBNUMSYMS);\r
-\r
-    void huffman_assign_code(int* CODES, int* PREFLEN, int NTEMP);\r
-\r
-    void huffman_assign_code(JBig2HuffmanCode *SBSYMCODES, int NTEMP);\r
-\r
-private:\r
-\r
-    CJBig2_Context *m_pGlobalContext;\r
-\r
-    FX_INT32 m_nStreamType;\r
-\r
-    CJBig2_BitStream *m_pStream;\r
-\r
-    FX_INT32 m_nState;\r
-\r
-    CJBig2_List<CJBig2_Segment> *m_pSegmentList;\r
-\r
-    CJBig2_List<JBig2PageInfo> *m_pPageInfoList;\r
-\r
-    CJBig2_Image *m_pPage;\r
-\r
-    FX_BOOL m_bBufSpecified;\r
-\r
-    FX_INT32 m_nSegmentDecoded;\r
-    IFX_Pause* m_pPause;\r
-    FX_INT32   m_PauseStep;\r
-    FXCODEC_STATUS m_ProcessiveStatus;\r
-    FX_BOOL    m_bFirstPage;\r
-    CJBig2_ArithDecoder *m_pArithDecoder;\r
-    CJBig2_GRDProc *m_pGRD;\r
-    JBig2ArithCtx *m_gbContext;\r
-    CJBig2_Segment *m_pSegment;\r
-    FX_DWORD m_dwOffset;\r
-    JBig2RegionInfo m_ri;\r
-};\r
-#endif\r
+// Copyright 2014 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
+
+#ifndef _JBIG2_CONTEXT_H_
+#define _JBIG2_CONTEXT_H_
+
+#include <list>
+#include <utility>
+
+#include "JBig2_Module.h"
+#include "JBig2_List.h"
+#include "JBig2_Segment.h"
+#include "JBig2_Page.h"
+#include "JBig2_GeneralDecoder.h"
+#include "../../../include/fxcodec/fx_codec_def.h"
+
+typedef std::pair<uint8_t*, CJBig2_SymbolDict*> CJBig2_CachePair;
+typedef enum {
+    JBIG2_OUT_OF_PAGE = 0,
+    JBIG2_IN_PAGE,
+} JBig2State;
+#define JBIG2_SUCCESS                   0
+#define JBIG2_FAILED                   -1
+#define JBIG2_ERROR_TOO_SHORT  -2
+#define JBIG2_ERROR_FETAL              -3
+#define JBIG2_END_OF_PAGE               2
+#define JBIG2_END_OF_FILE               3
+#define JBIG2_ERROR_FILE_FORMAT -4
+#define JBIG2_ERROR_STREAM_TYPE -5
+#define JBIG2_ERROR_LIMIT              -6
+#define JBIG2_FILE_STREAM                      0
+#define JBIG2_SQUENTIAL_STREAM         1
+#define JBIG2_RANDOM_STREAM                    2
+#define JBIG2_EMBED_STREAM                     3
+#define JBIG2_MIN_SEGMENT_SIZE                                 11
+class CJBig2_Context : public CJBig2_Object
+{
+public:
+
+    static CJBig2_Context *CreateContext(CJBig2_Module *pModule, uint8_t *pGlobalData, FX_DWORD dwGlobalLength,
+                                         uint8_t *pData, FX_DWORD dwLength, int32_t nStreamType, std::list<CJBig2_CachePair>* pSymbolDictCache, IFX_Pause* pPause = NULL);
+
+    static void DestroyContext(CJBig2_Context *pContext);
+
+    int32_t getFirstPage(uint8_t *pBuf, int32_t width, int32_t height, int32_t stride, IFX_Pause* pPause);
+
+    int32_t getNextPage(uint8_t *pBuf, int32_t width, int32_t height, int32_t stride, IFX_Pause* pPause);
+
+    int32_t getFirstPage(CJBig2_Image **image, IFX_Pause* pPause);
+
+    int32_t getNextPage(CJBig2_Image **image, IFX_Pause* pPause);
+    int32_t Continue(IFX_Pause* pPause);
+    FXCODEC_STATUS GetProcessiveStatus()
+    {
+        return m_ProcessiveStatus;
+    };
+private:
+
+    CJBig2_Context(uint8_t *pGlobalData, FX_DWORD dwGlobalLength,
+                   uint8_t *pData, FX_DWORD dwLength, int32_t nStreamType, std::list<CJBig2_CachePair>* pSymbolDictCache, IFX_Pause* pPause);
+
+    ~CJBig2_Context();
+
+    int32_t decodeFile(IFX_Pause* pPause);
+
+    int32_t decode_SquentialOrgnazation(IFX_Pause* pPause);
+
+    int32_t decode_EmbedOrgnazation(IFX_Pause* pPause);
+
+    int32_t decode_RandomOrgnazation_FirstPage(IFX_Pause* pPause);
+
+    int32_t decode_RandomOrgnazation(IFX_Pause* pPause);
+
+    CJBig2_Segment *findSegmentByNumber(FX_DWORD dwNumber);
+
+    CJBig2_Segment *findReferredSegmentByTypeAndIndex(CJBig2_Segment *pSegment, uint8_t cType, int32_t nIndex);
+
+    int32_t parseSegmentHeader(CJBig2_Segment *pSegment);
+
+    int32_t parseSegmentData(CJBig2_Segment *pSegment, IFX_Pause* pPause);
+    int32_t ProcessiveParseSegmentData(CJBig2_Segment *pSegment, IFX_Pause* pPause);
+
+    int32_t parseSymbolDict(CJBig2_Segment *pSegment, IFX_Pause* pPause);
+
+    int32_t parseTextRegion(CJBig2_Segment *pSegment);
+
+    int32_t parsePatternDict(CJBig2_Segment *pSegment, IFX_Pause* pPause);
+
+    int32_t parseHalftoneRegion(CJBig2_Segment *pSegment, IFX_Pause* pPause);
+
+    int32_t parseGenericRegion(CJBig2_Segment *pSegment, IFX_Pause* pPause);
+
+    int32_t parseGenericRefinementRegion(CJBig2_Segment *pSegment);
+
+    int32_t parseTable(CJBig2_Segment *pSegment);
+
+    int32_t parseRegionInfo(JBig2RegionInfo *pRI);
+
+
+
+    JBig2HuffmanCode *decodeSymbolIDHuffmanTable(CJBig2_BitStream *pStream, FX_DWORD SBNUMSYMS);
+
+    void huffman_assign_code(int* CODES, int* PREFLEN, int NTEMP);
+
+    void huffman_assign_code(JBig2HuffmanCode *SBSYMCODES, int NTEMP);
+
+private:
+
+    CJBig2_Context *m_pGlobalContext;
+
+    int32_t m_nStreamType;
+
+    CJBig2_BitStream *m_pStream;
+
+    int32_t m_nState;
+
+    CJBig2_List<CJBig2_Segment> *m_pSegmentList;
+
+    CJBig2_List<JBig2PageInfo> *m_pPageInfoList;
+
+    CJBig2_Image *m_pPage;
+
+    FX_BOOL m_bBufSpecified;
+
+    int32_t m_nSegmentDecoded;
+    IFX_Pause* m_pPause;
+    int32_t    m_PauseStep;
+    FXCODEC_STATUS m_ProcessiveStatus;
+    FX_BOOL    m_bFirstPage;
+    CJBig2_ArithDecoder *m_pArithDecoder;
+    CJBig2_GRDProc *m_pGRD;
+    JBig2ArithCtx *m_gbContext;
+    CJBig2_Segment *m_pSegment;
+    FX_DWORD m_dwOffset;
+    JBig2RegionInfo m_ri;
+    std::list<CJBig2_CachePair>* m_pSymbolDictCache;
+};
+#endif