cdb_make.c 5.4 KB

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