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 */
|
2023-10-09 09:59:28 +08:00
|
|
|
static int processerrchunk(NCD4response*, void* errchunk, unsigned int count);
|
2017-03-09 08:01:10 +08:00
|
|
|
|
|
|
|
/**************************************************/
|
|
|
|
int
|
2023-10-09 09:59:28 +08:00
|
|
|
NCD4_dechunk(NCD4response* resp)
|
2017-03-09 08:01:10 +08:00
|
|
|
{
|
2023-10-09 09:59:28 +08:00
|
|
|
unsigned char *praw, *pdmr, *phdr, *pdap, *pappend, *pchunk;
|
2021-01-15 12:39:08 +08:00
|
|
|
NCD4HDR hdr;
|
2023-10-09 09:59:28 +08:00
|
|
|
int firstchunk;
|
2017-03-09 08:01:10 +08:00
|
|
|
|
|
|
|
#ifdef D4DUMPRAW
|
2023-10-09 09:59:28 +08:00
|
|
|
NCD4_tagdump(resp->serial.raw.size,resp->serial.raw.data,0,"RAW");
|
2017-03-09 08:01:10 +08:00
|
|
|
#endif
|
|
|
|
|
2021-05-22 10:46:56 +08:00
|
|
|
/* Access the returned raw data */
|
2023-10-09 09:59:28 +08:00
|
|
|
praw = (unsigned char*)resp->raw.memory;
|
2021-05-22 10:46:56 +08:00
|
|
|
|
2023-10-09 09:59:28 +08:00
|
|
|
if(resp->mode == NCD4_DSR) {
|
2021-05-22 10:46:56 +08:00
|
|
|
return THROW(NC_EDMR);
|
2023-10-09 09:59:28 +08:00
|
|
|
} else if(resp->mode == NCD4_DMR) {
|
2021-05-22 10:46:56 +08:00
|
|
|
/* Verify the mode; assume that the <?xml...?> is optional */
|
|
|
|
if(memcmp(praw,"<?xml",strlen("<?xml"))==0
|
|
|
|
|| memcmp(praw,"<Dataset",strlen("<Dataset"))==0) {
|
|
|
|
size_t len = 0;
|
|
|
|
/* setup as dmr only */
|
|
|
|
/* Avoid strdup since rawdata might contain nul chars */
|
2023-10-09 09:59:28 +08:00
|
|
|
len = resp->raw.size;
|
|
|
|
if((resp->serial.dmr = malloc(len+1)) == NULL)
|
2021-05-22 10:46:56 +08:00
|
|
|
return THROW(NC_ENOMEM);
|
2023-10-09 09:59:28 +08:00
|
|
|
memcpy(resp->serial.dmr,praw,len);
|
|
|
|
resp->serial.dmr[len] = '\0';
|
2021-05-22 10:46:56 +08:00
|
|
|
/* Suppress nuls */
|
2023-10-09 09:59:28 +08:00
|
|
|
(void)NCD4_elidenuls(resp->serial.dmr,len);
|
2021-05-22 10:46:56 +08:00
|
|
|
return THROW(NC_NOERR);
|
|
|
|
}
|
2023-10-09 09:59:28 +08:00
|
|
|
} else if(resp->mode != NCD4_DAP)
|
2021-05-22 10:46:56 +08:00
|
|
|
return THROW(NC_EDAP);
|
|
|
|
|
2021-01-15 12:39:08 +08:00
|
|
|
/* We must be processing a DAP mode packet */
|
2023-10-09 09:59:28 +08:00
|
|
|
praw = resp->raw.memory;
|
2021-01-15 12:39:08 +08:00
|
|
|
|
2021-05-22 10:46:56 +08:00
|
|
|
/* If the raw data looks like xml, then we almost certainly have an error */
|
|
|
|
if(memcmp(praw,"<?xml",strlen("<?xml"))==0
|
|
|
|
|| memcmp(praw,"<!doctype",strlen("<!doctype"))==0) {
|
|
|
|
/* Set up to report the error */
|
2023-10-09 09:59:28 +08:00
|
|
|
int stat = NCD4_seterrormessage(resp, resp->raw.size, resp->raw.memory);
|
2021-05-22 10:46:56 +08:00
|
|
|
return THROW(stat); /* slight lie */
|
|
|
|
}
|
|
|
|
|
2023-10-09 09:59:28 +08:00
|
|
|
/* Get the first header to get dmr content and endian flags*/
|
|
|
|
pdmr = NCD4_getheader(praw,&hdr,resp->controller->platform.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);
|
2023-10-09 09:59:28 +08:00
|
|
|
if(hdr.flags & NCD4_ERR_CHUNK)
|
|
|
|
return processerrchunk(resp, (void*)pdmr, hdr.count);
|
|
|
|
resp->remotelittleendian = ((hdr.flags & NCD4_LITTLE_ENDIAN_CHUNK) ? 1 : 0);
|
2017-03-09 08:01:10 +08:00
|
|
|
|
2023-10-09 09:59:28 +08:00
|
|
|
/* avoid strxxx operations on dmr */
|
|
|
|
if((resp->serial.dmr = malloc(hdr.count+1)) == NULL)
|
2020-01-09 06:18:31 +08:00
|
|
|
return THROW(NC_ENOMEM);
|
2023-10-09 09:59:28 +08:00
|
|
|
memcpy(resp->serial.dmr,pdmr,hdr.count);
|
|
|
|
resp->serial.dmr[hdr.count-1] = '\0';
|
2020-01-09 06:18:31 +08:00
|
|
|
/* Suppress nuls */
|
2023-10-09 09:59:28 +08:00
|
|
|
(void)NCD4_elidenuls(resp->serial.dmr,hdr.count);
|
2017-03-09 08:01:10 +08:00
|
|
|
|
2023-10-09 09:59:28 +08:00
|
|
|
/* See if there is any data after the DMR */
|
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 */
|
2023-10-09 09:59:28 +08:00
|
|
|
phdr = pdmr + 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 */
|
2023-10-09 09:59:28 +08:00
|
|
|
if((hdr.count + CHUNKHDRSIZE) >= resp->raw.size) {
|
2020-01-09 06:18:31 +08:00
|
|
|
/* Server only sent the DMR part */
|
2023-10-09 09:59:28 +08:00
|
|
|
resp->serial.dapsize = 0;
|
2020-01-09 06:18:31 +08:00
|
|
|
return THROW(NC_EDATADDS);
|
|
|
|
}
|
2023-10-09 09:59:28 +08:00
|
|
|
/* walk all the data chunks */
|
|
|
|
/* invariants:
|
|
|
|
praw -- beginning of the raw response
|
|
|
|
pdmr -- beginning of the dmr in the raw data
|
|
|
|
pdap -- beginning of the dechunked dap data
|
|
|
|
phdr -- pointer to the hdr of the current chunk
|
|
|
|
pchunk -- pointer to the data part of the current chunk
|
|
|
|
pappend -- where to append next chunk to the growing dechunked data
|
|
|
|
*/
|
|
|
|
for(firstchunk=1;;firstchunk=0) {
|
|
|
|
pchunk = NCD4_getheader(phdr,&hdr,resp->controller->platform.hostlittleendian); /* Process first data chunk header */
|
|
|
|
if(firstchunk) {
|
|
|
|
pdap = phdr; /* remember start point of the dechunked data */
|
|
|
|
pappend = phdr; /* start appending here */
|
2020-01-09 06:18:31 +08:00
|
|
|
}
|
2023-10-09 09:59:28 +08:00
|
|
|
if(hdr.flags & NCD4_ERR_CHUNK)
|
|
|
|
return processerrchunk(resp, (void*)pchunk, hdr.count);
|
2020-01-09 06:18:31 +08:00
|
|
|
/* data chunk; possibly last; possibly empty */
|
2023-10-09 09:59:28 +08:00
|
|
|
if(hdr.count > 0)
|
|
|
|
d4memmove(pappend,pchunk,hdr.count); /* overwrite the header; this the heart of dechunking */
|
|
|
|
pappend += hdr.count; /* next append point */
|
|
|
|
phdr = pchunk + hdr.count; /* point to header of next chunk */
|
2021-01-15 12:39:08 +08:00
|
|
|
if(hdr.flags & NCD4_LAST_CHUNK) break;
|
2017-03-09 08:01:10 +08:00
|
|
|
}
|
2023-10-09 09:59:28 +08:00
|
|
|
resp->serial.dap = pdap; /* start of dechunked data */
|
|
|
|
resp->serial.dapsize = (size_t)DELTA(pappend,pdap);
|
2020-01-09 06:18:31 +08:00
|
|
|
|
2017-03-09 08:01:10 +08:00
|
|
|
#ifdef D4DUMPDMR
|
2023-10-09 09:59:28 +08:00
|
|
|
fprintf(stderr,"%s\n",resp->serial.dmr);
|
2017-03-09 08:01:10 +08:00
|
|
|
fflush(stderr);
|
|
|
|
#endif
|
|
|
|
#ifdef D4DUMPDAP
|
2023-10-09 09:59:28 +08:00
|
|
|
NCD4_tagdump(resp->serial.dapsize,resp->serial.dap,0,"DAP");
|
2017-03-09 08:01:10 +08:00
|
|
|
#endif
|
|
|
|
return THROW(NC_NOERR);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2023-10-09 09:59:28 +08:00
|
|
|
processerrchunk(NCD4response* resp, void* errchunk, unsigned int count)
|
2017-03-09 08:01:10 +08:00
|
|
|
{
|
2023-10-09 09:59:28 +08:00
|
|
|
resp->serial.errdata = (char*)d4alloc(count+1);
|
|
|
|
if(resp->serial.errdata == NULL)
|
2017-03-09 08:01:10 +08:00
|
|
|
return THROW(NC_ENOMEM);
|
2023-10-09 09:59:28 +08:00
|
|
|
memcpy(resp->serial.errdata,errchunk,count);
|
|
|
|
resp->serial.errdata[count] = '\0';
|
2017-03-09 08:01:10 +08:00
|
|
|
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
|
2023-10-09 09:59:28 +08:00
|
|
|
NCD4_infermode(NCD4response* resp)
|
2017-03-09 08:01:10 +08:00
|
|
|
{
|
2023-10-09 09:59:28 +08:00
|
|
|
d4size_t size = resp->raw.size;
|
|
|
|
char* raw = resp->raw.memory;
|
2017-03-09 08:01:10 +08:00
|
|
|
|
|
|
|
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) {
|
2023-10-09 09:59:28 +08:00
|
|
|
resp->mode = NCD4_DMR;
|
2020-01-09 06:18:31 +08:00
|
|
|
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) {
|
2023-10-09 09:59:28 +08:00
|
|
|
resp->mode = NCD4_DAP;
|
2020-01-09 06:18:31 +08:00
|
|
|
goto done;
|
2017-03-09 08:01:10 +08:00
|
|
|
}
|
|
|
|
/* Default to DSR */
|
2023-10-09 09:59:28 +08:00
|
|
|
resp->mode = NCD4_DSR;
|
2017-03-09 08:01:10 +08:00
|
|
|
|
|
|
|
done:
|
|
|
|
return NC_NOERR;
|
|
|
|
}
|