Mapping C semantics to Swift


(Ken Burgett) #1

I would like to know if a struct in Swift has any guarantee of contiguity, like a struct in C. I am attempting to port a C program that makes many assumptions about the underlying semantics of C structs, and does things like overlaying an array over the struct using a cast, and then performs and cryptographic hashing of the entire struct by treating it as an array.

1. I a Swift struct contiguous?

2. If 1 above is true, can I somehow cast an array over the struct?

···

--
Ken Burgett
Principal Software Engineer
Email: kenb@iotone.io
Office: 530.693.4449
Mobile: 831.332.6846
URL: www.iotone.co


(Joe Groff) #2

Swift structs have unspecified layout. If you depend on a specific layout, you should define the struct in C and import it into Swift for now.

-Joe

···

On May 19, 2016, at 10:00 AM, Ken Burgett via swift-users <swift-users@swift.org> wrote:

I would like to know if a struct in Swift has any guarantee of contiguity, like a struct in C. I am attempting to port a C program that makes many assumptions about the underlying semantics of C structs, and does things like overlaying an array over the struct using a cast, and then performs and cryptographic hashing of the entire struct by treating it as an array.

1. I a Swift struct contiguous?

2. If 1 above is true, can I somehow cast an array over the struct?


(Ken Burgett) #3

I have been told that the in-memory layout of a Swift struct is 'undefined', so the C style of aliasing an array over a struct and iterating across the struct is a definite no-no. Not surprising, since aliasing violates all type-safe rules.

So, if you have this situation, you must address it on a case-by-case basis. In my case, converting a hash algorithm from C to Swift, the required C struct morphs into a Swift class, with a member function 'as_C_byte_array' which answers an array of the required form. Internally, this function will shift and mask class attributes in order to build the necessary result.
Ugly, but I must preserve the semantics exactly if I hope to generate the exact same hash as the C version.

···

--
Ken Burgett
Principal Software Engineer
Email: kenb@iotone.io
Office: 530.693.4449
Mobile: 831.332.6846
URL: www.iotone.co


(Ramakrishna Mallireddy) #4

An working example,

let fm = NSFileManager.defaultManager()

  var mh = mach_header() // Defined as C struct, <mach-o/loader.h>

  var lc = load_command() // Defined as C struct <mach-o/loader.h>

  var location = 0

  if let data = fm.contentsAtPath("home/user/projects/some.o") {

    data.getBytes(&mh, length: sizeof(mach_header))

    location += sizeof(mach_header)

    data.getBytes(&lc, range: NSRange(location: location, length: sizeof(
load_command)) )

    location += sizeof(load_command)

}

-Rk

···

On Thu, May 19, 2016 at 10:52 PM, Joe Groff via swift-users < swift-users@swift.org> wrote:

> On May 19, 2016, at 10:00 AM, Ken Burgett via swift-users < > swift-users@swift.org> wrote:
>
> I would like to know if a struct in Swift has any guarantee of
contiguity, like a struct in C. I am attempting to port a C program that
makes many assumptions about the underlying semantics of C structs, and
does things like overlaying an array over the struct using a cast, and then
performs and cryptographic hashing of the entire struct by treating it as
an array.
>
> 1. I a Swift struct contiguous?
>
> 2. If 1 above is true, can I somehow cast an array over the struct?

Swift structs have unspecified layout. If you depend on a specific layout,
you should define the struct in C and import it into Swift for now.

-Joe
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Jens Alfke) #5

It would be nice to see Swift acquire structured-data I/O APIs as found in languages like Ruby and Python — these take a printf-like format string and parameters, but read and write values in binary. The format syntax offers modifiers to specify things like byte order, string encoding, etc. This is so much more reliable and readable than doing the encoding by hand.

—Jens

···

On May 20, 2016, at 7:33 AM, Ken Burgett via swift-users <swift-users@swift.org> wrote:

the required C struct morphs into a Swift class, with a member function 'as_C_byte_array' which answers an array of the required form. Internally, this function will shift and mask class attributes in order to build the necessary result.


(Joe Groff) #6

I have been told that the in-memory layout of a Swift struct is 'undefined', so the C style of aliasing an array over a struct and iterating across the struct is a definite no-no. Not surprising, since aliasing violates all type-safe rules.

So, if you have this situation, you must address it on a case-by-case basis. In my case, converting a hash algorithm from C to Swift, the required C struct morphs into a Swift class, with a member function 'as_C_byte_array' which answers an array of the required form. Internally, this function will shift and mask class attributes in order to build the necessary result.

That's not necessary. You can leave the struct defined in C and import it into Swift. Swift will respect C's layout.

-Joe

···

On May 20, 2016, at 7:33 AM, Ken Burgett via swift-users <swift-users@swift.org> wrote:

Ugly, but I must preserve the semantics exactly if I hope to generate the exact same hash as the C version.

--
Ken Burgett
Principal Software Engineer
Email: kenb@iotone.io
Office: 530.693.4449
Mobile: 831.332.6846
URL: www.iotone.co
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Ken Burgett) #7

Hi Jens,

I understand the value of strong typing from a runtime point of view, but I really miss the quickness and simplicity of writing a class in Ruby, where duck-typing is king.

···

On 2016-05-20 08:55, Jens Alfke wrote:

On May 20, 2016, at 7:33 AM, Ken Burgett via swift-users >> <swift-users@swift.org> wrote:

the required C struct morphs into a Swift class, with a member
function 'as_C_byte_array' which answers an array of the required
form. Internally, this function will shift and mask class attributes
in order to build the necessary result.

It would be nice to see Swift acquire structured-data I/O APIs as
found in languages like Ruby and Python — these take a printf-like
format string and parameters, but read and write values in binary. The
format syntax offers modifiers to specify things like byte order,
string encoding, etc. This is so much more reliable and readable than
doing the encoding by hand.

—Jens

--
Ken Burgett
Principal Software Engineer
Email: kenb@iotone.io
Office: 530.693.4449
Mobile: 831.332.6846
URL: www.iotone.co


(Matthew Johnson) #8

I have been told that the in-memory layout of a Swift struct is 'undefined', so the C style of aliasing an array over a struct and iterating across the struct is a definite no-no. Not surprising, since aliasing violates all type-safe rules.

So, if you have this situation, you must address it on a case-by-case basis. In my case, converting a hash algorithm from C to Swift, the required C struct morphs into a Swift class, with a member function 'as_C_byte_array' which answers an array of the required form. Internally, this function will shift and mask class attributes in order to build the necessary result.

That's not necessary. You can leave the struct defined in C and import it into Swift. Swift will respect C's layout.

Any thought on introducing an attribute or something into the “generated header” for these C imports to make this more clear? The same syntax could be used in the future if / when Swift supports more control over layout.

···

On May 20, 2016, at 12:54 PM, Joe Groff via swift-users <swift-users@swift.org> wrote:

On May 20, 2016, at 7:33 AM, Ken Burgett via swift-users <swift-users@swift.org> wrote:

-Joe

Ugly, but I must preserve the semantics exactly if I hope to generate the exact same hash as the C version.

--
Ken Burgett
Principal Software Engineer
Email: kenb@iotone.io
Office: 530.693.4449
Mobile: 831.332.6846
URL: www.iotone.co
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users