+/* The order of this list is taken from dpkg source lib/parse.c the fieldinfos
+ array. */
+static const char *iTFRewritePackageOrder[] = {
+ "Package",
+ "Essential",
+ "Status",
+ "Priority",
+ "Section",
+ "Installed-Size",
+ "Maintainer",
+ "Architecture",
+ "Source",
+ "Version",
+ "Revision", // Obsolete
+ "Config-Version", // Obsolete
+ "Replaces",
+ "Provides",
+ "Depends",
+ "Pre-Depends",
+ "Recommends",
+ "Suggests",
+ "Conflicts",
+ "Conffiles",
+ "Filename",
+ "Size",
+ "MD5Sum",
+ "SHA1Sum",
+ "MSDOS-Filename", // Obsolete
+ "Description",
+ 0};
+static const char *iTFRewriteSourceOrder[] = {"Package",
+ "Source",
+ "Binary",
+ "Version",
+ "Priority",
+ "Section",
+ "Maintainer",
+ "Build-Depends",
+ "Build-Depends-Indep",
+ "Build-Conflicts",
+ "Build-Conflicts-Indep",
+ "Architecture",
+ "Standards-Version",
+ "Format",
+ "Directory",
+ "Files",
+ 0};
+
+/* Two levels of initialization are used because gcc will set the symbol
+ size of an array to the length of the array, causing dynamic relinking
+ errors. Doing this makes the symbol size constant */
+const char **TFRewritePackageOrder = iTFRewritePackageOrder;
+const char **TFRewriteSourceOrder = iTFRewriteSourceOrder;
+
+bool TFRewrite(FILE *Output,pkgTagSection const &Tags,const char *Order[],
+ TFRewriteData *Rewrite)
+{
+ unsigned char Visited[256]; // Bit 1 is Order, Bit 2 is Rewrite
+ for (unsigned I = 0; I != 256; I++)
+ Visited[I] = 0;
+
+ // Set new tag up as necessary.
+ for (unsigned int J = 0; Rewrite != 0 && Rewrite[J].Tag != 0; J++)