// FindGenericPassword finds the first generic password item which matches the // attributes you provide. Most attributes are optional; you should pass only as // many as you need to narrow the search sufficiently for your application's // intended use. func FindGenericPassword(service, account string) (string, error) { if service == "" || account == "" { return "", errors.New("service and account are required") } cService := C.CString(service) defer C.free(unsafe.Pointer(cService)) cAccount := C.CString(account) defer C.free(unsafe.Pointer(cAccount)) cPasswordLen := C.UInt32(0) cPassword := unsafe.Pointer(nil) errCode := C.SecKeychainFindGenericPassword( nil, // default keychain C.UInt32(C.strlen(cService)), cService, C.UInt32(C.strlen(cAccount)), cAccount, &cPasswordLen, &cPassword, nil, ) if err := newKeychainError(errCode); err != nil { return "", err } defer C.SecKeychainItemFreeContent(nil, cPassword) return C.GoStringN((*C.char)(cPassword), C.int(cPasswordLen)), nil }
//export Go_callback func Go_callback(playerInfo unsafe.Pointer, queue C.AudioQueueRef, buffer C.AudioQueueBufferRef) { audioData := ((*[1 << 30]byte)(unsafe.Pointer((*buffer).mAudioData)))[:buffer.mAudioDataBytesCapacity] pktDescs := (*[1 << 30]C.AudioStreamPacketDescription)(unsafe.Pointer(buffer.mPacketDescriptions)) npackets := 0 upto := audioData if peeked != nil { copy(upto, peeked) pktDescs[npackets].mStartOffset = C.SInt64(int64(len(audioData) - len(upto))) pktDescs[npackets].mDataByteSize = C.UInt32(int32(len(peeked))) upto = upto[len(peeked):] npackets++ } peeked = <-packetsin for len(peeked) <= len(upto) { copy(upto, peeked) pktDescs[npackets].mStartOffset = C.SInt64(int64(len(audioData) - len(upto))) pktDescs[npackets].mDataByteSize = C.UInt32(int32(len(peeked))) pktDescs[npackets].mVariableFramesInPacket = C.UInt32(int32(0)) upto = upto[len(peeked):] npackets++ peeked = <-packetsin } buffer.mAudioDataByteSize = C.UInt32(len(audioData) - len(upto)) buffer.mPacketDescriptionCount = C.UInt32(npackets) C.AudioQueueEnqueueBuffer(queue, buffer, 0, (*C.AudioStreamPacketDescription)(nil)) }
func findGenericPasswordItem(attributes *GenericPasswordAttributes) (itemRef C.SecKeychainItemRef, err error) { if err = attributes.CheckValidity(); err != nil { return } serviceName := C.CString(attributes.ServiceName) defer C.free(unsafe.Pointer(serviceName)) accountName := C.CString(attributes.AccountName) defer C.free(unsafe.Pointer(accountName)) errCode := C.SecKeychainFindGenericPassword( nil, // default keychain C.UInt32(len(attributes.ServiceName)), serviceName, C.UInt32(len(attributes.AccountName)), accountName, nil, nil, &itemRef, ) err = newKeychainError(errCode) return }
// FindGenericPassword finds a generic password with the given // attributes in the default keychain and returns the password field // if found. If not found, an error is returned. func FindGenericPassword(attributes *GenericPasswordAttributes) ([]byte, error) { if err := attributes.CheckValidity(); err != nil { return nil, err } serviceName := C.CString(attributes.ServiceName) defer C.free(unsafe.Pointer(serviceName)) accountName := C.CString(attributes.AccountName) defer C.free(unsafe.Pointer(accountName)) var passwordLength C.UInt32 var password unsafe.Pointer errCode := C.SecKeychainFindGenericPassword( nil, // default keychain C.UInt32(len(attributes.ServiceName)), serviceName, C.UInt32(len(attributes.AccountName)), accountName, &passwordLength, &password, nil, ) if err := newKeychainError(errCode); err != nil { return nil, err } defer C.SecKeychainItemFreeContent(nil, password) return C.GoBytes(password, C.int(passwordLength)), nil }
// NewOutputWAVEFile opens a CoreAudio WAVE file suitable for output to target. // // rate is the sample rate, numchan is the number of channels in the output, and numbits is the number of bits per channel. // NOTE: In order to prevent unnecessary copying, the calls to target use buffers that are owned by CoreAudio. This means that // slices should not be made of buffers that will outlive the call to the WriteAt method. func NewOutputWAVEFile(target ReadWriterAt, rate float64, numchan int, numbits int) (*AudioFile, error) { bpf := C.UInt32(numbits * numchan / 8) asbd := C.AudioStreamBasicDescription{ mSampleRate: C.Float64(rate), mFormatID: C.kAudioFormatLinearPCM, mFormatFlags: C.kAudioFormatFlagIsSignedInteger | C.kAudioFormatFlagIsPacked, mBytesPerPacket: bpf, mFramesPerPacket: 1, mBytesPerFrame: bpf, mChannelsPerFrame: C.UInt32(numchan), mBitsPerChannel: C.UInt32(numbits), } return newOutputFile(target, &asbd, C.kAudioFileWAVEType) }
func (*keychainOSX) DeletePassword(service, account string) error { // Validate input serviceValid := isValidNonNullUTF8(service) accountValid := isValidNonNullUTF8(account) if !(serviceValid && accountValid) { return ErrInvalidValue } // Convert values to C strings serviceCStr := C.CString(service) defer C.free(unsafe.Pointer(serviceCStr)) accountCStr := C.CString(account) defer C.free(unsafe.Pointer(accountCStr)) // Grab the item var item C.SecKeychainItemRef status := C.SecKeychainFindGenericPassword( nil, C.UInt32(len(service)), serviceCStr, C.UInt32(len(account)), accountCStr, nil, nil, &item, ) // Check for errors if status != C.errSecSuccess { return ErrNotFound } // Delete the item status = C.SecKeychainItemDelete(item) // Free the item C.CFRelease(C.CFTypeRef(item)) // Check for errors if status != C.errSecSuccess { return ErrUnknown } // All done return nil }
// FindInternetPassword function finds the first Internet password item which // matches the attributes you provide. Most attributes are optional; you should // pass only as many as you need to narrow the search sufficiently for your // application's intended use. SecKeychainFindInternetPassword optionally // returns a reference to the found item. func FindInternetPassword(server, domain, account, path string, port uint16, protocol ProtocolType, authType AuthenticationType) (string, error) { if server == "" || account == "" { return "", errors.New("server and account are required") } cServer := C.CString(server) defer C.free(unsafe.Pointer(cServer)) cDomain := C.CString(domain) defer C.free(unsafe.Pointer(cDomain)) cAccount := C.CString(account) defer C.free(unsafe.Pointer(cAccount)) cPath := C.CString(path) defer C.free(unsafe.Pointer(cPath)) cPasswordLen := C.UInt32(0) cPassword := unsafe.Pointer(nil) errCode := C.SecKeychainFindInternetPassword( nil, // default keychain, C.UInt32(C.strlen(cServer)), cServer, C.UInt32(C.strlen(cDomain)), cDomain, C.UInt32(C.strlen(cAccount)), cAccount, C.UInt32(C.strlen(cPath)), cPath, C.UInt16(port), C.SecProtocolType(protocol), C.SecAuthenticationType(authType), &cPasswordLen, &cPassword, nil, ) if err := newKeychainError(errCode); err != nil { return "", err } defer C.SecKeychainItemFreeContent(nil, cPassword) return C.GoStringN((*C.char)(cPassword), C.int(cPasswordLen)), nil }
// NewOutputAACFile opens a CoreAudio MP4 encapsulated AAC file suitable for output to target. // // rate is the sample rate, numchan is the number of channels in the output, and numbits is the number of bits per channel. // NOTE: In order to prevent unnecessary copying, the calls to target use buffers that are owned by CoreAudio. This means that // slices should not be made of buffers that will outlive the call to the WriteAt method. func NewOutputAACFile(target ReadWriterAt, rate float64, numchan int, numbits int) (*AudioFile, error) { asbd := C.AudioStreamBasicDescription{ mSampleRate: C.Float64(rate), mFormatID: C.kAudioFormatMPEG4AAC, mFormatFlags: C.kMPEG4Object_AAC_Main, mChannelsPerFrame: C.UInt32(numchan), mFramesPerPacket: 1024, } return newOutputFile(target, &asbd, C.kAudioFileM4AType) }
func (*keychainOSX) GetPassword(service, account string) (string, error) { // Validate input serviceValid := isValidNonNullUTF8(service) accountValid := isValidNonNullUTF8(account) if !(serviceValid && accountValid) { return "", ErrInvalidValue } // Convert values to C strings serviceCStr := C.CString(service) defer C.free(unsafe.Pointer(serviceCStr)) accountCStr := C.CString(account) defer C.free(unsafe.Pointer(accountCStr)) // Look for a match var passwordData unsafe.Pointer var passwordDataLength C.UInt32 status := C.SecKeychainFindGenericPassword( nil, C.UInt32(len(service)), serviceCStr, C.UInt32(len(account)), accountCStr, &passwordDataLength, &passwordData, nil, ) // Check for errors if status != C.errSecSuccess { return "", ErrNotFound } // Create the result result := C.GoStringN((*C.char)(passwordData), C.int(passwordDataLength)) // Cleanup the temporary buffer C.SecKeychainItemFreeContent(nil, passwordData) // All done return result, nil }
// The returned SecKeychainRef, if non-nil, must be released via CFRelease. func createKeychain(path string, promptUser bool, password string) (C.SecKeychainRef, error) { pathName := C.CString(path) defer C.free(unsafe.Pointer(pathName)) var kref C.SecKeychainRef var errCode C.OSStatus if promptUser { errCode = C.SecKeychainCreate(pathName, C.UInt32(0), nil, C.Boolean(1), nil, &kref) } else { passwordRef := C.CString(password) defer C.free(unsafe.Pointer(passwordRef)) errCode = C.SecKeychainCreate(pathName, C.UInt32(len(password)), unsafe.Pointer(passwordRef), C.Boolean(0), nil, &kref) } if err := newKeychainError(errCode); err != nil { return nil, err } return kref, nil }
func (*keychainOSX) AddPassword(service, account, password string) error { // Validate input serviceValid := isValidNonNullUTF8(service) accountValid := isValidNonNullUTF8(account) passwordValid := isValidNonNullUTF8(password) if !(serviceValid && accountValid && passwordValid) { return ErrInvalidValue } // Convert values to C strings serviceCStr := C.CString(service) defer C.free(unsafe.Pointer(serviceCStr)) accountCStr := C.CString(account) defer C.free(unsafe.Pointer(accountCStr)) passwordBlob := unsafe.Pointer(C.CString(password)) defer C.free(passwordBlob) // Try to add the password status := C.SecKeychainAddGenericPassword( nil, C.UInt32(len(service)), serviceCStr, C.UInt32(len(account)), accountCStr, C.UInt32(len(password)), passwordBlob, nil, ) // Check for errors if status != C.errSecSuccess { return ErrUnknown } // All done return nil }
func newKeychain(path, password string, promptUser bool) (Keychain, error) { pathRef := C.CString(path) defer C.free(unsafe.Pointer(pathRef)) var errCode C.OSStatus var kref C.SecKeychainRef if promptUser { errCode = C.SecKeychainCreate(pathRef, C.UInt32(0), nil, C.Boolean(1), nil, &kref) } else { passwordRef := C.CString(password) defer C.free(unsafe.Pointer(passwordRef)) errCode = C.SecKeychainCreate(pathRef, C.UInt32(len(password)), unsafe.Pointer(passwordRef), C.Boolean(0), nil, &kref) } // TODO: Without passing in kref I get 'One or more parameters passed to the function were not valid (-50)' defer Release(C.CFTypeRef(kref)) if err := checkError(errCode); err != nil { return Keychain{}, err } return Keychain{path}, nil }
//export go_audiofile_readproc func go_audiofile_readproc(data unsafe.Pointer, inPosition C.SInt64, requestCount C.UInt32, buffer unsafe.Pointer, actualCount *C.UInt32) C.OSStatus { af := (*AudioFile)(data) length := int(requestCount) hdr := reflect.SliceHeader{ Data: uintptr(buffer), Len: length, Cap: length, } bslice := *(*[]byte)(unsafe.Pointer(&hdr)) n, err := af.target.ReadAt(bslice, int64(inPosition)) *actualCount = C.UInt32(n) if err == io.EOF { return C.kAudioFileEndOfFileError } else if err != nil { return C.kAudioFileUnspecifiedError } return C.OSStatus(0) }
// The byte array returned by this function is unsafe. // It is mapped to a C buffer, which can be erased/overwritten // when Close, ExtractUnsafe,or any function that calls ExtractUnsafe is called. // Unless your data is too big, and you are prepared to // avoid using another call to mentioned functions during the life-time of // the returned array, do not use this function. func (z *SevenZip) ExtractUnsafe(i int) ([]byte, error) { var outSizeProcessed, offset C.size_t if C.SzArEx_Extract( &z.db, &z.lookStream.s, C.UInt32(i), &z.blockIndex, &z.outBuffer, &z.outBufferSize, &offset, &outSizeProcessed, &z.allocImp, &z.allocTempImp) != C._SZ_OK { return []byte{}, errors.New("sevenzip: extract failed") } cbuf := C._ByteArrayIndex(z.outBuffer, offset) clen := int(outSizeProcessed) file := (*[1 << 30]byte)(unsafe.Pointer(cbuf))[:clen] hdr := (*reflect.SliceHeader)(unsafe.Pointer(&file)) hdr.Cap = clen hdr.Len = clen return file, nil }
//export go_audiofile_writeproc func go_audiofile_writeproc(data unsafe.Pointer, inPosition C.SInt64, requestCount C.UInt32, buffer unsafe.Pointer, actualCount *C.UInt32) C.OSStatus { af := (*AudioFile)(data) length := int(requestCount) hdr := reflect.SliceHeader{ Data: uintptr(buffer), Len: length, Cap: length, } bslice := *(*[]byte)(unsafe.Pointer(&hdr)) npos := int64(inPosition) n, err := af.target.WriteAt(bslice, npos) *actualCount = C.UInt32(n) if err != nil { return C.kAudioFileUnspecifiedError } if npos+int64(n) > af.fileSize { af.fileSize = npos + int64(n) } return C.OSStatus(0) }