MCPcopy Create free account
hub / github.com/ValveSoftware/openvr / V_URLDecodeInternal

Function V_URLDecodeInternal

src/vrcore/strtools_public.cpp:420–488  ·  view source on GitHub ↗

----------------------------------------------------------------------------- Purpose: Internal implementation of decode, works in the strict RFC manner, or with spaces turned to + like HTML form encoding. Returns the amount of space used in the output buffer. -----------------------------------------------------------------------------

Source from the content-addressed store, hash-verified

418// Returns the amount of space used in the output buffer.
419//-----------------------------------------------------------------------------
420size_t V_URLDecodeInternal( char *pchDecodeDest, int nDecodeDestLen, const char *pchEncodedSource, int nEncodedSourceLen, bool bUsePlusForSpace )
421{
422 if ( nDecodeDestLen < nEncodedSourceLen )
423 {
424 //AssertMsg( false, "V_URLDecode needs a dest buffer at least as large as the source" );
425 return 0;
426 }
427
428 int iDestPos = 0;
429 for( int i=0; i < nEncodedSourceLen; ++i )
430 {
431 if ( bUsePlusForSpace && pchEncodedSource[i] == '+' )
432 {
433 pchDecodeDest[ iDestPos++ ] = ' ';
434 }
435 else if ( pchEncodedSource[i] == '%' )
436 {
437 // Percent signifies an encoded value, look ahead for the hex code, convert to numeric, and use that
438
439 // First make sure we have 2 more chars
440 if ( i < nEncodedSourceLen - 2 )
441 {
442 char cHexDigit1 = pchEncodedSource[i+1];
443 char cHexDigit2 = pchEncodedSource[i+2];
444
445 // Turn the chars into a hex value, if they are not valid, then we'll
446 // just place the % and the following two chars direct into the string,
447 // even though this really shouldn't happen, who knows what bad clients
448 // may do with encoding.
449 bool bValid = false;
450 int iValue = iHexCharToInt( cHexDigit1 );
451 if ( iValue != -1 )
452 {
453 iValue *= 16;
454 int iValue2 = iHexCharToInt( cHexDigit2 );
455 if ( iValue2 != -1 )
456 {
457 iValue += iValue2;
458 pchDecodeDest[ iDestPos++ ] = (char)iValue;
459 bValid = true;
460 }
461 }
462
463 if ( !bValid )
464 {
465 pchDecodeDest[ iDestPos++ ] = '%';
466 pchDecodeDest[ iDestPos++ ] = cHexDigit1;
467 pchDecodeDest[ iDestPos++ ] = cHexDigit2;
468 }
469 }
470
471 // Skip ahead
472 i += 2;
473 }
474 else
475 {
476 pchDecodeDest[ iDestPos++ ] = pchEncodedSource[i];
477 }

Callers 2

V_URLDecodeFunction · 0.70

Calls 1

iHexCharToIntFunction · 0.70

Tested by

no test coverage detected