Пример #1
0
static muse_cell json_read( muse_port_t p )
{
	json_skip_whitespace(p);

	if ( !port_eof(p) ) {
		muse_char c = port_getchar(p);
		port_ungetchar(c,p);
		switch ( c ) {
			case '"': return json_read_string(p);
			case '-': 
			case '0':
			case '1':
			case '2':
			case '3':
			case '4':
			case '5':
			case '6':
			case '7':
			case '8':
			case '9': return json_read_number(p);
			case '[': return json_read_array(p);
			case '{': return json_read_object(p);
			default:
				if ( c >= 'a' && c <= 'z' )
					return json_read_keyword(p);
				else {
					return muse_raise_error( p->env, muse_csymbol( p->env, L"json:syntax-error" ), MUSE_NIL );
				}
		}
	} else {
		return muse_raise_error( p->env, muse_csymbol( p->env, L"json:unexpected-end-of-stream" ), MUSE_NIL );
	}
}
Пример #2
0
static void read_port_server_property(InputStream * inp, const char * name,
        void * args) {
    PortAttribute ** attrs = (PortAttribute **) args;
    PortAttribute * attr = (PortAttribute *)loc_alloc_zero(sizeof(PortAttribute));
    attr->value = json_read_object(inp);
    attr->name = loc_strdup(name);
    attr->next = *attrs;
    *attrs = attr;
}
static void read_port_server_property(InputStream * inp, const char * name,
        void * args) {
    PortRedirectionInfo * port = args;
    if (strcmp(name, "LocalPort") == 0) port->local_port =
            (unsigned int) json_read_uint64(inp);
    else {
        PortAttribute * attr = (PortAttribute *)loc_alloc_zero(sizeof(PortAttribute));
        attr->value = json_read_object(inp);
        attr->name = loc_strdup(name);
        attr->next = port->attrs;
        port->attrs = attr;
    }
}
Пример #4
0
static void read_reset_params(InputStream * inp, const char * name, void * x) {
    ResetParams * params = (ResetParams *)x;

    if (strcmp(name, "Suspend") == 0) {
        params->suspend = json_read_boolean(inp);
    }
    else {
        ResetParameter * param = (ResetParameter *)loc_alloc_zero(sizeof(ResetParameter));
        param->name = loc_strdup(name);
        param->value = json_read_object(inp);
        param->next = params->list;
        params->list = param;
    }
}
Пример #5
0
int json_ais_read(const char *buf,
		  char *path, size_t pathlen, struct ais_t *ais,
		  /*@null@*/ const char **endptr)
{
    /* collected but not actually used yet */
    bool scaled;
    /*@-compdef@*//* splint is confused by storage declared in the .i file */
    /*@-nullstate@*/

#define AIS_HEADER \
	{"class",          t_check,    .dflt.check = "AIS"}, \
	{"type",           t_uinteger, .addr.uinteger = &ais->type}, \
	{"device",         t_string,   .addr.string = path, \
	                                  .len = pathlen}, \
	{"repeat",         t_uinteger, .addr.uinteger = &ais->repeat}, \
	{"scaled",         t_boolean,  .addr.boolean = &scaled, \
		                          .dflt.boolean = false}, \
	{"mmsi",           t_uinteger, .addr.uinteger = &ais->mmsi},

#define AIS_TYPE6 \
	{"seqno",         t_uinteger,  .addr.uinteger = &ais->type6.seqno,\
                                       .dflt.uinteger = 0},\
	{"dest_mmsi",     t_uinteger,  .addr.uinteger = &ais->type6.dest_mmsi,\
                                       .dflt.uinteger = 0},\
	{"retransmit",    t_boolean,   .addr.boolean = &ais->type6.retransmit,\
                                       .dflt.boolean = false},\
	{"dac",           t_uinteger,  .addr.uinteger = &ais->type6.dac,\
                                       .dflt.uinteger = 0},\
	{"fid",           t_uinteger,  .addr.uinteger = &ais->type6.fid,\
                                       .dflt.uinteger = 0},
#define AIS_TYPE8 \
	{"dac",           t_uinteger,  .addr.uinteger = &ais->type8.dac,\
                                       .dflt.uinteger = 0},\
	{"fid",           t_uinteger,  .addr.uinteger = &ais->type8.fid,\
                                       .dflt.uinteger = 0},

    int status;

#include "ais_json.i"		/* JSON parser template structures */

#undef AIS_HEADER

    memset(ais, '\0', sizeof(struct ais_t));

    /*@-usedef@*/
    if (strstr(buf, "\"type\":1,") != NULL
	|| strstr(buf, "\"type\":2,") != NULL
	|| strstr(buf, "\"type\":3,") != NULL) {
	status = json_read_object(buf, json_ais1, endptr);
    } else if (strstr(buf, "\"type\":4,") != NULL
	       || strstr(buf, "\"type\":11,") != NULL) {
	status = json_read_object(buf, json_ais4, endptr);
	if (status == 0) {
	    ais->type4.year = AIS_YEAR_NOT_AVAILABLE;
	    ais->type4.month = AIS_MONTH_NOT_AVAILABLE;
	    ais->type4.day = AIS_DAY_NOT_AVAILABLE;
	    ais->type4.hour = AIS_HOUR_NOT_AVAILABLE;
	    ais->type4.minute = AIS_MINUTE_NOT_AVAILABLE;
	    ais->type4.second = AIS_SECOND_NOT_AVAILABLE;
	    (void)sscanf(timestamp, "%4u-%02u-%02uT%02u:%02u:%02uZ",
			 &ais->type4.year,
			 &ais->type4.month,
			 &ais->type4.day,
			 &ais->type4.hour,
			 &ais->type4.minute, 
			 &ais->type4.second);
	}
    } else if (strstr(buf, "\"type\":5,") != NULL) {
	status = json_read_object(buf, json_ais5, endptr);
	if (status == 0) {
	    ais->type5.month = AIS_MONTH_NOT_AVAILABLE;
	    ais->type5.day = AIS_DAY_NOT_AVAILABLE;
	    ais->type5.hour = AIS_HOUR_NOT_AVAILABLE;
	    ais->type5.minute = AIS_MINUTE_NOT_AVAILABLE;
	    (void)sscanf(eta, "%02u-%02uT%02u:%02uZ",
			 &ais->type5.month,
			 &ais->type5.day,
			 &ais->type5.hour, 
			 &ais->type5.minute);
	}
    } else if (strstr(buf, "\"type\":6,") != NULL) {
	bool imo = false;
	if (strstr(buf, "\"dac\":1,") != NULL) {
	    if (strstr(buf, "\"fid\":12,") != NULL) {
		status = json_read_object(buf, json_ais6_fid12, endptr);
		if (status == 0) {
		    ais->type6.dac1fid12.lmonth = AIS_MONTH_NOT_AVAILABLE;
		    ais->type6.dac1fid12.lday = AIS_DAY_NOT_AVAILABLE;
		    ais->type6.dac1fid12.lhour = AIS_HOUR_NOT_AVAILABLE;
		    ais->type6.dac1fid12.lminute = AIS_MINUTE_NOT_AVAILABLE;
		    (void)sscanf(departure, "%02u-%02uT%02u:%02uZ",
				 &ais->type6.dac1fid12.lmonth,
				 &ais->type6.dac1fid12.lday,
				 &ais->type6.dac1fid12.lhour, 
				 &ais->type6.dac1fid12.lminute);
		    ais->type6.dac1fid12.nmonth = AIS_MONTH_NOT_AVAILABLE;
		    ais->type6.dac1fid12.nday = AIS_DAY_NOT_AVAILABLE;
		    ais->type6.dac1fid12.nhour = AIS_HOUR_NOT_AVAILABLE;
		    ais->type6.dac1fid12.nminute = AIS_MINUTE_NOT_AVAILABLE;
		    (void)sscanf(eta, "%02u-%02uT%02u:%02uZ",
				 &ais->type6.dac1fid12.nmonth,
				 &ais->type6.dac1fid12.nday,
				 &ais->type6.dac1fid12.nhour, 
				 &ais->type6.dac1fid12.nminute);
		}
		imo = true;
	    }
	    else if (strstr(buf, "\"fid\":15,") != NULL) {
		status = json_read_object(buf, json_ais6_fid15, endptr);
		imo = true;
	    }
	    else if (strstr(buf, "\"fid\":16,") != NULL) {
		status = json_read_object(buf, json_ais6_fid16, endptr);
		imo = true;
	    }
	    else if (strstr(buf, "\"fid\":18,") != NULL || strstr(buf, "\"fid\":11,") != NULL) {
		status = json_read_object(buf, json_ais6_fid18, endptr);
		if (status == 0) {
		    ais->type6.dac1fid18.day = AIS_DAY_NOT_AVAILABLE;
		    ais->type6.dac1fid18.hour = AIS_HOUR_NOT_AVAILABLE;
		    ais->type6.dac1fid18.minute = AIS_MINUTE_NOT_AVAILABLE;
		    (void)sscanf(arrival, "%02uT%02u:%02uZ",
				 &ais->type6.dac1fid18.day,
				 &ais->type6.dac1fid18.hour, 
				 &ais->type6.dac1fid18.minute);
		}
		imo = true;
	    }
	    else if (strstr(buf, "\"fid\":20,") != NULL) {
		status = json_read_object(buf, json_ais6_fid20, endptr);
		if (status == 0) {
		    ais->type6.dac1fid20.month = AIS_MONTH_NOT_AVAILABLE;
		    ais->type6.dac1fid20.day = AIS_DAY_NOT_AVAILABLE;
		    ais->type6.dac1fid20.hour = AIS_HOUR_NOT_AVAILABLE;
		    ais->type6.dac1fid20.minute = AIS_MINUTE_NOT_AVAILABLE;
		    (void)sscanf(arrival, "%02u-%02uT%02u:%02uZ",
				 &ais->type6.dac1fid20.month,
				 &ais->type6.dac1fid20.day,
				 &ais->type6.dac1fid20.hour, 
				 &ais->type6.dac1fid20.minute);
		}
		imo = true;
	    }
	    else if (strstr(buf, "\"fid\":25,") != NULL) {
		status = json_read_object(buf, json_ais6_fid25, endptr);
		imo = true;
	    }
	    else if (strstr(buf, "\"fid\":28,") != NULL) {
		status = json_read_object(buf, json_ais6_fid28, endptr);
		if (status == 0) {
		    ais->type6.dac1fid28.month = AIS_MONTH_NOT_AVAILABLE;
		    ais->type6.dac1fid28.day = AIS_DAY_NOT_AVAILABLE;
		    ais->type6.dac1fid28.hour = AIS_HOUR_NOT_AVAILABLE;
		    ais->type6.dac1fid28.minute = AIS_MINUTE_NOT_AVAILABLE;
		    (void)sscanf(start, "%02u-%02uT%02u:%02uZ",
				 &ais->type6.dac1fid28.month,
				 &ais->type6.dac1fid28.day,
				 &ais->type6.dac1fid28.hour, 
				 &ais->type6.dac1fid28.minute);
		}
		imo = true;
	    }
	    else if (strstr(buf, "\"fid\":30,") != NULL) {
		status = json_read_object(buf, json_ais6_fid30, endptr);
		imo = true;
	    }
	    else if (strstr(buf, "\"fid\":32,") != NULL || strstr(buf, "\"fid\":14,") != NULL) {
		status = json_read_object(buf, json_ais6_fid32, endptr);
		imo = true;
	    }
	}
	if (!imo) {
	    status = json_read_object(buf, json_ais6, endptr);
	    if (status == 0)
		lenhex_unpack(data, &ais->type6.bitcount,
			      ais->type6.bitdata, sizeof(ais->type6.bitdata));
	}
    } else if (strstr(buf, "\"type\":7,") != NULL
	       || strstr(buf, "\"type\":13,") != NULL) {
	status = json_read_object(buf, json_ais7, endptr);
    } else if (strstr(buf, "\"type\":8,") != NULL) {
	bool imo = false;
	if (strstr(buf, "\"dac\":1,") != NULL) {
	    if (strstr(buf, "\"fid\":13,") != NULL) {
		status = json_read_object(buf, json_ais8_fid13, endptr);
		if (status == 0) {
		    ais->type8.dac1fid13.fmonth = AIS_MONTH_NOT_AVAILABLE;
		    ais->type8.dac1fid13.fday = AIS_DAY_NOT_AVAILABLE;
		    ais->type8.dac1fid13.fhour = AIS_HOUR_NOT_AVAILABLE;
		    ais->type8.dac1fid13.fminute = AIS_MINUTE_NOT_AVAILABLE;
		    (void)sscanf(departure, "%02u-%02uT%02u:%02uZ",
				 &ais->type8.dac1fid13.fmonth,
				 &ais->type8.dac1fid13.fday,
				 &ais->type8.dac1fid13.fhour, 
				 &ais->type8.dac1fid13.fminute);
		    ais->type8.dac1fid13.tmonth = AIS_MONTH_NOT_AVAILABLE;
		    ais->type8.dac1fid13.tday = AIS_DAY_NOT_AVAILABLE;
		    ais->type8.dac1fid13.thour = AIS_HOUR_NOT_AVAILABLE;
		    ais->type8.dac1fid13.tminute = AIS_MINUTE_NOT_AVAILABLE;
		    (void)sscanf(eta, "%02u-%02uT%02u:%02uZ",
				 &ais->type8.dac1fid13.tmonth,
				 &ais->type8.dac1fid13.tday,
				 &ais->type8.dac1fid13.thour, 
				 &ais->type8.dac1fid13.tminute);
		}
		imo = true;
	    }
	    else if (strstr(buf, "\"fid\":15,") != NULL) {
		status = json_read_object(buf, json_ais8_fid15, endptr);
		imo = true;
	    }
	    else if (strstr(buf, "\"fid\":17,") != NULL) {
		status = json_read_object(buf, json_ais8_fid17, endptr);
		imo = true;
	    }
	    else if (strstr(buf, "\"fid\":19,") != NULL) {
		status = json_read_object(buf, json_ais8_fid19, endptr);
		imo = true;
	    }
	    else if (strstr(buf, "\"fid\":27,") != NULL) {
		status = json_read_object(buf, json_ais8_fid27, endptr);
		if (status == 0) {
		    ais->type8.dac1fid27.month = AIS_MONTH_NOT_AVAILABLE;
		    ais->type8.dac1fid27.day = AIS_DAY_NOT_AVAILABLE;
		    ais->type8.dac1fid27.hour = AIS_HOUR_NOT_AVAILABLE;
		    ais->type8.dac1fid27.minute = AIS_MINUTE_NOT_AVAILABLE;
		    (void)sscanf(start, "%02u-%02uT%02u:%02uZ",
				 &ais->type8.dac1fid27.month,
				 &ais->type8.dac1fid27.day,
				 &ais->type8.dac1fid27.hour, 
				 &ais->type8.dac1fid27.minute);
		}
		imo = true;
	    }
	    else if (strstr(buf, "\"fid\":29,") != NULL) {
		status = json_read_object(buf, json_ais8_fid29, endptr);
		imo = true;
	    }
	    else if (strstr(buf, "\"fid\":31,") != NULL || strstr(buf, "\"fid\":11,") != NULL) {
		status = json_read_object(buf, json_ais8_fid31, endptr);
		if (status == 0) {
		    ais->type8.dac1fid31.day = AIS_DAY_NOT_AVAILABLE;
		    ais->type8.dac1fid31.hour = AIS_HOUR_NOT_AVAILABLE;
		    ais->type8.dac1fid31.minute = AIS_MINUTE_NOT_AVAILABLE;
		    (void)sscanf(eta, "%02uT%02u:%02uZ",
				 &ais->type8.dac1fid31.day,
				 &ais->type8.dac1fid31.hour, 
				 &ais->type8.dac1fid31.minute);
		}
		imo = true;
	    }
	}
	if (!imo) {
	    status = json_read_object(buf, json_ais8, endptr);
	    if (status == 0)
		lenhex_unpack(data, &ais->type8.bitcount,
			      ais->type8.bitdata, sizeof(ais->type8.bitdata));
	}
    } else if (strstr(buf, "\"type\":9,") != NULL) {
	status = json_read_object(buf, json_ais9, endptr);
    } else if (strstr(buf, "\"type\":10,") != NULL) {
	status = json_read_object(buf, json_ais10, endptr);
    } else if (strstr(buf, "\"type\":12,") != NULL) {
	status = json_read_object(buf, json_ais12, endptr);
    } else if (strstr(buf, "\"type\":14,") != NULL) {
	status = json_read_object(buf, json_ais14, endptr);
    } else if (strstr(buf, "\"type\":15,") != NULL) {
	status = json_read_object(buf, json_ais15, endptr);
    } else if (strstr(buf, "\"type\":16,") != NULL) {
	status = json_read_object(buf, json_ais16, endptr);
    } else if (strstr(buf, "\"type\":17,") != NULL) {
	status = json_read_object(buf, json_ais17, endptr);
	if (status == 0)
	    lenhex_unpack(data, &ais->type17.bitcount,
			  ais->type17.bitdata, sizeof(ais->type17.bitdata));
    } else if (strstr(buf, "\"type\":18,") != NULL) {
	status = json_read_object(buf, json_ais18, endptr);
    } else if (strstr(buf, "\"type\":19,") != NULL) {
	status = json_read_object(buf, json_ais19, endptr);
    } else if (strstr(buf, "\"type\":20,") != NULL) {
	status = json_read_object(buf, json_ais20, endptr);
    } else if (strstr(buf, "\"type\":21,") != NULL) {
	status = json_read_object(buf, json_ais21, endptr);
    } else if (strstr(buf, "\"type\":22,") != NULL) {
	status = json_read_object(buf, json_ais22, endptr);
    } else if (strstr(buf, "\"type\":23,") != NULL) {
	status = json_read_object(buf, json_ais23, endptr);
    } else if (strstr(buf, "\"type\":24,") != NULL) {
	status = json_read_object(buf, json_ais24, endptr);
    } else if (strstr(buf, "\"type\":25,") != NULL) {
	status = json_read_object(buf, json_ais25, endptr);
    } else if (strstr(buf, "\"type\":26,") != NULL) {
	status = json_read_object(buf, json_ais26, endptr);
    } else if (strstr(buf, "\"type\":27,") != NULL) {
	status = json_read_object(buf, json_ais27, endptr);
    } else {
	if (endptr != NULL)
	    *endptr = NULL;
	return JSON_ERR_MISC;
    }
    return status;
    /*@+compdef +usedef +nullstate@*/
}
Пример #6
0
            .addr.uinteger = &off
        },
        [1] = {
            .attribute = "name",
            .type = t_string,
            .addr.string = tmp_str,
            .len = sizeof(tmp_str)
        }
    };
    int rc;
    uint32_t out_len;
    struct fs_file *file;
    struct json_encoder *enc;
    struct json_value jv;

    rc = json_read_object(&njb->njb_buf, dload_attr);
    if (rc || off == UINT_MAX) {
        rc = NMGR_ERR_EINVAL;
        goto err;
    }

    rc = fs_open(tmp_str, FS_ACCESS_READ, &file);
    if (rc || !file) {
        rc = NMGR_ERR_ENOMEM;
        goto err;
    }

    rc = fs_seek(file, off);
    if (rc) {
        rc = NMGR_ERR_EUNKNOWN;
        goto err_close;
Пример #7
0
            .attribute = "len",
            .type = t_uinteger,
            .addr.uinteger = &size,
            .nodefault = true
        }
    };
    struct image_version ver;
    struct json_encoder *enc;
    struct json_value jv;
    int active;
    int best;
    int rc;
    int len;
    int i;

    rc = json_read_object(&njb->njb_buf, off_attr);
    if (rc || off == UINT_MAX) {
        return OS_EINVAL;
    }
    len = strlen(img_data);
    if (len) {
        len = base64_decode(img_data, img_data);
        if (len < 0) {
            return OS_EINVAL;
        }
    }

    if (off == 0) {
        /*
         * New upload.
         */
Пример #8
0
        [0] = {
            .attribute = "test",
            .type = t_string,
            .addr.string = test_ver_str,
            .len = sizeof(test_ver_str),
        },
        [1] = {
            .attribute = NULL
        }
    };
    struct json_encoder *enc;
    struct json_value jv;
    int rc;
    struct image_version ver;

    rc = json_read_object(&njb->njb_buf, boot_write_attr);
    if (rc) {
        rc = NMGR_ERR_EINVAL;
        goto err;
    }

    rc = imgr_ver_parse(boot_write_attr[0].addr.string, &ver);
    if (rc) {
        rc = NMGR_ERR_EINVAL;
        goto err;
    }

    rc = imgr_find_by_ver(&ver, hash);
    if (rc < 0) {
        rc = NMGR_ERR_EINVAL;
        goto err;
conf_json_line(struct json_buffer *jb, char *name, int nlen, char *value,
  int vlen)
{
    const struct json_attr_t val_attr[3] = {
        [0] = {
            .attribute = "name",
            .type = t_string,
            .addr.string = name,
            .len = nlen
        },
        [1] = {
            .attribute = "val",
            .type = t_string,
            .addr.string = value,
            .len = vlen
        },
        [2] = {
            .attribute = NULL
        }
    };
    int rc;

    rc = json_read_object(jb, val_attr);
    if (rc) {
        return rc;
    }
    return 0;
}

#endif