code
stringlengths 31
2.05k
| label_name
stringclasses 5
values | label
int64 0
4
|
---|---|---|
int VP8LDecodeImage(VP8LDecoder* const dec) {
VP8Io* io = NULL;
WebPDecParams* params = NULL;
if (dec == NULL) return 0;
assert(dec->hdr_.huffman_tables_ != NULL);
assert(dec->hdr_.htree_groups_ != NULL);
assert(dec->hdr_.num_htree_groups_ > 0);
io = dec->io_;
assert(io != NULL);
params = (WebPDecParams*)io->opaque;
assert(params != NULL);
// Initialization.
if (dec->state_ != READ_DATA) {
dec->output_ = params->output;
assert(dec->output_ != NULL);
if (!WebPIoInitFromOptions(params->options, io, MODE_BGRA)) {
VP8LSetError(dec, VP8_STATUS_INVALID_PARAM);
goto Err;
}
if (!AllocateInternalBuffers32b(dec, io->width)) goto Err;
#if !defined(WEBP_REDUCE_SIZE)
if (io->use_scaling && !AllocateAndInitRescaler(dec, io)) goto Err;
#else
if (io->use_scaling) {
VP8LSetError(dec, VP8_STATUS_INVALID_PARAM);
goto Err;
}
#endif
if (io->use_scaling || WebPIsPremultipliedMode(dec->output_->colorspace)) {
// need the alpha-multiply functions for premultiplied output or rescaling
WebPInitAlphaProcessing();
}
if (!WebPIsRGBMode(dec->output_->colorspace)) {
WebPInitConvertARGBToYUV();
if (dec->output_->u.YUVA.a != NULL) WebPInitAlphaProcessing();
}
if (dec->incremental_) {
if (dec->hdr_.color_cache_size_ > 0 &&
dec->hdr_.saved_color_cache_.colors_ == NULL) {
if (!VP8LColorCacheInit(&dec->hdr_.saved_color_cache_,
dec->hdr_.color_cache_.hash_bits_)) {
VP8LSetError(dec, VP8_STATUS_OUT_OF_MEMORY);
goto Err;
}
}
}
dec->state_ = READ_DATA;
}
// Decode.
if (!DecodeImageData(dec, dec->pixels_, dec->width_, dec->height_,
io->crop_bottom, ProcessRows)) {
goto Err;
}
params->last_y = dec->last_out_row_;
return 1;
Err:
VP8LClear(dec);
assert(dec->status_ != VP8_STATUS_OK);
return 0;
} | Base | 1 |
static int ReadHuffmanCodeLengths(
VP8LDecoder* const dec, const int* const code_length_code_lengths,
int num_symbols, int* const code_lengths) {
int ok = 0;
VP8LBitReader* const br = &dec->br_;
int symbol;
int max_symbol;
int prev_code_len = DEFAULT_CODE_LENGTH;
HuffmanCode table[1 << LENGTHS_TABLE_BITS];
if (!VP8LBuildHuffmanTable(table, LENGTHS_TABLE_BITS,
code_length_code_lengths,
NUM_CODE_LENGTH_CODES)) {
goto End;
}
if (VP8LReadBits(br, 1)) { // use length
const int length_nbits = 2 + 2 * VP8LReadBits(br, 3);
max_symbol = 2 + VP8LReadBits(br, length_nbits);
if (max_symbol > num_symbols) {
goto End;
}
} else {
max_symbol = num_symbols;
}
symbol = 0;
while (symbol < num_symbols) {
const HuffmanCode* p;
int code_len;
if (max_symbol-- == 0) break;
VP8LFillBitWindow(br);
p = &table[VP8LPrefetchBits(br) & LENGTHS_TABLE_MASK];
VP8LSetBitPos(br, br->bit_pos_ + p->bits);
code_len = p->value;
if (code_len < kCodeLengthLiterals) {
code_lengths[symbol++] = code_len;
if (code_len != 0) prev_code_len = code_len;
} else {
const int use_prev = (code_len == kCodeLengthRepeatCode);
const int slot = code_len - kCodeLengthLiterals;
const int extra_bits = kCodeLengthExtraBits[slot];
const int repeat_offset = kCodeLengthRepeatOffsets[slot];
int repeat = VP8LReadBits(br, extra_bits) + repeat_offset;
if (symbol + repeat > num_symbols) {
goto End;
} else {
const int length = use_prev ? prev_code_len : 0;
while (repeat-- > 0) code_lengths[symbol++] = length;
}
}
}
ok = 1;
End:
if (!ok) return VP8LSetError(dec, VP8_STATUS_BITSTREAM_ERROR);
return ok;
} | Base | 1 |
static int ReadHuffmanCode(int alphabet_size, VP8LDecoder* const dec,
int* const code_lengths, HuffmanCode* const table) {
int ok = 0;
int size = 0;
VP8LBitReader* const br = &dec->br_;
const int simple_code = VP8LReadBits(br, 1);
memset(code_lengths, 0, alphabet_size * sizeof(*code_lengths));
if (simple_code) { // Read symbols, codes & code lengths directly.
const int num_symbols = VP8LReadBits(br, 1) + 1;
const int first_symbol_len_code = VP8LReadBits(br, 1);
// The first code is either 1 bit or 8 bit code.
int symbol = VP8LReadBits(br, (first_symbol_len_code == 0) ? 1 : 8);
code_lengths[symbol] = 1;
// The second code (if present), is always 8 bits long.
if (num_symbols == 2) {
symbol = VP8LReadBits(br, 8);
code_lengths[symbol] = 1;
}
ok = 1;
} else { // Decode Huffman-coded code lengths.
int i;
int code_length_code_lengths[NUM_CODE_LENGTH_CODES] = { 0 };
const int num_codes = VP8LReadBits(br, 4) + 4;
assert(num_codes <= NUM_CODE_LENGTH_CODES);
for (i = 0; i < num_codes; ++i) {
code_length_code_lengths[kCodeLengthCodeOrder[i]] = VP8LReadBits(br, 3);
}
ok = ReadHuffmanCodeLengths(dec, code_length_code_lengths, alphabet_size,
code_lengths);
}
ok = ok && !br->eos_;
if (ok) {
size = VP8LBuildHuffmanTable(table, HUFFMAN_TABLE_BITS,
code_lengths, alphabet_size);
}
if (!ok || size == 0) {
return VP8LSetError(dec, VP8_STATUS_BITSTREAM_ERROR);
}
return size;
} | Base | 1 |
int VP8LBuildHuffmanTable(HuffmanCode* const root_table, int root_bits,
const int code_lengths[], int code_lengths_size) {
int total_size;
assert(code_lengths_size <= MAX_CODE_LENGTHS_SIZE);
if (root_table == NULL) {
total_size = BuildHuffmanTable(NULL, root_bits,
code_lengths, code_lengths_size, NULL);
} else if (code_lengths_size <= SORTED_SIZE_CUTOFF) {
// use local stack-allocated array.
uint16_t sorted[SORTED_SIZE_CUTOFF];
total_size = BuildHuffmanTable(root_table, root_bits,
code_lengths, code_lengths_size, sorted);
} else { // rare case. Use heap allocation.
uint16_t* const sorted =
(uint16_t*)WebPSafeMalloc(code_lengths_size, sizeof(*sorted));
if (sorted == NULL) return 0;
total_size = BuildHuffmanTable(root_table, root_bits,
code_lengths, code_lengths_size, sorted);
WebPSafeFree(sorted);
}
return total_size;
} | Base | 1 |
int jvp_number_cmp(jv a, jv b) {
assert(JVP_HAS_KIND(a, JV_KIND_NUMBER));
assert(JVP_HAS_KIND(b, JV_KIND_NUMBER));
#ifdef USE_DECNUM
if (JVP_HAS_FLAGS(a, JVP_FLAGS_NUMBER_LITERAL) && JVP_HAS_FLAGS(b, JVP_FLAGS_NUMBER_LITERAL)) {
decNumber res;
decNumberCompare(&res,
jvp_dec_number_ptr(a),
jvp_dec_number_ptr(b),
DEC_CONTEXT()
);
if (decNumberIsZero(&res)) {
return 0;
} else if (decNumberIsNegative(&res)) {
return -1;
} else {
return 1;
}
}
#endif
double da = jv_number_value(a), db = jv_number_value(b);
if (da < db) {
return -1;
} else if (da == db) {
return 0;
} else {
return 1;
}
} | Base | 1 |
static const char* jvp_literal_number_literal(jv n) {
assert(JVP_HAS_FLAGS(n, JVP_FLAGS_NUMBER_LITERAL));
decNumber *pdec = jvp_dec_number_ptr(n);
jvp_literal_number* plit = jvp_literal_number_ptr(n);
if (decNumberIsNaN(pdec)) {
return "null";
}
if (decNumberIsInfinite(pdec)) {
// We cannot preserve the literal data of numbers outside the limited
// range of exponent. Since `decNumberToString` returns "Infinity"
// (or "-Infinity"), and to reduce stack allocations as possible, we
// normalize infinities in the callers instead of printing the maximum
// (or minimum) double here.
return NULL;
}
if (plit->literal_data == NULL) {
int len = jvp_dec_number_ptr(n)->digits + 14;
plit->literal_data = jv_mem_alloc(len);
// Preserve the actual precision as we have parsed it
// don't do decNumberTrim(pdec);
decNumberToString(pdec, plit->literal_data);
}
return plit->literal_data;
} | Base | 1 |
comics_document_save (EvDocument *document,
const char *uri,
GError **error)
{
ComicsDocument *comics_document = COMICS_DOCUMENT (document);
return ev_xfer_uri_simple (comics_document->archive, uri, error);
} | Base | 1 |
render_pixbuf_size_prepared_cb (GdkPixbufLoader *loader,
gint width,
gint height,
gpointer data)
{
double *scale = data;
int w = (width * (*scale) + 0.5);
int h = (height * (*scale) + 0.5);
gdk_pixbuf_loader_set_size (loader, w, h);
} | Base | 1 |
comics_generate_command_lines (ComicsDocument *comics_document,
GError **error)
{
gchar *quoted_file, *quoted_file_aux;
gchar *quoted_command;
ComicBookDecompressType type;
type = comics_document->command_usage;
comics_document->regex_arg = command_usage_def[type].regex_arg;
quoted_command = g_shell_quote (comics_document->selected_command);
if (comics_document->regex_arg) {
quoted_file = comics_regex_quote (comics_document->archive);
quoted_file_aux = g_shell_quote (comics_document->archive);
comics_document->list_command =
g_strdup_printf (command_usage_def[type].list,
comics_document->alternative_command,
quoted_file_aux);
g_free (quoted_file_aux);
} else {
quoted_file = g_shell_quote (comics_document->archive);
comics_document->list_command =
g_strdup_printf (command_usage_def[type].list,
quoted_command, quoted_file);
}
comics_document->extract_command =
g_strdup_printf (command_usage_def[type].extract,
quoted_command);
comics_document->offset = command_usage_def[type].offset;
if (command_usage_def[type].decompress_tmp) {
comics_document->dir = ev_mkdtemp ("atril-comics-XXXXXX", error);
if (comics_document->dir == NULL)
return FALSE;
/* unrar-free can't create directories, but ev_mkdtemp already created the dir */
comics_document->decompress_tmp =
g_strdup_printf (command_usage_def[type].decompress_tmp,
quoted_command, quoted_file,
comics_document->dir);
g_free (quoted_file);
g_free (quoted_command);
if (!comics_decompress_temp_dir (comics_document->decompress_tmp,
comics_document->selected_command, error))
return FALSE;
else
return TRUE;
} else {
g_free (quoted_file);
g_free (quoted_command);
return TRUE;
}
} | Base | 1 |
comics_document_init (ComicsDocument *comics_document)
{
comics_document->archive = NULL;
comics_document->page_names = NULL;
comics_document->extract_command = NULL;
} | Base | 1 |
get_page_size_area_prepared_cb (GdkPixbufLoader *loader,
gpointer data)
{
gboolean *got_size = data;
*got_size = TRUE;
} | Base | 1 |
comics_remove_dir (gchar *path_name)
{
GDir *content_dir;
const gchar *filename;
gchar *filename_with_path;
if (g_file_test (path_name, G_FILE_TEST_IS_DIR)) {
content_dir = g_dir_open (path_name, 0, NULL);
filename = g_dir_read_name (content_dir);
while (filename) {
filename_with_path =
g_build_filename (path_name,
filename, NULL);
comics_remove_dir (filename_with_path);
g_free (filename_with_path);
filename = g_dir_read_name (content_dir);
}
g_dir_close (content_dir);
}
/* Note from g_remove() documentation: on Windows, it is in general not
* possible to remove a file that is open to some process, or mapped
* into memory.*/
return (g_remove (path_name));
} | Base | 1 |
comics_document_finalize (GObject *object)
{
ComicsDocument *comics_document = COMICS_DOCUMENT (object);
if (comics_document->decompress_tmp) {
if (comics_remove_dir (comics_document->dir) == -1)
g_warning (_("There was an error deleting β%sβ."),
comics_document->dir);
g_free (comics_document->dir);
}
if (comics_document->page_names) {
g_ptr_array_foreach (comics_document->page_names, (GFunc) g_free, NULL);
g_ptr_array_free (comics_document->page_names, TRUE);
}
g_free (comics_document->archive);
g_free (comics_document->selected_command);
g_free (comics_document->alternative_command);
g_free (comics_document->extract_command);
g_free (comics_document->list_command);
G_OBJECT_CLASS (comics_document_parent_class)->finalize (object);
} | Base | 1 |
comics_document_render (EvDocument *document,
EvRenderContext *rc)
{
GdkPixbuf *pixbuf;
cairo_surface_t *surface;
pixbuf = comics_document_render_pixbuf (document, rc);
surface = ev_document_misc_surface_from_pixbuf (pixbuf);
g_object_unref (pixbuf);
return surface;
} | Base | 1 |
sort_page_names (gconstpointer a,
gconstpointer b)
{
const char *name_1, *name_2;
gchar *key_1, *key_2;
gboolean sort_last_1, sort_last_2;
int compare;
name_1 = * (const char **) a;
name_2 = * (const char **) b;
#define SORT_LAST_CHAR1 '.'
#define SORT_LAST_CHAR2 '#'
sort_last_1 = name_1[0] == SORT_LAST_CHAR1 || name_1[0] == SORT_LAST_CHAR2;
sort_last_2 = name_2[0] == SORT_LAST_CHAR1 || name_2[0] == SORT_LAST_CHAR2;
#undef SORT_LAST_CHAR1
#undef SORT_LAST_CHAR2
if (sort_last_1 && !sort_last_2)
{
compare = +1;
}
else if (!sort_last_1 && sort_last_2)
{
compare = -1;
}
else
{
key_1 = g_utf8_collate_key_for_filename (name_1, -1);
key_2 = g_utf8_collate_key_for_filename (name_2, -1);
compare = strcmp (key_1, key_2);
g_free (key_1);
g_free (key_2);
}
return compare;
} | Base | 1 |
get_supported_image_extensions(void)
{
GSList *extensions = NULL;
GSList *formats = gdk_pixbuf_get_formats ();
GSList *l;
for (l = formats; l != NULL; l = l->next) {
int i;
gchar **ext = gdk_pixbuf_format_get_extensions (l->data);
for (i = 0; ext[i] != NULL; i++) {
extensions = g_slist_append (extensions,
g_strdup (ext[i]));
}
g_strfreev (ext);
}
g_slist_free (formats);
return extensions;
} | Base | 1 |
comics_decompress_temp_dir (const gchar *command_decompress_tmp,
const gchar *command,
GError **error)
{
gboolean success;
gchar *std_out, *basename;
GError *err = NULL;
gint retval;
success = g_spawn_command_line_sync (command_decompress_tmp, &std_out,
NULL, &retval, &err);
basename = g_path_get_basename (command);
if (!success) {
g_set_error (error,
EV_DOCUMENT_ERROR,
EV_DOCUMENT_ERROR_INVALID,
_("Error launching the command β%sβ in order to "
"decompress the comic book: %s"),
basename,
err->message);
g_error_free (err);
} else if (WIFEXITED (retval)) {
if (WEXITSTATUS (retval) == EXIT_SUCCESS) {
g_free (std_out);
g_free (basename);
return TRUE;
} else {
g_set_error (error,
EV_DOCUMENT_ERROR,
EV_DOCUMENT_ERROR_INVALID,
_("The command β%sβ failed at "
"decompressing the comic book."),
basename);
g_free (std_out);
}
} else {
g_set_error (error,
EV_DOCUMENT_ERROR,
EV_DOCUMENT_ERROR_INVALID,
_("The command β%sβ did not end normally."),
basename);
g_free (std_out);
}
g_free (basename);
return FALSE;
} | Base | 1 |
comics_document_get_page_size (EvDocument *document,
EvPage *page,
double *width,
double *height)
{
GdkPixbufLoader *loader;
char **argv;
guchar buf[1024];
gboolean success, got_size = FALSE;
gint outpipe = -1;
GPid child_pid;
gssize bytes;
GdkPixbuf *pixbuf;
gchar *filename;
ComicsDocument *comics_document = COMICS_DOCUMENT (document);
if (!comics_document->decompress_tmp) {
argv = extract_argv (document, page->index);
success = g_spawn_async_with_pipes (NULL, argv, NULL,
G_SPAWN_SEARCH_PATH |
G_SPAWN_STDERR_TO_DEV_NULL,
NULL, NULL,
&child_pid,
NULL, &outpipe, NULL, NULL);
g_strfreev (argv);
g_return_if_fail (success == TRUE);
loader = gdk_pixbuf_loader_new ();
g_signal_connect (loader, "area-prepared",
G_CALLBACK (get_page_size_area_prepared_cb),
&got_size);
while (outpipe >= 0) {
bytes = read (outpipe, buf, 1024);
if (bytes > 0)
gdk_pixbuf_loader_write (loader, buf, bytes, NULL);
if (bytes <= 0 || got_size) {
close (outpipe);
outpipe = -1;
gdk_pixbuf_loader_close (loader, NULL);
}
}
pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
if (pixbuf) {
if (width)
*width = gdk_pixbuf_get_width (pixbuf);
if (height)
*height = gdk_pixbuf_get_height (pixbuf);
}
g_spawn_close_pid (child_pid);
g_object_unref (loader);
} else {
filename = g_build_filename (comics_document->dir,
(char *) comics_document->page_names->pdata[page->index],
NULL);
pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
if (pixbuf) {
if (width)
*width = gdk_pixbuf_get_width (pixbuf);
if (height)
*height = gdk_pixbuf_get_height (pixbuf);
g_object_unref (pixbuf);
}
g_free (filename);
}
} | Base | 1 |
extract_argv (EvDocument *document, gint page)
{
ComicsDocument *comics_document = COMICS_DOCUMENT (document);
char **argv;
char *command_line, *quoted_archive, *quoted_filename;
GError *err = NULL;
if (g_strrstr (comics_document->page_names->pdata[page], "--checkpoint-action="))
{
g_warning ("File unsupported\n");
gtk_main_quit ();
}
if (page >= comics_document->page_names->len)
return NULL;
if (comics_document->regex_arg) {
quoted_archive = g_shell_quote (comics_document->archive);
quoted_filename =
comics_regex_quote (comics_document->page_names->pdata[page]);
} else {
quoted_archive = g_shell_quote (comics_document->archive);
quoted_filename = g_shell_quote (comics_document->page_names->pdata[page]);
}
command_line = g_strdup_printf ("%s %s %s",
comics_document->extract_command,
quoted_archive,
quoted_filename);
g_free (quoted_archive);
g_free (quoted_filename);
g_shell_parse_argv (command_line, NULL, &argv, &err);
g_free (command_line);
if (err) {
g_warning (_("Error %s"), err->message);
g_error_free (err);
return NULL;
}
return argv;
} | Base | 1 |
comics_regex_quote (const gchar *unquoted_string)
{
const gchar *p;
GString *dest;
dest = g_string_new ("'");
p = unquoted_string;
while (*p) {
switch (*p) {
/* * matches a sequence of 0 or more characters */
case ('*'):
/* ? matches exactly 1 charactere */
case ('?'):
/* [...] matches any single character found inside
* the brackets. Disabling the first bracket is enough.
*/
case ('['):
g_string_append (dest, "[");
g_string_append_c (dest, *p);
g_string_append (dest, "]");
break;
/* Because \ escapes regex expressions that we are
* disabling for unzip, we need to disable \ too */
case ('\\'):
g_string_append (dest, "[\\\\]");
break;
/* Escape single quote inside the string */
case ('\''):
g_string_append (dest, "'\\''");
break;
default:
g_string_append_c (dest, *p);
break;
}
++p;
}
g_string_append_c (dest, '\'');
return g_string_free (dest, FALSE);
} | Base | 1 |
comics_document_render_pixbuf (EvDocument *document,
EvRenderContext *rc)
{
GdkPixbufLoader *loader;
GdkPixbuf *rotated_pixbuf, *tmp_pixbuf;
char **argv;
guchar buf[4096];
gboolean success;
gint outpipe = -1;
GPid child_pid;
gssize bytes;
gint width, height;
gchar *filename;
ComicsDocument *comics_document = COMICS_DOCUMENT (document);
if (!comics_document->decompress_tmp) {
argv = extract_argv (document, rc->page->index);
success = g_spawn_async_with_pipes (NULL, argv, NULL,
G_SPAWN_SEARCH_PATH |
G_SPAWN_STDERR_TO_DEV_NULL,
NULL, NULL,
&child_pid,
NULL, &outpipe, NULL, NULL);
g_strfreev (argv);
g_return_val_if_fail (success == TRUE, NULL);
loader = gdk_pixbuf_loader_new ();
g_signal_connect (loader, "size-prepared",
G_CALLBACK (render_pixbuf_size_prepared_cb),
&rc->scale);
while (outpipe >= 0) {
bytes = read (outpipe, buf, 4096);
if (bytes > 0) {
gdk_pixbuf_loader_write (loader, buf, bytes,
NULL);
} else {
close (outpipe);
gdk_pixbuf_loader_close (loader, NULL);
outpipe = -1;
}
}
tmp_pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
rotated_pixbuf =
gdk_pixbuf_rotate_simple (tmp_pixbuf,
360 - rc->rotation);
g_spawn_close_pid (child_pid);
g_object_unref (loader);
} else {
filename =
g_build_filename (comics_document->dir,
(char *) comics_document->page_names->pdata[rc->page->index],
NULL);
gdk_pixbuf_get_file_info (filename, &width, &height);
tmp_pixbuf =
gdk_pixbuf_new_from_file_at_size (
filename, width * (rc->scale) + 0.5,
height * (rc->scale) + 0.5, NULL);
rotated_pixbuf =
gdk_pixbuf_rotate_simple (tmp_pixbuf,
360 - rc->rotation);
g_free (filename);
g_object_unref (tmp_pixbuf);
}
return rotated_pixbuf;
} | Base | 1 |
static int set_expected_hash(const char *val, const struct kernel_param *kp)
{
int rv = param_set_uint(val, kp);
ksu_invalidate_manager_uid();
pr_info("ksu_expected_hash set to %x\n", ksu_expected_hash);
return rv;
} | Class | 2 |
int is_manager_apk(char *path)
{
return check_v2_signature(path, ksu_expected_size, ksu_expected_hash);
} | Class | 2 |
static void fio_worker_cleanup(void) {
/* switch to winding down */
if (fio_data->is_worker)
FIO_LOG_INFO("(%d) detected exit signal.", (int)getpid());
else
FIO_LOG_INFO("Server Detected exit signal.");
fio_state_callback_force(FIO_CALL_ON_SHUTDOWN);
for (size_t i = 0; i <= fio_data->max_protocol_fd; ++i) {
if (fd_data(i).protocol) {
fio_defer_push_task(deferred_on_shutdown, (void *)fd2uuid(i), NULL);
}
}
fio_defer_push_task(fio_cycle_unwind, NULL, NULL);
fio_defer_perform();
for (size_t i = 0; i <= fio_data->max_protocol_fd; ++i) {
if (fd_data(i).protocol || fd_data(i).open) {
fio_force_close(fd2uuid(i));
}
}
fio_defer_perform();
fio_state_callback_force(FIO_CALL_ON_FINISH);
fio_defer_perform();
if (!fio_data->is_worker) {
fio_cluster_signal_children();
while (wait(NULL) != -1)
;
}
fio_defer_perform();
fio_signal_handler_reset();
if (fio_data->parent == getpid()) {
FIO_LOG_INFO(" --- Shutdown Complete ---\n");
} else {
FIO_LOG_INFO("(%d) cleanup complete.", (int)getpid());
}
} | Base | 1 |
void fio_signal_handler_reset(void) {
struct sigaction old;
if (!fio_old_sig_int.sa_handler)
return;
memset(&old, 0, sizeof(old));
sigaction(SIGINT, &fio_old_sig_int, &old);
sigaction(SIGTERM, &fio_old_sig_term, &old);
sigaction(SIGPIPE, &fio_old_sig_pipe, &old);
if (fio_old_sig_chld.sa_handler)
sigaction(SIGCHLD, &fio_old_sig_chld, &old);
#if !FIO_DISABLE_HOT_RESTART
sigaction(SIGUSR1, &fio_old_sig_usr1, &old);
memset(&fio_old_sig_usr1, 0, sizeof(fio_old_sig_usr1));
#endif
memset(&fio_old_sig_int, 0, sizeof(fio_old_sig_int));
memset(&fio_old_sig_term, 0, sizeof(fio_old_sig_term));
memset(&fio_old_sig_pipe, 0, sizeof(fio_old_sig_pipe));
memset(&fio_old_sig_chld, 0, sizeof(fio_old_sig_chld));
} | Base | 1 |
static void fio_cluster_signal_children(void) {
if (fio_parent_pid() != getpid()) {
fio_stop();
return;
}
fio_cluster_server_sender(fio_msg_internal_create(0, FIO_CLUSTER_MSG_SHUTDOWN,
(fio_str_info_s){.len = 0},
(fio_str_info_s){.len = 0},
0, 1),
-1);
} | Base | 1 |
static void fio_cluster_listen_on_close(intptr_t uuid,
fio_protocol_s *protocol) {
free(protocol);
cluster_data.uuid = -1;
if (fio_parent_pid() == getpid()) {
#if DEBUG
FIO_LOG_DEBUG("(%d) stopped listening for cluster connections",
(int)getpid());
#endif
if (fio_data->active)
fio_stop();
}
(void)uuid;
} | Base | 1 |
static void fio_signal_handler_setup(void) {
/* setup signal handling */
struct sigaction act;
if (fio_old_sig_int.sa_handler)
return;
memset(&act, 0, sizeof(act));
act.sa_handler = sig_int_handler;
sigemptyset(&act.sa_mask);
act.sa_flags = SA_RESTART | SA_NOCLDSTOP;
if (sigaction(SIGINT, &act, &fio_old_sig_int)) {
perror("couldn't set signal handler");
return;
};
if (sigaction(SIGTERM, &act, &fio_old_sig_term)) {
perror("couldn't set signal handler");
return;
};
#if !FIO_DISABLE_HOT_RESTART
if (sigaction(SIGUSR1, &act, &fio_old_sig_usr1)) {
perror("couldn't set signal handler");
return;
};
#endif
act.sa_handler = SIG_IGN;
if (sigaction(SIGPIPE, &act, &fio_old_sig_pipe)) {
perror("couldn't set signal handler");
return;
};
} | Base | 1 |
static void __attribute__((destructor)) fio_lib_destroy(void) {
uint8_t add_eol = fio_is_master();
fio_data->active = 0;
fio_on_fork();
fio_defer_perform();
fio_state_callback_force(FIO_CALL_AT_EXIT);
fio_state_callback_clear_all();
fio_defer_perform();
fio_poll_close();
fio_timer_clear_all();
fio_free(fio_data);
/* memory library destruction must be last */
fio_mem_destroy();
FIO_LOG_DEBUG("(%d) facil.io resources released, exit complete.",
(int)getpid());
if (add_eol)
fprintf(stderr, "\n"); /* add EOL to logs (logging adds EOL before text */
} | Base | 1 |
static struct timespec fio_timer_calc_due(size_t interval) {
struct timespec now = fio_last_tick();
if (interval > 1000) {
now.tv_sec += interval / 1000;
interval -= interval / 1000;
}
now.tv_nsec += (interval * 1000000UL);
if (now.tv_nsec > 1000000000L) {
now.tv_nsec -= 1000000000L;
now.tv_sec += 1;
}
return now;
} | Base | 1 |
static void sig_int_handler(int sig) {
struct sigaction *old = NULL;
switch (sig) {
#if !FIO_DISABLE_HOT_RESTART
case SIGUSR1:
fio_signal_children_flag = 1;
old = &fio_old_sig_usr1;
break;
#endif
/* fallthrough */
case SIGINT:
if (!old)
old = &fio_old_sig_int;
/* fallthrough */
case SIGTERM:
if (!old)
old = &fio_old_sig_term;
fio_stop();
break;
case SIGPIPE:
if (!old)
old = &fio_old_sig_pipe;
/* fallthrough */
default:
break;
}
/* propagate signale handling to previous existing handler (if any) */
if (old->sa_handler != SIG_IGN && old->sa_handler != SIG_DFL)
old->sa_handler(sig);
} | Base | 1 |
inline FIO_FUNC ssize_t fio_sendfile(intptr_t uuid, intptr_t source_fd,
off_t offset, size_t length) {
return fio_write2(uuid, .data.fd = source_fd, .length = length, .is_fd = 1,
.offset = offset);
} | Base | 1 |
FIO_FUNC inline void fio_throttle_thread(size_t nano_sec) {
const struct timespec tm = {.tv_nsec = (nano_sec % 1000000000),
.tv_sec = (nano_sec / 1000000000)};
nanosleep(&tm, NULL);
} | Base | 1 |
static inline __attribute__((unused)) ssize_t fiobj_send_free(intptr_t uuid,
FIOBJ o) {
fio_str_info_s s = fiobj_obj2cstr(o);
return fio_write2(uuid, .data.buffer = (void *)(o),
.offset = (((intptr_t)s.data) - ((intptr_t)(o))),
.length = s.len, .after.dealloc = fiobj4sock_dealloc);
} | Base | 1 |
static void http1_on_ready(intptr_t uuid, fio_protocol_s *protocol) {
/* resume slow clients from suspension */
http1pr_s *p = (http1pr_s *)protocol;
if ((p->stop & 4)) {
p->stop ^= 4;
fio_force_event(uuid, FIO_EVENT_ON_DATA);
}
(void)protocol;
} | Base | 1 |
static int http1_on_response(http1_parser_s *parser) {
http1pr_s *p = parser2http(parser);
http_on_response_handler______internal(&http1_pr2handle(p), p->p.settings);
if (p->request.status_str && !p->stop)
http_finish(&p->request);
h1_reset(p);
return !p->close && fio_is_closed(p->p.uuid);
} | Base | 1 |
static inline void http1_consume_data(intptr_t uuid, http1pr_s *p) {
if (fio_pending(uuid) > 4) {
goto throttle;
}
ssize_t i = 0;
size_t org_len = p->buf_len;
int pipeline_limit = 8;
if (!p->buf_len)
return;
do {
i = http1_fio_parser(.parser = &p->parser,
.buffer = p->buf + (org_len - p->buf_len),
.length = p->buf_len, .on_request = http1_on_request,
.on_response = http1_on_response,
.on_method = http1_on_method,
.on_status = http1_on_status, .on_path = http1_on_path,
.on_query = http1_on_query,
.on_http_version = http1_on_http_version,
.on_header = http1_on_header,
.on_body_chunk = http1_on_body_chunk,
.on_error = http1_on_error);
p->buf_len -= i;
--pipeline_limit;
} while (i && p->buf_len && pipeline_limit && !p->stop);
if (p->buf_len && org_len != p->buf_len) {
memmove(p->buf, p->buf + (org_len - p->buf_len), p->buf_len);
}
if (p->buf_len == HTTP_MAX_HEADER_LENGTH) {
/* no room to read... parser not consuming data */
if (p->request.method)
http_send_error(&p->request, 413);
else {
p->request.method = fiobj_str_tmp();
http_send_error(&p->request, 413);
}
}
if (!pipeline_limit) {
fio_force_event(uuid, FIO_EVENT_ON_DATA);
}
return;
throttle:
/* throttle busy clients (slowloris) */
fio_suspend(uuid);
p->stop |= 4;
FIO_LOG_DEBUG("(HTTP/1,1) throttling client at %.*s",
(int)fio_peer_addr(uuid).len, fio_peer_addr(uuid).data);
} | Base | 1 |
static void http1_on_data_first_time(intptr_t uuid, fio_protocol_s *protocol) {
http1pr_s *p = (http1pr_s *)protocol;
ssize_t i;
i = fio_read(uuid, p->buf + p->buf_len, HTTP_MAX_HEADER_LENGTH - p->buf_len);
if (i <= 0)
return;
p->buf_len += i;
/* ensure future reads skip this first time HTTP/2.0 test */
p->p.protocol.on_data = http1_on_data;
/* Test fot HTTP/2.0 pre-knowledge */
if (i >= 24 && !memcmp(p->buf, "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", 24)) {
FIO_LOG_WARNING("client claimed unsupported HTTP/2 prior knowledge.");
fio_close(uuid);
return;
}
/* Finish handling the same way as the normal `on_data` */
http1_consume_data(uuid, p);
} | Base | 1 |
static int http1_on_error(http1_parser_s *parser) {
FIO_LOG_DEBUG("HTTP parser error at HTTP/1.1 buffer position %zu/%zu",
parser->state.next - parser2http(parser)->buf,
parser2http(parser)->buf_len);
fio_close(parser2http(parser)->p.uuid);
return -1;
} | Base | 1 |
static int http1_on_request(http1_parser_s *parser) {
http1pr_s *p = parser2http(parser);
http_on_request_handler______internal(&http1_pr2handle(p), p->p.settings);
if (p->request.method && !p->stop)
http_finish(&p->request);
h1_reset(p);
return !p->close && fio_is_closed(p->p.uuid);
} | Base | 1 |