]> git.kaiwu.me - nginx.git/commitdiff
HTTP/3: fixed prefixed integer encoding and decoding.
authorRoman Arutyunyan <arut@nginx.com>
Thu, 2 Jul 2020 12:15:55 +0000 (15:15 +0300)
committerRoman Arutyunyan <arut@nginx.com>
Thu, 2 Jul 2020 12:15:55 +0000 (15:15 +0300)
Previously bytes were ordered from MSB to LSB, but the right order is the
reverse.

src/http/v3/ngx_http_v3.c
src/http/v3/ngx_http_v3_parse.c
src/http/v3/ngx_http_v3_parse.h

index f7f79b9f4acd2103c9d6ad64f98ccd5c799fd4b8..e796da772aae4ba80ebbe9abf59f520e2c5b0a78 100644 (file)
@@ -78,23 +78,22 @@ ngx_http_v3_encode_prefix_int(u_char *p, uint64_t value, ngx_uint_t prefix)
 
     value -= thresh;
 
-    for (n = 10; n > 1; n--) {
-        if (value >> (7 * (n - 1))) {
-            break;
+    if (p == NULL) {
+        for (n = 2; value >= 128; n++) {
+            value >>= 7;
         }
-    }
 
-    if (p == NULL) {
-        return n + 1;
+        return n;
     }
 
     *p++ |= thresh;
 
-    for ( /* void */ ; n > 1; n--) {
-        *p++ = 0x80 | (value >> 7 * (n - 1));
+    while (value >= 128) {
+        *p++ = 0x80 | value;
+        value >>= 7;
     }
 
-    *p++ = value & 0x7f;
+    *p++ = value;
 
     return (uintptr_t) p;
 }
index 71a643d70fe3dc8deb9ef8cc96fe4df7dd41915e..85491082e25fed1182aa6d34033e11e493ad2857 100644 (file)
@@ -91,6 +91,7 @@ ngx_int_t
 ngx_http_v3_parse_prefix_int(ngx_connection_t *c,
     ngx_http_v3_parse_prefix_int_t *st, ngx_uint_t prefix, u_char ch)
 {
+    ngx_uint_t  mask;
     enum {
         sw_start = 0,
         sw_value
@@ -100,25 +101,25 @@ ngx_http_v3_parse_prefix_int(ngx_connection_t *c,
 
     case sw_start:
 
-        st->mask = (1 << prefix) - 1;
-        st->value = (ch & st->mask);
+        mask = (1 << prefix) - 1;
+        st->value = ch & mask;
 
-        if (st->value != st->mask) {
+        if (st->value != mask) {
             goto done;
         }
 
-        st->value = 0;
+        st->shift = 0;
         st->state = sw_value;
         break;
 
     case sw_value:
 
-        st->value = (st->value << 7) + (ch & 0x7f);
+        st->value += (ch & 0x7f) << st->shift;
         if (ch & 0x80) {
+            st->shift += 7;
             break;
         }
 
-        st->value += st->mask;
         goto done;
     }
 
index ec78c7c35921cc0659a4e3d46c6beb71b51a5432..17ff6c777cb8d06d27dcbaa60c8eec3d9fe7f701 100644 (file)
@@ -22,7 +22,7 @@ typedef struct {
 
 typedef struct {
     ngx_uint_t                      state;
-    ngx_uint_t                      mask;
+    ngx_uint_t                      shift;
     uint64_t                        value;
 } ngx_http_v3_parse_prefix_int_t;