Skip to content

Commit

Permalink
Rewrite ptp_read_string
Browse files Browse the repository at this point in the history
  • Loading branch information
petabyt committed Mar 24, 2024
1 parent 0a8dbfe commit a2c6899
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 34 deletions.
3 changes: 0 additions & 3 deletions src/cl_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
// (accessing a 32 bit integer at an unaligned address - but some might have problems)
#pragma pack(push, 1)

// 4 Seems like a good limit?
struct PtpStorageIds {
uint32_t length;
uint32_t data[4];
Expand Down Expand Up @@ -75,8 +74,6 @@ struct PtpObjectInfo {
uint32_t assoc_desc;
uint32_t sequence_num;

#define PTP_OBJ_INFO_VAR_START 52

char filename[64];
char date_created[32];
char date_modified[32];
Expand Down
47 changes: 37 additions & 10 deletions src/data.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

#include <camlib.h>

// Custom snprint with offset - for safer string building
// Custom snprint with offset
static int osnprintf(char *str, int cur, int size, const char *format, ...) {
if (size - cur < 0) {
ptp_panic("osnprintf overflow %d/%d", cur, size);
Expand Down Expand Up @@ -138,7 +138,6 @@ int ptp_parse_prop_desc(struct PtpRuntime *r, struct PtpPropDesc *oi) {
d += ptp_read_u16(d, &oi->data_type);
d += ptp_read_u8(d, &oi->read_only);

// TODO: Arrays will be ignored
d += parse_data_data_or_u32(d, oi->data_type, &oi->default_value32, &oi->default_value);
d += parse_data_data_or_u32(d, oi->data_type, &oi->current_value32, &oi->current_value);

Expand Down Expand Up @@ -169,8 +168,23 @@ int ptp_parse_prop_desc(struct PtpRuntime *r, struct PtpPropDesc *oi) {

int ptp_parse_object_info(struct PtpRuntime *r, struct PtpObjectInfo *oi) {
uint8_t *d = ptp_get_payload(r);
memcpy(oi, d, PTP_OBJ_INFO_VAR_START);
d += PTP_OBJ_INFO_VAR_START;

d += ptp_read_u32(d, &oi->storage_id);
d += ptp_read_u16(d, &oi->obj_format);
d += ptp_read_u16(d, &oi->protection);
d += ptp_read_u32(d, &oi->compressed_size);
d += ptp_read_u16(d, &oi->thumb_format);
d += ptp_read_u32(d, &oi->thumb_compressed_size);
d += ptp_read_u32(d, &oi->thumb_width);
d += ptp_read_u32(d, &oi->thumb_height);
d += ptp_read_u32(d, &oi->img_width);
d += ptp_read_u32(d, &oi->img_height);
d += ptp_read_u32(d, &oi->img_bit_depth);
d += ptp_read_u32(d, &oi->parent_obj);
d += ptp_read_u16(d, &oi->assoc_type);
d += ptp_read_u32(d, &oi->assoc_desc);
d += ptp_read_u32(d, &oi->sequence_num);

d += ptp_read_string(d, oi->filename, sizeof(oi->filename));
d += ptp_read_string(d, oi->date_created, sizeof(oi->date_created));
d += ptp_read_string(d, oi->date_modified, sizeof(oi->date_modified));
Expand All @@ -185,11 +199,24 @@ int ptp_pack_object_info(struct PtpRuntime *r, struct PtpObjectInfo *oi, uint8_t
return 0;
}

memcpy(buf, oi, PTP_OBJ_INFO_VAR_START);
buf += PTP_OBJ_INFO_VAR_START;
int of = 0;
of += ptp_write_u32(buf + of, oi->storage_id);
of += ptp_write_u16(buf + of, oi->obj_format);
of += ptp_write_u16(buf + of, oi->protection);
of += ptp_write_u32(buf + of, oi->compressed_size);
of += ptp_write_u16(buf + of, oi->thumb_format);
of += ptp_write_u32(buf + of, oi->thumb_compressed_size);
of += ptp_write_u32(buf + of, oi->thumb_width);
of += ptp_write_u32(buf + of, oi->thumb_height);
of += ptp_write_u32(buf + of, oi->img_width);
of += ptp_write_u32(buf + of, oi->img_height);
of += ptp_write_u32(buf + of, oi->img_bit_depth);
of += ptp_write_u32(buf + of, oi->parent_obj);
of += ptp_write_u16(buf + of, oi->assoc_type);
of += ptp_write_u32(buf + of, oi->assoc_desc);
of += ptp_write_u32(buf + of, oi->sequence_num);

// If the string is empty, don't add it to the packet
int of = PTP_OBJ_INFO_VAR_START;
if (oi->filename[0] != '\0')
of += ptp_write_string(buf + of, oi->filename);
if (oi->date_created[0] != '\0')
Expand Down Expand Up @@ -290,7 +317,7 @@ int ptp_device_info_json(struct PtpDeviceInfo *di, char *buffer, int max) {
return curr;
}

const char *eval_obj_format(int code) {
static const char *eval_obj_format(int code) {
char *x = ptp_get_enum_all(code);
switch (code) {
case PTP_OF_Association:
Expand All @@ -301,7 +328,7 @@ const char *eval_obj_format(int code) {
}
}

const char *eval_protection(int code) {
static const char *eval_protection(int code) {
switch (code) {
case 0x0:
return "none";
Expand Down Expand Up @@ -336,7 +363,7 @@ int ptp_object_info_json(struct PtpObjectInfo *so, char *buffer, int max) {
return curr;
}

const char *eval_storage_type(int id) {
static const char *eval_storage_type(int id) {
switch (id) {
case 1:
return "FixedROM";
Expand Down
39 changes: 18 additions & 21 deletions src/packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,30 +45,28 @@ int ptp_write_uint32(void *dat, uint32_t b) {
}

// Read standard UTF16 string
void ptp_read_string_(void *dat, char *string, int max) {
int length = (int)ptp_read_uint8(dat);

int y = 0;
while (y < length) {
string[y] = (char)ptp_read_uint8(dat);
if (string[y] < 0) string[y] = '?'; // TODO: utf16 -> utf8
else if (string[y] != '\0' && string[y] < 32) string[y] = ' ';
ptp_read_uint8(dat); // skip \0
y++;
if (y >= max) { break; }
int ptp_read_string(uint8_t *d, char *string, int max) {
int of = 0;
uint8_t length;
of += ptp_read_u8(d + of, &length);

uint8_t i = 0;
while (i < length) {
uint16_t wchr;
of += ptp_read_u16(d + of, &wchr);
if (wchr > 128) wchr = '?';
else if (wchr != '\0' && wchr < 32) wchr = ' ';
string[i] = (char)wchr;
i++;
if (i >= max) break;
}

string[y] = '\0';
}
string[i] = '\0';

// Read standard UTF16 string
int ptp_read_string(uint8_t *dat, char *string, int max) {
uint8_t *two = dat;
ptp_read_string_(&two, string, max);
return two - dat;
return of;
}

int ptp_read_uint16_array_(void *dat, uint16_t *buf, int max) {
static int ptp_read_uint16_array_(void *dat, uint16_t *buf, int max) {
int n = ptp_read_uint32(dat);

for (int i = 0; i < n; i++) {
Expand All @@ -88,7 +86,7 @@ int ptp_read_uint16_array(uint8_t *dat, uint16_t *buf, int max, int *length) {
return two - dat;
}

int ptp_write_string_(void *dat, char *string) {
static int ptp_write_string_(void *dat, char *string) {
int length = strlen(string);
ptp_write_uint8(dat, length);

Expand Down Expand Up @@ -236,7 +234,6 @@ int ptp_new_data_packet(struct PtpRuntime *r, struct PtpCommand *cmd, void *data
struct PtpBulkContainer *c = (struct PtpBulkContainer *)(r->data);
c->length += data_length;

// Data packets are always 12 bytes (no parameters)
memcpy(r->data + 12, data, data_length);

return length + data_length;
Expand Down

0 comments on commit a2c6899

Please sign in to comment.