cdb_make.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. | Author: Marcus Boerger <helly@php.net> |
  16. +----------------------------------------------------------------------+
  17. */
  18. /* $Id$ */
  19. /* incorporated from D.J.Bernstein's cdb-0.75 (http://cr.yp.to/cdb.html)*/
  20. #ifdef HAVE_CONFIG_H
  21. #include "config.h"
  22. #endif
  23. #include "php.h"
  24. #include <sys/types.h>
  25. #ifdef HAVE_UNISTD_H
  26. #include <unistd.h>
  27. #endif
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. #include <errno.h>
  31. #include "cdb.h"
  32. #include "cdb_make.h"
  33. #include "uint32.h"
  34. /* {{{ cdb_make_write */
  35. static int cdb_make_write(struct cdb_make *c, char *buf, uint32 sz TSRMLS_DC) {
  36. return php_stream_write(c->fp, buf, sz) == sz ? 0 : -1;
  37. }
  38. /* {{{ cdb_posplus */
  39. static int cdb_posplus(struct cdb_make *c, uint32 len)
  40. {
  41. uint32 newpos = c->pos + len;
  42. if (newpos < len) {
  43. errno = ENOMEM;
  44. return -1;
  45. }
  46. c->pos = newpos;
  47. return 0;
  48. }
  49. /* }}} */
  50. /* {{{ cdb_make_start */
  51. int cdb_make_start(struct cdb_make *c, php_stream * f TSRMLS_DC)
  52. {
  53. c->head = 0;
  54. c->split = 0;
  55. c->hash = 0;
  56. c->numentries = 0;
  57. c->fp = f;
  58. c->pos = sizeof(c->final);
  59. if (php_stream_seek(f, c->pos, SEEK_SET) == -1) {
  60. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Fseek failed");
  61. return -1;
  62. }
  63. return php_stream_tell(c->fp);
  64. }
  65. /* }}} */
  66. /* {{{ cdb_make_addend */
  67. int cdb_make_addend(struct cdb_make *c, unsigned int keylen, unsigned int datalen, uint32 h TSRMLS_DC)
  68. {
  69. struct cdb_hplist *head;
  70. head = c->head;
  71. if (!head || (head->num >= CDB_HPLIST)) {
  72. head = (struct cdb_hplist *) emalloc(sizeof(struct cdb_hplist));
  73. if (!head)
  74. return -1;
  75. head->num = 0;
  76. head->next = c->head;
  77. c->head = head;
  78. }
  79. head->hp[head->num].h = h;
  80. head->hp[head->num].p = c->pos;
  81. ++head->num;
  82. ++c->numentries;
  83. if (cdb_posplus(c,8) == -1)
  84. return -1;
  85. if (cdb_posplus(c, keylen) == -1)
  86. return -1;
  87. if (cdb_posplus(c, datalen) == -1)
  88. return -1;
  89. return 0;
  90. }
  91. /* }}} */
  92. /* {{{ cdb_make_addbegin */
  93. int cdb_make_addbegin(struct cdb_make *c, unsigned int keylen, unsigned int datalen TSRMLS_DC)
  94. {
  95. char buf[8];
  96. if (keylen > 0xffffffff) {
  97. errno = ENOMEM;
  98. return -1;
  99. }
  100. if (datalen > 0xffffffff) {
  101. errno = ENOMEM;
  102. return -1;
  103. }
  104. uint32_pack(buf, keylen);
  105. uint32_pack(buf + 4, datalen);
  106. if (cdb_make_write(c, buf, 8 TSRMLS_CC) != 0)
  107. return -1;
  108. return 0;
  109. }
  110. /* {{{ cdb_make_add */
  111. int cdb_make_add(struct cdb_make *c,char *key,unsigned int keylen,char *data,unsigned int datalen TSRMLS_DC)
  112. {
  113. if (cdb_make_addbegin(c, keylen, datalen TSRMLS_CC) == -1)
  114. return -1;
  115. if (cdb_make_write(c, key, keylen TSRMLS_CC) != 0)
  116. return -1;
  117. if (cdb_make_write(c, data, datalen TSRMLS_CC) != 0)
  118. return -1;
  119. return cdb_make_addend(c, keylen, datalen, cdb_hash(key, keylen) TSRMLS_CC);
  120. }
  121. /* }}} */
  122. /* {{{ cdb_make_finish */
  123. int cdb_make_finish(struct cdb_make *c TSRMLS_DC)
  124. {
  125. char buf[8];
  126. int i;
  127. uint32 len;
  128. uint32 u;
  129. uint32 memsize;
  130. uint32 count;
  131. uint32 where;
  132. struct cdb_hplist *x;
  133. struct cdb_hp *hp;
  134. for (i = 0;i < 256;++i)
  135. c->count[i] = 0;
  136. for (x = c->head; x; x = x->next) {
  137. i = x->num;
  138. while (i--)
  139. ++c->count[255 & x->hp[i].h];
  140. }
  141. memsize = 1;
  142. for (i = 0;i < 256;++i) {
  143. u = c->count[i] * 2;
  144. if (u > memsize)
  145. memsize = u;
  146. }
  147. memsize += c->numentries; /* no overflow possible up to now */
  148. u = (uint32) 0 - (uint32) 1;
  149. u /= sizeof(struct cdb_hp);
  150. if (memsize > u) {
  151. errno = ENOMEM;
  152. return -1;
  153. }
  154. c->split = (struct cdb_hp *) safe_emalloc(memsize, sizeof(struct cdb_hp), 0);
  155. if (!c->split)
  156. return -1;
  157. c->hash = c->split + c->numentries;
  158. u = 0;
  159. for (i = 0;i < 256;++i) {
  160. u += c->count[i]; /* bounded by numentries, so no overflow */
  161. c->start[i] = u;
  162. }
  163. for (x = c->head; x; x = x->next) {
  164. i = x->num;
  165. while (i--)
  166. c->split[--c->start[255 & x->hp[i].h]] = x->hp[i];
  167. }
  168. for (i = 0;i < 256;++i) {
  169. count = c->count[i];
  170. len = count + count; /* no overflow possible */
  171. uint32_pack(c->final + 8 * i,c->pos);
  172. uint32_pack(c->final + 8 * i + 4,len);
  173. for (u = 0;u < len;++u)
  174. c->hash[u].h = c->hash[u].p = 0;
  175. hp = c->split + c->start[i];
  176. for (u = 0;u < count;++u) {
  177. where = (hp->h >> 8) % len;
  178. while (c->hash[where].p)
  179. if (++where == len)
  180. where = 0;
  181. c->hash[where] = *hp++;
  182. }
  183. for (u = 0;u < len;++u) {
  184. uint32_pack(buf, c->hash[u].h);
  185. uint32_pack(buf + 4, c->hash[u].p);
  186. if (cdb_make_write(c, buf, 8 TSRMLS_CC) != 0)
  187. return -1;
  188. if (cdb_posplus(c, 8) == -1)
  189. return -1;
  190. }
  191. }
  192. if (c->split)
  193. efree(c->split);
  194. for (x = c->head; x; c->head = x) {
  195. x = x->next;
  196. efree(c->head);
  197. }
  198. if (php_stream_flush(c->fp) != 0)
  199. return -1;
  200. php_stream_rewind(c->fp);
  201. if (php_stream_tell(c->fp) != 0)
  202. return -1;
  203. if (cdb_make_write(c, c->final, sizeof(c->final) TSRMLS_CC) != 0)
  204. return -1;
  205. return php_stream_flush(c->fp);
  206. }
  207. /* }}} */
  208. /* {{{ cdb_make_version */
  209. char *cdb_make_version()
  210. {
  211. return "0.75, $Id$";
  212. }