@@ -98,7 +98,7 @@ int WiFiPropConnection::identify(int *pVersion)
9898
9999int WiFiPropConnection::loadImage (const uint8_t *image, int imageSize, uint8_t *response, int responseSize)
100100{
101- uint8_t buffer[1024 ], *packet, *p ;
101+ uint8_t buffer[1024 ], *packet, *body ;
102102 int hdrCnt, result, cnt;
103103
104104 /* use the initial loader baud rate */
@@ -126,22 +126,13 @@ Content-Length: %d\r\n\
126126 }
127127
128128 /* find the response body */
129- p = buffer;
130- while (cnt >= 4 && (p[0 ] != ' \r ' || p[1 ] != ' \n ' || p[2 ] != ' \r ' || p[3 ] != ' \n ' )) {
131- --cnt;
132- ++p;
133- }
134-
135- /* make sure we found the \r\n\r\n that terminates the header */
136- if (cnt < 4 )
129+ if (!(body = getBody (buffer, cnt, &cnt)))
137130 return -1 ;
138- cnt -= 4 ;
139- p += 4 ;
140-
131+
141132 /* copy the body to the response if it fits */
142- if (cnt > responseSize)
133+ if (cnt != responseSize)
143134 return -1 ;
144- memcpy (response, p , cnt);
135+ memcpy (response, body , cnt);
145136
146137 return 0 ;
147138}
@@ -351,15 +342,15 @@ bool WiFiPropConnection::isOpen()
351342
352343int WiFiPropConnection::getVersion ()
353344{
354- uint8_t buffer[1024 ];
355- int hdrCnt, result, srcLen ;
356- char *src, * dst;
345+ uint8_t buffer[1024 ], *body ;
346+ int hdrCnt, result, cnt ;
347+ char *dst;
357348
358349 hdrCnt = snprintf ((char *)buffer, sizeof (buffer), " \
359350GET /wx/setting?name=version HTTP/1.1\r\n \
360351\r\n " );
361352
362- if (sendRequest (buffer, hdrCnt, buffer, sizeof (buffer), &result) == -1 ) {
353+ if ((cnt = sendRequest (buffer, hdrCnt, buffer, sizeof (buffer), &result) ) == -1 ) {
363354 message (" Get version failed" );
364355 return -1 ;
365356 }
@@ -368,28 +359,23 @@ GET /wx/setting?name=version HTTP/1.1\r\n\
368359 return -1 ;
369360 }
370361
371- src = (char *)buffer;
372- srcLen = strlen (src);
373-
374- while (srcLen >= 4 ) {
375- if (src[0 ] == ' \r ' && src[1 ] == ' \n ' && src[2 ] == ' \r ' && src[3 ] == ' \n ' )
376- break ;
377- --srcLen;
378- ++src;
379- }
380- if (srcLen <= 4 ) {
362+ if (!(body = getBody (buffer, cnt, &cnt)))
363+ return -1 ;
364+
365+ if (cnt <= 0 ) {
381366 message (" No version string" );
382367 return -1 ;
383368 }
384369
385- if (!(dst = (char *)malloc (srcLen - 4 + 1 ))) {
370+ if (!(dst = (char *)malloc (cnt + 1 ))) {
386371 nmessage (ERROR_INSUFFICIENT_MEMORY);
387372 return -1 ;
388373 }
389374
390375 if (m_version)
391376 free (m_version);
392- strcpy (dst, src + 4 );
377+ strncpy (dst, (char *)body, cnt);
378+ body[cnt] = ' \0 ' ;
393379 m_version = dst;
394380
395381 return 0 ;
@@ -559,6 +545,26 @@ int WiFiPropConnection::sendRequest(uint8_t *req, int reqSize, uint8_t *res, int
559545 return cnt;
560546}
561547
548+ uint8_t *WiFiPropConnection::getBody (uint8_t *msg, int msgSize, int *pBodySize)
549+ {
550+ uint8_t *p = msg;
551+ int cnt = msgSize;
552+
553+ /* find the message body */
554+ while (cnt >= 4 && (p[0 ] != ' \r ' || p[1 ] != ' \n ' || p[2 ] != ' \r ' || p[3 ] != ' \n ' )) {
555+ --cnt;
556+ ++p;
557+ }
558+
559+ /* make sure we found the \r\n\r\n that terminates the header */
560+ if (cnt < 4 )
561+ return NULL ;
562+
563+ /* return the body */
564+ *pBodySize = cnt - 4 ;
565+ return p + 4 ;
566+ }
567+
562568void WiFiPropConnection::dumpHdr (const uint8_t *buf, int size)
563569{
564570 int startOfLine = true ;
0 commit comments