]> git.lizzy.rs Git - plan9front.git/commitdiff
ip/torrent: alphabetically order dictionary keys when producing torrent
authorcinap_lenrek <cinap_lenrek@gmx.de>
Tue, 24 Apr 2012 21:50:08 +0000 (23:50 +0200)
committercinap_lenrek <cinap_lenrek@gmx.de>
Tue, 24 Apr 2012 21:50:08 +0000 (23:50 +0200)
sys/src/cmd/ip/torrent.c

index d144387b716377dda1db90d0694b28dff5b23634..b8034e20a6d8c0dec6946f0e3c6a748e3d0b919f 100644 (file)
@@ -850,8 +850,11 @@ mktorrent(int fd, Dict *alist, Dict *wlist)
                if(npieces <= 8*1024 || blocksize >= 2*1024*1024)
                        break;
        }
-       print("d");
-       print("8:announce%ld:%s", strlen(alist->str), alist->str);
+
+       /*
+        * keys in dictionaries have to be ordered alphabetically
+        */
+       print("d8:announce%ld:%s", strlen(alist->str), alist->str);
        if(alist->next){
                print("13:announce-listl");
                print("l%ld:%se", strlen(alist->str), alist->str);
@@ -859,19 +862,10 @@ mktorrent(int fd, Dict *alist, Dict *wlist)
                        print("l%ld:%se", strlen(alist->str), alist->str);
                print("e");
        }
-       if(wlist){
-               if(wlist->next){
-                       print("8:url-listl");
-                       for(; wlist; wlist = wlist->next)
-                               print("%ld:%s", strlen(wlist->str), wlist->str);
-                       print("e");
-               } else
-                       print("8:url-list%ld:%s", strlen(wlist->str), wlist->str);
-       }
-       print("4:info");
-       print("d");
-       print("4:name%ld:%s", strlen(d->name), d->name);
+
+       print("4:infod");
        print("6:lengthi%llde", d->length);
+       print("4:name%ld:%s", strlen(d->name), d->name);
        print("12:piece lengthi%de", blocksize);
        print("6:pieces%d:", npieces*sizeof(h));
        free(d);
@@ -884,13 +878,24 @@ mktorrent(int fd, Dict *alist, Dict *wlist)
                }
                npieces--;
        }
-       free(b);
        if(npieces){
                werrstr("read failed: %r");
                return -1;
        }
+       free(b);
        print("e");
+
+       if(wlist){
+               if(wlist->next){
+                       print("8:url-listl");
+                       for(; wlist; wlist = wlist->next)
+                               print("%ld:%s", strlen(wlist->str), wlist->str);
+                       print("e");
+               } else
+                       print("8:url-list%ld:%s", strlen(wlist->str), wlist->str);
+       }
        print("e");
+
        return 0;
 }