Ignore:
Timestamp:
Jan 10, 2009, 10:48:58 PM (13 years ago)
Author:
charles
Message:

(trunk libT) #1675: better utf8 validation of metainfo name, creator, and comment fields

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/ConvertUTF.c

    r7654 r7656  
    4040------------------------------------------------------------------------ */
    4141
    42 
     42#ifdef CVTUTF_DEBUG
     43 #include <stdio.h>
     44#endif
     45#include <string.h> /* strlen() */
     46#include <unistd.h> /* ssize_t */
    4347#include "ConvertUTF.h"
    44 #ifdef CVTUTF_DEBUG
    45 #include <stdio.h>
    46 #endif
    4748
    4849static const int halfShift  = 10; /* used for shifting by 10 bits */
     
    346347}
    347348
     349/**
     350 * This is a variation of isLegalUTF8Sequence() that behaves like g_utf8_validate().
     351 * In addition to knowing if the sequence is legal, it also tells you the last good character.
     352 */
     353Boolean
     354tr_utf8_validate( const char * str, ssize_t max_len, const char ** end )
     355{
     356    const UTF8* source = (const UTF8*) str;
     357    const UTF8* sourceEnd = source;
     358
     359    if( max_len == 0 )
     360        return true;
     361
     362    if( str == NULL )
     363        return false;
     364
     365    sourceEnd = source + ((max_len < 0) ? strlen(str) : (size_t)max_len);
     366
     367    if( source == sourceEnd )
     368    {
     369        if( end != NULL )
     370            *end = (const char*) source;
     371        return true;
     372    }
     373
     374    for( ;; )
     375    {
     376        const int length = trailingBytesForUTF8[*source] + 1;
     377        if (source + length > sourceEnd) {
     378            if( end != NULL )
     379                *end = (const char*) source;
     380            return false;
     381        }
     382        if (!isLegalUTF8(source, length)) {
     383            if( end != NULL )
     384                *end = (const char*) source;
     385            return false;
     386        }
     387        source += length;
     388        if (source >= sourceEnd) {
     389            if( end != NULL )
     390                *end = (const char*) source;
     391            return true;
     392        }
     393    }
     394
     395   
     396}
     397
     398
    348399/* --------------------------------------------------------------------- */
    349400
Note: See TracChangeset for help on using the changeset viewer.