ffmpeg, 'protocol not found' error - c

i'm using next code:
const char *sFileOutput;
AVOutputFormat *ofmt;
AVFormatContext *ofcx;
int main( int argc, char* argv[] )
{
av_log_set_level( AV_LOG_DEBUG );
av_register_all();
avcodec_register_all();
avformat_network_init();
char s1[40]={0};
const time_t timer = time(NULL);
u = localtime(&timer);
strftime(s1, 40, "%d.%m.%Y-%H:%M:%S.avi", u);
sFileOutput=&s1;
//char *sFileOutput = "01.01.2017-23.23.23.avi";
ofmt = av_guess_format( NULL, sFileOutput, NULL );
ofcx = avformat_alloc_context();
ofcx->oformat = ofmt;
int ret2=avio_open( &ofcx->pb, sFileOutput, AVIO_FLAG_WRITE);
if(ret2<0){
fprintf(stderr, "\nError occurred when opening output file: %s\n",av_err2str(ret2));
}
}
When i run it, i have error in console:
Error occurred when opening output file: Protocol not found
but if i uncomment string
char *sFileOutput = "01.01.2017-23.23.23.avi";
evirything is ok, progam is working without errors. please tell me what is wrong.

thank you for your answer, it helps me too.
But real problem was that generated name contained ':'. i changed string to
strftime(s1, 40, "%d.%m.%Y-%H.%M.%S.avi", u);
and it works well.

When you do:
sFileOutput=&s1
&s1 creates a pointer of type char (*)[40] and not a pointer to the first element of the array like you expect. You are passing around a pointer to the entire array which gets converted to an incompatible type. Check the compilation warnings/errors.
The solution is to use either the implicit conversion:
sFileOutput=s1
or:
sFileOutput=&s1[0]

Related

MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED

im trying to code a Diffie-Hellman key Exchange (client side) into a XMC4500 and I'm using ARMmbed lib.
This is the code I got (based on dh_client.c):
int dhm (void)
{
int ret;
size_t n, buflen;
unsigned char *p, *end;
unsigned char buf[512];
unsigned char hash[32];
const char *pers = "dh_client";
mbedtls_entropy_context entropy;
mbedtls_ctr_drbg_context ctr_drbg;
mbedtls_rsa_context rsa;
mbedtls_dhm_context dhm;
mbedtls_aes_context aes;
mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_SHA256 );
mbedtls_dhm_init( &dhm );
mbedtls_aes_init( &aes );
mbedtls_ctr_drbg_init( &ctr_drbg );
/*
* 1. Setup the RNG
*/
mbedtls_entropy_init( &entropy );
ret = mbedtls_ctr_drbg_seed( &ctr_drbg,
mbedtls_entropy_func,
&entropy,
(const unsigned char *) pers,
strlen( pers ) );
mbedtls_aes_free( &aes );
mbedtls_rsa_free( &rsa );
mbedtls_dhm_free( &dhm );
mbedtls_ctr_drbg_free( &ctr_drbg );
mbedtls_entropy_free( &entropy );
return ret;}
I did not try to go further this, because it is not working and it is the very beginning of dhm algorithm. The function mbedtls_ctr_drbg_seed is returning MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED.
Also in the ctr_drbg.h I had to edit the MBEDTLS_CTR_DRBG_MAX_SEED_INPUT from 384(default) to 336, otherwise the code would crash. Everything else is default.
Someone knows why is returning this error?
Thanks in advance.
Note: Im calling this function in main. Running the code gives me no errors.
This error is returned when your entropy function(mbedtls_entropy_func) fails. Do you have an entropy source enabled? You probably don't have any strong entropy source configured in your platform, thus causing this failure.

Parse to integer with argp.h

I would like to be able to receive program arguments and options in my C program. The options should be treated as floats or ints. For some reason, I couldn't find good articles, tutorials, docs about argp.h. I started my implementation with the examples on the GNU Libc Manual, though unfortunately, it didn't get me to the solution.
Here is how I tried to solve the problem (example can be compiled, included every necessary line):
#include <stdlib.h>
#include <argp.h>
static char doc[] = "Doc";
static char args_doc[] = "ARG1";
static struct argp_option options[] = {
{"bool", 'b', 0, 0, "Simple boolean flag, works as I expected."},
{"int", 'i', 0, 0, "Would like to be able to parse options as --int=4 or -i 4."}, // but I can't
{0}
};
struct arguments {char *args[1]; int xbool, xint;};
static error_t
parse_opt (int key, char *arg, struct argp_state *state) {
struct arguments *arguments = state->input;
printf("key = %c, arg = %s\n", key, arg); // My attempt to understand the problem
//if (arg == NULL) return 0; // Prevents segfaults, in order to see how the args and keys change
switch (key) {
case 'b': arguments->xbool = 1; break;
case 'i': arguments->xint = (int) strtol(arg, NULL, 10); break;
case ARGP_KEY_ARG: if (state->arg_num >= 1) {argp_usage(state);} arguments->args[state->arg_num] = arg; break;
case ARGP_KEY_END: if (state->arg_num < 1) {argp_usage(state);} break;
default: return ARGP_ERR_UNKNOWN;
}
return 0;
}
static struct argp argp = { options, parse_opt, args_doc, doc };
int main (int argc, char **argv) {
struct arguments arguments;
arguments.xbool = 0;
arguments.xint = 0;
argp_parse (&argp, argc, argv, 0, 0, &arguments);
printf("ARG1 = %s\nbool option = %s\nparsed int option = %d",
arguments.args[0], arguments.xbool ? "true" : "false", arguments.xint);
exit (0);
}
The simple boolean flag works, as expected, so ./myprogram.out myarg --bool and ./myprogram.out myarg -b changes the flag's value.
However, I can't seem to find a way to parse the arguments as integers or floating point numbers.
This it the output I get for ./a.out -b --int=2 myarg:
key = , arg = (null)
key = b, arg = (null)
./a.out: option '--int' doesn't allow an argument
Try `a.out --help' or `a.out --usage' for more information.
and for ./a.out -b --int 2 myarg I get a segmentation fault, as I try to parse a NULL: key = i, arg = (null). I added a NULL check, and this way I could see, that the option I would like to parse comes with a , key (expected to come with i).
key = i, arg = (null)
key = , arg = 2
I thought about using a library because the program needs to handle various float and int options, falling back to default values, and I've seen often that it's not recommended to roll your own argument and options parser. Based on the examples, argp.h looked promising, however I couldn't get it to work yet.
ps. I know that parsing directly to ints and floats are not part of argp, this is why I was (naively, it seems) trying to add it to the structures and parse_opt function.
As it turned out, there was an error with options[]. The third, const char *arg parameter in the argp_option struct must be provided, if the option has argument associated with it [source: GNU C: Argp Option Vectors].
static struct argp_option options[] = {
{"bool", 'b', 0, 0, "Simple boolean flag, works as I expected."},
{"int", 'i', "Numbah", 0, "Now everything works as expected, I get the correct key-value (key-arg) pair in the parse_opt function"},
{0}
};

Accessing a git_odb_writepack field in libgit2 gives error "dereferencing pointer to incomplete type"

I'm using libgit2 and I want to write a pack file to an odb created with git_repository_odb. So I call git_odb_write_pack and initialize a *git_odb_writepack. Then when I attempt to access a field of the writepack struct, I get a compiler error "dereferencing pointer to incomplete type". Here's the code:
#include <stdio.h>
#include <git2.h>
void check_error(int code, char *action) {
if (code) {
printf("Error %d, %s\n", code, action);
exit(1);
}
}
static int my_git_transfer_progress_callback(const git_transfer_progress *stats, void *payload) {
printf("Got transfer callback\n");
return 0;
}
int main(int argc, char **argv) {
int error;
const char *repo_path = "/path/to/repo";
git_repository *repo = NULL;
error = git_repository_open(&repo, repo_path);
check_error(error, "opening repo");
git_odb *odb = NULL;
error = git_repository_odb(&odb, repo);
check_error(error, "initializing odb");
git_odb_writepack *writepack = NULL;
char *payload = "here's my payload";
error = git_odb_write_pack(&writepack, odb, my_git_transfer_progress_callback, payload);
check_error(error, "opening pack writing stream");
printf("Address: %u\n", writepack->backend); // <-- Line generating the error.
return 0;
}
Then I compile and get the error:
$ gcc -lgit2 writepack_error.c && LD_LIBRARY_PATH=/usr/local/lib ./a.out
writepack_error.c: In function 'main':
writepack_error.c:33: error: dereferencing pointer to incomplete type
I'm using libgit2 version 0.21.0. I'm new to C and libgit2 so I may be doing something silly. My understanding is this "dereferencing" error means I failed to define or include a struct or typedef. However I thought libgit2 only requires one include, #include <git2.h>.
Normal usage is covered by git2.h. Some functionality is kept under the sys/ directory to indicate that it's considered more advanced usage.
This in particular looks like it might be a bug since git2.h does not include git2/odb_backend.h. For now you can simply include it manually.

C - segmentation fault using struct member values

I'm running head-long into a segmentation fault that I'm not sure of the reason behind.
Short story... I store file names into members of a struct, then use those members to open files to load their data into linked lists. This is working fine when I only have two file, but when I go to add a third, I get a segmentation fault opening the first file.
Code will hopefully illustrate better...
int main(int argc, char* argv[])
{
/* Initalise tennisStore struct */
TennisStoreType *ts;
systemInit(ts);
/* Variables */
ts->stockFile = "stock.csv";
ts->custFile = "customer.csv";
ts->salesFile = "sales.csv";
/* Load data from files */
loadData(ts, ts->custFile, ts->stockFile);
...
}
The struct details for ts...
typedef struct tennisStore
{
CustomerNodePtr headCust;
unsigned customerCount;
StockNodePtr headStock;
unsigned stockCount;
char *custFile;
char *stockFile;
char *salesFile;
} TennisStoreType;
systemInit() seems pretty innocuous, but here's the code just in case...
void systemInit(TennisStoreType *ts)
{
/* Set ts options to be ready */
ts->headCust = NULL;
ts->headStock = NULL;
ts->customerCount = 0;
ts->stockCount = 0;
}
loadData()...
void loadData(TennisStoreType* ts, char* customerFile, char* stockFile)
{
/* Load customer data */
addCustNode(ts, customerFile);
/* Load stock data */
addStockNode(ts, stockFile);
}
Here's where the problem occurs...
void addStockNode(TennisStoreType* ts, char* stockFile)
{
/* Variables */
StockNodePtr head, new, current, previous;
unsigned stkLevel;
char *stkTok1, *stkTok2, *stkTok3, *stkTok4;
char buf[BUFSIZ];
float stkPrice;
FILE *stream;
/* Set head */
head = NULL;
/* Open stock file */
stream = fopen(stockFile, "r"); <-- segmentation fault when sales.csv line included
assert(stream);
while (fgets(buf, BUFSIZ, stream))
{
...
}
...
}
As above, when the ts->salesFile = "sales.csv" line is included in main, the segmentation fault occurs. When it isn't, all is fine (file opens, I can read from it, write to it etc). Cannot for the life of me understand why, so I'm appealing to your good nature and superior knowledge of C for potential causes of this problem.
Thanks!
ts is uninitialized, and used as is, in systemInit().
It should be malloc()ed..
change
TennisStoreType *ts;
to
TennisStoreType *ts=malloc(sizeof(TennisStoreType));
or
change
TennisStoreType *ts;
systemInit(ts);
to
TennisStoreType ts;
systemInit(&ts);
You never actually created your TennisStoreType object.
int main(int argc, char* argv[])
{
TennisStoreType *ts; // <-- allocates 4 bytes for a pointer
systemInit(ts); // <-- pass the pointer to nowhere around.
Try inserting ts = malloc(sizeof(TennisStoreType)) in between those two lines.

Warnings with pointer using .cfg file

I try to setup a c programm using the libconfig. There is example1.c:
int main()
{
const char **channel;
config_t config;
config_init(&config);
config_read_file(&config, "example.cfg");
if( config_lookup_string(&config,"value.channel",&channel) == CONFIG_FALSE)
{
printf("Failed to read fields\n");
return 1;
}
printf("argumente = %s\n", (char *)channel);
return 0;
}
and the example.cfg file
value = { channel = "hello"; }
if I compile it
gcc example1.c -lconfig
it says:
example1.c:39:3: Warning: delivery of arguments 3 from »config_lookup_string« of a incompatible pointer
/usr/include/libconfig.h:244:26: Detailed: »const char **« expected, but argument has typ »const char ***«
The funny thing is it works... the output is:
argumente = hello
How can I get rid of this warning ?
If I change the decleration to const char *channel and the output printf("argumente = %s\n", channel);, I receive a segfault at start and a warning at compiling like ... Detailed: »const char **« expected, but argument has typ »const char *«
You just need to get rid of one * in your declaration of channel. If you do that, you can also remove the cast in your printf call.
The reason it's "working" now is that cast is hiding a second warning about your printf format. Your program is behaving just like the extra * was removed already - just in a confusing way.

Resources