bctbx_list_t* bctbx_list_free(bctbx_list_t* list) { bctbx_list_t* elem = list; bctbx_list_t* tmp; if (list==NULL) return NULL; while(elem->next!=NULL) { tmp = elem; elem = elem->next; bctbx_free(tmp); } bctbx_free(elem); return NULL; }
static char* ConvertFromUtf8Filename(const char* fName){ #if _WIN32 char* convertedFilename; int nChar, nb_byte; LPWSTR wideFilename; nChar = MultiByteToWideChar(CP_UTF8, 0, fName, -1, NULL, 0); if (nChar == 0) return NULL; wideFilename = bctbx_malloc(nChar*sizeof(wideFilename[0])); if (wideFilename == NULL) return NULL; nChar = MultiByteToWideChar(CP_UTF8, 0, fName, -1, wideFilename, nChar); if (nChar == 0) { bctbx_free(wideFilename); wideFilename = 0; } nb_byte = WideCharToMultiByte(CP_ACP, 0, wideFilename, -1, 0, 0, 0, 0); if (nb_byte == 0) return NULL; convertedFilename = bctbx_malloc(nb_byte); if (convertedFilename == NULL) return NULL; nb_byte = WideCharToMultiByte(CP_ACP, 0, wideFilename, -1, convertedFilename, nb_byte, 0, 0); if (nb_byte == 0) { bctbx_free(convertedFilename); convertedFilename = 0; } bctbx_free(wideFilename); return convertedFilename; #elif defined(__QNXNTO__) return bctbx_strdup(fName); #else #define MAX_PATH_SIZE 1024 char db_file_utf8[MAX_PATH_SIZE] = {'\0'}; char db_file_locale[MAX_PATH_SIZE] = ""; char *outbuf=db_file_locale, *inbuf=db_file_utf8; size_t inbyteleft = MAX_PATH_SIZE, outbyteleft = MAX_PATH_SIZE; iconv_t cb; if (strcasecmp("UTF-8", nl_langinfo(CODESET)) == 0) { strncpy(db_file_locale, fName, MAX_PATH_SIZE - 1); } else { strncpy(db_file_utf8, fName, MAX_PATH_SIZE-1); cb = iconv_open(nl_langinfo(CODESET), "UTF-8"); if (cb != (iconv_t)-1) { int ret; ret = iconv(cb, &inbuf, &inbyteleft, &outbuf, &outbyteleft); if(ret == -1) db_file_locale[0] = '\0'; iconv_close(cb); } } return bctbx_strdup(db_file_locale); #endif }
bctbx_list_t * bctbx_list_free_with_data(bctbx_list_t *list, bctbx_list_free_func freefunc) { bctbx_list_t* elem = list; bctbx_list_t* tmp; if (list==NULL) return NULL; while(elem->next!=NULL) { tmp = elem; elem = elem->next; freefunc(tmp->data); bctbx_free(tmp); } freefunc(elem->data); bctbx_free(elem); return NULL; }
int32_t bctbx_ssl_set_hostname(bctbx_ssl_context_t *ssl_ctx, const char *hostname){ if (ssl_ctx->cn) bctbx_free(ssl_ctx->cn); if (hostname) ssl_ctx->cn = bctbx_strdup(hostname); else ssl_ctx->cn = NULL; ssl_ctx->ssl_ctx.peer_cn = ssl_ctx->cn; return 0; }
/** * Opens the file fName and populates the structure pointed by p * with the necessary io_methods * Methods not implemented for version 1 : xTruncate, xSectorSize. * Initializes some fields in the p structure, some of which where already * initialized by SQLite. * @param pVfs sqlite3_vfs VFS pointer. * @param fName filename * @param p file handle pointer * @param flags db file access flags * @param pOutFlags flags used by SQLite * @return SQLITE_CANTOPEN on error, SQLITE_OK on success. */ static int sqlite3bctbx_Open(sqlite3_vfs *pVfs, const char *fName, sqlite3_file *p, int flags, int *pOutFlags ){ static const sqlite3_io_methods sqlite3_bctbx_io = { 1, /* iVersion Structure version number */ sqlite3bctbx_Close, /* xClose */ sqlite3bctbx_Read, /* xRead */ sqlite3bctbx_Write, /* xWrite */ 0, /*xTruncate*/ sqlite3bctbx_Sync, sqlite3bctbx_FileSize, sqlite3bctbx_nolockLock, sqlite3bctbx_nolockUnlock, sqlite3bctbx_nolockCheckReservedLock, sqlite3bctbx_FileControl, 0, /*xSectorSize*/ sqlite3bctbx_DeviceCharacteristics, }; sqlite3_bctbx_file_t * pFile = (sqlite3_bctbx_file_t*)p; /*File handle sqlite3_bctbx_file_t*/ int openFlags = 0; char* wFname; /*returns error if filename is empty or file handle not initialized*/ if (pFile == NULL || fName == NULL){ return SQLITE_IOERR; } /* Set flags to open the file with */ if( flags&SQLITE_OPEN_EXCLUSIVE ) openFlags |= O_EXCL; if( flags&SQLITE_OPEN_CREATE ) openFlags |= O_CREAT; if( flags&SQLITE_OPEN_READONLY ) openFlags |= O_RDONLY; if( flags&SQLITE_OPEN_READWRITE ) openFlags |= O_RDWR; #if defined(_WIN32) openFlags |= O_BINARY; #endif wFname = ConvertFromUtf8Filename(fName); if (wFname != NULL) { pFile->pbctbx_file = bctbx_file_open2(bctbx_vfs_get_default(), wFname, openFlags); bctbx_free(wFname); } else { pFile->pbctbx_file = NULL; } if( pFile->pbctbx_file == NULL){ return SQLITE_CANTOPEN; } if( pOutFlags ){ *pOutFlags = flags; } pFile->base.pMethods = &sqlite3_bctbx_io; return SQLITE_OK; }
bctbx_list_t * bctbx_list_pop_front(bctbx_list_t *list, void **front_data) { bctbx_list_t *front_elem=list; if (front_elem==NULL) { *front_data=NULL; return NULL; } *front_data=front_elem->data; list=bctbx_list_unlink(list,front_elem); bctbx_free(front_elem); return list; }
int bctbx_file_close(bctbx_vfs_file_t *pFile) { int ret = BCTBX_VFS_ERROR; if (pFile) { ret = pFile->pMethods->pFuncClose(pFile); if (ret != 0) { bctbx_error("bctbx_file_close: Error %s freeing file handle anyway", strerror(-(ret))); } } bctbx_free(pFile); return ret; }
/* portable named pipes */ bctbx_pipe_t bctbx_server_pipe_create(const char *name){ #ifdef BCTBX_WINDOWS_DESKTOP bctbx_pipe_t h; char *pipename=make_pipe_name(name); h=CreateNamedPipe(pipename,PIPE_ACCESS_DUPLEX|FILE_FLAG_OVERLAPPED,PIPE_TYPE_MESSAGE|PIPE_WAIT,1, 32768,32768,0,NULL); bctbx_free(pipename); if (h==INVALID_HANDLE_VALUE){ bctbx_error("Fail to create named pipe %s",pipename); } if (event==NULL) event=CreateEvent(NULL,TRUE,FALSE,NULL); return h; #else bctbx_error("%s not supported!", __FUNCTION__); return INVALID_HANDLE_VALUE; #endif }
/* portable named pipes */ bctbx_socket_t bctbx_server_pipe_create(const char *name){ struct sockaddr_un sa; char *pipename=make_pipe_name(name); bctbx_socket_t sock; sock=socket(AF_UNIX,SOCK_STREAM,0); sa.sun_family=AF_UNIX; strncpy(sa.sun_path,pipename,sizeof(sa.sun_path)-1); unlink(pipename);/*in case we didn't finished properly previous time */ bctbx_free(pipename); fchmod(sock,S_IRUSR|S_IWUSR); if (bind(sock,(struct sockaddr*)&sa,sizeof(sa))!=0){ bctbx_error("Failed to bind command unix socket: %s",strerror(errno)); return -1; } listen(sock,1); return sock; }
void bctbx_shm_close(void *mem){ #ifdef BCTBX_WINDOWS_DESKTOP bctbx_list_t *elem; for(elem=maplist;elem;elem=bctbx_list_next(elem)){ MapInfo *i=(MapInfo*)bctbx_list_get_data(elem); if (i->mem==mem){ CloseHandle(i->h); UnmapViewOfFile(mem); bctbx_free(i); maplist=bctbx_list_erase_link(maplist,elem); return; } } bctbx_error("No shared memory at %p was found.",mem); #else bctbx_error("%s not supported!", __FUNCTION__); #endif }
ssize_t bctbx_file_fprintf(bctbx_vfs_file_t *pFile, off_t offset, const char *fmt, ...) { char *ret = NULL; va_list args; ssize_t r = BCTBX_VFS_ERROR; size_t count = 0; va_start(args, fmt); ret = bctbx_strdup_vprintf(fmt, args); if (ret != NULL) { va_end(args); count = strlen(ret); if (offset != 0) pFile->offset = offset; r = bctbx_file_write(pFile, ret, count, pFile->offset); bctbx_free(ret); if (r > 0) pFile->offset += r; } return r; }
bctbx_pipe_t bctbx_client_pipe_connect(const char *name){ #ifdef BCTBX_WINDOWS_DESKTOP char *pipename=make_pipe_name(name); bctbx_pipe_t hpipe = CreateFile( pipename, // pipe name GENERIC_READ | // read and write access GENERIC_WRITE, 0, // no sharing NULL, // default security attributes OPEN_EXISTING, // opens existing pipe 0, // default attributes NULL); // no template file bctbx_free(pipename); return hpipe; #else bctbx_error("%s not supported!", __FUNCTION__); return INVALID_HANDLE_VALUE; #endif }
bctbx_socket_t bctbx_client_pipe_connect(const char *name){ bctbx_socket_t sock = -1; struct sockaddr_un sa; struct stat fstats; char *pipename=make_pipe_name(name); uid_t uid = getuid(); // check that the creator of the pipe is us if( (stat(name, &fstats) == 0) && (fstats.st_uid != uid) ){ bctbx_error("UID of file %s (%lu) differs from ours (%lu)", pipename, (unsigned long)fstats.st_uid, (unsigned long)uid); return -1; } sock = socket(AF_UNIX,SOCK_STREAM,0); sa.sun_family=AF_UNIX; strncpy(sa.sun_path,pipename,sizeof(sa.sun_path)-1); bctbx_free(pipename); if (connect(sock,(struct sockaddr*)&sa,sizeof(sa))!=0){ close(sock); return -1; } return sock; }
void bctbx_rng_context_free(bctbx_rng_context_t *context) { bctbx_free(context); }
static unsigned WINAPI thread_starter(void *data){ thread_param_t *params=(thread_param_t*)data; params->func(params->arg); bctbx_free(data); return 0; }
void bctbx_signing_key_free(bctbx_signing_key_t *key) { rsa_free((rsa_context *)key); bctbx_free(key); }
bctbx_list_t * bctbx_list_erase_link(bctbx_list_t* list, bctbx_list_t* elem) { bctbx_list_t *ret=bctbx_list_unlink(list,elem); bctbx_free(elem); return ret; }
void bctbx_x509_certificate_free(bctbx_x509_certificate_t *cert) { x509_free((x509_cert *)cert); bctbx_free(cert); }
void bctbx_ssl_context_free(bctbx_ssl_context_t *ssl_ctx) { if (ssl_ctx->cn) bctbx_free(ssl_ctx->cn); ssl_free(&(ssl_ctx->ssl_ctx)); bctbx_free(ssl_ctx); }
void bctbx_ssl_config_free(bctbx_ssl_config_t *ssl_config) { bctbx_free(ssl_config); }