Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:68733 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 53068 invoked from network); 30 Aug 2013 16:48:49 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 30 Aug 2013 16:48: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.46 cause and error) X-PHP-List-Original-Sender: rasmus@mindplay.dk X-Host-Fingerprint: 209.85.212.46 mail-vb0-f46.google.com Received: from [209.85.212.46] ([209.85.212.46:54575] helo=mail-vb0-f46.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 2D/14-32511-0FCC0225 for ; Fri, 30 Aug 2013 12:48:49 -0400 Received: by mail-vb0-f46.google.com with SMTP id p13so1456679vbe.5 for ; Fri, 30 Aug 2013 09:48:45 -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=8TO1uKo4chDdPLOPX2jEBlY9qi7yz9Y4JmeRi058hOY=; b=Qh79fsvDYDm+nO8lyyWsgXMq3djrkrIRGpJ3iuqFC17ggxwlRmAw+Hj+H1O24FKJ2U /FFkBsWxdj68bHMxncMj8kTflK2pD0X1Dw54KYOH7RQveQkFOqxrZl9JXVCrn5ukbq8D bnD4NaXqoToJ1fs0R19bxHZ+POGspHSKDRyCDmTuL2rjB6l2+OuykEacG3WnER8jzaxO 2IMn7UN1JA6fsuKIAeRoP0B9e6p21UADFie5cW/FSkF2pyO85XrjY30jceG+22FYLfCi kyIN5wdbD0UZc3J9AcJDBmrGCPxrfFSsJP2q8IHBL57GS330LhUYmJnvnWEDHMKxagQO mp0g== X-Gm-Message-State: ALoCoQkOmjcZjUwlBb3a0VyqSsU8HBqgplBHPfqDaE5kcwI/tjIk8v7E5rlLPWRttd7CaORx+EXO MIME-Version: 1.0 X-Received: by 10.220.47.10 with SMTP id l10mr914437vcf.32.1377881325707; Fri, 30 Aug 2013 09:48:45 -0700 (PDT) Received: by 10.58.190.194 with HTTP; Fri, 30 Aug 2013 09:48:45 -0700 (PDT) In-Reply-To: References: Date: Fri, 30 Aug 2013 12:48:45 -0400 Message-ID: To: Matthew Leverton Cc: PHP internals Content-Type: multipart/alternative; boundary=001a11c2a58cf6357804e52cfe66 Subject: Re: [PHP-DEV] Re: crc32() and ip2long() return values From: rasmus@mindplay.dk (Rasmus Schultz) --001a11c2a58cf6357804e52cfe66 Content-Type: text/plain; charset=ISO-8859-1 Matthew, To give another example: var_dump(array(sprintf('%u', ip2long('127.0.0.1')) => 'foo', sprintf('%u', ip2long('255.255.255.0')) => 'bar')); array(2) { [2130706433]=> string(3) "foo" ["4294967040"]=> string(3) "bar" } The keys are now two different types (string vs int) on 32-bit platforms, which leads to problems with strict comparison. Another issue is persistence - if you save these to unsigned integer columns in database, and your data access layer converts them back to integers on load, the values are going to change again depending on what platform you're on. To quote the specific case where I encountered this, I have an audit trail system that logs a lot of user activity - as an optimization, I hash and store certain keys using crc32() numeric values, since storing a 32-bit number is much cheaper (in terms of storage) than storing strings, as well as giving much faster queries. I encountered all of the problems mentioned while debugging an error in this system, and it seems completely bonkers to have to spend this much time on something that ought to be totally trivial, and would have been, if these functions returned consistent results on all platforms. Why would you consider a consistent function "crippled"? Look at the sheer number of comments on the crc32 manual page and tell me if you still think this works well for anybody... many of the comments (including mine!) aren't even correct and don't lead to predictable results... This should be easy but it is extremely hard. In my opinion, the function is "crippled" as it is... 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 > --001a11c2a58cf6357804e52cfe66--