kindle-drm 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/.project ADDED
@@ -0,0 +1,17 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <projectDescription>
3
+ <name>kindle-drm</name>
4
+ <comment></comment>
5
+ <projects>
6
+ </projects>
7
+ <buildSpec>
8
+ <buildCommand>
9
+ <name>org.rubypeople.rdt.core.rubybuilder</name>
10
+ <arguments>
11
+ </arguments>
12
+ </buildCommand>
13
+ </buildSpec>
14
+ <natures>
15
+ <nature>org.rubypeople.rdt.core.rubynature</nature>
16
+ </natures>
17
+ </projectDescription>
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Preston Lee
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,26 @@
1
+ = kindle-drm
2
+
3
+ This code can calculate the PID (Personal ID) number of a Kindle eBook reader device based on it's serial number. To determine the serial number of your Kindle:
4
+
5
+ 1. Navigate to the device's "Settitngs" screen.
6
+ 1. Type "411" (without the quotes) on the keyboard.
7
+ 1. A secret page should now open. Write down the 16-character serial number of the device. (iPhone serial numbers are different but should also work.)
8
+
9
+
10
+ == Note on Patches/Pull Requests
11
+
12
+ * Fork the project.
13
+ * Make your feature addition or bug fix.
14
+ * Add tests for it. This is important so I don't break it in a
15
+ future version unintentionally.
16
+ * Commit, do not mess with rakefile, version, or history.
17
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
18
+ * Send me a pull request. Bonus points for topic branches.
19
+
20
+ = History
21
+
22
+ This code was originally ported Mobipocket PID calculator v0.2 by Igor Skochinsky. I have added support for Kindle 2 and Kindle DX (serial numbers starting with B003 and B004) in addition to the port.
23
+
24
+ == Copyright
25
+
26
+ Copyright (c) 2010 Preston Lee. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "kindle-drm"
8
+ gem.summary = %Q{Amazon Kindle .azw file manipulation library.}
9
+ gem.description = %Q{A Ruby library for performing DRM-related operations on .mobi files, know as DRM'd .azw files on the Amazon Kindle.}
10
+ gem.email = "conmotto@gmail.com"
11
+ gem.homepage = "http://github.com/preston/kindle-drm"
12
+ gem.authors = ["Preston Lee", "Igor Skochinsky"]
13
+ gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
20
+
21
+ require 'rake/testtask'
22
+ Rake::TestTask.new(:test) do |test|
23
+ test.libs << 'lib' << 'test'
24
+ test.pattern = 'test/**/test_*.rb'
25
+ test.verbose = true
26
+ end
27
+
28
+ begin
29
+ require 'rcov/rcovtask'
30
+ Rcov::RcovTask.new do |test|
31
+ test.libs << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+ rescue LoadError
36
+ task :rcov do
37
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
38
+ end
39
+ end
40
+
41
+ task :test => :check_dependencies
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "kindle-drm #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,36 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ current_dir = File.dirname(File.expand_path(__FILE__))
4
+ lib_path = File.join(current_dir, '..', 'lib')
5
+ $LOAD_PATH.unshift lib_path
6
+
7
+
8
+ require 'zlib'
9
+ require 'kindle-drm'
10
+
11
+
12
+ include Kindle::DRM
13
+
14
+
15
+ code = 0
16
+ puts "\n"
17
+ puts "Mobipocket PID calculator for Amazon Kindle. Refactored, ported and packaged for Ruby by Preston Lee. Original Python code by Igor Skochinsky."
18
+ if ARGV.length == 1
19
+ serial = ARGV[0]
20
+ length = serial.length
21
+ type, size, pad = serialToDeviceTypeAndPidSize(serial)
22
+ if type.nil?
23
+ puts "Warning: unrecognized serial number. Please recheck input."
24
+ code = 1
25
+ else
26
+ puts "\n"
27
+ puts "Device Type:\t#{type}"
28
+ puts "PID:\t\t" + serialToPid(serial)
29
+ end
30
+ else
31
+ puts "Usage: #{__FILE__} <Kindle Serial Number>/<iPhone/iPod Touch UDID>"
32
+ end
33
+ puts "\n"
34
+
35
+ exit(code)
36
+
@@ -0,0 +1,59 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{kindle-drm}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Preston Lee", "Igor Skochinsky"]
12
+ s.date = %q{2010-02-20}
13
+ s.default_executable = %q{serial_to_pid.rb}
14
+ s.description = %q{A Ruby library for performing DRM-related operations on .mobi files, know as DRM'd .azw files on the Amazon Kindle.}
15
+ s.email = %q{conmotto@gmail.com}
16
+ s.executables = ["serial_to_pid.rb"]
17
+ s.extra_rdoc_files = [
18
+ "LICENSE",
19
+ "README.rdoc"
20
+ ]
21
+ s.files = [
22
+ ".document",
23
+ ".gitignore",
24
+ ".project",
25
+ "LICENSE",
26
+ "README.rdoc",
27
+ "Rakefile",
28
+ "VERSION",
29
+ "bin/serial_to_pid.rb",
30
+ "kindle-drm.gemspec",
31
+ "lib/kindle-drm.rb",
32
+ "lib/kindle/drm.rb",
33
+ "test/helper.rb",
34
+ "test/test_kindle-drm.rb"
35
+ ]
36
+ s.homepage = %q{http://github.com/preston/kindle-drm}
37
+ s.rdoc_options = ["--charset=UTF-8"]
38
+ s.require_paths = ["lib"]
39
+ s.rubygems_version = %q{1.3.6}
40
+ s.summary = %q{Amazon Kindle .azw file manipulation library.}
41
+ s.test_files = [
42
+ "test/helper.rb",
43
+ "test/test_kindle-drm.rb"
44
+ ]
45
+
46
+ if s.respond_to? :specification_version then
47
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
48
+ s.specification_version = 3
49
+
50
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
51
+ s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
52
+ else
53
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
54
+ end
55
+ else
56
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
57
+ end
58
+ end
59
+
data/lib/kindle-drm.rb ADDED
@@ -0,0 +1 @@
1
+ require 'kindle/drm'
data/lib/kindle/drm.rb ADDED
@@ -0,0 +1,102 @@
1
+ require 'zlib'
2
+
3
+ module Kindle
4
+
5
+ module DRM
6
+
7
+ LETTERS = "ABCDEFGHIJKLMNPQRSTUVWXYZ123456789".split(//)
8
+
9
+ # Special cyclical redundancy check (checksumming) function.
10
+ def crc32(s)
11
+ (~Zlib.crc32(s, -1)) & 0xFFFFFFFF
12
+ end
13
+
14
+ # Generates the actual Personal ID (PID) needed to tie/untie content
15
+ # (such as .mobi/.azw files) to a specific device.
16
+ def checksumPid(s)
17
+ tmp = crc32(s)
18
+ crc = tmp ^ (tmp >> 16)
19
+ res = s
20
+ l = LETTERS.length
21
+ for i in 0..1 do
22
+ b = crc & 0xFF
23
+ t = b.divmod(l)[0]
24
+ pos = t ^ (b % l)
25
+ res = "#{res}#{LETTERS[pos % l]}"
26
+ crc >>= 8
27
+ end
28
+ res
29
+ end
30
+
31
+ # Figures out the intermediary Personal ID (PID) of a device based on its
32
+ # serial number and expected length of the output. Apparently this
33
+ # varies by the type of device.
34
+ def serialToIntermediaryPid(s, l)
35
+ crc = crc32(s)
36
+ arr1 = Array.new(l, 0)
37
+ for i in 0..(s.length - 1) do
38
+ code = s[i]
39
+ code = code.ord if code.class == String # Fix for Ruby 1.9+
40
+ arr1[i%l] ^= code
41
+ end
42
+
43
+ # Grab each CRC byte and OR with a portion of the
44
+ crc_bytes = [crc >> 24 & 0xff, crc >> 16 & 0xff, crc >> 8 & 0xff, crc & 0xff]
45
+ for i in 0..(l - 1) do
46
+ arr1[i] ^= crc_bytes[i&3]
47
+ end
48
+
49
+ pid = ""
50
+ for i in 0..(l-1) do
51
+ b = arr1[i] & 0xff
52
+ pid += LETTERS[(b >> 7) + ((b >> 5 & 3) ^ (b & 0x1f))]
53
+ end
54
+ pid
55
+ end
56
+
57
+
58
+ def serialToDeviceTypeAndPidSize(serial)
59
+ type = nil
60
+ size = nil
61
+ pad = nil
62
+
63
+ l = serial.length
64
+ if l == 16
65
+ case serial[0, 4]
66
+ when "B001"
67
+ type = "Kindle 1"
68
+ when "B002"
69
+ type = "Kindle 2"
70
+ when "B003"
71
+ type = "Kindle 2 International"
72
+ when "B004"
73
+ type = "Kindle DX"
74
+ end
75
+ size = 7
76
+ pad = '*'
77
+ elsif l == 40
78
+ type = "iPhone"
79
+ size = 8
80
+ end
81
+
82
+ return nil if type.nil? || size.nil?
83
+ return [type, size, pad]
84
+ end
85
+
86
+
87
+ def serialToPid(serial)
88
+ pid = nil
89
+ type, size, pad = serialToDeviceTypeAndPidSize(serial)
90
+ if type.nil?
91
+ # No dice.. :(
92
+ else
93
+ ipid = serialToIntermediaryPid(serial, size)
94
+ ipid += pad unless pad.nil?
95
+ pid = checksumPid(ipid)
96
+ end
97
+ pid
98
+ end
99
+
100
+ end
101
+
102
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,12 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'kindle-drm'
8
+
9
+ include Kindle::DRM
10
+
11
+ class Test::Unit::TestCase
12
+ end
@@ -0,0 +1,14 @@
1
+ require 'helper'
2
+
3
+ class TestKindleDrm < Test::Unit::TestCase
4
+
5
+ should "generate Personal ID (PID) strings correctly" do
6
+ # Valid test output generated by original Python code.
7
+ assert_equal crc32('aoeuaoeuaoeuaoeu'), 593220825
8
+ assert_equal serialToIntermediaryPid('aoeuaoeuaoeuaoeu', 7), "RB5MXY5"
9
+ assert_equal checksumPid('aoeuaoeuaoeuaoeu'), "aoeuaoeuaoeuaoeu7P"
10
+ assert_equal checksumPid('RB5MXY5'), "RB5MXY5LR"
11
+ assert_equal serialToPid('B001DEADBABECAFE'), 'RB6IUSR*MU'
12
+ end
13
+
14
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kindle-drm
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Preston Lee
13
+ - Igor Skochinsky
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-02-20 00:00:00 -07:00
19
+ default_executable: serial_to_pid.rb
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: thoughtbot-shoulda
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ type: :development
32
+ version_requirements: *id001
33
+ description: A Ruby library for performing DRM-related operations on .mobi files, know as DRM'd .azw files on the Amazon Kindle.
34
+ email: conmotto@gmail.com
35
+ executables:
36
+ - serial_to_pid.rb
37
+ extensions: []
38
+
39
+ extra_rdoc_files:
40
+ - LICENSE
41
+ - README.rdoc
42
+ files:
43
+ - .document
44
+ - .gitignore
45
+ - .project
46
+ - LICENSE
47
+ - README.rdoc
48
+ - Rakefile
49
+ - VERSION
50
+ - bin/serial_to_pid.rb
51
+ - kindle-drm.gemspec
52
+ - lib/kindle-drm.rb
53
+ - lib/kindle/drm.rb
54
+ - test/helper.rb
55
+ - test/test_kindle-drm.rb
56
+ has_rdoc: true
57
+ homepage: http://github.com/preston/kindle-drm
58
+ licenses: []
59
+
60
+ post_install_message:
61
+ rdoc_options:
62
+ - --charset=UTF-8
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ segments:
70
+ - 0
71
+ version: "0"
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ segments:
77
+ - 0
78
+ version: "0"
79
+ requirements: []
80
+
81
+ rubyforge_project:
82
+ rubygems_version: 1.3.6
83
+ signing_key:
84
+ specification_version: 3
85
+ summary: Amazon Kindle .azw file manipulation library.
86
+ test_files:
87
+ - test/helper.rb
88
+ - test/test_kindle-drm.rb