Move the copies of unpad, crc32, checksumPid and pidFromSerial that were scattered across the plugin into utilities.py, and add a shared safe_join that generalizes the Topaz extraction path-traversal fix. - unpad: adobekey, ineptepub and ineptpdf import the shared helper. The four bare-script key tools keep their local copies since they run without a package context. - checksumPid is type-preserving (bytes for kgenpids/kindlepid, str for mobidedrm) so every call site keeps its exact behavior. - kgenpids falls back to an absolute import because it is imported top-level by the worker modules. - topazextract.extractFiles uses safe_join; genbook is unchanged as it only handles already-sanitized names. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
53 lines
1.8 KiB
Python
53 lines
1.8 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Mobipocket PID calculator v0.4 for Amazon Kindle.
|
|
# Copyright (c) 2007, 2009 Igor Skochinsky <skochinsky@mail.ru>
|
|
# History:
|
|
# 0.1 Initial release
|
|
# 0.2 Added support for generating PID for iPhone (thanks to mbp)
|
|
# 0.3 changed to autoflush stdout, fixed return code usage
|
|
# 0.3 updated for unicode
|
|
# 0.4 Added support for serial numbers starting with '9', fixed unicode bugs.
|
|
# 0.5 moved unicode_argv call inside main for Windows DeDRM compatibility
|
|
# 1.0 Python 3 for calibre 5.0
|
|
|
|
|
|
import sys
|
|
|
|
#@@CALIBRE_COMPAT_CODE@@
|
|
|
|
from .utilities import SafeUnbuffered, checksumPid, pidFromSerial
|
|
from .argv_utils import unicode_argv
|
|
|
|
def cli_main():
|
|
print("Mobipocket PID calculator for Amazon Kindle. Copyright © 2007, 2009 Igor Skochinsky")
|
|
argv=unicode_argv("kindlepid.py")
|
|
if len(argv)==2:
|
|
serial = argv[1]
|
|
else:
|
|
print("Usage: kindlepid.py <Kindle Serial Number>/<iPhone/iPod Touch UDID>")
|
|
return 1
|
|
if len(serial)==16:
|
|
if serial.startswith("B") or serial.startswith("9"):
|
|
print("Kindle serial number detected")
|
|
else:
|
|
print("Warning: unrecognized serial number. Please recheck input.")
|
|
return 1
|
|
pid = pidFromSerial(serial.encode("utf-8"),7)+'*'
|
|
print("Mobipocket PID for Kindle serial#{0} is {1}".format(serial,checksumPid(pid)))
|
|
return 0
|
|
elif len(serial)==40:
|
|
print("iPhone serial number (UDID) detected")
|
|
pid = pidFromSerial(serial.encode("utf-8"),8)
|
|
print("Mobipocket PID for iPhone serial#{0} is {1}".format(serial,checksumPid(pid)))
|
|
return 0
|
|
print("Warning: unrecognized serial number. Please recheck input.")
|
|
return 1
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.stdout=SafeUnbuffered(sys.stdout)
|
|
sys.stderr=SafeUnbuffered(sys.stderr)
|
|
sys.exit(cli_main())
|