| OLD | NEW |
| 1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | 6 |
| 7 #include <limits.h> | 7 #include <limits.h> |
| 8 | 8 |
| 9 #include "../../../include/fpdfapi/fpdf_module.h" | 9 #include "../../../include/fpdfapi/fpdf_module.h" |
| 10 #include "../../../include/fpdfapi/fpdf_page.h" | 10 #include "../../../include/fpdfapi/fpdf_page.h" |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 } | 57 } |
| 58 private: | 58 private: |
| 59 FX_FLOAT m_Stack[PSENGINE_STACKSIZE]; | 59 FX_FLOAT m_Stack[PSENGINE_STACKSIZE]; |
| 60 int m_StackCount; | 60 int m_StackCount; |
| 61 CPDF_PSProc m_MainProc; | 61 CPDF_PSProc m_MainProc; |
| 62 }; | 62 }; |
| 63 CPDF_PSProc::~CPDF_PSProc() | 63 CPDF_PSProc::~CPDF_PSProc() |
| 64 { | 64 { |
| 65 int size = m_Operators.GetSize(); | 65 int size = m_Operators.GetSize(); |
| 66 for (int i = 0; i < size; i ++) { | 66 for (int i = 0; i < size; i ++) { |
| 67 if (m_Operators[i] == (FX_LPVOID)PSOP_PROC) { | 67 if (m_Operators[i] == (void*)PSOP_PROC) { |
| 68 delete (CPDF_PSProc*)m_Operators[i + 1]; | 68 delete (CPDF_PSProc*)m_Operators[i + 1]; |
| 69 i ++; | 69 i ++; |
| 70 } else if (m_Operators[i] == (FX_LPVOID)PSOP_CONST) { | 70 } else if (m_Operators[i] == (void*)PSOP_CONST) { |
| 71 FX_Free((FX_FLOAT*)m_Operators[i + 1]); | 71 FX_Free((FX_FLOAT*)m_Operators[i + 1]); |
| 72 i ++; | 72 i ++; |
| 73 } | 73 } |
| 74 } | 74 } |
| 75 } | 75 } |
| 76 FX_BOOL CPDF_PSProc::Execute(CPDF_PSEngine* pEngine) | 76 FX_BOOL CPDF_PSProc::Execute(CPDF_PSEngine* pEngine) |
| 77 { | 77 { |
| 78 int size = m_Operators.GetSize(); | 78 int size = m_Operators.GetSize(); |
| 79 for (int i = 0; i < size; i ++) { | 79 for (int i = 0; i < size; i ++) { |
| 80 PDF_PSOP op = (PDF_PSOP)(uintptr_t)m_Operators[i]; | 80 PDF_PSOP op = (PDF_PSOP)(uintptr_t)m_Operators[i]; |
| 81 if (op == PSOP_PROC) { | 81 if (op == PSOP_PROC) { |
| 82 i ++; | 82 i ++; |
| 83 } else if (op == PSOP_CONST) { | 83 } else if (op == PSOP_CONST) { |
| 84 pEngine->Push(*(FX_FLOAT*)m_Operators[i + 1]); | 84 pEngine->Push(*(FX_FLOAT*)m_Operators[i + 1]); |
| 85 i ++; | 85 i ++; |
| 86 } else if (op == PSOP_IF) { | 86 } else if (op == PSOP_IF) { |
| 87 if (i < 2 || m_Operators[i - 2] != (FX_LPVOID)PSOP_PROC) { | 87 if (i < 2 || m_Operators[i - 2] != (void*)PSOP_PROC) { |
| 88 return FALSE; | 88 return FALSE; |
| 89 } | 89 } |
| 90 if ((int)pEngine->Pop()) { | 90 if ((int)pEngine->Pop()) { |
| 91 ((CPDF_PSProc*)m_Operators[i - 1])->Execute(pEngine); | 91 ((CPDF_PSProc*)m_Operators[i - 1])->Execute(pEngine); |
| 92 } | 92 } |
| 93 } else if (op == PSOP_IFELSE) { | 93 } else if (op == PSOP_IFELSE) { |
| 94 if (i < 4 || m_Operators[i - 2] != (FX_LPVOID)PSOP_PROC || | 94 if (i < 4 || m_Operators[i - 2] != (void*)PSOP_PROC || |
| 95 m_Operators[i - 4] != (FX_LPVOID)PSOP_PROC) { | 95 m_Operators[i - 4] != (void*)PSOP_PROC) { |
| 96 return FALSE; | 96 return FALSE; |
| 97 } | 97 } |
| 98 if ((int)pEngine->Pop()) { | 98 if ((int)pEngine->Pop()) { |
| 99 ((CPDF_PSProc*)m_Operators[i - 3])->Execute(pEngine); | 99 ((CPDF_PSProc*)m_Operators[i - 3])->Execute(pEngine); |
| 100 } else { | 100 } else { |
| 101 ((CPDF_PSProc*)m_Operators[i - 1])->Execute(pEngine); | 101 ((CPDF_PSProc*)m_Operators[i - 1])->Execute(pEngine); |
| 102 } | 102 } |
| 103 } else { | 103 } else { |
| 104 pEngine->DoOperator(op); | 104 pEngine->DoOperator(op); |
| 105 } | 105 } |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 140 {"ne", PSOP_NE}, {"gt", PSOP_GT}, {"ge", PSOP_GE}, {"lt", PSOP_LT}, | 140 {"ne", PSOP_NE}, {"gt", PSOP_GT}, {"ge", PSOP_GE}, {"lt", PSOP_LT}, |
| 141 {"le", PSOP_LE}, {"and", PSOP_AND}, {"or", PSOP_OR}, {"xor", PSOP_XOR}, | 141 {"le", PSOP_LE}, {"and", PSOP_AND}, {"or", PSOP_OR}, {"xor", PSOP_XOR}, |
| 142 {"not", PSOP_NOT}, {"bitshift", PSOP_BITSHIFT}, {"true", PSOP_TRUE}, | 142 {"not", PSOP_NOT}, {"bitshift", PSOP_BITSHIFT}, {"true", PSOP_TRUE}, |
| 143 {"false", PSOP_FALSE}, {"if", PSOP_IF}, {"ifelse", PSOP_IFELSE}, | 143 {"false", PSOP_FALSE}, {"if", PSOP_IF}, {"ifelse", PSOP_IFELSE}, |
| 144 {"pop", PSOP_POP}, {"exch", PSOP_EXCH}, {"dup", PSOP_DUP}, | 144 {"pop", PSOP_POP}, {"exch", PSOP_EXCH}, {"dup", PSOP_DUP}, |
| 145 {"copy", PSOP_COPY}, {"index", PSOP_INDEX}, {"roll", PSOP_ROLL}, | 145 {"copy", PSOP_COPY}, {"index", PSOP_INDEX}, {"roll", PSOP_ROLL}, |
| 146 {NULL, PSOP_PROC} | 146 {NULL, PSOP_PROC} |
| 147 }; | 147 }; |
| 148 FX_BOOL CPDF_PSEngine::Parse(const FX_CHAR* string, int size) | 148 FX_BOOL CPDF_PSEngine::Parse(const FX_CHAR* string, int size) |
| 149 { | 149 { |
| 150 CPDF_SimpleParser parser((FX_LPBYTE)string, size); | 150 CPDF_SimpleParser parser((uint8_t*)string, size); |
| 151 CFX_ByteStringC word = parser.GetWord(); | 151 CFX_ByteStringC word = parser.GetWord(); |
| 152 if (word != FX_BSTRC("{")) { | 152 if (word != FX_BSTRC("{")) { |
| 153 return FALSE; | 153 return FALSE; |
| 154 } | 154 } |
| 155 return m_MainProc.Parse(parser); | 155 return m_MainProc.Parse(parser); |
| 156 } | 156 } |
| 157 FX_BOOL CPDF_PSProc::Parse(CPDF_SimpleParser& parser) | 157 FX_BOOL CPDF_PSProc::Parse(CPDF_SimpleParser& parser) |
| 158 { | 158 { |
| 159 while (1) { | 159 while (1) { |
| 160 CFX_ByteStringC word = parser.GetWord(); | 160 CFX_ByteStringC word = parser.GetWord(); |
| 161 if (word.IsEmpty()) { | 161 if (word.IsEmpty()) { |
| 162 return FALSE; | 162 return FALSE; |
| 163 } | 163 } |
| 164 if (word == FX_BSTRC("}")) { | 164 if (word == FX_BSTRC("}")) { |
| 165 return TRUE; | 165 return TRUE; |
| 166 } | 166 } |
| 167 if (word == FX_BSTRC("{")) { | 167 if (word == FX_BSTRC("{")) { |
| 168 CPDF_PSProc* pProc = new CPDF_PSProc; | 168 CPDF_PSProc* pProc = new CPDF_PSProc; |
| 169 m_Operators.Add((FX_LPVOID)PSOP_PROC); | 169 m_Operators.Add((void*)PSOP_PROC); |
| 170 m_Operators.Add(pProc); | 170 m_Operators.Add(pProc); |
| 171 if (!pProc->Parse(parser)) { | 171 if (!pProc->Parse(parser)) { |
| 172 return FALSE; | 172 return FALSE; |
| 173 } | 173 } |
| 174 } else { | 174 } else { |
| 175 int i = 0; | 175 int i = 0; |
| 176 while (_PDF_PSOpNames[i].name) { | 176 while (_PDF_PSOpNames[i].name) { |
| 177 if (word == CFX_ByteStringC(_PDF_PSOpNames[i].name)) { | 177 if (word == CFX_ByteStringC(_PDF_PSOpNames[i].name)) { |
| 178 m_Operators.Add((FX_LPVOID)_PDF_PSOpNames[i].op); | 178 m_Operators.Add((void*)_PDF_PSOpNames[i].op); |
| 179 break; | 179 break; |
| 180 } | 180 } |
| 181 i ++; | 181 i ++; |
| 182 } | 182 } |
| 183 if (_PDF_PSOpNames[i].name == NULL) { | 183 if (_PDF_PSOpNames[i].name == NULL) { |
| 184 FX_FLOAT* pd = FX_Alloc(FX_FLOAT, 1); | 184 FX_FLOAT* pd = FX_Alloc(FX_FLOAT, 1); |
| 185 *pd = FX_atof(word); | 185 *pd = FX_atof(word); |
| 186 m_Operators.Add((FX_LPVOID)PSOP_CONST); | 186 m_Operators.Add((void*)PSOP_CONST); |
| 187 m_Operators.Add(pd); | 187 m_Operators.Add(pd); |
| 188 } | 188 } |
| 189 } | 189 } |
| 190 } | 190 } |
| 191 } | 191 } |
| 192 #define PI 3.1415926535897932384626433832795f | 192 #define PI 3.1415926535897932384626433832795f |
| 193 FX_BOOL CPDF_PSEngine::DoOperator(PDF_PSOP op) | 193 FX_BOOL CPDF_PSEngine::DoOperator(PDF_PSOP op) |
| 194 { | 194 { |
| 195 int i1, i2; | 195 int i1, i2; |
| 196 FX_FLOAT d1, d2; | 196 FX_FLOAT d1, d2; |
| (...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 417 } | 417 } |
| 418 default: | 418 default: |
| 419 break; | 419 break; |
| 420 } | 420 } |
| 421 return TRUE; | 421 return TRUE; |
| 422 } | 422 } |
| 423 static FX_FLOAT PDF_Interpolate(FX_FLOAT x, FX_FLOAT xmin, FX_FLOAT xmax, FX_FLO
AT ymin, FX_FLOAT ymax) | 423 static FX_FLOAT PDF_Interpolate(FX_FLOAT x, FX_FLOAT xmin, FX_FLOAT xmax, FX_FLO
AT ymin, FX_FLOAT ymax) |
| 424 { | 424 { |
| 425 return ((x - xmin) * (ymax - ymin) / (xmax - xmin)) + ymin; | 425 return ((x - xmin) * (ymax - ymin) / (xmax - xmin)) + ymin; |
| 426 } | 426 } |
| 427 static FX_DWORD _GetBits32(FX_LPCBYTE pData, int bitpos, int nbits) | 427 static FX_DWORD _GetBits32(const uint8_t* pData, int bitpos, int nbits) |
| 428 { | 428 { |
| 429 int result = 0; | 429 int result = 0; |
| 430 for (int i = 0; i < nbits; i ++) | 430 for (int i = 0; i < nbits; i ++) |
| 431 if (pData[(bitpos + i) / 8] & (1 << (7 - (bitpos + i) % 8))) { | 431 if (pData[(bitpos + i) / 8] & (1 << (7 - (bitpos + i) % 8))) { |
| 432 result |= 1 << (nbits - i - 1); | 432 result |= 1 << (nbits - i - 1); |
| 433 } | 433 } |
| 434 return result; | 434 return result; |
| 435 } | 435 } |
| 436 typedef struct { | 436 typedef struct { |
| 437 FX_FLOAT encode_max, encode_min; | 437 FX_FLOAT encode_max, encode_min; |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 554 index[i] = m_pEncodeInfo[i].sizes - 1; | 554 index[i] = m_pEncodeInfo[i].sizes - 1; |
| 555 } | 555 } |
| 556 pos += index[i] * blocksize[i]; | 556 pos += index[i] * blocksize[i]; |
| 557 } | 557 } |
| 558 FX_SAFE_INT32 bitpos = pos; | 558 FX_SAFE_INT32 bitpos = pos; |
| 559 bitpos *= m_nBitsPerSample; | 559 bitpos *= m_nBitsPerSample; |
| 560 bitpos *= m_nOutputs; | 560 bitpos *= m_nOutputs; |
| 561 if (!bitpos.IsValid()) { | 561 if (!bitpos.IsValid()) { |
| 562 return FALSE; | 562 return FALSE; |
| 563 } | 563 } |
| 564 FX_LPCBYTE pSampleData = m_pSampleStream->GetData(); | 564 const uint8_t* pSampleData = m_pSampleStream->GetData(); |
| 565 if (pSampleData == NULL) { | 565 if (pSampleData == NULL) { |
| 566 return FALSE; | 566 return FALSE; |
| 567 } | 567 } |
| 568 FX_SAFE_INT32 bitpos1 = m_nOutputs - 1 > 0 ? m_nOutputs - 1 : 0; | 568 FX_SAFE_INT32 bitpos1 = m_nOutputs - 1 > 0 ? m_nOutputs - 1 : 0; |
| 569 bitpos1 *= m_nBitsPerSample; | 569 bitpos1 *= m_nBitsPerSample; |
| 570 bitpos1 += bitpos.ValueOrDie(); | 570 bitpos1 += bitpos.ValueOrDie(); |
| 571 if (!bitpos1.IsValid()) { | 571 if (!bitpos1.IsValid()) { |
| 572 return FALSE; | 572 return FALSE; |
| 573 } | 573 } |
| 574 for (int j = 0; j < m_nOutputs; j ++) { | 574 for (int j = 0; j < m_nOutputs; j ++) { |
| (...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 896 for (int i = 0; i < m_nOutputs; i ++) { | 896 for (int i = 0; i < m_nOutputs; i ++) { |
| 897 if (results[i] < m_pRanges[i * 2]) { | 897 if (results[i] < m_pRanges[i * 2]) { |
| 898 results[i] = m_pRanges[i * 2]; | 898 results[i] = m_pRanges[i * 2]; |
| 899 } else if (results[i] > m_pRanges[i * 2 + 1]) { | 899 } else if (results[i] > m_pRanges[i * 2 + 1]) { |
| 900 results[i] = m_pRanges[i * 2 + 1]; | 900 results[i] = m_pRanges[i * 2 + 1]; |
| 901 } | 901 } |
| 902 } | 902 } |
| 903 } | 903 } |
| 904 return TRUE; | 904 return TRUE; |
| 905 } | 905 } |
| OLD | NEW |