{"id":1860,"date":"2010-03-17T18:20:18","date_gmt":"2010-03-17T08:20:18","guid":{"rendered":"http:\/\/www.flamingspork.com\/blog\/?p=1860"},"modified":"2014-10-08T09:16:07","modified_gmt":"2014-10-07T23:16:07","slug":"a-md5-stored-procedure-for-drizzle-in-c","status":"publish","type":"post","link":"https:\/\/www.flamingspork.com\/blog\/2010\/03\/17\/a-md5-stored-procedure-for-drizzle-in-c\/","title":{"rendered":"A MD5 stored procedure for Drizzle&#8230; in C"},"content":{"rendered":"<p>So, just in case <a href=\"http:\/\/www.flamingspork.com\/blog\/2010\/03\/17\/stored-proceduresfunctions-for-drizzle\/\">that wasn&#8217;t evil enough<\/a> for you&#8230; perhaps you have something you want to know the MD5 checksum of. So, you could just do this:<\/p>\n<pre>drizzle&gt; select md5('Hello World!');\r\n+----------------------------------+\r\n| md5('Hello World!')              |\r\n+----------------------------------+\r\n| ed076287532e86365e841e92bfc50d8c |\r\n+----------------------------------+\r\n1 row in set (0 sec)<\/pre>\n<p>But that is <strong>soooo boring<\/strong>.<\/p>\n<p>Since we have the SSL libs already loaded into <a href=\"http:\/\/www.drizzle.org\">Drizzle<\/a>, and using my very evil <a href=\"https:\/\/code.launchpad.net\/~stewart\/drizzle\/drizzle-libtcc-function\">libtcc plugin<\/a>&#8230; we could just implement it in C. We can even use malloc!<\/p>\n<p><code>\u00ef\u00bb\u00bf\u00ef\u00bb\u00bfdrizzle&gt; SELECT LIBTCC(\"#include &lt;string.h&gt;\\n#include &lt;stdlib.h&gt;\\n#include &lt;openssl\/md5.h&gt;\\nint foo(char* s) { char *a = malloc(100); MD5_CTX context; unsigned char digest[16]; MD5_Init(&amp;context); strcpy(a,\\\"Hello World!\\\"); MD5_Update(&amp;context, a, strlen(a)); MD5_Final(digest, &amp;context); snprintf(s, 33, \\\"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\\\", digest[0], digest[1], digest[2], digest[3],digest[4], digest[5], digest[6], digest[7],digest[8], digest[9], digest[10], digest[11],digest[12], digest[13], digest[14], digest[15]); free(a); return 32; }\") AS RESULT;<br \/>\n<\/code><\/p>\n<pre>+----------------------------------+\r\n| RESULT                           |\r\n+----------------------------------+\r\n| ed076287532e86365e841e92bfc50d8c | \r\n+----------------------------------+\r\n1 row in set (0.01 sec)\r\n\r\n<\/pre>\n<p>Currently the parameter is static in the C version due to me not having&#8230; well.. done a good job implementing the calling of C code.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So, just in case that wasn&#8217;t evil enough for you&#8230; perhaps you have something you want to know the MD5 checksum of. So, you could just do this: drizzle&gt; select md5(&#8216;Hello World!&#8217;); +&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+ | md5(&#8216;Hello World!&#8217;) | +&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+ | ed076287532e86365e841e92bfc50d8c &hellip; <a href=\"https:\/\/www.flamingspork.com\/blog\/2010\/03\/17\/a-md5-stored-procedure-for-drizzle-in-c\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[76,75],"tags":[138,70,262,265,266,260],"class_list":["post-1860","post","type-post","status-publish","format-standard","hentry","category-code","category-drizzle-work-et-al","tag-c","tag-drizzle","tag-evil","tag-md5","tag-ssl","tag-stored-function"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5a6n8-u0","jetpack-related-posts":[{"id":1863,"url":"https:\/\/www.flamingspork.com\/blog\/2010\/03\/17\/one-last-bit-of-evil\/","url_meta":{"origin":1860,"position":0},"title":"One last bit of evil&#8230;.","author":"Stewart Smith","date":"2010-03-17","format":false,"excerpt":"You can store things for later! drizzle> select libtcc(\"#include <string.h>\\n#include <stdlib.h>\\nint foo(char* s) { char *a= malloc(1000); return snprintf(s, 100, \\\"%p\\\", a); }\") as RESULT; +-----------+ | RESULT | +-----------+ | 0x199c610 | +-----------+ 1 row in set (0 sec) drizzle> select libtcc(\"#include <string.h>\\n#include <stdlib.h>\\nint foo(char* s) { char *a=\u2026","rel":"","context":"In &quot;code&quot;","block_context":{"text":"code","link":"https:\/\/www.flamingspork.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1855,"url":"https:\/\/www.flamingspork.com\/blog\/2010\/03\/17\/stored-proceduresfunctions-for-drizzle\/","url_meta":{"origin":1860,"position":1},"title":"Stored Procedures\/Functions for Drizzle","author":"Stewart Smith","date":"2010-03-17","format":false,"excerpt":"Previously, in \"Thoughts on Thoughts on Drizzle\" I theorized that one of the major reasons why we did not see lots of people jumping at stored procedures in MySQL was that it wasn't in their native language (for lack of a better term). We've seen External Language Stored Procedures for\u2026","rel":"","context":"In &quot;code&quot;","block_context":{"text":"code","link":"https:\/\/www.flamingspork.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1612,"url":"https:\/\/www.flamingspork.com\/blog\/2009\/04\/16\/drizzle-low-hanging-fruit\/","url_meta":{"origin":1860,"position":2},"title":"Drizzle low-hanging-fruit","author":"Stewart Smith","date":"2009-04-16","format":false,"excerpt":"We have an ongoing Drizzle milestone called low-hanging-fruit. The idea is that when there's something that\u00c2\u00a0 could be done, but we don't quite have the time to do it immediately, we'll add a low-hanging-fruit blueprint so that people looking to get a start on the codebase and contributing code to\u2026","rel":"","context":"In &quot;code&quot;","block_context":{"text":"code","link":"https:\/\/www.flamingspork.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1811,"url":"https:\/\/www.flamingspork.com\/blog\/2010\/03\/01\/writing-a-storage-engine-for-drizzle-part-1-plugin-basics\/","url_meta":{"origin":1860,"position":3},"title":"Writing A Storage Engine for Drizzle, Part 1: Plugin basics","author":"Stewart Smith","date":"2010-03-01","format":false,"excerpt":"So, you've decided to write a Storage Engine for Drizzle. This is excellent news! The API is continually being improved and if you've worked on a Storage Engine for MySQL, you'll notice quite a few differences in some areas. The first step is to create a skeleton StorageEngine plugin. You\u2026","rel":"","context":"In &quot;General&quot;","block_context":{"text":"General","link":"https:\/\/www.flamingspork.com\/blog\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3745,"url":"https:\/\/www.flamingspork.com\/blog\/2014\/05\/26\/efficiently-writing-to-a-log-file-from-multiple-threads\/","url_meta":{"origin":1860,"position":4},"title":"Efficiently writing to a log file from multiple threads","author":"Stewart Smith","date":"2014-05-26","format":false,"excerpt":"There's a pattern I keep seeing in threaded programs (or indeed multiple processes) writing to a common log file. This is more of an antipattern than a pattern, and is often found in code that has existed for years. Basically, it's having a mutex to control concurrent writing to the\u2026","rel":"","context":"In &quot;code&quot;","block_context":{"text":"code","link":"https:\/\/www.flamingspork.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1719,"url":"https:\/\/www.flamingspork.com\/blog\/2009\/10\/10\/how-many-cpu-cycles-does-a-sql-query-take-or-pagefaults-caused-or-l2-cache-misses-or-cpu-migrations\/","url_meta":{"origin":1860,"position":5},"title":"How many CPU cycles does a SQL query take? (or pagefaults caused&#8230; or L2 cache misses&#8230; or CPU migrations&#8230;)","author":"Stewart Smith","date":"2009-10-10","format":false,"excerpt":"I like profilers. I use them when trying to make software (such as Drizzle) faster. Many profilers suck - and pretty much all of them are impossible to attach to a running system. Two notable exceptions are oprofile and dtrace (for Linux and Solaris respectively). The downside of oprofile is\u2026","rel":"","context":"In &quot;drizzle&quot;","block_context":{"text":"drizzle","link":"https:\/\/www.flamingspork.com\/blog\/category\/work-et-al\/drizzle-work-et-al\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/posts\/1860","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/comments?post=1860"}],"version-history":[{"count":4,"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/posts\/1860\/revisions"}],"predecessor-version":[{"id":3835,"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/posts\/1860\/revisions\/3835"}],"wp:attachment":[{"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/media?parent=1860"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/categories?post=1860"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/tags?post=1860"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}