char *replaceAll( const char *inHaystack, const char *inTarget, const char *inSubstitute, char *outFound ) { // repeatedly replace once until replacing fails char lastFound = true; char atLeastOneFound = false; char *returnString = stringDuplicate( inHaystack ); while( lastFound ) { char *nextReturnString = replaceOnce( returnString, inTarget, inSubstitute, &lastFound ); delete [] returnString; returnString = nextReturnString; if( lastFound ) { atLeastOneFound = true; } } *outFound = atLeastOneFound; return returnString; }
char *replaceTicketHash( char *inString ) { const char *keyA = "sequence_number="; const char *keyB = "ticket_hmac="; if( inString == NULL ) { return NULL; } else if( strstr( inString, keyA ) != NULL && strstr( inString, keyB ) != NULL ) { // present char *copy = stringDuplicate( inString ); char *startPointerA = strstr( copy, keyA ); char *startPointerB = strstr( copy, keyB ); char *hmacStart = &( startPointerB[ strlen( keyB ) ] ); int i = 0; while( hmacStart[i] != '\0' && ( ( hmacStart[i] >= '0' && hmacStart[i] <= '9' ) || ( hmacStart[i] >= 'A' && hmacStart[i] <= 'F' ) ) ) { i++; } // truncate here, after end of hmac hmacStart[i] = '\0'; // now startPointerA points to the full hash char *newHash = getTicketHash(); char found; char *newHashInPlace = replaceOnce( inString, startPointerA, newHash, &found ); delete [] newHash; delete [] copy; delete [] inString; return newHashInPlace; } else { // no hash present char *result = stringDuplicate( inString ); delete [] inString; return result; } }
// reads full request up to [END_REQUEST] // returns result WITHOUT [END_REQUEST] marker at end // returns NULL on failure static char *readFullRequest( Socket *inSock ) { char *readSoFar = stringDuplicate( "" ); char error = false; while( !checkForEndMarker( readSoFar ) && ! error ) { int numRead = inSock->receive( (unsigned char *)readBuffer, READ_SIZE, 0 ); if( numRead >= 0 ) { readBuffer[ numRead ] = '\0'; char *newReadSoFar = concatonate( readSoFar, readBuffer ); delete [] readSoFar; readSoFar = newReadSoFar; } else if( numRead != -2 ) { // not timeout, real error error = true; } } if( error ) { return NULL; } char found; char *message = replaceOnce( readSoFar, END_REQUEST, "", &found); delete [] readSoFar; char *trimmedMessage = trimWhitespace( message ); delete [] message; return trimmedMessage; }