11// go:build ignore
2+ #include < cstdint>
23#include < cstring>
34#include < iostream>
45
@@ -78,20 +79,21 @@ taglib_file_write_tags(const char *filename, const char **tags, uint8_t opts) {
7879}
7980
8081struct FileProperties {
81- unsigned int lengthInMilliseconds;
82- unsigned int channels;
83- unsigned int sampleRate;
84- unsigned int bitrate;
82+ uint32_t lengthInMilliseconds;
83+ uint32_t channels;
84+ uint32_t sampleRate;
85+ uint32_t bitrate;
8586 char **imageMetadata;
8687};
8788
8889__attribute__ ((export_name(" taglib_file_read_properties" ))) FileProperties *
89- taglib_file_audioproperties (const char *filename) {
90+ taglib_file_read_properties (const char *filename) {
9091 TagLib::FileRef file (filename);
9192 if (file.isNull () || !file.audioProperties ())
9293 return nullptr ;
9394
94- FileProperties *props = static_cast <FileProperties *>(malloc (sizeof (FileProperties)));
95+ FileProperties *props =
96+ static_cast <FileProperties *>(malloc (sizeof (FileProperties)));
9597 if (!props)
9698 return nullptr ;
9799
@@ -106,7 +108,8 @@ taglib_file_audioproperties(const char *filename) {
106108 props->imageMetadata = nullptr ;
107109 if (!pictures.isEmpty ()) {
108110 size_t len = pictures.size ();
109- char **imageMetadata = static_cast <char **>(malloc (sizeof (char *) * (len + 1 )));
111+ char **imageMetadata =
112+ static_cast <char **>(malloc (sizeof (char *) * (len + 1 )));
110113 if (imageMetadata) {
111114 size_t i = 0 ;
112115 for (const auto &p : pictures) {
@@ -126,7 +129,7 @@ taglib_file_audioproperties(const char *filename) {
126129}
127130
128131struct ByteData {
129- unsigned int length;
132+ uint32_t length;
130133 char *data;
131134};
132135
@@ -143,21 +146,32 @@ taglib_file_read_image(const char *filename, int index) {
143146 if (index < 0 || index >= static_cast <int >(pictures.size ()))
144147 return nullptr ;
145148
149+ auto v = pictures[index][" data" ].toByteVector ();
146150 ByteData *bd = static_cast <ByteData *>(malloc (sizeof (ByteData)));
147151 if (!bd)
148152 return nullptr ;
149153
150- auto v = pictures[index][" data" ].toByteVector ();
151- bd->length = unsigned (v.size ());
152- bd->data = v.data ();
154+ bd->length = static_cast <uint32_t >(v.size ());
155+ if (bd->length == 0 ) {
156+ bd->data = nullptr ;
157+ return bd;
158+ }
159+
160+ // allocate and copy into module memory to keep it valid for go to read
161+ char *buf = static_cast <char *>(malloc (bd->length ));
162+ if (!buf)
163+ return nullptr ;
164+
165+ memcpy (buf, v.data (), bd->length );
166+ bd->data = buf;
167+
153168 return bd;
154169}
155170
156171__attribute__ ((export_name(" taglib_file_write_image" ))) bool
157- taglib_file_write_image(const char *filename, const char *buf,
158- unsigned int length, int index,
159- const char *pictureType, const char *description,
160- const char *mimeType) {
172+ taglib_file_write_image(const char *filename, const char *buf, uint32_t length,
173+ int index, const char *pictureType,
174+ const char *description, const char *mimeType) {
161175 TagLib::FileRef file (filename);
162176 if (file.isNull ())
163177 return false ;
@@ -177,12 +191,11 @@ taglib_file_write_image(const char *filename, const char *buf,
177191 newPicture[" description" ] = to_string (description);
178192 newPicture[" mimeType" ] = to_string (mimeType);
179193
180- // Replace image at index, or append if index is out of range
181- if (index >= 0 && index < static_cast <int >(pictures.size ())) {
194+ // replace image at index, or append if index is out of range
195+ if (index >= 0 && index < static_cast <int >(pictures.size ()))
182196 pictures[index] = newPicture;
183- } else {
197+ else
184198 pictures.append (newPicture);
185- }
186199
187200 if (!file.setComplexProperties (" PICTURE" , pictures))
188201 return false ;
0 commit comments