int ft_begin_node(struct ft_cxt *cxt, const char *name) { unsigned long nlen = strlen(name) + 1; unsigned long len = 8 + _ALIGN(nlen, 4); if (!ft_make_space(cxt, &cxt->p, FT_STRUCT, len)) return -1; ft_put_word(cxt, OF_DT_BEGIN_NODE); ft_put_bin(cxt, name, strlen(name) + 1); return 0; }
int ft_prop(struct ft_cxt *cxt, const char *name, const void *data, unsigned int sz) { int off, len; off = map_string(cxt, name); if (off == NO_STRING) return -1; len = 12 + _ALIGN(sz, 4); if (!ft_make_space(cxt, &cxt->p, FT_STRUCT, len)) return -1; ft_put_word(cxt, OF_DT_PROP); ft_put_word(cxt, sz); ft_put_word(cxt, off); ft_put_bin(cxt, data, sz); return 0; }
int ft_set_prop(struct ft_cxt *cxt, const void *phandle, const char *propname, const void *buf, const unsigned int buflen) { struct ft_atom atom; void *node; char *p, *next; int nextra, depth; node = ft_node_ph2node(cxt, phandle); if (node == NULL) return -1; depth = 0; p = node; while ((next = ft_next(cxt, p, &atom)) != NULL) { switch (atom.tag) { case OF_DT_BEGIN_NODE: ++depth; break; case OF_DT_END_NODE: if (--depth > 0) break; /* haven't found the property, insert here */ cxt->p = p; return ft_prop(cxt, propname, buf, buflen); case OF_DT_PROP: if ((depth != 1) || strcmp(atom.name, propname)) break; /* found an existing property, overwrite it */ nextra = _ALIGN(buflen, 4) - _ALIGN(atom.size, 4); cxt->p = atom.data; if (nextra && !ft_make_space(cxt, &cxt->p, FT_STRUCT, nextra)) return -1; *(u32 *) (cxt->p - 8) = cpu_to_be32(buflen); ft_put_bin(cxt, buf, buflen); return 0; } p = next; } return -1; }
static size_t ft_paramtwo(t_frmt *arg_frmt, double *param, size_t len) { char type; type = arg_frmt->type; if (type == 'c') len += ft_putchar((char)param); else if (type == 'u' || type == 'D') { ft_precision(arg_frmt->precision, (int)param); len += ft_put_uint((long unsigned int)param); } else if (type == '%') len += ft_putchar('%'); else if (type == 'p') { len += ft_putstr_l("0x7fff"); len += ft_put_hex((unsigned int)param, 0); } else if (type == 'b') ft_put_bin((long long int)param); return (len); }