Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ Version counting is based on semantic versioning (Major.Feature.Patch)

### YACReader

* Support for ComicBookDS (.cbds) comics. Only the pages are shown, not the previews stored next to them.
* Show the full title of the comic in the window title: series, volume, number and title.

### YACReaderLibrary

* Support for ComicBookDS (.cbds) comics.
* Show comic titles the same way in all views. The info panel, the current comic banner and continue reading show the full title: series, volume, number and title.
* Show the series next to the title in the grid cells of reading lists, labels, smart lists and search results, so comics from different series are easy to tell apart.

Expand Down
2 changes: 1 addition & 1 deletion YACReaderLibrary/db/comic_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -815,7 +815,7 @@ QVariant ComicModel::headerData(int section, Qt::Orientation orientation,

if (ext.compare("cbr", Qt::CaseInsensitive) == 0)
return QVariant(QIcon(":/images/comicRar.png"));
else if (ext.compare("cbz", Qt::CaseInsensitive) == 0)
else if (ext.compare("cbz", Qt::CaseInsensitive) == 0 || ext.compare("cbds", Qt::CaseInsensitive) == 0)
return QVariant(QIcon(":/images/comicZip.png"));
else if (Comic::fileIsEpub(fileName))
return QVariant(QIcon(":/images/comicZip.png"));
Expand Down
9 changes: 7 additions & 2 deletions YACReaderLibrary/initial_comic_info_extractor.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "initial_comic_info_extractor.h"

#include "cbds_page_index.h"
#include "comic.h"
#include "compressed_archive.h"
#include "cover_utils.h"
Expand Down Expand Up @@ -132,8 +133,12 @@ void InitialComicInfoExtractor::extract()
_numPages = epub.pageCount;
coverArchiveIndex = epub.coverArchiveIndex;
} else {
QList<QString> fileNames = FileComic::filter(order);
std::sort(fileNames.begin(), fileNames.end(), naturalSortLessThanCI);
QList<QString> fileNames = FileComic::filter(FileComic::cbdsPageCandidates(_fileSource, order));
if (YACReaderCbds::isCbds(_fileSource, order)) {
YACReaderCbds::sortPages(fileNames);
} else {
std::sort(fileNames.begin(), fileNames.end(), naturalSortLessThanCI);
}
_numPages = fileNames.size();
if (_coverPage > _numPages) {
_coverPage = 1;
Expand Down
10 changes: 10 additions & 0 deletions common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ target_include_directories(epub_page_index PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
yacreader_apply_build_options(epub_page_index)
target_link_libraries(epub_page_index PUBLIC Qt6::Core)

# --- ComicBookDS page index (selects the page entries of .cbds books) ---
add_library(cbds_page_index STATIC
cbds_page_index.h
cbds_page_index.cpp
)
target_include_directories(cbds_page_index PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
yacreader_apply_build_options(cbds_page_index)
target_link_libraries(cbds_page_index PUBLIC Qt6::Core naturalsort)

# --- concurrent_queue ---
add_library(concurrent_queue STATIC
concurrent_queue.h
Expand Down Expand Up @@ -121,6 +130,7 @@ target_link_libraries(comic_backend PUBLIC
pdf_backend_iface
cbx_backend
epub_page_index
cbds_page_index
QsLog
)

Expand Down
74 changes: 74 additions & 0 deletions common/cbds_page_index.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include "cbds_page_index.h"

#include "qnaturalsorting.h"

#include <QFileInfo>

#include <algorithm>

namespace {

const QString bookIniName = QStringLiteral("ComicBookDS_book.ini");
const QString pagesFolder = QStringLiteral("IMAGE");

// Leading separators are ignored, the names are relative to the archive root
// anyway. Directory entries (trailing separator) have no parts so they never match.
QStringList archivePathParts(QString name)
{
name.replace('\\', '/');
if (name.endsWith('/')) {
return { };
}
return name.split('/', Qt::SkipEmptyParts);
}

bool isBookIni(const QString &fileName)
{
const QStringList parts = archivePathParts(fileName);
return parts.size() == 1 && parts.first().compare(bookIniName, Qt::CaseInsensitive) == 0;
}

// Only files directly inside the top level IMAGE folder are pages, nested folders
// and entries trying to leave the archive root never match.
bool isPageEntry(const QString &fileName)
{
const QStringList parts = archivePathParts(fileName);
return parts.size() == 2 && parts.first().compare(pagesFolder, Qt::CaseInsensitive) == 0 && parts.last() != QStringLiteral("..") && parts.last() != QStringLiteral(".");
}

}

bool YACReaderCbds::isCbds(const QString &path, const QStringList &fileNames)
{
if (QFileInfo(path).suffix().compare(QStringLiteral("cbds"), Qt::CaseInsensitive) == 0) {
return true;
}

return std::any_of(fileNames.cbegin(), fileNames.cend(), isBookIni);
}

QStringList YACReaderCbds::pageCandidates(const QString &path, const QStringList &fileNames, const ImageFilter &acceptImage)
{
if (!isCbds(path, fileNames)) {
return fileNames;
}

QStringList pages;
for (const QString &fileName : fileNames) {
if (isPageEntry(fileName)) {
pages.append(fileName);
}
}

// A book without images in IMAGE/ does not follow the known layout, falling
// back to every image in the archive is better than refusing to open it.
const bool hasImages = acceptImage ? std::any_of(pages.cbegin(), pages.cend(), acceptImage) : !pages.isEmpty();
return hasImages ? pages : fileNames;
}

void YACReaderCbds::sortPages(QStringList &pages)
{
std::stable_sort(pages.begin(), pages.end(), [](const QString &left, const QString &right) {
return naturalSortLessThanCI(archivePathParts(left).join('/'), archivePathParts(right).join('/'));
});
}
32 changes: 32 additions & 0 deletions common/cbds_page_index.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef CBDS_PAGE_INDEX_H
#define CBDS_PAGE_INDEX_H

#include <QString>
#include <QStringList>

#include <functional>

// ComicBookDS (.cbds) books are zip archives made for a Nintendo DS reader.
// Besides the pages in IMAGE/ they store several resized copies of every page
// (SMALL_N/, SMALL_R/, THMB_N/, THMB_R/) that must not be shown as pages.
// See docs/CBDS_FORMAT.md for the layout.
namespace YACReaderCbds {

using ImageFilter = std::function<bool(const QString &)>;

bool isCbds(const QString &path, const QStringList &fileNames);

// Archive entries that may contain pages: the IMAGE/ entries of a CBDS book, or
// fileNames unchanged for any other archive. A book without any image in IMAGE/
// (as told by acceptImage) falls back to fileNames too. The names are returned
// exactly as listed by the archive so they can be looked up in fileNames afterwards.
QStringList pageCandidates(const QString &path, const QStringList &fileNames, const ImageFilter &acceptImage = { });

// Natural order of the pages of a CBDS book. Page names are plain sequence
// numbers, so there is no spread detection, and the comparison ignores how the
// path is written (separator, case, leading separator) like pageCandidates() does.
void sortPages(QStringList &pages);

}

#endif // CBDS_PAGE_INDEX_H
27 changes: 21 additions & 6 deletions common/comic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "QsLog.h"
#include "bookmarks.h" //TODO desacoplar la dependencia con bookmarks
#include "cbds_page_index.h"
#include "comic_db.h"
#include "compressed_archive.h"
#include "epub_page_index.h"
Expand Down Expand Up @@ -78,7 +79,8 @@ const QStringList ComicArchiveExtensions = QStringList() << "*.cbr"
<< "*.7z"
<< "*.cb7"
<< "*.arj"
<< "*.cbt";
<< "*.cbt"
<< "*.cbds";
const QStringList LiteralComicArchiveExtensions = QStringList() << "cbr"
<< "cbz"
<< "epub"
Expand All @@ -88,7 +90,8 @@ const QStringList LiteralComicArchiveExtensions = QStringList() << "cbr"
<< "7z"
<< "cb7"
<< "arj"
<< "cbt";
<< "cbt"
<< "cbds";
#else
const QStringList ComicArchiveExtensions = QStringList() << "*.cbr"
<< "*.cbz"
Expand All @@ -100,7 +103,8 @@ const QStringList ComicArchiveExtensions = QStringList() << "*.cbr"
<< "*.7z"
<< "*.cb7"
#endif
<< "*.cbt";
<< "*.cbt"
<< "*.cbds";
const QStringList LiteralComicArchiveExtensions = QStringList() << "cbr"
<< "cbz"
<< "epub"
Expand All @@ -111,7 +115,8 @@ const QStringList LiteralComicArchiveExtensions = QStringList() << "cbr"
<< "7z"
<< "cb7"
#endif
<< "cbt";
<< "cbt"
<< "cbds";
#endif // use_unarr
#ifndef NO_PDF
const QStringList Comic::comicExtensions = QStringList() << ComicArchiveExtensions << "*.pdf";
Expand Down Expand Up @@ -479,6 +484,12 @@ YACReaderEpub::ScanInfo FileComic::epubScanInfo(const QStringList &fileNames, Co
return YACReaderEpub::readScanInfo(fileNames, [&archive](int index) { return archive.getRawDataAtIndex(index); }, coverPage, [&supportedExtensions](const QString &fileName) { return isSupportedImage(fileName, supportedExtensions); });
}

QStringList FileComic::cbdsPageCandidates(const QString &path, const QStringList &fileNames)
{
const QStringList supportedExtensions = Comic::getSupportedImageLiteralFormats();
return YACReaderCbds::pageCandidates(path, fileNames, [&supportedExtensions](const QString &fileName) { return isSupportedImage(fileName, supportedExtensions); });
}

// DELEGATE methods
void FileComic::fileExtracted(int index, const QByteArray &rawData)
{
Expand Down Expand Up @@ -645,8 +656,12 @@ void FileComic::process()
_pageArchiveIndexes.append(page.archiveIndex);
}
} else {
pageFileNames = filter(archiveFileNames);
comic_pages_sort(pageFileNames, YACReaderHeuristicSorting);
pageFileNames = filter(cbdsPageCandidates(_path, archiveFileNames));
if (YACReaderCbds::isCbds(_path, archiveFileNames)) {
YACReaderCbds::sortPages(pageFileNames);
} else {
comic_pages_sort(pageFileNames, YACReaderHeuristicSorting);
}
for (const QString &fileName : std::as_const(pageFileNames)) {
_pageArchiveIndexes.append(archiveFileNames.indexOf(fileName));
}
Expand Down
1 change: 1 addition & 0 deletions common/comic.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ class FileComic : public Comic, public ExtractDelegate
static bool isSupportedImage(const QString &fileName, const QStringList &supportedExtensions);
static YACReaderEpub::PageIndex epubPageIndex(const QStringList &fileNames, CompressedArchive &archive);
static YACReaderEpub::ScanInfo epubScanInfo(const QStringList &fileNames, CompressedArchive &archive, int coverPage);
static QStringList cbdsPageCandidates(const QString &path, const QStringList &fileNames);

// ExtractDelegate
void fileExtracted(int index, const QByteArray &rawData);
Expand Down
113 changes: 113 additions & 0 deletions docs/CBDS_FORMAT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# ComicBookDS (`.cbds`) format

ComicBookDS is a homebrew comic reader for the Nintendo DS (last version 3.0, 2010). Its books
use the `.cbds` extension and are made on a PC with PictoDS (a Java program for Windows, Linux
and macOS; `.cbds` output since 2.0 beta 4 in January 2007, last version 2.1 from August 2007;
earlier versions built a complete `.nds` program per book), with `pycbds` or with the open
source `cbdsconv` script. This document describes what YACReader relies on and where that
knowledge comes from.

Sources:

- 25 real `.cbds` books made by PictoDS (all `Version = 200`), inspected entry by entry.
They are not part of the repository.
- The project website <http://cbds.free.fr> (ComicBookDS user guide, PictoDS page and version
history).
- The `cbdsconv` converter (<https://github.com/son-link/cbdsconv>, GPL), which writes the format.
- Descriptions of ComicBookDS and PictoDS on gamebrew.org and fileinfo.com.

## Container

A `.cbds` file is a regular ZIP archive. The real books use Deflate with data descriptors
(general purpose flag bit 3), `cbdsconv` uses Python's `ZIP_DEFLATED`. No encryption, no
ZIP64, no comments. Every decompression backend of YACReader opens it by its `PK\x03\x04`
signature, nothing format specific is needed to read the container.

## Layout

```
ComicBookDS_book.ini
IMAGE/1.jpg ... IMAGE/N.jpg pages
NAME/1.txt ... NAME/N.txt original file name of each page
SMALL_N/1.jpg ... SMALL_N/N.jpg preview, normal orientation
SMALL_R/1.jpg ... SMALL_R/N.jpg preview, rotated 90 degrees
THMB_N/1.jpg ... THMB_N/N.jpg thumbnail, normal orientation
THMB_R/1.jpg ... THMB_R/N.jpg thumbnail, rotated 90 degrees
```

- Pages are numbered from 1 without leading zeros. Entries are stored in lexicographic order
(`1, 10, 11, ..., 2, 20, ...`), so the archive order is **not** the reading order; natural
sorting of the names is.
- Every page exists in five resolutions. In the real books: `IMAGE` 700 px wide (about 900-1030
px high), `SMALL_N` 140x192, `SMALL_R` 256x187, `THMB_N` 33x46, `THMB_R` 62x45. `cbdsconv`
targets 700x1054, 127x192, 30x46, 256x169 and 62x40, rotating the image by -90 degrees for
the `_R` variants.
- The extra sizes match the DS screens (256x192) and the modes of the reader: `SMALL_*` is the
whole-page overview shown on the touch screen, `THMB_*` the thumbnails of the library and
thumbnail modes (10 per screen). `_N` and `_R` are the two orientations the reader switches
between (portrait and landscape, R button).
- Only `IMAGE/` holds pages to display. All other folders are derived data of the reader.
- `NAME/N.txt` holds the source file name of page N (Latin-1 text, CRLF). It is not needed to
order the pages.
- All images are JPEG. No tiling or other quality levels were found.

## `ComicBookDS_book.ini`

Latin-1 text with CRLF line endings, `;` comments and `key = value` lines, no sections:

| Key | Meaning |
|---|---|
| `CbCredits1..3` | Credit lines shown by the reader (title, author, provider), often `Unknown` |
| `LeftToRight` | Default reading direction of the book: `1` left to right (comic), `0` right to left (manga); the reader can switch it |
| `NbPages` | Number of pages |
| `Version` | Compatibility version, `200` in all known books |
| `iWidth`, `iHeight`, `iQuality`, `iSize` | Target size and JPEG quality of `IMAGE` |
| `oWidth`, `oHeight`, `oQuality`, `oSize` | Same for the `SMALL_*` previews |
| `thWidth`, `thHeight`, `thQuality`, `thSize` | Same for the `THMB_*` thumbnails |

In the 21 intact PictoDS books `NbPages` matches the number of `IMAGE/` entries exactly. It is
still not something to rely on in general: `cbdsconv` writes the number of pages plus one and
skips the number of any page it fails to convert, so its books can have gaps in their numbering.

## How YACReader reads it

An archive is handled as a ComicBookDS book when its name ends in `.cbds` or when it has a
`ComicBookDS_book.ini` entry at its root. For such a book only the files directly inside the
top level `IMAGE/` folder are page candidates (see `common/cbds_page_index.cpp`). After that it
goes through the same path as any other comic: unsupported files are filtered out by extension
and the cover is the first page. The reader and the library sort the pages the same way, by the
natural (numeric) order of their names written as `IMAGE/<file>`, whatever separator, case or
leading separator the archive uses. The reader's heuristic for spread files such as `1011.jpg`
is not applied, because ComicBookDS page names are plain sequence numbers.

- The ini file is not read. The page list comes from the archive, which is the only reliable
source, and the optional metadata is not used yet. Broken, huge or missing ini files cannot
affect the result.
- A book without any supported image in `IMAGE/` falls back to the regular behaviour (every
image in the archive is a page).
- The file is never modified or converted. Pages are read from the archive into memory like for
CBZ.
- Only an entry of the form `IMAGE/<file>` is a page. `\` is accepted as a separator and a
leading `/` or `\` is ignored, so names are always relative to the archive root. Nested
folders, `..`, drive letters such as `C:/IMAGE/...` and directory entries never match. The
fallback above returns the archive list unchanged, as for any other comic.
- YACReader does not extract archive entries to disk, so entry names cannot cause path traversal.
- Detecting a book renamed to another extension needs the ini entry in the archive listing. The
unarr backend leaves empty entries out of its listing, so a renamed book with an empty ini is
read as a regular archive there. Files with the `.cbds` extension are not affected.

## Damaged books

Some books in the wild are damaged: the file keeps its size, but from some point (in the books
seen, a 64 KiB boundary) to the end the data is zeros, including the ZIP central directory. Such
a file is not a valid ZIP archive and opens with an error, or only partially, depending on how
much the decompression backend can recover from the local headers. This is a data problem of the
individual file, not a format variant.

## Unknown

- Books from older ComicBookDS versions (before the `.cbds` extension and before `Version = 200`)
have not been seen.
- Whether other converters wrote different folder names, PNG pages or tiled pages is unknown.
- The use of `LeftToRight = 0` (all known books are left to right) has not been verified with a
real book; mapping it to YACReader's manga mode is a possible follow-up.
2 changes: 2 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ add_subdirectory(pdf_render_size_test)
add_subdirectory(folder_rename_test)
add_subdirectory(library_item_actions_test)
add_subdirectory(epub_page_index_test)
add_subdirectory(cbds_page_index_test)
add_subdirectory(cbds_comic_test)
add_subdirectory(comic_files_manager_test)
add_subdirectory(organize_files_test)
add_subdirectory(yacreader_libraries_test)
Expand Down
Loading