id3dev 26.01
An ID3 metadata library
Loading...
Searching...
No Matches
id3v1.c File Reference

Definitions of ID3v1 tag manipulation, file I/O, and utility functions. More...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "id3v1/id3v1Parser.h"
#include "id3v1/id3v1.h"
#include "id3dependencies/ByteStream/include/byteStream.h"
#include "id3dependencies/ByteStream/include/byteInt.h"
Include dependency graph for id3v1.c:

Go to the source code of this file.

Functions

Id3v1Tagid3v1TagFromFile (const char *filePath)
 Creates an Id3v1Tag from a provided file path.
Id3v1Tagid3v1CopyTag (Id3v1Tag *toCopy)
 Creates a deep copy of an Id3v1Tag structure.
int id3v1WriteTitle (const char *title, Id3v1Tag *tag)
 Writes a title to an Id3v1Tag.
int id3v1WriteArtist (const char *artist, Id3v1Tag *tag)
 Writes an artist to an Id3v1Tag.
int id3v1WriteAlbum (const char *album, Id3v1Tag *tag)
 Writes an album to an Id3v1Tag.
int id3v1WriteYear (int year, Id3v1Tag *tag)
 Writes a year to an Id3v1Tag.
int id3v1WriteComment (const char *comment, Id3v1Tag *tag)
 Writes a comment to an Id3v1Tag.
int id3v1WriteGenre (Genre genre, Id3v1Tag *tag)
 Writes a Genre to an Id3v1Tag.
int id3v1WriteTrack (int track, Id3v1Tag *tag)
 Writes a track number to an Id3v1Tag.
bool id3v1CompareTag (const Id3v1Tag *tag1, const Id3v1Tag *tag2)
 Compares two Id3v1Tag structures for equality.
char * id3v1GenreFromTable (Genre val)
 Converts a Genre enum value to it's string representation.
char * id3v1ReadTitle (const Id3v1Tag *tag)
 Reads the title member from an Id3v1Tag.
char * id3v1ReadArtist (const Id3v1Tag *tag)
 Reads the artist member from an Id3v1Tag.
char * id3v1ReadAlbum (const Id3v1Tag *tag)
 Reads the album member from an Id3v1Tag.
int id3v1ReadYear (const Id3v1Tag *tag)
 Reads the year member from an Id3v1Tag.
char * id3v1ReadComment (const Id3v1Tag *tag)
 Reads the comment member from an Id3v1Tag.
Genre id3v1ReadGenre (const Id3v1Tag *tag)
 Reads a Genre from a tag.
int id3v1ReadTrack (const Id3v1Tag *tag)
 Reads a track number from a tag.
char * id3v1ToJSON (const Id3v1Tag *tag)
 Converts an Id3v1Tag to a JSON string representation.
int id3v1WriteTagToFile (const char *filePath, const Id3v1Tag *tag)
 Writes/Overwrites an Id3v1Tag to the bottom of a file .

Detailed Description

Definitions of ID3v1 tag manipulation, file I/O, and utility functions.

Implementation of ID3v1 tag manipulation, file I/O, and utility functions.

Author
Ewan Jones
Version
26.01
Date
2023-10-03 - 2026-01-11

Definition in file id3v1.c.

Function Documentation

◆ id3v1CompareTag()

bool id3v1CompareTag ( const Id3v1Tag * tag1,
const Id3v1Tag * tag2 )

Compares two Id3v1Tag structures for equality.

Performs a field-by-field comparison of all tag members (title, artist, album, year, track, comment, genre).

Parameters
tag1- The first tag to compare
tag2- The second tag to compare
Returns
bool - true if all fields match, false if tags differ in any way.

Definition at line 192 of file id3v1.c.

◆ id3v1CopyTag()

Id3v1Tag * id3v1CopyTag ( Id3v1Tag * toCopy)

Creates a deep copy of an Id3v1Tag structure.

Uses id3v1CreateTag to allocate and initialize a new tag with copied member values.

Parameters
toCopy- The tag to copy. May be NULL.
Returns
Id3v1Tag* - New copy of the tag, or NULL if toCopy is NULL.

Definition at line 64 of file id3v1.c.

◆ id3v1GenreFromTable()

char * id3v1GenreFromTable ( Genre val)

Converts a Genre enum value to it's string representation.

Maps genre enum values to their corresponding string names. Returns "Other" for unrecognized values as specified by the ID3v1 standard

Parameters
val- The genre enum value to convert
Returns
char* - String representation of the genre, never NULL.

Definition at line 235 of file id3v1.c.

◆ id3v1ReadAlbum()

char * id3v1ReadAlbum ( const Id3v1Tag * tag)

Reads the album member from an Id3v1Tag.

Allocates and returns a null-terminated string copy of the tags album to the caller.

Parameters
tag- The tag to read from
Returns
char* - Newly allocated string containing the album.

Definition at line 657 of file id3v1.c.

◆ id3v1ReadArtist()

char * id3v1ReadArtist ( const Id3v1Tag * tag)

Reads the artist member from an Id3v1Tag.

Allocates and returns a null-terminated string copy of the tags artist to the caller.

Parameters
tag- The tag to read from
Returns
char* - Newly allocated string containing the artist.

Definition at line 645 of file id3v1.c.

◆ id3v1ReadComment()

char * id3v1ReadComment ( const Id3v1Tag * tag)

Reads the comment member from an Id3v1Tag.

Allocates and returns a null-terminated string copy of the tags comment to the caller.

Parameters
tag- The tag to read from
Returns
char* - Newly allocated string containing the comment.

Definition at line 679 of file id3v1.c.

◆ id3v1ReadGenre()

Genre id3v1ReadGenre ( const Id3v1Tag * tag)

Reads a Genre from a tag.

Returns the year value directly. Mainly, for compatibility for ffi or abi in other languages.

Parameters
tag- The tag to read from
Returns
Genre - The Genre value from the tag.

Definition at line 691 of file id3v1.c.

◆ id3v1ReadTitle()

char * id3v1ReadTitle ( const Id3v1Tag * tag)

Reads the title member from an Id3v1Tag.

Allocates and returns a null-terminated string copy of the tags title to the caller.

Parameters
tag- The tag to read from
Returns
char* Newly allocated string containing the title.

Definition at line 633 of file id3v1.c.

◆ id3v1ReadTrack()

int id3v1ReadTrack ( const Id3v1Tag * tag)

Reads a track number from a tag.

Returns the track number value directly. Mainly, for compatibility for ffi or abi in other languages.

Parameters
tag- The tag to read from
Returns
int - The track number value from the tag.

Definition at line 701 of file id3v1.c.

◆ id3v1ReadYear()

int id3v1ReadYear ( const Id3v1Tag * tag)

Reads the year member from an Id3v1Tag.

Returns the year value directly. Mainly, for compatibility for ffi or abi in other languages.

Parameters
tag- The tag to read from
Returns
int - The year value from the tag.

Definition at line 669 of file id3v1.c.

◆ id3v1TagFromFile()

Id3v1Tag * id3v1TagFromFile ( const char * filePath)

Creates an Id3v1Tag from a provided file path.

Reads the last 128 bytes from the specified file to extract ID3v1 metadata. The function opens the file in binary read mode, seeks to the position 128 bytes from the end of the file, reads the metadata bytes, and then delegates to id3v1TagFromBuffer for parsing.

Parameters
filePath- The path to the audio file containing ID3v1 metadata. Must not be NULL.
Returns
Id3v1Tag* - Pointer to the parsed ID3v1 tag structure, or NULL if the file cannot be opened, read operations fail, or the filePath parameter is NULL.

Definition at line 29 of file id3v1.c.

◆ id3v1ToJSON()

char * id3v1ToJSON ( const Id3v1Tag * tag)

Converts an Id3v1Tag to a JSON string representation.

Allocates and returns a JSON-formatted string containing all tag fields. Returns "{}" if tag is NULL. The JSON format is: {"title":"...","artist":"...","album":"...","year":n,"track":n,"comment":"...","genreNumber":n,"genre":"..."}. Caller must free the returned string.

Parameters
tag- The tag to convert.
Returns
char* - Newly allocated JSON string, never NULL.

Definition at line 713 of file id3v1.c.

◆ id3v1WriteAlbum()

int id3v1WriteAlbum ( const char * album,
Id3v1Tag * tag )

Writes an album to an Id3v1Tag.

Copies up to ID3V1_FIELD_SIZE bytes from the album string to the tag's album member.

Parameters
album- The album string to write
tag- The tag to write to
Returns
int - 1 on success, 0 on failure.

Definition at line 122 of file id3v1.c.

◆ id3v1WriteArtist()

int id3v1WriteArtist ( const char * artist,
Id3v1Tag * tag )

Writes an artist to an Id3v1Tag.

Copies up to ID3V1_FIELD_SIZE bytes from the artist string to the tag's artist member.

Parameters
artist- The artist string to write
tag- The tag to write to
Returns
int - 1 on success, 0 on failure.

Definition at line 111 of file id3v1.c.

◆ id3v1WriteComment()

int id3v1WriteComment ( const char * comment,
Id3v1Tag * tag )

Writes a comment to an Id3v1Tag.

Copies up to ID3V1_FIELD_SIZE bytes from the comment string to the tag's comment member.

Parameters
comment- The comment string to write
tag- The tag to write to
Returns
int - 1 on success, 0 on failure.

Definition at line 149 of file id3v1.c.

◆ id3v1WriteGenre()

int id3v1WriteGenre ( Genre genre,
Id3v1Tag * tag )

Writes a Genre to an Id3v1Tag.

Assigns the provided genre value to the tag's genre member.

Parameters
genre- The genre value to write
tag- The tag to write to
Returns
int - 1 on success, 0 on failure.

Definition at line 160 of file id3v1.c.

◆ id3v1WriteTagToFile()

int id3v1WriteTagToFile ( const char * filePath,
const Id3v1Tag * tag )

Writes/Overwrites an Id3v1Tag to the bottom of a file .

Serializes the tag to ID3v1 binary format and writes it to the file. Creates the file if it doesn't exist, overwrites existing ID3v1 tags if present, or appends to files without tags.

Parameters
filePath- The file path to write to. Must not be NULL.
tag- The tag to write. Must not be NULL.
Returns
int - 1 on success, 0 on failure.

Definition at line 759 of file id3v1.c.

◆ id3v1WriteTitle()

int id3v1WriteTitle ( const char * title,
Id3v1Tag * tag )

Writes a title to an Id3v1Tag.

Copies up to ID3V1_FIELD_SIZE bytes from the title string to the tag's title member.

Parameters
title- The title string to write
tag- The tag to write to
Returns
int - 1 on success, 0 on failure.

Definition at line 100 of file id3v1.c.

◆ id3v1WriteTrack()

int id3v1WriteTrack ( int track,
Id3v1Tag * tag )

Writes a track number to an Id3v1Tag.

Assigns the provided track number to the tag's track member.

Parameters
track- The track number to write
tag- The tag to write to
Returns
int - 1 on success, 0 on failure.

Definition at line 176 of file id3v1.c.

◆ id3v1WriteYear()

int id3v1WriteYear ( int year,
Id3v1Tag * tag )

Writes a year to an Id3v1Tag.

Copies a provided year to the tag's year member.

Parameters
year- The year as a signed number
tag- The tag to write to
Returns
int - 1 on success, 0 on failure.

Definition at line 133 of file id3v1.c.