1
0

xmlrpc_array.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /* Copyright information is at the end of the file */
  2. /*=========================================================================
  3. ** XML-RPC Array Functions
  4. **=========================================================================
  5. */
  6. #include "xmlrpc_config.h"
  7. #include <stddef.h>
  8. #include <stdlib.h>
  9. #include "xmlrpc.h"
  10. #include "xmlrpc_int.h"
  11. void
  12. xmlrpc_abort_if_array_bad(xmlrpc_value * const arrayP) {
  13. if (arrayP == NULL)
  14. abort();
  15. else if (arrayP->_type != XMLRPC_TYPE_ARRAY)
  16. abort();
  17. else {
  18. unsigned int const arraySize =
  19. XMLRPC_MEMBLOCK_SIZE(xmlrpc_value*, &arrayP->_block);
  20. xmlrpc_value ** const contents =
  21. XMLRPC_MEMBLOCK_CONTENTS(xmlrpc_value*, &arrayP->_block);
  22. if (contents == NULL)
  23. abort();
  24. else {
  25. unsigned int xmIndex;
  26. for (xmIndex = 0; xmIndex < arraySize; ++xmIndex) {
  27. xmlrpc_value * const itemP = contents[xmIndex];
  28. if (itemP == NULL)
  29. abort();
  30. else if (itemP->_refcount < 1)
  31. abort();
  32. }
  33. }
  34. }
  35. }
  36. void
  37. xmlrpc_destroyArrayContents(xmlrpc_value * const arrayP) {
  38. /*----------------------------------------------------------------------------
  39. Dispose of the contents of an array (but not the array value itself).
  40. The value is not valid after this.
  41. -----------------------------------------------------------------------------*/
  42. unsigned int const arraySize =
  43. XMLRPC_MEMBLOCK_SIZE(xmlrpc_value*, &arrayP->_block);
  44. xmlrpc_value ** const contents =
  45. XMLRPC_MEMBLOCK_CONTENTS(xmlrpc_value*, &arrayP->_block);
  46. unsigned int xmIndex;
  47. XMLRPC_ASSERT_ARRAY_OK(arrayP);
  48. /* Release our reference to each item in the array */
  49. for (xmIndex = 0; xmIndex < arraySize; ++xmIndex) {
  50. xmlrpc_value * const itemP = contents[xmIndex];
  51. xmlrpc_DECREF(itemP);
  52. }
  53. XMLRPC_MEMBLOCK_CLEAN(xmlrpc_value *, &arrayP->_block);
  54. }
  55. int
  56. xmlrpc_array_size(xmlrpc_env * const env,
  57. const xmlrpc_value * const array) {
  58. int retval;
  59. /* Suppress a compiler warning about uninitialized variables. */
  60. retval = 0;
  61. XMLRPC_ASSERT_ENV_OK(env);
  62. XMLRPC_ASSERT_VALUE_OK(array);
  63. XMLRPC_TYPE_CHECK(env, array, XMLRPC_TYPE_ARRAY);
  64. retval = XMLRPC_TYPED_MEM_BLOCK_SIZE(xmlrpc_value*, &array->_block);
  65. cleanup:
  66. if (env->fault_occurred)
  67. return -1;
  68. else
  69. return retval;
  70. }
  71. void
  72. xmlrpc_array_append_item(xmlrpc_env * const envP,
  73. xmlrpc_value * const arrayP,
  74. xmlrpc_value * const valueP) {
  75. XMLRPC_ASSERT_ENV_OK(envP);
  76. XMLRPC_ASSERT_VALUE_OK(arrayP);
  77. if (xmlrpc_value_type(arrayP) != XMLRPC_TYPE_ARRAY)
  78. xmlrpc_env_set_fault_formatted(
  79. envP, XMLRPC_TYPE_ERROR, "Value is not an array");
  80. else {
  81. size_t const size =
  82. XMLRPC_MEMBLOCK_SIZE(xmlrpc_value *, &arrayP->_block);
  83. XMLRPC_MEMBLOCK_RESIZE(xmlrpc_value *, envP, &arrayP->_block, size+1);
  84. if (!envP->fault_occurred) {
  85. xmlrpc_value ** const contents =
  86. XMLRPC_MEMBLOCK_CONTENTS(xmlrpc_value*, &arrayP->_block);
  87. xmlrpc_INCREF(valueP);
  88. contents[size] = valueP;
  89. }
  90. }
  91. }
  92. void
  93. xmlrpc_array_read_item(xmlrpc_env * const envP,
  94. const xmlrpc_value * const arrayP,
  95. unsigned int const xmIndex,
  96. xmlrpc_value ** const valuePP) {
  97. XMLRPC_ASSERT_ENV_OK(envP);
  98. XMLRPC_ASSERT_VALUE_OK(arrayP);
  99. XMLRPC_ASSERT_PTR_OK(valuePP);
  100. if (arrayP->_type != XMLRPC_TYPE_ARRAY)
  101. xmlrpc_env_set_fault_formatted(
  102. envP, XMLRPC_TYPE_ERROR, "Attempt to read array item from "
  103. "a value that is not an array");
  104. else {
  105. xmlrpc_value ** const contents =
  106. XMLRPC_MEMBLOCK_CONTENTS(xmlrpc_value *, &arrayP->_block);
  107. size_t const size =
  108. XMLRPC_MEMBLOCK_SIZE(xmlrpc_value *, &arrayP->_block);
  109. if (xmIndex >= size)
  110. xmlrpc_env_set_fault_formatted(
  111. envP, XMLRPC_INDEX_ERROR, "Array index %u is beyond end "
  112. "of %u-item array", xmIndex, (unsigned int)size);
  113. else {
  114. *valuePP = contents[xmIndex];
  115. xmlrpc_INCREF(*valuePP);
  116. }
  117. }
  118. }
  119. xmlrpc_value *
  120. xmlrpc_array_get_item(xmlrpc_env * const envP,
  121. const xmlrpc_value * const arrayP,
  122. int const xmIndex) {
  123. xmlrpc_value * valueP;
  124. if (xmIndex < 0)
  125. xmlrpc_env_set_fault_formatted(
  126. envP, XMLRPC_INDEX_ERROR, "Index %d is negative.");
  127. else {
  128. xmlrpc_array_read_item(envP, arrayP, xmIndex, &valueP);
  129. if (!envP->fault_occurred)
  130. xmlrpc_DECREF(valueP);
  131. }
  132. if (envP->fault_occurred)
  133. valueP = NULL;
  134. return valueP;
  135. }
  136. /* Copyright (C) 2001 by First Peer, Inc. All rights reserved.
  137. ** Copyright (C) 2001 by Eric Kidd. All rights reserved.
  138. **
  139. ** Redistribution and use in source and binary forms, with or without
  140. ** modification, are permitted provided that the following conditions
  141. ** are met:
  142. ** 1. Redistributions of source code must retain the above copyright
  143. ** notice, this list of conditions and the following disclaimer.
  144. ** 2. Redistributions in binary form must reproduce the above copyright
  145. ** notice, this list of conditions and the following disclaimer in the
  146. ** documentation and/or other materials provided with the distribution.
  147. ** 3. The name of the author may not be used to endorse or promote products
  148. ** derived from this software without specific prior written permission.
  149. **
  150. ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  151. ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  152. ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  153. ** ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  154. ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  155. ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  156. ** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  157. ** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  158. ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  159. ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  160. ** SUCH DAMAGE. */