|
| 1 | +/* This is a test for the NCEPLIBS-g2c project. This test is for a rare scenario where |
| 2 | + * the last 4 bytes of the packed data section could be all 55 or hex 0x37, which when |
| 3 | + * converted to ASCII, will resemble the GRIB end message terminator of "7777". |
| 4 | + * |
| 5 | + * This test purposely builds a very small GRIB2 message, with data values purposely |
| 6 | + * set so that when using simple packing with no scaling and nbits = 0, will make |
| 7 | + * the last 4 bytes of the packed data mimic the GRIB terminator. |
| 8 | + * |
| 9 | + * Eric Engle 3/17/25 |
| 10 | + */ |
| 11 | + |
| 12 | +#include "grib2_int.h" |
| 13 | +#include <stdio.h> |
| 14 | +#include <stdlib.h> |
| 15 | + |
| 16 | +#define SEC0_LEN 16 |
| 17 | +#define SEC1_LEN 21 |
| 18 | +#define SEC3_LEN 72 |
| 19 | +#define SEC4_LEN 58 |
| 20 | +#define SEC5_LEN 21 |
| 21 | +#define SEC6_LEN 6 |
| 22 | +#define SEC7_LEN 11 |
| 23 | +#define FULL_MSG_LEN 209 |
| 24 | + |
| 25 | +int |
| 26 | +main() |
| 27 | +{ |
| 28 | + printf("Testing rare packing condition and grid end.\n"); |
| 29 | + printf("Testing full message creation (expect and ignore error messages)...\n"); |
| 30 | + { |
| 31 | + unsigned char cgrib[FULL_MSG_LEN]; |
| 32 | + g2int listsec0[2] = {0, 2}; |
| 33 | + g2int listsec1[13] = {7, 0, 2, 1, 1, 2023, 9, 29, 0, 0, 0, 0, 1}; |
| 34 | + g2int igds[5] = {0, 6, 0, 0, 0}; |
| 35 | + g2int igdstmpl[19] = {6, 0, 0, 0, 0, 0, 0, 3, 2, 0, 0, 0, 0, 0, 0, 0, 50000, 50000, 64}; |
| 36 | + g2int ipdsnum = 8; |
| 37 | + g2int ipdstmpl[29] = {1, 8, 2, 0, 250, 0, 0, 1, 0, 1, 0, 0, 255, 0, 0, 2023, 9, 30, 0, 0, 0, 1, 0, 1, 2, 1, 24, 255, 0}; |
| 38 | + float coordlist[1] = {1}; |
| 39 | + g2int numcoord = 0; |
| 40 | + g2int idrsnum = 0; /* Grid Point Data - Simple Packing (see Template 5.0) */ |
| 41 | + g2int idrstmpl[5] = {0, 0, 0, 0, 0}; |
| 42 | + float fld[6] = {0.0, 176.9375, 54.9, 55.1, 54.7, 55.3}; |
| 43 | + g2int ngrdpts = 6; |
| 44 | + g2int ibmap = 255; // No bit map |
| 45 | + g2int bmap[6] = {1, 1, 1, 1, 1, 1}; |
| 46 | + int ret; |
| 47 | + |
| 48 | + /* Initialize empty message to quiet valgrind errors. */ |
| 49 | + for (int i = 0; i < FULL_MSG_LEN; i++) |
| 50 | + cgrib[i] = 0; |
| 51 | + |
| 52 | + /* Create the message, filling in sections 0 and 1. */ |
| 53 | + printf("Calling g2_create()... "); |
| 54 | + ret = g2_create(cgrib, listsec0, listsec1); |
| 55 | + printf("ret = %d\n", ret); |
| 56 | + if (ret != SEC0_LEN + SEC1_LEN) |
| 57 | + return 1; |
| 58 | + |
| 59 | + /* Add section 3. */ |
| 60 | + printf("Calling g2_addgrid()... "); |
| 61 | + ret = g2_addgrid(cgrib, igds, igdstmpl, NULL, 0); |
| 62 | + printf("ret = %d\n", ret); |
| 63 | + if (ret != SEC0_LEN + SEC1_LEN + SEC3_LEN) |
| 64 | + return 1; |
| 65 | + |
| 66 | + /* Add sections 4, 5, 6, and 7. */ |
| 67 | + printf("Calling g2_addfield()... "); |
| 68 | + ret = g2_addfield(cgrib, ipdsnum, ipdstmpl, coordlist, numcoord, |
| 69 | + idrsnum, idrstmpl, fld, ngrdpts, ibmap, bmap); |
| 70 | + printf("ret = %d\n", ret); |
| 71 | + if (ret != SEC0_LEN + SEC1_LEN + SEC3_LEN + SEC4_LEN + SEC5_LEN + SEC6_LEN + SEC7_LEN) |
| 72 | + return 1; |
| 73 | + |
| 74 | + /* Add section 8. */ |
| 75 | + printf("Calling g2_gribend()... "); |
| 76 | + ret = g2_gribend(cgrib); |
| 77 | + if (ret != FULL_MSG_LEN) |
| 78 | + return 1; |
| 79 | + } |
| 80 | + printf("ok!\n"); |
| 81 | + printf("SUCCESS!\n"); |
| 82 | + return 0; |
| 83 | +} |
0 commit comments