grbseek.c

来自「麻省理工学院的人工智能工具箱,很珍贵,希望对大家有用!」· C语言 代码 · 共 590 行 · 第 1/2 页

C
590
字号
/*--- READ ENTIRE MSG into GRIB HEADER's Entire_Msg ---*//*** A.3.3.9          READ the entire message into Grib Hdr's Entire_Msg;*                  IF (failed) THEN*                      RETURN Fread error stat=1  !errmsg filled*                  ENDIF*/	  fwa_msg = (char *)gh->entire_msg;          if ((nread=fread (fwa_msg, 1, lMessageSize, *fp)) != lMessageSize) 	    {            DPRINT2 ( "%s:  failed to Fread EntireMsg (sz=%ld)\n",	    func, lMessageSize);	    sprintf(errmsg,            "%s: %s has truncated msg @offs=%ld (got %ld out of %ld bytes)\n",            func, InFile, *offset, nread, lMessageSize);	    status= 1;  /* to get out of Outer loop */	    goto DONE;  /* get out of WHILE */	    }/*--- if see '7777', asssign GH's pointers & len  ---*//** A.3.3.10         IF ('7777' is where expected) THEN*/          if (!strncmp((fwa_msg + lMessageSize - 4),"7777",4))		/*  && fwa_msg[7]==1)  */	  {   	        end_ptr = fwa_msg + lMessageSize;		DPRINT0 ("Found string '7777' where expected\n");		gh->msg_length= lMessageSize;/** A.3.3.10.a.1         STORE loc & len of section 0 into Grib Hdr;*/		gh->ids_ptr= (unsigned char *)fwa_msg;    /* mark sect 0 */		gh->ids_len= 8L;/** A.3.3.10.a.2         STORE loc & len of PDS into Grib Hdr; *                     FUNCTION gbyte   !get 3-byte length*/	        ptr=  fwa_msg + gh->ids_len;		gh->pds_ptr= (unsigned char *)ptr;            /* mark PDS */		iskip= 0; gbyte(ptr ,(unsigned long *)&gh->pds_len,&iskip,24); 		DPRINT0 (" pds length\n");		iskip= 8*7; gbyte(ptr ,(unsigned long*)&gdsbmsflag, &iskip, 8);		DPRINT1 (" (%x hex) Gds/Bms flag\n", gdsbmsflag);/** A.3.3.10.a.3         IF (location of next Section  is out of bound) THEN*                        PRINT message;*                        GOTO drop this msg;*                     ENDIF*/		ptr += gh->pds_len;	        if (ptr > end_ptr) {		    sprintf(errmsg,		    "%s %s:  corrupt PDSlen= %ld, Totlen=%ld, drop msg @%ld;\n"		    , func,InFile,  gh->pds_len, gh->msg_length, *offset);		    gh->pds_len= 0;  		     /* reset */		    goto DROPMSG_N_LOOP;		    }/**                     IF (Debug) FUNCTION hdr_print  !print PDS*/	        DPRINT1 ("gh->pds_len= %ld\n", gh->pds_len);		HDR_PRINT("Grib_Seek's PDS",gh->pds_ptr, gh->pds_len);/** A.3.3.10.a.4         IF (GDS is present) THEN*                        STORE location & len of GDS into Grib Hdr's Gds_Ptr*                        FUNCTION gbyte   !get 3-byte length*                        IF (location of next Section  is out of bound) THEN*                           PRINT message;*                           DROP this msg & try to find another;*                        ENDIF*                        IF (Debug) FUNCTION hdr_print  !print GDS*                     ENDIF*/		bit_set= gdsbmsflag >> 7 & 1;	/* mark GDS if present */		if (bit_set) {		    gh->gds_ptr= (unsigned char *)ptr;		    iskip= 0; gbyte(ptr,(unsigned long*)&gh->gds_len,&iskip,24);		    DPRINT0 (" Gds length\n");		    ptr += gh->gds_len;	/* bump PTR to sect*/	             if (ptr > end_ptr) {		       sprintf(errmsg,     		       "%s %s: corrupt GDSlen= %ld, Totlen=%ld, drop msg @%ld\n"		        , func,InFile,  gh->gds_len, gh->msg_length, *offset);		        gh->gds_len= 0;  		/* reset */		        goto DROPMSG_N_LOOP;     		        }		       DPRINT1 ("gh->gds_len= %ld\n", gh->gds_len);		       HDR_PRINT("Grib_Seek's GDS",gh->gds_ptr,gh->gds_len);		    }/** A.3.3.10.a.5         IF (BMS is present) THEN*                        STORE location & len of BMS into Grib Hdr's Bms_Ptr*                        FUNCTION gbyte   !get 3-byte length*                        IF (location of next Section  is out of bound) THEN*                           PRINT message;*                           DROP this msg & try to find another;*                        ENDIF*                        IF (Debug) FUNCTION hdr_print   !byte dump*                     ENDIF */		bit_set= gdsbmsflag >> 6 & 1;	/* mark BMS if present */		if (bit_set) {		    gh->bms_ptr= (unsigned char *)ptr; 			    iskip= 0; gbyte(ptr,(unsigned long*)&gh->bms_len,&iskip,24); 		    DPRINT0 (" Bms length\n");		    ptr += gh->bms_len;		/* bump PTR to sect */	            if (ptr > end_ptr) {		       sprintf(errmsg,		       "%s %s: corrupt BMSlen= %ld, Totlen=%ld, drop msg @%ld\n"			, func, InFile, gh->bms_len, gh->msg_length, *offset);		        gh->bms_len= 0;  		/* reset */		        goto DROPMSG_N_LOOP;     		        }		     DPRINT1 ("gh->bms_len= %ld\n", gh->bms_len);		     HDR_PRINT ("Grib_Seek's BMS", gh->bms_ptr, 				(gh->bms_len>100? 100: gh->bms_len));		  }/** A.3.3.10.a.6        STORE location and length of BDS into Grib Hdr's Bds_Ptr*                    FUNCTION gbyte !get 3-byte length*                    IF (location of next Section  is out of bound) THEN*                        PRINT message;*                        DROP this msg & try to find another;*                    ENDIF*                    IF (Debug) FUNCTION hdr_print   !byte dump*/		gh->bds_ptr= (unsigned char *)ptr; 	/* mark BDS */		iskip= 0; gbyte(ptr,(unsigned long*)&gh->bds_len,&iskip,24);		DPRINT0 (" Bds length\n");		ptr += gh->bds_len;	        if (ptr > end_ptr) {		    sprintf(errmsg,		    "%s %s:  corrupt BDSlen= %ld, Totlen=%ld, drop msg @%ld\n"		    , func, InFile, gh->gds_len, gh->msg_length, *offset);		    gh->gds_len= 0;  /* reset */		    goto DROPMSG_N_LOOP;		    }		    DPRINT1 ("gh->bds_len= %ld\n", gh->bds_len);	  	    HDR_PRINT ("Grib_Seek's BDS", gh->bds_ptr, 			(gh->bds_len>100? 100: gh->bds_len));/** A.3.3.10.a.7        STORE location & len of EDS into Grib Hdr's Eds_Ptr*/		gh->eds_ptr= (unsigned char *)ptr;  /* mark EDS */		gh->eds_len= 4;   /** A.3.3.10.a.8        SET 'gotone' flag *                    ! Return with Msg in Grib hdr, and good stat*/	    	gotone=1; 	/* to get out of FOR loop */		status=0; 	/* Return with Msg , good stat */	        goto DONE; /** A.3.3.10          ENDIF */	   } /* saw 77s  */	else 	if (Read_Index) {		sprintf(errmsg,		"%s %s:  no 7777 found for msg at %ld, check indexfile\n",		func, InFile, *offset);		}/***          !====================================================*          ! Drop Msg Area:   Only get here if :*          ! - first G found not at Indexfile's offset;*          ! - no RIB after G;*          ! - GRIB string found not at Indexfile's offset;*          ! - no 7777 at expected offset;*          ! - got Corrupted Length; *          !====================================================*/DROPMSG_N_LOOP:	   /* ERRMSG must already be loaded and the corrupted len reset to 0	      so that Display GH won't go out of bound...	    */	   DPRINT2 ("\nDropping %s, cause=> %s\n", InFile, errmsg);/** A.3.3.11         IF (Debug mode) THEN *                     FUNCTION display_gribhdr    !show what got loaded sofar*                  ENDIF*/	  if (gh->msg_length > 0) 	  DISPLAY_GRIBHDR(gh); /* before dropping msg*//**                  ! no message found yet, OR Msg Section lens are corrupted * A.3.3.12         CLEAR out header struct   !data in array is not valid*/         	  gh->msg_length =0;	  gh->ids_ptr=gh->pds_ptr= gh->eds_ptr=0;   	  gh->gds_ptr=gh->bms_ptr=gh->bds_ptr= 0;   	  memset ((void *)gh->entire_msg, '\0', gh->abs_size);/** A.3.3.13         IF (Offset was read from Indexfile)   !quit searching* A.3.3.13.a       THEN*                      PUT Error msg in buffer*                      RETURN with No Error status* A.3.3.13.b       ELSE*                      CLEAR out 'G' in tmp block        !go find next 'G' *                  ENDIF*/	  if (Read_Index) {	      status = 0; 	/* Ret w/ no errors     */	      goto DONE;	/* send CAUSE back in Errmsg */	     }	  else {	     *GG='-';    /* let Memchr find next 'G' in curr. block */	     DPRINT1 ("'GRIB' at location %ld is not a valid message\n",	     bytenum+pos);	     errmsg[0]='\0';  /* clear out buff */	     }/** A.3.3         ENDWHILE */     }  /* WHILE seeing 'G'*/     if (Read_Index) {  /* Catch 3 cases:			 - if no 'G' at all ;			 - if no RIB after G ;			 - if found GRIB but not at expected place			*/	   sprintf(errmsg,	   "%s %s:  No Grib Msg found at IndexFile's offset = %ld; "\	   " Check Index File\n" , func, InFile, *offset);	   status = 0;  /* Return w/no errors  */	   goto DONE;	/* but w/ Warn Msg in buff*/	}/*A.3.4           DEBUG print  !no Sect0 found in this block*/      DPRINT2 ("No Section 0 found between address %ld and %ld\n",       pos, pos+check_limit);/*** A.3       ENDFOR    !Outer Loop,  stay until Status changes*/  }  /* check entire file */DONE:/*** A.4        CLOSE input file;     !get here when found no messages**//*   if (fp) fclose(fp); *//** * A.5        IF (found a msg) THEN*                BUMP caller's Offset to absolute Begining of Msg found;*                DEBUG Print*            ENDIF*/   if (gotone)         {       *offset = (long)(pos+bytenum);  /* bump offset to abs. beg. of Msg */       DPRINT3 ("Exiting %s w/stat=%d, offs=%d, msg in GRIB_HDR\n", 	func, status, *offset);       }   else DPRINT2 ("Exiting %s w/stat=%d, no messages\n", func, status);/** A.6        RETURN with status */   return (status); /*** END OF FUNCTION**/}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?