subpack.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. JAMLIB - A JAM subroutine library
  3. Copyright (C) 1999 Björn Stenberg
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with this library; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. Changes made by Johan Billing 2000-04-16:
  16. - Fixed broken JAM_GetSubfield()
  17. - #includes stdlib.h instead of malloc.h and memory.h
  18. Changes made by Johan Billing 2000-09-17:
  19. - Added JAM_GetSubfield_R()
  20. Backported changes from JAMLIB 1.4.7 made by Johan Billing 2003-10-22
  21. - JAM_NewSubPacket() and JAM_PutSubField() would give memory leaks under
  22. low memory conditions. Fixed.
  23. Other changes made by Johan Billing 2003-10-22
  24. - Fixed comparison between signed and unsigned variable in
  25. JAM_DelSubPacket() and JAM_GetSubField()
  26. */
  27. /***********************************************************************
  28. **
  29. ** SUBPACKET.C -- Subfield packet handling
  30. **
  31. ** Author: Bj”rn Stenberg ([email protected])
  32. **
  33. ***********************************************************************/
  34. #include <stdio.h>
  35. #include <errno.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include "jam.h"
  39. /***********************************************************************
  40. **
  41. ** JAM_NewSubPacket - Create a new subfield packet
  42. **
  43. ***********************************************************************/
  44. s_JamSubPacket* JAM_NewSubPacket( void )
  45. {
  46. s_JamSubPacket* Sub_PS;
  47. /* allocate packet struct */
  48. Sub_PS = (s_JamSubPacket*) malloc( sizeof( s_JamSubPacket ) );
  49. if ( !Sub_PS )
  50. return NULL;
  51. Sub_PS->NumAlloc = 20;
  52. Sub_PS->NumFields = 0;
  53. /* allocate pointer array */
  54. Sub_PS->Fields = (s_JamSubfield**) calloc( Sub_PS->NumAlloc,
  55. sizeof( s_JamSubfield* ) );
  56. if ( !Sub_PS->Fields ) {
  57. free (Sub_PS);
  58. return NULL;
  59. }
  60. return Sub_PS;
  61. }
  62. /***********************************************************************
  63. **
  64. ** JAM_DelSubPacket - Free the data associated with a subfield packet
  65. **
  66. ***********************************************************************/
  67. int JAM_DelSubPacket( s_JamSubPacket* SubPack_PS )
  68. {
  69. uint32_t i;
  70. if (!SubPack_PS)
  71. return JAM_BAD_PARAM;
  72. for ( i=0; i < SubPack_PS->NumFields; i++ ) {
  73. s_JamSubfield* Field_PS = SubPack_PS->Fields[i];
  74. if ( Field_PS->Buffer )
  75. free( Field_PS->Buffer );
  76. free( Field_PS );
  77. }
  78. free( SubPack_PS->Fields );
  79. free( SubPack_PS );
  80. return 0;
  81. }
  82. /***********************************************************************
  83. **
  84. ** JAM_GetSubfield -- Get first/next subfield from a subfield packet
  85. ** (not reentrant)
  86. **
  87. ***********************************************************************/
  88. s_JamSubfield* JAM_GetSubfield( s_JamSubPacket* SubPack_PS )
  89. {
  90. static s_JamSubPacket* LastPack_PS = NULL;
  91. static uint32_t NextIndex_I = 0;
  92. if ( SubPack_PS ) {
  93. LastPack_PS = SubPack_PS;
  94. NextIndex_I = 0;
  95. }
  96. if ( NextIndex_I < LastPack_PS->NumFields )
  97. return LastPack_PS->Fields[ NextIndex_I++ ];
  98. return NULL;
  99. }
  100. /***********************************************************************
  101. **
  102. ** JAM_GetSubfield_R -- Get first/next subfield from a subfield packet
  103. ** (reentrant)
  104. **
  105. ***********************************************************************/
  106. s_JamSubfield* JAM_GetSubfield_R( s_JamSubPacket* SubPack_PS , uint32_t* Count_PI)
  107. {
  108. if ( *Count_PI < SubPack_PS->NumFields )
  109. return SubPack_PS->Fields[ (*Count_PI)++ ];
  110. return NULL;
  111. }
  112. /***********************************************************************
  113. **
  114. ** JAM_PutSubfield -- Add a subfield to a subfield packet
  115. **
  116. ***********************************************************************/
  117. int JAM_PutSubfield( s_JamSubPacket* SubPack_PS, s_JamSubfield* Field_PS )
  118. {
  119. s_JamSubfield* NewField_PS;
  120. char* NewBuf_PC;
  121. /* do we have to expand the array? */
  122. if ( SubPack_PS->NumFields == SubPack_PS->NumAlloc ) {
  123. s_JamSubfield** Fields_PPS;
  124. SubPack_PS->NumAlloc *= 2;
  125. Fields_PPS = (s_JamSubfield**) realloc( SubPack_PS->Fields,
  126. SubPack_PS->NumAlloc *
  127. sizeof( s_JamSubfield* ) );
  128. if ( !Fields_PPS )
  129. return JAM_NO_MEMORY;
  130. SubPack_PS->Fields=Fields_PPS;
  131. }
  132. /*
  133. ** Copy the passed subfield
  134. */
  135. /* allocate a new subfield */
  136. NewField_PS = (s_JamSubfield*) malloc( sizeof( s_JamSubfield ) );
  137. if ( !NewField_PS )
  138. return JAM_NO_MEMORY;
  139. /* allocate a new buffer */
  140. if ( Field_PS->DatLen ) {
  141. NewBuf_PC = (char*) malloc( Field_PS->DatLen );
  142. if ( !NewBuf_PC ) {
  143. free (NewField_PS);
  144. return JAM_NO_MEMORY;
  145. }
  146. memcpy( NewBuf_PC, Field_PS->Buffer, Field_PS->DatLen );
  147. }
  148. else
  149. NewBuf_PC = NULL;
  150. /* copy field struct */
  151. NewField_PS->LoID = Field_PS->LoID;
  152. NewField_PS->HiID = Field_PS->HiID;
  153. NewField_PS->DatLen = Field_PS->DatLen;
  154. NewField_PS->Buffer = NewBuf_PC;
  155. /*
  156. ** Update subfield packet
  157. */
  158. SubPack_PS->Fields[ SubPack_PS->NumFields ] = NewField_PS;
  159. SubPack_PS->NumFields++;
  160. return 0;
  161. }