Coverage Report

Created: 2025-06-09 08:44

/src/gdal/frmts/grib/degrib/g2clib/pngunpack.c
Line
Count
Source (jump to first uncovered line)
1
#include <stdio.h>
2
#include <stdlib.h>
3
#include <limits.h>
4
#include "grib2.h"
5
6
g2int pngunpack(unsigned char *cpack,g2int len,g2int *idrstmpl,g2int ndpts,
7
                g2float *fld)
8
//$$$  SUBPROGRAM DOCUMENTATION BLOCK
9
//                .      .    .                                       .
10
// SUBPROGRAM:    pngunpack
11
//   PRGMMR: Gilbert          ORG: W/NP11    DATE: 2003-08-27
12
//
13
// ABSTRACT: This subroutine unpacks a data field that was packed into a
14
//   PNG image format
15
//   using info from the GRIB2 Data Representation Template 5.41 or 5.40010.
16
//
17
// PROGRAM HISTORY LOG:
18
// 2003-08-27  Gilbert
19
//
20
// USAGE:    pngunpack(unsigned char *cpack,g2int len,g2int *idrstmpl,g2int ndpts,
21
//                     g2float *fld)
22
//   INPUT ARGUMENT LIST:
23
//     cpack    - The packed data field (character*1 array)
24
//     len      - length of packed field cpack().
25
//     idrstmpl - Pointer to array of values for Data Representation
26
//                Template 5.41 or 5.40010
27
//     ndpts    - The number of data values to unpack
28
//
29
//   OUTPUT ARGUMENT LIST:
30
//     fld[]    - Contains the unpacked data values
31
//
32
// REMARKS: None
33
//
34
// ATTRIBUTES:
35
//   LANGUAGE: C
36
//   MACHINE:  IBM SP
37
//
38
//$$$
39
62
{
40
41
62
      g2int  *ifld;
42
62
      g2int  j,nbits,iret = 0,width,height;
43
62
      g2float  refD, refV,bscale,dscale, bdscale;
44
62
      unsigned char *ctemp;
45
46
62
      rdieee(idrstmpl+0,&refV,1);
47
62
      bscale = (g2float)int_power(2.0,idrstmpl[1]);
48
62
      dscale = (g2float)int_power(10.0,-idrstmpl[2]);
49
62
      bdscale = bscale * dscale;
50
62
      refD = refV * dscale;
51
52
62
      nbits = idrstmpl[3];
53
//
54
//  if nbits equals 0, we have a constant field where the reference value
55
//  is the data value at each gridpoint
56
//
57
62
      if (nbits != 0) {
58
3
         int nbytes = nbits/8;
59
3
         if( ndpts != 0 && nbytes > INT_MAX / ndpts )
60
0
         {
61
0
             return 1;
62
0
         }
63
3
         ifld=(g2int *)calloc(ndpts,sizeof(g2int));
64
         // Was checked just before
65
         // coverity[integer_overflow,overflow_sink]
66
3
         ctemp=(unsigned char *)calloc((size_t)(ndpts)*nbytes,1);
67
3
         if ( ifld == NULL || ctemp == NULL) {
68
0
            fprintf(stderr, "Could not allocate space in jpcunpack.\n"
69
0
                    "Data field NOT unpacked.\n");
70
0
            free(ifld);
71
0
            free(ctemp);
72
0
            return(1);
73
0
         }
74
3
         iret=(g2int)dec_png(cpack,len,&width,&height,ctemp, ndpts, nbits);
75
3
         gbits(ctemp,ndpts*nbytes,ifld,0,nbits,0,ndpts);
76
13.5M
         for (j=0;j<ndpts;j++) {
77
13.5M
            fld[j] = refD + bdscale*(g2float)(ifld[j]);
78
13.5M
         }
79
3
         free(ctemp);
80
3
         free(ifld);
81
3
      }
82
59
      else {
83
4.83k
         for (j=0;j<ndpts;j++) fld[j]=refD;
84
59
      }
85
86
62
      return(iret);
87
62
}