        -:    0:Source:/home/dsg/apr_trunk/json/apr_json_encode.c
        -:    0:Graph:./json/.libs/apr_json_encode.gcno
        -:    0:Data:./json/.libs/apr_json_encode.gcda
        -:    0:Runs:346
        -:    1:/* Licensed to the Apache Software Foundation (ASF) under one or more
        -:    2: * contributor license agreements.  See the NOTICE file distributed with
        -:    3: * this work for additional information regarding copyright ownership.
        -:    4: * The ASF licenses this file to You under the Apache License, Version 2.0
        -:    5: * (the "License"); you may not use this file except in compliance with
        -:    6: * the License.  You may obtain a copy of the License at
        -:    7: *
        -:    8: *     http://www.apache.org/licenses/LICENSE-2.0
        -:    9: *
        -:   10: * Unless required by applicable law or agreed to in writing, software
        -:   11: * distributed under the License is distributed on an "AS IS" BASIS,
        -:   12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
        -:   13: * See the License for the specific language governing permissions and
        -:   14: * limitations under the License.
        -:   15: */
        -:   16:
        -:   17:#include "apr_json.h"
        -:   18:
        -:   19:#if !APR_CHARSET_EBCDIC
        -:   20:
        -:   21:typedef struct apr_json_serializer_t {
        -:   22:    apr_pool_t *pool;
        -:   23:    apr_bucket_brigade *brigade;
        -:   24:    apr_brigade_flush flush;
        -:   25:    void *ctx;
        -:   26:    int flags;
        -:   27:} apr_json_serializer_t;
        -:   28:
        -:   29:static apr_status_t apr_json_encode_value(apr_json_serializer_t * self,
        -:   30:                                            const apr_json_value_t * value);
        -:   31:
    #####:   32:static apr_status_t apr_json_brigade_write(apr_json_serializer_t * self,
        -:   33:               const char *chunk, apr_size_t chunk_len, const char *escaped)
        -:   34:{
        -:   35:    apr_status_t status;
        -:   36:
    #####:   37:    status = apr_brigade_write(self->brigade, self->flush, self->ctx, chunk, chunk_len);
    #####:   38:    if (APR_SUCCESS == status) {
    #####:   39:        status = apr_brigade_puts(self->brigade, self->flush, self->ctx, escaped);
        -:   40:    }
        -:   41:
    #####:   42:    return status;
        -:   43:}
        -:   44:
    #####:   45:static apr_status_t apr_json_brigade_printf(apr_json_serializer_t * self,
        -:   46:               const char *chunk, apr_size_t chunk_len, const char *fmt, ...)
        -:   47:{
        -:   48:    va_list ap;
        -:   49:    apr_status_t status;
        -:   50:
    #####:   51:    status = apr_brigade_write(self->brigade, self->flush, self->ctx, chunk,
        -:   52:            chunk_len);
    #####:   53:    if (APR_SUCCESS == status) {
    #####:   54:        va_start(ap, fmt);
    #####:   55:        status = apr_brigade_vprintf(self->brigade, self->flush, self->ctx, fmt,
        -:   56:                ap);
    #####:   57:        va_end(ap);
        -:   58:    }
        -:   59:
    #####:   60:    return status;
        -:   61:}
        -:   62:
      268:   63:static apr_status_t apr_json_encode_string(apr_json_serializer_t * self,
        -:   64:        const apr_json_string_t * string)
        -:   65:{
        -:   66:    apr_status_t status;
        -:   67:    const char *p, *e, *chunk;
      268:   68:    const char invalid[4] = { 0xEF, 0xBF, 0xBD, 0x00 };
        -:   69:    unsigned char c;
        -:   70:
      268:   71:    status = apr_brigade_putc(self->brigade, self->flush, self->ctx, '\"');
      268:   72:    if (APR_SUCCESS != status) {
    #####:   73:        return status;
        -:   74:    }
        -:   75:
      268:   76:    for (p = chunk = string->p, e = string->p
      268:   77:            + (APR_JSON_VALUE_STRING == string->len ?
     5236:   78:                    strlen(string->p) : string->len); p < e; p++) {
     4700:   79:        switch (*p) {
    #####:   80:        case '\n':
    #####:   81:            status = apr_json_brigade_write(self, chunk, p - chunk, "\\n");
    #####:   82:            chunk = p + 1;
    #####:   83:            break;
    #####:   84:        case '\r':
    #####:   85:            status = apr_json_brigade_write(self, chunk, p - chunk, "\\r");
    #####:   86:            chunk = p + 1;
    #####:   87:            break;
    #####:   88:        case '\t':
    #####:   89:            status = apr_json_brigade_write(self, chunk, p - chunk, "\\t");
    #####:   90:            chunk = p + 1;
    #####:   91:            break;
    #####:   92:        case '\b':
    #####:   93:            status = apr_json_brigade_write(self, chunk, p - chunk, "\\b");
    #####:   94:            chunk = p + 1;
    #####:   95:            break;
    #####:   96:        case '\f':
    #####:   97:            status = apr_json_brigade_write(self, chunk, p - chunk, "\\f");
    #####:   98:            chunk = p + 1;
    #####:   99:            break;
    #####:  100:        case '\\':
    #####:  101:            status = apr_json_brigade_write(self, chunk, p - chunk, "\\\\");
    #####:  102:            chunk = p + 1;
    #####:  103:            break;
    #####:  104:        case '"':
    #####:  105:            status = apr_json_brigade_write(self, chunk, p - chunk, "\\\"");
    #####:  106:            chunk = p + 1;
    #####:  107:            break;
     4700:  108:        default:
     4700:  109:            c = (unsigned char)(*p);
     4700:  110:            apr_size_t left = e - p;
     4700:  111:            if (c < 0x20) {
    #####:  112:                status = apr_json_brigade_printf(self, chunk, p - chunk,
        -:  113:                        "\\u%04x", c);
    #####:  114:                chunk = p + 1;
        -:  115:            }
     4700:  116:            else if (((c >> 7) == 0x00)) {
        -:  117:                /* 1 byte */
        -:  118:            }
    #####:  119:            else if (left > 1 && ((c >> 5) == 0x06) && p[1]) {
        -:  120:                /* 2 bytes */
    #####:  121:                if (left < 2 || (p[1] >> 6) != 0x02) {
    #####:  122:                    status = apr_json_brigade_write(self, chunk, p - chunk,
        -:  123:                            invalid);
    #####:  124:                    chunk = p + 1;
        -:  125:                }
        -:  126:            }
    #####:  127:            else if (((c >> 4) == 0x0E)) {
        -:  128:                /* 3 bytes */
    #####:  129:                if (left < 3 || (p[1] >> 6) != 0x02 || (p[2] >> 6) != 0x02) {
    #####:  130:                    status = apr_json_brigade_write(self, chunk, p - chunk,
        -:  131:                            invalid);
    #####:  132:                    chunk = p + 1;
        -:  133:                }
        -:  134:            }
    #####:  135:            else if ((c >> 3) == 0x1E) {
        -:  136:                /* 4 bytes */
    #####:  137:                if (left < 4 || (p[1] >> 6) != 0x02 || (p[2] >> 6) != 0x02 || (p[3] >> 6) != 0x02) {
    #####:  138:                    status = apr_json_brigade_write(self, chunk, p - chunk,
        -:  139:                            invalid);
    #####:  140:                    chunk = p + 1;
        -:  141:                }
        -:  142:            }
        -:  143:            else {
    #####:  144:                status = apr_json_brigade_write(self, chunk, p - chunk,
        -:  145:                        invalid);
    #####:  146:                chunk = p + 1;
        -:  147:            }
     4700:  148:            break;
        -:  149:        }
        -:  150:
     4700:  151:        if (APR_SUCCESS != status) {
    #####:  152:            return status;
        -:  153:        }
        -:  154:    }
        -:  155:
      268:  156:    if (chunk < p) {
      268:  157:        status = apr_brigade_write(self->brigade, self->flush, self->ctx, chunk, p - chunk);
      268:  158:        if (APR_SUCCESS != status) {
    #####:  159:            return status;
        -:  160:        }
        -:  161:    }
        -:  162:
      268:  163:    return apr_brigade_putc(self->brigade, self->flush, self->ctx, '\"');
        -:  164:}
        -:  165:
        -:  166:
        6:  167:static apr_status_t apr_json_encode_array(apr_json_serializer_t * self,
        -:  168:        const apr_json_value_t * array)
        -:  169:{
        -:  170:    apr_status_t status;
        -:  171:    apr_json_value_t *val;
        6:  172:    apr_size_t count = 0;
        -:  173:
        6:  174:    status = apr_brigade_putc(self->brigade, self->flush, self->ctx, '[');
        6:  175:    if (APR_SUCCESS != status) {
    #####:  176:        return status;
        -:  177:    }
        -:  178:
        6:  179:    val = apr_json_array_first(array);
       22:  180:    while (val) {
        -:  181:
       16:  182:        if (count > 0) {
       10:  183:            status = apr_brigade_putc(self->brigade, self->flush, self->ctx, ',');
       10:  184:            if (APR_SUCCESS != status) {
    #####:  185:                return status;
        -:  186:            }
        -:  187:        }
        -:  188:
       16:  189:        status = apr_json_encode_value(self, val);
       16:  190:        if (APR_SUCCESS != status) {
    #####:  191:            return status;
        -:  192:        }
        -:  193:
       16:  194:        val = apr_json_array_next(array, val);
       16:  195:        count++;
        -:  196:    }
        -:  197:
        6:  198:    return apr_brigade_putc(self->brigade, self->flush, self->ctx, ']');
        -:  199:}
        -:  200:
       68:  201:static apr_status_t apr_json_encode_object(apr_json_serializer_t * self, apr_json_object_t * object)
        -:  202:{
        -:  203:    apr_status_t status;
        -:  204:    apr_json_kv_t *kv;
       68:  205:    int first = 1;
       68:  206:    status = apr_brigade_putc(self->brigade, self->flush, self->ctx, '{');
       68:  207:    if (APR_SUCCESS != status) {
    #####:  208:        return status;
        -:  209:    }
        -:  210:
       68:  211:    for (kv = APR_RING_FIRST(&(object)->list);
      224:  212:         kv != APR_RING_SENTINEL(&(object)->list, apr_json_kv_t, link);
      156:  213:         kv = APR_RING_NEXT((kv), link)) {
        -:  214:
      156:  215:        if (!first) {
       88:  216:            status = apr_brigade_putc(self->brigade, self->flush, self->ctx, ',');
       88:  217:            if (APR_SUCCESS != status) {
    #####:  218:                return status;
        -:  219:            }
        -:  220:        }
        -:  221:
        -:  222:        {
      156:  223:            status = apr_json_encode_value(self, kv->k);
      156:  224:            if (APR_SUCCESS != status) {
    #####:  225:                return status;
        -:  226:            }
        -:  227:
      156:  228:            status = apr_brigade_putc(self->brigade, self->flush, self->ctx, ':');
      156:  229:            if (APR_SUCCESS != status) {
    #####:  230:                return status;
        -:  231:            }
        -:  232:
      156:  233:            status = apr_json_encode_value(self, kv->v);
      156:  234:            if (APR_SUCCESS != status) {
    #####:  235:                return status;
        -:  236:            }
        -:  237:        }
      156:  238:        first = 0;
        -:  239:    }
       68:  240:    return apr_brigade_putc(self->brigade, self->flush, self->ctx, '}');
        -:  241:}
        -:  242:
      368:  243:static apr_status_t apr_json_encode_value(apr_json_serializer_t * self, const apr_json_value_t * value)
        -:  244:{
      368:  245:    apr_status_t status = APR_SUCCESS;
        -:  246:
      368:  247:    if (value->pre && (self->flags & APR_JSON_FLAGS_WHITESPACE)) {
       48:  248:        status = apr_brigade_puts(self->brigade, self->flush, self->ctx,
       48:  249:                value->pre);
        -:  250:    }
        -:  251:
      368:  252:    if (APR_SUCCESS == status) {
      368:  253:        switch (value->type) {
      268:  254:        case APR_JSON_STRING:
      268:  255:            status = apr_json_encode_string(self, &value->value.string);
      268:  256:            break;
       18:  257:        case APR_JSON_LONG:
       18:  258:            status = apr_brigade_printf(self->brigade, self->flush, self->ctx,
       18:  259:                    "%" APR_INT64_T_FMT, value->value.lnumber);
       18:  260:            break;
        2:  261:        case APR_JSON_DOUBLE:
        2:  262:            status = apr_brigade_printf(self->brigade, self->flush, self->ctx,
        2:  263:                    "%lf", value->value.dnumber);
        2:  264:            break;
        4:  265:        case APR_JSON_BOOLEAN:
        4:  266:            status = apr_brigade_puts(self->brigade, self->flush, self->ctx,
        4:  267:                    value->value.boolean ? "true" : "false");
        4:  268:            break;
        2:  269:        case APR_JSON_NULL:
        2:  270:            status = apr_brigade_puts(self->brigade, self->flush, self->ctx,
        -:  271:                    "null");
        2:  272:            break;
       68:  273:        case APR_JSON_OBJECT:
       68:  274:            status = apr_json_encode_object(self, value->value.object);
       68:  275:            break;
        6:  276:        case APR_JSON_ARRAY:
        6:  277:            status = apr_json_encode_array(self, value);
        6:  278:            break;
    #####:  279:        default:
    #####:  280:            return APR_EINVAL;
        -:  281:        }
        -:  282:    }
        -:  283:
      368:  284:    if (APR_SUCCESS == status && value->post
       26:  285:            && (self->flags & APR_JSON_FLAGS_WHITESPACE)) {
       26:  286:        status = apr_brigade_puts(self->brigade, self->flush, self->ctx,
       26:  287:                value->post);
        -:  288:    }
        -:  289:
      368:  290:    return status;
        -:  291:}
        -:  292:
       40:  293:APR_DECLARE(apr_status_t) apr_json_encode(apr_bucket_brigade * brigade,
        -:  294:                                          apr_brigade_flush flush,
        -:  295:                                          void *ctx, const apr_json_value_t * json,
        -:  296:                                          int flags, apr_pool_t * pool)
        -:  297:{
       40:  298:    apr_json_serializer_t serializer = {pool, brigade, flush, ctx, flags};
       40:  299:    return apr_json_encode_value(&serializer, json);
        -:  300:}
        -:  301:
        -:  302:#else
        -:  303: /* we do not yet support JSON on EBCDIC platforms, but will do in future */
        -:  304:apr_status_t apr_json_encode(apr_bucket_brigade * brigade, apr_brigade_flush flush,
        -:  305:                void *ctx, const apr_json_value_t * json, apr_pool_t * pool)
        -:  306:{
        -:  307:    return APR_ENOTIMPL;
        -:  308:}
        -:  309:#endif
