openbase 0.8.1

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.
Files changed (54) hide show
  1. data/History.txt +50 -0
  2. data/License.txt +20 -0
  3. data/Manifest.txt +53 -0
  4. data/README.txt +35 -0
  5. data/Rakefile +126 -0
  6. data/examples/example.rb +37 -0
  7. data/ext/CommAPI.c +643 -0
  8. data/ext/Headers/CommAPI.h +1 -0
  9. data/ext/Headers/NetClient.h +42 -0
  10. data/ext/Headers/NetClientLib.h +41 -0
  11. data/ext/Headers/OB_Memory.h +69 -0
  12. data/ext/Headers/OpenBaseAdmin.h +227 -0
  13. data/ext/Headers/OpenBaseConnection.h +337 -0
  14. data/ext/Headers/OpenBaseEncoding.h +302 -0
  15. data/ext/Headers/OpenBasePrepare.h +1 -0
  16. data/ext/Headers/OpenBaseSupport.h +1 -0
  17. data/ext/Headers/Platform_Carbon_Header.h +5 -0
  18. data/ext/Headers/Platform_Classic_Header.h +6 -0
  19. data/ext/Headers/Platform_Linux.h +4 -0
  20. data/ext/Headers/Platform_Mach_Header.h +4 -0
  21. data/ext/Headers/Platform_Windows.h +3 -0
  22. data/ext/Headers/conversion.h +1 -0
  23. data/ext/Headers/datetime.h +26 -0
  24. data/ext/Headers/longlong.h +46 -0
  25. data/ext/Headers/platform.h +67 -0
  26. data/ext/Headers/stringConversion.h +15 -0
  27. data/ext/NetClient.c +888 -0
  28. data/ext/OpenBaseAdmin.c +1884 -0
  29. data/ext/OpenBaseConnection.c +1841 -0
  30. data/ext/OpenBaseEncoding.c +993 -0
  31. data/ext/OpenBaseEncoding_DOS.c +1 -0
  32. data/ext/OpenBaseEncoding_ISO8859.c +1 -0
  33. data/ext/OpenBaseEncoding_MacOS.c +1 -0
  34. data/ext/OpenBaseEncoding_Windows.c +1150 -0
  35. data/ext/OpenBasePrepare.c +1 -0
  36. data/ext/OpenBaseSupport.c +1 -0
  37. data/ext/conversion.c +1 -0
  38. data/ext/datetime.c +816 -0
  39. data/ext/depend +1 -0
  40. data/ext/extconf.rb +10 -0
  41. data/ext/longlong.c +1 -0
  42. data/ext/openbase.c +980 -0
  43. data/ext/stringConversion.c +169 -0
  44. data/lib/ruby-openbase/version.rb +9 -0
  45. data/scripts/txt2html +67 -0
  46. data/setup.rb +1585 -0
  47. data/test/test_helper.rb +2 -0
  48. data/test/test_openbase.rb +241 -0
  49. data/website/index.html +114 -0
  50. data/website/index.txt +59 -0
  51. data/website/javascripts/rounded_corners_lite.inc.js +285 -0
  52. data/website/stylesheets/screen.css +133 -0
  53. data/website/template.rhtml +47 -0
  54. metadata +105 -0
@@ -0,0 +1,50 @@
1
+ === 0.8.1
2
+
3
+ * Converted the project to a rubygem using hoe & newgem
4
+ * Renamed README to README.txt for Hoe compatibility
5
+ * Renamed CHANGES to History.txt for Hoe compatibility
6
+ * Adjusted initialization routine for OpenBaseResult objects to prevent a certain memory error
7
+
8
+
9
+ === 0.8.0
10
+
11
+ Maintenance of this library is now being handled by Derrick Spell (derrick.spell@gmail.com)
12
+
13
+ * Complete rewrite of library in C
14
+ * now returns money columns as ruby BigDecimal
15
+
16
+
17
+ === 0.7.3
18
+
19
+ I received patches from Yuichi Fujishige and Derrick Spell. Thank you!
20
+
21
+ * Implemented OpenBase#insert_binary.
22
+ * Fixed memory bugs.
23
+ * Changed return value type of unique_row_id() to number.
24
+ * Changed to convert NULL to nil.
25
+ * Fixed to convert integer type records correctly.
26
+
27
+
28
+ === 0.7.2
29
+
30
+ * Added the following methods.
31
+ * OpenBase#tables
32
+ * OpenBase#columns,
33
+ * OpenBase#relationships
34
+ * OpenBaseResult#free
35
+ * Supports the following data types.
36
+ * boolean
37
+ * binary
38
+ * text
39
+ * timestamp
40
+
41
+
42
+ === 0.7.1
43
+
44
+ * fixed to return value of money type as string.
45
+
46
+
47
+ === 0.7.0
48
+
49
+ first release.
50
+
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2007 Derrick Spell
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,53 @@
1
+ History.txt
2
+ License.txt
3
+ Manifest.txt
4
+ README.txt
5
+ Rakefile
6
+ examples/example.rb
7
+ ext/CommAPI.c
8
+ ext/Headers/CommAPI.h
9
+ ext/Headers/NetClient.h
10
+ ext/Headers/NetClientLib.h
11
+ ext/Headers/OB_Memory.h
12
+ ext/Headers/OpenBaseAdmin.h
13
+ ext/Headers/OpenBaseConnection.h
14
+ ext/Headers/OpenBaseEncoding.h
15
+ ext/Headers/OpenBasePrepare.h
16
+ ext/Headers/OpenBaseSupport.h
17
+ ext/Headers/Platform_Carbon_Header.h
18
+ ext/Headers/Platform_Classic_Header.h
19
+ ext/Headers/Platform_Linux.h
20
+ ext/Headers/Platform_Mach_Header.h
21
+ ext/Headers/Platform_Windows.h
22
+ ext/Headers/conversion.h
23
+ ext/Headers/datetime.h
24
+ ext/Headers/longlong.h
25
+ ext/Headers/platform.h
26
+ ext/Headers/stringConversion.h
27
+ ext/NetClient.c
28
+ ext/OpenBaseAdmin.c
29
+ ext/OpenBaseConnection.c
30
+ ext/OpenBaseEncoding.c
31
+ ext/OpenBaseEncoding_DOS.c
32
+ ext/OpenBaseEncoding_ISO8859.c
33
+ ext/OpenBaseEncoding_MacOS.c
34
+ ext/OpenBaseEncoding_Windows.c
35
+ ext/OpenBasePrepare.c
36
+ ext/OpenBaseSupport.c
37
+ ext/conversion.c
38
+ ext/datetime.c
39
+ ext/depend
40
+ ext/extconf.rb
41
+ ext/longlong.c
42
+ ext/openbase.c
43
+ ext/stringConversion.c
44
+ lib/ruby-openbase/version.rb
45
+ scripts/txt2html
46
+ setup.rb
47
+ test/test_helper.rb
48
+ test/test_openbase.rb
49
+ website/index.html
50
+ website/index.txt
51
+ website/javascripts/rounded_corners_lite.inc.js
52
+ website/stylesheets/screen.css
53
+ website/template.rhtml
@@ -0,0 +1,35 @@
1
+ = Ruby/OpenBase - OpenBase Connectivity for Ruby
2
+
3
+ == What's Ruby/OpenBase?
4
+
5
+ Ruby/OpenBase is a ruby extension library to access OpenBase database from Ruby.
6
+
7
+
8
+ == Requirements
9
+
10
+ Version 0.8 of Ruby/OpenBase has the following requirements:
11
+ * Ruby 1.8.4 or later
12
+ * OpenBase 10.0.6 or later
13
+ * Mac OS X 10.4 or later
14
+
15
+
16
+ == Installation
17
+
18
+ % ruby setup.rb config
19
+ % ruby setup.rb setup
20
+ % sudo ruby setup.rb install
21
+
22
+
23
+ == License
24
+
25
+ Ruby/OpenBase is a free software distributed under the BSD license.
26
+
27
+
28
+ == Author
29
+
30
+ C translation and current maintenance provided by:
31
+ Derrick Spell <derrick.spell@gmail.com>
32
+
33
+ Original Cocoa library written by:
34
+ SUZUKI Tetsuya <suzuki@spice-of-life.net>
35
+
@@ -0,0 +1,126 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/clean'
4
+ require 'rake/testtask'
5
+ require 'rake/packagetask'
6
+ require 'rake/gempackagetask'
7
+ require 'rake/rdoctask'
8
+ require 'rake/contrib/rubyforgepublisher'
9
+ require 'fileutils'
10
+ require 'hoe'
11
+
12
+ include FileUtils
13
+ require File.join(File.dirname(__FILE__), 'lib', 'ruby-openbase','version')
14
+
15
+ AUTHOR = 'Derrick Spell' # can also be an array of Authors
16
+ EMAIL = "derrick.spell@gmail.com"
17
+ DESCRIPTION = "Provides access to OpenBase databases from Ruby."
18
+ GEM_NAME = 'openbase' # what ppl will type to install your gem
19
+
20
+ @config_file = "~/.rubyforge/user-config.yml"
21
+ @config = nil
22
+ def rubyforge_username
23
+ unless @config
24
+ begin
25
+ @config = YAML.load(File.read(File.expand_path(@config_file)))
26
+ rescue
27
+ puts <<-EOS
28
+ ERROR: No rubyforge config file found: #{@config_file}"
29
+ Run 'rubyforge setup' to prepare your env for access to Rubyforge
30
+ - See http://newgem.rubyforge.org/rubyforge.html for more details
31
+ EOS
32
+ exit
33
+ end
34
+ end
35
+ @rubyforge_username ||= @config["username"]
36
+ end
37
+
38
+ RUBYFORGE_PROJECT = 'ruby-openbase' # The unix name for your project
39
+ HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
40
+ DOWNLOAD_PATH = "http://rubyforge.org/projects/#{RUBYFORGE_PROJECT}"
41
+
42
+ NAME = "openbase"
43
+ REV = nil
44
+ # UNCOMMENT IF REQUIRED:
45
+ # REV = `svn info`.each {|line| if line =~ /^Revision:/ then k,v = line.split(': '); break v.chomp; else next; end} rescue nil
46
+ VERS = RubyOpenBase::VERSION::STRING + (REV ? ".#{REV}" : "")
47
+ CLEAN.include ['**/.*.sw?', '*.gem', '.config', '**/.DS_Store']
48
+ RDOC_OPTS = ['--quiet', '--title', 'ruby-openbase documentation',
49
+ "--opname", "index.html",
50
+ "--line-numbers",
51
+ "--main", "README",
52
+ "--inline-source"]
53
+
54
+ class Hoe
55
+ def extra_deps
56
+ @extra_deps.reject { |x| Array(x).first == 'hoe' }
57
+ end
58
+ end
59
+
60
+ # Generate all the Rake tasks
61
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
62
+ hoe = Hoe.new(GEM_NAME, VERS) do |p|
63
+ p.author = AUTHOR
64
+ p.description = DESCRIPTION
65
+ p.email = EMAIL
66
+ p.summary = DESCRIPTION
67
+ p.url = HOMEPATH
68
+ p.rubyforge_name = RUBYFORGE_PROJECT if RUBYFORGE_PROJECT
69
+ p.test_globs = ["test/**/test_*.rb"]
70
+ p.clean_globs |= CLEAN #An array of file patterns to delete on clean.
71
+
72
+ # == Optional
73
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
74
+ #p.extra_deps = [] # An array of rubygem dependencies [name, version], e.g. [ ['active_support', '>= 1.3.1'] ]
75
+ p.spec_extras = { # A hash of extra values to set in the gemspec.
76
+ :extensions => ["ext/extconf.rb"],
77
+ :autorequire => "openbase"
78
+ }
79
+ end
80
+
81
+ CHANGES = hoe.paragraphs_of('History.txt', 0..1).join("\n\n")
82
+ PATH = RUBYFORGE_PROJECT
83
+ hoe.remote_rdoc_dir = File.join(PATH.gsub(/^#{RUBYFORGE_PROJECT}\/?/,''), 'rdoc')
84
+
85
+ desc 'Generate website files'
86
+ task :website_generate do
87
+ Dir['website/**/*.txt'].each do |txt|
88
+ sh %{ ruby scripts/txt2html #{txt} > #{txt.gsub(/txt$/,'html')} }
89
+ end
90
+ end
91
+
92
+ desc 'Upload website files to rubyforge'
93
+ task :website_upload do
94
+ host = "#{rubyforge_username}@rubyforge.org"
95
+ remote_dir = "/var/www/gforge-projects/#{PATH}/"
96
+ local_dir = 'website'
97
+ sh %{rsync -aCv #{local_dir}/ #{host}:#{remote_dir}}
98
+ end
99
+
100
+ desc 'Generate and upload website files'
101
+ task :website => [:website_generate, :website_upload, :publish_docs]
102
+
103
+ desc 'Release the website and new gem version'
104
+ task :deploy => [:check_version, :website, :release] do
105
+ puts "Remember to create SVN tag:"
106
+ puts "svn copy svn+ssh://#{rubyforge_username}@rubyforge.org/var/svn/#{PATH}/trunk " +
107
+ "svn+ssh://#{rubyforge_username}@rubyforge.org/var/svn/#{PATH}/tags/REL-#{VERS} "
108
+ puts "Suggested comment:"
109
+ puts "Tagging release #{CHANGES}"
110
+ end
111
+
112
+ desc 'Runs tasks website_generate and install_gem as a local deployment of the gem'
113
+ task :local_deploy => [:website_generate, :install_gem]
114
+
115
+ task :check_version do
116
+ unless ENV['VERSION']
117
+ puts 'Must pass a VERSION=x.y.z release version'
118
+ exit
119
+ end
120
+ unless ENV['VERSION'] == VERS
121
+ puts "Please update your version.rb to match the release version, currently #{VERS}"
122
+ exit
123
+ end
124
+ end
125
+
126
+
@@ -0,0 +1,37 @@
1
+ $LOAD_PATH.unshift('ext')
2
+ require 'openbase'
3
+
4
+ # connects to database
5
+ ob = OpenBase.new('OBMovies', 'localhost', 'admin', '')
6
+
7
+ # scheme information
8
+ puts "* encoding = #{ob.encoding}"
9
+ tables = ob.tables
10
+ tables.reject! { |t| /\A_SYS_/ === t }
11
+ puts "* tables = #{tables.join(', ')}"
12
+ puts "* columns of MOVIE = #{ob.columns('MOVIE').join(', ')}"
13
+ puts "* relationships of MOVIE = #{ob.relationships('MOVIE').join(', ')}"
14
+
15
+ # insert binary
16
+ blob = '12345'
17
+ puts
18
+ print "* insert binary '#{blob}' for "
19
+ key = ob.insert_binary(blob)
20
+ puts key
21
+
22
+ # execute SQL statement
23
+ result = ob.execute "select t0.TITLE, t0.REVENUE, t1.NAME from MOVIE t0, STUDIO t1 where t0.STUDIO_ID = t1.STUDIO_ID order by t0.REVENUE ASC"
24
+
25
+ # show each row
26
+ puts
27
+ puts "* show infomation for the columns"
28
+ result.column_infos.each do |info|
29
+ puts "** #{info.table}.#{info.name} = #{info.type}"
30
+ end
31
+
32
+ puts
33
+ puts "* show rows"
34
+ result.each do |row|
35
+ p row
36
+ end
37
+
@@ -0,0 +1,643 @@
1
+ // ------------------------------------------------------------------------------
2
+ //
3
+ // Copyright (c) 1996-2006 OpenBase International Ltd.
4
+ // All rights reserved.
5
+ //
6
+ // ------------------------------------------------------------------------------
7
+ #include "platform.h"
8
+
9
+ #include <stdio.h>
10
+ #include <stdlib.h>
11
+ #include <string.h>
12
+ #include <time.h>
13
+
14
+ #include "CommAPI.h"
15
+ #include "OB_Memory.h"
16
+
17
+ #if MACOSX_CARBON
18
+ #include <strings.h>
19
+ #include <OpenTransport.h>
20
+ #endif
21
+
22
+ #ifdef WINNT
23
+ #include <wtypes.h>
24
+ #endif
25
+
26
+ #ifdef MACOSX_UNIX
27
+ #include <sys/socket.h>
28
+ #endif
29
+
30
+ #include "NetClient.h"
31
+ #include "OpenBaseConnection.h"
32
+ #include "OpenBaseSupport.h"
33
+
34
+
35
+ #define DISABLE_PRIVATE_TRANSMISSION
36
+
37
+ int _readDictionary(NetConnPtr conn);
38
+
39
+ int _readData(NetConnPtr conn);
40
+
41
+ // ------------------------------------------------------------------------------------------
42
+
43
+ #ifdef MACOS_CARBON
44
+
45
+ #ifdef TARGET_API_MAC_CARBON
46
+ char* MapAddressToName ( char* ipAddress, char* domainName,
47
+ OTClientContextPtr outClientContext)
48
+ #else
49
+ char* MapAddressToName ( char* ipAddress, char* domainName, OTClientContextPtr)
50
+ #endif
51
+
52
+ {
53
+ domainName[0] = '\0'; // assume failure
54
+ OSStatus err;
55
+
56
+ #ifdef TARGET_API_MAC_CARBON
57
+ InetSvcRef isRef = OTOpenInternetServicesInContext( kDefaultInternetServicesPath,
58
+ 0, &err, outClientContext);
59
+ #else
60
+ InetSvcRef isRef = OTOpenInternetServices(kDefaultInternetServicesPath, 0, &err);
61
+ #endif
62
+
63
+ if (!err)
64
+ {
65
+ InetHost ipAddr;
66
+ err = OTInetStringToHost(ipAddress,&ipAddr);
67
+ if (!err)
68
+ {
69
+ err = OTInetAddressToName(isRef,ipAddr,domainName);
70
+ if (err) domainName[0] = '\0'; // failure was indicated
71
+ }
72
+ }
73
+ return domainName;
74
+ }
75
+
76
+ #ifdef TARGET_API_MAC_CARBON
77
+ char* MapNameToAddress ( char* domainName, char* ipAddress,
78
+ OTClientContextPtr outClientContext)
79
+ #else
80
+ char* MapNameToAddress ( char* domainName, char* ipAddress, OTClientContextPtr)
81
+ #endif
82
+ {
83
+ ipAddress[0] = '\0'; // assume failure
84
+ OSStatus err;
85
+
86
+ #ifdef TARGET_API_MAC_CARBON
87
+ InetSvcRef isRef = OTOpenInternetServicesInContext( kDefaultInternetServicesPath,
88
+ 0, &err, outClientContext);
89
+ #else
90
+ InetSvcRef isRef = OTOpenInternetServices(kDefaultInternetServicesPath, 0, &err);
91
+ #endif
92
+ if (!err)
93
+ {
94
+ InetHostInfo hinfo;
95
+ err = OTInetStringToAddress(isRef,domainName,&hinfo);
96
+ if (!err) OTInetHostToString(hinfo.addrs[0],ipAddress);
97
+ }
98
+ return ipAddress;
99
+ }
100
+
101
+ // ------------------------------------------------------------------------------------------
102
+ #else
103
+ #ifdef WINNT
104
+
105
+ char* MapNameToAddress ( char* domainName, char* ipAddress )
106
+ {
107
+ unsigned long ulAddr;
108
+ struct hostent *hostEntP;
109
+
110
+ ipAddress[0] = '\0'; // assume failure
111
+
112
+ ulAddr = inet_addr((char*)domainName);
113
+
114
+ if (ulAddr != ((unsigned long)-1)) {
115
+ strcpy(ipAddress, domainName);
116
+ } else {
117
+ hostEntP = gethostbyname((char*)domainName);
118
+ if ((hostEntP != NULL) && (hostEntP->h_addr_list != NULL)) {
119
+ char* address_ptr = hostEntP->h_addr_list[0];
120
+ if (address_ptr != NULL) {
121
+ char* address = inet_ntoa(*(in_addr*) address_ptr);
122
+ if (address != NULL) {
123
+ strcpy(ipAddress, address);
124
+ }
125
+ }
126
+ }
127
+ }
128
+
129
+ return ipAddress;
130
+ }
131
+
132
+ #endif
133
+ #endif
134
+
135
+
136
+ // ------------------------------------------------------------------------------------------
137
+
138
+ // ------------------------------------------------------------------------------------------
139
+
140
+ int connectToPort(NetConnPtr conn, int portNumber,const char *hostName)
141
+ {
142
+ #ifdef MACOS_CARBON
143
+ EndpointRef ret = kOTInvalidEndpointRef;
144
+ TBind server_info;
145
+ TBind client_info;
146
+ unsigned char server_address[256];
147
+ unsigned char client_address[256];
148
+ OSStatus err = noErr;
149
+
150
+ server_info.addr.maxlen = 256;
151
+ server_info.addr.buf = server_address;
152
+ client_info.addr.maxlen = 256;
153
+ client_info.addr.buf = client_address;
154
+ #else
155
+
156
+ #ifdef WINNT
157
+ struct sockaddr_in client_address; // internet style socket address
158
+ struct sockaddr_in server_address; // internet style socket address
159
+ #else
160
+ struct sockaddr *client_address; // internet style socket address
161
+ struct sockaddr *server_address; // internet style socket address
162
+ #endif
163
+ int ret = -1;
164
+ // int client_len;
165
+ // int server_len;
166
+ #endif
167
+
168
+
169
+ #ifdef MACOS_CARBON
170
+ ret = netConnect(portNumber, hostName, conn->outClientContext);
171
+ if (ret == kOTInvalidEndpointRef)
172
+ {
173
+ conn->socketConnection = kOTInvalidEndpointRef; // ��dmr
174
+ return 0;
175
+ }
176
+ conn->socketConnection = ret;
177
+ #else
178
+ ret = netConnect(portNumber, hostName);
179
+ if (ret == -1) {
180
+ return 0;
181
+ }
182
+ conn->socketConnection = (int)ret;
183
+ #endif
184
+
185
+ #ifdef MACOS_CARBON
186
+ err = OTGetProtAddress(conn->socketConnection, &client_info, &server_info);
187
+ if (err == noErr)
188
+ {
189
+ sprintf(conn->serverAddress, "%u.%u.%u.%u", server_address[4],
190
+ server_address[5], server_address[6], server_address[7]);
191
+ sprintf(conn->clientAddress, "%u.%u.%u.%u", client_address[4],
192
+ client_address[5], client_address[6], client_address[7]);
193
+ }
194
+ #endif
195
+
196
+ #ifdef WINNT
197
+ client_len = sizeof(client_address);
198
+ if (getsockname(conn->socketConnection, (struct sockaddr*) &client_address, &client_len) == 0) {
199
+ sprintf(conn->clientAddress, "%u.%u.%u.%u",
200
+ client_address.sin_addr.S_un.S_un_b.s_b1,
201
+ client_address.sin_addr.S_un.S_un_b.s_b2,
202
+ client_address.sin_addr.S_un.S_un_b.s_b3,
203
+ client_address.sin_addr.S_un.S_un_b.s_b4 );
204
+
205
+ } else {
206
+ sprintf(conn->clientAddress, "Error...");
207
+ }
208
+
209
+ server_len = sizeof(server_address);
210
+ if (getpeername(conn->socketConnection, (struct sockaddr*) &server_address, &server_len) == 0) {
211
+ sprintf(conn->serverAddress, "%u.%u.%u.%u",
212
+ server_address.sin_addr.S_un.S_un_b.s_b1,
213
+ server_address.sin_addr.S_un.S_un_b.s_b2,
214
+ server_address.sin_addr.S_un.S_un_b.s_b3,
215
+ server_address.sin_addr.S_un.S_un_b.s_b4 );
216
+ } else {
217
+ sprintf(conn->clientAddress, "Error...");
218
+ }
219
+ #endif
220
+
221
+ return syncronizeVersion(conn);
222
+ }
223
+
224
+ // ------------------------------------------------------------------------------------------
225
+
226
+ // clearing data
227
+ void initComm(NetConnPtr conn)
228
+ {
229
+ int ct;
230
+
231
+ for (ct = 0; ct < OB_MAX_KEYS; ct++) {
232
+ conn->resultKeys[ct] = NULL;
233
+ conn->keyBufferSize[ct] = 0;
234
+ conn->resultValues[ct] = NULL;
235
+ conn->valueBufferSize[ct] = 0;
236
+ conn->resultLength[ct] = 0;
237
+ }
238
+
239
+ conn->didInit = YES;
240
+
241
+ conn->privateConnection = NO;
242
+ }
243
+
244
+ // ------------------------------------------------------------------------------------------
245
+
246
+ void clearComm(NetConnPtr conn)
247
+ {
248
+ if (0 == conn->didInit)
249
+ initComm(conn);
250
+ }
251
+
252
+
253
+ // ------------------------------------------------------------------------------------------
254
+
255
+ int ob_copyAndFixString(unsigned char **tostring, int *tosize, int position, const unsigned char *str, int length, unsigned char *codedmap)
256
+ {
257
+ char *strdest;
258
+
259
+ // make sure the string is long enough
260
+ if ((length + position) > (*tosize - 10)) {
261
+ *tosize = *tosize * 2;
262
+ // if doubling size isn't enough
263
+ if ((length + position) > (*tosize - 10)) {
264
+ *tosize = (length + position + 10);
265
+ }
266
+ // *tostring = (unsigned char *)realloc(*tostring, *tosize);
267
+ *tostring = (unsigned char *)ob_realloc(*tostring, *tosize);
268
+ }
269
+
270
+ strdest = (char *)*tostring + position;
271
+
272
+ // copy the size of the string to the buffer
273
+ ob_intToBytes((unsigned char *)strdest, length);
274
+ strdest = strdest + sizeof(int);
275
+
276
+ // copy the string itself to the buffer
277
+ if (!codedmap) {
278
+ memcpy(strdest, str, length);
279
+ } else { // encode during copy
280
+ int ct;
281
+
282
+ for (ct=0; ct != length; ct++) {
283
+ strdest[ct] = codedmap[(unsigned int)str[ct]];
284
+ }
285
+ }
286
+ strdest[length] = '\0';
287
+ //printf("SEND TOKEN(%d) = %s\n", length, strdest);
288
+ return (length + sizeof(int));
289
+ }
290
+
291
+ // ------------------------------------------------------------------------------------------
292
+
293
+ // extern int out ( const char* );
294
+
295
+ // SENDING INFORMATION TO SERVER
296
+ int prepareData(NetConnPtr conn, const char *data, int datalen,
297
+ const char *parameter, const char *action)
298
+ {
299
+ unsigned char **buffer = &(conn->dictionaryBuffer);
300
+
301
+ int *len = &(conn->dictionaryBufferSize);
302
+
303
+ unsigned char *privateEncriptionMap = conn->privateEncriptionMap;
304
+
305
+ int position = 0;
306
+
307
+ position = position + ob_copyAndFixString(buffer, len, position,
308
+ (const unsigned char*)"|data|", 6, privateEncriptionMap);
309
+ position = position + ob_copyAndFixString(buffer, len, position,
310
+ (const unsigned char*)action, strlen(action), privateEncriptionMap);
311
+ position = position + ob_copyAndFixString(buffer, len, position,
312
+ (const unsigned char*)parameter, strlen(parameter), privateEncriptionMap);
313
+ position = position + ob_copyAndFixString(buffer, len, position,
314
+ (const unsigned char*)data, datalen, privateEncriptionMap);
315
+ conn->dictionaryLength = position;
316
+
317
+ return position;
318
+ }
319
+
320
+ // ------------------------------------------------------------------------------------------
321
+
322
+ void prepareDictionary(NetConnPtr conn)
323
+ {
324
+ unsigned char *privateEncriptionMap = conn->privateEncriptionMap;
325
+ unsigned char **buffer = &(conn->dictionaryBuffer);
326
+ int *len = &(conn->dictionaryBufferSize);
327
+ int position = 0;
328
+
329
+ position = position + ob_copyAndFixString(buffer, len, position,
330
+ (const unsigned char*)"|dict|", 6, privateEncriptionMap);
331
+
332
+ conn->dictionaryLength = position;
333
+ }
334
+
335
+ // ------------------------------------------------------------------------------------------
336
+
337
+ void addDictionaryPair(NetConnPtr conn, const char *key, const char *value)
338
+ {
339
+ int position;
340
+ unsigned char *privateEncriptionMap = conn->privateEncriptionMap;
341
+ unsigned char **buffer = &(conn->dictionaryBuffer);
342
+ int *len = &(conn->dictionaryBufferSize);
343
+
344
+ position = conn->dictionaryLength;
345
+ position = position + ob_copyAndFixString( buffer, len, position, (const unsigned char*)key,
346
+ strlen(key), privateEncriptionMap);
347
+ position = position + ob_copyAndFixString( buffer, len, position, (const unsigned char*)value,
348
+ strlen(value), privateEncriptionMap);
349
+ conn->dictionaryLength = position;
350
+ }
351
+
352
+ // ------------------------------------------------------------------------------------------
353
+
354
+ int sendBuffer(NetConnPtr conn)
355
+ {
356
+ int terminator = -1;
357
+ int position;
358
+ unsigned char **buffer = &(conn->dictionaryBuffer);
359
+
360
+ // add terminator
361
+ position = conn->dictionaryLength;
362
+ ob_intToBytes((*buffer + position), terminator);
363
+ position = position + sizeof(int);
364
+ conn->dictionaryLength = position;
365
+
366
+ return netSndRaw(conn->dictionaryBuffer, conn->dictionaryLength, conn->socketConnection);
367
+ }
368
+
369
+ // ------------------------------------------------------------------------------------------
370
+
371
+ // RECEIVING FROM SERVER
372
+ int _readDictionary(NetConnPtr conn)
373
+ {
374
+ int ct;
375
+ int len = 0;
376
+ int flag = 0;
377
+
378
+ for (ct = 0;; ct++) {
379
+ if (conn->resultKeys[ct] == NULL) {
380
+ // conn->resultKeys[ct] = (unsigned char *)malloc(50);
381
+ conn->resultKeys[ct] = (unsigned char *)ob_malloc(50);
382
+ conn->keyBufferSize[ct] = 50;
383
+ }
384
+ if (conn->resultValues[ct] == NULL) {
385
+ // conn->resultValues[ct] = (unsigned char *)malloc(256);
386
+ conn->resultValues[ct] = (unsigned char *)ob_malloc(256);
387
+ conn->valueBufferSize[ct] = 256;
388
+ }
389
+
390
+ len = (int)netRcvToken(&(conn->resultKeys[ct]), &(conn->keyBufferSize[ct]),
391
+ conn->socketConnection, conn->privateDecriptionMap, &flag);
392
+ if (len == -2L) { // terminator found
393
+ break;
394
+ }
395
+ if (len == -1L) { // error
396
+ return -1;
397
+ }
398
+
399
+ len = (int)netRcvToken(&(conn->resultValues[ct]), &(conn->valueBufferSize[ct]),
400
+ conn->socketConnection, conn->privateDecriptionMap, &flag);
401
+
402
+ if ((len == -1L) || (len == -2L)) { // error
403
+ return -1;
404
+ }
405
+ conn->resultLength[ct] = len;
406
+ }
407
+
408
+ conn->numberOfKeys = ct;
409
+ conn->resultLength[ct] = 0;
410
+ return 1;
411
+ }
412
+
413
+ // ------------------------------------------------------------------------------------------
414
+
415
+ int _readData(NetConnPtr conn)
416
+ {
417
+ int len = 0;
418
+ int flag = 0;
419
+
420
+ if (conn->resultKeys[0] == NULL) {
421
+ // conn->resultKeys[0] = (unsigned char *)malloc(256);
422
+ conn->resultKeys[0] = (unsigned char *)ob_malloc(256);
423
+ conn->keyBufferSize[0] = 256;
424
+ }
425
+ if (conn->resultValues[0] == NULL) {
426
+ // conn->resultValues[0] = (unsigned char *)malloc(256);
427
+ conn->resultValues[0] = (unsigned char *)ob_malloc(256);
428
+ conn->valueBufferSize[0] = 256;
429
+ }
430
+
431
+ if (conn->resultKeys[1] == NULL) {
432
+ // conn->resultKeys[1] = (unsigned char *)malloc(256);
433
+ conn->resultKeys[1] = (unsigned char *)ob_malloc(256);
434
+ conn->keyBufferSize[1] = 256;
435
+ }
436
+ if (conn->resultValues[1] == NULL) {
437
+ // conn->resultValues[1] = (unsigned char *)malloc(256);
438
+ conn->resultValues[1] = (unsigned char *)ob_malloc(256);
439
+ conn->valueBufferSize[1] = 256;
440
+ }
441
+
442
+ if (conn->resultKeys[2] == NULL) {
443
+ // conn->resultKeys[2] = (unsigned char *)malloc(256);
444
+ conn->resultKeys[2] = (unsigned char *)ob_malloc(256);
445
+ conn->keyBufferSize[2] = 256;
446
+ }
447
+
448
+ if (conn->resultValues[2] == NULL) {
449
+ // conn->resultValues[2] = (unsigned char *)malloc(256);
450
+ conn->resultValues[2] = (unsigned char *)ob_malloc(256);
451
+ conn->valueBufferSize[2] = 256;
452
+ }
453
+
454
+ // only to get terminator
455
+ if (conn->resultValues[3] == NULL) {
456
+ // conn->resultValues[3] = (unsigned char *)malloc(256);
457
+ conn->resultValues[3] = (unsigned char *)ob_malloc(256);
458
+ conn->valueBufferSize[3] = 256;
459
+ }
460
+
461
+ strcpy((char*)conn->resultKeys[0],"action");
462
+ len = (int)netRcvToken(&(conn->resultValues[0]), &(conn->valueBufferSize[0]),
463
+ conn->socketConnection, conn->privateDecriptionMap, &flag);
464
+ if ((len == -1L) || (len == -2L)) { // error
465
+ return -1;
466
+ }
467
+ conn->resultLength[0] = len;
468
+
469
+ strcpy((char*)conn->resultKeys[1],"parameter");
470
+ len = (int)netRcvToken(&(conn->resultValues[1]), &(conn->valueBufferSize[1]),
471
+ conn->socketConnection, conn->privateDecriptionMap, &flag);
472
+ if ((len == -1L) || (len == -2L)) { // error
473
+ return -1;
474
+ }
475
+ conn->resultLength[1] = len;
476
+
477
+ strcpy((char*)conn->resultKeys[2],"data");
478
+ len = (int)netRcvToken(&(conn->resultValues[2]), &(conn->valueBufferSize[2]),
479
+ conn->socketConnection, conn->privateDecriptionMap, &flag);
480
+ if ((len == -1L) || (len == -2L)) { // error
481
+ return -1;
482
+ }
483
+ conn->resultLength[2] = len;
484
+
485
+ // this is only to get terminator
486
+ len = (int)netRcvToken(&(conn->resultValues[3]), &(conn->valueBufferSize[3]),
487
+ conn->socketConnection, conn->privateDecriptionMap, &flag);
488
+ if (len != -2L) { // must be a terminator
489
+ return -1;
490
+ }
491
+
492
+
493
+ conn->resultLength[3] = 0;
494
+ conn->numberOfKeys = 3;
495
+ return 1;
496
+ }
497
+
498
+ // ------------------------------------------------------------------------------------------
499
+
500
+ int readResult(NetConn *conn)
501
+ {
502
+ char *header = (char*)ob_malloc(10);
503
+ int headerLength = 10;
504
+ int ret = 0;
505
+ int len = 0;
506
+ int flag = 0;
507
+
508
+ clearComm(conn);
509
+
510
+ len = (int)netRcvToken((unsigned char **)&header, &headerLength, conn->socketConnection, conn->privateDecriptionMap, &flag);
511
+ if ((len == -1L) || (len == -2L)) { // error
512
+ ob_free(header);
513
+ return 0;
514
+ }
515
+
516
+ if (strcmp((char*)header,"|dict|")==0) {
517
+ ret = _readDictionary(conn);
518
+ } else if (strcmp((char*)header,"|data|")==0){
519
+ ret = _readData(conn);
520
+ }
521
+ ob_free(header);
522
+ return ret;
523
+ }
524
+
525
+
526
+ // ------------------------------------------------------------------------------------------
527
+
528
+
529
+ char *resultValueForKey(NetConnPtr conn, const char *key, int *valuelength)
530
+ {
531
+ int ct;
532
+
533
+ for (ct = 0; ct < conn->numberOfKeys; ct++) {
534
+ if (strcmp(key, (char*)conn->resultKeys[ct])==0) {
535
+ *valuelength = conn->resultLength[ct];
536
+ return (char*)conn->resultValues[ct];
537
+ }
538
+ }
539
+ *valuelength = 0;
540
+ return NULL;
541
+ }
542
+
543
+ // ------------------------------------------------------------------------------------------
544
+
545
+ static unsigned char *_decriptionKeyMap(unsigned char *keyString) //��dmr static!
546
+ {
547
+ // unsigned char *decriptMap = (unsigned char *)malloc(256);
548
+ unsigned char *decriptMap = (unsigned char *)ob_malloc(256);
549
+ int ct;
550
+ for (ct =0; ct < 256; ct++) {
551
+ decriptMap[keyString[ct]] = (unsigned char)ct;
552
+ }
553
+
554
+ return decriptMap;
555
+ }
556
+
557
+ // ------------------------------------------------------------------------------------------
558
+
559
+ int syncronizeVersion(NetConnPtr conn)
560
+ {
561
+ unsigned char buffer[20];
562
+ int ret = 0;
563
+ int flag = 0;
564
+
565
+ buffer[0] = '#';
566
+ buffer[1] = strlen(COMM_VERSION);
567
+ strcpy((char *)&buffer[2],COMM_VERSION);
568
+
569
+ ret = netSndRaw(buffer, 5, conn->socketConnection);
570
+ if (!ret)
571
+ {
572
+ netClose(conn->socketConnection);
573
+ #ifdef MACOS_CARBON
574
+ conn->socketConnection = kOTInvalidEndpointRef;
575
+ #endif
576
+ return 0;
577
+ }
578
+ if (OBSocketRead(conn->socketConnection, (unsigned char *)buffer, 1, &flag) != 1)
579
+ {
580
+ netClose(conn->socketConnection);
581
+ #ifdef MACOS_CARBON
582
+ conn->socketConnection = kOTInvalidEndpointRef;
583
+ #endif
584
+ return 0;
585
+ }
586
+ if (*buffer == 1) {
587
+ return 1;
588
+ }
589
+ netClose(conn->socketConnection);
590
+
591
+ #ifdef MACOS_CARBON
592
+ conn->socketConnection = kOTInvalidEndpointRef;
593
+ #endif
594
+
595
+ return 0;
596
+ }
597
+
598
+ // ------------------------------------------------------------------------------------------
599
+
600
+ void sendExitSignal(NetConnPtr conn)
601
+ {
602
+ unsigned char *privateEncriptionMap = conn->privateEncriptionMap;
603
+ unsigned char **buffer = &(conn->dictionaryBuffer);
604
+ int *len = &(conn->dictionaryBufferSize);
605
+ int position = 0;
606
+
607
+ position = position + ob_copyAndFixString(buffer, len, position,
608
+ (const unsigned char*)"|exit|", 6, privateEncriptionMap);
609
+
610
+ conn->dictionaryLength = position;
611
+ netSndRaw(conn->dictionaryBuffer, conn->dictionaryLength, conn->socketConnection);
612
+
613
+ return;
614
+ }
615
+
616
+ // ------------------------------------------------------------------------------------------
617
+
618
+ void startPrivateTransmission(NetConnPtr conn)
619
+ {
620
+ unsigned char *value;
621
+ int len = 0;
622
+
623
+ prepareDictionary(conn);
624
+ addDictionaryPair(conn, "action", "privateConnection");
625
+ sendBuffer(conn);
626
+ if (!readResult(conn)) {
627
+ return;
628
+ }
629
+
630
+ value = (unsigned char *)resultValueForKey(conn, "result", &len);
631
+ if (!value || strcmp((const char *)value,(const char*)"no") == 0 || len < 255) {
632
+ return;
633
+ }
634
+
635
+ // conn->privateEncriptionMap = (unsigned char *)malloc(256);
636
+ conn->privateEncriptionMap = (unsigned char *)ob_malloc(256);
637
+ conn->privateEncriptionMap[0] = '\0';
638
+ memcpy((conn->privateEncriptionMap + 1), value, 255);
639
+ conn->privateDecriptionMap = (unsigned char *)_decriptionKeyMap(conn->privateEncriptionMap);
640
+ conn->privateConnection = YES;
641
+ }
642
+
643
+ // ------------------------------------------------------------------------------------------