Coverage Report

Created: 2026-02-26 07:15

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gpac/src/utils/token.c
Line
Count
Source
1
/*
2
 *      GPAC - Multimedia Framework C SDK
3
 *
4
 *      Authors: Jean Le Feuvre
5
 *      Copyright (c) Telecom ParisTech 2000-2022
6
 *          All rights reserved
7
 *
8
 *  This file is part of GPAC / common tools sub-project
9
 *
10
 *  GPAC is free software; you can redistribute it and/or modify
11
 *  it under the terms of the GNU Lesser General Public License as published by
12
 *  the Free Software Foundation; either version 2, or (at your option)
13
 *  any later version.
14
 *
15
 *  GPAC is distributed in the hope that it will be useful,
16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 *  GNU Lesser General Public License for more details.
19
 *
20
 *  You should have received a copy of the GNU Lesser General Public
21
 *  License along with this library; see the file COPYING.  If not, write to
22
 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
 *
24
 */
25
26
#include <gpac/token.h>
27
28
static GFINLINE s32 gf_tok_is_char_in_set(const char TestChar, const char *TestSet)
29
0
{
30
0
  u32 i, Len;
31
0
  Len = (u32) strlen(TestSet);
32
0
  for (i=0; i<Len; i++) {
33
0
    if (TestChar == TestSet[i]) return 1;
34
0
  }
35
0
  return 0;
36
0
}
37
38
GF_EXPORT
39
s32 gf_token_get(const char *Buffer, s32 Start,  const char *Separator,  char *Container, s32 ContainerSize)
40
0
{
41
0
  s32 i, start, end, Len;
42
0
  Container[0]=0;
43
0
  if (Start<0) return -1;
44
45
0
  Len = (s32) strlen( Buffer );
46
0
  for (i=Start; i<Len; i++ ) {
47
0
    if (!gf_tok_is_char_in_set(Buffer[i], Separator)) break;
48
0
  }
49
0
  start = i;
50
0
  if (i == Len) return( -1 );
51
52
0
  for (i=start; i<Len; i++) {
53
0
    if (gf_tok_is_char_in_set(Buffer[i], Separator)) break;
54
0
  }
55
0
  end = i-1;
56
57
0
  for (i=start; ((i<=end) && (i< start+(ContainerSize-1))); i++) {
58
0
    Container[i-start] = Buffer[i];
59
0
  }
60
0
  Container[i-start] = 0;
61
62
0
  return (end+1);
63
0
}
64
65
GF_EXPORT
66
s32 gf_token_get_strip(const char *Buffer, s32 Start, const char *Separator, const char *strip_set, char *Container, s32 ContainerSize)
67
0
{
68
0
  u32 i, k, len;
69
0
  s32 res = gf_token_get(Buffer, Start, Separator, Container, ContainerSize);
70
0
  if (!strip_set || (res<0)) return res;
71
0
  i=k=0;
72
0
  len = (u32) strlen(Container);
73
0
  while (strchr(strip_set, Container[i]) ) i++;
74
0
  while (len && strchr(strip_set, Container[len]) ) {
75
0
    Container[len]=0;
76
0
    len--;
77
0
  }
78
0
  while (k+i<=len) {
79
0
    Container[k] = Container[k+i];
80
0
    k++;
81
0
  }
82
0
  Container[k] = 0;
83
0
  return res;
84
0
}
85
86
87
GF_EXPORT
88
s32 gf_token_get_line(const char *Buffer, u32 Start, u32 Size, char *LineBuffer, u32 LineBufferSize)
89
0
{
90
0
  u32 offset;
91
0
  s32 End, Total;
92
0
  LineBuffer[0] = 0;
93
0
  if (Start >= Size) return -1;
94
95
0
  offset = 2;
96
0
  End = gf_token_find(Buffer, Start, Size, "\r\n");
97
0
  if (End<0) {
98
0
    End = gf_token_find(Buffer, Start, Size, "\r");
99
0
    if (End<0) End = gf_token_find(Buffer, Start, Size, "\n");
100
0
    if (End < 0) return -1;
101
0
    offset = 1;
102
0
  }
103
104
0
  Total = End - Start + offset;
105
0
  if ((u32) Total >= LineBufferSize) Total = LineBufferSize-1;
106
0
  memcpy(LineBuffer, Buffer + Start, Total);
107
0
  LineBuffer[Total] = 0;
108
0
  return (End + offset);
109
0
}
110
111
GF_EXPORT
112
s32 gf_token_find(const char *Buffer, u32 Start, u32 Size, const char *Pattern)
113
0
{
114
0
  u32 i, j, flag;
115
0
  s32 Len;
116
117
0
  if (Start >= Size) return -1;
118
119
0
  Len = (u32) strlen(Pattern);
120
0
  if ( Len <= 0 ) return -1;
121
0
  if (Size - Start < (u32) Len) return -1;
122
123
0
  for (i=Start; i<= Size-Len; i++) {
124
0
    flag = 0;
125
0
    for (j=0; j< (u32) Len; j++) {
126
0
      if (Buffer[i+j] != Pattern[j]) {
127
0
        flag = 1;
128
0
        break;
129
0
      }
130
0
    }
131
    //found
132
0
    if (!flag) return i;
133
0
  }
134
0
  return -1;
135
0
}
136
137
GF_EXPORT
138
const char *gf_token_find_word(const char *in_str, const char *word, char *charsep)
139
0
{
140
0
  u32 len;
141
0
  if (!in_str || !word) return NULL;
142
0
  len = (u32) strlen(word);
143
0
  while (in_str) {
144
0
    char *sep = strstr(in_str, word);
145
0
    if (!sep) return NULL;
146
0
    if (!charsep) return sep;
147
148
0
    if ((sep>in_str) && strchr(charsep, sep[-1]))
149
0
      return sep;
150
151
0
    if (strchr(charsep, sep[len]))
152
0
      return sep;
153
0
    in_str = sep+len;
154
0
  }
155
0
  return NULL;
156
0
}