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

Function V_URLDecodeInternal

samples/shared/strtools.cpp:318–386  ·  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

316// Returns the amount of space used in the output buffer.
317//-----------------------------------------------------------------------------
318size_t V_URLDecodeInternal( char *pchDecodeDest, int nDecodeDestLen, const char *pchEncodedSource, int nEncodedSourceLen, bool bUsePlusForSpace )
319{
320 if ( nDecodeDestLen < nEncodedSourceLen )
321 {
322 //AssertMsg( false, "V_URLDecode needs a dest buffer at least as large as the source" );
323 return 0;
324 }
325
326 int iDestPos = 0;
327 for( int i=0; i < nEncodedSourceLen; ++i )
328 {
329 if ( bUsePlusForSpace && pchEncodedSource[i] == '+' )
330 {
331 pchDecodeDest[ iDestPos++ ] = ' ';
332 }
333 else if ( pchEncodedSource[i] == '%' )
334 {
335 // Percent signifies an encoded value, look ahead for the hex code, convert to numeric, and use that
336
337 // First make sure we have 2 more chars
338 if ( i < nEncodedSourceLen - 2 )
339 {
340 char cHexDigit1 = pchEncodedSource[i+1];
341 char cHexDigit2 = pchEncodedSource[i+2];
342
343 // Turn the chars into a hex value, if they are not valid, then we'll
344 // just place the % and the following two chars direct into the string,
345 // even though this really shouldn't happen, who knows what bad clients
346 // may do with encoding.
347 bool bValid = false;
348 int iValue = iHexCharToInt( cHexDigit1 );
349 if ( iValue != -1 )
350 {
351 iValue *= 16;
352 int iValue2 = iHexCharToInt( cHexDigit2 );
353 if ( iValue2 != -1 )
354 {
355 iValue += iValue2;
356 pchDecodeDest[ iDestPos++ ] = (char)iValue;
357 bValid = true;
358 }
359 }
360
361 if ( !bValid )
362 {
363 pchDecodeDest[ iDestPos++ ] = '%';
364 pchDecodeDest[ iDestPos++ ] = cHexDigit1;
365 pchDecodeDest[ iDestPos++ ] = cHexDigit2;
366 }
367 }
368
369 // Skip ahead
370 i += 2;
371 }
372 else
373 {
374 pchDecodeDest[ iDestPos++ ] = pchEncodedSource[i];
375 }

Callers 1

V_URLDecodeFunction · 0.70

Calls 1

iHexCharToIntFunction · 0.70

Tested by

no test coverage detected