Home AMX User Forum AMX General Discussion

URL Encoding?

I'm finishing up a module for Axis PTZ cameras and I realized after a bit of time I needed to encode or escape certain characters I was sending to the camera when creating named presets from the TP. The first function I wrote required a lookup table to determine which characters needed to be encoded and then a 2nd lookup table contained the encoding.

This worked great but I wasn't satisfied and wanted it shorter and simpler.
DEFINE_CONSTANT //URL ENCODING

URL_ENCODE_NUM_CHARS	= 23 ;

VOLATILE CHAR cURL_ENCODE_CHARS[URL_ENCODE_NUM_CHARS] = 
     {// 1    2     3     4     5     6     7     8     9     10    11    12    13    14    15    16   17    18    19    20    21    22    23
       ';',  '?',  '/',  ':',  '#',  '&',  '=',  '+',  '$',  ',',  ' ',  '%',  '<',  '>',  '~',  '{',  '}',  '[',  ']',  '|',  '\',  '`',  '^'
     }// |    |     |     |     |     |     |     |     |     |     |     |     |     |     |     |     |     |     |     |     |     |     |
VOLATILE CHAR cURL_ENCODE_ESCAPED[URL_ENCODE_NUM_CHARS][3] =
     {// |    |     |     |     |     |     |     |     |     |     |     |     |     |     |     |     |     |     |     |     |     |     |
     '%3B','%3F','%2F','%3A','%23','%26','%3D','%2B','%24','%2C','%20','%25','%3C','%3E','%7E','%7B','%7D','%5B','%5D','%7C','%5C','%60','%5E'
     }// |    |     |     |     |     |     |     |     |     |     |     |     |     |     |     |     |     |     |     |     |     |     |


DEFINE_FUNCTION CHAR[AXIS_PRESET_NAME_LEN * 3] fnURL_Encode(CHAR iStr[]) 

     {
     STACK_VAR INTEGER i ;
     STACK_VAR INTEGER r ;
     STACK_VAR INTEGER nLenURLStr ;
     STACK_VAR CHAR cURL_EncodeStr[AXIS_PRESET_NAME_LEN * 3] ;//must be bigger. you might be swapping 1 char for 3!
     
     fnAxis_DeBug("'URL Encode In: ',iSTR,'.>-line-<',ITOA(__LINE__),'>'") ;
     nLenURLStr = length_string(iStr) ;
     for(i = 1 ; i <= nLenURLStr ; i ++)
	  {  
	  for(r = 1 ; r <= URL_ENCODE_NUM_CHARS ; r ++)
	       {
	       if(iStr[i] == cURL_ENCODE_CHARS[r])
		    {
		    cURL_EncodeStr = "cURL_EncodeStr,GET_BUFFER_STRING(iStr,i-1),cURL_ENCODE_ESCAPED[r]" ;
		    GET_BUFFER_CHAR(iStr) ;
		    nLenURLStr = length_string(iStr) ;
		    r = URL_ENCODE_NUM_CHARS + 1 ;
		    i = 0 ;
		    }
	       }
	  }
     fnAxis_DeBug("'URL Encode Out: ',cURL_EncodeStr,iStr,'.>-line-<',ITOA(__LINE__),'>'") ;
     RETURN "cURL_EncodeStr,iStr" ;
     }

So I tried this route which doesn't require any look up tables and easier to throw into a module. When testing this version if you look at the print out in the diagnostics window where I should be getting %20 for the space after the word light I always get %70 (lower case p) in my string being sent to the camera. The camera receives this string and I get a button ofn the TP that reads " street lightp& ( pole) , , , .

Any ideas as to what could be causing this? ITOHEX?. I can always go back to my original version but I'd like to know what's the deal.
DEFINE_FUNCTION CHAR[AXIS_PRESET_NAME_LEN * 3] fnURL_Encode_2(CHAR iStr[]) 

     {
     STACK_VAR INTEGER i ;
     STACK_VAR INTEGER nLenURLStr ;
     STACK_VAR CHAR cURL_EncodeStr[AXIS_PRESET_NAME_LEN * 3] ;//must be bigger. you might be swapping 1 char for 3!
     
     fnAxis_DeBug("'URL Encode In: ',iSTR,'.>-line-<',ITOA(__LINE__),'>'") ;
     nLenURLStr = length_string(iStr) ;
     for(i = 1 ; i <= nLenURLStr ; i ++)
	  {
	  if(iStr[i] > 31 && iStr[i] < 48 || iStr[i] > 57 && iStr[i] < 65 || 
		    iStr[i] > 90 && iStr[i] < 97 || iStr[i] > 122 && iStr[i] < 127)
	       {
	       cURL_EncodeStr = "cURL_EncodeStr,GET_BUFFER_STRING(iStr,i-1),'%',ITOHEX(iStr[i])" ;
	       GET_BUFFER_CHAR(iStr) ;
	       nLenURLStr = length_string(iStr) ;
	       i = 0 ;
	       }
	  }
     fnAxis_DeBug("'URL Encode Out: ',cURL_EncodeStr,iStr,'.>-line-<',ITOA(__LINE__),'>'") ;
     RETURN "cURL_EncodeStr,iStr" ;
     }	  

Diagnostic window:
Line     19 (20:28:40):: AXISCAM MOD Instance 3: URL Encode In: street light & ( pole ) , , ,...>-line-<1787>
Line     20 (20:28:40):: AXISCAM MOD Instance 3: URL Encode Out: street%20light%70%26%20%28%20pole%20%29%20%2C%20%2C%20%2C%2E%2E.>-line-<1800>

Comments

  • Joe HebertJoe Hebert Posts: 2,159
    vining wrote: »
    Any ideas as to what could be causing this? ITOHEX?. I can always go back to my original version but I'd like to know what's the deal.
    GET_BUFFER_STRING(iStr,i-1) will change the position that ITOHEX(iStr) is pointing at so you need to capture the character to be encoded before altering the string.

    Try this simple change and you should be okay:

    DEFINE_FUNCTION CHAR[AXIS_PRESET_NAME_LEN * 3] fnURL_Encode_2(CHAR iStr[]) 
    
         {
         STACK_VAR INTEGER i ;
         [b]STACK_VAR INTEGER nEncodeByte;[/b]
         STACK_VAR INTEGER nLenURLStr ;
         STACK_VAR CHAR cURL_EncodeStr[AXIS_PRESET_NAME_LEN * 3] ;//must be bigger. you might be swapping 1 char for 3!
         
         fnAxis_DeBug("'URL Encode In: ',iSTR,'.>-line-<',ITOA(__LINE__),'>'") ;
         nLenURLStr = length_string(iStr) ;
         for(i = 1 ; i <= nLenURLStr ; i ++)
    	  {
    	  if(iStr[i] > 31 && iStr[i] < 48 || iStr[i] > 57 && iStr[i] < 65 || 
    		    iStr[i] > 90 && iStr[i] < 97 || iStr[i] > 122 && iStr[i] < 127)
    	       {
    	       [b]nEncodeByte = iStr[i][/b]
    	       //cURL_EncodeStr = "cURL_EncodeStr,GET_BUFFER_STRING(iStr,i-1),'%',ITOHEX(iStr[i])" ;
    	       [b]cURL_EncodeStr = "cURL_EncodeStr,GET_BUFFER_STRING(iStr,i-1),'%',ITOHEX(nEncodeByte)" ;[/b]
    	       GET_BUFFER_CHAR(iStr) ;
    	       nLenURLStr = length_string(iStr) ;
    	       i = 0 ;
    	       }
    	  }
         fnAxis_DeBug("'URL Encode Out: ',cURL_EncodeStr,iStr,'.>-line-<',ITOA(__LINE__),'>'") ;
         RETURN "cURL_EncodeStr,iStr" ;
         }
    

    HTH
  • viningvining Posts: 4,368
    Joe Hebert wrote:
    GET_BUFFER_STRING(iStr,i-1) will change the position that ITOHEX(iStr) is pointing at
    Well duh... Normally I would blame fatigue but it wasn't even 9pm when I threw that function together. Although that mistake is plain as day now I bet if I looked for it last night I would have stared at it for hours and not seen it. Thanks again! :)
Sign In or Register to comment.