secitem.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. /*
  5. * Support routines for SECItem data structure.
  6. */
  7. #include "seccomon.h"
  8. #include "secitem.h"
  9. #include "secerr.h"
  10. #include "secport.h"
  11. SECItem *
  12. SECITEM_AllocItem(PLArenaPool *arena, SECItem *item, unsigned int len)
  13. {
  14. SECItem *result = NULL;
  15. void *mark = NULL;
  16. if (arena != NULL) {
  17. mark = PORT_ArenaMark(arena);
  18. }
  19. if (item == NULL) {
  20. if (arena != NULL) {
  21. result = PORT_ArenaZAlloc(arena, sizeof(SECItem));
  22. } else {
  23. result = PORT_ZAlloc(sizeof(SECItem));
  24. }
  25. if (result == NULL) {
  26. goto loser;
  27. }
  28. } else {
  29. PORT_Assert(item->data == NULL);
  30. result = item;
  31. }
  32. result->len = len;
  33. if (len) {
  34. if (arena != NULL) {
  35. result->data = PORT_ArenaAlloc(arena, len);
  36. } else {
  37. result->data = PORT_Alloc(len);
  38. }
  39. if (result->data == NULL) {
  40. goto loser;
  41. }
  42. } else {
  43. result->data = NULL;
  44. }
  45. if (mark) {
  46. PORT_ArenaUnmark(arena, mark);
  47. }
  48. return (result);
  49. loser:
  50. if (arena != NULL) {
  51. if (mark) {
  52. PORT_ArenaRelease(arena, mark);
  53. }
  54. if (item != NULL) {
  55. item->data = NULL;
  56. item->len = 0;
  57. }
  58. } else {
  59. if (result != NULL) {
  60. SECITEM_FreeItem(result, (item == NULL) ? PR_TRUE : PR_FALSE);
  61. }
  62. /*
  63. * If item is not NULL, the above has set item->data and
  64. * item->len to 0.
  65. */
  66. }
  67. return (NULL);
  68. }
  69. SECStatus
  70. SECITEM_MakeItem(PLArenaPool *arena, SECItem *dest, const unsigned char *data,
  71. unsigned int len)
  72. {
  73. SECItem it = { siBuffer, (unsigned char *)data, len };
  74. return SECITEM_CopyItem(arena, dest, &it);
  75. }
  76. SECStatus
  77. SECITEM_ReallocItem(PLArenaPool *arena, SECItem *item, unsigned int oldlen,
  78. unsigned int newlen)
  79. {
  80. PORT_Assert(item != NULL);
  81. if (item == NULL) {
  82. /* XXX Set error. But to what? */
  83. return SECFailure;
  84. }
  85. /*
  86. * If no old length, degenerate to just plain alloc.
  87. */
  88. if (oldlen == 0) {
  89. PORT_Assert(item->data == NULL || item->len == 0);
  90. if (newlen == 0) {
  91. /* Nothing to do. Weird, but not a failure. */
  92. return SECSuccess;
  93. }
  94. item->len = newlen;
  95. if (arena != NULL) {
  96. item->data = PORT_ArenaAlloc(arena, newlen);
  97. } else {
  98. item->data = PORT_Alloc(newlen);
  99. }
  100. } else {
  101. if (arena != NULL) {
  102. item->data = PORT_ArenaGrow(arena, item->data, oldlen, newlen);
  103. } else {
  104. item->data = PORT_Realloc(item->data, newlen);
  105. }
  106. }
  107. if (item->data == NULL) {
  108. return SECFailure;
  109. }
  110. return SECSuccess;
  111. }
  112. SECStatus
  113. SECITEM_ReallocItemV2(PLArenaPool *arena, SECItem *item, unsigned int newlen)
  114. {
  115. unsigned char *newdata = NULL;
  116. PORT_Assert(item);
  117. if (!item) {
  118. PORT_SetError(SEC_ERROR_INVALID_ARGS);
  119. return SECFailure;
  120. }
  121. if (item->len == newlen) {
  122. return SECSuccess;
  123. }
  124. if (!newlen) {
  125. if (!arena) {
  126. PORT_Free(item->data);
  127. }
  128. item->data = NULL;
  129. item->len = 0;
  130. return SECSuccess;
  131. }
  132. if (!item->data) {
  133. /* allocate fresh block of memory */
  134. PORT_Assert(!item->len);
  135. if (arena) {
  136. newdata = PORT_ArenaAlloc(arena, newlen);
  137. } else {
  138. newdata = PORT_Alloc(newlen);
  139. }
  140. } else {
  141. /* reallocate or adjust existing block of memory */
  142. if (arena) {
  143. if (item->len > newlen) {
  144. /* There's no need to realloc a shorter block from the arena,
  145. * because it would result in using even more memory!
  146. * Therefore we'll continue to use the old block and
  147. * set the item to the shorter size.
  148. */
  149. item->len = newlen;
  150. return SECSuccess;
  151. }
  152. newdata = PORT_ArenaGrow(arena, item->data, item->len, newlen);
  153. } else {
  154. newdata = PORT_Realloc(item->data, newlen);
  155. }
  156. }
  157. if (!newdata) {
  158. PORT_SetError(SEC_ERROR_NO_MEMORY);
  159. return SECFailure;
  160. }
  161. item->len = newlen;
  162. item->data = newdata;
  163. return SECSuccess;
  164. }
  165. SECComparison
  166. SECITEM_CompareItem(const SECItem *a, const SECItem *b)
  167. {
  168. unsigned m;
  169. int rv;
  170. if (a == b)
  171. return SECEqual;
  172. if (!a || !a->len || !a->data)
  173. return (!b || !b->len || !b->data) ? SECEqual : SECLessThan;
  174. if (!b || !b->len || !b->data)
  175. return SECGreaterThan;
  176. m = ((a->len < b->len) ? a->len : b->len);
  177. rv = PORT_Memcmp(a->data, b->data, m);
  178. if (rv) {
  179. return rv < 0 ? SECLessThan : SECGreaterThan;
  180. }
  181. if (a->len < b->len) {
  182. return SECLessThan;
  183. }
  184. if (a->len == b->len) {
  185. return SECEqual;
  186. }
  187. return SECGreaterThan;
  188. }
  189. PRBool
  190. SECITEM_ItemsAreEqual(const SECItem *a, const SECItem *b)
  191. {
  192. if (a->len != b->len)
  193. return PR_FALSE;
  194. if (!a->len)
  195. return PR_TRUE;
  196. if (!a->data || !b->data) {
  197. /* avoid null pointer crash. */
  198. return (PRBool)(a->data == b->data);
  199. }
  200. return (PRBool)!PORT_Memcmp(a->data, b->data, a->len);
  201. }
  202. SECItem *
  203. SECITEM_DupItem(const SECItem *from)
  204. {
  205. return SECITEM_ArenaDupItem(NULL, from);
  206. }
  207. SECItem *
  208. SECITEM_ArenaDupItem(PLArenaPool *arena, const SECItem *from)
  209. {
  210. SECItem *to;
  211. if (from == NULL) {
  212. return (NULL);
  213. }
  214. if (arena != NULL) {
  215. to = (SECItem *)PORT_ArenaAlloc(arena, sizeof(SECItem));
  216. } else {
  217. to = (SECItem *)PORT_Alloc(sizeof(SECItem));
  218. }
  219. if (to == NULL) {
  220. return (NULL);
  221. }
  222. if (arena != NULL) {
  223. to->data = (unsigned char *)PORT_ArenaAlloc(arena, from->len);
  224. } else {
  225. to->data = (unsigned char *)PORT_Alloc(from->len);
  226. }
  227. if (to->data == NULL) {
  228. PORT_Free(to);
  229. return (NULL);
  230. }
  231. to->len = from->len;
  232. to->type = from->type;
  233. if (to->len) {
  234. PORT_Memcpy(to->data, from->data, to->len);
  235. }
  236. return (to);
  237. }
  238. SECStatus
  239. SECITEM_CopyItem(PLArenaPool *arena, SECItem *to, const SECItem *from)
  240. {
  241. to->type = from->type;
  242. if (from->data && from->len) {
  243. if (arena) {
  244. to->data = (unsigned char *)PORT_ArenaAlloc(arena, from->len);
  245. } else {
  246. to->data = (unsigned char *)PORT_Alloc(from->len);
  247. }
  248. if (!to->data) {
  249. return SECFailure;
  250. }
  251. PORT_Memcpy(to->data, from->data, from->len);
  252. to->len = from->len;
  253. } else {
  254. /*
  255. * If from->data is NULL but from->len is nonzero, this function
  256. * will succeed. Is this right?
  257. */
  258. to->data = 0;
  259. to->len = 0;
  260. }
  261. return SECSuccess;
  262. }
  263. void
  264. SECITEM_FreeItem(SECItem *zap, PRBool freeit)
  265. {
  266. if (zap) {
  267. PORT_Free(zap->data);
  268. zap->data = 0;
  269. zap->len = 0;
  270. if (freeit) {
  271. PORT_Free(zap);
  272. }
  273. }
  274. }
  275. void
  276. SECITEM_ZfreeItem(SECItem *zap, PRBool freeit)
  277. {
  278. if (zap) {
  279. PORT_ZFree(zap->data, zap->len);
  280. zap->data = 0;
  281. zap->len = 0;
  282. if (freeit) {
  283. PORT_ZFree(zap, sizeof(SECItem));
  284. }
  285. }
  286. }
  287. /* these reroutines were taken from pkix oid.c, which is supposed to
  288. * replace this file some day */
  289. /*
  290. * This is the hash function. We simply XOR the encoded form with
  291. * itself in sizeof(PLHashNumber)-byte chunks. Improving this
  292. * routine is left as an excercise for the more mathematically
  293. * inclined student.
  294. */
  295. PLHashNumber PR_CALLBACK
  296. SECITEM_Hash(const void *key)
  297. {
  298. const SECItem *item = (const SECItem *)key;
  299. PLHashNumber rv = 0;
  300. PRUint8 *data = (PRUint8 *)item->data;
  301. PRUint32 i;
  302. PRUint8 *rvc = (PRUint8 *)&rv;
  303. for (i = 0; i < item->len; i++) {
  304. rvc[i % sizeof(rv)] ^= *data;
  305. data++;
  306. }
  307. return rv;
  308. }
  309. /*
  310. * This is the key-compare function. It simply does a lexical
  311. * comparison on the item data. This does not result in
  312. * quite the same ordering as the "sequence of numbers" order,
  313. * but heck it's only used internally by the hash table anyway.
  314. */
  315. PRIntn PR_CALLBACK
  316. SECITEM_HashCompare(const void *k1, const void *k2)
  317. {
  318. const SECItem *i1 = (const SECItem *)k1;
  319. const SECItem *i2 = (const SECItem *)k2;
  320. return SECITEM_ItemsAreEqual(i1, i2);
  321. }
  322. SECItemArray *
  323. SECITEM_AllocArray(PLArenaPool *arena, SECItemArray *array, unsigned int len)
  324. {
  325. SECItemArray *result = NULL;
  326. void *mark = NULL;
  327. if (array != NULL && array->items != NULL) {
  328. PORT_Assert(0);
  329. PORT_SetError(SEC_ERROR_INVALID_ARGS);
  330. return NULL;
  331. }
  332. if (arena != NULL) {
  333. mark = PORT_ArenaMark(arena);
  334. }
  335. if (array == NULL) {
  336. if (arena != NULL) {
  337. result = PORT_ArenaZAlloc(arena, sizeof(SECItemArray));
  338. } else {
  339. result = PORT_ZAlloc(sizeof(SECItemArray));
  340. }
  341. if (result == NULL) {
  342. goto loser;
  343. }
  344. } else {
  345. result = array;
  346. }
  347. result->len = len;
  348. if (len) {
  349. if (arena != NULL) {
  350. result->items = PORT_ArenaZNewArray(arena, SECItem, len);
  351. } else {
  352. result->items = PORT_ZNewArray(SECItem, len);
  353. }
  354. if (result->items == NULL) {
  355. goto loser;
  356. }
  357. } else {
  358. result->items = NULL;
  359. }
  360. if (mark) {
  361. PORT_ArenaUnmark(arena, mark);
  362. }
  363. return result;
  364. loser:
  365. if (arena != NULL) {
  366. if (mark) {
  367. PORT_ArenaRelease(arena, mark);
  368. }
  369. } else {
  370. if (result != NULL && array == NULL) {
  371. PORT_Free(result);
  372. }
  373. }
  374. if (array != NULL) {
  375. array->items = NULL;
  376. array->len = 0;
  377. }
  378. return NULL;
  379. }
  380. static void
  381. secitem_FreeArray(SECItemArray *array, PRBool zero_items, PRBool freeit)
  382. {
  383. unsigned int i;
  384. if (!array || !array->len || !array->items)
  385. return;
  386. for (i = 0; i < array->len; ++i) {
  387. SECItem *item = &array->items[i];
  388. if (item->data) {
  389. if (zero_items) {
  390. SECITEM_ZfreeItem(item, PR_FALSE);
  391. } else {
  392. SECITEM_FreeItem(item, PR_FALSE);
  393. }
  394. }
  395. }
  396. PORT_Free(array->items);
  397. array->items = NULL;
  398. array->len = 0;
  399. if (freeit)
  400. PORT_Free(array);
  401. }
  402. void
  403. SECITEM_FreeArray(SECItemArray *array, PRBool freeit)
  404. {
  405. secitem_FreeArray(array, PR_FALSE, freeit);
  406. }
  407. void
  408. SECITEM_ZfreeArray(SECItemArray *array, PRBool freeit)
  409. {
  410. secitem_FreeArray(array, PR_TRUE, freeit);
  411. }
  412. SECItemArray *
  413. SECITEM_DupArray(PLArenaPool *arena, const SECItemArray *from)
  414. {
  415. SECItemArray *result;
  416. unsigned int i;
  417. /* Require a "from" array.
  418. * Reject an inconsistent "from" array with NULL data and nonzero length.
  419. * However, allow a "from" array of zero length.
  420. */
  421. if (!from || (!from->items && from->len))
  422. return NULL;
  423. result = SECITEM_AllocArray(arena, NULL, from->len);
  424. if (!result)
  425. return NULL;
  426. for (i = 0; i < from->len; ++i) {
  427. SECStatus rv = SECITEM_CopyItem(arena,
  428. &result->items[i], &from->items[i]);
  429. if (rv != SECSuccess) {
  430. SECITEM_ZfreeArray(result, PR_TRUE);
  431. return NULL;
  432. }
  433. }
  434. return result;
  435. }