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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T10:15:49+00:00 2026-06-03T10:15:49+00:00

I have a custom PHP extension which compares each byte in a binary data

  • 0

I have a custom PHP extension which compares each byte in a binary data string. If both bytes at the same location in the file are on (or greater than 1) then the output of the same position will be on. Otherwise its off. The extension is as follows…

PHP_FUNCTION(compare_memory)
{
    char *memory1, *memory2;
    int memory1_length, memory2_length, return_length;
    int length;
    char *output;
    int x;

    zval *param;

    // MAKE SURE WE HAVE
    // string, (string_len,), string2, (string2_len) and length
    if(ZEND_NUM_ARGS() != 2) WRONG_PARAM_COUNT;

    // GET ARGUMENTS FROM PHP 
    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &memory1, &memory1_length, &memory2, &memory2_length) == FAILURE)
    {
        return;
    }

    if (memory1_length < memory2_length)
    {
        length = memory1_length;
        return_length = memory2_length;
    } else
    {
        length = memory2_length;
        return_length = memory1_length;
    }

    if ((output = emalloc(return_length * sizeof(int))) == NULL)
    {
        printf("Error on malloc\n");
        return;
    }

    // START COMPARING
    for(x=1;x<length;x++)
    {
        if (memory1[x] > 0 && memory2[x] == 1)
        {
            output[x] = memory1[x];
        }
    }

    RETVAL_STRINGL(output, return_length, 1);
    efree(output);
}   

I also have a PHP implementation as follows…

function compare_memory($data1, $data2)
{
    $pack_one = pack("c", 1);
    $pack_zero = pack("c", 0);
    $strlen_data1 = strlen($data1);
    $strlen_data2 = strlen($data2);
    if ($strlen_data1 > $strlen_data2)
    {
        $strlen = $strlen_data1;
    } else
    {
        $strlen = $strlen_data2;
    }
    $output = "";

    for($x=0;$x<$strlen;$x++)
    {
        if ($strlen_data1 > $x)
        {
            $arr = unpack("cc", $data1[$x]);
            $d1 = $arr["c"];
        } else
        {
            $d1 = 0;
        }

        if ($strlen_data2 > $x)
        {
            $arr = unpack("cc", $data2[$x]);
            $d2 = $arr["c"];
        } else
        {
            $d2 = 0;
        }

        if ($d1 > 0 && $d2 == 1)
        {
            $output .= pack("c", $d1);
        } else
        {
            $output .= $pack_zero;
        }
    }

    return $output;
}

This extension was working fine with PHP 5.0 (which I’m aware is very old). We’ve recently upgraded to 5.3 and this no longer works. However the PHP implementation does.

I imagine there have been a whole host of changes to the PHP Internal API between the versions, including “Parameter parsing API unification will cause some functions to behave more or less strict when it comes to type juggling”.

Can you see anything obvious in the PHP Extension that may cause it to not work in more recent versions of PHP? My knowledge of C (and the PHP API) is sparse.

  • 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-03T10:15:50+00:00Added an answer on June 3, 2026 at 10:15 am

    I’m not sure you can guarantee that emalloc is going to return you memory initialised with zeros. you might try using ecalloc instead.
    Or do it yourself:

    if ((output = emalloc(return_length * sizeof(int))) == NULL)
    {
        printf("Error on malloc\n");
        return;
    }
    memset(output, 0, return_length * sizeof(int) );
    

    Also, i don’t think emalloc ever returns NULL

    (Also i’m not sure why your for(x=1;x<length;x++) starts from 1 instead of zero but perhaps there’s a reason)

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

Sidebar

Related Questions

I have a custom built application framework written in PHP which I have been
I have a load of php templates which uses a custom translate function __,
I have a minimum age custom validator which is straight forward enough: The constraint(Minage.php)
I have build a custom PHP extension in C and included it in php.ini.
On Drupal 6, I used to have a custom PHP block which accessed arg(0),
I have created a custom PHP script which uploads information submitted by a form
I have a custom PHP function which executes a stored procedure and returns an
I have a script in which I am trying to load a custom php.ini
Let's say we have a custom PHP extension like: PHP_RSHUTDOWN_FUNCTION(myextension) { // How do
I have a custom PHP framework in which everything after the domain is sent

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.