staf4ruby 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Rakefile ADDED
@@ -0,0 +1,58 @@
1
+ ##############################################################################
2
+ # Copyright (c) 2013 Spectra Logic corp
3
+ # All rights reserved. This program and the accompanying materials
4
+ # are made available under the terms of the Eclipse Public License v1.0
5
+ # which accompanies this distribution, and is available at
6
+ # http://www.eclipse.org/legal/epl-v10.html
7
+ ##############################################################################
8
+
9
+ require 'rubygems'
10
+ require 'rubygems/package_task'
11
+ require 'rake/testtask'
12
+ require 'rake/clean'
13
+
14
+ file "lib/staf4ruby_ext.so" => Dir.glob("ext/*.{rb,c}") do
15
+ Dir.chdir("ext") do
16
+ ruby "extconf.rb"
17
+ sh "make"
18
+ end
19
+ cp "ext/staf4ruby_ext.so", "lib"
20
+ end
21
+
22
+
23
+ Rake::TestTask.new :test do |test|
24
+ test.verbose = false
25
+ test.test_files = ['test/test.rb']
26
+ end
27
+ task :test => "lib/staf4ruby_ext.so"
28
+
29
+ CLEAN.include('ext/Makefile')
30
+ CLEAN.include('ext/*.{o,so}')
31
+ CLOBBER.include('lib/*.{o,so}')
32
+
33
+ gem_spec = Gem::Specification.new do |s|
34
+ s.name=%q{staf4ruby}
35
+ s.version="0.1.0"
36
+ s.date=%q{2014-01-08}
37
+ s.authors=["Alan Somers"]
38
+ s.email=%q{alans@spectralogic.com}
39
+ s.extensions="ext/extconf.rb"
40
+ s.homepage="http://www.spectralogic.com"
41
+ s.rubyforge_project="none"
42
+ s.summary=%q{Ruby interface to STAF.}
43
+ s.description=%q{Ruby interface to the STAF C APIs.}
44
+ s.license='EPL'
45
+ # Install the Rakefile and the test files so we can test it on the
46
+ # target
47
+ s.files=["Rakefile",
48
+ "ext/extconf.rb",
49
+ "ext/staf4ruby_ext.c",
50
+ "lib/staf4ruby.rb",
51
+ "test/test.rb",
52
+ "test/test_helper.rb"]
53
+ end
54
+
55
+ Gem::PackageTask.new(gem_spec) do |pkg|
56
+ pkg.need_tar = false
57
+ pkg.need_zip = false
58
+ end
data/ext/extconf.rb ADDED
@@ -0,0 +1,19 @@
1
+ ##############################################################################
2
+ # Copyright (c) 2013 Spectra Logic corp
3
+ # All rights reserved. This program and the accompanying materials
4
+ # are made available under the terms of the Eclipse Public License v1.0
5
+ # which accompanies this distribution, and is available at
6
+ # http://www.eclipse.org/legal/epl-v10.html
7
+ ##############################################################################
8
+
9
+ require 'mkmf'
10
+
11
+ $CFLAGS += " -g3 -O0 -Wall -Werror"
12
+ $INCFLAGS << " " <<
13
+ "-I/usr/local/staf/include"
14
+ $LDFLAGS += " -L/usr/local/staf/lib -lSTAF"
15
+
16
+ puts "Using INCFLAGS='#{$INCFLAGS}'"
17
+
18
+
19
+ create_makefile("staf4ruby_ext")
@@ -0,0 +1,270 @@
1
+ /* vim: ts=8,sw=8,noexpandtab */
2
+ /*****************************************************************************
3
+ * Copyright (c) 2013 Spectra Logic corp
4
+ * All rights reserved. This program and the accompanying materials
5
+ * are made available under the terms of the Eclipse Public License v1.0
6
+ * which accompanies this distribution, and is available at
7
+ * http://www.eclipse.org/legal/epl-v10.html
8
+ ****************************************************************************/
9
+
10
+ /*
11
+ * Provide a simple interface to the STAF C API, without marshalling
12
+ */
13
+
14
+ #include <unistd.h>
15
+ #include <stdint.h>
16
+ #include <stdbool.h>
17
+ #include <stdlib.h>
18
+ #include <errno.h>
19
+ #include <STAF.h>
20
+ #include <STAFString.h>
21
+
22
+ #include <ruby.h>
23
+
24
+ /* Global data */
25
+ VALUE rb_mStaf;
26
+
27
+ typedef struct {
28
+ STAFHandle_t handle;
29
+ STAFSyncOption_t syncOption;
30
+ char* machine;
31
+ char* service;
32
+ char* request;
33
+ unsigned int requestLength;
34
+ STAFRC_t* rc;
35
+ char** result;
36
+ unsigned int* resultLen;
37
+ } staf_submit_args_t;
38
+
39
+ /***************************** Private C Methods *****************************/
40
+
41
+ static VALUE
42
+ rb_STAFSubmit_blocking(void* data)
43
+ {
44
+ staf_submit_args_t* args = (staf_submit_args_t*)data;
45
+
46
+ *(args->rc) = STAFSubmit2(args->handle, args->syncOption,
47
+ args->machine, args->service, args->request,
48
+ args->requestLength, args->result, args->resultLen);
49
+ if (*(args->result) == NULL)
50
+ *(args->result) = "";
51
+
52
+ return (Qnil);
53
+ }
54
+
55
+ static void
56
+ rb_STAFCancel(void* data)
57
+ {
58
+ /*
59
+ * This function ought to cancel any pending submit. But AFAIK there
60
+ * is no way to do that in the STAF API. Perhaps unregistering the
61
+ * handle would work. I don't know. TODO: implement me.
62
+ */
63
+ return;
64
+ }
65
+
66
+
67
+ /*************************** Staf Module Functions ***********************/
68
+ /*
69
+ * Register with STAF
70
+ * \param name a Ruby string used as the handle's name
71
+ * \return a ruby array of [return code, STAF handle]
72
+ */
73
+ static VALUE
74
+ rb_STAFRegister(VALUE rb_self, VALUE rb_name)
75
+ {
76
+ char* name;
77
+ STAFHandle_t handle = 0;
78
+ STAFRC_t rc;
79
+
80
+ name = StringValueCStr(rb_name);
81
+ rc = STAFRegister(name, &handle);
82
+
83
+ return (rb_ary_new3(2, UINT2NUM(rc), UINT2NUM(handle)));
84
+ }
85
+
86
+ /*
87
+ * Unregister from STAF
88
+ * \param rb_handle a STAF handle as returned by STAFRegister
89
+ * \return a STAF return code as a ruby number
90
+ */
91
+ static VALUE
92
+ rb_STAFUnregister(VALUE rb_self, VALUE rb_handle)
93
+ {
94
+ STAFHandle_t handle;
95
+ STAFRC_t rc;
96
+
97
+ handle = NUM2UINT(rb_handle);
98
+ rc = STAFUnRegister(handle);
99
+
100
+ return (UINT2NUM(rc));
101
+ }
102
+
103
+ /*
104
+ * Submit a STAF request
105
+ * \param rb_handle a STAF handle as returned by STAFRegister
106
+ * \param rb_syncOption a ruby number sync option as used by STAFSubmit2
107
+ * \param rb_machine a ruby string specifying the machine to use
108
+ * \param rb_request The STAF request as a ruby string
109
+ * \return a STAF return code as a ruby number
110
+ */
111
+ static VALUE
112
+ rb_STAFSubmit(VALUE rb_self, VALUE rb_handle, VALUE rb_syncOption, VALUE rb_machine, VALUE rb_service, VALUE rb_request)
113
+ {
114
+ STAFRC_t rc;
115
+ char* result;
116
+ unsigned int resultLen;
117
+ staf_submit_args_t args;
118
+ VALUE rb_result;
119
+
120
+ args.handle = NUM2UINT(rb_handle);
121
+ args.syncOption = NUM2UINT(rb_syncOption);
122
+ args.machine = StringValueCStr(rb_machine);
123
+ args.service = StringValueCStr(rb_service);
124
+ args.request = StringValueCStr(rb_request);
125
+ args.requestLength = RSTRING_LEN(rb_request);
126
+ args.rc = &rc;
127
+ args.result = &result;
128
+ args.resultLen = &resultLen;
129
+
130
+ rb_thread_blocking_region(rb_STAFSubmit_blocking, (void*) &args,
131
+ rb_STAFCancel, (void*) NULL);
132
+
133
+ rb_result = rb_ary_new3(3, UINT2NUM(rc), rb_str_new(result, resultLen),
134
+ UINT2NUM(resultLen));
135
+ if (result != NULL && *result != '\0')
136
+ STAFFree(args.handle, result);
137
+ return (rb_result);
138
+ }
139
+
140
+ /*
141
+ * Add Privacy delimiters to a string
142
+ * \param rb_data A ruby string
143
+ * \return a ruby array of [return code, output string]
144
+ */
145
+ static VALUE
146
+ rb_STAFAddPrivacyDelimiters(VALUE rb_self, VALUE rb_data)
147
+ {
148
+ STAFRC_t rc;
149
+ char* data;
150
+ STAFString_t input, result;
151
+ unsigned int osRC, len;
152
+ char* buf;
153
+ VALUE output;
154
+
155
+ data = StringValueCStr(rb_data);
156
+ rc = STAFStringConstructFromCurrentCodePage(&input, data, strlen(data),
157
+ &osRC);
158
+ // TODO: validate return codes
159
+ rc = STAFAddPrivacyDelimiters(input, &result);
160
+ rc = STAFStringToCurrentCodePage(result, &buf, &len, &osRC);
161
+ output = rb_ary_new3(2, UINT2NUM(rc), rb_str_new2(buf));
162
+ STAFStringFreeBuffer(buf, &osRC);
163
+ return output;
164
+ }
165
+
166
+ /*
167
+ * Escape privacy delimiters present in a string
168
+ * \param rb_data A ruby string
169
+ * \return a ruby array of [return code, output string]
170
+ */
171
+ static VALUE
172
+ rb_STAFEscapePrivacyDelimiters(VALUE rb_self, VALUE rb_data)
173
+ {
174
+ STAFRC_t rc;
175
+ char* data;
176
+ STAFString_t input, result;
177
+ unsigned int osRC, len;
178
+ char* buf;
179
+ VALUE output;
180
+
181
+ data = StringValueCStr(rb_data);
182
+ rc = STAFStringConstructFromCurrentCodePage(&input, data, strlen(data),
183
+ &osRC);
184
+ // TODO: validate return codes
185
+ rc = STAFEscapePrivacyDelimiters(input, &result);
186
+ rc = STAFStringToCurrentCodePage(result, &buf, &len, &osRC);
187
+ output = rb_ary_new3(2, UINT2NUM(rc), rb_str_new2(buf));
188
+ STAFStringFreeBuffer(buf, &osRC);
189
+ return output;
190
+ }
191
+
192
+ /*
193
+ * Mask delimited private data in a string
194
+ * \param rb_data A ruby string
195
+ * \return a ruby array of [return code, output string]
196
+ */
197
+ static VALUE
198
+ rb_STAFMaskPrivateData(VALUE rb_self, VALUE rb_data)
199
+ {
200
+ STAFRC_t rc;
201
+ char* data;
202
+ STAFString_t input, result;
203
+ unsigned int osRC, len;
204
+ char* buf;
205
+ VALUE output;
206
+
207
+ data = StringValueCStr(rb_data);
208
+ rc = STAFStringConstructFromCurrentCodePage(&input, data, strlen(data),
209
+ &osRC);
210
+ // TODO: validate return codes
211
+ rc = STAFMaskPrivateData(input, &result);
212
+ rc = STAFStringToCurrentCodePage(result, &buf, &len, &osRC);
213
+ output = rb_ary_new3(2, UINT2NUM(rc), rb_str_new2(buf));
214
+ STAFStringFreeBuffer(buf, &osRC);
215
+ return output;
216
+ }
217
+
218
+ /*
219
+ * Remove privacy delimiters in a string
220
+ * \param rb_data A ruby string
221
+ * \return a ruby array of [return code, output string]
222
+ */
223
+ static VALUE
224
+ rb_STAFRemovePrivacyDelimiters(VALUE rb_self, VALUE rb_data, VALUE rb_levels)
225
+ {
226
+ STAFRC_t rc;
227
+ char* data;
228
+ STAFString_t input, result;
229
+ unsigned int osRC, len;
230
+ char* buf;
231
+ VALUE output;
232
+ unsigned int numLevels;
233
+
234
+ data = StringValueCStr(rb_data);
235
+ numLevels = NUM2UINT(rb_levels);
236
+ // TODO: validate return codes
237
+ rc = STAFStringConstructFromCurrentCodePage(&input, data, strlen(data),
238
+ &osRC);
239
+ rc = STAFRemovePrivacyDelimiters(input, numLevels, &result);
240
+ rc = STAFStringToCurrentCodePage(result, &buf, &len, &osRC);
241
+ output = rb_ary_new3(2, UINT2NUM(rc), rb_str_new2(buf));
242
+ STAFStringFreeBuffer(buf, &osRC);
243
+ return output;
244
+ }
245
+
246
+
247
+ /*************************** Initialize the module ***************************/
248
+
249
+ void Init_staf4ruby_ext()
250
+ {
251
+ rb_mStaf = rb_define_module("Staf");
252
+
253
+ /* Constants */
254
+
255
+ /* module functions */
256
+ rb_define_module_function(rb_mStaf, "register",
257
+ rb_STAFRegister, 1);
258
+ rb_define_module_function(rb_mStaf, "submit", rb_STAFSubmit,
259
+ 5);
260
+ rb_define_module_function(rb_mStaf, "unregister",
261
+ rb_STAFUnregister, 1);
262
+ rb_define_module_function(rb_mStaf, "addPrivacyDelimiters",
263
+ rb_STAFAddPrivacyDelimiters, 1);
264
+ rb_define_module_function(rb_mStaf, "escapePrivacyDelimiters",
265
+ rb_STAFEscapePrivacyDelimiters, 1);
266
+ rb_define_module_function(rb_mStaf, "maskPrivateData",
267
+ rb_STAFMaskPrivateData, 1);
268
+ rb_define_module_function(rb_mStaf, "removePrivacyDelimiters",
269
+ rb_STAFRemovePrivacyDelimiters, 2);
270
+ }