rhodes 2.2.6 → 2.3.0.beta.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (486) hide show
  1. data/CHANGELOG +20 -0
  2. data/README.md +46 -0
  3. data/Rakefile +85 -80
  4. data/bin/rhodes +2 -2
  5. data/bin/rhogen +1 -1
  6. data/doc/application.txt +277 -0
  7. data/doc/bb-css.txt +209 -0
  8. data/doc/build.txt +785 -0
  9. data/doc/configuration.txt +236 -0
  10. data/doc/connect-to-web-services.txt +282 -0
  11. data/doc/contributing.txt +64 -0
  12. data/doc/css-framework.txt +144 -0
  13. data/doc/device-caps.txt +1152 -0
  14. data/doc/extensions.txt +528 -0
  15. data/doc/files.txt +4 -0
  16. data/doc/generator.txt +141 -0
  17. data/doc/install.txt +35 -0
  18. data/doc/introduction.txt +88 -0
  19. data/doc/rhom.txt +826 -0
  20. data/doc/standard-css.txt +392 -0
  21. data/doc/synchronization.txt +542 -0
  22. data/doc/test-log-debug.txt +148 -0
  23. data/doc/tutorial.txt +136 -0
  24. data/doc/ui.txt +766 -0
  25. data/installer/EnvVarUpdate.nsh +328 -0
  26. data/installer/README.html +81 -0
  27. data/installer/RHOSYNC-LICENSE.txt +675 -0
  28. data/installer/icon.ico +0 -0
  29. data/installer/instant-rhodes.nsi +427 -0
  30. data/lib/build/jake.rb +15 -1
  31. data/lib/extensions/barcode/ext/barcode/platform/android/jni/src/imageprovider.cpp +1 -1
  32. data/lib/extensions/barcode/ext/barcode/platform/bb/Barcode6.files +45 -0
  33. data/lib/extensions/barcode/ext/barcode/platform/bb/Rakefile +6 -0
  34. data/lib/extensions/barcode/ext/barcode/platform/bb/barcode6.jdp +92 -0
  35. data/lib/extensions/barcode/ext/barcode/platform/iphone/Barcode.xcodeproj/project.pbxproj +7 -4
  36. data/lib/extensions/barcode/ext/barcode/platform/iphone/Rakefile +2 -1
  37. data/lib/extensions/barcode/ext/barcode/platform/wm/src/wm_imageprovider.cpp +1 -1
  38. data/lib/extensions/digest-sha1/ext/build.bat +1 -0
  39. data/lib/extensions/digest-sha1/ext/digest-sha1.vcproj +335 -0
  40. data/lib/extensions/esri/ext/build +17 -0
  41. data/lib/extensions/esri/ext/build.bat +29 -0
  42. data/lib/extensions/esri/ext/esri/platform/iphone/Classes/ESRIViewFactory.h +7 -0
  43. data/lib/extensions/esri/ext/esri/platform/iphone/Classes/ESRIViewFactory.mm +58 -0
  44. data/lib/extensions/esri/ext/esri/platform/iphone/Classes/ESRIViewFactoryRegister.mm +22 -0
  45. data/lib/extensions/esri/ext/esri/platform/iphone/Classes/ESRIViewManager.h +21 -0
  46. data/lib/extensions/esri/ext/esri/platform/iphone/Classes/ESRIViewManager.mm +32 -0
  47. data/lib/extensions/esri/ext/esri/platform/iphone/Classes/InitESRI.c +5 -0
  48. data/lib/extensions/esri/ext/esri/platform/iphone/Classes/MapViewControllerESRI.h +143 -0
  49. data/lib/extensions/esri/ext/esri/platform/iphone/Classes/MapViewControllerESRI.m +919 -0
  50. data/lib/extensions/esri/ext/esri/platform/iphone/ESRI.xcodeproj/project.pbxproj +320 -0
  51. data/lib/extensions/esri/ext/esri/platform/iphone/ESRI_Prefix.pch +7 -0
  52. data/lib/extensions/esri/ext/esri/platform/iphone/Rakefile +76 -0
  53. data/lib/extensions/esri/ext.yml +3 -0
  54. data/lib/extensions/net-http/monitor.rb +265 -0
  55. data/lib/extensions/net-http/net/ftp.rb +981 -0
  56. data/lib/extensions/net-http/thread.rb +367 -0
  57. data/lib/extensions/rhocgi/cgi.rb +16 -0
  58. data/lib/extensions/rhospec/rhospec.rb +118 -0
  59. data/lib/framework/base64.rb +91 -0
  60. data/lib/framework/builtinME.rb +13 -10
  61. data/lib/framework/ostruct.rb +145 -0
  62. data/lib/framework/res/back_btn.wm.png +0 -0
  63. data/lib/framework/res/blue_pushpin.wm.png +0 -0
  64. data/lib/framework/res/blue_pushpin_small.wm.png +0 -0
  65. data/lib/framework/res/callout.wm.png +0 -0
  66. data/lib/framework/res/callout_link.wm.png +0 -0
  67. data/lib/framework/res/forward_btn.wm.png +0 -0
  68. data/lib/framework/res/home_btn.wm.png +0 -0
  69. data/lib/framework/res/options_btn.wm.png +0 -0
  70. data/lib/framework/res/refresh_btn.wm.png +0 -0
  71. data/lib/framework/rho/render.rb +5 -2
  72. data/lib/framework/rho/rho.rb +320 -50
  73. data/lib/framework/rho/rhoapplication.rb +48 -10
  74. data/lib/framework/rho/rhonativeviewmanager.rb +73 -0
  75. data/lib/framework/rho/rhosupport.rb +15 -1
  76. data/lib/framework/rho/rhotabbar.rb +29 -0
  77. data/lib/framework/rho/rhotoolbar.rb +17 -0
  78. data/lib/framework/rho/rhoutils.rb +22 -6
  79. data/lib/framework/rhodes.rb +1 -1
  80. data/lib/framework/rholang/rhomsg_de.rb +4 -3
  81. data/lib/framework/rholang/rhomsg_en.rb +1 -0
  82. data/lib/framework/rholang/rhomsg_es.rb +1 -0
  83. data/lib/framework/rholang/rhomsg_it.rb +1 -0
  84. data/lib/framework/rhom/rhom.rb +31 -10
  85. data/lib/framework/rhom/rhom_db_adapter.rb +33 -23
  86. data/lib/framework/rhom/rhom_object_factory.rb +465 -249
  87. data/lib/framework/rhom/rhom_source.rb +22 -7
  88. data/lib/framework/version.rb +1 -1
  89. data/lib/rhodes.rb +1 -1
  90. data/platform/android/Rhodes/AndroidManifest.xml +83 -20
  91. data/platform/android/Rhodes/gen/com/rhomobile/rhodes/R.java +5 -0
  92. data/platform/android/Rhodes/jni/include/rhodes/RhoClassFactory.h +2 -0
  93. data/platform/android/Rhodes/jni/include/rhodes/details/rhojava.inc +9 -5
  94. data/platform/android/Rhodes/jni/include/rhodes/jni/com_rhomobile_rhodes_RhodesAppOptions.h +53 -0
  95. data/platform/android/Rhodes/jni/include/rhodes/jni/com_rhomobile_rhodes_RhodesService.h +46 -56
  96. data/platform/android/Rhodes/jni/include/rhodes/jni/com_rhomobile_rhodes_SplashScreen.h +10 -0
  97. data/platform/android/Rhodes/jni/include/rhodes/jni/com_rhomobile_rhodes_mapview_MapView.h +93 -0
  98. data/platform/android/Rhodes/jni/include/rhodes/rhocryptimpl.h +38 -0
  99. data/platform/android/Rhodes/jni/include/rhodes.h +64 -8
  100. data/platform/android/Rhodes/jni/src/RhoClassFactory.cpp +6 -0
  101. data/platform/android/Rhodes/jni/src/alert.cpp +5 -11
  102. data/platform/android/Rhodes/jni/src/bluetooth.cpp +66 -29
  103. data/platform/android/Rhodes/jni/src/callbacks.cpp +24 -21
  104. data/platform/android/Rhodes/jni/src/camera.cpp +2 -6
  105. data/platform/android/Rhodes/jni/src/datetimepicker.cpp +13 -11
  106. data/platform/android/Rhodes/jni/src/event.cpp +9 -15
  107. data/platform/android/Rhodes/jni/src/fileapi.cpp +13 -27
  108. data/platform/android/Rhodes/jni/src/logconf.cpp +3 -3
  109. data/platform/android/Rhodes/jni/src/mapview.cpp +696 -16
  110. data/platform/android/Rhodes/jni/src/menu.cpp +3 -3
  111. data/platform/android/Rhodes/jni/src/nativebar.cpp +55 -4
  112. data/platform/android/Rhodes/jni/src/nativeview.cpp +33 -0
  113. data/platform/android/Rhodes/jni/src/phonebook.cpp +6 -11
  114. data/platform/android/Rhodes/jni/src/rhoconf.cpp +4 -4
  115. data/platform/android/Rhodes/jni/src/rhocryptimpl.cpp +87 -0
  116. data/platform/android/Rhodes/jni/src/rhodes.cpp +55 -34
  117. data/platform/android/Rhodes/jni/src/ringtones.cpp +2 -3
  118. data/platform/android/Rhodes/jni/src/signature.cpp +3 -5
  119. data/platform/android/Rhodes/jni/src/splashscreen.cpp +7 -0
  120. data/platform/android/Rhodes/jni/src/sslimpl.cpp +17 -20
  121. data/platform/android/Rhodes/jni/src/webview.cpp +13 -12
  122. data/platform/android/Rhodes/res/drawable/callout.png +0 -0
  123. data/platform/android/Rhodes/res/drawable/callout_link.png +0 -0
  124. data/platform/android/Rhodes/res/drawable/signature_cancel.png +0 -0
  125. data/platform/android/Rhodes/res/drawable/signature_clear.png +0 -0
  126. data/platform/android/Rhodes/res/drawable/signature_ok.png +0 -0
  127. data/platform/android/Rhodes/res/layout/datetime.xml +4 -0
  128. data/platform/android/Rhodes/res/layout/signature.xml +24 -17
  129. data/platform/android/Rhodes/res/layout/status_bar_ongoing_event_progress_bar.xml +102 -0
  130. data/platform/android/Rhodes/res/values/strings.xml +1 -1
  131. data/platform/android/Rhodes/src/com/rhomobile/rhodes/AndroidR.java +6 -0
  132. data/platform/android/Rhodes/src/com/rhomobile/rhodes/Base64.java +575 -0
  133. data/platform/android/Rhodes/src/com/rhomobile/rhodes/BaseActivity.java +83 -0
  134. data/platform/android/Rhodes/src/com/rhomobile/rhodes/NativeBar.java +21 -3
  135. data/platform/android/Rhodes/src/com/rhomobile/rhodes/PushReceiver.java +23 -15
  136. data/platform/android/Rhodes/src/com/rhomobile/rhodes/PushService.java +2 -2
  137. data/platform/android/Rhodes/src/com/rhomobile/rhodes/RhoCryptImpl.java +151 -0
  138. data/platform/android/Rhodes/src/com/rhomobile/rhodes/RhoMenu.java +2 -2
  139. data/platform/android/Rhodes/src/com/rhomobile/rhodes/RhodesActivity.java +372 -0
  140. data/platform/android/Rhodes/src/com/rhomobile/rhodes/RhodesAppOptions.java +11 -0
  141. data/platform/android/Rhodes/src/com/rhomobile/rhodes/RhodesApplication.java +58 -0
  142. data/platform/android/Rhodes/src/com/rhomobile/rhodes/RhodesService.java +705 -523
  143. data/platform/android/Rhodes/src/com/rhomobile/rhodes/RingtoneManager.java +1 -1
  144. data/platform/android/Rhodes/src/com/rhomobile/rhodes/SplashScreen.java +167 -85
  145. data/platform/android/Rhodes/src/com/rhomobile/rhodes/Utils.java +36 -1
  146. data/platform/android/Rhodes/src/com/rhomobile/rhodes/WebView.java +4 -2
  147. data/platform/android/Rhodes/src/com/rhomobile/rhodes/alert/Alert.java +33 -20
  148. data/platform/android/Rhodes/src/com/rhomobile/rhodes/bluetooth/IRhoBluetoothManager.java +2 -6
  149. data/platform/android/Rhodes/src/com/rhomobile/rhodes/bluetooth/RhoBluetoothManager.java +2 -24
  150. data/platform/android/Rhodes/src/com/rhomobile/rhodes/bluetooth/RhoBluetoothManagerNew.java +106 -36
  151. data/platform/android/Rhodes/src/com/rhomobile/rhodes/bluetooth/RhoBluetoothManagerOld.java +15 -58
  152. data/platform/android/Rhodes/src/com/rhomobile/rhodes/bluetooth/RhoBluetoothSession.java +0 -7
  153. data/platform/android/Rhodes/src/com/rhomobile/rhodes/camera/Camera.java +6 -4
  154. data/platform/android/Rhodes/src/com/rhomobile/rhodes/camera/FileList.java +4 -3
  155. data/platform/android/Rhodes/src/com/rhomobile/rhodes/camera/ImageCapture.java +91 -27
  156. data/platform/android/Rhodes/src/com/rhomobile/rhodes/datetime/DateTimePicker.java +6 -3
  157. data/platform/android/Rhodes/src/com/rhomobile/rhodes/datetime/DateTimePickerScreen.java +275 -42
  158. data/platform/android/Rhodes/src/com/rhomobile/rhodes/event/EventStore.java +1 -1
  159. data/platform/android/Rhodes/src/com/rhomobile/rhodes/file/RhoFileApi.java +1 -3
  160. data/platform/android/Rhodes/src/com/rhomobile/rhodes/geolocation/GeoLocationImpl.java +1 -1
  161. data/platform/android/Rhodes/src/com/rhomobile/rhodes/mainview/MainView.java +1 -0
  162. data/platform/android/Rhodes/src/com/rhomobile/rhodes/mainview/SimpleMainView.java +120 -43
  163. data/platform/android/Rhodes/src/com/rhomobile/rhodes/mainview/TabbedMainView.java +518 -28
  164. data/platform/android/Rhodes/src/com/rhomobile/rhodes/mapview/AnnotationsOverlay.java +2 -2
  165. data/platform/android/Rhodes/src/com/rhomobile/rhodes/mapview/GoogleMapView.java +434 -0
  166. data/platform/android/Rhodes/src/com/rhomobile/rhodes/mapview/MapTouch.java +20 -0
  167. data/platform/android/Rhodes/src/com/rhomobile/rhodes/mapview/MapView.java +296 -336
  168. data/platform/android/Rhodes/src/com/rhomobile/rhodes/mapview/MultiTouchHandler.java +111 -0
  169. data/platform/android/Rhodes/src/com/rhomobile/rhodes/mapview/OneTouchHandler.java +77 -0
  170. data/platform/android/Rhodes/src/com/rhomobile/rhodes/mapview/TouchHandler.java +10 -0
  171. data/platform/android/Rhodes/src/com/rhomobile/rhodes/nativeview/RhoNativeViewManager.java +0 -1
  172. data/platform/android/Rhodes/src/com/rhomobile/rhodes/phonebook/Contact.java +5 -1
  173. data/platform/android/Rhodes/src/com/rhomobile/rhodes/phonebook/ContactAccessorNew.java +1 -1
  174. data/platform/android/Rhodes/src/com/rhomobile/rhodes/phonebook/ContactAccessorOld.java +1 -1
  175. data/platform/android/Rhodes/src/com/rhomobile/rhodes/signature/ImageCapture.java +6 -11
  176. data/platform/android/Rhodes/src/com/rhomobile/rhodes/signature/Signature.java +6 -4
  177. data/platform/android/Rhodes/src/com/rhomobile/rhodes/signature/SignatureView.java +0 -7
  178. data/platform/android/Rhodes/src/com/rhomobile/rhodes/ui/LogViewDialog.java +9 -4
  179. data/platform/android/Rhodes/src/com/rhomobile/rhodes/util/PerformOnUiThread.java +8 -8
  180. data/platform/android/Rhodes/src/com/rhomobile/rhodes/webview/ChromeClientNew.java +5 -1
  181. data/platform/android/Rhodes/src/com/rhomobile/rhodes/webview/ChromeClientOld.java +11 -7
  182. data/platform/android/Rhodes/src/com/rhomobile/rhodes/webview/RhoWebSettingsNew.java +3 -5
  183. data/platform/android/build/RhodesSRC_build.files +32 -23
  184. data/platform/android/build/android.rake +28 -11
  185. data/platform/android/build/androidcommon.rb +2 -0
  186. data/platform/android/build/librhocommon_build.files +9 -5
  187. data/platform/android/build/librhodes_build.files +1 -0
  188. data/platform/android/build/libruby_build.files +1 -0
  189. data/platform/bb/Hsqldb/Hsqldb.jdp +0 -2
  190. data/platform/bb/Hsqldb/src/com/rho/db/HsqlDBResult.java +10 -2
  191. data/platform/bb/Hsqldb/src/com/rho/db/HsqlDBStorage.java +37 -5
  192. data/platform/bb/Hsqldb/src/org/hsqldb/Column.java +5 -5
  193. data/platform/bb/Hsqldb/src/org/hsqldb/Expression.java +1 -1
  194. data/platform/bb/Hsqldb/src/org/hsqldb/Function.java +73 -78
  195. data/platform/bb/Hsqldb/src/org/hsqldb/Library.java +288 -8
  196. data/platform/bb/RubyVM/RubyVM.jdp +3 -0
  197. data/platform/bb/RubyVM/src/com/rho/FilePath.java +13 -1
  198. data/platform/bb/RubyVM/src/com/rho/IRhoRubyHelper.java +2 -2
  199. data/platform/bb/RubyVM/src/com/rho/RhoAppAdapter.java +12 -0
  200. data/platform/bb/RubyVM/src/com/rho/RhoClassFactory.java +18 -1
  201. data/platform/bb/RubyVM/src/com/rho/RhoConf.java +128 -108
  202. data/platform/bb/RubyVM/src/com/rho/RhoLogConf.java +9 -10
  203. data/platform/bb/RubyVM/src/com/rho/RhoLogger.java +16 -1
  204. data/platform/bb/RubyVM/src/com/rho/RhoProfiler.java +1 -1
  205. data/platform/bb/RubyVM/src/com/rho/RhoRuby.java +20 -0
  206. data/platform/bb/RubyVM/src/com/rho/RhodesApp.java +5 -2
  207. data/platform/bb/RubyVM/src/com/rho/ThreadQueue.java +65 -46
  208. data/platform/bb/RubyVM/src/com/rho/db/DBAdapter.java +192 -50
  209. data/platform/bb/RubyVM/src/com/rho/db/DBAttrManager.java +8 -114
  210. data/platform/bb/RubyVM/src/com/rho/db/DBException.java +1 -1
  211. data/platform/bb/RubyVM/src/com/rho/db/IDBCallback.java +1 -1
  212. data/platform/bb/RubyVM/src/com/rho/db/IDBResult.java +5 -3
  213. data/platform/bb/RubyVM/src/com/rho/db/IDBStorage.java +5 -1
  214. data/platform/bb/RubyVM/src/com/rho/file/FileAccessBB.java +109 -0
  215. data/platform/bb/RubyVM/src/com/rho/file/RhoFile.java +85 -0
  216. data/platform/bb/RubyVM/src/com/rho/net/NetRequest.java +12 -4
  217. data/platform/bb/RubyVM/src/com/rho/net/NetResponse.java +5 -0
  218. data/platform/bb/RubyVM/src/com/rho/net/RhoConnection.java +37 -23
  219. data/platform/bb/RubyVM/src/com/rho/net/URI.java +84 -0
  220. data/platform/bb/RubyVM/src/com/rho/sync/ClientRegister.java +1 -1
  221. data/platform/bb/RubyVM/src/com/rho/sync/SyncEngine.java +119 -61
  222. data/platform/bb/RubyVM/src/com/rho/sync/SyncNotify.java +61 -24
  223. data/platform/bb/RubyVM/src/com/rho/sync/SyncSource.java +108 -54
  224. data/platform/bb/RubyVM/src/com/rho/sync/SyncThread.java +121 -163
  225. data/platform/bb/RubyVM/src/com/xruby/GeneratedMethods/RubyString_Methods.java +23 -0
  226. data/platform/bb/RubyVM/src/com/xruby/runtime/builtin/RubyMutex.java +1 -1
  227. data/platform/bb/RubyVM/src/com/xruby/runtime/builtin/RubyRange.java +3 -1
  228. data/platform/bb/RubyVM/src/com/xruby/runtime/builtin/RubyRegexp.java +3 -1
  229. data/platform/bb/RubyVM/src/com/xruby/runtime/builtin/RubyString.java +208 -25
  230. data/platform/bb/RubyVM/src/com/xruby/runtime/builtin/RubyTime.java +3 -1
  231. data/platform/bb/RubyVM/src/com/xruby/runtime/lang/RhoSupport.java +5 -1
  232. data/platform/bb/RubyVM/src/com/xruby/runtime/lang/RubyAPI.java +12 -5
  233. data/platform/bb/RubyVM/src/com/xruby/runtime/lang/RubyID.java +1 -0
  234. data/platform/bb/RubyVM/src/com/xruby/runtime/lang/RubyKernelModule.java +16 -0
  235. data/platform/bb/RubyVM/src/com/xruby/runtime/lang/RubyKernelModule_Methods.java +5 -0
  236. data/platform/bb/RubyVM/src/com/xruby/runtime/lang/RubyValue.java +1 -1
  237. data/platform/bb/RubyVM/src/com/xruby/runtime/stdlib/RubyStringIO.java +4 -1
  238. data/platform/bb/{Hsqldb → RubyVM}/src/j2me/math/Number.java +33 -0
  239. data/platform/bb/build/RubyVM_build.files +3 -0
  240. data/platform/bb/build/bb.rake +140 -62
  241. data/platform/bb/build/hsqldb_build.files +0 -2
  242. data/platform/bb/build/rhodes_build.files +5 -0
  243. data/platform/bb/rhodes/platform/4.7/com/rho/RhoMainScreen.java +43 -7
  244. data/platform/bb/rhodes/platform/5.0/com/rho/BrowserAdapter5.java +23 -2
  245. data/platform/bb/rhodes/platform/5.0/com/rho/RhodesApplicationPlatform.java +5 -0
  246. data/platform/bb/rhodes/platform/5.0/com/rho/db/SqliteCopyResult.java +143 -0
  247. data/platform/bb/rhodes/platform/5.0/com/rho/db/SqliteResult.java +269 -0
  248. data/platform/bb/rhodes/platform/5.0/com/rho/db/SqliteStorage.java +446 -0
  249. data/platform/bb/rhodes/platform/common/com/rho/BrowserAdapter5.java +3 -0
  250. data/platform/bb/rhodes/platform/common/com/rho/RhoMainScreen.java +12 -2
  251. data/platform/bb/rhodes/platform/common/com/rho/db/SqliteCopyResult.java +2 -0
  252. data/platform/bb/rhodes/platform/common/com/rho/db/SqliteResult.java +2 -0
  253. data/platform/bb/rhodes/platform/common/com/rho/db/SqliteStorage.java +2 -0
  254. data/platform/bb/rhodes/resources/mappin.png +0 -0
  255. data/platform/bb/rhodes/rhodes.jdp +3 -0
  256. data/platform/bb/rhodes/src/com/rho/BrowserAdapter.java +21 -18
  257. data/platform/bb/rhodes/src/com/rho/BrowserAdapter5.java +150 -0
  258. data/platform/bb/rhodes/src/com/rho/IBrowserAdapter.java +1 -0
  259. data/platform/bb/rhodes/src/com/rho/RhoMainScreen.java +63 -4
  260. data/platform/bb/rhodes/src/com/rho/RhoRubyHelper.java +6 -5
  261. data/platform/bb/rhodes/src/com/rho/RhodesApplicationPlatform.java +242 -2
  262. data/platform/bb/rhodes/src/com/rho/db/SqliteCopyResult.java +143 -0
  263. data/platform/bb/rhodes/src/com/rho/db/SqliteResult.java +269 -0
  264. data/platform/bb/rhodes/src/com/rho/db/SqliteStorage.java +446 -0
  265. data/platform/bb/rhodes/src/com/rho/file/Jsr75File.java +1 -1
  266. data/platform/bb/rhodes/src/com/rho/net/NetworkAccess.java +71 -14
  267. data/platform/bb/rhodes/src/com/rho/net/bb/BBHttpConnection.java +4 -2
  268. data/platform/bb/rhodes/src/com/rho/rubyext/Alert.java +28 -12
  269. data/platform/bb/rhodes/src/com/rho/rubyext/System.java +100 -4
  270. data/platform/bb/rhodes/src/com/rho/rubyext/WebView.java +3 -2
  271. data/platform/bb/rhodes/src/com/rho/rubyext/XMLParser.java +1 -1
  272. data/platform/bb/rhodes/src/rhomobile/NativeBar.java +32 -0
  273. data/platform/bb/rhodes/src/rhomobile/PushListeningThread.java +9 -2
  274. data/platform/bb/rhodes/src/rhomobile/RhodesApplication.java +174 -41
  275. data/platform/bb/rhodes/src/rhomobile/datetime/DateTimePicker.java +52 -3
  276. data/platform/bb/rhodes/src/rhomobile/datetime/DateTimeScreen.java +33 -1
  277. data/platform/bb/rhodes/src/rhomobile/mapview/Annotation.java +2 -14
  278. data/platform/bb/rhodes/src/rhomobile/mapview/ESRIMapField.java +686 -0
  279. data/platform/bb/rhodes/src/rhomobile/mapview/ESRIMapProvider.java +14 -0
  280. data/platform/bb/rhodes/src/rhomobile/mapview/GeoCoding.java +13 -0
  281. data/platform/bb/rhodes/src/rhomobile/mapview/GoogleGeoCoding.java +174 -0
  282. data/platform/bb/rhodes/src/rhomobile/mapview/GoogleMapField.java +104 -330
  283. data/platform/bb/rhodes/src/rhomobile/mapview/MapTools.java +79 -0
  284. data/platform/bb/rhodes/src/rhomobile/mapview/MapView.java +35 -12
  285. data/platform/bb/rhodes/src/rhomobile/mapview/MapViewParent.java +2 -1
  286. data/platform/bb/rhodes/src/rhomobile/mapview/MapViewScreen.java +336 -44
  287. data/platform/bb/rhodes/src/rhomobile/mapview/RhoMapField.java +3 -6
  288. data/platform/iphone/Classes/AppManager/AppManager.m +19 -4
  289. data/platform/iphone/Classes/DateTime.h +4 -0
  290. data/platform/iphone/Classes/DateTime.m +1 -1
  291. data/platform/iphone/Classes/DateTimePicker.m +8 -1
  292. data/platform/iphone/Classes/DateTimePickerDelegate.m +7 -1
  293. data/platform/iphone/Classes/Event/Event.m +2 -2
  294. data/platform/iphone/Classes/MapView/MapViewController.h +4 -1
  295. data/platform/iphone/Classes/MapView/MapViewController.m +6 -46
  296. data/platform/iphone/Classes/MapView/MapViewManager.h +26 -0
  297. data/platform/iphone/Classes/MapView/MapViewManager.m +172 -0
  298. data/platform/iphone/Classes/NativeBar.h +34 -0
  299. data/platform/iphone/Classes/NativeBar.m +131 -26
  300. data/platform/iphone/Classes/NativeView/NVDelegate.h +26 -0
  301. data/platform/iphone/Classes/NativeView/NVDelegate.m +40 -0
  302. data/platform/iphone/Classes/NativeView/NVViewController.h +24 -0
  303. data/platform/iphone/Classes/NativeView/NVViewController.m +90 -0
  304. data/platform/iphone/Classes/NativeView/RhoNativeViewManager.mm +449 -0
  305. data/platform/iphone/Classes/{RhoNativeViewManagerOC.h → NativeView/RhoNativeViewManagerOC.h} +15 -0
  306. data/platform/iphone/Classes/RhoAlert.m +3 -3
  307. data/platform/iphone/Classes/RhoMainView.h +3 -0
  308. data/platform/iphone/Classes/Rhodes.h +6 -2
  309. data/platform/iphone/Classes/Rhodes.m +47 -79
  310. data/platform/iphone/Classes/Signature/SignatureViewController.m +1 -0
  311. data/platform/iphone/Classes/SimpleMainView.h +18 -1
  312. data/platform/iphone/Classes/SimpleMainView.m +180 -35
  313. data/platform/iphone/Classes/SplitView/LeftViewController.h +1 -1
  314. data/platform/iphone/Classes/SplitView/LeftViewController.m +13 -7
  315. data/platform/iphone/Classes/SplitView/RightViewController.h +1 -1
  316. data/platform/iphone/Classes/SplitView/RightViewController.m +26 -11
  317. data/platform/iphone/Classes/SplitView/SplittedMainView.h +1 -6
  318. data/platform/iphone/Classes/SplitView/SplittedMainView.m +14 -52
  319. data/platform/iphone/Classes/TabbedMainView.h +25 -3
  320. data/platform/iphone/Classes/TabbedMainView.m +340 -14
  321. data/platform/iphone/ESRI/BluePushpin.png +0 -0
  322. data/platform/iphone/ESRI/BluePushpin@2x.png +0 -0
  323. data/platform/iphone/ESRI/esri.png +0 -0
  324. data/platform/iphone/ESRI/esri@2x.png +0 -0
  325. data/platform/iphone/Info.plist +1 -1
  326. data/platform/iphone/RhoLib/RhoLib.xcodeproj/project.pbxproj +26 -3
  327. data/platform/iphone/rbuild/iphone.rake +100 -4
  328. data/platform/iphone/rhorubylib/rhorubylib.xcodeproj/project.pbxproj +12 -0
  329. data/platform/iphone/rhorunner.xcodeproj/project.pbxproj +116 -12
  330. data/platform/shared/SyncClient/SyncClient.cpp +29 -3
  331. data/platform/shared/SyncClient/SyncClient.h +2 -1
  332. data/platform/shared/common/IRhoClassFactory.h +3 -0
  333. data/platform/shared/common/IRhoCrypt.h +16 -0
  334. data/platform/shared/common/IRhoThreadImpl.h +1 -0
  335. data/platform/shared/common/RhoAppAdapter.h +2 -0
  336. data/platform/shared/common/RhoConf.cpp +97 -46
  337. data/platform/shared/common/RhoConf.h +14 -13
  338. data/platform/shared/common/RhoFile.cpp +10 -0
  339. data/platform/shared/common/RhoFile.h +1 -0
  340. data/platform/shared/common/RhoMath.cpp +43 -0
  341. data/platform/shared/common/RhoMath.h +27 -0
  342. data/platform/shared/common/RhoNativeViewManager.h +27 -1
  343. data/platform/shared/common/RhoPort.h +11 -0
  344. data/platform/shared/common/RhoStd.h +5 -0
  345. data/platform/shared/common/RhoThread.cpp +1 -1
  346. data/platform/shared/common/RhoThread.h +1 -1
  347. data/platform/shared/common/RhodesApp.cpp +381 -139
  348. data/platform/shared/common/RhodesApp.h +24 -10
  349. data/platform/shared/common/RhodesAppBase.cpp +72 -1
  350. data/platform/shared/common/RhodesAppBase.h +5 -1
  351. data/platform/shared/common/SplashScreen.h +4 -4
  352. data/platform/shared/common/ThreadQueue.cpp +44 -17
  353. data/platform/shared/common/ThreadQueue.h +4 -0
  354. data/platform/shared/common/app_build_configs.c +0 -1
  355. data/platform/shared/common/iphone/RhoClassfactory.h +6 -0
  356. data/platform/shared/common/iphone/RhoCryptImpl.h +47 -0
  357. data/platform/shared/common/iphone/RhoCryptImpl.mm +286 -0
  358. data/platform/shared/common/map/ESRIMapEngine.cpp +921 -0
  359. data/platform/shared/common/map/ESRIMapEngine.h +255 -0
  360. data/platform/shared/common/map/GoogleMapEngine.cpp +160 -0
  361. data/platform/shared/common/map/GoogleMapEngine.h +52 -0
  362. data/platform/shared/common/map/MapEngine.cpp +304 -0
  363. data/platform/shared/common/map/MapEngine.h +214 -0
  364. data/platform/shared/db/DBAdapter.cpp +147 -18
  365. data/platform/shared/db/DBAdapter.h +38 -12
  366. data/platform/shared/db/DBAttrManager.cpp +4 -3
  367. data/platform/shared/db/DBAttrManager.h +7 -7
  368. data/platform/shared/db/res/db/syncdb.triggers +0 -5
  369. data/platform/shared/db/res/db/syncdb_java.triggers +21 -0
  370. data/platform/shared/json/JSONIterator.cpp +11 -1
  371. data/platform/shared/json/JSONIterator.h +1 -0
  372. data/platform/shared/logging/RhoLogConf.cpp +29 -12
  373. data/platform/shared/net/CURLNetRequest.cpp +80 -25
  374. data/platform/shared/net/CURLNetRequest.h +6 -2
  375. data/platform/shared/net/HttpServer.cpp +12 -6
  376. data/platform/shared/net/HttpServer.h +2 -1
  377. data/platform/shared/net/INetRequest.h +1 -0
  378. data/platform/shared/ruby/ext/alert/alert.i +5 -0
  379. data/platform/shared/ruby/ext/alert/alert_wrap.c +439 -108
  380. data/platform/shared/ruby/ext/datetimepicker/datetimepicker.i +42 -0
  381. data/platform/shared/ruby/ext/datetimepicker/datetimepicker_wrap.c +521 -120
  382. data/platform/shared/ruby/ext/nativebar/nativebar.i +16 -0
  383. data/platform/shared/ruby/ext/nativebar/nativebar_wrap.c +623 -128
  384. data/platform/shared/ruby/ext/nativeviewmanager/nativeviewmanager.i +18 -0
  385. data/platform/shared/ruby/ext/nativeviewmanager/nativeviewmanager_wrap.c +2321 -0
  386. data/platform/shared/ruby/ext/rho/extensions.c +0 -1
  387. data/platform/shared/ruby/ext/rho/rhoruby.c +53 -0
  388. data/platform/shared/ruby/ext/rho/rhoruby.h +6 -0
  389. data/platform/shared/ruby/ext/sqlite3_api/sqlite3_api_wrap.c +6 -3
  390. data/platform/shared/ruby/ext/syncengine/syncengine.i +9 -1
  391. data/platform/shared/ruby/ext/syncengine/syncengine_wrap.c +61 -0
  392. data/platform/shared/ruby/ext/system/system.i +4 -0
  393. data/platform/shared/ruby/ext/system/system_wrap.c +33 -5
  394. data/platform/shared/rubyext/RhoAppAdapter.cpp +11 -1
  395. data/platform/shared/rubyext/System.cpp +9 -0
  396. data/platform/shared/rubyext/WebView.h +1 -0
  397. data/platform/shared/sqlite/crypto.c +907 -0
  398. data/platform/shared/sqlite/sqlite3.c +6 -0
  399. data/platform/shared/sync/SyncEngine.cpp +49 -19
  400. data/platform/shared/sync/SyncEngine.h +0 -1
  401. data/platform/shared/sync/SyncNotify.cpp +14 -5
  402. data/platform/shared/sync/SyncNotify.h +2 -2
  403. data/platform/shared/sync/SyncSource.cpp +79 -33
  404. data/platform/shared/sync/SyncSource.h +1 -0
  405. data/platform/shared/sync/SyncThread.cpp +26 -11
  406. data/platform/shared/xruby/src/com/xruby/compiler/codedom/StringExpression.java +4 -2
  407. data/platform/wm/RhoLib/RhoLib.vcproj +108 -72
  408. data/platform/wm/build/wm.rake +6 -0
  409. data/platform/wm/rhodes/Alert.cpp +4 -3
  410. data/platform/wm/rhodes/Alert.h +1 -1
  411. data/platform/wm/rhodes/DateTimePicker.cpp +30 -3
  412. data/platform/wm/rhodes/DateTimePicker.h +9 -1
  413. data/platform/wm/rhodes/MainWindow.cpp +121 -61
  414. data/platform/wm/rhodes/MainWindow.h +29 -4
  415. data/platform/wm/rhodes/MapView/Graphics.cpp +547 -0
  416. data/platform/wm/rhodes/MapView/Graphics.h +136 -0
  417. data/platform/wm/rhodes/MapView/MapViewManager.cpp +391 -0
  418. data/platform/wm/rhodes/MapView/MapViewManager.h +158 -0
  419. data/platform/wm/rhodes/RhoCryptImpl.cpp +248 -0
  420. data/platform/wm/rhodes/RhoCryptImpl.h +51 -0
  421. data/platform/wm/rhodes/RhoNativeViewManager.cpp +208 -0
  422. data/platform/wm/rhodes/Rhodes.cpp +94 -102
  423. data/platform/wm/rhodes/Rhodes.rc +27 -0
  424. data/platform/wm/rhodes/SyncStatusDlg.cpp +1 -0
  425. data/platform/wm/rhodes/SyncStatusDlg.h +2 -1
  426. data/platform/wm/rhodes/bluetooth/Bluetooth.cpp +7 -4
  427. data/platform/wm/rhodes/camera/Camera.cpp +36 -15
  428. data/platform/wm/rhodes/camera/Camera.h +0 -4
  429. data/platform/wm/rhodes/resource.h +10 -2
  430. data/platform/wm/rhodes/rho/common/RhoClassFactory.cpp +7 -0
  431. data/platform/wm/rhodes/rho/common/RhoClassFactory.h +1 -0
  432. data/platform/wm/rhodes/rho/net/NetRequestImpl.cpp +10 -2
  433. data/platform/wm/rhodes/rho/net/NetRequestImpl.h +10 -0
  434. data/platform/wm/rhodes/rho/rubyext/NativeToolbar.cpp +513 -0
  435. data/platform/wm/rhodes/rho/rubyext/NativeToolbar.h +74 -0
  436. data/platform/wm/rhodes/rho/rubyext/SystemImpl.cpp +22 -10
  437. data/platform/wm/rhodes/rho/rubyext/WebView.cpp +5 -0
  438. data/platform/wm/rhodes/rhodes.vcproj +38 -2
  439. data/platform/wm/rhodes/stdafx.h +2 -1
  440. data/platform/wm/rubylib/rubylib.vcproj +12 -0
  441. data/rakefile.rb +85 -80
  442. data/res/build-tools/iphonesim/Source/iPhoneSimulator.m +14 -3
  443. data/res/build-tools/iphonesim/build/Release/iphonesim +0 -0
  444. data/res/build-tools/iphonesim/iphonesim.xcodeproj/project.pbxproj +2 -0
  445. data/res/build-tools/xruby-0.3.3.jar +0 -0
  446. data/res/generators/templates/application/app/application.rb +2 -2
  447. data/res/generators/templates/application/app/helpers/application_helper.rb +1 -1
  448. data/res/generators/templates/application/public/css/android.css +1 -0
  449. data/res/generators/templates/application/public/jqtouch/jquery.1.3.2.min.js +1 -1
  450. data/res/generators/templates/application/rhoconfig.txt +66 -14
  451. data/rhodes.gemspec +3 -3
  452. data/spec/framework_spec/app/spec/core/file/expand_path_spec.rb +7 -3
  453. data/spec/framework_spec/build.yml +1 -1
  454. data/spec/phone_spec/Rakefile +1 -0
  455. data/spec/phone_spec/app/Account/account.rb +2 -0
  456. data/spec/phone_spec/app/Account_s/account_s.rb +7 -0
  457. data/spec/phone_spec/app/spec/{fixtures → array/fixtures}/classes.rb +0 -0
  458. data/spec/phone_spec/app/spec/{array_pack_spec.rb → array/pack_spec.rb} +2 -1
  459. data/spec/phone_spec/app/spec/bulksync_spec.rb +1 -0
  460. data/spec/phone_spec/app/spec/contacts_spec.rb +2 -0
  461. data/spec/phone_spec/app/spec/rho_spec.rb +34 -233
  462. data/spec/phone_spec/app/spec/rhom_object_spec.rb +396 -87
  463. data/spec/phone_spec/app/spec/string/element_set_spec.rb +251 -0
  464. data/spec/phone_spec/app/spec/string/end_with_spec.rb +49 -0
  465. data/spec/phone_spec/app/spec/string/fixtures/classes.rb +27 -0
  466. data/spec/phone_spec/app/spec/{lstrip_spec.rb → string/lstrip_spec.rb} +0 -0
  467. data/spec/phone_spec/app/spec/string/replace_spec.rb +52 -0
  468. data/spec/phone_spec/app/spec/{rstrip_spec.rb → string/rstrip_spec.rb} +0 -0
  469. data/spec/phone_spec/app/spec/string/slice_spec.rb +473 -0
  470. data/spec/phone_spec/app/spec/string/split_spec.rb +302 -0
  471. data/spec/phone_spec/app/spec/string/start_with_spec.rb +48 -0
  472. data/spec/phone_spec/app/spec/{strip_spec.rb → string/strip_spec.rb} +0 -0
  473. data/spec/phone_spec/app/spec/syncengine_spec.rb +30 -1
  474. data/spec/phone_spec/app/spec_runner.rb +15 -7
  475. data/spec/phone_spec/build.yml +2 -2
  476. metadata +154 -25
  477. data/README.textile +0 -45
  478. data/platform/android/Rhodes/AndroidManifest.full.xml +0 -62
  479. data/platform/android/Rhodes/gen/com/rhomobile/rhodes/Manifest.java +0 -14
  480. data/platform/android/Rhodes/src/com/rhomobile/rhodes/RhoActivity.java +0 -20
  481. data/platform/android/Rhodes/src/com/rhomobile/rhodes/Rhodes.java +0 -193
  482. data/platform/bb/Hsqldb/src/com/rho/file/FileAccessBB.java +0 -25
  483. data/platform/bb/RubyVM/src/com/rho/AppBuildConfig.java +0 -22
  484. data/platform/bb/RubyVM/src/com/rho/Capabilities.java +0 -6
  485. data/platform/bb/RubyVM/src/com/rho/Extensions.java +0 -11
  486. data/platform/iphone/Classes/RhoNativeViewManager.mm +0 -226
@@ -0,0 +1,907 @@
1
+ #include "logging/RhoLog.h"
2
+ #undef DEFAULT_LOGCATEGORY
3
+ #define DEFAULT_LOGCATEGORY "sqlite_crypt"
4
+
5
+ extern void rho_db_decrypt( const char* szPartition, int nPartLen, int size, unsigned char* data );
6
+ extern void rho_db_encrypt( const char* szPartition, int nPartLen, int size, unsigned char* data, unsigned char* dataOut );
7
+
8
+ #define FILE_HEADER_SZ 16
9
+ #define ENCRYPTED_SQLITE_FILE_HEADER "SQLite crypto 3"
10
+ #define EVP_MAX_KEY_LENGTH 32
11
+ #define EVP_MAX_IV_LENGTH 16
12
+
13
+ typedef struct
14
+ {
15
+ char* m_szPartition;
16
+ int m_nPartLen;
17
+ unsigned char* m_pPageBuffer;
18
+ } CRhoSqliteCodecCtx;
19
+
20
+ void* sqlite3Codec(void *iCtx, void *data, Pgno pgno, int mode);
21
+ int sqlite3CodecAttach(sqlite3* db, int nDb, const void *zKey, int nKey);
22
+
23
+ void sqlite3FreeCodecArg(void *pCodecArg)
24
+ {
25
+ CRhoSqliteCodecCtx *pRhoCtx = (CRhoSqliteCodecCtx *) pCodecArg;
26
+ if ( pCodecArg == NULL )
27
+ return;
28
+
29
+ if (pRhoCtx->m_szPartition )
30
+ sqlite3_free(pRhoCtx->m_szPartition);
31
+
32
+ pRhoCtx->m_szPartition = NULL;
33
+ pRhoCtx->m_nPartLen = 0;
34
+ if ( pRhoCtx->m_pPageBuffer )
35
+ sqlite3_free(pRhoCtx->m_pPageBuffer);
36
+
37
+ pRhoCtx->m_pPageBuffer = NULL;
38
+ }
39
+
40
+ int sqlite3_key(sqlite3 *db, const void *pKey, int nKey)
41
+ {
42
+ //RAWLOG_INFO("sqlite3_key");
43
+
44
+ if ( db && pKey && nKey )
45
+ {
46
+ sqlite3CodecAttach(db, 0, pKey, nKey);
47
+ return SQLITE_OK;
48
+ }
49
+
50
+ return SQLITE_ERROR;
51
+ }
52
+
53
+ void* sqlite3Codec(void *iCtx, void *data, Pgno pgno, int mode)
54
+ {
55
+ CRhoSqliteCodecCtx *pRhoCtx = (CRhoSqliteCodecCtx *) iCtx;
56
+ int pg_sz = SQLITE_DEFAULT_PAGE_SIZE;
57
+ int offset = 0;
58
+ unsigned char *pData = (unsigned char *) data;
59
+
60
+ //RAWLOG_INFO("sqlite3Codec");
61
+
62
+ if ( pgno == 1 )
63
+ offset = FILE_HEADER_SZ; /* adjust starting pointers in data page for header offset on first page*/
64
+
65
+ switch(mode)
66
+ {
67
+ case 0: /* decrypt */
68
+ case 2:
69
+ case 3:
70
+ if ( pgno == 1 )
71
+ memcpy(pData, SQLITE_FILE_HEADER, FILE_HEADER_SZ); /* copy file header to the first 16 bytes of the page */
72
+
73
+ rho_db_decrypt(pRhoCtx->m_szPartition, pRhoCtx->m_nPartLen, pg_sz - offset, pData + offset );
74
+ return pData;
75
+ case 6: /* encrypt */
76
+ case 7:
77
+ if ( pgno == 1 )
78
+ memcpy( pRhoCtx->m_pPageBuffer, ENCRYPTED_SQLITE_FILE_HEADER, FILE_HEADER_SZ); /* copy salt to output buffer */
79
+
80
+ rho_db_encrypt(pRhoCtx->m_szPartition, pRhoCtx->m_nPartLen, pg_sz - offset, pData + offset, pRhoCtx->m_pPageBuffer + offset);
81
+ return pRhoCtx->m_pPageBuffer; /* return persistent buffer data, pData remains intact */
82
+ default:
83
+ return pData;
84
+ }
85
+
86
+ }
87
+
88
+ int sqlite3CodecAttach(sqlite3* db, int nDb, const void *pKey, int nKey)
89
+ {
90
+ struct Db *pDb = &db->aDb[nDb];
91
+
92
+ //RAWLOG_INFO("sqlite3CodecAttach");
93
+
94
+ if ( nKey && pKey && pDb->pBt )
95
+ {
96
+ Pager *pPager = sqlite3BtreePager(pDb->pBt);
97
+ sqlite3_file *fd;
98
+
99
+ CRhoSqliteCodecCtx* pRhoCtx = sqlite3Malloc(sizeof(CRhoSqliteCodecCtx));
100
+ memset(pRhoCtx, 0, sizeof(CRhoSqliteCodecCtx));
101
+ pRhoCtx->m_szPartition = sqlite3Malloc(nKey);
102
+ memcpy(pRhoCtx->m_szPartition, pKey, nKey);
103
+ pRhoCtx->m_nPartLen = nKey;
104
+ pRhoCtx->m_pPageBuffer = sqlite3Malloc(SQLITE_DEFAULT_PAGE_SIZE);
105
+
106
+ sqlite3PagerSetCodec( pPager, sqlite3Codec, NULL, sqlite3FreeCodecArg, (void *)pRhoCtx );
107
+ fd = (isOpen(pPager->fd)) ? pPager->fd : NULL;
108
+
109
+ sqlite3_mutex_enter(db->mutex);
110
+
111
+ /* Always overwrite page size and set to the default because the first page of the database
112
+ in encrypted and thus sqlite can't effectively determine the pagesize. this causes an issue in
113
+ cases where bytes 16 & 17 of the page header are a power of 2 as reported by John Lehman
114
+
115
+ Note: before forcing the page size we need to force pageSizeFixed to 0, else
116
+ sqliteBtreeSetPageSize will block the change
117
+ */
118
+ pDb->pBt->pBt->pageSizeFixed = 0;
119
+ sqlite3BtreeSetPageSize( pDb->pBt, SQLITE_DEFAULT_PAGE_SIZE, EVP_MAX_IV_LENGTH, 0 );
120
+
121
+ /* if fd is null, then this is an in-memory database and
122
+ we dont' want to overwrite the AutoVacuum settings
123
+ if not null, then set to the default */
124
+ if ( fd != NULL )
125
+ sqlite3BtreeSetAutoVacuum(pDb->pBt, SQLITE_DEFAULT_AUTOVACUUM);
126
+
127
+ sqlite3_mutex_leave(db->mutex);
128
+ }
129
+
130
+ return SQLITE_OK;
131
+ }
132
+
133
+ void sqlite3CodecGetKey(sqlite3* db, int nDb, void **zKey, int *nKey)
134
+ {
135
+ struct Db *pDb = &db->aDb[nDb];
136
+
137
+ //RAWLOG_INFO("sqlite3CodecGetKey");
138
+
139
+ if( pDb->pBt )
140
+ {
141
+ Pager *pPager = sqlite3BtreePager(pDb->pBt);
142
+ CRhoSqliteCodecCtx *pRhoCtx = (CRhoSqliteCodecCtx *) sqlite3PagerGetCodec(pPager);
143
+
144
+ if ( pRhoCtx )
145
+ { /* if the codec has an attached codec_context user the raw key data */
146
+ *zKey = pRhoCtx->m_szPartition;
147
+ *nKey = pRhoCtx->m_nPartLen;
148
+ } else {
149
+ *zKey = NULL;
150
+ *nKey = 0;
151
+ }
152
+ }
153
+ }
154
+
155
+ void sqlite3_activate_see(const char* in)
156
+ {
157
+ /* do nothing, security enhancements are always active */
158
+ }
159
+
160
+ int sqlite3_rekey(sqlite3 *db, const void *pKey, int nKey)
161
+ {
162
+ return SQLITE_OK;
163
+ }
164
+
165
+ #if 0
166
+ /*
167
+ ** SQLCipher
168
+ ** crypto.c developed by Stephen Lombardo (Zetetic LLC)
169
+ ** sjlombardo at zetetic dot net
170
+ ** http://zetetic.net
171
+ **
172
+ ** Copyright (c) 2009, ZETETIC LLC
173
+ ** All rights reserved.
174
+ **
175
+ ** Redistribution and use in source and binary forms, with or without
176
+ ** modification, are permitted provided that the following conditions are met:
177
+ ** * Redistributions of source code must retain the above copyright
178
+ ** notice, this list of conditions and the following disclaimer.
179
+ ** * Redistributions in binary form must reproduce the above copyright
180
+ ** notice, this list of conditions and the following disclaimer in the
181
+ ** documentation and/or other materials provided with the distribution.
182
+ ** * Neither the name of the ZETETIC LLC nor the
183
+ ** names of its contributors may be used to endorse or promote products
184
+ ** derived from this software without specific prior written permission.
185
+ **
186
+ ** THIS SOFTWARE IS PROVIDED BY ZETETIC LLC ''AS IS'' AND ANY
187
+ ** EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
188
+ ** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
189
+ ** DISCLAIMED. IN NO EVENT SHALL ZETETIC LLC BE LIABLE FOR ANY
190
+ ** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
191
+ ** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
192
+ ** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
193
+ ** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
194
+ ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
195
+ ** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
196
+ **
197
+ */
198
+ /* BEGIN CRYPTO */
199
+ #ifdef SQLITE_HAS_CODEC
200
+
201
+ #include <assert.h>
202
+ //#include <Wincrypt.h>
203
+
204
+ #define EVP_MAX_KEY_LENGTH 32
205
+ #define EVP_MAX_IV_LENGTH 16
206
+
207
+ typedef struct _EVP_CIPHER
208
+ {
209
+ int iv_len;
210
+ int key_len;
211
+ }EVP_CIPHER;
212
+ EVP_CIPHER s_cipher = {EVP_MAX_IV_LENGTH, EVP_MAX_KEY_LENGTH};
213
+
214
+ const EVP_CIPHER *EVP_get_cipherbyname(const char *name)
215
+ {
216
+ return &s_cipher;
217
+ }
218
+
219
+ int PKCS5_PBKDF2_HMAC_SHA1(const char *pass, int passlen,
220
+ unsigned char *salt, int saltlen, int iter,
221
+ int keylen, unsigned char *out)
222
+ {
223
+ return 0;
224
+ }
225
+
226
+ int RAND_pseudo_bytes(unsigned char *buf, int num)
227
+ {
228
+ //TODO: implement RAND_pseudo_bytes
229
+ return 0;
230
+ }
231
+
232
+ #define EVP_CIPHER_iv_length(e) ((e)->iv_len)
233
+ #define EVP_CIPHER_key_length(e) ((e)->key_len)
234
+
235
+ //#include <openssl/evp.h>
236
+ //#include <openssl/rand.h>
237
+ //#include <openssl/hmac.h>
238
+ //#include "sqliteInt.h"
239
+ //#include "btreeInt.h"
240
+ #include "crypto.h"
241
+
242
+ void sqlite3pager_get_codec(Pager *pPager, void **ctx);
243
+ int sqlite3pager_is_mj_pgno(Pager *pPager, Pgno pgno);
244
+ sqlite3_file *sqlite3Pager_get_fd(Pager *pPager);
245
+ void sqlite3pager_sqlite3PagerSetCodec(
246
+ Pager *pPager,
247
+ void *(*xCodec)(void*,void*,Pgno,int),
248
+ void (*xCodecSizeChng)(void*,int,int),
249
+ void (*xCodecFree)(void*),
250
+ void *pCodec );
251
+ //RHO
252
+ #include "logging/RhoLog.h"
253
+ #undef DEFAULT_LOGCATEGORY
254
+ #define DEFAULT_LOGCATEGORY "Crypto"
255
+ //RHO
256
+ void rho_loginfo(const char* format, ...)
257
+ {
258
+ va_list ap;
259
+ va_start(ap, format);
260
+ rhoPlainLogArg(__FILE__,__LINE__,L_INFO,DEFAULT_LOGCATEGORY,format,ap);
261
+ va_end(ap);
262
+ }
263
+
264
+ #define CODEC_DEBUG 1
265
+ #ifdef CODEC_DEBUG
266
+ #define CODEC_TRACE(X) {rho_loginfo X;}
267
+ #else
268
+ #define CODEC_TRACE(X)
269
+ #endif
270
+
271
+ void sqlite3FreeCodecArg(void *pCodecArg);
272
+
273
+ typedef struct {
274
+ int derive_key;
275
+ EVP_CIPHER *evp_cipher;
276
+ int kdf_iter;
277
+ int key_sz;
278
+ int iv_sz;
279
+ int pass_sz;
280
+ unsigned char *key;
281
+ char *pass;
282
+ } cipher_ctx;
283
+
284
+ typedef struct {
285
+ int kdf_salt_sz;
286
+ int mode_rekey;
287
+ unsigned char *kdf_salt;
288
+ unsigned char *buffer;
289
+ Btree *pBt;
290
+ cipher_ctx *read_ctx;
291
+ cipher_ctx *write_ctx;
292
+ } codec_ctx;
293
+
294
+ static void activate_openssl() {
295
+ sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
296
+ /* if(EVP_get_cipherbyname(CIPHER) == NULL) {
297
+ OpenSSL_add_all_algorithms();
298
+ } */
299
+ sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
300
+ }
301
+
302
+ /*
303
+ ** Simple routines for converting hex char strings to binary data
304
+ */
305
+ static int cipher_hex2int(char c) {
306
+ return (c>='0' && c<='9') ? (c)-'0' :
307
+ (c>='A' && c<='F') ? (c)-'A'+10 :
308
+ (c>='a' && c<='f') ? (c)-'a'+10 : 0;
309
+ }
310
+
311
+ static void cipher_hex2bin(const char *hex, int sz, unsigned char *out){
312
+ int i;
313
+ for(i = 0; i < sz; i += 2){
314
+ out[i/2] = (cipher_hex2int(hex[i])<<4) | cipher_hex2int(hex[i+1]);
315
+ }
316
+ }
317
+
318
+
319
+ /**
320
+ * Free and wipe memory
321
+ * If ptr is not null memory will be freed.
322
+ * If sz is greater than zero, the memory will be overwritten with zero before it is freed
323
+ */
324
+ static void codec_free(void *ptr, int sz) {
325
+ if(ptr) {
326
+ if(sz > 0) memset(ptr, 0, sz); // FIXME - require buffer size
327
+ sqlite3_free(ptr);
328
+ }
329
+ }
330
+
331
+ /**
332
+ * Set the raw password / key data for a cipher context
333
+ *
334
+ * returns SQLITE_OK if assignment was successfull
335
+ * returns SQLITE_NOMEM if an error occured allocating memory
336
+ * returns SQLITE_ERROR if the key couldn't be set because the pass was null or size was zero
337
+ */
338
+ static int cipher_ctx_set_pass(cipher_ctx *ctx, const void *zKey, int nKey) {
339
+ codec_free(ctx->pass, ctx->pass_sz);
340
+ ctx->pass_sz = nKey;
341
+ if(zKey && nKey) {
342
+ ctx->pass = sqlite3Malloc(nKey);
343
+ if(ctx->pass == NULL) return SQLITE_NOMEM;
344
+ memcpy(ctx->pass, zKey, nKey);
345
+ return SQLITE_OK;
346
+ }
347
+ return SQLITE_ERROR;
348
+ }
349
+
350
+ /**
351
+ * Initialize a a new cipher_ctx struct. This function will allocate memory
352
+ * for the cipher context and for the key
353
+ *
354
+ * returns SQLITE_OK if initialization was successful
355
+ * returns SQLITE_NOMEM if an error occured allocating memory
356
+ */
357
+ static int cipher_ctx_init(cipher_ctx **iCtx) {
358
+ cipher_ctx *ctx;
359
+ *iCtx = sqlite3Malloc(sizeof(cipher_ctx));
360
+ ctx = *iCtx;
361
+ if(ctx == NULL) return SQLITE_NOMEM;
362
+ memset(ctx, 0, sizeof(cipher_ctx));
363
+ ctx->key = sqlite3Malloc(EVP_MAX_KEY_LENGTH);
364
+ if(ctx->key == NULL) return SQLITE_NOMEM;
365
+ return SQLITE_OK;
366
+ }
367
+
368
+ /**
369
+ * Free and wipe memory associated with a cipher_ctx
370
+ */
371
+ static void cipher_ctx_free(cipher_ctx **iCtx) {
372
+ cipher_ctx *ctx = *iCtx;
373
+ CODEC_TRACE(("cipher_ctx_free: entered iCtx=%d\n", iCtx));
374
+ codec_free(ctx->key, ctx->key_sz);
375
+ codec_free(ctx->pass, ctx->pass_sz);
376
+ codec_free(ctx, sizeof(cipher_ctx));
377
+ }
378
+
379
+ /**
380
+ * Copy one cipher_ctx to another. For instance, assuming that read_ctx is a
381
+ * fully initialized context, you could copy it to write_ctx and all yet data
382
+ * and pass information across
383
+ *
384
+ * returns SQLITE_OK if initialization was successful
385
+ * returns SQLITE_NOMEM if an error occured allocating memory
386
+ */
387
+ static int cipher_ctx_copy(cipher_ctx *target, cipher_ctx *source) {
388
+ void *key = target->key;
389
+ CODEC_TRACE(("cipher_ctx_copy: entered target=%d, source=%d\n", target, source));
390
+ codec_free(target->pass, target->pass_sz);
391
+ memcpy(target, source, sizeof(cipher_ctx));
392
+
393
+ target->key = key; //restore pointer to previously allocated key data
394
+ memcpy(target->key, source->key, EVP_MAX_KEY_LENGTH);
395
+ target->pass = sqlite3Malloc(source->pass_sz);
396
+ if(target->pass == NULL) return SQLITE_NOMEM;
397
+ memcpy(target->pass, source->pass, source->pass_sz);
398
+ return SQLITE_OK;
399
+ }
400
+
401
+ /**
402
+ * Compare one cipher_ctx to another.
403
+ *
404
+ * returns 0 if all the parameters (except the derived key data) are the same
405
+ * returns 1 otherwise
406
+ */
407
+ static int cipher_ctx_cmp(cipher_ctx *c1, cipher_ctx *c2) {
408
+ CODEC_TRACE(("cipher_ctx_cmp: entered c1=%d c2=%d\n", c1, c2));
409
+
410
+ if(
411
+ c1->evp_cipher == c2->evp_cipher
412
+ && c1->iv_sz == c2->iv_sz
413
+ && c1->kdf_iter == c2->kdf_iter
414
+ && c1->key_sz == c2->key_sz
415
+ && c1->pass_sz == c2->pass_sz
416
+ && (
417
+ c1->pass == c2->pass
418
+ || !memcmp(c1->pass, c2->pass, c1->pass_sz)
419
+ )
420
+ ) return 0;
421
+ return 1;
422
+ }
423
+
424
+ /**
425
+ * Free and wipe memory associated with a cipher_ctx, including the allocated
426
+ * read_ctx and write_ctx.
427
+ */
428
+ static void codec_ctx_free(codec_ctx **iCtx) {
429
+ codec_ctx *ctx = *iCtx;
430
+ CODEC_TRACE(("codec_ctx_free: entered iCtx=%d\n", iCtx));
431
+ codec_free(ctx->kdf_salt, ctx->kdf_salt_sz);
432
+ codec_free(ctx->buffer, 0);
433
+ cipher_ctx_free(&ctx->read_ctx);
434
+ cipher_ctx_free(&ctx->write_ctx);
435
+ codec_free(ctx, sizeof(codec_ctx));
436
+ }
437
+
438
+ /**
439
+ * Derive an encryption key for a cipher contex key based on the raw password.
440
+ *
441
+ * If the raw key data is formated as x'hex' and there are exactly enough hex chars to fill
442
+ * the key space (i.e 64 hex chars for a 256 bit key) then the key data will be used directly.
443
+ *
444
+ * Otherwise, a key data will be derived using PBKDF2
445
+ *
446
+ * returns SQLITE_OK if initialization was successful
447
+ * returns SQLITE_NOMEM if the key could't be derived (for instance if pass is NULL or pass_sz is 0)
448
+ */
449
+ static int codec_key_derive(codec_ctx *ctx, cipher_ctx *c_ctx) {
450
+ CODEC_TRACE(("codec_key_derive: entered c_ctx->pass=%s, c_ctx->pass_sz=%d ctx->kdf_salt=%d ctx->kdf_salt_sz=%d c_ctx->kdf_iter=%d c_ctx->key_sz=%d\n",
451
+ c_ctx->pass, c_ctx->pass_sz, ctx->kdf_salt, ctx->kdf_salt_sz, c_ctx->kdf_iter, c_ctx->key_sz));
452
+
453
+ if(c_ctx->pass && c_ctx->pass_sz) { // if pass is not null
454
+ if (c_ctx->pass_sz == ((c_ctx->key_sz*2)+3) && sqlite3StrNICmp(c_ctx->pass ,"x'", 2) == 0) {
455
+ int n = c_ctx->pass_sz - 3; /* adjust for leading x' and tailing ' */
456
+ const char *z = c_ctx->pass + 2; /* adjust lead offset of x' */
457
+ CODEC_TRACE(("codec_key_derive: deriving key from hex\n"));
458
+ cipher_hex2bin(z, n, c_ctx->key);
459
+ } else {
460
+ CODEC_TRACE(("codec_key_derive: deriving key using PBKDF2\n"));
461
+ PKCS5_PBKDF2_HMAC_SHA1(c_ctx->pass, c_ctx->pass_sz, ctx->kdf_salt, ctx->kdf_salt_sz, c_ctx->kdf_iter, c_ctx->key_sz, c_ctx->key);
462
+ }
463
+ return SQLITE_OK;
464
+ };
465
+ return SQLITE_ERROR;
466
+ }
467
+
468
+ static rho_codec* s_pRhoCodec = 0;
469
+ void rho_set_codec(rho_codec* pRhoCodec)
470
+ {
471
+ s_pRhoCodec = pRhoCodec;
472
+ }
473
+
474
+ /*
475
+ * ctx - codec context
476
+ * pgno - page number in database
477
+ * size - size in bytes of input and output buffers
478
+ * mode - 1 to encrypt, 0 to decrypt
479
+ * in - pointer to input bytes
480
+ * out - pouter to output bytes
481
+ */
482
+ static int codec_cipher(cipher_ctx *ctx, Pgno pgno, int mode, int size, unsigned char *in, unsigned char *out) {
483
+ // EVP_CIPHER_CTX ectx;
484
+ //unsigned char *iv;
485
+ //int tmp_csz, csz;
486
+ //int i;
487
+ //DWORD dwErr, dwType;
488
+
489
+ CODEC_TRACE(("codec_cipher:entered pgno=%d, mode=%d, size=%d\n", pgno, mode, size));
490
+
491
+ if ( s_pRhoCodec )
492
+ {
493
+ if ( mode == CIPHER_ENCRYPT )
494
+ {
495
+ s_pRhoCodec->pEncrypt(size, in, out);
496
+ }
497
+ else
498
+ {
499
+ s_pRhoCodec->pDecrypt(size, in, out);
500
+ }
501
+ }else
502
+ memcpy(out, in, size);
503
+
504
+ /* just copy raw data from in to out when key size is 0
505
+ * i.e. during a rekey of a plaintext database */
506
+ /* if(ctx->key_sz == 0) {
507
+ memcpy(out, in, size);
508
+ return SQLITE_OK;
509
+ }
510
+
511
+ // FIXME - only run if using an IV
512
+ size = size - ctx->iv_sz; // adjust size to useable size and memset reserve at end of page
513
+ iv = out + size;
514
+ if(mode == CIPHER_ENCRYPT) {
515
+ RAND_pseudo_bytes(iv, ctx->iv_sz);
516
+ } else {
517
+ memcpy(iv, in+size, ctx->iv_sz);
518
+ }*/
519
+ /*
520
+ EVP_CipherInit(&ectx, ctx->evp_cipher, NULL, NULL, mode);
521
+ EVP_CIPHER_CTX_set_padding(&ectx, 0);
522
+ EVP_CipherInit(&ectx, NULL, ctx->key, iv, mode);
523
+ EVP_CipherUpdate(&ectx, out, &tmp_csz, in, size);
524
+ csz = tmp_csz;
525
+ out += tmp_csz;
526
+ EVP_CipherFinal(&ectx, out, &tmp_csz);
527
+ csz += tmp_csz;
528
+ EVP_CIPHER_CTX_cleanup(&ectx);
529
+ assert(size == csz);*/
530
+
531
+ //CryptAcquireContext PROV_RSA_AES
532
+ /*
533
+ for (i=0;;i++)
534
+ {
535
+ if (!CryptEnumProviderTypes(i, NULL, 0, &dwType, NULL, NULL))
536
+ {if (ERROR_NO_MORE_ITEMS != (dwErr = GetLastError()))
537
+ {printf("ERROR - CryptEnumProviderTypes : %X\n", dwErr);
538
+ }
539
+ break;
540
+ }
541
+ printf ("Provider Type %d\n", dwType);
542
+ }*/
543
+
544
+ return SQLITE_OK;
545
+ }
546
+
547
+ int codec_set_kdf_iter(sqlite3* db, int nDb, int kdf_iter, int for_ctx) {
548
+ struct Db *pDb = &db->aDb[nDb];
549
+ CODEC_TRACE(("codec_set_kdf_iter: entered db=%d nDb=%d kdf_iter=%d for_ctx=%d\n", db, nDb, kdf_iter, for_ctx));
550
+
551
+ if(pDb->pBt) {
552
+ codec_ctx *ctx;
553
+ cipher_ctx *c_ctx;
554
+ sqlite3pager_get_codec(pDb->pBt->pBt->pPager, (void **) &ctx);
555
+ c_ctx = for_ctx ? ctx->write_ctx : ctx->read_ctx;
556
+
557
+ c_ctx->kdf_iter = kdf_iter;
558
+ c_ctx->derive_key = 1;
559
+
560
+ if(for_ctx == 2) cipher_ctx_copy( for_ctx ? ctx->read_ctx : ctx->write_ctx, c_ctx);
561
+ return SQLITE_OK;
562
+ }
563
+ return SQLITE_ERROR;
564
+ }
565
+
566
+ /**
567
+ *
568
+ * when for_ctx == 0 then it will change for read
569
+ * when for_ctx == 1 then it will change for write
570
+ * when for_ctx == 2 then it will change for both
571
+ */
572
+ int codec_set_cipher_name(sqlite3* db, int nDb, const char *cipher_name, int for_ctx) {
573
+ struct Db *pDb = &db->aDb[nDb];
574
+ CODEC_TRACE(("codec_set_cipher_name: entered db=%d nDb=%d cipher_name=%s for_ctx=%d\n", db, nDb, cipher_name, for_ctx));
575
+
576
+ if(pDb->pBt) {
577
+ codec_ctx *ctx;
578
+ cipher_ctx *c_ctx;
579
+ sqlite3pager_get_codec(pDb->pBt->pBt->pPager, (void **) &ctx);
580
+ c_ctx = for_ctx ? ctx->write_ctx : ctx->read_ctx;
581
+
582
+ c_ctx->evp_cipher = (EVP_CIPHER *) EVP_get_cipherbyname(cipher_name);
583
+ c_ctx->key_sz = EVP_CIPHER_key_length(c_ctx->evp_cipher);
584
+ c_ctx->iv_sz = EVP_CIPHER_iv_length(c_ctx->evp_cipher);
585
+ c_ctx->derive_key = 1;
586
+
587
+ if(for_ctx == 2) cipher_ctx_copy( for_ctx ? ctx->read_ctx : ctx->write_ctx, c_ctx);
588
+ return SQLITE_OK;
589
+ }
590
+ return SQLITE_ERROR;
591
+ }
592
+
593
+ int codec_set_pass_key(sqlite3* db, int nDb, const void *zKey, int nKey, int for_ctx) {
594
+ struct Db *pDb = &db->aDb[nDb];
595
+ CODEC_TRACE(("codec_set_pass_key: entered db=%d nDb=%d cipher_name=%s nKey=%d for_ctx=%d\n", db, nDb, zKey, nKey, for_ctx));
596
+ if(pDb->pBt) {
597
+ codec_ctx *ctx;
598
+ cipher_ctx *c_ctx;
599
+ sqlite3pager_get_codec(pDb->pBt->pBt->pPager, (void **) &ctx);
600
+ c_ctx = for_ctx ? ctx->write_ctx : ctx->read_ctx;
601
+
602
+ cipher_ctx_set_pass(c_ctx, zKey, nKey);
603
+ c_ctx->derive_key = 1;
604
+
605
+ if(for_ctx == 2) cipher_ctx_copy( for_ctx ? ctx->read_ctx : ctx->write_ctx, c_ctx);
606
+ return SQLITE_OK;
607
+ }
608
+ return SQLITE_ERROR;
609
+ }
610
+
611
+ /*
612
+ * sqlite3Codec can be called in multiple modes.
613
+ * encrypt mode - expected to return a pointer to the
614
+ * encrypted data without altering pData.
615
+ * decrypt mode - expected to return a pointer to pData, with
616
+ * the data decrypted in the input buffer
617
+ */
618
+ void* sqlite3Codec(void *iCtx, void *data, Pgno pgno, int mode) {
619
+ codec_ctx *ctx = (codec_ctx *) iCtx;
620
+ int pg_sz = SQLITE_DEFAULT_PAGE_SIZE;
621
+ int offset = 0;
622
+ unsigned char *pData = (unsigned char *) data;
623
+
624
+ CODEC_TRACE(("sqlite3Codec: entered pgno=%d, mode=%d, ctx->mode_rekey=%d, pg_sz=%d\n", pgno, mode, ctx->mode_rekey, pg_sz));
625
+
626
+ /* derive key on first use if necessary */
627
+ if(ctx->read_ctx->derive_key) {
628
+ codec_key_derive(ctx, ctx->read_ctx);
629
+ ctx->read_ctx->derive_key = 0;
630
+ }
631
+
632
+ if(ctx->write_ctx->derive_key) {
633
+ if(cipher_ctx_cmp(ctx->write_ctx, ctx->read_ctx) == 0) {
634
+ cipher_ctx_copy(ctx->write_ctx, ctx->read_ctx); // the relevant parameters are the same, just copy read key
635
+ } else {
636
+ codec_key_derive(ctx, ctx->write_ctx);
637
+ ctx->write_ctx->derive_key = 0;
638
+ }
639
+ }
640
+
641
+
642
+ if(pgno == 1) offset = FILE_HEADER_SZ; /* adjust starting pointers in data page for header offset on first page*/
643
+
644
+ CODEC_TRACE(("sqlite3Codec: switch mode=%d offset=%d\n", mode, offset));
645
+ switch(mode) {
646
+ case 0: /* decrypt */
647
+ case 2:
648
+ case 3:
649
+ if(pgno == 1) memcpy(ctx->buffer, SQLITE_FILE_HEADER, FILE_HEADER_SZ); /* copy file header to the first 16 bytes of the page */
650
+ codec_cipher(ctx->read_ctx, pgno, CIPHER_DECRYPT, pg_sz - offset, pData + offset, ctx->buffer + offset);
651
+ memcpy(pData, ctx->buffer, pg_sz); /* copy buffer data back to pData and return */
652
+ return pData;
653
+ break;
654
+ case 6: /* encrypt */
655
+ if(pgno == 1) memcpy(ctx->buffer, ctx->kdf_salt, FILE_HEADER_SZ); /* copy salt to output buffer */
656
+ codec_cipher(ctx->write_ctx, pgno, CIPHER_ENCRYPT, pg_sz - offset, pData + offset, ctx->buffer + offset);
657
+ return ctx->buffer; /* return persistent buffer data, pData remains intact */
658
+ break;
659
+ case 7:
660
+ if(pgno == 1) memcpy(ctx->buffer, ctx->kdf_salt, FILE_HEADER_SZ); /* copy salt to output buffer */
661
+ codec_cipher(ctx->read_ctx, pgno, CIPHER_ENCRYPT, pg_sz - offset, pData + offset, ctx->buffer + offset);
662
+ return ctx->buffer; /* return persistent buffer data, pData remains intact */
663
+ break;
664
+ default:
665
+ return pData;
666
+ break;
667
+ }
668
+ }
669
+
670
+
671
+ int sqlite3CodecAttach(sqlite3* db, int nDb, const void *zKey, int nKey) {
672
+ struct Db *pDb = &db->aDb[nDb];
673
+
674
+ CODEC_TRACE(("sqlite3CodecAttach: entered nDb=%d zKey=%s, nKey=%d\n", nDb, zKey, nKey));
675
+ activate_openssl();
676
+
677
+ if(nKey && zKey && pDb->pBt) {
678
+ codec_ctx *ctx;
679
+ int rc;
680
+ Pager *pPager = pDb->pBt->pBt->pPager;
681
+ sqlite3_file *fd;
682
+
683
+ ctx = sqlite3Malloc(sizeof(codec_ctx));
684
+ if(ctx == NULL) return SQLITE_NOMEM;
685
+ memset(ctx, 0, sizeof(codec_ctx)); /* initialize all pointers and values to 0 */
686
+
687
+ ctx->pBt = pDb->pBt; /* assign pointer to database btree structure */
688
+
689
+ if((rc = cipher_ctx_init(&ctx->read_ctx)) != SQLITE_OK) return rc;
690
+ if((rc = cipher_ctx_init(&ctx->write_ctx)) != SQLITE_OK) return rc;
691
+
692
+ /* pre-allocate a page buffer of PageSize bytes. This will
693
+ be used as a persistent buffer for encryption and decryption
694
+ operations to avoid overhead of multiple memory allocations*/
695
+ ctx->buffer = sqlite3Malloc(SQLITE_DEFAULT_PAGE_SIZE);
696
+ if(ctx->buffer == NULL) return SQLITE_NOMEM;
697
+
698
+ /* allocate space for salt data. Then read the first 16 bytes
699
+ directly off the database file. This is the salt for the
700
+ key derivation function. If we get a short read allocate
701
+ a new random salt value */
702
+ ctx->kdf_salt_sz = FILE_HEADER_SZ;
703
+ ctx->kdf_salt = sqlite3Malloc(ctx->kdf_salt_sz);
704
+ if(ctx->kdf_salt == NULL) return SQLITE_NOMEM;
705
+
706
+
707
+ fd = sqlite3Pager_get_fd(pPager);
708
+ if(fd == NULL || sqlite3OsRead(fd, ctx->kdf_salt, FILE_HEADER_SZ, 0) != SQLITE_OK) {
709
+ /* if unable to read the bytes, generate random salt */
710
+ RAND_pseudo_bytes(ctx->kdf_salt, FILE_HEADER_SZ);
711
+ }
712
+
713
+ sqlite3pager_sqlite3PagerSetCodec(sqlite3BtreePager(pDb->pBt), sqlite3Codec, NULL, sqlite3FreeCodecArg, (void *) ctx);
714
+
715
+ codec_set_cipher_name(db, nDb, CIPHER, 0);
716
+ codec_set_kdf_iter(db, nDb, PBKDF2_ITER, 0);
717
+ codec_set_pass_key(db, nDb, zKey, nKey, 0);
718
+ cipher_ctx_copy(ctx->write_ctx, ctx->read_ctx);
719
+
720
+ sqlite3_mutex_enter(db->mutex);
721
+
722
+ /* Always overwrite page size and set to the default because the first page of the database
723
+ in encrypted and thus sqlite can't effectively determine the pagesize. this causes an issue in
724
+ cases where bytes 16 & 17 of the page header are a power of 2 as reported by John Lehman
725
+
726
+ Note: before forcing the page size we need to force pageSizeFixed to 0, else
727
+ sqliteBtreeSetPageSize will block the change
728
+ */
729
+ pDb->pBt->pBt->pageSizeFixed = 0;
730
+ sqlite3BtreeSetPageSize(ctx->pBt, SQLITE_DEFAULT_PAGE_SIZE, EVP_MAX_IV_LENGTH, 0);
731
+
732
+ /* if fd is null, then this is an in-memory database and
733
+ we dont' want to overwrite the AutoVacuum settings
734
+ if not null, then set to the default */
735
+ if(fd != NULL) {
736
+ sqlite3BtreeSetAutoVacuum(ctx->pBt, SQLITE_DEFAULT_AUTOVACUUM);
737
+ }
738
+
739
+ sqlite3_mutex_leave(db->mutex);
740
+ }
741
+ return SQLITE_OK;
742
+ }
743
+
744
+ void sqlite3FreeCodecArg(void *pCodecArg) {
745
+ codec_ctx *ctx = (codec_ctx *) pCodecArg;
746
+ if(pCodecArg == NULL) return;
747
+ codec_ctx_free(&ctx); // wipe and free allocated memory for the context
748
+ }
749
+
750
+ void sqlite3_activate_see(const char* in) {
751
+ /* do nothing, security enhancements are always active */
752
+ }
753
+
754
+ int sqlite3_key(sqlite3 *db, const void *pKey, int nKey) {
755
+ CODEC_TRACE(("sqlite3_key: entered db=%d pKey=%s nKey=%d\n", db, pKey, nKey));
756
+ /* attach key if db and pKey are not null and nKey is > 0 */
757
+ if(db && pKey && nKey) {
758
+ sqlite3CodecAttach(db, 0, pKey, nKey); // operate only on the main db
759
+ return SQLITE_OK;
760
+ }
761
+ return SQLITE_ERROR;
762
+ }
763
+
764
+ /* sqlite3_rekey
765
+ ** Given a database, this will reencrypt the database using a new key.
766
+ ** There are two possible modes of operation. The first is rekeying
767
+ ** an existing database that was not previously encrypted. The second
768
+ ** is to change the key on an existing database.
769
+ **
770
+ ** The proposed logic for this function follows:
771
+ ** 1. Determine if there is already a key present
772
+ ** 2. If there is NOT already a key present, create one and attach a codec (key would be null)
773
+ ** 3. Initialize a ctx->rekey parameter of the codec
774
+ **
775
+ ** Note: this will require modifications to the sqlite3Codec to support rekey
776
+ **
777
+ */
778
+ int sqlite3_rekey(sqlite3 *db, const void *pKey, int nKey) {
779
+ CODEC_TRACE(("sqlite3_rekey: entered db=%d pKey=%s, nKey=%d\n", db, pKey, nKey));
780
+ activate_openssl();
781
+ if(db && pKey && nKey) {
782
+ struct Db *pDb = &db->aDb[0];
783
+ CODEC_TRACE(("sqlite3_rekey: database pDb=%d\n", pDb));
784
+ if(pDb->pBt) {
785
+ codec_ctx *ctx;
786
+ int rc, page_count;
787
+ Pgno pgno;
788
+ PgHdr *page;
789
+ Pager *pPager = pDb->pBt->pBt->pPager;
790
+
791
+ sqlite3pager_get_codec(pDb->pBt->pBt->pPager, (void **) &ctx);
792
+
793
+ if(ctx == NULL) {
794
+ CODEC_TRACE(("sqlite3_rekey: no codec attached to db, attaching now\n"));
795
+ /* there was no codec attached to this database,so attach one now with a null password */
796
+ sqlite3CodecAttach(db, 0, pKey, nKey);
797
+ sqlite3pager_get_codec(pDb->pBt->pBt->pPager, (void **) &ctx);
798
+
799
+ /* prepare this setup as if it had already been initialized */
800
+ RAND_pseudo_bytes(ctx->kdf_salt, ctx->kdf_salt_sz);
801
+ ctx->read_ctx->key_sz = ctx->read_ctx->iv_sz = ctx->read_ctx->pass_sz = 0;
802
+ }
803
+
804
+ sqlite3_mutex_enter(db->mutex);
805
+
806
+ if(ctx->read_ctx->iv_sz != ctx->write_ctx->iv_sz) {
807
+ char *error;
808
+ CODEC_TRACE(("sqlite3_rekey: updating page size for iv_sz change from %d to %d\n", ctx->read_ctx->iv_sz, ctx->write_ctx->iv_sz));
809
+ db->nextPagesize = SQLITE_DEFAULT_PAGE_SIZE;
810
+ pDb->pBt->pBt->pageSizeFixed = 0; /* required for sqlite3BtreeSetPageSize to modify pagesize setting */
811
+ sqlite3BtreeSetPageSize(pDb->pBt, db->nextPagesize, EVP_MAX_IV_LENGTH, 0);
812
+ sqlite3RunVacuum(&error, db);
813
+ }
814
+
815
+ codec_set_pass_key(db, 0, pKey, nKey, 1);
816
+ ctx->mode_rekey = 1;
817
+
818
+ /* do stuff here to rewrite the database
819
+ ** 1. Create a transaction on the database
820
+ ** 2. Iterate through each page, reading it and then writing it.
821
+ ** 3. If that goes ok then commit and put ctx->rekey into ctx->key
822
+ ** note: don't deallocate rekey since it may be used in a subsequent iteration
823
+ */
824
+ rc = sqlite3BtreeBeginTrans(pDb->pBt, 1); /* begin write transaction */
825
+ sqlite3PagerPagecount(pPager, &page_count);
826
+ for(pgno = 1; rc == SQLITE_OK && pgno <= page_count; pgno++) { /* pgno's start at 1 see pager.c:pagerAcquire */
827
+ if(!sqlite3pager_is_mj_pgno(pPager, pgno)) { /* skip this page (see pager.c:pagerAcquire for reasoning) */
828
+ rc = sqlite3PagerGet(pPager, pgno, &page);
829
+ if(rc == SQLITE_OK) { /* write page see pager_incr_changecounter for example */
830
+ rc = sqlite3PagerWrite(page);
831
+ //printf("sqlite3PagerWrite(%d)\n", pgno);
832
+ if(rc == SQLITE_OK) {
833
+ sqlite3PagerUnref(page);
834
+ }
835
+ }
836
+ }
837
+ }
838
+
839
+ /* if commit was successful commit and copy the rekey data to current key, else rollback to release locks */
840
+ if(rc == SQLITE_OK) {
841
+ CODEC_TRACE(("sqlite3_rekey: committing\n"));
842
+ db->nextPagesize = SQLITE_DEFAULT_PAGE_SIZE;
843
+ rc = sqlite3BtreeCommit(pDb->pBt);
844
+ cipher_ctx_copy(ctx->read_ctx, ctx->write_ctx);
845
+ } else {
846
+ CODEC_TRACE(("sqlite3_rekey: rollback\n"));
847
+ sqlite3BtreeRollback(pDb->pBt);
848
+ }
849
+
850
+ ctx->mode_rekey = 0;
851
+ sqlite3_mutex_leave(db->mutex);
852
+ }
853
+ return SQLITE_OK;
854
+ }
855
+ return SQLITE_ERROR;
856
+ }
857
+
858
+ void sqlite3CodecGetKey(sqlite3* db, int nDb, void **zKey, int *nKey) {
859
+ struct Db *pDb = &db->aDb[nDb];
860
+ CODEC_TRACE(("sqlite3CodecGetKey: entered db=%d, nDb=%d\n", db, nDb));
861
+
862
+ if( pDb->pBt ) {
863
+ codec_ctx *ctx;
864
+ sqlite3pager_get_codec(pDb->pBt->pBt->pPager, (void **) &ctx);
865
+
866
+ if(ctx) { /* if the codec has an attached codec_context user the raw key data */
867
+ *zKey = ctx->read_ctx->pass;
868
+ *nKey = ctx->read_ctx->pass_sz;
869
+ } else {
870
+ *zKey = NULL;
871
+ *nKey = 0;
872
+ }
873
+ }
874
+ }
875
+
876
+
877
+ /* BEGIN CRYPTO */
878
+ #ifdef SQLITE_HAS_CODEC
879
+ void sqlite3pager_get_codec(Pager *pPager, void **ctx) {
880
+ *ctx = pPager->pCodec;
881
+ }
882
+
883
+ int sqlite3pager_is_mj_pgno(Pager *pPager, Pgno pgno) {
884
+ return (PAGER_MJ_PGNO(pPager) == pgno) ? 1 : 0;
885
+ }
886
+
887
+ sqlite3_file *sqlite3Pager_get_fd(Pager *pPager) {
888
+ return (isOpen(pPager->fd)) ? pPager->fd : NULL;
889
+ }
890
+
891
+ void sqlite3pager_sqlite3PagerSetCodec(
892
+ Pager *pPager,
893
+ void *(*xCodec)(void*,void*,Pgno,int),
894
+ void (*xCodecSizeChng)(void*,int,int),
895
+ void (*xCodecFree)(void*),
896
+ void *pCodec
897
+ ){
898
+ sqlite3PagerSetCodec(pPager, xCodec, xCodecSizeChng, xCodecFree, pCodec);
899
+ }
900
+
901
+
902
+ #endif
903
+ /* END CRYPTO */
904
+
905
+ /* END CRYPTO */
906
+ #endif
907
+ #endif //0