Ignore:
Timestamp:
12/27/10 20:05:12 (10 years ago)
Author:
alno
Message:

XMLAPI: XML encode Channelnames in readerstats ecmstats. THX Admin for help and encoding function

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/oscam-http-helpers.c

    r4160 r4233  
    483483}
    484484
     485/* XML-Escapes a char array. The returned reference will be automatically cleaned through the templatevars-mechanism tpl_clear().
     486   Do not call free() or realloc on the returned reference or you will get memory corruption! */
     487char *xml_encode(struct templatevars *vars, char *chartoencode) {
     488    int i, pos = 0, len = strlen(chartoencode);
     489    /* In worst case, every character could get converted to 6 chars (we only support ASCII, for Unicode it would be 7)*/
     490    char encoded[len * 6 + 1], buffer[7];
     491    for (i = 0; i < len; ++i){
     492        switch(chartoencode[i]) {
     493            case '&': memcpy(encoded + pos, "&amp;", 5); pos+=5; break;
     494            case '<': memcpy(encoded + pos, "&lt;", 4); pos+=4; break;
     495            case '>': memcpy(encoded + pos, "&gt;", 4); pos+=4; break;
     496            case '"': memcpy(encoded + pos, "&quot;", 6); pos+=6; break;
     497            case '\'': memcpy(encoded + pos, "&apos;", 6); pos+=6; break;
     498
     499            default:
     500                if ( (unsigned int)chartoencode[i] < 32 || (unsigned int)chartoencode[i] > 127 ) {
     501                    snprintf(buffer, 7, "&#%d;", chartoencode[i] + 256);
     502                    memcpy(encoded + pos, buffer, strlen(buffer));
     503                    pos+=strlen(buffer);
     504
     505                } else {
     506                    encoded[pos] = chartoencode[i];
     507                    ++pos;
     508                }
     509
     510        }
     511    }
     512    /* Allocate the needed memory size and store it in the templatevars */
     513    char *result = (char *)malloc(pos + 1);
     514    memcpy(result, encoded, pos);
     515    result[pos] = '\0';
     516    return tpl_addTmp(vars, result);
     517}
    485518#endif
Note: See TracChangeset for help on using the changeset viewer.