<commit_before>
#include "httpd.h"
#include "http_config.h"
#include "http_log.h"
#include "http_protocol.h"
#include "ap_config.h"
#include "apr_tables.h"
#include "apr_strings.h"

#include "apreq2/apreq_module_apache2.h"
#include "apreq2/apreq_module.h"
#include "apreq2/apreq_util.h"

#include "qrencode.h"


#define QRG_OK 0
#define QRG_GENERAL 1
#define QRG_MISSING_TEXT 2
#define QRG_INVALID_TEXT 3

typedef struct
{
    const char *text;
    QRecLevel level;
    short case_sensitive;
} qrg_request_rec;

static int qrg_parse_request(request_rec * r, qrg_request_rec * req)
{
    apreq_handle_t *apreq;
    const apr_table_t *params;
    const char *level, *cas;
    apr_ssize_t textlen;

    /* Retrieve the apreq handle */
    apreq = apreq_handle_apache2(r);
    if (apreq == NULL) {
        ap_log_rerror(APLOG_MARK, LOG_ERR, HTTP_INTERNAL_SERVER_ERROR, r,
                      "Unable to get the apreq handle");
        return HTTP_INTERNAL_SERVER_ERROR;
    }

    /* Load the request parameters */
    params = apreq_params(apreq, r->pool);
    if (params == NULL) {
        ap_log_rerror(APLOG_MARK, LOG_WARNING, HTTP_BAD_REQUEST, r,
                      "Request did not have any parameters");
        return HTTP_BAD_REQUEST;
    }

    /* Get intermediate values */
    level = apr_table_get(params, "level");
    cas = apr_table_get(params, "case");

    /* Do we have the required 'text' param */
    req->text = apr_table_get(params, "text");
    if (req->text == NULL || (textlen = strlen(req->text)) == 0) {
        ap_log_rerror(APLOG_MARK, LOG_WARNING, HTTP_BAD_REQUEST, r,
                      "Request did not have a 'text' parameter");
        return HTTP_BAD_REQUEST;
    }

    /* Do we have the 'level' param */
    if (level == NULL || strlen(level) != 1) {
        ap_log_rerror(APLOG_MARK, LOG_DEBUG, 0, r,
                      "Request did not have a 'level' parameter, "
                      "using the default 'L'");
        req->level = QR_ECLEVEL_L;
    }
    /* Parse the 'level' param into a QRecLevel enum */
    else {
        switch ((int) *level) {
        case 'L':
        case 'l':
            req->level = QR_ECLEVEL_L;
            break;
        case 'M':
        case 'm':
            req->level = QR_ECLEVEL_M;
            break;
        case 'Q':
        case 'q':
            req->level = QR_ECLEVEL_Q;
            break;
        case 'H':
        case 'h':
            req->level = QR_ECLEVEL_H;
            break;
        default:
            ap_log_rerror(APLOG_MARK, LOG_WARNING, 0, r,
                          "Request has an invalid 'level' parameter, "
                          "using the default 'L'");
            req->level = QR_ECLEVEL_L;
        }
    }

    /* Do we have the 'case' param */
    if (cas == NULL || strlen(cas) == 0) {
        req->case_sensitive = 0;
    }
    else {
        req->case_sensitive = (*cas == '0' ? 0 : 1);
    }

    return OK;
}

#define HEADER \
"<?xml version=\"1.0\" standalone=\"no\"?>\n\
<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \
\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n\
<svg xmlns=\"http://www.w3.org/2000/svg\" \
version=\"1.1\" viewBox=\"0 0 %d %d\">\n<path d=\"\n"
#define FOOTER "\"/>\n</svg>\n"
#define DOT_PATH "M%d %dh1v1h-1z"

static char *qrcode_to_svg(apr_pool_t * p, QRcode *code)
{
    char *svg, *s;
    short row, col;
    size_t header_len = strlen(HEADER);
    size_t footer_len = strlen(FOOTER);
    size_t width_len, dot_len = 10U;

    /* How many characters are needed to display the width */
    /* QR Codes have a maximum width of 177 at this time */
    if (code->width > 9) {
        if (code->width > 99) {
            width_len = 3;
        }
        else {
            width_len = 2;
        }
    }
    else {
        width_len = 1;
    }

    /* Allocate the svg buffer */
    s = svg = apr_palloc(p,
                         /* total number of cells */
                         (code->width * code->width)
                         /* times the size of a dot */
                         * (width_len * 2 + dot_len)
                         /* plus the header and footer */
                         + (header_len + footer_len)
                         /* plus a '\n' after each row */
                         + code->width);

    s += sprintf(s, HEADER, code->width, code->width);

    for (row = 0; row < code->width; ++row) {
        for (col = 0; col < code->width; ++col) {
            /* 0x01 = black */
            if (code->data[(code->width * row) + col] & 0x01) {
                s += sprintf(s, DOT_PATH, row, col);
            }
        }
        *s++ = '\n';
    }

    memcpy(s, FOOTER, footer_len);
    *(s + footer_len) = '\0';

    return svg;
}<commit_msg>No need to check for single digit widths.<commit_after>
#include "httpd.h"
#include "http_config.h"
#include "http_log.h"
#include "http_protocol.h"
#include "ap_config.h"
#include "apr_tables.h"
#include "apr_strings.h"

#include "apreq2/apreq_module_apache2.h"
#include "apreq2/apreq_module.h"
#include "apreq2/apreq_util.h"

#include "qrencode.h"


#define QRG_OK 0
#define QRG_GENERAL 1
#define QRG_MISSING_TEXT 2
#define QRG_INVALID_TEXT 3

typedef struct
{
    const char *text;
    QRecLevel level;
    short case_sensitive;
} qrg_request_rec;

static int qrg_parse_request(request_rec * r, qrg_request_rec * req)
{
    apreq_handle_t *apreq;
    const apr_table_t *params;
    const char *level, *cas;
    apr_ssize_t textlen;

    /* Retrieve the apreq handle */
    apreq = apreq_handle_apache2(r);
    if (apreq == NULL) {
        ap_log_rerror(APLOG_MARK, LOG_ERR, HTTP_INTERNAL_SERVER_ERROR, r,
                      "Unable to get the apreq handle");
        return HTTP_INTERNAL_SERVER_ERROR;
    }

    /* Load the request parameters */
    params = apreq_params(apreq, r->pool);
    if (params == NULL) {
        ap_log_rerror(APLOG_MARK, LOG_WARNING, HTTP_BAD_REQUEST, r,
                      "Request did not have any parameters");
        return HTTP_BAD_REQUEST;
    }

    /* Get intermediate values */
    level = apr_table_get(params, "level");
    cas = apr_table_get(params, "case");

    /* Do we have the required 'text' param */
    req->text = apr_table_get(params, "text");
    if (req->text == NULL || (textlen = strlen(req->text)) == 0) {
        ap_log_rerror(APLOG_MARK, LOG_WARNING, HTTP_BAD_REQUEST, r,
                      "Request did not have a 'text' parameter");
        return HTTP_BAD_REQUEST;
    }

    /* Do we have the 'level' param */
    if (level == NULL || strlen(level) != 1) {
        ap_log_rerror(APLOG_MARK, LOG_DEBUG, 0, r,
                      "Request did not have a 'level' parameter, "
                      "using the default 'L'");
        req->level = QR_ECLEVEL_L;
    }
    /* Parse the 'level' param into a QRecLevel enum */
    else {
        switch ((int) *level) {
        case 'L':
        case 'l':
            req->level = QR_ECLEVEL_L;
            break;
        case 'M':
        case 'm':
            req->level = QR_ECLEVEL_M;
            break;
        case 'Q':
        case 'q':
            req->level = QR_ECLEVEL_Q;
            break;
        case 'H':
        case 'h':
            req->level = QR_ECLEVEL_H;
            break;
        default:
            ap_log_rerror(APLOG_MARK, LOG_WARNING, 0, r,
                          "Request has an invalid 'level' parameter, "
                          "using the default 'L'");
            req->level = QR_ECLEVEL_L;
        }
    }

    /* Do we have the 'case' param */
    if (cas == NULL || strlen(cas) == 0) {
        req->case_sensitive = 0;
    }
    else {
        req->case_sensitive = (*cas == '0' ? 0 : 1);
    }

    return OK;
}

#define HEADER \
"<?xml version=\"1.0\" standalone=\"no\"?>\n\
<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \
\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n\
<svg xmlns=\"http://www.w3.org/2000/svg\" \
version=\"1.1\" viewBox=\"0 0 %d %d\">\n<path d=\"\n"
#define FOOTER "\"/>\n</svg>\n"
#define DOT_PATH "M%d %dh1v1h-1z"

static char *qrcode_to_svg(apr_pool_t * p, QRcode *code)
{
    char *svg, *s;
    short row, col;
    size_t header_len = strlen(HEADER);
    size_t footer_len = strlen(FOOTER);
    size_t width_len, dot_len = 10U;

    /* How many characters are needed to display the width */
    /* QR Code widths are currently 21 <= width <= 177 */
    if (code->width > 99) {
        width_len = 3;
    }
    else {
        width_len = 2;
    }

    /* Allocate the svg buffer */
    s = svg = apr_palloc(p,
                         /* total number of cells */
                         (code->width * code->width)
                         /* times the size of a dot */
                         * (width_len * 2 + dot_len)
                         /* plus the header and footer */
                         + (header_len + footer_len)
                         /* plus a '\n' after each row */
                         + code->width);

    s += sprintf(s, HEADER, code->width, code->width);

    for (row = 0; row < code->width; ++row) {
        for (col = 0; col < code->width; ++col) {
            /* 0x01 = black */
            if (code->data[(code->width * row) + col] & 0x01) {
                s += sprintf(s, DOT_PATH, row, col);
            }
        }
        *s++ = '\n';
    }

    memcpy(s, FOOTER, footer_len);
    *(s + footer_len) = '\0';

    return svg;
}
{'fraction_non_alphanumeric': 0.13689021149962802, 'fraction_numerical': 0.012966308853225635, 'mean_word_length': 1.8317785133915137, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 2, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7409843', 'n_tokens_mistral': 3337, 'n_tokens_neox': 3161, 'n_words': 812}