Skip to content

Instantly share code, notes, and snippets.

@bpoweski
Last active August 29, 2015 14:10
Show Gist options
  • Save bpoweski/3450b21eaa45bdb23f1e to your computer and use it in GitHub Desktop.
Save bpoweski/3450b21eaa45bdb23f1e to your computer and use it in GitHub Desktop.
require "formula"
class Dynomite < Formula
homepage "https://github.com/Netflix/dynomite"
head "https://github.com/Netflix/dynomite.git"
option "enable-debug", "Debug mode with assertion panics enabled"
depends_on :automake => :build
depends_on :autoconf => :build
depends_on :libtool => :build
patch do
url "https://gist.githubusercontent.com/bpoweski/3450b21eaa45bdb23f1e/raw/8f8d0cff5caf37ead68e9f4feb4bd675659213ae/fmemopen.patch"
sha1 "413aa6e0f2fa21c300988e834cb1471cc28b0427"
end
def install
system "autoreconf", "-fvi"
args = ["--prefix=#{prefix}"]
if build.include? "enable-debug"
ENV['CFLAGS'] += "-ggdb3 -O0"
args << "--enable-debug=full"
end
system "./configure", *args
system "make"
system "make install"
end
end
diff --git a/src/Makefile.am b/src/Makefile.am
index 91b7fcc..b963d46 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -63,6 +63,7 @@ dynomite_SOURCES = \
dyn_queue.h \
dyn_gossip.c dyn_gossip.h \
dyn_dict.c dyn_dict.h \
+ fmemopen.c fmemopen.h \
dynomite.c
dynomite_LDADD = $(top_builddir)/src/hashkit/libhashkit.a
diff --git a/src/dyn_crypto.c b/src/dyn_crypto.c
index 0c195db..5720ff9 100644
--- a/src/dyn_crypto.c
+++ b/src/dyn_crypto.c
@@ -16,6 +16,8 @@
#include "dyn_crypto.h"
#include "dyn_server.h"
+#include "fmemopen.h"
+
static EVP_CIPHER *aes_cipher;
static RSA *rsa;
diff --git a/src/fmemopen.c b/src/fmemopen.c
new file mode 100644
index 0000000..11dd4eb
--- /dev/null
+++ b/src/fmemopen.c
@@ -0,0 +1,106 @@
+//
+// Copyright 2011-2014 NimbusKit
+// Originally ported from https://github.com/ingenuitas/python-tesseract/blob/master/fmemopen.c
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/mman.h>
+
+struct fmem {
+ size_t pos;
+ size_t size;
+ char *buffer;
+};
+typedef struct fmem fmem_t;
+
+static int readfn(void *handler, char *buf, int size) {
+ fmem_t *mem = handler;
+ size_t available = mem->size - mem->pos;
+
+ if (size > available) {
+ size = available;
+ }
+ memcpy(buf, mem->buffer + mem->pos, sizeof(char) * size);
+ mem->pos += size;
+
+ return size;
+}
+
+static int writefn(void *handler, const char *buf, int size) {
+ fmem_t *mem = handler;
+ size_t available = mem->size - mem->pos;
+
+ if (size > available) {
+ size = available;
+ }
+ memcpy(mem->buffer + mem->pos, buf, sizeof(char) * size);
+ mem->pos += size;
+
+ return size;
+}
+
+static fpos_t seekfn(void *handler, fpos_t offset, int whence) {
+ size_t pos;
+ fmem_t *mem = handler;
+
+ switch (whence) {
+ case SEEK_SET: {
+ if (offset >= 0) {
+ pos = (size_t)offset;
+ } else {
+ pos = 0;
+ }
+ break;
+ }
+ case SEEK_CUR: {
+ if (offset >= 0 || (size_t)(-offset) <= mem->pos) {
+ pos = mem->pos + (size_t)offset;
+ } else {
+ pos = 0;
+ }
+ break;
+ }
+ case SEEK_END: pos = mem->size + (size_t)offset; break;
+ default: return -1;
+ }
+
+ if (pos > mem->size) {
+ return -1;
+ }
+
+ mem->pos = pos;
+ return (fpos_t)pos;
+}
+
+static int closefn(void *handler) {
+ free(handler);
+ return 0;
+}
+
+FILE *fmemopen(void *buf, size_t size, const char *mode) {
+ // This data is released on fclose.
+ fmem_t* mem = (fmem_t *) malloc(sizeof(fmem_t));
+
+ // Zero-out the structure.
+ memset(mem, 0, sizeof(fmem_t));
+
+ mem->size = size;
+ mem->buffer = buf;
+
+ // funopen's man page: https://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man3/funopen.3.html
+ return funopen(mem, readfn, writefn, seekfn, closefn);
+}
diff --git a/src/fmemopen.h b/src/fmemopen.h
new file mode 100644
index 0000000..ef52a26
--- /dev/null
+++ b/src/fmemopen.h
@@ -0,0 +1,52 @@
+//
+// Copyright 2011-2014 NimbusKit
+// Originally ported from https://github.com/ingenuitas/python-tesseract/blob/master/fmemopen.c
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+#ifndef FMEMOPEN_H_
+#define FMEMOPEN_H_
+
+#if defined __cplusplus
+extern "C" {
+#endif
+
+/**
+ * A BSD port of the fmemopen Linux method using funopen.
+ *
+ * man docs for fmemopen:
+ * http://linux.die.net/man/3/fmemopen
+ *
+ * man docs for funopen:
+ * https://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man3/funopen.3.html
+ *
+ * This method is ported from ingenuitas' python-tesseract project.
+ *
+ * You must call fclose on the returned file pointer or memory will be leaked.
+ *
+ * @param buf The data that will be used to back the FILE* methods. Must be at least
+ * @c size bytes.
+ * @param size The size of the @c buf data.
+ * @param mode The permitted stream operation modes.
+ * @return A pointer that can be used in the fread/fwrite/fseek/fclose family of methods.
+ * If a failure occurred NULL will be returned.
+ * @ingroup NimbusMemoryMappping
+ */
+FILE *fmemopen(void *buf, size_t size, const char *mode);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // #ifndef FMEMOPEN_H_
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment