func HashesFromGZHeader(inpath string, md5crcBuffer []byte) (*Hashes, int64, error) { romGZ, err := os.Open(inpath) if err != nil { return nil, 0, err } defer romGZ.Close() gzr, err := gzip.NewReader(romGZ) if err != nil { return nil, 0, err } defer gzr.Close() md5crcBuffer = gzr.Header.Extra var hh *Hashes var size int64 if len(md5crcBuffer) == md5.Size+crc32.Size+8 { hh = new(Hashes) hh.Md5 = make([]byte, md5.Size) copy(hh.Md5, md5crcBuffer[:md5.Size]) hh.Crc = make([]byte, crc32.Size) copy(hh.Crc, md5crcBuffer[md5.Size:md5.Size+crc32.Size]) size = util.BytesToInt64(md5crcBuffer[md5.Size+crc32.Size:]) } return hh, size, nil }
func (depot *Depot) SHA1InDepot(sha1Hex string) (bool, *Hashes, string, int64, error) { for _, root := range depot.roots { rompath := pathFromSha1HexEncoding(root, sha1Hex, gzipSuffix) exists, err := PathExists(rompath) if err != nil { return false, nil, "", 0, err } var size int64 if exists { hh := new(Hashes) sha1Bytes, err := hex.DecodeString(sha1Hex) if err != nil { return false, nil, "", 0, err } hh.Sha1 = sha1Bytes romGZ, err := os.Open(rompath) if err != nil { return false, nil, "", 0, err } defer romGZ.Close() gzr, err := gzip.NewReader(romGZ) if err != nil { return false, nil, "", 0, err } defer gzr.Close() md5crcBuffer := gzr.Header.Extra if len(md5crcBuffer) == md5.Size+crc32.Size+8 { hh.Md5 = make([]byte, md5.Size) copy(hh.Md5, md5crcBuffer[:md5.Size]) hh.Crc = make([]byte, crc32.Size) copy(hh.Crc, md5crcBuffer[md5.Size:md5.Size+crc32.Size]) size = util.BytesToInt64(md5crcBuffer[md5.Size+crc32.Size:]) } else { glog.Warningf("rom %s has missing gzip md5 or crc header", rompath) } return true, hh, rompath, size, nil } } return false, nil, "", 0, nil }
func (rs *RombaService) lookup(cmd *commander.Command, args []string) error { size := cmd.Flag.Lookup("size").Value.Get().(int64) outpath := cmd.Flag.Lookup("out").Value.Get().(string) for _, arg := range args { fmt.Fprintf(cmd.Stdout, "----------------------------------------\n") fmt.Fprintf(cmd.Stdout, "key: %s\n", arg) if strings.HasPrefix(arg, "0x") { arg = arg[2:] } hash, err := hex.DecodeString(arg) if err != nil { return err } if len(hash) == sha1.Size { dat, err := rs.romDB.GetDat(hash) if err != nil { return err } if dat != nil { fmt.Fprintf(cmd.Stdout, "-----------------\n") fmt.Fprintf(cmd.Stdout, "dat with sha1 %s = %s\n", arg, types.PrintShortDat(dat)) } } if size != -1 || len(hash) == sha1.Size { r := new(types.Rom) r.Size = size switch len(hash) { case md5.Size: r.Md5 = hash case crc32.Size: r.Crc = hash case sha1.Size: r.Sha1 = hash default: return fmt.Errorf("found unknown hash size: %d", len(hash)) } err = rs.lookupRom(cmd, r, outpath) if err != nil { return err } fmt.Fprintf(cmd.Stdout, "-----------------\n") fmt.Fprintf(cmd.Stdout, "DebugGet:\n%s\n", rs.romDB.DebugGet(hash, size)) } else { suffixes, err := rs.romDB.ResolveHash(hash) if err != nil { return err } for i := 0; i < len(suffixes); i += sha1.Size + 8 { r := new(types.Rom) r.Size = util.BytesToInt64(suffixes[i : i+8]) switch len(hash) { case md5.Size: r.Md5 = hash case crc32.Size: r.Crc = hash default: return fmt.Errorf("found unknown hash size: %d", len(hash)) } r.Sha1 = suffixes[i+8 : i+8+sha1.Size] err = rs.lookupRom(cmd, r, outpath) if err != nil { return err } } } } return nil }