Author Topic: HTTPInsertString padding to 4 chars minimum - necessary? can disable?  (Read 7683 times)

Offline thamanjd

  • Jr. Member
  • **
  • Posts: 57
    • View Profile

I,m not a guru on http....

i notice that if the string length being inserted into a web page edit box is less than 4 characters, then the text is padded out to be 4 characters.

Is it necessary for some process for this to be the case?
Is it inadvisable to disable it?

John Dowdell

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3182
    • View Profile
    • uTasker
Hi John

The reason why the text is padded to 4 characters to to ensure that the special sequence in the html file (eg. xvM1 - where x is the special sign for string insertion, which can be defined for a project - I am using a Greek keyboard while away on holiday and can't find the normal sign I use!!) is fully overwritten.

It is a technique which was used when the dynamic generation was originally defined but is in fact a small weakness. I know of someone who made a modification to improve this but I unfortunately don't have a copy of my emails with me.
However maybe someone can give a tip of how it is possible to improve this. I will take a look since I do believe that it is quite easily possible, so will be a worthwhile improvement.

I will take a closer look and get back.

Regards

Mark

Offline thamanjd

  • Jr. Member
  • **
  • Posts: 57
    • View Profile
answering questions while on holiday... i hope youre not getting into trouble for doing that :)

So yeah ok, i kind of get what you mean. And of course i went to see what would happen if i took it out all together :) which screwed up the web page :)

For the moment i have edited the routine slightly so that the padding ends up after my text rather than before it and that'll work better for what i'm doing. I hav't seen any adverse effects from doing that so far.

thanks again
John Dowdell

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3182
    • View Profile
    • uTasker
Hi John

I think that I may have a solution.

1. In fnInsertHTMLString() in webutils.c exchange the following code:
Code: [Select]
// This routine is used to insert a string in place of the £XAB marker, which is assumed to be always 4 bytes long
// The inserted text should be at least 4 bytes of length to ensue that nothing is left in the buffer
// Typically this routine is used to set check boxes or selected fields, etc.
//
extern int fnInsertHTMLString(CHAR *cToAdd, unsigned short usAddLength, unsigned char **ptrBuffer, unsigned short *usMaxLen, unsigned short *usLen)
{
    unsigned char *ptrEnd;
    unsigned char *ptrShift;
    unsigned short usLength = *usLen - 1;
#ifndef TEST_INSERT
    CHAR           cShort[4];

    if (usAddLength < 4) {                                               // if the user wants to insert something which is shorter than the 4 command bytes,

we increase its length by inserting spaces before it
        unsigned char ucShift = (4 - usAddLength);
        uMemset(cShort, ' ', 4);
        while (ucShift < 4) {
            cShort[ucShift++] = *cToAdd++;
        }
        usAddLength = 4;
        cToAdd = cShort;
    }
#endif

    ptrEnd = *ptrBuffer + usLength;                                      // set pointer to end of buffer
    ptrShift = ptrEnd - usAddLength;                                     // set pointer to end after insertion

    if ((*ptrBuffer - 4 + usAddLength) >= ptrEnd) {
#ifdef TEST_INSERT
        int iFrameReduction = ptrEnd - *ptrBuffer + 4;
        *usMaxLen -= (usLength + 4);
        *ptrBuffer = 0;                                                  // present frame terminated so mark by clearing pointer
        return (iFrameReduction);
#else
        *usMaxLen -= (usLength + 4);
        return (ptrEnd - *ptrBuffer + 4);                                // we don't have the necessary space to add at the moment (shorten the present frame

to make it possible next time around)
#endif
    }

#ifdef TEST_INSERT
    if (usAddLength >= 4) {
        usLength -= (usAddLength - 4);                                   // the remaining length after insertion
        ptrShift += 4;
        if (ptrEnd == ptrShift) {                                        // no shift necessary in this case
            usLength = 0;
        }
        while (usLength--) {
            *(--ptrEnd) = *(--ptrShift);                                 // shift remaining bytes along in buffer, making room for the insertion
        }
    }
    else {
        ptrShift = *ptrBuffer - (4 - usAddLength);
        ptrEnd = *ptrBuffer;
        uMemcpy(ptrShift, ptrEnd, usLength);
    }
#else
    usLength -= (usAddLength - 4);                                       // the remaining length after insertion
    ptrShift += 4;
    while (usLength--) {
        *(--ptrEnd) = *(--ptrShift);                                     // shift remaining bytes along in buffer, making room for the insertion
    }
#endif

    ptrShift = *ptrBuffer - 4;
    uMemcpy(ptrShift, cToAdd, usAddLength);                              // insert the string
    *ptrBuffer = ptrShift + usAddLength;

#ifdef TEST_INSERT
    if (usAddLength >= 4) {
        usAddLength -= 4;
        *usLen -= (usAddLength);                                         // correct lengths after insertion
        *usMaxLen -= usAddLength;
    }
    else {
        return (4 - usAddLength);                                        // shorten complete file by this amount
    }
#else
    usAddLength -= 4;
    *usLen -= (usAddLength);                                             // correct lengths after insertion
    *usMaxLen -= usAddLength;
#endif
    return 0;
}
#endif


2. In http.c make the following change

Code: [Select]

                if ((usLost = fnInsertHTMLString((CHAR *)cInsertString, usInsertLength, &ptrBuffer, usMaxLen, &usLen)) != 0) {
#ifdef TEST_INSERT
                    if (ptrBuffer != 0) {                                // file length reduction
                        usToSend -= usLost;
                    }
                    else {
                        return (usToSend - usLost);                      // parameter list cut off, handle it next time around
                    }
#else
                    return (usToSend - usLost);                          // parameter list cut off, handle it next time around
#endif



By adding TEST_INSERT as project define - for example in config.h - the new method will be used. By removing it, you can fall back to the old method should

any new problem be encoutered,

Basically I was lazy when originally writing the string insertion routine. It padded to avoid buffer manipulation when inserting short strings (shorter that

the control sequence) which actually works fine in most html code since space padding is also ignored. But spaces in a form input are set as default input

which was not perfect - I believe the right alignment made it easier to see that the string wasn't followed by spaces.

My tests with the above code were good but I tested only the standard pages in the demo project and I can't exclude a surprise (originally it did take quite

a lot of work to ensure that every possible sequence was encountered for - eg. when frames have to be cut short because the new string won't fit, etc. etc.).

I will keep this new code active in my reference project and if there is no negative feedback from anyone who uses it I will activate it in the next service

packs. Should anyone detect a new problem, send me the offending web page and I will try to fix it asap. There is however a good chance that it will already

be stable.

Regards

Mark

Offline thamanjd

  • Jr. Member
  • **
  • Posts: 57
    • View Profile
Re: HTTPInsertString padding to 4 chars minimum - necessary? can disable?
« Reply #4 on: August 29, 2007, 03:59:27 PM »
Ive been using your modification.
I havn't encountered any problems with it yet.

Thanks,

John Dowdell

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3182
    • View Profile
    • uTasker
Re: HTTPInsertString padding to 4 chars minimum - necessary? can disable?
« Reply #5 on: August 29, 2007, 04:41:27 PM »
Hi John

Many thanks for the feedback.
I have committed the improvement to the latest SP for the M5223X project since I also sound it worked well - if there is a hidden issue it will be more likely found then.

Regards

Mark