static enum ihex_record parse_ihex(const char *line, uint32_t *offset, uint32_t *length, uint8_t **data) { enum ihex_record type; if(sscanf(line, ":%02X%04X%02X", length, offset, &type) != 3) { log("Got invalid IHEX record \"%s\"", line); return IHEX_INVALID; } if(type == IHEX_DATA) { const char *p = line + 9; if(strlen(p) - 2 < *length * 2) { log("Record too short"); return IHEX_INVALID; } if(data) { *data = malloc(*length); for(int i=0; i < *length; i++) { (*data)[i] = HEX2DEC(p[2*i]) << 4 | HEX2DEC(p[2*i+1]); } } } return type; }
/** decodes a URI encoded string into a normal string. */ static std::string uriDecode(const std::string & sSrc) { // Note from RFC1630: "Sequences which start with a percent // sign but are not followed by two hexadecimal characters // (0-9, A-F) are reserved for future extension" const unsigned char * pSrc = (const unsigned char *)sSrc.c_str(); const int SRC_LEN = sSrc.length(); const unsigned char * const SRC_END = pSrc + SRC_LEN; // last decodable '%' const unsigned char * const SRC_LAST_DEC = SRC_END - 2; char * const pStart = new char[SRC_LEN]; char * pEnd = pStart; while (pSrc < SRC_LAST_DEC) { if (*pSrc == '%') // replace %2A with corresponding ASCII character { char dec1, dec2; unsigned char c1=*(pSrc+1); unsigned char c2=*(pSrc+2); if (-1 != (dec1 = HEX2DEC(c1)) && -1 != (dec2 = HEX2DEC(c2))) { *pEnd++ = (dec1 << 4) + dec2; pSrc += 3; continue; } } else if (*pSrc == '+') // replace '+' with space { *pEnd++ = ' '; pSrc++; continue; } *pEnd++ = *pSrc++; } // the last 2- chars while (pSrc < SRC_END) *pEnd++ = *pSrc++; std::string sResult(pStart, pEnd); delete [] pStart; return sResult; }