2 ** Copyright (C) 2001-2012 Erik de Castro Lopo <erikd@mega-nerd.com>
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.
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.
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.
28 #define STRINGS_DEBUG 0
30 static void hexdump (void *data, int len) ;
34 psf_store_string (SF_PRIVATE *psf, int str_type, const char *str)
35 { char new_str [128] ;
40 return SFE_STR_BAD_STRING ;
42 str_len = strlen (str) ;
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 ;
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 ;
61 if (psf->strings.data [k].type == 0)
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 ;
73 /* More sanity checking. */
74 if (k >= SF_MAX_STRINGS)
75 return SFE_STR_MAX_COUNT ;
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 ;
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 ;
87 /* Special case for the first string. */
89 psf->strings.storage_used = 0 ;
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)
97 ** If the supplied string does not already contain a
98 ** libsndfile-X.Y.Z component, then add it.
100 if (strlen (str) == 0)
101 snprintf (new_str, sizeof (new_str), "%s-%s", PACKAGE, VERSION) ;
103 snprintf (new_str, sizeof (new_str), "%s (%s-%s)", str, PACKAGE, VERSION) ;
106 snprintf (new_str, sizeof (new_str), "%s", str) ;
113 case SF_STR_COPYRIGHT :
115 case SF_STR_COMMENT :
118 case SF_STR_LICENSE :
119 case SF_STR_TRACKNUMBER :
124 psf_log_printf (psf, "%s : SFE_STR_BAD_TYPE\n", __func__) ;
125 return SFE_STR_BAD_TYPE ;
128 /* Plus one to catch string terminator. */
129 str_len = strlen (str) + 1 ;
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 ;
135 newlen = newlen < 256 ? 256 : newlen ;
137 if ((psf->strings.storage = realloc (temp, newlen)) == NULL)
138 { psf->strings.storage = temp ;
139 return SFE_MALLOC_FAILED ;
142 psf->strings.storage_len = newlen ;
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 ;
149 memcpy (psf->strings.storage + psf->strings.storage_used, str, str_len) ;
150 psf->strings.storage_used += str_len ;
152 psf->strings.flags |= str_flags ;
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 ;
160 hexdump (psf->strings.storage, 300) ;
164 } /* psf_store_string */
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 ;
171 return psf_store_string (psf, str_type, str) ;
172 } /* psf_set_string */
175 psf_get_string (SF_PRIVATE *psf, int str_type)
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 ;
183 } /* psf_get_string */
186 psf_location_string_count (const SF_PRIVATE * psf, int location)
189 for (k = 0 ; k < SF_MAX_STRINGS ; k++)
190 if (psf->strings.data [k].type > 0 && psf->strings.data [k].flags & location)
194 } /* psf_location_string_count */
196 /*==============================================================================
203 hexdump (void *data, int len)
204 { unsigned char *ptr ;
209 puts ("---------------------------------------------------------") ;
211 { for (k = 0 ; k < 16 ; k++)
212 printf ("%02X ", ptr [k] & 0xFF) ;
214 for (k = 0 ; k < 16 ; k++)
215 printf ("%c", psf_isprint (ptr [k]) ? ptr [k] : '.') ;