static VALUE rbosa_element_eql (VALUE self, VALUE other) { AEDesc * self_desc; AEDesc * other_desc; Size data_size; void * self_data; void * other_data; OSErr error; Boolean ok; if (!rb_obj_is_kind_of (other, rb_class_real (rb_class_of (self)))) return Qfalse; self_desc = rbosa_element_aedesc (self); other_desc = rbosa_element_aedesc (other); if (self_desc == other_desc) return Qtrue; if (self_desc->descriptorType != other_desc->descriptorType) return Qfalse; data_size = AEGetDescDataSize (self_desc); if (data_size != AEGetDescDataSize (other_desc)) return Qfalse; self_data = (void *)malloc (data_size); other_data = (void *)malloc (data_size); ok = 0; if (self_data == NULL || other_data == NULL) rb_fatal ("cannot allocate memory"); error = AEGetDescData (self_desc, self_data, data_size); if (error != noErr) goto bails; error = AEGetDescData (other_desc, other_data, data_size); if (error != noErr) goto bails; ok = memcmp (self_data, other_data, data_size) == 0; bails: free (self_data); free (other_data); return CBOOL2RVAL (ok); }
// if the input is UTF8Text, returns its size in bytes. Otherwise returns -1 ptrdiff_t _hs_getUTF8Size(const AEDesc* input) { if (input != NULL && input->descriptorType == typeUTF8Text) { return AEGetDescDataSize(input); } else { return -1; } }
pascal OSErr MoreAEGetCFStringFromDescriptor(const AEDesc* pAEDesc, CFStringRef* pCFStringRef) { AEDesc uniAEDesc = {typeNull, NULL}; OSErr anErr; if (NULL == pCFStringRef) return paramErr; anErr = AECoerceDesc(pAEDesc, typeUnicodeText, &uniAEDesc); if (noErr == anErr) { if (typeUnicodeText == uniAEDesc.descriptorType) { Size bufSize = AEGetDescDataSize(&uniAEDesc); Ptr buf = NewPtr(bufSize); if ((noErr == (anErr = MemError())) && (NULL != buf)) { anErr = AEGetDescData(&uniAEDesc, buf, bufSize); if (noErr == anErr) *pCFStringRef = CFStringCreateWithCharacters(kCFAllocatorDefault, (UniChar*) buf, bufSize / (Size) sizeof(UniChar)); DisposePtr(buf); } } MoreAEDisposeDesc(&uniAEDesc); } return (anErr); }//end MoreAEGetCFStringFromDescriptor
static VALUE __rbosa_class_from_desc_data (VALUE app, AEDesc res) { DescType data; Size datasize; VALUE classes, klass; classes = rb_ivar_get (app, sClasses); if (NIL_P (classes)) return Qnil; klass = Qnil; datasize = AEGetDescDataSize (&res); /* This should always be a four-byte code. */ if (datasize != sizeof (DescType)) return Qnil; if (AEGetDescData (&res, &data, datasize) == noErr) { char dtStr[5]; *(DescType *)dtStr = CFSwapInt32HostToBig (data); klass = rb_hash_aref (classes, rb_str_new (dtStr, 4)); } return klass; }
OSErr getURLFromUTextDesc(const AEDesc *utdesc_p, CFURLRef *urlRef_p) { OSErr err; Size theLength = AEGetDescDataSize(utdesc_p); UInt8 *theData = malloc(theLength); err = AEGetDescData(utdesc_p, theData, theLength); if (err != noErr) goto bail; CFStringRef pathStr = CFStringCreateWithBytes(NULL, theData, theLength, kCFStringEncodingUnicode, false); CFURLPathStyle pathStyle; if (CFStringHasPrefix(pathStr, CFSTR("/"))) { pathStyle = kCFURLPOSIXPathStyle; } else { pathStyle = kCFURLHFSPathStyle; } *urlRef_p = CFURLCreateWithFileSystemPath(NULL, pathStr, pathStyle, true); CFRelease(pathStr); bail: free(theData); return err; }
static VALUE rbosa_element_data (int argc, VALUE *argv, VALUE self) { VALUE coerce_type; AEDesc coerced_desc; AEDesc * desc; OSErr error; void * data; Size datasize; VALUE retval; bool to_4cc; rb_scan_args (argc, argv, "01", &coerce_type); to_4cc = false; desc = rbosa_element_aedesc (self); if (!NIL_P (coerce_type)) { FourCharCode code; code = RVAL2FOURCHAR (coerce_type); error = AECoerceDesc (desc, code, &coerced_desc); if (error != noErr) rb_raise (rb_eRuntimeError, "Cannot coerce desc to type %s : %s (%d)", RVAL2CSTR (coerce_type), error_code_to_string (error), error); desc = &coerced_desc; to_4cc = code == 'type'; } datasize = AEGetDescDataSize (desc); data = (void *)malloc (datasize); if (data == NULL) rb_fatal ("cannot allocate memory"); error = AEGetDescData (desc, data, datasize); if (error == noErr) { if (to_4cc) *(DescType*)data = CFSwapInt32HostToBig (*(DescType*)data); retval = rb_str_new (data, datasize); } else { retval = Qnil; } if (!NIL_P (coerce_type)) AEDisposeDesc (&coerced_desc); free (data); if (error != noErr) rb_raise (rb_eRuntimeError, "Cannot get desc data : %s (%d)", error_code_to_string (error), error); return retval; }
static void __rbosa_raise_potential_app_error (AEDesc *reply) { OSErr error; AEDesc errorNumDesc; AEDesc errorStringDesc; int errorNum; const char * errorMsg; char exception[128]; if (AEGetParamDesc (reply, keyErrorNumber, typeSInt32, &errorNumDesc) != noErr) return; if (AEGetDescData (&errorNumDesc, &errorNum, sizeof errorNum) != noErr) { AEDisposeDesc (&errorNumDesc); return; } if (errorNum == noErr) return; /* The reply is an application error. */ errorMsg = error_code_to_string(errorNum); if (errorMsg == NULL) errorMsg = "Unknown error"; exception[0] = '\0'; error = AEGetParamDesc (reply, keyErrorString, typeChar, &errorStringDesc); if (error == noErr) { Size size; size = AEGetDescDataSize (&errorStringDesc); if (size > 0) { char *msg; msg = (char *)malloc (size + 1); if (msg != NULL) { if (AEGetDescData (&errorStringDesc, msg, size) == noErr) { msg[size] = '\0'; snprintf (exception, sizeof exception, "application returned error: %s (%d), with message: %s", errorMsg, errorNum, msg); } free (msg); } } AEDisposeDesc (&errorStringDesc); } if (exception[0] == '\0') snprintf (exception, sizeof exception, "application returned error: %s (%d)", errorMsg, errorNum); AEDisposeDesc (&errorNumDesc); rb_raise (rb_eRuntimeError, exception); }
static PyObject *AEDesc_repr(AEDescObject *self) { PyObject *type, *rep; type = AE_BuildOSType(self->ob_itself.descriptorType); rep = PyUnicode_FromFormat("<aem.ae.AEDesc type=%R size=%ld>", type, AEGetDescDataSize(&self->ob_itself)); Py_DECREF(type); return rep; }
static PyObject *AEDesc_repr(AEDescObject *self) { PyObject *type, *typerep, *rep; type = AE_BuildOSType(self->ob_itself.descriptorType); typerep = PyObject_Repr(type); rep = PyString_FromFormat("<aem.ae.AEDesc type=%s size=%ld>", PyString_AsString(typerep), AEGetDescDataSize(&self->ob_itself)); Py_DECREF(type); Py_DECREF(typerep); return rep; }
static PyObject *AEDesc_AEGetDescDataSize(AEDescObject *_self, PyObject *_args) { PyObject *_res = NULL; Size _rv; #ifndef AEGetDescDataSize PyMac_PRECHECK(AEGetDescDataSize); #endif if (!PyArg_ParseTuple(_args, "")) return NULL; _rv = AEGetDescDataSize(&_self->ob_itself); _res = Py_BuildValue("l", _rv); return _res; }
static PyObject *AEDesc_get_data(AEDescObject *self, void *closure) { PyObject *res; Size size; char *ptr; OSErr err; size = AEGetDescDataSize(&self->ob_itself); if ( (res = PyBytes_FromStringAndSize(NULL, size)) == NULL ) return NULL; if ( (ptr = PyBytes_AsString(res)) == NULL ) return NULL; if ( (err=AEGetDescData(&self->ob_itself, ptr, size)) < 0 ) return AE_MacOSError(err); return res; }
static boolean getdescdata (AEDesc *desc, Handle *h) { /* PBS 03/14/02: get data from AEDesc's data handle as a handle. The handle is allocated here. Caller will dispose of handle, unless this function returns false. */ Size len; len = AEGetDescDataSize (desc); /* kw - 2005-12-12 - we should return at least a zero sized handle if (len < 1) { *h = nil; return (false); } */ if (!newclearhandle (len, h)) { *h = nil; return (false); } lockhandle (*h); if (AEGetDescData (desc, **h, len) != noErr) { disposehandle (*h); *h = nil; return (false); } /*if*/ unlockhandle (*h); return (true); } /*getdescdata*/
CFStringRef CFStringCreateWithAEDesc(const AEDesc *desc, OSErr *errPtr) { CFStringEncoding encodeKey; CFStringRef result = NULL; Boolean isExternalRepresentation = false; void *dataPtr = NULL; switch (desc->descriptorType) { case typeChar: encodeKey = CFStringGetSystemEncoding(); break; case typeUTF8Text: encodeKey = kCFStringEncodingUTF8; break; case typeUnicodeText: encodeKey = kCFStringEncodingUnicode; break; case typeUTF16ExternalRepresentation: encodeKey = kCFStringEncodingUTF16; isExternalRepresentation = true; break; default : goto bail; } Size dataSize = AEGetDescDataSize (desc); dataPtr = malloc(dataSize); *errPtr = AEGetDescData(desc, dataPtr, dataSize); if (noErr != *errPtr) { goto bail; } result = CFStringCreateWithBytes(kCFAllocatorDefault, dataPtr, dataSize, encodeKey, isExternalRepresentation); bail: if (dataPtr) free(dataPtr); return result; }
static PyObject *AE_AddressDescToPath(PyObject *_self, PyObject *_args) { AEDesc desc; ProcessSerialNumber psn; pid_t pid; OSType creatorType = kLSUnknownCreator; Size cSize; char *cStr; CFStringRef bundleID = NULL; FSRef fsref; UInt8 path[PATH_MAX]; PyObject* pathObj; OSStatus err; if (!PyArg_ParseTuple(_args, "O&", AE_AEDesc_Convert, &desc)) return NULL; switch (desc.descriptorType) { case typeKernelProcessID: err = AEGetDescData(&desc, &pid, sizeof(pid)); if (err) return AE_MacOSError(err); err = GetProcessForPID(pid, &psn); if (err) return AE_MacOSError(err); err = GetProcessBundleLocation(&psn, &fsref); if (err) return AE_MacOSError(err); break; case typeProcessSerialNumber: err = AEGetDescData(&desc, &psn, sizeof(psn)); if (err) return AE_MacOSError(err); err = GetProcessBundleLocation(&psn, &fsref); if (err) return AE_MacOSError(err); break; case typeApplSignature: err = AEGetDescData(&desc, &creatorType, sizeof(creatorType)); if (err) return AE_MacOSError(err); err = LSFindApplicationForInfo(creatorType, bundleID, NULL, &fsref, NULL); if (err) return AE_MacOSError(err); break; case typeApplicationBundleID: cSize = AEGetDescDataSize(&desc); cStr = malloc((size_t)cSize); if (!cStr) return AE_MacOSError(errAECoercionFail); err = AEGetDescData(&desc, cStr, cSize); if (err) return AE_MacOSError(err); bundleID = CFStringCreateWithBytes(NULL, (UInt8 *)cStr, (CFIndex)cSize, kCFStringEncodingUTF8, 0); free(cStr); if (!bundleID) return AE_MacOSError(errAECoercionFail); err = LSFindApplicationForInfo(creatorType, bundleID, NULL, &fsref, NULL); if (err) return AE_MacOSError(err); break; case typeMachPort: // unsupported case typeApplicationURL: // unsupported (remote applications) default: return AE_MacOSError(errAECoercionFail); } err = FSRefMakePath(&fsref, path, sizeof(path)); if (err) return AE_MacOSError(err); pathObj = PyUnicode_DecodeUTF8((char *)path, strlen((char *)path), NULL); return Py_BuildValue("O", pathObj); }
static OSErr ScriptHandler( const AppleEvent *event, AppleEvent *reply, SRefCon handlerRefcon) { OSStatus theErr; AEDescList theDesc; int tclErr = -1; Tcl_Interp *interp = (Tcl_Interp *) handlerRefcon; char errString[128]; /* * The do script event receives one parameter that should be data or a * file. */ theErr = AEGetParamDesc(event, keyDirectObject, typeWildCard, &theDesc); if (theErr != noErr) { sprintf(errString, "AEDoScriptHandler: GetParamDesc error %d", (int)theErr); theErr = AEPutParamPtr(reply, keyErrorString, typeChar, errString, strlen(errString)); } else if (MissedAnyParameters(event)) { /* * Return error if parameter is missing. */ sprintf(errString, "AEDoScriptHandler: extra parameters"); AEPutParamPtr(reply, keyErrorString, typeChar, errString, strlen(errString)); theErr = -1771; } else if (theDesc.descriptorType == (DescType) typeChar) { /* * We've had some data sent to us. Evaluate it. */ Tcl_DString encodedText; short i; Size size = AEGetDescDataSize(&theDesc); char *data = ckalloc(size + 1); AEGetDescData(&theDesc, data, size); data[size] = 0; for (i = 0; i < size; i++) { if (data[i] == '\r') { data[i] = '\n'; } } AEReplaceDescData(theDesc.descriptorType, data, size + 1, &theDesc); Tcl_ExternalToUtfDString(NULL, data, size, &encodedText); tclErr = Tcl_EvalEx(interp, Tcl_DStringValue(&encodedText), Tcl_DStringLength(&encodedText), TCL_EVAL_GLOBAL); Tcl_DStringFree(&encodedText); } else if (theDesc.descriptorType == (DescType) typeAlias) { /* * We've had a file sent to us. Source it. */ Boolean dummy; FSRef file; Size theSize = AEGetDescDataSize(&theDesc); AliasPtr alias = (AliasPtr) ckalloc(theSize); if (alias) { AEGetDescData(&theDesc, alias, theSize); theErr = FSResolveAlias(NULL, &alias, &file, &dummy); ckfree((char*)alias); } else { theErr = memFullErr; } if (theErr == noErr) { Tcl_DString scriptName; theErr = FSRefToDString(&file, &scriptName); if (theErr == noErr) { Tcl_EvalFile(interp, Tcl_DStringValue(&scriptName)); Tcl_DStringFree(&scriptName); } } else { sprintf(errString, "AEDoScriptHandler: file not found"); AEPutParamPtr(reply, keyErrorString, typeChar, errString, strlen(errString)); } } else { /* * Umm, don't recognize what we've got... */ sprintf(errString, "AEDoScriptHandler: invalid script type '%-4.4s'," " must be 'alis' or 'TEXT'", (char*) &theDesc.descriptorType); AEPutParamPtr(reply, keyErrorString, typeChar, errString, strlen(errString)); theErr = -1770; } /* * If we actually go to run Tcl code - put the result in the reply. */ if (tclErr >= 0) { int reslen; const char *result = Tcl_GetStringFromObj(Tcl_GetObjResult(interp), &reslen); if (tclErr == TCL_OK) { AEPutParamPtr(reply, keyDirectObject, typeChar, result, reslen); } else { AEPutParamPtr(reply, keyErrorString, typeChar, result, reslen); AEPutParamPtr(reply, keyErrorNumber, typeSInt32, (Ptr) &tclErr, sizeof(int)); } } AEDisposeDesc(&theDesc); return theErr; }