AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   Want for urldecode method, need help with rewrite from C (https://forums.alliedmods.net/showthread.php?t=329846)

ZASTRELIS 01-10-2021 09:03

Want for urldecode method, need help with rewrite from C
 
Code:

https://github.com/abejfehr/URLDecode/blob/master/urldecode.c
Need help in written same code in SM. Thx. I use sockets which not provided UTF-8 decoding

ZASTRELIS 01-10-2021 09:37

Re: Want for urldecode method, need help with rewrite from C
 
I've something like this.. that all which can I do ))

Code:
stock void decodeURL(const char[] inStr, char[] outStr, int size) {         char buffer[4096]; int len = strlen(inStr); int wpos = 0; int diff = 0;         // "%D0%BF%D1%80%D0%BE%D1%81%D1%82%D0%BE+%D0%BF%D0%B0%D1%80%D0%B0%D0%BB%D0%BB%D0%B5%D0%BB%D1%8C%D0%BD%D1%8B%D0%B9+%D0%BC%D0%B8%D1%80" = len         for(int i = 0; i < len; i += diff)     {         special[4]; strcopy(special, sizeof(special), inStr[i]);         chars[6]; strcopy(chars, sizeof(chars), inStr[i]);             for(int j = 0; j < sizeof(dec); ++j)         {             if(StrEqual(special, dec[j][1], false))             {                 strcopy(outStr, size, inStr[wpos]); ++wpos; diff = 4;             }                         else if(StrEqual(chars, dec[j][1], false))             {                 strcopy(outStr, size, inStr[wpos]); ++wpos;  diff = 6;             }         }     }         PrintToServer("Decoded URL: %s", outStr); }

And some characters table..
Code:
static char dec[][] = {     {"~", "%7E"},     {"`", "60%"},     {"'", "27%"},         /* ....... */     {"Ю", "%D0%A%"},     {"ю", "%D1%8%"},     {"Я", "%D0%A%"},     {"я", "%D1%8F"} };

Bacardi 01-21-2021 18:23

Re: Want for urldecode method, need help with rewrite from C
 
....I don't know.
I maybe made it too complex. Have a fun.

resultss
Code:

char buffer[] = "https%3A%2F%2Fwww.google.ca%2F%3Fgws_rd%3Dssl%23q%3Durl%2Bdecoding";
PrintToServer "httpsAFFwww.google.caFFgws_rdDsslqDurlBdecoding"
PrintToServer "https://www.google.ca/?gws_rd=ssl#q=url+decoding"


PHP Code:

#include <regex>

Regex regex;

public 
void OnPluginStart()
{
    
regex = new Regex("^%[0-9a-fA-F]{2}");

    if(
regex == nullSetFailState("Regex fail");

    
RegConsoleCmd("sm_test"test);

}

public 
Action test(int clientint args)
{

    
char buffer[] = "https%3A%2F%2Fwww.google.ca%2F%3Fgws_rd%3Dssl%23q%3Durl%2Bdecoding";

    
char buffer2[PLATFORM_MAX_PATH];

    
PrintToServer(buffer); // ...you not see '%' symbols in console, function is formatting it.

    
int index 0;
    
int result;


    do
    {

        
result FindCharInString(buffer[index], '%'false);

        if(
result == -1) break;

        
index += result;

        
//PrintToServer("%s", buffer[index]);


        // found something like %3A
        
if(isxdigit(buffer[index]))
        {
            
// Change characters to hex value
            
char a[4]; // %3A
            
char b[2]; // new character

            
Format(asizeof(a), "%c%c%c"buffer[index], buffer[index+1], buffer[index+2]);

            
Format(bsizeof(b), "%c"StringToInt(a[1], 16)); // exclude %

            //PrintToServer("%s", b);

            
ReplaceString(buffersizeof(buffer), abtrue);
            
            
index = -1// reset loop

        
}



        
index++;

    }
    while( 
index strlen(buffer))
    

    
// Final
    
PrintToServer("%s"buffer);

}


bool isxdigit(const char[] x)
{
    
char a[4]; // %3A
    
Format(asizeof(a), "%c%c%c"x[0], x[1], x[2]);

    
//PrintToServer("%s", a);

    
return regex.Match(a) > 0;



asherkin 01-22-2021 05:42

Re: Want for urldecode method, need help with rewrite from C
 
Code:

PrintToServer(buffer); // ...you not see '%' symbols in console, function is formatting it.
This is dangerous, the correct usage is:

Code:

PrintToServer("%s", buffer);

Bacardi 01-22-2021 06:01

Re: Want for urldecode method, need help with rewrite from C
 
Ok, roger.

MAGNAT2645 05-06-2021 14:17

Re: Want for urldecode method, need help with rewrite from C
 
I'm late but there are 2 natives provided by System2 Extension.
Code:

/**
 * Converts a plain string to an URL encoded string.
 * All input characters that are not a-z, A-Z, 0-9, '-', '.', '_' or '~'
 * are converted to their "URL escaped" version (%NN where NN is a two-digit hexadecimal number).
 *
 * Be aware that the output string may be larger then the input string.
 *
 * @param output        Buffer to store encoded string in. May point to the input string (will replace the input string).
 * @param maxlength    Maxlength of the output buffer.
 * @param input        String to encode.
 * @param ...          Input string format arguments
 *
 * @return              True on success, false otherwise.
 */
native bool System2_URLEncode(char[] output, int maxlength, const char[] input, any ...);

/**
 * Converts an URL encoded string to a plain string.
 * All input characters that are URL encoded (%XX where XX is a two-digit hexadecimal number) are converted to their binary versions.
 *
 * @param output        Buffer to store decoded string in. May point to the input string (will replace the input string).
 * @param maxlength    Maxlength of the output buffer.
 * @param input        String to decode.
 * @param ...          Input string format arguments
 *
 * @return              True on success, false otherwise.
 */
native bool System2_URLDecode(char[] output, int maxlength, const char[] input, any ...);



All times are GMT -4. The time now is 14:02.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.