📄 ptp-pack.c
字号:
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 voidptp_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 voidptp_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_tptp_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 20static inline voidptp_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 15static inline voidptp_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 4static inline voidptp_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++; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -