Exemple #1
0
        public DBEntry(CTDBResponseEntry ctdbRespEntry)
        {
            this.syndrome = ctdbRespEntry.syndrome == null
                ? ParityToSyndrome.Parity2Syndrome(1, 1, 8, 8, Convert.FromBase64String(ctdbRespEntry.parity))
                : ParityToSyndrome.Bytes2Syndrome(1, Math.Min(AccurateRipVerify.maxNpar, ctdbRespEntry.npar), Convert.FromBase64String(ctdbRespEntry.syndrome));

            this.conf      = ctdbRespEntry.confidence;
            this.stride    = ctdbRespEntry.stride * 2;
            this.crc       = uint.Parse(ctdbRespEntry.crc32, NumberStyles.HexNumber);
            this.id        = ctdbRespEntry.id;
            this.toc       = CDImageLayout.FromString(ctdbRespEntry.toc);
            this.hasParity = ctdbRespEntry.hasparity;
            if (ctdbRespEntry.trackcrcs != null)
            {
                var crcs = ctdbRespEntry.trackcrcs.Split(' ');
                if (crcs.Length == this.toc.AudioTracks)
                {
                    this.trackcrcs = new uint[crcs.Length];
                    for (int i = 0; i < this.trackcrcs.Length; i++)
                    {
                        this.trackcrcs[i] = uint.Parse(crcs[i], NumberStyles.HexNumber);
                    }
                }
            }
        }
        public unsafe void CDRepairEncodeSynParTest()
        {
            var syndrome   = encode.AR.GetSyndrome();
            var parityCopy = ParityToSyndrome.Syndrome2Parity(syndrome);

            CollectionAssert.AreEqual(syndrome, ParityToSyndrome.Parity2Syndrome(syndrome.GetLength(0), syndrome.GetLength(0), syndrome.GetLength(1), syndrome.GetLength(1), parityCopy));
        }
Exemple #3
0
        private static byte[] Unparse(ushort[,] syndrome, int version)
        {
            if (version == 1)
            {
                return(ParityToSyndrome.Syndrome2Bytes(syndrome));
            }

            var output      = ParityToSyndrome.Syndrome2Parity(syndrome);
            var newcontents = new MemoryStream();

            using (DBHDR FTYP = new DBHDR(newcontents, "ftyp"))
                FTYP.Write("CTDB");
            using (DBHDR CTDB = new DBHDR(newcontents, "CTDB"))
            {
                using (DBHDR HEAD = CTDB.HDR("HEAD"))
                {
                    using (DBHDR VERS = HEAD.HDR("VERS")) VERS.Write(0x101);
                }
                using (DBHDR DISC = CTDB.HDR("DISC"))
                {
                    using (DBHDR CONF = DISC.HDR("CONF")) CONF.Write(1);
                    using (DBHDR NPAR = DISC.HDR("NPAR")) NPAR.Write(8);
                    using (DBHDR CRC_ = DISC.HDR("CRC ")) CRC_.Write(0);
                    using (DBHDR PAR_ = DISC.HDR("PAR ")) PAR_.Write(output);
                }
            }
            return(newcontents.ToArray());
        }
        public void CDRepairEncodeSpeedTest()
        {
            var generator = new TestImageGenerator("0 75000", seed, 0, 0);
            var encode    = generator.CreateCDRepairEncode(stride);

            Assert.AreEqual <string>(Convert.ToBase64String(Convert.FromBase64String(encodeSyndrome1), 0, AccurateRipVerify.maxNpar * 2 * 4),
                                     Convert.ToBase64String(ParityToSyndrome.Syndrome2Bytes(encode.AR.GetSyndrome(AccurateRipVerify.maxNpar, 4))), "syndrome mismatch");
        }
 public void CDRepairEncodeParityTest()
 {
     for (int n = 8; n <= AccurateRipVerify.maxNpar; n *= 2)
     {
         Assert.AreEqual <string>(encodeParity[n], Convert.ToBase64String(ParityToSyndrome.Syndrome2Parity(encode.AR.GetSyndrome(n)), 0, 64));
     }
     Assert.AreEqual <uint>(377539636, encode.CRC);
 }
        public unsafe void CDRepairSyndrome2ParitySpeedTest()
        {
            var syndrome = encode.AR.GetSyndrome();

            byte[] parityCopy = ParityToSyndrome.Syndrome2Parity(syndrome);
            for (int t = 0; t < 100; t++)
            {
                ParityToSyndrome.Syndrome2Parity(syndrome, parityCopy);
            }
            CollectionAssert.AreEqual(syndrome, ParityToSyndrome.Parity2Syndrome(syndrome.GetLength(0), syndrome.GetLength(0), syndrome.GetLength(1), syndrome.GetLength(1), parityCopy));
        }
Exemple #7
0
 static void Main(string[] args)
 {
     if (args.Length == 2 && (0 == string.Compare(args[0], "upconvert", true) || 0 == string.Compare(args[0], "downconvert", true)))
     {
         int id       = int.Parse(args[1]);
         var version  = 0 == string.Compare(args[0], "upconvert", true) ? 1 : 2;
         var contents = Fetch("http://p.cuetools.net/" + id.ToString());
         var syndrome = Parse(contents, version);
         var stdout   = System.Console.OpenStandardOutput();
         var output   = Unparse(syndrome, version);
         stdout.Write(output, 0, output.Length);
     }
     else if (args.Length == 2 && 0 == string.Compare(args[0], "p2s", true))
     {
         var p      = ParityToSyndrome.Parity2Syndrome(1, 1, 8, 8, Convert.FromBase64String(args[1]));
         var output = ParityToSyndrome.Syndrome2Bytes(p);
         System.Console.Write(Convert.ToBase64String(output));
     }
     else
     {
         throw new Exception("Usage: upconvert <id> | downconvert <id> | p2s <parity>");
     }
 }
        public unsafe void CDRepairSyndrome2BytesTest()
        {
            var syndrome = encode.AR.GetSyndrome();

            CollectionAssert.AreEqual(syndrome, ParityToSyndrome.Bytes2Syndrome(stride, AccurateRipVerify.maxNpar, ParityToSyndrome.Syndrome2Bytes(syndrome)));
        }
 public void CDRepairEncodeSyndromeTest()
 {
     for (int n = 4; n <= AccurateRipVerify.maxNpar; n *= 2)
     {
         Assert.AreEqual <string>(Convert.ToBase64String(Convert.FromBase64String(encodeSyndrome), 0, n * 2 * 4), Convert.ToBase64String(ParityToSyndrome.Syndrome2Bytes(encode.AR.GetSyndrome(n, 4))), "syndrome mismatch");
         Assert.AreEqual <string>(Convert.ToBase64String(Convert.FromBase64String(encodeSyndromePosOffset), 0, n * 2 * 4), Convert.ToBase64String(ParityToSyndrome.Syndrome2Bytes(encode.AR.GetSyndrome(n, 4, offset))), "syndrome with offset mismatch");
         Assert.AreEqual <string>(Convert.ToBase64String(Convert.FromBase64String(encodeSyndromeNegOffset), 0, n * 2 * 4), Convert.ToBase64String(ParityToSyndrome.Syndrome2Bytes(encode.AR.GetSyndrome(n, 4, -offset))), "syndrome with neg offset mismatch");
     }
     Assert.AreEqual <uint>(377539636, encode.CRC);
 }
Exemple #10
0
        protected CTDBResponse DoSubmit(int confidence, int quality, string artist, string title, string barcode, bool upload, DBEntry confirm, int npar)
        {
            var  files = new List <UploadFile>();
            long maxId = 0;

            foreach (var e in this.entries)
            {
                maxId = Math.Max(maxId, e.id);
            }

            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(urlbase + "/submit2.php");

            req.Proxy            = proxy;
            req.UserAgent        = this.userAgent;
            req.Timeout          = connectTimeout;
            req.ReadWriteTimeout = socketTimeout;
            NameValueCollection form = new NameValueCollection();
            int offset = 0;

            if (confirm != null)
            {
                offset = -confirm.offset;

#if DEBUG
                // Optional sanity check: should be done by server
                if (verify.AR.CTDBCRC(offset) != confirm.crc)
                {
                    throw new Exception("crc mismatch");
                }

                if (confirm.trackcrcs != null)
                {
                    bool crcEquals = true;
                    for (int i = 0; i < confirm.trackcrcs.Length; i++)
                    {
                        crcEquals &= verify.TrackCRC(i + 1, offset) == confirm.trackcrcs[i];
                    }
                    if (!crcEquals)
                    {
                        throw new Exception("track crc mismatch");
                    }
                }

                var  syn2   = verify.AR.GetSyndrome(confirm.Npar, 1, offset);
                bool equals = true;
                for (int i = 0; i < confirm.Npar; i++)
                {
                    equals &= confirm.syndrome[0, i] == syn2[0, i];
                }
                if (!equals)
                {
                    throw new Exception("syndrome mismatch");
                }
#endif
            }
            if (upload)
            {
                files.Add(new UploadFile(new MemoryStream(ParityToSyndrome.Syndrome2Bytes(verify.AR.GetSyndrome(npar, -1, offset))), "parityfile", "data.bin", "application/octet-stream"));
                form.Add("parityfile", "1");
            }
            form.Add("parity", Convert.ToBase64String(ParityToSyndrome.Syndrome2Parity(verify.AR.GetSyndrome(8, 1, offset))));
            form.Add("syndrome", Convert.ToBase64String(ParityToSyndrome.Syndrome2Bytes(verify.AR.GetSyndrome(npar, 1, offset))));
            if (confirm != null)
            {
                form.Add("confirmid", confirm.id.ToString());
            }
            form.Add("ctdb", "2");
            form.Add("npar", npar.ToString());
            form.Add("maxid", maxId.ToString());
            form.Add("toc", toc.ToString());
            form.Add("crc32", ((int)verify.AR.CTDBCRC(offset)).ToString());
            form.Add("trackcrcs", verify.GetTrackCRCs(offset));
            form.Add("confidence", confidence.ToString());
            form.Add("userid", GetUUID());
            form.Add("quality", quality.ToString());
            if (driveName != null && driveName != "")
            {
                form.Add("drivename", driveName);
            }
            if (barcode != null && barcode != "")
            {
                form.Add("barcode", barcode);
            }
            if (artist != null && artist != "")
            {
                form.Add("artist", artist);
            }
            if (title != null && title != "")
            {
                form.Add("title", title);
            }

            currentReq = req;
            try
            {
                using (HttpWebResponse resp = uploadHelper.Upload(req, files.ToArray(), form))
                {
                    if (resp.StatusCode == HttpStatusCode.OK)
                    {
                        using (Stream s = resp.GetResponseStream())
                        {
                            var serializer = new XmlSerializer(typeof(CTDBResponse));
                            return(serializer.Deserialize(s) as CTDBResponse);
                        }
                    }
                    else
                    {
                        return(new CTDBResponse()
                        {
                            status = "database access error", message = resp.StatusCode.ToString()
                        });
                    }
                }
            }
            catch (WebException ex)
            {
                return(new CTDBResponse()
                {
                    status = "database access error", message = ex.Message ?? ex.Status.ToString()
                });
            }
            catch (Exception ex)
            {
                return(new CTDBResponse()
                {
                    status = "database access error", message = ex.Message
                });
            }
            finally
            {
                currentReq = null;
            }
        }
Exemple #11
0
        public ushort[,] FetchDB(DBEntry entry, int npar, ushort[,] syn)
        {
            string         url     = entry.hasParity[0] == '/' ? urlbase + entry.hasParity : entry.hasParity;
            HttpWebRequest req     = (HttpWebRequest)WebRequest.Create(url);
            int            prevLen = syn == null ? 0 : syn.GetLength(1) * entry.stride * 2;

            req.Method                 = "GET";
            req.Proxy                  = proxy;
            req.UserAgent              = this.userAgent;
            req.Timeout                = connectTimeout;
            req.ReadWriteTimeout       = socketTimeout;
            req.AutomaticDecompression = DecompressionMethods.None;
            req.AddRange(prevLen, npar * entry.stride * 2 - 1);

            currentReq = req;
            try
            {
                using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse())
                {
                    if (resp.StatusCode != HttpStatusCode.OK && resp.StatusCode != HttpStatusCode.PartialContent)
                    {
                        entry.httpStatus = resp.StatusCode;
                        return(null);
                    }
                    if (resp.StatusCode == HttpStatusCode.OK && resp.ContentLength == entry.Npar * entry.stride * 2)
                    {
                        npar    = entry.Npar;
                        prevLen = 0;
                        syn     = null;
                    }
                    else if (resp.StatusCode != HttpStatusCode.PartialContent || (resp.ContentLength + prevLen) != npar * entry.stride * 2)
                    {
                        entry.httpStatus = HttpStatusCode.PartialContent;
                        return(null);
                    }

                    using (Stream responseStream = resp.GetResponseStream())
                    {
                        byte[] contents = syn == null ? null : ParityToSyndrome.Syndrome2Bytes(syn);
                        Array.Resize(ref contents, prevLen + (int)resp.ContentLength);
                        int pos = prevLen, count = 0;
                        do
                        {
                            if (uploadHelper.onProgress != null)
                            {
                                uploadHelper.onProgress(url, new UploadProgressEventArgs(req.RequestUri.AbsoluteUri, ((double)pos) / (entry.Npar * entry.stride * 2)));
                            }
                            count = responseStream.Read(contents, pos, Math.Min(contents.Length - pos, 32768));
                            pos  += count;
                        } while (count != 0);

                        if (pos != contents.Length)
                        {
                            entry.httpStatus = HttpStatusCode.PartialContent;
                            return(null);
                        }

                        syn = ParityToSyndrome.Bytes2Syndrome(entry.stride, npar, contents);
                        for (int i = 0; i < npar; i++)
                        {
                            if (syn[0, i] != entry.syndrome[0, i])
                            {
                                entry.httpStatus = HttpStatusCode.Conflict;
                                return(null);
                            }
                        }
                        entry.httpStatus = HttpStatusCode.OK;
                        return(syn);
                    }
                }
            }
            catch (WebException ex)
            {
                if (ex.Status == WebExceptionStatus.ProtocolError)
                {
                    entry.httpStatus = ((HttpWebResponse)ex.Response).StatusCode;
                }
                else
                {
                    entry.httpStatus = HttpStatusCode.BadRequest;
                }
            }
            finally
            {
                currentReq = null;
            }
            return(null);
        }
Exemple #12
0
        private static ushort[,] Parse(byte[] contents, int version)
        {
            if (version == 2)
            {
                int npar = contents.Length / stride / 2;
                if (npar < 8 || npar > 32 || contents.Length != npar * stride * 2)
                {
                    throw new Exception("invalid parity length");
                }
                return(ParityToSyndrome.Bytes2Syndrome(stride, 8, contents));
            }

            if (contents.Length < 8 * stride * 2 ||
                contents.Length > 8 * stride * 4)
            {
                throw new Exception("invalid length");
            }

            ReadDB rdr = new ReadDB(contents);

            int    end;
            string hdr   = rdr.ReadHDR(out end);
            uint   magic = rdr.ReadUInt();

            if (hdr != "ftyp" || magic != 0x43544442 || end != rdr.pos)
            {
                throw new Exception("invalid CTDB file");
            }
            hdr = rdr.ReadHDR(out end);
            if (hdr != "CTDB" || end != contents.Length)
            {
                throw new Exception("invalid CTDB file");
            }
            hdr = rdr.ReadHDR(out end);
            if (hdr != "HEAD")
            {
                throw new Exception("invalid CTDB file");
            }
            int endHead = end;

            while (rdr.pos < endHead)
            {
                hdr     = rdr.ReadHDR(out end);
                rdr.pos = end;
            }
            rdr.pos = endHead;
            while (rdr.pos < contents.Length)
            {
                hdr = rdr.ReadHDR(out end);
                if (hdr != "DISC")
                {
                    rdr.pos = end;
                    continue;
                }
                int endDisc = end;
                int parPos = 0, parLen = 0;
                while (rdr.pos < endDisc)
                {
                    hdr = rdr.ReadHDR(out end);
                    if (hdr == "PAR ")
                    {
                        parPos = rdr.pos;
                        parLen = end - rdr.pos;
                    }
                    rdr.pos = end;
                }
                if (parPos != 0)
                {
                    if (parLen != 8 * stride * 2)
                    {
                        throw new Exception("invalid parity length");
                    }
                    return(ParityToSyndrome.Parity2Syndrome(stride, stride, 8, 8, contents, parPos));
                }
            }
            throw new Exception("invalid CTDB file");
        }