Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitae61397

Browse files
committed
Movedget_word_alignment() from_compat.py tomachine_size.py
In preparation of removal of Python 2.7 support, I only want to havecompatibility code for Python 2.7 in `_compat.py`, and not other kindsof 'compatibility'.
1 parentad51018 commitae61397

File tree

2 files changed

+77
-3
lines changed

2 files changed

+77
-3
lines changed

‎rsa/machine_size.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Copyright 2011 Sybren A. Stüvel <sybren@stuvel.eu>
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# https://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
"""Detection of 32-bit and 64-bit machines and byte alignment."""
18+
19+
importsys
20+
21+
MAX_INT=sys.maxsize
22+
MAX_INT64= (1<<63)-1
23+
MAX_INT32= (1<<31)-1
24+
MAX_INT16= (1<<15)-1
25+
26+
# Determine the word size of the processor.
27+
ifMAX_INT==MAX_INT64:
28+
# 64-bit processor.
29+
MACHINE_WORD_SIZE=64
30+
elifMAX_INT==MAX_INT32:
31+
# 32-bit processor.
32+
MACHINE_WORD_SIZE=32
33+
else:
34+
# Else we just assume 64-bit processor keeping up with modern times.
35+
MACHINE_WORD_SIZE=64
36+
37+
38+
defget_word_alignment(num,force_arch=64,
39+
_machine_word_size=MACHINE_WORD_SIZE):
40+
"""
41+
Returns alignment details for the given number based on the platform
42+
Python is running on.
43+
44+
:param num:
45+
Unsigned integral number.
46+
:param force_arch:
47+
If you don't want to use 64-bit unsigned chunks, set this to
48+
anything other than 64. 32-bit chunks will be preferred then.
49+
Default 64 will be used when on a 64-bit machine.
50+
:param _machine_word_size:
51+
(Internal) The machine word size used for alignment.
52+
:returns:
53+
4-tuple::
54+
55+
(word_bits, word_bytes,
56+
max_uint, packing_format_type)
57+
"""
58+
max_uint64=0xffffffffffffffff
59+
max_uint32=0xffffffff
60+
max_uint16=0xffff
61+
max_uint8=0xff
62+
63+
ifforce_arch==64and_machine_word_size>=64andnum>max_uint32:
64+
# 64-bit unsigned integer.
65+
return64,8,max_uint64,"Q"
66+
elifnum>max_uint16:
67+
# 32-bit unsigned integer
68+
return32,4,max_uint32,"L"
69+
elifnum>max_uint8:
70+
# 16-bit unsigned integer.
71+
return16,2,max_uint16,"H"
72+
else:
73+
# 8-bit unsigned integer.
74+
return8,1,max_uint8,"B"

‎rsa/transform.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
importbinascii
2525
fromstructimportpack
2626

27-
fromrsaimportcommon
28-
fromrsa._compatimportbyte,is_integer,get_word_alignment
27+
fromrsa._compatimportbyte,is_integer
28+
fromrsaimportcommon,machine_size
2929

3030

3131
defbytes2int(raw_bytes):
@@ -181,7 +181,7 @@ def int2bytes(number, fill_size=None, chunk_size=None, overflow=False):
181181

182182
# Pack the integer one machine word at a time into bytes.
183183
num=number
184-
word_bits,_,max_uint,pack_type=get_word_alignment(num)
184+
word_bits,_,max_uint,pack_type=machine_size.get_word_alignment(num)
185185
pack_format=">%s"%pack_type
186186
whilenum>0:
187187
raw_bytes=pack(pack_format,num&max_uint)+raw_bytes

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp