// GetDeviceUsbStrings Gets USB device strings for manufacturer name, product // name, and serial number if they are available. func GetDeviceUsbStrings(index int) (string, string, string, error) { var manufacturer *C.char manufacturer = (*C.char)(C.calloc(1, 256)) defer C.free(unsafe.Pointer(manufacturer)) var product *C.char product = (*C.char)(C.calloc(1, 256)) defer C.free(unsafe.Pointer(product)) var serial *C.char serial = (*C.char)(C.calloc(1, 256)) defer C.free(unsafe.Pointer(serial)) // Returns 0 on success retval := C.rtlsdr_get_device_usb_strings( C.uint32_t(index), manufacturer, product, serial) if retval != 0 { return "", "", "", fmt.Errorf("GetDeviceUsbStrings returned error value: %d", retval) } return C.GoString(manufacturer), C.GoString(product), C.GoString(serial), nil }
func _InitializeGlobalCStructures() { _MainArgs = (*C.struct__cef_main_args_t)(C.calloc(1, C.sizeof_struct__cef_main_args_t)) go_AddRef(unsafe.Pointer(_MainArgs)) _AppHandler = (*C.cef_app_t)(C.calloc(1, C.sizeof_cef_app_t)) go_AddRef(unsafe.Pointer(_AppHandler)) C.initialize_app_handler(_AppHandler) _ClientHandler = (*C.struct__cef_client_t)(C.calloc(1, C.sizeof_struct__cef_client_t)) go_AddRef(unsafe.Pointer(_ClientHandler)) C.initialize_client_handler(_ClientHandler) }
func init() { size := 100000 head := (*reflect.SliceHeader)(unsafe.Pointer(&myTable)) head.Data = uintptr(C.calloc(C.size_t(size), C.size_t(unsafe.Sizeof(MyTable{})))) head.Cap = size head.Len = size sizeofChildData := C.size_t(unsafe.Sizeof(ChildData{})) for i := 0; i < size; i++ { myTable[i].C01 = i myTable[i].Child = (*ChildData)(C.calloc(1, sizeofChildData)) myTable[i].Child.C01 = i } }
// Fetch performs a fetch operation. refspecs specifies which refspecs // to use for this fetch, use an empty list to use the refspecs from // the configuration; msg specifies what to use for the reflog // entries. Leave "" to use defaults. func (o *Remote) Fetch(refspecs []string, opts *FetchOptions, msg string) error { var cmsg *C.char = nil if msg != "" { cmsg = C.CString(msg) defer C.free(unsafe.Pointer(cmsg)) } crefspecs := C.git_strarray{} crefspecs.count = C.size_t(len(refspecs)) crefspecs.strings = makeCStringsFromStrings(refspecs) defer freeStrarray(&crefspecs) coptions := (*C.git_fetch_options)(C.calloc(1, C.size_t(unsafe.Sizeof(C.git_fetch_options{})))) defer C.free(unsafe.Pointer(coptions)) populateFetchOptions(coptions, opts) defer untrackCalbacksPayload(&coptions.callbacks) runtime.LockOSThread() defer runtime.UnlockOSThread() ret := C.git_remote_fetch(o.ptr, &crefspecs, coptions, cmsg) if ret < 0 { return MakeGitError(ret) } return nil }
func Findalldevs() (ifs []Interface, err string) { var buf *C.char buf = (*C.char)(C.calloc(ERRBUF_SIZE, 1)) var alldevsp *C.struct_pcap_if if -1 == C.pcap_findalldevs((**C.pcap_if_t)(unsafe.Pointer(&alldevsp)), buf) { ifs = nil err = C.GoString(buf) } else { dev := alldevsp var i uint32 for i = 0; dev != nil; dev = dev.next { i++ } ifs = make([]Interface, i) dev = alldevsp for j := uint32(0); dev != nil; dev = dev.next { var iface Interface iface.Name = C.GoString(dev.name) iface.Description = C.GoString(dev.description) // TODO: add more elements ifs[j] = iface j++ } C.pcap_freealldevs((*C.pcap_if_t)(alldevsp)) } C.free(unsafe.Pointer(buf)) return }
func Findalldevs() (ifs []Interface, err error) { var buf *C.char buf = (*C.char)(C.calloc(ERRBUF_SIZE, 1)) defer C.free(unsafe.Pointer(buf)) var alldevsp *C.pcap_if_t if -1 == C.pcap_findalldevs((**C.pcap_if_t)(&alldevsp), buf) { return nil, errors.New(C.GoString(buf)) } defer C.pcap_freealldevs((*C.pcap_if_t)(alldevsp)) dev := alldevsp var i uint32 for i = 0; dev != nil; dev = (*C.pcap_if_t)(dev.next) { i++ } ifs = make([]Interface, i) dev = alldevsp for j := uint32(0); dev != nil; dev = (*C.pcap_if_t)(dev.next) { var iface Interface iface.Name = C.GoString(dev.name) iface.Description = C.GoString(dev.description) iface.Addresses = findalladdresses(dev.addresses) // TODO: add more elements ifs[j] = iface j++ } return }
// allocStreamCallbackTimeInfoMemory allocates memory for type C.PaStreamCallbackTimeInfo in C. // The caller is responsible for freeing the this memory via C.free. func allocStreamCallbackTimeInfoMemory(n int) unsafe.Pointer { mem, err := C.calloc(C.size_t(n), (C.size_t)(sizeOfStreamCallbackTimeInfoValue)) if err != nil { panic("memory alloc error: " + err.Error()) } return mem }
func alloc(size int) (unsafe.Pointer, error) { p := C.calloc(C.size_t(size), C.size_t(1)) if p == nil { return nil, AllocationFailed } return p, nil }
// allocStreamParametersMemory allocates memory for type C.PaStreamParameters in C. // The caller is responsible for freeing the this memory via C.free. func allocStreamParametersMemory(n int) unsafe.Pointer { mem, err := C.calloc(C.size_t(n), (C.size_t)(sizeOfStreamParametersValue)) if err != nil { panic("memory alloc error: " + err.Error()) } return mem }
// OpenLive opens a device and returns a *Handle. // It takes as arguments the name of the device ("eth0"), the maximum size to // read for each packet (snaplen), whether to put the interface in promiscuous // mode, and a timeout. func OpenLive(device string, snaplen int32, promisc bool, timeout time.Duration) (handle *Handle, _ error) { var buf *C.char buf = (*C.char)(C.calloc(errorBufferSize, 1)) defer C.free(unsafe.Pointer(buf)) var pro C.int if promisc { pro = 1 } dev := C.CString(device) defer C.free(unsafe.Pointer(dev)) // This copies a bunch of the pcap_open_live implementation from pcap.c: cptr := C.pcap_create(dev, buf) if cptr == nil { return nil, errors.New(C.GoString(buf)) } var status C.int if status = C.pcap_set_snaplen(cptr, C.int(snaplen)); status < 0 { goto fail } else if status = C.pcap_set_promisc(cptr, pro); status < 0 { goto fail } else if status = C.pcap_set_timeout(cptr, C.int(timeout/time.Millisecond)); status < 0 { goto fail } return newHandle(cptr), nil fail: C.pcap_close(cptr) return nil, statusError(status) }
// allocPFloatMemory allocates memory for type *C.float in C. // The caller is responsible for freeing the this memory via C.free. func allocPFloatMemory(n int) unsafe.Pointer { mem, err := C.calloc(C.size_t(n), (C.size_t)(sizeOfPFloatValue)) if err != nil { panic("memory alloc error: " + err.Error()) } return mem }
// compileBPFFilter always returns an allocated _Ctype_struct_bpf_program // It is the callers responsibility to free the memory again, e.g. // // C.pcap_freecode(&bpf) // func (p *Handle) compileBPFFilter(expr string) (_Ctype_struct_bpf_program, error) { errorBuf := (*C.char)(C.calloc(errorBufferSize, 1)) defer C.free(unsafe.Pointer(errorBuf)) var netp uint32 var maskp uint32 // Only do the lookup on network interfaces. // No device indicates we're handling a pcap file. if len(p.device) > 0 { dev := C.CString(p.device) defer C.free(unsafe.Pointer(dev)) if -1 == C.pcap_lookupnet( dev, (*C.bpf_u_int32)(unsafe.Pointer(&netp)), (*C.bpf_u_int32)(unsafe.Pointer(&maskp)), errorBuf, ) { // We can't lookup the network, but that could be because the interface // doesn't have an IPv4. } } var bpf _Ctype_struct_bpf_program cexpr := C.CString(expr) defer C.free(unsafe.Pointer(cexpr)) if -1 == C.pcap_compile(p.cptr, &bpf, cexpr, 1, C.bpf_u_int32(maskp)) { return bpf, p.Error() } return bpf, nil }
func CreateBrowser(hwnd unsafe.Pointer, browserSettings BrowserSettings, url string) *Browser { log.Debug("CreateBrowser, url:", url) // Initialize cef_window_info_t structure. var windowInfo *C.cef_window_info_t windowInfo = (*C.cef_window_info_t)( C.calloc(1, C.sizeof_cef_window_info_t)) FillWindowInfo(windowInfo, hwnd) // Do not create the browser synchronously using the // cef_browser_host_create_browser_sync() function, as // it is unreliable. Instead obtain browser object in // life_span_handler::on_after_created. In that callback // keep CEF browser objects in a global map (cef window // handle -> cef browser) and introduce // a GetBrowserByWindowHandle() function. This function // will first guess the CEF window handle using for example // WinAPI functions and then search the global map of cef // browser objects. C.cef_browser_host_create_browser(windowInfo, _ClientHandler, CEFString(url), browserSettings.ToCStruct(), nil) b, err := globalLifespanHandler.RegisterAndWaitForBrowser() if err != nil { log.Error("ERROR:", err) panic("Failed to create a browser") } b.RenderHandler = &DefaultRenderHandler{b} browsers[b.Id] = b return b }
func CEFString(original string) (final *C.cef_string_t) { final = (*C.cef_string_t)(C.calloc(1, C.sizeof_cef_string_t)) charString := C.CString(original) defer C.free(unsafe.Pointer(charString)) C.cef_string_from_utf8(charString, C.strlen(charString), final) return final }
func Clone(url string, path string, options *CloneOptions) (*Repository, error) { repo := new(Repository) curl := C.CString(url) defer C.free(unsafe.Pointer(curl)) cpath := C.CString(path) defer C.free(unsafe.Pointer(cpath)) copts := (*C.git_clone_options)(C.calloc(1, C.size_t(unsafe.Sizeof(C.git_clone_options{})))) populateCloneOptions(copts, options) defer freeCloneOptions(copts) if len(options.CheckoutBranch) != 0 { copts.checkout_branch = C.CString(options.CheckoutBranch) } runtime.LockOSThread() defer runtime.UnlockOSThread() ret := C.git_clone(&repo.ptr, curl, cpath, copts) if ret < 0 { return nil, MakeGitError(ret) } runtime.SetFinalizer(repo, (*Repository).Free) return repo, nil }
//FromImage copies an image into a surface. // //The created image surface will have the same size as img, //the optimal stride for img's width, and FormatARGB32. // //Originally cairo_image_surface_create_for_data and //cairo_format_stride_for_width. func FromImage(img image.Image) (ImageSurface, error) { f := FormatARGB32.c() b := img.Bounds() w, h := b.Dx(), b.Dy() s := int(C.cairo_format_stride_for_width(f, C.int(w))) n := s * h data := (*C.uchar)(C.calloc(C.size_t(uintptr(n)), 1)) pseudoslice := (*[1 << 30]C.uchar)(unsafe.Pointer(data))[:n:n] i := 0 for y := b.Min.Y; y < b.Max.Y; y++ { for x := b.Min.X; x < b.Max.X; x++ { r, g, b, a := img.At(x, y).RGBA() pseudoslice[i+oA] = C.uchar(a) pseudoslice[i+oR] = C.uchar(r) pseudoslice[i+oG] = C.uchar(g) pseudoslice[i+oB] = C.uchar(b) i += 4 } i += 4 * (s/4 - w) } is := C.cairo_image_surface_create_for_data(data, f, C.int(w), C.int(h), C.int(s)) C.cairo_surface_set_user_data(is, imgKey, unsafe.Pointer(data), free) return newImg(is, FormatARGB32, w, h, s) }
func GetDeviceList() ([]string, error) { var ifs []string var buf *C.char buf = (*C.char)(C.calloc(ERRBUF_SIZE, 1)) defer C.free(unsafe.Pointer(buf)) var devs *C.pcap_if_t if C.pcap_findalldevs((**C.pcap_if_t)(&devs), buf) == -1 { return nil, errors.New(C.GoString(buf)) } defer C.pcap_freealldevs((*C.pcap_if_t)(devs)) dev := devs var devcount uint32 // figure out how many devices we have available for devcount = 0; dev != nil; dev = (*C.pcap_if_t)(dev.next) { devcount++ } dev = devs for i := uint32(0); dev != nil; dev = (*C.pcap_if_t)(dev.next) { ifs = append(ifs, C.GoString(dev.name)) i++ } return ifs, nil }
//Opens a pcap handle func OpenActiveDevice(dev string, snaplen uint32, promisc bool, timeout_ms uint32, filter string) (*Pcap, error) { var cbuf *C.char cbuf = (*C.char)(C.calloc(ERRBUF_SIZE, 1)) handle := new(Pcap) var cpromisc int32 = 0 if promisc { cpromisc = 1 } cdev := C.CString(dev) defer C.free(unsafe.Pointer(cdev)) handle.cptr = C.pcap_open_live(cdev, C.int(snaplen), C.int(cpromisc), C.int(timeout_ms), cbuf) if handle.cptr == nil { return nil, errors.New(C.GoString(cbuf)) } x := C.pcap_datalink(handle.cptr) handle.linkLayer = uint16(x) C.free(unsafe.Pointer(cbuf)) if filter != "" { ret := int32(C.pcap_set_filter(handle.cptr, cdev, C.CString(filter), cbuf, ERRBUF_SIZE-1)) if ret != 0 { C.free(unsafe.Pointer(handle.cptr)) return nil, errors.New(C.GoString(cbuf)) } } return handle, nil }
// allocOggSyncStateMemory allocates memory for type C.ogg_sync_state in C. // The caller is responsible for freeing the this memory via C.free. func allocOggSyncStateMemory(n int) unsafe.Pointer { mem, err := C.calloc(C.size_t(n), (C.size_t)(sizeOfOggSyncStateValue)) if err != nil { panic("memory alloc error: " + err.Error()) } return mem }
// OpenLive opens a device and returns a *Handle. // It takes as arguments the name of the device ("eth0"), the maximum size to // read for each packet (snaplen), whether to put the interface in promiscuous // mode, and a timeout. // // See the package documentation for important details regarding 'timeout'. func OpenLive(device string, snaplen int32, promisc bool, timeout time.Duration) (handle *Handle, _ error) { buf := (*C.char)(C.calloc(errorBufferSize, 1)) defer C.free(unsafe.Pointer(buf)) var pro C.int if promisc { pro = 1 } p := &Handle{} p.blockForever = timeout < 0 p.device = device ifc, err := net.InterfaceByName(device) if err != nil { // The device wasn't found in the OS, but could be "any" // Set index to 0 p.deviceIndex = 0 } else { p.deviceIndex = ifc.Index } dev := C.CString(device) defer C.free(unsafe.Pointer(dev)) p.cptr = C.pcap_open_live(dev, C.int(snaplen), pro, timeoutMillis(timeout), buf) if p.cptr == nil { return nil, errors.New(C.GoString(buf)) } return p, nil }
func (d *VirDomain) BlockStatsFlags(disk string, params *VirTypedParameters, nParams int, flags uint32) (int, error) { var cParams C.virTypedParameterPtr cDisk := C.CString(disk) defer C.free(unsafe.Pointer(cDisk)) cParamsLen := C.int(nParams) if params != nil && nParams > 0 { cParams = (C.virTypedParameterPtr)(C.calloc(C.size_t(nParams), C.size_t(unsafe.Sizeof(C.struct__virTypedParameter{})))) defer C.virTypedParamsFree(cParams, cParamsLen) } else { cParams = nil } result := int(C.virDomainBlockStatsFlags(d.ptr, cDisk, (C.virTypedParameterPtr)(cParams), &cParamsLen, C.uint(flags))) if result == -1 { return result, GetLastError() } if cParamsLen > 0 && params != nil { params.loadFromCPtr(cParams, nParams) } return int(cParamsLen), nil }
func Clone(url string, path string, options *CloneOptions) (*Repository, error) { curl := C.CString(url) defer C.free(unsafe.Pointer(curl)) cpath := C.CString(path) defer C.free(unsafe.Pointer(cpath)) copts := (*C.git_clone_options)(C.calloc(1, C.size_t(unsafe.Sizeof(C.git_clone_options{})))) populateCloneOptions(copts, options) defer freeCloneOptions(copts) if len(options.CheckoutBranch) != 0 { copts.checkout_branch = C.CString(options.CheckoutBranch) } runtime.LockOSThread() defer runtime.UnlockOSThread() var ptr *C.git_repository ret := C.git_clone(&ptr, curl, cpath, copts) freeCheckoutOpts(&copts.checkout_opts) if ret < 0 { return nil, MakeGitError(ret) } return newRepositoryFromC(ptr), nil }
func (d *VirDomain) GetCPUStats(params *VirTypedParameters, nParams int, startCpu int, nCpus uint32, flags uint32) (int, error) { var cParams C.virTypedParameterPtr var cParamsLen int cParamsLen = int(nCpus) * nParams if params != nil && cParamsLen > 0 { cParams = (C.virTypedParameterPtr)(C.calloc(C.size_t(cParamsLen), C.size_t(unsafe.Sizeof(C.struct__virTypedParameter{})))) defer C.virTypedParamsFree(cParams, C.int(cParamsLen)) } else { cParamsLen = 0 cParams = nil } result := int(C.virDomainGetCPUStats(d.ptr, (C.virTypedParameterPtr)(cParams), C.uint(nParams), C.int(startCpu), C.uint(nCpus), C.uint(flags))) if result == -1 { return result, GetLastError() } if cParamsLen > 0 { params.loadFromCPtr(cParams, cParamsLen) } return result, nil }
// aiString allocates and returns a new aiString, it must later be free'd. func aiString(g string) *C.struct_aiString { s := (*C.struct_aiString)(C.calloc(1, C.size_t(len(g)))) s.length = C.size_t(len(g)) for i, c := range g { s.data[i] = C.char(c) } return s }
func OpenOffline(filename string) (*Pcap, error) { var pcap Pcap var errbuf *C.char = (*C.char)(C.calloc(1024, 1)) pcap.pcap = C.pcap_open_offline(C.CString(filename), errbuf) if pcap.pcap == nil { return nil, errors.New(C.GoString(errbuf)) } return &pcap, nil }
func (b BrowserSettings) toC() *C.struct__cef_browser_settings_t { var cefBrowserSettings *C.struct__cef_browser_settings_t cefBrowserSettings = (*C.struct__cef_browser_settings_t)( C.calloc(1, C.sizeof_struct__cef_browser_settings_t)) cefBrowserSettings.size = C.sizeof_struct__cef_browser_settings_t go_AddRef(unsafe.Pointer(cefBrowserSettings)) if b.StandardFontFamily != "" { toCefStringCopy(b.StandardFontFamily, &cefBrowserSettings.standard_font_family) } if b.FixedFontFamily != "" { toCefStringCopy(b.FixedFontFamily, &cefBrowserSettings.fixed_font_family) } if b.SerifFontFamily != "" { toCefStringCopy(b.SerifFontFamily, &cefBrowserSettings.serif_font_family) } if b.SansSerifFontFamily != "" { toCefStringCopy(b.SansSerifFontFamily, &cefBrowserSettings.sans_serif_font_family) } if b.CursiveFontFamily != "" { toCefStringCopy(b.CursiveFontFamily, &cefBrowserSettings.cursive_font_family) } if b.FantasyFontFamily != "" { toCefStringCopy(b.FantasyFontFamily, &cefBrowserSettings.fantasy_font_family) } cefBrowserSettings.default_font_size = C.int(b.DefaultFontSize) cefBrowserSettings.default_fixed_font_size = C.int(b.DefaultFixedFontSize) cefBrowserSettings.minimum_font_size = C.int(b.MinimumFontSize) cefBrowserSettings.minimum_logical_font_size = C.int(b.MinimumLogicalFontSize) if b.DefaultEncoding != "" { toCefStringCopy(b.DefaultEncoding, &cefBrowserSettings.default_encoding) } cefBrowserSettings.remote_fonts = C.cef_state_t(b.RemoteFonts) cefBrowserSettings.javascript = C.cef_state_t(b.Javascript) cefBrowserSettings.javascript_open_windows = C.cef_state_t(b.JavascriptOpenWindows) cefBrowserSettings.javascript_close_windows = C.cef_state_t(b.JavascriptCloseWindows) cefBrowserSettings.javascript_access_clipboard = C.cef_state_t(b.JavascriptAccessClipboard) cefBrowserSettings.javascript_dom_paste = C.cef_state_t(b.JavascriptDomPaste) cefBrowserSettings.caret_browsing = C.cef_state_t(b.CaretBrowsing) cefBrowserSettings.java = C.cef_state_t(b.Java) cefBrowserSettings.plugins = C.cef_state_t(b.Plugins) cefBrowserSettings.universal_access_from_file_urls = C.cef_state_t(b.UniversalAccessFromFileUrls) cefBrowserSettings.file_access_from_file_urls = C.cef_state_t(b.FileAccessFromFileUrls) cefBrowserSettings.web_security = C.cef_state_t(b.WebSecurity) cefBrowserSettings.image_loading = C.cef_state_t(b.ImageLoading) cefBrowserSettings.image_shrink_standalone_to_fit = C.cef_state_t(b.ImageShrinkStandaloneToFit) cefBrowserSettings.text_area_resize = C.cef_state_t(b.TextAreaResize) cefBrowserSettings.tab_to_links = C.cef_state_t(b.TabToLinks) cefBrowserSettings.local_storage = C.cef_state_t(b.LocalStorage) cefBrowserSettings.databases = C.cef_state_t(b.Databases) cefBrowserSettings.application_cache = C.cef_state_t(b.ApplicationCache) cefBrowserSettings.webgl = C.cef_state_t(b.Webgl) cefBrowserSettings.background_color = C.cef_color_t(b.BackgroundColor) return cefBrowserSettings }
func NewLifeSpanHandlerT(life LifeSpanHandler) LifeSpanHandlerT { var handler LifeSpanHandlerT handler.CStruct = (*C.struct__cef_life_span_handler_t)( C.calloc(1, C.sizeof_struct__cef_life_span_handler_t)) C.initialize_life_span_handler(handler.CStruct) go_AddRef(unsafe.Pointer(handler.CStruct)) Logger.Infof("_LifespanHandler: %x\n", unsafe.Pointer(handler.CStruct)) lifeSpanHandlerMap[unsafe.Pointer(handler.CStruct)] = life return handler }
func NewClientHandlerT(handler ClientHandler) ClientHandlerT { var c ClientHandlerT c.CStruct = (*C.struct__cef_client_t)( C.calloc(1, C.sizeof_struct__cef_client_t)) C.initialize_client_handler(c.CStruct) go_AddRef(unsafe.Pointer(c.CStruct)) Logger.Infof("_ClientHandler: %x", unsafe.Pointer(c.CStruct)) clientHandlerMap[unsafe.Pointer(c.CStruct)] = handler return c }
func NewDisplayHandlerT(display DisplayHandler) DisplayHandlerT { var handler DisplayHandlerT handler.CStruct = (*C.struct__cef_display_handler_t)( C.calloc(1, C.sizeof_struct__cef_display_handler_t)) C.initialize_display_handler(handler.CStruct) go_AddRef(unsafe.Pointer(handler.CStruct)) Logger.Infof("_DisplayHandler: %x", unsafe.Pointer(handler.CStruct)) displayHandlerMap[unsafe.Pointer(handler.CStruct)] = display return handler }
func NewBrowserProcessHandlerT(handler BrowserProcessHandler) BrowserProcessHandlerT { var b BrowserProcessHandlerT b.CStruct = (*C.struct__cef_browser_process_handler_t)( C.calloc(1, C.sizeof_struct__cef_browser_process_handler_t)) C.intialize_cef_browser_process_handler(b.CStruct) unsafeIt := unsafe.Pointer(b.CStruct) go_AddRef(unsafeIt) browserProcessHandlerMap[unsafeIt] = handler return b }