Imported Upstream version 1.0.26
[platform/upstream/libsndfile.git] / src / strings.c
1 /*
2 ** Copyright (C) 2001-2012 Erik de Castro Lopo <erikd@mega-nerd.com>
3 **
4 ** This program is free software; you can redistribute it and/or modify
5 ** it under the terms of the GNU Lesser General Public License as published by
6 ** the Free Software Foundation; either version 2.1 of the License, or
7 ** (at your option) any later version.
8 **
9 ** This program is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 ** GNU Lesser General Public License for more details.
13 **
14 ** You should have received a copy of the GNU Lesser General Public License
15 ** along with this program; if not, write to the Free Software
16 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 #include        "sfconfig.h"
20
21 #include        <stdio.h>
22 #include        <string.h>
23 #include        <math.h>
24
25 #include        "sndfile.h"
26 #include        "common.h"
27
28 #define STRINGS_DEBUG 0
29 #if STRINGS_DEBUG
30 static void hexdump (void *data, int len) ;
31 #endif
32
33 int
34 psf_store_string (SF_PRIVATE *psf, int str_type, const char *str)
35 {       char    new_str [128] ;
36         size_t  str_len ;
37         int             k, str_flags ;
38
39         if (str == NULL)
40                 return SFE_STR_BAD_STRING ;
41
42         str_len = strlen (str) ;
43
44         /* A few extra checks for write mode. */
45         if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR)
46         {       if ((psf->strings.flags & SF_STR_ALLOW_START) == 0)
47                         return SFE_STR_NO_SUPPORT ;
48                 if (psf->have_written && (psf->strings.flags & SF_STR_ALLOW_END) == 0)
49                         return SFE_STR_NO_SUPPORT ;
50                 /* Only allow zero length strings for software. */
51                 if (str_type != SF_STR_SOFTWARE && str_len == 0)
52                         return SFE_STR_BAD_STRING ;
53                 } ;
54
55         /* Find the next free slot in table. */
56         for (k = 0 ; k < SF_MAX_STRINGS ; k++)
57         {       /* If we find a matching entry clear it. */
58                 if (psf->strings.data [k].type == str_type)
59                         psf->strings.data [k].type = -1 ;
60
61                 if (psf->strings.data [k].type == 0)
62                         break ;
63                 } ;
64
65         /* Determine flags */
66         str_flags = SF_STR_LOCATE_START ;
67         if (psf->file.mode == SFM_RDWR || psf->have_written)
68         {       if ((psf->strings.flags & SF_STR_ALLOW_END) == 0)
69                         return SFE_STR_NO_ADD_END ;
70                 str_flags = SF_STR_LOCATE_END ;
71                 } ;
72
73         /* More sanity checking. */
74         if (k >= SF_MAX_STRINGS)
75                 return SFE_STR_MAX_COUNT ;
76
77         if (k == 0 && psf->strings.storage_used != 0)
78         {       psf_log_printf (psf, "SFE_STR_WEIRD : k == 0 && psf->strings.storage_used != 0\n") ;
79                 return SFE_STR_WEIRD ;
80                 } ;
81
82         if (k != 0 && psf->strings.storage_used == 0)
83         {       psf_log_printf (psf, "SFE_STR_WEIRD : k != 0 && psf->strings.storage_used == 0\n") ;
84                 return SFE_STR_WEIRD ;
85                 } ;
86
87         /* Special case for the first string. */
88         if (k == 0)
89                 psf->strings.storage_used = 0 ;
90
91         switch (str_type)
92         {       case SF_STR_SOFTWARE :
93                                 /* In write mode, want to append libsndfile-version to string. */
94                                 if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR)
95                                 {       if (strstr (str, PACKAGE) == NULL)
96                                         {       /*
97                                                 ** If the supplied string does not already contain a
98                                                 ** libsndfile-X.Y.Z component, then add it.
99                                                 */
100                                                 if (strlen (str) == 0)
101                                                         snprintf (new_str, sizeof (new_str), "%s-%s", PACKAGE, VERSION) ;
102                                                 else
103                                                         snprintf (new_str, sizeof (new_str), "%s (%s-%s)", str, PACKAGE, VERSION) ;
104                                                 }
105                                         else
106                                                 snprintf (new_str, sizeof (new_str), "%s", str) ;
107
108                                         str = new_str ;
109                                         } ;
110                                 break ;
111
112                 case SF_STR_TITLE :
113                 case SF_STR_COPYRIGHT :
114                 case SF_STR_ARTIST :
115                 case SF_STR_COMMENT :
116                 case SF_STR_DATE :
117                 case SF_STR_ALBUM :
118                 case SF_STR_LICENSE :
119                 case SF_STR_TRACKNUMBER :
120                 case SF_STR_GENRE :
121                                 break ;
122
123                 default :
124                         psf_log_printf (psf, "%s : SFE_STR_BAD_TYPE\n", __func__) ;
125                         return SFE_STR_BAD_TYPE ;
126                 } ;
127
128         /* Plus one to catch string terminator. */
129         str_len = strlen (str) + 1 ;
130
131         if (psf->strings.storage_used + str_len + 1 > psf->strings.storage_len)
132         {       char * temp = psf->strings.storage ;
133                 size_t newlen = 2 * psf->strings.storage_len + str_len + 1 ;
134
135                 newlen = newlen < 256 ? 256 : newlen ;
136
137                 if ((psf->strings.storage = realloc (temp, newlen)) == NULL)
138                 {       psf->strings.storage = temp ;
139                         return SFE_MALLOC_FAILED ;
140                         } ;
141
142                 psf->strings.storage_len = newlen ;
143                 } ;
144
145         psf->strings.data [k].type = str_type ;
146         psf->strings.data [k].offset = psf->strings.storage_used ;
147         psf->strings.data [k].flags = str_flags ;
148
149         memcpy (psf->strings.storage + psf->strings.storage_used, str, str_len) ;
150         psf->strings.storage_used += str_len ;
151
152         psf->strings.flags |= str_flags ;
153
154 #if STRINGS_DEBUG
155         psf_log_printf (psf, "str_storage          : %p\n", psf->strings.storage) ;
156         psf_log_printf (psf, "storage_used             : %u\n", psf->strings.storage_used) ;
157         psf_log_printf (psf, "used                 : %d\n", psf->strings.storage_used) ;
158         psf_log_printf (psf, "remaining            : %d\n", psf->strings.storage_len - psf->strings.storage_used ;
159
160         hexdump (psf->strings.storage, 300) ;
161 #endif
162
163         return 0 ;
164 } /* psf_store_string */
165
166 int
167 psf_set_string (SF_PRIVATE *psf, int str_type, const char *str)
168 {       if (psf->file.mode == SFM_READ)
169                 return SFE_STR_NOT_WRITE ;
170
171         return psf_store_string (psf, str_type, str) ;
172 } /* psf_set_string */
173
174 const char*
175 psf_get_string (SF_PRIVATE *psf, int str_type)
176 {       int k ;
177
178         for (k = 0 ; k < SF_MAX_STRINGS ; k++)
179                 if (str_type == psf->strings.data [k].type)
180                         return psf->strings.storage + psf->strings.data [k].offset ;
181
182         return NULL ;
183 } /* psf_get_string */
184
185 int
186 psf_location_string_count (const SF_PRIVATE * psf, int location)
187 {       int k, count = 0 ;
188
189         for (k = 0 ; k < SF_MAX_STRINGS ; k++)
190                 if (psf->strings.data [k].type > 0 && psf->strings.data [k].flags & location)
191                         count ++ ;
192
193         return count ;
194 } /* psf_location_string_count */
195
196 /*==============================================================================
197 */
198
199 #if STRINGS_DEBUG
200
201 #include <ctype.h>
202 static void
203 hexdump (void *data, int len)
204 {       unsigned char *ptr ;
205         int k ;
206
207         ptr = data ;
208
209         puts ("---------------------------------------------------------") ;
210         while (len >= 16)
211         {       for (k = 0 ; k < 16 ; k++)
212                         printf ("%02X ", ptr [k] & 0xFF) ;
213                 printf ("   ") ;
214                 for (k = 0 ; k < 16 ; k++)
215                         printf ("%c", psf_isprint (ptr [k]) ? ptr [k] : '.') ;
216                 puts ("") ;
217                 ptr += 16 ;
218                 len -= 16 ;
219                 } ;
220 } /* hexdump */
221
222 #endif