LCOV - code coverage report
Current view: top level - frmts/grib/degrib/g2clib - g2_unpack2.c (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 22 29 75.9 %
Date: 2024-05-06 13:02:59 Functions: 1 1 100.0 %

          Line data    Source code
       1             : #include <stdio.h>
       2             : #include <stdlib.h>
       3             : #include "grib2.h"
       4             : 
       5         424 : g2int g2_unpack2(unsigned char *cgrib,g2int *iofst,g2int *lencsec2,unsigned char **csec2)
       6             : ////$$$  SUBPROGRAM DOCUMENTATION BLOCK
       7             : //                .      .    .                                       .
       8             : // SUBPROGRAM:    g2_unpack2
       9             : //   PRGMMR: Gilbert         ORG: W/NP11    DATE: 2002-10-31
      10             : //
      11             : // ABSTRACT: This subroutine unpacks Section 2 (Local Use Section)
      12             : //           as defined in GRIB Edition 2.
      13             : //
      14             : // PROGRAM HISTORY LOG:
      15             : // 2002-10-31  Gilbert
      16             : // 2008-12-23  Wesley   - Initialize lencsec2 Length of Local Use data
      17             : // 2010-08-05  Vuong    - If section 2 has zero length, ierr=0
      18             : //
      19             : // USAGE:    int g2_unpack2(unsigned char *cgrib,g2int *iofst,g2int *lencsec2,
      20             : //                          unsigned char **csec2)
      21             : //   INPUT ARGUMENT LIST:
      22             : //     cgrib    - char array containing Section 2 of the GRIB2 message
      23             : //     iofst    - Bit offset for the beginning of Section 2 in cgrib.
      24             : //
      25             : //   OUTPUT ARGUMENT LIST:
      26             : //     iofst    - Bit offset at the end of Section 2, returned.
      27             : //     lencsec2 - Length (in octets) of Local Use data
      28             : //     csec2    - Pointer to a char array containing local use data
      29             : //
      30             : //   RETURN VALUES:
      31             : //     ierr     - Error return code.
      32             : //                0 = no error
      33             : //                2 = Array passed is not section 2
      34             : //                6 = memory allocation error
      35             : //
      36             : // REMARKS: None
      37             : //
      38             : // ATTRIBUTES:
      39             : //   LANGUAGE: C
      40             : //   MACHINE:
      41             : //
      42             : //$$$//
      43             : {
      44             : 
      45         424 :       g2int ierr,isecnum = 0;
      46         424 :       g2int lensec = 0,ipos,j;
      47             : 
      48         424 :       ierr=0;
      49         424 :       *lencsec2=0;
      50         424 :       *csec2=0;    // NULL
      51             : 
      52         424 :       gbit(cgrib,&lensec,*iofst,32);        // Get Length of Section
      53         424 :       *iofst=*iofst+32;
      54         424 :       *lencsec2=lensec-5;
      55         424 :       gbit(cgrib,&isecnum,*iofst,8);         // Get Section Number
      56         424 :       *iofst=*iofst+8;
      57         424 :       ipos=(*iofst/8);
      58             : 
      59         424 :       if ( isecnum != 2 ) {
      60           0 :          ierr=2;
      61           0 :          *lencsec2=0;
      62           0 :          fprintf(stderr,"g2_unpack2: Not Section 2 data.\n");
      63           0 :          return(ierr);
      64             :       }
      65             : 
      66         424 :       if (*lencsec2 == 0) {
      67         422 :          ierr = 0;
      68         422 :          return(ierr);
      69             :       }
      70             : 
      71           2 :       *csec2=(unsigned char *)malloc(*lencsec2+1);
      72           2 :       if (*csec2 == 0) {
      73           0 :          ierr=6;
      74           0 :          *lencsec2=0;
      75           0 :          return(ierr);
      76             :       }
      77             : 
      78             :       //printf(" SAGIPO %d \n",(int)ipos);
      79          26 :       for (j=0;j<*lencsec2;j++) {
      80          24 :          *(*csec2+j)=cgrib[ipos+j];
      81             :       }
      82           2 :       *iofst=*iofst+(*lencsec2*8);
      83             : 
      84           2 :       return(ierr);    // End of Section 2 processing
      85             : 
      86             : }

Generated by: LCOV version 1.14