void marshal_skshash(int (*putchar_func)(void *ctx, size_t count, void *c), void *ctx, const struct skshash *hash) { uint32_t len; len = htonl(sizeof(hash->hash)); putchar_func(ctx, sizeof(len), &len); putchar_func(ctx, sizeof(hash->hash), (void *) hash->hash); }
void marshal_string(int (*putchar_func)(void *ctx, size_t count, void *c), void *ctx, const char *string) { uint32_t len, nlen; len = strlen(string); nlen = htonl(len); putchar_func(ctx, sizeof(nlen), &nlen); putchar_func(ctx, len, &string); }
/* * use putchar_func to print a string * @putchar_func is a function pointer to print a character * @format is the control format string (e.g. "%d + %d = %d") * @data is the address of the first variable argument * please implement it. */ int vfprintf(void (*putchar_func)(char), const char *format, void **data) { int cnt=0; //number of the printed char char ch; char *pbuf; while(1){ while((ch=*format++)!='%'){ if(ch=='\0'){ return cnt; } putchar_func(ch); cnt++; } switch ((ch=*format++)){ case 'c': putchar_func(*(char *)data++); //Notice data=data+1, data proceeds 4 bytes, cnt++; //beacause of 4 bytes of alignment. break; case 'd': pbuf=itoa(*(int *)data++,10); while((ch=*pbuf++)!=0){ putchar_func(ch); cnt++; } break; case 'x': pbuf=utoa(*(unsigned int *)data++,16); while((ch=*pbuf++)!=0){ putchar_func(ch); cnt++; } break; case 's': pbuf=*(char **)data++; while((ch=*pbuf++)!=0){ putchar_func(ch); cnt++; } break; default : // If this foramt specifier is invalid, print it out // with '%' as const string. Specifically, if it's '\0' which // ends the whole format string, the pointer format needs to // go back, thus, we can return to the while loop safely, or unexpected // behavior happens putchar_func('%'); cnt++; if(ch!='\0'){ putchar_func(ch); cnt++; } else format--; } } }
void marshal_publickey(int (*putchar_func)(void *ctx, size_t count, void *c), void *ctx, const struct openpgp_publickey *key) { uint32_t len; struct openpgp_packet_list *packets = NULL, *list_end = NULL; struct buffer_ctx buf; buf.buffer = calloc(1, 1024); buf.size = 1024; buf.offset = 0; flatten_publickey((struct openpgp_publickey *) key, &packets, &list_end); write_openpgp_stream(buffer_putchar, &buf, packets); len = htonl(buf.offset); putchar_func(ctx, sizeof(len), &len); putchar_func(ctx, buf.offset, buf.buffer); free_packet_list(packets); }
void marshal_array(int (*putchar_func)(void *ctx, size_t count, void *c), void *ctx, void (*marshal_func)(int (*putchar_func)(void *ctx, size_t count, void *c), void *ctx, const void *item), void **array, int size) { uint32_t len; int i; len = htonl(size); putchar_func(ctx, sizeof(len), &len); for (i = 0; i < size; i++) { marshal_func(putchar_func, ctx, array[i]); } }