/src/gdal/frmts/grib/degrib/g2clib/g2_unpack2.c
Line | Count | Source (jump to first uncovered line) |
1 | | #include <stdio.h> |
2 | | #include <stdlib.h> |
3 | | #include "grib2.h" |
4 | | |
5 | | 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 | 8.15k | { |
44 | | |
45 | 8.15k | g2int ierr,isecnum = 0; |
46 | 8.15k | g2int lensec = 0,ipos,j; |
47 | | |
48 | 8.15k | ierr=0; |
49 | 8.15k | *lencsec2=0; |
50 | 8.15k | *csec2=0; // NULL |
51 | | |
52 | 8.15k | gbit(cgrib,&lensec,*iofst,32); // Get Length of Section |
53 | 8.15k | *iofst=*iofst+32; |
54 | 8.15k | *lencsec2=lensec-5; |
55 | 8.15k | gbit(cgrib,&isecnum,*iofst,8); // Get Section Number |
56 | 8.15k | *iofst=*iofst+8; |
57 | 8.15k | ipos=(*iofst/8); |
58 | | |
59 | 8.15k | 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 | 0 | } |
65 | | |
66 | 8.15k | if (*lencsec2 == 0) { |
67 | 8.14k | ierr = 0; |
68 | 8.14k | return(ierr); |
69 | 8.14k | } |
70 | | |
71 | 16 | *csec2=(unsigned char *)malloc(*lencsec2+1); |
72 | 16 | if (*csec2 == 0) { |
73 | 0 | ierr=6; |
74 | 0 | *lencsec2=0; |
75 | 0 | return(ierr); |
76 | 0 | } |
77 | | |
78 | | //printf(" SAGIPO %d \n",(int)ipos); |
79 | 208 | for (j=0;j<*lencsec2;j++) { |
80 | 192 | *(*csec2+j)=cgrib[ipos+j]; |
81 | 192 | } |
82 | 16 | *iofst=*iofst+(*lencsec2*8); |
83 | | |
84 | 16 | return(ierr); // End of Section 2 processing |
85 | | |
86 | 16 | } |