forked from odeke-em/chdkptp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ptp-pack.c
773 lines (704 loc) · 22.2 KB
/
ptp-pack.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
/* ptp-pack.c
*
* Copyright (C) 2002-2005 Mariusz Woloszyn <[email protected]>
*
* This file is part of libptp2.
*
* libptp2 is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* libptp2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with libptp2; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* curently this file is included into ptp.c */
static inline uint16_t
htod16p (PTPParams *params, uint16_t var)
{
return ((params->byteorder==PTP_DL_LE)?htole16(var):htobe16(var));
}
static inline uint32_t
htod32p (PTPParams *params, uint32_t var)
{
return ((params->byteorder==PTP_DL_LE)?htole32(var):htobe32(var));
}
static inline void
htod16ap (PTPParams *params, unsigned char *a, uint16_t val)
{
if (params->byteorder==PTP_DL_LE)
htole16a(a,val); else
htobe16a(a,val);
}
static inline void
htod32ap (PTPParams *params, unsigned char *a, uint32_t val)
{
if (params->byteorder==PTP_DL_LE)
htole32a(a,val); else
htobe32a(a,val);
}
static inline uint16_t
dtoh16p (PTPParams *params, uint16_t var)
{
return ((params->byteorder==PTP_DL_LE)?le16toh(var):be16toh(var));
}
static inline uint32_t
dtoh32p (PTPParams *params, uint32_t var)
{
return ((params->byteorder==PTP_DL_LE)?le32toh(var):be32toh(var));
}
static inline uint16_t
dtoh16ap (PTPParams *params, unsigned char *a)
{
return ((params->byteorder==PTP_DL_LE)?le16atoh(a):be16atoh(a));
}
static inline uint32_t
dtoh32ap (PTPParams *params, unsigned char *a)
{
return ((params->byteorder==PTP_DL_LE)?le32atoh(a):be32atoh(a));
}
#define htod8a(a,x) *(uint8_t*)(a) = x
#define htod16a(a,x) htod16ap(params,(unsigned char *)a,x)
#define htod32a(a,x) htod32ap(params,(unsigned char *)a,x)
#define htod16(x) htod16p(params,x)
#define htod32(x) htod32p(params,x)
#define dtoh8a(x) (*(uint8_t*)(x))
#define dtoh16a(a) dtoh16ap(params,(unsigned char *)a)
#define dtoh32a(a) dtoh32ap(params,(unsigned char *)a)
#define dtoh16(x) dtoh16p(params,x)
#define dtoh32(x) dtoh32p(params,x)
static inline char*
ptp_unpack_string(PTPParams *params, char* data, uint16_t offset, uint8_t *len)
{
int i;
char *string=NULL;
*len=dtoh8a(&data[offset]);
if (*len) {
string=malloc(*len);
memset(string, 0, *len);
for (i=0;i<*len && i< PTP_MAXSTRLEN; i++) {
string[i]=(char)dtoh16a(&data[offset+i*2+1]);
}
/* be paranoid! :( */
string[*len-1]=0;
}
return (string);
}
static inline void
ptp_pack_string(PTPParams *params, char *string, char* data, uint16_t offset, uint8_t *len)
{
int i;
*len = (uint8_t)strlen(string);
/* XXX: check strlen! */
htod8a(&data[offset],*len+1);
for (i=0;i<*len && i< PTP_MAXSTRLEN; i++) {
htod16a(&data[offset+i*2+1],(uint16_t)string[i]);
}
}
static inline uint32_t
ptp_unpack_uint32_t_array(PTPParams *params, char* data, uint16_t offset, uint32_t **array)
{
uint32_t n, i=0;
n=dtoh32a(&data[offset]);
*array = malloc (n*sizeof(uint32_t));
while (n>i) {
(*array)[i]=dtoh32a(&data[offset+(sizeof(uint32_t)*(i+1))]);
i++;
}
return n;
}
static inline uint32_t
ptp_unpack_uint16_t_array(PTPParams *params, char* data, uint16_t offset, uint16_t **array)
{
uint32_t n, i=0;
n=dtoh32a(&data[offset]);
*array = malloc (n*sizeof(uint16_t));
while (n>i) {
(*array)[i]=dtoh16a(&data[offset+(sizeof(uint16_t)*(i+2))]);
i++;
}
return n;
}
/* DeviceInfo pack/unpack */
#define PTP_di_StandardVersion 0
#define PTP_di_VendorExtensionID 2
#define PTP_di_VendorExtensionVersion 6
#define PTP_di_VendorExtensionDesc 8
#define PTP_di_FunctionalMode 8
#define PTP_di_OperationsSupported 10
static inline void
ptp_unpack_DI (PTPParams *params, char* data, PTPDeviceInfo *di)
{
uint8_t len;
unsigned int totallen;
di->StaqndardVersion = dtoh16a(&data[PTP_di_StandardVersion]);
di->VendorExtensionID =
dtoh32a(&data[PTP_di_VendorExtensionID]);
di->VendorExtensionVersion =
dtoh16a(&data[PTP_di_VendorExtensionVersion]);
di->VendorExtensionDesc =
ptp_unpack_string(params, data,
PTP_di_VendorExtensionDesc, &len);
totallen=len*2+1;
di->FunctionalMode =
dtoh16a(&data[PTP_di_FunctionalMode+totallen]);
di->OperationsSupported_len = ptp_unpack_uint16_t_array(params, data,
PTP_di_OperationsSupported+totallen,
&di->OperationsSupported);
totallen=totallen+di->OperationsSupported_len*sizeof(uint16_t)+sizeof(uint32_t);
di->EventsSupported_len = ptp_unpack_uint16_t_array(params, data,
PTP_di_OperationsSupported+totallen,
&di->EventsSupported);
totallen=totallen+di->EventsSupported_len*sizeof(uint16_t)+sizeof(uint32_t);
di->DevicePropertiesSupported_len =
ptp_unpack_uint16_t_array(params, data,
PTP_di_OperationsSupported+totallen,
&di->DevicePropertiesSupported);
totallen=totallen+di->DevicePropertiesSupported_len*sizeof(uint16_t)+sizeof(uint32_t);
di->CaptureFormats_len = ptp_unpack_uint16_t_array(params, data,
PTP_di_OperationsSupported+totallen,
&di->CaptureFormats);
totallen=totallen+di->CaptureFormats_len*sizeof(uint16_t)+sizeof(uint32_t);
di->ImageFormats_len = ptp_unpack_uint16_t_array(params, data,
PTP_di_OperationsSupported+totallen,
&di->ImageFormats);
totallen=totallen+di->ImageFormats_len*sizeof(uint16_t)+sizeof(uint32_t);
di->Manufacturer = ptp_unpack_string(params, data,
PTP_di_OperationsSupported+totallen,
&len);
totallen+=len*2+1;
di->Model = ptp_unpack_string(params, data,
PTP_di_OperationsSupported+totallen,
&len);
totallen+=len*2+1;
di->DeviceVersion = ptp_unpack_string(params, data,
PTP_di_OperationsSupported+totallen,
&len);
totallen+=len*2+1;
di->SerialNumber = ptp_unpack_string(params, data,
PTP_di_OperationsSupported+totallen,
&len);
}
/* ObjectHandles array pack/unpack */
#define PTP_oh 0
static inline void
ptp_unpack_OH (PTPParams *params, char* data, PTPObjectHandles *oh)
{
oh->n = ptp_unpack_uint32_t_array(params, data, PTP_oh, &oh->Handler);
}
/* StoreIDs array pack/unpack */
#define PTP_sids 0
static inline void
ptp_unpack_SIDs (PTPParams *params, char* data, PTPStorageIDs *sids)
{
sids->n = ptp_unpack_uint32_t_array(params, data, PTP_sids,
&sids->Storage);
}
/* StorageInfo pack/unpack */
#define PTP_si_StorageType 0
#define PTP_si_FilesystemType 2
#define PTP_si_AccessCapability 4
#define PTP_si_MaxCapability 6
#define PTP_si_FreeSpaceInBytes 14
#define PTP_si_FreeSpaceInImages 22
#define PTP_si_StorageDescription 26
static inline void
ptp_unpack_SI (PTPParams *params, char* data, PTPStorageInfo *si)
{
uint8_t storagedescriptionlen;
si->StorageType=dtoh16a(&data[PTP_si_StorageType]);
si->FilesystemType=dtoh16a(&data[PTP_si_FilesystemType]);
si->AccessCapability=dtoh16a(&data[PTP_si_AccessCapability]);
/* XXX no dtoh64a !!! skiping next two */
si->FreeSpaceInImages=dtoh32a(&data[PTP_si_FreeSpaceInImages]);
si->StorageDescription=ptp_unpack_string(params, data,
PTP_si_StorageDescription, &storagedescriptionlen);
si->VolumeLabel=ptp_unpack_string(params, data,
PTP_si_StorageDescription+storagedescriptionlen*2+1,
&storagedescriptionlen);
}
/* ObjectInfo pack/unpack */
#define PTP_oi_StorageID 0
#define PTP_oi_ObjectFormat 4
#define PTP_oi_ProtectionStatus 6
#define PTP_oi_ObjectCompressedSize 8
#define PTP_oi_ThumbFormat 12
#define PTP_oi_ThumbCompressedSize 14
#define PTP_oi_ThumbPixWidth 18
#define PTP_oi_ThumbPixHeight 22
#define PTP_oi_ImagePixWidth 26
#define PTP_oi_ImagePixHeight 30
#define PTP_oi_ImageBitDepth 34
#define PTP_oi_ParentObject 38
#define PTP_oi_AssociationType 42
#define PTP_oi_AssociationDesc 44
#define PTP_oi_SequenceNumber 48
#define PTP_oi_filenamelen 52
#define PTP_oi_Filename 53
static inline uint32_t
ptp_pack_OI (PTPParams *params, PTPObjectInfo *oi, char** oidataptr)
{
char* oidata;
uint8_t filenamelen;
uint8_t capturedatelen=0;
/* let's allocate some memory first; XXX i'm sure it's wrong */
oidata=malloc(PTP_oi_Filename+(strlen(oi->Filename)+1)*2+4);
/* the caller should free it after use! */
#if 0
char *capture_date="20020101T010101"; /* XXX Fake date */
#endif
memset (oidata, 0, (PTP_oi_Filename+(strlen(oi->Filename)+1)*2+4));
htod32a(&oidata[PTP_oi_StorageID],oi->StorageID);
htod16a(&oidata[PTP_oi_ObjectFormat],oi->ObjectFormat);
htod16a(&oidata[PTP_oi_ProtectionStatus],oi->ProtectionStatus);
htod32a(&oidata[PTP_oi_ObjectCompressedSize],oi->ObjectCompressedSize);
htod16a(&oidata[PTP_oi_ThumbFormat],oi->ThumbFormat);
htod32a(&oidata[PTP_oi_ThumbCompressedSize],oi->ThumbCompressedSize);
htod32a(&oidata[PTP_oi_ThumbPixWidth],oi->ThumbPixWidth);
htod32a(&oidata[PTP_oi_ThumbPixHeight],oi->ThumbPixHeight);
htod32a(&oidata[PTP_oi_ImagePixWidth],oi->ImagePixWidth);
htod32a(&oidata[PTP_oi_ImagePixHeight],oi->ImagePixHeight);
htod32a(&oidata[PTP_oi_ImageBitDepth],oi->ImageBitDepth);
htod32a(&oidata[PTP_oi_ParentObject],oi->ParentObject);
htod16a(&oidata[PTP_oi_AssociationType],oi->AssociationType);
htod32a(&oidata[PTP_oi_AssociationDesc],oi->AssociationDesc);
htod32a(&oidata[PTP_oi_SequenceNumber],oi->SequenceNumber);
ptp_pack_string(params, oi->Filename, oidata, PTP_oi_filenamelen, &filenamelen);
/*
filenamelen=(uint8_t)strlen(oi->Filename);
htod8a(&req->data[PTP_oi_filenamelen],filenamelen+1);
for (i=0;i<filenamelen && i< PTP_MAXSTRLEN; i++) {
req->data[PTP_oi_Filename+i*2]=oi->Filename[i];
}
*/
/*
*XXX Fake date.
* for example Kodak sets Capture date on the basis of EXIF data.
* Spec says that this field is from perspective of Initiator.
*/
#if 0 /* seems now we don't need any data packed in OI dataset... for now ;)*/
capturedatelen=strlen(capture_date);
htod8a(&data[PTP_oi_Filename+(filenamelen+1)*2],
capturedatelen+1);
for (i=0;i<capturedatelen && i< PTP_MAXSTRLEN; i++) {
data[PTP_oi_Filename+(i+filenamelen+1)*2+1]=capture_date[i];
}
htod8a(&data[PTP_oi_Filename+(filenamelen+capturedatelen+2)*2+1],
capturedatelen+1);
for (i=0;i<capturedatelen && i< PTP_MAXSTRLEN; i++) {
data[PTP_oi_Filename+(i+filenamelen+capturedatelen+2)*2+2]=
capture_date[i];
}
#endif
/* XXX this function should return dataset length */
*oidataptr=oidata;
return (PTP_oi_Filename+(filenamelen+1)*2+(capturedatelen+1)*4);
}
static inline void
ptp_unpack_OI (PTPParams *params, char* data, PTPObjectInfo *oi)
{
uint8_t filenamelen;
uint8_t capturedatelen;
char *capture_date;
char tmp[16];
struct tm tm;
memset(&tm,0,sizeof(tm));
oi->StorageID=dtoh32a(&data[PTP_oi_StorageID]);
oi->ObjectFormat=dtoh16a(&data[PTP_oi_ObjectFormat]);
oi->ProtectionStatus=dtoh16a(&data[PTP_oi_ProtectionStatus]);
oi->ObjectCompressedSize=dtoh32a(&data[PTP_oi_ObjectCompressedSize]);
oi->ThumbFormat=dtoh16a(&data[PTP_oi_ThumbFormat]);
oi->ThumbCompressedSize=dtoh32a(&data[PTP_oi_ThumbCompressedSize]);
oi->ThumbPixWidth=dtoh32a(&data[PTP_oi_ThumbPixWidth]);
oi->ThumbPixHeight=dtoh32a(&data[PTP_oi_ThumbPixHeight]);
oi->ImagePixWidth=dtoh32a(&data[PTP_oi_ImagePixWidth]);
oi->ImagePixHeight=dtoh32a(&data[PTP_oi_ImagePixHeight]);
oi->ImageBitDepth=dtoh32a(&data[PTP_oi_ImageBitDepth]);
oi->ParentObject=dtoh32a(&data[PTP_oi_ParentObject]);
oi->AssociationType=dtoh16a(&data[PTP_oi_AssociationType]);
oi->AssociationDesc=dtoh32a(&data[PTP_oi_AssociationDesc]);
oi->SequenceNumber=dtoh32a(&data[PTP_oi_SequenceNumber]);
oi->Filename= ptp_unpack_string(params, data, PTP_oi_filenamelen, &filenamelen);
capture_date = ptp_unpack_string(params, data,
PTP_oi_filenamelen+filenamelen*2+1, &capturedatelen);
/* subset of ISO 8601, without '.s' tenths of second and
* time zone
*/
if (capturedatelen>15)
{
strncpy (tmp, capture_date, 4);
tmp[4] = 0;
tm.tm_year=atoi (tmp) - 1900;
strncpy (tmp, capture_date + 4, 2);
tmp[2] = 0;
tm.tm_mon = atoi (tmp) - 1;
strncpy (tmp, capture_date + 6, 2);
tmp[2] = 0;
tm.tm_mday = atoi (tmp);
strncpy (tmp, capture_date + 9, 2);
tmp[2] = 0;
tm.tm_hour = atoi (tmp);
strncpy (tmp, capture_date + 11, 2);
tmp[2] = 0;
tm.tm_min = atoi (tmp);
strncpy (tmp, capture_date + 13, 2);
tmp[2] = 0;
tm.tm_sec = atoi (tmp);
oi->CaptureDate=mktime (&tm);
}
free(capture_date);
/* now it's modification date ;) */
capture_date = ptp_unpack_string(params, data,
PTP_oi_filenamelen+filenamelen*2
+capturedatelen*2+2,&capturedatelen);
if (capturedatelen>15)
{
strncpy (tmp, capture_date, 4);
tmp[4] = 0;
tm.tm_year=atoi (tmp) - 1900;
strncpy (tmp, capture_date + 4, 2);
tmp[2] = 0;
tm.tm_mon = atoi (tmp) - 1;
strncpy (tmp, capture_date + 6, 2);
tmp[2] = 0;
tm.tm_mday = atoi (tmp);
strncpy (tmp, capture_date + 9, 2);
tmp[2] = 0;
tm.tm_hour = atoi (tmp);
strncpy (tmp, capture_date + 11, 2);
tmp[2] = 0;
tm.tm_min = atoi (tmp);
strncpy (tmp, capture_date + 13, 2);
tmp[2] = 0;
tm.tm_sec = atoi (tmp);
oi->ModificationDate=mktime (&tm);
}
free(capture_date);
}
/* Custom Type Value Assignement (without Length) macro frequently used below */
#define CTVAL(type,func,target) { \
*target = malloc(sizeof(type)); \
**(type **)target = \
func(data);\
}
static inline void
ptp_unpack_DPV (PTPParams *params, char* data, void** value, uint16_t datatype)
{
switch (datatype) {
case PTP_DTC_INT8:
CTVAL(int8_t,dtoh8a,value);
break;
case PTP_DTC_UINT8:
CTVAL(uint8_t,dtoh8a,value);
break;
case PTP_DTC_INT16:
CTVAL(int16_t,dtoh16a,value);
break;
case PTP_DTC_UINT16:
CTVAL(uint16_t,dtoh16a,value);
break;
case PTP_DTC_INT32:
CTVAL(int32_t,dtoh32a,value);
break;
case PTP_DTC_UINT32:
CTVAL(uint32_t,dtoh32a,value);
break;
/* XXX: other int types are unimplemented */
/* XXX: int arrays are unimplemented also */
case PTP_DTC_STR:
{
uint8_t len;
*(char **)&(*value)=ptp_unpack_string(params,data,0,&len);
break;
}
}
}
/* Device Property pack/unpack */
#define PTP_dpd_DevicePropertyCode 0
#define PTP_dpd_DataType 2
#define PTP_dpd_GetSet 4
#define PTP_dpd_FactoryDefaultValue 5
/* Custom Type Value Assignement macro frequently used below */
#define CTVA(type,func,target) { \
target = malloc(sizeof(type)); \
*(type *)target = \
func(&data[PTP_dpd_FactoryDefaultValue+totallen]);\
totallen+=sizeof(type); \
}
/* Many Custom Types Vale Assignement macro frequently used below */
#define MCTVA(type,func,target,n) { \
uint16_t i; \
for (i=0;i<n;i++) { \
target[i] = malloc(sizeof(type)); \
*(type *)target[i] = \
func(&data[PTP_dpd_FactoryDefaultValue+totallen]);\
totallen+=sizeof(type); \
} \
}
static inline void
ptp_unpack_DPD (PTPParams *params, char* data, PTPDevicePropDesc *dpd)
{
uint8_t len;
int totallen=0;
dpd->DevicePropertyCode=dtoh16a(&data[PTP_dpd_DevicePropertyCode]);
dpd->DataType=dtoh16a(&data[PTP_dpd_DataType]);
dpd->GetSet=dtoh8a(&data[PTP_dpd_GetSet]);
dpd->FactoryDefaultValue = NULL;
dpd->CurrentValue = NULL;
switch (dpd->DataType) {
case PTP_DTC_INT8:
CTVA(int8_t,dtoh8a,dpd->FactoryDefaultValue);
CTVA(int8_t,dtoh8a,dpd->CurrentValue);
break;
case PTP_DTC_UINT8:
CTVA(uint8_t,dtoh8a,dpd->FactoryDefaultValue);
CTVA(uint8_t,dtoh8a,dpd->CurrentValue);
break;
case PTP_DTC_INT16:
CTVA(int16_t,dtoh16a,dpd->FactoryDefaultValue);
CTVA(int16_t,dtoh16a,dpd->CurrentValue);
break;
case PTP_DTC_UINT16:
CTVA(uint16_t,dtoh16a,dpd->FactoryDefaultValue);
CTVA(uint16_t,dtoh16a,dpd->CurrentValue);
break;
case PTP_DTC_INT32:
CTVA(int32_t,dtoh32a,dpd->FactoryDefaultValue);
CTVA(int32_t,dtoh32a,dpd->CurrentValue);
break;
case PTP_DTC_UINT32:
CTVA(uint32_t,dtoh32a,dpd->FactoryDefaultValue);
CTVA(uint32_t,dtoh32a,dpd->CurrentValue);
break;
/* XXX: other int types are unimplemented */
/* XXX: int arrays are unimplemented also */
case PTP_DTC_STR:
dpd->FactoryDefaultValue = (void *)ptp_unpack_string
(params,data,PTP_dpd_FactoryDefaultValue,&len);
totallen=len*2+1;
dpd->CurrentValue = (void *)ptp_unpack_string
(params, data, PTP_dpd_FactoryDefaultValue +
totallen, &len);
totallen+=len*2+1;
break;
}
/* if totallen==0 then Data Type format is not supported by this
code or the Data Type is a string (with two empty strings as
values). In both cases Form Flag should be set to 0x00 and FORM is
not present. */
dpd->FormFlag=PTP_DPFF_None;
if (totallen==0) return;
dpd->FormFlag=dtoh8a(&data[PTP_dpd_FactoryDefaultValue+totallen]);
totallen+=sizeof(uint8_t);
switch (dpd->FormFlag) {
case PTP_DPFF_Range:
switch (dpd->DataType) {
case PTP_DTC_INT8:
CTVA(int8_t,dtoh8a,dpd->FORM.Range.MinimumValue);
CTVA(int8_t,dtoh8a,dpd->FORM.Range.MaximumValue);
CTVA(int8_t,dtoh8a,dpd->FORM.Range.StepSize);
break;
case PTP_DTC_UINT8:
CTVA(uint8_t,dtoh8a,dpd->FORM.Range.MinimumValue);
CTVA(uint8_t,dtoh8a,dpd->FORM.Range.MaximumValue);
CTVA(uint8_t,dtoh8a,dpd->FORM.Range.StepSize);
break;
case PTP_DTC_INT16:
CTVA(int16_t,dtoh16a,dpd->FORM.Range.MinimumValue);
CTVA(int16_t,dtoh16a,dpd->FORM.Range.MaximumValue);
CTVA(int16_t,dtoh16a,dpd->FORM.Range.StepSize);
break;
case PTP_DTC_UINT16:
CTVA(uint16_t,dtoh16a,dpd->FORM.Range.MinimumValue);
CTVA(uint16_t,dtoh16a,dpd->FORM.Range.MaximumValue);
CTVA(uint16_t,dtoh16a,dpd->FORM.Range.StepSize);
break;
case PTP_DTC_INT32:
CTVA(int32_t,dtoh32a,dpd->FORM.Range.MinimumValue);
CTVA(int32_t,dtoh32a,dpd->FORM.Range.MaximumValue);
CTVA(int32_t,dtoh32a,dpd->FORM.Range.StepSize);
break;
case PTP_DTC_UINT32:
CTVA(uint32_t,dtoh32a,dpd->FORM.Range.MinimumValue);
CTVA(uint32_t,dtoh32a,dpd->FORM.Range.MaximumValue);
CTVA(uint32_t,dtoh32a,dpd->FORM.Range.StepSize);
break;
/* XXX: other int types are unimplemented */
/* XXX: int arrays are unimplemented also */
/* XXX: does it make any sense: "a range of strings"? */
}
break;
case PTP_DPFF_Enumeration:
#define N dpd->FORM.Enum.NumberOfValues
N = dtoh16a(&data[PTP_dpd_FactoryDefaultValue+totallen]);
totallen+=sizeof(uint16_t);
dpd->FORM.Enum.SupportedValue = malloc(N*sizeof(void *));
switch (dpd->DataType) {
case PTP_DTC_INT8:
MCTVA(int8_t,dtoh8a,dpd->FORM.Enum.SupportedValue,N);
break;
case PTP_DTC_UINT8:
MCTVA(uint8_t,dtoh8a,dpd->FORM.Enum.SupportedValue,N);
break;
case PTP_DTC_INT16:
MCTVA(int16_t,dtoh16a,dpd->FORM.Enum.SupportedValue,N);
break;
case PTP_DTC_UINT16:
MCTVA(uint16_t,dtoh16a,dpd->FORM.Enum.SupportedValue,N);
break;
case PTP_DTC_INT32:
MCTVA(int32_t,dtoh32a,dpd->FORM.Enum.SupportedValue,N);
break;
case PTP_DTC_UINT32:
MCTVA(uint32_t,dtoh32a,dpd->FORM.Enum.SupportedValue,N);
break;
case PTP_DTC_STR:
{
int i;
for(i=0;i<N;i++)
{
*(char **)&dpd->FORM.Enum.SupportedValue[i]=
ptp_unpack_string
(params,data,PTP_dpd_FactoryDefaultValue
+totallen,&len);
totallen+=len*2+1;
}
}
break;
}
}
}
static inline uint32_t
ptp_pack_DPV (PTPParams *params, void* value, char** dpvptr, uint16_t datatype)
{
char* dpv=NULL;
uint32_t size=0;
switch (datatype) {
case PTP_DTC_INT8:
size=sizeof(int8_t);
dpv=malloc(size);
htod8a(dpv,*(int8_t*)value);
break;
case PTP_DTC_UINT8:
size=sizeof(uint8_t);
dpv=malloc(size);
htod8a(dpv,*(uint8_t*)value);
break;
case PTP_DTC_INT16:
size=sizeof(int16_t);
dpv=malloc(size);
htod16a(dpv,*(int16_t*)value);
break;
case PTP_DTC_UINT16:
size=sizeof(uint16_t);
dpv=malloc(size);
htod16a(dpv,*(uint16_t*)value);
break;
case PTP_DTC_INT32:
size=sizeof(int32_t);
dpv=malloc(size);
htod32a(dpv,*(int32_t*)value);
break;
case PTP_DTC_UINT32:
size=sizeof(uint32_t);
dpv=malloc(size);
htod32a(dpv,*(uint32_t*)value);
break;
/* XXX: other int types are unimplemented */
/* XXX: int arrays are unimplemented also */
case PTP_DTC_STR:
{
uint8_t len;
size=strlen((char*)value)*2+3;
dpv=malloc(size);
memset(dpv,0,size);
ptp_pack_string(params, (char *)value, dpv, 0, &len);
}
break;
}
*dpvptr=dpv;
return size;
}
/*
PTP USB Event container unpack
Copyright (c) 2003 Nikolai Kopanygin
*/
#define PTP_ec_Length 0
#define PTP_ec_Type 4
#define PTP_ec_Code 6
#define PTP_ec_TransId 8
#define PTP_ec_Param1 12
#define PTP_ec_Param2 16
#define PTP_ec_Param3 20
static inline void
ptp_unpack_EC (PTPParams *params, char* data, PTPUSBEventContainer *ec)
{
if (data==NULL)
return;
ec->length=dtoh32a(&data[PTP_ec_Length]);
ec->type=dtoh16a(&data[PTP_ec_Type]);
ec->code=dtoh16a(&data[PTP_ec_Code]);
ec->trans_id=dtoh32a(&data[PTP_ec_TransId]);
if (ec->length>=(PTP_ec_Param1+4))
ec->param1=dtoh32a(&data[PTP_ec_Param1]);
else
ec->param1=0;
if (ec->length>=(PTP_ec_Param2+4))
ec->param2=dtoh32a(&data[PTP_ec_Param2]);
else
ec->param2=0;
if (ec->length>=(PTP_ec_Param3+4))
ec->param3=dtoh32a(&data[PTP_ec_Param3]);
else
ec->param3=0;
}
/*
PTP Canon Folder Entry unpack
Copyright (c) 2003 Nikolai Kopanygin
*/
#define PTP_cfe_ObjectHandle 0
#define PTP_cfe_ObjectFormatCode 4
#define PTP_cfe_Flags 6
#define PTP_cfe_ObjectSize 7
#define PTP_cfe_Time 11
#define PTP_cfe_Filename 15
static inline void
ptp_unpack_Canon_FE (PTPParams *params, char* data, PTPCANONFolderEntry *fe)
{
int i;
if (data==NULL)
return;
fe->ObjectHandle=dtoh32a(&data[PTP_cfe_ObjectHandle]);
fe->ObjectFormatCode=dtoh16a(&data[PTP_cfe_ObjectFormatCode]);
fe->Flags=dtoh8a(&data[PTP_cfe_Flags]);
fe->ObjectSize=dtoh32a(&data[PTP_cfe_ObjectSize]);
fe->Time=(time_t)dtoh32a(&data[PTP_cfe_Time]);
for (i=0; i<PTP_CANON_FilenameBufferLen; i++)
fe->Filename[i]=(char)dtoh8a(&data[PTP_cfe_Filename+i]);
}
#define PTP_NIKON_ec_Num 0
#define PTP_NIKON_ec_Code 2
#define PTP_NIKON_ec_Param1 4
static inline void
ptp_nikon_unpack_EC (PTPParams *params, char *evdata, PTPUSBEventContainer** event, uint16_t* evnum)
{
int i=0;
PTPUSBEventContainer *events;
*evnum=dtoh16a(&evdata[PTP_NIKON_ec_Num]);
events=calloc(*evnum, sizeof(PTPUSBEventContainer));
*event=events;
while (i<*evnum) {
events->code = dtoh16a(&evdata[PTP_NIKON_ec_Code+
((sizeof(uint16_t)+sizeof(uint32_t))*i)]);
events->param1 = dtoh32a(&evdata[PTP_NIKON_ec_Param1+
((sizeof(uint16_t)+sizeof(uint32_t))*i)]);
events++;
i++;
}
}