void encode_event(DBusMessageIter *iter, int *err, const alarm_event_t *eve, const char **def_args) { encode_cookie (iter, err, &eve->ALARMD_PRIVATE(cookie)); encode_time (iter, err, &eve->ALARMD_PRIVATE(trigger)); encode_string (iter, err, &eve->title); encode_string (iter, err, &eve->message); encode_string (iter, err, &eve->sound); encode_string (iter, err, &eve->icon); encode_unsigned (iter, err, &eve->flags); encode_string (iter, err, &eve->alarm_appid); encode_time (iter, err, &eve->alarm_time); encode_tm (iter, err, &eve->alarm_tm); encode_string (iter, err, &eve->alarm_tz); encode_time (iter, err, &eve->recur_secs); encode_int (iter, err, &eve->recur_count); encode_time (iter, err, &eve->snooze_secs); encode_time (iter, err, &eve->snooze_total); encode_size (iter, err, &eve->action_cnt); encode_int (iter, err, &eve->response); for( size_t i = 0; i < eve->action_cnt; ++i ) { encode_action(iter, err, &eve->action_tab[i], def_args); } encode_size (iter, err, &eve->recurrence_cnt); for( size_t i = 0; i < eve->recurrence_cnt; ++i ) { encode_recur(iter, err, &eve->recurrence_tab[i]); } encode_size (iter, err, &eve->attr_cnt); for( size_t i = 0; i < eve->attr_cnt; ++i ) { encode_attr(iter, err, eve->attr_tab[i]); } }
lsb_err_value heka_encode_message_table(lsb_lua_sandbox *lsb, int idx) { lsb_err_value ret = NULL; lsb_output_buffer *ob = &lsb->output; ob->pos = 0; // use existing or create a type 4 uuid lua_getfield(lsb->lua, idx, LSB_UUID); size_t len; const char *uuid = lua_tolstring(lsb->lua, -1, &len); ret = lsb_write_heka_uuid(ob, uuid, len); lua_pop(lsb->lua, 1); // remove uuid if (ret) return ret; // use existing or create a timestamp lua_getfield(lsb->lua, idx, LSB_TIMESTAMP); long long ts; if (lua_isnumber(lsb->lua, -1)) { ts = (long long)lua_tonumber(lsb->lua, -1); } else { ts = time(NULL) * 1000000000LL; } lua_pop(lsb->lua, 1); // remove timestamp ret = lsb_pb_write_key(ob, LSB_PB_TIMESTAMP, LSB_PB_WT_VARINT); if (!ret) ret = lsb_pb_write_varint(ob, ts); if (!ret) ret = encode_string(lsb, ob, LSB_PB_TYPE, LSB_TYPE, idx); if (!ret) ret = encode_string(lsb, ob, LSB_PB_LOGGER, LSB_LOGGER, idx); if (!ret) ret = encode_int(lsb, ob, LSB_PB_SEVERITY, LSB_SEVERITY, idx); if (!ret) ret = encode_string(lsb, ob, LSB_PB_PAYLOAD, LSB_PAYLOAD, idx); if (!ret) ret = encode_string(lsb, ob, LSB_PB_ENV_VERSION, LSB_ENV_VERSION, idx); if (!ret) ret = encode_int(lsb, ob, LSB_PB_PID, LSB_PID, idx); if (!ret) ret = encode_string(lsb, ob, LSB_PB_HOSTNAME, LSB_HOSTNAME, idx); if (!ret) ret = encode_fields(lsb, ob, LSB_PB_FIELDS, LSB_FIELDS, idx); if (!ret) ret = lsb_expand_output_buffer(ob, 1); ob->buf[ob->pos] = 0; // prevent possible overrun if treated as a string return ret; }
struct byte_array* serial_encode_string(struct byte_array* buf, const struct byte_array* bytes) { if (!bytes) return buf; if (!buf) buf = byte_array_new(); encode_int(buf, bytes->length); byte_array_resize(buf, buf->length + bytes->length); memcpy(buf->current, bytes->data, bytes->length); buf->current = buf->data + buf->length; return buf; }
/* Encodes the struct in the bcont list. The encoding is null terminated. The size * parameter does not account for the null at the end. * NOTE: bfree DOES NOT FREE THIS STRUCT OR ITS CONTENT! * Accepts: a Bcont struct * Returns: a Bencoded struct */ struct Bencoded *encode(struct Bcont *bcont) { int size; char *res; char *si; switch(bcont->type) { case 'l': size = blen_list(bcont->cnt); res = (char *)malloc_eoe((1 + size) * sizeof(char)); si = res; encode_list(bcont->cnt, &si); break; case 'd': size = blen_dict(bcont->cnt); res = (char *)malloc_eoe((1 + size) * sizeof(char)); si = res; encode_dict(bcont->cnt, &si); break; case 's': size = blen_str(bcont->cnt); res = (char *)malloc_eoe((1 + size) * sizeof(char)); si = res; encode_str(bcont->cnt, &si); break; case 'i': size = blen_int(bcont->cnt); res = (char *)malloc_eoe((1 + size) * sizeof(char)); si = res; encode_int(bcont->cnt, &si); break; default: fprintf(stderr, "error: bencode encountered poorly formed subtype\n"); exit(1); } struct Bencoded *benc = (struct Bencoded *)malloc_eoe(sizeof(struct Bencoded)); benc->size = size; benc->cnt = res; return benc; }
static void fill_polygon(PLStream *pls) { TekDev *dev = (TekDev *) pls->dev; int i; char fillcol[4], firstpoint[5]; if (pls->dev_npts < 1) return; tek_graph(pls); encode_int(fillcol, -dev->curcolor); encode_vector(firstpoint, pls->dev_x[0], pls->dev_y[0]); /* Select the fill pattern */ pls->bytecnt += fprintf(pls->OutFile, "\033MP%s", fillcol); /* Begin panel boundary */ /* Set pls->debug to see the boundary of each fill box -- cool! */ if (pls->debug) pls->bytecnt += fprintf(pls->OutFile, "\033LP%s1", firstpoint); else pls->bytecnt += fprintf(pls->OutFile, "\033LP%s0", firstpoint); /* Specify boundary (in vector mode) */ pls->bytecnt += fprintf(pls->OutFile, VECTOR_MODE); for (i = 1; i < pls->dev_npts; i++) tek_vector(pls, pls->dev_x[i], pls->dev_y[i]); /* End panel */ pls->bytecnt += fprintf(pls->OutFile, "\033LE"); }
struct byte_array* serial_encode_int(struct byte_array* buf, int32_t value) { if (!buf) buf = byte_array_new(); encode_int(buf, value); return buf; }
static SCM open_vorbis_stream(SCM server, SCM port, SCM password, SCM mount, SCM config) { SCM smob; double qual; char stream_name[32]; VORBIS_STREAM *stream; stream = (VORBIS_STREAM *)my_gc_malloc(sizeof(VORBIS_STREAM), "vorbis_stream", "vorbis stream"); stream->header.shutdown = vorbis_shutdown; vorbis_info_init(&(stream->vi)); SCM quality = get_property(config, "quality"); if (scm_is_number(quality)) qual = scm_to_double(quality); else qual = 0.6; scm_remember_upto_here_1(quality); stream->header.shoutbuf = NULL; stream->header.shoutbuf_size = 0; stream->header.rb_size = sizeof(sample_t) * RB_SECS * sampling_rate * QMX_CHANNELS + 0.5; stream->header.ringbuf = jack_ringbuffer_create(stream->header.rb_size); if (vorbis_encode_init_vbr(&(stream->vi), QMX_CHANNELS, sampling_rate, qual) != 0) { log_msg("vorbis init failed\n"); return SCM_BOOL_F; } if ((stream->header.shout = shout_new()) == NULL) { log_msg("can't create shout instance\n"); return SCM_BOOL_F; } vorbis_analysis_init(&(stream->vd), &(stream->vi)); vorbis_block_init(&(stream->vd), &(stream->vb)); ogg_stream_init(&(stream->os), rand()); vorbis_comment_init(&(stream->vc)); vorbis_comment_add_tag(&(stream->vc), "ENCODER", "QMX"); shout_set_protocol(stream->header.shout, SHOUT_PROTOCOL_HTTP); shout_set_format(stream->header.shout, SHOUT_FORMAT_OGG); char *server_s = scm_to_locale_string(server); // server shout_set_host(stream->header.shout, server_s); free(server_s); shout_set_port(stream->header.shout, scm_to_int(port)); // port shout_set_user(stream->header.shout, "source"); // user char *password_s = scm_to_locale_string(password); // password shout_set_password(stream->header.shout, password_s); free(password_s); char *mount_s = scm_to_locale_string(mount); // mount pt shout_set_mount(stream->header.shout, mount_s); SCM title = get_property(config, "title"); if (scm_is_string(title)) { char *title_s = scm_to_locale_string(title); if (strlen(title_s) > 0) vorbis_comment_add_tag(&(stream->vc), "TITLE", title_s); free(title_s); } scm_remember_upto_here_1(title); SCM name = get_property(config, "name"); stream_name[0] = '\0'; if (scm_is_string(name)) { char *name_s = scm_to_locale_string(name); shout_set_name(stream->header.shout, name_s); snprintf(stream_name, sizeof(stream_name), "'%s' ", name_s); free(name_s); } scm_remember_upto_here_1(name); SCM description = get_property(config, "description"); if (scm_is_string(description)) { char *description_s = scm_to_locale_string(description); shout_set_description(stream->header.shout, description_s); free(description_s); } scm_remember_upto_here_1(description); SCM genre = get_property(config, "genre"); if (scm_is_string(genre)) { char *genre_s = scm_to_locale_string(genre); shout_set_genre(stream->header.shout, genre_s); free(genre_s); } scm_remember_upto_here_1(genre); shout_set_audio_info(stream->header.shout, SHOUT_AI_CHANNELS, encode_int(QMX_CHANNELS)); shout_set_audio_info(stream->header.shout, SHOUT_AI_SAMPLERATE, encode_int(sampling_rate)); if (shout_open(stream->header.shout) == SHOUTERR_SUCCESS) { log_msg("vorbis stream %smounted at %s\n", stream_name, mount_s); free(mount_s); } else { log_msg("vorbis stream %sfailed to open: %s\n", stream_name, shout_get_error(stream->header.shout)); free(mount_s); shout_free(stream->header.shout); return SCM_BOOL_F; } vorbis_streams += 1; stream->header.eos = 0; SCM_NEWSMOB(smob, vorbis_stream_tag, stream); streams = link_in(streams, smob); stream->header.do_prep = need_prepper; need_prepper = 0; stream->header.bookmark = 0; add_outbuf_client(load_frames, process_frames, (void *)stream, "Ogg-Vorbis stream"); spawn_detached_thread(shout_thread, (void *)&(stream->header)); scm_remember_upto_here_1(smob); return smob; }
/* Returns 0 on success, non-zero on failure */ static int save_table( lua_State * L, luaamf_SaveBuffer * sb, int index ) { luaamf_SaveBuffer numeric; luaamf_SaveBuffer associative; int result = LUAAMF_ESUCCESS; int numeric_index = 1; int key_value_pairs_number = 0; { void * alloc_ud = NULL; lua_Alloc alloc_fn = lua_getallocf(L, &alloc_ud); sb_init(&numeric, alloc_fn, alloc_ud); sb_init(&associative, alloc_fn, alloc_ud); } lua_pushnil(L); while (result == LUAAMF_ESUCCESS && lua_next(L, index) != 0) { int value_pos = lua_gettop(L); /* We need absolute values */ int key_pos = value_pos - 1; if(lua_type(L, key_pos) == LUA_TNUMBER && lua_tonumber(L, key_pos) == (float)numeric_index) { /* Save enumerated value. */ result = save_value(&numeric, L, value_pos, 1); numeric_index++; } else { /* Save associative key. */ result = save_value(&associative, L, key_pos, 0); /* Save associative value. */ if (result == LUAAMF_ESUCCESS) { result = save_value(&associative, L, value_pos, 1); key_value_pairs_number++; } } if (result == LUAAMF_ESUCCESS) { /* Remove value from stack, leave key for the next iteration. */ lua_pop(L, 1); } else { return result; } } /* write serilization here */ sb_writechar(sb, LUAAMF_ARRAY); encode_int(sb, 2 * key_value_pairs_number + 1); sb_write(sb, sb_buffer(&associative, &(associative.buf_size)), associative.buf_size); sb_writechar(sb, 0x001); sb_write(sb, sb_buffer(&numeric, &(numeric.buf_size)), numeric.buf_size); result = LUAAMF_ESUCCESS; sb_destroy(&numeric); sb_destroy(&associative); return result; }
/* * addEntry: Adds an entry to a fcb. * * @start: Integer Pointer location of the starting block * @fcBlockID: Integer the target fcb id * @name: String name of the entry * @type: Integer type of the entry * * return 0: successful execution * return -1: file already exists in directory * return -2: error retrieving the parent file control block * return -3: error looking for a free block * return -4: error finding entry in the file control block * return -5: error creating file control block */ int addEntry(int* start, int fcBlockID, char* name, int type) { /* best case: * block: [1, a, b, c, d, e, f, ... ] * average case: * block: [1, a, b, c, 0, 0, 0, ... ] * worst case: * block: [1, 0, 0, 0, 0, 0, 0, ... ] * * Therefore, 7 chars must be skipped before any useful data is reached. */ if (getStart(malloc(1), fcBlockID, name) != -2) { fprintf(stderr, "File already exists in directory.\n"); return -1; } char* fcb = malloc(BLOCK_SIZE); if (get_block(fcBlockID, fcb)) { fprintf(stderr, "Error retrieving the parent file control block.\n"); return -2; } if (getFreeBlock(start)) { fprintf(stderr, "Error looking for a free block.\n"); return -3; } // Position in the file control block int position; if (getEntryPoint(&position, fcb)) { fprintf(stderr, "Error finding entry in the file control block.\n"); return -4; } /* * order of file attributes: * type name start */ fcb[position++] = type; for (unsigned int i = 0; i < strlen(name); i++) { fcb[i + position] = name[i]; } position += MAX_DIRNAME - 1; char* _start = encode_int(*start); for (int i = 0; i < START; i++) { fcb[i + position] = _start[i]; } position += START; if (fcb[position + 1] == '\0') { fcb[position] = ENTRY_END; } if (put_block(fcBlockID, fcb)) { fprintf(stderr, "Error creating file control block.\n"); return -5; } return 0; }
static void tek_init(PLStream *pls) { TekDev *dev; int xmin = 0; int xmax = TEKX; int ymin = 0; int ymax = TEKY; PLFLT pxlx = 4.771; PLFLT pxly = 4.653; pls->graphx = TEXT_MODE; /* Allocate and initialize device-specific data */ pls->dev = calloc(1, (size_t) sizeof(TekDev)); if (pls->dev == NULL) plexit("tek_init: Out of memory."); dev = (TekDev *) pls->dev; dev->curcolor = 1; dev->xold = PL_UNDEFINED; dev->yold = PL_UNDEFINED; plP_setpxl(pxlx, pxly); plP_setphy(xmin, xmax, ymin, ymax); /* Terminal/file initialization */ if (pls->termin) { pls->OutFile = stdout; tty_setup(); } else { plFamInit(pls); plOpenFile(pls); } switch (pls->dev_minor) { #ifdef PLD_tek4107 case tek4107: pls->graphx = GRAPHICS_MODE; fprintf(pls->OutFile, "\033%%!0"); /* set tek mode */ fprintf(pls->OutFile, "\033KN1"); /* clear the view */ fprintf(pls->OutFile, "\033LZ"); /* clear dialog buffer */ fprintf(pls->OutFile, "\033ML1"); /* set default color */ break; #endif /* PLD_tek4107 */ /* A sneaky hack: VLT sometimes has leftover panel information, causing * garbage at the beginning of a sequence of color fills. Since * there is no clear panel command, instead I set the fill color to the * same as background and issue an end panel command. */ #ifdef PLD_vlt case vlt:{ char fillcol[4]; tek_graph(pls); encode_int(fillcol, 0); fprintf(pls->OutFile, "\033MP%s\033LE", fillcol); break; } #endif /* PLD_vlt */ default: tek_graph(pls); } /* Initialize palette */ if ( pls->color & 0x01 ) { printf("\033TM111"); /* Switch to RGB colors */ setcmap(pls); } /* Finish initialization */ fprintf(pls->OutFile, VECTOR_MODE); /* Enter vector mode */ if (pls->termin) fprintf(pls->OutFile, CLEAR_VIEW);/* erase and home */ fflush(pls->OutFile); }
void encode_time(DBusMessageIter *iter, int *err, const time_t *pval) { int tmp = *pval; encode_int(iter, err, &tmp); }