tizen 2.4 release
[external/nghttp2.git] / lib / nghttp2_frame.c
1 /*
2  * nghttp2 - HTTP/2 C Library
3  *
4  * Copyright (c) 2013 Tatsuhiro Tsujikawa
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25 #include "nghttp2_frame.h"
26
27 #include <string.h>
28 #include <assert.h>
29 #include <stdio.h>
30 #include <errno.h>
31
32 #include "nghttp2_helper.h"
33 #include "nghttp2_net.h"
34 #include "nghttp2_priority_spec.h"
35
36 void nghttp2_frame_pack_frame_hd(uint8_t *buf, const nghttp2_frame_hd *hd) {
37   nghttp2_put_uint32be(&buf[0], (uint32_t)(hd->length << 8));
38   buf[3] = hd->type;
39   buf[4] = hd->flags;
40   nghttp2_put_uint32be(&buf[5], hd->stream_id);
41   /* ignore hd->reserved for now */
42 }
43
44 void nghttp2_frame_unpack_frame_hd(nghttp2_frame_hd *hd, const uint8_t *buf) {
45   hd->length = nghttp2_get_uint32(&buf[0]) >> 8;
46   hd->type = buf[3];
47   hd->flags = buf[4];
48   hd->stream_id = nghttp2_get_uint32(&buf[5]) & NGHTTP2_STREAM_ID_MASK;
49   hd->reserved = 0;
50 }
51
52 void nghttp2_frame_hd_init(nghttp2_frame_hd *hd, size_t length, uint8_t type,
53                            uint8_t flags, int32_t stream_id) {
54   hd->length = length;
55   hd->type = type;
56   hd->flags = flags;
57   hd->stream_id = stream_id;
58   hd->reserved = 0;
59 }
60
61 void nghttp2_frame_headers_init(nghttp2_headers *frame, uint8_t flags,
62                                 int32_t stream_id, nghttp2_headers_category cat,
63                                 const nghttp2_priority_spec *pri_spec,
64                                 nghttp2_nv *nva, size_t nvlen) {
65   nghttp2_frame_hd_init(&frame->hd, 0, NGHTTP2_HEADERS, flags, stream_id);
66   frame->padlen = 0;
67   frame->nva = nva;
68   frame->nvlen = nvlen;
69   frame->cat = cat;
70
71   if (pri_spec) {
72     frame->pri_spec = *pri_spec;
73   } else {
74     nghttp2_priority_spec_default_init(&frame->pri_spec);
75   }
76 }
77
78 void nghttp2_frame_headers_free(nghttp2_headers *frame, nghttp2_mem *mem) {
79   nghttp2_nv_array_del(frame->nva, mem);
80 }
81
82 void nghttp2_frame_priority_init(nghttp2_priority *frame, int32_t stream_id,
83                                  const nghttp2_priority_spec *pri_spec) {
84   nghttp2_frame_hd_init(&frame->hd, NGHTTP2_PRIORITY_SPECLEN, NGHTTP2_PRIORITY,
85                         NGHTTP2_FLAG_NONE, stream_id);
86   frame->pri_spec = *pri_spec;
87 }
88
89 void nghttp2_frame_priority_free(nghttp2_priority *frame _U_) {}
90
91 void nghttp2_frame_rst_stream_init(nghttp2_rst_stream *frame, int32_t stream_id,
92                                    uint32_t error_code) {
93   nghttp2_frame_hd_init(&frame->hd, 4, NGHTTP2_RST_STREAM, NGHTTP2_FLAG_NONE,
94                         stream_id);
95   frame->error_code = error_code;
96 }
97
98 void nghttp2_frame_rst_stream_free(nghttp2_rst_stream *frame _U_) {}
99
100 void nghttp2_frame_settings_init(nghttp2_settings *frame, uint8_t flags,
101                                  nghttp2_settings_entry *iv, size_t niv) {
102   nghttp2_frame_hd_init(&frame->hd, niv * NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH,
103                         NGHTTP2_SETTINGS, flags, 0);
104   frame->niv = niv;
105   frame->iv = iv;
106 }
107
108 void nghttp2_frame_settings_free(nghttp2_settings *frame, nghttp2_mem *mem) {
109   nghttp2_mem_free(mem, frame->iv);
110 }
111
112 void nghttp2_frame_push_promise_init(nghttp2_push_promise *frame, uint8_t flags,
113                                      int32_t stream_id,
114                                      int32_t promised_stream_id,
115                                      nghttp2_nv *nva, size_t nvlen) {
116   nghttp2_frame_hd_init(&frame->hd, 0, NGHTTP2_PUSH_PROMISE, flags, stream_id);
117   frame->padlen = 0;
118   frame->nva = nva;
119   frame->nvlen = nvlen;
120   frame->promised_stream_id = promised_stream_id;
121   frame->reserved = 0;
122 }
123
124 void nghttp2_frame_push_promise_free(nghttp2_push_promise *frame,
125                                      nghttp2_mem *mem) {
126   nghttp2_nv_array_del(frame->nva, mem);
127 }
128
129 void nghttp2_frame_ping_init(nghttp2_ping *frame, uint8_t flags,
130                              const uint8_t *opaque_data) {
131   nghttp2_frame_hd_init(&frame->hd, 8, NGHTTP2_PING, flags, 0);
132   if (opaque_data) {
133     memcpy(frame->opaque_data, opaque_data, sizeof(frame->opaque_data));
134   } else {
135     memset(frame->opaque_data, 0, sizeof(frame->opaque_data));
136   }
137 }
138
139 void nghttp2_frame_ping_free(nghttp2_ping *frame _U_) {}
140
141 void nghttp2_frame_goaway_init(nghttp2_goaway *frame, int32_t last_stream_id,
142                                uint32_t error_code, uint8_t *opaque_data,
143                                size_t opaque_data_len) {
144   nghttp2_frame_hd_init(&frame->hd, 8 + opaque_data_len, NGHTTP2_GOAWAY,
145                         NGHTTP2_FLAG_NONE, 0);
146   frame->last_stream_id = last_stream_id;
147   frame->error_code = error_code;
148   frame->opaque_data = opaque_data;
149   frame->opaque_data_len = opaque_data_len;
150   frame->reserved = 0;
151 }
152
153 void nghttp2_frame_goaway_free(nghttp2_goaway *frame, nghttp2_mem *mem) {
154   nghttp2_mem_free(mem, frame->opaque_data);
155 }
156
157 void nghttp2_frame_window_update_init(nghttp2_window_update *frame,
158                                       uint8_t flags, int32_t stream_id,
159                                       int32_t window_size_increment) {
160   nghttp2_frame_hd_init(&frame->hd, 4, NGHTTP2_WINDOW_UPDATE, flags, stream_id);
161   frame->window_size_increment = window_size_increment;
162   frame->reserved = 0;
163 }
164
165 void nghttp2_frame_window_update_free(nghttp2_window_update *frame _U_) {}
166
167 size_t nghttp2_frame_trail_padlen(nghttp2_frame *frame, size_t padlen) {
168   return padlen - ((frame->hd.flags & NGHTTP2_FLAG_PADDED) > 0);
169 }
170
171 void nghttp2_frame_data_init(nghttp2_data *frame, uint8_t flags,
172                              int32_t stream_id) {
173   /* At this moment, the length of DATA frame is unknown */
174   nghttp2_frame_hd_init(&frame->hd, 0, NGHTTP2_DATA, flags, stream_id);
175   frame->padlen = 0;
176 }
177
178 void nghttp2_frame_data_free(nghttp2_data *frame _U_) {}
179
180 size_t nghttp2_frame_priority_len(uint8_t flags) {
181   if (flags & NGHTTP2_FLAG_PRIORITY) {
182     return NGHTTP2_PRIORITY_SPECLEN;
183   }
184
185   return 0;
186 }
187
188 size_t nghttp2_frame_headers_payload_nv_offset(nghttp2_headers *frame) {
189   return nghttp2_frame_priority_len(frame->hd.flags);
190 }
191
192 /*
193  * Call this function after payload was serialized, but not before
194  * changing buf->pos and serializing frame header.
195  *
196  * This function assumes bufs->cur points to the last buf chain of the
197  * frame(s).
198  *
199  * This function serializes frame header for HEADERS/PUSH_PROMISE and
200  * handles their successive CONTINUATION frames.
201  *
202  * We don't process any padding here.
203  */
204 static int frame_pack_headers_shared(nghttp2_bufs *bufs,
205                                      nghttp2_frame_hd *frame_hd) {
206   nghttp2_buf *buf;
207   nghttp2_buf_chain *ci, *ce;
208   nghttp2_frame_hd hd;
209
210   buf = &bufs->head->buf;
211
212   hd = *frame_hd;
213   hd.length = nghttp2_buf_len(buf);
214
215   DEBUGF(fprintf(stderr, "send: HEADERS/PUSH_PROMISE, payloadlen=%zu\n",
216                  hd.length));
217
218   /* We have multiple frame buffers, which means one or more
219      CONTINUATION frame is involved. Remove END_HEADERS flag from the
220      first frame. */
221   if (bufs->head != bufs->cur) {
222     hd.flags &= ~NGHTTP2_FLAG_END_HEADERS;
223   }
224
225   buf->pos -= NGHTTP2_FRAME_HDLEN;
226   nghttp2_frame_pack_frame_hd(buf->pos, &hd);
227
228   if (bufs->head != bufs->cur) {
229     /* 2nd and later frames are CONTINUATION frames. */
230     hd.type = NGHTTP2_CONTINUATION;
231     /* We don't have no flags except for last CONTINUATION */
232     hd.flags = NGHTTP2_FLAG_NONE;
233
234     ce = bufs->cur;
235
236     for (ci = bufs->head->next; ci != ce; ci = ci->next) {
237       buf = &ci->buf;
238
239       hd.length = nghttp2_buf_len(buf);
240
241       DEBUGF(fprintf(stderr, "send: int CONTINUATION, payloadlen=%zu\n",
242                      hd.length));
243
244       buf->pos -= NGHTTP2_FRAME_HDLEN;
245       nghttp2_frame_pack_frame_hd(buf->pos, &hd);
246     }
247
248     buf = &ci->buf;
249     hd.length = nghttp2_buf_len(buf);
250     /* Set END_HEADERS flag for last CONTINUATION */
251     hd.flags = NGHTTP2_FLAG_END_HEADERS;
252
253     DEBUGF(fprintf(stderr, "send: last CONTINUATION, payloadlen=%zu\n",
254                    hd.length));
255
256     buf->pos -= NGHTTP2_FRAME_HDLEN;
257     nghttp2_frame_pack_frame_hd(buf->pos, &hd);
258   }
259
260   return 0;
261 }
262
263 int nghttp2_frame_pack_headers(nghttp2_bufs *bufs, nghttp2_headers *frame,
264                                nghttp2_hd_deflater *deflater) {
265   size_t nv_offset;
266   int rv;
267   nghttp2_buf *buf;
268
269   assert(bufs->head == bufs->cur);
270
271   nv_offset = nghttp2_frame_headers_payload_nv_offset(frame);
272
273   buf = &bufs->cur->buf;
274
275   buf->pos += nv_offset;
276   buf->last = buf->pos;
277
278   /* This call will adjust buf->last to the correct position */
279   rv = nghttp2_hd_deflate_hd_bufs(deflater, bufs, frame->nva, frame->nvlen);
280
281   if (rv == NGHTTP2_ERR_BUFFER_ERROR) {
282     rv = NGHTTP2_ERR_HEADER_COMP;
283   }
284
285   buf->pos -= nv_offset;
286
287   if (rv != 0) {
288     return rv;
289   }
290
291   if (frame->hd.flags & NGHTTP2_FLAG_PRIORITY) {
292     nghttp2_frame_pack_priority_spec(buf->pos, &frame->pri_spec);
293   }
294
295   frame->padlen = 0;
296   frame->hd.length = nghttp2_bufs_len(bufs);
297
298   return frame_pack_headers_shared(bufs, &frame->hd);
299 }
300
301 void nghttp2_frame_pack_priority_spec(uint8_t *buf,
302                                       const nghttp2_priority_spec *pri_spec) {
303   nghttp2_put_uint32be(buf, pri_spec->stream_id);
304   if (pri_spec->exclusive) {
305     buf[0] |= 0x80;
306   }
307   buf[4] = pri_spec->weight - 1;
308 }
309
310 void nghttp2_frame_unpack_priority_spec(nghttp2_priority_spec *pri_spec,
311                                         uint8_t flags _U_,
312                                         const uint8_t *payload,
313                                         size_t payloadlen _U_) {
314   int32_t dep_stream_id;
315   uint8_t exclusive;
316   int32_t weight;
317
318   dep_stream_id = nghttp2_get_uint32(payload) & NGHTTP2_STREAM_ID_MASK;
319   exclusive = (payload[0] & 0x80) > 0;
320   weight = payload[4] + 1;
321
322   nghttp2_priority_spec_init(pri_spec, dep_stream_id, weight, exclusive);
323 }
324
325 int nghttp2_frame_unpack_headers_payload(nghttp2_headers *frame,
326                                          const uint8_t *payload,
327                                          size_t payloadlen) {
328   if (frame->hd.flags & NGHTTP2_FLAG_PRIORITY) {
329     nghttp2_frame_unpack_priority_spec(&frame->pri_spec, frame->hd.flags,
330                                        payload, payloadlen);
331   } else {
332     nghttp2_priority_spec_default_init(&frame->pri_spec);
333   }
334
335   frame->nva = NULL;
336   frame->nvlen = 0;
337
338   return 0;
339 }
340
341 int nghttp2_frame_pack_priority(nghttp2_bufs *bufs, nghttp2_priority *frame) {
342   nghttp2_buf *buf;
343
344   assert(bufs->head == bufs->cur);
345
346   buf = &bufs->head->buf;
347
348   assert(nghttp2_buf_avail(buf) >= NGHTTP2_PRIORITY_SPECLEN);
349
350   buf->pos -= NGHTTP2_FRAME_HDLEN;
351
352   nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
353
354   nghttp2_frame_pack_priority_spec(buf->last, &frame->pri_spec);
355
356   buf->last += NGHTTP2_PRIORITY_SPECLEN;
357
358   return 0;
359 }
360
361 void nghttp2_frame_unpack_priority_payload(nghttp2_priority *frame,
362                                            const uint8_t *payload,
363                                            size_t payloadlen) {
364   nghttp2_frame_unpack_priority_spec(&frame->pri_spec, frame->hd.flags, payload,
365                                      payloadlen);
366 }
367
368 int nghttp2_frame_pack_rst_stream(nghttp2_bufs *bufs,
369                                   nghttp2_rst_stream *frame) {
370   nghttp2_buf *buf;
371
372   assert(bufs->head == bufs->cur);
373
374   buf = &bufs->head->buf;
375
376   assert(nghttp2_buf_avail(buf) >= 4);
377
378   buf->pos -= NGHTTP2_FRAME_HDLEN;
379
380   nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
381
382   nghttp2_put_uint32be(buf->last, frame->error_code);
383   buf->last += 4;
384
385   return 0;
386 }
387
388 void nghttp2_frame_unpack_rst_stream_payload(nghttp2_rst_stream *frame,
389                                              const uint8_t *payload,
390                                              size_t payloadlen _U_) {
391   frame->error_code = nghttp2_get_uint32(payload);
392 }
393
394 int nghttp2_frame_pack_settings(nghttp2_bufs *bufs, nghttp2_settings *frame) {
395   nghttp2_buf *buf;
396
397   assert(bufs->head == bufs->cur);
398
399   buf = &bufs->head->buf;
400
401   if (nghttp2_buf_avail(buf) < (ssize_t)frame->hd.length) {
402     return NGHTTP2_ERR_FRAME_SIZE_ERROR;
403   }
404
405   buf->pos -= NGHTTP2_FRAME_HDLEN;
406
407   nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
408
409   buf->last +=
410       nghttp2_frame_pack_settings_payload(buf->last, frame->iv, frame->niv);
411
412   return 0;
413 }
414
415 size_t nghttp2_frame_pack_settings_payload(uint8_t *buf,
416                                            const nghttp2_settings_entry *iv,
417                                            size_t niv) {
418   size_t i;
419   for (i = 0; i < niv; ++i, buf += NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH) {
420     nghttp2_put_uint16be(buf, iv[i].settings_id);
421     nghttp2_put_uint32be(buf + 2, iv[i].value);
422   }
423   return NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH * niv;
424 }
425
426 int nghttp2_frame_unpack_settings_payload(nghttp2_settings *frame,
427                                           nghttp2_settings_entry *iv,
428                                           size_t niv, nghttp2_mem *mem) {
429   size_t payloadlen = niv * sizeof(nghttp2_settings_entry);
430
431   if (niv == 0) {
432     frame->iv = NULL;
433   } else {
434     frame->iv = nghttp2_mem_malloc(mem, payloadlen);
435
436     if (frame->iv == NULL) {
437       return NGHTTP2_ERR_NOMEM;
438     }
439
440     memcpy(frame->iv, iv, payloadlen);
441   }
442
443   frame->niv = niv;
444   return 0;
445 }
446
447 void nghttp2_frame_unpack_settings_entry(nghttp2_settings_entry *iv,
448                                          const uint8_t *payload) {
449   iv->settings_id = nghttp2_get_uint16(&payload[0]);
450   iv->value = nghttp2_get_uint32(&payload[2]);
451 }
452
453 int nghttp2_frame_unpack_settings_payload2(nghttp2_settings_entry **iv_ptr,
454                                            size_t *niv_ptr,
455                                            const uint8_t *payload,
456                                            size_t payloadlen,
457                                            nghttp2_mem *mem) {
458   size_t i;
459
460   *niv_ptr = payloadlen / NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH;
461
462   if (*niv_ptr == 0) {
463     *iv_ptr = NULL;
464
465     return 0;
466   }
467
468   *iv_ptr =
469       nghttp2_mem_malloc(mem, (*niv_ptr) * sizeof(nghttp2_settings_entry));
470
471   if (*iv_ptr == NULL) {
472     return NGHTTP2_ERR_NOMEM;
473   }
474
475   for (i = 0; i < *niv_ptr; ++i) {
476     size_t off = i * NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH;
477     nghttp2_frame_unpack_settings_entry(&(*iv_ptr)[i], &payload[off]);
478   }
479
480   return 0;
481 }
482
483 int nghttp2_frame_pack_push_promise(nghttp2_bufs *bufs,
484                                     nghttp2_push_promise *frame,
485                                     nghttp2_hd_deflater *deflater) {
486   size_t nv_offset = 4;
487   int rv;
488   nghttp2_buf *buf;
489
490   assert(bufs->head == bufs->cur);
491
492   buf = &bufs->cur->buf;
493
494   buf->pos += nv_offset;
495   buf->last = buf->pos;
496
497   /* This call will adjust buf->last to the correct position */
498   rv = nghttp2_hd_deflate_hd_bufs(deflater, bufs, frame->nva, frame->nvlen);
499
500   if (rv == NGHTTP2_ERR_BUFFER_ERROR) {
501     rv = NGHTTP2_ERR_HEADER_COMP;
502   }
503
504   buf->pos -= nv_offset;
505
506   if (rv != 0) {
507     return rv;
508   }
509
510   nghttp2_put_uint32be(buf->pos, frame->promised_stream_id);
511
512   frame->padlen = 0;
513   frame->hd.length = nghttp2_bufs_len(bufs);
514
515   return frame_pack_headers_shared(bufs, &frame->hd);
516 }
517
518 int nghttp2_frame_unpack_push_promise_payload(nghttp2_push_promise *frame,
519                                               const uint8_t *payload,
520                                               size_t payloadlen _U_) {
521   frame->promised_stream_id =
522       nghttp2_get_uint32(payload) & NGHTTP2_STREAM_ID_MASK;
523   frame->nva = NULL;
524   frame->nvlen = 0;
525   return 0;
526 }
527
528 int nghttp2_frame_pack_ping(nghttp2_bufs *bufs, nghttp2_ping *frame) {
529   nghttp2_buf *buf;
530
531   assert(bufs->head == bufs->cur);
532
533   buf = &bufs->head->buf;
534
535   assert(nghttp2_buf_avail(buf) >= 8);
536
537   buf->pos -= NGHTTP2_FRAME_HDLEN;
538
539   nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
540
541   buf->last =
542       nghttp2_cpymem(buf->last, frame->opaque_data, sizeof(frame->opaque_data));
543
544   return 0;
545 }
546
547 void nghttp2_frame_unpack_ping_payload(nghttp2_ping *frame,
548                                        const uint8_t *payload,
549                                        size_t payloadlen _U_) {
550   memcpy(frame->opaque_data, payload, sizeof(frame->opaque_data));
551 }
552
553 int nghttp2_frame_pack_goaway(nghttp2_bufs *bufs, nghttp2_goaway *frame) {
554   int rv;
555   nghttp2_buf *buf;
556
557   assert(bufs->head == bufs->cur);
558
559   buf = &bufs->head->buf;
560
561   buf->pos -= NGHTTP2_FRAME_HDLEN;
562
563   nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
564
565   nghttp2_put_uint32be(buf->last, frame->last_stream_id);
566   buf->last += 4;
567
568   nghttp2_put_uint32be(buf->last, frame->error_code);
569   buf->last += 4;
570
571   rv = nghttp2_bufs_add(bufs, frame->opaque_data, frame->opaque_data_len);
572
573   if (rv == NGHTTP2_ERR_BUFFER_ERROR) {
574     return NGHTTP2_ERR_FRAME_SIZE_ERROR;
575   }
576
577   if (rv != 0) {
578     return rv;
579   }
580
581   return 0;
582 }
583
584 void nghttp2_frame_unpack_goaway_payload(nghttp2_goaway *frame,
585                                          const uint8_t *payload,
586                                          size_t payloadlen _U_,
587                                          uint8_t *var_gift_payload,
588                                          size_t var_gift_payloadlen) {
589   frame->last_stream_id = nghttp2_get_uint32(payload) & NGHTTP2_STREAM_ID_MASK;
590   frame->error_code = nghttp2_get_uint32(payload + 4);
591
592   frame->opaque_data = var_gift_payload;
593   frame->opaque_data_len = var_gift_payloadlen;
594 }
595
596 int nghttp2_frame_unpack_goaway_payload2(nghttp2_goaway *frame,
597                                          const uint8_t *payload,
598                                          size_t payloadlen, nghttp2_mem *mem) {
599   uint8_t *var_gift_payload;
600   size_t var_gift_payloadlen;
601
602   if (payloadlen > 8) {
603     var_gift_payloadlen = payloadlen - 8;
604   } else {
605     var_gift_payloadlen = 0;
606   }
607
608   payloadlen -= var_gift_payloadlen;
609
610   if (!var_gift_payloadlen) {
611     var_gift_payload = NULL;
612   } else {
613     var_gift_payload = nghttp2_mem_malloc(mem, var_gift_payloadlen);
614
615     if (var_gift_payload == NULL) {
616       return NGHTTP2_ERR_NOMEM;
617     }
618
619     memcpy(var_gift_payload, payload + 8, var_gift_payloadlen);
620   }
621
622   nghttp2_frame_unpack_goaway_payload(frame, payload, payloadlen,
623                                       var_gift_payload, var_gift_payloadlen);
624
625   return 0;
626 }
627
628 int nghttp2_frame_pack_window_update(nghttp2_bufs *bufs,
629                                      nghttp2_window_update *frame) {
630   nghttp2_buf *buf;
631
632   assert(bufs->head == bufs->cur);
633
634   buf = &bufs->head->buf;
635
636   assert(nghttp2_buf_avail(buf) >= 4);
637
638   buf->pos -= NGHTTP2_FRAME_HDLEN;
639
640   nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
641
642   nghttp2_put_uint32be(buf->last, frame->window_size_increment);
643   buf->last += 4;
644
645   return 0;
646 }
647
648 void nghttp2_frame_unpack_window_update_payload(nghttp2_window_update *frame,
649                                                 const uint8_t *payload,
650                                                 size_t payloadlen _U_) {
651   frame->window_size_increment =
652       nghttp2_get_uint32(payload) & NGHTTP2_WINDOW_SIZE_INCREMENT_MASK;
653 }
654
655 nghttp2_settings_entry *nghttp2_frame_iv_copy(const nghttp2_settings_entry *iv,
656                                               size_t niv, nghttp2_mem *mem) {
657   nghttp2_settings_entry *iv_copy;
658   size_t len = niv * sizeof(nghttp2_settings_entry);
659
660   if (len == 0) {
661     return NULL;
662   }
663
664   iv_copy = nghttp2_mem_malloc(mem, len);
665
666   if (iv_copy == NULL) {
667     return NULL;
668   }
669
670   memcpy(iv_copy, iv, len);
671
672   return iv_copy;
673 }
674
675 int nghttp2_nv_equal(const nghttp2_nv *a, const nghttp2_nv *b) {
676   return a->namelen == b->namelen && a->valuelen == b->valuelen &&
677          memcmp(a->name, b->name, a->namelen) == 0 &&
678          memcmp(a->value, b->value, a->valuelen) == 0;
679 }
680
681 void nghttp2_nv_array_del(nghttp2_nv *nva, nghttp2_mem *mem) {
682   nghttp2_mem_free(mem, nva);
683 }
684
685 static int bytes_compar(const uint8_t *a, size_t alen, const uint8_t *b,
686                         size_t blen) {
687   int rv;
688
689   if (alen == blen) {
690     return memcmp(a, b, alen);
691   }
692
693   if (alen < blen) {
694     rv = memcmp(a, b, alen);
695
696     if (rv == 0) {
697       return -1;
698     }
699
700     return rv;
701   }
702
703   rv = memcmp(a, b, blen);
704
705   if (rv == 0) {
706     return 1;
707   }
708
709   return rv;
710 }
711
712 int nghttp2_nv_compare_name(const nghttp2_nv *lhs, const nghttp2_nv *rhs) {
713   return bytes_compar(lhs->name, lhs->namelen, rhs->name, rhs->namelen);
714 }
715
716 static int nv_compar(const void *lhs, const void *rhs) {
717   const nghttp2_nv *a = (const nghttp2_nv *)lhs;
718   const nghttp2_nv *b = (const nghttp2_nv *)rhs;
719   int rv;
720
721   rv = bytes_compar(a->name, a->namelen, b->name, b->namelen);
722
723   if (rv == 0) {
724     return bytes_compar(a->value, a->valuelen, b->value, b->valuelen);
725   }
726
727   return rv;
728 }
729
730 void nghttp2_nv_array_sort(nghttp2_nv *nva, size_t nvlen) {
731   qsort(nva, nvlen, sizeof(nghttp2_nv), nv_compar);
732 }
733
734 int nghttp2_nv_array_copy(nghttp2_nv **nva_ptr, const nghttp2_nv *nva,
735                           size_t nvlen, nghttp2_mem *mem) {
736   size_t i;
737   uint8_t *data;
738   size_t buflen = 0;
739   nghttp2_nv *p;
740
741   for (i = 0; i < nvlen; ++i) {
742     buflen += nva[i].namelen + nva[i].valuelen;
743   }
744
745   if (nvlen == 0) {
746     *nva_ptr = NULL;
747
748     return 0;
749   }
750
751   buflen += sizeof(nghttp2_nv) * nvlen;
752
753   *nva_ptr = nghttp2_mem_malloc(mem, buflen);
754
755   if (*nva_ptr == NULL) {
756     return NGHTTP2_ERR_NOMEM;
757   }
758
759   p = *nva_ptr;
760   data = (uint8_t *)(*nva_ptr) + sizeof(nghttp2_nv) * nvlen;
761
762   for (i = 0; i < nvlen; ++i) {
763     p->flags = nva[i].flags;
764
765     memcpy(data, nva[i].name, nva[i].namelen);
766     p->name = data;
767     p->namelen = nva[i].namelen;
768     nghttp2_downcase(p->name, p->namelen);
769     data += nva[i].namelen;
770     memcpy(data, nva[i].value, nva[i].valuelen);
771     p->value = data;
772     p->valuelen = nva[i].valuelen;
773     data += nva[i].valuelen;
774     ++p;
775   }
776   return 0;
777 }
778
779 int nghttp2_iv_check(const nghttp2_settings_entry *iv, size_t niv) {
780   size_t i;
781   for (i = 0; i < niv; ++i) {
782     switch (iv[i].settings_id) {
783     case NGHTTP2_SETTINGS_HEADER_TABLE_SIZE:
784       if (iv[i].value > NGHTTP2_MAX_HEADER_TABLE_SIZE) {
785         return 0;
786       }
787       break;
788     case NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS:
789       break;
790     case NGHTTP2_SETTINGS_ENABLE_PUSH:
791       if (iv[i].value != 0 && iv[i].value != 1) {
792         return 0;
793       }
794       break;
795     case NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE:
796       if (iv[i].value > (uint32_t)NGHTTP2_MAX_WINDOW_SIZE) {
797         return 0;
798       }
799       break;
800     case NGHTTP2_SETTINGS_MAX_FRAME_SIZE:
801       if (iv[i].value < NGHTTP2_MAX_FRAME_SIZE_MIN ||
802           iv[i].value > NGHTTP2_MAX_FRAME_SIZE_MAX) {
803         return 0;
804       }
805       break;
806     case NGHTTP2_SETTINGS_MAX_HEADER_LIST_SIZE:
807       break;
808     }
809   }
810   return 1;
811 }
812
813 static void frame_set_pad(nghttp2_buf *buf, size_t padlen) {
814   size_t trail_padlen;
815   size_t newlen;
816
817   DEBUGF(fprintf(stderr, "send: padlen=%zu, shift left 1 bytes\n", padlen));
818
819   memmove(buf->pos - 1, buf->pos, NGHTTP2_FRAME_HDLEN);
820
821   --buf->pos;
822
823   buf->pos[4] |= NGHTTP2_FLAG_PADDED;
824
825   newlen = (nghttp2_get_uint32(buf->pos) >> 8) + padlen;
826   nghttp2_put_uint32be(buf->pos, (uint32_t)((newlen << 8) + buf->pos[3]));
827
828   trail_padlen = padlen - 1;
829   buf->pos[NGHTTP2_FRAME_HDLEN] = trail_padlen;
830
831   /* zero out padding */
832   memset(buf->last, 0, trail_padlen);
833   /* extend buffers trail_padlen bytes, since we ate previous padlen -
834      trail_padlen byte(s) */
835   buf->last += trail_padlen;
836
837   return;
838 }
839
840 int nghttp2_frame_add_pad(nghttp2_bufs *bufs, nghttp2_frame_hd *hd,
841                           size_t padlen) {
842   nghttp2_buf *buf;
843
844   if (padlen == 0) {
845     DEBUGF(fprintf(stderr, "send: padlen = 0, nothing to do\n"));
846
847     return 0;
848   }
849
850   /*
851    * We have arranged bufs like this:
852    *
853    *  0                   1                   2                   3
854    *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
855    * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
856    * | |Frame header     | Frame payload...                          :
857    * +-+-----------------+-------------------------------------------+
858    * | |Frame header     | Frame payload...                          :
859    * +-+-----------------+-------------------------------------------+
860    * | |Frame header     | Frame payload...                          :
861    * +-+-----------------+-------------------------------------------+
862    *
863    * We arranged padding so that it is included in the first frame
864    * completely.  For padded frame, we are going to adjust buf->pos of
865    * frame which includes padding and serialize (memmove) frame header
866    * in the correct position.  Also extends buf->last to include
867    * padding.
868    */
869
870   buf = &bufs->head->buf;
871
872   assert(nghttp2_buf_avail(buf) >= (ssize_t)padlen - 1);
873
874   frame_set_pad(buf, padlen);
875
876   hd->length += padlen;
877   hd->flags |= NGHTTP2_FLAG_PADDED;
878
879   DEBUGF(fprintf(stderr, "send: final payloadlen=%zu, padlen=%zu\n", hd->length,
880                  padlen));
881
882   return 0;
883 }