+ * 1 : csp = Current client state (buffers, headers, etc...)
+ * 2 : name = Name of the external filter to execute
+ * 3 : content = The original content to filter
+ * 4 : size = The size of the content buffer
+ *
+ * Returns : a pointer to the (newly allocated) modified buffer.
+ * or NULL if there were no hits or something went wrong
+ *
+ *********************************************************************/
+static char *execute_external_filter(const struct client_state *csp,
+ const char *name, char *content, size_t *size)
+{
+ char cmd[200];
+ char file_name[FILENAME_MAX];
+ FILE *fp;
+ char *filter_output;
+ int fd;
+ int ret;
+ size_t new_size;
+ const char *external_filter;
+
+ if (csp->config->temporary_directory == NULL)
+ {
+ log_error(LOG_LEVEL_ERROR,
+ "No temporary-directory configured. Can't execute filter: %s",
+ name);
+ return NULL;
+ }
+
+ external_filter = get_external_filter(csp, name);
+
+ if (sizeof(file_name) < snprintf(file_name, sizeof(file_name),
+ "%s/privoxy-XXXXXXXX", csp->config->temporary_directory))
+ {
+ log_error(LOG_LEVEL_ERROR, "temporary-directory path too long");
+ return NULL;
+ }
+
+ fd = mkstemp(file_name);
+ if (fd == -1)
+ {
+ log_error(LOG_LEVEL_ERROR, "mkstemp() failed to create %s: %E", file_name);
+ return NULL;
+ }
+
+ fp = fdopen(fd, "w");
+ if (fp == NULL)
+ {
+ log_error(LOG_LEVEL_ERROR, "fdopen() failed: %E");
+ unlink(file_name);
+ return NULL;
+ }
+
+ /*
+ * The size may be zero if a previous filter discarded everything.
+ *
+ * This isn't necessary unintentional, so we just don't try
+ * to fwrite() nothing and let the user deal with the rest.
+ */
+ if ((*size != 0) && fwrite(content, *size, 1, fp) != 1)
+ {
+ log_error(LOG_LEVEL_ERROR, "fwrite(..., %lu, 1, ..) failed: %E", *size);
+ unlink(file_name);
+ fclose(fp);
+ return NULL;
+ }
+ fclose(fp);
+
+ if (sizeof(cmd) < snprintf(cmd, sizeof(cmd), "%s < %s", external_filter, file_name))
+ {
+ log_error(LOG_LEVEL_ERROR,
+ "temporary-directory or external filter path too long");
+ unlink(file_name);
+ return NULL;
+ }
+
+ log_error(LOG_LEVEL_RE_FILTER, "Executing '%s': %s", name, cmd);
+
+ /*
+ * The locking is necessary to prevent other threads
+ * from overwriting the environment variables before
+ * the popen fork. Afterwards this no longer matters.
+ */
+ privoxy_mutex_lock(&external_filter_mutex);
+ set_privoxy_variables(csp);
+ fp = popen(cmd, "r");
+ privoxy_mutex_unlock(&external_filter_mutex);
+ if (fp == NULL)
+ {
+ log_error(LOG_LEVEL_ERROR, "popen(\"%s\", \"r\") failed: %E", cmd);
+ unlink(file_name);
+ return NULL;
+ }
+
+ /* Allocate at least one byte */
+ filter_output = malloc_or_die(*size + 1);
+
+ new_size = 0;
+ while (!feof(fp) && !ferror(fp))
+ {
+ size_t len;
+ /* Could be bigger ... */
+ enum { READ_LENGTH = 2048 };
+
+ if (new_size + READ_LENGTH >= *size)
+ {
+ char *p;
+
+ /* Could be considered wasteful if the content is 'large'. */
+ *size += (*size >= READ_LENGTH) ? *size : READ_LENGTH;
+
+ p = realloc(filter_output, *size);
+ if (p == NULL)
+ {
+ log_error(LOG_LEVEL_ERROR, "Out of memory while reading "
+ "external filter output. Using what we got so far.");
+ break;
+ }
+ filter_output = p;
+ }
+ assert(new_size + READ_LENGTH < *size);
+ len = fread(&filter_output[new_size], 1, READ_LENGTH, fp);
+ if (len > 0)
+ {
+ new_size += len;
+ }
+ }
+
+ ret = pclose(fp);
+ if (ret == -1)
+ {
+ log_error(LOG_LEVEL_ERROR, "Executing %s failed: %E", cmd);
+ }
+ else
+ {
+ log_error(LOG_LEVEL_RE_FILTER,
+ "Executing '%s' resulted in return value %d. "
+ "Read %lu of up to %lu bytes.", name, (ret >> 8), new_size, *size);
+ }
+
+ unlink(file_name);
+ *size = new_size;
+
+ return filter_output;
+
+}
+#endif /* def FEATURE_EXTERNAL_FILTERS */
+
+
+/*********************************************************************
+ *
+ * Function : gif_deanimate_response
+ *
+ * Description : Deanimate the GIF image that has been accumulated in
+ * csp->iob->buf, set csp->content_length to the modified
+ * size and raise the CSP_FLAG_MODIFIED flag.
+ *
+ * Parameters :
+ * 1 : csp = Current client state (buffers, headers, etc...)
+ *
+ * Returns : a pointer to the (newly allocated) modified buffer.
+ * or NULL in case something went wrong.
+ *
+ *********************************************************************/
+#ifdef FUZZ
+char *gif_deanimate_response(struct client_state *csp)
+#else
+static char *gif_deanimate_response(struct client_state *csp)
+#endif
+{
+ struct binbuffer *in, *out;
+ char *p;
+ size_t size;
+
+ size = (size_t)(csp->iob->eod - csp->iob->cur);
+
+ in = zalloc_or_die(sizeof(*in));
+ out = zalloc_or_die(sizeof(*out));
+
+ in->buffer = csp->iob->cur;
+ in->size = size;
+
+ if (gif_deanimate(in, out, strncmp("last", csp->action->string[ACTION_STRING_DEANIMATE], 4)))
+ {
+ log_error(LOG_LEVEL_DEANIMATE, "failed! (gif parsing)");
+ freez(in);
+ buf_free(out);
+ return(NULL);
+ }
+ else
+ {
+ if ((int)size == out->offset)
+ {
+ log_error(LOG_LEVEL_DEANIMATE, "GIF not changed.");
+ }
+ else
+ {
+ log_error(LOG_LEVEL_DEANIMATE,
+ "Success! GIF shrunk from %lu bytes to %lu.", size, out->offset);
+ }
+ csp->content_length = out->offset;
+ csp->flags |= CSP_FLAG_MODIFIED;
+ p = out->buffer;
+ freez(in);
+ freez(out);
+ return(p);
+ }
+
+}
+
+
+/*********************************************************************
+ *
+ * Function : get_filter_function
+ *
+ * Description : Decides which content filter function has
+ * to be applied (if any). Only considers functions
+ * for internal filters which are mutually-exclusive.
+ *
+ * Parameters :
+ * 1 : csp = Current client state (buffers, headers, etc...)
+ *
+ * Returns : The content filter function to run, or
+ * NULL if no content filter is active
+ *
+ *********************************************************************/
+static filter_function_ptr get_filter_function(const struct client_state *csp)
+{
+ filter_function_ptr filter_function = NULL;
+
+ /*
+ * Choose the applying filter function based on
+ * the content type and action settings.
+ */
+ if ((csp->content_type & CT_TEXT) &&
+ (!list_is_empty(csp->action->multi[ACTION_MULTI_FILTER])))
+ {
+ filter_function = pcrs_filter_response;
+ }
+ else if ((csp->content_type & CT_GIF) &&
+ (csp->action->flags & ACTION_DEANIMATE))
+ {
+ filter_function = gif_deanimate_response;
+ }
+
+ return filter_function;
+}
+
+
+/*********************************************************************
+ *
+ * Function : remove_chunked_transfer_coding
+ *
+ * Description : In-situ remove the "chunked" transfer coding as defined
+ * in RFC 7230 4.1 from a buffer. XXX: The implementation
+ * is neither complete nor compliant (TODO #129).
+ *
+ * Parameters :
+ * 1 : buffer = Pointer to the text buffer
+ * 2 : size = In: Number of bytes to be processed,
+ * Out: Number of bytes after de-chunking.
+ * (undefined in case of errors)