info.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // SPDX-License-Identifier: LGPL-2.1 OR BSD-3-Clause
  2. /********************************************************************
  3. Copyright (C) 2002-2009 Xiph.org Foundation
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions
  6. are met:
  7. - Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. - Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. - Neither the name of the Xiph.org Foundation nor the names of its
  13. contributors may be used to endorse or promote products derived from
  14. this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION
  19. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. ********************************************************************/
  27. #include <stdlib.h>
  28. #include <ctype.h>
  29. #include <string.h>
  30. #include "internal.h"
  31. /*This is more or less the same as strncasecmp, but that doesn't exist
  32. everywhere, and this is a fairly trivial function, so we include it.
  33. Note: We take advantage of the fact that we know _n is less than or equal to
  34. the length of at least one of the strings.*/
  35. static int32_t oc_tagcompare(const char *_s1,const char *_s2,int32_t _n)
  36. {
  37. int32_t c;
  38. for(c=0;c<_n;c++){
  39. if(toupper(_s1[c])!=toupper(_s2[c]))return !0;
  40. }
  41. return _s1[c]!='=';
  42. }
  43. void th_info_init(th_info *_info)
  44. {
  45. memset(_info,0,sizeof(*_info));
  46. _info->version_major=TH_VERSION_MAJOR;
  47. _info->version_minor=TH_VERSION_MINOR;
  48. _info->version_subminor=TH_VERSION_SUB;
  49. _info->keyframe_granule_shift=6;
  50. }
  51. void th_info_clear(th_info *_info){
  52. memset(_info,0,sizeof(*_info));
  53. }
  54. void th_comment_init(th_comment *_tc)
  55. {
  56. memset(_tc,0,sizeof(*_tc));
  57. }
  58. void th_comment_add(th_comment *_tc,char *_comment)
  59. {
  60. char **user_comments;
  61. int32_t *comment_lengths;
  62. int32_t comment_len;
  63. user_comments=realloc(_tc->user_comments,
  64. (_tc->comments+2)*sizeof(*_tc->user_comments));
  65. if(user_comments==NULL)return;
  66. _tc->user_comments=user_comments;
  67. comment_lengths=realloc(_tc->comment_lengths,
  68. (_tc->comments+2)*sizeof(*_tc->comment_lengths));
  69. if(comment_lengths==NULL)return;
  70. _tc->comment_lengths=comment_lengths;
  71. comment_len=strlen(_comment);
  72. comment_lengths[_tc->comments]=comment_len;
  73. user_comments[_tc->comments]=malloc(comment_len+1);
  74. if(user_comments[_tc->comments]==NULL)return;
  75. memcpy(_tc->user_comments[_tc->comments],_comment,comment_len+1);
  76. _tc->comments++;
  77. _tc->user_comments[_tc->comments]=NULL;
  78. }
  79. void th_comment_add_tag(th_comment *_tc,char *_tag,char *_val)
  80. {
  81. char *comment;
  82. int32_t tag_len;
  83. int32_t val_len;
  84. tag_len=strlen(_tag);
  85. val_len=strlen(_val);
  86. /*+2 for '=' and '\0'.*/
  87. comment=malloc(tag_len+val_len+2);
  88. if(comment==NULL)return;
  89. memcpy(comment,_tag,tag_len);
  90. comment[tag_len]='=';
  91. memcpy(comment+tag_len+1,_val,val_len+1);
  92. th_comment_add(_tc,comment);
  93. free(comment);
  94. }
  95. char *th_comment_query(th_comment *_tc,char *_tag,int32_t _count)
  96. {
  97. long i;
  98. int32_t found;
  99. int32_t tag_len;
  100. tag_len=strlen(_tag);
  101. found=0;
  102. for(i=0;i<_tc->comments;i++){
  103. if(!oc_tagcompare(_tc->user_comments[i],_tag,tag_len)){
  104. /*We return a pointer to the data, not a copy.*/
  105. if(_count==found++)return _tc->user_comments[i]+tag_len+1;
  106. }
  107. }
  108. /*Didn't find anything.*/
  109. return NULL;
  110. }
  111. int32_t th_comment_query_count(th_comment *_tc,char *_tag)
  112. {
  113. long i;
  114. int32_t tag_len;
  115. int32_t count;
  116. tag_len=strlen(_tag);
  117. count=0;
  118. for(i=0;i<_tc->comments;i++){
  119. if(!oc_tagcompare(_tc->user_comments[i],_tag,tag_len))count++;
  120. }
  121. return count;
  122. }
  123. void th_comment_clear(th_comment *_tc)
  124. {
  125. if(_tc!=NULL){
  126. long i;
  127. for(i=0;i<_tc->comments;i++) free(_tc->user_comments[i]);
  128. free(_tc->user_comments);
  129. free(_tc->comment_lengths);
  130. free(_tc->vendor);
  131. memset(_tc,0,sizeof(*_tc));
  132. }
  133. }