Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:68736 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 57633 invoked from network); 30 Aug 2013 17:06:49 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 30 Aug 2013 17:06:49 -0000 Authentication-Results: pb1.pair.com header.from=rasmus@mindplay.dk; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=rasmus@mindplay.dk; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain mindplay.dk from 209.85.212.48 cause and error) X-PHP-List-Original-Sender: rasmus@mindplay.dk X-Host-Fingerprint: 209.85.212.48 mail-vb0-f48.google.com Received: from [209.85.212.48] ([209.85.212.48:56099] helo=mail-vb0-f48.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 17/25-32511-921D0225 for ; Fri, 30 Aug 2013 13:06:49 -0400 Received: by mail-vb0-f48.google.com with SMTP id w16so1457193vbf.7 for ; Fri, 30 Aug 2013 10:06:46 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc:content-type; bh=DYi5LKfUq6qSM5unDZ+PcAqOvsqIUIIhaQZdKnQyOQk=; b=Eug7GQxWRt2hJxYixE1xMYdDTRK6HXS4KK802g11WaenoEZ2uYH1v4VLbLqjoxYuiq Nu7ApLoq+14aExVrTpYeMcKMnFE8yAlGKt0TI9u6ANfYf48dq4nE8vqxEyVMhw2PgI+g h5uL6N/uPXWwcUdogVNla3nq+PpSZCtDlb43feVfKIR3ZkJtRPl9Jmg7g9ii3M1poemv 8ebYx1/YaU5SuogeDoUgALyUEQcyLFF36Gs9TH4i9QnNhzGc6/r6ytARkej/RYWDBkKW ikvkHfDBRzPeqWDvyHQLb8wDJpiX+Pup61ySpm1mNc4gJdUYq4fBRNYyue4aQ73jp4V6 kwcA== X-Gm-Message-State: ALoCoQmUO9ds3I3WZctwKbwF7JGPUEw7vckRv/u9pshaysLn9QDekjFa2hwxaOG6K4ntn2vS/YSm MIME-Version: 1.0 X-Received: by 10.58.196.132 with SMTP id im4mr1429161vec.28.1377882406737; Fri, 30 Aug 2013 10:06:46 -0700 (PDT) Received: by 10.58.190.194 with HTTP; Fri, 30 Aug 2013 10:06:46 -0700 (PDT) In-Reply-To: References: Date: Fri, 30 Aug 2013 13:06:46 -0400 Message-ID: To: Matthew Leverton Cc: PHP internals Content-Type: multipart/alternative; boundary=047d7b6d8cc865654d04e52d3f8f Subject: Re: [PHP-DEV] Re: crc32() and ip2long() return values From: rasmus@mindplay.dk (Rasmus Schultz) --047d7b6d8cc865654d04e52d3f8f Content-Type: text/plain; charset=ISO-8859-1 Perhaps this illustrates the problem better: $value = sprintf('%u', ip2long('255.255.255.0')); var_dump($value, (int) $value); $a = array(); $a[$value] = 'foo'; var_dump($a); Output on 64-bit: string(10) "4294967040" int(4294967040) array(1) { [4294967040]=> string(3) "foo" } No problem. But - output on 32-bit: string(10) "4294967040" int(2147483647) array(1) { ["4294967040"]=> string(3) "foo" } In this example, $value and (int) $value lead to incompatible results - that's if your database access layer will let you store a string in an integer column in the first place. Which, even if it will, when you get the integer value back from the database and cast it to an integer, you have the same problem again. If you query against the database using integer values you computed, you have problems. And so on... On Fri, Aug 30, 2013 at 12:27 PM, Matthew Leverton wrote: > On Fri, Aug 30, 2013 at 10:29 AM, Rasmus Schultz > wrote: > > No replies probably means no one cares. oh well. > > > > For the record, the examples I posted are wrong - the correct way to > > convert the long values consistently appears to be: > > > > list($v) = array_values(unpack('l', pack('l', > ip2long('255.255.255.0')))); > > > I had spotted the error, but didn't want to reply because I don't > really understand what you are getting at. > > The core issue is that PHP doesn't provide a 32-bit unsigned integer > on a 32-bit platform ... and/or that the size of integer changes > depending on the platform. But I doubt that is going to change any > time soon. Crippling 64-bit systems due to old, legacy 32-bit > platforms is shortsighted. > > What's wrong with the manual's approach? > > $checksum = sprintf("%u", crc32("The quick brown fox jumped over the > lazy dog.")); > > Are you going to do further mathematical operations on it? You can > take that string and stuff it into an uint32 field into a db without > an issue. > > At the end of the day, there's no getting around that PHP programmers > need to be aware of the difference between 32-bit and 64-bit systems > ... it affects far more than these two particular functions. > > But if these two functions are particularly bothersome, a better "fix" > IMO is just: > > $crc = crc32("12341234", CRC32_UINT32_STRING); > > Where the second parameter is CRC32_INT (default & current behavior), > CRC32_INT32 (always negative if high bit is set), CRC32_UINT32_STRING, > CRC32_HEX_STRING > > Forgive the poor names. > > -- > Matthew Leverton > --047d7b6d8cc865654d04e52d3f8f--