Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 820843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:32:37+00:00 2026-05-15T02:32:37+00:00

I have a program that reads arbitrary data from a file system and outputs

  • 0

I have a program that reads arbitrary data from a file system and outputs results in Unicode. The problem I am having is that sometimes filenames are valid Unicode and sometimes they aren’t. So I want a function that can validate a string (in C or C++) and tell me if it is a valid UTF-8 encoding. If it is not, I want to have the invalid characters escaped so that it will be a valid UTF-8 encoding. This is different than escaping for XML — I need to do that also. But first I need to be sure that the Unicode is right.

I’ve seen some code from which I could hack this, but I would rather use some working code if it exists.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T02:32:37+00:00Added an answer on May 15, 2026 at 2:32 am

    The following code is based on an IRI library I have been working on for awhile. Section 3.2 (“Converting URIs to IRIs”) of RFC 3987 deals with converting invalid UTF-8 octets to valid UTF-8.

    #define IS_IN_RANGE(c, f, l)    (((c) >= (f)) && ((c) <= (l)))
    
    int UTF8BufferToUTF32Buffer(char *Data, int DataLen, unsigned long *Buffer, int BufLen, int *Eaten)
    {
        if( Eaten )
        {
            *Eaten = 0;
        }
    
        int Result = 0;
    
        unsigned char b, b2;
        unsigned char *ptr = (unsigned char*) Data;
        unsigned long uc;
    
        int i = 0;
        int seqlen;
    
        while( i < DataLen )
        {
            if( (Buffer) && (!BufLen) )
                break;
    
            b = ptr[i];
    
            if( (b & 0x80) == 0 )
            {
                uc = (unsigned long)(b & 0x7F);
                seqlen = 1;
            }
            else if( (b & 0xE0) == 0xC0 )
            {
                uc = (unsigned long)(b & 0x1F);
                seqlen = 2;
            }
            else if( (b & 0xF0) == 0xE0 )
            {
                uc = (unsigned long)(b & 0x0F);
                seqlen = 3;
            }
            else if( (b & 0xF8) == 0xF0 )
            {
                uc = (unsigned long)(b & 0x07);
                seqlen = 4;
            }
            else
            {
                uc = 0;
                return -1;
            }
    
            if( (i+seqlen) > DataLen )
            {
                return -1;
            }
    
            for(int j = 1; j < seqlen; ++j)
            {
                b = ptr[i+j];
    
                if( (b & 0xC0) != 0x80 )
                {
                    return -1;
                }
            }
    
            switch( seqlen )
            {
                case 2:
                {
                    b = ptr[i];
    
                    if( !IS_IN_RANGE(b, 0xC2, 0xDF) )
                    {
                        return -1;
                    }
    
                    break;
                }
    
                case 3:
                {
                    b = ptr[i];
                    b2 = ptr[i+1];
    
                    if( ((b == 0xE0) && !IS_IN_RANGE(b2, 0xA0, 0xBF)) ||
                        ((b == 0xED) && !IS_IN_RANGE(b2, 0x80, 0x9F)) ||
                        (!IS_IN_RANGE(b, 0xE1, 0xEC) && !IS_IN_RANGE(b, 0xEE, 0xEF)) )
                    {
                        return -1;
                    }
    
                    break;
                }
    
                case 4:
                {
                    b = ptr[i];
                    b2 = ptr[i+1];
    
                    if( ((b == 0xF0) && !IS_IN_RANGE(b2, 0x90, 0xBF)) ||
                        ((b == 0xF4) && !IS_IN_RANGE(b2, 0x80, 0x8F)) ||
                        !IS_IN_RANGE(b, 0xF1, 0xF3) )
                    {
                        return -1;
                    }
    
                    break;
                }
            }
    
            for(int j = 1; j < seqlen; ++j)
            {
                uc = ((uc << 6) | (unsigned long)(ptr[i+j] & 0x3F));
            }
    
            if( Buffer )
            {
                *Buffer++ = uc;
                --BufLen;
            }
    
            ++Result;
            i += seqlen;
        }
    
        if( Eaten )
        {
            *Eaten = i;
        }
    
        return Result;
    }
    
    {
        std::string filename = "...";
    
        unsigned long ch;
        int eaten;
    
        std::string::size_type i = 0;
        while( i < filename.length() )
        {
            if( UTF8BufferToUTF32Buffer(&filename[i], filename.length()-i, &ch, 1, &eaten) == 1 )
            {
                i += eaten;
            }
            else
            {
                // replace the character at filename[i] with your chosen
                // escaping, and then increment i by the number of
                // characters used...
            }
        }
    }
    

    In your case, all you have to do is decide what kind of escaping you want to use. URIs/IRIs uses percent-encoding (“%NN”, where “NN” is the 2-digit hex value of an octet).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program that reads data from a file line-by-line. I would like
I have a program that reads from a file that grabs 4 bytes from
I have a program that reads input from a file. I am trying to
I have a program that reads a file, treat it and put the results
I want to have a program that reads metadata from an MP3 file. My
I have a small program that reads in a file and processes the data
I have a simple program that reads data from a PNG into a 2D
I have a small Java program that reads list of IPs from text file
I have a program that reads server information from a configuration file and would
I have a program that reads data from a .csv and creates a multimap

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.