PNG (>= 10) and TIFF (2) predictors. false: parameters it cannot apply. */
| 2243 | |
| 2244 | /* PNG (>= 10) and TIFF (2) predictors. false: parameters it cannot apply. */ |
| 2245 | static bool f_predict(pdf_doc_t *d, pdf_val_t *parms, pdf_buf_t *io) { |
| 2246 | if (!parms || parms->kind != PV_DICT) { |
| 2247 | return true; |
| 2248 | } |
| 2249 | pdf_val_t *pv = pdf_dget(d, parms, "Predictor"); |
| 2250 | double pred = 1; |
| 2251 | if (pv) { |
| 2252 | if (pv->kind != PV_NUM) { |
| 2253 | return false; |
| 2254 | } |
| 2255 | pred = pv->num == 0.0 ? 1 : pv->num; |
| 2256 | } |
| 2257 | if (pred == 1) { |
| 2258 | return true; |
| 2259 | } |
| 2260 | static const char *const keys[] = {"Colors", "BitsPerComponent", "Columns"}; |
| 2261 | static const int64_t dflt[] = {1, 8, 1}; |
| 2262 | int64_t prm[3]; |
| 2263 | bool ints = true; |
| 2264 | for (int i = 0; i < 3; i++) { |
| 2265 | pdf_val_t *v = pdf_dget(d, parms, keys[i]); |
| 2266 | if (!v || (v->kind == PV_NUM && v->num == 0.0)) { |
| 2267 | prm[i] = dflt[i]; |
| 2268 | } else if (v->kind != PV_NUM) { |
| 2269 | return false; |
| 2270 | } else { |
| 2271 | ints = ints && v->is_int; |
| 2272 | prm[i] = v->is_int ? v->inum : pdf_d2i(v->num); |
| 2273 | } |
| 2274 | } |
| 2275 | int64_t colors = prm[0]; |
| 2276 | int64_t bpc = prm[1]; |
| 2277 | int64_t cols = prm[2]; |
| 2278 | if (pred < PDF_PNG_PRED && !(pred == PDF_TIFF_PRED && bpc == PDF_BITS_PER_BYTE)) { |
| 2279 | return true; |
| 2280 | } |
| 2281 | if (!ints) { |
| 2282 | return false; |
| 2283 | } |
| 2284 | /* products of hostile parameters: computed in double and bounded */ |
| 2285 | double bits = (double)colors * (double)bpc; |
| 2286 | double rowd = floor((bits * (double)cols + 7) / PDF_BITS_PER_BYTE); |
| 2287 | double bppd = floor(bits / PDF_BITS_PER_BYTE); |
| 2288 | if (rowd > (double)PDF_MAX_STREAM_OUT) { |
| 2289 | return false; |
| 2290 | } |
| 2291 | int64_t rowlen = rowd < -2 ? -2 : (int64_t)rowd; |
| 2292 | int64_t bpp = |
| 2293 | bppd<1 ? 1 : bppd>(double) PDF_MAX_STREAM_OUT ? (int64_t)PDF_MAX_STREAM_OUT : (int64_t)bppd; |
| 2294 | if (pred >= PDF_PNG_PRED) { |
| 2295 | if (rowlen == -1) { |
| 2296 | return false; |
| 2297 | } |
| 2298 | pdf_buf_t out = {0}; |
| 2299 | if (rowlen < 0) { |
| 2300 | buf_free(io); |
| 2301 | *io = out; |
| 2302 | return true; |
no test coverage detected