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 8283917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:54:47+00:00 2026-06-08T10:54:47+00:00

My C++ knowledge stinks and I need to convert a C++ function the correct

  • 0

My C++ knowledge stinks and I need to convert a C++ function the correct way.

This is the code I have (extracted from http://www.sky.franken.de/doxy/xmlstorage/c++/base64_8cpp_source.html).

void* base64_decode(const char* s, size_t& data_len)
{
  size_t len = strlen(s);

  if (len % 4)
          throw Exception("invalid BASE64 string length");

  unsigned char* data = (unsigned char*) malloc(len/4*3);

  int n[4];
  unsigned char* q = (unsigned char*) data;

  for(const char*p=s; *p; ) {
          n[0] = POS(*p++);
          n[1] = POS(*p++);
          n[2] = POS(*p++);
          n[3] = POS(*p++);

          if (n[0]==-1 || n[1]==-1)
              throw Exception("invalid BASE64 encoding");

      if (n[2]==-1 && n[3]!=-1)
              throw Exception("invalid BASE64 encoding");

      q[0] = (n[0] << 2) + (n[1] >> 4);
      if (n[2] != -1) q[1] = ((n[1] & 15) << 4) + (n[2] >> 2);
      if (n[3] != -1) q[2] = ((n[2] & 3) << 6) + n[3];
      q += 3;
  }

       data_len = q-data - (n[2]==-1) - (n[3]==-1);

       return data;
   }

I need this previous function to have this header

void * base64_decode(const char* s, size_t * data_len)

so, you see that is a matter of a “&” and a “*” on the header. As you see, the first function has this

size_t& data_len

in the header, but I need it to accept the data length in the form

size_t * data_len

I don’t know how to convert the first in the second, or in other words, what kind of changes I will have to perform on the method to make it work with a data_len provided as “size_t *”

  • 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-06-08T10:54:50+00:00Added an answer on June 8, 2026 at 10:54 am
    void* base64_decode(const char* s, size_t* data_len_ptr)
    {
        size_t len = strlen(s);
    
        if (len % 4)
            [NSException raise:@"Invalid input in base64_decode" format:@"%d is an invalid length for an input string for BASE64 decoding", len];
    
        unsigned char* data = (unsigned char*) malloc(len/4*3);
    
        int n[4];
        unsigned char* q = (unsigned char*) data;
    
        for(const char*p=s; *p; )
        {
            n[0] = POS(*p++);
            n[1] = POS(*p++);
            n[2] = POS(*p++);
            n[3] = POS(*p++);
    
            if (n[0]==-1 || n[1]==-1)
                [NSException raise:@"Invalid input in base64_decode" format:@"Invalid BASE64 encoding"];
    
            if (n[2]==-1 && n[3]!=-1)
                [NSException raise:@"Invalid input in base64_decode" format:@"Invalid BASE64 encoding"];
    
            q[0] = (n[0] << 2) + (n[1] >> 4);
            if (n[2] != -1) q[1] = ((n[1] & 15) << 4) + (n[2] >> 2);
            if (n[3] != -1) q[2] = ((n[2] & 3) << 6) + n[3];
            q += 3;
        }
    
        // make sure that data_len_ptr is not null
        if (!data_len_ptr)
            [NSException raise:@"Invalid input in base64_decode" format:@"Invalid destination for output string length"];
    
        *data_len_ptr = q-data - (n[2]==-1) - (n[3]==-1);
    
        return data;
    }
    

    Calling the function would look like this:

    const char* string = /*Base-64 encoded string*/;
    size_t data_len;
    void* decoded_data = base64_decode(string, &data_len);
    // the length of the decoded data is now indicated by data_len
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My knowledge of code just doesn't go this deep, and I have no idea
My C++ knowledge stinks. I have a code provided by Apple where they, as
I have limited knowledge of jQuery...I need help with this specific instance of a
I have little knowledge on oracle. I tried to create a function like below.
My knowledge of matlab is merely on a need to know basis, so this
It's common knowledge that using System.Diagnostics.Process.Start is the way to launch a url from
You may need some knowledge of cricket to help with this one. Given the
My knowledge of JS is very limited and i need help in validating this
My knowledge of regex is very weak. So far I have manage to create
My knowledge of xml and php is somewhat basic but have been able to

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.