oci8_lob.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2016 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Stig Sæther Bakken <ssb@php.net> |
  16. | Thies C. Arntzen <thies@thieso.net> |
  17. | |
  18. | Collection support by Andy Sautins <asautins@veripost.net> |
  19. | Temporary LOB support by David Benson <dbenson@mancala.com> |
  20. | ZTS per process OCIPLogon by Harald Radi <harald.radi@nme.at> |
  21. | |
  22. | Redesigned by: Antony Dovgal <antony@zend.com> |
  23. | Andi Gutmans <andi@zend.com> |
  24. | Wez Furlong <wez@omniti.com> |
  25. +----------------------------------------------------------------------+
  26. */
  27. /* $Id$ */
  28. #ifdef HAVE_CONFIG_H
  29. #include "config.h"
  30. #endif
  31. #include "php.h"
  32. #include "ext/standard/info.h"
  33. #include "php_ini.h"
  34. #if HAVE_OCI8
  35. #include "php_oci8.h"
  36. #include "php_oci8_int.h"
  37. /* for import/export functions */
  38. #include <fcntl.h>
  39. #ifndef O_BINARY
  40. #define O_BINARY 0
  41. #endif
  42. /* {{{ php_oci_lob_create()
  43. Create LOB descriptor and allocate all the resources needed */
  44. php_oci_descriptor *php_oci_lob_create (php_oci_connection *connection, long type TSRMLS_DC)
  45. {
  46. php_oci_descriptor *descriptor;
  47. sword errstatus;
  48. switch (type) {
  49. case OCI_DTYPE_FILE:
  50. case OCI_DTYPE_LOB:
  51. case OCI_DTYPE_ROWID:
  52. /* these three are allowed */
  53. break;
  54. default:
  55. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown descriptor type %ld", type);
  56. return NULL;
  57. break;
  58. }
  59. descriptor = ecalloc(1, sizeof(php_oci_descriptor));
  60. descriptor->type = type;
  61. descriptor->connection = connection;
  62. zend_list_addref(descriptor->connection->id);
  63. PHP_OCI_CALL_RETURN(errstatus, OCIDescriptorAlloc, (connection->env, (dvoid*)&(descriptor->descriptor), descriptor->type, (size_t) 0, (dvoid **) 0));
  64. if (errstatus != OCI_SUCCESS) {
  65. OCI_G(errcode) = php_oci_error(OCI_G(err), errstatus TSRMLS_CC);
  66. PHP_OCI_HANDLE_ERROR(connection, OCI_G(errcode));
  67. efree(descriptor);
  68. return NULL;
  69. } else {
  70. OCI_G(errcode) = 0; /* retain backwards compat with OCI8 1.4 */
  71. }
  72. PHP_OCI_REGISTER_RESOURCE(descriptor, le_descriptor);
  73. descriptor->lob_current_position = 0;
  74. descriptor->lob_size = -1; /* we should set it to -1 to know, that it's just not initialized */
  75. descriptor->buffering = PHP_OCI_LOB_BUFFER_DISABLED; /* buffering is off by default */
  76. descriptor->charset_form = SQLCS_IMPLICIT; /* default value */
  77. descriptor->charset_id = connection->charset;
  78. descriptor->is_open = 0;
  79. descriptor->chunk_size = 0;
  80. if (descriptor->type == OCI_DTYPE_LOB || descriptor->type == OCI_DTYPE_FILE) {
  81. /* add Lobs & Files to hash. we'll flush them at the end */
  82. if (!connection->descriptors) {
  83. ALLOC_HASHTABLE(connection->descriptors);
  84. zend_hash_init(connection->descriptors, 0, NULL, php_oci_descriptor_flush_hash_dtor, 0);
  85. connection->descriptor_count = 0;
  86. }
  87. descriptor->index = (connection->descriptor_count)++;
  88. if (connection->descriptor_count == LONG_MAX) {
  89. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Internal descriptor counter has reached limit");
  90. php_oci_connection_descriptors_free(connection TSRMLS_CC);
  91. return NULL;
  92. }
  93. zend_hash_index_update(connection->descriptors,descriptor->index,&descriptor,sizeof(php_oci_descriptor *),NULL);
  94. }
  95. return descriptor;
  96. }
  97. /* }}} */
  98. /* {{{ php_oci_lob_get_length()
  99. Get length of the LOB. The length is cached so we don't need to ask Oracle every time */
  100. int php_oci_lob_get_length (php_oci_descriptor *descriptor, ub4 *length TSRMLS_DC)
  101. {
  102. php_oci_connection *connection = descriptor->connection;
  103. sword errstatus;
  104. *length = 0;
  105. if (descriptor->lob_size >= 0) {
  106. *length = descriptor->lob_size;
  107. return 0;
  108. } else {
  109. if (descriptor->type == OCI_DTYPE_FILE) {
  110. PHP_OCI_CALL_RETURN(errstatus, OCILobFileOpen, (connection->svc, connection->err, descriptor->descriptor, OCI_FILE_READONLY));
  111. if (errstatus != OCI_SUCCESS) {
  112. connection->errcode = php_oci_error(connection->err, errstatus TSRMLS_CC);
  113. PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
  114. return 1;
  115. }
  116. }
  117. PHP_OCI_CALL_RETURN(errstatus, OCILobGetLength, (connection->svc, connection->err, descriptor->descriptor, (ub4 *)length));
  118. if (errstatus != OCI_SUCCESS) {
  119. connection->errcode = php_oci_error(connection->err, errstatus TSRMLS_CC);
  120. PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
  121. return 1;
  122. }
  123. descriptor->lob_size = *length;
  124. if (descriptor->type == OCI_DTYPE_FILE) {
  125. PHP_OCI_CALL_RETURN(errstatus, OCILobFileClose, (connection->svc, connection->err, descriptor->descriptor));
  126. if (errstatus != OCI_SUCCESS) {
  127. connection->errcode = php_oci_error(connection->err, errstatus TSRMLS_CC);
  128. PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
  129. return 1;
  130. }
  131. }
  132. connection->errcode = 0; /* retain backwards compat with OCI8 1.4 */
  133. }
  134. return 0;
  135. }
  136. /* }}} */
  137. /* {{{ php_oci_lob_callback()
  138. Append LOB portion to a memory buffer */
  139. sb4 php_oci_lob_callback (dvoid *ctxp, CONST dvoid *bufxp, oraub8 len, ub1 piece, dvoid **changed_bufpp, oraub8 *changed_lenp)
  140. {
  141. ub4 lenp = (ub4) len;
  142. php_oci_lob_ctx *ctx = (php_oci_lob_ctx *)ctxp;
  143. switch (piece)
  144. {
  145. case OCI_LAST_PIECE:
  146. if ((*(ctx->lob_len) + lenp) > (ctx->alloc_len)) {
  147. /* this should not happen ever */
  148. *(ctx->lob_data) = NULL;
  149. *(ctx->lob_len) = 0;
  150. return OCI_ERROR;
  151. }
  152. memcpy(*(ctx->lob_data) + *(ctx->lob_len), bufxp, (size_t) lenp);
  153. *(ctx->lob_len) += lenp;
  154. *(*(ctx->lob_data) + *(ctx->lob_len)) = 0x00;
  155. return OCI_CONTINUE;
  156. case OCI_FIRST_PIECE:
  157. case OCI_NEXT_PIECE:
  158. if ((*(ctx->lob_len) + lenp) > ctx->alloc_len) {
  159. /* this should not happen ever */
  160. *(ctx->lob_data) = NULL;
  161. *(ctx->lob_len) = 0;
  162. return OCI_ERROR;
  163. }
  164. memcpy(*(ctx->lob_data) + *(ctx->lob_len), bufxp, (size_t) lenp);
  165. *(ctx->lob_len) += lenp;
  166. return OCI_CONTINUE;
  167. default: {
  168. TSRMLS_FETCH();
  169. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unexpected LOB piece id received (value:%d)", piece);
  170. *(ctx->lob_data) = NULL;
  171. *(ctx->lob_len) = 0;
  172. return OCI_ERROR;
  173. }
  174. }
  175. }
  176. /* }}} */
  177. /* {{{ php_oci_lob_calculate_buffer()
  178. Work out the size for LOB buffering */
  179. static inline int php_oci_lob_calculate_buffer(php_oci_descriptor *descriptor, long read_length TSRMLS_DC)
  180. {
  181. php_oci_connection *connection = descriptor->connection;
  182. ub4 chunk_size;
  183. sword errstatus;
  184. if (descriptor->type == OCI_DTYPE_FILE) {
  185. return read_length;
  186. }
  187. if (!descriptor->chunk_size) {
  188. PHP_OCI_CALL_RETURN(errstatus, OCILobGetChunkSize, (connection->svc, connection->err, descriptor->descriptor, &chunk_size));
  189. if (errstatus != OCI_SUCCESS) {
  190. connection->errcode = php_oci_error(connection->err, errstatus TSRMLS_CC);
  191. PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
  192. return read_length; /* we have to return original length here */
  193. }
  194. descriptor->chunk_size = chunk_size;
  195. connection->errcode = 0; /* retain backwards compat with OCI8 1.4 */
  196. }
  197. if ((read_length % descriptor->chunk_size) != 0) {
  198. return descriptor->chunk_size * ((read_length / descriptor->chunk_size) + 1);
  199. }
  200. return read_length;
  201. }
  202. /* }}} */
  203. /* {{{ php_oci_lob_read()
  204. Read specified portion of the LOB into the buffer */
  205. int php_oci_lob_read (php_oci_descriptor *descriptor, long read_length, long initial_offset, char **data, ub4 *data_len TSRMLS_DC)
  206. {
  207. php_oci_connection *connection = descriptor->connection;
  208. ub4 length = 0;
  209. int buffer_size = PHP_OCI_LOB_BUFFER_SIZE;
  210. php_oci_lob_ctx ctx;
  211. ub1 *bufp;
  212. oraub8 bytes_read, offset = 0;
  213. oraub8 requested_len = read_length; /* this is by default */
  214. oraub8 chars_read = 0;
  215. int is_clob = 0;
  216. sb4 bytes_per_char = 1;
  217. sword errstatus;
  218. *data_len = 0;
  219. *data = NULL;
  220. ctx.lob_len = data_len;
  221. ctx.lob_data = data;
  222. ctx.alloc_len = 0;
  223. if (php_oci_lob_get_length(descriptor, &length TSRMLS_CC)) {
  224. return 1;
  225. }
  226. if (length <= 0) {
  227. return 0;
  228. }
  229. if (initial_offset > length) {
  230. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Offset must be less than size of the LOB");
  231. return 1;
  232. }
  233. if (read_length == -1) {
  234. requested_len = length;
  235. }
  236. if (requested_len > (length - initial_offset)) {
  237. requested_len = length - initial_offset;
  238. }
  239. if (requested_len <= 0) {
  240. return 0;
  241. }
  242. offset = initial_offset;
  243. if (descriptor->type == OCI_DTYPE_FILE) {
  244. PHP_OCI_CALL_RETURN(errstatus, OCILobFileOpen, (connection->svc, connection->err, descriptor->descriptor, OCI_FILE_READONLY));
  245. if (errstatus != OCI_SUCCESS) {
  246. connection->errcode = php_oci_error(connection->err, errstatus TSRMLS_CC);
  247. PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
  248. return 1;
  249. }
  250. } else {
  251. ub2 charset_id = 0;
  252. PHP_OCI_CALL_RETURN(errstatus, OCILobCharSetId, (connection->env, connection->err, descriptor->descriptor, &charset_id));
  253. if (errstatus != OCI_SUCCESS) {
  254. connection->errcode = php_oci_error(connection->err, errstatus TSRMLS_CC);
  255. PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
  256. return 1;
  257. }
  258. if (charset_id > 0) { /* charset_id is always > 0 for [N]CLOBs */
  259. is_clob = 1;
  260. }
  261. }
  262. if (is_clob) {
  263. PHP_OCI_CALL_RETURN(errstatus, OCINlsNumericInfoGet, (connection->env, connection->err, &bytes_per_char, OCI_NLS_CHARSET_MAXBYTESZ));
  264. if (errstatus != OCI_SUCCESS) {
  265. connection->errcode = php_oci_error(connection->err, errstatus TSRMLS_CC);
  266. PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
  267. return 1;
  268. }
  269. } else {
  270. /* BLOBs don't have encoding, so bytes_per_char == 1 */
  271. }
  272. ctx.alloc_len = (requested_len + 1) * bytes_per_char;
  273. *data = ecalloc(bytes_per_char, requested_len + 1);
  274. if (is_clob) {
  275. chars_read = requested_len;
  276. bytes_read = 0;
  277. } else {
  278. chars_read = 0;
  279. bytes_read = requested_len;
  280. }
  281. buffer_size = (requested_len < buffer_size ) ? requested_len : buffer_size; /* optimize buffer size */
  282. buffer_size = php_oci_lob_calculate_buffer(descriptor, buffer_size TSRMLS_CC); /* use chunk size */
  283. bufp = (ub1 *) ecalloc(1, buffer_size);
  284. PHP_OCI_CALL_RETURN(errstatus, OCILobRead2,
  285. (
  286. connection->svc,
  287. connection->err,
  288. descriptor->descriptor,
  289. (oraub8 *)&bytes_read, /* IN/OUT bytes toread/read */
  290. (oraub8 *)&chars_read, /* IN/OUT chars toread/read */
  291. (oraub8) offset + 1, /* offset (starts with 1) */
  292. (dvoid *) bufp,
  293. (oraub8) buffer_size, /* size of buffer */
  294. OCI_FIRST_PIECE,
  295. (dvoid *)&ctx,
  296. (OCICallbackLobRead2) php_oci_lob_callback, /* callback... */
  297. (ub2) descriptor->charset_id, /* The character set ID of the buffer data. */
  298. (ub1) descriptor->charset_form /* The character set form of the buffer data. */
  299. )
  300. );
  301. efree(bufp);
  302. if (is_clob) {
  303. offset = descriptor->lob_current_position + chars_read;
  304. } else {
  305. offset = descriptor->lob_current_position + bytes_read;
  306. }
  307. if (errstatus != OCI_SUCCESS) {
  308. connection->errcode = php_oci_error(connection->err, errstatus TSRMLS_CC);
  309. PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
  310. if (*data) {
  311. efree(*data);
  312. *data = NULL;
  313. }
  314. *data_len = 0;
  315. return 1;
  316. }
  317. descriptor->lob_current_position = (int)offset;
  318. if (descriptor->type == OCI_DTYPE_FILE) {
  319. PHP_OCI_CALL_RETURN(errstatus, OCILobFileClose, (connection->svc, connection->err, descriptor->descriptor));
  320. if (errstatus != OCI_SUCCESS) {
  321. connection->errcode = php_oci_error(connection->err, errstatus TSRMLS_CC);
  322. PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
  323. if (*data) {
  324. efree(*data);
  325. *data = NULL;
  326. }
  327. *data_len = 0;
  328. return 1;
  329. }
  330. }
  331. connection->errcode = 0; /* retain backwards compat with OCI8 1.4 */
  332. return 0;
  333. }
  334. /* }}} */
  335. /* {{{ php_oci_lob_write()
  336. Write data to the LOB */
  337. int php_oci_lob_write (php_oci_descriptor *descriptor, ub4 offset, char *data, int data_len, ub4 *bytes_written TSRMLS_DC)
  338. {
  339. OCILobLocator *lob = (OCILobLocator *) descriptor->descriptor;
  340. php_oci_connection *connection = (php_oci_connection *) descriptor->connection;
  341. ub4 lob_length;
  342. sword errstatus;
  343. *bytes_written = 0;
  344. if (php_oci_lob_get_length(descriptor, &lob_length TSRMLS_CC)) {
  345. return 1;
  346. }
  347. if (!data || data_len <= 0) {
  348. return 0;
  349. }
  350. if (offset > descriptor->lob_current_position) {
  351. offset = descriptor->lob_current_position;
  352. }
  353. PHP_OCI_CALL_RETURN(errstatus, OCILobWrite,
  354. (
  355. connection->svc,
  356. connection->err,
  357. lob,
  358. (ub4 *)&data_len,
  359. (ub4) offset + 1,
  360. (dvoid *) data,
  361. (ub4) data_len,
  362. OCI_ONE_PIECE,
  363. (dvoid *)0,
  364. (OCICallbackLobWrite) 0,
  365. (ub2) descriptor->charset_id,
  366. (ub1) descriptor->charset_form
  367. )
  368. );
  369. if (errstatus) {
  370. connection->errcode = php_oci_error(connection->err, errstatus TSRMLS_CC);
  371. PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
  372. *bytes_written = 0;
  373. return 1;
  374. }
  375. *bytes_written = data_len;
  376. descriptor->lob_current_position += data_len;
  377. if (descriptor->lob_current_position > descriptor->lob_size) {
  378. descriptor->lob_size = descriptor->lob_current_position;
  379. }
  380. /* marking buffer as used */
  381. if (descriptor->buffering == PHP_OCI_LOB_BUFFER_ENABLED) {
  382. descriptor->buffering = PHP_OCI_LOB_BUFFER_USED;
  383. }
  384. connection->errcode = 0; /* retain backwards compat with OCI8 1.4 */
  385. return 0;
  386. }
  387. /* }}} */
  388. /* {{{ php_oci_lob_set_buffering()
  389. Turn buffering off/onn for this particular LOB */
  390. int php_oci_lob_set_buffering (php_oci_descriptor *descriptor, int on_off TSRMLS_DC)
  391. {
  392. php_oci_connection *connection = descriptor->connection;
  393. sword errstatus;
  394. if (!on_off && descriptor->buffering == PHP_OCI_LOB_BUFFER_DISABLED) {
  395. /* disabling when it's already off */
  396. return 0;
  397. }
  398. if (on_off && descriptor->buffering != PHP_OCI_LOB_BUFFER_DISABLED) {
  399. /* enabling when it's already on */
  400. return 0;
  401. }
  402. if (on_off) {
  403. PHP_OCI_CALL_RETURN(errstatus, OCILobEnableBuffering, (connection->svc, connection->err, descriptor->descriptor));
  404. } else {
  405. PHP_OCI_CALL_RETURN(errstatus, OCILobDisableBuffering, (connection->svc, connection->err, descriptor->descriptor));
  406. }
  407. if (errstatus != OCI_SUCCESS) {
  408. connection->errcode = php_oci_error(connection->err, errstatus TSRMLS_CC);
  409. PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
  410. return 1;
  411. }
  412. descriptor->buffering = on_off ? PHP_OCI_LOB_BUFFER_ENABLED : PHP_OCI_LOB_BUFFER_DISABLED;
  413. connection->errcode = 0; /* retain backwards compat with OCI8 1.4 */
  414. return 0;
  415. }
  416. /* }}} */
  417. /* {{{ php_oci_lob_get_buffering()
  418. Return current buffering state for the LOB */
  419. int php_oci_lob_get_buffering (php_oci_descriptor *descriptor)
  420. {
  421. if (descriptor->buffering != PHP_OCI_LOB_BUFFER_DISABLED) {
  422. return 1;
  423. } else {
  424. return 0;
  425. }
  426. }
  427. /* }}} */
  428. /* {{{ php_oci_lob_copy()
  429. Copy one LOB (or its part) to another one */
  430. int php_oci_lob_copy (php_oci_descriptor *descriptor_dest, php_oci_descriptor *descriptor_from, long length TSRMLS_DC)
  431. {
  432. php_oci_connection *connection = descriptor_dest->connection;
  433. ub4 length_dest, length_from, copy_len;
  434. sword errstatus;
  435. if (php_oci_lob_get_length(descriptor_dest, &length_dest TSRMLS_CC)) {
  436. return 1;
  437. }
  438. if (php_oci_lob_get_length(descriptor_from, &length_from TSRMLS_CC)) {
  439. return 1;
  440. }
  441. if (length == -1) {
  442. copy_len = length_from - descriptor_from->lob_current_position;
  443. } else {
  444. copy_len = length;
  445. }
  446. if ((int)copy_len <= 0) {
  447. /* silently fail, there is nothing to copy */
  448. return 1;
  449. }
  450. PHP_OCI_CALL_RETURN(errstatus, OCILobCopy,
  451. (
  452. connection->svc,
  453. connection->err,
  454. descriptor_dest->descriptor,
  455. descriptor_from->descriptor,
  456. copy_len,
  457. descriptor_dest->lob_current_position+1,
  458. descriptor_from->lob_current_position+1
  459. )
  460. );
  461. if (errstatus != OCI_SUCCESS) {
  462. connection->errcode = php_oci_error(connection->err, errstatus TSRMLS_CC);
  463. PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
  464. return 1;
  465. }
  466. connection->errcode = 0; /* retain backwards compat with OCI8 1.4 */
  467. return 0;
  468. }
  469. /* }}} */
  470. /* {{{ php_oci_lob_close()
  471. Close LOB */
  472. int php_oci_lob_close (php_oci_descriptor *descriptor TSRMLS_DC)
  473. {
  474. php_oci_connection *connection = descriptor->connection;
  475. sword errstatus;
  476. if (descriptor->is_open) {
  477. PHP_OCI_CALL_RETURN(errstatus, OCILobClose, (connection->svc, connection->err, descriptor->descriptor));
  478. if (errstatus != OCI_SUCCESS) {
  479. connection->errcode = php_oci_error(connection->err, errstatus TSRMLS_CC);
  480. PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
  481. return 1;
  482. }
  483. connection->errcode = 0; /* retain backwards compat with OCI8 1.4 */
  484. }
  485. if (php_oci_temp_lob_close(descriptor TSRMLS_CC)) {
  486. return 1;
  487. }
  488. return 0;
  489. }
  490. /* }}} */
  491. /* {{{ php_oci_temp_lob_close()
  492. Close Temporary LOB */
  493. int php_oci_temp_lob_close (php_oci_descriptor *descriptor TSRMLS_DC)
  494. {
  495. php_oci_connection *connection = descriptor->connection;
  496. int is_temporary;
  497. sword errstatus;
  498. PHP_OCI_CALL_RETURN(errstatus, OCILobIsTemporary, (connection->env,connection->err, descriptor->descriptor, &is_temporary));
  499. if (errstatus != OCI_SUCCESS) {
  500. connection->errcode = php_oci_error(connection->err, errstatus TSRMLS_CC);
  501. PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
  502. return 1;
  503. }
  504. if (is_temporary) {
  505. PHP_OCI_CALL_RETURN(errstatus, OCILobFreeTemporary, (connection->svc, connection->err, descriptor->descriptor));
  506. if (errstatus != OCI_SUCCESS) {
  507. connection->errcode = php_oci_error(connection->err, errstatus TSRMLS_CC);
  508. PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
  509. return 1;
  510. }
  511. }
  512. connection->errcode = 0; /* retain backwards compat with OCI8 1.4 */
  513. return 0;
  514. }
  515. /* }}} */
  516. /* {{{ php_oci_lob_flush()
  517. Flush buffers for the LOB (only if they have been used) */
  518. int php_oci_lob_flush(php_oci_descriptor *descriptor, long flush_flag TSRMLS_DC)
  519. {
  520. OCILobLocator *lob = descriptor->descriptor;
  521. php_oci_connection *connection = descriptor->connection;
  522. sword errstatus;
  523. if (!lob) {
  524. return 1;
  525. }
  526. switch (flush_flag) {
  527. case 0:
  528. case OCI_LOB_BUFFER_FREE:
  529. /* only these two are allowed */
  530. break;
  531. default:
  532. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid flag value: %ld", flush_flag);
  533. return 1;
  534. break;
  535. }
  536. /* do not really flush buffer, but report success
  537. * to suppress OCI error when flushing not used buffer
  538. * */
  539. if (descriptor->buffering != PHP_OCI_LOB_BUFFER_USED) {
  540. return 0;
  541. }
  542. PHP_OCI_CALL_RETURN(errstatus, OCILobFlushBuffer, (connection->svc, connection->err, lob, flush_flag));
  543. if (errstatus != OCI_SUCCESS) {
  544. connection->errcode = php_oci_error(connection->err, errstatus TSRMLS_CC);
  545. PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
  546. return 1;
  547. }
  548. /* marking buffer as enabled and not used */
  549. descriptor->buffering = PHP_OCI_LOB_BUFFER_ENABLED;
  550. connection->errcode = 0; /* retain backwards compat with OCI8 1.4 */
  551. return 0;
  552. }
  553. /* }}} */
  554. /* {{{ php_oci_lob_free()
  555. Close LOB descriptor and free associated resources */
  556. void php_oci_lob_free (php_oci_descriptor *descriptor TSRMLS_DC)
  557. {
  558. if (!descriptor || !descriptor->connection) {
  559. return;
  560. }
  561. if (descriptor->connection->descriptors) {
  562. /* delete descriptor from the hash */
  563. zend_hash_index_del(descriptor->connection->descriptors, descriptor->index);
  564. if (zend_hash_num_elements(descriptor->connection->descriptors) == 0) {
  565. descriptor->connection->descriptor_count = 0;
  566. } else {
  567. if (descriptor->index + 1 == descriptor->connection->descriptor_count) {
  568. /* If the descriptor being freed is the end-most one
  569. * allocated, then the descriptor_count is reduced so
  570. * a future descriptor can reuse the hash table index.
  571. * This can prevent the hash index range increasing in
  572. * the common case that each descriptor is
  573. * allocated/used/freed before another descriptor is
  574. * needed. However it is possible that a script frees
  575. * descriptors in arbitrary order which would prevent
  576. * descriptor_count ever being reduced to zero until
  577. * zend_hash_num_elements() returns 0.
  578. */
  579. descriptor->connection->descriptor_count--;
  580. }
  581. }
  582. }
  583. /* flushing Lobs & Files with buffering enabled */
  584. if ((descriptor->type == OCI_DTYPE_FILE || descriptor->type == OCI_DTYPE_LOB) && descriptor->buffering == PHP_OCI_LOB_BUFFER_USED) {
  585. php_oci_lob_flush(descriptor, OCI_LOB_BUFFER_FREE TSRMLS_CC);
  586. }
  587. if (descriptor->type == OCI_DTYPE_LOB) {
  588. php_oci_temp_lob_close(descriptor TSRMLS_CC);
  589. }
  590. PHP_OCI_CALL(OCIDescriptorFree, (descriptor->descriptor, descriptor->type));
  591. zend_list_delete(descriptor->connection->id);
  592. efree(descriptor);
  593. }
  594. /* }}} */
  595. /* {{{ php_oci_lob_import()
  596. Import LOB contents from the given file */
  597. int php_oci_lob_import (php_oci_descriptor *descriptor, char *filename TSRMLS_DC)
  598. {
  599. int fp;
  600. ub4 loblen;
  601. OCILobLocator *lob = (OCILobLocator *)descriptor->descriptor;
  602. php_oci_connection *connection = descriptor->connection;
  603. char buf[8192];
  604. ub4 offset = 1;
  605. sword errstatus;
  606. #if (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION > 3) || (PHP_MAJOR_VERSION > 5)
  607. /* Safe mode has been removed in PHP 5.4 */
  608. if (php_check_open_basedir(filename TSRMLS_CC)) {
  609. #else
  610. if ((PG(safe_mode) && (!php_checkuid(filename, NULL, CHECKUID_CHECK_FILE_AND_DIR))) || php_check_open_basedir(filename TSRMLS_CC)) {
  611. #endif
  612. return 1;
  613. }
  614. if ((fp = VCWD_OPEN(filename, O_RDONLY|O_BINARY)) == -1) {
  615. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Can't open file %s", filename);
  616. return 1;
  617. }
  618. while ((loblen = read(fp, &buf, sizeof(buf))) > 0) {
  619. PHP_OCI_CALL_RETURN(errstatus,
  620. OCILobWrite,
  621. (
  622. connection->svc,
  623. connection->err,
  624. lob,
  625. &loblen,
  626. offset,
  627. (dvoid *) &buf,
  628. loblen,
  629. OCI_ONE_PIECE,
  630. (dvoid *)0,
  631. (OCICallbackLobWrite) 0,
  632. (ub2) descriptor->charset_id,
  633. (ub1) descriptor->charset_form
  634. )
  635. );
  636. if (errstatus != OCI_SUCCESS) {
  637. connection->errcode = php_oci_error(connection->err, errstatus TSRMLS_CC);
  638. PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
  639. close(fp);
  640. return 1;
  641. } else {
  642. connection->errcode = 0; /* retain backwards compat with OCI8 1.4 */
  643. }
  644. offset += loblen;
  645. }
  646. close(fp);
  647. return 0;
  648. }
  649. /* }}} */
  650. /* {{{ php_oci_lob_append()
  651. Append data to the end of the LOB */
  652. int php_oci_lob_append (php_oci_descriptor *descriptor_dest, php_oci_descriptor *descriptor_from TSRMLS_DC)
  653. {
  654. php_oci_connection *connection = descriptor_dest->connection;
  655. OCILobLocator *lob_dest = descriptor_dest->descriptor;
  656. OCILobLocator *lob_from = descriptor_from->descriptor;
  657. ub4 dest_len, from_len;
  658. sword errstatus;
  659. if (php_oci_lob_get_length(descriptor_dest, &dest_len TSRMLS_CC)) {
  660. return 1;
  661. }
  662. if (php_oci_lob_get_length(descriptor_from, &from_len TSRMLS_CC)) {
  663. return 1;
  664. }
  665. if (from_len <= 0) {
  666. return 0;
  667. }
  668. PHP_OCI_CALL_RETURN(errstatus, OCILobAppend, (connection->svc, connection->err, lob_dest, lob_from));
  669. if (errstatus != OCI_SUCCESS) {
  670. connection->errcode = php_oci_error(connection->err, errstatus TSRMLS_CC);
  671. PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
  672. return 1;
  673. }
  674. connection->errcode = 0; /* retain backwards compat with OCI8 1.4 */
  675. return 0;
  676. }
  677. /* }}} */
  678. /* {{{ php_oci_lob_truncate()
  679. Truncate LOB to the given length */
  680. int php_oci_lob_truncate (php_oci_descriptor *descriptor, long new_lob_length TSRMLS_DC)
  681. {
  682. php_oci_connection *connection = descriptor->connection;
  683. OCILobLocator *lob = descriptor->descriptor;
  684. ub4 lob_length;
  685. sword errstatus;
  686. if (php_oci_lob_get_length(descriptor, &lob_length TSRMLS_CC)) {
  687. return 1;
  688. }
  689. if (lob_length <= 0) {
  690. return 0;
  691. }
  692. if (new_lob_length < 0) {
  693. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Size must be greater than or equal to 0");
  694. return 1;
  695. }
  696. if (new_lob_length > lob_length) {
  697. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Size must be less than or equal to the current LOB size");
  698. return 1;
  699. }
  700. PHP_OCI_CALL_RETURN(errstatus, OCILobTrim, (connection->svc, connection->err, lob, new_lob_length));
  701. if (errstatus != OCI_SUCCESS) {
  702. connection->errcode = php_oci_error(connection->err, errstatus TSRMLS_CC);
  703. PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
  704. return 1;
  705. }
  706. descriptor->lob_size = new_lob_length;
  707. connection->errcode = 0; /* retain backwards compat with OCI8 1.4 */
  708. return 0;
  709. }
  710. /* }}} */
  711. /* {{{ php_oci_lob_erase()
  712. Erase (or fill with whitespaces, depending on LOB type) the LOB (or its part) */
  713. int php_oci_lob_erase (php_oci_descriptor *descriptor, long offset, ub4 length, ub4 *bytes_erased TSRMLS_DC)
  714. {
  715. php_oci_connection *connection = descriptor->connection;
  716. OCILobLocator *lob = descriptor->descriptor;
  717. ub4 lob_length;
  718. sword errstatus;
  719. *bytes_erased = 0;
  720. if (php_oci_lob_get_length(descriptor, &lob_length TSRMLS_CC)) {
  721. return 1;
  722. }
  723. if (offset == -1) {
  724. offset = descriptor->lob_current_position;
  725. }
  726. if (length == -1) {
  727. length = lob_length;
  728. }
  729. PHP_OCI_CALL_RETURN(errstatus, OCILobErase, (connection->svc, connection->err, lob, (ub4 *)&length, offset+1));
  730. if (errstatus != OCI_SUCCESS) {
  731. connection->errcode = php_oci_error(connection->err, errstatus TSRMLS_CC);
  732. PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
  733. return 1;
  734. }
  735. *bytes_erased = length;
  736. connection->errcode = 0; /* retain backwards compat with OCI8 1.4 */
  737. return 0;
  738. }
  739. /* }}} */
  740. /* {{{ php_oci_lob_is_equal()
  741. Compare two LOB descriptors and figure out if they are pointing to the same LOB */
  742. int php_oci_lob_is_equal (php_oci_descriptor *descriptor_first, php_oci_descriptor *descriptor_second, boolean *result TSRMLS_DC)
  743. {
  744. php_oci_connection *connection = descriptor_first->connection;
  745. OCILobLocator *first_lob = descriptor_first->descriptor;
  746. OCILobLocator *second_lob = descriptor_second->descriptor;
  747. sword errstatus;
  748. PHP_OCI_CALL_RETURN(errstatus, OCILobIsEqual, (connection->env, first_lob, second_lob, result));
  749. if (errstatus) {
  750. connection->errcode = php_oci_error(connection->err, errstatus TSRMLS_CC);
  751. PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
  752. return 1;
  753. }
  754. connection->errcode = 0; /* retain backwards compat with OCI8 1.4 */
  755. return 0;
  756. }
  757. /* }}} */
  758. /* {{{ php_oci_lob_write_tmp()
  759. Create temporary LOB and write data to it */
  760. int php_oci_lob_write_tmp (php_oci_descriptor *descriptor, long type, char *data, int data_len TSRMLS_DC)
  761. {
  762. php_oci_connection *connection = descriptor->connection;
  763. OCILobLocator *lob = descriptor->descriptor;
  764. ub4 bytes_written = 0;
  765. sword errstatus;
  766. switch (type) {
  767. case OCI_TEMP_BLOB:
  768. case OCI_TEMP_CLOB:
  769. /* only these two are allowed */
  770. break;
  771. default:
  772. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid temporary lob type: %ld", type);
  773. return 1;
  774. break;
  775. }
  776. if (data_len < 0) {
  777. return 1;
  778. }
  779. PHP_OCI_CALL_RETURN(errstatus, OCILobCreateTemporary,
  780. (
  781. connection->svc,
  782. connection->err,
  783. lob,
  784. OCI_DEFAULT,
  785. OCI_DEFAULT,
  786. (ub1)type,
  787. OCI_ATTR_NOCACHE,
  788. OCI_DURATION_SESSION
  789. )
  790. );
  791. if (errstatus) {
  792. connection->errcode = php_oci_error(connection->err, errstatus TSRMLS_CC);
  793. PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
  794. return 1;
  795. }
  796. PHP_OCI_CALL_RETURN(errstatus, OCILobOpen, (connection->svc, connection->err, lob, OCI_LOB_READWRITE));
  797. if (errstatus) {
  798. connection->errcode = php_oci_error(connection->err, errstatus TSRMLS_CC);
  799. PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
  800. return 1;
  801. }
  802. descriptor->is_open = 1;
  803. connection->errcode = 0; /* retain backwards compat with OCI8 1.4 */
  804. return php_oci_lob_write(descriptor, 0, data, data_len, &bytes_written TSRMLS_CC);
  805. }
  806. /* }}} */
  807. #endif /* HAVE_OCI8 */
  808. /*
  809. * Local variables:
  810. * tab-width: 4
  811. * c-basic-offset: 4
  812. * End:
  813. * vim600: noet sw=4 ts=4 fdm=marker
  814. * vim<600: noet sw=4 ts=4
  815. */