Exemplo n.º 1
0
// MakeOIDBytes makes an OID encapsulating a byte slice. Note that it does not
// duplicate the data, but rather it points to it directly.
func (lib *Lib) MakeOIDBytes(data []byte) (*OID, error) {
	oid := lib.NewOID()

	s := C.malloc(C.gss_OID_size) // s for struct
	if s == nil {
		return nil, ErrMallocFailed
	}
	C.memset(s, 0, C.gss_OID_size)

	l := C.size_t(len(data))
	e := C.malloc(l) // c for contents
	if e == nil {
		return nil, ErrMallocFailed
	}
	C.memmove(e, (unsafe.Pointer)(&data[0]), l)

	oid.C_gss_OID = C.gss_OID(s)
	oid.alloc = allocMalloc

	// because of the alignment issues I can't access o.oid's fields from go,
	// so invoking a C function to do the same as:
	// oid.C_gss_OID.length = l
	// oid.C_gss_OID.elements = c
	C.helper_gss_OID_desc_set_elements(oid.C_gss_OID, C.OM_uint32(l), e)

	return oid, nil
}
Exemplo n.º 2
0
// NewNode returns new Node with a given Logger.
func NewNodeConfig(logfile string, level string, cfg *NodeConfig) (node *Node, err error) {
	clevel := C.CString(level)
	defer C.free(unsafe.Pointer(clevel))

	clogfile := C.CString(logfile)
	defer C.free(unsafe.Pointer(clogfile))

	var dcfg C.struct_dnet_config
	C.memset(unsafe.Pointer(&dcfg), 0, C.sizeof_struct_dnet_config)

	dcfg.io_thread_num = C.int(cfg.IOThreadNum)
	dcfg.nonblocking_io_thread_num = C.int(cfg.NonBlockingIOThreadNum)
	dcfg.net_thread_num = C.int(cfg.NetThreadNum)
	dcfg.wait_timeout = C.long(cfg.WaitTimeout)
	dcfg.check_timeout = C.long(cfg.CheckTimeout)
	dcfg.flags = C.int(cfg.Flags)
	dcfg.stall_count = C.long(cfg.StallCount)

	cnode := C.new_node_config(clogfile, clevel, &dcfg)
	if cnode == nil {
		err = fmt.Errorf("could not create node, please check stderr output")
		return
	}
	node = &Node{cnode}
	return
}
Exemplo n.º 3
0
func newGeohash(geohash string) *C.CCGeohashStruct {
	cgeohash := C.CString(geohash)
	cg := &C.CCGeohashStruct{}
	C.memset(unsafe.Pointer(&(cg.hash[0])), '0', MaxGeohashLength) // move this on C side
	C.strncpy(&(cg.hash[0]), cgeohash, C.size_t(len(geohash)))
	C.init_from_hash(cg)
	return cg
}
Exemplo n.º 4
0
// getPPD gets the filename of the PPD for a printer by calling
// C.cupsGetPPD3. If the PPD hasn't changed since the time indicated
// by modtime, then the returned filename is a nil pointer.
//
// Note that modtime is a pointer whose value is changed by this
// function.
//
// The caller is responsible to C.free the returned *C.char filename
// if the returned filename is not nil.
func (cc *cupsCore) getPPD(printername *C.char, modtime *C.time_t) (*C.char, error) {
	bufsize := C.size_t(filePathMaxLength)
	buffer := (*C.char)(C.malloc(bufsize))
	if buffer == nil {
		return nil, errors.New("Failed to malloc; out of memory?")
	}
	C.memset(unsafe.Pointer(buffer), 0, bufsize)

	var http *C.http_t
	if !cc.hostIsLocal {
		// Don't need a connection or corresponding semaphore if the PPD
		// is on the local filesystem.
		// Still need OS thread lock; see else.
		var err error
		http, err = cc.connect()
		if err != nil {
			return nil, err
		}
		defer cc.disconnect(http)

	} else {
		// Lock the OS thread so that thread-local storage is available to
		// cupsLastError() and cupsLastErrorString().
		runtime.LockOSThread()
		defer runtime.UnlockOSThread()
	}

	httpStatus := C.cupsGetPPD3(http, printername, modtime, buffer, bufsize)

	switch httpStatus {
	case C.HTTP_STATUS_NOT_MODIFIED:
		// Cache hit.
		if len(C.GoString(buffer)) > 0 {
			os.Remove(C.GoString(buffer))
		}
		C.free(unsafe.Pointer(buffer))
		return nil, nil

	case C.HTTP_STATUS_OK:
		// Cache miss.
		return buffer, nil

	default:
		if len(C.GoString(buffer)) > 0 {
			os.Remove(C.GoString(buffer))
		}
		C.free(unsafe.Pointer(buffer))
		cupsLastError := C.cupsLastError()
		if cupsLastError != C.IPP_STATUS_OK {
			return nil, fmt.Errorf("Failed to call cupsGetPPD3(): %d %s",
				int(cupsLastError), C.GoString(C.cupsLastErrorString()))
		}

		return nil, fmt.Errorf("Failed to call cupsGetPPD3(); HTTP status: %d", int(httpStatus))
	}
}
Exemplo n.º 5
0
func strError(code C.int) string {
	size := C.size_t(256)
	buf := (*C.char)(C.malloc(size))
	defer C.free(unsafe.Pointer(buf))
	C.memset(unsafe.Pointer(buf), 0, size)
	if C.av_strerror(code, buf, size-1) == 0 {
		return C.GoString(buf)
	}
	return "Unknown error"
}
Exemplo n.º 6
0
Arquivo: util.go Projeto: duckbrain/ui
//export uimalloc
func uimalloc(n C.size_t) unsafe.Pointer {
	if n > C.size_t(len(uimallocBytes)) {
		// TODO round n up to a multiple of a power of 2?
		// for instance 0x1234 bytes -> 0x1800 bytes
		uimallocBytes = make([]byte, n)
	}
	p := C.malloc(n)
	if p == nil {
		panic("out of memory in uimalloc()")
	}
	C.memset(p, 0, n)
	return p
}
Exemplo n.º 7
0
// MakeBuffer returns a Buffer with an empty malloc-ed gss_buffer_desc in it.
// The return value must be .Release()-ed
func (lib *Lib) MakeBuffer(alloc int) (*Buffer, error) {
	s := C.malloc(C.gss_buffer_size)
	if s == nil {
		return nil, ErrMallocFailed
	}
	C.memset(s, 0, C.gss_buffer_size)

	b := &Buffer{
		Lib:            lib,
		C_gss_buffer_t: C.gss_buffer_t(s),
		alloc:          alloc,
	}
	return b, nil
}
Exemplo n.º 8
0
func CreateTun(name string) (*Tun, error) {
	f, err := os.OpenFile(TUN_DEV, os.O_RDWR, 0)
	if err != nil {
		return nil, err
	}

	cbuf := (*C.char)(C.malloc(C.IFNAMSIZ))
	defer C.free(unsafe.Pointer(cbuf))
	C.memset(unsafe.Pointer(cbuf), 0, C.IFNAMSIZ)
	c_name := C.CString(name)
	defer C.free(unsafe.Pointer(c_name))

	C.strncpy(cbuf, c_name, C.IFNAMSIZ-1)

	if C.create_tun(C.int(f.Fd()), cbuf) < 0 {
		return nil, fmt.Errorf("create tun fail")
	}

	return &Tun{C.GoString(cbuf), f}, nil
}
Exemplo n.º 9
0
func test6506() {
	// nothing to run, just make sure this compiles
	var x C.size_t

	C.calloc(x, x)
	C.malloc(x)
	C.realloc(nil, x)
	C.memcpy(nil, nil, x)
	C.memcmp(nil, nil, x)
	C.memmove(nil, nil, x)
	C.strncpy(nil, nil, x)
	C.strncmp(nil, nil, x)
	C.strncat(nil, nil, x)
	x = C.strxfrm(nil, nil, x)
	C.memchr(nil, 0, x)
	x = C.strcspn(nil, nil)
	x = C.strspn(nil, nil)
	C.memset(nil, 0, x)
	x = C.strlen(nil)
	_ = x
}
Exemplo n.º 10
0
// Encode writes the image to the given io.Writer, encoding
// it according to the info parameter. Please, see the
// Info type for the available encoding options.
func (im *Image) Encode(w io.Writer, info *Info) error {
	var ex C.ExceptionInfo
	C.GetExceptionInfo(&ex)
	defer C.DestroyExceptionInfo(&ex)
	if info == nil {
		info = NewInfo()
	}
	/* ImageToBlob copies the format from the image into
	the image info. Overwrite if required and then restore
	*/
	im.mu.Lock()
	var format *C.char
	copied := false
	if info.info.magick[0] != 0 {
		copied = true
		if im.image.magick[0] != 0 {
			format = C.strdup(&im.image.magick[0])
		}
		C.strncpy(&im.image.magick[0], &info.info.magick[0], C.MaxTextExtent)
	}
	var s C.size_t
	mem := imageToBlob(info, im, &s, &ex)
	if copied {
		/* Restore image format */
		if format != nil {
			C.strncpy(&im.image.magick[0], format, C.MaxTextExtent)
			C.free(unsafe.Pointer(format))
		} else {
			C.memset(unsafe.Pointer(&im.image.magick[0]), 0, C.MaxTextExtent)
		}
	}
	im.mu.Unlock()
	if mem == nil {
		return exError(&ex, "encoding")
	}
	b := goBytes(mem, int(s))
	w.Write(b)
	C.free(mem)
	return nil
}