2017-03-09 08:01:10 +08:00
|
|
|
/*********************************************************************
|
2018-12-07 05:24:28 +08:00
|
|
|
* Copyright 2018, UCAR/Unidata
|
2017-03-09 08:01:10 +08:00
|
|
|
* See netcdf/COPYRIGHT file for copying and redistribution conditions.
|
|
|
|
*********************************************************************/
|
|
|
|
|
|
|
|
#include "d4includes.h"
|
|
|
|
#include "d4chunk.h"
|
|
|
|
|
2020-05-31 07:36:25 +08:00
|
|
|
#define CHECKSUMFLAG
|
|
|
|
|
2017-03-09 08:01:10 +08:00
|
|
|
/**************************************************/
|
|
|
|
|
|
|
|
/**************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
Given a packet as read from the wire via http (or a file), convert in
|
2019-09-18 10:27:43 +08:00
|
|
|
place from chunked format to a single contiguous set of bytes. If an
|
2017-03-09 08:01:10 +08:00
|
|
|
error packet is recovered, then make that available to the caller and
|
|
|
|
return an error. Also return whether the data was big endian encoded
|
|
|
|
and whether it has checksums.
|
|
|
|
Notes:
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Forward */
|
|
|
|
static int processerrchunk(NCD4meta* metadata, void* errchunk, unsigned int count);
|
|
|
|
|
|
|
|
/**************************************************/
|
|
|
|
|
2021-01-15 12:39:08 +08:00
|
|
|
void
|
|
|
|
NCD4_resetSerial(NCD4serial* serial, size_t rawsize, void* rawdata)
|
|
|
|
{
|
|
|
|
nullfree(serial->errdata);
|
|
|
|
nullfree(serial->dmr);
|
|
|
|
nullfree(serial->dap);
|
|
|
|
nullfree(serial->rawdata);
|
|
|
|
/* clear all fields */
|
|
|
|
memset(serial,0,sizeof(NCD4serial));
|
|
|
|
/* Reset fields */
|
|
|
|
serial->hostlittleendian = NCD4_isLittleEndian();
|
|
|
|
serial->rawsize = rawsize;
|
|
|
|
serial->rawdata = rawdata;
|
|
|
|
}
|
|
|
|
|
2017-03-09 08:01:10 +08:00
|
|
|
int
|
|
|
|
NCD4_dechunk(NCD4meta* metadata)
|
|
|
|
{
|
2021-01-15 12:39:08 +08:00
|
|
|
unsigned char *praw, *phdr, *pdap;
|
|
|
|
NCD4HDR hdr;
|
2017-03-09 08:01:10 +08:00
|
|
|
|
|
|
|
if(metadata->mode == NCD4_DSR)
|
|
|
|
return THROW(NC_EDMR);
|
|
|
|
|
|
|
|
/* Assume proper mode has been inferred already. */
|
|
|
|
|
|
|
|
/* Verify the mode; assume that the <?xml...?> is optional */
|
2021-01-15 12:39:08 +08:00
|
|
|
praw = metadata->serial.rawdata;
|
|
|
|
if(memcmp(praw,"<?xml",strlen("<?xml"))==0
|
|
|
|
|| memcmp(praw,"<Dataset",strlen("<Dataset"))==0) {
|
|
|
|
size_t len = 0;
|
2020-01-09 06:18:31 +08:00
|
|
|
if(metadata->mode != NCD4_DMR)
|
|
|
|
return THROW(NC_EDMR);
|
|
|
|
/* setup as dmr only */
|
|
|
|
/* Avoid strdup since rawdata might contain nul chars */
|
2021-01-15 12:39:08 +08:00
|
|
|
len = metadata->serial.rawsize;
|
|
|
|
if((metadata->serial.dmr = malloc(len+1)) == NULL)
|
2020-01-09 06:18:31 +08:00
|
|
|
return THROW(NC_ENOMEM);
|
2021-01-15 12:39:08 +08:00
|
|
|
memcpy(metadata->serial.dmr,praw,len);
|
|
|
|
metadata->serial.dmr[len] = '\0';
|
2020-01-09 06:18:31 +08:00
|
|
|
/* Suppress nuls */
|
2021-01-15 12:39:08 +08:00
|
|
|
(void)NCD4_elidenuls(metadata->serial.dmr,len);
|
2020-01-09 06:18:31 +08:00
|
|
|
return THROW(NC_NOERR);
|
2017-03-09 08:01:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef D4DUMPRAW
|
|
|
|
NCD4_tagdump(metadata->serial.rawsize,metadata->serial.rawdata,0,"RAW");
|
|
|
|
#endif
|
|
|
|
|
2021-01-15 12:39:08 +08:00
|
|
|
/* We must be processing a DAP mode packet */
|
|
|
|
praw = (metadata->serial.dap = metadata->serial.rawdata);
|
|
|
|
metadata->serial.rawdata = NULL;
|
|
|
|
|
2017-03-09 08:01:10 +08:00
|
|
|
/* Get the DMR chunk header*/
|
2021-01-15 12:39:08 +08:00
|
|
|
phdr = NCD4_getheader(praw,&hdr,metadata->serial.hostlittleendian);
|
2017-03-09 08:01:10 +08:00
|
|
|
if(hdr.count == 0)
|
2020-01-09 06:18:31 +08:00
|
|
|
return THROW(NC_EDMR);
|
2021-01-15 12:39:08 +08:00
|
|
|
if(hdr.flags & NCD4_ERR_CHUNK) {
|
|
|
|
return processerrchunk(metadata, (void*)phdr, hdr.count);
|
2017-03-09 08:01:10 +08:00
|
|
|
}
|
|
|
|
|
2020-05-31 07:36:25 +08:00
|
|
|
#ifdef CHECKSUMHACK
|
|
|
|
/* Temporary hack; We mistakenly thought that bit 3 of the flags
|
|
|
|
of the first header indicated that checksumming was not in force.
|
|
|
|
Test for it, and propagate the _DAP4_Checksum_CRC32 attribute later */
|
2021-01-15 12:39:08 +08:00
|
|
|
metadata->serial.checksumhack = ((hdr.flags & NCD4_NOCHECKSUM_CHUNK) ? 1 : 0);
|
2020-05-31 07:36:25 +08:00
|
|
|
fprintf(stderr,"checksumhack=%d\n",metadata->serial.checksumhack);
|
|
|
|
#endif
|
2021-01-15 12:39:08 +08:00
|
|
|
metadata->serial.remotelittleendian = ((hdr.flags & NCD4_LITTLE_ENDIAN_CHUNK) ? 1 : 0);
|
2020-01-09 06:18:31 +08:00
|
|
|
/* Again, avoid strxxx operations on dmr */
|
|
|
|
if((metadata->serial.dmr = malloc(hdr.count+1)) == NULL)
|
|
|
|
return THROW(NC_ENOMEM);
|
2021-01-15 12:39:08 +08:00
|
|
|
memcpy(metadata->serial.dmr,phdr,hdr.count);
|
2017-03-09 08:01:10 +08:00
|
|
|
metadata->serial.dmr[hdr.count-1] = '\0';
|
2020-01-09 06:18:31 +08:00
|
|
|
/* Suppress nuls */
|
|
|
|
(void)NCD4_elidenuls(metadata->serial.dmr,hdr.count);
|
2017-03-09 08:01:10 +08:00
|
|
|
|
2021-01-15 12:39:08 +08:00
|
|
|
if(hdr.flags & NCD4_LAST_CHUNK)
|
2017-03-09 08:01:10 +08:00
|
|
|
return THROW(NC_ENODATA);
|
2021-01-15 12:39:08 +08:00
|
|
|
|
|
|
|
/* Read and concat together the data chunks */
|
|
|
|
phdr = phdr + hdr.count; /* point to data chunk header */
|
2020-01-09 06:18:31 +08:00
|
|
|
/* Do a sanity check in case the server has shorted us with no data */
|
|
|
|
if((hdr.count + CHUNKHDRSIZE) >= metadata->serial.rawsize) {
|
|
|
|
/* Server only sent the DMR part */
|
|
|
|
metadata->serial.dapsize = 0;
|
|
|
|
return THROW(NC_EDATADDS);
|
|
|
|
}
|
2021-01-15 12:39:08 +08:00
|
|
|
pdap = metadata->serial.dap;
|
2017-03-09 08:01:10 +08:00
|
|
|
for(;;) {
|
2021-01-15 12:39:08 +08:00
|
|
|
phdr = NCD4_getheader(phdr,&hdr,metadata->serial.hostlittleendian);
|
|
|
|
if(hdr.flags & NCD4_ERR_CHUNK) {
|
|
|
|
return processerrchunk(metadata, (void*)phdr, hdr.count);
|
2020-01-09 06:18:31 +08:00
|
|
|
}
|
|
|
|
/* data chunk; possibly last; possibly empty */
|
|
|
|
if(hdr.count > 0) {
|
2021-01-15 12:39:08 +08:00
|
|
|
d4memmove(pdap,phdr,hdr.count); /* will overwrite the header */
|
|
|
|
phdr += hdr.count;
|
|
|
|
pdap += hdr.count;
|
2020-01-09 06:18:31 +08:00
|
|
|
}
|
2021-01-15 12:39:08 +08:00
|
|
|
if(hdr.flags & NCD4_LAST_CHUNK) break;
|
2017-03-09 08:01:10 +08:00
|
|
|
}
|
2021-01-15 12:39:08 +08:00
|
|
|
metadata->serial.dapsize = (size_t)DELTA(pdap,metadata->serial.dap);
|
2020-01-09 06:18:31 +08:00
|
|
|
|
2017-03-09 08:01:10 +08:00
|
|
|
#ifdef D4DUMPDMR
|
|
|
|
fprintf(stderr,"%s\n",metadata->serial.dmr);
|
|
|
|
fflush(stderr);
|
|
|
|
#endif
|
|
|
|
#ifdef D4DUMPDAP
|
|
|
|
NCD4_tagdump(metadata->serial.dapsize,metadata->serial.dap,0,"DAP");
|
|
|
|
#endif
|
|
|
|
return THROW(NC_NOERR);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
processerrchunk(NCD4meta* metadata, void* errchunk, unsigned int count)
|
|
|
|
{
|
|
|
|
metadata->serial.errdata = (char*)d4alloc(count+1);
|
|
|
|
if(metadata->serial.errdata == NULL)
|
|
|
|
return THROW(NC_ENOMEM);
|
|
|
|
memcpy(metadata->serial.errdata,errchunk,count);
|
|
|
|
metadata->serial.errdata[count] = '\0';
|
|
|
|
return THROW(NC_ENODATA); /* slight lie */
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Given a raw response, attempt to infer the mode: DMR, DAP, DSR.
|
|
|
|
Since DSR is not standardizes, it becomes the default.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
NCD4_infermode(NCD4meta* meta)
|
|
|
|
{
|
|
|
|
d4size_t size = meta->serial.rawsize;
|
|
|
|
char* raw = meta->serial.rawdata;
|
|
|
|
|
|
|
|
if(size < 16)
|
2020-01-09 06:18:31 +08:00
|
|
|
return THROW(NC_EDAP); /* must have at least this to hold a hdr + partial dmr*/
|
2017-03-09 08:01:10 +08:00
|
|
|
if(memcmp(raw,"<?xml",strlen("<?xml"))==0
|
|
|
|
|| memcmp(raw,"<Dataset",strlen("<Dataset"))==0) {
|
2020-01-09 06:18:31 +08:00
|
|
|
meta->mode = NCD4_DMR;
|
|
|
|
goto done;
|
2017-03-09 08:01:10 +08:00
|
|
|
}
|
|
|
|
raw += 4; /* Pretend we have a DAP hdr */
|
|
|
|
if(memcmp(raw,"<?xml",strlen("<?xml"))==0
|
|
|
|
|| memcmp(raw,"<Dataset",strlen("<Dataset"))==0) {
|
2020-01-09 06:18:31 +08:00
|
|
|
meta->mode = NCD4_DAP;
|
|
|
|
goto done;
|
2017-03-09 08:01:10 +08:00
|
|
|
}
|
|
|
|
/* Default to DSR */
|
|
|
|
meta->mode = NCD4_DSR;
|
|
|
|
|
|
|
|
done:
|
|
|
|
return NC_NOERR;
|
|
|
|
}
|