long fish_sound_comments_encode (FishSound * fsound, unsigned char * buf, long length) { char * c = (char *)buf; const FishSoundComment * comment; int nb_fields = 0, vendor_length = 0; unsigned long actual_length = 0, remaining = length, field_length; /* Vendor string */ if (fsound->vendor) vendor_length = fs_comment_len (fsound->vendor); if (accum_length (&actual_length, 4 + vendor_length) == 0) return 0; /* user comment list length */ if (accum_length (&actual_length, 4) == 0) return 0; for (comment = fish_sound_comment_first (fsound); comment; comment = fish_sound_comment_next (fsound, comment)) { /* [size]"name" */ if (accum_length (&actual_length, 4 + fs_comment_len (comment->name)) == 0) return 0; if (comment->value) { /* "=value" */ if (accum_length (&actual_length, 1 + fs_comment_len (comment->value)) == 0) return 0; } debug_printf (1, "%s = %s", comment->name, comment->value); nb_fields++; } /* framing bit */ if (accum_length (&actual_length, 1) == 0) return 0; /* NB. actual_length is not modified from here onwards */ if (buf == NULL) return actual_length; remaining -= 4; if (remaining <= 0) return actual_length; writeint (c, 0, vendor_length); c += 4; if (fsound->vendor) { field_length = fs_comment_len (fsound->vendor); memcpy (c, fsound->vendor, MIN (field_length, remaining)); c += field_length; remaining -= field_length; if (remaining <= 0) return actual_length; } remaining -= 4; if (remaining <= 0) return actual_length; writeint (c, 0, nb_fields); c += 4; for (comment = fish_sound_comment_first (fsound); comment; comment = fish_sound_comment_next (fsound, comment)) { field_length = fs_comment_len (comment->name); /* [size]"name" */ if (comment->value) field_length += 1 + fs_comment_len (comment->value); /* "=value" */ remaining -= 4; if (remaining <= 0) return actual_length; writeint (c, 0, field_length); c += 4; field_length = fs_comment_len (comment->name); memcpy (c, comment->name, MIN (field_length, remaining)); c += field_length; remaining -= field_length; if (remaining <= 0) return actual_length; if (comment->value) { remaining --; if (remaining <= 0) return actual_length; *c = '='; c++; field_length = fs_comment_len (comment->value); memcpy (c, comment->value, MIN (field_length, remaining)); c += field_length; remaining -= field_length; if (remaining <= 0) return actual_length; } } if (remaining <= 0) return actual_length; *c = 0x01; return actual_length; }
static int decoded (FishSound * fsound, float ** pcm, long frames, void * user_data) { const FishSoundComment * comment; if (fsound == NULL) FAIL ("No Fish Found"); INFO ("+ Retrieving first (expect ARTIST1)"); comment = fish_sound_comment_first (fsound); if (comment == NULL) FAIL ("Recently inserted ARTIST1 not retrieved"); if (strcmp (comment->name, "ARTIST")) FAIL ("Incorrect ARTIST1 name found"); if (strcmp (comment->value, ARTIST1)) FAIL ("Incorrect ARTIST1 value found"); INFO ("+ Retrieving next (expect COPYRIGHT)"); comment = fish_sound_comment_next (fsound, comment); if (comment == NULL) FAIL ("Recently inserted COPYRIGHT not retrieved"); if (strcmp (comment->name, "COPYRIGHT")) FAIL ("Incorrect COPYRIGHT name found"); if (strcmp (comment->value, COPYRIGHT)) FAIL ("Incorrect COPYRIGHT value found"); INFO ("+ Retrieving next (expect LICENSE)"); comment = fish_sound_comment_next (fsound, comment); if (comment == NULL) FAIL ("Recently inserted LICENSE not retrieved"); if (strcmp (comment->name, "LICENSE")) FAIL ("Incorrect LICENSE name found"); if (strcmp (comment->value, LICENSE)) FAIL ("Incorrect LICENSE value found"); INFO ("+ Retrieving first ARTIST using wierd caps (expect ARTIST1)"); comment = fish_sound_comment_first_byname (fsound, "ArTiSt"); if (comment == NULL) FAIL ("First artist ARTIST1 not retrieved"); if (strcmp (comment->name, "ARTIST")) FAIL ("Incorrect ARTIST1 name found"); if (strcmp (comment->value, ARTIST1)) FAIL ("Incorrect ARTIST1 value found"); INFO ("+ Retrieving next ARTIST (expect ARTIST2)"); comment = fish_sound_comment_next_byname (fsound, comment); if (comment == NULL) FAIL ("Next artist ARTIST2 not retrieved"); if (strcmp (comment->name, "ARTIST")) FAIL ("Incorrect ARTIST2 name found"); if (strcmp (comment->value, ARTIST2)) FAIL ("Incorrect ARTIST2 value found"); return 0; }
int main (int argc, char * argv[]) { FishSoundInfo fsinfo; const FishSoundComment * comment, * comment2; FishSoundComment mycomment; int err; fsinfo.samplerate = 16000; fsinfo.channels = 1; /* The format doesn't really matter as we're not actually * going to encode any audio, so just ensure we can * set this to something that's configured. */ #if HAVE_VORBIS fsinfo.format = FISH_SOUND_VORBIS; #elif HAVE_SPEEX fsinfo.format = FISH_SOUND_SPEEX; #else fsinfo.format = FISH_SOUND_FLAC; #endif #if FS_ENCODE INFO ("Initializing FishSound for comments (encode)"); fsound = fish_sound_new (FISH_SOUND_ENCODE, &fsinfo); INFO ("+ Adding ARTIST1 byname"); err = fish_sound_comment_add_byname (fsound, "ARTIST", ARTIST1); if (err < 0) FAIL ("Operation failed"); INFO ("+ Adding COPYRIGHT byname"); err = fish_sound_comment_add_byname (fsound, "COPYRIGHT", COPYRIGHT); if (err < 0) FAIL ("Operation failed"); INFO ("+ Retrieving first (expect ARTIST1)"); comment = fish_sound_comment_first (fsound); if (comment == NULL) FAIL ("Recently inserted ARTIST1 not retrieved"); if (strcmp (comment->name, "ARTIST")) FAIL ("Incorrect ARTIST1 name found"); if (strcmp (comment->value, ARTIST1)) FAIL ("Incorrect ARTIST1 value found"); INFO ("+ Retrieving next (expect COPYRIGHT)"); comment = fish_sound_comment_next (fsound, comment); if (comment == NULL) FAIL ("Recently inserted COPYRIGHT not retrieved"); if (strcmp (comment->name, "COPYRIGHT")) FAIL ("Incorrect COPYRIGHT name found"); if (strcmp (comment->value, COPYRIGHT)) FAIL ("Incorrect COPYRIGHT value found"); INFO ("+ Checking comments termination"); comment2 = fish_sound_comment_next (fsound, comment); if (comment2 != NULL) FAIL ("Comments unterminated"); INFO ("+ Adding LICENSE from local storage"); mycomment.name = "LICENSE"; mycomment.value = LICENSE; err = fish_sound_comment_add (fsound, &mycomment); if (err < 0) FAIL ("Operation failed"); INFO ("+ Retrieving next (expect LICENSE)"); comment = fish_sound_comment_next (fsound, comment); if (comment == NULL) FAIL ("Recently inserted LICENSE not retrieved"); if (comment == &mycomment) FAIL ("Recently inserted LICENSE not restored"); if (strcmp (comment->name, "LICENSE")) FAIL ("Incorrect LICENSE name found"); if (strcmp (comment->value, LICENSE)) FAIL ("Incorrect LICENSE value found"); INFO ("+ Testing add of valid plain (not key=value) COMMENT byname"); err = fish_sound_comment_add_byname (fsound, COMMENT, NULL); if (err < 0) FAIL ("Operation failed"); INFO ("+ Testing add of valid plain (not key=value) COMMENT from local storage"); mycomment.name = COMMENT; mycomment.value = NULL; err = fish_sound_comment_add (fsound, &mycomment); if (err < 0) FAIL ("Operation failed"); INFO ("+ Adding ARTIST2 byname"); err = fish_sound_comment_add_byname (fsound, "ARTIST", ARTIST2); if (err < 0) FAIL ("Operation failed"); INFO ("+ Retrieving first ARTIST using wierd caps (expect ARTIST1)"); comment = fish_sound_comment_first_byname (fsound, "ArTiSt"); if (comment == NULL) FAIL ("Recently inserted ARTIST1 not retrieved"); if (strcmp (comment->name, "ARTIST")) FAIL ("Incorrect ARTIST1 name found"); if (strcmp (comment->value, ARTIST1)) FAIL ("Incorrect ARTIST1 value found"); INFO ("+ Retrieving next ARTIST (expect ARTIST2)"); comment = fish_sound_comment_next_byname (fsound, comment); if (comment == NULL) FAIL ("Recently inserted ARTIST2 not retrieved"); if (strcmp (comment->name, "ARTIST")) FAIL ("Incorrect ARTIST2 name found"); if (strcmp (comment->value, ARTIST2)) FAIL ("Incorrect ARTIST2 value found"); INFO ("+ Removing LICENSE byname"); err = fish_sound_comment_remove_byname (fsound, "LICENSE"); if (err != 1) FAIL ("Operation failed"); INFO ("+ Attempting to retrieve LICENSE"); comment = fish_sound_comment_first_byname (fsound, "LICENSE"); if (comment != NULL) FAIL ("Removed comment incorrectly retrieved"); INFO ("+ Removing COPYRIGHT from local storage"); mycomment.name = "COPYRIGHT"; mycomment.value = COPYRIGHT; err = fish_sound_comment_remove (fsound, &mycomment); if (err != 1) FAIL ("Operation failed"); INFO ("+ Attempting to retrieve COPYRIGHT"); comment = fish_sound_comment_first_byname (fsound, "COPYRIGHT"); if (comment != NULL) FAIL ("Removed comment incorrectly retrieved"); INFO ("Deleting FishSound (encode)"); fish_sound_delete (fsound); #endif /* FS_ENCODE */ #if FS_DECODE INFO ("Initializing FishSound for comments (decode)"); fsound = fish_sound_new (FISH_SOUND_DECODE, &fsinfo); INFO ("+ Adding ARTIST1 byname (invalid for decode)"); err = fish_sound_comment_add_byname (fsound, "ARTIST", ARTIST1); if (err == 0) FAIL ("Operation disallowed"); INFO ("+ Removing ARTIST byname (invalid for decode)"); err = fish_sound_comment_remove_byname (fsound, "ARTIST"); if (err == 0) FAIL ("Operation disallowed"); INFO ("Deleteing FishSound (decode)"); fish_sound_delete (fsound); #endif exit (0); }