I tried to use rebar to compile an iconv port extracted from ejabberd (http://www.ejabberd.im/), it successfully generated an "iconv_erl.so".
but when I use erl_ddll:load_driver("priv/", iconv_erl)
to load it, it returns {error,{open_error,-10}}.
Is there anything I am missing? thx in advance.
p.s. The c source is as follows:
/*
* ejabberd, Copyright (C) 2002-2010 ProcessOne
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
* 02111-1307 USA
*
*/
#include <stdio.h>
#include <string.h>
#include <erl_driver.h>
#include <ei.h>
#include <iconv.h>
typedef struct {
ErlDrvPort port;
iconv_t cd;
} iconv_data;
static ErlDrvData iconv_erl_start(ErlDrvPort port, char *buff)
{
iconv_data* d = (iconv_data*)driver_alloc(sizeof(iconv_data));
d->port = port;
d->cd = NULL;
set_port_control_flags(port, PORT_CONTROL_FLAG_BINARY);
return (ErlDrvData)d;
}
static void iconv_erl_stop(ErlDrvData handle)
{
driver_free((char*)handle);
}
static int iconv_erl_control(ErlDrvData drv_data,
unsigned int command,
char *buf, int len,
char **rbuf, int rlen)
{
int i;
int size;
int index = 0;
int avail;
size_t inleft, outleft;
ErlDrvBinary *b;
char *from, *to, *string, *stmp, *rstring, *rtmp;
iconv_t cd;
int invalid_utf8_as_latin1 = 0;
ei_decode_version(buf, &index, &i);
ei_decode_tuple_header(buf, &index, &i);
ei_get_type(buf, &index, &i, &size);
from = driver_alloc(size + 1);
ei_decode_string(buf, &index, from);
ei_get_type(buf, &index, &i, &size);
to = driver_alloc(size + 1);
ei_decode_string(buf, &index, to);
ei_get_type(buf, &index, &i, &size);
stmp = string = driver_alloc(size + 1);
ei_decode_string(buf, &index, string);
/* Special mode: parse as UTF-8 if possible; otherwise assume it's
Latin-1. Makes no difference when encoding. */
if (strcmp(from, "utf-8+latin-1") == 0) {
from[5] = '\0';
invalid_utf8_as_latin1 = 1;
}
if (strcmp(to, "utf-8+latin-1") == 0) {
to[5] = '\0';
}
cd = iconv_open(to, from);
if (cd == (iconv_t) -1) {
cd = iconv_open("ascii", "ascii");
if (cd == (iconv_t) -1) {
*rbuf = (char*)(b = driver_alloc_binary(size));
memcpy(b->orig_bytes, string, size);
driver_free(from);
driver_free(to);
driver_free(string);
return size;
}
}
outleft = avail = 4*size;
inleft = size;
rtmp = rstring = driver_alloc(avail);
while (inleft > 0) {
if (iconv(cd, &stmp, &inleft, &rtmp, &outleft) == (size_t) -1) {
if (invalid_utf8_as_latin1 && (*stmp & 0x80) && outleft >= 2) {
/* Encode one byte of (assumed) Latin-1 into two bytes of UTF-8 */
*rtmp++ = 0xc0 | ((*stmp & 0xc0) >> 6);
*rtmp++ = 0x80 | (*stmp & 0x3f);
outleft -= 2;
}
stmp++;
inleft--;
}
}
size = rtmp - rstring;
*rbuf = (char*)(b = driver_alloc_binary(size));
memcpy(b->orig_bytes, rstring, size);
driver_free(from);
driver_free(to);
driver_free(string);
driver_free(rstring);
iconv_close(cd);
return size;
}
ErlDrvEntry iconv_driver_entry = {
NULL, /* F_PTR init, N/A */
iconv_erl_start, /* L_PTR start, called when port is opened */
iconv_erl_stop, /* F_PTR stop, called when port is closed */
NULL, /* F_PTR output, called when erlang has sent */
NULL, /* F_PTR ready_input, called when input descriptor ready */
NULL, /* F_PTR ready_output, called when output descriptor ready */
"iconv_erl", /* char *driver_name, the argument to open_port */
NULL, /* F_PTR finish, called when unloaded */
NULL, /* handle */
iconv_erl_control, /* F_PTR control, port_command callback */
NULL, /* F_PTR timeout, reserved */
NULL /* F_PTR outputv, reserved */
};
DRIVER_INIT(iconv_erl) /* must match name in driver_entry */
{
return &iconv_driver_entry;
}
When you get an error from erl_ddll:load_driver/2, try passing the error to the erl_ddll:format_error/1 function to get more details. For example, I'm currently seeing the same error you are — {open_error, -10} — so I pass that to erl_ddll:format_error/1 in an Erlang shell, as shown below:
1> erl_ddll:format_error({open_error,-10}).
"undefined symbol: _ZTVN10__cxxabiv117__class_type_infoE"
The output shows that my driver won't load because it has an undefined symbol.
Related
I take sample.c of kcgi add sqlite3 and put what's in sendindex() into sqlite_exec() callback with adding some stuff.
struct kreq req is passed to callback.
Dunno if khtmlreq is good here but looks like yes.
/* $Id$ */
/*
* Copyright (c) 2014, 2015, 2017 Kristaps Dzonsons <kristaps#bsd.lv>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h> /* size_t, ssize_t */
#include <stdarg.h> /* va_list */
#include <stddef.h> /* NULL */
#include <stdint.h> /* int64_t */
#include <stdlib.h>
#include <string.h> /* memset */
#include <stdio.h>
#include <sqlite3.h>
#include <kcgi.h>
#include <kcgihtml.h>
/*
* Simple CGI application.
* Compile it with `make samples` (or using gmake) and install it into
* your web server's /cgi-bin.
* The "template.xml" file should be in the /cgi-bin directory as well
* and readable by the server process.
* (Obviously this is just for a sample.)
*
* Assuming localhost/cgi-bin, the script is localhost/cgi-bin/sample.
* The pages recognised are:
*
* - /cgi-bin/sample/index.html
* - /cgi-bin/sample/template.html
* - /cgi-bin/sample/senddata.html
*
* See the sendindex et al. functions for what these do.
*/
/* Recognised page requests. See pages[]. */
enum page {
PAGE_INDEX,
PAGE_TEMPLATE,
PAGE_SENDDATA,
PAGE__MAX
};
/*
* All of the keys (input field names) we accept.
* The key names are in the "keys" array.
* See sendindex() for how these are used.
*/
enum key {
KEY_INTEGER,
KEY_FILE,
KEY_PAGECOUNT,
KEY_PAGESIZE,
KEY__MAX
};
/*
* The elements in our template file.
* The element key names are in the "templs" array.
* See sendtemplate() for how this is used.
*/
enum templ {
TEMPL_TITLE,
TEMPL_NAME,
TEMPL_REMOTE_ADDR,
TEMPL__MAX
};
/*
* We need a structure because we can't get the "r" from the request.
* This is used by our template callback.
*/
struct tstrct {
struct khtmlreq req;
struct kreq *r;
};
/*
* We'll use this to route pages by creating an array indexed by our
* page.
* Then when the page is parsed, we'll route directly into it.
*/
typedef void (*disp)(struct kreq *);
static void senddata(struct kreq *);
static void sendindex(struct kreq *);
static void sendtemplate(struct kreq *);
static const disp disps[PAGE__MAX] = {
sendindex, /* PAGE_INDEX */
sendtemplate, /* PAGE_TEMPLATE */
senddata, /* PAGE_SENDDATA */
};
static const struct kvalid keys[KEY__MAX] = {
{ kvalid_int, "integer" }, /* KEY_INTEGER */
{ NULL, "file" }, /* KEY_FILE */
{ kvalid_uint, "count" }, /* KEY_PAGECOUNT */
{ kvalid_uint, "size" }, /* KEY_PAGESIZE */
};
/*
* Template key names (as in ##TITLE## in the file).
*/
static const char *const templs[TEMPL__MAX] = {
"title", /* TEMPL_TITLE */
"name", /* TEMPL_NAME */
"remote_addr", /* TEMPL_REMOTE_ADDR */
};
/*
* Page names (as in the URL component) mapped from the first name part
* of requests, e.g., /sample.cgi/index.html -> index -> PAGE_INDEX.
*/
static const char *const pages[PAGE__MAX] = {
"index", /* PAGE_INDEX */
"template", /* PAGE_TEMPLATE */
"senddata" /* PAGE_SENDDATA */
};
/*
* Open an HTTP response with a status code and a particular
* content-type, then open the HTTP content body.
* You can call khttp_head(3) before this: CGI doesn't dictate any
* particular header order.
*/
static void
resp_open(struct kreq *req, enum khttp http)
{
enum kmime mime;
/*
* If we've been sent an unknown suffix like '.foo', we won't
* know what it is.
* Default to an octet-stream response.
*/
if (KMIME__MAX == (mime = req->mime))
mime = KMIME_APP_OCTET_STREAM;
khttp_head(req, kresps[KRESP_STATUS],
"%s", khttps[http]);
khttp_head(req, kresps[KRESP_CONTENT_TYPE],
"%s", kmimetypes[mime]);
khttp_body(req);
}
/*
* Callback for filling in a particular template part.
* Let's just be simple for simplicity's sake.
*/
static int
template(size_t key, void *arg)
{
struct tstrct *p = arg;
switch (key) {
case (TEMPL_TITLE):
khtml_puts(&p->req, "title");
break;
case (TEMPL_NAME):
khtml_puts(&p->req, "name");
break;
case (TEMPL_REMOTE_ADDR):
khtml_puts(&p->req, p->r->remote);
break;
default:
return(0);
}
return(1);
}
/*
* Demonstrates how to use templates.
* Returns HTTP 200 and the template content.
*/
static void
sendtemplate(struct kreq *req)
{
struct ktemplate t;
struct tstrct p;
memset(&t, 0, sizeof(struct ktemplate));
memset(&p, 0, sizeof(struct tstrct));
p.r = req;
t.key = templs;
t.keysz = TEMPL__MAX;
t.arg = &p;
t.cb = template;
resp_open(req, KHTTP_200);
khtml_open(&p.req, req, 0);
khttp_template(req, &t, "template.xml");
khtml_close(&p.req);
}
/*
* Send a random amount of data.
* Requires KEY_PAGECOUNT (optional), KEY_PAGESIZE (optional).
* Page count is the number of times we flush a page (with the given
* size) to the wire.
* Returns HTTP 200 and the random data.
*/
static void
senddata(struct kreq *req)
{
int64_t i, j, nm, sz;
char *buf;
nm = 1024 * 1024;
if (NULL != req->fieldmap[KEY_PAGECOUNT])
nm = req->fieldmap[KEY_PAGECOUNT]->parsed.i;
if (0 == nm)
nm = 1;
sz = 1;
if (NULL != req->fieldmap[KEY_PAGESIZE])
sz = req->fieldmap[KEY_PAGESIZE]->parsed.i;
if (0 == sz || (uint64_t)sz > SIZE_MAX)
sz = 1;
buf = kmalloc(sz);
resp_open(req, KHTTP_200);
for (i = 0; i < nm; i++) {
for (j = 0; j < sz; j++)
#ifndef __linux__
buf[j] = 65 + arc4random_uniform(24);
#else
buf[j] = 65 + (random() % 24);
#endif
khttp_write(req, buf, sz);
}
free(buf);
}
/*
* Demonstrates how to use GET and POST forms and building with the HTML
* builder functions.
* Returns HTTP 200 and HTML content.
*/
Here is the callback function including what was in sendindex() :
static int callback(void *ptr, int argc, char **argv, char **azColName) {
struct kreq *req = (struct kreq *)ptr;
char *page;
struct khtmlreq r;
const char *cp;
cp = NULL == req->fieldmap[KEY_INTEGER] ?
"" : req->fieldmap[KEY_INTEGER]->val;
kasprintf(&page, "%s/%s", req->pname, pages[PAGE_INDEX]);
resp_open(req, KHTTP_200);
khtml_open(&r, req, 0);
khtml_elem(&r, KELEM_DOCTYPE);
khtml_elem(&r, KELEM_HTML);
khtml_elem(&r, KELEM_HEAD);
khtml_elem(&r, KELEM_TITLE);
khtml_puts(&r, "Welcome!");
khtml_closeelem(&r, 2);
khtml_elem(&r, KELEM_BODY);
khtml_puts(&r, "Welcome!");
khtml_attr(&r, KELEM_FORM,
KATTR_METHOD, "post",
KATTR_ENCTYPE, "multipart/form-data",
KATTR_ACTION, page,
KATTR__MAX);
khtml_elem(&r, KELEM_FIELDSET);
khtml_elem(&r, KELEM_LEGEND);
khtml_puts(&r, "Post (multipart)");
khtml_closeelem(&r, 1);
khtml_elem(&r, KELEM_P);
khtml_attr(&r, KELEM_INPUT,
KATTR_TYPE, "number",
KATTR_NAME, keys[KEY_INTEGER].name,
KATTR_VALUE, cp, KATTR__MAX);
khtml_closeelem(&r, 1);
khtml_elem(&r, KELEM_P);
khtml_attr(&r, KELEM_INPUT,
KATTR_TYPE, "file",
KATTR_MULTIPLE, "",
KATTR_NAME, keys[KEY_FILE].name,
KATTR__MAX);
if (NULL != req->fieldmap[KEY_FILE]) {
if (NULL != req->fieldmap[KEY_FILE]->file) {
khtml_puts(&r, "file: ");
khtml_puts(&r, req->fieldmap[KEY_FILE]->file);
khtml_puts(&r, " ");
}
if (NULL != req->fieldmap[KEY_FILE]->ctype) {
khtml_puts(&r, "ctype: ");
khtml_puts(&r, req->fieldmap[KEY_FILE]->ctype);
}
}
int i;
for(i=0; i<argc; i++){
printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
khtml_puts(&r, "OK");
}
khtml_closeelem(&r, 1);
khtml_elem(&r, KELEM_P);
khtml_attr(&r, KELEM_INPUT,
KATTR_TYPE, "submit",
KATTR__MAX);
khtml_closeelem(&r, 0);
khtml_close(&r);
free(page);
}
Here is sendindex() function :
static void
sendindex(struct kreq *req)
{
sqlite3 *db;
char *zErrMsg = 0;
int rc;
rc = sqlite3_open("/var/www/MaSSH/databases/massh.db", &db);
if(rc){
sqlite3_close(db);
}
rc = sqlite3_exec(db, "SELECT * FROM hosts;", callback, &req, &zErrMsg);
if(rc!= SQLITE_OK){
sqlite3_free(zErrMsg);
}
sqlite3_close(db);
}
int
main(void)
{
struct kreq r;
enum kcgi_err er;
/* Set up our main HTTP context. */
er = khttp_parse(&r, keys, KEY__MAX,
pages, PAGE__MAX, PAGE_INDEX);
if (KCGI_OK != er)
return(EXIT_FAILURE);
/*
* Accept only GET, POST, and OPTIONS.
* Restrict to text/html and a valid page.
* If all of our parameters are valid, use a dispatch array to
* send us to the page handlers.
*/
if (KMETHOD_OPTIONS == r.method) {
khttp_head(&r, kresps[KRESP_ALLOW],
"OPTIONS GET POST");
resp_open(&r, KHTTP_200);
} else if (KMETHOD_GET != r.method &&
KMETHOD_POST != r.method) {
resp_open(&r, KHTTP_405);
} else if (PAGE__MAX == r.page ||
KMIME_TEXT_HTML != r.mime) {
resp_open(&r, KHTTP_404);
} else
(*disps[r.page])(&r);
khttp_free(&r);
return(EXIT_SUCCESS);
}
If you run your code through a debugger you will see
Program received signal SIGSEGV, Segmentation fault.
0x00000b60dbbd86b6 in callback (ptr=0x7f7ffffeb878, argc=1, argv=0xb6317623210,azColName=0xb6317623208) at /tmp/so.c:262
262 cp = NULL == req->fieldmap[KEY_INTEGER] ?
(gdb) bt
#0 0x0000040c108fc6b6 in callback () from /home/dsp/a.out
#1 0x0000040e744f172a in sqlite3_exec () from /usr/local/lib/libsqlite3.so.37.7
#2 0x0000040c108fcb79 in sendindex () from /home/dsp/a.out
#3 0x0000040c108fccbe in main () from /home/dsp/a.out
that tells that that req is probably not holding what you think it's holding.
We see from looking at the source that the callback function does:
static int callback(void *ptr, int argc, char **argv, char **azColName) {
struct kreq *req = (struct kreq *)ptr;
so the void * passed to it is not what it should be. we see from the backtrace that this is passed from the sqlite3_exec function.
It seems it requires the pointer to the request to be the 4th argument. A google search turns out this SO answer that you probably received yesterday from Chris Loonam and he correctly says that in setindex your code should pass the pointer to the req as the 4th argument like so
rc = sqlite3_exec(db, "SELECT * FROM hosts;", callback, req, &zErrMsg);
since that function takes req as
static void sendindex(struct kreq *req)
in your code you are passing it in sqlite3_exec as &req therefore it ends up being a pointer to a pointer of a request and the casting in the callback of course ends up leading to an invalid dereference.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I'm currently working on a project in C which I'm required to create a buffer program. For the most part my program is finished but I'm just missing one required function: a location function to return the distance between the head (beginning of array) to the desired location designated by short loc_offset.
Given what I already have, how do I return a pointer to an index or location from my short?
Here is my given and created code:
Main (plat_bt.c):
/* File name: platy_bt.c
* Purpose:This is the main program for Assignment #1, CST8152
* Version: 1.18.1
* Author: Svillen Ranev
* Date: 16 January 2018
*/
/* The #define _CRT_SECURE_NO_WARNINGS should be used in MS Visual Studio projects
* to suppress the warnings about using "unsafe" functions like fopen()
* and standard sting library functions defined in string.h.
* The define directive does not have any effect on other compiler projects (gcc, Borland).
*/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <limits.h>
#include "buffer.h"
/* constant definitions */
#define INIT_CAPACITY 200 /* initial buffer capacity */
#define INC_FACTOR 15 /* increment factor */
/*check for ANSI C compliancy */
#define ANSI_C 0
#if defined(__STDC__)
#undef ANSI_C
#define ANSI_C 1
#endif
/* Declaration of an error printing function with
* variable number of arguments
*/
void err_printf(char *fmt, ...);
/* Declaration of a buffer contents display function */
void display (Buffer *ptr_Buffer);
long get_filesize(char *fname);
int main(int argc, char **argv){
pBuffer ptr_Buffer; /* pointer to Buffer structure */
FILE *fi; /* input file handle */
int loadsize = 0; /*the size of the file loaded in the buffer */
int ansi_c = !ANSI_C; /* ANSI C compliancy flag */
/* Check if the compiler option is set to compile ANSI C */
/* __DATE__, __TIME__, __LINE__, __FILE__, __STDC__ are predefined preprocessor macros*/
if(ansi_c){
err_printf("Date: %s Time: %s",__DATE__, __TIME__);
err_printf("ERROR: Compiler is not ANSI C compliant!\n");
exit(1);
}
/* missing file name or/and mode parameter */
if (argc <= 2){
err_printf("\nDate: %s Time: %s",__DATE__, __TIME__);
err_printf("\nRuntime error at line %d in file %s\n", __LINE__, __FILE__);
err_printf("%s\b\b\b\b%s%s",argv[0],": ","Missing parameters.");
err_printf("Usage: platybt source_file_name mode");
exit(1);
}
/* create a source code input buffer */
switch(*argv[2]){
case 'f': case 'a': case 'm': break;
default:
err_printf("%s%s%s",argv[0],": ","Wrong mode parameter.");
exit(1);
}
/*create the input buffer */
ptr_Buffer = b_allocate(INIT_CAPACITY,INC_FACTOR,*argv[2]);
if (ptr_Buffer == NULL){
err_printf("%s%s%s",argv[0],": ","Cannot allocate buffer.");
exit(1);
}
/* open the source file */
if ((fi = fopen(argv[1],"r")) == NULL){
err_printf("%s%s%s%s",argv[0],": ", "Cannot open file: ",argv[1]);
exit (1);
}
/* load a source file into the input buffer */
printf("Reading file %s ....Please wait\n",argv[1]);
loadsize = b_load (fi,ptr_Buffer);
if(loadsize == RT_FAIL1)
err_printf("%s%s%s",argv[0],": ","Error in loading buffer.");
/* close the source file */
fclose(fi);
/*find the size of the file */
if (loadsize == LOAD_FAIL){
printf("The input file %s %s\n", argv[1],"is not completely loaded.");
printf("Input file size: %ld\n", get_filesize(argv[1]));
}
/* display the contents of the input buffer */
display(ptr_Buffer);
/* compact the buffer
* add end-of-file character (EOF) to the buffer
* display again
*/
if(!b_compact(ptr_Buffer,EOF)){
err_printf("%s%s%s",argv[0],": ","Error in compacting buffer.");
}
display(ptr_Buffer);
/* free the dynamic memory used by the buffer */
b_free(ptr_Buffer);
/* make the buffer invalid
It is not necessary here because the function terminates anyway,
but will prevent run-time errors and crashes in future expansions
*/
ptr_Buffer = NULL;
/*return success */
return (0);
}
/* error printing function with variable number of arguments*/
void err_printf( char *fmt, ... ){
/*Initialize variable list */
va_list ap;
va_start(ap, fmt);
(void)vfprintf(stderr, fmt, ap);
va_end(ap);
/* Move to new line */
if( strchr(fmt,'\n') == NULL )
fprintf(stderr,"\n");
}
void display (Buffer *ptr_Buffer){
printf("\nPrinting buffer parameters:\n\n");
printf("The capacity of the buffer is: %d\n",b_capacity(ptr_Buffer));
printf("The current size of the buffer is: %d\n",b_limit(ptr_Buffer));
printf("The operational mode of the buffer is: %d\n",b_mode(ptr_Buffer));
printf("The increment factor of the buffer is: %lu\n",b_incfactor(ptr_Buffer));
printf("The current mark of the buffer is: %d\n", b_mark(ptr_Buffer, b_limit(ptr_Buffer)));
/*printf("The reallocation flag is: %d\n",b_rflag(ptr_Buffer));*/
printf("\nPrinting buffer contents:\n\n");
b_rewind(ptr_Buffer);
b_print(ptr_Buffer);
}
long get_filesize(char *fname){
FILE *input;
long flength;
input = fopen(fname, "r");
if(input == NULL){
err_printf("%s%s","Cannot open file: ",fname);
return 0;
}
fseek(input, 0L, SEEK_END);
flength = ftell(input);
fclose(input);
return flength;
}
That main file has no errors or warnings, it was given to me to be used as my main.
Here is my edited source file (buffer.c):
/* File name: buffer.c
* Purpose: This is the main program for Assignment #01, CST8152
* Version: 1.0.3
* Author: Jack Loveday
* Date: 7 Febuary, 2018
*/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <limits.h>
#include "buffer.h"
/* b_allocate: Creates new buffer on heap
Tries to allocate memory for:
- one Buffer (calloc)
- one dynamic character buffer (char array) (malloc)
Sets buffer operational mode and inc factor
Copies given init_capacity into Buffer capacity val */
Buffer * b_allocate(short init_capacity, char inc_factor, char o_mode) {
Buffer *pBuffer;
if (init_capacity < 0 || (unsigned char)inc_factor < 0)
return NULL;
pBuffer = (Buffer *)calloc(1, sizeof(Buffer));
if (!pBuffer) {
free(pBuffer);
return NULL;
}
pBuffer->cb_head = (char *)malloc(init_capacity);
if (o_mode == 'f' || (unsigned char)inc_factor == 0) {
pBuffer->mode = 0;
pBuffer->inc_factor = 0;
}
else if (o_mode == 'a') {
pBuffer->mode = ADD_MODE;
pBuffer->inc_factor = (unsigned char)inc_factor;
}
else if (o_mode == 'm' && (unsigned char)inc_factor <= 100) {
pBuffer->mode = MULTI_MODE;
pBuffer->inc_factor = (unsigned char)inc_factor;
}
else {
free(pBuffer->cb_head);
free(pBuffer);
return NULL;
}
pBuffer->capacity = init_capacity;
return pBuffer;
}
/* b_addc: resets r_flag to 0 & tries to add symbol to char array pointed by pBD */
pBuffer b_addc(pBuffer const pBD, char symbol) {
char *tBuffer;
short aSpace = 0, newInc = 0, newCapacity = 0;
if (!pBD)
return NULL;
pBD->r_flag = 0;
if ((short)(pBD->addc_offset * sizeof(char)) < pBD->capacity) {
pBD->cb_head[pBD->addc_offset] = symbol;
pBD->addc_offset++;
}
else if ((short)(pBD->addc_offset * sizeof(char)) == pBD->capacity) {
if (pBD->mode == 0)
return NULL;
else if (pBD->mode == 1) {
newCapacity = pBD->capacity + (unsigned char)pBD->inc_factor * sizeof(char);
if (newCapacity <= 0)
return NULL;
pBD->capacity = newCapacity;
}
else if (pBD->mode == -1) {
if (pBD->capacity == SHRT_MAX)
return NULL;
else {
aSpace = SHRT_MAX - pBD->capacity;
newInc = (short)((aSpace * pBD->inc_factor) / 100);
if (!newInc)
newInc = NEW_ONE;
newCapacity = (short)((pBD->capacity + newInc));
if (newCapacity <= 0)
newCapacity = SHRT_MAX;
}
}
tBuffer = (char *)realloc(pBD->cb_head, newCapacity);
if (tBuffer != pBD->cb_head)
pBD->r_flag = SET_R_FLAG;
pBD->cb_head = tBuffer;
pBD->cb_head[pBD->addc_offset] = symbol;
pBD->addc_offset++;
pBD->capacity = newCapacity;
}
return pBD;
}
/* b_clear: retains memory space currently allocated to buffer, but
re-initializes all appropriate members of given Buffer */
int b_clear(Buffer * const pBD){
if (!pBD)
return RT_FAIL1;
pBD->capacity = 0;
pBD->addc_offset = 0;
pBD->getc_offset = 0;
pBD->eob = 0;
return 1;
}
/* b_free: de-allocates memory for char array & Buffer struct */
void b_free(Buffer * const pBD) {
if (pBD) {
free(pBD->cb_head);
free(pBD);
}
}
/* b_isfull: returns 1 if buffer is full, otherwise returns 0, if rt error return -1 */
int b_isfull(Buffer * const pBD) {
if (!pBD)
return RT_FAIL1;
if ((short)(pBD->addc_offset * sizeof(char)) == pBD->capacity)
return 1;
else
return 0;
}
/* b_limit: returns current limit of buffer, return -1 on error */
short b_limit(Buffer * const pBD) {
if (!pBD)
return RT_FAIL1;
return pBD->addc_offset;
}
/* b_capacity: returns current capacity of buffer, return -1 on error */
short b_capacity(Buffer * const pBD) {
if (!pBD)
return RT_FAIL1;
return pBD->capacity;
}
/* b_mark: sets markc_offset to mark, must be between 0 and addc_offset, return -1 on error */
short b_mark(pBuffer const pBD, short mark) {
if (!pBD || mark < 0 || mark > pBD->addc_offset)
return RT_FAIL1;
pBD->markc_offset = mark;
return pBD->markc_offset;
}
/* b_mode: returns value of mode, or -1 on error */
int b_mode(Buffer * const pBD) {
if (!pBD)
return RT_FAIL1;
return pBD->mode;
}
/* b_infactor: returns non-negative value of inc_factor, return 0x100 (256) */
size_t b_incfactor(Buffer * const pBD) {
if (!pBD)
return NEW_FULL;
return (unsigned char)pBD->inc_factor;
}
/* b_load: returns value of mode */
int b_load(FILE * const fi, Buffer * const pBD) {
char c;
int cNum = 0;
if (!fi || !pBD)
return RT_FAIL1;
for (;;) {
c = (char)fgetc(fi);
if (feof(fi))
break;
if (!b_addc(pBD, c))
return LOAD_FAIL;
++cNum;
}
fclose(fi);
return cNum;
}
/* b_isempty: if addc_offset is 0 return 1, otherwise return 0, return -1 on error */
int b_isempty(Buffer * const pBD) {
if (!pBD)
return RT_FAIL1;
if (!pBD->addc_offset)
return 1;
else
return 0;
}
/* b_eob: returns eob (end of buffer), returns -1 on error */
int b_eob(Buffer * const pBD) {
if (!pBD)
return RT_FAIL1;
return pBD->eob;
}
/* b_getc: checks logic, if not valid returns -2, if getc_offset and addc_offset
are equal, set eob to 1 and returns -1, otherwise set eob to 0 */
char b_getc(Buffer * const pBD) {
char returner;
if (!pBD || !pBD->cb_head)
return RT_FAIL2;
if (pBD->getc_offset == pBD->addc_offset) {
pBD->eob = 1;
return RT_FAIL1;
}
else {
pBD->eob = 0;
}
returner = *(pBD->cb_head + pBD->getc_offset);
pBD->getc_offset++;
return returner;
}
/* b_print: used for diagnostic purposes only, returns -1 on error */
int b_print(Buffer * const pBD) {
int numOfChars = 0;
char c;
if (!pBD->addc_offset) {
printf("The Buffer is empty.\n");
return numOfChars;
}
pBD->getc_offset = 0;
while (1) {
c = b_getc(pBD);
if (b_eob(pBD))
break;
printf("%c", c);
++numOfChars;
}
numOfChars = pBD->getc_offset;
pBD->getc_offset = 0;
printf("\n");
return numOfChars;
}
/* b_compact: shrinks buffer to new capacity, before returning to a pointer add symbol
to the end of buffer, must set r_flag appropriatley */
Buffer * b_compact(Buffer * const pBD, char symbol) {
char *tempBuffer;
short tempCap;
if (!pBD)
return NULL;
tempCap = (pBD->addc_offset + 1) * sizeof(char);
if (tempCap == SHRT_MAX)
return pBD;
tempBuffer = (char *)realloc(pBD->cb_head, (unsigned short)tempCap);
if (tempCap == 0)
return NULL;
if (tempBuffer != pBD->cb_head)
pBD->r_flag = SET_R_FLAG;
pBD->cb_head = tempBuffer;
pBD->capacity = tempCap;
return pBD;
}
/* b_rflag: returns r_flag, returns -1 on error */
char b_rflag(Buffer * const pBD) {
if (!pBD)
return RT_FAIL1;
return pBD->r_flag;
}
/* b_retract: decrements getc_offset by 1, returns -1 on error, otherwise returns getc_offset */
short b_retract(Buffer * const pBD) {
if (!pBD)
return RT_FAIL1;
pBD->getc_offset--;
return pBD->getc_offset;
}
/* b_reset: sets getc_offset to current markc_offset, returns -1 on error and getc_offset otherwise */
short b_reset(Buffer * const pBD) {
if (!pBD)
return RT_FAIL1;
pBD->getc_offset = pBD->markc_offset;
return pBD->getc_offset;
}
/* b_getcoffset: returns getc_offset, or returns -1 on error */
short b_getcoffset(Buffer * const pBD) {
if (!pBD)
return RT_FAIL1;
return pBD->getc_offset;
}
/* b_rewind: set getc_offset and markc_offset to 0, return -1 on error and 0 otherwise */
int b_rewind(Buffer * const pBD) {
if (!pBD)
return RT_FAIL1;
pBD->addc_offset = 0;
pBD->markc_offset = 0;
return 0;
}
/* b_location: returns a pointer to location indicated by loc_offset, returns null on error */
char * b_location(Buffer * const pBD, short loc_offset) {
if (!pBD)
return NULL;
return *loc_offset;
}
And finally my buffer.h:
/*File Name: buffer.h
* Version: 1.18.1
* Author: S^R
* Date: 16 January 2018
* Preprocessor directives, type declarations and prototypes necessary for buffer implementation
* as required for CST8152-Assignment #1.
* The file is not completed.
* You must add your function declarations (prototypes).
* You must also add your constant definitions and macros,if any.
*/
#ifndef BUFFER_H_
#define BUFFER_H_
/*#pragma warning(1:4001) *//*to enforce C89 type comments - to make //comments an warning */
/* standard header files */
#include <stdio.h> /* standard input/output */
#include <malloc.h> /* for dynamic memory allocation*/
#include <limits.h> /* implementation-defined data type ranges and limits */
/* constant definitions */
/* You may add your own constant definitions here */
#define RT_FAIL1 -1 /* fail return value */
#define RT_FAIL2 -2 /* fail return value */
#define LOAD_FAIL -2 /* load fail error */
#define SET_R_FLAG 1 /* realloc flag set value */
#define ADD_MODE 1; /* named constant for additive mode */
#define MULTI_MODE -1; /* named constant for multiplicative mode */
#define NEW_ONE 1; /* generic named constant value of 1 */
#define NEW_FULL 256; /* generic named constant value of 256 */
/* user data type declarations */
typedef struct BufferDescriptor {
char *cb_head; /* pointer to the beginning of character array (character buffer) */
short capacity; /* current dynamic memory size (in bytes) allocated to character buffer */
short addc_offset; /* the offset (in chars) to the add-character location */
short getc_offset; /* the offset (in chars) to the get-character location */
short markc_offset; /* the offset (in chars) to the mark location */
char inc_factor; /* character array increment factor */
char r_flag; /* reallocation flag */
char mode; /* operational mode indicator*/
int eob; /* end-of-buffer flag */
} Buffer, *pBuffer;
/* function declarations */
/*
Place your function declarations here.
Do not include the function header comments here.
Place them in the buffer.c file
*/
Buffer * b_allocate(short init_capacity, char inc_factor, char o_mode);
pBuffer b_addc(pBuffer const pBD, char symbol);
int b_clear(Buffer * const pBD);
void b_free(Buffer * const pBD);
int b_isfull(Buffer * const pBD);
short b_limit(Buffer * const pBD);
short b_capacity(Buffer * const pBD);
short b_mark(pBuffer const pBD, short mark);
int b_mode(Buffer * const pBD);
size_t b_incfactor(Buffer * const pBD);
int b_load(FILE * const fi, Buffer * const pBD);
int b_isempty(Buffer * const pBD);
int b_eob(Buffer * const pBD);
char b_getc(Buffer * const pBD);
int b_print(Buffer * const pBD);
Buffer * b_compact(Buffer * const pBD, char symbol);
char b_rflag(Buffer * const pBD);
short b_retract(Buffer * const pBD);
short b_reset(Buffer * const pBD);
short b_getcoffset(Buffer * const pBD);
int b_rewind(Buffer * const pBD);
char * b_location(Buffer * const pBD, short loc_offset);
#endif
So again, I need to find the location of loc_offset in my b_location function and return it as a pointer. Thanks in advance!
char * b_location(Buffer * const pBD, short loc_offset) {
if (!pBD)
return NULL;
/* Make sure loc_offset doesn't go beyond the length of the array */
if( ! in_bounds )
return NULL;
return &(pBD->cb_head[loc_offset]);
}
I am trying to get the flite speech synthesis library to work on my Mac, but my sound architecture isn't supported within the flite library. To fix that problem, I am using PortAudio to playback the synthesized audio; so I had to do a little bit of hacking within the audio.c file to get flite to use that library. I managed to get everything compiling just fine after mucking around in the GNU AutoTools for a while, but then I run the program and get this output:
$ ./flite -t "test"
frameIndex: 0
maxFrameIndex: 0
numChannels: 1
numSamples: 7225
sampleRate: 8000
=== Now playing back. ===
Waiting for playback to finish.
frameIndex in callback: -2008986336
maxFrameIndex in callback: 32655
numChannels in callback: 152579008
numSamples in callback: 0
sampleRate in callback: 0
Segmentation fault: 11
$ ./flite -t "test"
frameIndex: 0
maxFrameIndex: 0
numChannels: 1
numSamples: 7225
sampleRate: 8000
=== Now playing back. ===
Waiting for playback to finish.
frameIndex in callback: -71217888
maxFrameIndex in callback: 32712
numChannels in callback: 232979392
numSamples in callback: 0
sampleRate in callback: 0
Segmentation fault: 11
Here is the relevant code from the audio.c file, which is called when I supply the command line argument -t. I marked the area of interest where the segmentation fault occurs in the playCallback() function after a bit of debugging.
static int playCallback( const void *inputBuffer, void *outputBuffer,
unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags,
void *userData )
{
cst_wave *data = (cst_wave*)userData;
short *rptr = &data->samples[data->frameIndex * data->num_channels];
short *wptr = (short*)outputBuffer;
unsigned int i;
int finished;
unsigned int framesLeft = cst_wave_maxFrameIndex(data) - cst_wave_frameIndex(data);
(void) inputBuffer; /* Prevent unused variable warnings. */
(void) timeInfo;
(void) statusFlags;
(void) userData;
printf("frameIndex in callback: %d\n", cst_wave_frameIndex(data));
printf("maxFrameIndex in callback: %d\n", cst_wave_maxFrameIndex(data));
printf("numChannels in callback: %d\n", cst_wave_num_channels(data));
printf("numSamples in callback: %d\n", cst_wave_num_samples(data));
printf("sampleRate in callback: %d\n\n", cst_wave_sample_rate(data));
if( framesLeft < framesPerBuffer )
{
/* final buffer... */
for( i=0; i<framesLeft; i++ )
{
*wptr++ = *rptr++; /* left */
if( cst_wave_num_channels(data) == 2 ) *wptr++ = *rptr++; /* right */
}
for( ; i<framesPerBuffer; i++ )
{
*wptr++ = 0; /* left */
if( cst_wave_num_channels(data) == 2) *wptr++ = 0; /* right */
}
data->frameIndex += framesLeft;
finished = paComplete;
}
else
{
for( i=0; i<framesPerBuffer; i++ )
{
*wptr++ = *rptr++; /* left */
if( cst_wave_num_channels(data) == 2 ) *wptr++ = *rptr++; /* right */
}
cst_wave_set_frameIndex(data, framesPerBuffer);
finished = paContinue;
}
return finished;
}
int play_wave(cst_wave *w)
{
PaStream* stream;
PaStreamParameters outputParameters;
cst_wave_set_frameIndex(w, 0);
cst_wave_set_maxFrameIndex(w, (cst_wave_num_samples(w) / cst_wave_sample_rate(w)) * cst_wave_num_channels(w) * sizeof(short));
int err = 0;
err = Pa_Initialize();
outputParameters.device = Pa_GetDefaultOutputDevice();
if (outputParameters.device == paNoDevice)
{
fprintf(stderr,"Error: No default output device.\n");
return -5;
}
printf("frameIndex: %d\n", cst_wave_frameIndex(w));
printf("maxFrameIndex: %d\n", cst_wave_maxFrameIndex(w));
printf("numChannels: %d\n", cst_wave_num_channels(w));
printf("numSamples: %d\n", cst_wave_num_samples(w));
printf("sampleRate: %d\n", cst_wave_sample_rate(w));
outputParameters.channelCount = cst_wave_num_channels(w);
outputParameters.sampleFormat = paInt16;
outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
outputParameters.hostApiSpecificStreamInfo = NULL;
puts("=== Now playing back. ===");
err = Pa_OpenStream(&stream,
NULL, /* no input */
&outputParameters,
cst_wave_sample_rate(w),
512,
paClipOff,
playCallback,
&w);
if( stream )
{
err = Pa_StartStream( stream );
if( err != paNoError ) goto done;
puts("Waiting for playback to finish.");
while((err = Pa_IsStreamActive(stream)) == 1) Pa_Sleep(100);
if( err < 0 ) goto done;
err = Pa_CloseStream( stream );
if( err != paNoError ) goto done;
puts("Done.");
}
done:
Pa_Terminate();
free(cst_wave_samples(w));
}
Because it is relevant, I also slightly modified the cst_wave structure in cst_wave.h so that it contains my data I have to store, as well as adding a few #defines to the ones that were already present:
typedef struct cst_wave_struct {
const char *type;
int frameIndex;
int maxFrameIndex;
int sample_rate;
int num_samples;
int num_channels;
short *samples;
} cst_wave;
#define cst_wave_num_samples(w) (w?w->num_samples:0)
#define cst_wave_num_channels(w) (w?w->num_channels:0)
#define cst_wave_sample_rate(w) (w?w->sample_rate:0)
#define cst_wave_samples(w) (w->samples)
#define cst_wave_frameIndex(w) (w->frameIndex)
#define cst_wave_maxFrameIndex(w) (w->maxFrameIndex)
#define cst_wave_set_num_samples(w,s) w->num_samples=s
#define cst_wave_set_num_channels(w,s) w->num_channels=s
#define cst_wave_set_sample_rate(w,s) w->sample_rate=s
#define cst_wave_set_frameIndex(w,s) w->frameIndex=s
#define cst_wave_set_maxFrameIndex(w,s) w->maxFrameIndex=s
Update 1:
Following the advice of #Rohan now gives me this output:
$ ./bin/flite -t "test"
frameIndex: 0
maxFrameIndex: 0
numChannels: 1
numSamples: 7225
sampleRate: 8000
=== Now playing back. ===
Waiting for playback to finish.
frameIndex in callback: 0
maxFrameIndex in callback: 0
numChannels in callback: 1
numSamples in callback: 7225
sampleRate in callback: 8000
Done.
flite(68929,0x7fff71c0d310) malloc: *** error for object 0x7fd6e2809800: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Abort trap: 6
To fix that, I removed the free(cst_wave_samples(w));. Now the program executes normally with no visible errors, but there is still no audio output on my Mac. Any suggestions?
It looks to me like the problem is probably elsewhere.
The routine where you've added the comment is really pretty trivial when all is said and done. It's basically just copying a buffer full of data from one place to another, and if the data doesn't fill the input buffer, zero-filling the remainder. If I were writing the code, I'd probably do something more along these general lines:
const unsigned frame_size = sizeof(short) * data->num_channels;
char *source = &data->samples[data->frameIndex * data->num_channels];
char *dest = outputBuffer;
unsigned framesLeft = data->maxFrameIndex - data->frameIndex;
unsigned framesEmpty = framesPerBuffer - framesLeft;
memcpy(source, dest, framesLeft * frame_size);
memset(dest+framesLeft * frame_size, 0, framesEmpty * frame_size);
data->frameIndex += framesPerBuffer;
Although rather clumsily written, the if/else in the question just skips doing the memset part at all if the size that needs to be filled is zero.
So, this copies a buffer full of data from one place to another, and zero-fills any remainder. If you're getting a segfault, whatever's allocating the destination buffer apparently hasn't allocated enough space there. Without doing some looking, it's impossible to guess whether the allocation happens in Pa_Initialize, Pa_OpenStream, Pa_StartStream, or possibly somewhere else--and most likely you care less about the code that actually does the allocation than the code that computes how much space to allocate (which might be in one of the above, or somewhere else completely).
In your play_wave function you are calling:
err = Pa_OpenStream(&stream,
NULL, /* no input */
&outputParameters,
cst_wave_sample_rate(w),
512,
paClipOff,
playCallback,
&w);
Here as last parameter you are passing &w, so you are passing cst_wave ** as w is defined as cst_wave *w.
But in playCallback() you are using it as
cst_wave *data = (cst_wave*)userData;
So in this function you are incorrectly accessing cst_wave ** as cst_wave *. So at some point you will access the invalid memory while using some member of w.
Also, this is the reason you are getting the incorrect output for other parameters e.g. frameIndex, maxFrameIndex etc. as your output shows.
Solution is just pass w to Pa_OpenStream() function rather than &w.
Your next problem is that you aren't setting your maxFrameIndex correctly. As you stated in the comments, this shouldn't be 0. In order to set it properly, you should have something like this:
cst_wave_set_maxFrameIndex(w, cst_wave_num_samples(w) * cst_wave_num_channels(w));
Lastly, it looks like your callback could be messing things up a bit. Here is a better and more efficient way to write it:
static int playCallback( const void *inputBuffer, void *outputBuffer,
unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags,
void *userData )
{
cst_wave *data = (cst_wave*)userData;
short *rptr = &data->samples[data->frameIndex * data->num_channels];
short *wptr = (short*)outputBuffer;
int finished;
unsigned int framesLeft = data->maxFrameIndex - data->frameIndex;
(void) inputBuffer; /* Prevent unused variable warnings. */
(void) timeInfo;
(void) statusFlags;
(void) userData;
if( framesLeft < framesPerBuffer )
{
/* final buffer... */
memcpy(wptr, rptr, sizeof(*wptr) * data->num_channels * framesLeft);
memset(wptr, sizeof(*wptr) * data->num_channels * framesPerBuffer, 0);
data->frameIndex += framesLeft;
finished = paComplete;
}
else
{
memcpy(wptr, rptr, sizeof(*wptr) * data->num_channels * framesPerBuffer);
data->frameIndex += framesPerBuffer;
finished = paContinue;
}
return finished;
}
You're in luck. I was able to compile both PortAudio and flite on my own Mac, and solve your problem.
You have several issues other than those mentioned before, all of which I have addressed in the code dump below.
Minor: You don't use consistently your own API for cst_wave.
Minor: I prefer to enclose my while and if blocks with {} always. This has a habit of preventing mysterious bugs.
Max Frames was being set to zero. That's because in (cst_wave_num_samples(w) / cst_wave_sample_rate(w)) * cst_wave_num_channels(w) * sizeof(short), you were dividing by the sample rate, which was greater than your number of samples. Given that integer division is left-associative and truncating, yadda yadda yadda zero.
Max Frames is still wrong, as a frame includes all channel samples. The number of frames is thus agnostic to both number of channels and the size of the samples themselves. Allowing myself to guess that flite misuses sample to mean frame, your max frame index is just cst_wave_num_samples(w). Else it will be cst_wave_num_samples(w) / cst_wave_num_channels(w).
PortAudio's documentation states you should call Pa_StopStream(stream) after the stream becomes inactive, whether or not you were waiting until it became so.
I simplified the callback, and corrected it for
Minor: Consistent use of your API
MAJOR: Ehm... cst_wave_set_frameIndex(data, framesPerBuffer); is definitely wrong. You're pinning yourself at frame index 512 instead of incrementing! That's because you asked for 512 frames per buffer when opening the stream and you're not incrementing the frame index by framesPerBuffer, you're setting the frame index to framesPerBuffer. You hadn't made it that far because your maxFrameIndex was 0 anyways so you were exiting. I fixed it so that the frame index increments - with your API of course.
Here is the code, which I took the freedom of documenting and cleaning until it approached my standards of elegance. Enjoy!
#include <stdio.h>
#include <string.h>
/**
* Audio play callback.
*
* Follows the PaStreamCallback signature, wherein:
*
* #param input and
* #param output are either arrays of interleaved samples or; if
* non-interleaved samples were requested using the
* paNonInterleaved sample format flag, an array of buffer
* pointers, one non-interleaved buffer for each channel.
* #param frameCount The number of sample frames to be processed by the
* stream callback.
* #param timeInfo Timestamps indicating the ADC capture time of the first
* sample in the input buffer, the DAC output time of the
* first sample in the output buffer and the time the
* callback was invoked. See PaStreamCallbackTimeInfo and
* Pa_GetStreamTime()
* #param statusFlags Flags indicating whether input and/or output buffers
* have been inserted or will be dropped to overcome
* underflow or overflow conditions.
* #param userData The value of a user supplied pointer passed to
* Pa_OpenStream() intended for storing synthesis data
* etc.
*/
static int playCallback(const void* inputBuffer,
void* outputBuffer,
unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags,
void* userData){
(void) inputBuffer; /* Prevent unused variable warnings. */
(void) timeInfo;
(void) statusFlags;
(void) userData;
/**
* Compute current processing state.
*/
cst_wave* data;
short* rptr;
short* wptr;
unsigned int framesLeft, /* Number of frames of data remaining within the stream ***as a whole*** */
frames, /* Number of frames of data to be written for this buffer. */
framesPad, /* Number of frames of padding required within the final buffer. */
samples, /* Number of samples of data to be written for this buffer. */
samplesPad, /* Number of samples of padding required within the final buffer. */
numBytes, /* Number of bytes of data to be written for this buffer. */
numBytesPad;/* Number of bytes of padding required within the final buffer. */
int finalBuffer;/* Stores whether or not this is the final buffer. */
data = (cst_wave*)userData;
rptr = &data->samples[cst_wave_frameIndex (data) *
cst_wave_num_channels(data)];
wptr = (short*)outputBuffer;
framesLeft = cst_wave_maxFrameIndex(data) - cst_wave_frameIndex(data);
finalBuffer = framesLeft <= framesPerBuffer;
frames = finalBuffer ? framesLeft : framesPerBuffer;
framesPad = framesPerBuffer - frames;
samples = frames * cst_wave_num_channels(data);
samplesPad = framesPad * cst_wave_num_channels(data);
numBytes = samples * sizeof(short);
numBytesPad = samplesPad * sizeof(short);
/**
* Debug code. Comment out in production.
*/
printf("framesLeft in callback: %u\n", framesLeft);
printf("framesPerBuffer in callback: %lu\n", framesPerBuffer);
printf("frames in callback: %u\n", frames);
printf("frameIndex in callback: %d\n", cst_wave_frameIndex(data));
printf("maxFrameIndex in callback: %d\n", cst_wave_maxFrameIndex(data));
printf("numChannels in callback: %d\n", cst_wave_num_channels(data));
printf("numSamples in callback: %d\n", cst_wave_num_samples(data));
printf("sampleRate in callback: %d\n\n", cst_wave_sample_rate(data));
/**
* Output data. We handle the final buffer specially, padding it with zeros.
*/
memcpy(wptr, rptr, numBytes);
wptr += samples;
rptr += samples;
cst_wave_set_frameIndex(data, cst_wave_frameIndex(data) + frames);
memset(wptr, 0, numBytesPad);
wptr += samplesPad;
rptr += samplesPad;
/**
* Return a completion or continue code depending on whether this was the
* final buffer or not respectively.
*/
return finalBuffer ? paComplete : paContinue;
}
/**
* Play wave function.
*
* Plays the given cst_wave data as audio, blocking until this is done.
*/
int play_wave(cst_wave *w){
PaStream* stream;
PaStreamParameters outputParameters;
int err;
/**
* Initialize custom fields in cst_wave struct.
*/
cst_wave_set_frameIndex(w, 0);
cst_wave_set_maxFrameIndex(w, (cst_wave_num_samples(w)));
// / cst_wave_sample_rate(w) * cst_wave_num_channels(w) * sizeof(short)
/**
* Initialize Port Audio device and stream parameters.
*/
err = Pa_Initialize();
outputParameters.device = Pa_GetDefaultOutputDevice();
if (outputParameters.device == paNoDevice){
fprintf(stderr,"Error: No default output device.\n");
return -5;
}
printf("frameIndex: %d\n", cst_wave_frameIndex(w));
printf("maxFrameIndex: %d\n", cst_wave_maxFrameIndex(w));
printf("numChannels: %d\n", cst_wave_num_channels(w));
printf("numSamples: %d\n", cst_wave_num_samples(w));
printf("sampleRate: %d\n", cst_wave_sample_rate(w));
outputParameters.channelCount = cst_wave_num_channels(w);
outputParameters.sampleFormat = paInt16;
outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
outputParameters.hostApiSpecificStreamInfo = NULL;
/**
* Open the stream for playback.
*/
puts("=== Now playing back. ===");
err = Pa_OpenStream(&stream,
NULL, /* no input */
&outputParameters,
cst_wave_sample_rate(w),
512,
paClipOff,
playCallback,
w);
if(stream){
/**
* Start the stream.
*/
err = Pa_StartStream(stream);
if(err != paNoError){
goto done;
}
/**
* Block while it plays.
*/
puts("Waiting for playback to finish.");
while((err = Pa_IsStreamActive(stream)) == 1){
Pa_Sleep(100);
}
if(err < 0){
goto done;
}
/**
* Stop and close the stream. Both are necessary.
*/
Pa_StopStream(stream);
err = Pa_CloseStream(stream);
if(err != paNoError){
goto done;
}
puts("Done.");
}
/**
* Terminate and leave.
*/
done:
Pa_Terminate();
return 0;
}
For whatever reason, MinGW's gcc can't seem to find the '#define' block for this variable. The full error is
.../mapped.c:396:28: error: '_zzip_strcasecmp' undeclared (first use in this function)
Contents of mapped.c :
/*
* NOTE: this is part of libzzipmmapped (i.e. it is not libzzip).
* ==================
*
* These routines are fully independent from the traditional zzip
* implementation. They assume a readonly mmapped sharedmem block
* representing a complete zip file. The functions show how to
* parse the structure, find files and return a decoded bytestream.
*
* These routines are a bit simple and really here for documenting
* the way to access a zip file. The complexity of zip access comes
* from staggered reading of bytes and reposition of a filepointer in
* a big archive with lots of files and long compressed datastreams.
* Plus varaints of drop-in stdio replacements, obfuscation routines,
* auto fileextensions, drop-in dirent replacements, and so on...
*
* Author:
* Guido Draheim <guidod#gmx.de>
*
* Copyright (c) 2003,2004,2006 Guido Draheim
* All rights reserved,
* use under the restrictions of the
* Lesser GNU General Public License
* or alternatively the restrictions
* of the Mozilla Public License 1.1
*/
#define _ZZIP_DISK_FILE_STRUCT 1
#ifdef __linux__
#define _GNU_SOURCE _glibc_developers_are_idiots_to_call_strndup_gnu_specific_
#endif
#include <zzip/mmapped.h>
#include <zzip/format.h>
#include <zzip/fetch.h>
#include <zzip/__mmap.h>
#include <zzip/__fnmatch.h>
#include <stdlib.h>
#include <sys/stat.h>
#if defined ZZIP_HAVE_UNISTD_H
#include <unistd.h>
#elif defined ZZIP_HAVE_IO_H
#include <io.h>
#endif
#if defined ZZIP_HAVE_STRING_H
#include <string.h>
#elif defined ZZIP_HAVE_STRINGS_H
#include <strings.h>
#endif
#if __STDC_VERSION__+0 > 199900L
#define ___
#define ____
#else
#define ___ {
#define ____ }
#endif
/** => zzip_disk_mmap
* This function does primary initialization of a disk-buffer struct.
*/
int
zzip_disk_init(ZZIP_DISK* disk, void* buffer, zzip_size_t buflen)
{
disk->buffer = (zzip_byte_t*) buffer;
disk->endbuf = (zzip_byte_t*) buffer + buflen;
disk->reserved = 0;
disk->flags = 0;
disk->mapped = 0;
/* do not touch disk->user */
/* do not touch disk->code */
return 0;
}
/** => zzip_disk_mmap
* This function allocates a new disk-buffer with => malloc(3)
*/
zzip__new__ ZZIP_DISK*
zzip_disk_new(void)
{
ZZIP_DISK* disk = malloc(sizeof(disk));
if (! disk) return disk;
zzip_disk_init (disk, 0, 0);
return disk;
}
/** turn a filehandle into a mmapped zip disk archive handle
*
* This function uses the given file-descriptor to detect the length of the
* file and calls the system => mmap(2) to put it in main memory. If it is
* successful then a newly allocated ZZIP_DISK* is returned with
* disk->buffer pointing to the mapview of the zipdisk content.
*/
zzip__new__ ZZIP_DISK*
zzip_disk_mmap(int fd)
{
struct stat st;
if (fstat (fd, &st) || ! st.st_size) return 0;
___ ZZIP_DISK* disk = zzip_disk_new (); if (! disk) return 0;
disk->buffer = _zzip_mmap (& disk->mapped, fd, 0, st.st_size);
if (disk->buffer == MAP_FAILED) { free (disk); return 0; }
disk->endbuf = disk->buffer + st.st_size;
return disk; ____;
}
/** => zzip_disk_mmap
* This function is the inverse of => zzip_disk_mmap and using the system
* munmap(2) on the buffer area and => free(3) on the ZZIP_DISK structure.
*/
int
zzip_disk_munmap(ZZIP_DISK* disk)
{
if (! disk) return 0;
_zzip_munmap (disk->mapped, disk->buffer, disk->endbuf-disk->buffer);
free (disk);
return 0;
}
/** => zzip_disk_mmap
*
* This function opens the given archive by name and turn the filehandle
* to => zzip_disk_mmap for bringing it to main memory. If it can not
* be => mmap(2)'ed then we slurp the whole file into a newly => malloc(2)'ed
* memory block. Only if that fails too then we return null. Since handling
* of disk->buffer is ambigous it should not be snatched away please.
*/
ZZIP_DISK* zzip__new__
zzip_disk_open(char* filename)
{
# ifndef O_BINARY
# define O_BINARY 0
# endif
struct stat st;
if (stat (filename, &st) || ! st.st_size) return 0;
___ int fd = open (filename, O_RDONLY|O_BINARY);
if (fd <= 0) return 0;
___ ZZIP_DISK* disk = zzip_disk_mmap (fd);
if (disk) return disk;
___ zzip_byte_t* buffer = malloc (st.st_size);
if (! buffer) return 0;
if ((st.st_size == read (fd, buffer, st.st_size)) &&
(disk = zzip_disk_new ()))
{
disk->buffer = buffer;
disk->endbuf = buffer+st.st_size;
disk->mapped = -1;
}else free (buffer);
return disk; ____;____;____;
}
/** => zzip_disk_mmap
*
* This function will release all data needed to access a (mmapped)
* zip archive, including any malloc()ed blocks, sharedmem mappings
* and it dumps the handle struct as well.
*/
int
zzip_disk_close(ZZIP_DISK* disk)
{
if (! disk) return 0;
if (disk->mapped != -1) return zzip_disk_munmap (disk);
free (disk->buffer);
free (disk);
return 0;
}
/* ====================================================================== */
/* helper functions */
#ifdef ZZIP_HAVE_STRNDUP
#define _zzip_strndup strndup
#else
/* if your system does not have strndup: */
zzip__new__ static char* _zzip_strndup(char* p, size_t maxlen)
{
if (! p) return 0;
___ zzip_byte_t* r = malloc (maxlen+1);
if (! r) return r;
strncpy (r, p, maxlen);
r[maxlen] = '\0';
return r; ____;
}
#endif
#if defined ZZIP_HAVE_STRCASECMP || defined strcasecmp
#define _zzip_strcasecmp strcasecmp
#else
/* if your system does not have strcasecmp: */
static int _zzip_strcasecmp(char* __zzip_restrict a, char* _zzip_restrict b)
{
if (! a) return (b) ? 1 : 0;
if (! b) return -1;
while (1)
{
int v = tolower(*a) - tolower(*b);
if (v) return v;
if (! *a) return 1;
if (! *b) return -1;
a++; b++;
}
}
#endif
/** helper functions for (mmapped) zip access api
*
* This function augments the other zzip_disk_entry_* helpers: here we move
* a disk_entry pointer (as returned by _find* functions) into a pointer to
* the data block right after the file_header. Only disk->buffer would be
* needed to perform the seek but we check the mmapped range end as well.
*/
zzip_byte_t*
zzip_disk_entry_to_data(ZZIP_DISK* disk, struct zzip_disk_entry* entry)
{
struct zzip_file_header* file =
zzip_disk_entry_to_file_header(disk, entry);
if (file) return zzip_file_header_to_data (file);
return 0;
}
/** => zzip_disk_entry_to_data
* This function does half the job of => zzip_disk_entry_to_data where it
* can augment with => zzip_file_header_to_data helper from format/fetch.h
*/
struct zzip_file_header*
zzip_disk_entry_to_file_header(ZZIP_DISK* disk, struct zzip_disk_entry* entry)
{
zzip_byte_t* file_header = /* (struct zzip_file_header*) */
(disk->buffer + zzip_disk_entry_fileoffset (entry));
if (disk->buffer > file_header || file_header >= disk->endbuf)
return 0;
return (struct zzip_file_header*) file_header;
}
/** => zzip_disk_entry_to_data
* This function is a big helper despite its little name: in a zip file the
* encoded filenames are usually NOT zero-terminated but for common usage
* with libc we need it that way. Secondly, the filename SHOULD be present
* in the zip central directory but if not then we fallback to the filename
* given in the file_header of each compressed data portion.
*/
zzip__new__ char*
zzip_disk_entry_strdup_name(ZZIP_DISK* disk, struct zzip_disk_entry* entry)
{
if (! disk || ! entry) return 0;
___ char* name; zzip_size_t len;
struct zzip_file_header* file;
if ((len = zzip_disk_entry_namlen (entry)))
name = zzip_disk_entry_to_filename (entry);
else if ((file = zzip_disk_entry_to_file_header (disk, entry)) &&
(len = zzip_file_header_namlen (file)))
name = zzip_file_header_to_filename (file);
else
return 0;
if ((zzip_byte_t*) name < disk->buffer ||
(zzip_byte_t*) name+len > disk->endbuf)
return 0;
return _zzip_strndup (name, len); ____;
}
/** => zzip_disk_entry_to_data
* This function is similar creating a reference to a zero terminated
* string but it can only exist in the zip central directory entry.
*/
zzip__new__ char*
zzip_disk_entry_strdup_comment(ZZIP_DISK* disk, struct zzip_disk_entry* entry)
{
if (! disk || ! entry) return 0;
___ char* text; zzip_size_t len;
if ((len = zzip_disk_entry_comment (entry)))
text = zzip_disk_entry_to_comment (entry);
else
return 0;
if ((zzip_byte_t*) text < disk->buffer ||
(zzip_byte_t*) text+len > disk->endbuf)
return 0;
return _zzip_strndup (text, len); ____;
}
/* ====================================================================== */
/** => zzip_disk_findfile
*
* This function is the first call of all the zip access functions here.
* It contains the code to find the first entry of the zip central directory.
* Here we require the mmapped block to represent a real zip file where the
* disk_trailer is _last_ in the file area, so that its position would be at
* a fixed offset from the end of the file area if not for the comment field
* allowed to be of variable length (which needs us to do a little search
* for the disk_tailer). However, in this simple implementation we disregard
* any disk_trailer info telling about multidisk archives, so we just return
* a pointer to the zip central directory.
*
* For an actual means, we are going to search backwards from the end
* of the mmaped block looking for the PK-magic signature of a
* disk_trailer. If we see one then we check the rootseek value to
* find the first disk_entry of the root central directory. If we find
* the correct PK-magic signature of a disk_entry over there then we
* assume we are done and we are going to return a pointer to that label.
*
* The return value is a pointer to the first zzip_disk_entry being checked
* to be within the bounds of the file area specified by the arguments. If
* no disk_trailer was found then null is returned, and likewise we only
* accept a disk_trailer with a seekvalue that points to a disk_entry and
* both parts have valid PK-magic parts. Beyond some sanity check we try to
* catch a common brokeness with zip archives that still allows us to find
* the start of the zip central directory.
*/
struct zzip_disk_entry*
zzip_disk_findfirst(ZZIP_DISK* disk)
{
if (disk->buffer > disk->endbuf-sizeof(struct zzip_disk_trailer))
return 0;
___ zzip_byte_t* p = disk->endbuf-sizeof(struct zzip_disk_trailer);
for (; p >= disk->buffer ; p--)
{
zzip_byte_t* root; /* (struct zzip_disk_entry*) */
if (zzip_disk_trailer_check_magic(p)) {
root = disk->buffer + zzip_disk_trailer_get_rootseek (
(struct zzip_disk_trailer*)p);
if (root > p)
{ /* the first disk_entry is after the disk_trailer? can't be! */
zzip_size_t rootsize = zzip_disk_trailer_get_rootsize (
(struct zzip_disk_trailer*)p);
if (disk->buffer+rootsize > p) continue;
/* a common brokeness that can be fixed: we just assume the
* central directory was written directly before the trailer:*/
root = p - rootsize;
}
} else if (zzip_disk64_trailer_check_magic(p)) {
if (sizeof(void*) < 8) return 0; /* EOVERFLOW */
root = disk->buffer + zzip_disk64_trailer_get_rootseek (
(struct zzip_disk64_trailer*)p);
if (root > p) continue;
} else continue;
if (root < disk->buffer) continue;
if (zzip_disk_entry_check_magic(root))
return (struct zzip_disk_entry*) root;
}____;
return 0;
}
/** => zzip_disk_findfile
*
* This function takes an existing disk_entry in the central root directory
* (e.g. from zzip_disk_findfirst) and returns the next entry within in
* the given bounds of the mmapped file area.
*/
struct zzip_disk_entry*
zzip_disk_findnext(ZZIP_DISK* disk, struct zzip_disk_entry* entry)
{
if ((zzip_byte_t*)entry < disk->buffer ||
(zzip_byte_t*)entry > disk->endbuf-sizeof(entry) ||
! zzip_disk_entry_check_magic (entry) ||
zzip_disk_entry_sizeto_end (entry) > 64*1024)
return 0;
entry = zzip_disk_entry_to_next_entry (entry);
if ((zzip_byte_t*)entry > disk->endbuf-sizeof(entry) ||
! zzip_disk_entry_check_magic (entry) ||
zzip_disk_entry_sizeto_end (entry) > 64*1024 ||
zzip_disk_entry_skipto_end (entry) + sizeof(entry) > disk->endbuf)
return 0;
else
return entry;
}
/** search for files in the (mmapped) zip central directory
*
* This function is given a filename as an additional argument, to find the
* disk_entry matching a given filename. The compare-function is usually
* strcmp or strcasecmp or perhaps strcoll, if null then strcmp is used.
* - use null as argument for "after"-entry when searching the first
* matching entry, otherwise the last returned value if you look for other
* entries with a special "compare" function (if null then a doubled search
* is rather useless with this variant of _findfile).
*/
struct zzip_disk_entry*
zzip_disk_findfile(ZZIP_DISK* disk, char* filename,
struct zzip_disk_entry* after, zzip_strcmp_fn_t compare)
{
struct zzip_disk_entry* entry = (! after ? zzip_disk_findfirst (disk)
: zzip_disk_findnext (disk, after));
if (! compare)
compare = (zzip_strcmp_fn_t)( (disk->flags&1) ?
(_zzip_strcasecmp) : (strcmp));
for (; entry ; entry = zzip_disk_findnext (disk, entry))
{
/* filenames within zip files are often not null-terminated! */
char* realname = zzip_disk_entry_strdup_name (disk, entry);
if (realname && ! compare(filename, realname))
{
free (realname);
return entry;
}
free (realname);
}
return 0;
}
/** => zzip_disk_findfile
*
* This function uses a compare-function with an additional argument
* and it is called just like fnmatch(3) from POSIX.2 AD:1993), i.e.
* the argument filespec first and the ziplocal filename second with
* the integer-flags put in as third to the indirect call. If the
* platform has fnmatch available then null-compare will use that one
* and otherwise we fall back to mere strcmp, so if you need fnmatch
* searching then please provide an implementation somewhere else.
* - use null as argument for "after"-entry when searching the first
* matching entry, or the last disk_entry return-value to find the
* next entry matching the given filespec.
*/
struct zzip_disk_entry*
zzip_disk_findmatch(ZZIP_DISK* disk, char* filespec,
struct zzip_disk_entry* after,
zzip_fnmatch_fn_t compare, int flags)
{
struct zzip_disk_entry* entry = (! after ? zzip_disk_findfirst (disk)
: zzip_disk_findnext (disk, after));
if (! compare) {
compare = (zzip_fnmatch_fn_t) _zzip_fnmatch;
if (disk->flags&1) disk->flags |= _zzip_fnmatch_CASEFOLD;
}
for (; entry ; entry = zzip_disk_findnext (disk, entry))
{
/* filenames within zip files are often not null-terminated! */
char* realname = zzip_disk_entry_strdup_name(disk, entry);
if (realname && ! compare(filespec, realname, flags))
{
free (realname);
return entry;
}
free (realname);
}
return 0;
}
/* ====================================================================== */
/** => zzip_disk_fopen
*
* the ZZIP_DISK_FILE* is rather simple in just encapsulating the
* arguments given to this function plus a zlib deflate buffer.
* Note that the ZZIP_DISK pointer does already contain the full
* mmapped file area of a zip disk, so open()ing a file part within
* that area happens to be a lookup of its bounds and encoding. That
* information is memorized on the ZZIP_DISK_FILE so that subsequent
* _read() operations will be able to get the next data portion or
* return an eof condition for that file part wrapped in the zip archive.
*/
zzip__new__ ZZIP_DISK_FILE*
zzip_disk_entry_fopen (ZZIP_DISK* disk, ZZIP_DISK_ENTRY* entry)
{
/* keep this in sync with zzip_mem_entry_fopen */
struct zzip_file_header* header =
zzip_disk_entry_to_file_header (disk, entry);
if (! header) return 0;
___ ZZIP_DISK_FILE* file = malloc(sizeof(ZZIP_DISK_FILE));
if (! file) return file;
file->buffer = disk->buffer;
file->endbuf = disk->endbuf;
file->avail = zzip_file_header_usize (header);
if (! file->avail || zzip_file_header_data_stored (header))
{ file->stored = zzip_file_header_to_data (header); return file; }
file->stored = 0;
file->zlib.opaque = 0;
file->zlib.zalloc = Z_NULL;
file->zlib.zfree = Z_NULL;
file->zlib.avail_in = zzip_file_header_csize (header);
file->zlib.next_in = zzip_file_header_to_data (header);
if (! zzip_file_header_data_deflated(header) ||
inflateInit2(&file->zlib, -MAX_WBITS) != Z_OK)
{ free (file); return 0; }
return file;
____;
}
/** openening a file part wrapped within a (mmapped) zip archive
*
* This function opens a file found by name, so it does a search into
* the zip central directory with => zzip_disk_findfile and whatever
* is found first is given to => zzip_disk_entry_fopen
*/
zzip__new__ ZZIP_DISK_FILE*
zzip_disk_fopen (ZZIP_DISK* disk, char* filename)
{
ZZIP_DISK_ENTRY* entry = zzip_disk_findfile (disk, filename, 0, 0);
if (! entry) return 0; else return zzip_disk_entry_fopen (disk, entry);
}
/** => zzip_disk_fopen
*
* This function reads more bytes into the output buffer specified as
* arguments. The return value is null on eof or error, the stdio-like
* interface can not distinguish between these so you need to check
* with => zzip_disk_feof for the difference.
*/
zzip_size_t
zzip_disk_fread (void* ptr, zzip_size_t sized, zzip_size_t nmemb,
ZZIP_DISK_FILE* file)
{
zzip_size_t size = sized*nmemb;
if (size > file->avail) size = file->avail;
if (file->stored)
{
memcpy (ptr, file->stored, size);
file->stored += size;
file->avail -= size;
return size;
}
file->zlib.avail_out = sized*nmemb;
file->zlib.next_out = ptr;
___ zzip_size_t total_old = file->zlib.total_out;
___ int err = inflate (& file->zlib, Z_NO_FLUSH);
if (err == Z_STREAM_END)
file->avail = 0;
else if (err == Z_OK)
file->avail -= file->zlib.total_out - total_old;
else
return 0;
return file->zlib.total_out - total_old;
____;____;
}
/** => zzip_disk_fopen
* This function releases any zlib decoder info needed for decompression
* and dumps the ZZIP_DISK_FILE* then.
*/
int
zzip_disk_fclose (ZZIP_DISK_FILE* file)
{
if (! file->stored)
inflateEnd (& file->zlib);
free (file);
return 0;
}
/** => zzip_disk_fopen
*
* This function allows to distinguish an error from an eof condition.
* Actually, if we found an error but we did already reach eof then we
* just keep on saying that it was an eof, so the app can just continue.
*/
int
zzip_disk_feof (ZZIP_DISK_FILE* file)
{
return ! file || ! file->avail;
}
Does anyone have any idea what's going on? Sorry if it may look obvious to some of you, I'm a newbie with this stuff.
Thanks!
Edit: Since I couldn't figure what's wrong, I decided to use the visual studio solution that came with the source code and I finally managed to build it successfully. Thanks to everyone who tried to help me with this.
The reason for your error is that in C language names enclosed in () are not treated as macros. That's actually a feature of the language that exists specifically for that purpose: to give you an opportunity to "ignore" macro definitions when you want to.
When you attempt to compile the code with GCC, it appears that strcasecmp is available. The true branch of the #if is taken by the compiler, which means that _zzip_strcasecmp gets defined as a macro
#define _zzip_strcasecmp strcasecmp
This will work fine as long as you call it as
_zzip_strcasecmp( /* whatever */ )
But this will not work when you refer to it as (_zzip_strcasecmp) here
compare = (zzip_strcmp_fn_t)( (disk->flags&1) ?
(_zzip_strcasecmp) : (strcmp));
The compiler sees (_zzip_strcasecmp) as a request to ignore macros when looking for _zzip_strcasecmp. The compiler will look for non-macro definition of _zzip_strcasecmp and fail with the error message, since such definition does not exist.
When you compile in Visual Siudio, strcasecmp is not available. The false branch of the #if is taken and _zzip_strcasecmp gets defined internally as an ordinary function. So, everything works fine.
One way to fix it for macro version is to remove these excessive () around function names in the problematic erroneous statement
compare = (zzip_strcmp_fn_t)( (disk->flags&1) ?
_zzip_strcasecmp : strcmp);
This should compile in GCC assuming strcasecmp is really available there.
I don't know why they put these superfluous () in the original version. Might be an instance of "rampant parenthesizing" and misguided "better safe than sorry" sentiment. Might actually be done intentionally to prevent use of macros in this context (in which case the bad guy is whoever wrote that #if).
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I seem to be going in circles with this project! I get so many errors of 'dereferencing pointer to incomplete type', and have quite a few others. it seems like when I fix one another one will pop up to take its place!
It's my first time using hash tables, and I admit that I am rather lost but I think I made a very good start at least. Any input as to how to solve my 'dereferencing pointer to incomplete type' problems would be amazing!
htable.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "htable.h"
struct htablerec {
int size;
int num_entries;
hashing_t method;
char **keys;
int *freqs;
int *stats;
};
void *emalloc(size_t s) {
void *result = malloc(s);
if (NULL == result) {
fprintf(stderr, "Memory allocation failed!\n");
exit(EXIT_FAILURE);
}
return result;
}
/*moves pointer to point to something appropriate*/
htable htable_new(int capacity) {
int i;
htable ht = emalloc(sizeof *ht);
ht->size = size;
ht->method = method;
ht->num_keys = 0;
ht->keys = emalloc(size * sizeof ht->keys[0]);
ht->freqs = emalloc(size * sizeof ht->freqs[0]);
ht->stats = emalloc(size * sizeof ht->stats[0]);
for(i = 0; i<size; i++){
ht->keys[i] = NULL;
ht->freqs[i] = 0;
ht->stats[i] = 0;
}
return ht;
}
static unsigned int htable_step(htable h, unsigned int i_key){
return 1 + (i_key % (h->size - 1));
}
static unsigned int htable_wtoi(char *word){
unsigned int result = 0;
while(*word != '\0') result = (*word++ +31 * result);
return result;
}
static unsigned int htable_hash(htable h, unsigned int i_key){
return i_key % h->size;
}
void htable_free(htable h) {
int i;
for(i = 0; i<h->size; i++){
if(h->keys[i] != NULL){
free(h->keys[i]);
}
}
if(h->keys != NULL){
free(h->keys);
}
if(h->freqs != NULL){
free(h->freqs);
}
free(h);
}
static unsigned int htable_word_to_int(char *word) {
unsigned int result = 0;
while (*word != '\0') {
result = (*word++ + 31 * result);
}
return result;
}
int htable_insert(htable h, char *str) {
int num_collisions = 0;
int i_key = htable_wtoi(key);
int pos = htable_hash(h, i_key);
int step = 1;
if(h->method == DOUBLE)
step = htable_step(h, i_key);
while(h->keys[pos]!=NULL &&
strcmp(h->keys[pos],key)!=0 &&
num_collisions < h->size ){
pos = htable_hash(h, pos + step);
num_collisions++;
}
if(h->keys[pos] == NULL){
h->keys[pos] = emalloc((strlen(key)+1) * sizeof h->keys[0][0]);
strcpy(h->keys[pos],key);
h->stats[h->num_keys] = num_collisions;
h->num_keys++;
}
if(num_collisions >= h->size) /* We must be full, so return zero.*/
return 0;
return ++(h->freqs[pos]);
}
static int htable_search(htable h, char *key){
int num_collisions = 0;
int i_key = htable_wtoi(key);
int pos = htable_hash(h, i_key);
int step = 1;
if(h->method == DOUBLE)
step = htable_step(h, i_key);
while(h->keys[pos]!=NULL &&
strcmp(h->keys[pos],key)!=0 &&
num_collisions < h->size ){
pos = htable_hash(h, pos + step);
num_keys++;
}
if(num_keys >= h->size)
return 0;
else
return h->freqs[pos];
}
void htable_print(htable h, FILE *stream){
int i;
for(i = 0; i<h->size; i++){
if(h->keys[i] != NULL)
fprintf(stream, "%d\t%s\n",i, h->freqs[i], h->keys[i]);
}
}
void htable_print_entire_table(htable h, FILE *stream) {
int i;
for (i=0; loop < h->capacity; i++) {
if (h->key[i] != NULL) {
fprintf("%d\t%s\n", h->freqs[i], h->key[i]);
}
}
}
/**
* Prints a line of data indicating the state of the hash table when
* it is a given percentage full.
*
* The data is printed out right justified (with the given field widths,
* and decimal places) in this order:
*
* - How full the hash-table is as a percentage (4)
* - How many keys are in the hash-table at that point (11)
* - What percentage of those keys were placed 'at home' (12, 1 dp)
* - The average number of collisions per key placed (12, 2 dp)
* - The maximum number of collisions while placing a key (12)
*
* #param h the hash-table to get data from.
* #param stream the place to send output to.
* #param percent_full the point at which to print the statistics.
* If the hashtable is less full than that, then
* nothing will be printed.
*/
static void print_stats_line(htable h, FILE *stream, int percent_full) {
int current_entries = h->capacity * percent_full / 100;
double average_collisions = 0.0;
int at_home = 0;
int max_collisions = 0;
int i = 0;
if (current_entries > 0 && current_entries <= h->num_keys) {
for (i = 0; i < current_entries; i++) {
if (h->stats[i] == 0) {
at_home++;
}
if (h->stats[i] > max_collisions) {
max_collisions = h->stats[i];
}
average_collisions += h->stats[i];
}
fprintf(stream, "%4d%11d%12.1f%12.2f%12d\n", percent_full,
current_entries, at_home * 100.0 / current_entries,
average_collisions / current_entries, max_collisions);
}
}
void htable_print_stats(htable ht, FILE *stream, int num_stats) {
int i;
fprintf(stream, "Percent Current Percent Average Maximum\n");
fprintf(stream, " Full Entries At Home Collisions Collisions\n");
fprintf(stream, "-----------------------------------------------------\n");
for (i = 1; i <= num_stats; i++) {
print_stats_line(ht, stream, 100 * i / num_stats);
}
fprintf(stream, "-----------------------------------------------------\n\n");
}
htable.h
#ifndef HTABLE_H
#define HTABLE_H
#include <stdio.h>
typedef struct hashtable *htable;
typedef enum hashing_e { LINEAR, DOUBLE } hashing_t;
extern htable htable_new(int size);
extern void htable_destroy(htable ht);
extern int htable_insert(htable h, char *key);
extern int htable_search(htable h, char *key);
extern void htable_print(htable h, FILE *stream);
extern void htable_print_stats(htable ht, FILE *stream, int num_stats);
#endif
main.c (given by tutor for project to fit)
/**
* #file main.c
* #author Iain Hewson
* #date August 2012
*
* This program is written to test the hash table ADT specified in
* Cosc242 assignment two. It creates a hash table which can use
* linear-probing or double-hashing as a collision resolution
* strategy. Various options are provided which make it possible to
* examine a hash table as well as see how it performs while being
* filled.
*/
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include "mylib.h"
#include "htable.h"
/* A boolean type which can be TRUE or FALSE */
typedef enum bool_e {FALSE, TRUE} bool_t;
/* function declarations */
static void usage(char *progname);
static void setup(int argc, char **argv, bool_t *double_hashing,
bool_t *entire_table, bool_t *print_stats,
int *snapshots, int *tablesize);
/**
*
* Creates a hash-table and inserts words into it read from stdin.
* Arguments on the command line alter the behaviour of the program
* as follows:
* - -d Use double hashing (linear probing is the default)
* - -e Display entire contents of hash-table on stderr
* - -n NUM Show NUM statistics snapshots (if -p is used)
* - -p Print stats info instead of frequencies & words
* - -s SIZE Use the first prime >= SIZE as htable size
* - -h Display this message
*
* By default each word and it's frequency are printed to stdout.
*
* #param argc the number of command-line arguments.
* #param argv an array of strings containing the command-line arguments.
*
* #return EXIT_SUCCESS if the program is successful.
*/
int main(int argc,char **argv) {
bool_t entire_table = FALSE, double_hashing = FALSE, print_stats = FALSE;
int tablesize = 0, snapshots = 0;
char word[256];
htable ht;
setup(argc, argv, &double_hashing, &entire_table, &print_stats,
&snapshots, &tablesize);
ht = htable_new(tablesize, (double_hashing) ? DOUBLE_H : LINEAR_P);
while (getword(word, sizeof word, stdin) != EOF) {
htable_insert(ht, word);
}
if (entire_table) {
htable_print_entire_table(ht, stderr);
}
if (print_stats) {
htable_print_stats(ht, stdout, snapshots);
} else {
htable_print(ht, stdout); /* print words and frequencies */
}
htable_free(ht);
return EXIT_SUCCESS;
}
/**
* Prints out a usage message to stderr outlining all of the options.
* #param prog_name the name of the program to include in usage message.
*/
static void usage(char *prog_name) {
fprintf(stderr, "Usage: %s [OPTION]... <STDIN>\n\n%s%s", prog_name,
"Perform various operations using a hash-table. By default read\n"
"words from stdin and print them with their frequencies to stdout.\n\n"
" -d Use double hashing (linear probing is the default)\n"
" -e Display entire contents of hash-table on stderr\n",
" -n NUM Show NUM stats snapshots (if -p is used)\n"
" -p Print stats info instead of frequencies & words\n"
" -s SIZE Use the first prime >= SIZE as htable size\n\n"
" -h Display this message\n\n");
}
/**
* Handle options given on the command-line by setting a number of
* variables appropriately. May call usage() if incorrect arguments
* or -h given.
*
* #param argc the number of command-line arguments.
* #param argv an array of strings contain the command-line arguments.
* #param double_hashing set to TRUE if -d given
* #param entire_table set to TRUE if -e given
* #param snapshots set to NUM if -n NUM given and NUM > 0 else set to 10
* #param print_stats set to TRUE if -p given
* #param tablesize set to SIZE if -t SIZE given and SIZE > 0 else set to 113
*/
static void setup(int argc, char **argv, bool_t *double_hashing,
bool_t *entire_table, bool_t *print_stats,
int *snapshots, int *tablesize) {
const char *optstring = "dehpn:s:";
char option;
while ((option = getopt(argc, argv, optstring)) != EOF) {
switch (option) {
case 'd':
*double_hashing = TRUE;
break;
case 'e':
*entire_table = TRUE;
break;
case 'p':
*print_stats = TRUE;
break;
case 'n':
*snapshots = atoi(optarg);
break;
case 's':
*tablesize = atoi(optarg);
break;
case 'h':
default:
usage(argv[0]);
exit(EXIT_SUCCESS);
}
}
/* set default values if nothing sensible entered */
if (*tablesize < 1) *tablesize = 113;
if (*snapshots < 1) *snapshots = 10;
}
mylib.c
#include <stdlib.h>
#include "mylib.h"
/**************************
* *
* emalloc *
* *
**************************
Used to handle new memory allocation to a pointer and handle exceptions
which may arrise if memory allocation fails, which happens all the time
when you try and enter negative values.
PARAMETERS: s = calculated size of required memory.
RETURN VALUE: a pointer of any type.
*/
void *emalloc(size_t s){
void *result = malloc(s);
if(NULL == result){
fprintf(stderr, "Memory allocation failed!\n");
exit(EXIT_FAILURE);
}
return result;
}
/**************************
* *
* erealloc *
* *
**************************
Handles the reallocation of an updated amount of memory to an existing
with existing data attached.
PARAMETERS: p = the existing pointer we would like additional memory
allocated to.
s = calculated size of required memory.
RETURN VALUE: a pointer of any type.
*/
void *erealloc(void *p, size_t s){
void *result = realloc(p,s);
if(NULL == result){
fprintf(stderr, "Memory allocation failed!\n");
exit(EXIT_FAILURE);
}
return result;
}
/**************************
* *
* getword *
* *
**************************
Is used to read input from the designated file stream (standard in for the
assignment). Getword removes white space with the first while loop. And only
returns one word. Maintaining continous input is therefore the responsibility
of the calling function.
PARAMETERS: s = the pointer to the character array.
limit = the maximum size a word can be.
stream = where to read from.
RETURN VALUE: the integer value of the character at s[0]. The string s does
not need to be returned, since it is an array and is passed as
a memory address. If no chars were read into the string s, then
s[0] would have the '\0' [NULL] value which equates to 0 if used
in a boolean equation.
*/
int getword(char *s, int limit, FILE *stream){
int c;
while(!isalnum( c = getc(stream)) && c != EOF);
if(c == EOF)
return EOF;
else
*s++ = tolower(c);
while(--limit > 0){
if(isalnum(c = getc(stream)))
*s++ = tolower(c);
else if('\'' == c) continue;
else break;
}
*s = '\0';
return s[0];
}
/**************************
* *
* stoi *
* *
**************************
Not using this function now.
PARAMETERS: s = string representation of a number.
RETURN VALUE: the integer value.
*/
int stoi(char *s){
int i,j,r;
int sign = 1;
i=1;
r=0;
for(j=my_strlen(s)-1; j>=0; j--){
if(j == 0 && s[j] == '-')
sign = -1;
else {
if(s[j]>='0' && s[j]<='9'){
r+=((s[j]-'0')*i);
i*=10;
} else {
fprintf(stderr, "Invalid input for String to int\n");
exit(EXIT_FAILURE);
}
}
}
return r * sign;
}
/**************************
* *
* my_strlen *
* *
**************************
I am using my own string length function, but I wrote it with the stoi
function, so am using it here. Perhaps not as safe as the library
functions?
PARAMETERS: s = a string delimited by the NULL character.
RETURN VALUE: the number of characters in the string.
*/
int my_strlen(char *s){
int i=0;
while(s[i]!='\0')
i++;
return i;
}
/**************************
* *
* factors *
* *
**************************
Another unrequired function used to calculate the possiblity of factorisation.
PARAMETERS: x = An integer to be factored towards.
RETURN VALUE: 0 if x has factors, 1 if x is a prime number.
*/
static int factors(int x){
int f = 2;
while(f*f < x){
if(x % f == 0){
return 0;
} else {
f++;
}
}
return 1;
}
/**************************
* *
* prime_gt *
* *
**************************
Used in conjunction with factors to find factorless integers. We increment
bound until it is truely prime.
Bound - We start with bound, sending it to the factors function. If it is
a prime number, then stop searching. Otherwise loop until we find
an prime integer larger than the input integer.
PARAMETERS: s = the input integer.
RETURN VALUE: Bound, for it is now a prime number.
*/
static int prime_gt(int s){
int bound = s;
while(bound > 0){
if(factors(bound))
break;
else
bound++;
}
return bound;
}
/**************************
* *
* relative_prime *
* *
**************************
Decides on a prime number to use to set the table size to.
PARAMETERS: s = the required size of the table.
RETURN VALUE: the newer beter prime number size for the table.
*/
unsigned int relative_prime(int s){
return prime_gt(s);
}
Sorry for it being so big, it's ok if it's just a complete unfixable jumble.
You don't seem to have defined struct hashtable anywhere. You need to say somewhere what fields that struct should actually contain, at the moment there is only a forward declaration in htable.h.
Such a forward declaration just says that the type exists, but not how it exactly looks like. Therefore it is considered an incomplete type, until the compiler sees a full definition of it.
Cause the definition of struct hashtable does not exist, the following line creates an alias of an incomplete pointer type (htable).
typedef struct hashtable *htable;
It is impossible to deference such pointer (the compiler doesn't know the different fields of your structure).
In your file htable.c, maybe you meant :
/* instead of struct htablered */
struct htable {
int size;
int num_entries;
hashing_t method;
char **keys;
int *freqs;
int *stats;
};