oci8_lob.c 27 KB

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