{"id":477,"date":"2015-06-28T20:44:00","date_gmt":"2015-06-28T20:44:00","guid":{"rendered":"http:\/\/blogs.gentoo.org\/lu_zero\/?p=477"},"modified":"2015-06-28T20:44:00","modified_gmt":"2015-06-28T20:44:00","slug":"broken-endian","status":"publish","type":"post","link":"https:\/\/blogs.gentoo.org\/lu_zero\/2015\/06\/28\/broken-endian\/","title":{"rendered":"broken-endian"},"content":{"rendered":"<p>You wrote your code, you wrote the tests and everything seems working.<\/p>\n<p>Then you got somebody running your code on a <strong>big-endian<\/strong> machine and reports that <strong>EVERYTHING<\/strong> is broken.<\/p>\n<p>Usually most of the data is serialized to disk or wire as <strong>big-endian<\/strong>, most of cpu usually do the computation in <strong>little-endian<\/strong> (with MIPS and PowerPC as rare exception). If you assume the relationship between the data on-wire and data in the cpu registers is always the same you are bound to have problems (and it gets even worse if you decide to write the data down as little-endian to disk because swapping from cpu to disk feels <em>slow<\/em>, you are doing it <strong>wrong<\/strong>).<\/p>\n<h2>Checklist<\/h2>\n<p>The problem is mainly while reading or writing:<\/p>\n<ul>\n<li>Sometimes feels simpler to copy over some <strong>packed structure<\/strong> using the equivalent of <code>read(fd, &amp;amp;my_struct, sizeof(struct))<\/code>. if the struct contains anything different from byte-sized variables it <strong>won&#8217;t<\/strong> work, so is safe to say it won&#8217;t work at all. Gets even worse if you forgot to mark the structure as packed. <\/li>\n<li>Writing has the same issue, never try to directly write a structure or even 16bit integers w\/out making sure you get the expected endianess right.<\/li>\n<\/ul>\n<p>Mini-post written to recall what not to do (more examples later).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You wrote your code, you wrote the tests and everything seems working. Then you got somebody running your code on a big-endian machine and reports that EVERYTHING is broken. Usually most of the data is serialized to disk or wire as big-endian, most of cpu usually do the computation in little-endian (with MIPS and PowerPC &hellip; <a href=\"https:\/\/blogs.gentoo.org\/lu_zero\/2015\/06\/28\/broken-endian\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">broken-endian<\/span><\/a><\/p>\n","protected":false},"author":10,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[1],"tags":[20],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p1aGWH-7H","_links":{"self":[{"href":"https:\/\/blogs.gentoo.org\/lu_zero\/wp-json\/wp\/v2\/posts\/477"}],"collection":[{"href":"https:\/\/blogs.gentoo.org\/lu_zero\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.gentoo.org\/lu_zero\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.gentoo.org\/lu_zero\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.gentoo.org\/lu_zero\/wp-json\/wp\/v2\/comments?post=477"}],"version-history":[{"count":2,"href":"https:\/\/blogs.gentoo.org\/lu_zero\/wp-json\/wp\/v2\/posts\/477\/revisions"}],"predecessor-version":[{"id":480,"href":"https:\/\/blogs.gentoo.org\/lu_zero\/wp-json\/wp\/v2\/posts\/477\/revisions\/480"}],"wp:attachment":[{"href":"https:\/\/blogs.gentoo.org\/lu_zero\/wp-json\/wp\/v2\/media?parent=477"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.gentoo.org\/lu_zero\/wp-json\/wp\/v2\/categories?post=477"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.gentoo.org\/lu_zero\/wp-json\/wp\/v2\/tags?post=477"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}