Previous 199869 Revisions Next

r17996 Tuesday 18th September, 2012 at 13:38:30 UTC by smf
fix for "file note writeable" when creating, plus consolidated some duplicate/redundant code. [smf]
[src/lib/util]chd.c

trunk/src/lib/util/chd.c
r17995r17996
540540      return CHDERR_ALREADY_OPEN;
541541
542542   // set the header parameters
543   m_version = HEADER_VERSION;
544543   m_logicalbytes = logicalbytes;
545   m_metaoffset = 0;
546544   m_hunkbytes = hunkbytes;
547   m_hunkcount = (m_logicalbytes + m_hunkbytes - 1) / m_hunkbytes;
548545   m_unitbytes = unitbytes;
549546   memcpy(m_compression, compression, sizeof(m_compression));
550547   m_parent = NULL;
r17995r17996
568565      return CHDERR_ALREADY_OPEN;
569566
570567   // set the header parameters
571   m_version = HEADER_VERSION;
572568   m_logicalbytes = logicalbytes;
573   m_metaoffset = 0;
574569   m_hunkbytes = hunkbytes;
575   m_hunkcount = (m_logicalbytes + m_hunkbytes - 1) / m_hunkbytes;
576570   m_unitbytes = parent.unit_bytes();
577571   memcpy(m_compression, compression, sizeof(m_compression));
578572   m_parent = &parent;
r17995r17996
14751469
14761470   // extract parent SHA-1
14771471   UINT32 flags = be_read(&rawheader[16], 4);
1478   if ((flags & 2) && m_allow_writes)
1479      throw CHDERR_FILE_NOT_WRITEABLE;
1472   m_allow_writes = (flags & 2) == 0;
14801473
14811474   // determine compression
14821475   switch (be_read(&rawheader[20], 4))
r17995r17996
15291522
15301523   // extract parent SHA-1
15311524   UINT32 flags = be_read(&rawheader[16], 4);
1532   if ((flags & 2) && m_allow_writes)
1533      throw CHDERR_FILE_NOT_WRITEABLE;
1525   m_allow_writes = (flags & 2) == 0;
15341526
15351527   // determine compression
15361528   switch (be_read(&rawheader[20], 4))
r17995r17996
15891581   m_compression[2] = be_read(&rawheader[24], 4);
15901582   m_compression[3] = be_read(&rawheader[28], 4);
15911583
1592   if (compressed() && m_allow_writes)
1593      throw CHDERR_FILE_NOT_WRITEABLE;
1584   m_allow_writes = !compressed();
15941585
15951586   // describe the format
15961587   m_mapoffset_offset = 40;
r17995r17996
19421933   // wrap in try for proper error handling
19431934   try
19441935   {
1936      m_version = HEADER_VERSION;
1937      m_metaoffset = 0;
1938
19451939      // if we have a parent, it must be V3 or later
19461940      if (m_parent != NULL && m_parent->version() < 3)
19471941         throw CHDERR_UNSUPPORTED_VERSION;
r17995r17996
19521946      if (m_parent != NULL && m_unitbytes != m_parent->unit_bytes())
19531947         throw CHDERR_INVALID_PARAMETER;
19541948
1955      // writes are obviously permitted; reads only if uncompressed
1956      m_allow_writes = true;
1957      m_allow_reads = !compressed();
1958
19591949      // verify the compression types
19601950      bool found_zero = false;
19611951      for (int codecnum = 0; codecnum < ARRAY_LENGTH(m_compression); codecnum++)
r17995r17996
19701960      }
19711961
19721962      // create our V5 header
1973      assert(m_version == HEADER_VERSION);
19741963      UINT8 rawheader[V5_HEADER_SIZE];
19751964      memcpy(&rawheader[0], "MComprHD", 8);
19761965      be_write(&rawheader[8], V5_HEADER_SIZE, 4);
r17995r17996
19951984      sha1_t parentsha1;
19961985      parse_v5_header(rawheader, parentsha1);
19971986
1987      // writes are obviously permitted; reads only if uncompressed
1988      m_allow_writes = true;
1989      m_allow_reads = !compressed();
1990
19981991      // write out the map (if not compressed)
19991992      if (!compressed())
20001993      {
r17995r17996
20392032   // wrap in try for proper error handling
20402033   try
20412034   {
2042      // reads are always permitted; writes possibly as well
2035      // reads are always permitted
20432036      m_allow_reads = true;
2044      m_allow_writes = writeable;
20452037
20462038      // read the raw header
20472039      UINT8 rawheader[MAX_HEADER_SIZE];
r17995r17996
20532045
20542046      // only allow writes to the most recent version
20552047      m_version = be_read(&rawheader[12], 4);
2056      if (m_allow_writes && m_version < HEADER_VERSION)
2048      if (writeable && m_version < HEADER_VERSION)
20572049         throw CHDERR_UNSUPPORTED_VERSION;
20582050
20592051      // read the header if we support it
r17995r17996
20662058         default:   throw CHDERR_UNSUPPORTED_VERSION;
20672059      }
20682060
2061      if (writeable && !m_allow_writes)
2062         throw CHDERR_FILE_NOT_WRITEABLE;
2063
20692064      // make sure we have a parent if we need one (and don't if we don't)
20702065      if (parentsha1 != sha1_t::null)
20712066      {
r17995r17996
20742069         else if (m_parent->sha1() != parentsha1)
20752070            throw CHDERR_INVALID_PARENT;
20762071      }
2077      else if (parentsha1 == sha1_t::null && m_parent != NULL)
2072      else if (m_parent != NULL)
20782073         throw CHDERR_INVALID_PARAMETER;
20792074
20802075      // finish opening the file

Previous 199869 Revisions Next


© 1997-2024 The MAME Team