/* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */ int zsend_router_id_update (struct zserv *client, struct prefix *p) { struct stream *s; int blen; /* Check this client need interface information. */ if (!client->ridinfo) return 0; s = client->obuf; stream_reset (s); /* Message type. */ zserv_create_header (s, ZEBRA_ROUTER_ID_UPDATE); /* Prefix information. */ stream_putc (s, p->family); blen = prefix_blen (p); stream_put (s, &p->u.prefix, blen); stream_putc (s, p->prefixlen); /* Write packet size. */ stream_putw_at (s, 0, stream_get_endp (s)); return zebra_server_send_message(client); }
static int write_pubkey_enc (cdk_stream_t out, cdk_pkt_pubkey_enc_t pke, int old_ctb) { size_t size; int rc, nenc; assert (out); assert (pke); if (pke->version < 2 || pke->version > 3) return CDK_Inv_Packet; if (!KEY_CAN_ENCRYPT (pke->pubkey_algo)) return CDK_Inv_Algo; if (DEBUG_PKT) _cdk_log_debug ("write_pubkey_enc:\n"); nenc = cdk_pk_get_nenc (pke->pubkey_algo); size = 10 + calc_mpisize (pke->mpi, nenc); rc = pkt_write_head (out, old_ctb, size, CDK_PKT_PUBKEY_ENC); if (rc) return rc; rc = stream_putc (out, pke->version); if (!rc) rc = write_32 (out, pke->keyid[0]); if (!rc) rc = write_32 (out, pke->keyid[1]); if (!rc) rc = stream_putc (out, _cdk_pub_algo_to_pgp (pke->pubkey_algo)); if (!rc) rc = write_mpibuf (out, pke->mpi, nenc); return rc; }
static cdk_error_t write_head_old (cdk_stream_t out, size_t size, int type) { cdk_error_t rc; int ctb; assert (out); if (type < 0 || type > 16) return CDK_Inv_Packet; ctb = 0x80 | (type << 2); if (!size) ctb |= 3; else if (size < 256) ; else if (size < 65536) ctb |= 1; else ctb |= 2; rc = stream_putc (out, ctb); if (!size) return rc; if (!rc) { if (size < 256) rc = stream_putc (out, size); else if (size < 65536) rc = write_16 (out, size); else rc = write_32 (out, size); } return rc; }
static cdk_error_t write_onepass_sig (cdk_stream_t out, cdk_pkt_onepass_sig_t sig) { cdk_error_t rc; assert (out); assert (sig); if (sig->version != 3) return CDK_Inv_Packet; if (DEBUG_PKT) _cdk_log_debug ("write_onepass_sig:\n"); rc = pkt_write_head (out, 0, 13, CDK_PKT_ONEPASS_SIG); if (!rc) rc = stream_putc (out, sig->version); if (!rc) rc = stream_putc (out, sig->sig_class); if (!rc) rc = stream_putc (out, _gnutls_hash_algo_to_pgp (sig->digest_algo)); if (!rc) rc = stream_putc (out, _cdk_pub_algo_to_pgp (sig->pubkey_algo)); if (!rc) rc = write_32 (out, sig->keyid[0]); if (!rc) rc = write_32 (out, sig->keyid[1]); if (!rc) rc = stream_putc (out, sig->last); return rc; }
/* * "xdr_encode"-like interface that allows daemon (client) to send * a message to zebra server for a route that needs to be * added/deleted to the kernel. Info about the route is specified * by the caller in a struct zapi_ipv4. zapi_ipv4_read() then writes * the info down the zclient socket using the stream_* functions. * * The corresponding read ("xdr_decode") function on the server * side is zread_ipv4_add()/zread_ipv4_delete(). * * 0 1 2 3 4 5 6 7 8 9 A B C D E F 0 1 2 3 4 5 6 7 8 9 A B C D E F * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * | Length (2) | Command | Route Type | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * | ZEBRA Flags | Message Flags | Prefix length | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * | Destination IPv4 Prefix for route | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * | Nexthop count | * +-+-+-+-+-+-+-+-+ * * * A number of IPv4 nexthop(s) or nexthop interface index(es) are then * described, as per the Nexthop count. Each nexthop described as: * * +-+-+-+-+-+-+-+-+ * | Nexthop Type | Set to one of ZEBRA_NEXTHOP_* * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * | IPv4 Nexthop address or Interface Index number | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * * Alternatively, if the flags field has ZEBRA_FLAG_BLACKHOLE or * ZEBRA_FLAG_REJECT is set then Nexthop count is set to 1, then _no_ * nexthop information is provided, and the message describes a prefix * to blackhole or reject route. * * If ZAPI_MESSAGE_DISTANCE is set, the distance value is written as a 1 * byte value. * * If ZAPI_MESSAGE_METRIC is set, the metric value is written as an 8 * byte value. * * XXX: No attention paid to alignment. */ int zapi_ipv4_route (u_char cmd, struct zclient *zclient, struct prefix_ipv4 *p, struct zapi_ipv4 *api) { int i; int psize; struct stream *s; /* Reset stream. */ s = zclient->obuf; stream_reset (s); zclient_create_header (s, cmd); /* Put type and nexthop. */ stream_putc (s, api->type); stream_putc (s, api->flags); stream_putc (s, api->message); stream_putw (s, api->safi); /* Put prefix information. */ psize = PSIZE (p->prefixlen); stream_putc (s, p->prefixlen); stream_write (s, (u_char *) & p->prefix, psize); /* Nexthop, ifindex, distance and metric information. */ if (CHECK_FLAG (api->message, ZAPI_MESSAGE_NEXTHOP)) { if (CHECK_FLAG (api->flags, ZEBRA_FLAG_BLACKHOLE)) { stream_putc (s, 1); stream_putc (s, ZEBRA_NEXTHOP_BLACKHOLE); /* XXX assert(api->nexthop_num == 0); */ /* XXX assert(api->ifindex_num == 0); */ } else stream_putc (s, api->nexthop_num + api->ifindex_num); for (i = 0; i < api->nexthop_num; i++) { stream_putc (s, ZEBRA_NEXTHOP_IPV4); stream_put_in_addr (s, api->nexthop[i]); } for (i = 0; i < api->ifindex_num; i++) { stream_putc (s, ZEBRA_NEXTHOP_IFINDEX); stream_putl (s, api->ifindex[i]); } } if (CHECK_FLAG (api->message, ZAPI_MESSAGE_DISTANCE)) stream_putc (s, api->distance); if (CHECK_FLAG (api->message, ZAPI_MESSAGE_METRIC)) stream_putl (s, api->metric); /* Put length at the first point of the stream. */ stream_putw_at (s, 0, stream_get_endp (s)); return zclient_send_message(zclient); }
/* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */ int zsend_router_id_update (struct zserv *client, struct prefix *p) { struct stream *s; int blen; /* Check this client need interface information. */ if (!client->ridinfo) return -1; s = client->obuf; stream_reset (s); /* Place holder for size. */ stream_putw (s, 0); /* Message type. */ stream_putc (s, ZEBRA_ROUTER_ID_UPDATE); /* Prefix information. */ stream_putc (s, p->family); blen = prefix_blen (p); stream_put (s, &p->u.prefix, blen); stream_putc (s, p->prefixlen); /* Write packet size. */ stream_putw_at (s, 0, stream_get_endp (s)); return writen (client->sock, s->data, stream_get_endp (s)); }
static int zsend_ipv4_import_lookup (struct zserv *client, struct prefix_ipv4 *p) { struct stream *s; struct rib *rib; unsigned long nump; u_char num; struct nexthop *nexthop; /* Lookup nexthop. */ rib = rib_lookup_ipv4 (p); /* Get output stream. */ s = client->obuf; stream_reset (s); /* Fill in result. */ stream_putw (s, 0); stream_putc (s, ZEBRA_IPV4_IMPORT_LOOKUP); stream_put_in_addr (s, &p->prefix); if (rib) { stream_putl (s, rib->metric); num = 0; nump = s->putp; stream_putc (s, 0); for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)) { stream_putc (s, nexthop->type); switch (nexthop->type) { case ZEBRA_NEXTHOP_IPV4: stream_put_in_addr (s, &nexthop->gate.ipv4); break; case ZEBRA_NEXTHOP_IFINDEX: case ZEBRA_NEXTHOP_IFNAME: stream_putl (s, nexthop->ifindex); break; default: /* do nothing */ break; } num++; } stream_putc_at (s, nump, num); } else { stream_putl (s, 0); stream_putc (s, 0); } stream_putw_at (s, 0, stream_get_endp (s)); zebra_server_send_message (client->sock, s->data, stream_get_endp (s)); return 0; }
static cdk_error_t write_user_id (cdk_stream_t out, cdk_pkt_userid_t id, int old_ctb, int pkttype) { cdk_error_t rc; if (!out || !id) return CDK_Inv_Value; if (pkttype == CDK_PKT_ATTRIBUTE) { if (!id->attrib_img) return CDK_Inv_Value; rc = pkt_write_head (out, old_ctb, id->attrib_len + 6, CDK_PKT_ATTRIBUTE); if (rc) return rc; /* Write subpacket part. */ stream_putc (out, 255); write_32 (out, id->attrib_len + 1); stream_putc (out, 1); rc = stream_write (out, id->attrib_img, id->attrib_len); } else { if (!id->name) return CDK_Inv_Value; rc = pkt_write_head (out, old_ctb, id->len, CDK_PKT_USER_ID); if (!rc) rc = stream_write (out, id->name, id->len); } return rc; }
UINT16 put_opl(imgtool_stream *instream, imgtool_stream *outstream) { UINT16 out_size = 0; UINT32 rec_start = stream_tell(outstream); char *line; // reset stream stream_seek(instream, 0, SEEK_SET); stream_fill(outstream, 0x00, 4); // replace all eol with 0x00 while ((line = stream_getline(instream, 256))) { // replace tab with space for (int i=0; i<strlen(line); i++) if (line[i] == '\t') line[i] = ' '; stream_write(outstream, line, strlen(line)); stream_putc(outstream, 0x00); out_size += strlen(line) + 1; free(line); } // end of pack stream_fill(outstream, 0xff, 2); // update the size in the head stream_seek(outstream, rec_start + 2, SEEK_SET); stream_putc(outstream, (out_size>>8) & 0xff); stream_putc(outstream, out_size & 0xff); return out_size + 4; }
/* * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or * ZEBRA_INTERFACE_DOWN. * * The ZEBRA_INTERFACE_UP message is sent from the zebra server to * the clients in one of 2 situations: * - an if_up is detected e.g., as a result of an RTM_IFINFO message * - a vty command modifying the bandwidth of an interface is received. * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected. */ int zsend_interface_update (int cmd, struct zserv *client, struct interface *ifp) { struct stream *s; /* Check this client need interface information. */ if (! client->ifinfo) return -1; s = client->obuf; stream_reset (s); /* Place holder for size. */ stream_putw (s, 0); /* Zebra command. */ stream_putc (s, cmd); /* Interface information. */ stream_put (s, ifp->name, INTERFACE_NAMSIZ); stream_putl (s, ifp->ifindex); stream_putc (s, ifp->status); stream_putl (s, ifp->flags); stream_putl (s, ifp->metric); stream_putl (s, ifp->mtu); stream_putl (s, ifp->mtu6); stream_putl (s, ifp->bandwidth); /* Write packet size. */ stream_putw_at (s, 0, stream_get_endp (s)); zebra_server_send_message (client->sock, s->data, stream_get_endp (s)); return 0; }
int zsend_interface_delete (struct zserv *client, struct interface *ifp) { struct stream *s; /* Check this client need interface information. */ if (! client->ifinfo) return -1; s = client->obuf; stream_reset (s); /* Packet length placeholder. */ stream_putw (s, 0); /* Interface information. */ stream_putc (s, ZEBRA_INTERFACE_DELETE); stream_put (s, ifp->name, INTERFACE_NAMSIZ); stream_putl (s, ifp->ifindex); stream_putc (s, ifp->status); stream_putl (s, ifp->flags); stream_putl (s, ifp->metric); stream_putl (s, ifp->mtu); stream_putl (s, ifp->mtu6); stream_putl (s, ifp->bandwidth); /* Write packet length. */ stream_putw_at (s, 0, stream_get_endp (s)); zebra_server_send_message (client->sock, s->data, stream_get_endp (s)); return 0; }
UINT16 get_ob3(imgtool_stream *instream, imgtool_stream *outstream, UINT8 type, UINT8 file_id) { UINT8 data, *buffer = NULL; UINT16 size = 0; static const char ob3_magic[3] = {'O', 'R', 'G'}; stream_read(instream, &data, 1); if (data == 0x02) { stream_seek(instream, 1, SEEK_CUR); size = get_long_rec_size(instream); buffer = (UINT8*)malloc(size); stream_read(instream, buffer, size); } stream_write(outstream, ob3_magic, 3); stream_putc(outstream, (size>>8) & 0xff); stream_putc(outstream, size & 0xff); stream_putc(outstream, type | 0x80); if (buffer) { stream_write(outstream, buffer, size); free (buffer); } return size; }
UINT16 put_odb(imgtool_stream *instream, imgtool_stream *outstream, UINT8 file_id) { char *line; UINT16 out_size = 0; // reset stream stream_seek(instream, 0, SEEK_SET); while ((line = stream_getline(instream, 256))) { UINT16 len = strlen(line); stream_putc(outstream, (UINT8)len); stream_putc(outstream, file_id); stream_write(outstream, line, len); out_size += (len + 1); free(line); } // end of pack stream_fill(outstream, 0xff, 2); return out_size + 4; }
static cdk_error_t pkt_encode_len (cdk_stream_t out, size_t pktlen) { cdk_error_t rc; assert (out); rc = 0; if (!pktlen) { /* Block mode, partial bodies, with 'DEF_BLOCKSIZE' from main.h */ rc = stream_putc (out, (0xE0 | DEF_BLOCKBITS)); } else if (pktlen < 192) rc = stream_putc (out, pktlen); else if (pktlen < 8384) { pktlen -= 192; rc = stream_putc (out, (pktlen / 256) + 192); if (!rc) rc = stream_putc (out, (pktlen % 256)); } else { rc = stream_putc (out, 255); if (!rc) rc = write_32 (out, pktlen); } return rc; }
void write_todolist(STREAM *output, TODOLIST *todolist) { OPTION *opt = NULL; TASK *task = NULL; int width = 0; stream_printf(output, "%s\n", todolist->title); task = todolist->first; while (task) { stream_printf(output, "[%c] %s\n", task->done ? 'X' : ' ', task->message); task = task->next; } opt = todolist->first_option; if (opt) { width += stream_printf(output, "#"); while (opt) { if (width >= 80) { stream_printf(output, "\n#"); width = 1; } width += 2; stream_putc(' ', output); width += escape_string(output, opt->key); stream_putc('=', output); width += escape_string(output, opt->value); opt = opt->next; } stream_printf(output, "\n"); } }
static void rip_auth_write_leading_rte ( struct stream *s, struct rip_interface *ri, const u_int8_t key_id, char *auth_str, u_int16_t main_body_len ) { u_int8_t dlen; if (IS_RIP_DEBUG_AUTH) zlog_debug ("writing authentication header for %uB of main body", main_body_len); stream_putw (s, RIP_FAMILY_AUTH); switch (ri->auth_type) { case RIP_AUTH_SIMPLE_PASSWORD: { u_int8_t padded_simple_password[RIP_AUTH_SIMPLE_SIZE] = { 0 }; memcpy (padded_simple_password, auth_str, MIN (RIP_AUTH_SIMPLE_SIZE, strlen (auth_str))); stream_putw (s, RIP_AUTH_SIMPLE_PASSWORD); stream_put (s, padded_simple_password, RIP_AUTH_SIMPLE_SIZE); break; } case RIP_AUTH_HASH: if (IS_RIP_DEBUG_AUTH) zlog_debug ("hash algorithm is '%s'", LOOKUP (hash_algo_str, ri->hash_algo)); stream_putw (s, RIP_AUTH_HASH); stream_putw (s, main_body_len); stream_putc (s, key_id); switch (ri->hash_algo) { case HASH_KEYED_MD5: /* Auth Data Len. Set 16 for MD5 authentication data. Older ripds * however expect RIP_HEADER_SIZE + HASH_SIZE_MD5 so we allow for this * to be configurable. */ dlen = ri->md5_auth_len; break; case HASH_HMAC_SHA1: case HASH_HMAC_SHA256: case HASH_HMAC_SHA384: case HASH_HMAC_SHA512: dlen = hash_digest_length[ri->hash_algo]; break; default: assert (0); } if (IS_RIP_DEBUG_AUTH) zlog_debug ("declared auth data length is %uB", dlen); stream_putc (s, dlen); stream_putl (s, time (NULL)); /* Sequence Number (non-decreasing). */ stream_putl (s, 0); /* reserved, MBZ */ stream_putl (s, 0); /* reserved, MBZ */ break; default: assert (0); } }
void tipc_packet_create_header (struct stream *s, uint16_t cmd) { /* length placeholder, caller can update */ stream_putw (s, TIPC_PACKET_HEADER_SIZE); stream_putc (s, TIPC_PACKET_HEADER_MARKER); stream_putc (s, TIPC_PACKET_VERSION); stream_putw (s, cmd); }
void zclient_create_header (struct stream *s, uint16_t command) { /* length placeholder, caller can update */ stream_putw (s, ZEBRA_HEADER_SIZE); stream_putc (s, ZEBRA_HEADER_MARKER); stream_putc (s, ZSERV_VERSION); stream_putw (s, command); }
size_t escape_string(STREAM *output, const char *str) { size_t l = 0; char c; while ((c = *str) != '\0') { if (c == ' ' || c == '\\' || c == '\n' || c == '=') { stream_putc('\\', output); } stream_putc(c, output); str++; } return l; }
/* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or * ZEBRA_INTERFACE_ADDRESS_DELETE to the client. * * A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations: * - in response to a 3-byte ZEBRA_INTERFACE_ADD request * from the client, after the ZEBRA_INTERFACE_ADD has been * sent from zebra to the client * - redistribute new address info to all clients in the following situations * - at startup, when zebra figures out the available interfaces * - when an interface is added (where support for * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is * received) * - for the vty commands "ip address A.B.C.D/M [<secondary>|<label LINE>]" * and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M" * - when an RTM_NEWADDR message is received from the kernel, * * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE: * * zsend_interface_address(DELETE) * ^ * | * zebra_interface_address_delete_update * ^ ^ ^ * | | if_delete_update (not called on * | | Solaris) * ip_address_uninstall connected_delete_ipv4 * [ipv6_addresss_uninstall] [connected_delete_ipv6] * ^ ^ * | | * | RTM_NEWADDR on routing/netlink socket * | * vty commands: * "no ip address A.B.C.D/M [label LINE]" * "no ip address A.B.C.D/M secondary" * ["no ipv6 address X:X::X:X/M"] * */ int zsend_interface_address (int cmd, struct zserv *client, struct interface *ifp, struct connected *ifc) { int blen; struct stream *s; struct prefix *p; /* Check this client need interface information. */ if (! client->ifinfo) return -1; s = client->obuf; stream_reset (s); /* Place holder for size. */ stream_putw (s, 0); stream_putc (s, cmd); stream_putl (s, ifp->ifindex); /* Interface address flag. */ stream_putc (s, ifc->flags); /* Prefix information. */ p = ifc->address; stream_putc (s, p->family); blen = prefix_blen (p); stream_put (s, &p->u.prefix, blen); /* * XXX gnu version does not send prefixlen for ZEBRA_INTERFACE_ADDRESS_DELETE * but zebra_interface_address_delete_read() in the gnu version * expects to find it */ stream_putc (s, p->prefixlen); /* Destination. */ p = ifc->destination; if (p) stream_put (s, &p->u.prefix, blen); else stream_put (s, NULL, blen); /* Write packet size. */ stream_putw_at (s, 0, stream_get_endp (s)); zebra_server_send_message (client->sock, s->data, stream_get_endp (s)); return 0; }
int zapi_ipv6_route (u_char cmd, struct_zclient *zclient, struct prefix_ipv6 *p, struct zapi_ipv6 *api) { int i; int psize; struct stream *s; /* Reset stream. */ s = zclient->obuf; stream_reset (s); /* Length place holder. */ stream_putw (s, 0); /* Put command, type and nexthop. */ stream_putc (s, cmd); stream_putc (s, api->type); stream_putc (s, api->flags); stream_putc (s, api->message); /* Put prefix information. */ psize = PSIZE (p->prefixlen); stream_putc (s, p->prefixlen); stream_write (s, (u_char *)&p->prefix, psize); /* Nexthop, ifindex, distance and metric information. */ if (CHECK_FLAG (api->message, ZAPI_MESSAGE_NEXTHOP)) { stream_putc (s, api->nexthop_num + api->ifindex_num); for (i = 0; i < api->nexthop_num; i++) { stream_putc (s, ZEBRA_NEXTHOP_IPV6); stream_write (s, (u_char *)api->nexthop[i], 16); } for (i = 0; i < api->ifindex_num; i++) { stream_putc (s, ZEBRA_NEXTHOP_IFINDEX); stream_putl (s, api->ifindex[i]); } } if (CHECK_FLAG (api->message, ZAPI_MESSAGE_DISTANCE)) stream_putc (s, api->distance); if (CHECK_FLAG (api->message, ZAPI_MESSAGE_METRIC)) stream_putl (s, api->metric); /* Put length at the first point of the stream. */ stream_putw_at (s, 0, stream_get_endp (s)); return writen (zclient->sock, s->data, stream_get_endp (s)); }
/* <file> <cmap> .writecmap - */ static int zfcmap_put_name_default(stream *s, const byte *str, uint size) { stream_putc(s, '/'); stream_write(s, str, size); return 0; }
/* * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or * ZEBRA_INTERFACE_DOWN. * * The ZEBRA_INTERFACE_UP message is sent from the zebra server to * the clients in one of 2 situations: * - an if_up is detected e.g., as a result of an RTM_IFINFO message * - a vty command modifying the bandwidth of an interface is received. * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected. */ int zsend_interface_update (int cmd, struct zserv *client, struct interface_FOO *ifp) { struct stream *s; /* Check this client need interface information. */ if (! client->ifinfo) return 0; s = client->obuf; stream_reset (s); zserv_create_header (s, cmd); /* Interface information. */ stream_put (s, ifp->name, INTERFACE_NAMSIZ); stream_putl (s, ifp->ifindex); stream_putc (s, ifp->status); stream_putq (s, ifp->flags); stream_putl (s, ifp->metric); stream_putl (s, ifp->mtu); stream_putl (s, ifp->mtu6); stream_putl (s, ifp->bandwidth); /* Write packet size. */ stream_putw_at (s, 0, stream_get_endp (s)); return zebra_server_send_message(client); }
int main (void) { struct stream *s; s = stream_new (1024); stream_putc (s, ham); stream_putw (s, ham); stream_putl (s, ham); stream_putq (s, ham); print_stream (s); stream_resize (s, stream_get_endp (s)); print_stream (s); printf ("c: 0x%hhx\n", stream_getc (s)); printf ("w: 0x%hx\n", stream_getw (s)); printf ("l: 0x%x\n", stream_getl (s)); printf ("q: 0x%" PRIu64 "\n", stream_getq (s)); return 0; }
static cdk_error_t write_literal (cdk_stream_t out, cdk_pkt_literal_t pt, int old_ctb) { byte buf[BUFSIZE]; size_t size; cdk_error_t rc; assert (out); assert (pt); /* We consider a packet without a body as an invalid packet. At least one octet must be present. */ if (!pt->len) return CDK_Inv_Packet; if (DEBUG_PKT) _cdk_log_debug ("write_literal:\n"); size = 6 + pt->namelen + pt->len; rc = pkt_write_head (out, old_ctb, size, CDK_PKT_LITERAL); if (rc) return rc; rc = stream_putc (out, pt->mode); if (rc) return rc; rc = stream_putc (out, pt->namelen); if (rc) return rc; if (pt->namelen > 0) rc = stream_write (out, pt->name, pt->namelen); if (!rc) rc = write_32 (out, pt->timestamp); if (rc) return rc; while (!cdk_stream_eof (pt->buf) && !rc) { rc = stream_read (pt->buf, buf, DIM (buf), &size); if (!rc) rc = stream_write (out, buf, size); } wipemem (buf, sizeof (buf)); return rc; }
static cdk_error_t write_public_key (cdk_stream_t out, cdk_pkt_pubkey_t pk, int is_subkey, int old_ctb) { int pkttype, ndays = 0; size_t npkey = 0, size = 6; cdk_error_t rc; assert (out); assert (pk); if (pk->version < 2 || pk->version > 4) return CDK_Inv_Packet; if (DEBUG_PKT) _cdk_log_debug ("write_public_key: subkey=%d\n", is_subkey); pkttype = is_subkey ? CDK_PKT_PUBLIC_SUBKEY : CDK_PKT_PUBLIC_KEY; npkey = cdk_pk_get_npkey (pk->pubkey_algo); if (!npkey) return CDK_Inv_Algo; if (pk->version < 4) size += 2; /* expire date */ if (is_subkey) old_ctb = 0; size += calc_mpisize (pk->mpi, npkey); if (old_ctb) rc = pkt_write_head2 (out, size, pkttype); else rc = pkt_write_head (out, old_ctb, size, pkttype); if (!rc) rc = stream_putc (out, pk->version); if (!rc) rc = write_32 (out, pk->timestamp); if (!rc && pk->version < 4) { if (pk->expiredate) ndays = (u16) ((pk->expiredate - pk->timestamp) / 86400L); rc = write_16 (out, ndays); } if (!rc) rc = stream_putc (out, _cdk_pub_algo_to_pgp (pk->pubkey_algo)); if (!rc) rc = write_mpibuf (out, pk->mpi, npkey); return rc; }
void update_opk_head(imgtool_stream *stream) { UINT16 size = stream_size(stream) - 6; stream_seek(stream, 4, SEEK_SET); stream_putc(stream, (size>>8) & 0xff); stream_putc(stream, size & 0xff); }
static cdk_error_t write_v3_sig (cdk_stream_t out, cdk_pkt_signature_t sig, int nsig) { size_t size; cdk_error_t rc; size = 19 + calc_mpisize (sig->mpi, nsig); if (is_RSA (sig->pubkey_algo)) rc = pkt_write_head2 (out, size, CDK_PKT_SIGNATURE); else rc = pkt_write_head (out, 1, size, CDK_PKT_SIGNATURE); if (!rc) rc = stream_putc (out, sig->version); if (!rc) rc = stream_putc (out, 5); if (!rc) rc = stream_putc (out, sig->sig_class); if (!rc) rc = write_32 (out, sig->timestamp); if (!rc) rc = write_32 (out, sig->keyid[0]); if (!rc) rc = write_32 (out, sig->keyid[1]); if (!rc) rc = stream_putc (out, _cdk_pub_algo_to_pgp (sig->pubkey_algo)); if (!rc) rc = stream_putc (out, _gnutls_hash_algo_to_pgp (sig->digest_algo)); if (!rc) rc = stream_putc (out, sig->digest_start[0]); if (!rc) rc = stream_putc (out, sig->digest_start[1]); if (!rc) rc = write_mpibuf (out, sig->mpi, nsig); return rc; }
/* * Write a string in its shortest form ( () or <> ). Note that * this form is different depending on whether binary data are allowed. * Currently we don't support ASCII85 strings ( <~ ~> ). */ void s_write_ps_string(stream * s, const byte * str, uint size, int print_ok) { uint added = 0; uint i; const stream_template *template; stream_AXE_state state; stream_state *st = NULL; if (print_ok & PRINT_BINARY_OK) { /* Only need to escape (, ), \, CR, EOL. */ stream_putc(s, '('); for (i = 0; i < size; ++i) { byte ch = str[i]; switch (ch) { case char_CR: stream_puts(s, "\\r"); continue; case char_EOL: stream_puts(s, "\\n"); continue; case '(': case ')': case '\\': stream_putc(s, '\\'); } stream_putc(s, ch); } stream_putc(s, ')'); return; } for (i = 0; i < size; ++i) { byte ch = str[i]; if (ch == 0 || ch >= 127) added += 3; else if (strchr("()\\\n\r\t\b\f", ch) != 0) ++added; else if (ch < 32) added += 3; } if (added < size || (print_ok & PRINT_HEX_NOT_OK)) {
static cdk_error_t write_mdc (cdk_stream_t out, cdk_pkt_mdc_t mdc) { cdk_error_t rc; assert (mdc); assert (out); if (DEBUG_PKT) _cdk_log_debug ("write_mdc:\n"); /* This packet requires a fixed header encoding */ rc = stream_putc (out, 0xD3); /* packet ID and 1 byte length */ if (!rc) rc = stream_putc (out, 0x14); if (!rc) rc = stream_write (out, mdc->hash, DIM (mdc->hash)); return rc; }