blob: 9d7a7dc6cad1f4e7f1417b5c0ab2a1cc7b41776f [file] [log] [blame]
diff --git a/src/libmtp.c b/src/libmtp.c
index da2cf9a..14d1532 100644
--- a/src/libmtp.c
+++ b/src/libmtp.c
@@ -5145,6 +5145,56 @@ LIBMTP_track_t *LIBMTP_Get_Trackmetadata(LIBMTP_mtpdevice_t *device, uint32_t co
return track;
}
+/**
+ * This copies a chunk of a file off the device to memory.
+ * @param device a pointer to the device to get the track from.
+ * @param id the file ID of the file to retrieve.
+ * @param offset the offset of the file to read from.
+ * @param count the amount of data to read.
+ * @param data a pointer to the data from the device. Must be
+ * <coded>free()</code>:ed by the caller after use.
+ * @param datalen the amount of data written to <code>data</code>.
+ * @return 0 if the transfer was successful, any other value means
+ * failure.
+ */
+int LIBMTP_Get_File_Chunk(LIBMTP_mtpdevice_t* device, uint32_t id,
+ uint32_t offset, uint32_t count,
+ unsigned char** data, uint32_t* datalen)
+{
+ uint16_t ret;
+ PTPParams *params = (PTPParams *) device->params;
+ PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
+ PTPObject *ob;
+
+ if (!data || !datalen) {
+ add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Get_File_Chunk(): Invalid parameter.");
+ return -1;
+ }
+
+ ret = ptp_object_want (params, id, PTPOBJECT_OBJECTINFO_LOADED, &ob);
+ if (ret != PTP_RC_OK) {
+ add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Get_File_Chunk(): Could not get object info.");
+ return -1;
+ }
+ if (ob->oi.ObjectFormat == PTP_OFC_Association) {
+ add_error_to_errorstack(device, LIBMTP_ERROR_GENERAL, "LIBMTP_Get_File_Chunk(): Bad object format.");
+ return -1;
+ }
+
+ ret = ptp_getpartialobject(params, id, offset, count, data, datalen);
+
+ if (ret == PTP_ERROR_CANCEL) {
+ add_error_to_errorstack(device, LIBMTP_ERROR_CANCELLED, "LIBMTP_Get_File_Chunk(): Cancelled transfer.");
+ return -1;
+ }
+ if (ret != PTP_RC_OK) {
+ add_ptp_error_to_errorstack(device, ret, "LIBMTP_Get_File_Chunk(): Could not get file from device.");
+ return -1;
+ }
+
+ return 0;
+}
+
/**
* This is a manual conversion from MTPDataGetFunc to PTPDataGetFunc
* to isolate the internal type.
diff --git a/src/libmtp.h.in b/src/libmtp.h.in
index f292ec1..671b922 100644
--- a/src/libmtp.h.in
+++ b/src/libmtp.h.in
@@ -923,6 +923,8 @@ LIBMTP_file_t * LIBMTP_Get_Files_And_Folders(LIBMTP_mtpdevice_t *,
uint32_t const,
uint32_t const);
LIBMTP_file_t *LIBMTP_Get_Filemetadata(LIBMTP_mtpdevice_t *, uint32_t const);
+int LIBMTP_Get_File_Chunk(LIBMTP_mtpdevice_t*, uint32_t, uint32_t, uint32_t,
+ unsigned char**, uint32_t*);
int LIBMTP_Get_File_To_File(LIBMTP_mtpdevice_t*, uint32_t, char const * const,
LIBMTP_progressfunc_t const, void const * const);
int LIBMTP_Get_File_To_File_Descriptor(LIBMTP_mtpdevice_t*,
diff --git a/src/libmtp.sym b/src/libmtp.sym
index fec6e58..6736dc4 100644
--- a/src/libmtp.sym
+++ b/src/libmtp.sym
@@ -50,6 +50,7 @@ LIBMTP_Get_Filelisting
LIBMTP_Get_Filelisting_With_Callback
LIBMTP_Get_Files_And_Folders
LIBMTP_Get_Filemetadata
+LIBMTP_Get_File_Chunk
LIBMTP_Get_File_To_File
LIBMTP_Get_File_To_File_Descriptor
LIBMTP_Get_File_To_Handler